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

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

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ 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 #define CREATE_TRACE_POINTS
 31 #include <trace/events/devlink.h>
 32 
 33 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
 34         {
 35                 .name = "destination mac",
 36                 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
 37                 .bitwidth = 48,
 38         },
 39 };
 40 
 41 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
 42         .name = "ethernet",
 43         .id = DEVLINK_DPIPE_HEADER_ETHERNET,
 44         .fields = devlink_dpipe_fields_ethernet,
 45         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
 46         .global = true,
 47 };
 48 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
 49 
 50 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
 51         {
 52                 .name = "destination ip",
 53                 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
 54                 .bitwidth = 32,
 55         },
 56 };
 57 
 58 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
 59         .name = "ipv4",
 60         .id = DEVLINK_DPIPE_HEADER_IPV4,
 61         .fields = devlink_dpipe_fields_ipv4,
 62         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
 63         .global = true,
 64 };
 65 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
 66 
 67 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
 68         {
 69                 .name = "destination ip",
 70                 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
 71                 .bitwidth = 128,
 72         },
 73 };
 74 
 75 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
 76         .name = "ipv6",
 77         .id = DEVLINK_DPIPE_HEADER_IPV6,
 78         .fields = devlink_dpipe_fields_ipv6,
 79         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
 80         .global = true,
 81 };
 82 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
 83 
 84 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
 85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
 86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
 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 devlink_port *port;
