~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/devlink.c

Version: ~ [ linux-5.13-rc1 ] ~ [ linux-5.12.2 ] ~ [ linux-5.11.19 ] ~ [ linux-5.10.35 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.117 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.190 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.232 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.268 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.268 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * net/core/devlink.c - Network physical/parent device Netlink interface
  4  *
  5  * Heavily inspired by net/wireless/
  6  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  7  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  8  */
  9 
 10 #include <linux/kernel.h>
 11 #include <linux/module.h>
 12 #include <linux/types.h>
 13 #include <linux/slab.h>
 14 #include <linux/gfp.h>
 15 #include <linux/device.h>
 16 #include <linux/list.h>
 17 #include <linux/netdevice.h>
 18 #include <linux/spinlock.h>
 19 #include <linux/refcount.h>
 20 #include <linux/workqueue.h>
 21 #include <linux/u64_stats_sync.h>
 22 #include <linux/timekeeping.h>
 23 #include <rdma/ib_verbs.h>
 24 #include <net/netlink.h>
 25 #include <net/genetlink.h>
 26 #include <net/rtnetlink.h>
 27 #include <net/net_namespace.h>
 28 #include <net/sock.h>
 29 #include <net/devlink.h>
 30 #include <net/drop_monitor.h>
 31 #define CREATE_TRACE_POINTS
 32 #include <trace/events/devlink.h>
 33 
 34 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
 35         {
 36                 .name = "destination mac",
 37                 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
 38                 .bitwidth = 48,
 39         },
 40 };
 41 
 42 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
 43         .name = "ethernet",
 44         .id = DEVLINK_DPIPE_HEADER_ETHERNET,
 45         .fields = devlink_dpipe_fields_ethernet,
 46         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
 47         .global = true,
 48 };
 49 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
 50 
 51 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
 52         {
 53                 .name = "destination ip",
 54                 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
 55                 .bitwidth = 32,
 56         },
 57 };
 58 
 59 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
 60         .name = "ipv4",
 61         .id = DEVLINK_DPIPE_HEADER_IPV4,
 62         .fields = devlink_dpipe_fields_ipv4,
 63         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
 64         .global = true,
 65 };
 66 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
 67 
 68 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
 69         {
 70                 .name = "destination ip",
 71                 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
 72                 .bitwidth = 128,
 73         },
 74 };
 75 
 76 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
 77         .name = "ipv6",
 78         .id = DEVLINK_DPIPE_HEADER_IPV6,
 79         .fields = devlink_dpipe_fields_ipv6,
 80         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
 81         .global = true,
 82 };
 83 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
 84 
 85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
 86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
 87 
 88 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
 89         [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
 90 };
 91 
 92 static LIST_HEAD(devlink_list);
 93 
 94 /* devlink_mutex
 95  *
 96  * An overall lock guarding every operation coming from userspace.
 97  * It also guards devlink devices list and it is taken when
 98  * driver registers/unregisters it.
 99  */
