Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / core / devlink.c
blobee828e4b1007e9809e2495a759b869dafb8daa14
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/core/devlink.c - Network physical/parent device Netlink interface
5 * Heavily inspired by net/wireless/
6 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/gfp.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/netdevice.h>
18 #include <linux/spinlock.h>
19 #include <linux/refcount.h>
20 #include <linux/workqueue.h>
21 #include <linux/u64_stats_sync.h>
22 #include <linux/timekeeping.h>
23 #include <rdma/ib_verbs.h>
24 #include <net/netlink.h>
25 #include <net/genetlink.h>
26 #include <net/rtnetlink.h>
27 #include <net/net_namespace.h>
28 #include <net/sock.h>
29 #include <net/devlink.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/devlink.h>
33 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
35 .name = "destination mac",
36 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
37 .bitwidth = 48,
41 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
42 .name = "ethernet",
43 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
44 .fields = devlink_dpipe_fields_ethernet,
45 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
46 .global = true,
48 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
50 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
52 .name = "destination ip",
53 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
54 .bitwidth = 32,
58 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
59 .name = "ipv4",
60 .id = DEVLINK_DPIPE_HEADER_IPV4,
61 .fields = devlink_dpipe_fields_ipv4,
62 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
63 .global = true,
65 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
67 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
69 .name = "destination ip",
70 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
71 .bitwidth = 128,
75 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
76 .name = "ipv6",
77 .id = DEVLINK_DPIPE_HEADER_IPV6,
78 .fields = devlink_dpipe_fields_ipv6,
79 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
80 .global = true,
82 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
84 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
88 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
89 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
92 static LIST_HEAD(devlink_list);
94 /* devlink_mutex
96 * An overall lock guarding every operation coming from userspace.
97 * It also guards devlink devices list and it is taken when
98 * driver registers/unregisters it.
100 static DEFINE_MUTEX(devlink_mutex);
102 struct net *devlink_net(const struct devlink *devlink)
104 return read_pnet(&devlink->_net);
106 EXPORT_SYMBOL_GPL(devlink_net);
108 static void __devlink_net_set(struct devlink *devlink, struct net *net)
110 write_pnet(&devlink->_net, net);
113 void devlink_net_set(struct devlink *devlink, struct net *net)
115 if (WARN_ON(devlink->registered))
116 return;
117 __devlink_net_set(devlink, net);
119 EXPORT_SYMBOL_GPL(devlink_net_set);
121 static struct devlink *devlink_get_from_attrs(struct net *net,
122 struct nlattr **attrs)
124 struct devlink *devlink;
125 char *busname;
126 char *devname;
128 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
129 return ERR_PTR(-EINVAL);
131 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
132 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
134 lockdep_assert_held(&devlink_mutex);
136 list_for_each_entry(devlink, &devlink_list, list) {
137 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
138 strcmp(dev_name(devlink->dev), devname) == 0 &&
139 net_eq(devlink_net(devlink), net))
140 return devlink;
143 return ERR_PTR(-ENODEV);
146 static struct devlink *devlink_get_from_info(struct genl_info *info)
148 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
151 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
152 unsigned int port_index)
154 struct devlink_port *devlink_port;
156 list_for_each_entry(devlink_port, &devlink->port_list, list) {
157 if (devlink_port->index == port_index)
158 return devlink_port;
160 return NULL;
163 static bool devlink_port_index_exists(struct devlink *devlink,
164 unsigned int port_index)
166 return devlink_port_get_by_index(devlink, port_index);
169 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
170 struct nlattr **attrs)
172 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
173 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
174 struct devlink_port *devlink_port;
176 devlink_port = devlink_port_get_by_index(devlink, port_index);
177 if (!devlink_port)
178 return ERR_PTR(-ENODEV);
179 return devlink_port;
181 return ERR_PTR(-EINVAL);
184 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
185 struct genl_info *info)
187 return devlink_port_get_from_attrs(devlink, info->attrs);
190 struct devlink_sb {
191 struct list_head list;
192 unsigned int index;
193 u32 size;
194 u16 ingress_pools_count;
195 u16 egress_pools_count;
196 u16 ingress_tc_count;
197 u16 egress_tc_count;
200 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
202 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
205 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
206 unsigned int sb_index)
208 struct devlink_sb *devlink_sb;
210 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
211 if (devlink_sb->index == sb_index)
212 return devlink_sb;
214 return NULL;
217 static bool devlink_sb_index_exists(struct devlink *devlink,
218 unsigned int sb_index)
220 return devlink_sb_get_by_index(devlink, sb_index);
223 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
224 struct nlattr **attrs)
226 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
227 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
228 struct devlink_sb *devlink_sb;
230 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
231 if (!devlink_sb)
232 return ERR_PTR(-ENODEV);
233 return devlink_sb;
235 return ERR_PTR(-EINVAL);
238 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
239 struct genl_info *info)
241 return devlink_sb_get_from_attrs(devlink, info->attrs);
244 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
245 struct nlattr **attrs,
246 u16 *p_pool_index)
248 u16 val;
250 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
251 return -EINVAL;
253 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
254 if (val >= devlink_sb_pool_count(devlink_sb))
255 return -EINVAL;
256 *p_pool_index = val;
257 return 0;
260 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
261 struct genl_info *info,
262 u16 *p_pool_index)
264 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
265 p_pool_index);
268 static int
269 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
270 enum devlink_sb_pool_type *p_pool_type)
272 u8 val;
274 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
275 return -EINVAL;
277 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
278 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
279 val != DEVLINK_SB_POOL_TYPE_EGRESS)
280 return -EINVAL;
281 *p_pool_type = val;
282 return 0;
285 static int
286 devlink_sb_pool_type_get_from_info(struct genl_info *info,
287 enum devlink_sb_pool_type *p_pool_type)
289 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
292 static int
293 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
294 enum devlink_sb_threshold_type *p_th_type)
296 u8 val;
298 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
299 return -EINVAL;
301 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
302 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
303 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
304 return -EINVAL;
305 *p_th_type = val;
306 return 0;
309 static int
310 devlink_sb_th_type_get_from_info(struct genl_info *info,
311 enum devlink_sb_threshold_type *p_th_type)
313 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
316 static int
317 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
318 struct nlattr **attrs,
319 enum devlink_sb_pool_type pool_type,
320 u16 *p_tc_index)
322 u16 val;
324 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
325 return -EINVAL;
327 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
328 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
329 val >= devlink_sb->ingress_tc_count)
330 return -EINVAL;
331 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
332 val >= devlink_sb->egress_tc_count)
333 return -EINVAL;
334 *p_tc_index = val;
335 return 0;
338 static int
339 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
340 struct genl_info *info,
341 enum devlink_sb_pool_type pool_type,
342 u16 *p_tc_index)
344 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
345 pool_type, p_tc_index);
348 struct devlink_region {
349 struct devlink *devlink;
350 struct devlink_port *port;
351 struct list_head list;
352 union {
353 const struct devlink_region_ops *ops;
354 const struct devlink_port_region_ops *port_ops;
356 struct list_head snapshot_list;
357 u32 max_snapshots;
358 u32 cur_snapshots;
359 u64 size;
362 struct devlink_snapshot {
363 struct list_head list;
364 struct devlink_region *region;
365 u8 *data;
366 u32 id;
369 static struct devlink_region *
370 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
372 struct devlink_region *region;
374 list_for_each_entry(region, &devlink->region_list, list)
375 if (!strcmp(region->ops->name, region_name))
376 return region;
378 return NULL;
381 static struct devlink_region *
382 devlink_port_region_get_by_name(struct devlink_port *port,
383 const char *region_name)
385 struct devlink_region *region;
387 list_for_each_entry(region, &port->region_list, list)
388 if (!strcmp(region->ops->name, region_name))
389 return region;
391 return NULL;
394 static struct devlink_snapshot *
395 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
397 struct devlink_snapshot *snapshot;
399 list_for_each_entry(snapshot, &region->snapshot_list, list)
400 if (snapshot->id == id)
401 return snapshot;
403 return NULL;
406 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
407 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
409 /* The per devlink instance lock is taken by default in the pre-doit
410 * operation, yet several commands do not require this. The global
411 * devlink lock is taken and protects from disruption by user-calls.
413 #define DEVLINK_NL_FLAG_NO_LOCK BIT(2)
415 static int devlink_nl_pre_doit(const struct genl_ops *ops,
416 struct sk_buff *skb, struct genl_info *info)
418 struct devlink_port *devlink_port;
419 struct devlink *devlink;
420 int err;
422 mutex_lock(&devlink_mutex);
423 devlink = devlink_get_from_info(info);
424 if (IS_ERR(devlink)) {
425 mutex_unlock(&devlink_mutex);
426 return PTR_ERR(devlink);
428 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
429 mutex_lock(&devlink->lock);
430 info->user_ptr[0] = devlink;
431 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
432 devlink_port = devlink_port_get_from_info(devlink, info);
433 if (IS_ERR(devlink_port)) {
434 err = PTR_ERR(devlink_port);
435 goto unlock;
437 info->user_ptr[1] = devlink_port;
438 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
439 devlink_port = devlink_port_get_from_info(devlink, info);
440 if (!IS_ERR(devlink_port))
441 info->user_ptr[1] = devlink_port;
443 return 0;
445 unlock:
446 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
447 mutex_unlock(&devlink->lock);
448 mutex_unlock(&devlink_mutex);
449 return err;
452 static void devlink_nl_post_doit(const struct genl_ops *ops,
453 struct sk_buff *skb, struct genl_info *info)
455 struct devlink *devlink;
457 devlink = info->user_ptr[0];
458 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
459 mutex_unlock(&devlink->lock);
460 mutex_unlock(&devlink_mutex);
463 static struct genl_family devlink_nl_family;
465 enum devlink_multicast_groups {
466 DEVLINK_MCGRP_CONFIG,
469 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
470 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
473 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
475 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
476 return -EMSGSIZE;
477 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
478 return -EMSGSIZE;
479 return 0;
482 struct devlink_reload_combination {
483 enum devlink_reload_action action;
484 enum devlink_reload_limit limit;
487 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
489 /* can't reinitialize driver with no down time */
490 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
491 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
495 static bool
496 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
497 enum devlink_reload_limit limit)
499 int i;
501 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
502 if (devlink_reload_invalid_combinations[i].action == action &&
503 devlink_reload_invalid_combinations[i].limit == limit)
504 return true;
505 return false;
508 static bool
509 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
511 return test_bit(action, &devlink->ops->reload_actions);
514 static bool
515 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
517 return test_bit(limit, &devlink->ops->reload_limits);
520 static int devlink_reload_stat_put(struct sk_buff *msg,
521 enum devlink_reload_limit limit, u32 value)
523 struct nlattr *reload_stats_entry;
525 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
526 if (!reload_stats_entry)
527 return -EMSGSIZE;
529 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
530 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
531 goto nla_put_failure;
532 nla_nest_end(msg, reload_stats_entry);
533 return 0;
535 nla_put_failure:
536 nla_nest_cancel(msg, reload_stats_entry);
537 return -EMSGSIZE;
540 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
542 struct nlattr *reload_stats_attr, *act_info, *act_stats;
543 int i, j, stat_idx;
544 u32 value;
546 if (!is_remote)
547 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
548 else
549 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
551 if (!reload_stats_attr)
552 return -EMSGSIZE;
554 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
555 if ((!is_remote &&
556 !devlink_reload_action_is_supported(devlink, i)) ||
557 i == DEVLINK_RELOAD_ACTION_UNSPEC)
558 continue;
559 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
560 if (!act_info)
561 goto nla_put_failure;
563 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
564 goto action_info_nest_cancel;
565 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
566 if (!act_stats)
567 goto action_info_nest_cancel;
569 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
570 /* Remote stats are shown even if not locally supported.
571 * Stats of actions with unspecified limit are shown
572 * though drivers don't need to register unspecified
573 * limit.
575 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
576 !devlink_reload_limit_is_supported(devlink, j)) ||
577 devlink_reload_combination_is_invalid(i, j))
578 continue;
580 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
581 if (!is_remote)
582 value = devlink->stats.reload_stats[stat_idx];
583 else
584 value = devlink->stats.remote_reload_stats[stat_idx];
585 if (devlink_reload_stat_put(msg, j, value))
586 goto action_stats_nest_cancel;
588 nla_nest_end(msg, act_stats);
589 nla_nest_end(msg, act_info);
591 nla_nest_end(msg, reload_stats_attr);
592 return 0;
594 action_stats_nest_cancel:
595 nla_nest_cancel(msg, act_stats);
596 action_info_nest_cancel:
597 nla_nest_cancel(msg, act_info);
598 nla_put_failure:
599 nla_nest_cancel(msg, reload_stats_attr);
600 return -EMSGSIZE;
603 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
604 enum devlink_command cmd, u32 portid,
605 u32 seq, int flags)
607 struct nlattr *dev_stats;
608 void *hdr;
610 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
611 if (!hdr)
612 return -EMSGSIZE;
614 if (devlink_nl_put_handle(msg, devlink))
615 goto nla_put_failure;
616 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
617 goto nla_put_failure;
619 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
620 if (!dev_stats)
621 goto nla_put_failure;
623 if (devlink_reload_stats_put(msg, devlink, false))
624 goto dev_stats_nest_cancel;
625 if (devlink_reload_stats_put(msg, devlink, true))
626 goto dev_stats_nest_cancel;
628 nla_nest_end(msg, dev_stats);
629 genlmsg_end(msg, hdr);
630 return 0;
632 dev_stats_nest_cancel:
633 nla_nest_cancel(msg, dev_stats);
634 nla_put_failure:
635 genlmsg_cancel(msg, hdr);
636 return -EMSGSIZE;
639 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
641 struct sk_buff *msg;
642 int err;
644 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
647 if (!msg)
648 return;
650 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
651 if (err) {
652 nlmsg_free(msg);
653 return;
656 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
657 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
660 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
661 struct devlink_port *devlink_port)
663 struct devlink_port_attrs *attrs = &devlink_port->attrs;
665 if (!devlink_port->attrs_set)
666 return 0;
667 if (attrs->lanes) {
668 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
669 return -EMSGSIZE;
671 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
672 return -EMSGSIZE;
673 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
674 return -EMSGSIZE;
675 switch (devlink_port->attrs.flavour) {
676 case DEVLINK_PORT_FLAVOUR_PCI_PF:
677 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
678 attrs->pci_pf.controller) ||
679 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
680 return -EMSGSIZE;
681 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
682 return -EMSGSIZE;
683 break;
684 case DEVLINK_PORT_FLAVOUR_PCI_VF:
685 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
686 attrs->pci_vf.controller) ||
687 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
688 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
689 return -EMSGSIZE;
690 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
691 return -EMSGSIZE;
692 break;
693 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
694 case DEVLINK_PORT_FLAVOUR_CPU:
695 case DEVLINK_PORT_FLAVOUR_DSA:
696 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
697 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
698 attrs->phys.port_number))
699 return -EMSGSIZE;
700 if (!attrs->split)
701 return 0;
702 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
703 attrs->phys.port_number))
704 return -EMSGSIZE;
705 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
706 attrs->phys.split_subport_number))
707 return -EMSGSIZE;
708 break;
709 default:
710 break;
712 return 0;
715 static int
716 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
717 struct netlink_ext_ack *extack)
719 struct devlink *devlink = port->devlink;
720 const struct devlink_ops *ops;
721 struct nlattr *function_attr;
722 bool empty_nest = true;
723 int err = 0;
725 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
726 if (!function_attr)
727 return -EMSGSIZE;
729 ops = devlink->ops;
730 if (ops->port_function_hw_addr_get) {
731 int hw_addr_len;
732 u8 hw_addr[MAX_ADDR_LEN];
734 err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack);
735 if (err == -EOPNOTSUPP) {
736 /* Port function attributes are optional for a port. If port doesn't
737 * support function attribute, returning -EOPNOTSUPP is not an error.
739 err = 0;
740 goto out;
741 } else if (err) {
742 goto out;
744 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
745 if (err)
746 goto out;
747 empty_nest = false;
750 out:
751 if (err || empty_nest)
752 nla_nest_cancel(msg, function_attr);
753 else
754 nla_nest_end(msg, function_attr);
755 return err;
758 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
759 struct devlink_port *devlink_port,
760 enum devlink_command cmd, u32 portid,
761 u32 seq, int flags,
762 struct netlink_ext_ack *extack)
764 void *hdr;
766 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
767 if (!hdr)
768 return -EMSGSIZE;
770 if (devlink_nl_put_handle(msg, devlink))
771 goto nla_put_failure;
772 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
773 goto nla_put_failure;
775 /* Hold rtnl lock while accessing port's netdev attributes. */
776 rtnl_lock();
777 spin_lock_bh(&devlink_port->type_lock);
778 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
779 goto nla_put_failure_type_locked;
780 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
781 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
782 devlink_port->desired_type))
783 goto nla_put_failure_type_locked;
784 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
785 struct net *net = devlink_net(devlink_port->devlink);
786 struct net_device *netdev = devlink_port->type_dev;
788 if (netdev && net_eq(net, dev_net(netdev)) &&
789 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
790 netdev->ifindex) ||
791 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
792 netdev->name)))
793 goto nla_put_failure_type_locked;
795 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
796 struct ib_device *ibdev = devlink_port->type_dev;
798 if (ibdev &&
799 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
800 ibdev->name))
801 goto nla_put_failure_type_locked;
803 spin_unlock_bh(&devlink_port->type_lock);
804 rtnl_unlock();
805 if (devlink_nl_port_attrs_put(msg, devlink_port))
806 goto nla_put_failure;
807 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
808 goto nla_put_failure;
810 genlmsg_end(msg, hdr);
811 return 0;
813 nla_put_failure_type_locked:
814 spin_unlock_bh(&devlink_port->type_lock);
815 rtnl_unlock();
816 nla_put_failure:
817 genlmsg_cancel(msg, hdr);
818 return -EMSGSIZE;
821 static void devlink_port_notify(struct devlink_port *devlink_port,
822 enum devlink_command cmd)
824 struct devlink *devlink = devlink_port->devlink;
825 struct sk_buff *msg;
826 int err;
828 if (!devlink_port->registered)
829 return;
831 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
833 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
834 if (!msg)
835 return;
837 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0,
838 NULL);
839 if (err) {
840 nlmsg_free(msg);
841 return;
844 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
845 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
848 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
850 struct devlink *devlink = info->user_ptr[0];
851 struct sk_buff *msg;
852 int err;
854 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
855 if (!msg)
856 return -ENOMEM;
858 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
859 info->snd_portid, info->snd_seq, 0);
860 if (err) {
861 nlmsg_free(msg);
862 return err;
865 return genlmsg_reply(msg, info);
868 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
869 struct netlink_callback *cb)
871 struct devlink *devlink;
872 int start = cb->args[0];
873 int idx = 0;
874 int err;
876 mutex_lock(&devlink_mutex);
877 list_for_each_entry(devlink, &devlink_list, list) {
878 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
879 continue;
880 if (idx < start) {
881 idx++;
882 continue;
884 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
885 NETLINK_CB(cb->skb).portid,
886 cb->nlh->nlmsg_seq, NLM_F_MULTI);
887 if (err)
888 goto out;
889 idx++;
891 out:
892 mutex_unlock(&devlink_mutex);
894 cb->args[0] = idx;
895 return msg->len;
898 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
899 struct genl_info *info)
901 struct devlink_port *devlink_port = info->user_ptr[1];
902 struct devlink *devlink = devlink_port->devlink;
903 struct sk_buff *msg;
904 int err;
906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
907 if (!msg)
908 return -ENOMEM;
910 err = devlink_nl_port_fill(msg, devlink, devlink_port,
911 DEVLINK_CMD_PORT_NEW,
912 info->snd_portid, info->snd_seq, 0,
913 info->extack);
914 if (err) {
915 nlmsg_free(msg);
916 return err;
919 return genlmsg_reply(msg, info);
922 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
923 struct netlink_callback *cb)
925 struct devlink *devlink;
926 struct devlink_port *devlink_port;
927 int start = cb->args[0];
928 int idx = 0;
929 int err;
931 mutex_lock(&devlink_mutex);
932 list_for_each_entry(devlink, &devlink_list, list) {
933 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
934 continue;
935 mutex_lock(&devlink->lock);
936 list_for_each_entry(devlink_port, &devlink->port_list, list) {
937 if (idx < start) {
938 idx++;
939 continue;
941 err = devlink_nl_port_fill(msg, devlink, devlink_port,
942 DEVLINK_CMD_NEW,
943 NETLINK_CB(cb->skb).portid,
944 cb->nlh->nlmsg_seq,
945 NLM_F_MULTI,
946 cb->extack);
947 if (err) {
948 mutex_unlock(&devlink->lock);
949 goto out;
951 idx++;
953 mutex_unlock(&devlink->lock);
955 out:
956 mutex_unlock(&devlink_mutex);
958 cb->args[0] = idx;
959 return msg->len;
962 static int devlink_port_type_set(struct devlink *devlink,
963 struct devlink_port *devlink_port,
964 enum devlink_port_type port_type)
967 int err;
969 if (devlink->ops->port_type_set) {
970 if (port_type == devlink_port->type)
971 return 0;
972 err = devlink->ops->port_type_set(devlink_port, port_type);
973 if (err)
974 return err;
975 devlink_port->desired_type = port_type;
976 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
977 return 0;
979 return -EOPNOTSUPP;
982 static int
983 devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port,
984 const struct nlattr *attr, struct netlink_ext_ack *extack)
986 const struct devlink_ops *ops;
987 const u8 *hw_addr;
988 int hw_addr_len;
989 int err;
991 hw_addr = nla_data(attr);
992 hw_addr_len = nla_len(attr);
993 if (hw_addr_len > MAX_ADDR_LEN) {
994 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
995 return -EINVAL;
997 if (port->type == DEVLINK_PORT_TYPE_ETH) {
998 if (hw_addr_len != ETH_ALEN) {
999 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1000 return -EINVAL;
1002 if (!is_unicast_ether_addr(hw_addr)) {
1003 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1004 return -EINVAL;
1008 ops = devlink->ops;
1009 if (!ops->port_function_hw_addr_set) {
1010 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1011 return -EOPNOTSUPP;
1014 err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack);
1015 if (err)
1016 return err;
1018 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1019 return 0;
1022 static int
1023 devlink_port_function_set(struct devlink *devlink, struct devlink_port *port,
1024 const struct nlattr *attr, struct netlink_ext_ack *extack)
1026 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1027 int err;
1029 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1030 devlink_function_nl_policy, extack);
1031 if (err < 0) {
1032 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1033 return err;
1036 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1037 if (attr)
1038 err = devlink_port_function_hw_addr_set(devlink, port, attr, extack);
1040 return err;
1043 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1044 struct genl_info *info)
1046 struct devlink_port *devlink_port = info->user_ptr[1];
1047 struct devlink *devlink = devlink_port->devlink;
1048 int err;
1050 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1051 enum devlink_port_type port_type;
1053 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1054 err = devlink_port_type_set(devlink, devlink_port, port_type);
1055 if (err)
1056 return err;
1059 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1060 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1061 struct netlink_ext_ack *extack = info->extack;
1063 err = devlink_port_function_set(devlink, devlink_port, attr, extack);
1064 if (err)
1065 return err;
1068 return 0;
1071 static int devlink_port_split(struct devlink *devlink, u32 port_index,
1072 u32 count, struct netlink_ext_ack *extack)
1075 if (devlink->ops->port_split)
1076 return devlink->ops->port_split(devlink, port_index, count,
1077 extack);
1078 return -EOPNOTSUPP;
1081 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1082 struct genl_info *info)
1084 struct devlink *devlink = info->user_ptr[0];
1085 struct devlink_port *devlink_port;
1086 u32 port_index;
1087 u32 count;
1089 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
1090 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1091 return -EINVAL;
1093 devlink_port = devlink_port_get_from_info(devlink, info);
1094 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1095 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1097 if (IS_ERR(devlink_port))
1098 return -EINVAL;
1100 if (!devlink_port->attrs.splittable) {
1101 /* Split ports cannot be split. */
1102 if (devlink_port->attrs.split)
1103 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1104 else
1105 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1106 return -EINVAL;
1109 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1110 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1111 return -EINVAL;
1114 return devlink_port_split(devlink, port_index, count, info->extack);
1117 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
1118 struct netlink_ext_ack *extack)
1121 if (devlink->ops->port_unsplit)
1122 return devlink->ops->port_unsplit(devlink, port_index, extack);
1123 return -EOPNOTSUPP;
1126 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1127 struct genl_info *info)
1129 struct devlink *devlink = info->user_ptr[0];
1130 u32 port_index;
1132 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
1133 return -EINVAL;
1135 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1136 return devlink_port_unsplit(devlink, port_index, info->extack);
1139 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
1140 struct devlink_sb *devlink_sb,
1141 enum devlink_command cmd, u32 portid,
1142 u32 seq, int flags)
1144 void *hdr;
1146 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1147 if (!hdr)
1148 return -EMSGSIZE;
1150 if (devlink_nl_put_handle(msg, devlink))
1151 goto nla_put_failure;
1152 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1153 goto nla_put_failure;
1154 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
1155 goto nla_put_failure;
1156 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
1157 devlink_sb->ingress_pools_count))
1158 goto nla_put_failure;
1159 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
1160 devlink_sb->egress_pools_count))
1161 goto nla_put_failure;
1162 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
1163 devlink_sb->ingress_tc_count))
1164 goto nla_put_failure;
1165 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
1166 devlink_sb->egress_tc_count))
1167 goto nla_put_failure;
1169 genlmsg_end(msg, hdr);
1170 return 0;
1172 nla_put_failure:
1173 genlmsg_cancel(msg, hdr);
1174 return -EMSGSIZE;
1177 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
1178 struct genl_info *info)
1180 struct devlink *devlink = info->user_ptr[0];
1181 struct devlink_sb *devlink_sb;
1182 struct sk_buff *msg;
1183 int err;
1185 devlink_sb = devlink_sb_get_from_info(devlink, info);
1186 if (IS_ERR(devlink_sb))
1187 return PTR_ERR(devlink_sb);
1189 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1190 if (!msg)
1191 return -ENOMEM;
1193 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1194 DEVLINK_CMD_SB_NEW,
1195 info->snd_portid, info->snd_seq, 0);
1196 if (err) {
1197 nlmsg_free(msg);
1198 return err;
1201 return genlmsg_reply(msg, info);
1204 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
1205 struct netlink_callback *cb)
1207 struct devlink *devlink;
1208 struct devlink_sb *devlink_sb;
1209 int start = cb->args[0];
1210 int idx = 0;
1211 int err;
1213 mutex_lock(&devlink_mutex);
1214 list_for_each_entry(devlink, &devlink_list, list) {
1215 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1216 continue;
1217 mutex_lock(&devlink->lock);
1218 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1219 if (idx < start) {
1220 idx++;
1221 continue;
1223 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1224 DEVLINK_CMD_SB_NEW,
1225 NETLINK_CB(cb->skb).portid,
1226 cb->nlh->nlmsg_seq,
1227 NLM_F_MULTI);
1228 if (err) {
1229 mutex_unlock(&devlink->lock);
1230 goto out;
1232 idx++;
1234 mutex_unlock(&devlink->lock);
1236 out:
1237 mutex_unlock(&devlink_mutex);
1239 cb->args[0] = idx;
1240 return msg->len;
1243 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
1244 struct devlink_sb *devlink_sb,
1245 u16 pool_index, enum devlink_command cmd,
1246 u32 portid, u32 seq, int flags)
1248 struct devlink_sb_pool_info pool_info;
1249 void *hdr;
1250 int err;
1252 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
1253 pool_index, &pool_info);
1254 if (err)
1255 return err;
1257 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1258 if (!hdr)
1259 return -EMSGSIZE;
1261 if (devlink_nl_put_handle(msg, devlink))
1262 goto nla_put_failure;
1263 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1264 goto nla_put_failure;
1265 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1266 goto nla_put_failure;
1267 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
1268 goto nla_put_failure;
1269 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
1270 goto nla_put_failure;
1271 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1272 pool_info.threshold_type))
1273 goto nla_put_failure;
1274 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
1275 pool_info.cell_size))
1276 goto nla_put_failure;
1278 genlmsg_end(msg, hdr);
1279 return 0;
1281 nla_put_failure:
1282 genlmsg_cancel(msg, hdr);
1283 return -EMSGSIZE;
1286 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
1287 struct genl_info *info)
1289 struct devlink *devlink = info->user_ptr[0];
1290 struct devlink_sb *devlink_sb;
1291 struct sk_buff *msg;
1292 u16 pool_index;
1293 int err;
1295 devlink_sb = devlink_sb_get_from_info(devlink, info);
1296 if (IS_ERR(devlink_sb))
1297 return PTR_ERR(devlink_sb);
1299 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1300 &pool_index);
1301 if (err)
1302 return err;
1304 if (!devlink->ops->sb_pool_get)
1305 return -EOPNOTSUPP;
1307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1308 if (!msg)
1309 return -ENOMEM;
1311 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
1312 DEVLINK_CMD_SB_POOL_NEW,
1313 info->snd_portid, info->snd_seq, 0);
1314 if (err) {
1315 nlmsg_free(msg);
1316 return err;
1319 return genlmsg_reply(msg, info);
1322 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1323 struct devlink *devlink,
1324 struct devlink_sb *devlink_sb,
1325 u32 portid, u32 seq)
1327 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1328 u16 pool_index;
1329 int err;
1331 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1332 if (*p_idx < start) {
1333 (*p_idx)++;
1334 continue;
1336 err = devlink_nl_sb_pool_fill(msg, devlink,
1337 devlink_sb,
1338 pool_index,
1339 DEVLINK_CMD_SB_POOL_NEW,
1340 portid, seq, NLM_F_MULTI);
1341 if (err)
1342 return err;
1343 (*p_idx)++;
1345 return 0;
1348 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1349 struct netlink_callback *cb)
1351 struct devlink *devlink;
1352 struct devlink_sb *devlink_sb;
1353 int start = cb->args[0];
1354 int idx = 0;
1355 int err = 0;
1357 mutex_lock(&devlink_mutex);
1358 list_for_each_entry(devlink, &devlink_list, list) {
1359 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1360 !devlink->ops->sb_pool_get)
1361 continue;
1362 mutex_lock(&devlink->lock);
1363 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1364 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1365 devlink_sb,
1366 NETLINK_CB(cb->skb).portid,
1367 cb->nlh->nlmsg_seq);
1368 if (err == -EOPNOTSUPP) {
1369 err = 0;
1370 } else if (err) {
1371 mutex_unlock(&devlink->lock);
1372 goto out;
1375 mutex_unlock(&devlink->lock);
1377 out:
1378 mutex_unlock(&devlink_mutex);
1380 if (err != -EMSGSIZE)
1381 return err;
1383 cb->args[0] = idx;
1384 return msg->len;
1387 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1388 u16 pool_index, u32 size,
1389 enum devlink_sb_threshold_type threshold_type,
1390 struct netlink_ext_ack *extack)
1393 const struct devlink_ops *ops = devlink->ops;
1395 if (ops->sb_pool_set)
1396 return ops->sb_pool_set(devlink, sb_index, pool_index,
1397 size, threshold_type, extack);
1398 return -EOPNOTSUPP;
1401 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1402 struct genl_info *info)
1404 struct devlink *devlink = info->user_ptr[0];
1405 enum devlink_sb_threshold_type threshold_type;
1406 struct devlink_sb *devlink_sb;
1407 u16 pool_index;
1408 u32 size;
1409 int err;
1411 devlink_sb = devlink_sb_get_from_info(devlink, info);
1412 if (IS_ERR(devlink_sb))
1413 return PTR_ERR(devlink_sb);
1415 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1416 &pool_index);
1417 if (err)
1418 return err;
1420 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1421 if (err)
1422 return err;
1424 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1425 return -EINVAL;
1427 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1428 return devlink_sb_pool_set(devlink, devlink_sb->index,
1429 pool_index, size, threshold_type,
1430 info->extack);
1433 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1434 struct devlink *devlink,
1435 struct devlink_port *devlink_port,
1436 struct devlink_sb *devlink_sb,
1437 u16 pool_index,
1438 enum devlink_command cmd,
1439 u32 portid, u32 seq, int flags)
1441 const struct devlink_ops *ops = devlink->ops;
1442 u32 threshold;
1443 void *hdr;
1444 int err;
1446 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1447 pool_index, &threshold);
1448 if (err)
1449 return err;
1451 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1452 if (!hdr)
1453 return -EMSGSIZE;
1455 if (devlink_nl_put_handle(msg, devlink))
1456 goto nla_put_failure;
1457 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1458 goto nla_put_failure;
1459 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1460 goto nla_put_failure;
1461 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1462 goto nla_put_failure;
1463 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1464 goto nla_put_failure;
1466 if (ops->sb_occ_port_pool_get) {
1467 u32 cur;
1468 u32 max;
1470 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1471 pool_index, &cur, &max);
1472 if (err && err != -EOPNOTSUPP)
1473 goto sb_occ_get_failure;
1474 if (!err) {
1475 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1476 goto nla_put_failure;
1477 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1478 goto nla_put_failure;
1482 genlmsg_end(msg, hdr);
1483 return 0;
1485 nla_put_failure:
1486 err = -EMSGSIZE;
1487 sb_occ_get_failure:
1488 genlmsg_cancel(msg, hdr);
1489 return err;
1492 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1493 struct genl_info *info)
1495 struct devlink_port *devlink_port = info->user_ptr[1];
1496 struct devlink *devlink = devlink_port->devlink;
1497 struct devlink_sb *devlink_sb;
1498 struct sk_buff *msg;
1499 u16 pool_index;
1500 int err;
1502 devlink_sb = devlink_sb_get_from_info(devlink, info);
1503 if (IS_ERR(devlink_sb))
1504 return PTR_ERR(devlink_sb);
1506 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1507 &pool_index);
1508 if (err)
1509 return err;
1511 if (!devlink->ops->sb_port_pool_get)
1512 return -EOPNOTSUPP;
1514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1515 if (!msg)
1516 return -ENOMEM;
1518 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1519 devlink_sb, pool_index,
1520 DEVLINK_CMD_SB_PORT_POOL_NEW,
1521 info->snd_portid, info->snd_seq, 0);
1522 if (err) {
1523 nlmsg_free(msg);
1524 return err;
1527 return genlmsg_reply(msg, info);
1530 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1531 struct devlink *devlink,
1532 struct devlink_sb *devlink_sb,
1533 u32 portid, u32 seq)
1535 struct devlink_port *devlink_port;
1536 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1537 u16 pool_index;
1538 int err;
1540 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1541 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1542 if (*p_idx < start) {
1543 (*p_idx)++;
1544 continue;
1546 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1547 devlink_port,
1548 devlink_sb,
1549 pool_index,
1550 DEVLINK_CMD_SB_PORT_POOL_NEW,
1551 portid, seq,
1552 NLM_F_MULTI);
1553 if (err)
1554 return err;
1555 (*p_idx)++;
1558 return 0;
1561 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1562 struct netlink_callback *cb)
1564 struct devlink *devlink;
1565 struct devlink_sb *devlink_sb;
1566 int start = cb->args[0];
1567 int idx = 0;
1568 int err = 0;
1570 mutex_lock(&devlink_mutex);
1571 list_for_each_entry(devlink, &devlink_list, list) {
1572 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1573 !devlink->ops->sb_port_pool_get)
1574 continue;
1575 mutex_lock(&devlink->lock);
1576 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1577 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1578 devlink, devlink_sb,
1579 NETLINK_CB(cb->skb).portid,
1580 cb->nlh->nlmsg_seq);
1581 if (err == -EOPNOTSUPP) {
1582 err = 0;
1583 } else if (err) {
1584 mutex_unlock(&devlink->lock);
1585 goto out;
1588 mutex_unlock(&devlink->lock);
1590 out:
1591 mutex_unlock(&devlink_mutex);
1593 if (err != -EMSGSIZE)
1594 return err;
1596 cb->args[0] = idx;
1597 return msg->len;
1600 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1601 unsigned int sb_index, u16 pool_index,
1602 u32 threshold,
1603 struct netlink_ext_ack *extack)
1606 const struct devlink_ops *ops = devlink_port->devlink->ops;
1608 if (ops->sb_port_pool_set)
1609 return ops->sb_port_pool_set(devlink_port, sb_index,
1610 pool_index, threshold, extack);
1611 return -EOPNOTSUPP;
1614 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1615 struct genl_info *info)
1617 struct devlink_port *devlink_port = info->user_ptr[1];
1618 struct devlink *devlink = info->user_ptr[0];
1619 struct devlink_sb *devlink_sb;
1620 u16 pool_index;
1621 u32 threshold;
1622 int err;
1624 devlink_sb = devlink_sb_get_from_info(devlink, info);
1625 if (IS_ERR(devlink_sb))
1626 return PTR_ERR(devlink_sb);
1628 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1629 &pool_index);
1630 if (err)
1631 return err;
1633 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1634 return -EINVAL;
1636 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1637 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1638 pool_index, threshold, info->extack);
1641 static int
1642 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1643 struct devlink_port *devlink_port,
1644 struct devlink_sb *devlink_sb, u16 tc_index,
1645 enum devlink_sb_pool_type pool_type,
1646 enum devlink_command cmd,
1647 u32 portid, u32 seq, int flags)
1649 const struct devlink_ops *ops = devlink->ops;
1650 u16 pool_index;
1651 u32 threshold;
1652 void *hdr;
1653 int err;
1655 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1656 tc_index, pool_type,
1657 &pool_index, &threshold);
1658 if (err)
1659 return err;
1661 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1662 if (!hdr)
1663 return -EMSGSIZE;
1665 if (devlink_nl_put_handle(msg, devlink))
1666 goto nla_put_failure;
1667 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1668 goto nla_put_failure;
1669 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1670 goto nla_put_failure;
1671 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1672 goto nla_put_failure;
1673 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1674 goto nla_put_failure;
1675 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1676 goto nla_put_failure;
1677 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1678 goto nla_put_failure;
1680 if (ops->sb_occ_tc_port_bind_get) {
1681 u32 cur;
1682 u32 max;
1684 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1685 devlink_sb->index,
1686 tc_index, pool_type,
1687 &cur, &max);
1688 if (err && err != -EOPNOTSUPP)
1689 return err;
1690 if (!err) {
1691 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1692 goto nla_put_failure;
1693 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1694 goto nla_put_failure;
1698 genlmsg_end(msg, hdr);
1699 return 0;
1701 nla_put_failure:
1702 genlmsg_cancel(msg, hdr);
1703 return -EMSGSIZE;
1706 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1707 struct genl_info *info)
1709 struct devlink_port *devlink_port = info->user_ptr[1];
1710 struct devlink *devlink = devlink_port->devlink;
1711 struct devlink_sb *devlink_sb;
1712 struct sk_buff *msg;
1713 enum devlink_sb_pool_type pool_type;
1714 u16 tc_index;
1715 int err;
1717 devlink_sb = devlink_sb_get_from_info(devlink, info);
1718 if (IS_ERR(devlink_sb))
1719 return PTR_ERR(devlink_sb);
1721 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1722 if (err)
1723 return err;
1725 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1726 pool_type, &tc_index);
1727 if (err)
1728 return err;
1730 if (!devlink->ops->sb_tc_pool_bind_get)
1731 return -EOPNOTSUPP;
1733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1734 if (!msg)
1735 return -ENOMEM;
1737 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1738 devlink_sb, tc_index, pool_type,
1739 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1740 info->snd_portid,
1741 info->snd_seq, 0);
1742 if (err) {
1743 nlmsg_free(msg);
1744 return err;
1747 return genlmsg_reply(msg, info);
1750 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1751 int start, int *p_idx,
1752 struct devlink *devlink,
1753 struct devlink_sb *devlink_sb,
1754 u32 portid, u32 seq)
1756 struct devlink_port *devlink_port;
1757 u16 tc_index;
1758 int err;
1760 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1761 for (tc_index = 0;
1762 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1763 if (*p_idx < start) {
1764 (*p_idx)++;
1765 continue;
1767 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1768 devlink_port,
1769 devlink_sb,
1770 tc_index,
1771 DEVLINK_SB_POOL_TYPE_INGRESS,
1772 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1773 portid, seq,
1774 NLM_F_MULTI);
1775 if (err)
1776 return err;
1777 (*p_idx)++;
1779 for (tc_index = 0;
1780 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1781 if (*p_idx < start) {
1782 (*p_idx)++;
1783 continue;
1785 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1786 devlink_port,
1787 devlink_sb,
1788 tc_index,
1789 DEVLINK_SB_POOL_TYPE_EGRESS,
1790 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1791 portid, seq,
1792 NLM_F_MULTI);
1793 if (err)
1794 return err;
1795 (*p_idx)++;
1798 return 0;
1801 static int
1802 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1803 struct netlink_callback *cb)
1805 struct devlink *devlink;
1806 struct devlink_sb *devlink_sb;
1807 int start = cb->args[0];
1808 int idx = 0;
1809 int err = 0;
1811 mutex_lock(&devlink_mutex);
1812 list_for_each_entry(devlink, &devlink_list, list) {
1813 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1814 !devlink->ops->sb_tc_pool_bind_get)
1815 continue;
1817 mutex_lock(&devlink->lock);
1818 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1819 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1820 devlink,
1821 devlink_sb,
1822 NETLINK_CB(cb->skb).portid,
1823 cb->nlh->nlmsg_seq);
1824 if (err == -EOPNOTSUPP) {
1825 err = 0;
1826 } else if (err) {
1827 mutex_unlock(&devlink->lock);
1828 goto out;
1831 mutex_unlock(&devlink->lock);
1833 out:
1834 mutex_unlock(&devlink_mutex);
1836 if (err != -EMSGSIZE)
1837 return err;
1839 cb->args[0] = idx;
1840 return msg->len;
1843 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1844 unsigned int sb_index, u16 tc_index,
1845 enum devlink_sb_pool_type pool_type,
1846 u16 pool_index, u32 threshold,
1847 struct netlink_ext_ack *extack)
1850 const struct devlink_ops *ops = devlink_port->devlink->ops;
1852 if (ops->sb_tc_pool_bind_set)
1853 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1854 tc_index, pool_type,
1855 pool_index, threshold, extack);
1856 return -EOPNOTSUPP;
1859 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1860 struct genl_info *info)
1862 struct devlink_port *devlink_port = info->user_ptr[1];
1863 struct devlink *devlink = info->user_ptr[0];
1864 enum devlink_sb_pool_type pool_type;
1865 struct devlink_sb *devlink_sb;
1866 u16 tc_index;
1867 u16 pool_index;
1868 u32 threshold;
1869 int err;
1871 devlink_sb = devlink_sb_get_from_info(devlink, info);
1872 if (IS_ERR(devlink_sb))
1873 return PTR_ERR(devlink_sb);
1875 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1876 if (err)
1877 return err;
1879 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1880 pool_type, &tc_index);
1881 if (err)
1882 return err;
1884 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1885 &pool_index);
1886 if (err)
1887 return err;
1889 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1890 return -EINVAL;
1892 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1893 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1894 tc_index, pool_type,
1895 pool_index, threshold, info->extack);
1898 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1899 struct genl_info *info)
1901 struct devlink *devlink = info->user_ptr[0];
1902 const struct devlink_ops *ops = devlink->ops;
1903 struct devlink_sb *devlink_sb;
1905 devlink_sb = devlink_sb_get_from_info(devlink, info);
1906 if (IS_ERR(devlink_sb))
1907 return PTR_ERR(devlink_sb);
1909 if (ops->sb_occ_snapshot)
1910 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1911 return -EOPNOTSUPP;
1914 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1915 struct genl_info *info)
1917 struct devlink *devlink = info->user_ptr[0];
1918 const struct devlink_ops *ops = devlink->ops;
1919 struct devlink_sb *devlink_sb;
1921 devlink_sb = devlink_sb_get_from_info(devlink, info);
1922 if (IS_ERR(devlink_sb))
1923 return PTR_ERR(devlink_sb);
1925 if (ops->sb_occ_max_clear)
1926 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1927 return -EOPNOTSUPP;
1930 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1931 enum devlink_command cmd, u32 portid,
1932 u32 seq, int flags)
1934 const struct devlink_ops *ops = devlink->ops;
1935 enum devlink_eswitch_encap_mode encap_mode;
1936 u8 inline_mode;
1937 void *hdr;
1938 int err = 0;
1939 u16 mode;
1941 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1942 if (!hdr)
1943 return -EMSGSIZE;
1945 err = devlink_nl_put_handle(msg, devlink);
1946 if (err)
1947 goto nla_put_failure;
1949 if (ops->eswitch_mode_get) {
1950 err = ops->eswitch_mode_get(devlink, &mode);
1951 if (err)
1952 goto nla_put_failure;
1953 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1954 if (err)
1955 goto nla_put_failure;
1958 if (ops->eswitch_inline_mode_get) {
1959 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1960 if (err)
1961 goto nla_put_failure;
1962 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1963 inline_mode);
1964 if (err)
1965 goto nla_put_failure;
1968 if (ops->eswitch_encap_mode_get) {
1969 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1970 if (err)
1971 goto nla_put_failure;
1972 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1973 if (err)
1974 goto nla_put_failure;
1977 genlmsg_end(msg, hdr);
1978 return 0;
1980 nla_put_failure:
1981 genlmsg_cancel(msg, hdr);
1982 return err;
1985 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1986 struct genl_info *info)
1988 struct devlink *devlink = info->user_ptr[0];
1989 struct sk_buff *msg;
1990 int err;
1992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1993 if (!msg)
1994 return -ENOMEM;
1996 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1997 info->snd_portid, info->snd_seq, 0);
1999 if (err) {
2000 nlmsg_free(msg);
2001 return err;
2004 return genlmsg_reply(msg, info);
2007 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
2008 struct genl_info *info)
2010 struct devlink *devlink = info->user_ptr[0];
2011 const struct devlink_ops *ops = devlink->ops;
2012 enum devlink_eswitch_encap_mode encap_mode;
2013 u8 inline_mode;
2014 int err = 0;
2015 u16 mode;
2017 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
2018 if (!ops->eswitch_mode_set)
2019 return -EOPNOTSUPP;
2020 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
2021 err = ops->eswitch_mode_set(devlink, mode, info->extack);
2022 if (err)
2023 return err;
2026 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2027 if (!ops->eswitch_inline_mode_set)
2028 return -EOPNOTSUPP;
2029 inline_mode = nla_get_u8(
2030 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
2031 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
2032 info->extack);
2033 if (err)
2034 return err;
2037 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2038 if (!ops->eswitch_encap_mode_set)
2039 return -EOPNOTSUPP;
2040 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2041 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
2042 info->extack);
2043 if (err)
2044 return err;
2047 return 0;
2050 int devlink_dpipe_match_put(struct sk_buff *skb,
2051 struct devlink_dpipe_match *match)
2053 struct devlink_dpipe_header *header = match->header;
2054 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2055 struct nlattr *match_attr;
2057 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2058 if (!match_attr)
2059 return -EMSGSIZE;
2061 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2062 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2063 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2064 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2065 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2066 goto nla_put_failure;
2068 nla_nest_end(skb, match_attr);
2069 return 0;
2071 nla_put_failure:
2072 nla_nest_cancel(skb, match_attr);
2073 return -EMSGSIZE;
2075 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2077 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2078 struct sk_buff *skb)
2080 struct nlattr *matches_attr;
2082 matches_attr = nla_nest_start_noflag(skb,
2083 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2084 if (!matches_attr)
2085 return -EMSGSIZE;
2087 if (table->table_ops->matches_dump(table->priv, skb))
2088 goto nla_put_failure;
2090 nla_nest_end(skb, matches_attr);
2091 return 0;
2093 nla_put_failure:
2094 nla_nest_cancel(skb, matches_attr);
2095 return -EMSGSIZE;
2098 int devlink_dpipe_action_put(struct sk_buff *skb,
2099 struct devlink_dpipe_action *action)
2101 struct devlink_dpipe_header *header = action->header;
2102 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2103 struct nlattr *action_attr;
2105 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2106 if (!action_attr)
2107 return -EMSGSIZE;
2109 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2110 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2111 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2112 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2113 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2114 goto nla_put_failure;
2116 nla_nest_end(skb, action_attr);
2117 return 0;
2119 nla_put_failure:
2120 nla_nest_cancel(skb, action_attr);
2121 return -EMSGSIZE;
2123 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
2125 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
2126 struct sk_buff *skb)
2128 struct nlattr *actions_attr;
2130 actions_attr = nla_nest_start_noflag(skb,
2131 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
2132 if (!actions_attr)
2133 return -EMSGSIZE;
2135 if (table->table_ops->actions_dump(table->priv, skb))
2136 goto nla_put_failure;
2138 nla_nest_end(skb, actions_attr);
2139 return 0;
2141 nla_put_failure:
2142 nla_nest_cancel(skb, actions_attr);
2143 return -EMSGSIZE;
2146 static int devlink_dpipe_table_put(struct sk_buff *skb,
2147 struct devlink_dpipe_table *table)
2149 struct nlattr *table_attr;
2150 u64 table_size;
2152 table_size = table->table_ops->size_get(table->priv);
2153 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
2154 if (!table_attr)
2155 return -EMSGSIZE;
2157 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2158 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2159 DEVLINK_ATTR_PAD))
2160 goto nla_put_failure;
2161 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2162 table->counters_enabled))
2163 goto nla_put_failure;
2165 if (table->resource_valid) {
2166 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2167 table->resource_id, DEVLINK_ATTR_PAD) ||
2168 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2169 table->resource_units, DEVLINK_ATTR_PAD))
2170 goto nla_put_failure;
2172 if (devlink_dpipe_matches_put(table, skb))
2173 goto nla_put_failure;
2175 if (devlink_dpipe_actions_put(table, skb))
2176 goto nla_put_failure;
2178 nla_nest_end(skb, table_attr);
2179 return 0;
2181 nla_put_failure:
2182 nla_nest_cancel(skb, table_attr);
2183 return -EMSGSIZE;
2186 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2187 struct genl_info *info)
2189 int err;
2191 if (*pskb) {
2192 err = genlmsg_reply(*pskb, info);
2193 if (err)
2194 return err;
2196 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2197 if (!*pskb)
2198 return -ENOMEM;
2199 return 0;
2202 static int devlink_dpipe_tables_fill(struct genl_info *info,
2203 enum devlink_command cmd, int flags,
2204 struct list_head *dpipe_tables,
2205 const char *table_name)
2207 struct devlink *devlink = info->user_ptr[0];
2208 struct devlink_dpipe_table *table;
2209 struct nlattr *tables_attr;
2210 struct sk_buff *skb = NULL;
2211 struct nlmsghdr *nlh;
2212 bool incomplete;
2213 void *hdr;
2214 int i;
2215 int err;
2217 table = list_first_entry(dpipe_tables,
2218 struct devlink_dpipe_table, list);
2219 start_again:
2220 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2221 if (err)
2222 return err;
2224 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2225 &devlink_nl_family, NLM_F_MULTI, cmd);
2226 if (!hdr) {
2227 nlmsg_free(skb);
2228 return -EMSGSIZE;
2231 if (devlink_nl_put_handle(skb, devlink))
2232 goto nla_put_failure;
2233 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
2234 if (!tables_attr)
2235 goto nla_put_failure;
2237 i = 0;
2238 incomplete = false;
2239 list_for_each_entry_from(table, dpipe_tables, list) {
2240 if (!table_name) {
2241 err = devlink_dpipe_table_put(skb, table);
2242 if (err) {
2243 if (!i)
2244 goto err_table_put;
2245 incomplete = true;
2246 break;
2248 } else {
2249 if (!strcmp(table->name, table_name)) {
2250 err = devlink_dpipe_table_put(skb, table);
2251 if (err)
2252 break;
2255 i++;
2258 nla_nest_end(skb, tables_attr);
2259 genlmsg_end(skb, hdr);
2260 if (incomplete)
2261 goto start_again;
2263 send_done:
2264 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2265 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2266 if (!nlh) {
2267 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2268 if (err)
2269 return err;
2270 goto send_done;
2273 return genlmsg_reply(skb, info);
2275 nla_put_failure:
2276 err = -EMSGSIZE;
2277 err_table_put:
2278 nlmsg_free(skb);
2279 return err;
2282 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
2283 struct genl_info *info)
2285 struct devlink *devlink = info->user_ptr[0];
2286 const char *table_name = NULL;
2288 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2289 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2291 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
2292 &devlink->dpipe_table_list,
2293 table_name);
2296 static int devlink_dpipe_value_put(struct sk_buff *skb,
2297 struct devlink_dpipe_value *value)
2299 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
2300 value->value_size, value->value))
2301 return -EMSGSIZE;
2302 if (value->mask)
2303 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
2304 value->value_size, value->mask))
2305 return -EMSGSIZE;
2306 if (value->mapping_valid)
2307 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
2308 value->mapping_value))
2309 return -EMSGSIZE;
2310 return 0;
2313 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
2314 struct devlink_dpipe_value *value)
2316 if (!value->action)
2317 return -EINVAL;
2318 if (devlink_dpipe_action_put(skb, value->action))
2319 return -EMSGSIZE;
2320 if (devlink_dpipe_value_put(skb, value))
2321 return -EMSGSIZE;
2322 return 0;
2325 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
2326 struct devlink_dpipe_value *values,
2327 unsigned int values_count)
2329 struct nlattr *action_attr;
2330 int i;
2331 int err;
2333 for (i = 0; i < values_count; i++) {
2334 action_attr = nla_nest_start_noflag(skb,
2335 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
2336 if (!action_attr)
2337 return -EMSGSIZE;
2338 err = devlink_dpipe_action_value_put(skb, &values[i]);
2339 if (err)
2340 goto err_action_value_put;
2341 nla_nest_end(skb, action_attr);
2343 return 0;
2345 err_action_value_put:
2346 nla_nest_cancel(skb, action_attr);
2347 return err;
2350 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
2351 struct devlink_dpipe_value *value)
2353 if (!value->match)
2354 return -EINVAL;
2355 if (devlink_dpipe_match_put(skb, value->match))
2356 return -EMSGSIZE;
2357 if (devlink_dpipe_value_put(skb, value))
2358 return -EMSGSIZE;
2359 return 0;
2362 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2363 struct devlink_dpipe_value *values,
2364 unsigned int values_count)
2366 struct nlattr *match_attr;
2367 int i;
2368 int err;
2370 for (i = 0; i < values_count; i++) {
2371 match_attr = nla_nest_start_noflag(skb,
2372 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
2373 if (!match_attr)
2374 return -EMSGSIZE;
2375 err = devlink_dpipe_match_value_put(skb, &values[i]);
2376 if (err)
2377 goto err_match_value_put;
2378 nla_nest_end(skb, match_attr);
2380 return 0;
2382 err_match_value_put:
2383 nla_nest_cancel(skb, match_attr);
2384 return err;
2387 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2388 struct devlink_dpipe_entry *entry)
2390 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2391 int err;
2393 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2394 if (!entry_attr)
2395 return -EMSGSIZE;
2397 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2398 DEVLINK_ATTR_PAD))
2399 goto nla_put_failure;
2400 if (entry->counter_valid)
2401 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2402 entry->counter, DEVLINK_ATTR_PAD))
2403 goto nla_put_failure;
2405 matches_attr = nla_nest_start_noflag(skb,
2406 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2407 if (!matches_attr)
2408 goto nla_put_failure;
2410 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2411 entry->match_values_count);
2412 if (err) {
2413 nla_nest_cancel(skb, matches_attr);
2414 goto err_match_values_put;
2416 nla_nest_end(skb, matches_attr);
2418 actions_attr = nla_nest_start_noflag(skb,
2419 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2420 if (!actions_attr)
2421 goto nla_put_failure;
2423 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2424 entry->action_values_count);
2425 if (err) {
2426 nla_nest_cancel(skb, actions_attr);
2427 goto err_action_values_put;
2429 nla_nest_end(skb, actions_attr);
2431 nla_nest_end(skb, entry_attr);
2432 return 0;
2434 nla_put_failure:
2435 err = -EMSGSIZE;
2436 err_match_values_put:
2437 err_action_values_put:
2438 nla_nest_cancel(skb, entry_attr);
2439 return err;
2442 static struct devlink_dpipe_table *
2443 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2444 const char *table_name, struct devlink *devlink)
2446 struct devlink_dpipe_table *table;
2447 list_for_each_entry_rcu(table, dpipe_tables, list,
2448 lockdep_is_held(&devlink->lock)) {
2449 if (!strcmp(table->name, table_name))
2450 return table;
2452 return NULL;
2455 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2457 struct devlink *devlink;
2458 int err;
2460 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2461 dump_ctx->info);
2462 if (err)
2463 return err;
2465 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2466 dump_ctx->info->snd_portid,
2467 dump_ctx->info->snd_seq,
2468 &devlink_nl_family, NLM_F_MULTI,
2469 dump_ctx->cmd);
2470 if (!dump_ctx->hdr)
2471 goto nla_put_failure;
2473 devlink = dump_ctx->info->user_ptr[0];
2474 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2475 goto nla_put_failure;
2476 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2477 DEVLINK_ATTR_DPIPE_ENTRIES);
2478 if (!dump_ctx->nest)
2479 goto nla_put_failure;
2480 return 0;
2482 nla_put_failure:
2483 nlmsg_free(dump_ctx->skb);
2484 return -EMSGSIZE;
2486 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2488 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2489 struct devlink_dpipe_entry *entry)
2491 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2493 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2495 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2497 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2498 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2499 return 0;
2501 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2503 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2506 unsigned int value_count, value_index;
2507 struct devlink_dpipe_value *value;
2509 value = entry->action_values;
2510 value_count = entry->action_values_count;
2511 for (value_index = 0; value_index < value_count; value_index++) {
2512 kfree(value[value_index].value);
2513 kfree(value[value_index].mask);
2516 value = entry->match_values;
2517 value_count = entry->match_values_count;
2518 for (value_index = 0; value_index < value_count; value_index++) {
2519 kfree(value[value_index].value);
2520 kfree(value[value_index].mask);
2523 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2525 static int devlink_dpipe_entries_fill(struct genl_info *info,
2526 enum devlink_command cmd, int flags,
2527 struct devlink_dpipe_table *table)
2529 struct devlink_dpipe_dump_ctx dump_ctx;
2530 struct nlmsghdr *nlh;
2531 int err;
2533 dump_ctx.skb = NULL;
2534 dump_ctx.cmd = cmd;
2535 dump_ctx.info = info;
2537 err = table->table_ops->entries_dump(table->priv,
2538 table->counters_enabled,
2539 &dump_ctx);
2540 if (err)
2541 return err;
2543 send_done:
2544 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2545 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2546 if (!nlh) {
2547 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2548 if (err)
2549 return err;
2550 goto send_done;
2552 return genlmsg_reply(dump_ctx.skb, info);
2555 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2556 struct genl_info *info)
2558 struct devlink *devlink = info->user_ptr[0];
2559 struct devlink_dpipe_table *table;
2560 const char *table_name;
2562 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2563 return -EINVAL;
2565 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2566 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2567 table_name, devlink);
2568 if (!table)
2569 return -EINVAL;
2571 if (!table->table_ops->entries_dump)
2572 return -EINVAL;
2574 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2575 0, table);
2578 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2579 const struct devlink_dpipe_header *header)
2581 struct devlink_dpipe_field *field;
2582 struct nlattr *field_attr;
2583 int i;
2585 for (i = 0; i < header->fields_count; i++) {
2586 field = &header->fields[i];
2587 field_attr = nla_nest_start_noflag(skb,
2588 DEVLINK_ATTR_DPIPE_FIELD);
2589 if (!field_attr)
2590 return -EMSGSIZE;
2591 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2592 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2593 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2594 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2595 goto nla_put_failure;
2596 nla_nest_end(skb, field_attr);
2598 return 0;
2600 nla_put_failure:
2601 nla_nest_cancel(skb, field_attr);
2602 return -EMSGSIZE;
2605 static int devlink_dpipe_header_put(struct sk_buff *skb,
2606 struct devlink_dpipe_header *header)
2608 struct nlattr *fields_attr, *header_attr;
2609 int err;
2611 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2612 if (!header_attr)
2613 return -EMSGSIZE;
2615 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2616 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2617 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2618 goto nla_put_failure;
2620 fields_attr = nla_nest_start_noflag(skb,
2621 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2622 if (!fields_attr)
2623 goto nla_put_failure;
2625 err = devlink_dpipe_fields_put(skb, header);
2626 if (err) {
2627 nla_nest_cancel(skb, fields_attr);
2628 goto nla_put_failure;
2630 nla_nest_end(skb, fields_attr);
2631 nla_nest_end(skb, header_attr);
2632 return 0;
2634 nla_put_failure:
2635 err = -EMSGSIZE;
2636 nla_nest_cancel(skb, header_attr);
2637 return err;
2640 static int devlink_dpipe_headers_fill(struct genl_info *info,
2641 enum devlink_command cmd, int flags,
2642 struct devlink_dpipe_headers *
2643 dpipe_headers)
2645 struct devlink *devlink = info->user_ptr[0];
2646 struct nlattr *headers_attr;
2647 struct sk_buff *skb = NULL;
2648 struct nlmsghdr *nlh;
2649 void *hdr;
2650 int i, j;
2651 int err;
2653 i = 0;
2654 start_again:
2655 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2656 if (err)
2657 return err;
2659 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2660 &devlink_nl_family, NLM_F_MULTI, cmd);
2661 if (!hdr) {
2662 nlmsg_free(skb);
2663 return -EMSGSIZE;
2666 if (devlink_nl_put_handle(skb, devlink))
2667 goto nla_put_failure;
2668 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2669 if (!headers_attr)
2670 goto nla_put_failure;
2672 j = 0;
2673 for (; i < dpipe_headers->headers_count; i++) {
2674 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2675 if (err) {
2676 if (!j)
2677 goto err_table_put;
2678 break;
2680 j++;
2682 nla_nest_end(skb, headers_attr);
2683 genlmsg_end(skb, hdr);
2684 if (i != dpipe_headers->headers_count)
2685 goto start_again;
2687 send_done:
2688 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2689 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2690 if (!nlh) {
2691 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2692 if (err)
2693 return err;
2694 goto send_done;
2696 return genlmsg_reply(skb, info);
2698 nla_put_failure:
2699 err = -EMSGSIZE;
2700 err_table_put:
2701 nlmsg_free(skb);
2702 return err;
2705 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2706 struct genl_info *info)
2708 struct devlink *devlink = info->user_ptr[0];
2710 if (!devlink->dpipe_headers)
2711 return -EOPNOTSUPP;
2712 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2713 0, devlink->dpipe_headers);
2716 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2717 const char *table_name,
2718 bool enable)
2720 struct devlink_dpipe_table *table;
2722 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2723 table_name, devlink);
2724 if (!table)
2725 return -EINVAL;
2727 if (table->counter_control_extern)
2728 return -EOPNOTSUPP;
2730 if (!(table->counters_enabled ^ enable))
2731 return 0;
2733 table->counters_enabled = enable;
2734 if (table->table_ops->counters_set_update)
2735 table->table_ops->counters_set_update(table->priv, enable);
2736 return 0;
2739 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2740 struct genl_info *info)
2742 struct devlink *devlink = info->user_ptr[0];
2743 const char *table_name;
2744 bool counters_enable;
2746 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2747 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2748 return -EINVAL;
2750 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2751 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2753 return devlink_dpipe_table_counters_set(devlink, table_name,
2754 counters_enable);
2757 static struct devlink_resource *
2758 devlink_resource_find(struct devlink *devlink,
2759 struct devlink_resource *resource, u64 resource_id)
2761 struct list_head *resource_list;
2763 if (resource)
2764 resource_list = &resource->resource_list;
2765 else
2766 resource_list = &devlink->resource_list;
2768 list_for_each_entry(resource, resource_list, list) {
2769 struct devlink_resource *child_resource;
2771 if (resource->id == resource_id)
2772 return resource;
2774 child_resource = devlink_resource_find(devlink, resource,
2775 resource_id);
2776 if (child_resource)
2777 return child_resource;
2779 return NULL;
2782 static void
2783 devlink_resource_validate_children(struct devlink_resource *resource)
2785 struct devlink_resource *child_resource;
2786 bool size_valid = true;
2787 u64 parts_size = 0;
2789 if (list_empty(&resource->resource_list))
2790 goto out;
2792 list_for_each_entry(child_resource, &resource->resource_list, list)
2793 parts_size += child_resource->size_new;
2795 if (parts_size > resource->size_new)
2796 size_valid = false;
2797 out:
2798 resource->size_valid = size_valid;
2801 static int
2802 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2803 struct netlink_ext_ack *extack)
2805 u64 reminder;
2806 int err = 0;
2808 if (size > resource->size_params.size_max) {
2809 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2810 err = -EINVAL;
2813 if (size < resource->size_params.size_min) {
2814 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2815 err = -EINVAL;
2818 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2819 if (reminder) {
2820 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2821 err = -EINVAL;
2824 return err;
2827 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2828 struct genl_info *info)
2830 struct devlink *devlink = info->user_ptr[0];
2831 struct devlink_resource *resource;
2832 u64 resource_id;
2833 u64 size;
2834 int err;
2836 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2837 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2838 return -EINVAL;
2839 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2841 resource = devlink_resource_find(devlink, NULL, resource_id);
2842 if (!resource)
2843 return -EINVAL;
2845 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2846 err = devlink_resource_validate_size(resource, size, info->extack);
2847 if (err)
2848 return err;
2850 resource->size_new = size;
2851 devlink_resource_validate_children(resource);
2852 if (resource->parent)
2853 devlink_resource_validate_children(resource->parent);
2854 return 0;
2857 static int
2858 devlink_resource_size_params_put(struct devlink_resource *resource,
2859 struct sk_buff *skb)
2861 struct devlink_resource_size_params *size_params;
2863 size_params = &resource->size_params;
2864 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2865 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2866 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2867 size_params->size_max, DEVLINK_ATTR_PAD) ||
2868 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2869 size_params->size_min, DEVLINK_ATTR_PAD) ||
2870 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2871 return -EMSGSIZE;
2872 return 0;
2875 static int devlink_resource_occ_put(struct devlink_resource *resource,
2876 struct sk_buff *skb)
2878 if (!resource->occ_get)
2879 return 0;
2880 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2881 resource->occ_get(resource->occ_get_priv),
2882 DEVLINK_ATTR_PAD);
2885 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2886 struct devlink_resource *resource)
2888 struct devlink_resource *child_resource;
2889 struct nlattr *child_resource_attr;
2890 struct nlattr *resource_attr;
2892 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2893 if (!resource_attr)
2894 return -EMSGSIZE;
2896 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2897 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2898 DEVLINK_ATTR_PAD) ||
2899 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2900 DEVLINK_ATTR_PAD))
2901 goto nla_put_failure;
2902 if (resource->size != resource->size_new)
2903 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2904 resource->size_new, DEVLINK_ATTR_PAD);
2905 if (devlink_resource_occ_put(resource, skb))
2906 goto nla_put_failure;
2907 if (devlink_resource_size_params_put(resource, skb))
2908 goto nla_put_failure;
2909 if (list_empty(&resource->resource_list))
2910 goto out;
2912 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2913 resource->size_valid))
2914 goto nla_put_failure;
2916 child_resource_attr = nla_nest_start_noflag(skb,
2917 DEVLINK_ATTR_RESOURCE_LIST);
2918 if (!child_resource_attr)
2919 goto nla_put_failure;
2921 list_for_each_entry(child_resource, &resource->resource_list, list) {
2922 if (devlink_resource_put(devlink, skb, child_resource))
2923 goto resource_put_failure;
2926 nla_nest_end(skb, child_resource_attr);
2927 out:
2928 nla_nest_end(skb, resource_attr);
2929 return 0;
2931 resource_put_failure:
2932 nla_nest_cancel(skb, child_resource_attr);
2933 nla_put_failure:
2934 nla_nest_cancel(skb, resource_attr);
2935 return -EMSGSIZE;
2938 static int devlink_resource_fill(struct genl_info *info,
2939 enum devlink_command cmd, int flags)
2941 struct devlink *devlink = info->user_ptr[0];
2942 struct devlink_resource *resource;
2943 struct nlattr *resources_attr;
2944 struct sk_buff *skb = NULL;
2945 struct nlmsghdr *nlh;
2946 bool incomplete;
2947 void *hdr;
2948 int i;
2949 int err;
2951 resource = list_first_entry(&devlink->resource_list,
2952 struct devlink_resource, list);
2953 start_again:
2954 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2955 if (err)
2956 return err;
2958 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2959 &devlink_nl_family, NLM_F_MULTI, cmd);
2960 if (!hdr) {
2961 nlmsg_free(skb);
2962 return -EMSGSIZE;
2965 if (devlink_nl_put_handle(skb, devlink))
2966 goto nla_put_failure;
2968 resources_attr = nla_nest_start_noflag(skb,
2969 DEVLINK_ATTR_RESOURCE_LIST);
2970 if (!resources_attr)
2971 goto nla_put_failure;
2973 incomplete = false;
2974 i = 0;
2975 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2976 err = devlink_resource_put(devlink, skb, resource);
2977 if (err) {
2978 if (!i)
2979 goto err_resource_put;
2980 incomplete = true;
2981 break;
2983 i++;
2985 nla_nest_end(skb, resources_attr);
2986 genlmsg_end(skb, hdr);
2987 if (incomplete)
2988 goto start_again;
2989 send_done:
2990 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2991 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2992 if (!nlh) {
2993 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2994 if (err)
2995 return err;
2996 goto send_done;
2998 return genlmsg_reply(skb, info);
3000 nla_put_failure:
3001 err = -EMSGSIZE;
3002 err_resource_put:
3003 nlmsg_free(skb);
3004 return err;
3007 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
3008 struct genl_info *info)
3010 struct devlink *devlink = info->user_ptr[0];
3012 if (list_empty(&devlink->resource_list))
3013 return -EOPNOTSUPP;
3015 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
3018 static int
3019 devlink_resources_validate(struct devlink *devlink,
3020 struct devlink_resource *resource,
3021 struct genl_info *info)
3023 struct list_head *resource_list;
3024 int err = 0;
3026 if (resource)
3027 resource_list = &resource->resource_list;
3028 else
3029 resource_list = &devlink->resource_list;
3031 list_for_each_entry(resource, resource_list, list) {
3032 if (!resource->size_valid)
3033 return -EINVAL;
3034 err = devlink_resources_validate(devlink, resource, info);
3035 if (err)
3036 return err;
3038 return err;
3041 static struct net *devlink_netns_get(struct sk_buff *skb,
3042 struct genl_info *info)
3044 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
3045 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
3046 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
3047 struct net *net;
3049 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
3050 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
3051 return ERR_PTR(-EINVAL);
3054 if (netns_pid_attr) {
3055 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
3056 } else if (netns_fd_attr) {
3057 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
3058 } else if (netns_id_attr) {
3059 net = get_net_ns_by_id(sock_net(skb->sk),
3060 nla_get_u32(netns_id_attr));
3061 if (!net)
3062 net = ERR_PTR(-EINVAL);
3063 } else {
3064 WARN_ON(1);
3065 net = ERR_PTR(-EINVAL);
3067 if (IS_ERR(net)) {
3068 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
3069 return ERR_PTR(-EINVAL);
3071 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
3072 put_net(net);
3073 return ERR_PTR(-EPERM);
3075 return net;
3078 static void devlink_param_notify(struct devlink *devlink,
3079 unsigned int port_index,
3080 struct devlink_param_item *param_item,
3081 enum devlink_command cmd);
3083 static void devlink_reload_netns_change(struct devlink *devlink,
3084 struct net *dest_net)
3086 struct devlink_param_item *param_item;
3088 /* Userspace needs to be notified about devlink objects
3089 * removed from original and entering new network namespace.
3090 * The rest of the devlink objects are re-created during
3091 * reload process so the notifications are generated separatelly.
3094 list_for_each_entry(param_item, &devlink->param_list, list)
3095 devlink_param_notify(devlink, 0, param_item,
3096 DEVLINK_CMD_PARAM_DEL);
3097 devlink_notify(devlink, DEVLINK_CMD_DEL);
3099 __devlink_net_set(devlink, dest_net);
3101 devlink_notify(devlink, DEVLINK_CMD_NEW);
3102 list_for_each_entry(param_item, &devlink->param_list, list)
3103 devlink_param_notify(devlink, 0, param_item,
3104 DEVLINK_CMD_PARAM_NEW);
3107 static bool devlink_reload_supported(const struct devlink_ops *ops)
3109 return ops->reload_down && ops->reload_up;
3112 static void devlink_reload_failed_set(struct devlink *devlink,
3113 bool reload_failed)
3115 if (devlink->reload_failed == reload_failed)
3116 return;
3117 devlink->reload_failed = reload_failed;
3118 devlink_notify(devlink, DEVLINK_CMD_NEW);
3121 bool devlink_is_reload_failed(const struct devlink *devlink)
3123 return devlink->reload_failed;
3125 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
3127 static void
3128 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
3129 enum devlink_reload_limit limit, u32 actions_performed)
3131 unsigned long actions = actions_performed;
3132 int stat_idx;
3133 int action;
3135 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
3136 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
3137 reload_stats[stat_idx]++;
3139 devlink_notify(devlink, DEVLINK_CMD_NEW);
3142 static void
3143 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
3144 u32 actions_performed)
3146 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
3147 actions_performed);
3151 * devlink_remote_reload_actions_performed - Update devlink on reload actions
3152 * performed which are not a direct result of devlink reload call.
3154 * This should be called by a driver after performing reload actions in case it was not
3155 * a result of devlink reload call. For example fw_activate was performed as a result
3156 * of devlink reload triggered fw_activate on another host.
3157 * The motivation for this function is to keep data on reload actions performed on this
3158 * function whether it was done due to direct devlink reload call or not.
3160 * @devlink: devlink
3161 * @limit: reload limit
3162 * @actions_performed: bitmask of actions performed
3164 void devlink_remote_reload_actions_performed(struct devlink *devlink,
3165 enum devlink_reload_limit limit,
3166 u32 actions_performed)
3168 if (WARN_ON(!actions_performed ||
3169 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
3170 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
3171 limit > DEVLINK_RELOAD_LIMIT_MAX))
3172 return;
3174 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
3175 actions_performed);
3177 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
3179 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
3180 enum devlink_reload_action action, enum devlink_reload_limit limit,
3181 u32 *actions_performed, struct netlink_ext_ack *extack)
3183 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
3184 int err;
3186 if (!devlink->reload_enabled)
3187 return -EOPNOTSUPP;
3189 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
3190 sizeof(remote_reload_stats));
3191 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
3192 if (err)
3193 return err;
3195 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
3196 devlink_reload_netns_change(devlink, dest_net);
3198 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
3199 devlink_reload_failed_set(devlink, !!err);
3200 if (err)
3201 return err;
3203 WARN_ON(!(*actions_performed & BIT(action)));
3204 /* Catch driver on updating the remote action within devlink reload */
3205 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
3206 sizeof(remote_reload_stats)));
3207 devlink_reload_stats_update(devlink, limit, *actions_performed);
3208 return 0;
3211 static int
3212 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
3213 enum devlink_command cmd, struct genl_info *info)
3215 struct sk_buff *msg;
3216 void *hdr;
3218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3219 if (!msg)
3220 return -ENOMEM;
3222 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
3223 if (!hdr)
3224 goto free_msg;
3226 if (devlink_nl_put_handle(msg, devlink))
3227 goto nla_put_failure;
3229 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
3230 actions_performed))
3231 goto nla_put_failure;
3232 genlmsg_end(msg, hdr);
3234 return genlmsg_reply(msg, info);
3236 nla_put_failure:
3237 genlmsg_cancel(msg, hdr);
3238 free_msg:
3239 nlmsg_free(msg);
3240 return -EMSGSIZE;
3243 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
3245 struct devlink *devlink = info->user_ptr[0];
3246 enum devlink_reload_action action;
3247 enum devlink_reload_limit limit;
3248 struct net *dest_net = NULL;
3249 u32 actions_performed;
3250 int err;
3252 if (!devlink_reload_supported(devlink->ops))
3253 return -EOPNOTSUPP;
3255 err = devlink_resources_validate(devlink, NULL, info);
3256 if (err) {
3257 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
3258 return err;
3261 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
3262 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
3263 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
3264 dest_net = devlink_netns_get(skb, info);
3265 if (IS_ERR(dest_net))
3266 return PTR_ERR(dest_net);
3269 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
3270 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
3271 else
3272 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
3274 if (!devlink_reload_action_is_supported(devlink, action)) {
3275 NL_SET_ERR_MSG_MOD(info->extack,
3276 "Requested reload action is not supported by the driver");
3277 return -EOPNOTSUPP;
3280 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
3281 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
3282 struct nla_bitfield32 limits;
3283 u32 limits_selected;
3285 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
3286 limits_selected = limits.value & limits.selector;
3287 if (!limits_selected) {
3288 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
3289 return -EINVAL;
3291 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
3292 if (limits_selected & BIT(limit))
3293 break;
3294 /* UAPI enables multiselection, but currently it is not used */
3295 if (limits_selected != BIT(limit)) {
3296 NL_SET_ERR_MSG_MOD(info->extack,
3297 "Multiselection of limit is not supported");
3298 return -EOPNOTSUPP;
3300 if (!devlink_reload_limit_is_supported(devlink, limit)) {
3301 NL_SET_ERR_MSG_MOD(info->extack,
3302 "Requested limit is not supported by the driver");
3303 return -EOPNOTSUPP;
3305 if (devlink_reload_combination_is_invalid(action, limit)) {
3306 NL_SET_ERR_MSG_MOD(info->extack,
3307 "Requested limit is invalid for this action");
3308 return -EINVAL;
3311 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
3313 if (dest_net)
3314 put_net(dest_net);
3316 if (err)
3317 return err;
3318 /* For backward compatibility generate reply only if attributes used by user */
3319 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
3320 return 0;
3322 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
3323 DEVLINK_CMD_RELOAD, info);
3326 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
3327 struct devlink *devlink,
3328 enum devlink_command cmd,
3329 struct devlink_flash_notify *params)
3331 void *hdr;
3333 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3334 if (!hdr)
3335 return -EMSGSIZE;
3337 if (devlink_nl_put_handle(msg, devlink))
3338 goto nla_put_failure;
3340 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
3341 goto out;
3343 if (params->status_msg &&
3344 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
3345 params->status_msg))
3346 goto nla_put_failure;
3347 if (params->component &&
3348 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
3349 params->component))
3350 goto nla_put_failure;
3351 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
3352 params->done, DEVLINK_ATTR_PAD))
3353 goto nla_put_failure;
3354 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
3355 params->total, DEVLINK_ATTR_PAD))
3356 goto nla_put_failure;
3357 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
3358 params->timeout, DEVLINK_ATTR_PAD))
3359 goto nla_put_failure;
3361 out:
3362 genlmsg_end(msg, hdr);
3363 return 0;
3365 nla_put_failure:
3366 genlmsg_cancel(msg, hdr);
3367 return -EMSGSIZE;
3370 static void __devlink_flash_update_notify(struct devlink *devlink,
3371 enum devlink_command cmd,
3372 struct devlink_flash_notify *params)
3374 struct sk_buff *msg;
3375 int err;
3377 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
3378 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
3379 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
3381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3382 if (!msg)
3383 return;
3385 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
3386 if (err)
3387 goto out_free_msg;
3389 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3390 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3391 return;
3393 out_free_msg:
3394 nlmsg_free(msg);
3397 static void devlink_flash_update_begin_notify(struct devlink *devlink)
3399 struct devlink_flash_notify params = { 0 };
3401 __devlink_flash_update_notify(devlink,
3402 DEVLINK_CMD_FLASH_UPDATE,
3403 &params);
3406 static void devlink_flash_update_end_notify(struct devlink *devlink)
3408 struct devlink_flash_notify params = { 0 };
3410 __devlink_flash_update_notify(devlink,
3411 DEVLINK_CMD_FLASH_UPDATE_END,
3412 &params);
3415 void devlink_flash_update_status_notify(struct devlink *devlink,
3416 const char *status_msg,
3417 const char *component,
3418 unsigned long done,
3419 unsigned long total)
3421 struct devlink_flash_notify params = {
3422 .status_msg = status_msg,
3423 .component = component,
3424 .done = done,
3425 .total = total,
3428 __devlink_flash_update_notify(devlink,
3429 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3430 &params);
3432 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
3434 void devlink_flash_update_timeout_notify(struct devlink *devlink,
3435 const char *status_msg,
3436 const char *component,
3437 unsigned long timeout)
3439 struct devlink_flash_notify params = {
3440 .status_msg = status_msg,
3441 .component = component,
3442 .timeout = timeout,
3445 __devlink_flash_update_notify(devlink,
3446 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3447 &params);
3449 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
3451 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
3452 struct genl_info *info)
3454 struct nlattr *nla_component, *nla_overwrite_mask, *nla_file_name;
3455 struct devlink_flash_update_params params = {};
3456 struct devlink *devlink = info->user_ptr[0];
3457 const char *file_name;
3458 u32 supported_params;
3459 int ret;
3461 if (!devlink->ops->flash_update)
3462 return -EOPNOTSUPP;
3464 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
3465 return -EINVAL;
3467 supported_params = devlink->ops->supported_flash_update_params;
3469 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
3470 if (nla_component) {
3471 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
3472 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
3473 "component update is not supported by this device");
3474 return -EOPNOTSUPP;
3476 params.component = nla_data(nla_component);
3479 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
3480 if (nla_overwrite_mask) {
3481 struct nla_bitfield32 sections;
3483 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
3484 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
3485 "overwrite settings are not supported by this device");
3486 return -EOPNOTSUPP;
3488 sections = nla_get_bitfield32(nla_overwrite_mask);
3489 params.overwrite_mask = sections.value & sections.selector;
3492 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
3493 file_name = nla_data(nla_file_name);
3494 ret = request_firmware(&params.fw, file_name, devlink->dev);
3495 if (ret) {
3496 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
3497 return ret;
3500 devlink_flash_update_begin_notify(devlink);
3501 ret = devlink->ops->flash_update(devlink, &params, info->extack);
3502 devlink_flash_update_end_notify(devlink);
3504 release_firmware(params.fw);
3506 return ret;
3509 static const struct devlink_param devlink_param_generic[] = {
3511 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
3512 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
3513 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
3516 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3517 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
3518 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
3521 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
3522 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
3523 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
3526 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
3527 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
3528 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
3531 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
3532 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
3533 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
3536 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
3537 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
3538 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
3541 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
3542 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
3543 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
3546 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3547 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3548 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3551 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3552 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3553 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3556 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3557 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3558 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3561 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
3562 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
3563 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
3567 static int devlink_param_generic_verify(const struct devlink_param *param)
3569 /* verify it match generic parameter by id and name */
3570 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3571 return -EINVAL;
3572 if (strcmp(param->name, devlink_param_generic[param->id].name))
3573 return -ENOENT;
3575 WARN_ON(param->type != devlink_param_generic[param->id].type);
3577 return 0;
3580 static int devlink_param_driver_verify(const struct devlink_param *param)
3582 int i;
3584 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3585 return -EINVAL;
3586 /* verify no such name in generic params */
3587 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3588 if (!strcmp(param->name, devlink_param_generic[i].name))
3589 return -EEXIST;
3591 return 0;
3594 static struct devlink_param_item *
3595 devlink_param_find_by_name(struct list_head *param_list,
3596 const char *param_name)
3598 struct devlink_param_item *param_item;
3600 list_for_each_entry(param_item, param_list, list)
3601 if (!strcmp(param_item->param->name, param_name))
3602 return param_item;
3603 return NULL;
3606 static struct devlink_param_item *
3607 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3609 struct devlink_param_item *param_item;
3611 list_for_each_entry(param_item, param_list, list)
3612 if (param_item->param->id == param_id)
3613 return param_item;
3614 return NULL;
3617 static bool
3618 devlink_param_cmode_is_supported(const struct devlink_param *param,
3619 enum devlink_param_cmode cmode)
3621 return test_bit(cmode, &param->supported_cmodes);
3624 static int devlink_param_get(struct devlink *devlink,
3625 const struct devlink_param *param,
3626 struct devlink_param_gset_ctx *ctx)
3628 if (!param->get)
3629 return -EOPNOTSUPP;
3630 return param->get(devlink, param->id, ctx);
3633 static int devlink_param_set(struct devlink *devlink,
3634 const struct devlink_param *param,
3635 struct devlink_param_gset_ctx *ctx)
3637 if (!param->set)
3638 return -EOPNOTSUPP;
3639 return param->set(devlink, param->id, ctx);
3642 static int
3643 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3645 switch (param_type) {
3646 case DEVLINK_PARAM_TYPE_U8:
3647 return NLA_U8;
3648 case DEVLINK_PARAM_TYPE_U16:
3649 return NLA_U16;
3650 case DEVLINK_PARAM_TYPE_U32:
3651 return NLA_U32;
3652 case DEVLINK_PARAM_TYPE_STRING:
3653 return NLA_STRING;
3654 case DEVLINK_PARAM_TYPE_BOOL:
3655 return NLA_FLAG;
3656 default:
3657 return -EINVAL;
3661 static int
3662 devlink_nl_param_value_fill_one(struct sk_buff *msg,
3663 enum devlink_param_type type,
3664 enum devlink_param_cmode cmode,
3665 union devlink_param_value val)
3667 struct nlattr *param_value_attr;
3669 param_value_attr = nla_nest_start_noflag(msg,
3670 DEVLINK_ATTR_PARAM_VALUE);
3671 if (!param_value_attr)
3672 goto nla_put_failure;
3674 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3675 goto value_nest_cancel;
3677 switch (type) {
3678 case DEVLINK_PARAM_TYPE_U8:
3679 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3680 goto value_nest_cancel;
3681 break;
3682 case DEVLINK_PARAM_TYPE_U16:
3683 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3684 goto value_nest_cancel;
3685 break;
3686 case DEVLINK_PARAM_TYPE_U32:
3687 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3688 goto value_nest_cancel;
3689 break;
3690 case DEVLINK_PARAM_TYPE_STRING:
3691 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3692 val.vstr))
3693 goto value_nest_cancel;
3694 break;
3695 case DEVLINK_PARAM_TYPE_BOOL:
3696 if (val.vbool &&
3697 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3698 goto value_nest_cancel;
3699 break;
3702 nla_nest_end(msg, param_value_attr);
3703 return 0;
3705 value_nest_cancel:
3706 nla_nest_cancel(msg, param_value_attr);
3707 nla_put_failure:
3708 return -EMSGSIZE;
3711 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
3712 unsigned int port_index,
3713 struct devlink_param_item *param_item,
3714 enum devlink_command cmd,
3715 u32 portid, u32 seq, int flags)
3717 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
3718 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
3719 const struct devlink_param *param = param_item->param;
3720 struct devlink_param_gset_ctx ctx;
3721 struct nlattr *param_values_list;
3722 struct nlattr *param_attr;
3723 int nla_type;
3724 void *hdr;
3725 int err;
3726 int i;
3728 /* Get value from driver part to driverinit configuration mode */
3729 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3730 if (!devlink_param_cmode_is_supported(param, i))
3731 continue;
3732 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3733 if (!param_item->driverinit_value_valid)
3734 return -EOPNOTSUPP;
3735 param_value[i] = param_item->driverinit_value;
3736 } else {
3737 if (!param_item->published)
3738 continue;
3739 ctx.cmode = i;
3740 err = devlink_param_get(devlink, param, &ctx);
3741 if (err)
3742 return err;
3743 param_value[i] = ctx.val;
3745 param_value_set[i] = true;
3748 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3749 if (!hdr)
3750 return -EMSGSIZE;
3752 if (devlink_nl_put_handle(msg, devlink))
3753 goto genlmsg_cancel;
3755 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3756 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3757 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
3758 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3759 goto genlmsg_cancel;
3761 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
3762 if (!param_attr)
3763 goto genlmsg_cancel;
3764 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3765 goto param_nest_cancel;
3766 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3767 goto param_nest_cancel;
3769 nla_type = devlink_param_type_to_nla_type(param->type);
3770 if (nla_type < 0)
3771 goto param_nest_cancel;
3772 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3773 goto param_nest_cancel;
3775 param_values_list = nla_nest_start_noflag(msg,
3776 DEVLINK_ATTR_PARAM_VALUES_LIST);
3777 if (!param_values_list)
3778 goto param_nest_cancel;
3780 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3781 if (!param_value_set[i])
3782 continue;
3783 err = devlink_nl_param_value_fill_one(msg, param->type,
3784 i, param_value[i]);
3785 if (err)
3786 goto values_list_nest_cancel;
3789 nla_nest_end(msg, param_values_list);
3790 nla_nest_end(msg, param_attr);
3791 genlmsg_end(msg, hdr);
3792 return 0;
3794 values_list_nest_cancel:
3795 nla_nest_end(msg, param_values_list);
3796 param_nest_cancel:
3797 nla_nest_cancel(msg, param_attr);
3798 genlmsg_cancel:
3799 genlmsg_cancel(msg, hdr);
3800 return -EMSGSIZE;
3803 static void devlink_param_notify(struct devlink *devlink,
3804 unsigned int port_index,
3805 struct devlink_param_item *param_item,
3806 enum devlink_command cmd)
3808 struct sk_buff *msg;
3809 int err;
3811 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3812 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3813 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
3815 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3816 if (!msg)
3817 return;
3818 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3819 0, 0, 0);
3820 if (err) {
3821 nlmsg_free(msg);
3822 return;
3825 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3826 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3829 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3830 struct netlink_callback *cb)
3832 struct devlink_param_item *param_item;
3833 struct devlink *devlink;
3834 int start = cb->args[0];
3835 int idx = 0;
3836 int err = 0;
3838 mutex_lock(&devlink_mutex);
3839 list_for_each_entry(devlink, &devlink_list, list) {
3840 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3841 continue;
3842 mutex_lock(&devlink->lock);
3843 list_for_each_entry(param_item, &devlink->param_list, list) {
3844 if (idx < start) {
3845 idx++;
3846 continue;
3848 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3849 DEVLINK_CMD_PARAM_GET,
3850 NETLINK_CB(cb->skb).portid,
3851 cb->nlh->nlmsg_seq,
3852 NLM_F_MULTI);
3853 if (err == -EOPNOTSUPP) {
3854 err = 0;
3855 } else if (err) {
3856 mutex_unlock(&devlink->lock);
3857 goto out;
3859 idx++;
3861 mutex_unlock(&devlink->lock);
3863 out:
3864 mutex_unlock(&devlink_mutex);
3866 if (err != -EMSGSIZE)
3867 return err;
3869 cb->args[0] = idx;
3870 return msg->len;
3873 static int
3874 devlink_param_type_get_from_info(struct genl_info *info,
3875 enum devlink_param_type *param_type)
3877 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3878 return -EINVAL;
3880 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3881 case NLA_U8:
3882 *param_type = DEVLINK_PARAM_TYPE_U8;
3883 break;
3884 case NLA_U16:
3885 *param_type = DEVLINK_PARAM_TYPE_U16;
3886 break;
3887 case NLA_U32:
3888 *param_type = DEVLINK_PARAM_TYPE_U32;
3889 break;
3890 case NLA_STRING:
3891 *param_type = DEVLINK_PARAM_TYPE_STRING;
3892 break;
3893 case NLA_FLAG:
3894 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3895 break;
3896 default:
3897 return -EINVAL;
3900 return 0;
3903 static int
3904 devlink_param_value_get_from_info(const struct devlink_param *param,
3905 struct genl_info *info,
3906 union devlink_param_value *value)
3908 struct nlattr *param_data;
3909 int len;
3911 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3913 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3914 return -EINVAL;
3916 switch (param->type) {
3917 case DEVLINK_PARAM_TYPE_U8:
3918 if (nla_len(param_data) != sizeof(u8))
3919 return -EINVAL;
3920 value->vu8 = nla_get_u8(param_data);
3921 break;
3922 case DEVLINK_PARAM_TYPE_U16:
3923 if (nla_len(param_data) != sizeof(u16))
3924 return -EINVAL;
3925 value->vu16 = nla_get_u16(param_data);
3926 break;
3927 case DEVLINK_PARAM_TYPE_U32:
3928 if (nla_len(param_data) != sizeof(u32))
3929 return -EINVAL;
3930 value->vu32 = nla_get_u32(param_data);
3931 break;
3932 case DEVLINK_PARAM_TYPE_STRING:
3933 len = strnlen(nla_data(param_data), nla_len(param_data));
3934 if (len == nla_len(param_data) ||
3935 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3936 return -EINVAL;
3937 strcpy(value->vstr, nla_data(param_data));
3938 break;
3939 case DEVLINK_PARAM_TYPE_BOOL:
3940 if (param_data && nla_len(param_data))
3941 return -EINVAL;
3942 value->vbool = nla_get_flag(param_data);
3943 break;
3945 return 0;
3948 static struct devlink_param_item *
3949 devlink_param_get_from_info(struct list_head *param_list,
3950 struct genl_info *info)
3952 char *param_name;
3954 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3955 return NULL;
3957 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3958 return devlink_param_find_by_name(param_list, param_name);
3961 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3962 struct genl_info *info)
3964 struct devlink *devlink = info->user_ptr[0];
3965 struct devlink_param_item *param_item;
3966 struct sk_buff *msg;
3967 int err;
3969 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3970 if (!param_item)
3971 return -EINVAL;
3973 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3974 if (!msg)
3975 return -ENOMEM;
3977 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3978 DEVLINK_CMD_PARAM_GET,
3979 info->snd_portid, info->snd_seq, 0);
3980 if (err) {
3981 nlmsg_free(msg);
3982 return err;
3985 return genlmsg_reply(msg, info);
3988 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3989 unsigned int port_index,
3990 struct list_head *param_list,
3991 struct genl_info *info,
3992 enum devlink_command cmd)
3994 enum devlink_param_type param_type;
3995 struct devlink_param_gset_ctx ctx;
3996 enum devlink_param_cmode cmode;
3997 struct devlink_param_item *param_item;
3998 const struct devlink_param *param;
3999 union devlink_param_value value;
4000 int err = 0;
4002 param_item = devlink_param_get_from_info(param_list, info);
4003 if (!param_item)
4004 return -EINVAL;
4005 param = param_item->param;
4006 err = devlink_param_type_get_from_info(info, &param_type);
4007 if (err)
4008 return err;
4009 if (param_type != param->type)
4010 return -EINVAL;
4011 err = devlink_param_value_get_from_info(param, info, &value);
4012 if (err)
4013 return err;
4014 if (param->validate) {
4015 err = param->validate(devlink, param->id, value, info->extack);
4016 if (err)
4017 return err;
4020 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
4021 return -EINVAL;
4022 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
4023 if (!devlink_param_cmode_is_supported(param, cmode))
4024 return -EOPNOTSUPP;
4026 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4027 if (param->type == DEVLINK_PARAM_TYPE_STRING)
4028 strcpy(param_item->driverinit_value.vstr, value.vstr);
4029 else
4030 param_item->driverinit_value = value;
4031 param_item->driverinit_value_valid = true;
4032 } else {
4033 if (!param->set)
4034 return -EOPNOTSUPP;
4035 ctx.val = value;
4036 ctx.cmode = cmode;
4037 err = devlink_param_set(devlink, param, &ctx);
4038 if (err)
4039 return err;
4042 devlink_param_notify(devlink, port_index, param_item, cmd);
4043 return 0;
4046 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4047 struct genl_info *info)
4049 struct devlink *devlink = info->user_ptr[0];
4051 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
4052 info, DEVLINK_CMD_PARAM_NEW);
4055 static int devlink_param_register_one(struct devlink *devlink,
4056 unsigned int port_index,
4057 struct list_head *param_list,
4058 const struct devlink_param *param,
4059 enum devlink_command cmd)
4061 struct devlink_param_item *param_item;
4063 if (devlink_param_find_by_name(param_list, param->name))
4064 return -EEXIST;
4066 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
4067 WARN_ON(param->get || param->set);
4068 else
4069 WARN_ON(!param->get || !param->set);
4071 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
4072 if (!param_item)
4073 return -ENOMEM;
4074 param_item->param = param;
4076 list_add_tail(&param_item->list, param_list);
4077 devlink_param_notify(devlink, port_index, param_item, cmd);
4078 return 0;
4081 static void devlink_param_unregister_one(struct devlink *devlink,
4082 unsigned int port_index,
4083 struct list_head *param_list,
4084 const struct devlink_param *param,
4085 enum devlink_command cmd)
4087 struct devlink_param_item *param_item;
4089 param_item = devlink_param_find_by_name(param_list, param->name);
4090 WARN_ON(!param_item);
4091 devlink_param_notify(devlink, port_index, param_item, cmd);
4092 list_del(&param_item->list);
4093 kfree(param_item);
4096 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4097 struct netlink_callback *cb)
4099 struct devlink_param_item *param_item;
4100 struct devlink_port *devlink_port;
4101 struct devlink *devlink;
4102 int start = cb->args[0];
4103 int idx = 0;
4104 int err = 0;
4106 mutex_lock(&devlink_mutex);
4107 list_for_each_entry(devlink, &devlink_list, list) {
4108 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4109 continue;
4110 mutex_lock(&devlink->lock);
4111 list_for_each_entry(devlink_port, &devlink->port_list, list) {
4112 list_for_each_entry(param_item,
4113 &devlink_port->param_list, list) {
4114 if (idx < start) {
4115 idx++;
4116 continue;
4118 err = devlink_nl_param_fill(msg,
4119 devlink_port->devlink,
4120 devlink_port->index, param_item,
4121 DEVLINK_CMD_PORT_PARAM_GET,
4122 NETLINK_CB(cb->skb).portid,
4123 cb->nlh->nlmsg_seq,
4124 NLM_F_MULTI);
4125 if (err == -EOPNOTSUPP) {
4126 err = 0;
4127 } else if (err) {
4128 mutex_unlock(&devlink->lock);
4129 goto out;
4131 idx++;
4134 mutex_unlock(&devlink->lock);
4136 out:
4137 mutex_unlock(&devlink_mutex);
4139 if (err != -EMSGSIZE)
4140 return err;
4142 cb->args[0] = idx;
4143 return msg->len;
4146 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
4147 struct genl_info *info)
4149 struct devlink_port *devlink_port = info->user_ptr[0];
4150 struct devlink_param_item *param_item;
4151 struct sk_buff *msg;
4152 int err;
4154 param_item = devlink_param_get_from_info(&devlink_port->param_list,
4155 info);
4156 if (!param_item)
4157 return -EINVAL;
4159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4160 if (!msg)
4161 return -ENOMEM;
4163 err = devlink_nl_param_fill(msg, devlink_port->devlink,
4164 devlink_port->index, param_item,
4165 DEVLINK_CMD_PORT_PARAM_GET,
4166 info->snd_portid, info->snd_seq, 0);
4167 if (err) {
4168 nlmsg_free(msg);
4169 return err;
4172 return genlmsg_reply(msg, info);
4175 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
4176 struct genl_info *info)
4178 struct devlink_port *devlink_port = info->user_ptr[0];
4180 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
4181 devlink_port->index,
4182 &devlink_port->param_list, info,
4183 DEVLINK_CMD_PORT_PARAM_NEW);
4186 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
4187 struct devlink *devlink,
4188 struct devlink_snapshot *snapshot)
4190 struct nlattr *snap_attr;
4191 int err;
4193 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
4194 if (!snap_attr)
4195 return -EINVAL;
4197 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
4198 if (err)
4199 goto nla_put_failure;
4201 nla_nest_end(msg, snap_attr);
4202 return 0;
4204 nla_put_failure:
4205 nla_nest_cancel(msg, snap_attr);
4206 return err;
4209 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
4210 struct devlink *devlink,
4211 struct devlink_region *region)
4213 struct devlink_snapshot *snapshot;
4214 struct nlattr *snapshots_attr;
4215 int err;
4217 snapshots_attr = nla_nest_start_noflag(msg,
4218 DEVLINK_ATTR_REGION_SNAPSHOTS);
4219 if (!snapshots_attr)
4220 return -EINVAL;
4222 list_for_each_entry(snapshot, &region->snapshot_list, list) {
4223 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
4224 if (err)
4225 goto nla_put_failure;
4228 nla_nest_end(msg, snapshots_attr);
4229 return 0;
4231 nla_put_failure:
4232 nla_nest_cancel(msg, snapshots_attr);
4233 return err;
4236 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
4237 enum devlink_command cmd, u32 portid,
4238 u32 seq, int flags,
4239 struct devlink_region *region)
4241 void *hdr;
4242 int err;
4244 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4245 if (!hdr)
4246 return -EMSGSIZE;
4248 err = devlink_nl_put_handle(msg, devlink);
4249 if (err)
4250 goto nla_put_failure;
4252 if (region->port) {
4253 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4254 region->port->index);
4255 if (err)
4256 goto nla_put_failure;
4259 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
4260 if (err)
4261 goto nla_put_failure;
4263 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4264 region->size,
4265 DEVLINK_ATTR_PAD);
4266 if (err)
4267 goto nla_put_failure;
4269 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
4270 if (err)
4271 goto nla_put_failure;
4273 genlmsg_end(msg, hdr);
4274 return 0;
4276 nla_put_failure:
4277 genlmsg_cancel(msg, hdr);
4278 return err;
4281 static struct sk_buff *
4282 devlink_nl_region_notify_build(struct devlink_region *region,
4283 struct devlink_snapshot *snapshot,
4284 enum devlink_command cmd, u32 portid, u32 seq)
4286 struct devlink *devlink = region->devlink;
4287 struct sk_buff *msg;
4288 void *hdr;
4289 int err;
4292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4293 if (!msg)
4294 return ERR_PTR(-ENOMEM);
4296 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
4297 if (!hdr) {
4298 err = -EMSGSIZE;
4299 goto out_free_msg;
4302 err = devlink_nl_put_handle(msg, devlink);
4303 if (err)
4304 goto out_cancel_msg;
4306 if (region->port) {
4307 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4308 region->port->index);
4309 if (err)
4310 goto out_cancel_msg;
4313 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
4314 region->ops->name);
4315 if (err)
4316 goto out_cancel_msg;
4318 if (snapshot) {
4319 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
4320 snapshot->id);
4321 if (err)
4322 goto out_cancel_msg;
4323 } else {
4324 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4325 region->size, DEVLINK_ATTR_PAD);
4326 if (err)
4327 goto out_cancel_msg;
4329 genlmsg_end(msg, hdr);
4331 return msg;
4333 out_cancel_msg:
4334 genlmsg_cancel(msg, hdr);
4335 out_free_msg:
4336 nlmsg_free(msg);
4337 return ERR_PTR(err);
4340 static void devlink_nl_region_notify(struct devlink_region *region,
4341 struct devlink_snapshot *snapshot,
4342 enum devlink_command cmd)
4344 struct devlink *devlink = region->devlink;
4345 struct sk_buff *msg;
4347 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4349 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4350 if (IS_ERR(msg))
4351 return;
4353 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4354 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4358 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4359 * @devlink: devlink instance
4360 * @id: the snapshot id
4362 * Track when a new snapshot begins using an id. Load the count for the
4363 * given id from the snapshot xarray, increment it, and store it back.
4365 * Called when a new snapshot is created with the given id.
4367 * The id *must* have been previously allocated by
4368 * devlink_region_snapshot_id_get().
4370 * Returns 0 on success, or an error on failure.
4372 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4374 unsigned long count;
4375 void *p;
4377 lockdep_assert_held(&devlink->lock);
4379 p = xa_load(&devlink->snapshot_ids, id);
4380 if (WARN_ON(!p))
4381 return -EINVAL;
4383 if (WARN_ON(!xa_is_value(p)))
4384 return -EINVAL;
4386 count = xa_to_value(p);
4387 count++;
4389 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4390 GFP_KERNEL));
4394 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4395 * @devlink: devlink instance
4396 * @id: the snapshot id
4398 * Track when a snapshot is deleted and stops using an id. Load the count
4399 * for the given id from the snapshot xarray, decrement it, and store it
4400 * back.
4402 * If the count reaches zero, erase this id from the xarray, freeing it
4403 * up for future re-use by devlink_region_snapshot_id_get().
4405 * Called when a snapshot using the given id is deleted, and when the
4406 * initial allocator of the id is finished using it.
4408 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4410 unsigned long count;
4411 void *p;
4413 lockdep_assert_held(&devlink->lock);
4415 p = xa_load(&devlink->snapshot_ids, id);
4416 if (WARN_ON(!p))
4417 return;
4419 if (WARN_ON(!xa_is_value(p)))
4420 return;
4422 count = xa_to_value(p);
4424 if (count > 1) {
4425 count--;
4426 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4427 GFP_KERNEL);
4428 } else {
4429 /* If this was the last user, we can erase this id */
4430 xa_erase(&devlink->snapshot_ids, id);
4435 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4436 * @devlink: devlink instance
4437 * @id: the snapshot id
4439 * Mark the given snapshot id as used by inserting a zero value into the
4440 * snapshot xarray.
4442 * This must be called while holding the devlink instance lock. Unlike
4443 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4444 * It is expected that the id will immediately be used before
4445 * releasing the devlink instance lock.
4447 * Returns zero on success, or an error code if the snapshot id could not
4448 * be inserted.
4450 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4452 lockdep_assert_held(&devlink->lock);
4454 if (xa_load(&devlink->snapshot_ids, id))
4455 return -EEXIST;
4457 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4458 GFP_KERNEL));
4462 * __devlink_region_snapshot_id_get - get snapshot ID
4463 * @devlink: devlink instance
4464 * @id: storage to return snapshot id
4466 * Allocates a new snapshot id. Returns zero on success, or a negative
4467 * error on failure. Must be called while holding the devlink instance
4468 * lock.
4470 * Snapshot IDs are tracked using an xarray which stores the number of
4471 * users of the snapshot id.
4473 * Note that the caller of this function counts as a 'user', in order to
4474 * avoid race conditions. The caller must release its hold on the
4475 * snapshot by using devlink_region_snapshot_id_put.
4477 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4479 lockdep_assert_held(&devlink->lock);
4481 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4482 xa_limit_32b, GFP_KERNEL);
4486 * __devlink_region_snapshot_create - create a new snapshot
4487 * This will add a new snapshot of a region. The snapshot
4488 * will be stored on the region struct and can be accessed
4489 * from devlink. This is useful for future analyses of snapshots.
4490 * Multiple snapshots can be created on a region.
4491 * The @snapshot_id should be obtained using the getter function.
4493 * Must be called only while holding the devlink instance lock.
4495 * @region: devlink region of the snapshot
4496 * @data: snapshot data
4497 * @snapshot_id: snapshot id to be created
4499 static int
4500 __devlink_region_snapshot_create(struct devlink_region *region,
4501 u8 *data, u32 snapshot_id)
4503 struct devlink *devlink = region->devlink;
4504 struct devlink_snapshot *snapshot;
4505 int err;
4507 lockdep_assert_held(&devlink->lock);
4509 /* check if region can hold one more snapshot */
4510 if (region->cur_snapshots == region->max_snapshots)
4511 return -ENOSPC;
4513 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4514 return -EEXIST;
4516 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4517 if (!snapshot)
4518 return -ENOMEM;
4520 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4521 if (err)
4522 goto err_snapshot_id_increment;
4524 snapshot->id = snapshot_id;
4525 snapshot->region = region;
4526 snapshot->data = data;
4528 list_add_tail(&snapshot->list, &region->snapshot_list);
4530 region->cur_snapshots++;
4532 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4533 return 0;
4535 err_snapshot_id_increment:
4536 kfree(snapshot);
4537 return err;
4540 static void devlink_region_snapshot_del(struct devlink_region *region,
4541 struct devlink_snapshot *snapshot)
4543 struct devlink *devlink = region->devlink;
4545 lockdep_assert_held(&devlink->lock);
4547 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4548 region->cur_snapshots--;
4549 list_del(&snapshot->list);
4550 region->ops->destructor(snapshot->data);
4551 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4552 kfree(snapshot);
4555 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4556 struct genl_info *info)
4558 struct devlink *devlink = info->user_ptr[0];
4559 struct devlink_port *port = NULL;
4560 struct devlink_region *region;
4561 const char *region_name;
4562 struct sk_buff *msg;
4563 unsigned int index;
4564 int err;
4566 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
4567 return -EINVAL;
4569 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4570 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4572 port = devlink_port_get_by_index(devlink, index);
4573 if (!port)
4574 return -ENODEV;
4577 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4578 if (port)
4579 region = devlink_port_region_get_by_name(port, region_name);
4580 else
4581 region = devlink_region_get_by_name(devlink, region_name);
4583 if (!region)
4584 return -EINVAL;
4586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4587 if (!msg)
4588 return -ENOMEM;
4590 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4591 info->snd_portid, info->snd_seq, 0,
4592 region);
4593 if (err) {
4594 nlmsg_free(msg);
4595 return err;
4598 return genlmsg_reply(msg, info);
4601 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
4602 struct netlink_callback *cb,
4603 struct devlink_port *port,
4604 int *idx,
4605 int start)
4607 struct devlink_region *region;
4608 int err = 0;
4610 list_for_each_entry(region, &port->region_list, list) {
4611 if (*idx < start) {
4612 (*idx)++;
4613 continue;
4615 err = devlink_nl_region_fill(msg, port->devlink,
4616 DEVLINK_CMD_REGION_GET,
4617 NETLINK_CB(cb->skb).portid,
4618 cb->nlh->nlmsg_seq,
4619 NLM_F_MULTI, region);
4620 if (err)
4621 goto out;
4622 (*idx)++;
4625 out:
4626 return err;
4629 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
4630 struct netlink_callback *cb,
4631 struct devlink *devlink,
4632 int *idx,
4633 int start)
4635 struct devlink_region *region;
4636 struct devlink_port *port;
4637 int err = 0;
4639 mutex_lock(&devlink->lock);
4640 list_for_each_entry(region, &devlink->region_list, list) {
4641 if (*idx < start) {
4642 (*idx)++;
4643 continue;
4645 err = devlink_nl_region_fill(msg, devlink,
4646 DEVLINK_CMD_REGION_GET,
4647 NETLINK_CB(cb->skb).portid,
4648 cb->nlh->nlmsg_seq,
4649 NLM_F_MULTI, region);
4650 if (err)
4651 goto out;
4652 (*idx)++;
4655 list_for_each_entry(port, &devlink->port_list, list) {
4656 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
4657 start);
4658 if (err)
4659 goto out;
4662 out:
4663 mutex_unlock(&devlink->lock);
4664 return err;
4667 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4668 struct netlink_callback *cb)
4670 struct devlink *devlink;
4671 int start = cb->args[0];
4672 int idx = 0;
4673 int err;
4675 mutex_lock(&devlink_mutex);
4676 list_for_each_entry(devlink, &devlink_list, list) {
4677 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4678 continue;
4679 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
4680 &idx, start);
4681 if (err)
4682 goto out;
4684 out:
4685 mutex_unlock(&devlink_mutex);
4686 cb->args[0] = idx;
4687 return msg->len;
4690 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4691 struct genl_info *info)
4693 struct devlink *devlink = info->user_ptr[0];
4694 struct devlink_snapshot *snapshot;
4695 struct devlink_port *port = NULL;
4696 struct devlink_region *region;
4697 const char *region_name;
4698 unsigned int index;
4699 u32 snapshot_id;
4701 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4702 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4703 return -EINVAL;
4705 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4706 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4708 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4709 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4711 port = devlink_port_get_by_index(devlink, index);
4712 if (!port)
4713 return -ENODEV;
4716 if (port)
4717 region = devlink_port_region_get_by_name(port, region_name);
4718 else
4719 region = devlink_region_get_by_name(devlink, region_name);
4721 if (!region)
4722 return -EINVAL;
4724 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4725 if (!snapshot)
4726 return -EINVAL;
4728 devlink_region_snapshot_del(region, snapshot);
4729 return 0;
4732 static int
4733 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4735 struct devlink *devlink = info->user_ptr[0];
4736 struct devlink_snapshot *snapshot;
4737 struct devlink_port *port = NULL;
4738 struct nlattr *snapshot_id_attr;
4739 struct devlink_region *region;
4740 const char *region_name;
4741 unsigned int index;
4742 u32 snapshot_id;
4743 u8 *data;
4744 int err;
4746 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4747 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4748 return -EINVAL;
4751 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4753 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4754 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4756 port = devlink_port_get_by_index(devlink, index);
4757 if (!port)
4758 return -ENODEV;
4761 if (port)
4762 region = devlink_port_region_get_by_name(port, region_name);
4763 else
4764 region = devlink_region_get_by_name(devlink, region_name);
4766 if (!region) {
4767 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4768 return -EINVAL;
4771 if (!region->ops->snapshot) {
4772 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4773 return -EOPNOTSUPP;
4776 if (region->cur_snapshots == region->max_snapshots) {
4777 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4778 return -ENOSPC;
4781 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4782 if (snapshot_id_attr) {
4783 snapshot_id = nla_get_u32(snapshot_id_attr);
4785 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4786 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4787 return -EEXIST;
4790 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4791 if (err)
4792 return err;
4793 } else {
4794 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4795 if (err) {
4796 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4797 return err;
4801 if (port)
4802 err = region->port_ops->snapshot(port, region->port_ops,
4803 info->extack, &data);
4804 else
4805 err = region->ops->snapshot(devlink, region->ops,
4806 info->extack, &data);
4807 if (err)
4808 goto err_snapshot_capture;
4810 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4811 if (err)
4812 goto err_snapshot_create;
4814 if (!snapshot_id_attr) {
4815 struct sk_buff *msg;
4817 snapshot = devlink_region_snapshot_get_by_id(region,
4818 snapshot_id);
4819 if (WARN_ON(!snapshot))
4820 return -EINVAL;
4822 msg = devlink_nl_region_notify_build(region, snapshot,
4823 DEVLINK_CMD_REGION_NEW,
4824 info->snd_portid,
4825 info->snd_seq);
4826 err = PTR_ERR_OR_ZERO(msg);
4827 if (err)
4828 goto err_notify;
4830 err = genlmsg_reply(msg, info);
4831 if (err)
4832 goto err_notify;
4835 return 0;
4837 err_snapshot_create:
4838 region->ops->destructor(data);
4839 err_snapshot_capture:
4840 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4841 return err;
4843 err_notify:
4844 devlink_region_snapshot_del(region, snapshot);
4845 return err;
4848 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4849 struct devlink *devlink,
4850 u8 *chunk, u32 chunk_size,
4851 u64 addr)
4853 struct nlattr *chunk_attr;
4854 int err;
4856 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
4857 if (!chunk_attr)
4858 return -EINVAL;
4860 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4861 if (err)
4862 goto nla_put_failure;
4864 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4865 DEVLINK_ATTR_PAD);
4866 if (err)
4867 goto nla_put_failure;
4869 nla_nest_end(msg, chunk_attr);
4870 return 0;
4872 nla_put_failure:
4873 nla_nest_cancel(msg, chunk_attr);
4874 return err;
4877 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4879 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4880 struct devlink *devlink,
4881 struct devlink_region *region,
4882 struct nlattr **attrs,
4883 u64 start_offset,
4884 u64 end_offset,
4885 u64 *new_offset)
4887 struct devlink_snapshot *snapshot;
4888 u64 curr_offset = start_offset;
4889 u32 snapshot_id;
4890 int err = 0;
4892 *new_offset = start_offset;
4894 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4895 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4896 if (!snapshot)
4897 return -EINVAL;
4899 while (curr_offset < end_offset) {
4900 u32 data_size;
4901 u8 *data;
4903 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4904 data_size = end_offset - curr_offset;
4905 else
4906 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4908 data = &snapshot->data[curr_offset];
4909 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4910 data, data_size,
4911 curr_offset);
4912 if (err)
4913 break;
4915 curr_offset += data_size;
4917 *new_offset = curr_offset;
4919 return err;
4922 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4923 struct netlink_callback *cb)
4925 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
4926 u64 ret_offset, start_offset, end_offset = U64_MAX;
4927 struct nlattr **attrs = info->attrs;
4928 struct devlink_port *port = NULL;
4929 struct devlink_region *region;
4930 struct nlattr *chunks_attr;
4931 const char *region_name;
4932 struct devlink *devlink;
4933 unsigned int index;
4934 void *hdr;
4935 int err;
4937 start_offset = *((u64 *)&cb->args[0]);
4939 mutex_lock(&devlink_mutex);
4940 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4941 if (IS_ERR(devlink)) {
4942 err = PTR_ERR(devlink);
4943 goto out_dev;
4946 mutex_lock(&devlink->lock);
4948 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
4949 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4950 err = -EINVAL;
4951 goto out_unlock;
4954 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4955 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4957 port = devlink_port_get_by_index(devlink, index);
4958 if (!port) {
4959 err = -ENODEV;
4960 goto out_unlock;
4964 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4966 if (port)
4967 region = devlink_port_region_get_by_name(port, region_name);
4968 else
4969 region = devlink_region_get_by_name(devlink, region_name);
4971 if (!region) {
4972 err = -EINVAL;
4973 goto out_unlock;
4976 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4977 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4978 if (!start_offset)
4979 start_offset =
4980 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4982 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4983 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4986 if (end_offset > region->size)
4987 end_offset = region->size;
4989 /* return 0 if there is no further data to read */
4990 if (start_offset == end_offset) {
4991 err = 0;
4992 goto out_unlock;
4995 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4996 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4997 DEVLINK_CMD_REGION_READ);
4998 if (!hdr) {
4999 err = -EMSGSIZE;
5000 goto out_unlock;
5003 err = devlink_nl_put_handle(skb, devlink);
5004 if (err)
5005 goto nla_put_failure;
5007 if (region->port) {
5008 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
5009 region->port->index);
5010 if (err)
5011 goto nla_put_failure;
5014 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
5015 if (err)
5016 goto nla_put_failure;
5018 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
5019 if (!chunks_attr) {
5020 err = -EMSGSIZE;
5021 goto nla_put_failure;
5024 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
5025 region, attrs,
5026 start_offset,
5027 end_offset, &ret_offset);
5029 if (err && err != -EMSGSIZE)
5030 goto nla_put_failure;
5032 /* Check if there was any progress done to prevent infinite loop */
5033 if (ret_offset == start_offset) {
5034 err = -EINVAL;
5035 goto nla_put_failure;
5038 *((u64 *)&cb->args[0]) = ret_offset;
5040 nla_nest_end(skb, chunks_attr);
5041 genlmsg_end(skb, hdr);
5042 mutex_unlock(&devlink->lock);
5043 mutex_unlock(&devlink_mutex);
5045 return skb->len;
5047 nla_put_failure:
5048 genlmsg_cancel(skb, hdr);
5049 out_unlock:
5050 mutex_unlock(&devlink->lock);
5051 out_dev:
5052 mutex_unlock(&devlink_mutex);
5053 return err;
5056 struct devlink_info_req {
5057 struct sk_buff *msg;
5060 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5062 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5064 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5066 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5068 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5070 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5072 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5073 const char *bsn)
5075 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5076 bsn);
5078 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5080 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5081 const char *version_name,
5082 const char *version_value)
5084 struct nlattr *nest;
5085 int err;
5087 nest = nla_nest_start_noflag(req->msg, attr);
5088 if (!nest)
5089 return -EMSGSIZE;
5091 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5092 version_name);
5093 if (err)
5094 goto nla_put_failure;
5096 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
5097 version_value);
5098 if (err)
5099 goto nla_put_failure;
5101 nla_nest_end(req->msg, nest);
5103 return 0;
5105 nla_put_failure:
5106 nla_nest_cancel(req->msg, nest);
5107 return err;
5110 int devlink_info_version_fixed_put(struct devlink_info_req *req,
5111 const char *version_name,
5112 const char *version_value)
5114 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
5115 version_name, version_value);
5117 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
5119 int devlink_info_version_stored_put(struct devlink_info_req *req,
5120 const char *version_name,
5121 const char *version_value)
5123 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
5124 version_name, version_value);
5126 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
5128 int devlink_info_version_running_put(struct devlink_info_req *req,
5129 const char *version_name,
5130 const char *version_value)
5132 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
5133 version_name, version_value);
5135 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
5137 static int
5138 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
5139 enum devlink_command cmd, u32 portid,
5140 u32 seq, int flags, struct netlink_ext_ack *extack)
5142 struct devlink_info_req req;
5143 void *hdr;
5144 int err;
5146 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5147 if (!hdr)
5148 return -EMSGSIZE;
5150 err = -EMSGSIZE;
5151 if (devlink_nl_put_handle(msg, devlink))
5152 goto err_cancel_msg;
5154 req.msg = msg;
5155 err = devlink->ops->info_get(devlink, &req, extack);
5156 if (err)
5157 goto err_cancel_msg;
5159 genlmsg_end(msg, hdr);
5160 return 0;
5162 err_cancel_msg:
5163 genlmsg_cancel(msg, hdr);
5164 return err;
5167 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
5168 struct genl_info *info)
5170 struct devlink *devlink = info->user_ptr[0];
5171 struct sk_buff *msg;
5172 int err;
5174 if (!devlink->ops->info_get)
5175 return -EOPNOTSUPP;
5177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5178 if (!msg)
5179 return -ENOMEM;
5181 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5182 info->snd_portid, info->snd_seq, 0,
5183 info->extack);
5184 if (err) {
5185 nlmsg_free(msg);
5186 return err;
5189 return genlmsg_reply(msg, info);
5192 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
5193 struct netlink_callback *cb)
5195 struct devlink *devlink;
5196 int start = cb->args[0];
5197 int idx = 0;
5198 int err = 0;
5200 mutex_lock(&devlink_mutex);
5201 list_for_each_entry(devlink, &devlink_list, list) {
5202 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5203 continue;
5204 if (idx < start) {
5205 idx++;
5206 continue;
5209 if (!devlink->ops->info_get) {
5210 idx++;
5211 continue;
5214 mutex_lock(&devlink->lock);
5215 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5216 NETLINK_CB(cb->skb).portid,
5217 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5218 cb->extack);
5219 mutex_unlock(&devlink->lock);
5220 if (err == -EOPNOTSUPP)
5221 err = 0;
5222 else if (err)
5223 break;
5224 idx++;
5226 mutex_unlock(&devlink_mutex);
5228 if (err != -EMSGSIZE)
5229 return err;
5231 cb->args[0] = idx;
5232 return msg->len;
5235 struct devlink_fmsg_item {
5236 struct list_head list;
5237 int attrtype;
5238 u8 nla_type;
5239 u16 len;
5240 int value[];
5243 struct devlink_fmsg {
5244 struct list_head item_list;
5245 bool putting_binary; /* This flag forces enclosing of binary data
5246 * in an array brackets. It forces using
5247 * of designated API:
5248 * devlink_fmsg_binary_pair_nest_start()
5249 * devlink_fmsg_binary_pair_nest_end()
5253 static struct devlink_fmsg *devlink_fmsg_alloc(void)
5255 struct devlink_fmsg *fmsg;
5257 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
5258 if (!fmsg)
5259 return NULL;
5261 INIT_LIST_HEAD(&fmsg->item_list);
5263 return fmsg;
5266 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
5268 struct devlink_fmsg_item *item, *tmp;
5270 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
5271 list_del(&item->list);
5272 kfree(item);
5274 kfree(fmsg);
5277 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
5278 int attrtype)
5280 struct devlink_fmsg_item *item;
5282 item = kzalloc(sizeof(*item), GFP_KERNEL);
5283 if (!item)
5284 return -ENOMEM;
5286 item->attrtype = attrtype;
5287 list_add_tail(&item->list, &fmsg->item_list);
5289 return 0;
5292 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
5294 if (fmsg->putting_binary)
5295 return -EINVAL;
5297 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
5299 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
5301 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
5303 if (fmsg->putting_binary)
5304 return -EINVAL;
5306 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
5309 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
5311 if (fmsg->putting_binary)
5312 return -EINVAL;
5314 return devlink_fmsg_nest_end(fmsg);
5316 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
5318 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
5320 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
5322 struct devlink_fmsg_item *item;
5324 if (fmsg->putting_binary)
5325 return -EINVAL;
5327 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
5328 return -EMSGSIZE;
5330 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
5331 if (!item)
5332 return -ENOMEM;
5334 item->nla_type = NLA_NUL_STRING;
5335 item->len = strlen(name) + 1;
5336 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
5337 memcpy(&item->value, name, item->len);
5338 list_add_tail(&item->list, &fmsg->item_list);
5340 return 0;
5343 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
5345 int err;
5347 if (fmsg->putting_binary)
5348 return -EINVAL;
5350 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
5351 if (err)
5352 return err;
5354 err = devlink_fmsg_put_name(fmsg, name);
5355 if (err)
5356 return err;
5358 return 0;
5360 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
5362 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
5364 if (fmsg->putting_binary)
5365 return -EINVAL;
5367 return devlink_fmsg_nest_end(fmsg);
5369 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
5371 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
5372 const char *name)
5374 int err;
5376 if (fmsg->putting_binary)
5377 return -EINVAL;
5379 err = devlink_fmsg_pair_nest_start(fmsg, name);
5380 if (err)
5381 return err;
5383 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
5384 if (err)
5385 return err;
5387 return 0;
5389 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
5391 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
5393 int err;
5395 if (fmsg->putting_binary)
5396 return -EINVAL;
5398 err = devlink_fmsg_nest_end(fmsg);
5399 if (err)
5400 return err;
5402 err = devlink_fmsg_nest_end(fmsg);
5403 if (err)
5404 return err;
5406 return 0;
5408 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
5410 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
5411 const char *name)
5413 int err;
5415 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
5416 if (err)
5417 return err;
5419 fmsg->putting_binary = true;
5420 return err;
5422 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
5424 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
5426 if (!fmsg->putting_binary)
5427 return -EINVAL;
5429 fmsg->putting_binary = false;
5430 return devlink_fmsg_arr_pair_nest_end(fmsg);
5432 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
5434 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
5435 const void *value, u16 value_len,
5436 u8 value_nla_type)
5438 struct devlink_fmsg_item *item;
5440 if (value_len > DEVLINK_FMSG_MAX_SIZE)
5441 return -EMSGSIZE;
5443 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
5444 if (!item)
5445 return -ENOMEM;
5447 item->nla_type = value_nla_type;
5448 item->len = value_len;
5449 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5450 memcpy(&item->value, value, item->len);
5451 list_add_tail(&item->list, &fmsg->item_list);
5453 return 0;
5456 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
5458 if (fmsg->putting_binary)
5459 return -EINVAL;
5461 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
5463 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
5465 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
5467 if (fmsg->putting_binary)
5468 return -EINVAL;
5470 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
5472 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
5474 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
5476 if (fmsg->putting_binary)
5477 return -EINVAL;
5479 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
5481 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
5483 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
5485 if (fmsg->putting_binary)
5486 return -EINVAL;
5488 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
5490 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
5492 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
5494 if (fmsg->putting_binary)
5495 return -EINVAL;
5497 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
5498 NLA_NUL_STRING);
5500 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
5502 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
5503 u16 value_len)
5505 if (!fmsg->putting_binary)
5506 return -EINVAL;
5508 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
5510 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
5512 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
5513 bool value)
5515 int err;
5517 err = devlink_fmsg_pair_nest_start(fmsg, name);
5518 if (err)
5519 return err;
5521 err = devlink_fmsg_bool_put(fmsg, value);
5522 if (err)
5523 return err;
5525 err = devlink_fmsg_pair_nest_end(fmsg);
5526 if (err)
5527 return err;
5529 return 0;
5531 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
5533 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
5534 u8 value)
5536 int err;
5538 err = devlink_fmsg_pair_nest_start(fmsg, name);
5539 if (err)
5540 return err;
5542 err = devlink_fmsg_u8_put(fmsg, value);
5543 if (err)
5544 return err;
5546 err = devlink_fmsg_pair_nest_end(fmsg);
5547 if (err)
5548 return err;
5550 return 0;
5552 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
5554 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
5555 u32 value)
5557 int err;
5559 err = devlink_fmsg_pair_nest_start(fmsg, name);
5560 if (err)
5561 return err;
5563 err = devlink_fmsg_u32_put(fmsg, value);
5564 if (err)
5565 return err;
5567 err = devlink_fmsg_pair_nest_end(fmsg);
5568 if (err)
5569 return err;
5571 return 0;
5573 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
5575 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
5576 u64 value)
5578 int err;
5580 err = devlink_fmsg_pair_nest_start(fmsg, name);
5581 if (err)
5582 return err;
5584 err = devlink_fmsg_u64_put(fmsg, value);
5585 if (err)
5586 return err;
5588 err = devlink_fmsg_pair_nest_end(fmsg);
5589 if (err)
5590 return err;
5592 return 0;
5594 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
5596 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
5597 const char *value)
5599 int err;
5601 err = devlink_fmsg_pair_nest_start(fmsg, name);
5602 if (err)
5603 return err;
5605 err = devlink_fmsg_string_put(fmsg, value);
5606 if (err)
5607 return err;
5609 err = devlink_fmsg_pair_nest_end(fmsg);
5610 if (err)
5611 return err;
5613 return 0;
5615 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
5617 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
5618 const void *value, u32 value_len)
5620 u32 data_size;
5621 int end_err;
5622 u32 offset;
5623 int err;
5625 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
5626 if (err)
5627 return err;
5629 for (offset = 0; offset < value_len; offset += data_size) {
5630 data_size = value_len - offset;
5631 if (data_size > DEVLINK_FMSG_MAX_SIZE)
5632 data_size = DEVLINK_FMSG_MAX_SIZE;
5633 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
5634 if (err)
5635 break;
5636 /* Exit from loop with a break (instead of
5637 * return) to make sure putting_binary is turned off in
5638 * devlink_fmsg_binary_pair_nest_end
5642 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
5643 if (end_err)
5644 err = end_err;
5646 return err;
5648 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
5650 static int
5651 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5653 switch (msg->nla_type) {
5654 case NLA_FLAG:
5655 case NLA_U8:
5656 case NLA_U32:
5657 case NLA_U64:
5658 case NLA_NUL_STRING:
5659 case NLA_BINARY:
5660 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
5661 msg->nla_type);
5662 default:
5663 return -EINVAL;
5667 static int
5668 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5670 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5671 u8 tmp;
5673 switch (msg->nla_type) {
5674 case NLA_FLAG:
5675 /* Always provide flag data, regardless of its value */
5676 tmp = *(bool *) msg->value;
5678 return nla_put_u8(skb, attrtype, tmp);
5679 case NLA_U8:
5680 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
5681 case NLA_U32:
5682 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
5683 case NLA_U64:
5684 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
5685 DEVLINK_ATTR_PAD);
5686 case NLA_NUL_STRING:
5687 return nla_put_string(skb, attrtype, (char *) &msg->value);
5688 case NLA_BINARY:
5689 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
5690 default:
5691 return -EINVAL;
5695 static int
5696 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5697 int *start)
5699 struct devlink_fmsg_item *item;
5700 struct nlattr *fmsg_nlattr;
5701 int i = 0;
5702 int err;
5704 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
5705 if (!fmsg_nlattr)
5706 return -EMSGSIZE;
5708 list_for_each_entry(item, &fmsg->item_list, list) {
5709 if (i < *start) {
5710 i++;
5711 continue;
5714 switch (item->attrtype) {
5715 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5716 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5717 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5718 case DEVLINK_ATTR_FMSG_NEST_END:
5719 err = nla_put_flag(skb, item->attrtype);
5720 break;
5721 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5722 err = devlink_fmsg_item_fill_type(item, skb);
5723 if (err)
5724 break;
5725 err = devlink_fmsg_item_fill_data(item, skb);
5726 break;
5727 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5728 err = nla_put_string(skb, item->attrtype,
5729 (char *) &item->value);
5730 break;
5731 default:
5732 err = -EINVAL;
5733 break;
5735 if (!err)
5736 *start = ++i;
5737 else
5738 break;
5741 nla_nest_end(skb, fmsg_nlattr);
5742 return err;
5745 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5746 struct genl_info *info,
5747 enum devlink_command cmd, int flags)
5749 struct nlmsghdr *nlh;
5750 struct sk_buff *skb;
5751 bool last = false;
5752 int index = 0;
5753 void *hdr;
5754 int err;
5756 while (!last) {
5757 int tmp_index = index;
5759 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5760 if (!skb)
5761 return -ENOMEM;
5763 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5764 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5765 if (!hdr) {
5766 err = -EMSGSIZE;
5767 goto nla_put_failure;
5770 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5771 if (!err)
5772 last = true;
5773 else if (err != -EMSGSIZE || tmp_index == index)
5774 goto nla_put_failure;
5776 genlmsg_end(skb, hdr);
5777 err = genlmsg_reply(skb, info);
5778 if (err)
5779 return err;
5782 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5783 if (!skb)
5784 return -ENOMEM;
5785 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5786 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5787 if (!nlh) {
5788 err = -EMSGSIZE;
5789 goto nla_put_failure;
5792 return genlmsg_reply(skb, info);
5794 nla_put_failure:
5795 nlmsg_free(skb);
5796 return err;
5799 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5800 struct netlink_callback *cb,
5801 enum devlink_command cmd)
5803 int index = cb->args[0];
5804 int tmp_index = index;
5805 void *hdr;
5806 int err;
5808 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5809 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5810 if (!hdr) {
5811 err = -EMSGSIZE;
5812 goto nla_put_failure;
5815 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5816 if ((err && err != -EMSGSIZE) || tmp_index == index)
5817 goto nla_put_failure;
5819 cb->args[0] = index;
5820 genlmsg_end(skb, hdr);
5821 return skb->len;
5823 nla_put_failure:
5824 genlmsg_cancel(skb, hdr);
5825 return err;
5828 struct devlink_health_reporter {
5829 struct list_head list;
5830 void *priv;
5831 const struct devlink_health_reporter_ops *ops;
5832 struct devlink *devlink;
5833 struct devlink_port *devlink_port;
5834 struct devlink_fmsg *dump_fmsg;
5835 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
5836 u64 graceful_period;
5837 bool auto_recover;
5838 bool auto_dump;
5839 u8 health_state;
5840 u64 dump_ts;
5841 u64 dump_real_ts;
5842 u64 error_count;
5843 u64 recovery_count;
5844 u64 last_recovery_ts;
5845 refcount_t refcount;
5848 void *
5849 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5851 return reporter->priv;
5853 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5855 static struct devlink_health_reporter *
5856 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
5857 struct mutex *list_lock,
5858 const char *reporter_name)
5860 struct devlink_health_reporter *reporter;
5862 lockdep_assert_held(list_lock);
5863 list_for_each_entry(reporter, reporter_list, list)
5864 if (!strcmp(reporter->ops->name, reporter_name))
5865 return reporter;
5866 return NULL;
5869 static struct devlink_health_reporter *
5870 devlink_health_reporter_find_by_name(struct devlink *devlink,
5871 const char *reporter_name)
5873 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
5874 &devlink->reporters_lock,
5875 reporter_name);
5878 static struct devlink_health_reporter *
5879 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
5880 const char *reporter_name)
5882 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
5883 &devlink_port->reporters_lock,
5884 reporter_name);
5887 static struct devlink_health_reporter *
5888 __devlink_health_reporter_create(struct devlink *devlink,
5889 const struct devlink_health_reporter_ops *ops,
5890 u64 graceful_period, void *priv)
5892 struct devlink_health_reporter *reporter;
5894 if (WARN_ON(graceful_period && !ops->recover))
5895 return ERR_PTR(-EINVAL);
5897 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5898 if (!reporter)
5899 return ERR_PTR(-ENOMEM);
5901 reporter->priv = priv;
5902 reporter->ops = ops;
5903 reporter->devlink = devlink;
5904 reporter->graceful_period = graceful_period;
5905 reporter->auto_recover = !!ops->recover;
5906 reporter->auto_dump = !!ops->dump;
5907 mutex_init(&reporter->dump_lock);
5908 refcount_set(&reporter->refcount, 1);
5909 return reporter;
5913 * devlink_port_health_reporter_create - create devlink health reporter for
5914 * specified port instance
5916 * @port: devlink_port which should contain the new reporter
5917 * @ops: ops
5918 * @graceful_period: to avoid recovery loops, in msecs
5919 * @priv: priv
5921 struct devlink_health_reporter *
5922 devlink_port_health_reporter_create(struct devlink_port *port,
5923 const struct devlink_health_reporter_ops *ops,
5924 u64 graceful_period, void *priv)
5926 struct devlink_health_reporter *reporter;
5928 mutex_lock(&port->reporters_lock);
5929 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
5930 &port->reporters_lock, ops->name)) {
5931 reporter = ERR_PTR(-EEXIST);
5932 goto unlock;
5935 reporter = __devlink_health_reporter_create(port->devlink, ops,
5936 graceful_period, priv);
5937 if (IS_ERR(reporter))
5938 goto unlock;
5940 reporter->devlink_port = port;
5941 list_add_tail(&reporter->list, &port->reporter_list);
5942 unlock:
5943 mutex_unlock(&port->reporters_lock);
5944 return reporter;
5946 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
5949 * devlink_health_reporter_create - create devlink health reporter
5951 * @devlink: devlink
5952 * @ops: ops
5953 * @graceful_period: to avoid recovery loops, in msecs
5954 * @priv: priv
5956 struct devlink_health_reporter *
5957 devlink_health_reporter_create(struct devlink *devlink,
5958 const struct devlink_health_reporter_ops *ops,
5959 u64 graceful_period, void *priv)
5961 struct devlink_health_reporter *reporter;
5963 mutex_lock(&devlink->reporters_lock);
5964 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5965 reporter = ERR_PTR(-EEXIST);
5966 goto unlock;
5969 reporter = __devlink_health_reporter_create(devlink, ops,
5970 graceful_period, priv);
5971 if (IS_ERR(reporter))
5972 goto unlock;
5974 list_add_tail(&reporter->list, &devlink->reporter_list);
5975 unlock:
5976 mutex_unlock(&devlink->reporters_lock);
5977 return reporter;
5979 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5981 static void
5982 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
5984 mutex_destroy(&reporter->dump_lock);
5985 if (reporter->dump_fmsg)
5986 devlink_fmsg_free(reporter->dump_fmsg);
5987 kfree(reporter);
5990 static void
5991 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5993 if (refcount_dec_and_test(&reporter->refcount))
5994 devlink_health_reporter_free(reporter);
5997 static void
5998 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6000 list_del(&reporter->list);
6001 devlink_health_reporter_put(reporter);
6005 * devlink_health_reporter_destroy - destroy devlink health reporter
6007 * @reporter: devlink health reporter to destroy
6009 void
6010 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6012 struct mutex *lock = &reporter->devlink->reporters_lock;
6014 mutex_lock(lock);
6015 __devlink_health_reporter_destroy(reporter);
6016 mutex_unlock(lock);
6018 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
6021 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
6023 * @reporter: devlink health reporter to destroy
6025 void
6026 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
6028 struct mutex *lock = &reporter->devlink_port->reporters_lock;
6030 mutex_lock(lock);
6031 __devlink_health_reporter_destroy(reporter);
6032 mutex_unlock(lock);
6034 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
6036 static int
6037 devlink_nl_health_reporter_fill(struct sk_buff *msg,
6038 struct devlink *devlink,
6039 struct devlink_health_reporter *reporter,
6040 enum devlink_command cmd, u32 portid,
6041 u32 seq, int flags)
6043 struct nlattr *reporter_attr;
6044 void *hdr;
6046 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6047 if (!hdr)
6048 return -EMSGSIZE;
6050 if (devlink_nl_put_handle(msg, devlink))
6051 goto genlmsg_cancel;
6053 if (reporter->devlink_port) {
6054 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6055 goto genlmsg_cancel;
6057 reporter_attr = nla_nest_start_noflag(msg,
6058 DEVLINK_ATTR_HEALTH_REPORTER);
6059 if (!reporter_attr)
6060 goto genlmsg_cancel;
6061 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6062 reporter->ops->name))
6063 goto reporter_nest_cancel;
6064 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6065 reporter->health_state))
6066 goto reporter_nest_cancel;
6067 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6068 reporter->error_count, DEVLINK_ATTR_PAD))
6069 goto reporter_nest_cancel;
6070 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6071 reporter->recovery_count, DEVLINK_ATTR_PAD))
6072 goto reporter_nest_cancel;
6073 if (reporter->ops->recover &&
6074 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6075 reporter->graceful_period,
6076 DEVLINK_ATTR_PAD))
6077 goto reporter_nest_cancel;
6078 if (reporter->ops->recover &&
6079 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6080 reporter->auto_recover))
6081 goto reporter_nest_cancel;
6082 if (reporter->dump_fmsg &&
6083 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6084 jiffies_to_msecs(reporter->dump_ts),
6085 DEVLINK_ATTR_PAD))
6086 goto reporter_nest_cancel;
6087 if (reporter->dump_fmsg &&
6088 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6089 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6090 goto reporter_nest_cancel;
6091 if (reporter->ops->dump &&
6092 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6093 reporter->auto_dump))
6094 goto reporter_nest_cancel;
6096 nla_nest_end(msg, reporter_attr);
6097 genlmsg_end(msg, hdr);
6098 return 0;
6100 reporter_nest_cancel:
6101 nla_nest_end(msg, reporter_attr);
6102 genlmsg_cancel:
6103 genlmsg_cancel(msg, hdr);
6104 return -EMSGSIZE;
6107 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
6108 enum devlink_command cmd)
6110 struct sk_buff *msg;
6111 int err;
6113 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6116 if (!msg)
6117 return;
6119 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
6120 reporter, cmd, 0, 0, 0);
6121 if (err) {
6122 nlmsg_free(msg);
6123 return;
6126 genlmsg_multicast_netns(&devlink_nl_family,
6127 devlink_net(reporter->devlink),
6128 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
6131 void
6132 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
6134 reporter->recovery_count++;
6135 reporter->last_recovery_ts = jiffies;
6137 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
6139 static int
6140 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
6141 void *priv_ctx, struct netlink_ext_ack *extack)
6143 int err;
6145 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
6146 return 0;
6148 if (!reporter->ops->recover)
6149 return -EOPNOTSUPP;
6151 err = reporter->ops->recover(reporter, priv_ctx, extack);
6152 if (err)
6153 return err;
6155 devlink_health_reporter_recovery_done(reporter);
6156 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
6157 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6159 return 0;
6162 static void
6163 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
6165 if (!reporter->dump_fmsg)
6166 return;
6167 devlink_fmsg_free(reporter->dump_fmsg);
6168 reporter->dump_fmsg = NULL;
6171 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
6172 void *priv_ctx,
6173 struct netlink_ext_ack *extack)
6175 int err;
6177 if (!reporter->ops->dump)
6178 return 0;
6180 if (reporter->dump_fmsg)
6181 return 0;
6183 reporter->dump_fmsg = devlink_fmsg_alloc();
6184 if (!reporter->dump_fmsg) {
6185 err = -ENOMEM;
6186 return err;
6189 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
6190 if (err)
6191 goto dump_err;
6193 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
6194 priv_ctx, extack);
6195 if (err)
6196 goto dump_err;
6198 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
6199 if (err)
6200 goto dump_err;
6202 reporter->dump_ts = jiffies;
6203 reporter->dump_real_ts = ktime_get_real_ns();
6205 return 0;
6207 dump_err:
6208 devlink_health_dump_clear(reporter);
6209 return err;
6212 int devlink_health_report(struct devlink_health_reporter *reporter,
6213 const char *msg, void *priv_ctx)
6215 enum devlink_health_reporter_state prev_health_state;
6216 struct devlink *devlink = reporter->devlink;
6217 unsigned long recover_ts_threshold;
6219 /* write a log message of the current error */
6220 WARN_ON(!msg);
6221 trace_devlink_health_report(devlink, reporter->ops->name, msg);
6222 reporter->error_count++;
6223 prev_health_state = reporter->health_state;
6224 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6225 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6227 /* abort if the previous error wasn't recovered */
6228 recover_ts_threshold = reporter->last_recovery_ts +
6229 msecs_to_jiffies(reporter->graceful_period);
6230 if (reporter->auto_recover &&
6231 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
6232 (reporter->last_recovery_ts && reporter->recovery_count &&
6233 time_is_after_jiffies(recover_ts_threshold)))) {
6234 trace_devlink_health_recover_aborted(devlink,
6235 reporter->ops->name,
6236 reporter->health_state,
6237 jiffies -
6238 reporter->last_recovery_ts);
6239 return -ECANCELED;
6242 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6244 if (reporter->auto_dump) {
6245 mutex_lock(&reporter->dump_lock);
6246 /* store current dump of current error, for later analysis */
6247 devlink_health_do_dump(reporter, priv_ctx, NULL);
6248 mutex_unlock(&reporter->dump_lock);
6251 if (reporter->auto_recover)
6252 return devlink_health_reporter_recover(reporter,
6253 priv_ctx, NULL);
6255 return 0;
6257 EXPORT_SYMBOL_GPL(devlink_health_report);
6259 static struct devlink_health_reporter *
6260 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
6261 struct nlattr **attrs)
6263 struct devlink_health_reporter *reporter;
6264 struct devlink_port *devlink_port;
6265 char *reporter_name;
6267 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
6268 return NULL;
6270 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
6271 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
6272 if (IS_ERR(devlink_port)) {
6273 mutex_lock(&devlink->reporters_lock);
6274 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
6275 if (reporter)
6276 refcount_inc(&reporter->refcount);
6277 mutex_unlock(&devlink->reporters_lock);
6278 } else {
6279 mutex_lock(&devlink_port->reporters_lock);
6280 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
6281 if (reporter)
6282 refcount_inc(&reporter->refcount);
6283 mutex_unlock(&devlink_port->reporters_lock);
6286 return reporter;
6289 static struct devlink_health_reporter *
6290 devlink_health_reporter_get_from_info(struct devlink *devlink,
6291 struct genl_info *info)
6293 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
6296 static struct devlink_health_reporter *
6297 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
6299 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6300 struct devlink_health_reporter *reporter;
6301 struct nlattr **attrs = info->attrs;
6302 struct devlink *devlink;
6304 mutex_lock(&devlink_mutex);
6305 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6306 if (IS_ERR(devlink))
6307 goto unlock;
6309 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
6310 mutex_unlock(&devlink_mutex);
6311 return reporter;
6312 unlock:
6313 mutex_unlock(&devlink_mutex);
6314 return NULL;
6317 void
6318 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
6319 enum devlink_health_reporter_state state)
6321 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
6322 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
6323 return;
6325 if (reporter->health_state == state)
6326 return;
6328 reporter->health_state = state;
6329 trace_devlink_health_reporter_state_update(reporter->devlink,
6330 reporter->ops->name, state);
6331 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6333 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
6335 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
6336 struct genl_info *info)
6338 struct devlink *devlink = info->user_ptr[0];
6339 struct devlink_health_reporter *reporter;
6340 struct sk_buff *msg;
6341 int err;
6343 reporter = devlink_health_reporter_get_from_info(devlink, info);
6344 if (!reporter)
6345 return -EINVAL;
6347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6348 if (!msg) {
6349 err = -ENOMEM;
6350 goto out;
6353 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6354 DEVLINK_CMD_HEALTH_REPORTER_GET,
6355 info->snd_portid, info->snd_seq,
6357 if (err) {
6358 nlmsg_free(msg);
6359 goto out;
6362 err = genlmsg_reply(msg, info);
6363 out:
6364 devlink_health_reporter_put(reporter);
6365 return err;
6368 static int
6369 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
6370 struct netlink_callback *cb)
6372 struct devlink_health_reporter *reporter;
6373 struct devlink_port *port;
6374 struct devlink *devlink;
6375 int start = cb->args[0];
6376 int idx = 0;
6377 int err;
6379 mutex_lock(&devlink_mutex);
6380 list_for_each_entry(devlink, &devlink_list, list) {
6381 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6382 continue;
6383 mutex_lock(&devlink->reporters_lock);
6384 list_for_each_entry(reporter, &devlink->reporter_list,
6385 list) {
6386 if (idx < start) {
6387 idx++;
6388 continue;
6390 err = devlink_nl_health_reporter_fill(msg, devlink,
6391 reporter,
6392 DEVLINK_CMD_HEALTH_REPORTER_GET,
6393 NETLINK_CB(cb->skb).portid,
6394 cb->nlh->nlmsg_seq,
6395 NLM_F_MULTI);
6396 if (err) {
6397 mutex_unlock(&devlink->reporters_lock);
6398 goto out;
6400 idx++;
6402 mutex_unlock(&devlink->reporters_lock);
6405 list_for_each_entry(devlink, &devlink_list, list) {
6406 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6407 continue;
6408 mutex_lock(&devlink->lock);
6409 list_for_each_entry(port, &devlink->port_list, list) {
6410 mutex_lock(&port->reporters_lock);
6411 list_for_each_entry(reporter, &port->reporter_list, list) {
6412 if (idx < start) {
6413 idx++;
6414 continue;
6416 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6417 DEVLINK_CMD_HEALTH_REPORTER_GET,
6418 NETLINK_CB(cb->skb).portid,
6419 cb->nlh->nlmsg_seq,
6420 NLM_F_MULTI);
6421 if (err) {
6422 mutex_unlock(&port->reporters_lock);
6423 mutex_unlock(&devlink->lock);
6424 goto out;
6426 idx++;
6428 mutex_unlock(&port->reporters_lock);
6430 mutex_unlock(&devlink->lock);
6432 out:
6433 mutex_unlock(&devlink_mutex);
6435 cb->args[0] = idx;
6436 return msg->len;
6439 static int
6440 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
6441 struct genl_info *info)
6443 struct devlink *devlink = info->user_ptr[0];
6444 struct devlink_health_reporter *reporter;
6445 int err;
6447 reporter = devlink_health_reporter_get_from_info(devlink, info);
6448 if (!reporter)
6449 return -EINVAL;
6451 if (!reporter->ops->recover &&
6452 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
6453 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
6454 err = -EOPNOTSUPP;
6455 goto out;
6457 if (!reporter->ops->dump &&
6458 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
6459 err = -EOPNOTSUPP;
6460 goto out;
6463 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6464 reporter->graceful_period =
6465 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
6467 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
6468 reporter->auto_recover =
6469 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
6471 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
6472 reporter->auto_dump =
6473 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
6475 devlink_health_reporter_put(reporter);
6476 return 0;
6477 out:
6478 devlink_health_reporter_put(reporter);
6479 return err;
6482 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
6483 struct genl_info *info)
6485 struct devlink *devlink = info->user_ptr[0];
6486 struct devlink_health_reporter *reporter;
6487 int err;
6489 reporter = devlink_health_reporter_get_from_info(devlink, info);
6490 if (!reporter)
6491 return -EINVAL;
6493 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
6495 devlink_health_reporter_put(reporter);
6496 return err;
6499 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
6500 struct genl_info *info)
6502 struct devlink *devlink = info->user_ptr[0];
6503 struct devlink_health_reporter *reporter;
6504 struct devlink_fmsg *fmsg;
6505 int err;
6507 reporter = devlink_health_reporter_get_from_info(devlink, info);
6508 if (!reporter)
6509 return -EINVAL;
6511 if (!reporter->ops->diagnose) {
6512 devlink_health_reporter_put(reporter);
6513 return -EOPNOTSUPP;
6516 fmsg = devlink_fmsg_alloc();
6517 if (!fmsg) {
6518 devlink_health_reporter_put(reporter);
6519 return -ENOMEM;
6522 err = devlink_fmsg_obj_nest_start(fmsg);
6523 if (err)
6524 goto out;
6526 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
6527 if (err)
6528 goto out;
6530 err = devlink_fmsg_obj_nest_end(fmsg);
6531 if (err)
6532 goto out;
6534 err = devlink_fmsg_snd(fmsg, info,
6535 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
6537 out:
6538 devlink_fmsg_free(fmsg);
6539 devlink_health_reporter_put(reporter);
6540 return err;
6543 static int
6544 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
6545 struct netlink_callback *cb)
6547 struct devlink_health_reporter *reporter;
6548 u64 start = cb->args[0];
6549 int err;
6551 reporter = devlink_health_reporter_get_from_cb(cb);
6552 if (!reporter)
6553 return -EINVAL;
6555 if (!reporter->ops->dump) {
6556 err = -EOPNOTSUPP;
6557 goto out;
6559 mutex_lock(&reporter->dump_lock);
6560 if (!start) {
6561 err = devlink_health_do_dump(reporter, NULL, cb->extack);
6562 if (err)
6563 goto unlock;
6564 cb->args[1] = reporter->dump_ts;
6566 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
6567 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
6568 err = -EAGAIN;
6569 goto unlock;
6572 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
6573 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
6574 unlock:
6575 mutex_unlock(&reporter->dump_lock);
6576 out:
6577 devlink_health_reporter_put(reporter);
6578 return err;
6581 static int
6582 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
6583 struct genl_info *info)
6585 struct devlink *devlink = info->user_ptr[0];
6586 struct devlink_health_reporter *reporter;
6588 reporter = devlink_health_reporter_get_from_info(devlink, info);
6589 if (!reporter)
6590 return -EINVAL;
6592 if (!reporter->ops->dump) {
6593 devlink_health_reporter_put(reporter);
6594 return -EOPNOTSUPP;
6597 mutex_lock(&reporter->dump_lock);
6598 devlink_health_dump_clear(reporter);
6599 mutex_unlock(&reporter->dump_lock);
6600 devlink_health_reporter_put(reporter);
6601 return 0;
6604 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
6605 struct genl_info *info)
6607 struct devlink *devlink = info->user_ptr[0];
6608 struct devlink_health_reporter *reporter;
6609 int err;
6611 reporter = devlink_health_reporter_get_from_info(devlink, info);
6612 if (!reporter)
6613 return -EINVAL;
6615 if (!reporter->ops->test) {
6616 devlink_health_reporter_put(reporter);
6617 return -EOPNOTSUPP;
6620 err = reporter->ops->test(reporter, info->extack);
6622 devlink_health_reporter_put(reporter);
6623 return err;
6626 struct devlink_stats {
6627 u64 rx_bytes;
6628 u64 rx_packets;
6629 struct u64_stats_sync syncp;
6633 * struct devlink_trap_policer_item - Packet trap policer attributes.
6634 * @policer: Immutable packet trap policer attributes.
6635 * @rate: Rate in packets / sec.
6636 * @burst: Burst size in packets.
6637 * @list: trap_policer_list member.
6639 * Describes packet trap policer attributes. Created by devlink during trap
6640 * policer registration.
6642 struct devlink_trap_policer_item {
6643 const struct devlink_trap_policer *policer;
6644 u64 rate;
6645 u64 burst;
6646 struct list_head list;
6650 * struct devlink_trap_group_item - Packet trap group attributes.
6651 * @group: Immutable packet trap group attributes.
6652 * @policer_item: Associated policer item. Can be NULL.
6653 * @list: trap_group_list member.
6654 * @stats: Trap group statistics.
6656 * Describes packet trap group attributes. Created by devlink during trap
6657 * group registration.
6659 struct devlink_trap_group_item {
6660 const struct devlink_trap_group *group;
6661 struct devlink_trap_policer_item *policer_item;
6662 struct list_head list;
6663 struct devlink_stats __percpu *stats;
6667 * struct devlink_trap_item - Packet trap attributes.
6668 * @trap: Immutable packet trap attributes.
6669 * @group_item: Associated group item.
6670 * @list: trap_list member.
6671 * @action: Trap action.
6672 * @stats: Trap statistics.
6673 * @priv: Driver private information.
6675 * Describes both mutable and immutable packet trap attributes. Created by
6676 * devlink during trap registration and used for all trap related operations.
6678 struct devlink_trap_item {
6679 const struct devlink_trap *trap;
6680 struct devlink_trap_group_item *group_item;
6681 struct list_head list;
6682 enum devlink_trap_action action;
6683 struct devlink_stats __percpu *stats;
6684 void *priv;
6687 static struct devlink_trap_policer_item *
6688 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
6690 struct devlink_trap_policer_item *policer_item;
6692 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6693 if (policer_item->policer->id == id)
6694 return policer_item;
6697 return NULL;
6700 static struct devlink_trap_item *
6701 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
6703 struct devlink_trap_item *trap_item;
6705 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6706 if (!strcmp(trap_item->trap->name, name))
6707 return trap_item;
6710 return NULL;
6713 static struct devlink_trap_item *
6714 devlink_trap_item_get_from_info(struct devlink *devlink,
6715 struct genl_info *info)
6717 struct nlattr *attr;
6719 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
6720 return NULL;
6721 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
6723 return devlink_trap_item_lookup(devlink, nla_data(attr));
6726 static int
6727 devlink_trap_action_get_from_info(struct genl_info *info,
6728 enum devlink_trap_action *p_trap_action)
6730 u8 val;
6732 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
6733 switch (val) {
6734 case DEVLINK_TRAP_ACTION_DROP:
6735 case DEVLINK_TRAP_ACTION_TRAP:
6736 case DEVLINK_TRAP_ACTION_MIRROR:
6737 *p_trap_action = val;
6738 break;
6739 default:
6740 return -EINVAL;
6743 return 0;
6746 static int devlink_trap_metadata_put(struct sk_buff *msg,
6747 const struct devlink_trap *trap)
6749 struct nlattr *attr;
6751 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
6752 if (!attr)
6753 return -EMSGSIZE;
6755 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
6756 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
6757 goto nla_put_failure;
6758 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
6759 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
6760 goto nla_put_failure;
6762 nla_nest_end(msg, attr);
6764 return 0;
6766 nla_put_failure:
6767 nla_nest_cancel(msg, attr);
6768 return -EMSGSIZE;
6771 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
6772 struct devlink_stats *stats)
6774 int i;
6776 memset(stats, 0, sizeof(*stats));
6777 for_each_possible_cpu(i) {
6778 struct devlink_stats *cpu_stats;
6779 u64 rx_packets, rx_bytes;
6780 unsigned int start;
6782 cpu_stats = per_cpu_ptr(trap_stats, i);
6783 do {
6784 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6785 rx_packets = cpu_stats->rx_packets;
6786 rx_bytes = cpu_stats->rx_bytes;
6787 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6789 stats->rx_packets += rx_packets;
6790 stats->rx_bytes += rx_bytes;
6794 static int devlink_trap_stats_put(struct sk_buff *msg,
6795 struct devlink_stats __percpu *trap_stats)
6797 struct devlink_stats stats;
6798 struct nlattr *attr;
6800 devlink_trap_stats_read(trap_stats, &stats);
6802 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6803 if (!attr)
6804 return -EMSGSIZE;
6806 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
6807 stats.rx_packets, DEVLINK_ATTR_PAD))
6808 goto nla_put_failure;
6810 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
6811 stats.rx_bytes, DEVLINK_ATTR_PAD))
6812 goto nla_put_failure;
6814 nla_nest_end(msg, attr);
6816 return 0;
6818 nla_put_failure:
6819 nla_nest_cancel(msg, attr);
6820 return -EMSGSIZE;
6823 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
6824 const struct devlink_trap_item *trap_item,
6825 enum devlink_command cmd, u32 portid, u32 seq,
6826 int flags)
6828 struct devlink_trap_group_item *group_item = trap_item->group_item;
6829 void *hdr;
6830 int err;
6832 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6833 if (!hdr)
6834 return -EMSGSIZE;
6836 if (devlink_nl_put_handle(msg, devlink))
6837 goto nla_put_failure;
6839 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6840 group_item->group->name))
6841 goto nla_put_failure;
6843 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
6844 goto nla_put_failure;
6846 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
6847 goto nla_put_failure;
6849 if (trap_item->trap->generic &&
6850 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6851 goto nla_put_failure;
6853 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
6854 goto nla_put_failure;
6856 err = devlink_trap_metadata_put(msg, trap_item->trap);
6857 if (err)
6858 goto nla_put_failure;
6860 err = devlink_trap_stats_put(msg, trap_item->stats);
6861 if (err)
6862 goto nla_put_failure;
6864 genlmsg_end(msg, hdr);
6866 return 0;
6868 nla_put_failure:
6869 genlmsg_cancel(msg, hdr);
6870 return -EMSGSIZE;
6873 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6874 struct genl_info *info)
6876 struct netlink_ext_ack *extack = info->extack;
6877 struct devlink *devlink = info->user_ptr[0];
6878 struct devlink_trap_item *trap_item;
6879 struct sk_buff *msg;
6880 int err;
6882 if (list_empty(&devlink->trap_list))
6883 return -EOPNOTSUPP;
6885 trap_item = devlink_trap_item_get_from_info(devlink, info);
6886 if (!trap_item) {
6887 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6888 return -ENOENT;
6891 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6892 if (!msg)
6893 return -ENOMEM;
6895 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6896 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6897 info->snd_seq, 0);
6898 if (err)
6899 goto err_trap_fill;
6901 return genlmsg_reply(msg, info);
6903 err_trap_fill:
6904 nlmsg_free(msg);
6905 return err;
6908 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6909 struct netlink_callback *cb)
6911 struct devlink_trap_item *trap_item;
6912 struct devlink *devlink;
6913 int start = cb->args[0];
6914 int idx = 0;
6915 int err;
6917 mutex_lock(&devlink_mutex);
6918 list_for_each_entry(devlink, &devlink_list, list) {
6919 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6920 continue;
6921 mutex_lock(&devlink->lock);
6922 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6923 if (idx < start) {
6924 idx++;
6925 continue;
6927 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6928 DEVLINK_CMD_TRAP_NEW,
6929 NETLINK_CB(cb->skb).portid,
6930 cb->nlh->nlmsg_seq,
6931 NLM_F_MULTI);
6932 if (err) {
6933 mutex_unlock(&devlink->lock);
6934 goto out;
6936 idx++;
6938 mutex_unlock(&devlink->lock);
6940 out:
6941 mutex_unlock(&devlink_mutex);
6943 cb->args[0] = idx;
6944 return msg->len;
6947 static int __devlink_trap_action_set(struct devlink *devlink,
6948 struct devlink_trap_item *trap_item,
6949 enum devlink_trap_action trap_action,
6950 struct netlink_ext_ack *extack)
6952 int err;
6954 if (trap_item->action != trap_action &&
6955 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6956 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6957 return 0;
6960 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6961 trap_action, extack);
6962 if (err)
6963 return err;
6965 trap_item->action = trap_action;
6967 return 0;
6970 static int devlink_trap_action_set(struct devlink *devlink,
6971 struct devlink_trap_item *trap_item,
6972 struct genl_info *info)
6974 enum devlink_trap_action trap_action;
6975 int err;
6977 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6978 return 0;
6980 err = devlink_trap_action_get_from_info(info, &trap_action);
6981 if (err) {
6982 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6983 return -EINVAL;
6986 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6987 info->extack);
6990 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6991 struct genl_info *info)
6993 struct netlink_ext_ack *extack = info->extack;
6994 struct devlink *devlink = info->user_ptr[0];
6995 struct devlink_trap_item *trap_item;
6997 if (list_empty(&devlink->trap_list))
6998 return -EOPNOTSUPP;
7000 trap_item = devlink_trap_item_get_from_info(devlink, info);
7001 if (!trap_item) {
7002 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
7003 return -ENOENT;
7006 return devlink_trap_action_set(devlink, trap_item, info);
7009 static struct devlink_trap_group_item *
7010 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
7012 struct devlink_trap_group_item *group_item;
7014 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7015 if (!strcmp(group_item->group->name, name))
7016 return group_item;
7019 return NULL;
7022 static struct devlink_trap_group_item *
7023 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
7025 struct devlink_trap_group_item *group_item;
7027 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7028 if (group_item->group->id == id)
7029 return group_item;
7032 return NULL;
7035 static struct devlink_trap_group_item *
7036 devlink_trap_group_item_get_from_info(struct devlink *devlink,
7037 struct genl_info *info)
7039 char *name;
7041 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
7042 return NULL;
7043 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
7045 return devlink_trap_group_item_lookup(devlink, name);
7048 static int
7049 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
7050 const struct devlink_trap_group_item *group_item,
7051 enum devlink_command cmd, u32 portid, u32 seq,
7052 int flags)
7054 void *hdr;
7055 int err;
7057 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7058 if (!hdr)
7059 return -EMSGSIZE;
7061 if (devlink_nl_put_handle(msg, devlink))
7062 goto nla_put_failure;
7064 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7065 group_item->group->name))
7066 goto nla_put_failure;
7068 if (group_item->group->generic &&
7069 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7070 goto nla_put_failure;
7072 if (group_item->policer_item &&
7073 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7074 group_item->policer_item->policer->id))
7075 goto nla_put_failure;
7077 err = devlink_trap_stats_put(msg, group_item->stats);
7078 if (err)
7079 goto nla_put_failure;
7081 genlmsg_end(msg, hdr);
7083 return 0;
7085 nla_put_failure:
7086 genlmsg_cancel(msg, hdr);
7087 return -EMSGSIZE;
7090 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
7091 struct genl_info *info)
7093 struct netlink_ext_ack *extack = info->extack;
7094 struct devlink *devlink = info->user_ptr[0];
7095 struct devlink_trap_group_item *group_item;
7096 struct sk_buff *msg;
7097 int err;
7099 if (list_empty(&devlink->trap_group_list))
7100 return -EOPNOTSUPP;
7102 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7103 if (!group_item) {
7104 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7105 return -ENOENT;
7108 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7109 if (!msg)
7110 return -ENOMEM;
7112 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
7113 DEVLINK_CMD_TRAP_GROUP_NEW,
7114 info->snd_portid, info->snd_seq, 0);
7115 if (err)
7116 goto err_trap_group_fill;
7118 return genlmsg_reply(msg, info);
7120 err_trap_group_fill:
7121 nlmsg_free(msg);
7122 return err;
7125 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
7126 struct netlink_callback *cb)
7128 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
7129 struct devlink_trap_group_item *group_item;
7130 u32 portid = NETLINK_CB(cb->skb).portid;
7131 struct devlink *devlink;
7132 int start = cb->args[0];
7133 int idx = 0;
7134 int err;
7136 mutex_lock(&devlink_mutex);
7137 list_for_each_entry(devlink, &devlink_list, list) {
7138 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7139 continue;
7140 mutex_lock(&devlink->lock);
7141 list_for_each_entry(group_item, &devlink->trap_group_list,
7142 list) {
7143 if (idx < start) {
7144 idx++;
7145 continue;
7147 err = devlink_nl_trap_group_fill(msg, devlink,
7148 group_item, cmd,
7149 portid,
7150 cb->nlh->nlmsg_seq,
7151 NLM_F_MULTI);
7152 if (err) {
7153 mutex_unlock(&devlink->lock);
7154 goto out;
7156 idx++;
7158 mutex_unlock(&devlink->lock);
7160 out:
7161 mutex_unlock(&devlink_mutex);
7163 cb->args[0] = idx;
7164 return msg->len;
7167 static int
7168 __devlink_trap_group_action_set(struct devlink *devlink,
7169 struct devlink_trap_group_item *group_item,
7170 enum devlink_trap_action trap_action,
7171 struct netlink_ext_ack *extack)
7173 const char *group_name = group_item->group->name;
7174 struct devlink_trap_item *trap_item;
7175 int err;
7177 if (devlink->ops->trap_group_action_set) {
7178 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
7179 trap_action, extack);
7180 if (err)
7181 return err;
7183 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7184 if (strcmp(trap_item->group_item->group->name, group_name))
7185 continue;
7186 if (trap_item->action != trap_action &&
7187 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
7188 continue;
7189 trap_item->action = trap_action;
7192 return 0;
7195 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7196 if (strcmp(trap_item->group_item->group->name, group_name))
7197 continue;
7198 err = __devlink_trap_action_set(devlink, trap_item,
7199 trap_action, extack);
7200 if (err)
7201 return err;
7204 return 0;
7207 static int
7208 devlink_trap_group_action_set(struct devlink *devlink,
7209 struct devlink_trap_group_item *group_item,
7210 struct genl_info *info, bool *p_modified)
7212 enum devlink_trap_action trap_action;
7213 int err;
7215 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7216 return 0;
7218 err = devlink_trap_action_get_from_info(info, &trap_action);
7219 if (err) {
7220 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7221 return -EINVAL;
7224 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
7225 info->extack);
7226 if (err)
7227 return err;
7229 *p_modified = true;
7231 return 0;
7234 static int devlink_trap_group_set(struct devlink *devlink,
7235 struct devlink_trap_group_item *group_item,
7236 struct genl_info *info)
7238 struct devlink_trap_policer_item *policer_item;
7239 struct netlink_ext_ack *extack = info->extack;
7240 const struct devlink_trap_policer *policer;
7241 struct nlattr **attrs = info->attrs;
7242 int err;
7244 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7245 return 0;
7247 if (!devlink->ops->trap_group_set)
7248 return -EOPNOTSUPP;
7250 policer_item = group_item->policer_item;
7251 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
7252 u32 policer_id;
7254 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7255 policer_item = devlink_trap_policer_item_lookup(devlink,
7256 policer_id);
7257 if (policer_id && !policer_item) {
7258 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7259 return -ENOENT;
7262 policer = policer_item ? policer_item->policer : NULL;
7264 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
7265 extack);
7266 if (err)
7267 return err;
7269 group_item->policer_item = policer_item;
7271 return 0;
7274 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
7275 struct genl_info *info)
7277 struct netlink_ext_ack *extack = info->extack;
7278 struct devlink *devlink = info->user_ptr[0];
7279 struct devlink_trap_group_item *group_item;
7280 bool modified = false;
7281 int err;
7283 if (list_empty(&devlink->trap_group_list))
7284 return -EOPNOTSUPP;
7286 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7287 if (!group_item) {
7288 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7289 return -ENOENT;
7292 err = devlink_trap_group_action_set(devlink, group_item, info,
7293 &modified);
7294 if (err)
7295 return err;
7297 err = devlink_trap_group_set(devlink, group_item, info);
7298 if (err)
7299 goto err_trap_group_set;
7301 return 0;
7303 err_trap_group_set:
7304 if (modified)
7305 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
7306 return err;
7309 static struct devlink_trap_policer_item *
7310 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
7311 struct genl_info *info)
7313 u32 id;
7315 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7316 return NULL;
7317 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7319 return devlink_trap_policer_item_lookup(devlink, id);
7322 static int
7323 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
7324 const struct devlink_trap_policer *policer)
7326 struct nlattr *attr;
7327 u64 drops;
7328 int err;
7330 if (!devlink->ops->trap_policer_counter_get)
7331 return 0;
7333 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
7334 if (err)
7335 return err;
7337 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7338 if (!attr)
7339 return -EMSGSIZE;
7341 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7342 DEVLINK_ATTR_PAD))
7343 goto nla_put_failure;
7345 nla_nest_end(msg, attr);
7347 return 0;
7349 nla_put_failure:
7350 nla_nest_cancel(msg, attr);
7351 return -EMSGSIZE;
7354 static int
7355 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
7356 const struct devlink_trap_policer_item *policer_item,
7357 enum devlink_command cmd, u32 portid, u32 seq,
7358 int flags)
7360 void *hdr;
7361 int err;
7363 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7364 if (!hdr)
7365 return -EMSGSIZE;
7367 if (devlink_nl_put_handle(msg, devlink))
7368 goto nla_put_failure;
7370 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7371 policer_item->policer->id))
7372 goto nla_put_failure;
7374 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
7375 policer_item->rate, DEVLINK_ATTR_PAD))
7376 goto nla_put_failure;
7378 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
7379 policer_item->burst, DEVLINK_ATTR_PAD))
7380 goto nla_put_failure;
7382 err = devlink_trap_policer_stats_put(msg, devlink,
7383 policer_item->policer);
7384 if (err)
7385 goto nla_put_failure;
7387 genlmsg_end(msg, hdr);
7389 return 0;
7391 nla_put_failure:
7392 genlmsg_cancel(msg, hdr);
7393 return -EMSGSIZE;
7396 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
7397 struct genl_info *info)
7399 struct devlink_trap_policer_item *policer_item;
7400 struct netlink_ext_ack *extack = info->extack;
7401 struct devlink *devlink = info->user_ptr[0];
7402 struct sk_buff *msg;
7403 int err;
7405 if (list_empty(&devlink->trap_policer_list))
7406 return -EOPNOTSUPP;
7408 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7409 if (!policer_item) {
7410 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7411 return -ENOENT;
7414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7415 if (!msg)
7416 return -ENOMEM;
7418 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
7419 DEVLINK_CMD_TRAP_POLICER_NEW,
7420 info->snd_portid, info->snd_seq, 0);
7421 if (err)
7422 goto err_trap_policer_fill;
7424 return genlmsg_reply(msg, info);
7426 err_trap_policer_fill:
7427 nlmsg_free(msg);
7428 return err;
7431 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
7432 struct netlink_callback *cb)
7434 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
7435 struct devlink_trap_policer_item *policer_item;
7436 u32 portid = NETLINK_CB(cb->skb).portid;
7437 struct devlink *devlink;
7438 int start = cb->args[0];
7439 int idx = 0;
7440 int err;
7442 mutex_lock(&devlink_mutex);
7443 list_for_each_entry(devlink, &devlink_list, list) {
7444 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7445 continue;
7446 mutex_lock(&devlink->lock);
7447 list_for_each_entry(policer_item, &devlink->trap_policer_list,
7448 list) {
7449 if (idx < start) {
7450 idx++;
7451 continue;
7453 err = devlink_nl_trap_policer_fill(msg, devlink,
7454 policer_item, cmd,
7455 portid,
7456 cb->nlh->nlmsg_seq,
7457 NLM_F_MULTI);
7458 if (err) {
7459 mutex_unlock(&devlink->lock);
7460 goto out;
7462 idx++;
7464 mutex_unlock(&devlink->lock);
7466 out:
7467 mutex_unlock(&devlink_mutex);
7469 cb->args[0] = idx;
7470 return msg->len;
7473 static int
7474 devlink_trap_policer_set(struct devlink *devlink,
7475 struct devlink_trap_policer_item *policer_item,
7476 struct genl_info *info)
7478 struct netlink_ext_ack *extack = info->extack;
7479 struct nlattr **attrs = info->attrs;
7480 u64 rate, burst;
7481 int err;
7483 rate = policer_item->rate;
7484 burst = policer_item->burst;
7486 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
7487 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
7489 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
7490 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
7492 if (rate < policer_item->policer->min_rate) {
7493 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
7494 return -EINVAL;
7497 if (rate > policer_item->policer->max_rate) {
7498 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
7499 return -EINVAL;
7502 if (burst < policer_item->policer->min_burst) {
7503 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
7504 return -EINVAL;
7507 if (burst > policer_item->policer->max_burst) {
7508 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
7509 return -EINVAL;
7512 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
7513 rate, burst, info->extack);
7514 if (err)
7515 return err;
7517 policer_item->rate = rate;
7518 policer_item->burst = burst;
7520 return 0;
7523 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
7524 struct genl_info *info)
7526 struct devlink_trap_policer_item *policer_item;
7527 struct netlink_ext_ack *extack = info->extack;
7528 struct devlink *devlink = info->user_ptr[0];
7530 if (list_empty(&devlink->trap_policer_list))
7531 return -EOPNOTSUPP;
7533 if (!devlink->ops->trap_policer_set)
7534 return -EOPNOTSUPP;
7536 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7537 if (!policer_item) {
7538 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7539 return -ENOENT;
7542 return devlink_trap_policer_set(devlink, policer_item, info);
7545 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
7546 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
7547 DEVLINK_ATTR_TRAP_POLICER_ID },
7548 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
7549 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
7550 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
7551 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
7552 DEVLINK_PORT_TYPE_IB),
7553 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
7554 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
7555 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
7556 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
7557 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
7558 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
7559 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
7560 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
7561 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
7562 DEVLINK_ESWITCH_MODE_SWITCHDEV),
7563 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
7564 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
7565 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
7566 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
7567 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
7568 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
7569 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
7570 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
7571 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
7572 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
7573 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
7574 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
7575 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
7576 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
7577 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
7578 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
7579 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
7580 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
7581 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
7582 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
7583 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
7584 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
7585 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
7586 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
7587 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
7588 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
7589 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
7590 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
7591 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
7592 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
7593 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
7594 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
7595 DEVLINK_RELOAD_ACTION_MAX),
7596 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
7599 static const struct genl_small_ops devlink_nl_ops[] = {
7601 .cmd = DEVLINK_CMD_GET,
7602 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7603 .doit = devlink_nl_cmd_get_doit,
7604 .dumpit = devlink_nl_cmd_get_dumpit,
7605 /* can be retrieved by unprivileged users */
7608 .cmd = DEVLINK_CMD_PORT_GET,
7609 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7610 .doit = devlink_nl_cmd_port_get_doit,
7611 .dumpit = devlink_nl_cmd_port_get_dumpit,
7612 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7613 /* can be retrieved by unprivileged users */
7616 .cmd = DEVLINK_CMD_PORT_SET,
7617 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7618 .doit = devlink_nl_cmd_port_set_doit,
7619 .flags = GENL_ADMIN_PERM,
7620 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7623 .cmd = DEVLINK_CMD_PORT_SPLIT,
7624 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7625 .doit = devlink_nl_cmd_port_split_doit,
7626 .flags = GENL_ADMIN_PERM,
7627 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7630 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
7631 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7632 .doit = devlink_nl_cmd_port_unsplit_doit,
7633 .flags = GENL_ADMIN_PERM,
7634 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7637 .cmd = DEVLINK_CMD_SB_GET,
7638 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7639 .doit = devlink_nl_cmd_sb_get_doit,
7640 .dumpit = devlink_nl_cmd_sb_get_dumpit,
7641 /* can be retrieved by unprivileged users */
7644 .cmd = DEVLINK_CMD_SB_POOL_GET,
7645 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7646 .doit = devlink_nl_cmd_sb_pool_get_doit,
7647 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
7648 /* can be retrieved by unprivileged users */
7651 .cmd = DEVLINK_CMD_SB_POOL_SET,
7652 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7653 .doit = devlink_nl_cmd_sb_pool_set_doit,
7654 .flags = GENL_ADMIN_PERM,
7657 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
7658 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7659 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
7660 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
7661 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7662 /* can be retrieved by unprivileged users */
7665 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
7666 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7667 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
7668 .flags = GENL_ADMIN_PERM,
7669 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7672 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
7673 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7674 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
7675 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
7676 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7677 /* can be retrieved by unprivileged users */
7680 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
7681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7682 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
7683 .flags = GENL_ADMIN_PERM,
7684 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7687 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
7688 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7689 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
7690 .flags = GENL_ADMIN_PERM,
7693 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
7694 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7695 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
7696 .flags = GENL_ADMIN_PERM,
7699 .cmd = DEVLINK_CMD_ESWITCH_GET,
7700 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7701 .doit = devlink_nl_cmd_eswitch_get_doit,
7702 .flags = GENL_ADMIN_PERM,
7703 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7706 .cmd = DEVLINK_CMD_ESWITCH_SET,
7707 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7708 .doit = devlink_nl_cmd_eswitch_set_doit,
7709 .flags = GENL_ADMIN_PERM,
7710 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7713 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
7714 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7715 .doit = devlink_nl_cmd_dpipe_table_get,
7716 /* can be retrieved by unprivileged users */
7719 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
7720 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7721 .doit = devlink_nl_cmd_dpipe_entries_get,
7722 /* can be retrieved by unprivileged users */
7725 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
7726 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7727 .doit = devlink_nl_cmd_dpipe_headers_get,
7728 /* can be retrieved by unprivileged users */
7731 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
7732 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7733 .doit = devlink_nl_cmd_dpipe_table_counters_set,
7734 .flags = GENL_ADMIN_PERM,
7737 .cmd = DEVLINK_CMD_RESOURCE_SET,
7738 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7739 .doit = devlink_nl_cmd_resource_set,
7740 .flags = GENL_ADMIN_PERM,
7743 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
7744 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7745 .doit = devlink_nl_cmd_resource_dump,
7746 /* can be retrieved by unprivileged users */
7749 .cmd = DEVLINK_CMD_RELOAD,
7750 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7751 .doit = devlink_nl_cmd_reload,
7752 .flags = GENL_ADMIN_PERM,
7753 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7756 .cmd = DEVLINK_CMD_PARAM_GET,
7757 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7758 .doit = devlink_nl_cmd_param_get_doit,
7759 .dumpit = devlink_nl_cmd_param_get_dumpit,
7760 /* can be retrieved by unprivileged users */
7763 .cmd = DEVLINK_CMD_PARAM_SET,
7764 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7765 .doit = devlink_nl_cmd_param_set_doit,
7766 .flags = GENL_ADMIN_PERM,
7769 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
7770 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7771 .doit = devlink_nl_cmd_port_param_get_doit,
7772 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
7773 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7774 /* can be retrieved by unprivileged users */
7777 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
7778 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7779 .doit = devlink_nl_cmd_port_param_set_doit,
7780 .flags = GENL_ADMIN_PERM,
7781 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7784 .cmd = DEVLINK_CMD_REGION_GET,
7785 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7786 .doit = devlink_nl_cmd_region_get_doit,
7787 .dumpit = devlink_nl_cmd_region_get_dumpit,
7788 .flags = GENL_ADMIN_PERM,
7791 .cmd = DEVLINK_CMD_REGION_NEW,
7792 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7793 .doit = devlink_nl_cmd_region_new,
7794 .flags = GENL_ADMIN_PERM,
7797 .cmd = DEVLINK_CMD_REGION_DEL,
7798 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7799 .doit = devlink_nl_cmd_region_del,
7800 .flags = GENL_ADMIN_PERM,
7803 .cmd = DEVLINK_CMD_REGION_READ,
7804 .validate = GENL_DONT_VALIDATE_STRICT |
7805 GENL_DONT_VALIDATE_DUMP_STRICT,
7806 .dumpit = devlink_nl_cmd_region_read_dumpit,
7807 .flags = GENL_ADMIN_PERM,
7810 .cmd = DEVLINK_CMD_INFO_GET,
7811 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7812 .doit = devlink_nl_cmd_info_get_doit,
7813 .dumpit = devlink_nl_cmd_info_get_dumpit,
7814 /* can be retrieved by unprivileged users */
7817 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
7818 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7819 .doit = devlink_nl_cmd_health_reporter_get_doit,
7820 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
7821 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7822 DEVLINK_NL_FLAG_NO_LOCK,
7823 /* can be retrieved by unprivileged users */
7826 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
7827 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7828 .doit = devlink_nl_cmd_health_reporter_set_doit,
7829 .flags = GENL_ADMIN_PERM,
7830 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7831 DEVLINK_NL_FLAG_NO_LOCK,
7834 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7835 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7836 .doit = devlink_nl_cmd_health_reporter_recover_doit,
7837 .flags = GENL_ADMIN_PERM,
7838 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7839 DEVLINK_NL_FLAG_NO_LOCK,
7842 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
7843 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7844 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
7845 .flags = GENL_ADMIN_PERM,
7846 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7847 DEVLINK_NL_FLAG_NO_LOCK,
7850 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
7851 .validate = GENL_DONT_VALIDATE_STRICT |
7852 GENL_DONT_VALIDATE_DUMP_STRICT,
7853 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
7854 .flags = GENL_ADMIN_PERM,
7855 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7856 DEVLINK_NL_FLAG_NO_LOCK,
7859 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7860 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7861 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
7862 .flags = GENL_ADMIN_PERM,
7863 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7864 DEVLINK_NL_FLAG_NO_LOCK,
7867 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
7868 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7869 .doit = devlink_nl_cmd_health_reporter_test_doit,
7870 .flags = GENL_ADMIN_PERM,
7871 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7872 DEVLINK_NL_FLAG_NO_LOCK,
7875 .cmd = DEVLINK_CMD_FLASH_UPDATE,
7876 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7877 .doit = devlink_nl_cmd_flash_update,
7878 .flags = GENL_ADMIN_PERM,
7881 .cmd = DEVLINK_CMD_TRAP_GET,
7882 .doit = devlink_nl_cmd_trap_get_doit,
7883 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7884 /* can be retrieved by unprivileged users */
7887 .cmd = DEVLINK_CMD_TRAP_SET,
7888 .doit = devlink_nl_cmd_trap_set_doit,
7889 .flags = GENL_ADMIN_PERM,
7892 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7893 .doit = devlink_nl_cmd_trap_group_get_doit,
7894 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7895 /* can be retrieved by unprivileged users */
7898 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7899 .doit = devlink_nl_cmd_trap_group_set_doit,
7900 .flags = GENL_ADMIN_PERM,
7903 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7904 .doit = devlink_nl_cmd_trap_policer_get_doit,
7905 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7906 /* can be retrieved by unprivileged users */
7909 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7910 .doit = devlink_nl_cmd_trap_policer_set_doit,
7911 .flags = GENL_ADMIN_PERM,
7915 static struct genl_family devlink_nl_family __ro_after_init = {
7916 .name = DEVLINK_GENL_NAME,
7917 .version = DEVLINK_GENL_VERSION,
7918 .maxattr = DEVLINK_ATTR_MAX,
7919 .policy = devlink_nl_policy,
7920 .netnsok = true,
7921 .pre_doit = devlink_nl_pre_doit,
7922 .post_doit = devlink_nl_post_doit,
7923 .module = THIS_MODULE,
7924 .small_ops = devlink_nl_ops,
7925 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
7926 .mcgrps = devlink_nl_mcgrps,
7927 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7930 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
7932 const struct devlink_reload_combination *comb;
7933 int i;
7935 if (!devlink_reload_supported(ops)) {
7936 if (WARN_ON(ops->reload_actions))
7937 return false;
7938 return true;
7941 if (WARN_ON(!ops->reload_actions ||
7942 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
7943 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
7944 return false;
7946 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
7947 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
7948 return false;
7950 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
7951 comb = &devlink_reload_invalid_combinations[i];
7952 if (ops->reload_actions == BIT(comb->action) &&
7953 ops->reload_limits == BIT(comb->limit))
7954 return false;
7956 return true;
7960 * devlink_alloc - Allocate new devlink instance resources
7962 * @ops: ops
7963 * @priv_size: size of user private data
7965 * Allocate new devlink instance resources, including devlink index
7966 * and name.
7968 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7970 struct devlink *devlink;
7972 if (WARN_ON(!ops))
7973 return NULL;
7975 if (!devlink_reload_actions_valid(ops))
7976 return NULL;
7978 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7979 if (!devlink)
7980 return NULL;
7981 devlink->ops = ops;
7982 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
7983 __devlink_net_set(devlink, &init_net);
7984 INIT_LIST_HEAD(&devlink->port_list);
7985 INIT_LIST_HEAD(&devlink->sb_list);
7986 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
7987 INIT_LIST_HEAD(&devlink->resource_list);
7988 INIT_LIST_HEAD(&devlink->param_list);
7989 INIT_LIST_HEAD(&devlink->region_list);
7990 INIT_LIST_HEAD(&devlink->reporter_list);
7991 INIT_LIST_HEAD(&devlink->trap_list);
7992 INIT_LIST_HEAD(&devlink->trap_group_list);
7993 INIT_LIST_HEAD(&devlink->trap_policer_list);
7994 mutex_init(&devlink->lock);
7995 mutex_init(&devlink->reporters_lock);
7996 return devlink;
7998 EXPORT_SYMBOL_GPL(devlink_alloc);
8001 * devlink_register - Register devlink instance
8003 * @devlink: devlink
8004 * @dev: parent device
8006 int devlink_register(struct devlink *devlink, struct device *dev)
8008 devlink->dev = dev;
8009 devlink->registered = true;
8010 mutex_lock(&devlink_mutex);
8011 list_add_tail(&devlink->list, &devlink_list);
8012 devlink_notify(devlink, DEVLINK_CMD_NEW);
8013 mutex_unlock(&devlink_mutex);
8014 return 0;
8016 EXPORT_SYMBOL_GPL(devlink_register);
8019 * devlink_unregister - Unregister devlink instance
8021 * @devlink: devlink
8023 void devlink_unregister(struct devlink *devlink)
8025 mutex_lock(&devlink_mutex);
8026 WARN_ON(devlink_reload_supported(devlink->ops) &&
8027 devlink->reload_enabled);
8028 devlink_notify(devlink, DEVLINK_CMD_DEL);
8029 list_del(&devlink->list);
8030 mutex_unlock(&devlink_mutex);
8032 EXPORT_SYMBOL_GPL(devlink_unregister);
8035 * devlink_reload_enable - Enable reload of devlink instance
8037 * @devlink: devlink
8039 * Should be called at end of device initialization
8040 * process when reload operation is supported.
8042 void devlink_reload_enable(struct devlink *devlink)
8044 mutex_lock(&devlink_mutex);
8045 devlink->reload_enabled = true;
8046 mutex_unlock(&devlink_mutex);
8048 EXPORT_SYMBOL_GPL(devlink_reload_enable);
8051 * devlink_reload_disable - Disable reload of devlink instance
8053 * @devlink: devlink
8055 * Should be called at the beginning of device cleanup
8056 * process when reload operation is supported.
8058 void devlink_reload_disable(struct devlink *devlink)
8060 mutex_lock(&devlink_mutex);
8061 /* Mutex is taken which ensures that no reload operation is in
8062 * progress while setting up forbidded flag.
8064 devlink->reload_enabled = false;
8065 mutex_unlock(&devlink_mutex);
8067 EXPORT_SYMBOL_GPL(devlink_reload_disable);
8070 * devlink_free - Free devlink instance resources
8072 * @devlink: devlink
8074 void devlink_free(struct devlink *devlink)
8076 mutex_destroy(&devlink->reporters_lock);
8077 mutex_destroy(&devlink->lock);
8078 WARN_ON(!list_empty(&devlink->trap_policer_list));
8079 WARN_ON(!list_empty(&devlink->trap_group_list));
8080 WARN_ON(!list_empty(&devlink->trap_list));
8081 WARN_ON(!list_empty(&devlink->reporter_list));
8082 WARN_ON(!list_empty(&devlink->region_list));
8083 WARN_ON(!list_empty(&devlink->param_list));
8084 WARN_ON(!list_empty(&devlink->resource_list));
8085 WARN_ON(!list_empty(&devlink->dpipe_table_list));
8086 WARN_ON(!list_empty(&devlink->sb_list));
8087 WARN_ON(!list_empty(&devlink->port_list));
8089 xa_destroy(&devlink->snapshot_ids);
8091 kfree(devlink);
8093 EXPORT_SYMBOL_GPL(devlink_free);
8095 static void devlink_port_type_warn(struct work_struct *work)
8097 WARN(true, "Type was not set for devlink port.");
8100 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
8102 /* Ignore CPU and DSA flavours. */
8103 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
8104 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
8105 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
8108 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
8110 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
8112 if (!devlink_port_type_should_warn(devlink_port))
8113 return;
8114 /* Schedule a work to WARN in case driver does not set port
8115 * type within timeout.
8117 schedule_delayed_work(&devlink_port->type_warn_dw,
8118 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
8121 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
8123 if (!devlink_port_type_should_warn(devlink_port))
8124 return;
8125 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
8129 * devlink_port_register - Register devlink port
8131 * @devlink: devlink
8132 * @devlink_port: devlink port
8133 * @port_index: driver-specific numerical identifier of the port
8135 * Register devlink port with provided port index. User can use
8136 * any indexing, even hw-related one. devlink_port structure
8137 * is convenient to be embedded inside user driver private structure.
8138 * Note that the caller should take care of zeroing the devlink_port
8139 * structure.
8141 int devlink_port_register(struct devlink *devlink,
8142 struct devlink_port *devlink_port,
8143 unsigned int port_index)
8145 mutex_lock(&devlink->lock);
8146 if (devlink_port_index_exists(devlink, port_index)) {
8147 mutex_unlock(&devlink->lock);
8148 return -EEXIST;
8150 devlink_port->devlink = devlink;
8151 devlink_port->index = port_index;
8152 devlink_port->registered = true;
8153 spin_lock_init(&devlink_port->type_lock);
8154 INIT_LIST_HEAD(&devlink_port->reporter_list);
8155 mutex_init(&devlink_port->reporters_lock);
8156 list_add_tail(&devlink_port->list, &devlink->port_list);
8157 INIT_LIST_HEAD(&devlink_port->param_list);
8158 INIT_LIST_HEAD(&devlink_port->region_list);
8159 mutex_unlock(&devlink->lock);
8160 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
8161 devlink_port_type_warn_schedule(devlink_port);
8162 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8163 return 0;
8165 EXPORT_SYMBOL_GPL(devlink_port_register);
8168 * devlink_port_unregister - Unregister devlink port
8170 * @devlink_port: devlink port
8172 void devlink_port_unregister(struct devlink_port *devlink_port)
8174 struct devlink *devlink = devlink_port->devlink;
8176 devlink_port_type_warn_cancel(devlink_port);
8177 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
8178 mutex_lock(&devlink->lock);
8179 list_del(&devlink_port->list);
8180 mutex_unlock(&devlink->lock);
8181 WARN_ON(!list_empty(&devlink_port->reporter_list));
8182 WARN_ON(!list_empty(&devlink_port->region_list));
8183 mutex_destroy(&devlink_port->reporters_lock);
8185 EXPORT_SYMBOL_GPL(devlink_port_unregister);
8187 static void __devlink_port_type_set(struct devlink_port *devlink_port,
8188 enum devlink_port_type type,
8189 void *type_dev)
8191 if (WARN_ON(!devlink_port->registered))
8192 return;
8193 devlink_port_type_warn_cancel(devlink_port);
8194 spin_lock_bh(&devlink_port->type_lock);
8195 devlink_port->type = type;
8196 devlink_port->type_dev = type_dev;
8197 spin_unlock_bh(&devlink_port->type_lock);
8198 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8201 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
8202 struct net_device *netdev)
8204 const struct net_device_ops *ops = netdev->netdev_ops;
8206 /* If driver registers devlink port, it should set devlink port
8207 * attributes accordingly so the compat functions are called
8208 * and the original ops are not used.
8210 if (ops->ndo_get_phys_port_name) {
8211 /* Some drivers use the same set of ndos for netdevs
8212 * that have devlink_port registered and also for
8213 * those who don't. Make sure that ndo_get_phys_port_name
8214 * returns -EOPNOTSUPP here in case it is defined.
8215 * Warn if not.
8217 char name[IFNAMSIZ];
8218 int err;
8220 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
8221 WARN_ON(err != -EOPNOTSUPP);
8223 if (ops->ndo_get_port_parent_id) {
8224 /* Some drivers use the same set of ndos for netdevs
8225 * that have devlink_port registered and also for
8226 * those who don't. Make sure that ndo_get_port_parent_id
8227 * returns -EOPNOTSUPP here in case it is defined.
8228 * Warn if not.
8230 struct netdev_phys_item_id ppid;
8231 int err;
8233 err = ops->ndo_get_port_parent_id(netdev, &ppid);
8234 WARN_ON(err != -EOPNOTSUPP);
8239 * devlink_port_type_eth_set - Set port type to Ethernet
8241 * @devlink_port: devlink port
8242 * @netdev: related netdevice
8244 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
8245 struct net_device *netdev)
8247 if (netdev)
8248 devlink_port_type_netdev_checks(devlink_port, netdev);
8249 else
8250 dev_warn(devlink_port->devlink->dev,
8251 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
8252 devlink_port->index);
8254 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
8256 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
8259 * devlink_port_type_ib_set - Set port type to InfiniBand
8261 * @devlink_port: devlink port
8262 * @ibdev: related IB device
8264 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
8265 struct ib_device *ibdev)
8267 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
8269 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
8272 * devlink_port_type_clear - Clear port type
8274 * @devlink_port: devlink port
8276 void devlink_port_type_clear(struct devlink_port *devlink_port)
8278 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
8279 devlink_port_type_warn_schedule(devlink_port);
8281 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
8283 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
8284 enum devlink_port_flavour flavour)
8286 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8288 devlink_port->attrs_set = true;
8289 attrs->flavour = flavour;
8290 if (attrs->switch_id.id_len) {
8291 devlink_port->switch_port = true;
8292 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
8293 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
8294 } else {
8295 devlink_port->switch_port = false;
8297 return 0;
8301 * devlink_port_attrs_set - Set port attributes
8303 * @devlink_port: devlink port
8304 * @attrs: devlink port attrs
8306 void devlink_port_attrs_set(struct devlink_port *devlink_port,
8307 struct devlink_port_attrs *attrs)
8309 int ret;
8311 if (WARN_ON(devlink_port->registered))
8312 return;
8313 devlink_port->attrs = *attrs;
8314 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
8315 if (ret)
8316 return;
8317 WARN_ON(attrs->splittable && attrs->split);
8319 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
8322 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
8324 * @devlink_port: devlink port
8325 * @controller: associated controller number for the devlink port instance
8326 * @pf: associated PF for the devlink port instance
8327 * @external: indicates if the port is for an external controller
8329 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
8330 u16 pf, bool external)
8332 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8333 int ret;
8335 if (WARN_ON(devlink_port->registered))
8336 return;
8337 ret = __devlink_port_attrs_set(devlink_port,
8338 DEVLINK_PORT_FLAVOUR_PCI_PF);
8339 if (ret)
8340 return;
8341 attrs->pci_pf.controller = controller;
8342 attrs->pci_pf.pf = pf;
8343 attrs->pci_pf.external = external;
8345 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
8348 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
8350 * @devlink_port: devlink port
8351 * @controller: associated controller number for the devlink port instance
8352 * @pf: associated PF for the devlink port instance
8353 * @vf: associated VF of a PF for the devlink port instance
8354 * @external: indicates if the port is for an external controller
8356 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
8357 u16 pf, u16 vf, bool external)
8359 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8360 int ret;
8362 if (WARN_ON(devlink_port->registered))
8363 return;
8364 ret = __devlink_port_attrs_set(devlink_port,
8365 DEVLINK_PORT_FLAVOUR_PCI_VF);
8366 if (ret)
8367 return;
8368 attrs->pci_vf.controller = controller;
8369 attrs->pci_vf.pf = pf;
8370 attrs->pci_vf.vf = vf;
8371 attrs->pci_vf.external = external;
8373 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
8375 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
8376 char *name, size_t len)
8378 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8379 int n = 0;
8381 if (!devlink_port->attrs_set)
8382 return -EOPNOTSUPP;
8384 switch (attrs->flavour) {
8385 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
8386 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
8387 if (!attrs->split)
8388 n = snprintf(name, len, "p%u", attrs->phys.port_number);
8389 else
8390 n = snprintf(name, len, "p%us%u",
8391 attrs->phys.port_number,
8392 attrs->phys.split_subport_number);
8393 break;
8394 case DEVLINK_PORT_FLAVOUR_CPU:
8395 case DEVLINK_PORT_FLAVOUR_DSA:
8396 case DEVLINK_PORT_FLAVOUR_UNUSED:
8397 /* As CPU and DSA ports do not have a netdevice associated
8398 * case should not ever happen.
8400 WARN_ON(1);
8401 return -EINVAL;
8402 case DEVLINK_PORT_FLAVOUR_PCI_PF:
8403 if (attrs->pci_pf.external) {
8404 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
8405 if (n >= len)
8406 return -EINVAL;
8407 len -= n;
8408 name += n;
8410 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
8411 break;
8412 case DEVLINK_PORT_FLAVOUR_PCI_VF:
8413 if (attrs->pci_vf.external) {
8414 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
8415 if (n >= len)
8416 return -EINVAL;
8417 len -= n;
8418 name += n;
8420 n = snprintf(name, len, "pf%uvf%u",
8421 attrs->pci_vf.pf, attrs->pci_vf.vf);
8422 break;
8425 if (n >= len)
8426 return -EINVAL;
8428 return 0;
8431 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
8432 u32 size, u16 ingress_pools_count,
8433 u16 egress_pools_count, u16 ingress_tc_count,
8434 u16 egress_tc_count)
8436 struct devlink_sb *devlink_sb;
8437 int err = 0;
8439 mutex_lock(&devlink->lock);
8440 if (devlink_sb_index_exists(devlink, sb_index)) {
8441 err = -EEXIST;
8442 goto unlock;
8445 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
8446 if (!devlink_sb) {
8447 err = -ENOMEM;
8448 goto unlock;
8450 devlink_sb->index = sb_index;
8451 devlink_sb->size = size;
8452 devlink_sb->ingress_pools_count = ingress_pools_count;
8453 devlink_sb->egress_pools_count = egress_pools_count;
8454 devlink_sb->ingress_tc_count = ingress_tc_count;
8455 devlink_sb->egress_tc_count = egress_tc_count;
8456 list_add_tail(&devlink_sb->list, &devlink->sb_list);
8457 unlock:
8458 mutex_unlock(&devlink->lock);
8459 return err;
8461 EXPORT_SYMBOL_GPL(devlink_sb_register);
8463 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
8465 struct devlink_sb *devlink_sb;
8467 mutex_lock(&devlink->lock);
8468 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
8469 WARN_ON(!devlink_sb);
8470 list_del(&devlink_sb->list);
8471 mutex_unlock(&devlink->lock);
8472 kfree(devlink_sb);
8474 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
8477 * devlink_dpipe_headers_register - register dpipe headers
8479 * @devlink: devlink
8480 * @dpipe_headers: dpipe header array
8482 * Register the headers supported by hardware.
8484 int devlink_dpipe_headers_register(struct devlink *devlink,
8485 struct devlink_dpipe_headers *dpipe_headers)
8487 mutex_lock(&devlink->lock);
8488 devlink->dpipe_headers = dpipe_headers;
8489 mutex_unlock(&devlink->lock);
8490 return 0;
8492 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
8495 * devlink_dpipe_headers_unregister - unregister dpipe headers
8497 * @devlink: devlink
8499 * Unregister the headers supported by hardware.
8501 void devlink_dpipe_headers_unregister(struct devlink *devlink)
8503 mutex_lock(&devlink->lock);
8504 devlink->dpipe_headers = NULL;
8505 mutex_unlock(&devlink->lock);
8507 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
8510 * devlink_dpipe_table_counter_enabled - check if counter allocation
8511 * required
8512 * @devlink: devlink
8513 * @table_name: tables name
8515 * Used by driver to check if counter allocation is required.
8516 * After counter allocation is turned on the table entries
8517 * are updated to include counter statistics.
8519 * After that point on the driver must respect the counter
8520 * state so that each entry added to the table is added
8521 * with a counter.
8523 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
8524 const char *table_name)
8526 struct devlink_dpipe_table *table;
8527 bool enabled;
8529 rcu_read_lock();
8530 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8531 table_name, devlink);
8532 enabled = false;
8533 if (table)
8534 enabled = table->counters_enabled;
8535 rcu_read_unlock();
8536 return enabled;
8538 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
8541 * devlink_dpipe_table_register - register dpipe table
8543 * @devlink: devlink
8544 * @table_name: table name
8545 * @table_ops: table ops
8546 * @priv: priv
8547 * @counter_control_extern: external control for counters
8549 int devlink_dpipe_table_register(struct devlink *devlink,
8550 const char *table_name,
8551 struct devlink_dpipe_table_ops *table_ops,
8552 void *priv, bool counter_control_extern)
8554 struct devlink_dpipe_table *table;
8555 int err = 0;
8557 if (WARN_ON(!table_ops->size_get))
8558 return -EINVAL;
8560 mutex_lock(&devlink->lock);
8562 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
8563 devlink)) {
8564 err = -EEXIST;
8565 goto unlock;
8568 table = kzalloc(sizeof(*table), GFP_KERNEL);
8569 if (!table) {
8570 err = -ENOMEM;
8571 goto unlock;
8574 table->name = table_name;
8575 table->table_ops = table_ops;
8576 table->priv = priv;
8577 table->counter_control_extern = counter_control_extern;
8579 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
8580 unlock:
8581 mutex_unlock(&devlink->lock);
8582 return err;
8584 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
8587 * devlink_dpipe_table_unregister - unregister dpipe table
8589 * @devlink: devlink
8590 * @table_name: table name
8592 void devlink_dpipe_table_unregister(struct devlink *devlink,
8593 const char *table_name)
8595 struct devlink_dpipe_table *table;
8597 mutex_lock(&devlink->lock);
8598 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8599 table_name, devlink);
8600 if (!table)
8601 goto unlock;
8602 list_del_rcu(&table->list);
8603 mutex_unlock(&devlink->lock);
8604 kfree_rcu(table, rcu);
8605 return;
8606 unlock:
8607 mutex_unlock(&devlink->lock);
8609 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
8612 * devlink_resource_register - devlink resource register
8614 * @devlink: devlink
8615 * @resource_name: resource's name
8616 * @resource_size: resource's size
8617 * @resource_id: resource's id
8618 * @parent_resource_id: resource's parent id
8619 * @size_params: size parameters
8621 int devlink_resource_register(struct devlink *devlink,
8622 const char *resource_name,
8623 u64 resource_size,
8624 u64 resource_id,
8625 u64 parent_resource_id,
8626 const struct devlink_resource_size_params *size_params)
8628 struct devlink_resource *resource;
8629 struct list_head *resource_list;
8630 bool top_hierarchy;
8631 int err = 0;
8633 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
8635 mutex_lock(&devlink->lock);
8636 resource = devlink_resource_find(devlink, NULL, resource_id);
8637 if (resource) {
8638 err = -EINVAL;
8639 goto out;
8642 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
8643 if (!resource) {
8644 err = -ENOMEM;
8645 goto out;
8648 if (top_hierarchy) {
8649 resource_list = &devlink->resource_list;
8650 } else {
8651 struct devlink_resource *parent_resource;
8653 parent_resource = devlink_resource_find(devlink, NULL,
8654 parent_resource_id);
8655 if (parent_resource) {
8656 resource_list = &parent_resource->resource_list;
8657 resource->parent = parent_resource;
8658 } else {
8659 kfree(resource);
8660 err = -EINVAL;
8661 goto out;
8665 resource->name = resource_name;
8666 resource->size = resource_size;
8667 resource->size_new = resource_size;
8668 resource->id = resource_id;
8669 resource->size_valid = true;
8670 memcpy(&resource->size_params, size_params,
8671 sizeof(resource->size_params));
8672 INIT_LIST_HEAD(&resource->resource_list);
8673 list_add_tail(&resource->list, resource_list);
8674 out:
8675 mutex_unlock(&devlink->lock);
8676 return err;
8678 EXPORT_SYMBOL_GPL(devlink_resource_register);
8681 * devlink_resources_unregister - free all resources
8683 * @devlink: devlink
8684 * @resource: resource
8686 void devlink_resources_unregister(struct devlink *devlink,
8687 struct devlink_resource *resource)
8689 struct devlink_resource *tmp, *child_resource;
8690 struct list_head *resource_list;
8692 if (resource)
8693 resource_list = &resource->resource_list;
8694 else
8695 resource_list = &devlink->resource_list;
8697 if (!resource)
8698 mutex_lock(&devlink->lock);
8700 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
8701 devlink_resources_unregister(devlink, child_resource);
8702 list_del(&child_resource->list);
8703 kfree(child_resource);
8706 if (!resource)
8707 mutex_unlock(&devlink->lock);
8709 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8712 * devlink_resource_size_get - get and update size
8714 * @devlink: devlink
8715 * @resource_id: the requested resource id
8716 * @p_resource_size: ptr to update
8718 int devlink_resource_size_get(struct devlink *devlink,
8719 u64 resource_id,
8720 u64 *p_resource_size)
8722 struct devlink_resource *resource;
8723 int err = 0;
8725 mutex_lock(&devlink->lock);
8726 resource = devlink_resource_find(devlink, NULL, resource_id);
8727 if (!resource) {
8728 err = -EINVAL;
8729 goto out;
8731 *p_resource_size = resource->size_new;
8732 resource->size = resource->size_new;
8733 out:
8734 mutex_unlock(&devlink->lock);
8735 return err;
8737 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
8740 * devlink_dpipe_table_resource_set - set the resource id
8742 * @devlink: devlink
8743 * @table_name: table name
8744 * @resource_id: resource id
8745 * @resource_units: number of resource's units consumed per table's entry
8747 int devlink_dpipe_table_resource_set(struct devlink *devlink,
8748 const char *table_name, u64 resource_id,
8749 u64 resource_units)
8751 struct devlink_dpipe_table *table;
8752 int err = 0;
8754 mutex_lock(&devlink->lock);
8755 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8756 table_name, devlink);
8757 if (!table) {
8758 err = -EINVAL;
8759 goto out;
8761 table->resource_id = resource_id;
8762 table->resource_units = resource_units;
8763 table->resource_valid = true;
8764 out:
8765 mutex_unlock(&devlink->lock);
8766 return err;
8768 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
8771 * devlink_resource_occ_get_register - register occupancy getter
8773 * @devlink: devlink
8774 * @resource_id: resource id
8775 * @occ_get: occupancy getter callback
8776 * @occ_get_priv: occupancy getter callback priv
8778 void devlink_resource_occ_get_register(struct devlink *devlink,
8779 u64 resource_id,
8780 devlink_resource_occ_get_t *occ_get,
8781 void *occ_get_priv)
8783 struct devlink_resource *resource;
8785 mutex_lock(&devlink->lock);
8786 resource = devlink_resource_find(devlink, NULL, resource_id);
8787 if (WARN_ON(!resource))
8788 goto out;
8789 WARN_ON(resource->occ_get);
8791 resource->occ_get = occ_get;
8792 resource->occ_get_priv = occ_get_priv;
8793 out:
8794 mutex_unlock(&devlink->lock);
8796 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8799 * devlink_resource_occ_get_unregister - unregister occupancy getter
8801 * @devlink: devlink
8802 * @resource_id: resource id
8804 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8805 u64 resource_id)
8807 struct devlink_resource *resource;
8809 mutex_lock(&devlink->lock);
8810 resource = devlink_resource_find(devlink, NULL, resource_id);
8811 if (WARN_ON(!resource))
8812 goto out;
8813 WARN_ON(!resource->occ_get);
8815 resource->occ_get = NULL;
8816 resource->occ_get_priv = NULL;
8817 out:
8818 mutex_unlock(&devlink->lock);
8820 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8822 static int devlink_param_verify(const struct devlink_param *param)
8824 if (!param || !param->name || !param->supported_cmodes)
8825 return -EINVAL;
8826 if (param->generic)
8827 return devlink_param_generic_verify(param);
8828 else
8829 return devlink_param_driver_verify(param);
8832 static int __devlink_params_register(struct devlink *devlink,
8833 unsigned int port_index,
8834 struct list_head *param_list,
8835 const struct devlink_param *params,
8836 size_t params_count,
8837 enum devlink_command reg_cmd,
8838 enum devlink_command unreg_cmd)
8840 const struct devlink_param *param = params;
8841 int i;
8842 int err;
8844 mutex_lock(&devlink->lock);
8845 for (i = 0; i < params_count; i++, param++) {
8846 err = devlink_param_verify(param);
8847 if (err)
8848 goto rollback;
8850 err = devlink_param_register_one(devlink, port_index,
8851 param_list, param, reg_cmd);
8852 if (err)
8853 goto rollback;
8856 mutex_unlock(&devlink->lock);
8857 return 0;
8859 rollback:
8860 if (!i)
8861 goto unlock;
8862 for (param--; i > 0; i--, param--)
8863 devlink_param_unregister_one(devlink, port_index, param_list,
8864 param, unreg_cmd);
8865 unlock:
8866 mutex_unlock(&devlink->lock);
8867 return err;
8870 static void __devlink_params_unregister(struct devlink *devlink,
8871 unsigned int port_index,
8872 struct list_head *param_list,
8873 const struct devlink_param *params,
8874 size_t params_count,
8875 enum devlink_command cmd)
8877 const struct devlink_param *param = params;
8878 int i;
8880 mutex_lock(&devlink->lock);
8881 for (i = 0; i < params_count; i++, param++)
8882 devlink_param_unregister_one(devlink, 0, param_list, param,
8883 cmd);
8884 mutex_unlock(&devlink->lock);
8888 * devlink_params_register - register configuration parameters
8890 * @devlink: devlink
8891 * @params: configuration parameters array
8892 * @params_count: number of parameters provided
8894 * Register the configuration parameters supported by the driver.
8896 int devlink_params_register(struct devlink *devlink,
8897 const struct devlink_param *params,
8898 size_t params_count)
8900 return __devlink_params_register(devlink, 0, &devlink->param_list,
8901 params, params_count,
8902 DEVLINK_CMD_PARAM_NEW,
8903 DEVLINK_CMD_PARAM_DEL);
8905 EXPORT_SYMBOL_GPL(devlink_params_register);
8908 * devlink_params_unregister - unregister configuration parameters
8909 * @devlink: devlink
8910 * @params: configuration parameters to unregister
8911 * @params_count: number of parameters provided
8913 void devlink_params_unregister(struct devlink *devlink,
8914 const struct devlink_param *params,
8915 size_t params_count)
8917 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8918 params, params_count,
8919 DEVLINK_CMD_PARAM_DEL);
8921 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8924 * devlink_params_publish - publish configuration parameters
8926 * @devlink: devlink
8928 * Publish previously registered configuration parameters.
8930 void devlink_params_publish(struct devlink *devlink)
8932 struct devlink_param_item *param_item;
8934 list_for_each_entry(param_item, &devlink->param_list, list) {
8935 if (param_item->published)
8936 continue;
8937 param_item->published = true;
8938 devlink_param_notify(devlink, 0, param_item,
8939 DEVLINK_CMD_PARAM_NEW);
8942 EXPORT_SYMBOL_GPL(devlink_params_publish);
8945 * devlink_params_unpublish - unpublish configuration parameters
8947 * @devlink: devlink
8949 * Unpublish previously registered configuration parameters.
8951 void devlink_params_unpublish(struct devlink *devlink)
8953 struct devlink_param_item *param_item;
8955 list_for_each_entry(param_item, &devlink->param_list, list) {
8956 if (!param_item->published)
8957 continue;
8958 param_item->published = false;
8959 devlink_param_notify(devlink, 0, param_item,
8960 DEVLINK_CMD_PARAM_DEL);
8963 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8966 * devlink_port_params_register - register port configuration parameters
8968 * @devlink_port: devlink port
8969 * @params: configuration parameters array
8970 * @params_count: number of parameters provided
8972 * Register the configuration parameters supported by the port.
8974 int devlink_port_params_register(struct devlink_port *devlink_port,
8975 const struct devlink_param *params,
8976 size_t params_count)
8978 return __devlink_params_register(devlink_port->devlink,
8979 devlink_port->index,
8980 &devlink_port->param_list, params,
8981 params_count,
8982 DEVLINK_CMD_PORT_PARAM_NEW,
8983 DEVLINK_CMD_PORT_PARAM_DEL);
8985 EXPORT_SYMBOL_GPL(devlink_port_params_register);
8988 * devlink_port_params_unregister - unregister port configuration
8989 * parameters
8991 * @devlink_port: devlink port
8992 * @params: configuration parameters array
8993 * @params_count: number of parameters provided
8995 void devlink_port_params_unregister(struct devlink_port *devlink_port,
8996 const struct devlink_param *params,
8997 size_t params_count)
8999 return __devlink_params_unregister(devlink_port->devlink,
9000 devlink_port->index,
9001 &devlink_port->param_list,
9002 params, params_count,
9003 DEVLINK_CMD_PORT_PARAM_DEL);
9005 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
9007 static int
9008 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
9009 union devlink_param_value *init_val)
9011 struct devlink_param_item *param_item;
9013 param_item = devlink_param_find_by_id(param_list, param_id);
9014 if (!param_item)
9015 return -EINVAL;
9017 if (!param_item->driverinit_value_valid ||
9018 !devlink_param_cmode_is_supported(param_item->param,
9019 DEVLINK_PARAM_CMODE_DRIVERINIT))
9020 return -EOPNOTSUPP;
9022 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
9023 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
9024 else
9025 *init_val = param_item->driverinit_value;
9027 return 0;
9030 static int
9031 __devlink_param_driverinit_value_set(struct devlink *devlink,
9032 unsigned int port_index,
9033 struct list_head *param_list, u32 param_id,
9034 union devlink_param_value init_val,
9035 enum devlink_command cmd)
9037 struct devlink_param_item *param_item;
9039 param_item = devlink_param_find_by_id(param_list, param_id);
9040 if (!param_item)
9041 return -EINVAL;
9043 if (!devlink_param_cmode_is_supported(param_item->param,
9044 DEVLINK_PARAM_CMODE_DRIVERINIT))
9045 return -EOPNOTSUPP;
9047 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
9048 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
9049 else
9050 param_item->driverinit_value = init_val;
9051 param_item->driverinit_value_valid = true;
9053 devlink_param_notify(devlink, port_index, param_item, cmd);
9054 return 0;
9058 * devlink_param_driverinit_value_get - get configuration parameter
9059 * value for driver initializing
9061 * @devlink: devlink
9062 * @param_id: parameter ID
9063 * @init_val: value of parameter in driverinit configuration mode
9065 * This function should be used by the driver to get driverinit
9066 * configuration for initialization after reload command.
9068 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
9069 union devlink_param_value *init_val)
9071 if (!devlink_reload_supported(devlink->ops))
9072 return -EOPNOTSUPP;
9074 return __devlink_param_driverinit_value_get(&devlink->param_list,
9075 param_id, init_val);
9077 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
9080 * devlink_param_driverinit_value_set - set value of configuration
9081 * parameter for driverinit
9082 * configuration mode
9084 * @devlink: devlink
9085 * @param_id: parameter ID
9086 * @init_val: value of parameter to set for driverinit configuration mode
9088 * This function should be used by the driver to set driverinit
9089 * configuration mode default value.
9091 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
9092 union devlink_param_value init_val)
9094 return __devlink_param_driverinit_value_set(devlink, 0,
9095 &devlink->param_list,
9096 param_id, init_val,
9097 DEVLINK_CMD_PARAM_NEW);
9099 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
9102 * devlink_port_param_driverinit_value_get - get configuration parameter
9103 * value for driver initializing
9105 * @devlink_port: devlink_port
9106 * @param_id: parameter ID
9107 * @init_val: value of parameter in driverinit configuration mode
9109 * This function should be used by the driver to get driverinit
9110 * configuration for initialization after reload command.
9112 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
9113 u32 param_id,
9114 union devlink_param_value *init_val)
9116 struct devlink *devlink = devlink_port->devlink;
9118 if (!devlink_reload_supported(devlink->ops))
9119 return -EOPNOTSUPP;
9121 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
9122 param_id, init_val);
9124 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
9127 * devlink_port_param_driverinit_value_set - set value of configuration
9128 * parameter for driverinit
9129 * configuration mode
9131 * @devlink_port: devlink_port
9132 * @param_id: parameter ID
9133 * @init_val: value of parameter to set for driverinit configuration mode
9135 * This function should be used by the driver to set driverinit
9136 * configuration mode default value.
9138 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
9139 u32 param_id,
9140 union devlink_param_value init_val)
9142 return __devlink_param_driverinit_value_set(devlink_port->devlink,
9143 devlink_port->index,
9144 &devlink_port->param_list,
9145 param_id, init_val,
9146 DEVLINK_CMD_PORT_PARAM_NEW);
9148 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
9151 * devlink_param_value_changed - notify devlink on a parameter's value
9152 * change. Should be called by the driver
9153 * right after the change.
9155 * @devlink: devlink
9156 * @param_id: parameter ID
9158 * This function should be used by the driver to notify devlink on value
9159 * change, excluding driverinit configuration mode.
9160 * For driverinit configuration mode driver should use the function
9162 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
9164 struct devlink_param_item *param_item;
9166 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
9167 WARN_ON(!param_item);
9169 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
9171 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
9174 * devlink_port_param_value_changed - notify devlink on a parameter's value
9175 * change. Should be called by the driver
9176 * right after the change.
9178 * @devlink_port: devlink_port
9179 * @param_id: parameter ID
9181 * This function should be used by the driver to notify devlink on value
9182 * change, excluding driverinit configuration mode.
9183 * For driverinit configuration mode driver should use the function
9184 * devlink_port_param_driverinit_value_set() instead.
9186 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
9187 u32 param_id)
9189 struct devlink_param_item *param_item;
9191 param_item = devlink_param_find_by_id(&devlink_port->param_list,
9192 param_id);
9193 WARN_ON(!param_item);
9195 devlink_param_notify(devlink_port->devlink, devlink_port->index,
9196 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
9198 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
9201 * devlink_param_value_str_fill - Safely fill-up the string preventing
9202 * from overflow of the preallocated buffer
9204 * @dst_val: destination devlink_param_value
9205 * @src: source buffer
9207 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
9208 const char *src)
9210 size_t len;
9212 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
9213 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
9215 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
9218 * devlink_region_create - create a new address region
9220 * @devlink: devlink
9221 * @ops: region operations and name
9222 * @region_max_snapshots: Maximum supported number of snapshots for region
9223 * @region_size: size of region
9225 struct devlink_region *
9226 devlink_region_create(struct devlink *devlink,
9227 const struct devlink_region_ops *ops,
9228 u32 region_max_snapshots, u64 region_size)
9230 struct devlink_region *region;
9231 int err = 0;
9233 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9234 return ERR_PTR(-EINVAL);
9236 mutex_lock(&devlink->lock);
9238 if (devlink_region_get_by_name(devlink, ops->name)) {
9239 err = -EEXIST;
9240 goto unlock;
9243 region = kzalloc(sizeof(*region), GFP_KERNEL);
9244 if (!region) {
9245 err = -ENOMEM;
9246 goto unlock;
9249 region->devlink = devlink;
9250 region->max_snapshots = region_max_snapshots;
9251 region->ops = ops;
9252 region->size = region_size;
9253 INIT_LIST_HEAD(&region->snapshot_list);
9254 list_add_tail(&region->list, &devlink->region_list);
9255 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9257 mutex_unlock(&devlink->lock);
9258 return region;
9260 unlock:
9261 mutex_unlock(&devlink->lock);
9262 return ERR_PTR(err);
9264 EXPORT_SYMBOL_GPL(devlink_region_create);
9267 * devlink_port_region_create - create a new address region for a port
9269 * @port: devlink port
9270 * @ops: region operations and name
9271 * @region_max_snapshots: Maximum supported number of snapshots for region
9272 * @region_size: size of region
9274 struct devlink_region *
9275 devlink_port_region_create(struct devlink_port *port,
9276 const struct devlink_port_region_ops *ops,
9277 u32 region_max_snapshots, u64 region_size)
9279 struct devlink *devlink = port->devlink;
9280 struct devlink_region *region;
9281 int err = 0;
9283 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9284 return ERR_PTR(-EINVAL);
9286 mutex_lock(&devlink->lock);
9288 if (devlink_port_region_get_by_name(port, ops->name)) {
9289 err = -EEXIST;
9290 goto unlock;
9293 region = kzalloc(sizeof(*region), GFP_KERNEL);
9294 if (!region) {
9295 err = -ENOMEM;
9296 goto unlock;
9299 region->devlink = devlink;
9300 region->port = port;
9301 region->max_snapshots = region_max_snapshots;
9302 region->port_ops = ops;
9303 region->size = region_size;
9304 INIT_LIST_HEAD(&region->snapshot_list);
9305 list_add_tail(&region->list, &port->region_list);
9306 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9308 mutex_unlock(&devlink->lock);
9309 return region;
9311 unlock:
9312 mutex_unlock(&devlink->lock);
9313 return ERR_PTR(err);
9315 EXPORT_SYMBOL_GPL(devlink_port_region_create);
9318 * devlink_region_destroy - destroy address region
9320 * @region: devlink region to destroy
9322 void devlink_region_destroy(struct devlink_region *region)
9324 struct devlink *devlink = region->devlink;
9325 struct devlink_snapshot *snapshot, *ts;
9327 mutex_lock(&devlink->lock);
9329 /* Free all snapshots of region */
9330 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
9331 devlink_region_snapshot_del(region, snapshot);
9333 list_del(&region->list);
9335 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9336 mutex_unlock(&devlink->lock);
9337 kfree(region);
9339 EXPORT_SYMBOL_GPL(devlink_region_destroy);
9342 * devlink_region_snapshot_id_get - get snapshot ID
9344 * This callback should be called when adding a new snapshot,
9345 * Driver should use the same id for multiple snapshots taken
9346 * on multiple regions at the same time/by the same trigger.
9348 * The caller of this function must use devlink_region_snapshot_id_put
9349 * when finished creating regions using this id.
9351 * Returns zero on success, or a negative error code on failure.
9353 * @devlink: devlink
9354 * @id: storage to return id
9356 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
9358 int err;
9360 mutex_lock(&devlink->lock);
9361 err = __devlink_region_snapshot_id_get(devlink, id);
9362 mutex_unlock(&devlink->lock);
9364 return err;
9366 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
9369 * devlink_region_snapshot_id_put - put snapshot ID reference
9371 * This should be called by a driver after finishing creating snapshots
9372 * with an id. Doing so ensures that the ID can later be released in the
9373 * event that all snapshots using it have been destroyed.
9375 * @devlink: devlink
9376 * @id: id to release reference on
9378 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
9380 mutex_lock(&devlink->lock);
9381 __devlink_snapshot_id_decrement(devlink, id);
9382 mutex_unlock(&devlink->lock);
9384 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
9387 * devlink_region_snapshot_create - create a new snapshot
9388 * This will add a new snapshot of a region. The snapshot
9389 * will be stored on the region struct and can be accessed
9390 * from devlink. This is useful for future analyses of snapshots.
9391 * Multiple snapshots can be created on a region.
9392 * The @snapshot_id should be obtained using the getter function.
9394 * @region: devlink region of the snapshot
9395 * @data: snapshot data
9396 * @snapshot_id: snapshot id to be created
9398 int devlink_region_snapshot_create(struct devlink_region *region,
9399 u8 *data, u32 snapshot_id)
9401 struct devlink *devlink = region->devlink;
9402 int err;
9404 mutex_lock(&devlink->lock);
9405 err = __devlink_region_snapshot_create(region, data, snapshot_id);
9406 mutex_unlock(&devlink->lock);
9408 return err;
9410 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
9412 #define DEVLINK_TRAP(_id, _type) \
9414 .type = DEVLINK_TRAP_TYPE_##_type, \
9415 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
9416 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
9419 static const struct devlink_trap devlink_trap_generic[] = {
9420 DEVLINK_TRAP(SMAC_MC, DROP),
9421 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
9422 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
9423 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
9424 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
9425 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
9426 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
9427 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
9428 DEVLINK_TRAP(TAIL_DROP, DROP),
9429 DEVLINK_TRAP(NON_IP_PACKET, DROP),
9430 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
9431 DEVLINK_TRAP(DIP_LB, DROP),
9432 DEVLINK_TRAP(SIP_MC, DROP),
9433 DEVLINK_TRAP(SIP_LB, DROP),
9434 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
9435 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
9436 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
9437 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
9438 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
9439 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
9440 DEVLINK_TRAP(RPF, EXCEPTION),
9441 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
9442 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
9443 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
9444 DEVLINK_TRAP(NON_ROUTABLE, DROP),
9445 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
9446 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
9447 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
9448 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
9449 DEVLINK_TRAP(STP, CONTROL),
9450 DEVLINK_TRAP(LACP, CONTROL),
9451 DEVLINK_TRAP(LLDP, CONTROL),
9452 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
9453 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
9454 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
9455 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
9456 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
9457 DEVLINK_TRAP(MLD_QUERY, CONTROL),
9458 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
9459 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
9460 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
9461 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
9462 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
9463 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
9464 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
9465 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
9466 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
9467 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
9468 DEVLINK_TRAP(IPV4_BFD, CONTROL),
9469 DEVLINK_TRAP(IPV6_BFD, CONTROL),
9470 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
9471 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
9472 DEVLINK_TRAP(IPV4_BGP, CONTROL),
9473 DEVLINK_TRAP(IPV6_BGP, CONTROL),
9474 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
9475 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
9476 DEVLINK_TRAP(IPV4_PIM, CONTROL),
9477 DEVLINK_TRAP(IPV6_PIM, CONTROL),
9478 DEVLINK_TRAP(UC_LB, CONTROL),
9479 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
9480 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
9481 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
9482 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
9483 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
9484 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
9485 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
9486 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
9487 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
9488 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
9489 DEVLINK_TRAP(PTP_EVENT, CONTROL),
9490 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
9491 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
9492 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
9493 DEVLINK_TRAP(EARLY_DROP, DROP),
9494 DEVLINK_TRAP(VXLAN_PARSING, DROP),
9495 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
9496 DEVLINK_TRAP(VLAN_PARSING, DROP),
9497 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
9498 DEVLINK_TRAP(MPLS_PARSING, DROP),
9499 DEVLINK_TRAP(ARP_PARSING, DROP),
9500 DEVLINK_TRAP(IP_1_PARSING, DROP),
9501 DEVLINK_TRAP(IP_N_PARSING, DROP),
9502 DEVLINK_TRAP(GRE_PARSING, DROP),
9503 DEVLINK_TRAP(UDP_PARSING, DROP),
9504 DEVLINK_TRAP(TCP_PARSING, DROP),
9505 DEVLINK_TRAP(IPSEC_PARSING, DROP),
9506 DEVLINK_TRAP(SCTP_PARSING, DROP),
9507 DEVLINK_TRAP(DCCP_PARSING, DROP),
9508 DEVLINK_TRAP(GTP_PARSING, DROP),
9509 DEVLINK_TRAP(ESP_PARSING, DROP),
9510 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
9513 #define DEVLINK_TRAP_GROUP(_id) \
9515 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
9516 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
9519 static const struct devlink_trap_group devlink_trap_group_generic[] = {
9520 DEVLINK_TRAP_GROUP(L2_DROPS),
9521 DEVLINK_TRAP_GROUP(L3_DROPS),
9522 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
9523 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
9524 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
9525 DEVLINK_TRAP_GROUP(ACL_DROPS),
9526 DEVLINK_TRAP_GROUP(STP),
9527 DEVLINK_TRAP_GROUP(LACP),
9528 DEVLINK_TRAP_GROUP(LLDP),
9529 DEVLINK_TRAP_GROUP(MC_SNOOPING),
9530 DEVLINK_TRAP_GROUP(DHCP),
9531 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
9532 DEVLINK_TRAP_GROUP(BFD),
9533 DEVLINK_TRAP_GROUP(OSPF),
9534 DEVLINK_TRAP_GROUP(BGP),
9535 DEVLINK_TRAP_GROUP(VRRP),
9536 DEVLINK_TRAP_GROUP(PIM),
9537 DEVLINK_TRAP_GROUP(UC_LB),
9538 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
9539 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
9540 DEVLINK_TRAP_GROUP(IPV6),
9541 DEVLINK_TRAP_GROUP(PTP_EVENT),
9542 DEVLINK_TRAP_GROUP(PTP_GENERAL),
9543 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
9544 DEVLINK_TRAP_GROUP(ACL_TRAP),
9545 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
9548 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
9550 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
9551 return -EINVAL;
9553 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
9554 return -EINVAL;
9556 if (trap->type != devlink_trap_generic[trap->id].type)
9557 return -EINVAL;
9559 return 0;
9562 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
9564 int i;
9566 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
9567 return -EINVAL;
9569 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
9570 if (!strcmp(trap->name, devlink_trap_generic[i].name))
9571 return -EEXIST;
9574 return 0;
9577 static int devlink_trap_verify(const struct devlink_trap *trap)
9579 if (!trap || !trap->name)
9580 return -EINVAL;
9582 if (trap->generic)
9583 return devlink_trap_generic_verify(trap);
9584 else
9585 return devlink_trap_driver_verify(trap);
9588 static int
9589 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
9591 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9592 return -EINVAL;
9594 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
9595 return -EINVAL;
9597 return 0;
9600 static int
9601 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
9603 int i;
9605 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9606 return -EINVAL;
9608 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
9609 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
9610 return -EEXIST;
9613 return 0;
9616 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
9618 if (group->generic)
9619 return devlink_trap_group_generic_verify(group);
9620 else
9621 return devlink_trap_group_driver_verify(group);
9624 static void
9625 devlink_trap_group_notify(struct devlink *devlink,
9626 const struct devlink_trap_group_item *group_item,
9627 enum devlink_command cmd)
9629 struct sk_buff *msg;
9630 int err;
9632 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
9633 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
9635 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9636 if (!msg)
9637 return;
9639 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
9641 if (err) {
9642 nlmsg_free(msg);
9643 return;
9646 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9647 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9650 static int
9651 devlink_trap_item_group_link(struct devlink *devlink,
9652 struct devlink_trap_item *trap_item)
9654 u16 group_id = trap_item->trap->init_group_id;
9655 struct devlink_trap_group_item *group_item;
9657 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
9658 if (WARN_ON_ONCE(!group_item))
9659 return -EINVAL;
9661 trap_item->group_item = group_item;
9663 return 0;
9666 static void devlink_trap_notify(struct devlink *devlink,
9667 const struct devlink_trap_item *trap_item,
9668 enum devlink_command cmd)
9670 struct sk_buff *msg;
9671 int err;
9673 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
9674 cmd != DEVLINK_CMD_TRAP_DEL);
9676 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9677 if (!msg)
9678 return;
9680 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
9681 if (err) {
9682 nlmsg_free(msg);
9683 return;
9686 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9687 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9690 static int
9691 devlink_trap_register(struct devlink *devlink,
9692 const struct devlink_trap *trap, void *priv)
9694 struct devlink_trap_item *trap_item;
9695 int err;
9697 if (devlink_trap_item_lookup(devlink, trap->name))
9698 return -EEXIST;
9700 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
9701 if (!trap_item)
9702 return -ENOMEM;
9704 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9705 if (!trap_item->stats) {
9706 err = -ENOMEM;
9707 goto err_stats_alloc;
9710 trap_item->trap = trap;
9711 trap_item->action = trap->init_action;
9712 trap_item->priv = priv;
9714 err = devlink_trap_item_group_link(devlink, trap_item);
9715 if (err)
9716 goto err_group_link;
9718 err = devlink->ops->trap_init(devlink, trap, trap_item);
9719 if (err)
9720 goto err_trap_init;
9722 list_add_tail(&trap_item->list, &devlink->trap_list);
9723 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9725 return 0;
9727 err_trap_init:
9728 err_group_link:
9729 free_percpu(trap_item->stats);
9730 err_stats_alloc:
9731 kfree(trap_item);
9732 return err;
9735 static void devlink_trap_unregister(struct devlink *devlink,
9736 const struct devlink_trap *trap)
9738 struct devlink_trap_item *trap_item;
9740 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9741 if (WARN_ON_ONCE(!trap_item))
9742 return;
9744 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9745 list_del(&trap_item->list);
9746 if (devlink->ops->trap_fini)
9747 devlink->ops->trap_fini(devlink, trap, trap_item);
9748 free_percpu(trap_item->stats);
9749 kfree(trap_item);
9752 static void devlink_trap_disable(struct devlink *devlink,
9753 const struct devlink_trap *trap)
9755 struct devlink_trap_item *trap_item;
9757 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9758 if (WARN_ON_ONCE(!trap_item))
9759 return;
9761 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
9762 NULL);
9763 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9767 * devlink_traps_register - Register packet traps with devlink.
9768 * @devlink: devlink.
9769 * @traps: Packet traps.
9770 * @traps_count: Count of provided packet traps.
9771 * @priv: Driver private information.
9773 * Return: Non-zero value on failure.
9775 int devlink_traps_register(struct devlink *devlink,
9776 const struct devlink_trap *traps,
9777 size_t traps_count, void *priv)
9779 int i, err;
9781 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9782 return -EINVAL;
9784 mutex_lock(&devlink->lock);
9785 for (i = 0; i < traps_count; i++) {
9786 const struct devlink_trap *trap = &traps[i];
9788 err = devlink_trap_verify(trap);
9789 if (err)
9790 goto err_trap_verify;
9792 err = devlink_trap_register(devlink, trap, priv);
9793 if (err)
9794 goto err_trap_register;
9796 mutex_unlock(&devlink->lock);
9798 return 0;
9800 err_trap_register:
9801 err_trap_verify:
9802 for (i--; i >= 0; i--)
9803 devlink_trap_unregister(devlink, &traps[i]);
9804 mutex_unlock(&devlink->lock);
9805 return err;
9807 EXPORT_SYMBOL_GPL(devlink_traps_register);
9810 * devlink_traps_unregister - Unregister packet traps from devlink.
9811 * @devlink: devlink.
9812 * @traps: Packet traps.
9813 * @traps_count: Count of provided packet traps.
9815 void devlink_traps_unregister(struct devlink *devlink,
9816 const struct devlink_trap *traps,
9817 size_t traps_count)
9819 int i;
9821 mutex_lock(&devlink->lock);
9822 /* Make sure we do not have any packets in-flight while unregistering
9823 * traps by disabling all of them and waiting for a grace period.
9825 for (i = traps_count - 1; i >= 0; i--)
9826 devlink_trap_disable(devlink, &traps[i]);
9827 synchronize_rcu();
9828 for (i = traps_count - 1; i >= 0; i--)
9829 devlink_trap_unregister(devlink, &traps[i]);
9830 mutex_unlock(&devlink->lock);
9832 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9834 static void
9835 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9836 size_t skb_len)
9838 struct devlink_stats *stats;
9840 stats = this_cpu_ptr(trap_stats);
9841 u64_stats_update_begin(&stats->syncp);
9842 stats->rx_bytes += skb_len;
9843 stats->rx_packets++;
9844 u64_stats_update_end(&stats->syncp);
9847 static void
9848 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9849 const struct devlink_trap_item *trap_item,
9850 struct devlink_port *in_devlink_port,
9851 const struct flow_action_cookie *fa_cookie)
9853 metadata->trap_name = trap_item->trap->name;
9854 metadata->trap_group_name = trap_item->group_item->group->name;
9855 metadata->fa_cookie = fa_cookie;
9856 metadata->trap_type = trap_item->trap->type;
9858 spin_lock(&in_devlink_port->type_lock);
9859 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9860 metadata->input_dev = in_devlink_port->type_dev;
9861 spin_unlock(&in_devlink_port->type_lock);
9865 * devlink_trap_report - Report trapped packet to drop monitor.
9866 * @devlink: devlink.
9867 * @skb: Trapped packet.
9868 * @trap_ctx: Trap context.
9869 * @in_devlink_port: Input devlink port.
9870 * @fa_cookie: Flow action cookie. Could be NULL.
9872 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9873 void *trap_ctx, struct devlink_port *in_devlink_port,
9874 const struct flow_action_cookie *fa_cookie)
9877 struct devlink_trap_item *trap_item = trap_ctx;
9879 devlink_trap_stats_update(trap_item->stats, skb->len);
9880 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9882 if (trace_devlink_trap_report_enabled()) {
9883 struct devlink_trap_metadata metadata = {};
9885 devlink_trap_report_metadata_set(&metadata, trap_item,
9886 in_devlink_port, fa_cookie);
9887 trace_devlink_trap_report(devlink, skb, &metadata);
9890 EXPORT_SYMBOL_GPL(devlink_trap_report);
9893 * devlink_trap_ctx_priv - Trap context to driver private information.
9894 * @trap_ctx: Trap context.
9896 * Return: Driver private information passed during registration.
9898 void *devlink_trap_ctx_priv(void *trap_ctx)
9900 struct devlink_trap_item *trap_item = trap_ctx;
9902 return trap_item->priv;
9904 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9906 static int
9907 devlink_trap_group_item_policer_link(struct devlink *devlink,
9908 struct devlink_trap_group_item *group_item)
9910 u32 policer_id = group_item->group->init_policer_id;
9911 struct devlink_trap_policer_item *policer_item;
9913 if (policer_id == 0)
9914 return 0;
9916 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9917 if (WARN_ON_ONCE(!policer_item))
9918 return -EINVAL;
9920 group_item->policer_item = policer_item;
9922 return 0;
9925 static int
9926 devlink_trap_group_register(struct devlink *devlink,
9927 const struct devlink_trap_group *group)
9929 struct devlink_trap_group_item *group_item;
9930 int err;
9932 if (devlink_trap_group_item_lookup(devlink, group->name))
9933 return -EEXIST;
9935 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9936 if (!group_item)
9937 return -ENOMEM;
9939 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9940 if (!group_item->stats) {
9941 err = -ENOMEM;
9942 goto err_stats_alloc;
9945 group_item->group = group;
9947 err = devlink_trap_group_item_policer_link(devlink, group_item);
9948 if (err)
9949 goto err_policer_link;
9951 if (devlink->ops->trap_group_init) {
9952 err = devlink->ops->trap_group_init(devlink, group);
9953 if (err)
9954 goto err_group_init;
9957 list_add_tail(&group_item->list, &devlink->trap_group_list);
9958 devlink_trap_group_notify(devlink, group_item,
9959 DEVLINK_CMD_TRAP_GROUP_NEW);
9961 return 0;
9963 err_group_init:
9964 err_policer_link:
9965 free_percpu(group_item->stats);
9966 err_stats_alloc:
9967 kfree(group_item);
9968 return err;
9971 static void
9972 devlink_trap_group_unregister(struct devlink *devlink,
9973 const struct devlink_trap_group *group)
9975 struct devlink_trap_group_item *group_item;
9977 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9978 if (WARN_ON_ONCE(!group_item))
9979 return;
9981 devlink_trap_group_notify(devlink, group_item,
9982 DEVLINK_CMD_TRAP_GROUP_DEL);
9983 list_del(&group_item->list);
9984 free_percpu(group_item->stats);
9985 kfree(group_item);
9989 * devlink_trap_groups_register - Register packet trap groups with devlink.
9990 * @devlink: devlink.
9991 * @groups: Packet trap groups.
9992 * @groups_count: Count of provided packet trap groups.
9994 * Return: Non-zero value on failure.
9996 int devlink_trap_groups_register(struct devlink *devlink,
9997 const struct devlink_trap_group *groups,
9998 size_t groups_count)
10000 int i, err;
10002 mutex_lock(&devlink->lock);
10003 for (i = 0; i < groups_count; i++) {
10004 const struct devlink_trap_group *group = &groups[i];
10006 err = devlink_trap_group_verify(group);
10007 if (err)
10008 goto err_trap_group_verify;
10010 err = devlink_trap_group_register(devlink, group);
10011 if (err)
10012 goto err_trap_group_register;
10014 mutex_unlock(&devlink->lock);
10016 return 0;
10018 err_trap_group_register:
10019 err_trap_group_verify:
10020 for (i--; i >= 0; i--)
10021 devlink_trap_group_unregister(devlink, &groups[i]);
10022 mutex_unlock(&devlink->lock);
10023 return err;
10025 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
10028 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
10029 * @devlink: devlink.
10030 * @groups: Packet trap groups.
10031 * @groups_count: Count of provided packet trap groups.
10033 void devlink_trap_groups_unregister(struct devlink *devlink,
10034 const struct devlink_trap_group *groups,
10035 size_t groups_count)
10037 int i;
10039 mutex_lock(&devlink->lock);
10040 for (i = groups_count - 1; i >= 0; i--)
10041 devlink_trap_group_unregister(devlink, &groups[i]);
10042 mutex_unlock(&devlink->lock);
10044 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
10046 static void
10047 devlink_trap_policer_notify(struct devlink *devlink,
10048 const struct devlink_trap_policer_item *policer_item,
10049 enum devlink_command cmd)
10051 struct sk_buff *msg;
10052 int err;
10054 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
10055 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
10057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10058 if (!msg)
10059 return;
10061 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
10062 0, 0);
10063 if (err) {
10064 nlmsg_free(msg);
10065 return;
10068 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10069 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10072 static int
10073 devlink_trap_policer_register(struct devlink *devlink,
10074 const struct devlink_trap_policer *policer)
10076 struct devlink_trap_policer_item *policer_item;
10077 int err;
10079 if (devlink_trap_policer_item_lookup(devlink, policer->id))
10080 return -EEXIST;
10082 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
10083 if (!policer_item)
10084 return -ENOMEM;
10086 policer_item->policer = policer;
10087 policer_item->rate = policer->init_rate;
10088 policer_item->burst = policer->init_burst;
10090 if (devlink->ops->trap_policer_init) {
10091 err = devlink->ops->trap_policer_init(devlink, policer);
10092 if (err)
10093 goto err_policer_init;
10096 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
10097 devlink_trap_policer_notify(devlink, policer_item,
10098 DEVLINK_CMD_TRAP_POLICER_NEW);
10100 return 0;
10102 err_policer_init:
10103 kfree(policer_item);
10104 return err;
10107 static void
10108 devlink_trap_policer_unregister(struct devlink *devlink,
10109 const struct devlink_trap_policer *policer)
10111 struct devlink_trap_policer_item *policer_item;
10113 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
10114 if (WARN_ON_ONCE(!policer_item))
10115 return;
10117 devlink_trap_policer_notify(devlink, policer_item,
10118 DEVLINK_CMD_TRAP_POLICER_DEL);
10119 list_del(&policer_item->list);
10120 if (devlink->ops->trap_policer_fini)
10121 devlink->ops->trap_policer_fini(devlink, policer);
10122 kfree(policer_item);
10126 * devlink_trap_policers_register - Register packet trap policers with devlink.
10127 * @devlink: devlink.
10128 * @policers: Packet trap policers.
10129 * @policers_count: Count of provided packet trap policers.
10131 * Return: Non-zero value on failure.
10134 devlink_trap_policers_register(struct devlink *devlink,
10135 const struct devlink_trap_policer *policers,
10136 size_t policers_count)
10138 int i, err;
10140 mutex_lock(&devlink->lock);
10141 for (i = 0; i < policers_count; i++) {
10142 const struct devlink_trap_policer *policer = &policers[i];
10144 if (WARN_ON(policer->id == 0 ||
10145 policer->max_rate < policer->min_rate ||
10146 policer->max_burst < policer->min_burst)) {
10147 err = -EINVAL;
10148 goto err_trap_policer_verify;
10151 err = devlink_trap_policer_register(devlink, policer);
10152 if (err)
10153 goto err_trap_policer_register;
10155 mutex_unlock(&devlink->lock);
10157 return 0;
10159 err_trap_policer_register:
10160 err_trap_policer_verify:
10161 for (i--; i >= 0; i--)
10162 devlink_trap_policer_unregister(devlink, &policers[i]);
10163 mutex_unlock(&devlink->lock);
10164 return err;
10166 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
10169 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
10170 * @devlink: devlink.
10171 * @policers: Packet trap policers.
10172 * @policers_count: Count of provided packet trap policers.
10174 void
10175 devlink_trap_policers_unregister(struct devlink *devlink,
10176 const struct devlink_trap_policer *policers,
10177 size_t policers_count)
10179 int i;
10181 mutex_lock(&devlink->lock);
10182 for (i = policers_count - 1; i >= 0; i--)
10183 devlink_trap_policer_unregister(devlink, &policers[i]);
10184 mutex_unlock(&devlink->lock);
10186 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
10188 static void __devlink_compat_running_version(struct devlink *devlink,
10189 char *buf, size_t len)
10191 const struct nlattr *nlattr;
10192 struct devlink_info_req req;
10193 struct sk_buff *msg;
10194 int rem, err;
10196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10197 if (!msg)
10198 return;
10200 req.msg = msg;
10201 err = devlink->ops->info_get(devlink, &req, NULL);
10202 if (err)
10203 goto free_msg;
10205 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
10206 const struct nlattr *kv;
10207 int rem_kv;
10209 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
10210 continue;
10212 nla_for_each_nested(kv, nlattr, rem_kv) {
10213 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
10214 continue;
10216 strlcat(buf, nla_data(kv), len);
10217 strlcat(buf, " ", len);
10220 free_msg:
10221 nlmsg_free(msg);
10224 void devlink_compat_running_version(struct net_device *dev,
10225 char *buf, size_t len)
10227 struct devlink *devlink;
10229 dev_hold(dev);
10230 rtnl_unlock();
10232 devlink = netdev_to_devlink(dev);
10233 if (!devlink || !devlink->ops->info_get)
10234 goto out;
10236 mutex_lock(&devlink->lock);
10237 __devlink_compat_running_version(devlink, buf, len);
10238 mutex_unlock(&devlink->lock);
10240 out:
10241 rtnl_lock();
10242 dev_put(dev);
10245 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
10247 struct devlink_flash_update_params params = {};
10248 struct devlink *devlink;
10249 int ret;
10251 dev_hold(dev);
10252 rtnl_unlock();
10254 devlink = netdev_to_devlink(dev);
10255 if (!devlink || !devlink->ops->flash_update) {
10256 ret = -EOPNOTSUPP;
10257 goto out;
10260 ret = request_firmware(&params.fw, file_name, devlink->dev);
10261 if (ret)
10262 goto out;
10264 mutex_lock(&devlink->lock);
10265 devlink_flash_update_begin_notify(devlink);
10266 ret = devlink->ops->flash_update(devlink, &params, NULL);
10267 devlink_flash_update_end_notify(devlink);
10268 mutex_unlock(&devlink->lock);
10270 release_firmware(params.fw);
10272 out:
10273 rtnl_lock();
10274 dev_put(dev);
10276 return ret;
10279 int devlink_compat_phys_port_name_get(struct net_device *dev,
10280 char *name, size_t len)
10282 struct devlink_port *devlink_port;
10284 /* RTNL mutex is held here which ensures that devlink_port
10285 * instance cannot disappear in the middle. No need to take
10286 * any devlink lock as only permanent values are accessed.
10288 ASSERT_RTNL();
10290 devlink_port = netdev_to_devlink_port(dev);
10291 if (!devlink_port)
10292 return -EOPNOTSUPP;
10294 return __devlink_port_phys_port_name_get(devlink_port, name, len);
10297 int devlink_compat_switch_id_get(struct net_device *dev,
10298 struct netdev_phys_item_id *ppid)
10300 struct devlink_port *devlink_port;
10302 /* Caller must hold RTNL mutex or reference to dev, which ensures that
10303 * devlink_port instance cannot disappear in the middle. No need to take
10304 * any devlink lock as only permanent values are accessed.
10306 devlink_port = netdev_to_devlink_port(dev);
10307 if (!devlink_port || !devlink_port->switch_port)
10308 return -EOPNOTSUPP;
10310 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
10312 return 0;
10315 static void __net_exit devlink_pernet_pre_exit(struct net *net)
10317 struct devlink *devlink;
10318 u32 actions_performed;
10319 int err;
10321 /* In case network namespace is getting destroyed, reload
10322 * all devlink instances from this namespace into init_net.
10324 mutex_lock(&devlink_mutex);
10325 list_for_each_entry(devlink, &devlink_list, list) {
10326 if (net_eq(devlink_net(devlink), net)) {
10327 if (WARN_ON(!devlink_reload_supported(devlink->ops)))
10328 continue;
10329 err = devlink_reload(devlink, &init_net,
10330 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
10331 DEVLINK_RELOAD_LIMIT_UNSPEC,
10332 &actions_performed, NULL);
10333 if (err && err != -EOPNOTSUPP)
10334 pr_warn("Failed to reload devlink instance into init_net\n");
10337 mutex_unlock(&devlink_mutex);
10340 static struct pernet_operations devlink_pernet_ops __net_initdata = {
10341 .pre_exit = devlink_pernet_pre_exit,
10344 static int __init devlink_init(void)
10346 int err;
10348 err = genl_register_family(&devlink_nl_family);
10349 if (err)
10350 goto out;
10351 err = register_pernet_subsys(&devlink_pernet_ops);
10353 out:
10354 WARN_ON(err);
10355 return err;
10358 subsys_initcall(devlink_init);