351         struct list_head list;
352         union {
353                 const struct devlink_region_ops *ops;
354                 const struct devlink_port_region_ops *port_ops;
355         };
356         struct list_head snapshot_list;
357         u32 max_snapshots;
358         u32 cur_snapshots;
359         u64 size;
360 };
361 
362 struct devlink_snapshot {
363         struct list_head list;
364         struct devlink_region *region;
365         u8 *data;
366         u32 id;
367 };
368 
369 static struct devlink_region *
370 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
371 {
372         struct devlink_region *region;
373 
374         list_for_each_entry(region, &devlink->region_list, list)
375                 if (!strcmp(region->ops->name, region_name))
376                         return region;
377 
378         return NULL;
379 }
380 
381 static struct devlink_region *
382 devlink_port_region_get_by_name(struct devlink_port *port,
383                                 const char *region_name)
384 {
385         struct devlink_region *region;
386 
387         list_for_each_entry(region, &port->region_list, list)
388                 if (!strcmp(region->ops->name, region_name))
389                         return region;
390 
391         return NULL;
392 }
393 
394 static struct devlink_snapshot *
395 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
396 {
397         struct devlink_snapshot *snapshot;
398 
399         list_for_each_entry(snapshot, &region->snapshot_list, list)
400                 if (snapshot->id == id)
401                         return snapshot;
402 
403         return NULL;
404 }
405 
406 #define DEVLINK_NL_FLAG_NEED_PORT               BIT(0)
407 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT    BIT(1)
408 
409 /* The per devlink instance lock is taken by default in the pre-doit
410  * operation, yet several commands do not require this. The global
411  * devlink lock is taken and protects from disruption by user-calls.
412  */
413 #define DEVLINK_NL_FLAG_NO_LOCK                 BIT(2)
414 
415 static int devlink_nl_pre_doit(const struct genl_ops *ops,
416                                struct sk_buff *skb, struct genl_info *info)
417 {
418         struct devlink_port *devlink_port;
419         struct devlink *devlink;
420         int err;
421 
422         mutex_lock(&devlink_mutex);
423         devlink = devlink_get_from_info(info);
424         if (IS_ERR(devlink)) {
425                 mutex_unlock(&devlink_mutex);
426                 return PTR_ERR(devlink);
427         }
428         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
429                 mutex_lock(&devlink->lock);
430         info->user_ptr[0] = devlink;
431         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
432                 devlink_port = devlink_port_get_from_info(devlink, info);
433                 if (IS_ERR(devlink_port)) {
434                         err = PTR_ERR(devlink_port);
435                         goto unlock;
436                 }
437                 info->user_ptr[1] = devlink_port;
438         } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
439                 devlink_port = devlink_port_get_from_info(devlink, info);
440                 if (!IS_ERR(devlink_port))
441                         info->user_ptr[1] = devlink_port;
442         }
443         return 0;
444 
445 unlock:
446         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
447                 mutex_unlock(&devlink->lock);
448         mutex_unlock(&devlink_mutex);
449         return err;
450 }
451 
452 static void devlink_nl_post_doit(const struct genl_ops *ops,
453                                  struct sk_buff *skb, struct genl_info *info)
454 {
455         struct devlink *devlink;
456 
457         devlink = info->user_ptr[0];
458         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
459                 mutex_unlock(&devlink->lock);
460         mutex_unlock(&devlink_mutex);
461 }
462 
463 static struct genl_family devlink_nl_family;
464 
465 enum devlink_multicast_groups {
466         DEVLINK_MCGRP_CONFIG,
467 };
468 
469 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
470         [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
471 };
472 
473 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
474 {
475         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
476                 return -EMSGSIZE;
477         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
478                 return -EMSGSIZE;
479         return 0;
480 }
481 
482 struct devlink_reload_combination {
483         enum devlink_reload_action action;
484         enum devlink_reload_limit limit;
485 };
486 
487 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
488         {
489                 /* can't reinitialize driver with no down time */
490                 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
491                 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
492         },
493 };
494 
495 static bool
496 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
497                                       enum devlink_reload_limit limit)
498 {
499         int i;
500 
501         for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
502                 if (devlink_reload_invalid_combinations[i].action == action &&
503                     devlink_reload_invalid_combinations[i].limit == limit)
504                         return true;
505         return false;
506 }
507 
508 static bool
509 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
510 {
511         return test_bit(action, &devlink->ops->reload_actions);
512 }
513 
514 static bool
515 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
516 {
517         return test_bit(limit, &devlink->ops->reload_limits);
518 }
519 
520 static int devlink_reload_stat_put(struct sk_buff *msg,
521                                    enum devlink_reload_limit limit, u32 value)
522 {
523         struct nlattr *reload_stats_entry;
524 
525         reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
526         if (!reload_stats_entry)
527                 return -EMSGSIZE;
528 
529         if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
530             nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
531                 goto nla_put_failure;
532         nla_nest_end(msg, reload_stats_entry);
533         return 0;
534 
535 nla_put_failure:
536         nla_nest_cancel(msg, reload_stats_entry);
537         return -EMSGSIZE;
538 }
539 
540 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
541 {
542         struct nlattr *reload_stats_attr, *act_info, *act_stats;
543         int i, j, stat_idx;
544         u32 value;
545 
546         if (!is_remote)
547                 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
548         else
549                 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
550 
551         if (!reload_stats_attr)
552                 return -EMSGSIZE;
553 
554         for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
555                 if ((!is_remote &&
556                      !devlink_reload_action_is_supported(devlink, i)) ||
557                     i == DEVLINK_RELOAD_ACTION_UNSPEC)
558                         continue;
559                 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
560                 if (!act_info)
561                         goto nla_put_failure;
562 
563                 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
564                         goto action_info_nest_cancel;
565                 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
566                 if (!act_stats)
567                         goto action_info_nest_cancel;
568 
569                 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
570                         /* Remote stats are shown even if not locally supported.
571                          * Stats of actions with unspecified limit are shown
572                          * though drivers don't need to register unspecified
573                          * limit.
574                          */
575                         if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
576                              !devlink_reload_limit_is_supported(devlink, j)) ||
577                             devlink_reload_combination_is_invalid(i, j))
578                                 continue;
579 
580                         stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
581                         if (!is_remote)
582                                 value = devlink->stats.reload_stats[stat_idx];
583                         else
584                                 value = devlink->stats.remote_reload_stats[stat_idx];
585                         if (devlink_reload_stat_put(msg, j, value))
586                                 goto action_stats_nest_cancel;
587                 }
588                 nla_nest_end(msg, act_stats);
589                 nla_nest_end(msg, act_info);
590         }
591         nla_nest_end(msg, reload_stats_attr);
592         return 0;
593 
594 action_stats_nest_cancel:
595         nla_nest_cancel(msg, act_stats);
596 action_info_nest_cancel:
597         nla_nest_cancel(msg, act_info);
598 nla_put_failure:
599         nla_nest_cancel(msg, reload_stats_attr);
600         return -EMSGSIZE;
601 }
602 
603 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
604                            enum devlink_command cmd, u32 portid,
605                            u32 seq, int flags)
606 {
607         struct nlattr *dev_stats;
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_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
617                 goto nla_put_failure;
618 
619         dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
620         if (!dev_stats)
621                 goto nla_put_failure;
622 
623         if (devlink_reload_stats_put(msg, devlink, false))
624                 goto dev_stats_nest_cancel;
625         if (devlink_reload_stats_put(msg, devlink, true))
626                 goto dev_stats_nest_cancel;
627 
628         nla_nest_end(msg, dev_stats);
629         genlmsg_end(msg, hdr);
630         return 0;
631 
632 dev_stats_nest_cancel:
633         nla_nest_cancel(msg, dev_stats);
634 nla_put_failure:
635         genlmsg_cancel(msg, hdr);
636         return -EMSGSIZE;
637 }
638 
639 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
640 {
641         struct sk_buff *msg;
642         int err;
643 
644         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
645 
646         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
647         if (!msg)
648                 return;
649 
650         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
651         if (err) {
652                 nlmsg_free(msg);
653                 return;
654         }
655 
656         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
657                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
658 }
659 
660 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
661                                      struct devlink_port *devlink_port)
662 {
663         struct devlink_port_attrs *attrs = &devlink_port->attrs;
664 
665         if (!devlink_port->attrs_set)
666                 return 0;
667         if (attrs->lanes) {
668                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
669                         return -EMSGSIZE;
670         }
671         if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
672                 return -EMSGSIZE;
673         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
674                 return -EMSGSIZE;
675         switch (devlink_port->attrs.flavour) {
676         case DEVLINK_PORT_FLAVOUR_PCI_PF:
677                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
678                                 attrs->pci_pf.controller) ||
679                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
680                         return -EMSGSIZE;
681                 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
682                         return -EMSGSIZE;
683                 break;
684         case DEVLINK_PORT_FLAVOUR_PCI_VF:
685                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
686                                 attrs->pci_vf.controller) ||
687                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
688                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
689                         return -EMSGSIZE;
690                 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
691                         return -EMSGSIZE;
692                 break;
693         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
694         case DEVLINK_PORT_FLAVOUR_CPU:
695         case DEVLINK_PORT_FLAVOUR_DSA:
696         case DEVLINK_PORT_FLAVOUR_VIRTUAL:
697                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
698                                 attrs->phys.port_number))
699                         return -EMSGSIZE;
700                 if (!attrs->split)
701                         return 0;
702                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
703                                 attrs->phys.port_number))
704                         return -EMSGSIZE;
705                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
706                                 attrs->phys.split_subport_number))
707                         return -EMSGSIZE;
708                 break;
709         default:
710                 break;
711         }
712         return 0;
713 }
714 
715 static int
716 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
717                                    struct netlink_ext_ack *extack)
718 {
719         struct devlink *devlink = port->devlink;
720         const struct devlink_ops *ops;
721         struct nlattr *function_attr;
722         bool empty_nest = true;
723         int err = 0;
724 
725         function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
726         if (!function_attr)
727                 return -EMSGSIZE;
728 
729         ops = devlink->ops;
730         if (ops->port_function_hw_addr_get) {
731                 int hw_addr_len;
732                 u8 hw_addr[MAX_ADDR_LEN];
733 
734                 err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack);
735                 if (err == -EOPNOTSUPP) {
736                         /* Port function attributes are optional for a port. If port doesn't
737                          * support function attribute, returning -EOPNOTSUPP is not an error.
738                          */
739                         err = 0;
740                         goto out;
741                 } else if (err) {
742                         goto out;
743                 }
744                 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
745                 if (err)
746                         goto out;
747                 empty_nest = false;
748         }
749 
750 out:
751         if (err || empty_nest)
752                 nla_nest_cancel(msg, function_attr);
753         else
754                 nla_nest_end(msg, function_attr);
755         return err;
756 }
757 
758 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
759                                 struct devlink_port *devlink_port,
760                                 enum devlink_command cmd, u32 portid,
761                                 u32 seq, int flags,
762                                 struct netlink_ext_ack *extack)
763 {
764         void *hdr;
765 
766         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
767         if (!hdr)
768                 return -EMSGSIZE;
769 
770         if (devlink_nl_put_handle(msg, devlink))
771                 goto nla_put_failure;
772         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
773                 goto nla_put_failure;
774 
775         /* Hold rtnl lock while accessing port's netdev attributes. */
776         rtnl_lock();
777         spin_lock_bh(&devlink_port->type_lock);
778         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
779                 goto nla_put_failure_type_locked;
780         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
781             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
782                         devlink_port->desired_type))
783                 goto nla_put_failure_type_locked;
784         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
785                 struct net *net = devlink_net(devlink_port->devlink);
786                 struct net_device *netdev = devlink_port->type_dev;
787 
788                 if (netdev && net_eq(net, dev_net(netdev)) &&
789                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
790                                  netdev->ifindex) ||
791                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
792                                     netdev->name)))
793                         goto nla_put_failure_type_locked;
794         }
795         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
796                 struct ib_device *ibdev = devlink_port->type_dev;
797 
798                 if (ibdev &&
799                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
800                                    ibdev->name))
801                         goto nla_put_failure_type_locked;
802         }
803         spin_unlock_bh(&devlink_port->type_lock);
804         rtnl_unlock();
805         if (devlink_nl_port_attrs_put(msg, devlink_port))
806                 goto nla_put_failure;
807         if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
808                 goto nla_put_failure;
809 
810         genlmsg_end(msg, hdr);
811         return 0;
812 
813 nla_put_failure_type_locked:
814         spin_unlock_bh(&devlink_port->type_lock);
815         rtnl_unlock();
816 nla_put_failure:
817         genlmsg_cancel(msg, hdr);
818         return -EMSGSIZE;
819 }
820 
821 static void devlink_port_notify(struct devlink_port *devlink_port,
822                                 enum devlink_command cmd)
823 {
824         struct devlink *devlink = devlink_port->devlink;
825         struct sk_buff *msg;
826         int err;
827 
828         if (!devlink_port->registered)
829                 return;
830 
831         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
832 
833         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
834         if (!msg)
835                 return;
836 
837         err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0,
838                                    NULL);
839         if (err) {
840                 nlmsg_free(msg);
841                 return;
842         }
843 
844         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
845                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
846 }
847 
848 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
849 {
850         struct devlink *devlink = info->user_ptr[0];
851         struct sk_buff *msg;
852         int err;
853 
854         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
855         if (!msg)
856                 return -ENOMEM;
857 
858         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
859                               info->snd_portid, info->snd_seq, 0);
860         if (err) {
861                 nlmsg_free(msg);
862                 return err;
863         }
864 
865         return genlmsg_reply(msg, info);
866 }
867 
868 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
869                                      struct netlink_callback *cb)
870 {
871         struct devlink *devlink;
872         int start = cb->args[0];
873         int idx = 0;
874         int err;
875 
876         mutex_lock(&devlink_mutex);
877         list_for_each_entry(devlink, &devlink_list, list) {
878                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
879                         continue;
880                 if (idx < start) {
881                         idx++;
882                         continue;
883                 }
884                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
885                                       NETLINK_CB(cb->skb).portid,
886                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
887                 if (err)
888                         goto out;
889                 idx++;
890         }
891 out:
892         mutex_unlock(&devlink_mutex);
893 
894         cb->args[0] = idx;
895         return msg->len;
896 }
897 
898 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
899                                         struct genl_info *info)
900 {
901         struct devlink_port *devlink_port = info->user_ptr[1];
902         struct devlink *devlink = devlink_port->devlink;
903         struct sk_buff *msg;
904         int err;
905 
906         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
907         if (!msg)
908                 return -ENOMEM;
909 
910         err = devlink_nl_port_fill(msg, devlink, devlink_port,
911                                    DEVLINK_CMD_PORT_NEW,
912                                    info->snd_portid, info->snd_seq, 0,
913                                    info->extack);
914         if (err) {
915                 nlmsg_free(msg);
916                 return err;
917         }
918 
919         return genlmsg_reply(msg, info);
920 }
921 
922 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
923                                           struct netlink_callback *cb)
924 {
925         struct devlink *devlink;
926         struct devlink_port *devlink_port;
927         int start = cb->args[0];
928         int idx = 0;
929         int err;
930 
931         mutex_lock(&devlink_mutex);
932         list_for_each_entry(devlink, &devlink_list, list) {
933                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
934                         continue;
935                 mutex_lock(&devlink->lock);
936                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
937                         if (idx < start) {
938                                 idx++;
939                                 continue;
940                         }
941                         err = devlink_nl_port_fill(msg, devlink, devlink_port,
942                                                    DEVLINK_CMD_NEW,
943                                                    NETLINK_CB(cb->skb).portid,
944                                                    cb->nlh->nlmsg_seq,
945                                                    NLM_F_MULTI,
946                                                    cb->extack);
947                         if (err) {
948                                 mutex_unlock(&devlink->lock);
949                                 goto out;
950                         }
951                         idx++;
952                 }
953                 mutex_unlock(&devlink->lock);
954         }
955 out:
956         mutex_unlock(&devlink_mutex);
957 
958         cb->args[0] = idx;
959         return msg->len;
960 }
961 
962 static int devlink_port_type_set(struct devlink *devlink,
963                                  struct devlink_port *devlink_port,
964                                  enum devlink_port_type port_type)
965 
966 {
967         int err;
968 
969         if (devlink->ops->port_type_set) {
970                 if (port_type == devlink_port->type)
971                         return 0;
972                 err = devlink->ops->port_type_set(devlink_port, port_type);
973                 if (err)
974                         return err;
975                 devlink_port->desired_type = port_type;
976                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
977                 return 0;
978         }
979         return -EOPNOTSUPP;
980 }
981 
982 static int
983 devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port,
984                                   const struct nlattr *attr, struct netlink_ext_ack *extack)
985 {
986         const struct devlink_ops *ops;
987         const u8 *hw_addr;
988         int hw_addr_len;
989         int err;
990 
991         hw_addr = nla_data(attr);
992         hw_addr_len = nla_len(attr);
993         if (hw_addr_len > MAX_ADDR_LEN) {
994                 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
995                 return -EINVAL;
996         }
997         if (port->type == DEVLINK_PORT_TYPE_ETH) {
998                 if (hw_addr_len != ETH_ALEN) {
999                         NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1000                         return -EINVAL;
1001                 }
1002                 if (!is_unicast_ether_addr(hw_addr)) {
1003                         NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1004                         return -EINVAL;
1005                 }
1006         }
1007 
1008         ops = devlink->ops;
1009         if (!ops->port_function_hw_addr_set) {
1010                 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1011                 return -EOPNOTSUPP;
1012         }
1013 
1014         err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack);
1015         if (err)
1016                 return err;
1017 
1018         devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1019         return 0;
1020 }
1021 
1022 static int
1023 devlink_port_function_set(struct devlink *devlink, struct devlink_port *port,
1024                           const struct nlattr *attr, struct netlink_ext_ack *extack)
1025 {
1026         struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1027         int err;
1028 
1029         err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1030                                devlink_function_nl_policy, extack);
1031         if (err < 0) {
1032                 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1033                 return err;
1034         }
1035 
1036         attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1037         if (attr)
1038                 err = devlink_port_function_hw_addr_set(devlink, port, attr, extack);
1039 
1040         return err;
1041 }
1042 
1043 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1044                                         struct genl_info *info)
1045 {
1046         struct devlink_port *devlink_port = info->user_ptr[1];
1047         struct devlink *devlink = devlink_port->devlink;
1048         int err;
1049 
1050         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1051                 enum devlink_port_type port_type;
1052 
1053                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1054                 err = devlink_port_type_set(devlink, devlink_port, port_type);
1055                 if (err)
1056                         return err;
1057         }
1058 
1059         if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1060                 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1061                 struct netlink_ext_ack *extack = info->extack;
1062 
1063                 err = devlink_port_function_set(devlink, devlink_port, attr, extack);
1064                 if (err)
1065                         return err;
1066         }
1067 
1068         return 0;
1069 }
1070 
1071 static int devlink_port_split(struct devlink *devlink, u32 port_index,
1072                               u32 count, struct netlink_ext_ack *extack)
1073 
1074 {
1075         if (devlink->ops->port_split)
1076                 return devlink->ops->port_split(devlink, port_index, count,
1077                                                 extack);
1078         return -EOPNOTSUPP;
1079 }
1080 
1081 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1082                                           struct genl_info *info)
1083 {
1084         struct devlink *devlink = info->user_ptr[0];
1085         struct devlink_port *devlink_port;
1086         u32 port_index;
1087         u32 count;
1088 
1089         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
1090             !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1091                 return -EINVAL;
1092 
1093         devlink_port = devlink_port_get_from_info(devlink, info);
1094         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1095         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1096 
1097         if (IS_ERR(devlink_port))
1098                 return -EINVAL;
1099 
1100         if (!devlink_port->attrs.splittable) {
1101                 /* Split ports cannot be split. */
1102                 if (devlink_port->attrs.split)
1103                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1104                 else
1105                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1106                 return -EINVAL;
1107         }
1108 
1109         if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1110                 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count"