blob: 475246b355f090a082d23076596b7fc74f7b9fe7 [file] [log] [blame]
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001/*
2 * net/core/devlink.c - Network physical/parent device Netlink interface
3 *
4 * Heavily inspired by net/wireless/
5 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/gfp.h>
19#include <linux/device.h>
20#include <linux/list.h>
21#include <linux/netdevice.h>
22#include <rdma/ib_verbs.h>
23#include <net/netlink.h>
24#include <net/genetlink.h>
25#include <net/rtnetlink.h>
26#include <net/net_namespace.h>
27#include <net/sock.h>
28#include <net/devlink.h>
Jiri Pirkoe5224f02016-07-12 18:05:03 +020029#define CREATE_TRACE_POINTS
30#include <trace/events/devlink.h>
31
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020032static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
33 {
David Ahern12bdc5e2017-08-30 17:07:30 -070034 .name = "destination mac",
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020035 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
36 .bitwidth = 48,
37 },
38};
39
40struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
41 .name = "ethernet",
42 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
43 .fields = devlink_dpipe_fields_ethernet,
44 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
45 .global = true,
46};
47EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
48
Arkadi Sharshevsky3fb886e2017-08-24 08:40:00 +020049static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
50 {
51 .name = "destination ip",
52 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
53 .bitwidth = 32,
54 },
55};
56
57struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
58 .name = "ipv4",
59 .id = DEVLINK_DPIPE_HEADER_IPV4,
60 .fields = devlink_dpipe_fields_ipv4,
61 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
62 .global = true,
63};
64EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
65
Arkadi Sharshevsky1797f5b2017-08-31 17:59:12 +020066static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
67 {
68 .name = "destination ip",
69 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
70 .bitwidth = 128,
71 },
72};
73
74struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
75 .name = "ipv6",
76 .id = DEVLINK_DPIPE_HEADER_IPV6,
77 .fields = devlink_dpipe_fields_ipv6,
78 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
79 .global = true,
80};
81EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
82
Jiri Pirkoe5224f02016-07-12 18:05:03 +020083EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010084
85static LIST_HEAD(devlink_list);
86
87/* devlink_mutex
88 *
89 * An overall lock guarding every operation coming from userspace.
90 * It also guards devlink devices list and it is taken when
91 * driver registers/unregisters it.
92 */
93static DEFINE_MUTEX(devlink_mutex);
94
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010095static struct net *devlink_net(const struct devlink *devlink)
96{
97 return read_pnet(&devlink->_net);
98}
99
100static void devlink_net_set(struct devlink *devlink, struct net *net)
101{
102 write_pnet(&devlink->_net, net);
103}
104
105static struct devlink *devlink_get_from_attrs(struct net *net,
106 struct nlattr **attrs)
107{
108 struct devlink *devlink;
109 char *busname;
110 char *devname;
111
112 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
113 return ERR_PTR(-EINVAL);
114
115 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
116 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
117
118 list_for_each_entry(devlink, &devlink_list, list) {
119 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
120 strcmp(dev_name(devlink->dev), devname) == 0 &&
121 net_eq(devlink_net(devlink), net))
122 return devlink;
123 }
124
125 return ERR_PTR(-ENODEV);
126}
127
128static struct devlink *devlink_get_from_info(struct genl_info *info)
129{
130 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
131}
132
133static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
134 int port_index)
135{
136 struct devlink_port *devlink_port;
137
138 list_for_each_entry(devlink_port, &devlink->port_list, list) {
139 if (devlink_port->index == port_index)
140 return devlink_port;
141 }
142 return NULL;
143}
144
145static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
146{
147 return devlink_port_get_by_index(devlink, port_index);
148}
149
150static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
151 struct nlattr **attrs)
152{
153 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
154 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
155 struct devlink_port *devlink_port;
156
157 devlink_port = devlink_port_get_by_index(devlink, port_index);
158 if (!devlink_port)
159 return ERR_PTR(-ENODEV);
160 return devlink_port;
161 }
162 return ERR_PTR(-EINVAL);
163}
164
165static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
166 struct genl_info *info)
167{
168 return devlink_port_get_from_attrs(devlink, info->attrs);
169}
170
Jiri Pirkobf797472016-04-14 18:19:13 +0200171struct devlink_sb {
172 struct list_head list;
173 unsigned int index;
174 u32 size;
175 u16 ingress_pools_count;
176 u16 egress_pools_count;
177 u16 ingress_tc_count;
178 u16 egress_tc_count;
179};
180
181static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
182{
183 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
184}
185
186static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
187 unsigned int sb_index)
188{
189 struct devlink_sb *devlink_sb;
190
191 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
192 if (devlink_sb->index == sb_index)
193 return devlink_sb;
194 }
195 return NULL;
196}
197
198static bool devlink_sb_index_exists(struct devlink *devlink,
199 unsigned int sb_index)
200{
201 return devlink_sb_get_by_index(devlink, sb_index);
202}
203
204static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
205 struct nlattr **attrs)
206{
207 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
208 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
209 struct devlink_sb *devlink_sb;
210
211 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
212 if (!devlink_sb)
213 return ERR_PTR(-ENODEV);
214 return devlink_sb;
215 }
216 return ERR_PTR(-EINVAL);
217}
218
219static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
220 struct genl_info *info)
221{
222 return devlink_sb_get_from_attrs(devlink, info->attrs);
223}
224
225static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
226 struct nlattr **attrs,
227 u16 *p_pool_index)
228{
229 u16 val;
230
231 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
232 return -EINVAL;
233
234 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
235 if (val >= devlink_sb_pool_count(devlink_sb))
236 return -EINVAL;
237 *p_pool_index = val;
238 return 0;
239}
240
241static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
242 struct genl_info *info,
243 u16 *p_pool_index)
244{
245 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
246 p_pool_index);
247}
248
249static int
250devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
251 enum devlink_sb_pool_type *p_pool_type)
252{
253 u8 val;
254
255 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
256 return -EINVAL;
257
258 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
259 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
260 val != DEVLINK_SB_POOL_TYPE_EGRESS)
261 return -EINVAL;
262 *p_pool_type = val;
263 return 0;
264}
265
266static int
267devlink_sb_pool_type_get_from_info(struct genl_info *info,
268 enum devlink_sb_pool_type *p_pool_type)
269{
270 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
271}
272
273static int
274devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
275 enum devlink_sb_threshold_type *p_th_type)
276{
277 u8 val;
278
279 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
280 return -EINVAL;
281
282 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
283 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
284 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
285 return -EINVAL;
286 *p_th_type = val;
287 return 0;
288}
289
290static int
291devlink_sb_th_type_get_from_info(struct genl_info *info,
292 enum devlink_sb_threshold_type *p_th_type)
293{
294 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
295}
296
297static int
298devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
299 struct nlattr **attrs,
300 enum devlink_sb_pool_type pool_type,
301 u16 *p_tc_index)
302{
303 u16 val;
304
305 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
306 return -EINVAL;
307
308 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
309 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
310 val >= devlink_sb->ingress_tc_count)
311 return -EINVAL;
312 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
313 val >= devlink_sb->egress_tc_count)
314 return -EINVAL;
315 *p_tc_index = val;
316 return 0;
317}
318
319static int
320devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
321 struct genl_info *info,
322 enum devlink_sb_pool_type pool_type,
323 u16 *p_tc_index)
324{
325 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
326 pool_type, p_tc_index);
327}
328
Jiri Pirko1fc22572016-04-08 19:12:48 +0200329#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
330#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200331#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100332
333/* The per devlink instance lock is taken by default in the pre-doit
334 * operation, yet several commands do not require this. The global
335 * devlink lock is taken and protects from disruption by user-calls.
336 */
337#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100338
339static int devlink_nl_pre_doit(const struct genl_ops *ops,
340 struct sk_buff *skb, struct genl_info *info)
341{
342 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100343 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100344
345 mutex_lock(&devlink_mutex);
346 devlink = devlink_get_from_info(info);
347 if (IS_ERR(devlink)) {
348 mutex_unlock(&devlink_mutex);
349 return PTR_ERR(devlink);
350 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100351 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
352 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200353 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
354 info->user_ptr[0] = devlink;
355 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100356 struct devlink_port *devlink_port;
357
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100358 devlink_port = devlink_port_get_from_info(devlink, info);
359 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100360 err = PTR_ERR(devlink_port);
361 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100362 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200363 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100364 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200365 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
366 struct devlink_sb *devlink_sb;
367
368 devlink_sb = devlink_sb_get_from_info(devlink, info);
369 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100370 err = PTR_ERR(devlink_sb);
371 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200372 }
373 info->user_ptr[1] = devlink_sb;
374 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100375 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100376
377unlock:
378 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
379 mutex_unlock(&devlink->lock);
380 mutex_unlock(&devlink_mutex);
381 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100382}
383
384static void devlink_nl_post_doit(const struct genl_ops *ops,
385 struct sk_buff *skb, struct genl_info *info)
386{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100387 struct devlink *devlink;
388
389 devlink = devlink_get_from_info(info);
390 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
391 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100392 mutex_unlock(&devlink_mutex);
393}
394
Johannes Berg489111e2016-10-24 14:40:03 +0200395static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100396
397enum devlink_multicast_groups {
398 DEVLINK_MCGRP_CONFIG,
399};
400
401static const struct genl_multicast_group devlink_nl_mcgrps[] = {
402 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
403};
404
405static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
406{
407 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
408 return -EMSGSIZE;
409 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
410 return -EMSGSIZE;
411 return 0;
412}
413
414static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
415 enum devlink_command cmd, u32 portid,
416 u32 seq, int flags)
417{
418 void *hdr;
419
420 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
421 if (!hdr)
422 return -EMSGSIZE;
423
424 if (devlink_nl_put_handle(msg, devlink))
425 goto nla_put_failure;
426
427 genlmsg_end(msg, hdr);
428 return 0;
429
430nla_put_failure:
431 genlmsg_cancel(msg, hdr);
432 return -EMSGSIZE;
433}
434
435static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
436{
437 struct sk_buff *msg;
438 int err;
439
440 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
441
442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443 if (!msg)
444 return;
445
446 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
447 if (err) {
448 nlmsg_free(msg);
449 return;
450 }
451
452 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
453 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
454}
455
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200456static int devlink_nl_port_attrs_put(struct sk_buff *msg,
457 struct devlink_port *devlink_port)
458{
459 struct devlink_port_attrs *attrs = &devlink_port->attrs;
460
461 if (!attrs->set)
462 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200463 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
464 return -EMSGSIZE;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200465 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
466 return -EMSGSIZE;
467 if (!attrs->split)
468 return 0;
469 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
470 return -EMSGSIZE;
471 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
472 attrs->split_subport_number))
473 return -EMSGSIZE;
474 return 0;
475}
476
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100477static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
478 struct devlink_port *devlink_port,
479 enum devlink_command cmd, u32 portid,
480 u32 seq, int flags)
481{
482 void *hdr;
483
484 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
485 if (!hdr)
486 return -EMSGSIZE;
487
488 if (devlink_nl_put_handle(msg, devlink))
489 goto nla_put_failure;
490 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
491 goto nla_put_failure;
492 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
493 goto nla_put_failure;
494 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
495 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
496 devlink_port->desired_type))
497 goto nla_put_failure;
498 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
499 struct net_device *netdev = devlink_port->type_dev;
500
501 if (netdev &&
502 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
503 netdev->ifindex) ||
504 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
505 netdev->name)))
506 goto nla_put_failure;
507 }
508 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
509 struct ib_device *ibdev = devlink_port->type_dev;
510
511 if (ibdev &&
512 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
513 ibdev->name))
514 goto nla_put_failure;
515 }
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200516 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100517 goto nla_put_failure;
518
519 genlmsg_end(msg, hdr);
520 return 0;
521
522nla_put_failure:
523 genlmsg_cancel(msg, hdr);
524 return -EMSGSIZE;
525}
526
527static void devlink_port_notify(struct devlink_port *devlink_port,
528 enum devlink_command cmd)
529{
530 struct devlink *devlink = devlink_port->devlink;
531 struct sk_buff *msg;
532 int err;
533
534 if (!devlink_port->registered)
535 return;
536
537 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
538
539 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
540 if (!msg)
541 return;
542
543 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
544 if (err) {
545 nlmsg_free(msg);
546 return;
547 }
548
549 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
550 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
551}
552
553static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
554{
555 struct devlink *devlink = info->user_ptr[0];
556 struct sk_buff *msg;
557 int err;
558
559 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
560 if (!msg)
561 return -ENOMEM;
562
563 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
564 info->snd_portid, info->snd_seq, 0);
565 if (err) {
566 nlmsg_free(msg);
567 return err;
568 }
569
570 return genlmsg_reply(msg, info);
571}
572
573static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
574 struct netlink_callback *cb)
575{
576 struct devlink *devlink;
577 int start = cb->args[0];
578 int idx = 0;
579 int err;
580
581 mutex_lock(&devlink_mutex);
582 list_for_each_entry(devlink, &devlink_list, list) {
583 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
584 continue;
585 if (idx < start) {
586 idx++;
587 continue;
588 }
589 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
590 NETLINK_CB(cb->skb).portid,
591 cb->nlh->nlmsg_seq, NLM_F_MULTI);
592 if (err)
593 goto out;
594 idx++;
595 }
596out:
597 mutex_unlock(&devlink_mutex);
598
599 cb->args[0] = idx;
600 return msg->len;
601}
602
603static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
604 struct genl_info *info)
605{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200606 struct devlink_port *devlink_port = info->user_ptr[0];
607 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100608 struct sk_buff *msg;
609 int err;
610
611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
612 if (!msg)
613 return -ENOMEM;
614
615 err = devlink_nl_port_fill(msg, devlink, devlink_port,
616 DEVLINK_CMD_PORT_NEW,
617 info->snd_portid, info->snd_seq, 0);
618 if (err) {
619 nlmsg_free(msg);
620 return err;
621 }
622
623 return genlmsg_reply(msg, info);
624}
625
626static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
627 struct netlink_callback *cb)
628{
629 struct devlink *devlink;
630 struct devlink_port *devlink_port;
631 int start = cb->args[0];
632 int idx = 0;
633 int err;
634
635 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100636 list_for_each_entry(devlink, &devlink_list, list) {
637 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
638 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100639 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100640 list_for_each_entry(devlink_port, &devlink->port_list, list) {
641 if (idx < start) {
642 idx++;
643 continue;
644 }
645 err = devlink_nl_port_fill(msg, devlink, devlink_port,
646 DEVLINK_CMD_NEW,
647 NETLINK_CB(cb->skb).portid,
648 cb->nlh->nlmsg_seq,
649 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100650 if (err) {
651 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100652 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100653 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100654 idx++;
655 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100656 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100657 }
658out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100659 mutex_unlock(&devlink_mutex);
660
661 cb->args[0] = idx;
662 return msg->len;
663}
664
665static int devlink_port_type_set(struct devlink *devlink,
666 struct devlink_port *devlink_port,
667 enum devlink_port_type port_type)
668
669{
670 int err;
671
672 if (devlink->ops && devlink->ops->port_type_set) {
673 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
674 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200675 if (port_type == devlink_port->type)
676 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100677 err = devlink->ops->port_type_set(devlink_port, port_type);
678 if (err)
679 return err;
680 devlink_port->desired_type = port_type;
681 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
682 return 0;
683 }
684 return -EOPNOTSUPP;
685}
686
687static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
688 struct genl_info *info)
689{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200690 struct devlink_port *devlink_port = info->user_ptr[0];
691 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100692 int err;
693
694 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
695 enum devlink_port_type port_type;
696
697 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
698 err = devlink_port_type_set(devlink, devlink_port, port_type);
699 if (err)
700 return err;
701 }
702 return 0;
703}
704
705static int devlink_port_split(struct devlink *devlink,
706 u32 port_index, u32 count)
707
708{
709 if (devlink->ops && devlink->ops->port_split)
710 return devlink->ops->port_split(devlink, port_index, count);
711 return -EOPNOTSUPP;
712}
713
714static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
715 struct genl_info *info)
716{
717 struct devlink *devlink = info->user_ptr[0];
718 u32 port_index;
719 u32 count;
720
721 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
722 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
723 return -EINVAL;
724
725 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
726 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
727 return devlink_port_split(devlink, port_index, count);
728}
729
730static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
731
732{
733 if (devlink->ops && devlink->ops->port_unsplit)
734 return devlink->ops->port_unsplit(devlink, port_index);
735 return -EOPNOTSUPP;
736}
737
738static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
739 struct genl_info *info)
740{
741 struct devlink *devlink = info->user_ptr[0];
742 u32 port_index;
743
744 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
745 return -EINVAL;
746
747 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
748 return devlink_port_unsplit(devlink, port_index);
749}
750
Jiri Pirkobf797472016-04-14 18:19:13 +0200751static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
752 struct devlink_sb *devlink_sb,
753 enum devlink_command cmd, u32 portid,
754 u32 seq, int flags)
755{
756 void *hdr;
757
758 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
759 if (!hdr)
760 return -EMSGSIZE;
761
762 if (devlink_nl_put_handle(msg, devlink))
763 goto nla_put_failure;
764 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
765 goto nla_put_failure;
766 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
767 goto nla_put_failure;
768 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
769 devlink_sb->ingress_pools_count))
770 goto nla_put_failure;
771 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
772 devlink_sb->egress_pools_count))
773 goto nla_put_failure;
774 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
775 devlink_sb->ingress_tc_count))
776 goto nla_put_failure;
777 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
778 devlink_sb->egress_tc_count))
779 goto nla_put_failure;
780
781 genlmsg_end(msg, hdr);
782 return 0;
783
784nla_put_failure:
785 genlmsg_cancel(msg, hdr);
786 return -EMSGSIZE;
787}
788
789static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
790 struct genl_info *info)
791{
792 struct devlink *devlink = info->user_ptr[0];
793 struct devlink_sb *devlink_sb = info->user_ptr[1];
794 struct sk_buff *msg;
795 int err;
796
797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
798 if (!msg)
799 return -ENOMEM;
800
801 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
802 DEVLINK_CMD_SB_NEW,
803 info->snd_portid, info->snd_seq, 0);
804 if (err) {
805 nlmsg_free(msg);
806 return err;
807 }
808
809 return genlmsg_reply(msg, info);
810}
811
812static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
813 struct netlink_callback *cb)
814{
815 struct devlink *devlink;
816 struct devlink_sb *devlink_sb;
817 int start = cb->args[0];
818 int idx = 0;
819 int err;
820
821 mutex_lock(&devlink_mutex);
822 list_for_each_entry(devlink, &devlink_list, list) {
823 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
824 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100825 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200826 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
827 if (idx < start) {
828 idx++;
829 continue;
830 }
831 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
832 DEVLINK_CMD_SB_NEW,
833 NETLINK_CB(cb->skb).portid,
834 cb->nlh->nlmsg_seq,
835 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100836 if (err) {
837 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200838 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100839 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200840 idx++;
841 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100842 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200843 }
844out:
845 mutex_unlock(&devlink_mutex);
846
847 cb->args[0] = idx;
848 return msg->len;
849}
850
851static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
852 struct devlink_sb *devlink_sb,
853 u16 pool_index, enum devlink_command cmd,
854 u32 portid, u32 seq, int flags)
855{
856 struct devlink_sb_pool_info pool_info;
857 void *hdr;
858 int err;
859
860 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
861 pool_index, &pool_info);
862 if (err)
863 return err;
864
865 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
866 if (!hdr)
867 return -EMSGSIZE;
868
869 if (devlink_nl_put_handle(msg, devlink))
870 goto nla_put_failure;
871 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
872 goto nla_put_failure;
873 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
874 goto nla_put_failure;
875 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
876 goto nla_put_failure;
877 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
878 goto nla_put_failure;
879 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
880 pool_info.threshold_type))
881 goto nla_put_failure;
882
883 genlmsg_end(msg, hdr);
884 return 0;
885
886nla_put_failure:
887 genlmsg_cancel(msg, hdr);
888 return -EMSGSIZE;
889}
890
891static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
892 struct genl_info *info)
893{
894 struct devlink *devlink = info->user_ptr[0];
895 struct devlink_sb *devlink_sb = info->user_ptr[1];
896 struct sk_buff *msg;
897 u16 pool_index;
898 int err;
899
900 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
901 &pool_index);
902 if (err)
903 return err;
904
905 if (!devlink->ops || !devlink->ops->sb_pool_get)
906 return -EOPNOTSUPP;
907
908 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
909 if (!msg)
910 return -ENOMEM;
911
912 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
913 DEVLINK_CMD_SB_POOL_NEW,
914 info->snd_portid, info->snd_seq, 0);
915 if (err) {
916 nlmsg_free(msg);
917 return err;
918 }
919
920 return genlmsg_reply(msg, info);
921}
922
923static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
924 struct devlink *devlink,
925 struct devlink_sb *devlink_sb,
926 u32 portid, u32 seq)
927{
928 u16 pool_count = devlink_sb_pool_count(devlink_sb);
929 u16 pool_index;
930 int err;
931
932 for (pool_index = 0; pool_index < pool_count; pool_index++) {
933 if (*p_idx < start) {
934 (*p_idx)++;
935 continue;
936 }
937 err = devlink_nl_sb_pool_fill(msg, devlink,
938 devlink_sb,
939 pool_index,
940 DEVLINK_CMD_SB_POOL_NEW,
941 portid, seq, NLM_F_MULTI);
942 if (err)
943 return err;
944 (*p_idx)++;
945 }
946 return 0;
947}
948
949static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
950 struct netlink_callback *cb)
951{
952 struct devlink *devlink;
953 struct devlink_sb *devlink_sb;
954 int start = cb->args[0];
955 int idx = 0;
956 int err;
957
958 mutex_lock(&devlink_mutex);
959 list_for_each_entry(devlink, &devlink_list, list) {
960 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
961 !devlink->ops || !devlink->ops->sb_pool_get)
962 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100963 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200964 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
965 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
966 devlink_sb,
967 NETLINK_CB(cb->skb).portid,
968 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100969 if (err && err != -EOPNOTSUPP) {
970 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200971 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100972 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200973 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100974 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200975 }
976out:
977 mutex_unlock(&devlink_mutex);
978
979 cb->args[0] = idx;
980 return msg->len;
981}
982
983static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
984 u16 pool_index, u32 size,
985 enum devlink_sb_threshold_type threshold_type)
986
987{
988 const struct devlink_ops *ops = devlink->ops;
989
990 if (ops && ops->sb_pool_set)
991 return ops->sb_pool_set(devlink, sb_index, pool_index,
992 size, threshold_type);
993 return -EOPNOTSUPP;
994}
995
996static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
997 struct genl_info *info)
998{
999 struct devlink *devlink = info->user_ptr[0];
1000 struct devlink_sb *devlink_sb = info->user_ptr[1];
1001 enum devlink_sb_threshold_type threshold_type;
1002 u16 pool_index;
1003 u32 size;
1004 int err;
1005
1006 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1007 &pool_index);
1008 if (err)
1009 return err;
1010
1011 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1012 if (err)
1013 return err;
1014
1015 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1016 return -EINVAL;
1017
1018 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1019 return devlink_sb_pool_set(devlink, devlink_sb->index,
1020 pool_index, size, threshold_type);
1021}
1022
1023static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1024 struct devlink *devlink,
1025 struct devlink_port *devlink_port,
1026 struct devlink_sb *devlink_sb,
1027 u16 pool_index,
1028 enum devlink_command cmd,
1029 u32 portid, u32 seq, int flags)
1030{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001031 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001032 u32 threshold;
1033 void *hdr;
1034 int err;
1035
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001036 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1037 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001038 if (err)
1039 return err;
1040
1041 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1042 if (!hdr)
1043 return -EMSGSIZE;
1044
1045 if (devlink_nl_put_handle(msg, devlink))
1046 goto nla_put_failure;
1047 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1048 goto nla_put_failure;
1049 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1050 goto nla_put_failure;
1051 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1052 goto nla_put_failure;
1053 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1054 goto nla_put_failure;
1055
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001056 if (ops->sb_occ_port_pool_get) {
1057 u32 cur;
1058 u32 max;
1059
1060 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1061 pool_index, &cur, &max);
1062 if (err && err != -EOPNOTSUPP)
1063 return err;
1064 if (!err) {
1065 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1066 goto nla_put_failure;
1067 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1068 goto nla_put_failure;
1069 }
1070 }
1071
Jiri Pirkobf797472016-04-14 18:19:13 +02001072 genlmsg_end(msg, hdr);
1073 return 0;
1074
1075nla_put_failure:
1076 genlmsg_cancel(msg, hdr);
1077 return -EMSGSIZE;
1078}
1079
1080static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1081 struct genl_info *info)
1082{
1083 struct devlink_port *devlink_port = info->user_ptr[0];
1084 struct devlink *devlink = devlink_port->devlink;
1085 struct devlink_sb *devlink_sb = info->user_ptr[1];
1086 struct sk_buff *msg;
1087 u16 pool_index;
1088 int err;
1089
1090 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1091 &pool_index);
1092 if (err)
1093 return err;
1094
1095 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1096 return -EOPNOTSUPP;
1097
1098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1099 if (!msg)
1100 return -ENOMEM;
1101
1102 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1103 devlink_sb, pool_index,
1104 DEVLINK_CMD_SB_PORT_POOL_NEW,
1105 info->snd_portid, info->snd_seq, 0);
1106 if (err) {
1107 nlmsg_free(msg);
1108 return err;
1109 }
1110
1111 return genlmsg_reply(msg, info);
1112}
1113
1114static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1115 struct devlink *devlink,
1116 struct devlink_sb *devlink_sb,
1117 u32 portid, u32 seq)
1118{
1119 struct devlink_port *devlink_port;
1120 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1121 u16 pool_index;
1122 int err;
1123
1124 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1125 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1126 if (*p_idx < start) {
1127 (*p_idx)++;
1128 continue;
1129 }
1130 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1131 devlink_port,
1132 devlink_sb,
1133 pool_index,
1134 DEVLINK_CMD_SB_PORT_POOL_NEW,
1135 portid, seq,
1136 NLM_F_MULTI);
1137 if (err)
1138 return err;
1139 (*p_idx)++;
1140 }
1141 }
1142 return 0;
1143}
1144
1145static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1146 struct netlink_callback *cb)
1147{
1148 struct devlink *devlink;
1149 struct devlink_sb *devlink_sb;
1150 int start = cb->args[0];
1151 int idx = 0;
1152 int err;
1153
1154 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001155 list_for_each_entry(devlink, &devlink_list, list) {
1156 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1157 !devlink->ops || !devlink->ops->sb_port_pool_get)
1158 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001159 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001160 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1161 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1162 devlink, devlink_sb,
1163 NETLINK_CB(cb->skb).portid,
1164 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001165 if (err && err != -EOPNOTSUPP) {
1166 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001167 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001168 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001169 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001170 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001171 }
1172out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001173 mutex_unlock(&devlink_mutex);
1174
1175 cb->args[0] = idx;
1176 return msg->len;
1177}
1178
1179static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1180 unsigned int sb_index, u16 pool_index,
1181 u32 threshold)
1182
1183{
1184 const struct devlink_ops *ops = devlink_port->devlink->ops;
1185
1186 if (ops && ops->sb_port_pool_set)
1187 return ops->sb_port_pool_set(devlink_port, sb_index,
1188 pool_index, threshold);
1189 return -EOPNOTSUPP;
1190}
1191
1192static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1193 struct genl_info *info)
1194{
1195 struct devlink_port *devlink_port = info->user_ptr[0];
1196 struct devlink_sb *devlink_sb = info->user_ptr[1];
1197 u16 pool_index;
1198 u32 threshold;
1199 int err;
1200
1201 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1202 &pool_index);
1203 if (err)
1204 return err;
1205
1206 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1207 return -EINVAL;
1208
1209 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1210 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1211 pool_index, threshold);
1212}
1213
1214static int
1215devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1216 struct devlink_port *devlink_port,
1217 struct devlink_sb *devlink_sb, u16 tc_index,
1218 enum devlink_sb_pool_type pool_type,
1219 enum devlink_command cmd,
1220 u32 portid, u32 seq, int flags)
1221{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001222 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001223 u16 pool_index;
1224 u32 threshold;
1225 void *hdr;
1226 int err;
1227
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001228 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1229 tc_index, pool_type,
1230 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001231 if (err)
1232 return err;
1233
1234 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1235 if (!hdr)
1236 return -EMSGSIZE;
1237
1238 if (devlink_nl_put_handle(msg, devlink))
1239 goto nla_put_failure;
1240 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1241 goto nla_put_failure;
1242 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1243 goto nla_put_failure;
1244 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1245 goto nla_put_failure;
1246 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1247 goto nla_put_failure;
1248 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1249 goto nla_put_failure;
1250 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1251 goto nla_put_failure;
1252
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001253 if (ops->sb_occ_tc_port_bind_get) {
1254 u32 cur;
1255 u32 max;
1256
1257 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1258 devlink_sb->index,
1259 tc_index, pool_type,
1260 &cur, &max);
1261 if (err && err != -EOPNOTSUPP)
1262 return err;
1263 if (!err) {
1264 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1265 goto nla_put_failure;
1266 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1267 goto nla_put_failure;
1268 }
1269 }
1270
Jiri Pirkobf797472016-04-14 18:19:13 +02001271 genlmsg_end(msg, hdr);
1272 return 0;
1273
1274nla_put_failure:
1275 genlmsg_cancel(msg, hdr);
1276 return -EMSGSIZE;
1277}
1278
1279static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1280 struct genl_info *info)
1281{
1282 struct devlink_port *devlink_port = info->user_ptr[0];
1283 struct devlink *devlink = devlink_port->devlink;
1284 struct devlink_sb *devlink_sb = info->user_ptr[1];
1285 struct sk_buff *msg;
1286 enum devlink_sb_pool_type pool_type;
1287 u16 tc_index;
1288 int err;
1289
1290 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1291 if (err)
1292 return err;
1293
1294 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1295 pool_type, &tc_index);
1296 if (err)
1297 return err;
1298
1299 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1300 return -EOPNOTSUPP;
1301
1302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1303 if (!msg)
1304 return -ENOMEM;
1305
1306 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1307 devlink_sb, tc_index, pool_type,
1308 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1309 info->snd_portid,
1310 info->snd_seq, 0);
1311 if (err) {
1312 nlmsg_free(msg);
1313 return err;
1314 }
1315
1316 return genlmsg_reply(msg, info);
1317}
1318
1319static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1320 int start, int *p_idx,
1321 struct devlink *devlink,
1322 struct devlink_sb *devlink_sb,
1323 u32 portid, u32 seq)
1324{
1325 struct devlink_port *devlink_port;
1326 u16 tc_index;
1327 int err;
1328
1329 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1330 for (tc_index = 0;
1331 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1332 if (*p_idx < start) {
1333 (*p_idx)++;
1334 continue;
1335 }
1336 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1337 devlink_port,
1338 devlink_sb,
1339 tc_index,
1340 DEVLINK_SB_POOL_TYPE_INGRESS,
1341 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1342 portid, seq,
1343 NLM_F_MULTI);
1344 if (err)
1345 return err;
1346 (*p_idx)++;
1347 }
1348 for (tc_index = 0;
1349 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1350 if (*p_idx < start) {
1351 (*p_idx)++;
1352 continue;
1353 }
1354 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1355 devlink_port,
1356 devlink_sb,
1357 tc_index,
1358 DEVLINK_SB_POOL_TYPE_EGRESS,
1359 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1360 portid, seq,
1361 NLM_F_MULTI);
1362 if (err)
1363 return err;
1364 (*p_idx)++;
1365 }
1366 }
1367 return 0;
1368}
1369
1370static int
1371devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1372 struct netlink_callback *cb)
1373{
1374 struct devlink *devlink;
1375 struct devlink_sb *devlink_sb;
1376 int start = cb->args[0];
1377 int idx = 0;
1378 int err;
1379
1380 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001381 list_for_each_entry(devlink, &devlink_list, list) {
1382 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1383 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1384 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001385
1386 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001387 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1388 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1389 devlink,
1390 devlink_sb,
1391 NETLINK_CB(cb->skb).portid,
1392 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001393 if (err && err != -EOPNOTSUPP) {
1394 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001395 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001396 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001397 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001398 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001399 }
1400out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001401 mutex_unlock(&devlink_mutex);
1402
1403 cb->args[0] = idx;
1404 return msg->len;
1405}
1406
1407static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1408 unsigned int sb_index, u16 tc_index,
1409 enum devlink_sb_pool_type pool_type,
1410 u16 pool_index, u32 threshold)
1411
1412{
1413 const struct devlink_ops *ops = devlink_port->devlink->ops;
1414
1415 if (ops && ops->sb_tc_pool_bind_set)
1416 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1417 tc_index, pool_type,
1418 pool_index, threshold);
1419 return -EOPNOTSUPP;
1420}
1421
1422static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1423 struct genl_info *info)
1424{
1425 struct devlink_port *devlink_port = info->user_ptr[0];
1426 struct devlink_sb *devlink_sb = info->user_ptr[1];
1427 enum devlink_sb_pool_type pool_type;
1428 u16 tc_index;
1429 u16 pool_index;
1430 u32 threshold;
1431 int err;
1432
1433 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1434 if (err)
1435 return err;
1436
1437 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1438 pool_type, &tc_index);
1439 if (err)
1440 return err;
1441
1442 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1443 &pool_index);
1444 if (err)
1445 return err;
1446
1447 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1448 return -EINVAL;
1449
1450 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1451 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1452 tc_index, pool_type,
1453 pool_index, threshold);
1454}
1455
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001456static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1457 struct genl_info *info)
1458{
1459 struct devlink *devlink = info->user_ptr[0];
1460 struct devlink_sb *devlink_sb = info->user_ptr[1];
1461 const struct devlink_ops *ops = devlink->ops;
1462
1463 if (ops && ops->sb_occ_snapshot)
1464 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1465 return -EOPNOTSUPP;
1466}
1467
1468static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1469 struct genl_info *info)
1470{
1471 struct devlink *devlink = info->user_ptr[0];
1472 struct devlink_sb *devlink_sb = info->user_ptr[1];
1473 const struct devlink_ops *ops = devlink->ops;
1474
1475 if (ops && ops->sb_occ_max_clear)
1476 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1477 return -EOPNOTSUPP;
1478}
1479
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001480static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1481 enum devlink_command cmd, u32 portid,
1482 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001483{
Roi Dayan59bfde02016-11-22 23:09:57 +02001484 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001485 u8 inline_mode, encap_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001486 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001487 int err = 0;
1488 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001489
1490 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1491 if (!hdr)
1492 return -EMSGSIZE;
1493
Roi Dayan59bfde02016-11-22 23:09:57 +02001494 err = devlink_nl_put_handle(msg, devlink);
1495 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001496 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001497
Jiri Pirko4456f612017-02-09 15:54:36 +01001498 if (ops->eswitch_mode_get) {
1499 err = ops->eswitch_mode_get(devlink, &mode);
1500 if (err)
1501 goto nla_put_failure;
1502 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1503 if (err)
1504 goto nla_put_failure;
1505 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001506
1507 if (ops->eswitch_inline_mode_get) {
1508 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1509 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001510 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001511 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1512 inline_mode);
1513 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001514 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001515 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001516
Roi Dayanf43e9b02016-09-25 13:52:44 +03001517 if (ops->eswitch_encap_mode_get) {
1518 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1519 if (err)
1520 goto nla_put_failure;
1521 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1522 if (err)
1523 goto nla_put_failure;
1524 }
1525
Or Gerlitz08f4b592016-07-01 14:51:01 +03001526 genlmsg_end(msg, hdr);
1527 return 0;
1528
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001529nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001530 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001531 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001532}
1533
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001534static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1535 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001536{
1537 struct devlink *devlink = info->user_ptr[0];
1538 const struct devlink_ops *ops = devlink->ops;
1539 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001540 int err;
1541
Jiri Pirko4456f612017-02-09 15:54:36 +01001542 if (!ops)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001543 return -EOPNOTSUPP;
1544
Or Gerlitz08f4b592016-07-01 14:51:01 +03001545 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1546 if (!msg)
1547 return -ENOMEM;
1548
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001549 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1550 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001551
1552 if (err) {
1553 nlmsg_free(msg);
1554 return err;
1555 }
1556
1557 return genlmsg_reply(msg, info);
1558}
1559
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001560static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1561 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001562{
1563 struct devlink *devlink = info->user_ptr[0];
1564 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001565 u8 inline_mode, encap_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001566 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001567 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001568
Roi Dayan59bfde02016-11-22 23:09:57 +02001569 if (!ops)
1570 return -EOPNOTSUPP;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001571
Roi Dayan59bfde02016-11-22 23:09:57 +02001572 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1573 if (!ops->eswitch_mode_set)
1574 return -EOPNOTSUPP;
1575 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1576 err = ops->eswitch_mode_set(devlink, mode);
1577 if (err)
1578 return err;
1579 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001580
Roi Dayan59bfde02016-11-22 23:09:57 +02001581 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1582 if (!ops->eswitch_inline_mode_set)
1583 return -EOPNOTSUPP;
1584 inline_mode = nla_get_u8(
1585 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1586 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1587 if (err)
1588 return err;
1589 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001590
1591 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1592 if (!ops->eswitch_encap_mode_set)
1593 return -EOPNOTSUPP;
1594 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1595 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1596 if (err)
1597 return err;
1598 }
1599
Roi Dayan59bfde02016-11-22 23:09:57 +02001600 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001601}
1602
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001603int devlink_dpipe_match_put(struct sk_buff *skb,
1604 struct devlink_dpipe_match *match)
1605{
1606 struct devlink_dpipe_header *header = match->header;
1607 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1608 struct nlattr *match_attr;
1609
1610 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1611 if (!match_attr)
1612 return -EMSGSIZE;
1613
1614 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1615 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1616 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1617 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1618 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1619 goto nla_put_failure;
1620
1621 nla_nest_end(skb, match_attr);
1622 return 0;
1623
1624nla_put_failure:
1625 nla_nest_cancel(skb, match_attr);
1626 return -EMSGSIZE;
1627}
1628EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1629
1630static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1631 struct sk_buff *skb)
1632{
1633 struct nlattr *matches_attr;
1634
1635 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1636 if (!matches_attr)
1637 return -EMSGSIZE;
1638
1639 if (table->table_ops->matches_dump(table->priv, skb))
1640 goto nla_put_failure;
1641
1642 nla_nest_end(skb, matches_attr);
1643 return 0;
1644
1645nla_put_failure:
1646 nla_nest_cancel(skb, matches_attr);
1647 return -EMSGSIZE;
1648}
1649
1650int devlink_dpipe_action_put(struct sk_buff *skb,
1651 struct devlink_dpipe_action *action)
1652{
1653 struct devlink_dpipe_header *header = action->header;
1654 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1655 struct nlattr *action_attr;
1656
1657 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1658 if (!action_attr)
1659 return -EMSGSIZE;
1660
1661 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1662 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1663 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1664 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1665 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1666 goto nla_put_failure;
1667
1668 nla_nest_end(skb, action_attr);
1669 return 0;
1670
1671nla_put_failure:
1672 nla_nest_cancel(skb, action_attr);
1673 return -EMSGSIZE;
1674}
1675EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1676
1677static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1678 struct sk_buff *skb)
1679{
1680 struct nlattr *actions_attr;
1681
1682 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1683 if (!actions_attr)
1684 return -EMSGSIZE;
1685
1686 if (table->table_ops->actions_dump(table->priv, skb))
1687 goto nla_put_failure;
1688
1689 nla_nest_end(skb, actions_attr);
1690 return 0;
1691
1692nla_put_failure:
1693 nla_nest_cancel(skb, actions_attr);
1694 return -EMSGSIZE;
1695}
1696
1697static int devlink_dpipe_table_put(struct sk_buff *skb,
1698 struct devlink_dpipe_table *table)
1699{
1700 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001701 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001702
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001703 table_size = table->table_ops->size_get(table->priv);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001704 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1705 if (!table_attr)
1706 return -EMSGSIZE;
1707
1708 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001709 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001710 DEVLINK_ATTR_PAD))
1711 goto nla_put_failure;
1712 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1713 table->counters_enabled))
1714 goto nla_put_failure;
1715
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001716 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001717 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1718 table->resource_id, DEVLINK_ATTR_PAD) ||
1719 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1720 table->resource_units, DEVLINK_ATTR_PAD))
1721 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001722 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001723 if (devlink_dpipe_matches_put(table, skb))
1724 goto nla_put_failure;
1725
1726 if (devlink_dpipe_actions_put(table, skb))
1727 goto nla_put_failure;
1728
1729 nla_nest_end(skb, table_attr);
1730 return 0;
1731
1732nla_put_failure:
1733 nla_nest_cancel(skb, table_attr);
1734 return -EMSGSIZE;
1735}
1736
1737static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1738 struct genl_info *info)
1739{
1740 int err;
1741
1742 if (*pskb) {
1743 err = genlmsg_reply(*pskb, info);
1744 if (err)
1745 return err;
1746 }
1747 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1748 if (!*pskb)
1749 return -ENOMEM;
1750 return 0;
1751}
1752
1753static int devlink_dpipe_tables_fill(struct genl_info *info,
1754 enum devlink_command cmd, int flags,
1755 struct list_head *dpipe_tables,
1756 const char *table_name)
1757{
1758 struct devlink *devlink = info->user_ptr[0];
1759 struct devlink_dpipe_table *table;
1760 struct nlattr *tables_attr;
1761 struct sk_buff *skb = NULL;
1762 struct nlmsghdr *nlh;
1763 bool incomplete;
1764 void *hdr;
1765 int i;
1766 int err;
1767
1768 table = list_first_entry(dpipe_tables,
1769 struct devlink_dpipe_table, list);
1770start_again:
1771 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1772 if (err)
1773 return err;
1774
1775 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1776 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001777 if (!hdr) {
1778 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001779 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001780 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001781
1782 if (devlink_nl_put_handle(skb, devlink))
1783 goto nla_put_failure;
1784 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1785 if (!tables_attr)
1786 goto nla_put_failure;
1787
1788 i = 0;
1789 incomplete = false;
1790 list_for_each_entry_from(table, dpipe_tables, list) {
1791 if (!table_name) {
1792 err = devlink_dpipe_table_put(skb, table);
1793 if (err) {
1794 if (!i)
1795 goto err_table_put;
1796 incomplete = true;
1797 break;
1798 }
1799 } else {
1800 if (!strcmp(table->name, table_name)) {
1801 err = devlink_dpipe_table_put(skb, table);
1802 if (err)
1803 break;
1804 }
1805 }
1806 i++;
1807 }
1808
1809 nla_nest_end(skb, tables_attr);
1810 genlmsg_end(skb, hdr);
1811 if (incomplete)
1812 goto start_again;
1813
1814send_done:
1815 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1816 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1817 if (!nlh) {
1818 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1819 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001820 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001821 goto send_done;
1822 }
1823
1824 return genlmsg_reply(skb, info);
1825
1826nla_put_failure:
1827 err = -EMSGSIZE;
1828err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001829 genlmsg_cancel(skb, hdr);
1830 nlmsg_free(skb);
1831 return err;
1832}
1833
1834static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1835 struct genl_info *info)
1836{
1837 struct devlink *devlink = info->user_ptr[0];
1838 const char *table_name = NULL;
1839
1840 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1841 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1842
1843 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1844 &devlink->dpipe_table_list,
1845 table_name);
1846}
1847
1848static int devlink_dpipe_value_put(struct sk_buff *skb,
1849 struct devlink_dpipe_value *value)
1850{
1851 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1852 value->value_size, value->value))
1853 return -EMSGSIZE;
1854 if (value->mask)
1855 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1856 value->value_size, value->mask))
1857 return -EMSGSIZE;
1858 if (value->mapping_valid)
1859 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1860 value->mapping_value))
1861 return -EMSGSIZE;
1862 return 0;
1863}
1864
1865static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1866 struct devlink_dpipe_value *value)
1867{
1868 if (!value->action)
1869 return -EINVAL;
1870 if (devlink_dpipe_action_put(skb, value->action))
1871 return -EMSGSIZE;
1872 if (devlink_dpipe_value_put(skb, value))
1873 return -EMSGSIZE;
1874 return 0;
1875}
1876
1877static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1878 struct devlink_dpipe_value *values,
1879 unsigned int values_count)
1880{
1881 struct nlattr *action_attr;
1882 int i;
1883 int err;
1884
1885 for (i = 0; i < values_count; i++) {
1886 action_attr = nla_nest_start(skb,
1887 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1888 if (!action_attr)
1889 return -EMSGSIZE;
1890 err = devlink_dpipe_action_value_put(skb, &values[i]);
1891 if (err)
1892 goto err_action_value_put;
1893 nla_nest_end(skb, action_attr);
1894 }
1895 return 0;
1896
1897err_action_value_put:
1898 nla_nest_cancel(skb, action_attr);
1899 return err;
1900}
1901
1902static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1903 struct devlink_dpipe_value *value)
1904{
1905 if (!value->match)
1906 return -EINVAL;
1907 if (devlink_dpipe_match_put(skb, value->match))
1908 return -EMSGSIZE;
1909 if (devlink_dpipe_value_put(skb, value))
1910 return -EMSGSIZE;
1911 return 0;
1912}
1913
1914static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1915 struct devlink_dpipe_value *values,
1916 unsigned int values_count)
1917{
1918 struct nlattr *match_attr;
1919 int i;
1920 int err;
1921
1922 for (i = 0; i < values_count; i++) {
1923 match_attr = nla_nest_start(skb,
1924 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1925 if (!match_attr)
1926 return -EMSGSIZE;
1927 err = devlink_dpipe_match_value_put(skb, &values[i]);
1928 if (err)
1929 goto err_match_value_put;
1930 nla_nest_end(skb, match_attr);
1931 }
1932 return 0;
1933
1934err_match_value_put:
1935 nla_nest_cancel(skb, match_attr);
1936 return err;
1937}
1938
1939static int devlink_dpipe_entry_put(struct sk_buff *skb,
1940 struct devlink_dpipe_entry *entry)
1941{
1942 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1943 int err;
1944
1945 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1946 if (!entry_attr)
1947 return -EMSGSIZE;
1948
1949 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
1950 DEVLINK_ATTR_PAD))
1951 goto nla_put_failure;
1952 if (entry->counter_valid)
1953 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
1954 entry->counter, DEVLINK_ATTR_PAD))
1955 goto nla_put_failure;
1956
1957 matches_attr = nla_nest_start(skb,
1958 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
1959 if (!matches_attr)
1960 goto nla_put_failure;
1961
1962 err = devlink_dpipe_match_values_put(skb, entry->match_values,
1963 entry->match_values_count);
1964 if (err) {
1965 nla_nest_cancel(skb, matches_attr);
1966 goto err_match_values_put;
1967 }
1968 nla_nest_end(skb, matches_attr);
1969
1970 actions_attr = nla_nest_start(skb,
1971 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
1972 if (!actions_attr)
1973 goto nla_put_failure;
1974
1975 err = devlink_dpipe_action_values_put(skb, entry->action_values,
1976 entry->action_values_count);
1977 if (err) {
1978 nla_nest_cancel(skb, actions_attr);
1979 goto err_action_values_put;
1980 }
1981 nla_nest_end(skb, actions_attr);
1982
1983 nla_nest_end(skb, entry_attr);
1984 return 0;
1985
1986nla_put_failure:
1987 err = -EMSGSIZE;
1988err_match_values_put:
1989err_action_values_put:
1990 nla_nest_cancel(skb, entry_attr);
1991 return err;
1992}
1993
1994static struct devlink_dpipe_table *
1995devlink_dpipe_table_find(struct list_head *dpipe_tables,
1996 const char *table_name)
1997{
1998 struct devlink_dpipe_table *table;
1999
2000 list_for_each_entry_rcu(table, dpipe_tables, list) {
2001 if (!strcmp(table->name, table_name))
2002 return table;
2003 }
2004 return NULL;
2005}
2006
2007int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2008{
2009 struct devlink *devlink;
2010 int err;
2011
2012 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2013 dump_ctx->info);
2014 if (err)
2015 return err;
2016
2017 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2018 dump_ctx->info->snd_portid,
2019 dump_ctx->info->snd_seq,
2020 &devlink_nl_family, NLM_F_MULTI,
2021 dump_ctx->cmd);
2022 if (!dump_ctx->hdr)
2023 goto nla_put_failure;
2024
2025 devlink = dump_ctx->info->user_ptr[0];
2026 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2027 goto nla_put_failure;
2028 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2029 DEVLINK_ATTR_DPIPE_ENTRIES);
2030 if (!dump_ctx->nest)
2031 goto nla_put_failure;
2032 return 0;
2033
2034nla_put_failure:
2035 genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
2036 nlmsg_free(dump_ctx->skb);
2037 return -EMSGSIZE;
2038}
2039EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2040
2041int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2042 struct devlink_dpipe_entry *entry)
2043{
2044 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2045}
2046EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2047
2048int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2049{
2050 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2051 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2052 return 0;
2053}
2054EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2055
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002056void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2057
2058{
2059 unsigned int value_count, value_index;
2060 struct devlink_dpipe_value *value;
2061
2062 value = entry->action_values;
2063 value_count = entry->action_values_count;
2064 for (value_index = 0; value_index < value_count; value_index++) {
2065 kfree(value[value_index].value);
2066 kfree(value[value_index].mask);
2067 }
2068
2069 value = entry->match_values;
2070 value_count = entry->match_values_count;
2071 for (value_index = 0; value_index < value_count; value_index++) {
2072 kfree(value[value_index].value);
2073 kfree(value[value_index].mask);
2074 }
2075}
2076EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2077
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002078static int devlink_dpipe_entries_fill(struct genl_info *info,
2079 enum devlink_command cmd, int flags,
2080 struct devlink_dpipe_table *table)
2081{
2082 struct devlink_dpipe_dump_ctx dump_ctx;
2083 struct nlmsghdr *nlh;
2084 int err;
2085
2086 dump_ctx.skb = NULL;
2087 dump_ctx.cmd = cmd;
2088 dump_ctx.info = info;
2089
2090 err = table->table_ops->entries_dump(table->priv,
2091 table->counters_enabled,
2092 &dump_ctx);
2093 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002094 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002095
2096send_done:
2097 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2098 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2099 if (!nlh) {
2100 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2101 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002102 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002103 goto send_done;
2104 }
2105 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002106}
2107
2108static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2109 struct genl_info *info)
2110{
2111 struct devlink *devlink = info->user_ptr[0];
2112 struct devlink_dpipe_table *table;
2113 const char *table_name;
2114
2115 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2116 return -EINVAL;
2117
2118 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2119 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2120 table_name);
2121 if (!table)
2122 return -EINVAL;
2123
2124 if (!table->table_ops->entries_dump)
2125 return -EINVAL;
2126
2127 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2128 0, table);
2129}
2130
2131static int devlink_dpipe_fields_put(struct sk_buff *skb,
2132 const struct devlink_dpipe_header *header)
2133{
2134 struct devlink_dpipe_field *field;
2135 struct nlattr *field_attr;
2136 int i;
2137
2138 for (i = 0; i < header->fields_count; i++) {
2139 field = &header->fields[i];
2140 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2141 if (!field_attr)
2142 return -EMSGSIZE;
2143 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2144 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2145 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2146 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2147 goto nla_put_failure;
2148 nla_nest_end(skb, field_attr);
2149 }
2150 return 0;
2151
2152nla_put_failure:
2153 nla_nest_cancel(skb, field_attr);
2154 return -EMSGSIZE;
2155}
2156
2157static int devlink_dpipe_header_put(struct sk_buff *skb,
2158 struct devlink_dpipe_header *header)
2159{
2160 struct nlattr *fields_attr, *header_attr;
2161 int err;
2162
2163 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002164 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002165 return -EMSGSIZE;
2166
2167 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2168 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2169 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2170 goto nla_put_failure;
2171
2172 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2173 if (!fields_attr)
2174 goto nla_put_failure;
2175
2176 err = devlink_dpipe_fields_put(skb, header);
2177 if (err) {
2178 nla_nest_cancel(skb, fields_attr);
2179 goto nla_put_failure;
2180 }
2181 nla_nest_end(skb, fields_attr);
2182 nla_nest_end(skb, header_attr);
2183 return 0;
2184
2185nla_put_failure:
2186 err = -EMSGSIZE;
2187 nla_nest_cancel(skb, header_attr);
2188 return err;
2189}
2190
2191static int devlink_dpipe_headers_fill(struct genl_info *info,
2192 enum devlink_command cmd, int flags,
2193 struct devlink_dpipe_headers *
2194 dpipe_headers)
2195{
2196 struct devlink *devlink = info->user_ptr[0];
2197 struct nlattr *headers_attr;
2198 struct sk_buff *skb = NULL;
2199 struct nlmsghdr *nlh;
2200 void *hdr;
2201 int i, j;
2202 int err;
2203
2204 i = 0;
2205start_again:
2206 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2207 if (err)
2208 return err;
2209
2210 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2211 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002212 if (!hdr) {
2213 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002214 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002215 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002216
2217 if (devlink_nl_put_handle(skb, devlink))
2218 goto nla_put_failure;
2219 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2220 if (!headers_attr)
2221 goto nla_put_failure;
2222
2223 j = 0;
2224 for (; i < dpipe_headers->headers_count; i++) {
2225 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2226 if (err) {
2227 if (!j)
2228 goto err_table_put;
2229 break;
2230 }
2231 j++;
2232 }
2233 nla_nest_end(skb, headers_attr);
2234 genlmsg_end(skb, hdr);
2235 if (i != dpipe_headers->headers_count)
2236 goto start_again;
2237
2238send_done:
2239 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2240 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2241 if (!nlh) {
2242 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2243 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002244 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002245 goto send_done;
2246 }
2247 return genlmsg_reply(skb, info);
2248
2249nla_put_failure:
2250 err = -EMSGSIZE;
2251err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002252 genlmsg_cancel(skb, hdr);
2253 nlmsg_free(skb);
2254 return err;
2255}
2256
2257static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2258 struct genl_info *info)
2259{
2260 struct devlink *devlink = info->user_ptr[0];
2261
2262 if (!devlink->dpipe_headers)
2263 return -EOPNOTSUPP;
2264 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2265 0, devlink->dpipe_headers);
2266}
2267
2268static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2269 const char *table_name,
2270 bool enable)
2271{
2272 struct devlink_dpipe_table *table;
2273
2274 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2275 table_name);
2276 if (!table)
2277 return -EINVAL;
2278
2279 if (table->counter_control_extern)
2280 return -EOPNOTSUPP;
2281
2282 if (!(table->counters_enabled ^ enable))
2283 return 0;
2284
2285 table->counters_enabled = enable;
2286 if (table->table_ops->counters_set_update)
2287 table->table_ops->counters_set_update(table->priv, enable);
2288 return 0;
2289}
2290
2291static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2292 struct genl_info *info)
2293{
2294 struct devlink *devlink = info->user_ptr[0];
2295 const char *table_name;
2296 bool counters_enable;
2297
2298 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2299 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2300 return -EINVAL;
2301
2302 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2303 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2304
2305 return devlink_dpipe_table_counters_set(devlink, table_name,
2306 counters_enable);
2307}
2308
Wei Yongjun43dd7512018-01-17 03:27:42 +00002309static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002310devlink_resource_find(struct devlink *devlink,
2311 struct devlink_resource *resource, u64 resource_id)
2312{
2313 struct list_head *resource_list;
2314
2315 if (resource)
2316 resource_list = &resource->resource_list;
2317 else
2318 resource_list = &devlink->resource_list;
2319
2320 list_for_each_entry(resource, resource_list, list) {
2321 struct devlink_resource *child_resource;
2322
2323 if (resource->id == resource_id)
2324 return resource;
2325
2326 child_resource = devlink_resource_find(devlink, resource,
2327 resource_id);
2328 if (child_resource)
2329 return child_resource;
2330 }
2331 return NULL;
2332}
2333
Wei Yongjun43dd7512018-01-17 03:27:42 +00002334static void
2335devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002336{
2337 struct devlink_resource *child_resource;
2338 bool size_valid = true;
2339 u64 parts_size = 0;
2340
2341 if (list_empty(&resource->resource_list))
2342 goto out;
2343
2344 list_for_each_entry(child_resource, &resource->resource_list, list)
2345 parts_size += child_resource->size_new;
2346
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002347 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002348 size_valid = false;
2349out:
2350 resource->size_valid = size_valid;
2351}
2352
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002353static int
2354devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2355 struct netlink_ext_ack *extack)
2356{
2357 u64 reminder;
2358 int err = 0;
2359
David S. Miller0f3e9c92018-03-06 00:53:44 -05002360 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002361 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2362 err = -EINVAL;
2363 }
2364
David S. Miller0f3e9c92018-03-06 00:53:44 -05002365 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002366 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2367 err = -EINVAL;
2368 }
2369
David S. Miller0f3e9c92018-03-06 00:53:44 -05002370 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002371 if (reminder) {
2372 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2373 err = -EINVAL;
2374 }
2375
2376 return err;
2377}
2378
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002379static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2380 struct genl_info *info)
2381{
2382 struct devlink *devlink = info->user_ptr[0];
2383 struct devlink_resource *resource;
2384 u64 resource_id;
2385 u64 size;
2386 int err;
2387
2388 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2389 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2390 return -EINVAL;
2391 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2392
2393 resource = devlink_resource_find(devlink, NULL, resource_id);
2394 if (!resource)
2395 return -EINVAL;
2396
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002397 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002398 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002399 if (err)
2400 return err;
2401
2402 resource->size_new = size;
2403 devlink_resource_validate_children(resource);
2404 if (resource->parent)
2405 devlink_resource_validate_children(resource->parent);
2406 return 0;
2407}
2408
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002409static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002410devlink_resource_size_params_put(struct devlink_resource *resource,
2411 struct sk_buff *skb)
2412{
2413 struct devlink_resource_size_params *size_params;
2414
Jiri Pirko77d27092018-02-28 13:12:09 +01002415 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002416 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2417 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2418 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2419 size_params->size_max, DEVLINK_ATTR_PAD) ||
2420 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2421 size_params->size_min, DEVLINK_ATTR_PAD) ||
2422 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2423 return -EMSGSIZE;
2424 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002425}
2426
Jiri Pirkofc56be42018-04-05 22:13:21 +02002427static int devlink_resource_occ_put(struct devlink_resource *resource,
2428 struct sk_buff *skb)
2429{
2430 if (!resource->occ_get)
2431 return 0;
2432 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2433 resource->occ_get(resource->occ_get_priv),
2434 DEVLINK_ATTR_PAD);
2435}
2436
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002437static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2438 struct devlink_resource *resource)
2439{
2440 struct devlink_resource *child_resource;
2441 struct nlattr *child_resource_attr;
2442 struct nlattr *resource_attr;
2443
2444 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2445 if (!resource_attr)
2446 return -EMSGSIZE;
2447
2448 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2449 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2450 DEVLINK_ATTR_PAD) ||
2451 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2452 DEVLINK_ATTR_PAD))
2453 goto nla_put_failure;
2454 if (resource->size != resource->size_new)
2455 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2456 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002457 if (devlink_resource_occ_put(resource, skb))
2458 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002459 if (devlink_resource_size_params_put(resource, skb))
2460 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002461 if (list_empty(&resource->resource_list))
2462 goto out;
2463
2464 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2465 resource->size_valid))
2466 goto nla_put_failure;
2467
2468 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2469 if (!child_resource_attr)
2470 goto nla_put_failure;
2471
2472 list_for_each_entry(child_resource, &resource->resource_list, list) {
2473 if (devlink_resource_put(devlink, skb, child_resource))
2474 goto resource_put_failure;
2475 }
2476
2477 nla_nest_end(skb, child_resource_attr);
2478out:
2479 nla_nest_end(skb, resource_attr);
2480 return 0;
2481
2482resource_put_failure:
2483 nla_nest_cancel(skb, child_resource_attr);
2484nla_put_failure:
2485 nla_nest_cancel(skb, resource_attr);
2486 return -EMSGSIZE;
2487}
2488
2489static int devlink_resource_fill(struct genl_info *info,
2490 enum devlink_command cmd, int flags)
2491{
2492 struct devlink *devlink = info->user_ptr[0];
2493 struct devlink_resource *resource;
2494 struct nlattr *resources_attr;
2495 struct sk_buff *skb = NULL;
2496 struct nlmsghdr *nlh;
2497 bool incomplete;
2498 void *hdr;
2499 int i;
2500 int err;
2501
2502 resource = list_first_entry(&devlink->resource_list,
2503 struct devlink_resource, list);
2504start_again:
2505 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2506 if (err)
2507 return err;
2508
2509 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2510 &devlink_nl_family, NLM_F_MULTI, cmd);
2511 if (!hdr) {
2512 nlmsg_free(skb);
2513 return -EMSGSIZE;
2514 }
2515
2516 if (devlink_nl_put_handle(skb, devlink))
2517 goto nla_put_failure;
2518
2519 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2520 if (!resources_attr)
2521 goto nla_put_failure;
2522
2523 incomplete = false;
2524 i = 0;
2525 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2526 err = devlink_resource_put(devlink, skb, resource);
2527 if (err) {
2528 if (!i)
2529 goto err_resource_put;
2530 incomplete = true;
2531 break;
2532 }
2533 i++;
2534 }
2535 nla_nest_end(skb, resources_attr);
2536 genlmsg_end(skb, hdr);
2537 if (incomplete)
2538 goto start_again;
2539send_done:
2540 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2541 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2542 if (!nlh) {
2543 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2544 if (err)
2545 goto err_skb_send_alloc;
2546 goto send_done;
2547 }
2548 return genlmsg_reply(skb, info);
2549
2550nla_put_failure:
2551 err = -EMSGSIZE;
2552err_resource_put:
2553err_skb_send_alloc:
2554 genlmsg_cancel(skb, hdr);
2555 nlmsg_free(skb);
2556 return err;
2557}
2558
2559static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2560 struct genl_info *info)
2561{
2562 struct devlink *devlink = info->user_ptr[0];
2563
2564 if (list_empty(&devlink->resource_list))
2565 return -EOPNOTSUPP;
2566
2567 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2568}
2569
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002570static int
2571devlink_resources_validate(struct devlink *devlink,
2572 struct devlink_resource *resource,
2573 struct genl_info *info)
2574{
2575 struct list_head *resource_list;
2576 int err = 0;
2577
2578 if (resource)
2579 resource_list = &resource->resource_list;
2580 else
2581 resource_list = &devlink->resource_list;
2582
2583 list_for_each_entry(resource, resource_list, list) {
2584 if (!resource->size_valid)
2585 return -EINVAL;
2586 err = devlink_resources_validate(devlink, resource, info);
2587 if (err)
2588 return err;
2589 }
2590 return err;
2591}
2592
2593static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2594{
2595 struct devlink *devlink = info->user_ptr[0];
2596 int err;
2597
2598 if (!devlink->ops->reload)
2599 return -EOPNOTSUPP;
2600
2601 err = devlink_resources_validate(devlink, NULL, info);
2602 if (err) {
2603 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2604 return err;
2605 }
2606 return devlink->ops->reload(devlink);
2607}
2608
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002609static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
2610 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
2611 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
2612 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
2613 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
2614 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02002615 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
2616 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
2617 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
2618 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
2619 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
2620 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
2621 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03002622 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02002623 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03002624 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002625 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
2626 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002627 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
2628 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002629};
2630
2631static const struct genl_ops devlink_nl_ops[] = {
2632 {
2633 .cmd = DEVLINK_CMD_GET,
2634 .doit = devlink_nl_cmd_get_doit,
2635 .dumpit = devlink_nl_cmd_get_dumpit,
2636 .policy = devlink_nl_policy,
Jiri Pirko1fc22572016-04-08 19:12:48 +02002637 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002638 /* can be retrieved by unprivileged users */
2639 },
2640 {
2641 .cmd = DEVLINK_CMD_PORT_GET,
2642 .doit = devlink_nl_cmd_port_get_doit,
2643 .dumpit = devlink_nl_cmd_port_get_dumpit,
2644 .policy = devlink_nl_policy,
2645 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2646 /* can be retrieved by unprivileged users */
2647 },
2648 {
2649 .cmd = DEVLINK_CMD_PORT_SET,
2650 .doit = devlink_nl_cmd_port_set_doit,
2651 .policy = devlink_nl_policy,
2652 .flags = GENL_ADMIN_PERM,
2653 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2654 },
2655 {
2656 .cmd = DEVLINK_CMD_PORT_SPLIT,
2657 .doit = devlink_nl_cmd_port_split_doit,
2658 .policy = devlink_nl_policy,
2659 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002660 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2661 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002662 },
2663 {
2664 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
2665 .doit = devlink_nl_cmd_port_unsplit_doit,
2666 .policy = devlink_nl_policy,
2667 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002668 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2669 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002670 },
Jiri Pirkobf797472016-04-14 18:19:13 +02002671 {
2672 .cmd = DEVLINK_CMD_SB_GET,
2673 .doit = devlink_nl_cmd_sb_get_doit,
2674 .dumpit = devlink_nl_cmd_sb_get_dumpit,
2675 .policy = devlink_nl_policy,
2676 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2677 DEVLINK_NL_FLAG_NEED_SB,
2678 /* can be retrieved by unprivileged users */
2679 },
2680 {
2681 .cmd = DEVLINK_CMD_SB_POOL_GET,
2682 .doit = devlink_nl_cmd_sb_pool_get_doit,
2683 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
2684 .policy = devlink_nl_policy,
2685 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2686 DEVLINK_NL_FLAG_NEED_SB,
2687 /* can be retrieved by unprivileged users */
2688 },
2689 {
2690 .cmd = DEVLINK_CMD_SB_POOL_SET,
2691 .doit = devlink_nl_cmd_sb_pool_set_doit,
2692 .policy = devlink_nl_policy,
2693 .flags = GENL_ADMIN_PERM,
2694 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2695 DEVLINK_NL_FLAG_NEED_SB,
2696 },
2697 {
2698 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
2699 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
2700 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
2701 .policy = devlink_nl_policy,
2702 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2703 DEVLINK_NL_FLAG_NEED_SB,
2704 /* can be retrieved by unprivileged users */
2705 },
2706 {
2707 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
2708 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
2709 .policy = devlink_nl_policy,
2710 .flags = GENL_ADMIN_PERM,
2711 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2712 DEVLINK_NL_FLAG_NEED_SB,
2713 },
2714 {
2715 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
2716 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
2717 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
2718 .policy = devlink_nl_policy,
2719 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2720 DEVLINK_NL_FLAG_NEED_SB,
2721 /* can be retrieved by unprivileged users */
2722 },
2723 {
2724 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
2725 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
2726 .policy = devlink_nl_policy,
2727 .flags = GENL_ADMIN_PERM,
2728 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2729 DEVLINK_NL_FLAG_NEED_SB,
2730 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02002731 {
2732 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
2733 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
2734 .policy = devlink_nl_policy,
2735 .flags = GENL_ADMIN_PERM,
2736 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002737 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02002738 },
2739 {
2740 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
2741 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
2742 .policy = devlink_nl_policy,
2743 .flags = GENL_ADMIN_PERM,
2744 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002745 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02002746 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03002747 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01002748 .cmd = DEVLINK_CMD_ESWITCH_GET,
2749 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03002750 .policy = devlink_nl_policy,
2751 .flags = GENL_ADMIN_PERM,
2752 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2753 },
2754 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01002755 .cmd = DEVLINK_CMD_ESWITCH_SET,
2756 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03002757 .policy = devlink_nl_policy,
2758 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07002759 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2760 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03002761 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002762 {
2763 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
2764 .doit = devlink_nl_cmd_dpipe_table_get,
2765 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002766 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02002767 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002768 },
2769 {
2770 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
2771 .doit = devlink_nl_cmd_dpipe_entries_get,
2772 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002773 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02002774 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002775 },
2776 {
2777 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
2778 .doit = devlink_nl_cmd_dpipe_headers_get,
2779 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002780 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02002781 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002782 },
2783 {
2784 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
2785 .doit = devlink_nl_cmd_dpipe_table_counters_set,
2786 .policy = devlink_nl_policy,
2787 .flags = GENL_ADMIN_PERM,
2788 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2789 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002790 {
2791 .cmd = DEVLINK_CMD_RESOURCE_SET,
2792 .doit = devlink_nl_cmd_resource_set,
2793 .policy = devlink_nl_policy,
2794 .flags = GENL_ADMIN_PERM,
2795 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2796 },
2797 {
2798 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
2799 .doit = devlink_nl_cmd_resource_dump,
2800 .policy = devlink_nl_policy,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002801 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02002802 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002803 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002804 {
2805 .cmd = DEVLINK_CMD_RELOAD,
2806 .doit = devlink_nl_cmd_reload,
2807 .policy = devlink_nl_policy,
2808 .flags = GENL_ADMIN_PERM,
2809 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2810 DEVLINK_NL_FLAG_NO_LOCK,
2811 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002812};
2813
Johannes Berg56989f62016-10-24 14:40:05 +02002814static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02002815 .name = DEVLINK_GENL_NAME,
2816 .version = DEVLINK_GENL_VERSION,
2817 .maxattr = DEVLINK_ATTR_MAX,
2818 .netnsok = true,
2819 .pre_doit = devlink_nl_pre_doit,
2820 .post_doit = devlink_nl_post_doit,
2821 .module = THIS_MODULE,
2822 .ops = devlink_nl_ops,
2823 .n_ops = ARRAY_SIZE(devlink_nl_ops),
2824 .mcgrps = devlink_nl_mcgrps,
2825 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
2826};
2827
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002828/**
2829 * devlink_alloc - Allocate new devlink instance resources
2830 *
2831 * @ops: ops
2832 * @priv_size: size of user private data
2833 *
2834 * Allocate new devlink instance resources, including devlink index
2835 * and name.
2836 */
2837struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
2838{
2839 struct devlink *devlink;
2840
2841 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
2842 if (!devlink)
2843 return NULL;
2844 devlink->ops = ops;
2845 devlink_net_set(devlink, &init_net);
2846 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02002847 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002848 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002849 INIT_LIST_HEAD(&devlink->resource_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002850 mutex_init(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002851 return devlink;
2852}
2853EXPORT_SYMBOL_GPL(devlink_alloc);
2854
2855/**
2856 * devlink_register - Register devlink instance
2857 *
2858 * @devlink: devlink
2859 */
2860int devlink_register(struct devlink *devlink, struct device *dev)
2861{
2862 mutex_lock(&devlink_mutex);
2863 devlink->dev = dev;
2864 list_add_tail(&devlink->list, &devlink_list);
2865 devlink_notify(devlink, DEVLINK_CMD_NEW);
2866 mutex_unlock(&devlink_mutex);
2867 return 0;
2868}
2869EXPORT_SYMBOL_GPL(devlink_register);
2870
2871/**
2872 * devlink_unregister - Unregister devlink instance
2873 *
2874 * @devlink: devlink
2875 */
2876void devlink_unregister(struct devlink *devlink)
2877{
2878 mutex_lock(&devlink_mutex);
2879 devlink_notify(devlink, DEVLINK_CMD_DEL);
2880 list_del(&devlink->list);
2881 mutex_unlock(&devlink_mutex);
2882}
2883EXPORT_SYMBOL_GPL(devlink_unregister);
2884
2885/**
2886 * devlink_free - Free devlink instance resources
2887 *
2888 * @devlink: devlink
2889 */
2890void devlink_free(struct devlink *devlink)
2891{
2892 kfree(devlink);
2893}
2894EXPORT_SYMBOL_GPL(devlink_free);
2895
2896/**
2897 * devlink_port_register - Register devlink port
2898 *
2899 * @devlink: devlink
2900 * @devlink_port: devlink port
2901 * @port_index
2902 *
2903 * Register devlink port with provided port index. User can use
2904 * any indexing, even hw-related one. devlink_port structure
2905 * is convenient to be embedded inside user driver private structure.
2906 * Note that the caller should take care of zeroing the devlink_port
2907 * structure.
2908 */
2909int devlink_port_register(struct devlink *devlink,
2910 struct devlink_port *devlink_port,
2911 unsigned int port_index)
2912{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002913 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002914 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002915 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002916 return -EEXIST;
2917 }
2918 devlink_port->devlink = devlink;
2919 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002920 devlink_port->registered = true;
2921 list_add_tail(&devlink_port->list, &devlink->port_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002922 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002923 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(devlink_port_register);
2927
2928/**
2929 * devlink_port_unregister - Unregister devlink port
2930 *
2931 * @devlink_port: devlink port
2932 */
2933void devlink_port_unregister(struct devlink_port *devlink_port)
2934{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002935 struct devlink *devlink = devlink_port->devlink;
2936
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002937 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002938 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002939 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01002940 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002941}
2942EXPORT_SYMBOL_GPL(devlink_port_unregister);
2943
2944static void __devlink_port_type_set(struct devlink_port *devlink_port,
2945 enum devlink_port_type type,
2946 void *type_dev)
2947{
2948 devlink_port->type = type;
2949 devlink_port->type_dev = type_dev;
2950 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2951}
2952
2953/**
2954 * devlink_port_type_eth_set - Set port type to Ethernet
2955 *
2956 * @devlink_port: devlink port
2957 * @netdev: related netdevice
2958 */
2959void devlink_port_type_eth_set(struct devlink_port *devlink_port,
2960 struct net_device *netdev)
2961{
2962 return __devlink_port_type_set(devlink_port,
2963 DEVLINK_PORT_TYPE_ETH, netdev);
2964}
2965EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
2966
2967/**
2968 * devlink_port_type_ib_set - Set port type to InfiniBand
2969 *
2970 * @devlink_port: devlink port
2971 * @ibdev: related IB device
2972 */
2973void devlink_port_type_ib_set(struct devlink_port *devlink_port,
2974 struct ib_device *ibdev)
2975{
2976 return __devlink_port_type_set(devlink_port,
2977 DEVLINK_PORT_TYPE_IB, ibdev);
2978}
2979EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
2980
2981/**
2982 * devlink_port_type_clear - Clear port type
2983 *
2984 * @devlink_port: devlink port
2985 */
2986void devlink_port_type_clear(struct devlink_port *devlink_port)
2987{
2988 return __devlink_port_type_set(devlink_port,
2989 DEVLINK_PORT_TYPE_NOTSET, NULL);
2990}
2991EXPORT_SYMBOL_GPL(devlink_port_type_clear);
2992
2993/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02002994 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002995 *
2996 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02002997 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02002998 * @port_number: number of the port that is facing user, for example
2999 * the front panel port number
3000 * @split: indicates if this is split port
3001 * @split_subport_number: if the port is split, this is the number
3002 * of subport.
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003003 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003004void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02003005 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003006 u32 port_number, bool split,
3007 u32 split_subport_number)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003008{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003009 struct devlink_port_attrs *attrs = &devlink_port->attrs;
3010
3011 attrs->set = true;
Jiri Pirko5ec13802018-05-18 09:29:01 +02003012 attrs->flavour = flavour;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003013 attrs->port_number = port_number;
3014 attrs->split = split;
3015 attrs->split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003016 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3017}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003018EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003019
Jiri Pirko08474c12018-05-18 09:29:02 +02003020int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
3021 char *name, size_t len)
3022{
3023 struct devlink_port_attrs *attrs = &devlink_port->attrs;
3024 int n = 0;
3025
3026 if (!attrs->set)
3027 return -EOPNOTSUPP;
3028
3029 switch (attrs->flavour) {
3030 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
3031 if (!attrs->split)
3032 n = snprintf(name, len, "p%u", attrs->port_number);
3033 else
3034 n = snprintf(name, len, "p%us%u", attrs->port_number,
3035 attrs->split_subport_number);
3036 break;
3037 case DEVLINK_PORT_FLAVOUR_CPU:
3038 case DEVLINK_PORT_FLAVOUR_DSA:
3039 /* As CPU and DSA ports do not have a netdevice associated
3040 * case should not ever happen.
3041 */
3042 WARN_ON(1);
3043 return -EINVAL;
3044 }
3045
3046 if (n >= len)
3047 return -EINVAL;
3048
3049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
3052
Jiri Pirkobf797472016-04-14 18:19:13 +02003053int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
3054 u32 size, u16 ingress_pools_count,
3055 u16 egress_pools_count, u16 ingress_tc_count,
3056 u16 egress_tc_count)
3057{
3058 struct devlink_sb *devlink_sb;
3059 int err = 0;
3060
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003061 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003062 if (devlink_sb_index_exists(devlink, sb_index)) {
3063 err = -EEXIST;
3064 goto unlock;
3065 }
3066
3067 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
3068 if (!devlink_sb) {
3069 err = -ENOMEM;
3070 goto unlock;
3071 }
3072 devlink_sb->index = sb_index;
3073 devlink_sb->size = size;
3074 devlink_sb->ingress_pools_count = ingress_pools_count;
3075 devlink_sb->egress_pools_count = egress_pools_count;
3076 devlink_sb->ingress_tc_count = ingress_tc_count;
3077 devlink_sb->egress_tc_count = egress_tc_count;
3078 list_add_tail(&devlink_sb->list, &devlink->sb_list);
3079unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003080 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003081 return err;
3082}
3083EXPORT_SYMBOL_GPL(devlink_sb_register);
3084
3085void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
3086{
3087 struct devlink_sb *devlink_sb;
3088
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003089 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003090 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
3091 WARN_ON(!devlink_sb);
3092 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003093 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003094 kfree(devlink_sb);
3095}
3096EXPORT_SYMBOL_GPL(devlink_sb_unregister);
3097
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003098/**
3099 * devlink_dpipe_headers_register - register dpipe headers
3100 *
3101 * @devlink: devlink
3102 * @dpipe_headers: dpipe header array
3103 *
3104 * Register the headers supported by hardware.
3105 */
3106int devlink_dpipe_headers_register(struct devlink *devlink,
3107 struct devlink_dpipe_headers *dpipe_headers)
3108{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003109 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003110 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003111 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003112 return 0;
3113}
3114EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
3115
3116/**
3117 * devlink_dpipe_headers_unregister - unregister dpipe headers
3118 *
3119 * @devlink: devlink
3120 *
3121 * Unregister the headers supported by hardware.
3122 */
3123void devlink_dpipe_headers_unregister(struct devlink *devlink)
3124{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003125 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003126 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003127 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003128}
3129EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
3130
3131/**
3132 * devlink_dpipe_table_counter_enabled - check if counter allocation
3133 * required
3134 * @devlink: devlink
3135 * @table_name: tables name
3136 *
3137 * Used by driver to check if counter allocation is required.
3138 * After counter allocation is turned on the table entries
3139 * are updated to include counter statistics.
3140 *
3141 * After that point on the driver must respect the counter
3142 * state so that each entry added to the table is added
3143 * with a counter.
3144 */
3145bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
3146 const char *table_name)
3147{
3148 struct devlink_dpipe_table *table;
3149 bool enabled;
3150
3151 rcu_read_lock();
3152 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3153 table_name);
3154 enabled = false;
3155 if (table)
3156 enabled = table->counters_enabled;
3157 rcu_read_unlock();
3158 return enabled;
3159}
3160EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
3161
3162/**
3163 * devlink_dpipe_table_register - register dpipe table
3164 *
3165 * @devlink: devlink
3166 * @table_name: table name
3167 * @table_ops: table ops
3168 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003169 * @counter_control_extern: external control for counters
3170 */
3171int devlink_dpipe_table_register(struct devlink *devlink,
3172 const char *table_name,
3173 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02003174 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003175{
3176 struct devlink_dpipe_table *table;
3177
3178 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
3179 return -EEXIST;
3180
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02003181 if (WARN_ON(!table_ops->size_get))
3182 return -EINVAL;
3183
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003184 table = kzalloc(sizeof(*table), GFP_KERNEL);
3185 if (!table)
3186 return -ENOMEM;
3187
3188 table->name = table_name;
3189 table->table_ops = table_ops;
3190 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003191 table->counter_control_extern = counter_control_extern;
3192
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003193 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003194 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003195 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003196 return 0;
3197}
3198EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
3199
3200/**
3201 * devlink_dpipe_table_unregister - unregister dpipe table
3202 *
3203 * @devlink: devlink
3204 * @table_name: table name
3205 */
3206void devlink_dpipe_table_unregister(struct devlink *devlink,
3207 const char *table_name)
3208{
3209 struct devlink_dpipe_table *table;
3210
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003211 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003212 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3213 table_name);
3214 if (!table)
3215 goto unlock;
3216 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003217 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003218 kfree_rcu(table, rcu);
3219 return;
3220unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003221 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003222}
3223EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
3224
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003225/**
3226 * devlink_resource_register - devlink resource register
3227 *
3228 * @devlink: devlink
3229 * @resource_name: resource's name
3230 * @top_hierarchy: top hierarchy
3231 * @reload_required: reload is required for new configuration to
3232 * apply
3233 * @resource_size: resource's size
3234 * @resource_id: resource's id
3235 * @parent_reosurce_id: resource's parent id
3236 * @size params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003237 */
3238int devlink_resource_register(struct devlink *devlink,
3239 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003240 u64 resource_size,
3241 u64 resource_id,
3242 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02003243 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003244{
3245 struct devlink_resource *resource;
3246 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07003247 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003248 int err = 0;
3249
David Ahern14530742018-03-20 19:31:14 -07003250 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
3251
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003252 mutex_lock(&devlink->lock);
3253 resource = devlink_resource_find(devlink, NULL, resource_id);
3254 if (resource) {
3255 err = -EINVAL;
3256 goto out;
3257 }
3258
3259 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
3260 if (!resource) {
3261 err = -ENOMEM;
3262 goto out;
3263 }
3264
3265 if (top_hierarchy) {
3266 resource_list = &devlink->resource_list;
3267 } else {
3268 struct devlink_resource *parent_resource;
3269
3270 parent_resource = devlink_resource_find(devlink, NULL,
3271 parent_resource_id);
3272 if (parent_resource) {
3273 resource_list = &parent_resource->resource_list;
3274 resource->parent = parent_resource;
3275 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00003276 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003277 err = -EINVAL;
3278 goto out;
3279 }
3280 }
3281
3282 resource->name = resource_name;
3283 resource->size = resource_size;
3284 resource->size_new = resource_size;
3285 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003286 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01003287 memcpy(&resource->size_params, size_params,
3288 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003289 INIT_LIST_HEAD(&resource->resource_list);
3290 list_add_tail(&resource->list, resource_list);
3291out:
3292 mutex_unlock(&devlink->lock);
3293 return err;
3294}
3295EXPORT_SYMBOL_GPL(devlink_resource_register);
3296
3297/**
3298 * devlink_resources_unregister - free all resources
3299 *
3300 * @devlink: devlink
3301 * @resource: resource
3302 */
3303void devlink_resources_unregister(struct devlink *devlink,
3304 struct devlink_resource *resource)
3305{
3306 struct devlink_resource *tmp, *child_resource;
3307 struct list_head *resource_list;
3308
3309 if (resource)
3310 resource_list = &resource->resource_list;
3311 else
3312 resource_list = &devlink->resource_list;
3313
3314 if (!resource)
3315 mutex_lock(&devlink->lock);
3316
3317 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
3318 devlink_resources_unregister(devlink, child_resource);
3319 list_del(&child_resource->list);
3320 kfree(child_resource);
3321 }
3322
3323 if (!resource)
3324 mutex_unlock(&devlink->lock);
3325}
3326EXPORT_SYMBOL_GPL(devlink_resources_unregister);
3327
3328/**
3329 * devlink_resource_size_get - get and update size
3330 *
3331 * @devlink: devlink
3332 * @resource_id: the requested resource id
3333 * @p_resource_size: ptr to update
3334 */
3335int devlink_resource_size_get(struct devlink *devlink,
3336 u64 resource_id,
3337 u64 *p_resource_size)
3338{
3339 struct devlink_resource *resource;
3340 int err = 0;
3341
3342 mutex_lock(&devlink->lock);
3343 resource = devlink_resource_find(devlink, NULL, resource_id);
3344 if (!resource) {
3345 err = -EINVAL;
3346 goto out;
3347 }
3348 *p_resource_size = resource->size_new;
3349 resource->size = resource->size_new;
3350out:
3351 mutex_unlock(&devlink->lock);
3352 return err;
3353}
3354EXPORT_SYMBOL_GPL(devlink_resource_size_get);
3355
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01003356/**
3357 * devlink_dpipe_table_resource_set - set the resource id
3358 *
3359 * @devlink: devlink
3360 * @table_name: table name
3361 * @resource_id: resource id
3362 * @resource_units: number of resource's units consumed per table's entry
3363 */
3364int devlink_dpipe_table_resource_set(struct devlink *devlink,
3365 const char *table_name, u64 resource_id,
3366 u64 resource_units)
3367{
3368 struct devlink_dpipe_table *table;
3369 int err = 0;
3370
3371 mutex_lock(&devlink->lock);
3372 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3373 table_name);
3374 if (!table) {
3375 err = -EINVAL;
3376 goto out;
3377 }
3378 table->resource_id = resource_id;
3379 table->resource_units = resource_units;
3380 table->resource_valid = true;
3381out:
3382 mutex_unlock(&devlink->lock);
3383 return err;
3384}
3385EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
3386
Jiri Pirkofc56be42018-04-05 22:13:21 +02003387/**
3388 * devlink_resource_occ_get_register - register occupancy getter
3389 *
3390 * @devlink: devlink
3391 * @resource_id: resource id
3392 * @occ_get: occupancy getter callback
3393 * @occ_get_priv: occupancy getter callback priv
3394 */
3395void devlink_resource_occ_get_register(struct devlink *devlink,
3396 u64 resource_id,
3397 devlink_resource_occ_get_t *occ_get,
3398 void *occ_get_priv)
3399{
3400 struct devlink_resource *resource;
3401
3402 mutex_lock(&devlink->lock);
3403 resource = devlink_resource_find(devlink, NULL, resource_id);
3404 if (WARN_ON(!resource))
3405 goto out;
3406 WARN_ON(resource->occ_get);
3407
3408 resource->occ_get = occ_get;
3409 resource->occ_get_priv = occ_get_priv;
3410out:
3411 mutex_unlock(&devlink->lock);
3412}
3413EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
3414
3415/**
3416 * devlink_resource_occ_get_unregister - unregister occupancy getter
3417 *
3418 * @devlink: devlink
3419 * @resource_id: resource id
3420 */
3421void devlink_resource_occ_get_unregister(struct devlink *devlink,
3422 u64 resource_id)
3423{
3424 struct devlink_resource *resource;
3425
3426 mutex_lock(&devlink->lock);
3427 resource = devlink_resource_find(devlink, NULL, resource_id);
3428 if (WARN_ON(!resource))
3429 goto out;
3430 WARN_ON(!resource->occ_get);
3431
3432 resource->occ_get = NULL;
3433 resource->occ_get_priv = NULL;
3434out:
3435 mutex_unlock(&devlink->lock);
3436}
3437EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
3438
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003439static int __init devlink_module_init(void)
3440{
Johannes Berg489111e2016-10-24 14:40:03 +02003441 return genl_register_family(&devlink_nl_family);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003442}
3443
3444static void __exit devlink_module_exit(void)
3445{
3446 genl_unregister_family(&devlink_nl_family);
3447}
3448
3449module_init(devlink_module_init);
3450module_exit(devlink_module_exit);
3451
3452MODULE_LICENSE("GPL v2");
3453MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
3454MODULE_DESCRIPTION("Network physical device Netlink interface");
3455MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);