blob: d2fd736de6a29f8cbf77da29ba5162245f919ce5 [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
32EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010033
34static LIST_HEAD(devlink_list);
35
36/* devlink_mutex
37 *
38 * An overall lock guarding every operation coming from userspace.
39 * It also guards devlink devices list and it is taken when
40 * driver registers/unregisters it.
41 */
42static DEFINE_MUTEX(devlink_mutex);
43
44/* devlink_port_mutex
45 *
46 * Shared lock to guard lists of ports in all devlink devices.
47 */
48static DEFINE_MUTEX(devlink_port_mutex);
49
50static struct net *devlink_net(const struct devlink *devlink)
51{
52 return read_pnet(&devlink->_net);
53}
54
55static void devlink_net_set(struct devlink *devlink, struct net *net)
56{
57 write_pnet(&devlink->_net, net);
58}
59
60static struct devlink *devlink_get_from_attrs(struct net *net,
61 struct nlattr **attrs)
62{
63 struct devlink *devlink;
64 char *busname;
65 char *devname;
66
67 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
68 return ERR_PTR(-EINVAL);
69
70 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
71 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
72
73 list_for_each_entry(devlink, &devlink_list, list) {
74 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
75 strcmp(dev_name(devlink->dev), devname) == 0 &&
76 net_eq(devlink_net(devlink), net))
77 return devlink;
78 }
79
80 return ERR_PTR(-ENODEV);
81}
82
83static struct devlink *devlink_get_from_info(struct genl_info *info)
84{
85 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
86}
87
88static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
89 int port_index)
90{
91 struct devlink_port *devlink_port;
92
93 list_for_each_entry(devlink_port, &devlink->port_list, list) {
94 if (devlink_port->index == port_index)
95 return devlink_port;
96 }
97 return NULL;
98}
99
100static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
101{
102 return devlink_port_get_by_index(devlink, port_index);
103}
104
105static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
106 struct nlattr **attrs)
107{
108 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
109 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
110 struct devlink_port *devlink_port;
111
112 devlink_port = devlink_port_get_by_index(devlink, port_index);
113 if (!devlink_port)
114 return ERR_PTR(-ENODEV);
115 return devlink_port;
116 }
117 return ERR_PTR(-EINVAL);
118}
119
120static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
121 struct genl_info *info)
122{
123 return devlink_port_get_from_attrs(devlink, info->attrs);
124}
125
Jiri Pirkobf797472016-04-14 18:19:13 +0200126struct devlink_sb {
127 struct list_head list;
128 unsigned int index;
129 u32 size;
130 u16 ingress_pools_count;
131 u16 egress_pools_count;
132 u16 ingress_tc_count;
133 u16 egress_tc_count;
134};
135
136static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
137{
138 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
139}
140
141static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
142 unsigned int sb_index)
143{
144 struct devlink_sb *devlink_sb;
145
146 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
147 if (devlink_sb->index == sb_index)
148 return devlink_sb;
149 }
150 return NULL;
151}
152
153static bool devlink_sb_index_exists(struct devlink *devlink,
154 unsigned int sb_index)
155{
156 return devlink_sb_get_by_index(devlink, sb_index);
157}
158
159static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
160 struct nlattr **attrs)
161{
162 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
163 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
164 struct devlink_sb *devlink_sb;
165
166 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
167 if (!devlink_sb)
168 return ERR_PTR(-ENODEV);
169 return devlink_sb;
170 }
171 return ERR_PTR(-EINVAL);
172}
173
174static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
175 struct genl_info *info)
176{
177 return devlink_sb_get_from_attrs(devlink, info->attrs);
178}
179
180static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
181 struct nlattr **attrs,
182 u16 *p_pool_index)
183{
184 u16 val;
185
186 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
187 return -EINVAL;
188
189 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
190 if (val >= devlink_sb_pool_count(devlink_sb))
191 return -EINVAL;
192 *p_pool_index = val;
193 return 0;
194}
195
196static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
197 struct genl_info *info,
198 u16 *p_pool_index)
199{
200 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
201 p_pool_index);
202}
203
204static int
205devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
206 enum devlink_sb_pool_type *p_pool_type)
207{
208 u8 val;
209
210 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
211 return -EINVAL;
212
213 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
214 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
215 val != DEVLINK_SB_POOL_TYPE_EGRESS)
216 return -EINVAL;
217 *p_pool_type = val;
218 return 0;
219}
220
221static int
222devlink_sb_pool_type_get_from_info(struct genl_info *info,
223 enum devlink_sb_pool_type *p_pool_type)
224{
225 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
226}
227
228static int
229devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
230 enum devlink_sb_threshold_type *p_th_type)
231{
232 u8 val;
233
234 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
235 return -EINVAL;
236
237 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
238 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
239 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
240 return -EINVAL;
241 *p_th_type = val;
242 return 0;
243}
244
245static int
246devlink_sb_th_type_get_from_info(struct genl_info *info,
247 enum devlink_sb_threshold_type *p_th_type)
248{
249 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
250}
251
252static int
253devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
254 struct nlattr **attrs,
255 enum devlink_sb_pool_type pool_type,
256 u16 *p_tc_index)
257{
258 u16 val;
259
260 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
261 return -EINVAL;
262
263 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
264 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
265 val >= devlink_sb->ingress_tc_count)
266 return -EINVAL;
267 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
268 val >= devlink_sb->egress_tc_count)
269 return -EINVAL;
270 *p_tc_index = val;
271 return 0;
272}
273
274static int
275devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
276 struct genl_info *info,
277 enum devlink_sb_pool_type pool_type,
278 u16 *p_tc_index)
279{
280 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
281 pool_type, p_tc_index);
282}
283
Jiri Pirko1fc22572016-04-08 19:12:48 +0200284#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
285#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200286#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200287#define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
288 /* port is not needed but we need to ensure they don't
289 * change in the middle of command
290 */
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100291
292static int devlink_nl_pre_doit(const struct genl_ops *ops,
293 struct sk_buff *skb, struct genl_info *info)
294{
295 struct devlink *devlink;
296
297 mutex_lock(&devlink_mutex);
298 devlink = devlink_get_from_info(info);
299 if (IS_ERR(devlink)) {
300 mutex_unlock(&devlink_mutex);
301 return PTR_ERR(devlink);
302 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200303 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
304 info->user_ptr[0] = devlink;
305 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100306 struct devlink_port *devlink_port;
307
308 mutex_lock(&devlink_port_mutex);
309 devlink_port = devlink_port_get_from_info(devlink, info);
310 if (IS_ERR(devlink_port)) {
311 mutex_unlock(&devlink_port_mutex);
312 mutex_unlock(&devlink_mutex);
313 return PTR_ERR(devlink_port);
314 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200315 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100316 }
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200317 if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) {
318 mutex_lock(&devlink_port_mutex);
319 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200320 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
321 struct devlink_sb *devlink_sb;
322
323 devlink_sb = devlink_sb_get_from_info(devlink, info);
324 if (IS_ERR(devlink_sb)) {
325 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT)
326 mutex_unlock(&devlink_port_mutex);
327 mutex_unlock(&devlink_mutex);
328 return PTR_ERR(devlink_sb);
329 }
330 info->user_ptr[1] = devlink_sb;
331 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100332 return 0;
333}
334
335static void devlink_nl_post_doit(const struct genl_ops *ops,
336 struct sk_buff *skb, struct genl_info *info)
337{
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200338 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT ||
339 ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100340 mutex_unlock(&devlink_port_mutex);
341 mutex_unlock(&devlink_mutex);
342}
343
344static struct genl_family devlink_nl_family = {
345 .id = GENL_ID_GENERATE,
346 .name = DEVLINK_GENL_NAME,
347 .version = DEVLINK_GENL_VERSION,
348 .maxattr = DEVLINK_ATTR_MAX,
349 .netnsok = true,
350 .pre_doit = devlink_nl_pre_doit,
351 .post_doit = devlink_nl_post_doit,
352};
353
354enum devlink_multicast_groups {
355 DEVLINK_MCGRP_CONFIG,
356};
357
358static const struct genl_multicast_group devlink_nl_mcgrps[] = {
359 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
360};
361
362static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
363{
364 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
365 return -EMSGSIZE;
366 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
367 return -EMSGSIZE;
368 return 0;
369}
370
371static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
372 enum devlink_command cmd, u32 portid,
373 u32 seq, int flags)
374{
375 void *hdr;
376
377 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
378 if (!hdr)
379 return -EMSGSIZE;
380
381 if (devlink_nl_put_handle(msg, devlink))
382 goto nla_put_failure;
383
384 genlmsg_end(msg, hdr);
385 return 0;
386
387nla_put_failure:
388 genlmsg_cancel(msg, hdr);
389 return -EMSGSIZE;
390}
391
392static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
393{
394 struct sk_buff *msg;
395 int err;
396
397 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
398
399 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
400 if (!msg)
401 return;
402
403 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
404 if (err) {
405 nlmsg_free(msg);
406 return;
407 }
408
409 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
410 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
411}
412
413static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
414 struct devlink_port *devlink_port,
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 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
427 goto nla_put_failure;
428 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
429 goto nla_put_failure;
430 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
431 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
432 devlink_port->desired_type))
433 goto nla_put_failure;
434 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
435 struct net_device *netdev = devlink_port->type_dev;
436
437 if (netdev &&
438 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
439 netdev->ifindex) ||
440 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
441 netdev->name)))
442 goto nla_put_failure;
443 }
444 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
445 struct ib_device *ibdev = devlink_port->type_dev;
446
447 if (ibdev &&
448 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
449 ibdev->name))
450 goto nla_put_failure;
451 }
452 if (devlink_port->split &&
453 nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
454 devlink_port->split_group))
455 goto nla_put_failure;
456
457 genlmsg_end(msg, hdr);
458 return 0;
459
460nla_put_failure:
461 genlmsg_cancel(msg, hdr);
462 return -EMSGSIZE;
463}
464
465static void devlink_port_notify(struct devlink_port *devlink_port,
466 enum devlink_command cmd)
467{
468 struct devlink *devlink = devlink_port->devlink;
469 struct sk_buff *msg;
470 int err;
471
472 if (!devlink_port->registered)
473 return;
474
475 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
476
477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
478 if (!msg)
479 return;
480
481 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
482 if (err) {
483 nlmsg_free(msg);
484 return;
485 }
486
487 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
488 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
489}
490
491static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
492{
493 struct devlink *devlink = info->user_ptr[0];
494 struct sk_buff *msg;
495 int err;
496
497 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
498 if (!msg)
499 return -ENOMEM;
500
501 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
502 info->snd_portid, info->snd_seq, 0);
503 if (err) {
504 nlmsg_free(msg);
505 return err;
506 }
507
508 return genlmsg_reply(msg, info);
509}
510
511static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
512 struct netlink_callback *cb)
513{
514 struct devlink *devlink;
515 int start = cb->args[0];
516 int idx = 0;
517 int err;
518
519 mutex_lock(&devlink_mutex);
520 list_for_each_entry(devlink, &devlink_list, list) {
521 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
522 continue;
523 if (idx < start) {
524 idx++;
525 continue;
526 }
527 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
528 NETLINK_CB(cb->skb).portid,
529 cb->nlh->nlmsg_seq, NLM_F_MULTI);
530 if (err)
531 goto out;
532 idx++;
533 }
534out:
535 mutex_unlock(&devlink_mutex);
536
537 cb->args[0] = idx;
538 return msg->len;
539}
540
541static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
542 struct genl_info *info)
543{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200544 struct devlink_port *devlink_port = info->user_ptr[0];
545 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100546 struct sk_buff *msg;
547 int err;
548
549 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
550 if (!msg)
551 return -ENOMEM;
552
553 err = devlink_nl_port_fill(msg, devlink, devlink_port,
554 DEVLINK_CMD_PORT_NEW,
555 info->snd_portid, info->snd_seq, 0);
556 if (err) {
557 nlmsg_free(msg);
558 return err;
559 }
560
561 return genlmsg_reply(msg, info);
562}
563
564static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
565 struct netlink_callback *cb)
566{
567 struct devlink *devlink;
568 struct devlink_port *devlink_port;
569 int start = cb->args[0];
570 int idx = 0;
571 int err;
572
573 mutex_lock(&devlink_mutex);
574 mutex_lock(&devlink_port_mutex);
575 list_for_each_entry(devlink, &devlink_list, list) {
576 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
577 continue;
578 list_for_each_entry(devlink_port, &devlink->port_list, list) {
579 if (idx < start) {
580 idx++;
581 continue;
582 }
583 err = devlink_nl_port_fill(msg, devlink, devlink_port,
584 DEVLINK_CMD_NEW,
585 NETLINK_CB(cb->skb).portid,
586 cb->nlh->nlmsg_seq,
587 NLM_F_MULTI);
588 if (err)
589 goto out;
590 idx++;
591 }
592 }
593out:
594 mutex_unlock(&devlink_port_mutex);
595 mutex_unlock(&devlink_mutex);
596
597 cb->args[0] = idx;
598 return msg->len;
599}
600
601static int devlink_port_type_set(struct devlink *devlink,
602 struct devlink_port *devlink_port,
603 enum devlink_port_type port_type)
604
605{
606 int err;
607
608 if (devlink->ops && devlink->ops->port_type_set) {
609 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
610 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200611 if (port_type == devlink_port->type)
612 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100613 err = devlink->ops->port_type_set(devlink_port, port_type);
614 if (err)
615 return err;
616 devlink_port->desired_type = port_type;
617 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
618 return 0;
619 }
620 return -EOPNOTSUPP;
621}
622
623static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
624 struct genl_info *info)
625{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200626 struct devlink_port *devlink_port = info->user_ptr[0];
627 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100628 int err;
629
630 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
631 enum devlink_port_type port_type;
632
633 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
634 err = devlink_port_type_set(devlink, devlink_port, port_type);
635 if (err)
636 return err;
637 }
638 return 0;
639}
640
641static int devlink_port_split(struct devlink *devlink,
642 u32 port_index, u32 count)
643
644{
645 if (devlink->ops && devlink->ops->port_split)
646 return devlink->ops->port_split(devlink, port_index, count);
647 return -EOPNOTSUPP;
648}
649
650static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
651 struct genl_info *info)
652{
653 struct devlink *devlink = info->user_ptr[0];
654 u32 port_index;
655 u32 count;
656
657 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
658 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
659 return -EINVAL;
660
661 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
662 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
663 return devlink_port_split(devlink, port_index, count);
664}
665
666static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
667
668{
669 if (devlink->ops && devlink->ops->port_unsplit)
670 return devlink->ops->port_unsplit(devlink, port_index);
671 return -EOPNOTSUPP;
672}
673
674static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
675 struct genl_info *info)
676{
677 struct devlink *devlink = info->user_ptr[0];
678 u32 port_index;
679
680 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
681 return -EINVAL;
682
683 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
684 return devlink_port_unsplit(devlink, port_index);
685}
686
Jiri Pirkobf797472016-04-14 18:19:13 +0200687static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
688 struct devlink_sb *devlink_sb,
689 enum devlink_command cmd, u32 portid,
690 u32 seq, int flags)
691{
692 void *hdr;
693
694 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
695 if (!hdr)
696 return -EMSGSIZE;
697
698 if (devlink_nl_put_handle(msg, devlink))
699 goto nla_put_failure;
700 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
701 goto nla_put_failure;
702 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
703 goto nla_put_failure;
704 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
705 devlink_sb->ingress_pools_count))
706 goto nla_put_failure;
707 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
708 devlink_sb->egress_pools_count))
709 goto nla_put_failure;
710 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
711 devlink_sb->ingress_tc_count))
712 goto nla_put_failure;
713 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
714 devlink_sb->egress_tc_count))
715 goto nla_put_failure;
716
717 genlmsg_end(msg, hdr);
718 return 0;
719
720nla_put_failure:
721 genlmsg_cancel(msg, hdr);
722 return -EMSGSIZE;
723}
724
725static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
726 struct genl_info *info)
727{
728 struct devlink *devlink = info->user_ptr[0];
729 struct devlink_sb *devlink_sb = info->user_ptr[1];
730 struct sk_buff *msg;
731 int err;
732
733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
734 if (!msg)
735 return -ENOMEM;
736
737 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
738 DEVLINK_CMD_SB_NEW,
739 info->snd_portid, info->snd_seq, 0);
740 if (err) {
741 nlmsg_free(msg);
742 return err;
743 }
744
745 return genlmsg_reply(msg, info);
746}
747
748static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
749 struct netlink_callback *cb)
750{
751 struct devlink *devlink;
752 struct devlink_sb *devlink_sb;
753 int start = cb->args[0];
754 int idx = 0;
755 int err;
756
757 mutex_lock(&devlink_mutex);
758 list_for_each_entry(devlink, &devlink_list, list) {
759 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
760 continue;
761 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
762 if (idx < start) {
763 idx++;
764 continue;
765 }
766 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
767 DEVLINK_CMD_SB_NEW,
768 NETLINK_CB(cb->skb).portid,
769 cb->nlh->nlmsg_seq,
770 NLM_F_MULTI);
771 if (err)
772 goto out;
773 idx++;
774 }
775 }
776out:
777 mutex_unlock(&devlink_mutex);
778
779 cb->args[0] = idx;
780 return msg->len;
781}
782
783static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
784 struct devlink_sb *devlink_sb,
785 u16 pool_index, enum devlink_command cmd,
786 u32 portid, u32 seq, int flags)
787{
788 struct devlink_sb_pool_info pool_info;
789 void *hdr;
790 int err;
791
792 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
793 pool_index, &pool_info);
794 if (err)
795 return err;
796
797 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
798 if (!hdr)
799 return -EMSGSIZE;
800
801 if (devlink_nl_put_handle(msg, devlink))
802 goto nla_put_failure;
803 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
804 goto nla_put_failure;
805 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
806 goto nla_put_failure;
807 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
808 goto nla_put_failure;
809 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
810 goto nla_put_failure;
811 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
812 pool_info.threshold_type))
813 goto nla_put_failure;
814
815 genlmsg_end(msg, hdr);
816 return 0;
817
818nla_put_failure:
819 genlmsg_cancel(msg, hdr);
820 return -EMSGSIZE;
821}
822
823static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
824 struct genl_info *info)
825{
826 struct devlink *devlink = info->user_ptr[0];
827 struct devlink_sb *devlink_sb = info->user_ptr[1];
828 struct sk_buff *msg;
829 u16 pool_index;
830 int err;
831
832 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
833 &pool_index);
834 if (err)
835 return err;
836
837 if (!devlink->ops || !devlink->ops->sb_pool_get)
838 return -EOPNOTSUPP;
839
840 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
841 if (!msg)
842 return -ENOMEM;
843
844 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
845 DEVLINK_CMD_SB_POOL_NEW,
846 info->snd_portid, info->snd_seq, 0);
847 if (err) {
848 nlmsg_free(msg);
849 return err;
850 }
851
852 return genlmsg_reply(msg, info);
853}
854
855static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
856 struct devlink *devlink,
857 struct devlink_sb *devlink_sb,
858 u32 portid, u32 seq)
859{
860 u16 pool_count = devlink_sb_pool_count(devlink_sb);
861 u16 pool_index;
862 int err;
863
864 for (pool_index = 0; pool_index < pool_count; pool_index++) {
865 if (*p_idx < start) {
866 (*p_idx)++;
867 continue;
868 }
869 err = devlink_nl_sb_pool_fill(msg, devlink,
870 devlink_sb,
871 pool_index,
872 DEVLINK_CMD_SB_POOL_NEW,
873 portid, seq, NLM_F_MULTI);
874 if (err)
875 return err;
876 (*p_idx)++;
877 }
878 return 0;
879}
880
881static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
882 struct netlink_callback *cb)
883{
884 struct devlink *devlink;
885 struct devlink_sb *devlink_sb;
886 int start = cb->args[0];
887 int idx = 0;
888 int err;
889
890 mutex_lock(&devlink_mutex);
891 list_for_each_entry(devlink, &devlink_list, list) {
892 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
893 !devlink->ops || !devlink->ops->sb_pool_get)
894 continue;
895 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
896 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
897 devlink_sb,
898 NETLINK_CB(cb->skb).portid,
899 cb->nlh->nlmsg_seq);
900 if (err && err != -EOPNOTSUPP)
901 goto out;
902 }
903 }
904out:
905 mutex_unlock(&devlink_mutex);
906
907 cb->args[0] = idx;
908 return msg->len;
909}
910
911static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
912 u16 pool_index, u32 size,
913 enum devlink_sb_threshold_type threshold_type)
914
915{
916 const struct devlink_ops *ops = devlink->ops;
917
918 if (ops && ops->sb_pool_set)
919 return ops->sb_pool_set(devlink, sb_index, pool_index,
920 size, threshold_type);
921 return -EOPNOTSUPP;
922}
923
924static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
925 struct genl_info *info)
926{
927 struct devlink *devlink = info->user_ptr[0];
928 struct devlink_sb *devlink_sb = info->user_ptr[1];
929 enum devlink_sb_threshold_type threshold_type;
930 u16 pool_index;
931 u32 size;
932 int err;
933
934 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
935 &pool_index);
936 if (err)
937 return err;
938
939 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
940 if (err)
941 return err;
942
943 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
944 return -EINVAL;
945
946 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
947 return devlink_sb_pool_set(devlink, devlink_sb->index,
948 pool_index, size, threshold_type);
949}
950
951static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
952 struct devlink *devlink,
953 struct devlink_port *devlink_port,
954 struct devlink_sb *devlink_sb,
955 u16 pool_index,
956 enum devlink_command cmd,
957 u32 portid, u32 seq, int flags)
958{
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200959 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +0200960 u32 threshold;
961 void *hdr;
962 int err;
963
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200964 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
965 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +0200966 if (err)
967 return err;
968
969 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
970 if (!hdr)
971 return -EMSGSIZE;
972
973 if (devlink_nl_put_handle(msg, devlink))
974 goto nla_put_failure;
975 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
976 goto nla_put_failure;
977 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
978 goto nla_put_failure;
979 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
980 goto nla_put_failure;
981 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
982 goto nla_put_failure;
983
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200984 if (ops->sb_occ_port_pool_get) {
985 u32 cur;
986 u32 max;
987
988 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
989 pool_index, &cur, &max);
990 if (err && err != -EOPNOTSUPP)
991 return err;
992 if (!err) {
993 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
994 goto nla_put_failure;
995 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
996 goto nla_put_failure;
997 }
998 }
999
Jiri Pirkobf797472016-04-14 18:19:13 +02001000 genlmsg_end(msg, hdr);
1001 return 0;
1002
1003nla_put_failure:
1004 genlmsg_cancel(msg, hdr);
1005 return -EMSGSIZE;
1006}
1007
1008static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1009 struct genl_info *info)
1010{
1011 struct devlink_port *devlink_port = info->user_ptr[0];
1012 struct devlink *devlink = devlink_port->devlink;
1013 struct devlink_sb *devlink_sb = info->user_ptr[1];
1014 struct sk_buff *msg;
1015 u16 pool_index;
1016 int err;
1017
1018 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1019 &pool_index);
1020 if (err)
1021 return err;
1022
1023 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1024 return -EOPNOTSUPP;
1025
1026 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1027 if (!msg)
1028 return -ENOMEM;
1029
1030 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1031 devlink_sb, pool_index,
1032 DEVLINK_CMD_SB_PORT_POOL_NEW,
1033 info->snd_portid, info->snd_seq, 0);
1034 if (err) {
1035 nlmsg_free(msg);
1036 return err;
1037 }
1038
1039 return genlmsg_reply(msg, info);
1040}
1041
1042static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1043 struct devlink *devlink,
1044 struct devlink_sb *devlink_sb,
1045 u32 portid, u32 seq)
1046{
1047 struct devlink_port *devlink_port;
1048 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1049 u16 pool_index;
1050 int err;
1051
1052 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1053 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1054 if (*p_idx < start) {
1055 (*p_idx)++;
1056 continue;
1057 }
1058 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1059 devlink_port,
1060 devlink_sb,
1061 pool_index,
1062 DEVLINK_CMD_SB_PORT_POOL_NEW,
1063 portid, seq,
1064 NLM_F_MULTI);
1065 if (err)
1066 return err;
1067 (*p_idx)++;
1068 }
1069 }
1070 return 0;
1071}
1072
1073static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1074 struct netlink_callback *cb)
1075{
1076 struct devlink *devlink;
1077 struct devlink_sb *devlink_sb;
1078 int start = cb->args[0];
1079 int idx = 0;
1080 int err;
1081
1082 mutex_lock(&devlink_mutex);
1083 mutex_lock(&devlink_port_mutex);
1084 list_for_each_entry(devlink, &devlink_list, list) {
1085 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1086 !devlink->ops || !devlink->ops->sb_port_pool_get)
1087 continue;
1088 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1089 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1090 devlink, devlink_sb,
1091 NETLINK_CB(cb->skb).portid,
1092 cb->nlh->nlmsg_seq);
1093 if (err && err != -EOPNOTSUPP)
1094 goto out;
1095 }
1096 }
1097out:
1098 mutex_unlock(&devlink_port_mutex);
1099 mutex_unlock(&devlink_mutex);
1100
1101 cb->args[0] = idx;
1102 return msg->len;
1103}
1104
1105static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1106 unsigned int sb_index, u16 pool_index,
1107 u32 threshold)
1108
1109{
1110 const struct devlink_ops *ops = devlink_port->devlink->ops;
1111
1112 if (ops && ops->sb_port_pool_set)
1113 return ops->sb_port_pool_set(devlink_port, sb_index,
1114 pool_index, threshold);
1115 return -EOPNOTSUPP;
1116}
1117
1118static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1119 struct genl_info *info)
1120{
1121 struct devlink_port *devlink_port = info->user_ptr[0];
1122 struct devlink_sb *devlink_sb = info->user_ptr[1];
1123 u16 pool_index;
1124 u32 threshold;
1125 int err;
1126
1127 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1128 &pool_index);
1129 if (err)
1130 return err;
1131
1132 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1133 return -EINVAL;
1134
1135 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1136 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1137 pool_index, threshold);
1138}
1139
1140static int
1141devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1142 struct devlink_port *devlink_port,
1143 struct devlink_sb *devlink_sb, u16 tc_index,
1144 enum devlink_sb_pool_type pool_type,
1145 enum devlink_command cmd,
1146 u32 portid, u32 seq, int flags)
1147{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001148 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001149 u16 pool_index;
1150 u32 threshold;
1151 void *hdr;
1152 int err;
1153
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001154 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1155 tc_index, pool_type,
1156 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001157 if (err)
1158 return err;
1159
1160 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1161 if (!hdr)
1162 return -EMSGSIZE;
1163
1164 if (devlink_nl_put_handle(msg, devlink))
1165 goto nla_put_failure;
1166 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1167 goto nla_put_failure;
1168 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1169 goto nla_put_failure;
1170 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1171 goto nla_put_failure;
1172 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1173 goto nla_put_failure;
1174 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1175 goto nla_put_failure;
1176 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1177 goto nla_put_failure;
1178
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001179 if (ops->sb_occ_tc_port_bind_get) {
1180 u32 cur;
1181 u32 max;
1182
1183 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1184 devlink_sb->index,
1185 tc_index, pool_type,
1186 &cur, &max);
1187 if (err && err != -EOPNOTSUPP)
1188 return err;
1189 if (!err) {
1190 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1191 goto nla_put_failure;
1192 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1193 goto nla_put_failure;
1194 }
1195 }
1196
Jiri Pirkobf797472016-04-14 18:19:13 +02001197 genlmsg_end(msg, hdr);
1198 return 0;
1199
1200nla_put_failure:
1201 genlmsg_cancel(msg, hdr);
1202 return -EMSGSIZE;
1203}
1204
1205static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1206 struct genl_info *info)
1207{
1208 struct devlink_port *devlink_port = info->user_ptr[0];
1209 struct devlink *devlink = devlink_port->devlink;
1210 struct devlink_sb *devlink_sb = info->user_ptr[1];
1211 struct sk_buff *msg;
1212 enum devlink_sb_pool_type pool_type;
1213 u16 tc_index;
1214 int err;
1215
1216 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1217 if (err)
1218 return err;
1219
1220 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1221 pool_type, &tc_index);
1222 if (err)
1223 return err;
1224
1225 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1226 return -EOPNOTSUPP;
1227
1228 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1229 if (!msg)
1230 return -ENOMEM;
1231
1232 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1233 devlink_sb, tc_index, pool_type,
1234 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1235 info->snd_portid,
1236 info->snd_seq, 0);
1237 if (err) {
1238 nlmsg_free(msg);
1239 return err;
1240 }
1241
1242 return genlmsg_reply(msg, info);
1243}
1244
1245static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1246 int start, int *p_idx,
1247 struct devlink *devlink,
1248 struct devlink_sb *devlink_sb,
1249 u32 portid, u32 seq)
1250{
1251 struct devlink_port *devlink_port;
1252 u16 tc_index;
1253 int err;
1254
1255 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1256 for (tc_index = 0;
1257 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1258 if (*p_idx < start) {
1259 (*p_idx)++;
1260 continue;
1261 }
1262 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1263 devlink_port,
1264 devlink_sb,
1265 tc_index,
1266 DEVLINK_SB_POOL_TYPE_INGRESS,
1267 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1268 portid, seq,
1269 NLM_F_MULTI);
1270 if (err)
1271 return err;
1272 (*p_idx)++;
1273 }
1274 for (tc_index = 0;
1275 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1276 if (*p_idx < start) {
1277 (*p_idx)++;
1278 continue;
1279 }
1280 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1281 devlink_port,
1282 devlink_sb,
1283 tc_index,
1284 DEVLINK_SB_POOL_TYPE_EGRESS,
1285 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1286 portid, seq,
1287 NLM_F_MULTI);
1288 if (err)
1289 return err;
1290 (*p_idx)++;
1291 }
1292 }
1293 return 0;
1294}
1295
1296static int
1297devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1298 struct netlink_callback *cb)
1299{
1300 struct devlink *devlink;
1301 struct devlink_sb *devlink_sb;
1302 int start = cb->args[0];
1303 int idx = 0;
1304 int err;
1305
1306 mutex_lock(&devlink_mutex);
1307 mutex_lock(&devlink_port_mutex);
1308 list_for_each_entry(devlink, &devlink_list, list) {
1309 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1310 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1311 continue;
1312 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1313 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1314 devlink,
1315 devlink_sb,
1316 NETLINK_CB(cb->skb).portid,
1317 cb->nlh->nlmsg_seq);
1318 if (err && err != -EOPNOTSUPP)
1319 goto out;
1320 }
1321 }
1322out:
1323 mutex_unlock(&devlink_port_mutex);
1324 mutex_unlock(&devlink_mutex);
1325
1326 cb->args[0] = idx;
1327 return msg->len;
1328}
1329
1330static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1331 unsigned int sb_index, u16 tc_index,
1332 enum devlink_sb_pool_type pool_type,
1333 u16 pool_index, u32 threshold)
1334
1335{
1336 const struct devlink_ops *ops = devlink_port->devlink->ops;
1337
1338 if (ops && ops->sb_tc_pool_bind_set)
1339 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1340 tc_index, pool_type,
1341 pool_index, threshold);
1342 return -EOPNOTSUPP;
1343}
1344
1345static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1346 struct genl_info *info)
1347{
1348 struct devlink_port *devlink_port = info->user_ptr[0];
1349 struct devlink_sb *devlink_sb = info->user_ptr[1];
1350 enum devlink_sb_pool_type pool_type;
1351 u16 tc_index;
1352 u16 pool_index;
1353 u32 threshold;
1354 int err;
1355
1356 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1357 if (err)
1358 return err;
1359
1360 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1361 pool_type, &tc_index);
1362 if (err)
1363 return err;
1364
1365 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1366 &pool_index);
1367 if (err)
1368 return err;
1369
1370 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1371 return -EINVAL;
1372
1373 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1374 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1375 tc_index, pool_type,
1376 pool_index, threshold);
1377}
1378
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001379static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1380 struct genl_info *info)
1381{
1382 struct devlink *devlink = info->user_ptr[0];
1383 struct devlink_sb *devlink_sb = info->user_ptr[1];
1384 const struct devlink_ops *ops = devlink->ops;
1385
1386 if (ops && ops->sb_occ_snapshot)
1387 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1388 return -EOPNOTSUPP;
1389}
1390
1391static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1392 struct genl_info *info)
1393{
1394 struct devlink *devlink = info->user_ptr[0];
1395 struct devlink_sb *devlink_sb = info->user_ptr[1];
1396 const struct devlink_ops *ops = devlink->ops;
1397
1398 if (ops && ops->sb_occ_max_clear)
1399 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1400 return -EOPNOTSUPP;
1401}
1402
Or Gerlitz08f4b592016-07-01 14:51:01 +03001403static int devlink_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1404 enum devlink_command cmd, u32 portid,
1405 u32 seq, int flags, u16 mode)
1406{
1407 void *hdr;
1408
1409 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1410 if (!hdr)
1411 return -EMSGSIZE;
1412
1413 if (devlink_nl_put_handle(msg, devlink))
1414 goto nla_put_failure;
1415
1416 if (nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode))
1417 goto nla_put_failure;
1418
1419 genlmsg_end(msg, hdr);
1420 return 0;
1421
1422nla_put_failure:
1423 genlmsg_cancel(msg, hdr);
1424 return -EMSGSIZE;
1425}
1426
1427static int devlink_nl_cmd_eswitch_mode_get_doit(struct sk_buff *skb,
1428 struct genl_info *info)
1429{
1430 struct devlink *devlink = info->user_ptr[0];
1431 const struct devlink_ops *ops = devlink->ops;
1432 struct sk_buff *msg;
1433 u16 mode;
1434 int err;
1435
1436 if (!ops || !ops->eswitch_mode_get)
1437 return -EOPNOTSUPP;
1438
1439 err = ops->eswitch_mode_get(devlink, &mode);
1440 if (err)
1441 return err;
1442
1443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1444 if (!msg)
1445 return -ENOMEM;
1446
1447 err = devlink_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_MODE_GET,
1448 info->snd_portid, info->snd_seq, 0, mode);
1449
1450 if (err) {
1451 nlmsg_free(msg);
1452 return err;
1453 }
1454
1455 return genlmsg_reply(msg, info);
1456}
1457
1458static int devlink_nl_cmd_eswitch_mode_set_doit(struct sk_buff *skb,
1459 struct genl_info *info)
1460{
1461 struct devlink *devlink = info->user_ptr[0];
1462 const struct devlink_ops *ops = devlink->ops;
1463 u16 mode;
1464
1465 if (!info->attrs[DEVLINK_ATTR_ESWITCH_MODE])
1466 return -EINVAL;
1467
1468 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1469
1470 if (ops && ops->eswitch_mode_set)
1471 return ops->eswitch_mode_set(devlink, mode);
1472 return -EOPNOTSUPP;
1473}
1474
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001475static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
1476 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
1477 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
1478 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
1479 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
1480 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02001481 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
1482 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
1483 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
1484 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
1485 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
1486 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
1487 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03001488 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001489};
1490
1491static const struct genl_ops devlink_nl_ops[] = {
1492 {
1493 .cmd = DEVLINK_CMD_GET,
1494 .doit = devlink_nl_cmd_get_doit,
1495 .dumpit = devlink_nl_cmd_get_dumpit,
1496 .policy = devlink_nl_policy,
Jiri Pirko1fc22572016-04-08 19:12:48 +02001497 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001498 /* can be retrieved by unprivileged users */
1499 },
1500 {
1501 .cmd = DEVLINK_CMD_PORT_GET,
1502 .doit = devlink_nl_cmd_port_get_doit,
1503 .dumpit = devlink_nl_cmd_port_get_dumpit,
1504 .policy = devlink_nl_policy,
1505 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
1506 /* can be retrieved by unprivileged users */
1507 },
1508 {
1509 .cmd = DEVLINK_CMD_PORT_SET,
1510 .doit = devlink_nl_cmd_port_set_doit,
1511 .policy = devlink_nl_policy,
1512 .flags = GENL_ADMIN_PERM,
1513 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
1514 },
1515 {
1516 .cmd = DEVLINK_CMD_PORT_SPLIT,
1517 .doit = devlink_nl_cmd_port_split_doit,
1518 .policy = devlink_nl_policy,
1519 .flags = GENL_ADMIN_PERM,
Jiri Pirko1fc22572016-04-08 19:12:48 +02001520 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001521 },
1522 {
1523 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
1524 .doit = devlink_nl_cmd_port_unsplit_doit,
1525 .policy = devlink_nl_policy,
1526 .flags = GENL_ADMIN_PERM,
Jiri Pirko1fc22572016-04-08 19:12:48 +02001527 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001528 },
Jiri Pirkobf797472016-04-14 18:19:13 +02001529 {
1530 .cmd = DEVLINK_CMD_SB_GET,
1531 .doit = devlink_nl_cmd_sb_get_doit,
1532 .dumpit = devlink_nl_cmd_sb_get_dumpit,
1533 .policy = devlink_nl_policy,
1534 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
1535 DEVLINK_NL_FLAG_NEED_SB,
1536 /* can be retrieved by unprivileged users */
1537 },
1538 {
1539 .cmd = DEVLINK_CMD_SB_POOL_GET,
1540 .doit = devlink_nl_cmd_sb_pool_get_doit,
1541 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
1542 .policy = devlink_nl_policy,
1543 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
1544 DEVLINK_NL_FLAG_NEED_SB,
1545 /* can be retrieved by unprivileged users */
1546 },
1547 {
1548 .cmd = DEVLINK_CMD_SB_POOL_SET,
1549 .doit = devlink_nl_cmd_sb_pool_set_doit,
1550 .policy = devlink_nl_policy,
1551 .flags = GENL_ADMIN_PERM,
1552 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
1553 DEVLINK_NL_FLAG_NEED_SB,
1554 },
1555 {
1556 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
1557 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
1558 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
1559 .policy = devlink_nl_policy,
1560 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
1561 DEVLINK_NL_FLAG_NEED_SB,
1562 /* can be retrieved by unprivileged users */
1563 },
1564 {
1565 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
1566 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
1567 .policy = devlink_nl_policy,
1568 .flags = GENL_ADMIN_PERM,
1569 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
1570 DEVLINK_NL_FLAG_NEED_SB,
1571 },
1572 {
1573 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
1574 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
1575 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
1576 .policy = devlink_nl_policy,
1577 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
1578 DEVLINK_NL_FLAG_NEED_SB,
1579 /* can be retrieved by unprivileged users */
1580 },
1581 {
1582 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
1583 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
1584 .policy = devlink_nl_policy,
1585 .flags = GENL_ADMIN_PERM,
1586 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
1587 DEVLINK_NL_FLAG_NEED_SB,
1588 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001589 {
1590 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
1591 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
1592 .policy = devlink_nl_policy,
1593 .flags = GENL_ADMIN_PERM,
1594 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
1595 DEVLINK_NL_FLAG_NEED_SB |
1596 DEVLINK_NL_FLAG_LOCK_PORTS,
1597 },
1598 {
1599 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
1600 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
1601 .policy = devlink_nl_policy,
1602 .flags = GENL_ADMIN_PERM,
1603 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
1604 DEVLINK_NL_FLAG_NEED_SB |
1605 DEVLINK_NL_FLAG_LOCK_PORTS,
1606 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03001607 {
1608 .cmd = DEVLINK_CMD_ESWITCH_MODE_GET,
1609 .doit = devlink_nl_cmd_eswitch_mode_get_doit,
1610 .policy = devlink_nl_policy,
1611 .flags = GENL_ADMIN_PERM,
1612 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
1613 },
1614 {
1615 .cmd = DEVLINK_CMD_ESWITCH_MODE_SET,
1616 .doit = devlink_nl_cmd_eswitch_mode_set_doit,
1617 .policy = devlink_nl_policy,
1618 .flags = GENL_ADMIN_PERM,
1619 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
1620 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001621};
1622
1623/**
1624 * devlink_alloc - Allocate new devlink instance resources
1625 *
1626 * @ops: ops
1627 * @priv_size: size of user private data
1628 *
1629 * Allocate new devlink instance resources, including devlink index
1630 * and name.
1631 */
1632struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
1633{
1634 struct devlink *devlink;
1635
1636 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
1637 if (!devlink)
1638 return NULL;
1639 devlink->ops = ops;
1640 devlink_net_set(devlink, &init_net);
1641 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02001642 INIT_LIST_HEAD(&devlink->sb_list);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001643 return devlink;
1644}
1645EXPORT_SYMBOL_GPL(devlink_alloc);
1646
1647/**
1648 * devlink_register - Register devlink instance
1649 *
1650 * @devlink: devlink
1651 */
1652int devlink_register(struct devlink *devlink, struct device *dev)
1653{
1654 mutex_lock(&devlink_mutex);
1655 devlink->dev = dev;
1656 list_add_tail(&devlink->list, &devlink_list);
1657 devlink_notify(devlink, DEVLINK_CMD_NEW);
1658 mutex_unlock(&devlink_mutex);
1659 return 0;
1660}
1661EXPORT_SYMBOL_GPL(devlink_register);
1662
1663/**
1664 * devlink_unregister - Unregister devlink instance
1665 *
1666 * @devlink: devlink
1667 */
1668void devlink_unregister(struct devlink *devlink)
1669{
1670 mutex_lock(&devlink_mutex);
1671 devlink_notify(devlink, DEVLINK_CMD_DEL);
1672 list_del(&devlink->list);
1673 mutex_unlock(&devlink_mutex);
1674}
1675EXPORT_SYMBOL_GPL(devlink_unregister);
1676
1677/**
1678 * devlink_free - Free devlink instance resources
1679 *
1680 * @devlink: devlink
1681 */
1682void devlink_free(struct devlink *devlink)
1683{
1684 kfree(devlink);
1685}
1686EXPORT_SYMBOL_GPL(devlink_free);
1687
1688/**
1689 * devlink_port_register - Register devlink port
1690 *
1691 * @devlink: devlink
1692 * @devlink_port: devlink port
1693 * @port_index
1694 *
1695 * Register devlink port with provided port index. User can use
1696 * any indexing, even hw-related one. devlink_port structure
1697 * is convenient to be embedded inside user driver private structure.
1698 * Note that the caller should take care of zeroing the devlink_port
1699 * structure.
1700 */
1701int devlink_port_register(struct devlink *devlink,
1702 struct devlink_port *devlink_port,
1703 unsigned int port_index)
1704{
1705 mutex_lock(&devlink_port_mutex);
1706 if (devlink_port_index_exists(devlink, port_index)) {
1707 mutex_unlock(&devlink_port_mutex);
1708 return -EEXIST;
1709 }
1710 devlink_port->devlink = devlink;
1711 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001712 devlink_port->registered = true;
1713 list_add_tail(&devlink_port->list, &devlink->port_list);
1714 mutex_unlock(&devlink_port_mutex);
1715 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(devlink_port_register);
1719
1720/**
1721 * devlink_port_unregister - Unregister devlink port
1722 *
1723 * @devlink_port: devlink port
1724 */
1725void devlink_port_unregister(struct devlink_port *devlink_port)
1726{
1727 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
1728 mutex_lock(&devlink_port_mutex);
1729 list_del(&devlink_port->list);
1730 mutex_unlock(&devlink_port_mutex);
1731}
1732EXPORT_SYMBOL_GPL(devlink_port_unregister);
1733
1734static void __devlink_port_type_set(struct devlink_port *devlink_port,
1735 enum devlink_port_type type,
1736 void *type_dev)
1737{
1738 devlink_port->type = type;
1739 devlink_port->type_dev = type_dev;
1740 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1741}
1742
1743/**
1744 * devlink_port_type_eth_set - Set port type to Ethernet
1745 *
1746 * @devlink_port: devlink port
1747 * @netdev: related netdevice
1748 */
1749void devlink_port_type_eth_set(struct devlink_port *devlink_port,
1750 struct net_device *netdev)
1751{
1752 return __devlink_port_type_set(devlink_port,
1753 DEVLINK_PORT_TYPE_ETH, netdev);
1754}
1755EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
1756
1757/**
1758 * devlink_port_type_ib_set - Set port type to InfiniBand
1759 *
1760 * @devlink_port: devlink port
1761 * @ibdev: related IB device
1762 */
1763void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1764 struct ib_device *ibdev)
1765{
1766 return __devlink_port_type_set(devlink_port,
1767 DEVLINK_PORT_TYPE_IB, ibdev);
1768}
1769EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
1770
1771/**
1772 * devlink_port_type_clear - Clear port type
1773 *
1774 * @devlink_port: devlink port
1775 */
1776void devlink_port_type_clear(struct devlink_port *devlink_port)
1777{
1778 return __devlink_port_type_set(devlink_port,
1779 DEVLINK_PORT_TYPE_NOTSET, NULL);
1780}
1781EXPORT_SYMBOL_GPL(devlink_port_type_clear);
1782
1783/**
1784 * devlink_port_split_set - Set port is split
1785 *
1786 * @devlink_port: devlink port
1787 * @split_group: split group - identifies group split port is part of
1788 */
1789void devlink_port_split_set(struct devlink_port *devlink_port,
1790 u32 split_group)
1791{
1792 devlink_port->split = true;
1793 devlink_port->split_group = split_group;
1794 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1795}
1796EXPORT_SYMBOL_GPL(devlink_port_split_set);
1797
Jiri Pirkobf797472016-04-14 18:19:13 +02001798int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
1799 u32 size, u16 ingress_pools_count,
1800 u16 egress_pools_count, u16 ingress_tc_count,
1801 u16 egress_tc_count)
1802{
1803 struct devlink_sb *devlink_sb;
1804 int err = 0;
1805
1806 mutex_lock(&devlink_mutex);
1807 if (devlink_sb_index_exists(devlink, sb_index)) {
1808 err = -EEXIST;
1809 goto unlock;
1810 }
1811
1812 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
1813 if (!devlink_sb) {
1814 err = -ENOMEM;
1815 goto unlock;
1816 }
1817 devlink_sb->index = sb_index;
1818 devlink_sb->size = size;
1819 devlink_sb->ingress_pools_count = ingress_pools_count;
1820 devlink_sb->egress_pools_count = egress_pools_count;
1821 devlink_sb->ingress_tc_count = ingress_tc_count;
1822 devlink_sb->egress_tc_count = egress_tc_count;
1823 list_add_tail(&devlink_sb->list, &devlink->sb_list);
1824unlock:
1825 mutex_unlock(&devlink_mutex);
1826 return err;
1827}
1828EXPORT_SYMBOL_GPL(devlink_sb_register);
1829
1830void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
1831{
1832 struct devlink_sb *devlink_sb;
1833
1834 mutex_lock(&devlink_mutex);
1835 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
1836 WARN_ON(!devlink_sb);
1837 list_del(&devlink_sb->list);
1838 mutex_unlock(&devlink_mutex);
1839 kfree(devlink_sb);
1840}
1841EXPORT_SYMBOL_GPL(devlink_sb_unregister);
1842
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001843static int __init devlink_module_init(void)
1844{
1845 return genl_register_family_with_ops_groups(&devlink_nl_family,
1846 devlink_nl_ops,
1847 devlink_nl_mcgrps);
1848}
1849
1850static void __exit devlink_module_exit(void)
1851{
1852 genl_unregister_family(&devlink_nl_family);
1853}
1854
1855module_init(devlink_module_init);
1856module_exit(devlink_module_exit);
1857
1858MODULE_LICENSE("GPL v2");
1859MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1860MODULE_DESCRIPTION("Network physical device Netlink interface");
1861MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);