1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
10 #include <linux/etherdevice.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/gfp.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/refcount.h>
21 #include <linux/workqueue.h>
22 #include <linux/u64_stats_sync.h>
23 #include <linux/timekeeping.h>
24 #include <rdma/ib_verbs.h>
25 #include <net/netlink.h>
26 #include <net/genetlink.h>
27 #include <net/rtnetlink.h>
28 #include <net/net_namespace.h>
30 #include <net/devlink.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devlink.h>
34 #include "devl_internal.h"
36 struct devlink_linecard {
37 struct list_head list;
38 struct devlink *devlink;
40 const struct devlink_linecard_ops *ops;
42 enum devlink_linecard_state state;
43 struct mutex state_lock; /* Protects state */
45 struct devlink_linecard_type *types;
46 unsigned int types_count;
47 struct devlink *nested_devlink;
51 * struct devlink_resource - devlink resource
52 * @name: name of the resource
53 * @id: id, per devlink instance
54 * @size: size of the resource
55 * @size_new: updated size of the resource, reload is needed
56 * @size_valid: valid in case the total size of the resource is valid
57 * including its children
58 * @parent: parent resource
59 * @size_params: size parameters
61 * @resource_list: list of child resources
62 * @occ_get: occupancy getter callback
63 * @occ_get_priv: occupancy getter callback priv
65 struct devlink_resource {
71 struct devlink_resource *parent;
72 struct devlink_resource_size_params size_params;
73 struct list_head list;
74 struct list_head resource_list;
75 devlink_resource_occ_get_t *occ_get;
79 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
81 .name = "destination mac",
82 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
87 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
89 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
90 .fields = devlink_dpipe_fields_ethernet,
91 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
94 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
96 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
98 .name = "destination ip",
99 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
104 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
106 .id = DEVLINK_DPIPE_HEADER_IPV4,
107 .fields = devlink_dpipe_fields_ipv4,
108 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
111 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
113 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
115 .name = "destination ip",
116 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
121 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
123 .id = DEVLINK_DPIPE_HEADER_IPV6,
124 .fields = devlink_dpipe_fields_ipv6,
125 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
128 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
130 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
131 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
132 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
134 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
135 (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
137 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
138 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
139 [DEVLINK_PORT_FN_ATTR_STATE] =
140 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
141 DEVLINK_PORT_FN_STATE_ACTIVE),
142 [DEVLINK_PORT_FN_ATTR_CAPS] =
143 NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
146 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \
147 WARN_ON_ONCE(!(devlink_port)->registered)
148 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \
149 WARN_ON_ONCE((devlink_port)->registered)
150 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \
151 WARN_ON_ONCE(!(devlink_port)->initialized)
153 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
154 unsigned int port_index)
156 return xa_load(&devlink->ports, port_index);
159 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
160 struct nlattr **attrs)
162 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
163 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
164 struct devlink_port *devlink_port;
166 devlink_port = devlink_port_get_by_index(devlink, port_index);
168 return ERR_PTR(-ENODEV);
171 return ERR_PTR(-EINVAL);
174 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
175 struct genl_info *info)
177 return devlink_port_get_from_attrs(devlink, info->attrs);
181 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
183 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
187 devlink_rate_is_node(struct devlink_rate *devlink_rate)
189 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
192 static struct devlink_rate *
193 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
195 struct devlink_rate *devlink_rate;
196 struct devlink_port *devlink_port;
198 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
199 if (IS_ERR(devlink_port))
200 return ERR_CAST(devlink_port);
201 devlink_rate = devlink_port->devlink_rate;
202 return devlink_rate ?: ERR_PTR(-ENODEV);
205 static struct devlink_rate *
206 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
208 static struct devlink_rate *devlink_rate;
210 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
211 if (devlink_rate_is_node(devlink_rate) &&
212 !strcmp(node_name, devlink_rate->name))
215 return ERR_PTR(-ENODEV);
218 static struct devlink_rate *
219 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
221 const char *rate_node_name;
224 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
225 return ERR_PTR(-EINVAL);
226 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
227 len = strlen(rate_node_name);
228 /* Name cannot be empty or decimal number */
229 if (!len || strspn(rate_node_name, "0123456789") == len)
230 return ERR_PTR(-EINVAL);
232 return devlink_rate_node_get_by_name(devlink, rate_node_name);
235 struct devlink_rate *
236 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
238 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
241 struct devlink_rate *
242 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
244 struct nlattr **attrs = info->attrs;
246 if (attrs[DEVLINK_ATTR_PORT_INDEX])
247 return devlink_rate_leaf_get_from_info(devlink, info);
248 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
249 return devlink_rate_node_get_from_info(devlink, info);
251 return ERR_PTR(-EINVAL);
254 static struct devlink_linecard *
255 devlink_linecard_get_by_index(struct devlink *devlink,
256 unsigned int linecard_index)
258 struct devlink_linecard *devlink_linecard;
260 list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
261 if (devlink_linecard->index == linecard_index)
262 return devlink_linecard;
267 static bool devlink_linecard_index_exists(struct devlink *devlink,
268 unsigned int linecard_index)
270 return devlink_linecard_get_by_index(devlink, linecard_index);
273 static struct devlink_linecard *
274 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
276 if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
277 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
278 struct devlink_linecard *linecard;
280 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
282 return ERR_PTR(-ENODEV);
285 return ERR_PTR(-EINVAL);
288 struct devlink_linecard *
289 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
291 return devlink_linecard_get_from_attrs(devlink, info->attrs);
295 struct list_head list;
298 u16 ingress_pools_count;
299 u16 egress_pools_count;
300 u16 ingress_tc_count;
304 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
306 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
309 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
310 unsigned int sb_index)
312 struct devlink_sb *devlink_sb;
314 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
315 if (devlink_sb->index == sb_index)
321 static bool devlink_sb_index_exists(struct devlink *devlink,
322 unsigned int sb_index)
324 return devlink_sb_get_by_index(devlink, sb_index);
327 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
328 struct nlattr **attrs)
330 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
331 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
332 struct devlink_sb *devlink_sb;
334 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
336 return ERR_PTR(-ENODEV);
339 return ERR_PTR(-EINVAL);
342 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
343 struct genl_info *info)
345 return devlink_sb_get_from_attrs(devlink, info->attrs);
348 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
349 struct nlattr **attrs,
354 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
357 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
358 if (val >= devlink_sb_pool_count(devlink_sb))
364 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
365 struct genl_info *info,
368 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
373 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
374 enum devlink_sb_pool_type *p_pool_type)
378 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
381 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
382 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
383 val != DEVLINK_SB_POOL_TYPE_EGRESS)
390 devlink_sb_pool_type_get_from_info(struct genl_info *info,
391 enum devlink_sb_pool_type *p_pool_type)
393 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
397 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
398 enum devlink_sb_threshold_type *p_th_type)
402 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
405 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
406 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
407 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
414 devlink_sb_th_type_get_from_info(struct genl_info *info,
415 enum devlink_sb_threshold_type *p_th_type)
417 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
421 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
422 struct nlattr **attrs,
423 enum devlink_sb_pool_type pool_type,
428 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
431 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
432 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
433 val >= devlink_sb->ingress_tc_count)
435 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
436 val >= devlink_sb->egress_tc_count)
442 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
443 u32 cap, bool is_enable)
445 caps->selector |= cap;
450 static int devlink_port_fn_roce_fill(const struct devlink_ops *ops,
451 struct devlink_port *devlink_port,
452 struct nla_bitfield32 *caps,
453 struct netlink_ext_ack *extack)
458 if (!ops->port_fn_roce_get)
461 err = ops->port_fn_roce_get(devlink_port, &is_enable, extack);
463 if (err == -EOPNOTSUPP)
468 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
472 static int devlink_port_fn_migratable_fill(const struct devlink_ops *ops,
473 struct devlink_port *devlink_port,
474 struct nla_bitfield32 *caps,
475 struct netlink_ext_ack *extack)
480 if (!ops->port_fn_migratable_get ||
481 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
484 err = ops->port_fn_migratable_get(devlink_port, &is_enable, extack);
486 if (err == -EOPNOTSUPP)
491 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
495 static int devlink_port_fn_caps_fill(const struct devlink_ops *ops,
496 struct devlink_port *devlink_port,
498 struct netlink_ext_ack *extack,
501 struct nla_bitfield32 caps = {};
504 err = devlink_port_fn_roce_fill(ops, devlink_port, &caps, extack);
508 err = devlink_port_fn_migratable_fill(ops, devlink_port, &caps, extack);
514 err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
524 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
525 struct genl_info *info,
526 enum devlink_sb_pool_type pool_type,
529 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
530 pool_type, p_tc_index);
533 struct devlink_region {
534 struct devlink *devlink;
535 struct devlink_port *port;
536 struct list_head list;
538 const struct devlink_region_ops *ops;
539 const struct devlink_port_region_ops *port_ops;
541 struct mutex snapshot_lock; /* protects snapshot_list,
542 * max_snapshots and cur_snapshots
545 struct list_head snapshot_list;
551 struct devlink_snapshot {
552 struct list_head list;
553 struct devlink_region *region;
558 static struct devlink_region *
559 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
561 struct devlink_region *region;
563 list_for_each_entry(region, &devlink->region_list, list)
564 if (!strcmp(region->ops->name, region_name))
570 static struct devlink_region *
571 devlink_port_region_get_by_name(struct devlink_port *port,
572 const char *region_name)
574 struct devlink_region *region;
576 list_for_each_entry(region, &port->region_list, list)
577 if (!strcmp(region->ops->name, region_name))
583 static struct devlink_snapshot *
584 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
586 struct devlink_snapshot *snapshot;
588 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
589 if (snapshot->id == id)
595 static int devlink_nl_put_nested_handle(struct sk_buff *msg, struct devlink *devlink)
597 struct nlattr *nested_attr;
599 nested_attr = nla_nest_start(msg, DEVLINK_ATTR_NESTED_DEVLINK);
602 if (devlink_nl_put_handle(msg, devlink))
603 goto nla_put_failure;
605 nla_nest_end(msg, nested_attr);
609 nla_nest_cancel(msg, nested_attr);
613 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
615 if (devlink_nl_put_handle(msg, devlink_port->devlink))
617 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
622 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
624 struct devlink *devlink = devlink_port->devlink;
626 return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
627 + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
628 + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
631 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
632 struct devlink_port *devlink_port)
634 struct devlink_port_attrs *attrs = &devlink_port->attrs;
636 if (!devlink_port->attrs_set)
639 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
642 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
644 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
646 switch (devlink_port->attrs.flavour) {
647 case DEVLINK_PORT_FLAVOUR_PCI_PF:
648 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
649 attrs->pci_pf.controller) ||
650 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
652 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
655 case DEVLINK_PORT_FLAVOUR_PCI_VF:
656 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
657 attrs->pci_vf.controller) ||
658 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
659 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
661 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
664 case DEVLINK_PORT_FLAVOUR_PCI_SF:
665 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
666 attrs->pci_sf.controller) ||
667 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
669 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
673 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
674 case DEVLINK_PORT_FLAVOUR_CPU:
675 case DEVLINK_PORT_FLAVOUR_DSA:
676 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
677 attrs->phys.port_number))
681 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
682 attrs->phys.port_number))
684 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
685 attrs->phys.split_subport_number))
694 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
695 struct devlink_port *port,
697 struct netlink_ext_ack *extack,
700 u8 hw_addr[MAX_ADDR_LEN];
704 if (!ops->port_function_hw_addr_get)
707 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
710 if (err == -EOPNOTSUPP)
714 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
721 static int devlink_nl_rate_fill(struct sk_buff *msg,
722 struct devlink_rate *devlink_rate,
723 enum devlink_command cmd, u32 portid, u32 seq,
724 int flags, struct netlink_ext_ack *extack)
726 struct devlink *devlink = devlink_rate->devlink;
729 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
733 if (devlink_nl_put_handle(msg, devlink))
734 goto nla_put_failure;
736 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
737 goto nla_put_failure;
739 if (devlink_rate_is_leaf(devlink_rate)) {
740 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
741 devlink_rate->devlink_port->index))
742 goto nla_put_failure;
743 } else if (devlink_rate_is_node(devlink_rate)) {
744 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
746 goto nla_put_failure;
749 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
750 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
751 goto nla_put_failure;
753 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
754 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
755 goto nla_put_failure;
757 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_PRIORITY,
758 devlink_rate->tx_priority))
759 goto nla_put_failure;
761 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_WEIGHT,
762 devlink_rate->tx_weight))
763 goto nla_put_failure;
765 if (devlink_rate->parent)
766 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
767 devlink_rate->parent->name))
768 goto nla_put_failure;
770 genlmsg_end(msg, hdr);
774 genlmsg_cancel(msg, hdr);
779 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
781 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
782 state == DEVLINK_PORT_FN_STATE_ACTIVE;
786 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
788 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
789 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
792 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
793 struct devlink_port *port,
795 struct netlink_ext_ack *extack,
798 enum devlink_port_fn_opstate opstate;
799 enum devlink_port_fn_state state;
802 if (!ops->port_fn_state_get)
805 err = ops->port_fn_state_get(port, &state, &opstate, extack);
807 if (err == -EOPNOTSUPP)
811 if (!devlink_port_fn_state_valid(state)) {
813 NL_SET_ERR_MSG(extack, "Invalid state read from driver");
816 if (!devlink_port_fn_opstate_valid(opstate)) {
818 NL_SET_ERR_MSG(extack, "Invalid operational state read from driver");
821 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
822 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
829 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
830 struct netlink_ext_ack *extack)
832 const struct devlink_ops *ops = devlink_port->devlink->ops;
834 return ops->port_fn_migratable_set(devlink_port, enable, extack);
838 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
839 struct netlink_ext_ack *extack)
841 const struct devlink_ops *ops = devlink_port->devlink->ops;
843 return ops->port_fn_roce_set(devlink_port, enable, extack);
846 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
847 const struct nlattr *attr,
848 struct netlink_ext_ack *extack)
850 struct nla_bitfield32 caps;
854 caps = nla_get_bitfield32(attr);
855 caps_value = caps.value & caps.selector;
856 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) {
857 err = devlink_port_fn_roce_set(devlink_port,
858 caps_value & DEVLINK_PORT_FN_CAP_ROCE,
863 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
864 err = devlink_port_fn_mig_set(devlink_port, caps_value &
865 DEVLINK_PORT_FN_CAP_MIGRATABLE,
874 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
875 struct netlink_ext_ack *extack)
877 const struct devlink_ops *ops;
878 struct nlattr *function_attr;
879 bool msg_updated = false;
882 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
886 ops = port->devlink->ops;
887 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
891 err = devlink_port_fn_caps_fill(ops, port, msg, extack,
895 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
897 if (err || !msg_updated)
898 nla_nest_cancel(msg, function_attr);
900 nla_nest_end(msg, function_attr);
904 static int devlink_nl_port_fill(struct sk_buff *msg,
905 struct devlink_port *devlink_port,
906 enum devlink_command cmd, u32 portid, u32 seq,
907 int flags, struct netlink_ext_ack *extack)
909 struct devlink *devlink = devlink_port->devlink;
912 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
916 if (devlink_nl_put_handle(msg, devlink))
917 goto nla_put_failure;
918 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
919 goto nla_put_failure;
921 spin_lock_bh(&devlink_port->type_lock);
922 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
923 goto nla_put_failure_type_locked;
924 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
925 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
926 devlink_port->desired_type))
927 goto nla_put_failure_type_locked;
928 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
929 if (devlink_port->type_eth.netdev &&
930 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
931 devlink_port->type_eth.ifindex) ||
932 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
933 devlink_port->type_eth.ifname)))
934 goto nla_put_failure_type_locked;
936 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
937 struct ib_device *ibdev = devlink_port->type_ib.ibdev;
940 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
942 goto nla_put_failure_type_locked;
944 spin_unlock_bh(&devlink_port->type_lock);
945 if (devlink_nl_port_attrs_put(msg, devlink_port))
946 goto nla_put_failure;
947 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
948 goto nla_put_failure;
949 if (devlink_port->linecard &&
950 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
951 devlink_port->linecard->index))
952 goto nla_put_failure;
954 genlmsg_end(msg, hdr);
957 nla_put_failure_type_locked:
958 spin_unlock_bh(&devlink_port->type_lock);
960 genlmsg_cancel(msg, hdr);
964 static void devlink_port_notify(struct devlink_port *devlink_port,
965 enum devlink_command cmd)
967 struct devlink *devlink = devlink_port->devlink;
971 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
973 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
976 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
980 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
986 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
987 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
990 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
991 enum devlink_command cmd)
993 struct devlink *devlink = devlink_rate->devlink;
997 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
999 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1006 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1012 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1013 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1017 devlink_nl_cmd_rate_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
1018 struct netlink_callback *cb)
1020 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1021 struct devlink_rate *devlink_rate;
1025 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1026 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1027 u32 id = NETLINK_CB(cb->skb).portid;
1029 if (idx < state->idx) {
1033 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1046 const struct devlink_cmd devl_cmd_rate_get = {
1047 .dump_one = devlink_nl_cmd_rate_get_dump_one,
1050 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1051 struct genl_info *info)
1053 struct devlink_rate *devlink_rate = info->user_ptr[1];
1054 struct sk_buff *msg;
1057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1061 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1062 info->snd_portid, info->snd_seq, 0,
1069 return genlmsg_reply(msg, info);
1073 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1074 struct devlink_rate *parent)
1077 if (parent == devlink_rate)
1079 parent = parent->parent;
1084 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1085 struct genl_info *info)
1087 struct devlink_port *devlink_port = info->user_ptr[1];
1088 struct sk_buff *msg;
1091 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1095 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1096 info->snd_portid, info->snd_seq, 0,
1103 return genlmsg_reply(msg, info);
1107 devlink_nl_cmd_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
1108 struct netlink_callback *cb)
1110 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1111 struct devlink_port *devlink_port;
1112 unsigned long port_index;
1115 xa_for_each_start(&devlink->ports, port_index, devlink_port, state->idx) {
1116 err = devlink_nl_port_fill(msg, devlink_port,
1118 NETLINK_CB(cb->skb).portid,
1120 NLM_F_MULTI, cb->extack);
1122 state->idx = port_index;
1130 const struct devlink_cmd devl_cmd_port_get = {
1131 .dump_one = devlink_nl_cmd_port_get_dump_one,
1134 static int devlink_port_type_set(struct devlink_port *devlink_port,
1135 enum devlink_port_type port_type)
1140 if (!devlink_port->devlink->ops->port_type_set)
1143 if (port_type == devlink_port->type)
1146 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1151 devlink_port->desired_type = port_type;
1152 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1156 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1157 const struct nlattr *attr,
1158 struct netlink_ext_ack *extack)
1160 const struct devlink_ops *ops = port->devlink->ops;
1164 hw_addr = nla_data(attr);
1165 hw_addr_len = nla_len(attr);
1166 if (hw_addr_len > MAX_ADDR_LEN) {
1167 NL_SET_ERR_MSG(extack, "Port function hardware address too long");
1170 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1171 if (hw_addr_len != ETH_ALEN) {
1172 NL_SET_ERR_MSG(extack, "Address must be 6 bytes for Ethernet device");
1175 if (!is_unicast_ether_addr(hw_addr)) {
1176 NL_SET_ERR_MSG(extack, "Non-unicast hardware address unsupported");
1181 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1185 static int devlink_port_fn_state_set(struct devlink_port *port,
1186 const struct nlattr *attr,
1187 struct netlink_ext_ack *extack)
1189 enum devlink_port_fn_state state;
1190 const struct devlink_ops *ops;
1192 state = nla_get_u8(attr);
1193 ops = port->devlink->ops;
1194 return ops->port_fn_state_set(port, state, extack);
1197 static int devlink_port_function_validate(struct devlink_port *devlink_port,
1199 struct netlink_ext_ack *extack)
1201 const struct devlink_ops *ops = devlink_port->devlink->ops;
1202 struct nlattr *attr;
1204 if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
1205 !ops->port_function_hw_addr_set) {
1206 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1207 "Port doesn't support function attributes");
1210 if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
1211 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1212 "Function does not support state setting");
1215 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1217 struct nla_bitfield32 caps;
1219 caps = nla_get_bitfield32(attr);
1220 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
1221 !ops->port_fn_roce_set) {
1222 NL_SET_ERR_MSG_ATTR(extack, attr,
1223 "Port doesn't support RoCE function attribute");
1226 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1227 if (!ops->port_fn_migratable_set) {
1228 NL_SET_ERR_MSG_ATTR(extack, attr,
1229 "Port doesn't support migratable function attribute");
1232 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
1233 NL_SET_ERR_MSG_ATTR(extack, attr,
1234 "migratable function attribute supported for VFs only");
1242 static int devlink_port_function_set(struct devlink_port *port,
1243 const struct nlattr *attr,
1244 struct netlink_ext_ack *extack)
1246 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1249 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1250 devlink_function_nl_policy, extack);
1252 NL_SET_ERR_MSG(extack, "Fail to parse port function attributes");
1256 err = devlink_port_function_validate(port, tb, extack);
1260 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1262 err = devlink_port_function_hw_addr_set(port, attr, extack);
1267 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1269 err = devlink_port_fn_caps_set(port, attr, extack);
1274 /* Keep this as the last function attribute set, so that when
1275 * multiple port function attributes are set along with state,
1276 * Those can be applied first before activating the state.
1278 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1280 err = devlink_port_fn_state_set(port, attr, extack);
1283 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1287 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1288 struct genl_info *info)
1290 struct devlink_port *devlink_port = info->user_ptr[1];
1293 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1294 enum devlink_port_type port_type;
1296 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1297 err = devlink_port_type_set(devlink_port, port_type);
1302 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1303 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1304 struct netlink_ext_ack *extack = info->extack;
1306 err = devlink_port_function_set(devlink_port, attr, extack);
1314 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1315 struct genl_info *info)
1317 struct devlink_port *devlink_port = info->user_ptr[1];
1318 struct devlink *devlink = info->user_ptr[0];
1321 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
1323 if (!devlink->ops->port_split)
1326 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1328 if (!devlink_port->attrs.splittable) {
1329 /* Split ports cannot be split. */
1330 if (devlink_port->attrs.split)
1331 NL_SET_ERR_MSG(info->extack, "Port cannot be split further");
1333 NL_SET_ERR_MSG(info->extack, "Port cannot be split");
1337 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1338 NL_SET_ERR_MSG(info->extack, "Invalid split count");
1342 return devlink->ops->port_split(devlink, devlink_port, count,
1346 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1347 struct genl_info *info)
1349 struct devlink_port *devlink_port = info->user_ptr[1];
1350 struct devlink *devlink = info->user_ptr[0];
1352 if (!devlink->ops->port_unsplit)
1354 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1357 static int devlink_port_new_notify(struct devlink *devlink,
1358 unsigned int port_index,
1359 struct genl_info *info)
1361 struct devlink_port *devlink_port;
1362 struct sk_buff *msg;
1365 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1369 lockdep_assert_held(&devlink->lock);
1370 devlink_port = devlink_port_get_by_index(devlink, port_index);
1371 if (!devlink_port) {
1376 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1377 info->snd_portid, info->snd_seq, 0, NULL);
1381 return genlmsg_reply(msg, info);
1388 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1389 struct genl_info *info)
1391 struct netlink_ext_ack *extack = info->extack;
1392 struct devlink_port_new_attrs new_attrs = {};
1393 struct devlink *devlink = info->user_ptr[0];
1394 unsigned int new_port_index;
1397 if (!devlink->ops->port_new || !devlink->ops->port_del)
1400 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1401 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1402 NL_SET_ERR_MSG(extack, "Port flavour or PCI PF are not specified");
1405 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1407 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1409 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1410 /* Port index of the new port being created by driver. */
1411 new_attrs.port_index =
1412 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1413 new_attrs.port_index_valid = true;
1415 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1416 new_attrs.controller =
1417 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1418 new_attrs.controller_valid = true;
1420 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1421 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1422 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1423 new_attrs.sfnum_valid = true;
1426 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1431 err = devlink_port_new_notify(devlink, new_port_index, info);
1432 if (err && err != -ENODEV) {
1433 /* Fail to send the response; destroy newly created port. */
1434 devlink->ops->port_del(devlink, new_port_index, extack);
1439 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1440 struct genl_info *info)
1442 struct netlink_ext_ack *extack = info->extack;
1443 struct devlink *devlink = info->user_ptr[0];
1444 unsigned int port_index;
1446 if (!devlink->ops->port_del)
1449 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_INDEX)) {
1450 NL_SET_ERR_MSG(extack, "Port index is not specified");
1453 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1455 return devlink->ops->port_del(devlink, port_index, extack);
1459 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1460 struct genl_info *info,
1461 struct nlattr *nla_parent)
1463 struct devlink *devlink = devlink_rate->devlink;
1464 const char *parent_name = nla_data(nla_parent);
1465 const struct devlink_ops *ops = devlink->ops;
1466 size_t len = strlen(parent_name);
1467 struct devlink_rate *parent;
1468 int err = -EOPNOTSUPP;
1470 parent = devlink_rate->parent;
1472 if (parent && !len) {
1473 if (devlink_rate_is_leaf(devlink_rate))
1474 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1475 devlink_rate->priv, NULL,
1477 else if (devlink_rate_is_node(devlink_rate))
1478 err = ops->rate_node_parent_set(devlink_rate, NULL,
1479 devlink_rate->priv, NULL,
1484 refcount_dec(&parent->refcnt);
1485 devlink_rate->parent = NULL;
1487 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1491 if (parent == devlink_rate) {
1492 NL_SET_ERR_MSG(info->extack, "Parent to self is not allowed");
1496 if (devlink_rate_is_node(devlink_rate) &&
1497 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1498 NL_SET_ERR_MSG(info->extack, "Node is already a parent of parent node.");
1502 if (devlink_rate_is_leaf(devlink_rate))
1503 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1504 devlink_rate->priv, parent->priv,
1506 else if (devlink_rate_is_node(devlink_rate))
1507 err = ops->rate_node_parent_set(devlink_rate, parent,
1508 devlink_rate->priv, parent->priv,
1513 if (devlink_rate->parent)
1514 /* we're reassigning to other parent in this case */
1515 refcount_dec(&devlink_rate->parent->refcnt);
1517 refcount_inc(&parent->refcnt);
1518 devlink_rate->parent = parent;
1524 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1525 const struct devlink_ops *ops,
1526 struct genl_info *info)
1528 struct nlattr *nla_parent, **attrs = info->attrs;
1529 int err = -EOPNOTSUPP;
1534 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1535 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1536 if (devlink_rate_is_leaf(devlink_rate))
1537 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1538 rate, info->extack);
1539 else if (devlink_rate_is_node(devlink_rate))
1540 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1541 rate, info->extack);
1544 devlink_rate->tx_share = rate;
1547 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1548 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1549 if (devlink_rate_is_leaf(devlink_rate))
1550 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1551 rate, info->extack);
1552 else if (devlink_rate_is_node(devlink_rate))
1553 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1554 rate, info->extack);
1557 devlink_rate->tx_max = rate;
1560 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]) {
1561 priority = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]);
1562 if (devlink_rate_is_leaf(devlink_rate))
1563 err = ops->rate_leaf_tx_priority_set(devlink_rate, devlink_rate->priv,
1564 priority, info->extack);
1565 else if (devlink_rate_is_node(devlink_rate))
1566 err = ops->rate_node_tx_priority_set(devlink_rate, devlink_rate->priv,
1567 priority, info->extack);
1571 devlink_rate->tx_priority = priority;
1574 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]) {
1575 weight = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]);
1576 if (devlink_rate_is_leaf(devlink_rate))
1577 err = ops->rate_leaf_tx_weight_set(devlink_rate, devlink_rate->priv,
1578 weight, info->extack);
1579 else if (devlink_rate_is_node(devlink_rate))
1580 err = ops->rate_node_tx_weight_set(devlink_rate, devlink_rate->priv,
1581 weight, info->extack);
1585 devlink_rate->tx_weight = weight;
1588 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1590 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1599 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1600 struct genl_info *info,
1601 enum devlink_rate_type type)
1603 struct nlattr **attrs = info->attrs;
1605 if (type == DEVLINK_RATE_TYPE_LEAF) {
1606 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1607 NL_SET_ERR_MSG(info->extack, "TX share set isn't supported for the leafs");
1610 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1611 NL_SET_ERR_MSG(info->extack, "TX max set isn't supported for the leafs");
1614 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1615 !ops->rate_leaf_parent_set) {
1616 NL_SET_ERR_MSG(info->extack, "Parent set isn't supported for the leafs");
1619 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_leaf_tx_priority_set) {
1620 NL_SET_ERR_MSG_ATTR(info->extack,
1621 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1622 "TX priority set isn't supported for the leafs");
1625 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_leaf_tx_weight_set) {
1626 NL_SET_ERR_MSG_ATTR(info->extack,
1627 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1628 "TX weight set isn't supported for the leafs");
1631 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1632 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1633 NL_SET_ERR_MSG(info->extack, "TX share set isn't supported for the nodes");
1636 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1637 NL_SET_ERR_MSG(info->extack, "TX max set isn't supported for the nodes");
1640 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1641 !ops->rate_node_parent_set) {
1642 NL_SET_ERR_MSG(info->extack, "Parent set isn't supported for the nodes");
1645 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_node_tx_priority_set) {
1646 NL_SET_ERR_MSG_ATTR(info->extack,
1647 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1648 "TX priority set isn't supported for the nodes");
1651 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_node_tx_weight_set) {
1652 NL_SET_ERR_MSG_ATTR(info->extack,
1653 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1654 "TX weight set isn't supported for the nodes");
1658 WARN(1, "Unknown type of rate object");
1665 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1666 struct genl_info *info)
1668 struct devlink_rate *devlink_rate = info->user_ptr[1];
1669 struct devlink *devlink = devlink_rate->devlink;
1670 const struct devlink_ops *ops = devlink->ops;
1673 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1676 err = devlink_nl_rate_set(devlink_rate, ops, info);
1679 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1683 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1684 struct genl_info *info)
1686 struct devlink *devlink = info->user_ptr[0];
1687 struct devlink_rate *rate_node;
1688 const struct devlink_ops *ops;
1692 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1693 NL_SET_ERR_MSG(info->extack, "Rate nodes aren't supported");
1697 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1700 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1701 if (!IS_ERR(rate_node))
1703 else if (rate_node == ERR_PTR(-EINVAL))
1706 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1710 rate_node->devlink = devlink;
1711 rate_node->type = DEVLINK_RATE_TYPE_NODE;
1712 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1713 if (!rate_node->name) {
1718 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1722 err = devlink_nl_rate_set(rate_node, ops, info);
1726 refcount_set(&rate_node->refcnt, 1);
1727 list_add(&rate_node->list, &devlink->rate_list);
1728 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
1732 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1734 kfree(rate_node->name);
1740 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
1741 struct genl_info *info)
1743 struct devlink_rate *rate_node = info->user_ptr[1];
1744 struct devlink *devlink = rate_node->devlink;
1745 const struct devlink_ops *ops = devlink->ops;
1748 if (refcount_read(&rate_node->refcnt) > 1) {
1749 NL_SET_ERR_MSG(info->extack, "Node has children. Cannot delete node.");
1753 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
1754 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1755 if (rate_node->parent)
1756 refcount_dec(&rate_node->parent->refcnt);
1757 list_del(&rate_node->list);
1758 kfree(rate_node->name);
1763 struct devlink_linecard_type {
1768 static int devlink_nl_linecard_fill(struct sk_buff *msg,
1769 struct devlink *devlink,
1770 struct devlink_linecard *linecard,
1771 enum devlink_command cmd, u32 portid,
1773 struct netlink_ext_ack *extack)
1775 struct devlink_linecard_type *linecard_type;
1776 struct nlattr *attr;
1780 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1784 if (devlink_nl_put_handle(msg, devlink))
1785 goto nla_put_failure;
1786 if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
1787 goto nla_put_failure;
1788 if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
1789 goto nla_put_failure;
1790 if (linecard->type &&
1791 nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
1792 goto nla_put_failure;
1794 if (linecard->types_count) {
1795 attr = nla_nest_start(msg,
1796 DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
1798 goto nla_put_failure;
1799 for (i = 0; i < linecard->types_count; i++) {
1800 linecard_type = &linecard->types[i];
1801 if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
1802 linecard_type->type)) {
1803 nla_nest_cancel(msg, attr);
1804 goto nla_put_failure;
1807 nla_nest_end(msg, attr);
1810 if (linecard->nested_devlink &&
1811 devlink_nl_put_nested_handle(msg, linecard->nested_devlink))
1812 goto nla_put_failure;
1814 genlmsg_end(msg, hdr);
1818 genlmsg_cancel(msg, hdr);
1822 static void devlink_linecard_notify(struct devlink_linecard *linecard,
1823 enum devlink_command cmd)
1825 struct devlink *devlink = linecard->devlink;
1826 struct sk_buff *msg;
1829 WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
1830 cmd != DEVLINK_CMD_LINECARD_DEL);
1832 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1835 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1839 err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
1846 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
1847 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1850 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
1851 struct genl_info *info)
1853 struct devlink_linecard *linecard = info->user_ptr[1];
1854 struct devlink *devlink = linecard->devlink;
1855 struct sk_buff *msg;
1858 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1862 mutex_lock(&linecard->state_lock);
1863 err = devlink_nl_linecard_fill(msg, devlink, linecard,
1864 DEVLINK_CMD_LINECARD_NEW,
1865 info->snd_portid, info->snd_seq, 0,
1867 mutex_unlock(&linecard->state_lock);
1873 return genlmsg_reply(msg, info);
1876 static int devlink_nl_cmd_linecard_get_dump_one(struct sk_buff *msg,
1877 struct devlink *devlink,
1878 struct netlink_callback *cb)
1880 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1881 struct devlink_linecard *linecard;
1885 list_for_each_entry(linecard, &devlink->linecard_list, list) {
1886 if (idx < state->idx) {
1890 mutex_lock(&linecard->state_lock);
1891 err = devlink_nl_linecard_fill(msg, devlink, linecard,
1892 DEVLINK_CMD_LINECARD_NEW,
1893 NETLINK_CB(cb->skb).portid,
1897 mutex_unlock(&linecard->state_lock);
1908 const struct devlink_cmd devl_cmd_linecard_get = {
1909 .dump_one = devlink_nl_cmd_linecard_get_dump_one,
1912 static struct devlink_linecard_type *
1913 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
1916 struct devlink_linecard_type *linecard_type;
1919 for (i = 0; i < linecard->types_count; i++) {
1920 linecard_type = &linecard->types[i];
1921 if (!strcmp(type, linecard_type->type))
1922 return linecard_type;
1927 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
1929 struct netlink_ext_ack *extack)
1931 const struct devlink_linecard_ops *ops = linecard->ops;
1932 struct devlink_linecard_type *linecard_type;
1935 mutex_lock(&linecard->state_lock);
1936 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
1937 NL_SET_ERR_MSG(extack, "Line card is currently being provisioned");
1941 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
1942 NL_SET_ERR_MSG(extack, "Line card is currently being unprovisioned");
1947 linecard_type = devlink_linecard_type_lookup(linecard, type);
1948 if (!linecard_type) {
1949 NL_SET_ERR_MSG(extack, "Unsupported line card type provided");
1954 if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
1955 linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
1956 NL_SET_ERR_MSG(extack, "Line card already provisioned");
1958 /* Check if the line card is provisioned in the same
1959 * way the user asks. In case it is, make the operation
1960 * to return success.
1962 if (ops->same_provision &&
1963 ops->same_provision(linecard, linecard->priv,
1964 linecard_type->type,
1965 linecard_type->priv))
1970 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
1971 linecard->type = linecard_type->type;
1972 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
1973 mutex_unlock(&linecard->state_lock);
1974 err = ops->provision(linecard, linecard->priv, linecard_type->type,
1975 linecard_type->priv, extack);
1977 /* Provisioning failed. Assume the linecard is unprovisioned
1978 * for future operations.
1980 mutex_lock(&linecard->state_lock);
1981 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
1982 linecard->type = NULL;
1983 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
1984 mutex_unlock(&linecard->state_lock);
1989 mutex_unlock(&linecard->state_lock);
1993 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
1994 struct netlink_ext_ack *extack)
1998 mutex_lock(&linecard->state_lock);
1999 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2000 NL_SET_ERR_MSG(extack, "Line card is currently being provisioned");
2004 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2005 NL_SET_ERR_MSG(extack, "Line card is currently being unprovisioned");
2009 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2010 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2011 linecard->type = NULL;
2012 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2017 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2018 NL_SET_ERR_MSG(extack, "Line card is not provisioned");
2022 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2023 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2024 mutex_unlock(&linecard->state_lock);
2025 err = linecard->ops->unprovision(linecard, linecard->priv,
2028 /* Unprovisioning failed. Assume the linecard is unprovisioned
2029 * for future operations.
2031 mutex_lock(&linecard->state_lock);
2032 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2033 linecard->type = NULL;
2034 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2035 mutex_unlock(&linecard->state_lock);
2040 mutex_unlock(&linecard->state_lock);
2044 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2045 struct genl_info *info)
2047 struct devlink_linecard *linecard = info->user_ptr[1];
2048 struct netlink_ext_ack *extack = info->extack;
2051 if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2054 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2055 if (strcmp(type, "")) {
2056 err = devlink_linecard_type_set(linecard, type, extack);
2060 err = devlink_linecard_type_unset(linecard, extack);
2069 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2070 struct devlink_sb *devlink_sb,
2071 enum devlink_command cmd, u32 portid,
2076 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2080 if (devlink_nl_put_handle(msg, devlink))
2081 goto nla_put_failure;
2082 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2083 goto nla_put_failure;
2084 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2085 goto nla_put_failure;
2086 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2087 devlink_sb->ingress_pools_count))
2088 goto nla_put_failure;
2089 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2090 devlink_sb->egress_pools_count))
2091 goto nla_put_failure;
2092 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2093 devlink_sb->ingress_tc_count))
2094 goto nla_put_failure;
2095 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2096 devlink_sb->egress_tc_count))
2097 goto nla_put_failure;
2099 genlmsg_end(msg, hdr);
2103 genlmsg_cancel(msg, hdr);
2107 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2108 struct genl_info *info)
2110 struct devlink *devlink = info->user_ptr[0];
2111 struct devlink_sb *devlink_sb;
2112 struct sk_buff *msg;
2115 devlink_sb = devlink_sb_get_from_info(devlink, info);
2116 if (IS_ERR(devlink_sb))
2117 return PTR_ERR(devlink_sb);
2119 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2123 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2125 info->snd_portid, info->snd_seq, 0);
2131 return genlmsg_reply(msg, info);
2135 devlink_nl_cmd_sb_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
2136 struct netlink_callback *cb)
2138 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2139 struct devlink_sb *devlink_sb;
2143 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2144 if (idx < state->idx) {
2148 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2150 NETLINK_CB(cb->skb).portid,
2163 const struct devlink_cmd devl_cmd_sb_get = {
2164 .dump_one = devlink_nl_cmd_sb_get_dump_one,
2167 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2168 struct devlink_sb *devlink_sb,
2169 u16 pool_index, enum devlink_command cmd,
2170 u32 portid, u32 seq, int flags)
2172 struct devlink_sb_pool_info pool_info;
2176 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2177 pool_index, &pool_info);
2181 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2185 if (devlink_nl_put_handle(msg, devlink))
2186 goto nla_put_failure;
2187 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2188 goto nla_put_failure;
2189 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2190 goto nla_put_failure;
2191 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2192 goto nla_put_failure;
2193 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2194 goto nla_put_failure;
2195 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2196 pool_info.threshold_type))
2197 goto nla_put_failure;
2198 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2199 pool_info.cell_size))
2200 goto nla_put_failure;
2202 genlmsg_end(msg, hdr);
2206 genlmsg_cancel(msg, hdr);
2210 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2211 struct genl_info *info)
2213 struct devlink *devlink = info->user_ptr[0];
2214 struct devlink_sb *devlink_sb;
2215 struct sk_buff *msg;
2219 devlink_sb = devlink_sb_get_from_info(devlink, info);
2220 if (IS_ERR(devlink_sb))
2221 return PTR_ERR(devlink_sb);
2223 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2228 if (!devlink->ops->sb_pool_get)
2231 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2235 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2236 DEVLINK_CMD_SB_POOL_NEW,
2237 info->snd_portid, info->snd_seq, 0);
2243 return genlmsg_reply(msg, info);
2246 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2247 struct devlink *devlink,
2248 struct devlink_sb *devlink_sb,
2249 u32 portid, u32 seq)
2251 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2255 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2256 if (*p_idx < start) {
2260 err = devlink_nl_sb_pool_fill(msg, devlink,
2263 DEVLINK_CMD_SB_POOL_NEW,
2264 portid, seq, NLM_F_MULTI);
2273 devlink_nl_cmd_sb_pool_get_dump_one(struct sk_buff *msg,
2274 struct devlink *devlink,
2275 struct netlink_callback *cb)
2277 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2278 struct devlink_sb *devlink_sb;
2282 if (!devlink->ops->sb_pool_get)
2285 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2286 err = __sb_pool_get_dumpit(msg, state->idx, &idx,
2287 devlink, devlink_sb,
2288 NETLINK_CB(cb->skb).portid,
2289 cb->nlh->nlmsg_seq);
2290 if (err == -EOPNOTSUPP) {
2301 const struct devlink_cmd devl_cmd_sb_pool_get = {
2302 .dump_one = devlink_nl_cmd_sb_pool_get_dump_one,
2305 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2306 u16 pool_index, u32 size,
2307 enum devlink_sb_threshold_type threshold_type,
2308 struct netlink_ext_ack *extack)
2311 const struct devlink_ops *ops = devlink->ops;
2313 if (ops->sb_pool_set)
2314 return ops->sb_pool_set(devlink, sb_index, pool_index,
2315 size, threshold_type, extack);
2319 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2320 struct genl_info *info)
2322 struct devlink *devlink = info->user_ptr[0];
2323 enum devlink_sb_threshold_type threshold_type;
2324 struct devlink_sb *devlink_sb;
2329 devlink_sb = devlink_sb_get_from_info(devlink, info);
2330 if (IS_ERR(devlink_sb))
2331 return PTR_ERR(devlink_sb);
2333 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2338 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2342 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_POOL_SIZE))
2345 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2346 return devlink_sb_pool_set(devlink, devlink_sb->index,
2347 pool_index, size, threshold_type,
2351 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2352 struct devlink *devlink,
2353 struct devlink_port *devlink_port,
2354 struct devlink_sb *devlink_sb,
2356 enum devlink_command cmd,
2357 u32 portid, u32 seq, int flags)
2359 const struct devlink_ops *ops = devlink->ops;
2364 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2365 pool_index, &threshold);
2369 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2373 if (devlink_nl_put_handle(msg, devlink))
2374 goto nla_put_failure;
2375 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2376 goto nla_put_failure;
2377 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2378 goto nla_put_failure;
2379 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2380 goto nla_put_failure;
2381 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2382 goto nla_put_failure;
2384 if (ops->sb_occ_port_pool_get) {
2388 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2389 pool_index, &cur, &max);
2390 if (err && err != -EOPNOTSUPP)
2391 goto sb_occ_get_failure;
2393 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2394 goto nla_put_failure;
2395 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2396 goto nla_put_failure;
2400 genlmsg_end(msg, hdr);
2406 genlmsg_cancel(msg, hdr);
2410 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2411 struct genl_info *info)
2413 struct devlink_port *devlink_port = info->user_ptr[1];
2414 struct devlink *devlink = devlink_port->devlink;
2415 struct devlink_sb *devlink_sb;
2416 struct sk_buff *msg;
2420 devlink_sb = devlink_sb_get_from_info(devlink, info);
2421 if (IS_ERR(devlink_sb))
2422 return PTR_ERR(devlink_sb);
2424 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2429 if (!devlink->ops->sb_port_pool_get)
2432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2436 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2437 devlink_sb, pool_index,
2438 DEVLINK_CMD_SB_PORT_POOL_NEW,
2439 info->snd_portid, info->snd_seq, 0);
2445 return genlmsg_reply(msg, info);
2448 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2449 struct devlink *devlink,
2450 struct devlink_sb *devlink_sb,
2451 u32 portid, u32 seq)
2453 struct devlink_port *devlink_port;
2454 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2455 unsigned long port_index;
2459 xa_for_each(&devlink->ports, port_index, devlink_port) {
2460 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2461 if (*p_idx < start) {
2465 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2469 DEVLINK_CMD_SB_PORT_POOL_NEW,
2481 devlink_nl_cmd_sb_port_pool_get_dump_one(struct sk_buff *msg,
2482 struct devlink *devlink,
2483 struct netlink_callback *cb)
2485 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2486 struct devlink_sb *devlink_sb;
2490 if (!devlink->ops->sb_port_pool_get)
2493 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2494 err = __sb_port_pool_get_dumpit(msg, state->idx, &idx,
2495 devlink, devlink_sb,
2496 NETLINK_CB(cb->skb).portid,
2497 cb->nlh->nlmsg_seq);
2498 if (err == -EOPNOTSUPP) {
2509 const struct devlink_cmd devl_cmd_sb_port_pool_get = {
2510 .dump_one = devlink_nl_cmd_sb_port_pool_get_dump_one,
2513 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2514 unsigned int sb_index, u16 pool_index,
2516 struct netlink_ext_ack *extack)
2519 const struct devlink_ops *ops = devlink_port->devlink->ops;
2521 if (ops->sb_port_pool_set)
2522 return ops->sb_port_pool_set(devlink_port, sb_index,
2523 pool_index, threshold, extack);
2527 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2528 struct genl_info *info)
2530 struct devlink_port *devlink_port = info->user_ptr[1];
2531 struct devlink *devlink = info->user_ptr[0];
2532 struct devlink_sb *devlink_sb;
2537 devlink_sb = devlink_sb_get_from_info(devlink, info);
2538 if (IS_ERR(devlink_sb))
2539 return PTR_ERR(devlink_sb);
2541 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2546 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2549 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2550 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2551 pool_index, threshold, info->extack);
2555 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2556 struct devlink_port *devlink_port,
2557 struct devlink_sb *devlink_sb, u16 tc_index,
2558 enum devlink_sb_pool_type pool_type,
2559 enum devlink_command cmd,
2560 u32 portid, u32 seq, int flags)
2562 const struct devlink_ops *ops = devlink->ops;
2568 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2569 tc_index, pool_type,
2570 &pool_index, &threshold);
2574 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2578 if (devlink_nl_put_handle(msg, devlink))
2579 goto nla_put_failure;
2580 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2581 goto nla_put_failure;
2582 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2583 goto nla_put_failure;
2584 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2585 goto nla_put_failure;
2586 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2587 goto nla_put_failure;
2588 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2589 goto nla_put_failure;
2590 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2591 goto nla_put_failure;
2593 if (ops->sb_occ_tc_port_bind_get) {
2597 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2599 tc_index, pool_type,
2601 if (err && err != -EOPNOTSUPP)
2604 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2605 goto nla_put_failure;
2606 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2607 goto nla_put_failure;
2611 genlmsg_end(msg, hdr);
2615 genlmsg_cancel(msg, hdr);
2619 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2620 struct genl_info *info)
2622 struct devlink_port *devlink_port = info->user_ptr[1];
2623 struct devlink *devlink = devlink_port->devlink;
2624 struct devlink_sb *devlink_sb;
2625 struct sk_buff *msg;
2626 enum devlink_sb_pool_type pool_type;
2630 devlink_sb = devlink_sb_get_from_info(devlink, info);
2631 if (IS_ERR(devlink_sb))
2632 return PTR_ERR(devlink_sb);
2634 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2638 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2639 pool_type, &tc_index);
2643 if (!devlink->ops->sb_tc_pool_bind_get)
2646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2650 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2651 devlink_sb, tc_index, pool_type,
2652 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2660 return genlmsg_reply(msg, info);
2663 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2664 int start, int *p_idx,
2665 struct devlink *devlink,
2666 struct devlink_sb *devlink_sb,
2667 u32 portid, u32 seq)
2669 struct devlink_port *devlink_port;
2670 unsigned long port_index;
2674 xa_for_each(&devlink->ports, port_index, devlink_port) {
2676 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2677 if (*p_idx < start) {
2681 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2685 DEVLINK_SB_POOL_TYPE_INGRESS,
2686 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2694 tc_index < devlink_sb->egress_tc_count; tc_index++) {
2695 if (*p_idx < start) {
2699 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2703 DEVLINK_SB_POOL_TYPE_EGRESS,
2704 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2716 devlink_nl_cmd_sb_tc_pool_bind_get_dump_one(struct sk_buff *msg,
2717 struct devlink *devlink,
2718 struct netlink_callback *cb)
2720 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2721 struct devlink_sb *devlink_sb;
2725 if (!devlink->ops->sb_tc_pool_bind_get)
2728 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2729 err = __sb_tc_pool_bind_get_dumpit(msg, state->idx, &idx,
2730 devlink, devlink_sb,
2731 NETLINK_CB(cb->skb).portid,
2732 cb->nlh->nlmsg_seq);
2733 if (err == -EOPNOTSUPP) {
2744 const struct devlink_cmd devl_cmd_sb_tc_pool_bind_get = {
2745 .dump_one = devlink_nl_cmd_sb_tc_pool_bind_get_dump_one,
2748 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
2749 unsigned int sb_index, u16 tc_index,
2750 enum devlink_sb_pool_type pool_type,
2751 u16 pool_index, u32 threshold,
2752 struct netlink_ext_ack *extack)
2755 const struct devlink_ops *ops = devlink_port->devlink->ops;
2757 if (ops->sb_tc_pool_bind_set)
2758 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
2759 tc_index, pool_type,
2760 pool_index, threshold, extack);
2764 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
2765 struct genl_info *info)
2767 struct devlink_port *devlink_port = info->user_ptr[1];
2768 struct devlink *devlink = info->user_ptr[0];
2769 enum devlink_sb_pool_type pool_type;
2770 struct devlink_sb *devlink_sb;
2776 devlink_sb = devlink_sb_get_from_info(devlink, info);
2777 if (IS_ERR(devlink_sb))
2778 return PTR_ERR(devlink_sb);
2780 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2784 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2785 pool_type, &tc_index);
2789 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2794 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2797 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2798 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
2799 tc_index, pool_type,
2800 pool_index, threshold, info->extack);
2803 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
2804 struct genl_info *info)
2806 struct devlink *devlink = info->user_ptr[0];
2807 const struct devlink_ops *ops = devlink->ops;
2808 struct devlink_sb *devlink_sb;
2810 devlink_sb = devlink_sb_get_from_info(devlink, info);
2811 if (IS_ERR(devlink_sb))
2812 return PTR_ERR(devlink_sb);
2814 if (ops->sb_occ_snapshot)
2815 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
2819 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
2820 struct genl_info *info)
2822 struct devlink *devlink = info->user_ptr[0];
2823 const struct devlink_ops *ops = devlink->ops;
2824 struct devlink_sb *devlink_sb;
2826 devlink_sb = devlink_sb_get_from_info(devlink, info);
2827 if (IS_ERR(devlink_sb))
2828 return PTR_ERR(devlink_sb);
2830 if (ops->sb_occ_max_clear)
2831 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
2835 int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
2836 struct netlink_ext_ack *extack)
2838 struct devlink_rate *devlink_rate;
2840 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
2841 if (devlink_rate_is_node(devlink_rate)) {
2842 NL_SET_ERR_MSG(extack, "Rate node(s) exists.");
2848 int devlink_dpipe_match_put(struct sk_buff *skb,
2849 struct devlink_dpipe_match *match)
2851 struct devlink_dpipe_header *header = match->header;
2852 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2853 struct nlattr *match_attr;
2855 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2859 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2860 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2861 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2862 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2863 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2864 goto nla_put_failure;
2866 nla_nest_end(skb, match_attr);
2870 nla_nest_cancel(skb, match_attr);
2873 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2875 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2876 struct sk_buff *skb)
2878 struct nlattr *matches_attr;
2880 matches_attr = nla_nest_start_noflag(skb,
2881 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2885 if (table->table_ops->matches_dump(table->priv, skb))
2886 goto nla_put_failure;
2888 nla_nest_end(skb, matches_attr);
2892 nla_nest_cancel(skb, matches_attr);
2896 int devlink_dpipe_action_put(struct sk_buff *skb,
2897 struct devlink_dpipe_action *action)
2899 struct devlink_dpipe_header *header = action->header;
2900 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2901 struct nlattr *action_attr;
2903 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2907 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2908 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2909 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2910 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2911 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2912 goto nla_put_failure;
2914 nla_nest_end(skb, action_attr);
2918 nla_nest_cancel(skb, action_attr);
2921 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
2923 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
2924 struct sk_buff *skb)
2926 struct nlattr *actions_attr;
2928 actions_attr = nla_nest_start_noflag(skb,
2929 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
2933 if (table->table_ops->actions_dump(table->priv, skb))
2934 goto nla_put_failure;
2936 nla_nest_end(skb, actions_attr);
2940 nla_nest_cancel(skb, actions_attr);
2944 static int devlink_dpipe_table_put(struct sk_buff *skb,
2945 struct devlink_dpipe_table *table)
2947 struct nlattr *table_attr;
2950 table_size = table->table_ops->size_get(table->priv);
2951 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
2955 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2956 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2958 goto nla_put_failure;
2959 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2960 table->counters_enabled))
2961 goto nla_put_failure;
2963 if (table->resource_valid) {
2964 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2965 table->resource_id, DEVLINK_ATTR_PAD) ||
2966 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2967 table->resource_units, DEVLINK_ATTR_PAD))
2968 goto nla_put_failure;
2970 if (devlink_dpipe_matches_put(table, skb))
2971 goto nla_put_failure;
2973 if (devlink_dpipe_actions_put(table, skb))
2974 goto nla_put_failure;
2976 nla_nest_end(skb, table_attr);
2980 nla_nest_cancel(skb, table_attr);
2984 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2985 struct genl_info *info)
2990 err = genlmsg_reply(*pskb, info);
2994 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3000 static int devlink_dpipe_tables_fill(struct genl_info *info,
3001 enum devlink_command cmd, int flags,
3002 struct list_head *dpipe_tables,
3003 const char *table_name)
3005 struct devlink *devlink = info->user_ptr[0];
3006 struct devlink_dpipe_table *table;
3007 struct nlattr *tables_attr;
3008 struct sk_buff *skb = NULL;
3009 struct nlmsghdr *nlh;
3015 table = list_first_entry(dpipe_tables,
3016 struct devlink_dpipe_table, list);
3018 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3022 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3023 &devlink_nl_family, NLM_F_MULTI, cmd);
3029 if (devlink_nl_put_handle(skb, devlink))
3030 goto nla_put_failure;
3031 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3033 goto nla_put_failure;
3037 list_for_each_entry_from(table, dpipe_tables, list) {
3039 err = devlink_dpipe_table_put(skb, table);
3047 if (!strcmp(table->name, table_name)) {
3048 err = devlink_dpipe_table_put(skb, table);
3056 nla_nest_end(skb, tables_attr);
3057 genlmsg_end(skb, hdr);
3062 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3063 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3065 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3071 return genlmsg_reply(skb, info);
3080 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3081 struct genl_info *info)
3083 struct devlink *devlink = info->user_ptr[0];
3084 const char *table_name = NULL;
3086 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3087 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3089 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3090 &devlink->dpipe_table_list,
3094 static int devlink_dpipe_value_put(struct sk_buff *skb,
3095 struct devlink_dpipe_value *value)
3097 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3098 value->value_size, value->value))
3101 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3102 value->value_size, value->mask))
3104 if (value->mapping_valid)
3105 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3106 value->mapping_value))
3111 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3112 struct devlink_dpipe_value *value)
3116 if (devlink_dpipe_action_put(skb, value->action))
3118 if (devlink_dpipe_value_put(skb, value))
3123 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3124 struct devlink_dpipe_value *values,
3125 unsigned int values_count)
3127 struct nlattr *action_attr;
3131 for (i = 0; i < values_count; i++) {
3132 action_attr = nla_nest_start_noflag(skb,
3133 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3136 err = devlink_dpipe_action_value_put(skb, &values[i]);
3138 goto err_action_value_put;
3139 nla_nest_end(skb, action_attr);
3143 err_action_value_put:
3144 nla_nest_cancel(skb, action_attr);
3148 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3149 struct devlink_dpipe_value *value)
3153 if (devlink_dpipe_match_put(skb, value->match))
3155 if (devlink_dpipe_value_put(skb, value))
3160 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3161 struct devlink_dpipe_value *values,
3162 unsigned int values_count)
3164 struct nlattr *match_attr;
3168 for (i = 0; i < values_count; i++) {
3169 match_attr = nla_nest_start_noflag(skb,
3170 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3173 err = devlink_dpipe_match_value_put(skb, &values[i]);
3175 goto err_match_value_put;
3176 nla_nest_end(skb, match_attr);
3180 err_match_value_put:
3181 nla_nest_cancel(skb, match_attr);
3185 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3186 struct devlink_dpipe_entry *entry)
3188 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3191 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3195 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3197 goto nla_put_failure;
3198 if (entry->counter_valid)
3199 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3200 entry->counter, DEVLINK_ATTR_PAD))
3201 goto nla_put_failure;
3203 matches_attr = nla_nest_start_noflag(skb,
3204 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3206 goto nla_put_failure;
3208 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3209 entry->match_values_count);
3211 nla_nest_cancel(skb, matches_attr);
3212 goto err_match_values_put;
3214 nla_nest_end(skb, matches_attr);
3216 actions_attr = nla_nest_start_noflag(skb,
3217 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3219 goto nla_put_failure;
3221 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3222 entry->action_values_count);
3224 nla_nest_cancel(skb, actions_attr);
3225 goto err_action_values_put;
3227 nla_nest_end(skb, actions_attr);
3229 nla_nest_end(skb, entry_attr);
3234 err_match_values_put:
3235 err_action_values_put:
3236 nla_nest_cancel(skb, entry_attr);
3240 static struct devlink_dpipe_table *
3241 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3242 const char *table_name, struct devlink *devlink)
3244 struct devlink_dpipe_table *table;
3245 list_for_each_entry_rcu(table, dpipe_tables, list,
3246 lockdep_is_held(&devlink->lock)) {
3247 if (!strcmp(table->name, table_name))
3253 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3255 struct devlink *devlink;
3258 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3263 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3264 dump_ctx->info->snd_portid,
3265 dump_ctx->info->snd_seq,
3266 &devlink_nl_family, NLM_F_MULTI,
3269 goto nla_put_failure;
3271 devlink = dump_ctx->info->user_ptr[0];
3272 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3273 goto nla_put_failure;
3274 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3275 DEVLINK_ATTR_DPIPE_ENTRIES);
3276 if (!dump_ctx->nest)
3277 goto nla_put_failure;
3281 nlmsg_free(dump_ctx->skb);
3284 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3286 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3287 struct devlink_dpipe_entry *entry)
3289 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3291 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3293 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3295 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3296 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3299 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3301 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3304 unsigned int value_count, value_index;
3305 struct devlink_dpipe_value *value;
3307 value = entry->action_values;
3308 value_count = entry->action_values_count;
3309 for (value_index = 0; value_index < value_count; value_index++) {
3310 kfree(value[value_index].value);
3311 kfree(value[value_index].mask);
3314 value = entry->match_values;
3315 value_count = entry->match_values_count;
3316 for (value_index = 0; value_index < value_count; value_index++) {
3317 kfree(value[value_index].value);
3318 kfree(value[value_index].mask);
3321 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3323 static int devlink_dpipe_entries_fill(struct genl_info *info,
3324 enum devlink_command cmd, int flags,
3325 struct devlink_dpipe_table *table)
3327 struct devlink_dpipe_dump_ctx dump_ctx;
3328 struct nlmsghdr *nlh;
3331 dump_ctx.skb = NULL;
3333 dump_ctx.info = info;
3335 err = table->table_ops->entries_dump(table->priv,
3336 table->counters_enabled,
3342 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3343 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3345 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3350 return genlmsg_reply(dump_ctx.skb, info);
3353 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3354 struct genl_info *info)
3356 struct devlink *devlink = info->user_ptr[0];
3357 struct devlink_dpipe_table *table;
3358 const char *table_name;
3360 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
3363 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3364 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3365 table_name, devlink);
3369 if (!table->table_ops->entries_dump)
3372 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3376 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3377 const struct devlink_dpipe_header *header)
3379 struct devlink_dpipe_field *field;
3380 struct nlattr *field_attr;
3383 for (i = 0; i < header->fields_count; i++) {
3384 field = &header->fields[i];
3385 field_attr = nla_nest_start_noflag(skb,
3386 DEVLINK_ATTR_DPIPE_FIELD);
3389 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3390 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3391 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3392 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3393 goto nla_put_failure;
3394 nla_nest_end(skb, field_attr);
3399 nla_nest_cancel(skb, field_attr);
3403 static int devlink_dpipe_header_put(struct sk_buff *skb,
3404 struct devlink_dpipe_header *header)
3406 struct nlattr *fields_attr, *header_attr;
3409 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3413 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3414 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3415 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3416 goto nla_put_failure;
3418 fields_attr = nla_nest_start_noflag(skb,
3419 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3421 goto nla_put_failure;
3423 err = devlink_dpipe_fields_put(skb, header);
3425 nla_nest_cancel(skb, fields_attr);
3426 goto nla_put_failure;
3428 nla_nest_end(skb, fields_attr);
3429 nla_nest_end(skb, header_attr);
3434 nla_nest_cancel(skb, header_attr);
3438 static int devlink_dpipe_headers_fill(struct genl_info *info,
3439 enum devlink_command cmd, int flags,
3440 struct devlink_dpipe_headers *
3443 struct devlink *devlink = info->user_ptr[0];
3444 struct nlattr *headers_attr;
3445 struct sk_buff *skb = NULL;
3446 struct nlmsghdr *nlh;
3453 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3457 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3458 &devlink_nl_family, NLM_F_MULTI, cmd);
3464 if (devlink_nl_put_handle(skb, devlink))
3465 goto nla_put_failure;
3466 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3468 goto nla_put_failure;
3471 for (; i < dpipe_headers->headers_count; i++) {
3472 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3480 nla_nest_end(skb, headers_attr);
3481 genlmsg_end(skb, hdr);
3482 if (i != dpipe_headers->headers_count)
3486 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3487 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3489 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3494 return genlmsg_reply(skb, info);
3503 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3504 struct genl_info *info)
3506 struct devlink *devlink = info->user_ptr[0];
3508 if (!devlink->dpipe_headers)
3510 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3511 0, devlink->dpipe_headers);
3514 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3515 const char *table_name,
3518 struct devlink_dpipe_table *table;
3520 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3521 table_name, devlink);
3525 if (table->counter_control_extern)
3528 if (!(table->counters_enabled ^ enable))
3531 table->counters_enabled = enable;
3532 if (table->table_ops->counters_set_update)
3533 table->table_ops->counters_set_update(table->priv, enable);
3537 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3538 struct genl_info *info)
3540 struct devlink *devlink = info->user_ptr[0];
3541 const char *table_name;
3542 bool counters_enable;
3544 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
3545 GENL_REQ_ATTR_CHECK(info,
3546 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
3549 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3550 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3552 return devlink_dpipe_table_counters_set(devlink, table_name,
3556 static struct devlink_resource *
3557 devlink_resource_find(struct devlink *devlink,
3558 struct devlink_resource *resource, u64 resource_id)
3560 struct list_head *resource_list;
3563 resource_list = &resource->resource_list;
3565 resource_list = &devlink->resource_list;
3567 list_for_each_entry(resource, resource_list, list) {
3568 struct devlink_resource *child_resource;
3570 if (resource->id == resource_id)
3573 child_resource = devlink_resource_find(devlink, resource,
3576 return child_resource;
3582 devlink_resource_validate_children(struct devlink_resource *resource)
3584 struct devlink_resource *child_resource;
3585 bool size_valid = true;
3588 if (list_empty(&resource->resource_list))
3591 list_for_each_entry(child_resource, &resource->resource_list, list)
3592 parts_size += child_resource->size_new;
3594 if (parts_size > resource->size_new)
3597 resource->size_valid = size_valid;
3601 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
3602 struct netlink_ext_ack *extack)
3607 if (size > resource->size_params.size_max) {
3608 NL_SET_ERR_MSG(extack, "Size larger than maximum");
3612 if (size < resource->size_params.size_min) {
3613 NL_SET_ERR_MSG(extack, "Size smaller than minimum");
3617 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
3619 NL_SET_ERR_MSG(extack, "Wrong granularity");
3626 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
3627 struct genl_info *info)
3629 struct devlink *devlink = info->user_ptr[0];
3630 struct devlink_resource *resource;
3635 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_ID) ||
3636 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_SIZE))
3638 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
3640 resource = devlink_resource_find(devlink, NULL, resource_id);
3644 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
3645 err = devlink_resource_validate_size(resource, size, info->extack);
3649 resource->size_new = size;
3650 devlink_resource_validate_children(resource);
3651 if (resource->parent)
3652 devlink_resource_validate_children(resource->parent);
3657 devlink_resource_size_params_put(struct devlink_resource *resource,
3658 struct sk_buff *skb)
3660 struct devlink_resource_size_params *size_params;
3662 size_params = &resource->size_params;
3663 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
3664 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
3665 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
3666 size_params->size_max, DEVLINK_ATTR_PAD) ||
3667 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
3668 size_params->size_min, DEVLINK_ATTR_PAD) ||
3669 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
3674 static int devlink_resource_occ_put(struct devlink_resource *resource,
3675 struct sk_buff *skb)
3677 if (!resource->occ_get)
3679 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
3680 resource->occ_get(resource->occ_get_priv),
3684 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
3685 struct devlink_resource *resource)
3687 struct devlink_resource *child_resource;
3688 struct nlattr *child_resource_attr;
3689 struct nlattr *resource_attr;
3691 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
3695 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
3696 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
3697 DEVLINK_ATTR_PAD) ||
3698 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
3700 goto nla_put_failure;
3701 if (resource->size != resource->size_new &&
3702 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
3703 resource->size_new, DEVLINK_ATTR_PAD))
3704 goto nla_put_failure;
3705 if (devlink_resource_occ_put(resource, skb))
3706 goto nla_put_failure;
3707 if (devlink_resource_size_params_put(resource, skb))
3708 goto nla_put_failure;
3709 if (list_empty(&resource->resource_list))
3712 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
3713 resource->size_valid))
3714 goto nla_put_failure;
3716 child_resource_attr = nla_nest_start_noflag(skb,
3717 DEVLINK_ATTR_RESOURCE_LIST);
3718 if (!child_resource_attr)
3719 goto nla_put_failure;
3721 list_for_each_entry(child_resource, &resource->resource_list, list) {
3722 if (devlink_resource_put(devlink, skb, child_resource))
3723 goto resource_put_failure;
3726 nla_nest_end(skb, child_resource_attr);
3728 nla_nest_end(skb, resource_attr);
3731 resource_put_failure:
3732 nla_nest_cancel(skb, child_resource_attr);
3734 nla_nest_cancel(skb, resource_attr);
3738 static int devlink_resource_fill(struct genl_info *info,
3739 enum devlink_command cmd, int flags)
3741 struct devlink *devlink = info->user_ptr[0];
3742 struct devlink_resource *resource;
3743 struct nlattr *resources_attr;
3744 struct sk_buff *skb = NULL;
3745 struct nlmsghdr *nlh;
3751 resource = list_first_entry(&devlink->resource_list,
3752 struct devlink_resource, list);
3754 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3758 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3759 &devlink_nl_family, NLM_F_MULTI, cmd);
3765 if (devlink_nl_put_handle(skb, devlink))
3766 goto nla_put_failure;
3768 resources_attr = nla_nest_start_noflag(skb,
3769 DEVLINK_ATTR_RESOURCE_LIST);
3770 if (!resources_attr)
3771 goto nla_put_failure;
3775 list_for_each_entry_from(resource, &devlink->resource_list, list) {
3776 err = devlink_resource_put(devlink, skb, resource);
3779 goto err_resource_put;
3785 nla_nest_end(skb, resources_attr);
3786 genlmsg_end(skb, hdr);
3790 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3791 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3793 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3798 return genlmsg_reply(skb, info);
3807 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
3808 struct genl_info *info)
3810 struct devlink *devlink = info->user_ptr[0];
3812 if (list_empty(&devlink->resource_list))
3815 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
3818 int devlink_resources_validate(struct devlink *devlink,
3819 struct devlink_resource *resource,
3820 struct genl_info *info)
3822 struct list_head *resource_list;
3826 resource_list = &resource->resource_list;
3828 resource_list = &devlink->resource_list;
3830 list_for_each_entry(resource, resource_list, list) {
3831 if (!resource->size_valid)
3833 err = devlink_resources_validate(devlink, resource, info);
3840 static const struct devlink_param devlink_param_generic[] = {
3842 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
3843 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
3844 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
3847 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3848 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
3849 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
3852 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
3853 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
3854 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
3857 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
3858 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
3859 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
3862 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
3863 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
3864 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
3867 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
3868 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
3869 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
3872 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
3873 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
3874 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
3877 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3878 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3879 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3882 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3883 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3884 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3887 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3888 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3889 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3892 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
3893 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
3894 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
3897 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
3898 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
3899 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
3902 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
3903 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
3904 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
3907 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
3908 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
3909 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
3912 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
3913 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
3914 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
3917 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
3918 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
3919 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
3922 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
3923 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
3924 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
3928 static int devlink_param_generic_verify(const struct devlink_param *param)
3930 /* verify it match generic parameter by id and name */
3931 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3933 if (strcmp(param->name, devlink_param_generic[param->id].name))
3936 WARN_ON(param->type != devlink_param_generic[param->id].type);
3941 static int devlink_param_driver_verify(const struct devlink_param *param)
3945 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3947 /* verify no such name in generic params */
3948 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3949 if (!strcmp(param->name, devlink_param_generic[i].name))
3955 static struct devlink_param_item *
3956 devlink_param_find_by_name(struct xarray *params, const char *param_name)
3958 struct devlink_param_item *param_item;
3959 unsigned long param_id;
3961 xa_for_each(params, param_id, param_item) {
3962 if (!strcmp(param_item->param->name, param_name))
3968 static struct devlink_param_item *
3969 devlink_param_find_by_id(struct xarray *params, u32 param_id)
3971 return xa_load(params, param_id);
3975 devlink_param_cmode_is_supported(const struct devlink_param *param,
3976 enum devlink_param_cmode cmode)
3978 return test_bit(cmode, ¶m->supported_cmodes);
3981 static int devlink_param_get(struct devlink *devlink,
3982 const struct devlink_param *param,
3983 struct devlink_param_gset_ctx *ctx)
3985 if (!param->get || devlink->reload_failed)
3987 return param->get(devlink, param->id, ctx);
3990 static int devlink_param_set(struct devlink *devlink,
3991 const struct devlink_param *param,
3992 struct devlink_param_gset_ctx *ctx)
3994 if (!param->set || devlink->reload_failed)
3996 return param->set(devlink, param->id, ctx);
4000 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
4002 switch (param_type) {
4003 case DEVLINK_PARAM_TYPE_U8:
4005 case DEVLINK_PARAM_TYPE_U16:
4007 case DEVLINK_PARAM_TYPE_U32:
4009 case DEVLINK_PARAM_TYPE_STRING:
4011 case DEVLINK_PARAM_TYPE_BOOL:
4019 devlink_nl_param_value_fill_one(struct sk_buff *msg,
4020 enum devlink_param_type type,
4021 enum devlink_param_cmode cmode,
4022 union devlink_param_value val)
4024 struct nlattr *param_value_attr;
4026 param_value_attr = nla_nest_start_noflag(msg,
4027 DEVLINK_ATTR_PARAM_VALUE);
4028 if (!param_value_attr)
4029 goto nla_put_failure;
4031 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
4032 goto value_nest_cancel;
4035 case DEVLINK_PARAM_TYPE_U8:
4036 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
4037 goto value_nest_cancel;
4039 case DEVLINK_PARAM_TYPE_U16:
4040 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
4041 goto value_nest_cancel;
4043 case DEVLINK_PARAM_TYPE_U32:
4044 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
4045 goto value_nest_cancel;
4047 case DEVLINK_PARAM_TYPE_STRING:
4048 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
4050 goto value_nest_cancel;
4052 case DEVLINK_PARAM_TYPE_BOOL:
4054 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
4055 goto value_nest_cancel;
4059 nla_nest_end(msg, param_value_attr);
4063 nla_nest_cancel(msg, param_value_attr);
4068 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
4069 unsigned int port_index,
4070 struct devlink_param_item *param_item,
4071 enum devlink_command cmd,
4072 u32 portid, u32 seq, int flags)
4074 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
4075 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
4076 const struct devlink_param *param = param_item->param;
4077 struct devlink_param_gset_ctx ctx;
4078 struct nlattr *param_values_list;
4079 struct nlattr *param_attr;
4085 /* Get value from driver part to driverinit configuration mode */
4086 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4087 if (!devlink_param_cmode_is_supported(param, i))
4089 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4090 if (param_item->driverinit_value_new_valid)
4091 param_value[i] = param_item->driverinit_value_new;
4092 else if (param_item->driverinit_value_valid)
4093 param_value[i] = param_item->driverinit_value;
4098 err = devlink_param_get(devlink, param, &ctx);
4101 param_value[i] = ctx.val;
4103 param_value_set[i] = true;
4106 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4110 if (devlink_nl_put_handle(msg, devlink))
4111 goto genlmsg_cancel;
4113 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
4114 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
4115 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
4116 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
4117 goto genlmsg_cancel;
4119 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
4121 goto genlmsg_cancel;
4122 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
4123 goto param_nest_cancel;
4124 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
4125 goto param_nest_cancel;
4127 nla_type = devlink_param_type_to_nla_type(param->type);
4129 goto param_nest_cancel;
4130 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
4131 goto param_nest_cancel;
4133 param_values_list = nla_nest_start_noflag(msg,
4134 DEVLINK_ATTR_PARAM_VALUES_LIST);
4135 if (!param_values_list)
4136 goto param_nest_cancel;
4138 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4139 if (!param_value_set[i])
4141 err = devlink_nl_param_value_fill_one(msg, param->type,
4144 goto values_list_nest_cancel;
4147 nla_nest_end(msg, param_values_list);
4148 nla_nest_end(msg, param_attr);
4149 genlmsg_end(msg, hdr);
4152 values_list_nest_cancel:
4153 nla_nest_end(msg, param_values_list);
4155 nla_nest_cancel(msg, param_attr);
4157 genlmsg_cancel(msg, hdr);
4161 static void devlink_param_notify(struct devlink *devlink,
4162 unsigned int port_index,
4163 struct devlink_param_item *param_item,
4164 enum devlink_command cmd)
4166 struct sk_buff *msg;
4169 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
4170 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
4171 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
4173 /* devlink_notify_register() / devlink_notify_unregister()
4174 * will replay the notifications if the params are added/removed
4175 * outside of the lifetime of the instance.
4177 if (!devl_is_registered(devlink))
4180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4183 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
4190 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4191 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4195 devlink_nl_cmd_param_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
4196 struct netlink_callback *cb)
4198 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
4199 struct devlink_param_item *param_item;
4200 unsigned long param_id;
4203 xa_for_each_start(&devlink->params, param_id, param_item, state->idx) {
4204 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4205 DEVLINK_CMD_PARAM_GET,
4206 NETLINK_CB(cb->skb).portid,
4209 if (err == -EOPNOTSUPP) {
4212 state->idx = param_id;
4220 const struct devlink_cmd devl_cmd_param_get = {
4221 .dump_one = devlink_nl_cmd_param_get_dump_one,
4225 devlink_param_type_get_from_info(struct genl_info *info,
4226 enum devlink_param_type *param_type)
4228 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
4231 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
4233 *param_type = DEVLINK_PARAM_TYPE_U8;
4236 *param_type = DEVLINK_PARAM_TYPE_U16;
4239 *param_type = DEVLINK_PARAM_TYPE_U32;
4242 *param_type = DEVLINK_PARAM_TYPE_STRING;
4245 *param_type = DEVLINK_PARAM_TYPE_BOOL;
4255 devlink_param_value_get_from_info(const struct devlink_param *param,
4256 struct genl_info *info,
4257 union devlink_param_value *value)
4259 struct nlattr *param_data;
4262 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
4264 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
4267 switch (param->type) {
4268 case DEVLINK_PARAM_TYPE_U8:
4269 if (nla_len(param_data) != sizeof(u8))
4271 value->vu8 = nla_get_u8(param_data);
4273 case DEVLINK_PARAM_TYPE_U16:
4274 if (nla_len(param_data) != sizeof(u16))
4276 value->vu16 = nla_get_u16(param_data);
4278 case DEVLINK_PARAM_TYPE_U32:
4279 if (nla_len(param_data) != sizeof(u32))
4281 value->vu32 = nla_get_u32(param_data);
4283 case DEVLINK_PARAM_TYPE_STRING:
4284 len = strnlen(nla_data(param_data), nla_len(param_data));
4285 if (len == nla_len(param_data) ||
4286 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
4288 strcpy(value->vstr, nla_data(param_data));
4290 case DEVLINK_PARAM_TYPE_BOOL:
4291 if (param_data && nla_len(param_data))
4293 value->vbool = nla_get_flag(param_data);
4299 static struct devlink_param_item *
4300 devlink_param_get_from_info(struct xarray *params, struct genl_info *info)
4304 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
4307 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
4308 return devlink_param_find_by_name(params, param_name);
4311 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
4312 struct genl_info *info)
4314 struct devlink *devlink = info->user_ptr[0];
4315 struct devlink_param_item *param_item;
4316 struct sk_buff *msg;
4319 param_item = devlink_param_get_from_info(&devlink->params, info);
4323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4327 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4328 DEVLINK_CMD_PARAM_GET,
4329 info->snd_portid, info->snd_seq, 0);
4335 return genlmsg_reply(msg, info);
4338 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
4339 unsigned int port_index,
4340 struct xarray *params,
4341 struct genl_info *info,
4342 enum devlink_command cmd)
4344 enum devlink_param_type param_type;
4345 struct devlink_param_gset_ctx ctx;
4346 enum devlink_param_cmode cmode;
4347 struct devlink_param_item *param_item;
4348 const struct devlink_param *param;
4349 union devlink_param_value value;
4352 param_item = devlink_param_get_from_info(params, info);
4355 param = param_item->param;
4356 err = devlink_param_type_get_from_info(info, ¶m_type);
4359 if (param_type != param->type)
4361 err = devlink_param_value_get_from_info(param, info, &value);
4364 if (param->validate) {
4365 err = param->validate(devlink, param->id, value, info->extack);
4370 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
4372 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
4373 if (!devlink_param_cmode_is_supported(param, cmode))
4376 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4377 param_item->driverinit_value_new = value;
4378 param_item->driverinit_value_new_valid = true;
4384 err = devlink_param_set(devlink, param, &ctx);
4389 devlink_param_notify(devlink, port_index, param_item, cmd);
4393 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4394 struct genl_info *info)
4396 struct devlink *devlink = info->user_ptr[0];
4398 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->params,
4399 info, DEVLINK_CMD_PARAM_NEW);
4402 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4403 struct netlink_callback *cb)
4405 NL_SET_ERR_MSG(cb->extack, "Port params are not supported");
4409 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
4410 struct genl_info *info)
4412 NL_SET_ERR_MSG(info->extack, "Port params are not supported");
4416 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
4417 struct genl_info *info)
4419 NL_SET_ERR_MSG(info->extack, "Port params are not supported");
4423 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
4424 struct devlink *devlink,
4425 struct devlink_snapshot *snapshot)
4427 struct nlattr *snap_attr;
4430 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
4434 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
4436 goto nla_put_failure;
4438 nla_nest_end(msg, snap_attr);
4442 nla_nest_cancel(msg, snap_attr);
4446 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
4447 struct devlink *devlink,
4448 struct devlink_region *region)
4450 struct devlink_snapshot *snapshot;
4451 struct nlattr *snapshots_attr;
4454 snapshots_attr = nla_nest_start_noflag(msg,
4455 DEVLINK_ATTR_REGION_SNAPSHOTS);
4456 if (!snapshots_attr)
4459 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
4460 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
4462 goto nla_put_failure;
4465 nla_nest_end(msg, snapshots_attr);
4469 nla_nest_cancel(msg, snapshots_attr);
4473 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
4474 enum devlink_command cmd, u32 portid,
4476 struct devlink_region *region)
4481 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4485 err = devlink_nl_put_handle(msg, devlink);
4487 goto nla_put_failure;
4490 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4491 region->port->index);
4493 goto nla_put_failure;
4496 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
4498 goto nla_put_failure;
4500 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4504 goto nla_put_failure;
4506 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
4507 region->max_snapshots);
4509 goto nla_put_failure;
4511 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
4513 goto nla_put_failure;
4515 genlmsg_end(msg, hdr);
4519 genlmsg_cancel(msg, hdr);
4523 static struct sk_buff *
4524 devlink_nl_region_notify_build(struct devlink_region *region,
4525 struct devlink_snapshot *snapshot,
4526 enum devlink_command cmd, u32 portid, u32 seq)
4528 struct devlink *devlink = region->devlink;
4529 struct sk_buff *msg;
4534 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4536 return ERR_PTR(-ENOMEM);
4538 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
4544 err = devlink_nl_put_handle(msg, devlink);
4546 goto out_cancel_msg;
4549 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4550 region->port->index);
4552 goto out_cancel_msg;
4555 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
4558 goto out_cancel_msg;
4561 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
4564 goto out_cancel_msg;
4566 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4567 region->size, DEVLINK_ATTR_PAD);
4569 goto out_cancel_msg;
4571 genlmsg_end(msg, hdr);
4576 genlmsg_cancel(msg, hdr);
4579 return ERR_PTR(err);
4582 static void devlink_nl_region_notify(struct devlink_region *region,
4583 struct devlink_snapshot *snapshot,
4584 enum devlink_command cmd)
4586 struct devlink *devlink = region->devlink;
4587 struct sk_buff *msg;
4589 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4590 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4593 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4597 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
4598 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4602 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4603 * @devlink: devlink instance
4604 * @id: the snapshot id
4606 * Track when a new snapshot begins using an id. Load the count for the
4607 * given id from the snapshot xarray, increment it, and store it back.
4609 * Called when a new snapshot is created with the given id.
4611 * The id *must* have been previously allocated by
4612 * devlink_region_snapshot_id_get().
4614 * Returns 0 on success, or an error on failure.
4616 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4618 unsigned long count;
4622 xa_lock(&devlink->snapshot_ids);
4623 p = xa_load(&devlink->snapshot_ids, id);
4629 if (WARN_ON(!xa_is_value(p))) {
4634 count = xa_to_value(p);
4637 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4640 xa_unlock(&devlink->snapshot_ids);
4645 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4646 * @devlink: devlink instance
4647 * @id: the snapshot id
4649 * Track when a snapshot is deleted and stops using an id. Load the count
4650 * for the given id from the snapshot xarray, decrement it, and store it
4653 * If the count reaches zero, erase this id from the xarray, freeing it
4654 * up for future re-use by devlink_region_snapshot_id_get().
4656 * Called when a snapshot using the given id is deleted, and when the
4657 * initial allocator of the id is finished using it.
4659 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4661 unsigned long count;
4664 xa_lock(&devlink->snapshot_ids);
4665 p = xa_load(&devlink->snapshot_ids, id);
4669 if (WARN_ON(!xa_is_value(p)))
4672 count = xa_to_value(p);
4676 __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4679 /* If this was the last user, we can erase this id */
4680 __xa_erase(&devlink->snapshot_ids, id);
4683 xa_unlock(&devlink->snapshot_ids);
4687 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4688 * @devlink: devlink instance
4689 * @id: the snapshot id
4691 * Mark the given snapshot id as used by inserting a zero value into the
4694 * This must be called while holding the devlink instance lock. Unlike
4695 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4696 * It is expected that the id will immediately be used before
4697 * releasing the devlink instance lock.
4699 * Returns zero on success, or an error code if the snapshot id could not
4702 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4706 xa_lock(&devlink->snapshot_ids);
4707 if (xa_load(&devlink->snapshot_ids, id)) {
4708 xa_unlock(&devlink->snapshot_ids);
4711 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4713 xa_unlock(&devlink->snapshot_ids);
4718 * __devlink_region_snapshot_id_get - get snapshot ID
4719 * @devlink: devlink instance
4720 * @id: storage to return snapshot id
4722 * Allocates a new snapshot id. Returns zero on success, or a negative
4723 * error on failure. Must be called while holding the devlink instance
4726 * Snapshot IDs are tracked using an xarray which stores the number of
4727 * users of the snapshot id.
4729 * Note that the caller of this function counts as a 'user', in order to
4730 * avoid race conditions. The caller must release its hold on the
4731 * snapshot by using devlink_region_snapshot_id_put.
4733 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4735 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4736 xa_limit_32b, GFP_KERNEL);
4740 * __devlink_region_snapshot_create - create a new snapshot
4741 * This will add a new snapshot of a region. The snapshot
4742 * will be stored on the region struct and can be accessed
4743 * from devlink. This is useful for future analyses of snapshots.
4744 * Multiple snapshots can be created on a region.
4745 * The @snapshot_id should be obtained using the getter function.
4747 * Must be called only while holding the region snapshot lock.
4749 * @region: devlink region of the snapshot
4750 * @data: snapshot data
4751 * @snapshot_id: snapshot id to be created
4754 __devlink_region_snapshot_create(struct devlink_region *region,
4755 u8 *data, u32 snapshot_id)
4757 struct devlink *devlink = region->devlink;
4758 struct devlink_snapshot *snapshot;
4761 lockdep_assert_held(®ion->snapshot_lock);
4763 /* check if region can hold one more snapshot */
4764 if (region->cur_snapshots == region->max_snapshots)
4767 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4770 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4774 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4776 goto err_snapshot_id_increment;
4778 snapshot->id = snapshot_id;
4779 snapshot->region = region;
4780 snapshot->data = data;
4782 list_add_tail(&snapshot->list, ®ion->snapshot_list);
4784 region->cur_snapshots++;
4786 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4789 err_snapshot_id_increment:
4794 static void devlink_region_snapshot_del(struct devlink_region *region,
4795 struct devlink_snapshot *snapshot)
4797 struct devlink *devlink = region->devlink;
4799 lockdep_assert_held(®ion->snapshot_lock);
4801 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4802 region->cur_snapshots--;
4803 list_del(&snapshot->list);
4804 region->ops->destructor(snapshot->data);
4805 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4809 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4810 struct genl_info *info)
4812 struct devlink *devlink = info->user_ptr[0];
4813 struct devlink_port *port = NULL;
4814 struct devlink_region *region;
4815 const char *region_name;
4816 struct sk_buff *msg;
4820 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
4823 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4824 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4826 port = devlink_port_get_by_index(devlink, index);
4831 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4833 region = devlink_port_region_get_by_name(port, region_name);
4835 region = devlink_region_get_by_name(devlink, region_name);
4840 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4844 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4845 info->snd_portid, info->snd_seq, 0,
4852 return genlmsg_reply(msg, info);
4855 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
4856 struct netlink_callback *cb,
4857 struct devlink_port *port,
4861 struct devlink_region *region;
4864 list_for_each_entry(region, &port->region_list, list) {
4869 err = devlink_nl_region_fill(msg, port->devlink,
4870 DEVLINK_CMD_REGION_GET,
4871 NETLINK_CB(cb->skb).portid,
4873 NLM_F_MULTI, region);
4884 devlink_nl_cmd_region_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
4885 struct netlink_callback *cb)
4887 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
4888 struct devlink_region *region;
4889 struct devlink_port *port;
4890 unsigned long port_index;
4894 list_for_each_entry(region, &devlink->region_list, list) {
4895 if (idx < state->idx) {
4899 err = devlink_nl_region_fill(msg, devlink,
4900 DEVLINK_CMD_REGION_GET,
4901 NETLINK_CB(cb->skb).portid,
4903 NLM_F_MULTI, region);
4911 xa_for_each(&devlink->ports, port_index, port) {
4912 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, &idx,
4923 const struct devlink_cmd devl_cmd_region_get = {
4924 .dump_one = devlink_nl_cmd_region_get_dump_one,
4927 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4928 struct genl_info *info)
4930 struct devlink *devlink = info->user_ptr[0];
4931 struct devlink_snapshot *snapshot;
4932 struct devlink_port *port = NULL;
4933 struct devlink_region *region;
4934 const char *region_name;
4938 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
4939 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
4942 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4943 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4945 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4946 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4948 port = devlink_port_get_by_index(devlink, index);
4954 region = devlink_port_region_get_by_name(port, region_name);
4956 region = devlink_region_get_by_name(devlink, region_name);
4961 mutex_lock(®ion->snapshot_lock);
4962 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4964 mutex_unlock(®ion->snapshot_lock);
4968 devlink_region_snapshot_del(region, snapshot);
4969 mutex_unlock(®ion->snapshot_lock);
4974 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4976 struct devlink *devlink = info->user_ptr[0];
4977 struct devlink_snapshot *snapshot;
4978 struct devlink_port *port = NULL;
4979 struct nlattr *snapshot_id_attr;
4980 struct devlink_region *region;
4981 const char *region_name;
4987 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
4988 NL_SET_ERR_MSG(info->extack, "No region name provided");
4992 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4994 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4995 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4997 port = devlink_port_get_by_index(devlink, index);
5003 region = devlink_port_region_get_by_name(port, region_name);
5005 region = devlink_region_get_by_name(devlink, region_name);
5008 NL_SET_ERR_MSG(info->extack, "The requested region does not exist");
5012 if (!region->ops->snapshot) {
5013 NL_SET_ERR_MSG(info->extack, "The requested region does not support taking an immediate snapshot");
5017 mutex_lock(®ion->snapshot_lock);
5019 if (region->cur_snapshots == region->max_snapshots) {
5020 NL_SET_ERR_MSG(info->extack, "The region has reached the maximum number of stored snapshots");
5025 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
5026 if (snapshot_id_attr) {
5027 snapshot_id = nla_get_u32(snapshot_id_attr);
5029 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
5030 NL_SET_ERR_MSG(info->extack, "The requested snapshot id is already in use");
5035 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
5039 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
5041 NL_SET_ERR_MSG(info->extack, "Failed to allocate a new snapshot id");
5047 err = region->port_ops->snapshot(port, region->port_ops,
5048 info->extack, &data);
5050 err = region->ops->snapshot(devlink, region->ops,
5051 info->extack, &data);
5053 goto err_snapshot_capture;
5055 err = __devlink_region_snapshot_create(region, data, snapshot_id);
5057 goto err_snapshot_create;
5059 if (!snapshot_id_attr) {
5060 struct sk_buff *msg;
5062 snapshot = devlink_region_snapshot_get_by_id(region,
5064 if (WARN_ON(!snapshot)) {
5069 msg = devlink_nl_region_notify_build(region, snapshot,
5070 DEVLINK_CMD_REGION_NEW,
5073 err = PTR_ERR_OR_ZERO(msg);
5077 err = genlmsg_reply(msg, info);
5082 mutex_unlock(®ion->snapshot_lock);
5085 err_snapshot_create:
5086 region->ops->destructor(data);
5087 err_snapshot_capture:
5088 __devlink_snapshot_id_decrement(devlink, snapshot_id);
5089 mutex_unlock(®ion->snapshot_lock);
5093 devlink_region_snapshot_del(region, snapshot);
5095 mutex_unlock(®ion->snapshot_lock);
5099 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
5100 u8 *chunk, u32 chunk_size,
5103 struct nlattr *chunk_attr;
5106 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
5110 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
5112 goto nla_put_failure;
5114 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
5117 goto nla_put_failure;
5119 nla_nest_end(msg, chunk_attr);
5123 nla_nest_cancel(msg, chunk_attr);
5127 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
5129 typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
5131 struct netlink_ext_ack *extack);
5134 devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
5135 void *cb_priv, u64 start_offset, u64 end_offset,
5136 u64 *new_offset, struct netlink_ext_ack *extack)
5138 u64 curr_offset = start_offset;
5142 /* Allocate and re-use a single buffer */
5143 data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
5147 *new_offset = start_offset;
5149 while (curr_offset < end_offset) {
5152 data_size = min_t(u32, end_offset - curr_offset,
5153 DEVLINK_REGION_READ_CHUNK_SIZE);
5155 err = cb(cb_priv, data, data_size, curr_offset, extack);
5159 err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset);
5163 curr_offset += data_size;
5165 *new_offset = curr_offset;
5173 devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
5175 struct netlink_ext_ack __always_unused *extack)
5177 struct devlink_snapshot *snapshot = cb_priv;
5179 memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
5185 devlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
5186 u64 curr_offset, struct netlink_ext_ack *extack)
5188 struct devlink_region *region = cb_priv;
5190 return region->port_ops->read(region->port, region->port_ops, extack,
5191 curr_offset, chunk_size, chunk);
5195 devlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
5196 u64 curr_offset, struct netlink_ext_ack *extack)
5198 struct devlink_region *region = cb_priv;
5200 return region->ops->read(region->devlink, region->ops, extack,
5201 curr_offset, chunk_size, chunk);
5204 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
5205 struct netlink_callback *cb)
5207 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5208 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5209 struct nlattr *chunks_attr, *region_attr, *snapshot_attr;
5210 u64 ret_offset, start_offset, end_offset = U64_MAX;
5211 struct nlattr **attrs = info->attrs;
5212 struct devlink_port *port = NULL;
5213 devlink_chunk_fill_t *region_cb;
5214 struct devlink_region *region;
5215 const char *region_name;
5216 struct devlink *devlink;
5218 void *region_cb_priv;
5222 start_offset = state->start_offset;
5224 devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
5225 if (IS_ERR(devlink))
5226 return PTR_ERR(devlink);
5228 if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
5229 NL_SET_ERR_MSG(cb->extack, "No region name provided");
5234 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5235 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5237 port = devlink_port_get_by_index(devlink, index);
5244 region_attr = attrs[DEVLINK_ATTR_REGION_NAME];
5245 region_name = nla_data(region_attr);
5248 region = devlink_port_region_get_by_name(port, region_name);
5250 region = devlink_region_get_by_name(devlink, region_name);
5253 NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist");
5258 snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
5259 if (!snapshot_attr) {
5260 if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
5261 NL_SET_ERR_MSG(cb->extack, "No snapshot id provided");
5266 if (!region->ops->read) {
5267 NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read");
5273 region_cb = &devlink_region_port_direct_fill;
5275 region_cb = &devlink_region_direct_fill;
5276 region_cb_priv = region;
5278 struct devlink_snapshot *snapshot;
5281 if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
5282 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot");
5287 snapshot_id = nla_get_u32(snapshot_attr);
5288 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
5290 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist");
5294 region_cb = &devlink_region_snapshot_fill;
5295 region_cb_priv = snapshot;
5298 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
5299 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
5302 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5304 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5305 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
5308 if (end_offset > region->size)
5309 end_offset = region->size;
5311 /* return 0 if there is no further data to read */
5312 if (start_offset == end_offset) {
5317 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5318 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
5319 DEVLINK_CMD_REGION_READ);
5325 err = devlink_nl_put_handle(skb, devlink);
5327 goto nla_put_failure;
5330 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
5331 region->port->index);
5333 goto nla_put_failure;
5336 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
5338 goto nla_put_failure;
5340 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
5343 goto nla_put_failure;
5346 err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv,
5347 start_offset, end_offset, &ret_offset,
5350 if (err && err != -EMSGSIZE)
5351 goto nla_put_failure;
5353 /* Check if there was any progress done to prevent infinite loop */
5354 if (ret_offset == start_offset) {
5356 goto nla_put_failure;
5359 state->start_offset = ret_offset;
5361 nla_nest_end(skb, chunks_attr);
5362 genlmsg_end(skb, hdr);
5363 devl_unlock(devlink);
5364 devlink_put(devlink);
5368 genlmsg_cancel(skb, hdr);
5370 devl_unlock(devlink);
5371 devlink_put(devlink);
5375 struct devlink_stats {
5376 u64_stats_t rx_bytes;
5377 u64_stats_t rx_packets;
5378 struct u64_stats_sync syncp;
5382 * struct devlink_trap_policer_item - Packet trap policer attributes.
5383 * @policer: Immutable packet trap policer attributes.
5384 * @rate: Rate in packets / sec.
5385 * @burst: Burst size in packets.
5386 * @list: trap_policer_list member.
5388 * Describes packet trap policer attributes. Created by devlink during trap
5389 * policer registration.
5391 struct devlink_trap_policer_item {
5392 const struct devlink_trap_policer *policer;
5395 struct list_head list;
5399 * struct devlink_trap_group_item - Packet trap group attributes.
5400 * @group: Immutable packet trap group attributes.
5401 * @policer_item: Associated policer item. Can be NULL.
5402 * @list: trap_group_list member.
5403 * @stats: Trap group statistics.
5405 * Describes packet trap group attributes. Created by devlink during trap
5406 * group registration.
5408 struct devlink_trap_group_item {
5409 const struct devlink_trap_group *group;
5410 struct devlink_trap_policer_item *policer_item;
5411 struct list_head list;
5412 struct devlink_stats __percpu *stats;
5416 * struct devlink_trap_item - Packet trap attributes.
5417 * @trap: Immutable packet trap attributes.
5418 * @group_item: Associated group item.
5419 * @list: trap_list member.
5420 * @action: Trap action.
5421 * @stats: Trap statistics.
5422 * @priv: Driver private information.
5424 * Describes both mutable and immutable packet trap attributes. Created by
5425 * devlink during trap registration and used for all trap related operations.
5427 struct devlink_trap_item {
5428 const struct devlink_trap *trap;
5429 struct devlink_trap_group_item *group_item;
5430 struct list_head list;
5431 enum devlink_trap_action action;
5432 struct devlink_stats __percpu *stats;
5436 static struct devlink_trap_policer_item *
5437 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
5439 struct devlink_trap_policer_item *policer_item;
5441 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
5442 if (policer_item->policer->id == id)
5443 return policer_item;
5449 static struct devlink_trap_item *
5450 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5452 struct devlink_trap_item *trap_item;
5454 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5455 if (!strcmp(trap_item->trap->name, name))
5462 static struct devlink_trap_item *
5463 devlink_trap_item_get_from_info(struct devlink *devlink,
5464 struct genl_info *info)
5466 struct nlattr *attr;
5468 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5470 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5472 return devlink_trap_item_lookup(devlink, nla_data(attr));
5476 devlink_trap_action_get_from_info(struct genl_info *info,
5477 enum devlink_trap_action *p_trap_action)
5481 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5483 case DEVLINK_TRAP_ACTION_DROP:
5484 case DEVLINK_TRAP_ACTION_TRAP:
5485 case DEVLINK_TRAP_ACTION_MIRROR:
5486 *p_trap_action = val;
5495 static int devlink_trap_metadata_put(struct sk_buff *msg,
5496 const struct devlink_trap *trap)
5498 struct nlattr *attr;
5500 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5504 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5505 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5506 goto nla_put_failure;
5507 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
5508 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
5509 goto nla_put_failure;
5511 nla_nest_end(msg, attr);
5516 nla_nest_cancel(msg, attr);
5520 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5521 struct devlink_stats *stats)
5525 memset(stats, 0, sizeof(*stats));
5526 for_each_possible_cpu(i) {
5527 struct devlink_stats *cpu_stats;
5528 u64 rx_packets, rx_bytes;
5531 cpu_stats = per_cpu_ptr(trap_stats, i);
5533 start = u64_stats_fetch_begin(&cpu_stats->syncp);
5534 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
5535 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
5536 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
5538 u64_stats_add(&stats->rx_packets, rx_packets);
5539 u64_stats_add(&stats->rx_bytes, rx_bytes);
5544 devlink_trap_group_stats_put(struct sk_buff *msg,
5545 struct devlink_stats __percpu *trap_stats)
5547 struct devlink_stats stats;
5548 struct nlattr *attr;
5550 devlink_trap_stats_read(trap_stats, &stats);
5552 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5556 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5557 u64_stats_read(&stats.rx_packets),
5559 goto nla_put_failure;
5561 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5562 u64_stats_read(&stats.rx_bytes),
5564 goto nla_put_failure;
5566 nla_nest_end(msg, attr);
5571 nla_nest_cancel(msg, attr);
5575 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
5576 const struct devlink_trap_item *trap_item)
5578 struct devlink_stats stats;
5579 struct nlattr *attr;
5583 if (devlink->ops->trap_drop_counter_get) {
5584 err = devlink->ops->trap_drop_counter_get(devlink,
5591 devlink_trap_stats_read(trap_item->stats, &stats);
5593 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5597 if (devlink->ops->trap_drop_counter_get &&
5598 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
5600 goto nla_put_failure;
5602 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5603 u64_stats_read(&stats.rx_packets),
5605 goto nla_put_failure;
5607 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5608 u64_stats_read(&stats.rx_bytes),
5610 goto nla_put_failure;
5612 nla_nest_end(msg, attr);
5617 nla_nest_cancel(msg, attr);
5621 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5622 const struct devlink_trap_item *trap_item,
5623 enum devlink_command cmd, u32 portid, u32 seq,
5626 struct devlink_trap_group_item *group_item = trap_item->group_item;
5630 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5634 if (devlink_nl_put_handle(msg, devlink))
5635 goto nla_put_failure;
5637 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5638 group_item->group->name))
5639 goto nla_put_failure;
5641 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5642 goto nla_put_failure;
5644 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5645 goto nla_put_failure;
5647 if (trap_item->trap->generic &&
5648 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5649 goto nla_put_failure;
5651 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5652 goto nla_put_failure;
5654 err = devlink_trap_metadata_put(msg, trap_item->trap);
5656 goto nla_put_failure;
5658 err = devlink_trap_stats_put(msg, devlink, trap_item);
5660 goto nla_put_failure;
5662 genlmsg_end(msg, hdr);
5667 genlmsg_cancel(msg, hdr);
5671 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
5672 struct genl_info *info)
5674 struct netlink_ext_ack *extack = info->extack;
5675 struct devlink *devlink = info->user_ptr[0];
5676 struct devlink_trap_item *trap_item;
5677 struct sk_buff *msg;
5680 if (list_empty(&devlink->trap_list))
5683 trap_item = devlink_trap_item_get_from_info(devlink, info);
5685 NL_SET_ERR_MSG(extack, "Device did not register this trap");
5689 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5693 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5694 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
5699 return genlmsg_reply(msg, info);
5707 devlink_nl_cmd_trap_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
5708 struct netlink_callback *cb)
5710 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5711 struct devlink_trap_item *trap_item;
5715 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5716 if (idx < state->idx) {
5720 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5721 DEVLINK_CMD_TRAP_NEW,
5722 NETLINK_CB(cb->skb).portid,
5735 const struct devlink_cmd devl_cmd_trap_get = {
5736 .dump_one = devlink_nl_cmd_trap_get_dump_one,
5739 static int __devlink_trap_action_set(struct devlink *devlink,
5740 struct devlink_trap_item *trap_item,
5741 enum devlink_trap_action trap_action,
5742 struct netlink_ext_ack *extack)
5746 if (trap_item->action != trap_action &&
5747 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
5748 NL_SET_ERR_MSG(extack, "Cannot change action of non-drop traps. Skipping");
5752 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
5753 trap_action, extack);
5757 trap_item->action = trap_action;
5762 static int devlink_trap_action_set(struct devlink *devlink,
5763 struct devlink_trap_item *trap_item,
5764 struct genl_info *info)
5766 enum devlink_trap_action trap_action;
5769 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5772 err = devlink_trap_action_get_from_info(info, &trap_action);
5774 NL_SET_ERR_MSG(info->extack, "Invalid trap action");
5778 return __devlink_trap_action_set(devlink, trap_item, trap_action,
5782 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
5783 struct genl_info *info)
5785 struct netlink_ext_ack *extack = info->extack;
5786 struct devlink *devlink = info->user_ptr[0];
5787 struct devlink_trap_item *trap_item;
5789 if (list_empty(&devlink->trap_list))
5792 trap_item = devlink_trap_item_get_from_info(devlink, info);
5794 NL_SET_ERR_MSG(extack, "Device did not register this trap");
5798 return devlink_trap_action_set(devlink, trap_item, info);
5801 static struct devlink_trap_group_item *
5802 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
5804 struct devlink_trap_group_item *group_item;
5806 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5807 if (!strcmp(group_item->group->name, name))
5814 static struct devlink_trap_group_item *
5815 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
5817 struct devlink_trap_group_item *group_item;
5819 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5820 if (group_item->group->id == id)
5827 static struct devlink_trap_group_item *
5828 devlink_trap_group_item_get_from_info(struct devlink *devlink,
5829 struct genl_info *info)
5833 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
5835 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
5837 return devlink_trap_group_item_lookup(devlink, name);
5841 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
5842 const struct devlink_trap_group_item *group_item,
5843 enum devlink_command cmd, u32 portid, u32 seq,
5849 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5853 if (devlink_nl_put_handle(msg, devlink))
5854 goto nla_put_failure;
5856 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5857 group_item->group->name))
5858 goto nla_put_failure;
5860 if (group_item->group->generic &&
5861 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5862 goto nla_put_failure;
5864 if (group_item->policer_item &&
5865 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
5866 group_item->policer_item->policer->id))
5867 goto nla_put_failure;
5869 err = devlink_trap_group_stats_put(msg, group_item->stats);
5871 goto nla_put_failure;
5873 genlmsg_end(msg, hdr);
5878 genlmsg_cancel(msg, hdr);
5882 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
5883 struct genl_info *info)
5885 struct netlink_ext_ack *extack = info->extack;
5886 struct devlink *devlink = info->user_ptr[0];
5887 struct devlink_trap_group_item *group_item;
5888 struct sk_buff *msg;
5891 if (list_empty(&devlink->trap_group_list))
5894 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5896 NL_SET_ERR_MSG(extack, "Device did not register this trap group");
5900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5904 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5905 DEVLINK_CMD_TRAP_GROUP_NEW,
5906 info->snd_portid, info->snd_seq, 0);
5908 goto err_trap_group_fill;
5910 return genlmsg_reply(msg, info);
5912 err_trap_group_fill:
5918 devlink_nl_cmd_trap_group_get_dump_one(struct sk_buff *msg,
5919 struct devlink *devlink,
5920 struct netlink_callback *cb)
5922 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5923 struct devlink_trap_group_item *group_item;
5928 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5929 if (idx < state->idx) {
5933 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5934 DEVLINK_CMD_TRAP_GROUP_NEW,
5935 NETLINK_CB(cb->skb).portid,
5948 const struct devlink_cmd devl_cmd_trap_group_get = {
5949 .dump_one = devlink_nl_cmd_trap_group_get_dump_one,
5953 __devlink_trap_group_action_set(struct devlink *devlink,
5954 struct devlink_trap_group_item *group_item,
5955 enum devlink_trap_action trap_action,
5956 struct netlink_ext_ack *extack)
5958 const char *group_name = group_item->group->name;
5959 struct devlink_trap_item *trap_item;
5962 if (devlink->ops->trap_group_action_set) {
5963 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
5964 trap_action, extack);
5968 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5969 if (strcmp(trap_item->group_item->group->name, group_name))
5971 if (trap_item->action != trap_action &&
5972 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
5974 trap_item->action = trap_action;
5980 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5981 if (strcmp(trap_item->group_item->group->name, group_name))
5983 err = __devlink_trap_action_set(devlink, trap_item,
5984 trap_action, extack);
5993 devlink_trap_group_action_set(struct devlink *devlink,
5994 struct devlink_trap_group_item *group_item,
5995 struct genl_info *info, bool *p_modified)
5997 enum devlink_trap_action trap_action;
6000 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6003 err = devlink_trap_action_get_from_info(info, &trap_action);
6005 NL_SET_ERR_MSG(info->extack, "Invalid trap action");
6009 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
6019 static int devlink_trap_group_set(struct devlink *devlink,
6020 struct devlink_trap_group_item *group_item,
6021 struct genl_info *info)
6023 struct devlink_trap_policer_item *policer_item;
6024 struct netlink_ext_ack *extack = info->extack;
6025 const struct devlink_trap_policer *policer;
6026 struct nlattr **attrs = info->attrs;
6030 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6033 if (!devlink->ops->trap_group_set)
6036 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6037 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
6038 if (policer_id && !policer_item) {
6039 NL_SET_ERR_MSG(extack, "Device did not register this trap policer");
6042 policer = policer_item ? policer_item->policer : NULL;
6044 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
6049 group_item->policer_item = policer_item;
6054 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
6055 struct genl_info *info)
6057 struct netlink_ext_ack *extack = info->extack;
6058 struct devlink *devlink = info->user_ptr[0];
6059 struct devlink_trap_group_item *group_item;
6060 bool modified = false;
6063 if (list_empty(&devlink->trap_group_list))
6066 group_item = devlink_trap_group_item_get_from_info(devlink, info);
6068 NL_SET_ERR_MSG(extack, "Device did not register this trap group");
6072 err = devlink_trap_group_action_set(devlink, group_item, info,
6077 err = devlink_trap_group_set(devlink, group_item, info);
6079 goto err_trap_group_set;
6085 NL_SET_ERR_MSG(extack, "Trap group set failed, but some changes were committed already");
6089 static struct devlink_trap_policer_item *
6090 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
6091 struct genl_info *info)
6095 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6097 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6099 return devlink_trap_policer_item_lookup(devlink, id);
6103 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
6104 const struct devlink_trap_policer *policer)
6106 struct nlattr *attr;
6110 if (!devlink->ops->trap_policer_counter_get)
6113 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
6117 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6121 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
6123 goto nla_put_failure;
6125 nla_nest_end(msg, attr);
6130 nla_nest_cancel(msg, attr);
6135 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
6136 const struct devlink_trap_policer_item *policer_item,
6137 enum devlink_command cmd, u32 portid, u32 seq,
6143 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6147 if (devlink_nl_put_handle(msg, devlink))
6148 goto nla_put_failure;
6150 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
6151 policer_item->policer->id))
6152 goto nla_put_failure;
6154 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
6155 policer_item->rate, DEVLINK_ATTR_PAD))
6156 goto nla_put_failure;
6158 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
6159 policer_item->burst, DEVLINK_ATTR_PAD))
6160 goto nla_put_failure;
6162 err = devlink_trap_policer_stats_put(msg, devlink,
6163 policer_item->policer);
6165 goto nla_put_failure;
6167 genlmsg_end(msg, hdr);
6172 genlmsg_cancel(msg, hdr);
6176 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
6177 struct genl_info *info)
6179 struct devlink_trap_policer_item *policer_item;
6180 struct netlink_ext_ack *extack = info->extack;
6181 struct devlink *devlink = info->user_ptr[0];
6182 struct sk_buff *msg;
6185 if (list_empty(&devlink->trap_policer_list))
6188 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
6189 if (!policer_item) {
6190 NL_SET_ERR_MSG(extack, "Device did not register this trap policer");
6194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6198 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
6199 DEVLINK_CMD_TRAP_POLICER_NEW,
6200 info->snd_portid, info->snd_seq, 0);
6202 goto err_trap_policer_fill;
6204 return genlmsg_reply(msg, info);
6206 err_trap_policer_fill:
6212 devlink_nl_cmd_trap_policer_get_dump_one(struct sk_buff *msg,
6213 struct devlink *devlink,
6214 struct netlink_callback *cb)
6216 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6217 struct devlink_trap_policer_item *policer_item;
6221 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6222 if (idx < state->idx) {
6226 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
6227 DEVLINK_CMD_TRAP_POLICER_NEW,
6228 NETLINK_CB(cb->skb).portid,
6241 const struct devlink_cmd devl_cmd_trap_policer_get = {
6242 .dump_one = devlink_nl_cmd_trap_policer_get_dump_one,
6246 devlink_trap_policer_set(struct devlink *devlink,
6247 struct devlink_trap_policer_item *policer_item,
6248 struct genl_info *info)
6250 struct netlink_ext_ack *extack = info->extack;
6251 struct nlattr **attrs = info->attrs;
6255 rate = policer_item->rate;
6256 burst = policer_item->burst;
6258 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
6259 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
6261 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
6262 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
6264 if (rate < policer_item->policer->min_rate) {
6265 NL_SET_ERR_MSG(extack, "Policer rate lower than limit");
6269 if (rate > policer_item->policer->max_rate) {
6270 NL_SET_ERR_MSG(extack, "Policer rate higher than limit");
6274 if (burst < policer_item->policer->min_burst) {
6275 NL_SET_ERR_MSG(extack, "Policer burst size lower than limit");
6279 if (burst > policer_item->policer->max_burst) {
6280 NL_SET_ERR_MSG(extack, "Policer burst size higher than limit");
6284 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
6285 rate, burst, info->extack);
6289 policer_item->rate = rate;
6290 policer_item->burst = burst;
6295 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
6296 struct genl_info *info)
6298 struct devlink_trap_policer_item *policer_item;
6299 struct netlink_ext_ack *extack = info->extack;
6300 struct devlink *devlink = info->user_ptr[0];
6302 if (list_empty(&devlink->trap_policer_list))
6305 if (!devlink->ops->trap_policer_set)
6308 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
6309 if (!policer_item) {
6310 NL_SET_ERR_MSG(extack, "Device did not register this trap policer");
6314 return devlink_trap_policer_set(devlink, policer_item, info);
6317 const struct genl_small_ops devlink_nl_ops[56] = {
6319 .cmd = DEVLINK_CMD_GET,
6320 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6321 .doit = devlink_nl_cmd_get_doit,
6322 .dumpit = devlink_nl_instance_iter_dumpit,
6323 /* can be retrieved by unprivileged users */
6326 .cmd = DEVLINK_CMD_PORT_GET,
6327 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6328 .doit = devlink_nl_cmd_port_get_doit,
6329 .dumpit = devlink_nl_instance_iter_dumpit,
6330 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6331 /* can be retrieved by unprivileged users */
6334 .cmd = DEVLINK_CMD_PORT_SET,
6335 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6336 .doit = devlink_nl_cmd_port_set_doit,
6337 .flags = GENL_ADMIN_PERM,
6338 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6341 .cmd = DEVLINK_CMD_RATE_GET,
6342 .doit = devlink_nl_cmd_rate_get_doit,
6343 .dumpit = devlink_nl_instance_iter_dumpit,
6344 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
6345 /* can be retrieved by unprivileged users */
6348 .cmd = DEVLINK_CMD_RATE_SET,
6349 .doit = devlink_nl_cmd_rate_set_doit,
6350 .flags = GENL_ADMIN_PERM,
6351 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
6354 .cmd = DEVLINK_CMD_RATE_NEW,
6355 .doit = devlink_nl_cmd_rate_new_doit,
6356 .flags = GENL_ADMIN_PERM,
6359 .cmd = DEVLINK_CMD_RATE_DEL,
6360 .doit = devlink_nl_cmd_rate_del_doit,
6361 .flags = GENL_ADMIN_PERM,
6362 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
6365 .cmd = DEVLINK_CMD_PORT_SPLIT,
6366 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6367 .doit = devlink_nl_cmd_port_split_doit,
6368 .flags = GENL_ADMIN_PERM,
6369 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6372 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
6373 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6374 .doit = devlink_nl_cmd_port_unsplit_doit,
6375 .flags = GENL_ADMIN_PERM,
6376 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6379 .cmd = DEVLINK_CMD_PORT_NEW,
6380 .doit = devlink_nl_cmd_port_new_doit,
6381 .flags = GENL_ADMIN_PERM,
6384 .cmd = DEVLINK_CMD_PORT_DEL,
6385 .doit = devlink_nl_cmd_port_del_doit,
6386 .flags = GENL_ADMIN_PERM,
6389 .cmd = DEVLINK_CMD_LINECARD_GET,
6390 .doit = devlink_nl_cmd_linecard_get_doit,
6391 .dumpit = devlink_nl_instance_iter_dumpit,
6392 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
6393 /* can be retrieved by unprivileged users */
6396 .cmd = DEVLINK_CMD_LINECARD_SET,
6397 .doit = devlink_nl_cmd_linecard_set_doit,
6398 .flags = GENL_ADMIN_PERM,
6399 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
6402 .cmd = DEVLINK_CMD_SB_GET,
6403 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6404 .doit = devlink_nl_cmd_sb_get_doit,
6405 .dumpit = devlink_nl_instance_iter_dumpit,
6406 /* can be retrieved by unprivileged users */
6409 .cmd = DEVLINK_CMD_SB_POOL_GET,
6410 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6411 .doit = devlink_nl_cmd_sb_pool_get_doit,
6412 .dumpit = devlink_nl_instance_iter_dumpit,
6413 /* can be retrieved by unprivileged users */
6416 .cmd = DEVLINK_CMD_SB_POOL_SET,
6417 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6418 .doit = devlink_nl_cmd_sb_pool_set_doit,
6419 .flags = GENL_ADMIN_PERM,
6422 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
6423 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6424 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
6425 .dumpit = devlink_nl_instance_iter_dumpit,
6426 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6427 /* can be retrieved by unprivileged users */
6430 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
6431 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6432 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
6433 .flags = GENL_ADMIN_PERM,
6434 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6437 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
6438 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6439 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
6440 .dumpit = devlink_nl_instance_iter_dumpit,
6441 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6442 /* can be retrieved by unprivileged users */
6445 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
6446 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6447 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
6448 .flags = GENL_ADMIN_PERM,
6449 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6452 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
6453 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6454 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
6455 .flags = GENL_ADMIN_PERM,
6458 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
6459 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6460 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
6461 .flags = GENL_ADMIN_PERM,
6464 .cmd = DEVLINK_CMD_ESWITCH_GET,
6465 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6466 .doit = devlink_nl_cmd_eswitch_get_doit,
6467 .flags = GENL_ADMIN_PERM,
6470 .cmd = DEVLINK_CMD_ESWITCH_SET,
6471 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6472 .doit = devlink_nl_cmd_eswitch_set_doit,
6473 .flags = GENL_ADMIN_PERM,
6476 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
6477 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6478 .doit = devlink_nl_cmd_dpipe_table_get,
6479 /* can be retrieved by unprivileged users */
6482 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
6483 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6484 .doit = devlink_nl_cmd_dpipe_entries_get,
6485 /* can be retrieved by unprivileged users */
6488 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
6489 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6490 .doit = devlink_nl_cmd_dpipe_headers_get,
6491 /* can be retrieved by unprivileged users */
6494 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
6495 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6496 .doit = devlink_nl_cmd_dpipe_table_counters_set,
6497 .flags = GENL_ADMIN_PERM,
6500 .cmd = DEVLINK_CMD_RESOURCE_SET,
6501 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6502 .doit = devlink_nl_cmd_resource_set,
6503 .flags = GENL_ADMIN_PERM,
6506 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
6507 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6508 .doit = devlink_nl_cmd_resource_dump,
6509 /* can be retrieved by unprivileged users */
6512 .cmd = DEVLINK_CMD_RELOAD,
6513 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6514 .doit = devlink_nl_cmd_reload,
6515 .flags = GENL_ADMIN_PERM,
6518 .cmd = DEVLINK_CMD_PARAM_GET,
6519 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6520 .doit = devlink_nl_cmd_param_get_doit,
6521 .dumpit = devlink_nl_instance_iter_dumpit,
6522 /* can be retrieved by unprivileged users */
6525 .cmd = DEVLINK_CMD_PARAM_SET,
6526 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6527 .doit = devlink_nl_cmd_param_set_doit,
6528 .flags = GENL_ADMIN_PERM,
6531 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
6532 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6533 .doit = devlink_nl_cmd_port_param_get_doit,
6534 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
6535 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6536 /* can be retrieved by unprivileged users */
6539 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
6540 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6541 .doit = devlink_nl_cmd_port_param_set_doit,
6542 .flags = GENL_ADMIN_PERM,
6543 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6546 .cmd = DEVLINK_CMD_REGION_GET,
6547 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6548 .doit = devlink_nl_cmd_region_get_doit,
6549 .dumpit = devlink_nl_instance_iter_dumpit,
6550 .flags = GENL_ADMIN_PERM,
6553 .cmd = DEVLINK_CMD_REGION_NEW,
6554 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6555 .doit = devlink_nl_cmd_region_new,
6556 .flags = GENL_ADMIN_PERM,
6559 .cmd = DEVLINK_CMD_REGION_DEL,
6560 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6561 .doit = devlink_nl_cmd_region_del,
6562 .flags = GENL_ADMIN_PERM,
6565 .cmd = DEVLINK_CMD_REGION_READ,
6566 .validate = GENL_DONT_VALIDATE_STRICT |
6567 GENL_DONT_VALIDATE_DUMP_STRICT,
6568 .dumpit = devlink_nl_cmd_region_read_dumpit,
6569 .flags = GENL_ADMIN_PERM,
6572 .cmd = DEVLINK_CMD_INFO_GET,
6573 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6574 .doit = devlink_nl_cmd_info_get_doit,
6575 .dumpit = devlink_nl_instance_iter_dumpit,
6576 /* can be retrieved by unprivileged users */
6579 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
6580 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6581 .doit = devlink_nl_cmd_health_reporter_get_doit,
6582 .dumpit = devlink_nl_instance_iter_dumpit,
6583 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6584 /* can be retrieved by unprivileged users */
6587 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
6588 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6589 .doit = devlink_nl_cmd_health_reporter_set_doit,
6590 .flags = GENL_ADMIN_PERM,
6591 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6594 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
6595 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6596 .doit = devlink_nl_cmd_health_reporter_recover_doit,
6597 .flags = GENL_ADMIN_PERM,
6598 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6601 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
6602 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6603 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
6604 .flags = GENL_ADMIN_PERM,
6605 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6608 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
6609 .validate = GENL_DONT_VALIDATE_STRICT |
6610 GENL_DONT_VALIDATE_DUMP_STRICT,
6611 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
6612 .flags = GENL_ADMIN_PERM,
6615 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
6616 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6617 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
6618 .flags = GENL_ADMIN_PERM,
6619 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6622 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
6623 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6624 .doit = devlink_nl_cmd_health_reporter_test_doit,
6625 .flags = GENL_ADMIN_PERM,
6626 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
6629 .cmd = DEVLINK_CMD_FLASH_UPDATE,
6630 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6631 .doit = devlink_nl_cmd_flash_update,
6632 .flags = GENL_ADMIN_PERM,
6635 .cmd = DEVLINK_CMD_TRAP_GET,
6636 .doit = devlink_nl_cmd_trap_get_doit,
6637 .dumpit = devlink_nl_instance_iter_dumpit,
6638 /* can be retrieved by unprivileged users */
6641 .cmd = DEVLINK_CMD_TRAP_SET,
6642 .doit = devlink_nl_cmd_trap_set_doit,
6643 .flags = GENL_ADMIN_PERM,
6646 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
6647 .doit = devlink_nl_cmd_trap_group_get_doit,
6648 .dumpit = devlink_nl_instance_iter_dumpit,
6649 /* can be retrieved by unprivileged users */
6652 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
6653 .doit = devlink_nl_cmd_trap_group_set_doit,
6654 .flags = GENL_ADMIN_PERM,
6657 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
6658 .doit = devlink_nl_cmd_trap_policer_get_doit,
6659 .dumpit = devlink_nl_instance_iter_dumpit,
6660 /* can be retrieved by unprivileged users */
6663 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
6664 .doit = devlink_nl_cmd_trap_policer_set_doit,
6665 .flags = GENL_ADMIN_PERM,
6668 .cmd = DEVLINK_CMD_SELFTESTS_GET,
6669 .doit = devlink_nl_cmd_selftests_get_doit,
6670 .dumpit = devlink_nl_instance_iter_dumpit,
6671 /* can be retrieved by unprivileged users */
6674 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
6675 .doit = devlink_nl_cmd_selftests_run,
6676 .flags = GENL_ADMIN_PERM,
6678 /* -- No new ops here! Use split ops going forward! -- */
6682 devlink_trap_policer_notify(struct devlink *devlink,
6683 const struct devlink_trap_policer_item *policer_item,
6684 enum devlink_command cmd);
6686 devlink_trap_group_notify(struct devlink *devlink,
6687 const struct devlink_trap_group_item *group_item,
6688 enum devlink_command cmd);
6689 static void devlink_trap_notify(struct devlink *devlink,
6690 const struct devlink_trap_item *trap_item,
6691 enum devlink_command cmd);
6693 void devlink_notify_register(struct devlink *devlink)
6695 struct devlink_trap_policer_item *policer_item;
6696 struct devlink_trap_group_item *group_item;
6697 struct devlink_param_item *param_item;
6698 struct devlink_trap_item *trap_item;
6699 struct devlink_port *devlink_port;
6700 struct devlink_linecard *linecard;
6701 struct devlink_rate *rate_node;
6702 struct devlink_region *region;
6703 unsigned long port_index;
6704 unsigned long param_id;
6706 devlink_notify(devlink, DEVLINK_CMD_NEW);
6707 list_for_each_entry(linecard, &devlink->linecard_list, list)
6708 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
6710 xa_for_each(&devlink->ports, port_index, devlink_port)
6711 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6713 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
6714 devlink_trap_policer_notify(devlink, policer_item,
6715 DEVLINK_CMD_TRAP_POLICER_NEW);
6717 list_for_each_entry(group_item, &devlink->trap_group_list, list)
6718 devlink_trap_group_notify(devlink, group_item,
6719 DEVLINK_CMD_TRAP_GROUP_NEW);
6721 list_for_each_entry(trap_item, &devlink->trap_list, list)
6722 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
6724 list_for_each_entry(rate_node, &devlink->rate_list, list)
6725 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
6727 list_for_each_entry(region, &devlink->region_list, list)
6728 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
6730 xa_for_each(&devlink->params, param_id, param_item)
6731 devlink_param_notify(devlink, 0, param_item,
6732 DEVLINK_CMD_PARAM_NEW);
6735 void devlink_notify_unregister(struct devlink *devlink)
6737 struct devlink_trap_policer_item *policer_item;
6738 struct devlink_trap_group_item *group_item;
6739 struct devlink_param_item *param_item;
6740 struct devlink_trap_item *trap_item;
6741 struct devlink_port *devlink_port;
6742 struct devlink_rate *rate_node;
6743 struct devlink_region *region;
6744 unsigned long port_index;
6745 unsigned long param_id;
6747 xa_for_each(&devlink->params, param_id, param_item)
6748 devlink_param_notify(devlink, 0, param_item,
6749 DEVLINK_CMD_PARAM_DEL);
6751 list_for_each_entry_reverse(region, &devlink->region_list, list)
6752 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
6754 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
6755 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
6757 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
6758 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
6760 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
6761 devlink_trap_group_notify(devlink, group_item,
6762 DEVLINK_CMD_TRAP_GROUP_DEL);
6763 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
6765 devlink_trap_policer_notify(devlink, policer_item,
6766 DEVLINK_CMD_TRAP_POLICER_DEL);
6768 xa_for_each(&devlink->ports, port_index, devlink_port)
6769 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
6770 devlink_notify(devlink, DEVLINK_CMD_DEL);
6773 static void devlink_port_type_warn(struct work_struct *work)
6775 WARN(true, "Type was not set for devlink port.");
6778 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
6780 /* Ignore CPU and DSA flavours. */
6781 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
6782 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
6783 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
6786 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
6788 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
6790 if (!devlink_port_type_should_warn(devlink_port))
6792 /* Schedule a work to WARN in case driver does not set port
6793 * type within timeout.
6795 schedule_delayed_work(&devlink_port->type_warn_dw,
6796 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
6799 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
6801 if (!devlink_port_type_should_warn(devlink_port))
6803 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
6807 * devlink_port_init() - Init devlink port
6810 * @devlink_port: devlink port
6812 * Initialize essencial stuff that is needed for functions
6813 * that may be called before devlink port registration.
6814 * Call to this function is optional and not needed
6815 * in case the driver does not use such functions.
6817 void devlink_port_init(struct devlink *devlink,
6818 struct devlink_port *devlink_port)
6820 if (devlink_port->initialized)
6822 devlink_port->devlink = devlink;
6823 INIT_LIST_HEAD(&devlink_port->region_list);
6824 devlink_port->initialized = true;
6826 EXPORT_SYMBOL_GPL(devlink_port_init);
6829 * devlink_port_fini() - Deinitialize devlink port
6831 * @devlink_port: devlink port
6833 * Deinitialize essencial stuff that is in use for functions
6834 * that may be called after devlink port unregistration.
6835 * Call to this function is optional and not needed
6836 * in case the driver does not use such functions.
6838 void devlink_port_fini(struct devlink_port *devlink_port)
6840 WARN_ON(!list_empty(&devlink_port->region_list));
6842 EXPORT_SYMBOL_GPL(devlink_port_fini);
6845 * devl_port_register() - Register devlink port
6848 * @devlink_port: devlink port
6849 * @port_index: driver-specific numerical identifier of the port
6851 * Register devlink port with provided port index. User can use
6852 * any indexing, even hw-related one. devlink_port structure
6853 * is convenient to be embedded inside user driver private structure.
6854 * Note that the caller should take care of zeroing the devlink_port
6857 int devl_port_register(struct devlink *devlink,
6858 struct devlink_port *devlink_port,
6859 unsigned int port_index)
6863 devl_assert_locked(devlink);
6865 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
6867 devlink_port_init(devlink, devlink_port);
6868 devlink_port->registered = true;
6869 devlink_port->index = port_index;
6870 spin_lock_init(&devlink_port->type_lock);
6871 INIT_LIST_HEAD(&devlink_port->reporter_list);
6872 err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
6876 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
6877 devlink_port_type_warn_schedule(devlink_port);
6878 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6881 EXPORT_SYMBOL_GPL(devl_port_register);
6884 * devlink_port_register - Register devlink port
6887 * @devlink_port: devlink port
6888 * @port_index: driver-specific numerical identifier of the port
6890 * Register devlink port with provided port index. User can use
6891 * any indexing, even hw-related one. devlink_port structure
6892 * is convenient to be embedded inside user driver private structure.
6893 * Note that the caller should take care of zeroing the devlink_port
6896 * Context: Takes and release devlink->lock <mutex>.
6898 int devlink_port_register(struct devlink *devlink,
6899 struct devlink_port *devlink_port,
6900 unsigned int port_index)
6905 err = devl_port_register(devlink, devlink_port, port_index);
6906 devl_unlock(devlink);
6909 EXPORT_SYMBOL_GPL(devlink_port_register);
6912 * devl_port_unregister() - Unregister devlink port
6914 * @devlink_port: devlink port
6916 void devl_port_unregister(struct devlink_port *devlink_port)
6918 lockdep_assert_held(&devlink_port->devlink->lock);
6919 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
6921 devlink_port_type_warn_cancel(devlink_port);
6922 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
6923 xa_erase(&devlink_port->devlink->ports, devlink_port->index);
6924 WARN_ON(!list_empty(&devlink_port->reporter_list));
6925 devlink_port->registered = false;
6927 EXPORT_SYMBOL_GPL(devl_port_unregister);
6930 * devlink_port_unregister - Unregister devlink port
6932 * @devlink_port: devlink port
6934 * Context: Takes and release devlink->lock <mutex>.
6936 void devlink_port_unregister(struct devlink_port *devlink_port)
6938 struct devlink *devlink = devlink_port->devlink;
6941 devl_port_unregister(devlink_port);
6942 devl_unlock(devlink);
6944 EXPORT_SYMBOL_GPL(devlink_port_unregister);
6946 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
6947 struct net_device *netdev)
6949 const struct net_device_ops *ops = netdev->netdev_ops;
6951 /* If driver registers devlink port, it should set devlink port
6952 * attributes accordingly so the compat functions are called
6953 * and the original ops are not used.
6955 if (ops->ndo_get_phys_port_name) {
6956 /* Some drivers use the same set of ndos for netdevs
6957 * that have devlink_port registered and also for
6958 * those who don't. Make sure that ndo_get_phys_port_name
6959 * returns -EOPNOTSUPP here in case it is defined.
6962 char name[IFNAMSIZ];
6965 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
6966 WARN_ON(err != -EOPNOTSUPP);
6968 if (ops->ndo_get_port_parent_id) {
6969 /* Some drivers use the same set of ndos for netdevs
6970 * that have devlink_port registered and also for
6971 * those who don't. Make sure that ndo_get_port_parent_id
6972 * returns -EOPNOTSUPP here in case it is defined.
6975 struct netdev_phys_item_id ppid;
6978 err = ops->ndo_get_port_parent_id(netdev, &ppid);
6979 WARN_ON(err != -EOPNOTSUPP);
6983 static void __devlink_port_type_set(struct devlink_port *devlink_port,
6984 enum devlink_port_type type,
6987 struct net_device *netdev = type_dev;
6989 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
6991 if (type == DEVLINK_PORT_TYPE_NOTSET) {
6992 devlink_port_type_warn_schedule(devlink_port);
6994 devlink_port_type_warn_cancel(devlink_port);
6995 if (type == DEVLINK_PORT_TYPE_ETH && netdev)
6996 devlink_port_type_netdev_checks(devlink_port, netdev);
6999 spin_lock_bh(&devlink_port->type_lock);
7000 devlink_port->type = type;
7002 case DEVLINK_PORT_TYPE_ETH:
7003 devlink_port->type_eth.netdev = netdev;
7006 devlink_port->type_eth.ifindex = netdev->ifindex;
7007 BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
7008 sizeof(netdev->name));
7009 strcpy(devlink_port->type_eth.ifname, netdev->name);
7012 case DEVLINK_PORT_TYPE_IB:
7013 devlink_port->type_ib.ibdev = type_dev;
7018 spin_unlock_bh(&devlink_port->type_lock);
7019 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
7023 * devlink_port_type_eth_set - Set port type to Ethernet
7025 * @devlink_port: devlink port
7027 * If driver is calling this, most likely it is doing something wrong.
7029 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
7031 dev_warn(devlink_port->devlink->dev,
7032 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
7033 devlink_port->index);
7034 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
7036 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
7039 * devlink_port_type_ib_set - Set port type to InfiniBand
7041 * @devlink_port: devlink port
7042 * @ibdev: related IB device
7044 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
7045 struct ib_device *ibdev)
7047 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
7049 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
7052 * devlink_port_type_clear - Clear port type
7054 * @devlink_port: devlink port
7056 * If driver is calling this for clearing Ethernet type, most likely
7057 * it is doing something wrong.
7059 void devlink_port_type_clear(struct devlink_port *devlink_port)
7061 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
7062 dev_warn(devlink_port->devlink->dev,
7063 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
7064 devlink_port->index);
7065 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
7067 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
7069 int devlink_port_netdevice_event(struct notifier_block *nb,
7070 unsigned long event, void *ptr)
7072 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
7073 struct devlink_port *devlink_port = netdev->devlink_port;
7074 struct devlink *devlink;
7076 devlink = container_of(nb, struct devlink, netdevice_nb);
7078 if (!devlink_port || devlink_port->devlink != devlink)
7082 case NETDEV_POST_INIT:
7083 /* Set the type but not netdev pointer. It is going to be set
7084 * later on by NETDEV_REGISTER event. Happens once during
7085 * netdevice register
7087 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
7090 case NETDEV_REGISTER:
7091 case NETDEV_CHANGENAME:
7092 if (devlink_net(devlink) != dev_net(netdev))
7094 /* Set the netdev on top of previously set type. Note this
7095 * event happens also during net namespace change so here
7096 * we take into account netdev pointer appearing in this
7099 __devlink_port_type_set(devlink_port, devlink_port->type,
7102 case NETDEV_UNREGISTER:
7103 if (devlink_net(devlink) != dev_net(netdev))
7105 /* Clear netdev pointer, but not the type. This event happens
7106 * also during net namespace change so we need to clear
7107 * pointer to netdev that is going to another net namespace.
7109 __devlink_port_type_set(devlink_port, devlink_port->type,
7112 case NETDEV_PRE_UNINIT:
7113 /* Clear the type and the netdev pointer. Happens one during
7114 * netdevice unregister.
7116 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
7124 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
7125 enum devlink_port_flavour flavour)
7127 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7129 devlink_port->attrs_set = true;
7130 attrs->flavour = flavour;
7131 if (attrs->switch_id.id_len) {
7132 devlink_port->switch_port = true;
7133 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
7134 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
7136 devlink_port->switch_port = false;
7142 * devlink_port_attrs_set - Set port attributes
7144 * @devlink_port: devlink port
7145 * @attrs: devlink port attrs
7147 void devlink_port_attrs_set(struct devlink_port *devlink_port,
7148 struct devlink_port_attrs *attrs)
7152 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
7154 devlink_port->attrs = *attrs;
7155 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
7158 WARN_ON(attrs->splittable && attrs->split);
7160 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
7163 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
7165 * @devlink_port: devlink port
7166 * @controller: associated controller number for the devlink port instance
7167 * @pf: associated PF for the devlink port instance
7168 * @external: indicates if the port is for an external controller
7170 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
7171 u16 pf, bool external)
7173 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7176 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
7178 ret = __devlink_port_attrs_set(devlink_port,
7179 DEVLINK_PORT_FLAVOUR_PCI_PF);
7182 attrs->pci_pf.controller = controller;
7183 attrs->pci_pf.pf = pf;
7184 attrs->pci_pf.external = external;
7186 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
7189 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
7191 * @devlink_port: devlink port
7192 * @controller: associated controller number for the devlink port instance
7193 * @pf: associated PF for the devlink port instance
7194 * @vf: associated VF of a PF for the devlink port instance
7195 * @external: indicates if the port is for an external controller
7197 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
7198 u16 pf, u16 vf, bool external)
7200 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7203 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
7205 ret = __devlink_port_attrs_set(devlink_port,
7206 DEVLINK_PORT_FLAVOUR_PCI_VF);
7209 attrs->pci_vf.controller = controller;
7210 attrs->pci_vf.pf = pf;
7211 attrs->pci_vf.vf = vf;
7212 attrs->pci_vf.external = external;
7214 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
7217 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
7219 * @devlink_port: devlink port
7220 * @controller: associated controller number for the devlink port instance
7221 * @pf: associated PF for the devlink port instance
7222 * @sf: associated SF of a PF for the devlink port instance
7223 * @external: indicates if the port is for an external controller
7225 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
7226 u16 pf, u32 sf, bool external)
7228 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7231 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
7233 ret = __devlink_port_attrs_set(devlink_port,
7234 DEVLINK_PORT_FLAVOUR_PCI_SF);
7237 attrs->pci_sf.controller = controller;
7238 attrs->pci_sf.pf = pf;
7239 attrs->pci_sf.sf = sf;
7240 attrs->pci_sf.external = external;
7242 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
7245 * devl_rate_node_create - create devlink rate node
7246 * @devlink: devlink instance
7247 * @priv: driver private data
7248 * @node_name: name of the resulting node
7249 * @parent: parent devlink_rate struct
7251 * Create devlink rate object of type node
7253 struct devlink_rate *
7254 devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name,
7255 struct devlink_rate *parent)
7257 struct devlink_rate *rate_node;
7259 rate_node = devlink_rate_node_get_by_name(devlink, node_name);
7260 if (!IS_ERR(rate_node))
7261 return ERR_PTR(-EEXIST);
7263 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
7265 return ERR_PTR(-ENOMEM);
7268 rate_node->parent = parent;
7269 refcount_inc(&rate_node->parent->refcnt);
7272 rate_node->type = DEVLINK_RATE_TYPE_NODE;
7273 rate_node->devlink = devlink;
7274 rate_node->priv = priv;
7276 rate_node->name = kstrdup(node_name, GFP_KERNEL);
7277 if (!rate_node->name) {
7279 return ERR_PTR(-ENOMEM);
7282 refcount_set(&rate_node->refcnt, 1);
7283 list_add(&rate_node->list, &devlink->rate_list);
7284 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
7287 EXPORT_SYMBOL_GPL(devl_rate_node_create);
7290 * devl_rate_leaf_create - create devlink rate leaf
7291 * @devlink_port: devlink port object to create rate object on
7292 * @priv: driver private data
7293 * @parent: parent devlink_rate struct
7295 * Create devlink rate object of type leaf on provided @devlink_port.
7297 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,
7298 struct devlink_rate *parent)
7300 struct devlink *devlink = devlink_port->devlink;
7301 struct devlink_rate *devlink_rate;
7303 devl_assert_locked(devlink_port->devlink);
7305 if (WARN_ON(devlink_port->devlink_rate))
7308 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
7313 devlink_rate->parent = parent;
7314 refcount_inc(&devlink_rate->parent->refcnt);
7317 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
7318 devlink_rate->devlink = devlink;
7319 devlink_rate->devlink_port = devlink_port;
7320 devlink_rate->priv = priv;
7321 list_add_tail(&devlink_rate->list, &devlink->rate_list);
7322 devlink_port->devlink_rate = devlink_rate;
7323 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
7327 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
7330 * devl_rate_leaf_destroy - destroy devlink rate leaf
7332 * @devlink_port: devlink port linked to the rate object
7334 * Destroy the devlink rate object of type leaf on provided @devlink_port.
7336 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
7338 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
7340 devl_assert_locked(devlink_port->devlink);
7344 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
7345 if (devlink_rate->parent)
7346 refcount_dec(&devlink_rate->parent->refcnt);
7347 list_del(&devlink_rate->list);
7348 devlink_port->devlink_rate = NULL;
7349 kfree(devlink_rate);
7351 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
7354 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
7355 * @devlink: devlink instance
7357 * Unset parent for all rate objects and destroy all rate nodes
7358 * on specified device.
7360 void devl_rate_nodes_destroy(struct devlink *devlink)
7362 static struct devlink_rate *devlink_rate, *tmp;
7363 const struct devlink_ops *ops = devlink->ops;
7365 devl_assert_locked(devlink);
7367 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
7368 if (!devlink_rate->parent)
7371 refcount_dec(&devlink_rate->parent->refcnt);
7372 if (devlink_rate_is_leaf(devlink_rate))
7373 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
7375 else if (devlink_rate_is_node(devlink_rate))
7376 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
7379 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
7380 if (devlink_rate_is_node(devlink_rate)) {
7381 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
7382 list_del(&devlink_rate->list);
7383 kfree(devlink_rate->name);
7384 kfree(devlink_rate);
7388 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
7391 * devlink_port_linecard_set - Link port with a linecard
7393 * @devlink_port: devlink port
7394 * @linecard: devlink linecard
7396 void devlink_port_linecard_set(struct devlink_port *devlink_port,
7397 struct devlink_linecard *linecard)
7399 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
7401 devlink_port->linecard = linecard;
7403 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
7405 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
7406 char *name, size_t len)
7408 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7411 if (!devlink_port->attrs_set)
7414 switch (attrs->flavour) {
7415 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
7416 if (devlink_port->linecard)
7417 n = snprintf(name, len, "l%u",
7418 devlink_port->linecard->index);
7420 n += snprintf(name + n, len - n, "p%u",
7421 attrs->phys.port_number);
7422 if (n < len && attrs->split)
7423 n += snprintf(name + n, len - n, "s%u",
7424 attrs->phys.split_subport_number);
7426 case DEVLINK_PORT_FLAVOUR_CPU:
7427 case DEVLINK_PORT_FLAVOUR_DSA:
7428 case DEVLINK_PORT_FLAVOUR_UNUSED:
7429 /* As CPU and DSA ports do not have a netdevice associated
7430 * case should not ever happen.
7434 case DEVLINK_PORT_FLAVOUR_PCI_PF:
7435 if (attrs->pci_pf.external) {
7436 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
7442 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
7444 case DEVLINK_PORT_FLAVOUR_PCI_VF:
7445 if (attrs->pci_vf.external) {
7446 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
7452 n = snprintf(name, len, "pf%uvf%u",
7453 attrs->pci_vf.pf, attrs->pci_vf.vf);
7455 case DEVLINK_PORT_FLAVOUR_PCI_SF:
7456 if (attrs->pci_sf.external) {
7457 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
7463 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
7466 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
7476 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
7478 struct devlink_linecard_type *linecard_type;
7482 count = linecard->ops->types_count(linecard, linecard->priv);
7483 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
7485 if (!linecard->types)
7487 linecard->types_count = count;
7489 for (i = 0; i < count; i++) {
7490 linecard_type = &linecard->types[i];
7491 linecard->ops->types_get(linecard, linecard->priv, i,
7492 &linecard_type->type,
7493 &linecard_type->priv);
7498 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
7500 kfree(linecard->types);
7504 * devl_linecard_create - Create devlink linecard
7507 * @linecard_index: driver-specific numerical identifier of the linecard
7508 * @ops: linecards ops
7509 * @priv: user priv pointer
7511 * Create devlink linecard instance with provided linecard index.
7512 * Caller can use any indexing, even hw-related one.
7514 * Return: Line card structure or an ERR_PTR() encoded error code.
7516 struct devlink_linecard *
7517 devl_linecard_create(struct devlink *devlink, unsigned int linecard_index,
7518 const struct devlink_linecard_ops *ops, void *priv)
7520 struct devlink_linecard *linecard;
7523 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
7524 !ops->types_count || !ops->types_get))
7525 return ERR_PTR(-EINVAL);
7527 if (devlink_linecard_index_exists(devlink, linecard_index))
7528 return ERR_PTR(-EEXIST);
7530 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
7532 return ERR_PTR(-ENOMEM);
7534 linecard->devlink = devlink;
7535 linecard->index = linecard_index;
7536 linecard->ops = ops;
7537 linecard->priv = priv;
7538 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
7539 mutex_init(&linecard->state_lock);
7541 err = devlink_linecard_types_init(linecard);
7543 mutex_destroy(&linecard->state_lock);
7545 return ERR_PTR(err);
7548 list_add_tail(&linecard->list, &devlink->linecard_list);
7549 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7552 EXPORT_SYMBOL_GPL(devl_linecard_create);
7555 * devl_linecard_destroy - Destroy devlink linecard
7557 * @linecard: devlink linecard
7559 void devl_linecard_destroy(struct devlink_linecard *linecard)
7561 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
7562 list_del(&linecard->list);
7563 devlink_linecard_types_fini(linecard);
7564 mutex_destroy(&linecard->state_lock);
7567 EXPORT_SYMBOL_GPL(devl_linecard_destroy);
7570 * devlink_linecard_provision_set - Set provisioning on linecard
7572 * @linecard: devlink linecard
7573 * @type: linecard type
7575 * This is either called directly from the provision() op call or
7576 * as a result of the provision() op call asynchronously.
7578 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
7581 mutex_lock(&linecard->state_lock);
7582 WARN_ON(linecard->type && strcmp(linecard->type, type));
7583 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
7584 linecard->type = type;
7585 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7586 mutex_unlock(&linecard->state_lock);
7588 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
7591 * devlink_linecard_provision_clear - Clear provisioning on linecard
7593 * @linecard: devlink linecard
7595 * This is either called directly from the unprovision() op call or
7596 * as a result of the unprovision() op call asynchronously.
7598 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
7600 mutex_lock(&linecard->state_lock);
7601 WARN_ON(linecard->nested_devlink);
7602 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
7603 linecard->type = NULL;
7604 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7605 mutex_unlock(&linecard->state_lock);
7607 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
7610 * devlink_linecard_provision_fail - Fail provisioning on linecard
7612 * @linecard: devlink linecard
7614 * This is either called directly from the provision() op call or
7615 * as a result of the provision() op call asynchronously.
7617 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
7619 mutex_lock(&linecard->state_lock);
7620 WARN_ON(linecard->nested_devlink);
7621 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
7622 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7623 mutex_unlock(&linecard->state_lock);
7625 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
7628 * devlink_linecard_activate - Set linecard active
7630 * @linecard: devlink linecard
7632 void devlink_linecard_activate(struct devlink_linecard *linecard)
7634 mutex_lock(&linecard->state_lock);
7635 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
7636 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
7637 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7638 mutex_unlock(&linecard->state_lock);
7640 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
7643 * devlink_linecard_deactivate - Set linecard inactive
7645 * @linecard: devlink linecard
7647 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
7649 mutex_lock(&linecard->state_lock);
7650 switch (linecard->state) {
7651 case DEVLINK_LINECARD_STATE_ACTIVE:
7652 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
7653 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7655 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
7656 /* Line card is being deactivated as part
7657 * of unprovisioning flow.
7664 mutex_unlock(&linecard->state_lock);
7666 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
7669 * devlink_linecard_nested_dl_set - Attach/detach nested devlink
7670 * instance to linecard.
7672 * @linecard: devlink linecard
7673 * @nested_devlink: devlink instance to attach or NULL to detach
7675 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
7676 struct devlink *nested_devlink)
7678 mutex_lock(&linecard->state_lock);
7679 linecard->nested_devlink = nested_devlink;
7680 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
7681 mutex_unlock(&linecard->state_lock);
7683 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
7685 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
7686 u32 size, u16 ingress_pools_count,
7687 u16 egress_pools_count, u16 ingress_tc_count,
7688 u16 egress_tc_count)
7690 struct devlink_sb *devlink_sb;
7692 lockdep_assert_held(&devlink->lock);
7694 if (devlink_sb_index_exists(devlink, sb_index))
7697 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
7700 devlink_sb->index = sb_index;
7701 devlink_sb->size = size;
7702 devlink_sb->ingress_pools_count = ingress_pools_count;
7703 devlink_sb->egress_pools_count = egress_pools_count;
7704 devlink_sb->ingress_tc_count = ingress_tc_count;
7705 devlink_sb->egress_tc_count = egress_tc_count;
7706 list_add_tail(&devlink_sb->list, &devlink->sb_list);
7709 EXPORT_SYMBOL_GPL(devl_sb_register);
7711 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
7712 u32 size, u16 ingress_pools_count,
7713 u16 egress_pools_count, u16 ingress_tc_count,
7714 u16 egress_tc_count)
7719 err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
7720 egress_pools_count, ingress_tc_count,
7722 devl_unlock(devlink);
7725 EXPORT_SYMBOL_GPL(devlink_sb_register);
7727 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
7729 struct devlink_sb *devlink_sb;
7731 lockdep_assert_held(&devlink->lock);
7733 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
7734 WARN_ON(!devlink_sb);
7735 list_del(&devlink_sb->list);
7738 EXPORT_SYMBOL_GPL(devl_sb_unregister);
7740 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
7743 devl_sb_unregister(devlink, sb_index);
7744 devl_unlock(devlink);
7746 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
7749 * devl_dpipe_headers_register - register dpipe headers
7752 * @dpipe_headers: dpipe header array
7754 * Register the headers supported by hardware.
7756 void devl_dpipe_headers_register(struct devlink *devlink,
7757 struct devlink_dpipe_headers *dpipe_headers)
7759 lockdep_assert_held(&devlink->lock);
7761 devlink->dpipe_headers = dpipe_headers;
7763 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
7766 * devl_dpipe_headers_unregister - unregister dpipe headers
7770 * Unregister the headers supported by hardware.
7772 void devl_dpipe_headers_unregister(struct devlink *devlink)
7774 lockdep_assert_held(&devlink->lock);
7776 devlink->dpipe_headers = NULL;
7778 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
7781 * devlink_dpipe_table_counter_enabled - check if counter allocation
7784 * @table_name: tables name
7786 * Used by driver to check if counter allocation is required.
7787 * After counter allocation is turned on the table entries
7788 * are updated to include counter statistics.
7790 * After that point on the driver must respect the counter
7791 * state so that each entry added to the table is added
7794 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
7795 const char *table_name)
7797 struct devlink_dpipe_table *table;
7801 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
7802 table_name, devlink);
7805 enabled = table->counters_enabled;
7809 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
7812 * devl_dpipe_table_register - register dpipe table
7815 * @table_name: table name
7816 * @table_ops: table ops
7818 * @counter_control_extern: external control for counters
7820 int devl_dpipe_table_register(struct devlink *devlink,
7821 const char *table_name,
7822 struct devlink_dpipe_table_ops *table_ops,
7823 void *priv, bool counter_control_extern)
7825 struct devlink_dpipe_table *table;
7827 lockdep_assert_held(&devlink->lock);
7829 if (WARN_ON(!table_ops->size_get))
7832 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
7836 table = kzalloc(sizeof(*table), GFP_KERNEL);
7840 table->name = table_name;
7841 table->table_ops = table_ops;
7843 table->counter_control_extern = counter_control_extern;
7845 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
7849 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
7852 * devl_dpipe_table_unregister - unregister dpipe table
7855 * @table_name: table name
7857 void devl_dpipe_table_unregister(struct devlink *devlink,
7858 const char *table_name)
7860 struct devlink_dpipe_table *table;
7862 lockdep_assert_held(&devlink->lock);
7864 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
7865 table_name, devlink);
7868 list_del_rcu(&table->list);
7869 kfree_rcu(table, rcu);
7871 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
7874 * devl_resource_register - devlink resource register
7877 * @resource_name: resource's name
7878 * @resource_size: resource's size
7879 * @resource_id: resource's id
7880 * @parent_resource_id: resource's parent id
7881 * @size_params: size parameters
7883 * Generic resources should reuse the same names across drivers.
7884 * Please see the generic resources list at:
7885 * Documentation/networking/devlink/devlink-resource.rst
7887 int devl_resource_register(struct devlink *devlink,
7888 const char *resource_name,
7891 u64 parent_resource_id,
7892 const struct devlink_resource_size_params *size_params)
7894 struct devlink_resource *resource;
7895 struct list_head *resource_list;
7898 lockdep_assert_held(&devlink->lock);
7900 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
7902 resource = devlink_resource_find(devlink, NULL, resource_id);
7906 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
7910 if (top_hierarchy) {
7911 resource_list = &devlink->resource_list;
7913 struct devlink_resource *parent_resource;
7915 parent_resource = devlink_resource_find(devlink, NULL,
7916 parent_resource_id);
7917 if (parent_resource) {
7918 resource_list = &parent_resource->resource_list;
7919 resource->parent = parent_resource;
7926 resource->name = resource_name;
7927 resource->size = resource_size;
7928 resource->size_new = resource_size;
7929 resource->id = resource_id;
7930 resource->size_valid = true;
7931 memcpy(&resource->size_params, size_params,
7932 sizeof(resource->size_params));
7933 INIT_LIST_HEAD(&resource->resource_list);
7934 list_add_tail(&resource->list, resource_list);
7938 EXPORT_SYMBOL_GPL(devl_resource_register);
7941 * devlink_resource_register - devlink resource register
7944 * @resource_name: resource's name
7945 * @resource_size: resource's size
7946 * @resource_id: resource's id
7947 * @parent_resource_id: resource's parent id
7948 * @size_params: size parameters
7950 * Generic resources should reuse the same names across drivers.
7951 * Please see the generic resources list at:
7952 * Documentation/networking/devlink/devlink-resource.rst
7954 * Context: Takes and release devlink->lock <mutex>.
7956 int devlink_resource_register(struct devlink *devlink,
7957 const char *resource_name,
7960 u64 parent_resource_id,
7961 const struct devlink_resource_size_params *size_params)
7966 err = devl_resource_register(devlink, resource_name, resource_size,
7967 resource_id, parent_resource_id, size_params);
7968 devl_unlock(devlink);
7971 EXPORT_SYMBOL_GPL(devlink_resource_register);
7973 static void devlink_resource_unregister(struct devlink *devlink,
7974 struct devlink_resource *resource)
7976 struct devlink_resource *tmp, *child_resource;
7978 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
7980 devlink_resource_unregister(devlink, child_resource);
7981 list_del(&child_resource->list);
7982 kfree(child_resource);
7987 * devl_resources_unregister - free all resources
7991 void devl_resources_unregister(struct devlink *devlink)
7993 struct devlink_resource *tmp, *child_resource;
7995 lockdep_assert_held(&devlink->lock);
7997 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
7999 devlink_resource_unregister(devlink, child_resource);
8000 list_del(&child_resource->list);
8001 kfree(child_resource);
8004 EXPORT_SYMBOL_GPL(devl_resources_unregister);
8007 * devlink_resources_unregister - free all resources
8011 * Context: Takes and release devlink->lock <mutex>.
8013 void devlink_resources_unregister(struct devlink *devlink)
8016 devl_resources_unregister(devlink);
8017 devl_unlock(devlink);
8019 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8022 * devl_resource_size_get - get and update size
8025 * @resource_id: the requested resource id
8026 * @p_resource_size: ptr to update
8028 int devl_resource_size_get(struct devlink *devlink,
8030 u64 *p_resource_size)
8032 struct devlink_resource *resource;
8034 lockdep_assert_held(&devlink->lock);
8036 resource = devlink_resource_find(devlink, NULL, resource_id);
8039 *p_resource_size = resource->size_new;
8040 resource->size = resource->size_new;
8043 EXPORT_SYMBOL_GPL(devl_resource_size_get);
8046 * devl_dpipe_table_resource_set - set the resource id
8049 * @table_name: table name
8050 * @resource_id: resource id
8051 * @resource_units: number of resource's units consumed per table's entry
8053 int devl_dpipe_table_resource_set(struct devlink *devlink,
8054 const char *table_name, u64 resource_id,
8057 struct devlink_dpipe_table *table;
8059 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8060 table_name, devlink);
8064 table->resource_id = resource_id;
8065 table->resource_units = resource_units;
8066 table->resource_valid = true;
8069 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
8072 * devl_resource_occ_get_register - register occupancy getter
8075 * @resource_id: resource id
8076 * @occ_get: occupancy getter callback
8077 * @occ_get_priv: occupancy getter callback priv
8079 void devl_resource_occ_get_register(struct devlink *devlink,
8081 devlink_resource_occ_get_t *occ_get,
8084 struct devlink_resource *resource;
8086 lockdep_assert_held(&devlink->lock);
8088 resource = devlink_resource_find(devlink, NULL, resource_id);
8089 if (WARN_ON(!resource))
8091 WARN_ON(resource->occ_get);
8093 resource->occ_get = occ_get;
8094 resource->occ_get_priv = occ_get_priv;
8096 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
8099 * devlink_resource_occ_get_register - register occupancy getter
8102 * @resource_id: resource id
8103 * @occ_get: occupancy getter callback
8104 * @occ_get_priv: occupancy getter callback priv
8106 * Context: Takes and release devlink->lock <mutex>.
8108 void devlink_resource_occ_get_register(struct devlink *devlink,
8110 devlink_resource_occ_get_t *occ_get,
8114 devl_resource_occ_get_register(devlink, resource_id,
8115 occ_get, occ_get_priv);
8116 devl_unlock(devlink);
8118 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8121 * devl_resource_occ_get_unregister - unregister occupancy getter
8124 * @resource_id: resource id
8126 void devl_resource_occ_get_unregister(struct devlink *devlink,
8129 struct devlink_resource *resource;
8131 lockdep_assert_held(&devlink->lock);
8133 resource = devlink_resource_find(devlink, NULL, resource_id);
8134 if (WARN_ON(!resource))
8136 WARN_ON(!resource->occ_get);
8138 resource->occ_get = NULL;
8139 resource->occ_get_priv = NULL;
8141 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
8144 * devlink_resource_occ_get_unregister - unregister occupancy getter
8147 * @resource_id: resource id
8149 * Context: Takes and release devlink->lock <mutex>.
8151 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8155 devl_resource_occ_get_unregister(devlink, resource_id);
8156 devl_unlock(devlink);
8158 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8160 static int devlink_param_verify(const struct devlink_param *param)
8162 if (!param || !param->name || !param->supported_cmodes)
8165 return devlink_param_generic_verify(param);
8167 return devlink_param_driver_verify(param);
8170 static int devlink_param_register(struct devlink *devlink,
8171 const struct devlink_param *param)
8173 struct devlink_param_item *param_item;
8176 WARN_ON(devlink_param_verify(param));
8177 WARN_ON(devlink_param_find_by_name(&devlink->params, param->name));
8179 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
8180 WARN_ON(param->get || param->set);
8182 WARN_ON(!param->get || !param->set);
8184 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
8188 param_item->param = param;
8190 err = xa_insert(&devlink->params, param->id, param_item, GFP_KERNEL);
8194 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
8202 static void devlink_param_unregister(struct devlink *devlink,
8203 const struct devlink_param *param)
8205 struct devlink_param_item *param_item;
8207 param_item = devlink_param_find_by_id(&devlink->params, param->id);
8208 if (WARN_ON(!param_item))
8210 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_DEL);
8211 xa_erase(&devlink->params, param->id);
8216 * devl_params_register - register configuration parameters
8219 * @params: configuration parameters array
8220 * @params_count: number of parameters provided
8222 * Register the configuration parameters supported by the driver.
8224 int devl_params_register(struct devlink *devlink,
8225 const struct devlink_param *params,
8226 size_t params_count)
8228 const struct devlink_param *param = params;
8231 lockdep_assert_held(&devlink->lock);
8233 for (i = 0; i < params_count; i++, param++) {
8234 err = devlink_param_register(devlink, param);
8244 for (param--; i > 0; i--, param--)
8245 devlink_param_unregister(devlink, param);
8248 EXPORT_SYMBOL_GPL(devl_params_register);
8250 int devlink_params_register(struct devlink *devlink,
8251 const struct devlink_param *params,
8252 size_t params_count)
8257 err = devl_params_register(devlink, params, params_count);
8258 devl_unlock(devlink);
8261 EXPORT_SYMBOL_GPL(devlink_params_register);
8264 * devl_params_unregister - unregister configuration parameters
8266 * @params: configuration parameters to unregister
8267 * @params_count: number of parameters provided
8269 void devl_params_unregister(struct devlink *devlink,
8270 const struct devlink_param *params,
8271 size_t params_count)
8273 const struct devlink_param *param = params;
8276 lockdep_assert_held(&devlink->lock);
8278 for (i = 0; i < params_count; i++, param++)
8279 devlink_param_unregister(devlink, param);
8281 EXPORT_SYMBOL_GPL(devl_params_unregister);
8283 void devlink_params_unregister(struct devlink *devlink,
8284 const struct devlink_param *params,
8285 size_t params_count)
8288 devl_params_unregister(devlink, params, params_count);
8289 devl_unlock(devlink);
8291 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8294 * devl_param_driverinit_value_get - get configuration parameter
8295 * value for driver initializing
8298 * @param_id: parameter ID
8299 * @val: pointer to store the value of parameter in driverinit
8300 * configuration mode
8302 * This function should be used by the driver to get driverinit
8303 * configuration for initialization after reload command.
8305 * Note that lockless call of this function relies on the
8306 * driver to maintain following basic sane behavior:
8307 * 1) Driver ensures a call to this function cannot race with
8308 * registering/unregistering the parameter with the same parameter ID.
8309 * 2) Driver ensures a call to this function cannot race with
8310 * devl_param_driverinit_value_set() call with the same parameter ID.
8311 * 3) Driver ensures a call to this function cannot race with
8313 * If the driver is not able to comply, it has to take the devlink->lock
8314 * while calling this.
8316 int devl_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
8317 union devlink_param_value *val)
8319 struct devlink_param_item *param_item;
8321 if (WARN_ON(!devlink_reload_supported(devlink->ops)))
8324 param_item = devlink_param_find_by_id(&devlink->params, param_id);
8328 if (!param_item->driverinit_value_valid)
8331 if (WARN_ON(!devlink_param_cmode_is_supported(param_item->param,
8332 DEVLINK_PARAM_CMODE_DRIVERINIT)))
8335 *val = param_item->driverinit_value;
8339 EXPORT_SYMBOL_GPL(devl_param_driverinit_value_get);
8342 * devl_param_driverinit_value_set - set value of configuration
8343 * parameter for driverinit
8344 * configuration mode
8347 * @param_id: parameter ID
8348 * @init_val: value of parameter to set for driverinit configuration mode
8350 * This function should be used by the driver to set driverinit
8351 * configuration mode default value.
8353 void devl_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
8354 union devlink_param_value init_val)
8356 struct devlink_param_item *param_item;
8358 devl_assert_locked(devlink);
8360 param_item = devlink_param_find_by_id(&devlink->params, param_id);
8361 if (WARN_ON(!param_item))
8364 if (WARN_ON(!devlink_param_cmode_is_supported(param_item->param,
8365 DEVLINK_PARAM_CMODE_DRIVERINIT)))
8368 param_item->driverinit_value = init_val;
8369 param_item->driverinit_value_valid = true;
8371 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
8373 EXPORT_SYMBOL_GPL(devl_param_driverinit_value_set);
8375 void devlink_params_driverinit_load_new(struct devlink *devlink)
8377 struct devlink_param_item *param_item;
8378 unsigned long param_id;
8380 xa_for_each(&devlink->params, param_id, param_item) {
8381 if (!devlink_param_cmode_is_supported(param_item->param,
8382 DEVLINK_PARAM_CMODE_DRIVERINIT) ||
8383 !param_item->driverinit_value_new_valid)
8385 param_item->driverinit_value = param_item->driverinit_value_new;
8386 param_item->driverinit_value_valid = true;
8387 param_item->driverinit_value_new_valid = false;
8392 * devl_param_value_changed - notify devlink on a parameter's value
8393 * change. Should be called by the driver
8394 * right after the change.
8397 * @param_id: parameter ID
8399 * This function should be used by the driver to notify devlink on value
8400 * change, excluding driverinit configuration mode.
8401 * For driverinit configuration mode driver should use the function
8403 void devl_param_value_changed(struct devlink *devlink, u32 param_id)
8405 struct devlink_param_item *param_item;
8407 param_item = devlink_param_find_by_id(&devlink->params, param_id);
8408 WARN_ON(!param_item);
8410 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
8412 EXPORT_SYMBOL_GPL(devl_param_value_changed);
8415 * devl_region_create - create a new address region
8418 * @ops: region operations and name
8419 * @region_max_snapshots: Maximum supported number of snapshots for region
8420 * @region_size: size of region
8422 struct devlink_region *devl_region_create(struct devlink *devlink,
8423 const struct devlink_region_ops *ops,
8424 u32 region_max_snapshots,
8427 struct devlink_region *region;
8429 devl_assert_locked(devlink);
8431 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
8432 return ERR_PTR(-EINVAL);
8434 if (devlink_region_get_by_name(devlink, ops->name))
8435 return ERR_PTR(-EEXIST);
8437 region = kzalloc(sizeof(*region), GFP_KERNEL);
8439 return ERR_PTR(-ENOMEM);
8441 region->devlink = devlink;
8442 region->max_snapshots = region_max_snapshots;
8444 region->size = region_size;
8445 INIT_LIST_HEAD(®ion->snapshot_list);
8446 mutex_init(®ion->snapshot_lock);
8447 list_add_tail(®ion->list, &devlink->region_list);
8448 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
8452 EXPORT_SYMBOL_GPL(devl_region_create);
8455 * devlink_region_create - create a new address region
8458 * @ops: region operations and name
8459 * @region_max_snapshots: Maximum supported number of snapshots for region
8460 * @region_size: size of region
8462 * Context: Takes and release devlink->lock <mutex>.
8464 struct devlink_region *
8465 devlink_region_create(struct devlink *devlink,
8466 const struct devlink_region_ops *ops,
8467 u32 region_max_snapshots, u64 region_size)
8469 struct devlink_region *region;
8472 region = devl_region_create(devlink, ops, region_max_snapshots,
8474 devl_unlock(devlink);
8477 EXPORT_SYMBOL_GPL(devlink_region_create);
8480 * devlink_port_region_create - create a new address region for a port
8482 * @port: devlink port
8483 * @ops: region operations and name
8484 * @region_max_snapshots: Maximum supported number of snapshots for region
8485 * @region_size: size of region
8487 * Context: Takes and release devlink->lock <mutex>.
8489 struct devlink_region *
8490 devlink_port_region_create(struct devlink_port *port,
8491 const struct devlink_port_region_ops *ops,
8492 u32 region_max_snapshots, u64 region_size)
8494 struct devlink *devlink = port->devlink;
8495 struct devlink_region *region;
8498 ASSERT_DEVLINK_PORT_INITIALIZED(port);
8500 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
8501 return ERR_PTR(-EINVAL);
8505 if (devlink_port_region_get_by_name(port, ops->name)) {
8510 region = kzalloc(sizeof(*region), GFP_KERNEL);
8516 region->devlink = devlink;
8517 region->port = port;
8518 region->max_snapshots = region_max_snapshots;
8519 region->port_ops = ops;
8520 region->size = region_size;
8521 INIT_LIST_HEAD(®ion->snapshot_list);
8522 mutex_init(®ion->snapshot_lock);
8523 list_add_tail(®ion->list, &port->region_list);
8524 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
8526 devl_unlock(devlink);
8530 devl_unlock(devlink);
8531 return ERR_PTR(err);
8533 EXPORT_SYMBOL_GPL(devlink_port_region_create);
8536 * devl_region_destroy - destroy address region
8538 * @region: devlink region to destroy
8540 void devl_region_destroy(struct devlink_region *region)
8542 struct devlink *devlink = region->devlink;
8543 struct devlink_snapshot *snapshot, *ts;
8545 devl_assert_locked(devlink);
8547 /* Free all snapshots of region */
8548 mutex_lock(®ion->snapshot_lock);
8549 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
8550 devlink_region_snapshot_del(region, snapshot);
8551 mutex_unlock(®ion->snapshot_lock);
8553 list_del(®ion->list);
8554 mutex_destroy(®ion->snapshot_lock);
8556 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
8559 EXPORT_SYMBOL_GPL(devl_region_destroy);
8562 * devlink_region_destroy - destroy address region
8564 * @region: devlink region to destroy
8566 * Context: Takes and release devlink->lock <mutex>.
8568 void devlink_region_destroy(struct devlink_region *region)
8570 struct devlink *devlink = region->devlink;
8573 devl_region_destroy(region);
8574 devl_unlock(devlink);
8576 EXPORT_SYMBOL_GPL(devlink_region_destroy);
8579 * devlink_region_snapshot_id_get - get snapshot ID
8581 * This callback should be called when adding a new snapshot,
8582 * Driver should use the same id for multiple snapshots taken
8583 * on multiple regions at the same time/by the same trigger.
8585 * The caller of this function must use devlink_region_snapshot_id_put
8586 * when finished creating regions using this id.
8588 * Returns zero on success, or a negative error code on failure.
8591 * @id: storage to return id
8593 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
8595 return __devlink_region_snapshot_id_get(devlink, id);
8597 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
8600 * devlink_region_snapshot_id_put - put snapshot ID reference
8602 * This should be called by a driver after finishing creating snapshots
8603 * with an id. Doing so ensures that the ID can later be released in the
8604 * event that all snapshots using it have been destroyed.
8607 * @id: id to release reference on
8609 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
8611 __devlink_snapshot_id_decrement(devlink, id);
8613 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
8616 * devlink_region_snapshot_create - create a new snapshot
8617 * This will add a new snapshot of a region. The snapshot
8618 * will be stored on the region struct and can be accessed
8619 * from devlink. This is useful for future analyses of snapshots.
8620 * Multiple snapshots can be created on a region.
8621 * The @snapshot_id should be obtained using the getter function.
8623 * @region: devlink region of the snapshot
8624 * @data: snapshot data
8625 * @snapshot_id: snapshot id to be created
8627 int devlink_region_snapshot_create(struct devlink_region *region,
8628 u8 *data, u32 snapshot_id)
8632 mutex_lock(®ion->snapshot_lock);
8633 err = __devlink_region_snapshot_create(region, data, snapshot_id);
8634 mutex_unlock(®ion->snapshot_lock);
8637 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
8639 #define DEVLINK_TRAP(_id, _type) \
8641 .type = DEVLINK_TRAP_TYPE_##_type, \
8642 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
8643 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
8646 static const struct devlink_trap devlink_trap_generic[] = {
8647 DEVLINK_TRAP(SMAC_MC, DROP),
8648 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
8649 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
8650 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
8651 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
8652 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
8653 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
8654 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
8655 DEVLINK_TRAP(TAIL_DROP, DROP),
8656 DEVLINK_TRAP(NON_IP_PACKET, DROP),
8657 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
8658 DEVLINK_TRAP(DIP_LB, DROP),
8659 DEVLINK_TRAP(SIP_MC, DROP),
8660 DEVLINK_TRAP(SIP_LB, DROP),
8661 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
8662 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
8663 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
8664 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
8665 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
8666 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
8667 DEVLINK_TRAP(RPF, EXCEPTION),
8668 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
8669 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
8670 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
8671 DEVLINK_TRAP(NON_ROUTABLE, DROP),
8672 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
8673 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
8674 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
8675 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
8676 DEVLINK_TRAP(STP, CONTROL),
8677 DEVLINK_TRAP(LACP, CONTROL),
8678 DEVLINK_TRAP(LLDP, CONTROL),
8679 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
8680 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
8681 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
8682 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
8683 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
8684 DEVLINK_TRAP(MLD_QUERY, CONTROL),
8685 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
8686 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
8687 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
8688 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
8689 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
8690 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
8691 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
8692 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
8693 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
8694 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
8695 DEVLINK_TRAP(IPV4_BFD, CONTROL),
8696 DEVLINK_TRAP(IPV6_BFD, CONTROL),
8697 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
8698 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
8699 DEVLINK_TRAP(IPV4_BGP, CONTROL),
8700 DEVLINK_TRAP(IPV6_BGP, CONTROL),
8701 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
8702 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
8703 DEVLINK_TRAP(IPV4_PIM, CONTROL),
8704 DEVLINK_TRAP(IPV6_PIM, CONTROL),
8705 DEVLINK_TRAP(UC_LB, CONTROL),
8706 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
8707 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
8708 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
8709 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
8710 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
8711 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
8712 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
8713 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
8714 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
8715 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
8716 DEVLINK_TRAP(PTP_EVENT, CONTROL),
8717 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
8718 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
8719 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
8720 DEVLINK_TRAP(EARLY_DROP, DROP),
8721 DEVLINK_TRAP(VXLAN_PARSING, DROP),
8722 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
8723 DEVLINK_TRAP(VLAN_PARSING, DROP),
8724 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
8725 DEVLINK_TRAP(MPLS_PARSING, DROP),
8726 DEVLINK_TRAP(ARP_PARSING, DROP),
8727 DEVLINK_TRAP(IP_1_PARSING, DROP),
8728 DEVLINK_TRAP(IP_N_PARSING, DROP),
8729 DEVLINK_TRAP(GRE_PARSING, DROP),
8730 DEVLINK_TRAP(UDP_PARSING, DROP),
8731 DEVLINK_TRAP(TCP_PARSING, DROP),
8732 DEVLINK_TRAP(IPSEC_PARSING, DROP),
8733 DEVLINK_TRAP(SCTP_PARSING, DROP),
8734 DEVLINK_TRAP(DCCP_PARSING, DROP),
8735 DEVLINK_TRAP(GTP_PARSING, DROP),
8736 DEVLINK_TRAP(ESP_PARSING, DROP),
8737 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
8738 DEVLINK_TRAP(DMAC_FILTER, DROP),
8739 DEVLINK_TRAP(EAPOL, CONTROL),
8740 DEVLINK_TRAP(LOCKED_PORT, DROP),
8743 #define DEVLINK_TRAP_GROUP(_id) \
8745 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
8746 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
8749 static const struct devlink_trap_group devlink_trap_group_generic[] = {
8750 DEVLINK_TRAP_GROUP(L2_DROPS),
8751 DEVLINK_TRAP_GROUP(L3_DROPS),
8752 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
8753 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
8754 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
8755 DEVLINK_TRAP_GROUP(ACL_DROPS),
8756 DEVLINK_TRAP_GROUP(STP),
8757 DEVLINK_TRAP_GROUP(LACP),
8758 DEVLINK_TRAP_GROUP(LLDP),
8759 DEVLINK_TRAP_GROUP(MC_SNOOPING),
8760 DEVLINK_TRAP_GROUP(DHCP),
8761 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
8762 DEVLINK_TRAP_GROUP(BFD),
8763 DEVLINK_TRAP_GROUP(OSPF),
8764 DEVLINK_TRAP_GROUP(BGP),
8765 DEVLINK_TRAP_GROUP(VRRP),
8766 DEVLINK_TRAP_GROUP(PIM),
8767 DEVLINK_TRAP_GROUP(UC_LB),
8768 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
8769 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
8770 DEVLINK_TRAP_GROUP(IPV6),
8771 DEVLINK_TRAP_GROUP(PTP_EVENT),
8772 DEVLINK_TRAP_GROUP(PTP_GENERAL),
8773 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
8774 DEVLINK_TRAP_GROUP(ACL_TRAP),
8775 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
8776 DEVLINK_TRAP_GROUP(EAPOL),
8779 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
8781 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
8784 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
8787 if (trap->type != devlink_trap_generic[trap->id].type)
8793 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
8797 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
8800 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
8801 if (!strcmp(trap->name, devlink_trap_generic[i].name))
8808 static int devlink_trap_verify(const struct devlink_trap *trap)
8810 if (!trap || !trap->name)
8814 return devlink_trap_generic_verify(trap);
8816 return devlink_trap_driver_verify(trap);
8820 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
8822 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
8825 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
8832 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
8836 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
8839 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
8840 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
8847 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
8850 return devlink_trap_group_generic_verify(group);
8852 return devlink_trap_group_driver_verify(group);
8856 devlink_trap_group_notify(struct devlink *devlink,
8857 const struct devlink_trap_group_item *group_item,
8858 enum devlink_command cmd)
8860 struct sk_buff *msg;
8863 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
8864 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
8865 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
8868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8872 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
8879 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
8880 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
8884 devlink_trap_item_group_link(struct devlink *devlink,
8885 struct devlink_trap_item *trap_item)
8887 u16 group_id = trap_item->trap->init_group_id;
8888 struct devlink_trap_group_item *group_item;
8890 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
8891 if (WARN_ON_ONCE(!group_item))
8894 trap_item->group_item = group_item;
8899 static void devlink_trap_notify(struct devlink *devlink,
8900 const struct devlink_trap_item *trap_item,
8901 enum devlink_command cmd)
8903 struct sk_buff *msg;
8906 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
8907 cmd != DEVLINK_CMD_TRAP_DEL);
8908 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
8911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8915 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
8921 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
8922 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
8926 devlink_trap_register(struct devlink *devlink,
8927 const struct devlink_trap *trap, void *priv)
8929 struct devlink_trap_item *trap_item;
8932 if (devlink_trap_item_lookup(devlink, trap->name))
8935 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
8939 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
8940 if (!trap_item->stats) {
8942 goto err_stats_alloc;
8945 trap_item->trap = trap;
8946 trap_item->action = trap->init_action;
8947 trap_item->priv = priv;
8949 err = devlink_trap_item_group_link(devlink, trap_item);
8951 goto err_group_link;
8953 err = devlink->ops->trap_init(devlink, trap, trap_item);
8957 list_add_tail(&trap_item->list, &devlink->trap_list);
8958 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
8964 free_percpu(trap_item->stats);
8970 static void devlink_trap_unregister(struct devlink *devlink,
8971 const struct devlink_trap *trap)
8973 struct devlink_trap_item *trap_item;
8975 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8976 if (WARN_ON_ONCE(!trap_item))
8979 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
8980 list_del(&trap_item->list);
8981 if (devlink->ops->trap_fini)
8982 devlink->ops->trap_fini(devlink, trap, trap_item);
8983 free_percpu(trap_item->stats);
8987 static void devlink_trap_disable(struct devlink *devlink,
8988 const struct devlink_trap *trap)
8990 struct devlink_trap_item *trap_item;
8992 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8993 if (WARN_ON_ONCE(!trap_item))
8996 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
8998 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9002 * devl_traps_register - Register packet traps with devlink.
9003 * @devlink: devlink.
9004 * @traps: Packet traps.
9005 * @traps_count: Count of provided packet traps.
9006 * @priv: Driver private information.
9008 * Return: Non-zero value on failure.
9010 int devl_traps_register(struct devlink *devlink,
9011 const struct devlink_trap *traps,
9012 size_t traps_count, void *priv)
9016 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9019 devl_assert_locked(devlink);
9020 for (i = 0; i < traps_count; i++) {
9021 const struct devlink_trap *trap = &traps[i];
9023 err = devlink_trap_verify(trap);
9025 goto err_trap_verify;
9027 err = devlink_trap_register(devlink, trap, priv);
9029 goto err_trap_register;
9036 for (i--; i >= 0; i--)
9037 devlink_trap_unregister(devlink, &traps[i]);
9040 EXPORT_SYMBOL_GPL(devl_traps_register);
9043 * devlink_traps_register - Register packet traps with devlink.
9044 * @devlink: devlink.
9045 * @traps: Packet traps.
9046 * @traps_count: Count of provided packet traps.
9047 * @priv: Driver private information.
9049 * Context: Takes and release devlink->lock <mutex>.
9051 * Return: Non-zero value on failure.
9053 int devlink_traps_register(struct devlink *devlink,
9054 const struct devlink_trap *traps,
9055 size_t traps_count, void *priv)
9060 err = devl_traps_register(devlink, traps, traps_count, priv);
9061 devl_unlock(devlink);
9064 EXPORT_SYMBOL_GPL(devlink_traps_register);
9067 * devl_traps_unregister - Unregister packet traps from devlink.
9068 * @devlink: devlink.
9069 * @traps: Packet traps.
9070 * @traps_count: Count of provided packet traps.
9072 void devl_traps_unregister(struct devlink *devlink,
9073 const struct devlink_trap *traps,
9078 devl_assert_locked(devlink);
9079 /* Make sure we do not have any packets in-flight while unregistering
9080 * traps by disabling all of them and waiting for a grace period.
9082 for (i = traps_count - 1; i >= 0; i--)
9083 devlink_trap_disable(devlink, &traps[i]);
9085 for (i = traps_count - 1; i >= 0; i--)
9086 devlink_trap_unregister(devlink, &traps[i]);
9088 EXPORT_SYMBOL_GPL(devl_traps_unregister);
9091 * devlink_traps_unregister - Unregister packet traps from devlink.
9092 * @devlink: devlink.
9093 * @traps: Packet traps.
9094 * @traps_count: Count of provided packet traps.
9096 * Context: Takes and release devlink->lock <mutex>.
9098 void devlink_traps_unregister(struct devlink *devlink,
9099 const struct devlink_trap *traps,
9103 devl_traps_unregister(devlink, traps, traps_count);
9104 devl_unlock(devlink);
9106 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9109 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9112 struct devlink_stats *stats;
9114 stats = this_cpu_ptr(trap_stats);
9115 u64_stats_update_begin(&stats->syncp);
9116 u64_stats_add(&stats->rx_bytes, skb_len);
9117 u64_stats_inc(&stats->rx_packets);
9118 u64_stats_update_end(&stats->syncp);
9122 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9123 const struct devlink_trap_item *trap_item,
9124 struct devlink_port *in_devlink_port,
9125 const struct flow_action_cookie *fa_cookie)
9127 metadata->trap_name = trap_item->trap->name;
9128 metadata->trap_group_name = trap_item->group_item->group->name;
9129 metadata->fa_cookie = fa_cookie;
9130 metadata->trap_type = trap_item->trap->type;
9132 spin_lock(&in_devlink_port->type_lock);
9133 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9134 metadata->input_dev = in_devlink_port->type_eth.netdev;
9135 spin_unlock(&in_devlink_port->type_lock);
9139 * devlink_trap_report - Report trapped packet to drop monitor.
9140 * @devlink: devlink.
9141 * @skb: Trapped packet.
9142 * @trap_ctx: Trap context.
9143 * @in_devlink_port: Input devlink port.
9144 * @fa_cookie: Flow action cookie. Could be NULL.
9146 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9147 void *trap_ctx, struct devlink_port *in_devlink_port,
9148 const struct flow_action_cookie *fa_cookie)
9151 struct devlink_trap_item *trap_item = trap_ctx;
9153 devlink_trap_stats_update(trap_item->stats, skb->len);
9154 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9156 if (trace_devlink_trap_report_enabled()) {
9157 struct devlink_trap_metadata metadata = {};
9159 devlink_trap_report_metadata_set(&metadata, trap_item,
9160 in_devlink_port, fa_cookie);
9161 trace_devlink_trap_report(devlink, skb, &metadata);
9164 EXPORT_SYMBOL_GPL(devlink_trap_report);
9167 * devlink_trap_ctx_priv - Trap context to driver private information.
9168 * @trap_ctx: Trap context.
9170 * Return: Driver private information passed during registration.
9172 void *devlink_trap_ctx_priv(void *trap_ctx)
9174 struct devlink_trap_item *trap_item = trap_ctx;
9176 return trap_item->priv;
9178 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9181 devlink_trap_group_item_policer_link(struct devlink *devlink,
9182 struct devlink_trap_group_item *group_item)
9184 u32 policer_id = group_item->group->init_policer_id;
9185 struct devlink_trap_policer_item *policer_item;
9187 if (policer_id == 0)
9190 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9191 if (WARN_ON_ONCE(!policer_item))
9194 group_item->policer_item = policer_item;
9200 devlink_trap_group_register(struct devlink *devlink,
9201 const struct devlink_trap_group *group)
9203 struct devlink_trap_group_item *group_item;
9206 if (devlink_trap_group_item_lookup(devlink, group->name))
9209 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9213 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9214 if (!group_item->stats) {
9216 goto err_stats_alloc;
9219 group_item->group = group;
9221 err = devlink_trap_group_item_policer_link(devlink, group_item);
9223 goto err_policer_link;
9225 if (devlink->ops->trap_group_init) {
9226 err = devlink->ops->trap_group_init(devlink, group);
9228 goto err_group_init;
9231 list_add_tail(&group_item->list, &devlink->trap_group_list);
9232 devlink_trap_group_notify(devlink, group_item,
9233 DEVLINK_CMD_TRAP_GROUP_NEW);
9239 free_percpu(group_item->stats);
9246 devlink_trap_group_unregister(struct devlink *devlink,
9247 const struct devlink_trap_group *group)
9249 struct devlink_trap_group_item *group_item;
9251 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9252 if (WARN_ON_ONCE(!group_item))
9255 devlink_trap_group_notify(devlink, group_item,
9256 DEVLINK_CMD_TRAP_GROUP_DEL);
9257 list_del(&group_item->list);
9258 free_percpu(group_item->stats);
9263 * devl_trap_groups_register - Register packet trap groups with devlink.
9264 * @devlink: devlink.
9265 * @groups: Packet trap groups.
9266 * @groups_count: Count of provided packet trap groups.
9268 * Return: Non-zero value on failure.
9270 int devl_trap_groups_register(struct devlink *devlink,
9271 const struct devlink_trap_group *groups,
9272 size_t groups_count)
9276 devl_assert_locked(devlink);
9277 for (i = 0; i < groups_count; i++) {
9278 const struct devlink_trap_group *group = &groups[i];
9280 err = devlink_trap_group_verify(group);
9282 goto err_trap_group_verify;
9284 err = devlink_trap_group_register(devlink, group);
9286 goto err_trap_group_register;
9291 err_trap_group_register:
9292 err_trap_group_verify:
9293 for (i--; i >= 0; i--)
9294 devlink_trap_group_unregister(devlink, &groups[i]);
9297 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
9300 * devlink_trap_groups_register - Register packet trap groups with devlink.
9301 * @devlink: devlink.
9302 * @groups: Packet trap groups.
9303 * @groups_count: Count of provided packet trap groups.
9305 * Context: Takes and release devlink->lock <mutex>.
9307 * Return: Non-zero value on failure.
9309 int devlink_trap_groups_register(struct devlink *devlink,
9310 const struct devlink_trap_group *groups,
9311 size_t groups_count)
9316 err = devl_trap_groups_register(devlink, groups, groups_count);
9317 devl_unlock(devlink);
9320 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9323 * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
9324 * @devlink: devlink.
9325 * @groups: Packet trap groups.
9326 * @groups_count: Count of provided packet trap groups.
9328 void devl_trap_groups_unregister(struct devlink *devlink,
9329 const struct devlink_trap_group *groups,
9330 size_t groups_count)
9334 devl_assert_locked(devlink);
9335 for (i = groups_count - 1; i >= 0; i--)
9336 devlink_trap_group_unregister(devlink, &groups[i]);
9338 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
9341 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9342 * @devlink: devlink.
9343 * @groups: Packet trap groups.
9344 * @groups_count: Count of provided packet trap groups.
9346 * Context: Takes and release devlink->lock <mutex>.
9348 void devlink_trap_groups_unregister(struct devlink *devlink,
9349 const struct devlink_trap_group *groups,
9350 size_t groups_count)
9353 devl_trap_groups_unregister(devlink, groups, groups_count);
9354 devl_unlock(devlink);
9356 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
9359 devlink_trap_policer_notify(struct devlink *devlink,
9360 const struct devlink_trap_policer_item *policer_item,
9361 enum devlink_command cmd)
9363 struct sk_buff *msg;
9366 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
9367 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
9368 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
9371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9375 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
9382 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9383 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9387 devlink_trap_policer_register(struct devlink *devlink,
9388 const struct devlink_trap_policer *policer)
9390 struct devlink_trap_policer_item *policer_item;
9393 if (devlink_trap_policer_item_lookup(devlink, policer->id))
9396 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
9400 policer_item->policer = policer;
9401 policer_item->rate = policer->init_rate;
9402 policer_item->burst = policer->init_burst;
9404 if (devlink->ops->trap_policer_init) {
9405 err = devlink->ops->trap_policer_init(devlink, policer);
9407 goto err_policer_init;
9410 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
9411 devlink_trap_policer_notify(devlink, policer_item,
9412 DEVLINK_CMD_TRAP_POLICER_NEW);
9417 kfree(policer_item);
9422 devlink_trap_policer_unregister(struct devlink *devlink,
9423 const struct devlink_trap_policer *policer)
9425 struct devlink_trap_policer_item *policer_item;
9427 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
9428 if (WARN_ON_ONCE(!policer_item))
9431 devlink_trap_policer_notify(devlink, policer_item,
9432 DEVLINK_CMD_TRAP_POLICER_DEL);
9433 list_del(&policer_item->list);
9434 if (devlink->ops->trap_policer_fini)
9435 devlink->ops->trap_policer_fini(devlink, policer);
9436 kfree(policer_item);
9440 * devl_trap_policers_register - Register packet trap policers with devlink.
9441 * @devlink: devlink.
9442 * @policers: Packet trap policers.
9443 * @policers_count: Count of provided packet trap policers.
9445 * Return: Non-zero value on failure.
9448 devl_trap_policers_register(struct devlink *devlink,
9449 const struct devlink_trap_policer *policers,
9450 size_t policers_count)
9454 devl_assert_locked(devlink);
9455 for (i = 0; i < policers_count; i++) {
9456 const struct devlink_trap_policer *policer = &policers[i];
9458 if (WARN_ON(policer->id == 0 ||
9459 policer->max_rate < policer->min_rate ||
9460 policer->max_burst < policer->min_burst)) {
9462 goto err_trap_policer_verify;
9465 err = devlink_trap_policer_register(devlink, policer);
9467 goto err_trap_policer_register;
9471 err_trap_policer_register:
9472 err_trap_policer_verify:
9473 for (i--; i >= 0; i--)
9474 devlink_trap_policer_unregister(devlink, &policers[i]);
9477 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
9480 * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
9481 * @devlink: devlink.
9482 * @policers: Packet trap policers.
9483 * @policers_count: Count of provided packet trap policers.
9486 devl_trap_policers_unregister(struct devlink *devlink,
9487 const struct devlink_trap_policer *policers,
9488 size_t policers_count)
9492 devl_assert_locked(devlink);
9493 for (i = policers_count - 1; i >= 0; i--)
9494 devlink_trap_policer_unregister(devlink, &policers[i]);
9496 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
9498 int devlink_compat_phys_port_name_get(struct net_device *dev,
9499 char *name, size_t len)
9501 struct devlink_port *devlink_port;
9503 /* RTNL mutex is held here which ensures that devlink_port
9504 * instance cannot disappear in the middle. No need to take
9505 * any devlink lock as only permanent values are accessed.
9509 devlink_port = dev->devlink_port;
9513 return __devlink_port_phys_port_name_get(devlink_port, name, len);
9516 int devlink_compat_switch_id_get(struct net_device *dev,
9517 struct netdev_phys_item_id *ppid)
9519 struct devlink_port *devlink_port;
9521 /* Caller must hold RTNL mutex or reference to dev, which ensures that
9522 * devlink_port instance cannot disappear in the middle. No need to take
9523 * any devlink lock as only permanent values are accessed.
9525 devlink_port = dev->devlink_port;
9526 if (!devlink_port || !devlink_port->switch_port)
9529 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));