Linux 4.19.133
[linux/fpc-iii.git] / net / core / devlink.c
bloba77e3777c8dd3bc6f67911ad125e0293e6bbca9b
1 /*
2 * net/core/devlink.c - Network physical/parent device Netlink interface
4 * Heavily inspired by net/wireless/
5 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
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.
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>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/devlink.h>
32 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
34 .name = "destination mac",
35 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
36 .bitwidth = 48,
40 struct 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,
47 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
49 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
51 .name = "destination ip",
52 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
53 .bitwidth = 32,
57 struct 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,
64 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
66 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
68 .name = "destination ip",
69 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
70 .bitwidth = 128,
74 struct 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,
81 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
85 static LIST_HEAD(devlink_list);
87 /* devlink_mutex
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.
93 static DEFINE_MUTEX(devlink_mutex);
95 static struct net *devlink_net(const struct devlink *devlink)
97 return read_pnet(&devlink->_net);
100 static void devlink_net_set(struct devlink *devlink, struct net *net)
102 write_pnet(&devlink->_net, net);
105 static struct devlink *devlink_get_from_attrs(struct net *net,
106 struct nlattr **attrs)
108 struct devlink *devlink;
109 char *busname;
110 char *devname;
112 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
113 return ERR_PTR(-EINVAL);
115 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
116 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
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;
125 return ERR_PTR(-ENODEV);
128 static struct devlink *devlink_get_from_info(struct genl_info *info)
130 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
133 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
134 int port_index)
136 struct devlink_port *devlink_port;
138 list_for_each_entry(devlink_port, &devlink->port_list, list) {
139 if (devlink_port->index == port_index)
140 return devlink_port;
142 return NULL;
145 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
147 return devlink_port_get_by_index(devlink, port_index);
150 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
151 struct nlattr **attrs)
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;
157 devlink_port = devlink_port_get_by_index(devlink, port_index);
158 if (!devlink_port)
159 return ERR_PTR(-ENODEV);
160 return devlink_port;
162 return ERR_PTR(-EINVAL);
165 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
166 struct genl_info *info)
168 return devlink_port_get_from_attrs(devlink, info->attrs);
171 struct 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;
181 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
183 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
186 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
187 unsigned int sb_index)
189 struct devlink_sb *devlink_sb;
191 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
192 if (devlink_sb->index == sb_index)
193 return devlink_sb;
195 return NULL;
198 static bool devlink_sb_index_exists(struct devlink *devlink,
199 unsigned int sb_index)
201 return devlink_sb_get_by_index(devlink, sb_index);
204 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
205 struct nlattr **attrs)
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;
211 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
212 if (!devlink_sb)
213 return ERR_PTR(-ENODEV);
214 return devlink_sb;
216 return ERR_PTR(-EINVAL);
219 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
220 struct genl_info *info)
222 return devlink_sb_get_from_attrs(devlink, info->attrs);
225 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
226 struct nlattr **attrs,
227 u16 *p_pool_index)
229 u16 val;
231 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
232 return -EINVAL;
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;
241 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
242 struct genl_info *info,
243 u16 *p_pool_index)
245 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
246 p_pool_index);
249 static int
250 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
251 enum devlink_sb_pool_type *p_pool_type)
253 u8 val;
255 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
256 return -EINVAL;
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;
266 static int
267 devlink_sb_pool_type_get_from_info(struct genl_info *info,
268 enum devlink_sb_pool_type *p_pool_type)
270 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
273 static int
274 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
275 enum devlink_sb_threshold_type *p_th_type)
277 u8 val;
279 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
280 return -EINVAL;
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;
290 static int
291 devlink_sb_th_type_get_from_info(struct genl_info *info,
292 enum devlink_sb_threshold_type *p_th_type)
294 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
297 static int
298 devlink_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)
303 u16 val;
305 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
306 return -EINVAL;
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;
319 static int
320 devlink_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)
325 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
326 pool_type, p_tc_index);
329 struct devlink_region {
330 struct devlink *devlink;
331 struct list_head list;
332 const char *name;
333 struct list_head snapshot_list;
334 u32 max_snapshots;
335 u32 cur_snapshots;
336 u64 size;
339 struct devlink_snapshot {
340 struct list_head list;
341 struct devlink_region *region;
342 devlink_snapshot_data_dest_t *data_destructor;
343 u64 data_len;
344 u8 *data;
345 u32 id;
348 static struct devlink_region *
349 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
351 struct devlink_region *region;
353 list_for_each_entry(region, &devlink->region_list, list)
354 if (!strcmp(region->name, region_name))
355 return region;
357 return NULL;
360 static struct devlink_snapshot *
361 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
363 struct devlink_snapshot *snapshot;
365 list_for_each_entry(snapshot, &region->snapshot_list, list)
366 if (snapshot->id == id)
367 return snapshot;
369 return NULL;
372 static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
374 snapshot->region->cur_snapshots--;
375 list_del(&snapshot->list);
376 (*snapshot->data_destructor)(snapshot->data);
377 kfree(snapshot);
380 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
381 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
382 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
384 /* The per devlink instance lock is taken by default in the pre-doit
385 * operation, yet several commands do not require this. The global
386 * devlink lock is taken and protects from disruption by user-calls.
388 #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
390 static int devlink_nl_pre_doit(const struct genl_ops *ops,
391 struct sk_buff *skb, struct genl_info *info)
393 struct devlink *devlink;
394 int err;
396 mutex_lock(&devlink_mutex);
397 devlink = devlink_get_from_info(info);
398 if (IS_ERR(devlink)) {
399 mutex_unlock(&devlink_mutex);
400 return PTR_ERR(devlink);
402 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
403 mutex_lock(&devlink->lock);
404 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
405 info->user_ptr[0] = devlink;
406 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
407 struct devlink_port *devlink_port;
409 devlink_port = devlink_port_get_from_info(devlink, info);
410 if (IS_ERR(devlink_port)) {
411 err = PTR_ERR(devlink_port);
412 goto unlock;
414 info->user_ptr[0] = devlink_port;
416 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
417 struct devlink_sb *devlink_sb;
419 devlink_sb = devlink_sb_get_from_info(devlink, info);
420 if (IS_ERR(devlink_sb)) {
421 err = PTR_ERR(devlink_sb);
422 goto unlock;
424 info->user_ptr[1] = devlink_sb;
426 return 0;
428 unlock:
429 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
430 mutex_unlock(&devlink->lock);
431 mutex_unlock(&devlink_mutex);
432 return err;
435 static void devlink_nl_post_doit(const struct genl_ops *ops,
436 struct sk_buff *skb, struct genl_info *info)
438 struct devlink *devlink;
440 devlink = devlink_get_from_info(info);
441 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
442 mutex_unlock(&devlink->lock);
443 mutex_unlock(&devlink_mutex);
446 static struct genl_family devlink_nl_family;
448 enum devlink_multicast_groups {
449 DEVLINK_MCGRP_CONFIG,
452 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
453 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
456 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
458 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
459 return -EMSGSIZE;
460 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
461 return -EMSGSIZE;
462 return 0;
465 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
466 enum devlink_command cmd, u32 portid,
467 u32 seq, int flags)
469 void *hdr;
471 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
472 if (!hdr)
473 return -EMSGSIZE;
475 if (devlink_nl_put_handle(msg, devlink))
476 goto nla_put_failure;
478 genlmsg_end(msg, hdr);
479 return 0;
481 nla_put_failure:
482 genlmsg_cancel(msg, hdr);
483 return -EMSGSIZE;
486 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
488 struct sk_buff *msg;
489 int err;
491 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
493 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
494 if (!msg)
495 return;
497 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
498 if (err) {
499 nlmsg_free(msg);
500 return;
503 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
504 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
507 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
508 struct devlink_port *devlink_port)
510 struct devlink_port_attrs *attrs = &devlink_port->attrs;
512 if (!attrs->set)
513 return 0;
514 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
515 return -EMSGSIZE;
516 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
517 return -EMSGSIZE;
518 if (!attrs->split)
519 return 0;
520 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
521 return -EMSGSIZE;
522 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
523 attrs->split_subport_number))
524 return -EMSGSIZE;
525 return 0;
528 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
529 struct devlink_port *devlink_port,
530 enum devlink_command cmd, u32 portid,
531 u32 seq, int flags)
533 void *hdr;
535 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
536 if (!hdr)
537 return -EMSGSIZE;
539 if (devlink_nl_put_handle(msg, devlink))
540 goto nla_put_failure;
541 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
542 goto nla_put_failure;
543 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
544 goto nla_put_failure;
545 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
546 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
547 devlink_port->desired_type))
548 goto nla_put_failure;
549 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
550 struct net_device *netdev = devlink_port->type_dev;
552 if (netdev &&
553 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
554 netdev->ifindex) ||
555 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
556 netdev->name)))
557 goto nla_put_failure;
559 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
560 struct ib_device *ibdev = devlink_port->type_dev;
562 if (ibdev &&
563 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
564 ibdev->name))
565 goto nla_put_failure;
567 if (devlink_nl_port_attrs_put(msg, devlink_port))
568 goto nla_put_failure;
570 genlmsg_end(msg, hdr);
571 return 0;
573 nla_put_failure:
574 genlmsg_cancel(msg, hdr);
575 return -EMSGSIZE;
578 static void devlink_port_notify(struct devlink_port *devlink_port,
579 enum devlink_command cmd)
581 struct devlink *devlink = devlink_port->devlink;
582 struct sk_buff *msg;
583 int err;
585 if (!devlink_port->registered)
586 return;
588 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
591 if (!msg)
592 return;
594 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
595 if (err) {
596 nlmsg_free(msg);
597 return;
600 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
601 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
604 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
606 struct devlink *devlink = info->user_ptr[0];
607 struct sk_buff *msg;
608 int err;
610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
611 if (!msg)
612 return -ENOMEM;
614 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
615 info->snd_portid, info->snd_seq, 0);
616 if (err) {
617 nlmsg_free(msg);
618 return err;
621 return genlmsg_reply(msg, info);
624 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
625 struct netlink_callback *cb)
627 struct devlink *devlink;
628 int start = cb->args[0];
629 int idx = 0;
630 int err;
632 mutex_lock(&devlink_mutex);
633 list_for_each_entry(devlink, &devlink_list, list) {
634 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
635 continue;
636 if (idx < start) {
637 idx++;
638 continue;
640 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
641 NETLINK_CB(cb->skb).portid,
642 cb->nlh->nlmsg_seq, NLM_F_MULTI);
643 if (err)
644 goto out;
645 idx++;
647 out:
648 mutex_unlock(&devlink_mutex);
650 cb->args[0] = idx;
651 return msg->len;
654 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
655 struct genl_info *info)
657 struct devlink_port *devlink_port = info->user_ptr[0];
658 struct devlink *devlink = devlink_port->devlink;
659 struct sk_buff *msg;
660 int err;
662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
663 if (!msg)
664 return -ENOMEM;
666 err = devlink_nl_port_fill(msg, devlink, devlink_port,
667 DEVLINK_CMD_PORT_NEW,
668 info->snd_portid, info->snd_seq, 0);
669 if (err) {
670 nlmsg_free(msg);
671 return err;
674 return genlmsg_reply(msg, info);
677 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
678 struct netlink_callback *cb)
680 struct devlink *devlink;
681 struct devlink_port *devlink_port;
682 int start = cb->args[0];
683 int idx = 0;
684 int err;
686 mutex_lock(&devlink_mutex);
687 list_for_each_entry(devlink, &devlink_list, list) {
688 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
689 continue;
690 mutex_lock(&devlink->lock);
691 list_for_each_entry(devlink_port, &devlink->port_list, list) {
692 if (idx < start) {
693 idx++;
694 continue;
696 err = devlink_nl_port_fill(msg, devlink, devlink_port,
697 DEVLINK_CMD_NEW,
698 NETLINK_CB(cb->skb).portid,
699 cb->nlh->nlmsg_seq,
700 NLM_F_MULTI);
701 if (err) {
702 mutex_unlock(&devlink->lock);
703 goto out;
705 idx++;
707 mutex_unlock(&devlink->lock);
709 out:
710 mutex_unlock(&devlink_mutex);
712 cb->args[0] = idx;
713 return msg->len;
716 static int devlink_port_type_set(struct devlink *devlink,
717 struct devlink_port *devlink_port,
718 enum devlink_port_type port_type)
721 int err;
723 if (devlink->ops && devlink->ops->port_type_set) {
724 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
725 return -EINVAL;
726 if (port_type == devlink_port->type)
727 return 0;
728 err = devlink->ops->port_type_set(devlink_port, port_type);
729 if (err)
730 return err;
731 devlink_port->desired_type = port_type;
732 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
733 return 0;
735 return -EOPNOTSUPP;
738 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
739 struct genl_info *info)
741 struct devlink_port *devlink_port = info->user_ptr[0];
742 struct devlink *devlink = devlink_port->devlink;
743 int err;
745 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
746 enum devlink_port_type port_type;
748 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
749 err = devlink_port_type_set(devlink, devlink_port, port_type);
750 if (err)
751 return err;
753 return 0;
756 static int devlink_port_split(struct devlink *devlink, u32 port_index,
757 u32 count, struct netlink_ext_ack *extack)
760 if (devlink->ops && devlink->ops->port_split)
761 return devlink->ops->port_split(devlink, port_index, count,
762 extack);
763 return -EOPNOTSUPP;
766 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
767 struct genl_info *info)
769 struct devlink *devlink = info->user_ptr[0];
770 u32 port_index;
771 u32 count;
773 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
774 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
775 return -EINVAL;
777 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
778 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
779 return devlink_port_split(devlink, port_index, count, info->extack);
782 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
783 struct netlink_ext_ack *extack)
786 if (devlink->ops && devlink->ops->port_unsplit)
787 return devlink->ops->port_unsplit(devlink, port_index, extack);
788 return -EOPNOTSUPP;
791 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
792 struct genl_info *info)
794 struct devlink *devlink = info->user_ptr[0];
795 u32 port_index;
797 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
798 return -EINVAL;
800 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
801 return devlink_port_unsplit(devlink, port_index, info->extack);
804 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
805 struct devlink_sb *devlink_sb,
806 enum devlink_command cmd, u32 portid,
807 u32 seq, int flags)
809 void *hdr;
811 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
812 if (!hdr)
813 return -EMSGSIZE;
815 if (devlink_nl_put_handle(msg, devlink))
816 goto nla_put_failure;
817 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
818 goto nla_put_failure;
819 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
820 goto nla_put_failure;
821 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
822 devlink_sb->ingress_pools_count))
823 goto nla_put_failure;
824 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
825 devlink_sb->egress_pools_count))
826 goto nla_put_failure;
827 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
828 devlink_sb->ingress_tc_count))
829 goto nla_put_failure;
830 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
831 devlink_sb->egress_tc_count))
832 goto nla_put_failure;
834 genlmsg_end(msg, hdr);
835 return 0;
837 nla_put_failure:
838 genlmsg_cancel(msg, hdr);
839 return -EMSGSIZE;
842 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
843 struct genl_info *info)
845 struct devlink *devlink = info->user_ptr[0];
846 struct devlink_sb *devlink_sb = info->user_ptr[1];
847 struct sk_buff *msg;
848 int err;
850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
851 if (!msg)
852 return -ENOMEM;
854 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
855 DEVLINK_CMD_SB_NEW,
856 info->snd_portid, info->snd_seq, 0);
857 if (err) {
858 nlmsg_free(msg);
859 return err;
862 return genlmsg_reply(msg, info);
865 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
866 struct netlink_callback *cb)
868 struct devlink *devlink;
869 struct devlink_sb *devlink_sb;
870 int start = cb->args[0];
871 int idx = 0;
872 int err;
874 mutex_lock(&devlink_mutex);
875 list_for_each_entry(devlink, &devlink_list, list) {
876 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
877 continue;
878 mutex_lock(&devlink->lock);
879 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
880 if (idx < start) {
881 idx++;
882 continue;
884 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
885 DEVLINK_CMD_SB_NEW,
886 NETLINK_CB(cb->skb).portid,
887 cb->nlh->nlmsg_seq,
888 NLM_F_MULTI);
889 if (err) {
890 mutex_unlock(&devlink->lock);
891 goto out;
893 idx++;
895 mutex_unlock(&devlink->lock);
897 out:
898 mutex_unlock(&devlink_mutex);
900 cb->args[0] = idx;
901 return msg->len;
904 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
905 struct devlink_sb *devlink_sb,
906 u16 pool_index, enum devlink_command cmd,
907 u32 portid, u32 seq, int flags)
909 struct devlink_sb_pool_info pool_info;
910 void *hdr;
911 int err;
913 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
914 pool_index, &pool_info);
915 if (err)
916 return err;
918 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
919 if (!hdr)
920 return -EMSGSIZE;
922 if (devlink_nl_put_handle(msg, devlink))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
925 goto nla_put_failure;
926 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
927 goto nla_put_failure;
928 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
929 goto nla_put_failure;
930 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
931 goto nla_put_failure;
932 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
933 pool_info.threshold_type))
934 goto nla_put_failure;
936 genlmsg_end(msg, hdr);
937 return 0;
939 nla_put_failure:
940 genlmsg_cancel(msg, hdr);
941 return -EMSGSIZE;
944 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
945 struct genl_info *info)
947 struct devlink *devlink = info->user_ptr[0];
948 struct devlink_sb *devlink_sb = info->user_ptr[1];
949 struct sk_buff *msg;
950 u16 pool_index;
951 int err;
953 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
954 &pool_index);
955 if (err)
956 return err;
958 if (!devlink->ops || !devlink->ops->sb_pool_get)
959 return -EOPNOTSUPP;
961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
962 if (!msg)
963 return -ENOMEM;
965 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
966 DEVLINK_CMD_SB_POOL_NEW,
967 info->snd_portid, info->snd_seq, 0);
968 if (err) {
969 nlmsg_free(msg);
970 return err;
973 return genlmsg_reply(msg, info);
976 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
977 struct devlink *devlink,
978 struct devlink_sb *devlink_sb,
979 u32 portid, u32 seq)
981 u16 pool_count = devlink_sb_pool_count(devlink_sb);
982 u16 pool_index;
983 int err;
985 for (pool_index = 0; pool_index < pool_count; pool_index++) {
986 if (*p_idx < start) {
987 (*p_idx)++;
988 continue;
990 err = devlink_nl_sb_pool_fill(msg, devlink,
991 devlink_sb,
992 pool_index,
993 DEVLINK_CMD_SB_POOL_NEW,
994 portid, seq, NLM_F_MULTI);
995 if (err)
996 return err;
997 (*p_idx)++;
999 return 0;
1002 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1003 struct netlink_callback *cb)
1005 struct devlink *devlink;
1006 struct devlink_sb *devlink_sb;
1007 int start = cb->args[0];
1008 int idx = 0;
1009 int err;
1011 mutex_lock(&devlink_mutex);
1012 list_for_each_entry(devlink, &devlink_list, list) {
1013 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1014 !devlink->ops || !devlink->ops->sb_pool_get)
1015 continue;
1016 mutex_lock(&devlink->lock);
1017 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1018 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1019 devlink_sb,
1020 NETLINK_CB(cb->skb).portid,
1021 cb->nlh->nlmsg_seq);
1022 if (err && err != -EOPNOTSUPP) {
1023 mutex_unlock(&devlink->lock);
1024 goto out;
1027 mutex_unlock(&devlink->lock);
1029 out:
1030 mutex_unlock(&devlink_mutex);
1032 cb->args[0] = idx;
1033 return msg->len;
1036 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1037 u16 pool_index, u32 size,
1038 enum devlink_sb_threshold_type threshold_type)
1041 const struct devlink_ops *ops = devlink->ops;
1043 if (ops && ops->sb_pool_set)
1044 return ops->sb_pool_set(devlink, sb_index, pool_index,
1045 size, threshold_type);
1046 return -EOPNOTSUPP;
1049 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1050 struct genl_info *info)
1052 struct devlink *devlink = info->user_ptr[0];
1053 struct devlink_sb *devlink_sb = info->user_ptr[1];
1054 enum devlink_sb_threshold_type threshold_type;
1055 u16 pool_index;
1056 u32 size;
1057 int err;
1059 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1060 &pool_index);
1061 if (err)
1062 return err;
1064 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1065 if (err)
1066 return err;
1068 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1069 return -EINVAL;
1071 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1072 return devlink_sb_pool_set(devlink, devlink_sb->index,
1073 pool_index, size, threshold_type);
1076 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1077 struct devlink *devlink,
1078 struct devlink_port *devlink_port,
1079 struct devlink_sb *devlink_sb,
1080 u16 pool_index,
1081 enum devlink_command cmd,
1082 u32 portid, u32 seq, int flags)
1084 const struct devlink_ops *ops = devlink->ops;
1085 u32 threshold;
1086 void *hdr;
1087 int err;
1089 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1090 pool_index, &threshold);
1091 if (err)
1092 return err;
1094 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1095 if (!hdr)
1096 return -EMSGSIZE;
1098 if (devlink_nl_put_handle(msg, devlink))
1099 goto nla_put_failure;
1100 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1101 goto nla_put_failure;
1102 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1103 goto nla_put_failure;
1104 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1105 goto nla_put_failure;
1106 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1107 goto nla_put_failure;
1109 if (ops->sb_occ_port_pool_get) {
1110 u32 cur;
1111 u32 max;
1113 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1114 pool_index, &cur, &max);
1115 if (err && err != -EOPNOTSUPP)
1116 return err;
1117 if (!err) {
1118 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1119 goto nla_put_failure;
1120 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1121 goto nla_put_failure;
1125 genlmsg_end(msg, hdr);
1126 return 0;
1128 nla_put_failure:
1129 genlmsg_cancel(msg, hdr);
1130 return -EMSGSIZE;
1133 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1134 struct genl_info *info)
1136 struct devlink_port *devlink_port = info->user_ptr[0];
1137 struct devlink *devlink = devlink_port->devlink;
1138 struct devlink_sb *devlink_sb = info->user_ptr[1];
1139 struct sk_buff *msg;
1140 u16 pool_index;
1141 int err;
1143 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1144 &pool_index);
1145 if (err)
1146 return err;
1148 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1149 return -EOPNOTSUPP;
1151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1152 if (!msg)
1153 return -ENOMEM;
1155 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1156 devlink_sb, pool_index,
1157 DEVLINK_CMD_SB_PORT_POOL_NEW,
1158 info->snd_portid, info->snd_seq, 0);
1159 if (err) {
1160 nlmsg_free(msg);
1161 return err;
1164 return genlmsg_reply(msg, info);
1167 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1168 struct devlink *devlink,
1169 struct devlink_sb *devlink_sb,
1170 u32 portid, u32 seq)
1172 struct devlink_port *devlink_port;
1173 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1174 u16 pool_index;
1175 int err;
1177 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1178 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1179 if (*p_idx < start) {
1180 (*p_idx)++;
1181 continue;
1183 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1184 devlink_port,
1185 devlink_sb,
1186 pool_index,
1187 DEVLINK_CMD_SB_PORT_POOL_NEW,
1188 portid, seq,
1189 NLM_F_MULTI);
1190 if (err)
1191 return err;
1192 (*p_idx)++;
1195 return 0;
1198 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1199 struct netlink_callback *cb)
1201 struct devlink *devlink;
1202 struct devlink_sb *devlink_sb;
1203 int start = cb->args[0];
1204 int idx = 0;
1205 int err;
1207 mutex_lock(&devlink_mutex);
1208 list_for_each_entry(devlink, &devlink_list, list) {
1209 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1210 !devlink->ops || !devlink->ops->sb_port_pool_get)
1211 continue;
1212 mutex_lock(&devlink->lock);
1213 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1214 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1215 devlink, devlink_sb,
1216 NETLINK_CB(cb->skb).portid,
1217 cb->nlh->nlmsg_seq);
1218 if (err && err != -EOPNOTSUPP) {
1219 mutex_unlock(&devlink->lock);
1220 goto out;
1223 mutex_unlock(&devlink->lock);
1225 out:
1226 mutex_unlock(&devlink_mutex);
1228 cb->args[0] = idx;
1229 return msg->len;
1232 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1233 unsigned int sb_index, u16 pool_index,
1234 u32 threshold)
1237 const struct devlink_ops *ops = devlink_port->devlink->ops;
1239 if (ops && ops->sb_port_pool_set)
1240 return ops->sb_port_pool_set(devlink_port, sb_index,
1241 pool_index, threshold);
1242 return -EOPNOTSUPP;
1245 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1246 struct genl_info *info)
1248 struct devlink_port *devlink_port = info->user_ptr[0];
1249 struct devlink_sb *devlink_sb = info->user_ptr[1];
1250 u16 pool_index;
1251 u32 threshold;
1252 int err;
1254 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1255 &pool_index);
1256 if (err)
1257 return err;
1259 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1260 return -EINVAL;
1262 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1263 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1264 pool_index, threshold);
1267 static int
1268 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1269 struct devlink_port *devlink_port,
1270 struct devlink_sb *devlink_sb, u16 tc_index,
1271 enum devlink_sb_pool_type pool_type,
1272 enum devlink_command cmd,
1273 u32 portid, u32 seq, int flags)
1275 const struct devlink_ops *ops = devlink->ops;
1276 u16 pool_index;
1277 u32 threshold;
1278 void *hdr;
1279 int err;
1281 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1282 tc_index, pool_type,
1283 &pool_index, &threshold);
1284 if (err)
1285 return err;
1287 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1288 if (!hdr)
1289 return -EMSGSIZE;
1291 if (devlink_nl_put_handle(msg, devlink))
1292 goto nla_put_failure;
1293 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1294 goto nla_put_failure;
1295 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1296 goto nla_put_failure;
1297 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1298 goto nla_put_failure;
1299 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1300 goto nla_put_failure;
1301 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1302 goto nla_put_failure;
1303 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1304 goto nla_put_failure;
1306 if (ops->sb_occ_tc_port_bind_get) {
1307 u32 cur;
1308 u32 max;
1310 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1311 devlink_sb->index,
1312 tc_index, pool_type,
1313 &cur, &max);
1314 if (err && err != -EOPNOTSUPP)
1315 return err;
1316 if (!err) {
1317 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1318 goto nla_put_failure;
1319 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1320 goto nla_put_failure;
1324 genlmsg_end(msg, hdr);
1325 return 0;
1327 nla_put_failure:
1328 genlmsg_cancel(msg, hdr);
1329 return -EMSGSIZE;
1332 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1333 struct genl_info *info)
1335 struct devlink_port *devlink_port = info->user_ptr[0];
1336 struct devlink *devlink = devlink_port->devlink;
1337 struct devlink_sb *devlink_sb = info->user_ptr[1];
1338 struct sk_buff *msg;
1339 enum devlink_sb_pool_type pool_type;
1340 u16 tc_index;
1341 int err;
1343 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1344 if (err)
1345 return err;
1347 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1348 pool_type, &tc_index);
1349 if (err)
1350 return err;
1352 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1353 return -EOPNOTSUPP;
1355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1356 if (!msg)
1357 return -ENOMEM;
1359 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1360 devlink_sb, tc_index, pool_type,
1361 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1362 info->snd_portid,
1363 info->snd_seq, 0);
1364 if (err) {
1365 nlmsg_free(msg);
1366 return err;
1369 return genlmsg_reply(msg, info);
1372 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1373 int start, int *p_idx,
1374 struct devlink *devlink,
1375 struct devlink_sb *devlink_sb,
1376 u32 portid, u32 seq)
1378 struct devlink_port *devlink_port;
1379 u16 tc_index;
1380 int err;
1382 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1383 for (tc_index = 0;
1384 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1385 if (*p_idx < start) {
1386 (*p_idx)++;
1387 continue;
1389 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1390 devlink_port,
1391 devlink_sb,
1392 tc_index,
1393 DEVLINK_SB_POOL_TYPE_INGRESS,
1394 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1395 portid, seq,
1396 NLM_F_MULTI);
1397 if (err)
1398 return err;
1399 (*p_idx)++;
1401 for (tc_index = 0;
1402 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1403 if (*p_idx < start) {
1404 (*p_idx)++;
1405 continue;
1407 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1408 devlink_port,
1409 devlink_sb,
1410 tc_index,
1411 DEVLINK_SB_POOL_TYPE_EGRESS,
1412 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1413 portid, seq,
1414 NLM_F_MULTI);
1415 if (err)
1416 return err;
1417 (*p_idx)++;
1420 return 0;
1423 static int
1424 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1425 struct netlink_callback *cb)
1427 struct devlink *devlink;
1428 struct devlink_sb *devlink_sb;
1429 int start = cb->args[0];
1430 int idx = 0;
1431 int err;
1433 mutex_lock(&devlink_mutex);
1434 list_for_each_entry(devlink, &devlink_list, list) {
1435 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1436 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1437 continue;
1439 mutex_lock(&devlink->lock);
1440 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1441 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1442 devlink,
1443 devlink_sb,
1444 NETLINK_CB(cb->skb).portid,
1445 cb->nlh->nlmsg_seq);
1446 if (err && err != -EOPNOTSUPP) {
1447 mutex_unlock(&devlink->lock);
1448 goto out;
1451 mutex_unlock(&devlink->lock);
1453 out:
1454 mutex_unlock(&devlink_mutex);
1456 cb->args[0] = idx;
1457 return msg->len;
1460 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1461 unsigned int sb_index, u16 tc_index,
1462 enum devlink_sb_pool_type pool_type,
1463 u16 pool_index, u32 threshold)
1466 const struct devlink_ops *ops = devlink_port->devlink->ops;
1468 if (ops && ops->sb_tc_pool_bind_set)
1469 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1470 tc_index, pool_type,
1471 pool_index, threshold);
1472 return -EOPNOTSUPP;
1475 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1476 struct genl_info *info)
1478 struct devlink_port *devlink_port = info->user_ptr[0];
1479 struct devlink_sb *devlink_sb = info->user_ptr[1];
1480 enum devlink_sb_pool_type pool_type;
1481 u16 tc_index;
1482 u16 pool_index;
1483 u32 threshold;
1484 int err;
1486 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1487 if (err)
1488 return err;
1490 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1491 pool_type, &tc_index);
1492 if (err)
1493 return err;
1495 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1496 &pool_index);
1497 if (err)
1498 return err;
1500 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1501 return -EINVAL;
1503 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1504 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1505 tc_index, pool_type,
1506 pool_index, threshold);
1509 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1510 struct genl_info *info)
1512 struct devlink *devlink = info->user_ptr[0];
1513 struct devlink_sb *devlink_sb = info->user_ptr[1];
1514 const struct devlink_ops *ops = devlink->ops;
1516 if (ops && ops->sb_occ_snapshot)
1517 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1518 return -EOPNOTSUPP;
1521 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1522 struct genl_info *info)
1524 struct devlink *devlink = info->user_ptr[0];
1525 struct devlink_sb *devlink_sb = info->user_ptr[1];
1526 const struct devlink_ops *ops = devlink->ops;
1528 if (ops && ops->sb_occ_max_clear)
1529 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1530 return -EOPNOTSUPP;
1533 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1534 enum devlink_command cmd, u32 portid,
1535 u32 seq, int flags)
1537 const struct devlink_ops *ops = devlink->ops;
1538 u8 inline_mode, encap_mode;
1539 void *hdr;
1540 int err = 0;
1541 u16 mode;
1543 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1544 if (!hdr)
1545 return -EMSGSIZE;
1547 err = devlink_nl_put_handle(msg, devlink);
1548 if (err)
1549 goto nla_put_failure;
1551 if (ops->eswitch_mode_get) {
1552 err = ops->eswitch_mode_get(devlink, &mode);
1553 if (err)
1554 goto nla_put_failure;
1555 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1556 if (err)
1557 goto nla_put_failure;
1560 if (ops->eswitch_inline_mode_get) {
1561 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1562 if (err)
1563 goto nla_put_failure;
1564 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1565 inline_mode);
1566 if (err)
1567 goto nla_put_failure;
1570 if (ops->eswitch_encap_mode_get) {
1571 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1572 if (err)
1573 goto nla_put_failure;
1574 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1575 if (err)
1576 goto nla_put_failure;
1579 genlmsg_end(msg, hdr);
1580 return 0;
1582 nla_put_failure:
1583 genlmsg_cancel(msg, hdr);
1584 return err;
1587 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1588 struct genl_info *info)
1590 struct devlink *devlink = info->user_ptr[0];
1591 const struct devlink_ops *ops = devlink->ops;
1592 struct sk_buff *msg;
1593 int err;
1595 if (!ops)
1596 return -EOPNOTSUPP;
1598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1599 if (!msg)
1600 return -ENOMEM;
1602 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1603 info->snd_portid, info->snd_seq, 0);
1605 if (err) {
1606 nlmsg_free(msg);
1607 return err;
1610 return genlmsg_reply(msg, info);
1613 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1614 struct genl_info *info)
1616 struct devlink *devlink = info->user_ptr[0];
1617 const struct devlink_ops *ops = devlink->ops;
1618 u8 inline_mode, encap_mode;
1619 int err = 0;
1620 u16 mode;
1622 if (!ops)
1623 return -EOPNOTSUPP;
1625 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1626 if (!ops->eswitch_mode_set)
1627 return -EOPNOTSUPP;
1628 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1629 err = ops->eswitch_mode_set(devlink, mode);
1630 if (err)
1631 return err;
1634 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1635 if (!ops->eswitch_inline_mode_set)
1636 return -EOPNOTSUPP;
1637 inline_mode = nla_get_u8(
1638 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1639 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1640 if (err)
1641 return err;
1644 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1645 if (!ops->eswitch_encap_mode_set)
1646 return -EOPNOTSUPP;
1647 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1648 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1649 if (err)
1650 return err;
1653 return 0;
1656 int devlink_dpipe_match_put(struct sk_buff *skb,
1657 struct devlink_dpipe_match *match)
1659 struct devlink_dpipe_header *header = match->header;
1660 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1661 struct nlattr *match_attr;
1663 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1664 if (!match_attr)
1665 return -EMSGSIZE;
1667 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1668 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1669 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1670 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1671 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1672 goto nla_put_failure;
1674 nla_nest_end(skb, match_attr);
1675 return 0;
1677 nla_put_failure:
1678 nla_nest_cancel(skb, match_attr);
1679 return -EMSGSIZE;
1681 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1683 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1684 struct sk_buff *skb)
1686 struct nlattr *matches_attr;
1688 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1689 if (!matches_attr)
1690 return -EMSGSIZE;
1692 if (table->table_ops->matches_dump(table->priv, skb))
1693 goto nla_put_failure;
1695 nla_nest_end(skb, matches_attr);
1696 return 0;
1698 nla_put_failure:
1699 nla_nest_cancel(skb, matches_attr);
1700 return -EMSGSIZE;
1703 int devlink_dpipe_action_put(struct sk_buff *skb,
1704 struct devlink_dpipe_action *action)
1706 struct devlink_dpipe_header *header = action->header;
1707 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1708 struct nlattr *action_attr;
1710 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1711 if (!action_attr)
1712 return -EMSGSIZE;
1714 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1715 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1716 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1717 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1718 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1719 goto nla_put_failure;
1721 nla_nest_end(skb, action_attr);
1722 return 0;
1724 nla_put_failure:
1725 nla_nest_cancel(skb, action_attr);
1726 return -EMSGSIZE;
1728 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1730 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1731 struct sk_buff *skb)
1733 struct nlattr *actions_attr;
1735 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1736 if (!actions_attr)
1737 return -EMSGSIZE;
1739 if (table->table_ops->actions_dump(table->priv, skb))
1740 goto nla_put_failure;
1742 nla_nest_end(skb, actions_attr);
1743 return 0;
1745 nla_put_failure:
1746 nla_nest_cancel(skb, actions_attr);
1747 return -EMSGSIZE;
1750 static int devlink_dpipe_table_put(struct sk_buff *skb,
1751 struct devlink_dpipe_table *table)
1753 struct nlattr *table_attr;
1754 u64 table_size;
1756 table_size = table->table_ops->size_get(table->priv);
1757 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1758 if (!table_attr)
1759 return -EMSGSIZE;
1761 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1762 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1763 DEVLINK_ATTR_PAD))
1764 goto nla_put_failure;
1765 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1766 table->counters_enabled))
1767 goto nla_put_failure;
1769 if (table->resource_valid) {
1770 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1771 table->resource_id, DEVLINK_ATTR_PAD) ||
1772 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1773 table->resource_units, DEVLINK_ATTR_PAD))
1774 goto nla_put_failure;
1776 if (devlink_dpipe_matches_put(table, skb))
1777 goto nla_put_failure;
1779 if (devlink_dpipe_actions_put(table, skb))
1780 goto nla_put_failure;
1782 nla_nest_end(skb, table_attr);
1783 return 0;
1785 nla_put_failure:
1786 nla_nest_cancel(skb, table_attr);
1787 return -EMSGSIZE;
1790 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1791 struct genl_info *info)
1793 int err;
1795 if (*pskb) {
1796 err = genlmsg_reply(*pskb, info);
1797 if (err)
1798 return err;
1800 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1801 if (!*pskb)
1802 return -ENOMEM;
1803 return 0;
1806 static int devlink_dpipe_tables_fill(struct genl_info *info,
1807 enum devlink_command cmd, int flags,
1808 struct list_head *dpipe_tables,
1809 const char *table_name)
1811 struct devlink *devlink = info->user_ptr[0];
1812 struct devlink_dpipe_table *table;
1813 struct nlattr *tables_attr;
1814 struct sk_buff *skb = NULL;
1815 struct nlmsghdr *nlh;
1816 bool incomplete;
1817 void *hdr;
1818 int i;
1819 int err;
1821 table = list_first_entry(dpipe_tables,
1822 struct devlink_dpipe_table, list);
1823 start_again:
1824 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1825 if (err)
1826 return err;
1828 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1829 &devlink_nl_family, NLM_F_MULTI, cmd);
1830 if (!hdr) {
1831 nlmsg_free(skb);
1832 return -EMSGSIZE;
1835 if (devlink_nl_put_handle(skb, devlink))
1836 goto nla_put_failure;
1837 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1838 if (!tables_attr)
1839 goto nla_put_failure;
1841 i = 0;
1842 incomplete = false;
1843 list_for_each_entry_from(table, dpipe_tables, list) {
1844 if (!table_name) {
1845 err = devlink_dpipe_table_put(skb, table);
1846 if (err) {
1847 if (!i)
1848 goto err_table_put;
1849 incomplete = true;
1850 break;
1852 } else {
1853 if (!strcmp(table->name, table_name)) {
1854 err = devlink_dpipe_table_put(skb, table);
1855 if (err)
1856 break;
1859 i++;
1862 nla_nest_end(skb, tables_attr);
1863 genlmsg_end(skb, hdr);
1864 if (incomplete)
1865 goto start_again;
1867 send_done:
1868 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1869 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1870 if (!nlh) {
1871 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1872 if (err)
1873 return err;
1874 goto send_done;
1877 return genlmsg_reply(skb, info);
1879 nla_put_failure:
1880 err = -EMSGSIZE;
1881 err_table_put:
1882 nlmsg_free(skb);
1883 return err;
1886 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1887 struct genl_info *info)
1889 struct devlink *devlink = info->user_ptr[0];
1890 const char *table_name = NULL;
1892 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1893 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1895 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1896 &devlink->dpipe_table_list,
1897 table_name);
1900 static int devlink_dpipe_value_put(struct sk_buff *skb,
1901 struct devlink_dpipe_value *value)
1903 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1904 value->value_size, value->value))
1905 return -EMSGSIZE;
1906 if (value->mask)
1907 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1908 value->value_size, value->mask))
1909 return -EMSGSIZE;
1910 if (value->mapping_valid)
1911 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1912 value->mapping_value))
1913 return -EMSGSIZE;
1914 return 0;
1917 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1918 struct devlink_dpipe_value *value)
1920 if (!value->action)
1921 return -EINVAL;
1922 if (devlink_dpipe_action_put(skb, value->action))
1923 return -EMSGSIZE;
1924 if (devlink_dpipe_value_put(skb, value))
1925 return -EMSGSIZE;
1926 return 0;
1929 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1930 struct devlink_dpipe_value *values,
1931 unsigned int values_count)
1933 struct nlattr *action_attr;
1934 int i;
1935 int err;
1937 for (i = 0; i < values_count; i++) {
1938 action_attr = nla_nest_start(skb,
1939 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1940 if (!action_attr)
1941 return -EMSGSIZE;
1942 err = devlink_dpipe_action_value_put(skb, &values[i]);
1943 if (err)
1944 goto err_action_value_put;
1945 nla_nest_end(skb, action_attr);
1947 return 0;
1949 err_action_value_put:
1950 nla_nest_cancel(skb, action_attr);
1951 return err;
1954 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1955 struct devlink_dpipe_value *value)
1957 if (!value->match)
1958 return -EINVAL;
1959 if (devlink_dpipe_match_put(skb, value->match))
1960 return -EMSGSIZE;
1961 if (devlink_dpipe_value_put(skb, value))
1962 return -EMSGSIZE;
1963 return 0;
1966 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1967 struct devlink_dpipe_value *values,
1968 unsigned int values_count)
1970 struct nlattr *match_attr;
1971 int i;
1972 int err;
1974 for (i = 0; i < values_count; i++) {
1975 match_attr = nla_nest_start(skb,
1976 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1977 if (!match_attr)
1978 return -EMSGSIZE;
1979 err = devlink_dpipe_match_value_put(skb, &values[i]);
1980 if (err)
1981 goto err_match_value_put;
1982 nla_nest_end(skb, match_attr);
1984 return 0;
1986 err_match_value_put:
1987 nla_nest_cancel(skb, match_attr);
1988 return err;
1991 static int devlink_dpipe_entry_put(struct sk_buff *skb,
1992 struct devlink_dpipe_entry *entry)
1994 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1995 int err;
1997 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1998 if (!entry_attr)
1999 return -EMSGSIZE;
2001 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2002 DEVLINK_ATTR_PAD))
2003 goto nla_put_failure;
2004 if (entry->counter_valid)
2005 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2006 entry->counter, DEVLINK_ATTR_PAD))
2007 goto nla_put_failure;
2009 matches_attr = nla_nest_start(skb,
2010 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2011 if (!matches_attr)
2012 goto nla_put_failure;
2014 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2015 entry->match_values_count);
2016 if (err) {
2017 nla_nest_cancel(skb, matches_attr);
2018 goto err_match_values_put;
2020 nla_nest_end(skb, matches_attr);
2022 actions_attr = nla_nest_start(skb,
2023 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2024 if (!actions_attr)
2025 goto nla_put_failure;
2027 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2028 entry->action_values_count);
2029 if (err) {
2030 nla_nest_cancel(skb, actions_attr);
2031 goto err_action_values_put;
2033 nla_nest_end(skb, actions_attr);
2035 nla_nest_end(skb, entry_attr);
2036 return 0;
2038 nla_put_failure:
2039 err = -EMSGSIZE;
2040 err_match_values_put:
2041 err_action_values_put:
2042 nla_nest_cancel(skb, entry_attr);
2043 return err;
2046 static struct devlink_dpipe_table *
2047 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2048 const char *table_name)
2050 struct devlink_dpipe_table *table;
2052 list_for_each_entry_rcu(table, dpipe_tables, list) {
2053 if (!strcmp(table->name, table_name))
2054 return table;
2056 return NULL;
2059 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2061 struct devlink *devlink;
2062 int err;
2064 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2065 dump_ctx->info);
2066 if (err)
2067 return err;
2069 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2070 dump_ctx->info->snd_portid,
2071 dump_ctx->info->snd_seq,
2072 &devlink_nl_family, NLM_F_MULTI,
2073 dump_ctx->cmd);
2074 if (!dump_ctx->hdr)
2075 goto nla_put_failure;
2077 devlink = dump_ctx->info->user_ptr[0];
2078 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2079 goto nla_put_failure;
2080 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2081 DEVLINK_ATTR_DPIPE_ENTRIES);
2082 if (!dump_ctx->nest)
2083 goto nla_put_failure;
2084 return 0;
2086 nla_put_failure:
2087 nlmsg_free(dump_ctx->skb);
2088 return -EMSGSIZE;
2090 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2092 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2093 struct devlink_dpipe_entry *entry)
2095 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2097 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2099 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2101 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2102 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2103 return 0;
2105 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2107 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2110 unsigned int value_count, value_index;
2111 struct devlink_dpipe_value *value;
2113 value = entry->action_values;
2114 value_count = entry->action_values_count;
2115 for (value_index = 0; value_index < value_count; value_index++) {
2116 kfree(value[value_index].value);
2117 kfree(value[value_index].mask);
2120 value = entry->match_values;
2121 value_count = entry->match_values_count;
2122 for (value_index = 0; value_index < value_count; value_index++) {
2123 kfree(value[value_index].value);
2124 kfree(value[value_index].mask);
2127 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2129 static int devlink_dpipe_entries_fill(struct genl_info *info,
2130 enum devlink_command cmd, int flags,
2131 struct devlink_dpipe_table *table)
2133 struct devlink_dpipe_dump_ctx dump_ctx;
2134 struct nlmsghdr *nlh;
2135 int err;
2137 dump_ctx.skb = NULL;
2138 dump_ctx.cmd = cmd;
2139 dump_ctx.info = info;
2141 err = table->table_ops->entries_dump(table->priv,
2142 table->counters_enabled,
2143 &dump_ctx);
2144 if (err)
2145 return err;
2147 send_done:
2148 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2149 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2150 if (!nlh) {
2151 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2152 if (err)
2153 return err;
2154 goto send_done;
2156 return genlmsg_reply(dump_ctx.skb, info);
2159 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2160 struct genl_info *info)
2162 struct devlink *devlink = info->user_ptr[0];
2163 struct devlink_dpipe_table *table;
2164 const char *table_name;
2166 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2167 return -EINVAL;
2169 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2170 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2171 table_name);
2172 if (!table)
2173 return -EINVAL;
2175 if (!table->table_ops->entries_dump)
2176 return -EINVAL;
2178 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2179 0, table);
2182 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2183 const struct devlink_dpipe_header *header)
2185 struct devlink_dpipe_field *field;
2186 struct nlattr *field_attr;
2187 int i;
2189 for (i = 0; i < header->fields_count; i++) {
2190 field = &header->fields[i];
2191 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2192 if (!field_attr)
2193 return -EMSGSIZE;
2194 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2195 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2196 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2197 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2198 goto nla_put_failure;
2199 nla_nest_end(skb, field_attr);
2201 return 0;
2203 nla_put_failure:
2204 nla_nest_cancel(skb, field_attr);
2205 return -EMSGSIZE;
2208 static int devlink_dpipe_header_put(struct sk_buff *skb,
2209 struct devlink_dpipe_header *header)
2211 struct nlattr *fields_attr, *header_attr;
2212 int err;
2214 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2215 if (!header_attr)
2216 return -EMSGSIZE;
2218 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2219 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2220 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2221 goto nla_put_failure;
2223 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2224 if (!fields_attr)
2225 goto nla_put_failure;
2227 err = devlink_dpipe_fields_put(skb, header);
2228 if (err) {
2229 nla_nest_cancel(skb, fields_attr);
2230 goto nla_put_failure;
2232 nla_nest_end(skb, fields_attr);
2233 nla_nest_end(skb, header_attr);
2234 return 0;
2236 nla_put_failure:
2237 err = -EMSGSIZE;
2238 nla_nest_cancel(skb, header_attr);
2239 return err;
2242 static int devlink_dpipe_headers_fill(struct genl_info *info,
2243 enum devlink_command cmd, int flags,
2244 struct devlink_dpipe_headers *
2245 dpipe_headers)
2247 struct devlink *devlink = info->user_ptr[0];
2248 struct nlattr *headers_attr;
2249 struct sk_buff *skb = NULL;
2250 struct nlmsghdr *nlh;
2251 void *hdr;
2252 int i, j;
2253 int err;
2255 i = 0;
2256 start_again:
2257 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2258 if (err)
2259 return err;
2261 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2262 &devlink_nl_family, NLM_F_MULTI, cmd);
2263 if (!hdr) {
2264 nlmsg_free(skb);
2265 return -EMSGSIZE;
2268 if (devlink_nl_put_handle(skb, devlink))
2269 goto nla_put_failure;
2270 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2271 if (!headers_attr)
2272 goto nla_put_failure;
2274 j = 0;
2275 for (; i < dpipe_headers->headers_count; i++) {
2276 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2277 if (err) {
2278 if (!j)
2279 goto err_table_put;
2280 break;
2282 j++;
2284 nla_nest_end(skb, headers_attr);
2285 genlmsg_end(skb, hdr);
2286 if (i != dpipe_headers->headers_count)
2287 goto start_again;
2289 send_done:
2290 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2291 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2292 if (!nlh) {
2293 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2294 if (err)
2295 return err;
2296 goto send_done;
2298 return genlmsg_reply(skb, info);
2300 nla_put_failure:
2301 err = -EMSGSIZE;
2302 err_table_put:
2303 nlmsg_free(skb);
2304 return err;
2307 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2308 struct genl_info *info)
2310 struct devlink *devlink = info->user_ptr[0];
2312 if (!devlink->dpipe_headers)
2313 return -EOPNOTSUPP;
2314 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2315 0, devlink->dpipe_headers);
2318 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2319 const char *table_name,
2320 bool enable)
2322 struct devlink_dpipe_table *table;
2324 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2325 table_name);
2326 if (!table)
2327 return -EINVAL;
2329 if (table->counter_control_extern)
2330 return -EOPNOTSUPP;
2332 if (!(table->counters_enabled ^ enable))
2333 return 0;
2335 table->counters_enabled = enable;
2336 if (table->table_ops->counters_set_update)
2337 table->table_ops->counters_set_update(table->priv, enable);
2338 return 0;
2341 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2342 struct genl_info *info)
2344 struct devlink *devlink = info->user_ptr[0];
2345 const char *table_name;
2346 bool counters_enable;
2348 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2349 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2350 return -EINVAL;
2352 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2353 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2355 return devlink_dpipe_table_counters_set(devlink, table_name,
2356 counters_enable);
2359 static struct devlink_resource *
2360 devlink_resource_find(struct devlink *devlink,
2361 struct devlink_resource *resource, u64 resource_id)
2363 struct list_head *resource_list;
2365 if (resource)
2366 resource_list = &resource->resource_list;
2367 else
2368 resource_list = &devlink->resource_list;
2370 list_for_each_entry(resource, resource_list, list) {
2371 struct devlink_resource *child_resource;
2373 if (resource->id == resource_id)
2374 return resource;
2376 child_resource = devlink_resource_find(devlink, resource,
2377 resource_id);
2378 if (child_resource)
2379 return child_resource;
2381 return NULL;
2384 static void
2385 devlink_resource_validate_children(struct devlink_resource *resource)
2387 struct devlink_resource *child_resource;
2388 bool size_valid = true;
2389 u64 parts_size = 0;
2391 if (list_empty(&resource->resource_list))
2392 goto out;
2394 list_for_each_entry(child_resource, &resource->resource_list, list)
2395 parts_size += child_resource->size_new;
2397 if (parts_size > resource->size_new)
2398 size_valid = false;
2399 out:
2400 resource->size_valid = size_valid;
2403 static int
2404 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2405 struct netlink_ext_ack *extack)
2407 u64 reminder;
2408 int err = 0;
2410 if (size > resource->size_params.size_max) {
2411 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2412 err = -EINVAL;
2415 if (size < resource->size_params.size_min) {
2416 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2417 err = -EINVAL;
2420 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2421 if (reminder) {
2422 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2423 err = -EINVAL;
2426 return err;
2429 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2430 struct genl_info *info)
2432 struct devlink *devlink = info->user_ptr[0];
2433 struct devlink_resource *resource;
2434 u64 resource_id;
2435 u64 size;
2436 int err;
2438 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2439 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2440 return -EINVAL;
2441 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2443 resource = devlink_resource_find(devlink, NULL, resource_id);
2444 if (!resource)
2445 return -EINVAL;
2447 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2448 err = devlink_resource_validate_size(resource, size, info->extack);
2449 if (err)
2450 return err;
2452 resource->size_new = size;
2453 devlink_resource_validate_children(resource);
2454 if (resource->parent)
2455 devlink_resource_validate_children(resource->parent);
2456 return 0;
2459 static int
2460 devlink_resource_size_params_put(struct devlink_resource *resource,
2461 struct sk_buff *skb)
2463 struct devlink_resource_size_params *size_params;
2465 size_params = &resource->size_params;
2466 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2467 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2468 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2469 size_params->size_max, DEVLINK_ATTR_PAD) ||
2470 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2471 size_params->size_min, DEVLINK_ATTR_PAD) ||
2472 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2473 return -EMSGSIZE;
2474 return 0;
2477 static int devlink_resource_occ_put(struct devlink_resource *resource,
2478 struct sk_buff *skb)
2480 if (!resource->occ_get)
2481 return 0;
2482 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2483 resource->occ_get(resource->occ_get_priv),
2484 DEVLINK_ATTR_PAD);
2487 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2488 struct devlink_resource *resource)
2490 struct devlink_resource *child_resource;
2491 struct nlattr *child_resource_attr;
2492 struct nlattr *resource_attr;
2494 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2495 if (!resource_attr)
2496 return -EMSGSIZE;
2498 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2499 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2500 DEVLINK_ATTR_PAD) ||
2501 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2502 DEVLINK_ATTR_PAD))
2503 goto nla_put_failure;
2504 if (resource->size != resource->size_new)
2505 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2506 resource->size_new, DEVLINK_ATTR_PAD);
2507 if (devlink_resource_occ_put(resource, skb))
2508 goto nla_put_failure;
2509 if (devlink_resource_size_params_put(resource, skb))
2510 goto nla_put_failure;
2511 if (list_empty(&resource->resource_list))
2512 goto out;
2514 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2515 resource->size_valid))
2516 goto nla_put_failure;
2518 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2519 if (!child_resource_attr)
2520 goto nla_put_failure;
2522 list_for_each_entry(child_resource, &resource->resource_list, list) {
2523 if (devlink_resource_put(devlink, skb, child_resource))
2524 goto resource_put_failure;
2527 nla_nest_end(skb, child_resource_attr);
2528 out:
2529 nla_nest_end(skb, resource_attr);
2530 return 0;
2532 resource_put_failure:
2533 nla_nest_cancel(skb, child_resource_attr);
2534 nla_put_failure:
2535 nla_nest_cancel(skb, resource_attr);
2536 return -EMSGSIZE;
2539 static int devlink_resource_fill(struct genl_info *info,
2540 enum devlink_command cmd, int flags)
2542 struct devlink *devlink = info->user_ptr[0];
2543 struct devlink_resource *resource;
2544 struct nlattr *resources_attr;
2545 struct sk_buff *skb = NULL;
2546 struct nlmsghdr *nlh;
2547 bool incomplete;
2548 void *hdr;
2549 int i;
2550 int err;
2552 resource = list_first_entry(&devlink->resource_list,
2553 struct devlink_resource, list);
2554 start_again:
2555 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2556 if (err)
2557 return err;
2559 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2560 &devlink_nl_family, NLM_F_MULTI, cmd);
2561 if (!hdr) {
2562 nlmsg_free(skb);
2563 return -EMSGSIZE;
2566 if (devlink_nl_put_handle(skb, devlink))
2567 goto nla_put_failure;
2569 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2570 if (!resources_attr)
2571 goto nla_put_failure;
2573 incomplete = false;
2574 i = 0;
2575 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2576 err = devlink_resource_put(devlink, skb, resource);
2577 if (err) {
2578 if (!i)
2579 goto err_resource_put;
2580 incomplete = true;
2581 break;
2583 i++;
2585 nla_nest_end(skb, resources_attr);
2586 genlmsg_end(skb, hdr);
2587 if (incomplete)
2588 goto start_again;
2589 send_done:
2590 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2591 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2592 if (!nlh) {
2593 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2594 if (err)
2595 return err;
2596 goto send_done;
2598 return genlmsg_reply(skb, info);
2600 nla_put_failure:
2601 err = -EMSGSIZE;
2602 err_resource_put:
2603 nlmsg_free(skb);
2604 return err;
2607 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2608 struct genl_info *info)
2610 struct devlink *devlink = info->user_ptr[0];
2612 if (list_empty(&devlink->resource_list))
2613 return -EOPNOTSUPP;
2615 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2618 static int
2619 devlink_resources_validate(struct devlink *devlink,
2620 struct devlink_resource *resource,
2621 struct genl_info *info)
2623 struct list_head *resource_list;
2624 int err = 0;
2626 if (resource)
2627 resource_list = &resource->resource_list;
2628 else
2629 resource_list = &devlink->resource_list;
2631 list_for_each_entry(resource, resource_list, list) {
2632 if (!resource->size_valid)
2633 return -EINVAL;
2634 err = devlink_resources_validate(devlink, resource, info);
2635 if (err)
2636 return err;
2638 return err;
2641 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2643 struct devlink *devlink = info->user_ptr[0];
2644 int err;
2646 if (!devlink->ops->reload)
2647 return -EOPNOTSUPP;
2649 err = devlink_resources_validate(devlink, NULL, info);
2650 if (err) {
2651 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2652 return err;
2654 return devlink->ops->reload(devlink, info->extack);
2657 static const struct devlink_param devlink_param_generic[] = {
2659 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2660 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2661 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2664 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2665 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2666 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2669 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2670 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2671 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2674 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2675 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2676 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2680 static int devlink_param_generic_verify(const struct devlink_param *param)
2682 /* verify it match generic parameter by id and name */
2683 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2684 return -EINVAL;
2685 if (strcmp(param->name, devlink_param_generic[param->id].name))
2686 return -ENOENT;
2688 WARN_ON(param->type != devlink_param_generic[param->id].type);
2690 return 0;
2693 static int devlink_param_driver_verify(const struct devlink_param *param)
2695 int i;
2697 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2698 return -EINVAL;
2699 /* verify no such name in generic params */
2700 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2701 if (!strcmp(param->name, devlink_param_generic[i].name))
2702 return -EEXIST;
2704 return 0;
2707 static struct devlink_param_item *
2708 devlink_param_find_by_name(struct list_head *param_list,
2709 const char *param_name)
2711 struct devlink_param_item *param_item;
2713 list_for_each_entry(param_item, param_list, list)
2714 if (!strcmp(param_item->param->name, param_name))
2715 return param_item;
2716 return NULL;
2719 static struct devlink_param_item *
2720 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2722 struct devlink_param_item *param_item;
2724 list_for_each_entry(param_item, param_list, list)
2725 if (param_item->param->id == param_id)
2726 return param_item;
2727 return NULL;
2730 static bool
2731 devlink_param_cmode_is_supported(const struct devlink_param *param,
2732 enum devlink_param_cmode cmode)
2734 return test_bit(cmode, &param->supported_cmodes);
2737 static int devlink_param_get(struct devlink *devlink,
2738 const struct devlink_param *param,
2739 struct devlink_param_gset_ctx *ctx)
2741 if (!param->get)
2742 return -EOPNOTSUPP;
2743 return param->get(devlink, param->id, ctx);
2746 static int devlink_param_set(struct devlink *devlink,
2747 const struct devlink_param *param,
2748 struct devlink_param_gset_ctx *ctx)
2750 if (!param->set)
2751 return -EOPNOTSUPP;
2752 return param->set(devlink, param->id, ctx);
2755 static int
2756 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2758 switch (param_type) {
2759 case DEVLINK_PARAM_TYPE_U8:
2760 return NLA_U8;
2761 case DEVLINK_PARAM_TYPE_U16:
2762 return NLA_U16;
2763 case DEVLINK_PARAM_TYPE_U32:
2764 return NLA_U32;
2765 case DEVLINK_PARAM_TYPE_STRING:
2766 return NLA_STRING;
2767 case DEVLINK_PARAM_TYPE_BOOL:
2768 return NLA_FLAG;
2769 default:
2770 return -EINVAL;
2774 static int
2775 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2776 enum devlink_param_type type,
2777 enum devlink_param_cmode cmode,
2778 union devlink_param_value val)
2780 struct nlattr *param_value_attr;
2782 param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2783 if (!param_value_attr)
2784 goto nla_put_failure;
2786 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2787 goto value_nest_cancel;
2789 switch (type) {
2790 case DEVLINK_PARAM_TYPE_U8:
2791 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2792 goto value_nest_cancel;
2793 break;
2794 case DEVLINK_PARAM_TYPE_U16:
2795 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2796 goto value_nest_cancel;
2797 break;
2798 case DEVLINK_PARAM_TYPE_U32:
2799 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2800 goto value_nest_cancel;
2801 break;
2802 case DEVLINK_PARAM_TYPE_STRING:
2803 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2804 val.vstr))
2805 goto value_nest_cancel;
2806 break;
2807 case DEVLINK_PARAM_TYPE_BOOL:
2808 if (val.vbool &&
2809 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2810 goto value_nest_cancel;
2811 break;
2814 nla_nest_end(msg, param_value_attr);
2815 return 0;
2817 value_nest_cancel:
2818 nla_nest_cancel(msg, param_value_attr);
2819 nla_put_failure:
2820 return -EMSGSIZE;
2823 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2824 struct devlink_param_item *param_item,
2825 enum devlink_command cmd,
2826 u32 portid, u32 seq, int flags)
2828 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2829 const struct devlink_param *param = param_item->param;
2830 struct devlink_param_gset_ctx ctx;
2831 struct nlattr *param_values_list;
2832 struct nlattr *param_attr;
2833 int nla_type;
2834 void *hdr;
2835 int err;
2836 int i;
2838 /* Get value from driver part to driverinit configuration mode */
2839 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2840 if (!devlink_param_cmode_is_supported(param, i))
2841 continue;
2842 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2843 if (!param_item->driverinit_value_valid)
2844 return -EOPNOTSUPP;
2845 param_value[i] = param_item->driverinit_value;
2846 } else {
2847 ctx.cmode = i;
2848 err = devlink_param_get(devlink, param, &ctx);
2849 if (err)
2850 return err;
2851 param_value[i] = ctx.val;
2855 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2856 if (!hdr)
2857 return -EMSGSIZE;
2859 if (devlink_nl_put_handle(msg, devlink))
2860 goto genlmsg_cancel;
2861 param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2862 if (!param_attr)
2863 goto genlmsg_cancel;
2864 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2865 goto param_nest_cancel;
2866 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2867 goto param_nest_cancel;
2869 nla_type = devlink_param_type_to_nla_type(param->type);
2870 if (nla_type < 0)
2871 goto param_nest_cancel;
2872 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2873 goto param_nest_cancel;
2875 param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2876 if (!param_values_list)
2877 goto param_nest_cancel;
2879 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2880 if (!devlink_param_cmode_is_supported(param, i))
2881 continue;
2882 err = devlink_nl_param_value_fill_one(msg, param->type,
2883 i, param_value[i]);
2884 if (err)
2885 goto values_list_nest_cancel;
2888 nla_nest_end(msg, param_values_list);
2889 nla_nest_end(msg, param_attr);
2890 genlmsg_end(msg, hdr);
2891 return 0;
2893 values_list_nest_cancel:
2894 nla_nest_end(msg, param_values_list);
2895 param_nest_cancel:
2896 nla_nest_cancel(msg, param_attr);
2897 genlmsg_cancel:
2898 genlmsg_cancel(msg, hdr);
2899 return -EMSGSIZE;
2902 static void devlink_param_notify(struct devlink *devlink,
2903 struct devlink_param_item *param_item,
2904 enum devlink_command cmd)
2906 struct sk_buff *msg;
2907 int err;
2909 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
2911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2912 if (!msg)
2913 return;
2914 err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
2915 if (err) {
2916 nlmsg_free(msg);
2917 return;
2920 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2921 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2924 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2925 struct netlink_callback *cb)
2927 struct devlink_param_item *param_item;
2928 struct devlink *devlink;
2929 int start = cb->args[0];
2930 int idx = 0;
2931 int err;
2933 mutex_lock(&devlink_mutex);
2934 list_for_each_entry(devlink, &devlink_list, list) {
2935 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2936 continue;
2937 mutex_lock(&devlink->lock);
2938 list_for_each_entry(param_item, &devlink->param_list, list) {
2939 if (idx < start) {
2940 idx++;
2941 continue;
2943 err = devlink_nl_param_fill(msg, devlink, param_item,
2944 DEVLINK_CMD_PARAM_GET,
2945 NETLINK_CB(cb->skb).portid,
2946 cb->nlh->nlmsg_seq,
2947 NLM_F_MULTI);
2948 if (err) {
2949 mutex_unlock(&devlink->lock);
2950 goto out;
2952 idx++;
2954 mutex_unlock(&devlink->lock);
2956 out:
2957 mutex_unlock(&devlink_mutex);
2959 cb->args[0] = idx;
2960 return msg->len;
2963 static int
2964 devlink_param_type_get_from_info(struct genl_info *info,
2965 enum devlink_param_type *param_type)
2967 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
2968 return -EINVAL;
2970 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
2971 case NLA_U8:
2972 *param_type = DEVLINK_PARAM_TYPE_U8;
2973 break;
2974 case NLA_U16:
2975 *param_type = DEVLINK_PARAM_TYPE_U16;
2976 break;
2977 case NLA_U32:
2978 *param_type = DEVLINK_PARAM_TYPE_U32;
2979 break;
2980 case NLA_STRING:
2981 *param_type = DEVLINK_PARAM_TYPE_STRING;
2982 break;
2983 case NLA_FLAG:
2984 *param_type = DEVLINK_PARAM_TYPE_BOOL;
2985 break;
2986 default:
2987 return -EINVAL;
2990 return 0;
2993 static int
2994 devlink_param_value_get_from_info(const struct devlink_param *param,
2995 struct genl_info *info,
2996 union devlink_param_value *value)
2998 struct nlattr *param_data;
2999 int len;
3001 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3003 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3004 return -EINVAL;
3006 switch (param->type) {
3007 case DEVLINK_PARAM_TYPE_U8:
3008 if (nla_len(param_data) != sizeof(u8))
3009 return -EINVAL;
3010 value->vu8 = nla_get_u8(param_data);
3011 break;
3012 case DEVLINK_PARAM_TYPE_U16:
3013 if (nla_len(param_data) != sizeof(u16))
3014 return -EINVAL;
3015 value->vu16 = nla_get_u16(param_data);
3016 break;
3017 case DEVLINK_PARAM_TYPE_U32:
3018 if (nla_len(param_data) != sizeof(u32))
3019 return -EINVAL;
3020 value->vu32 = nla_get_u32(param_data);
3021 break;
3022 case DEVLINK_PARAM_TYPE_STRING:
3023 len = strnlen(nla_data(param_data), nla_len(param_data));
3024 if (len == nla_len(param_data) ||
3025 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3026 return -EINVAL;
3027 strcpy(value->vstr, nla_data(param_data));
3028 break;
3029 case DEVLINK_PARAM_TYPE_BOOL:
3030 if (param_data && nla_len(param_data))
3031 return -EINVAL;
3032 value->vbool = nla_get_flag(param_data);
3033 break;
3035 return 0;
3038 static struct devlink_param_item *
3039 devlink_param_get_from_info(struct devlink *devlink,
3040 struct genl_info *info)
3042 char *param_name;
3044 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3045 return NULL;
3047 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3048 return devlink_param_find_by_name(&devlink->param_list, param_name);
3051 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3052 struct genl_info *info)
3054 struct devlink *devlink = info->user_ptr[0];
3055 struct devlink_param_item *param_item;
3056 struct sk_buff *msg;
3057 int err;
3059 param_item = devlink_param_get_from_info(devlink, info);
3060 if (!param_item)
3061 return -EINVAL;
3063 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3064 if (!msg)
3065 return -ENOMEM;
3067 err = devlink_nl_param_fill(msg, devlink, param_item,
3068 DEVLINK_CMD_PARAM_GET,
3069 info->snd_portid, info->snd_seq, 0);
3070 if (err) {
3071 nlmsg_free(msg);
3072 return err;
3075 return genlmsg_reply(msg, info);
3078 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3079 struct genl_info *info)
3081 struct devlink *devlink = info->user_ptr[0];
3082 enum devlink_param_type param_type;
3083 struct devlink_param_gset_ctx ctx;
3084 enum devlink_param_cmode cmode;
3085 struct devlink_param_item *param_item;
3086 const struct devlink_param *param;
3087 union devlink_param_value value;
3088 int err = 0;
3090 param_item = devlink_param_get_from_info(devlink, info);
3091 if (!param_item)
3092 return -EINVAL;
3093 param = param_item->param;
3094 err = devlink_param_type_get_from_info(info, &param_type);
3095 if (err)
3096 return err;
3097 if (param_type != param->type)
3098 return -EINVAL;
3099 err = devlink_param_value_get_from_info(param, info, &value);
3100 if (err)
3101 return err;
3102 if (param->validate) {
3103 err = param->validate(devlink, param->id, value, info->extack);
3104 if (err)
3105 return err;
3108 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3109 return -EINVAL;
3110 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3111 if (!devlink_param_cmode_is_supported(param, cmode))
3112 return -EOPNOTSUPP;
3114 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3115 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3116 strcpy(param_item->driverinit_value.vstr, value.vstr);
3117 else
3118 param_item->driverinit_value = value;
3119 param_item->driverinit_value_valid = true;
3120 } else {
3121 if (!param->set)
3122 return -EOPNOTSUPP;
3123 ctx.val = value;
3124 ctx.cmode = cmode;
3125 err = devlink_param_set(devlink, param, &ctx);
3126 if (err)
3127 return err;
3130 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
3131 return 0;
3134 static int devlink_param_register_one(struct devlink *devlink,
3135 const struct devlink_param *param)
3137 struct devlink_param_item *param_item;
3139 if (devlink_param_find_by_name(&devlink->param_list,
3140 param->name))
3141 return -EEXIST;
3143 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3144 WARN_ON(param->get || param->set);
3145 else
3146 WARN_ON(!param->get || !param->set);
3148 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3149 if (!param_item)
3150 return -ENOMEM;
3151 param_item->param = param;
3153 list_add_tail(&param_item->list, &devlink->param_list);
3154 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
3155 return 0;
3158 static void devlink_param_unregister_one(struct devlink *devlink,
3159 const struct devlink_param *param)
3161 struct devlink_param_item *param_item;
3163 param_item = devlink_param_find_by_name(&devlink->param_list,
3164 param->name);
3165 WARN_ON(!param_item);
3166 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
3167 list_del(&param_item->list);
3168 kfree(param_item);
3171 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3172 struct devlink *devlink,
3173 struct devlink_snapshot *snapshot)
3175 struct nlattr *snap_attr;
3176 int err;
3178 snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3179 if (!snap_attr)
3180 return -EINVAL;
3182 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3183 if (err)
3184 goto nla_put_failure;
3186 nla_nest_end(msg, snap_attr);
3187 return 0;
3189 nla_put_failure:
3190 nla_nest_cancel(msg, snap_attr);
3191 return err;
3194 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3195 struct devlink *devlink,
3196 struct devlink_region *region)
3198 struct devlink_snapshot *snapshot;
3199 struct nlattr *snapshots_attr;
3200 int err;
3202 snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
3203 if (!snapshots_attr)
3204 return -EINVAL;
3206 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3207 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3208 if (err)
3209 goto nla_put_failure;
3212 nla_nest_end(msg, snapshots_attr);
3213 return 0;
3215 nla_put_failure:
3216 nla_nest_cancel(msg, snapshots_attr);
3217 return err;
3220 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3221 enum devlink_command cmd, u32 portid,
3222 u32 seq, int flags,
3223 struct devlink_region *region)
3225 void *hdr;
3226 int err;
3228 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3229 if (!hdr)
3230 return -EMSGSIZE;
3232 err = devlink_nl_put_handle(msg, devlink);
3233 if (err)
3234 goto nla_put_failure;
3236 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3237 if (err)
3238 goto nla_put_failure;
3240 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3241 region->size,
3242 DEVLINK_ATTR_PAD);
3243 if (err)
3244 goto nla_put_failure;
3246 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3247 if (err)
3248 goto nla_put_failure;
3250 genlmsg_end(msg, hdr);
3251 return 0;
3253 nla_put_failure:
3254 genlmsg_cancel(msg, hdr);
3255 return err;
3258 static void devlink_nl_region_notify(struct devlink_region *region,
3259 struct devlink_snapshot *snapshot,
3260 enum devlink_command cmd)
3262 struct devlink *devlink = region->devlink;
3263 struct sk_buff *msg;
3264 void *hdr;
3265 int err;
3267 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3269 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3270 if (!msg)
3271 return;
3273 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3274 if (!hdr)
3275 goto out_free_msg;
3277 err = devlink_nl_put_handle(msg, devlink);
3278 if (err)
3279 goto out_cancel_msg;
3281 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3282 region->name);
3283 if (err)
3284 goto out_cancel_msg;
3286 if (snapshot) {
3287 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3288 snapshot->id);
3289 if (err)
3290 goto out_cancel_msg;
3291 } else {
3292 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3293 region->size, DEVLINK_ATTR_PAD);
3294 if (err)
3295 goto out_cancel_msg;
3297 genlmsg_end(msg, hdr);
3299 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3300 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3302 return;
3304 out_cancel_msg:
3305 genlmsg_cancel(msg, hdr);
3306 out_free_msg:
3307 nlmsg_free(msg);
3310 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3311 struct genl_info *info)
3313 struct devlink *devlink = info->user_ptr[0];
3314 struct devlink_region *region;
3315 const char *region_name;
3316 struct sk_buff *msg;
3317 int err;
3319 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3320 return -EINVAL;
3322 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3323 region = devlink_region_get_by_name(devlink, region_name);
3324 if (!region)
3325 return -EINVAL;
3327 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3328 if (!msg)
3329 return -ENOMEM;
3331 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3332 info->snd_portid, info->snd_seq, 0,
3333 region);
3334 if (err) {
3335 nlmsg_free(msg);
3336 return err;
3339 return genlmsg_reply(msg, info);
3342 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3343 struct netlink_callback *cb)
3345 struct devlink_region *region;
3346 struct devlink *devlink;
3347 int start = cb->args[0];
3348 int idx = 0;
3349 int err;
3351 mutex_lock(&devlink_mutex);
3352 list_for_each_entry(devlink, &devlink_list, list) {
3353 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3354 continue;
3356 mutex_lock(&devlink->lock);
3357 list_for_each_entry(region, &devlink->region_list, list) {
3358 if (idx < start) {
3359 idx++;
3360 continue;
3362 err = devlink_nl_region_fill(msg, devlink,
3363 DEVLINK_CMD_REGION_GET,
3364 NETLINK_CB(cb->skb).portid,
3365 cb->nlh->nlmsg_seq,
3366 NLM_F_MULTI, region);
3367 if (err) {
3368 mutex_unlock(&devlink->lock);
3369 goto out;
3371 idx++;
3373 mutex_unlock(&devlink->lock);
3375 out:
3376 mutex_unlock(&devlink_mutex);
3377 cb->args[0] = idx;
3378 return msg->len;
3381 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3382 struct genl_info *info)
3384 struct devlink *devlink = info->user_ptr[0];
3385 struct devlink_snapshot *snapshot;
3386 struct devlink_region *region;
3387 const char *region_name;
3388 u32 snapshot_id;
3390 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3391 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3392 return -EINVAL;
3394 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3395 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3397 region = devlink_region_get_by_name(devlink, region_name);
3398 if (!region)
3399 return -EINVAL;
3401 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3402 if (!snapshot)
3403 return -EINVAL;
3405 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3406 devlink_region_snapshot_del(snapshot);
3407 return 0;
3410 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3411 struct devlink *devlink,
3412 u8 *chunk, u32 chunk_size,
3413 u64 addr)
3415 struct nlattr *chunk_attr;
3416 int err;
3418 chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
3419 if (!chunk_attr)
3420 return -EINVAL;
3422 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3423 if (err)
3424 goto nla_put_failure;
3426 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3427 DEVLINK_ATTR_PAD);
3428 if (err)
3429 goto nla_put_failure;
3431 nla_nest_end(msg, chunk_attr);
3432 return 0;
3434 nla_put_failure:
3435 nla_nest_cancel(msg, chunk_attr);
3436 return err;
3439 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3441 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3442 struct devlink *devlink,
3443 struct devlink_region *region,
3444 struct nlattr **attrs,
3445 u64 start_offset,
3446 u64 end_offset,
3447 bool dump,
3448 u64 *new_offset)
3450 struct devlink_snapshot *snapshot;
3451 u64 curr_offset = start_offset;
3452 u32 snapshot_id;
3453 int err = 0;
3455 *new_offset = start_offset;
3457 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3458 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3459 if (!snapshot)
3460 return -EINVAL;
3462 if (end_offset > snapshot->data_len || dump)
3463 end_offset = snapshot->data_len;
3465 while (curr_offset < end_offset) {
3466 u32 data_size;
3467 u8 *data;
3469 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3470 data_size = end_offset - curr_offset;
3471 else
3472 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3474 data = &snapshot->data[curr_offset];
3475 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3476 data, data_size,
3477 curr_offset);
3478 if (err)
3479 break;
3481 curr_offset += data_size;
3483 *new_offset = curr_offset;
3485 return err;
3488 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3489 struct netlink_callback *cb)
3491 u64 ret_offset, start_offset, end_offset = 0;
3492 struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
3493 const struct genl_ops *ops = cb->data;
3494 struct devlink_region *region;
3495 struct nlattr *chunks_attr;
3496 const char *region_name;
3497 struct devlink *devlink;
3498 bool dump = true;
3499 void *hdr;
3500 int err;
3502 start_offset = *((u64 *)&cb->args[0]);
3504 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
3505 attrs, DEVLINK_ATTR_MAX, ops->policy, NULL);
3506 if (err)
3507 goto out;
3509 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3510 if (IS_ERR(devlink))
3511 goto out;
3513 mutex_lock(&devlink_mutex);
3514 mutex_lock(&devlink->lock);
3516 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3517 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3518 goto out_unlock;
3520 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3521 region = devlink_region_get_by_name(devlink, region_name);
3522 if (!region)
3523 goto out_unlock;
3525 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3526 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3527 DEVLINK_CMD_REGION_READ);
3528 if (!hdr)
3529 goto out_unlock;
3531 err = devlink_nl_put_handle(skb, devlink);
3532 if (err)
3533 goto nla_put_failure;
3535 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3536 if (err)
3537 goto nla_put_failure;
3539 chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
3540 if (!chunks_attr)
3541 goto nla_put_failure;
3543 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3544 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3545 if (!start_offset)
3546 start_offset =
3547 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3549 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3550 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3551 dump = false;
3554 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3555 region, attrs,
3556 start_offset,
3557 end_offset, dump,
3558 &ret_offset);
3560 if (err && err != -EMSGSIZE)
3561 goto nla_put_failure;
3563 /* Check if there was any progress done to prevent infinite loop */
3564 if (ret_offset == start_offset)
3565 goto nla_put_failure;
3567 *((u64 *)&cb->args[0]) = ret_offset;
3569 nla_nest_end(skb, chunks_attr);
3570 genlmsg_end(skb, hdr);
3571 mutex_unlock(&devlink->lock);
3572 mutex_unlock(&devlink_mutex);
3574 return skb->len;
3576 nla_put_failure:
3577 genlmsg_cancel(skb, hdr);
3578 out_unlock:
3579 mutex_unlock(&devlink->lock);
3580 mutex_unlock(&devlink_mutex);
3581 out:
3582 return 0;
3585 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
3586 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
3587 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
3588 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
3589 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
3590 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
3591 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
3592 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
3593 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
3594 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
3595 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
3596 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
3597 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
3598 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
3599 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
3600 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
3601 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
3602 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
3603 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
3604 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
3605 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
3606 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
3607 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
3608 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
3609 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
3610 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
3611 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
3614 static const struct genl_ops devlink_nl_ops[] = {
3616 .cmd = DEVLINK_CMD_GET,
3617 .doit = devlink_nl_cmd_get_doit,
3618 .dumpit = devlink_nl_cmd_get_dumpit,
3619 .policy = devlink_nl_policy,
3620 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3621 /* can be retrieved by unprivileged users */
3624 .cmd = DEVLINK_CMD_PORT_GET,
3625 .doit = devlink_nl_cmd_port_get_doit,
3626 .dumpit = devlink_nl_cmd_port_get_dumpit,
3627 .policy = devlink_nl_policy,
3628 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3629 /* can be retrieved by unprivileged users */
3632 .cmd = DEVLINK_CMD_PORT_SET,
3633 .doit = devlink_nl_cmd_port_set_doit,
3634 .policy = devlink_nl_policy,
3635 .flags = GENL_ADMIN_PERM,
3636 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3639 .cmd = DEVLINK_CMD_PORT_SPLIT,
3640 .doit = devlink_nl_cmd_port_split_doit,
3641 .policy = devlink_nl_policy,
3642 .flags = GENL_ADMIN_PERM,
3643 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3644 DEVLINK_NL_FLAG_NO_LOCK,
3647 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
3648 .doit = devlink_nl_cmd_port_unsplit_doit,
3649 .policy = devlink_nl_policy,
3650 .flags = GENL_ADMIN_PERM,
3651 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3652 DEVLINK_NL_FLAG_NO_LOCK,
3655 .cmd = DEVLINK_CMD_SB_GET,
3656 .doit = devlink_nl_cmd_sb_get_doit,
3657 .dumpit = devlink_nl_cmd_sb_get_dumpit,
3658 .policy = devlink_nl_policy,
3659 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3660 DEVLINK_NL_FLAG_NEED_SB,
3661 /* can be retrieved by unprivileged users */
3664 .cmd = DEVLINK_CMD_SB_POOL_GET,
3665 .doit = devlink_nl_cmd_sb_pool_get_doit,
3666 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
3667 .policy = devlink_nl_policy,
3668 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3669 DEVLINK_NL_FLAG_NEED_SB,
3670 /* can be retrieved by unprivileged users */
3673 .cmd = DEVLINK_CMD_SB_POOL_SET,
3674 .doit = devlink_nl_cmd_sb_pool_set_doit,
3675 .policy = devlink_nl_policy,
3676 .flags = GENL_ADMIN_PERM,
3677 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3678 DEVLINK_NL_FLAG_NEED_SB,
3681 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
3682 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
3683 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
3684 .policy = devlink_nl_policy,
3685 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3686 DEVLINK_NL_FLAG_NEED_SB,
3687 /* can be retrieved by unprivileged users */
3690 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
3691 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
3692 .policy = devlink_nl_policy,
3693 .flags = GENL_ADMIN_PERM,
3694 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3695 DEVLINK_NL_FLAG_NEED_SB,
3698 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
3699 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
3700 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
3701 .policy = devlink_nl_policy,
3702 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3703 DEVLINK_NL_FLAG_NEED_SB,
3704 /* can be retrieved by unprivileged users */
3707 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3708 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
3709 .policy = devlink_nl_policy,
3710 .flags = GENL_ADMIN_PERM,
3711 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3712 DEVLINK_NL_FLAG_NEED_SB,
3715 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
3716 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
3717 .policy = devlink_nl_policy,
3718 .flags = GENL_ADMIN_PERM,
3719 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3720 DEVLINK_NL_FLAG_NEED_SB,
3723 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
3724 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
3725 .policy = devlink_nl_policy,
3726 .flags = GENL_ADMIN_PERM,
3727 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3728 DEVLINK_NL_FLAG_NEED_SB,
3731 .cmd = DEVLINK_CMD_ESWITCH_GET,
3732 .doit = devlink_nl_cmd_eswitch_get_doit,
3733 .policy = devlink_nl_policy,
3734 .flags = GENL_ADMIN_PERM,
3735 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3738 .cmd = DEVLINK_CMD_ESWITCH_SET,
3739 .doit = devlink_nl_cmd_eswitch_set_doit,
3740 .policy = devlink_nl_policy,
3741 .flags = GENL_ADMIN_PERM,
3742 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3743 DEVLINK_NL_FLAG_NO_LOCK,
3746 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
3747 .doit = devlink_nl_cmd_dpipe_table_get,
3748 .policy = devlink_nl_policy,
3749 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3750 /* can be retrieved by unprivileged users */
3753 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
3754 .doit = devlink_nl_cmd_dpipe_entries_get,
3755 .policy = devlink_nl_policy,
3756 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3757 /* can be retrieved by unprivileged users */
3760 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
3761 .doit = devlink_nl_cmd_dpipe_headers_get,
3762 .policy = devlink_nl_policy,
3763 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3764 /* can be retrieved by unprivileged users */
3767 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
3768 .doit = devlink_nl_cmd_dpipe_table_counters_set,
3769 .policy = devlink_nl_policy,
3770 .flags = GENL_ADMIN_PERM,
3771 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3774 .cmd = DEVLINK_CMD_RESOURCE_SET,
3775 .doit = devlink_nl_cmd_resource_set,
3776 .policy = devlink_nl_policy,
3777 .flags = GENL_ADMIN_PERM,
3778 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3781 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
3782 .doit = devlink_nl_cmd_resource_dump,
3783 .policy = devlink_nl_policy,
3784 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3785 /* can be retrieved by unprivileged users */
3788 .cmd = DEVLINK_CMD_RELOAD,
3789 .doit = devlink_nl_cmd_reload,
3790 .policy = devlink_nl_policy,
3791 .flags = GENL_ADMIN_PERM,
3792 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3793 DEVLINK_NL_FLAG_NO_LOCK,
3796 .cmd = DEVLINK_CMD_PARAM_GET,
3797 .doit = devlink_nl_cmd_param_get_doit,
3798 .dumpit = devlink_nl_cmd_param_get_dumpit,
3799 .policy = devlink_nl_policy,
3800 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3801 /* can be retrieved by unprivileged users */
3804 .cmd = DEVLINK_CMD_PARAM_SET,
3805 .doit = devlink_nl_cmd_param_set_doit,
3806 .policy = devlink_nl_policy,
3807 .flags = GENL_ADMIN_PERM,
3808 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3811 .cmd = DEVLINK_CMD_REGION_GET,
3812 .doit = devlink_nl_cmd_region_get_doit,
3813 .dumpit = devlink_nl_cmd_region_get_dumpit,
3814 .policy = devlink_nl_policy,
3815 .flags = GENL_ADMIN_PERM,
3816 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3819 .cmd = DEVLINK_CMD_REGION_DEL,
3820 .doit = devlink_nl_cmd_region_del,
3821 .policy = devlink_nl_policy,
3822 .flags = GENL_ADMIN_PERM,
3823 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3826 .cmd = DEVLINK_CMD_REGION_READ,
3827 .dumpit = devlink_nl_cmd_region_read_dumpit,
3828 .policy = devlink_nl_policy,
3829 .flags = GENL_ADMIN_PERM,
3830 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3834 static struct genl_family devlink_nl_family __ro_after_init = {
3835 .name = DEVLINK_GENL_NAME,
3836 .version = DEVLINK_GENL_VERSION,
3837 .maxattr = DEVLINK_ATTR_MAX,
3838 .netnsok = true,
3839 .pre_doit = devlink_nl_pre_doit,
3840 .post_doit = devlink_nl_post_doit,
3841 .module = THIS_MODULE,
3842 .ops = devlink_nl_ops,
3843 .n_ops = ARRAY_SIZE(devlink_nl_ops),
3844 .mcgrps = devlink_nl_mcgrps,
3845 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
3849 * devlink_alloc - Allocate new devlink instance resources
3851 * @ops: ops
3852 * @priv_size: size of user private data
3854 * Allocate new devlink instance resources, including devlink index
3855 * and name.
3857 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
3859 struct devlink *devlink;
3861 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
3862 if (!devlink)
3863 return NULL;
3864 devlink->ops = ops;
3865 devlink_net_set(devlink, &init_net);
3866 INIT_LIST_HEAD(&devlink->port_list);
3867 INIT_LIST_HEAD(&devlink->sb_list);
3868 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
3869 INIT_LIST_HEAD(&devlink->resource_list);
3870 INIT_LIST_HEAD(&devlink->param_list);
3871 INIT_LIST_HEAD(&devlink->region_list);
3872 mutex_init(&devlink->lock);
3873 return devlink;
3875 EXPORT_SYMBOL_GPL(devlink_alloc);
3878 * devlink_register - Register devlink instance
3880 * @devlink: devlink
3882 int devlink_register(struct devlink *devlink, struct device *dev)
3884 mutex_lock(&devlink_mutex);
3885 devlink->dev = dev;
3886 list_add_tail(&devlink->list, &devlink_list);
3887 devlink_notify(devlink, DEVLINK_CMD_NEW);
3888 mutex_unlock(&devlink_mutex);
3889 return 0;
3891 EXPORT_SYMBOL_GPL(devlink_register);
3894 * devlink_unregister - Unregister devlink instance
3896 * @devlink: devlink
3898 void devlink_unregister(struct devlink *devlink)
3900 mutex_lock(&devlink_mutex);
3901 devlink_notify(devlink, DEVLINK_CMD_DEL);
3902 list_del(&devlink->list);
3903 mutex_unlock(&devlink_mutex);
3905 EXPORT_SYMBOL_GPL(devlink_unregister);
3908 * devlink_free - Free devlink instance resources
3910 * @devlink: devlink
3912 void devlink_free(struct devlink *devlink)
3914 kfree(devlink);
3916 EXPORT_SYMBOL_GPL(devlink_free);
3919 * devlink_port_register - Register devlink port
3921 * @devlink: devlink
3922 * @devlink_port: devlink port
3923 * @port_index
3925 * Register devlink port with provided port index. User can use
3926 * any indexing, even hw-related one. devlink_port structure
3927 * is convenient to be embedded inside user driver private structure.
3928 * Note that the caller should take care of zeroing the devlink_port
3929 * structure.
3931 int devlink_port_register(struct devlink *devlink,
3932 struct devlink_port *devlink_port,
3933 unsigned int port_index)
3935 mutex_lock(&devlink->lock);
3936 if (devlink_port_index_exists(devlink, port_index)) {
3937 mutex_unlock(&devlink->lock);
3938 return -EEXIST;
3940 devlink_port->devlink = devlink;
3941 devlink_port->index = port_index;
3942 devlink_port->registered = true;
3943 list_add_tail(&devlink_port->list, &devlink->port_list);
3944 mutex_unlock(&devlink->lock);
3945 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3946 return 0;
3948 EXPORT_SYMBOL_GPL(devlink_port_register);
3951 * devlink_port_unregister - Unregister devlink port
3953 * @devlink_port: devlink port
3955 void devlink_port_unregister(struct devlink_port *devlink_port)
3957 struct devlink *devlink = devlink_port->devlink;
3959 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
3960 mutex_lock(&devlink->lock);
3961 list_del(&devlink_port->list);
3962 mutex_unlock(&devlink->lock);
3964 EXPORT_SYMBOL_GPL(devlink_port_unregister);
3966 static void __devlink_port_type_set(struct devlink_port *devlink_port,
3967 enum devlink_port_type type,
3968 void *type_dev)
3970 devlink_port->type = type;
3971 devlink_port->type_dev = type_dev;
3972 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3976 * devlink_port_type_eth_set - Set port type to Ethernet
3978 * @devlink_port: devlink port
3979 * @netdev: related netdevice
3981 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
3982 struct net_device *netdev)
3984 return __devlink_port_type_set(devlink_port,
3985 DEVLINK_PORT_TYPE_ETH, netdev);
3987 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
3990 * devlink_port_type_ib_set - Set port type to InfiniBand
3992 * @devlink_port: devlink port
3993 * @ibdev: related IB device
3995 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
3996 struct ib_device *ibdev)
3998 return __devlink_port_type_set(devlink_port,
3999 DEVLINK_PORT_TYPE_IB, ibdev);
4001 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
4004 * devlink_port_type_clear - Clear port type
4006 * @devlink_port: devlink port
4008 void devlink_port_type_clear(struct devlink_port *devlink_port)
4010 return __devlink_port_type_set(devlink_port,
4011 DEVLINK_PORT_TYPE_NOTSET, NULL);
4013 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
4016 * devlink_port_attrs_set - Set port attributes
4018 * @devlink_port: devlink port
4019 * @flavour: flavour of the port
4020 * @port_number: number of the port that is facing user, for example
4021 * the front panel port number
4022 * @split: indicates if this is split port
4023 * @split_subport_number: if the port is split, this is the number
4024 * of subport.
4026 void devlink_port_attrs_set(struct devlink_port *devlink_port,
4027 enum devlink_port_flavour flavour,
4028 u32 port_number, bool split,
4029 u32 split_subport_number)
4031 struct devlink_port_attrs *attrs = &devlink_port->attrs;
4033 attrs->set = true;
4034 attrs->flavour = flavour;
4035 attrs->port_number = port_number;
4036 attrs->split = split;
4037 attrs->split_subport_number = split_subport_number;
4038 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4040 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
4042 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
4043 char *name, size_t len)
4045 struct devlink_port_attrs *attrs = &devlink_port->attrs;
4046 int n = 0;
4048 if (!attrs->set)
4049 return -EOPNOTSUPP;
4051 switch (attrs->flavour) {
4052 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
4053 if (!attrs->split)
4054 n = snprintf(name, len, "p%u", attrs->port_number);
4055 else
4056 n = snprintf(name, len, "p%us%u", attrs->port_number,
4057 attrs->split_subport_number);
4058 break;
4059 case DEVLINK_PORT_FLAVOUR_CPU:
4060 case DEVLINK_PORT_FLAVOUR_DSA:
4061 /* As CPU and DSA ports do not have a netdevice associated
4062 * case should not ever happen.
4064 WARN_ON(1);
4065 return -EINVAL;
4068 if (n >= len)
4069 return -EINVAL;
4071 return 0;
4073 EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
4075 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
4076 u32 size, u16 ingress_pools_count,
4077 u16 egress_pools_count, u16 ingress_tc_count,
4078 u16 egress_tc_count)
4080 struct devlink_sb *devlink_sb;
4081 int err = 0;
4083 mutex_lock(&devlink->lock);
4084 if (devlink_sb_index_exists(devlink, sb_index)) {
4085 err = -EEXIST;
4086 goto unlock;
4089 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
4090 if (!devlink_sb) {
4091 err = -ENOMEM;
4092 goto unlock;
4094 devlink_sb->index = sb_index;
4095 devlink_sb->size = size;
4096 devlink_sb->ingress_pools_count = ingress_pools_count;
4097 devlink_sb->egress_pools_count = egress_pools_count;
4098 devlink_sb->ingress_tc_count = ingress_tc_count;
4099 devlink_sb->egress_tc_count = egress_tc_count;
4100 list_add_tail(&devlink_sb->list, &devlink->sb_list);
4101 unlock:
4102 mutex_unlock(&devlink->lock);
4103 return err;
4105 EXPORT_SYMBOL_GPL(devlink_sb_register);
4107 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
4109 struct devlink_sb *devlink_sb;
4111 mutex_lock(&devlink->lock);
4112 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
4113 WARN_ON(!devlink_sb);
4114 list_del(&devlink_sb->list);
4115 mutex_unlock(&devlink->lock);
4116 kfree(devlink_sb);
4118 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
4121 * devlink_dpipe_headers_register - register dpipe headers
4123 * @devlink: devlink
4124 * @dpipe_headers: dpipe header array
4126 * Register the headers supported by hardware.
4128 int devlink_dpipe_headers_register(struct devlink *devlink,
4129 struct devlink_dpipe_headers *dpipe_headers)
4131 mutex_lock(&devlink->lock);
4132 devlink->dpipe_headers = dpipe_headers;
4133 mutex_unlock(&devlink->lock);
4134 return 0;
4136 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
4139 * devlink_dpipe_headers_unregister - unregister dpipe headers
4141 * @devlink: devlink
4143 * Unregister the headers supported by hardware.
4145 void devlink_dpipe_headers_unregister(struct devlink *devlink)
4147 mutex_lock(&devlink->lock);
4148 devlink->dpipe_headers = NULL;
4149 mutex_unlock(&devlink->lock);
4151 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
4154 * devlink_dpipe_table_counter_enabled - check if counter allocation
4155 * required
4156 * @devlink: devlink
4157 * @table_name: tables name
4159 * Used by driver to check if counter allocation is required.
4160 * After counter allocation is turned on the table entries
4161 * are updated to include counter statistics.
4163 * After that point on the driver must respect the counter
4164 * state so that each entry added to the table is added
4165 * with a counter.
4167 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
4168 const char *table_name)
4170 struct devlink_dpipe_table *table;
4171 bool enabled;
4173 rcu_read_lock();
4174 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4175 table_name);
4176 enabled = false;
4177 if (table)
4178 enabled = table->counters_enabled;
4179 rcu_read_unlock();
4180 return enabled;
4182 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
4185 * devlink_dpipe_table_register - register dpipe table
4187 * @devlink: devlink
4188 * @table_name: table name
4189 * @table_ops: table ops
4190 * @priv: priv
4191 * @counter_control_extern: external control for counters
4193 int devlink_dpipe_table_register(struct devlink *devlink,
4194 const char *table_name,
4195 struct devlink_dpipe_table_ops *table_ops,
4196 void *priv, bool counter_control_extern)
4198 struct devlink_dpipe_table *table;
4200 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
4201 return -EEXIST;
4203 if (WARN_ON(!table_ops->size_get))
4204 return -EINVAL;
4206 table = kzalloc(sizeof(*table), GFP_KERNEL);
4207 if (!table)
4208 return -ENOMEM;
4210 table->name = table_name;
4211 table->table_ops = table_ops;
4212 table->priv = priv;
4213 table->counter_control_extern = counter_control_extern;
4215 mutex_lock(&devlink->lock);
4216 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
4217 mutex_unlock(&devlink->lock);
4218 return 0;
4220 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
4223 * devlink_dpipe_table_unregister - unregister dpipe table
4225 * @devlink: devlink
4226 * @table_name: table name
4228 void devlink_dpipe_table_unregister(struct devlink *devlink,
4229 const char *table_name)
4231 struct devlink_dpipe_table *table;
4233 mutex_lock(&devlink->lock);
4234 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4235 table_name);
4236 if (!table)
4237 goto unlock;
4238 list_del_rcu(&table->list);
4239 mutex_unlock(&devlink->lock);
4240 kfree_rcu(table, rcu);
4241 return;
4242 unlock:
4243 mutex_unlock(&devlink->lock);
4245 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
4248 * devlink_resource_register - devlink resource register
4250 * @devlink: devlink
4251 * @resource_name: resource's name
4252 * @top_hierarchy: top hierarchy
4253 * @reload_required: reload is required for new configuration to
4254 * apply
4255 * @resource_size: resource's size
4256 * @resource_id: resource's id
4257 * @parent_reosurce_id: resource's parent id
4258 * @size params: size parameters
4260 int devlink_resource_register(struct devlink *devlink,
4261 const char *resource_name,
4262 u64 resource_size,
4263 u64 resource_id,
4264 u64 parent_resource_id,
4265 const struct devlink_resource_size_params *size_params)
4267 struct devlink_resource *resource;
4268 struct list_head *resource_list;
4269 bool top_hierarchy;
4270 int err = 0;
4272 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
4274 mutex_lock(&devlink->lock);
4275 resource = devlink_resource_find(devlink, NULL, resource_id);
4276 if (resource) {
4277 err = -EINVAL;
4278 goto out;
4281 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
4282 if (!resource) {
4283 err = -ENOMEM;
4284 goto out;
4287 if (top_hierarchy) {
4288 resource_list = &devlink->resource_list;
4289 } else {
4290 struct devlink_resource *parent_resource;
4292 parent_resource = devlink_resource_find(devlink, NULL,
4293 parent_resource_id);
4294 if (parent_resource) {
4295 resource_list = &parent_resource->resource_list;
4296 resource->parent = parent_resource;
4297 } else {
4298 kfree(resource);
4299 err = -EINVAL;
4300 goto out;
4304 resource->name = resource_name;
4305 resource->size = resource_size;
4306 resource->size_new = resource_size;
4307 resource->id = resource_id;
4308 resource->size_valid = true;
4309 memcpy(&resource->size_params, size_params,
4310 sizeof(resource->size_params));
4311 INIT_LIST_HEAD(&resource->resource_list);
4312 list_add_tail(&resource->list, resource_list);
4313 out:
4314 mutex_unlock(&devlink->lock);
4315 return err;
4317 EXPORT_SYMBOL_GPL(devlink_resource_register);
4320 * devlink_resources_unregister - free all resources
4322 * @devlink: devlink
4323 * @resource: resource
4325 void devlink_resources_unregister(struct devlink *devlink,
4326 struct devlink_resource *resource)
4328 struct devlink_resource *tmp, *child_resource;
4329 struct list_head *resource_list;
4331 if (resource)
4332 resource_list = &resource->resource_list;
4333 else
4334 resource_list = &devlink->resource_list;
4336 if (!resource)
4337 mutex_lock(&devlink->lock);
4339 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
4340 devlink_resources_unregister(devlink, child_resource);
4341 list_del(&child_resource->list);
4342 kfree(child_resource);
4345 if (!resource)
4346 mutex_unlock(&devlink->lock);
4348 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
4351 * devlink_resource_size_get - get and update size
4353 * @devlink: devlink
4354 * @resource_id: the requested resource id
4355 * @p_resource_size: ptr to update
4357 int devlink_resource_size_get(struct devlink *devlink,
4358 u64 resource_id,
4359 u64 *p_resource_size)
4361 struct devlink_resource *resource;
4362 int err = 0;
4364 mutex_lock(&devlink->lock);
4365 resource = devlink_resource_find(devlink, NULL, resource_id);
4366 if (!resource) {
4367 err = -EINVAL;
4368 goto out;
4370 *p_resource_size = resource->size_new;
4371 resource->size = resource->size_new;
4372 out:
4373 mutex_unlock(&devlink->lock);
4374 return err;
4376 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
4379 * devlink_dpipe_table_resource_set - set the resource id
4381 * @devlink: devlink
4382 * @table_name: table name
4383 * @resource_id: resource id
4384 * @resource_units: number of resource's units consumed per table's entry
4386 int devlink_dpipe_table_resource_set(struct devlink *devlink,
4387 const char *table_name, u64 resource_id,
4388 u64 resource_units)
4390 struct devlink_dpipe_table *table;
4391 int err = 0;
4393 mutex_lock(&devlink->lock);
4394 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4395 table_name);
4396 if (!table) {
4397 err = -EINVAL;
4398 goto out;
4400 table->resource_id = resource_id;
4401 table->resource_units = resource_units;
4402 table->resource_valid = true;
4403 out:
4404 mutex_unlock(&devlink->lock);
4405 return err;
4407 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
4410 * devlink_resource_occ_get_register - register occupancy getter
4412 * @devlink: devlink
4413 * @resource_id: resource id
4414 * @occ_get: occupancy getter callback
4415 * @occ_get_priv: occupancy getter callback priv
4417 void devlink_resource_occ_get_register(struct devlink *devlink,
4418 u64 resource_id,
4419 devlink_resource_occ_get_t *occ_get,
4420 void *occ_get_priv)
4422 struct devlink_resource *resource;
4424 mutex_lock(&devlink->lock);
4425 resource = devlink_resource_find(devlink, NULL, resource_id);
4426 if (WARN_ON(!resource))
4427 goto out;
4428 WARN_ON(resource->occ_get);
4430 resource->occ_get = occ_get;
4431 resource->occ_get_priv = occ_get_priv;
4432 out:
4433 mutex_unlock(&devlink->lock);
4435 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
4438 * devlink_resource_occ_get_unregister - unregister occupancy getter
4440 * @devlink: devlink
4441 * @resource_id: resource id
4443 void devlink_resource_occ_get_unregister(struct devlink *devlink,
4444 u64 resource_id)
4446 struct devlink_resource *resource;
4448 mutex_lock(&devlink->lock);
4449 resource = devlink_resource_find(devlink, NULL, resource_id);
4450 if (WARN_ON(!resource))
4451 goto out;
4452 WARN_ON(!resource->occ_get);
4454 resource->occ_get = NULL;
4455 resource->occ_get_priv = NULL;
4456 out:
4457 mutex_unlock(&devlink->lock);
4459 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
4462 * devlink_params_register - register configuration parameters
4464 * @devlink: devlink
4465 * @params: configuration parameters array
4466 * @params_count: number of parameters provided
4468 * Register the configuration parameters supported by the driver.
4470 int devlink_params_register(struct devlink *devlink,
4471 const struct devlink_param *params,
4472 size_t params_count)
4474 const struct devlink_param *param = params;
4475 int i;
4476 int err;
4478 mutex_lock(&devlink->lock);
4479 for (i = 0; i < params_count; i++, param++) {
4480 if (!param || !param->name || !param->supported_cmodes) {
4481 err = -EINVAL;
4482 goto rollback;
4484 if (param->generic) {
4485 err = devlink_param_generic_verify(param);
4486 if (err)
4487 goto rollback;
4488 } else {
4489 err = devlink_param_driver_verify(param);
4490 if (err)
4491 goto rollback;
4493 err = devlink_param_register_one(devlink, param);
4494 if (err)
4495 goto rollback;
4498 mutex_unlock(&devlink->lock);
4499 return 0;
4501 rollback:
4502 if (!i)
4503 goto unlock;
4504 for (param--; i > 0; i--, param--)
4505 devlink_param_unregister_one(devlink, param);
4506 unlock:
4507 mutex_unlock(&devlink->lock);
4508 return err;
4510 EXPORT_SYMBOL_GPL(devlink_params_register);
4513 * devlink_params_unregister - unregister configuration parameters
4514 * @devlink: devlink
4515 * @params: configuration parameters to unregister
4516 * @params_count: number of parameters provided
4518 void devlink_params_unregister(struct devlink *devlink,
4519 const struct devlink_param *params,
4520 size_t params_count)
4522 const struct devlink_param *param = params;
4523 int i;
4525 mutex_lock(&devlink->lock);
4526 for (i = 0; i < params_count; i++, param++)
4527 devlink_param_unregister_one(devlink, param);
4528 mutex_unlock(&devlink->lock);
4530 EXPORT_SYMBOL_GPL(devlink_params_unregister);
4533 * devlink_param_driverinit_value_get - get configuration parameter
4534 * value for driver initializing
4536 * @devlink: devlink
4537 * @param_id: parameter ID
4538 * @init_val: value of parameter in driverinit configuration mode
4540 * This function should be used by the driver to get driverinit
4541 * configuration for initialization after reload command.
4543 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
4544 union devlink_param_value *init_val)
4546 struct devlink_param_item *param_item;
4548 if (!devlink->ops || !devlink->ops->reload)
4549 return -EOPNOTSUPP;
4551 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4552 if (!param_item)
4553 return -EINVAL;
4555 if (!param_item->driverinit_value_valid ||
4556 !devlink_param_cmode_is_supported(param_item->param,
4557 DEVLINK_PARAM_CMODE_DRIVERINIT))
4558 return -EOPNOTSUPP;
4560 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4561 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
4562 else
4563 *init_val = param_item->driverinit_value;
4565 return 0;
4567 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
4570 * devlink_param_driverinit_value_set - set value of configuration
4571 * parameter for driverinit
4572 * configuration mode
4574 * @devlink: devlink
4575 * @param_id: parameter ID
4576 * @init_val: value of parameter to set for driverinit configuration mode
4578 * This function should be used by the driver to set driverinit
4579 * configuration mode default value.
4581 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
4582 union devlink_param_value init_val)
4584 struct devlink_param_item *param_item;
4586 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4587 if (!param_item)
4588 return -EINVAL;
4590 if (!devlink_param_cmode_is_supported(param_item->param,
4591 DEVLINK_PARAM_CMODE_DRIVERINIT))
4592 return -EOPNOTSUPP;
4594 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4595 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
4596 else
4597 param_item->driverinit_value = init_val;
4598 param_item->driverinit_value_valid = true;
4600 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4601 return 0;
4603 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
4606 * devlink_param_value_changed - notify devlink on a parameter's value
4607 * change. Should be called by the driver
4608 * right after the change.
4610 * @devlink: devlink
4611 * @param_id: parameter ID
4613 * This function should be used by the driver to notify devlink on value
4614 * change, excluding driverinit configuration mode.
4615 * For driverinit configuration mode driver should use the function
4616 * devlink_param_driverinit_value_set() instead.
4618 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
4620 struct devlink_param_item *param_item;
4622 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4623 WARN_ON(!param_item);
4625 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4627 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
4630 * devlink_param_value_str_fill - Safely fill-up the string preventing
4631 * from overflow of the preallocated buffer
4633 * @dst_val: destination devlink_param_value
4634 * @src: source buffer
4636 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
4637 const char *src)
4639 size_t len;
4641 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
4642 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
4644 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
4647 * devlink_region_create - create a new address region
4649 * @devlink: devlink
4650 * @region_name: region name
4651 * @region_max_snapshots: Maximum supported number of snapshots for region
4652 * @region_size: size of region
4654 struct devlink_region *devlink_region_create(struct devlink *devlink,
4655 const char *region_name,
4656 u32 region_max_snapshots,
4657 u64 region_size)
4659 struct devlink_region *region;
4660 int err = 0;
4662 mutex_lock(&devlink->lock);
4664 if (devlink_region_get_by_name(devlink, region_name)) {
4665 err = -EEXIST;
4666 goto unlock;
4669 region = kzalloc(sizeof(*region), GFP_KERNEL);
4670 if (!region) {
4671 err = -ENOMEM;
4672 goto unlock;
4675 region->devlink = devlink;
4676 region->max_snapshots = region_max_snapshots;
4677 region->name = region_name;
4678 region->size = region_size;
4679 INIT_LIST_HEAD(&region->snapshot_list);
4680 list_add_tail(&region->list, &devlink->region_list);
4681 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
4683 mutex_unlock(&devlink->lock);
4684 return region;
4686 unlock:
4687 mutex_unlock(&devlink->lock);
4688 return ERR_PTR(err);
4690 EXPORT_SYMBOL_GPL(devlink_region_create);
4693 * devlink_region_destroy - destroy address region
4695 * @region: devlink region to destroy
4697 void devlink_region_destroy(struct devlink_region *region)
4699 struct devlink *devlink = region->devlink;
4700 struct devlink_snapshot *snapshot, *ts;
4702 mutex_lock(&devlink->lock);
4704 /* Free all snapshots of region */
4705 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
4706 devlink_region_snapshot_del(snapshot);
4708 list_del(&region->list);
4710 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
4711 mutex_unlock(&devlink->lock);
4712 kfree(region);
4714 EXPORT_SYMBOL_GPL(devlink_region_destroy);
4717 * devlink_region_shapshot_id_get - get snapshot ID
4719 * This callback should be called when adding a new snapshot,
4720 * Driver should use the same id for multiple snapshots taken
4721 * on multiple regions at the same time/by the same trigger.
4723 * @devlink: devlink
4725 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
4727 u32 id;
4729 mutex_lock(&devlink->lock);
4730 id = ++devlink->snapshot_id;
4731 mutex_unlock(&devlink->lock);
4733 return id;
4735 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
4738 * devlink_region_snapshot_create - create a new snapshot
4739 * This will add a new snapshot of a region. The snapshot
4740 * will be stored on the region struct and can be accessed
4741 * from devlink. This is useful for future analyses of snapshots.
4742 * Multiple snapshots can be created on a region.
4743 * The @snapshot_id should be obtained using the getter function.
4745 * @devlink_region: devlink region of the snapshot
4746 * @data_len: size of snapshot data
4747 * @data: snapshot data
4748 * @snapshot_id: snapshot id to be created
4749 * @data_destructor: pointer to destructor function to free data
4751 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
4752 u8 *data, u32 snapshot_id,
4753 devlink_snapshot_data_dest_t *data_destructor)
4755 struct devlink *devlink = region->devlink;
4756 struct devlink_snapshot *snapshot;
4757 int err;
4759 mutex_lock(&devlink->lock);
4761 /* check if region can hold one more snapshot */
4762 if (region->cur_snapshots == region->max_snapshots) {
4763 err = -ENOMEM;
4764 goto unlock;
4767 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4768 err = -EEXIST;
4769 goto unlock;
4772 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4773 if (!snapshot) {
4774 err = -ENOMEM;
4775 goto unlock;
4778 snapshot->id = snapshot_id;
4779 snapshot->region = region;
4780 snapshot->data = data;
4781 snapshot->data_len = data_len;
4782 snapshot->data_destructor = data_destructor;
4784 list_add_tail(&snapshot->list, &region->snapshot_list);
4786 region->cur_snapshots++;
4788 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4789 mutex_unlock(&devlink->lock);
4790 return 0;
4792 unlock:
4793 mutex_unlock(&devlink->lock);
4794 return err;
4796 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
4798 static int __init devlink_module_init(void)
4800 return genl_register_family(&devlink_nl_family);
4803 static void __exit devlink_module_exit(void)
4805 genl_unregister_family(&devlink_nl_family);
4808 module_init(devlink_module_init);
4809 module_exit(devlink_module_exit);
4811 MODULE_LICENSE("GPL v2");
4812 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4813 MODULE_DESCRIPTION("Network physical device Netlink interface");
4814 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);