100 static DEFINE_MUTEX(devlink_mutex);
101 
102 struct net *devlink_net(const struct devlink *devlink)
103 {
104         return read_pnet(&devlink->_net);
105 }
106 EXPORT_SYMBOL_GPL(devlink_net);
107 
108 static void __devlink_net_set(struct devlink *devlink, struct net *net)
109 {
110         write_pnet(&devlink->_net, net);
111 }
112 
113 void devlink_net_set(struct devlink *devlink, struct net *net)
114 {
115         if (WARN_ON(devlink->registered))
116                 return;
117         __devlink_net_set(devlink, net);
118 }
119 EXPORT_SYMBOL_GPL(devlink_net_set);
120 
121 static struct devlink *devlink_get_from_attrs(struct net *net,
122                                               struct nlattr **attrs)
123 {
124         struct devlink *devlink;
125         char *busname;
126         char *devname;
127 
128         if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
129                 return ERR_PTR(-EINVAL);
130 
131         busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
132         devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
133 
134         lockdep_assert_held(&devlink_mutex);
135 
136         list_for_each_entry(devlink, &devlink_list, list) {
137                 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
138                     strcmp(dev_name(devlink->dev), devname) == 0 &&
139                     net_eq(devlink_net(devlink), net))
140                         return devlink;
141         }
142 
143         return ERR_PTR(-ENODEV);
144 }
145 
146 static struct devlink *devlink_get_from_info(struct genl_info *info)
147 {
148         return devlink_get_from_attrs(genl_info_net(info), info->attrs);
149 }
150 
151 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
152                                                       unsigned int port_index)
153 {
154         struct devlink_port *devlink_port;
155 
156         list_for_each_entry(devlink_port, &devlink->port_list, list) {
157                 if (devlink_port->index == port_index)
158                         return devlink_port;
159         }
160         return NULL;
161 }
162 
163 static bool devlink_port_index_exists(struct devlink *devlink,
164                                       unsigned int port_index)
165 {
166         return devlink_port_get_by_index(devlink, port_index);
167 }
168 
169 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
170                                                         struct nlattr **attrs)
171 {
172         if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
173                 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
174                 struct devlink_port *devlink_port;
175 
176                 devlink_port = devlink_port_get_by_index(devlink, port_index);
177                 if (!devlink_port)
178                         return ERR_PTR(-ENODEV);
179                 return devlink_port;
180         }
181         return ERR_PTR(-EINVAL);
182 }
183 
184 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
185                                                        struct genl_info *info)
186 {
187         return devlink_port_get_from_attrs(devlink, info->attrs);
188 }
189 
190 struct devlink_sb {
191         struct list_head list;
192         unsigned int index;
193         u32 size;
194         u16 ingress_pools_count;
195         u16 egress_pools_count;
196         u16 ingress_tc_count;
197         u16 egress_tc_count;
198 };
199 
200 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
201 {
202         return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
203 }
204 
205 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
206                                                   unsigned int sb_index)
207 {
208         struct devlink_sb *devlink_sb;
209 
210         list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
211                 if (devlink_sb->index == sb_index)
212                         return devlink_sb;
213         }
214         return NULL;
215 }
216 
217 static bool devlink_sb_index_exists(struct devlink *devlink,
218                                     unsigned int sb_index)
219 {
220         return devlink_sb_get_by_index(devlink, sb_index);
221 }
222 
223 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
224                                                     struct nlattr **attrs)
225 {
226         if (attrs[DEVLINK_ATTR_SB_INDEX]) {
227                 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
228                 struct devlink_sb *devlink_sb;
229 
230                 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
231                 if (!devlink_sb)
232                         return ERR_PTR(-ENODEV);
233                 return devlink_sb;
234         }
235         return ERR_PTR(-EINVAL);
236 }
237 
238 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
239                                                    struct genl_info *info)
240 {
241         return devlink_sb_get_from_attrs(devlink, info->attrs);
242 }
243 
244 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
245                                                 struct nlattr **attrs,
246                                                 u16 *p_pool_index)
247 {
248         u16 val;
249 
250         if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
251                 return -EINVAL;
252 
253         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
254         if (val >= devlink_sb_pool_count(devlink_sb))
255                 return -EINVAL;
256         *p_pool_index = val;
257         return 0;
258 }
259 
260 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
261                                                struct genl_info *info,
262                                                u16 *p_pool_index)
263 {
264         return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
265                                                     p_pool_index);
266 }
267 
268 static int
269 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
270                                     enum devlink_sb_pool_type *p_pool_type)
271 {
272         u8 val;
273 
274         if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
275                 return -EINVAL;
276 
277         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
278         if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
279             val != DEVLINK_SB_POOL_TYPE_EGRESS)
280                 return -EINVAL;
281         *p_pool_type = val;
282         return 0;
283 }
284 
285 static int
286 devlink_sb_pool_type_get_from_info(struct genl_info *info,
287                                    enum devlink_sb_pool_type *p_pool_type)
288 {
289         return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
290 }
291 
292 static int
293 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
294                                   enum devlink_sb_threshold_type *p_th_type)
295 {
296         u8 val;
297 
298         if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
299                 return -EINVAL;
300 
301         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
302         if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
303             val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
304                 return -EINVAL;
305         *p_th_type = val;
306         return 0;
307 }
308 
309 static int
310 devlink_sb_th_type_get_from_info(struct genl_info *info,
311                                  enum devlink_sb_threshold_type *p_th_type)
312 {
313         return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
314 }
315 
316 static int
317 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
318                                    struct nlattr **attrs,
319                                    enum devlink_sb_pool_type pool_type,
320                                    u16 *p_tc_index)
321 {
322         u16 val;
323 
324         if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
325                 return -EINVAL;
326 
327         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
328         if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
329             val >= devlink_sb->ingress_tc_count)
330                 return -EINVAL;
331         if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
332             val >= devlink_sb->egress_tc_count)
333                 return -EINVAL;
334         *p_tc_index = val;
335         return 0;
336 }
337 
338 static int
339 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
340                                   struct genl_info *info,
341                                   enum devlink_sb_pool_type pool_type,
342                                   u16 *p_tc_index)
343 {
344         return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
345                                                   pool_type, p_tc_index);
346 }
347 
348 struct devlink_region {
349         struct devlink *devlink;
350         struct list_head list;
351         const struct devlink_region_ops *ops;
352         struct list_head snapshot_list;
353         u32 max_snapshots;
354         u32 cur_snapshots;
355         u64 size;
356 };
357 
358 struct devlink_snapshot {
359         struct list_head list;
360         struct devlink_region *region;
361         u8 *data;
362         u32 id;
363 };
364 
365 static struct devlink_region *
366 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
367 {
368         struct devlink_region *region;
369 
370         list_for_each_entry(region, &devlink->region_list, list)
371                 if (!strcmp(region->ops->name, region_name))
372                         return region;
373 
374         return NULL;
375 }
376 
377 static struct devlink_snapshot *
378 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
379 {
380         struct devlink_snapshot *snapshot;
381 
382         list_for_each_entry(snapshot, &region->snapshot_list, list)
383                 if (snapshot->id == id)
384                         return snapshot;
385 
386         return NULL;
387 }
388 
389 #define DEVLINK_NL_FLAG_NEED_PORT               BIT(0)
390 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT    BIT(1)
391 
392 /* The per devlink instance lock is taken by default in the pre-doit
393  * operation, yet several commands do not require this. The global
394  * devlink lock is taken and protects from disruption by user-calls.
395  */
396 #define DEVLINK_NL_FLAG_NO_LOCK                 BIT(2)
397 
398 static int devlink_nl_pre_doit(const struct genl_ops *ops,
399                                struct sk_buff *skb, struct genl_info *info)
400 {
401         struct devlink_port *devlink_port;
402         struct devlink *devlink;
403         int err;
404 
405         mutex_lock(&devlink_mutex);
406         devlink = devlink_get_from_info(info);
407         if (IS_ERR(devlink)) {
408                 mutex_unlock(&devlink_mutex);
409                 return PTR_ERR(devlink);
410         }
411         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
412                 mutex_lock(&devlink->lock);
413         info->user_ptr[0] = devlink;
414         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
415                 devlink_port = devlink_port_get_from_info(devlink, info);
416                 if (IS_ERR(devlink_port)) {
417                         err = PTR_ERR(devlink_port);
418                         goto unlock;
419                 }
420                 info->user_ptr[1] = devlink_port;
421         } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
422                 devlink_port = devlink_port_get_from_info(devlink, info);
423                 if (!IS_ERR(devlink_port))
424                         info->user_ptr[1] = devlink_port;
425         }
426         return 0;
427 
428 unlock:
429         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
430                 mutex_unlock(&devlink->lock);
431         mutex_unlock(&devlink_mutex);
432         return err;
433 }
434 
435 static void devlink_nl_post_doit(const struct genl_ops *ops,
436                                  struct sk_buff *skb, struct genl_info *info)
437 {
438         struct devlink *devlink;
439 
440         devlink = info->user_ptr[0];
441         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
442                 mutex_unlock(&devlink->lock);
443         mutex_unlock(&devlink_mutex);
444 }
445 
446 static struct genl_family devlink_nl_family;
447 
448 enum devlink_multicast_groups {
449         DEVLINK_MCGRP_CONFIG,
450 };
451 
452 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
453         [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
454 };
455 
456 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
457 {
458         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
459                 return -EMSGSIZE;
460         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
461                 return -EMSGSIZE;
462         return 0;
463 }
464 
465 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
466                            enum devlink_command cmd, u32 portid,
467                            u32 seq, int flags)
468 {
469         void *hdr;
470 
471         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
472         if (!hdr)
473                 return -EMSGSIZE;
474 
475         if (devlink_nl_put_handle(msg, devlink))
476                 goto nla_put_failure;
477         if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
478                 goto nla_put_failure;
479 
480         genlmsg_end(msg, hdr);
481         return 0;
482 
483 nla_put_failure:
484         genlmsg_cancel(msg, hdr);
485         return -EMSGSIZE;
486 }
487 
488 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
489 {
490         struct sk_buff *msg;
491         int err;
492 
493         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
494 
495         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
496         if (!msg)
497                 return;
498 
499         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
500         if (err) {
501                 nlmsg_free(msg);
502                 return;
503         }
504 
505         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
506                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
507 }
508 
509 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
510                                      struct devlink_port *devlink_port)
511 {
512         struct devlink_port_attrs *attrs = &devlink_port->attrs;
513 
514         if (!devlink_port->attrs_set)
515                 return 0;
516         if (attrs->lanes) {
517                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
518                         return -EMSGSIZE;
519         }
520         if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
521                 return -EMSGSIZE;
522         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
523                 return -EMSGSIZE;
524         switch (devlink_port->attrs.flavour) {
525         case DEVLINK_PORT_FLAVOUR_PCI_PF:
526                 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
527                                 attrs->pci_pf.pf))
528                         return -EMSGSIZE;
529                 break;
530         case DEVLINK_PORT_FLAVOUR_PCI_VF:
531                 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
532                                 attrs->pci_vf.pf) ||
533                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER,
534                                 attrs->pci_vf.vf))
535                         return -EMSGSIZE;
536                 break;
537         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
538         case DEVLINK_PORT_FLAVOUR_CPU:
539         case DEVLINK_PORT_FLAVOUR_DSA:
540         case DEVLINK_PORT_FLAVOUR_VIRTUAL:
541                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
542                                 attrs->phys.port_number))
543                         return -EMSGSIZE;
544                 if (!attrs->split)
545                         return 0;
546                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
547                                 attrs->phys.port_number))
548                         return -EMSGSIZE;
549                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
550                                 attrs->phys.split_subport_number))
551                         return -EMSGSIZE;
552                 break;
553         default:
554                 break;
555         }
556         return 0;
557 }
558 
559 static int
560 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
561                                    struct netlink_ext_ack *extack)
562 {
563         struct devlink *devlink = port->devlink;
564         const struct devlink_ops *ops;
565         struct nlattr *function_attr;
566         bool empty_nest = true;
567         int err = 0;
568 
569         function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
570         if (!function_attr)
571                 return -EMSGSIZE;
572 
573         ops = devlink->ops;
574         if (ops->port_function_hw_addr_get) {
575                 int hw_addr_len;
576                 u8 hw_addr[MAX_ADDR_LEN];
577 
578                 err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack);
579                 if (err == -EOPNOTSUPP) {
580                         /* Port function attributes are optional for a port. If port doesn't
581                          * support function attribute, returning -EOPNOTSUPP is not an error.
582                          */
583                         err = 0;
584                         goto out;
585                 } else if (err) {
586                         goto out;
587                 }
588                 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
589                 if (err)
590                         goto out;
591                 empty_nest = false;
592         }
593 
594 out:
595         if (err || empty_nest)
596                 nla_nest_cancel(msg, function_attr);
597         else
598                 nla_nest_end(msg, function_attr);
599         return err;
600 }
601 
602 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
603                                 struct devlink_port *devlink_port,
604                                 enum devlink_command cmd, u32 portid,
605                                 u32 seq, int flags,
606                                 struct netlink_ext_ack *extack)
607 {
608         void *hdr;
609 
610         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
611         if (!hdr)
612                 return -EMSGSIZE;
613 
614         if (devlink_nl_put_handle(msg, devlink))
615                 goto nla_put_failure;
616         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
617                 goto nla_put_failure;
618 
619         /* Hold rtnl lock while accessing port's netdev attributes. */
620         rtnl_lock();
621         spin_lock_bh(&devlink_port->type_lock);
622         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
623                 goto nla_put_failure_type_locked;
624         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
625             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
626                         devlink_port->desired_type))
627                 goto nla_put_failure_type_locked;
628         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
629                 struct net *net = devlink_net(devlink_port->devlink);
630                 struct net_device *netdev = devlink_port->type_dev;
631 
632                 if (netdev && net_eq(net, dev_net(netdev)) &&
633                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
634                                  netdev->ifindex) ||
635                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
636                                     netdev->name)))
637                         goto nla_put_failure_type_locked;
638         }
639         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
640                 struct ib_device *ibdev = devlink_port->type_dev;
641 
642                 if (ibdev &&
643                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
644                                    ibdev->name))
645                         goto nla_put_failure_type_locked;
646         }
647         spin_unlock_bh(&devlink_port->type_lock);
648         rtnl_unlock();
649         if (devlink_nl_port_attrs_put(msg, devlink_port))
650                 goto nla_put_failure;
651         if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
652                 goto nla_put_failure;
653 
654         genlmsg_end(msg, hdr);
655         return 0;
656 
657 nla_put_failure_type_locked:
658         spin_unlock_bh(&devlink_port->type_lock);
659         rtnl_unlock();
660 nla_put_failure:
661         genlmsg_cancel(msg, hdr);
662         return -EMSGSIZE;
663 }
664 
665 static void devlink_port_notify(struct devlink_port *devlink_port,
666                                 enum devlink_command cmd)
667 {
668         struct devlink *devlink = devlink_port->devlink;
669         struct sk_buff *msg;
670         int err;
671 
672         if (!devlink_port->registered)
673                 return;
674 
675         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
676 
677         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
678         if (!msg)
679                 return;
680 
681         err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0,
682                                    NULL);
683         if (err) {
684                 nlmsg_free(msg);
685                 return;
686         }
687 
688         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
689                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
690 }
691 
692 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
693 {
694         struct devlink *devlink = info->user_ptr[0];
695         struct sk_buff *msg;
696         int err;
697 
698         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
699         if (!msg)
700                 return -ENOMEM;
701 
702         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
703                               info->snd_portid, info->snd_seq, 0);
704         if (err) {
705                 nlmsg_free(msg);
706                 return err;
707         }
708 
709         return genlmsg_reply(msg, info);
710 }
711 
712 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
713                                      struct netlink_callback *cb)
714 {
715         struct devlink *devlink;
716         int start = cb->args[0];
717         int idx = 0;
718         int err;
719 
720         mutex_lock(&devlink_mutex);
721         list_for_each_entry(devlink, &devlink_list, list) {
722                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
723                         continue;
724                 if (idx < start) {
725                         idx++;
726                         continue;
727                 }
728                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
729                                       NETLINK_CB(cb->skb).portid,
730                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
731                 if (err)
732                         goto out;
733                 idx++;
734         }
735 out:
736         mutex_unlock(&devlink_mutex);
737 
738         cb->args[0] = idx;
739         return msg->len;
740 }
741 
742 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
743                                         struct genl_info *info)
744 {
745         struct devlink_port *devlink_port = info->user_ptr[1];
746         struct devlink *devlink = devlink_port->devlink;
747         struct sk_buff *msg;
748         int err;
749 
750         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
751         if (!msg)
752                 return -ENOMEM;
753 
754         err = devlink_nl_port_fill(msg, devlink, devlink_port,
755                                    DEVLINK_CMD_PORT_NEW,
756                                    info->snd_portid, info->snd_seq, 0,
757                                    info->extack);
758         if (err) {
759                 nlmsg_free(msg);
760                 return err;
761         }
762 
763         return genlmsg_reply(msg, info);
764 }
765 
766 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
767                                           struct netlink_callback *cb)
768 {
769         struct devlink *devlink;
770         struct devlink_port *devlink_port;
771         int start = cb->args[0];
772         int idx = 0;
773         int err;
774 
775         mutex_lock(&devlink_mutex);
776         list_for_each_entry(devlink, &devlink_list, list) {
777                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
778                         continue;
779                 mutex_lock(&devlink->lock);
780                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
781                         if (idx < start) {
782                                 idx++;
783                                 continue;
784                         }
785                         err = devlink_nl_port_fill(msg, devlink, devlink_port,
786                                                    DEVLINK_CMD_NEW,
787                                                    NETLINK_CB(cb->skb).portid,
788                                                    cb->nlh->nlmsg_seq,
789                                                    NLM_F_MULTI,
790                                                    cb->extack);
791                         if (err) {
792                                 mutex_unlock(&devlink->lock);
793                                 goto out;
794                         }
795                         idx++;
796                 }
797                 mutex_unlock(&devlink->lock);
798         }
799 out:
800         mutex_unlock(&devlink_mutex);
801 
802         cb->args[0] = idx;
803         return msg->len;
804 }
805 
806 static int devlink_port_type_set(struct devlink *devlink,
807                                  struct devlink_port *devlink_port,
808                                  enum devlink_port_type port_type)
809 
810 {
811         int err;
812 
813         if (devlink->ops->port_type_set) {
814                 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
815                         return -EINVAL;
816                 if (port_type == devlink_port->type)
817                         return 0;
818                 err = devlink->ops->port_type_set(devlink_port, port_type);
819                 if (err)
820                         return err;
821                 devlink_port->desired_type = port_type;
822                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
823                 return 0;
824         }
825         return -EOPNOTSUPP;
826 }
827 
828 static int
829 devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port,
830                                   const struct nlattr *attr, struct netlink_ext_ack *extack)
831 {
832         const struct devlink_ops *ops;
833         const u8 *hw_addr;
834         int hw_addr_len;
835         int err;
836 
837         hw_addr = nla_data(attr);
838         hw_addr_len = nla_len(attr);
839         if (hw_addr_len > MAX_ADDR_LEN) {
840                 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
841                 return -EINVAL;
842         }
843         if (port->type == DEVLINK_PORT_TYPE_ETH) {
844                 if (hw_addr_len != ETH_ALEN) {
845                         NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
846                         return -EINVAL;
847                 }
848                 if (!is_unicast_ether_addr(hw_addr)) {
849                         NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
850                         return -EINVAL;
851                 }
852         }
853 
854         ops = devlink->ops;
855         if (!ops->port_function_hw_addr_set) {
856                 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
857                 return -EOPNOTSUPP;
858         }
859 
860         err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack);
861         if (err)
862                 return err;
863 
864         devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
865         return 0;
866 }
867 
868 static int
869 devlink_port_function_set(struct devlink *devlink, struct devlink_port *port,
870                           const struct nlattr *attr, struct netlink_ext_ack *extack)
871 {
872         struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
873         int err;
874 
875         err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
876                                devlink_function_nl_policy, extack);
877         if (err < 0) {
878                 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
879                 return err;
880         }
881 
882         attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
883         if (attr)
884                 err = devlink_port_function_hw_addr_set(devlink, port, attr, extack);
885 
886         return err;
887 }
888 
889 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
890                                         struct genl_info *info)
891 {
892         struct devlink_port *devlink_port = info->user_ptr[1];
893         struct devlink *devlink = devlink_port->devlink;
894         int err;
895 
896         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
897                 enum devlink_port_type port_type;
898 
899                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
900                 err = devlink_port_type_set(devlink, devlink_port, port_type);
901                 if (err)
902                         return err;
903         }
904 
905         if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
906                 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
907                 struct netlink_ext_ack *extack = info->extack;
908 
909                 err = devlink_port_function_set(devlink, devlink_port, attr, extack);
910                 if (err)
911                         return err;
912         }
913 
914         return 0;
915 }
916 
917 static int devlink_port_split(struct devlink *devlink, u32 port_index,
918                               u32 count, struct netlink_ext_ack *extack)
919 
920 {
921         if (devlink->ops->port_split)
922                 return devlink->ops->port_split(devlink, port_index, count,
923                                                 extack);
924         return -EOPNOTSUPP;
925 }
926 
927 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
928                                           struct genl_info *info)
929 {
930         struct devlink *devlink = info->user_ptr[0];
931         struct devlink_port *devlink_port;
932         u32 port_index;
933         u32 count;
934 
935         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
936             !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
937                 return -EINVAL;
938 
939         devlink_port = devlink_port_get_from_info(devlink, info);
940         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
941         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
942 
943         if (IS_ERR(devlink_port))
944                 return -EINVAL;
945 
946         if (!devlink_port->attrs.splittable) {
947                 /* Split ports cannot be split. */
948                 if (devlink_port->attrs.split)
949                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
950                 else
951                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
952                 return -EINVAL;
953         }
954 
955         if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
956                 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count"