1/*
2 * net/core/devlink.c - Network physical/parent device Netlink interface
3 *
4 * Heavily inspired by net/wireless/
5 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/gfp.h>
19#include <linux/device.h>
20#include <linux/list.h>
21#include <linux/netdevice.h>
22#include <rdma/ib_verbs.h>
23#include <net/netlink.h>
24#include <net/genetlink.h>
25#include <net/rtnetlink.h>
26#include <net/net_namespace.h>
27#include <net/sock.h>
28#include <net/devlink.h>
29#define CREATE_TRACE_POINTS
30#include <trace/events/devlink.h>
31
32static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
33 {
34 .name = "destination mac",
35 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
36 .bitwidth = 48,
37 },
38};
39
40struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
41 .name = "ethernet",
42 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
43 .fields = devlink_dpipe_fields_ethernet,
44 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
45 .global = true,
46};
47EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
48
49static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
50 {
51 .name = "destination ip",
52 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
53 .bitwidth = 32,
54 },
55};
56
57struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
58 .name = "ipv4",
59 .id = DEVLINK_DPIPE_HEADER_IPV4,
60 .fields = devlink_dpipe_fields_ipv4,
61 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
62 .global = true,
63};
64EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
65
66static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
67 {
68 .name = "destination ip",
69 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
70 .bitwidth = 128,
71 },
72};
73
74struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
75 .name = "ipv6",
76 .id = DEVLINK_DPIPE_HEADER_IPV6,
77 .fields = devlink_dpipe_fields_ipv6,
78 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
79 .global = true,
80};
81EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
82
83EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
84EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
85
86static LIST_HEAD(devlink_list);
87
88/* devlink_mutex
89 *
90 * An overall lock guarding every operation coming from userspace.
91 * It also guards devlink devices list and it is taken when
92 * driver registers/unregisters it.
93 */
94static DEFINE_MUTEX(devlink_mutex);
95
96static struct net *devlink_net(const struct devlink *devlink)
97{
98 return read_pnet(&devlink->_net);
99}
100
101static void devlink_net_set(struct devlink *devlink, struct net *net)
102{
103 write_pnet(&devlink->_net, net);
104}
105
106static struct devlink *devlink_get_from_attrs(struct net *net,
107 struct nlattr **attrs)
108{
109 struct devlink *devlink;
110 char *busname;
111 char *devname;
112
113 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
114 return ERR_PTR(-EINVAL);
115
116 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
117 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
118
119 lockdep_assert_held(&devlink_mutex);
120
121 list_for_each_entry(devlink, &devlink_list, list) {
122 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
123 strcmp(dev_name(devlink->dev), devname) == 0 &&
124 net_eq(devlink_net(devlink), net))
125 return devlink;
126 }
127
128 return ERR_PTR(-ENODEV);
129}
130
131static struct devlink *devlink_get_from_info(struct genl_info *info)
132{
133 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
134}
135
136static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
137 int port_index)
138{
139 struct devlink_port *devlink_port;
140
141 list_for_each_entry(devlink_port, &devlink->port_list, list) {
142 if (devlink_port->index == port_index)
143 return devlink_port;
144 }
145 return NULL;
146}
147
148static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
149{
150 return devlink_port_get_by_index(devlink, port_index);
151}
152
153static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
154 struct nlattr **attrs)
155{
156 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
157 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
158 struct devlink_port *devlink_port;
159
160 devlink_port = devlink_port_get_by_index(devlink, port_index);
161 if (!devlink_port)
162 return ERR_PTR(-ENODEV);
163 return devlink_port;
164 }
165 return ERR_PTR(-EINVAL);
166}
167
168static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
169 struct genl_info *info)
170{
171 return devlink_port_get_from_attrs(devlink, info->attrs);
172}
173
174struct devlink_sb {
175 struct list_head list;
176 unsigned int index;
177 u32 size;
178 u16 ingress_pools_count;
179 u16 egress_pools_count;
180 u16 ingress_tc_count;
181 u16 egress_tc_count;
182};
183
184static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
185{
186 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
187}
188
189static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
190 unsigned int sb_index)
191{
192 struct devlink_sb *devlink_sb;
193
194 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
195 if (devlink_sb->index == sb_index)
196 return devlink_sb;
197 }
198 return NULL;
199}
200
201static bool devlink_sb_index_exists(struct devlink *devlink,
202 unsigned int sb_index)
203{
204 return devlink_sb_get_by_index(devlink, sb_index);
205}
206
207static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
208 struct nlattr **attrs)
209{
210 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
211 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
212 struct devlink_sb *devlink_sb;
213
214 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
215 if (!devlink_sb)
216 return ERR_PTR(-ENODEV);
217 return devlink_sb;
218 }
219 return ERR_PTR(-EINVAL);
220}
221
222static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
223 struct genl_info *info)
224{
225 return devlink_sb_get_from_attrs(devlink, info->attrs);
226}
227
228static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
229 struct nlattr **attrs,
230 u16 *p_pool_index)
231{
232 u16 val;
233
234 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
235 return -EINVAL;
236
237 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
238 if (val >= devlink_sb_pool_count(devlink_sb))
239 return -EINVAL;
240 *p_pool_index = val;
241 return 0;
242}
243
244static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
245 struct genl_info *info,
246 u16 *p_pool_index)
247{
248 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
249 p_pool_index);
250}
251
252static int
253devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
254 enum devlink_sb_pool_type *p_pool_type)
255{
256 u8 val;
257
258 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
259 return -EINVAL;
260
261 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
262 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
263 val != DEVLINK_SB_POOL_TYPE_EGRESS)
264 return -EINVAL;
265 *p_pool_type = val;
266 return 0;
267}
268
269static int
270devlink_sb_pool_type_get_from_info(struct genl_info *info,
271 enum devlink_sb_pool_type *p_pool_type)
272{
273 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
274}
275
276static int
277devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
278 enum devlink_sb_threshold_type *p_th_type)
279{
280 u8 val;
281
282 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
283 return -EINVAL;
284
285 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
286 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
287 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
288 return -EINVAL;
289 *p_th_type = val;
290 return 0;
291}
292
293static int
294devlink_sb_th_type_get_from_info(struct genl_info *info,
295 enum devlink_sb_threshold_type *p_th_type)
296{
297 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
298}
299
300static int
301devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
302 struct nlattr **attrs,
303 enum devlink_sb_pool_type pool_type,
304 u16 *p_tc_index)
305{
306 u16 val;
307
308 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
309 return -EINVAL;
310
311 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
312 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
313 val >= devlink_sb->ingress_tc_count)
314 return -EINVAL;
315 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
316 val >= devlink_sb->egress_tc_count)
317 return -EINVAL;
318 *p_tc_index = val;
319 return 0;
320}
321
322static int
323devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
324 struct genl_info *info,
325 enum devlink_sb_pool_type pool_type,
326 u16 *p_tc_index)
327{
328 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
329 pool_type, p_tc_index);
330}
331
332struct devlink_region {
333 struct devlink *devlink;
334 struct list_head list;
335 const char *name;
336 struct list_head snapshot_list;
337 u32 max_snapshots;
338 u32 cur_snapshots;
339 u64 size;
340};
341
342struct devlink_snapshot {
343 struct list_head list;
344 struct devlink_region *region;
345 devlink_snapshot_data_dest_t *data_destructor;
346 u64 data_len;
347 u8 *data;
348 u32 id;
349};
350
351static struct devlink_region *
352devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
353{
354 struct devlink_region *region;
355
356 list_for_each_entry(region, &devlink->region_list, list)
357 if (!strcmp(region->name, region_name))
358 return region;
359
360 return NULL;
361}
362
363static struct devlink_snapshot *
364devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
365{
366 struct devlink_snapshot *snapshot;
367
368 list_for_each_entry(snapshot, &region->snapshot_list, list)
369 if (snapshot->id == id)
370 return snapshot;
371
372 return NULL;
373}
374
375static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
376{
377 snapshot->region->cur_snapshots--;
378 list_del(&snapshot->list);
379 (*snapshot->data_destructor)(snapshot->data);
380 kfree(snapshot);
381}
382
383#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
384#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
385#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
386
387/* The per devlink instance lock is taken by default in the pre-doit
388 * operation, yet several commands do not require this. The global
389 * devlink lock is taken and protects from disruption by user-calls.
390 */
391#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
392
393static int devlink_nl_pre_doit(const struct genl_ops *ops,
394 struct sk_buff *skb, struct genl_info *info)
395{
396 struct devlink *devlink;
397 int err;
398
399 mutex_lock(&devlink_mutex);
400 devlink = devlink_get_from_info(info);
401 if (IS_ERR(devlink)) {
402 mutex_unlock(&devlink_mutex);
403 return PTR_ERR(devlink);
404 }
405 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
406 mutex_lock(&devlink->lock);
407 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
408 info->user_ptr[0] = devlink;
409 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
410 struct devlink_port *devlink_port;
411
412 devlink_port = devlink_port_get_from_info(devlink, info);
413 if (IS_ERR(devlink_port)) {
414 err = PTR_ERR(devlink_port);
415 goto unlock;
416 }
417 info->user_ptr[0] = devlink_port;
418 }
419 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
420 struct devlink_sb *devlink_sb;
421
422 devlink_sb = devlink_sb_get_from_info(devlink, info);
423 if (IS_ERR(devlink_sb)) {
424 err = PTR_ERR(devlink_sb);
425 goto unlock;
426 }
427 info->user_ptr[1] = devlink_sb;
428 }
429 return 0;
430
431unlock:
432 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
433 mutex_unlock(&devlink->lock);
434 mutex_unlock(&devlink_mutex);
435 return err;
436}
437
438static void devlink_nl_post_doit(const struct genl_ops *ops,
439 struct sk_buff *skb, struct genl_info *info)
440{
441 struct devlink *devlink;
442
443 devlink = devlink_get_from_info(info);
444 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
445 mutex_unlock(&devlink->lock);
446 mutex_unlock(&devlink_mutex);
447}
448
449static struct genl_family devlink_nl_family;
450
451enum devlink_multicast_groups {
452 DEVLINK_MCGRP_CONFIG,
453};
454
455static const struct genl_multicast_group devlink_nl_mcgrps[] = {
456 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
457};
458
459static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
460{
461 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
462 return -EMSGSIZE;
463 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
464 return -EMSGSIZE;
465 return 0;
466}
467
468static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
469 enum devlink_command cmd, u32 portid,
470 u32 seq, int flags)
471{
472 void *hdr;
473
474 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
475 if (!hdr)
476 return -EMSGSIZE;
477
478 if (devlink_nl_put_handle(msg, devlink))
479 goto nla_put_failure;
480
481 genlmsg_end(msg, hdr);
482 return 0;
483
484nla_put_failure:
485 genlmsg_cancel(msg, hdr);
486 return -EMSGSIZE;
487}
488
489static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
490{
491 struct sk_buff *msg;
492 int err;
493
494 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
495
496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
497 if (!msg)
498 return;
499
500 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
501 if (err) {
502 nlmsg_free(msg);
503 return;
504 }
505
506 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
507 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
508}
509
510static int devlink_nl_port_attrs_put(struct sk_buff *msg,
511 struct devlink_port *devlink_port)
512{
513 struct devlink_port_attrs *attrs = &devlink_port->attrs;
514
515 if (!attrs->set)
516 return 0;
517 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
518 return -EMSGSIZE;
519 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
520 return -EMSGSIZE;
521 if (!attrs->split)
522 return 0;
523 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
524 return -EMSGSIZE;
525 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
526 attrs->split_subport_number))
527 return -EMSGSIZE;
528 return 0;
529}
530
531static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
532 struct devlink_port *devlink_port,
533 enum devlink_command cmd, u32 portid,
534 u32 seq, int flags)
535{
536 void *hdr;
537
538 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
539 if (!hdr)
540 return -EMSGSIZE;
541
542 if (devlink_nl_put_handle(msg, devlink))
543 goto nla_put_failure;
544 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
545 goto nla_put_failure;
546 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
547 goto nla_put_failure;
548 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
549 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
550 devlink_port->desired_type))
551 goto nla_put_failure;
552 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
553 struct net_device *netdev = devlink_port->type_dev;
554
555 if (netdev &&
556 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
557 netdev->ifindex) ||
558 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
559 netdev->name)))
560 goto nla_put_failure;
561 }
562 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
563 struct ib_device *ibdev = devlink_port->type_dev;
564
565 if (ibdev &&
566 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
567 ibdev->name))
568 goto nla_put_failure;
569 }
570 if (devlink_nl_port_attrs_put(msg, devlink_port))
571 goto nla_put_failure;
572
573 genlmsg_end(msg, hdr);
574 return 0;
575
576nla_put_failure:
577 genlmsg_cancel(msg, hdr);
578 return -EMSGSIZE;
579}
580
581static void devlink_port_notify(struct devlink_port *devlink_port,
582 enum devlink_command cmd)
583{
584 struct devlink *devlink = devlink_port->devlink;
585 struct sk_buff *msg;
586 int err;
587
588 if (!devlink_port->registered)
589 return;
590
591 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
592
593 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
594 if (!msg)
595 return;
596
597 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
598 if (err) {
599 nlmsg_free(msg);
600 return;
601 }
602
603 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
604 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
605}
606
607static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
608{
609 struct devlink *devlink = info->user_ptr[0];
610 struct sk_buff *msg;
611 int err;
612
613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
614 if (!msg)
615 return -ENOMEM;
616
617 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
618 info->snd_portid, info->snd_seq, 0);
619 if (err) {
620 nlmsg_free(msg);
621 return err;
622 }
623
624 return genlmsg_reply(msg, info);
625}
626
627static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
628 struct netlink_callback *cb)
629{
630 struct devlink *devlink;
631 int start = cb->args[0];
632 int idx = 0;
633 int err;
634
635 mutex_lock(&devlink_mutex);
636 list_for_each_entry(devlink, &devlink_list, list) {
637 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
638 continue;
639 if (idx < start) {
640 idx++;
641 continue;
642 }
643 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
644 NETLINK_CB(cb->skb).portid,
645 cb->nlh->nlmsg_seq, NLM_F_MULTI);
646 if (err)
647 goto out;
648 idx++;
649 }
650out:
651 mutex_unlock(&devlink_mutex);
652
653 cb->args[0] = idx;
654 return msg->len;
655}
656
657static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
658 struct genl_info *info)
659{
660 struct devlink_port *devlink_port = info->user_ptr[0];
661 struct devlink *devlink = devlink_port->devlink;
662 struct sk_buff *msg;
663 int err;
664
665 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
666 if (!msg)
667 return -ENOMEM;
668
669 err = devlink_nl_port_fill(msg, devlink, devlink_port,
670 DEVLINK_CMD_PORT_NEW,
671 info->snd_portid, info->snd_seq, 0);
672 if (err) {
673 nlmsg_free(msg);
674 return err;
675 }
676
677 return genlmsg_reply(msg, info);
678}
679
680static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
681 struct netlink_callback *cb)
682{
683 struct devlink *devlink;
684 struct devlink_port *devlink_port;
685 int start = cb->args[0];
686 int idx = 0;
687 int err;
688
689 mutex_lock(&devlink_mutex);
690 list_for_each_entry(devlink, &devlink_list, list) {
691 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
692 continue;
693 mutex_lock(&devlink->lock);
694 list_for_each_entry(devlink_port, &devlink->port_list, list) {
695 if (idx < start) {
696 idx++;
697 continue;
698 }
699 err = devlink_nl_port_fill(msg, devlink, devlink_port,
700 DEVLINK_CMD_NEW,
701 NETLINK_CB(cb->skb).portid,
702 cb->nlh->nlmsg_seq,
703 NLM_F_MULTI);
704 if (err) {
705 mutex_unlock(&devlink->lock);
706 goto out;
707 }
708 idx++;
709 }
710 mutex_unlock(&devlink->lock);
711 }
712out:
713 mutex_unlock(&devlink_mutex);
714
715 cb->args[0] = idx;
716 return msg->len;
717}
718
719static int devlink_port_type_set(struct devlink *devlink,
720 struct devlink_port *devlink_port,
721 enum devlink_port_type port_type)
722
723{
724 int err;
725
726 if (devlink->ops->port_type_set) {
727 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
728 return -EINVAL;
729 if (port_type == devlink_port->type)
730 return 0;
731 err = devlink->ops->port_type_set(devlink_port, port_type);
732 if (err)
733 return err;
734 devlink_port->desired_type = port_type;
735 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
736 return 0;
737 }
738 return -EOPNOTSUPP;
739}
740
741static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
742 struct genl_info *info)
743{
744 struct devlink_port *devlink_port = info->user_ptr[0];
745 struct devlink *devlink = devlink_port->devlink;
746 int err;
747
748 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
749 enum devlink_port_type port_type;
750
751 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
752 err = devlink_port_type_set(devlink, devlink_port, port_type);
753 if (err)
754 return err;
755 }
756 return 0;
757}
758
759static int devlink_port_split(struct devlink *devlink, u32 port_index,
760 u32 count, struct netlink_ext_ack *extack)
761
762{
763 if (devlink->ops->port_split)
764 return devlink->ops->port_split(devlink, port_index, count,
765 extack);
766 return -EOPNOTSUPP;
767}
768
769static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
770 struct genl_info *info)
771{
772 struct devlink *devlink = info->user_ptr[0];
773 u32 port_index;
774 u32 count;
775
776 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
777 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
778 return -EINVAL;
779
780 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
781 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
782 return devlink_port_split(devlink, port_index, count, info->extack);
783}
784
785static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
786 struct netlink_ext_ack *extack)
787
788{
789 if (devlink->ops->port_unsplit)
790 return devlink->ops->port_unsplit(devlink, port_index, extack);
791 return -EOPNOTSUPP;
792}
793
794static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
795 struct genl_info *info)
796{
797 struct devlink *devlink = info->user_ptr[0];
798 u32 port_index;
799
800 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
801 return -EINVAL;
802
803 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
804 return devlink_port_unsplit(devlink, port_index, info->extack);
805}
806
807static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
808 struct devlink_sb *devlink_sb,
809 enum devlink_command cmd, u32 portid,
810 u32 seq, int flags)
811{
812 void *hdr;
813
814 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
815 if (!hdr)
816 return -EMSGSIZE;
817
818 if (devlink_nl_put_handle(msg, devlink))
819 goto nla_put_failure;
820 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
821 goto nla_put_failure;
822 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
823 goto nla_put_failure;
824 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
825 devlink_sb->ingress_pools_count))
826 goto nla_put_failure;
827 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
828 devlink_sb->egress_pools_count))
829 goto nla_put_failure;
830 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
831 devlink_sb->ingress_tc_count))
832 goto nla_put_failure;
833 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
834 devlink_sb->egress_tc_count))
835 goto nla_put_failure;
836
837 genlmsg_end(msg, hdr);
838 return 0;
839
840nla_put_failure:
841 genlmsg_cancel(msg, hdr);
842 return -EMSGSIZE;
843}
844
845static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
846 struct genl_info *info)
847{
848 struct devlink *devlink = info->user_ptr[0];
849 struct devlink_sb *devlink_sb = info->user_ptr[1];
850 struct sk_buff *msg;
851 int err;
852
853 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
854 if (!msg)
855 return -ENOMEM;
856
857 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
858 DEVLINK_CMD_SB_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
868static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
869 struct netlink_callback *cb)
870{
871 struct devlink *devlink;
872 struct devlink_sb *devlink_sb;
873 int start = cb->args[0];
874 int idx = 0;
875 int err;
876
877 mutex_lock(&devlink_mutex);
878 list_for_each_entry(devlink, &devlink_list, list) {
879 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
880 continue;
881 mutex_lock(&devlink->lock);
882 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
883 if (idx < start) {
884 idx++;
885 continue;
886 }
887 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
888 DEVLINK_CMD_SB_NEW,
889 NETLINK_CB(cb->skb).portid,
890 cb->nlh->nlmsg_seq,
891 NLM_F_MULTI);
892 if (err) {
893 mutex_unlock(&devlink->lock);
894 goto out;
895 }
896 idx++;
897 }
898 mutex_unlock(&devlink->lock);
899 }
900out:
901 mutex_unlock(&devlink_mutex);
902
903 cb->args[0] = idx;
904 return msg->len;
905}
906
907static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
908 struct devlink_sb *devlink_sb,
909 u16 pool_index, enum devlink_command cmd,
910 u32 portid, u32 seq, int flags)
911{
912 struct devlink_sb_pool_info pool_info;
913 void *hdr;
914 int err;
915
916 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
917 pool_index, &pool_info);
918 if (err)
919 return err;
920
921 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
922 if (!hdr)
923 return -EMSGSIZE;
924
925 if (devlink_nl_put_handle(msg, devlink))
926 goto nla_put_failure;
927 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
928 goto nla_put_failure;
929 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
930 goto nla_put_failure;
931 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
932 goto nla_put_failure;
933 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
934 goto nla_put_failure;
935 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
936 pool_info.threshold_type))
937 goto nla_put_failure;
938 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
939 pool_info.cell_size))
940 goto nla_put_failure;
941
942 genlmsg_end(msg, hdr);
943 return 0;
944
945nla_put_failure:
946 genlmsg_cancel(msg, hdr);
947 return -EMSGSIZE;
948}
949
950static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
951 struct genl_info *info)
952{
953 struct devlink *devlink = info->user_ptr[0];
954 struct devlink_sb *devlink_sb = info->user_ptr[1];
955 struct sk_buff *msg;
956 u16 pool_index;
957 int err;
958
959 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
960 &pool_index);
961 if (err)
962 return err;
963
964 if (!devlink->ops->sb_pool_get)
965 return -EOPNOTSUPP;
966
967 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
968 if (!msg)
969 return -ENOMEM;
970
971 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
972 DEVLINK_CMD_SB_POOL_NEW,
973 info->snd_portid, info->snd_seq, 0);
974 if (err) {
975 nlmsg_free(msg);
976 return err;
977 }
978
979 return genlmsg_reply(msg, info);
980}
981
982static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
983 struct devlink *devlink,
984 struct devlink_sb *devlink_sb,
985 u32 portid, u32 seq)
986{
987 u16 pool_count = devlink_sb_pool_count(devlink_sb);
988 u16 pool_index;
989 int err;
990
991 for (pool_index = 0; pool_index < pool_count; pool_index++) {
992 if (*p_idx < start) {
993 (*p_idx)++;
994 continue;
995 }
996 err = devlink_nl_sb_pool_fill(msg, devlink,
997 devlink_sb,
998 pool_index,
999 DEVLINK_CMD_SB_POOL_NEW,
1000 portid, seq, NLM_F_MULTI);
1001 if (err)
1002 return err;
1003 (*p_idx)++;
1004 }
1005 return 0;
1006}
1007
1008static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1009 struct netlink_callback *cb)
1010{
1011 struct devlink *devlink;
1012 struct devlink_sb *devlink_sb;
1013 int start = cb->args[0];
1014 int idx = 0;
1015 int err;
1016
1017 mutex_lock(&devlink_mutex);
1018 list_for_each_entry(devlink, &devlink_list, list) {
1019 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1020 !devlink->ops->sb_pool_get)
1021 continue;
1022 mutex_lock(&devlink->lock);
1023 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1024 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1025 devlink_sb,
1026 NETLINK_CB(cb->skb).portid,
1027 cb->nlh->nlmsg_seq);
1028 if (err && err != -EOPNOTSUPP) {
1029 mutex_unlock(&devlink->lock);
1030 goto out;
1031 }
1032 }
1033 mutex_unlock(&devlink->lock);
1034 }
1035out:
1036 mutex_unlock(&devlink_mutex);
1037
1038 cb->args[0] = idx;
1039 return msg->len;
1040}
1041
1042static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1043 u16 pool_index, u32 size,
1044 enum devlink_sb_threshold_type threshold_type)
1045
1046{
1047 const struct devlink_ops *ops = devlink->ops;
1048
1049 if (ops->sb_pool_set)
1050 return ops->sb_pool_set(devlink, sb_index, pool_index,
1051 size, threshold_type);
1052 return -EOPNOTSUPP;
1053}
1054
1055static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1056 struct genl_info *info)
1057{
1058 struct devlink *devlink = info->user_ptr[0];
1059 struct devlink_sb *devlink_sb = info->user_ptr[1];
1060 enum devlink_sb_threshold_type threshold_type;
1061 u16 pool_index;
1062 u32 size;
1063 int err;
1064
1065 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1066 &pool_index);
1067 if (err)
1068 return err;
1069
1070 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1071 if (err)
1072 return err;
1073
1074 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1075 return -EINVAL;
1076
1077 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1078 return devlink_sb_pool_set(devlink, devlink_sb->index,
1079 pool_index, size, threshold_type);
1080}
1081
1082static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1083 struct devlink *devlink,
1084 struct devlink_port *devlink_port,
1085 struct devlink_sb *devlink_sb,
1086 u16 pool_index,
1087 enum devlink_command cmd,
1088 u32 portid, u32 seq, int flags)
1089{
1090 const struct devlink_ops *ops = devlink->ops;
1091 u32 threshold;
1092 void *hdr;
1093 int err;
1094
1095 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1096 pool_index, &threshold);
1097 if (err)
1098 return err;
1099
1100 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1101 if (!hdr)
1102 return -EMSGSIZE;
1103
1104 if (devlink_nl_put_handle(msg, devlink))
1105 goto nla_put_failure;
1106 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1107 goto nla_put_failure;
1108 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1109 goto nla_put_failure;
1110 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1111 goto nla_put_failure;
1112 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1113 goto nla_put_failure;
1114
1115 if (ops->sb_occ_port_pool_get) {
1116 u32 cur;
1117 u32 max;
1118
1119 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1120 pool_index, &cur, &max);
1121 if (err && err != -EOPNOTSUPP)
1122 return err;
1123 if (!err) {
1124 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1125 goto nla_put_failure;
1126 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1127 goto nla_put_failure;
1128 }
1129 }
1130
1131 genlmsg_end(msg, hdr);
1132 return 0;
1133
1134nla_put_failure:
1135 genlmsg_cancel(msg, hdr);
1136 return -EMSGSIZE;
1137}
1138
1139static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1140 struct genl_info *info)
1141{
1142 struct devlink_port *devlink_port = info->user_ptr[0];
1143 struct devlink *devlink = devlink_port->devlink;
1144 struct devlink_sb *devlink_sb = info->user_ptr[1];
1145 struct sk_buff *msg;
1146 u16 pool_index;
1147 int err;
1148
1149 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1150 &pool_index);
1151 if (err)
1152 return err;
1153
1154 if (!devlink->ops->sb_port_pool_get)
1155 return -EOPNOTSUPP;
1156
1157 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1158 if (!msg)
1159 return -ENOMEM;
1160
1161 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1162 devlink_sb, pool_index,
1163 DEVLINK_CMD_SB_PORT_POOL_NEW,
1164 info->snd_portid, info->snd_seq, 0);
1165 if (err) {
1166 nlmsg_free(msg);
1167 return err;
1168 }
1169
1170 return genlmsg_reply(msg, info);
1171}
1172
1173static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1174 struct devlink *devlink,
1175 struct devlink_sb *devlink_sb,
1176 u32 portid, u32 seq)
1177{
1178 struct devlink_port *devlink_port;
1179 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1180 u16 pool_index;
1181 int err;
1182
1183 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1184 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1185 if (*p_idx < start) {
1186 (*p_idx)++;
1187 continue;
1188 }
1189 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1190 devlink_port,
1191 devlink_sb,
1192 pool_index,
1193 DEVLINK_CMD_SB_PORT_POOL_NEW,
1194 portid, seq,
1195 NLM_F_MULTI);
1196 if (err)
1197 return err;
1198 (*p_idx)++;
1199 }
1200 }
1201 return 0;
1202}
1203
1204static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1205 struct netlink_callback *cb)
1206{
1207 struct devlink *devlink;
1208 struct devlink_sb *devlink_sb;
1209 int start = cb->args[0];
1210 int idx = 0;
1211 int err;
1212
1213 mutex_lock(&devlink_mutex);
1214 list_for_each_entry(devlink, &devlink_list, list) {
1215 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1216 !devlink->ops->sb_port_pool_get)
1217 continue;
1218 mutex_lock(&devlink->lock);
1219 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1220 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1221 devlink, devlink_sb,
1222 NETLINK_CB(cb->skb).portid,
1223 cb->nlh->nlmsg_seq);
1224 if (err && err != -EOPNOTSUPP) {
1225 mutex_unlock(&devlink->lock);
1226 goto out;
1227 }
1228 }
1229 mutex_unlock(&devlink->lock);
1230 }
1231out:
1232 mutex_unlock(&devlink_mutex);
1233
1234 cb->args[0] = idx;
1235 return msg->len;
1236}
1237
1238static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1239 unsigned int sb_index, u16 pool_index,
1240 u32 threshold)
1241
1242{
1243 const struct devlink_ops *ops = devlink_port->devlink->ops;
1244
1245 if (ops->sb_port_pool_set)
1246 return ops->sb_port_pool_set(devlink_port, sb_index,
1247 pool_index, threshold);
1248 return -EOPNOTSUPP;
1249}
1250
1251static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1252 struct genl_info *info)
1253{
1254 struct devlink_port *devlink_port = info->user_ptr[0];
1255 struct devlink_sb *devlink_sb = info->user_ptr[1];
1256 u16 pool_index;
1257 u32 threshold;
1258 int err;
1259
1260 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1261 &pool_index);
1262 if (err)
1263 return err;
1264
1265 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1266 return -EINVAL;
1267
1268 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1269 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1270 pool_index, threshold);
1271}
1272
1273static int
1274devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1275 struct devlink_port *devlink_port,
1276 struct devlink_sb *devlink_sb, u16 tc_index,
1277 enum devlink_sb_pool_type pool_type,
1278 enum devlink_command cmd,
1279 u32 portid, u32 seq, int flags)
1280{
1281 const struct devlink_ops *ops = devlink->ops;
1282 u16 pool_index;
1283 u32 threshold;
1284 void *hdr;
1285 int err;
1286
1287 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1288 tc_index, pool_type,
1289 &pool_index, &threshold);
1290 if (err)
1291 return err;
1292
1293 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1294 if (!hdr)
1295 return -EMSGSIZE;
1296
1297 if (devlink_nl_put_handle(msg, devlink))
1298 goto nla_put_failure;
1299 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1300 goto nla_put_failure;
1301 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1302 goto nla_put_failure;
1303 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1304 goto nla_put_failure;
1305 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1306 goto nla_put_failure;
1307 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1308 goto nla_put_failure;
1309 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1310 goto nla_put_failure;
1311
1312 if (ops->sb_occ_tc_port_bind_get) {
1313 u32 cur;
1314 u32 max;
1315
1316 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1317 devlink_sb->index,
1318 tc_index, pool_type,
1319 &cur, &max);
1320 if (err && err != -EOPNOTSUPP)
1321 return err;
1322 if (!err) {
1323 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1324 goto nla_put_failure;
1325 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1326 goto nla_put_failure;
1327 }
1328 }
1329
1330 genlmsg_end(msg, hdr);
1331 return 0;
1332
1333nla_put_failure:
1334 genlmsg_cancel(msg, hdr);
1335 return -EMSGSIZE;
1336}
1337
1338static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1339 struct genl_info *info)
1340{
1341 struct devlink_port *devlink_port = info->user_ptr[0];
1342 struct devlink *devlink = devlink_port->devlink;
1343 struct devlink_sb *devlink_sb = info->user_ptr[1];
1344 struct sk_buff *msg;
1345 enum devlink_sb_pool_type pool_type;
1346 u16 tc_index;
1347 int err;
1348
1349 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1350 if (err)
1351 return err;
1352
1353 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1354 pool_type, &tc_index);
1355 if (err)
1356 return err;
1357
1358 if (!devlink->ops->sb_tc_pool_bind_get)
1359 return -EOPNOTSUPP;
1360
1361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1362 if (!msg)
1363 return -ENOMEM;
1364
1365 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1366 devlink_sb, tc_index, pool_type,
1367 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1368 info->snd_portid,
1369 info->snd_seq, 0);
1370 if (err) {
1371 nlmsg_free(msg);
1372 return err;
1373 }
1374
1375 return genlmsg_reply(msg, info);
1376}
1377
1378static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1379 int start, int *p_idx,
1380 struct devlink *devlink,
1381 struct devlink_sb *devlink_sb,
1382 u32 portid, u32 seq)
1383{
1384 struct devlink_port *devlink_port;
1385 u16 tc_index;
1386 int err;
1387
1388 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1389 for (tc_index = 0;
1390 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1391 if (*p_idx < start) {
1392 (*p_idx)++;
1393 continue;
1394 }
1395 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1396 devlink_port,
1397 devlink_sb,
1398 tc_index,
1399 DEVLINK_SB_POOL_TYPE_INGRESS,
1400 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1401 portid, seq,
1402 NLM_F_MULTI);
1403 if (err)
1404 return err;
1405 (*p_idx)++;
1406 }
1407 for (tc_index = 0;
1408 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1409 if (*p_idx < start) {
1410 (*p_idx)++;
1411 continue;
1412 }
1413 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1414 devlink_port,
1415 devlink_sb,
1416 tc_index,
1417 DEVLINK_SB_POOL_TYPE_EGRESS,
1418 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1419 portid, seq,
1420 NLM_F_MULTI);
1421 if (err)
1422 return err;
1423 (*p_idx)++;
1424 }
1425 }
1426 return 0;
1427}
1428
1429static int
1430devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1431 struct netlink_callback *cb)
1432{
1433 struct devlink *devlink;
1434 struct devlink_sb *devlink_sb;
1435 int start = cb->args[0];
1436 int idx = 0;
1437 int err;
1438
1439 mutex_lock(&devlink_mutex);
1440 list_for_each_entry(devlink, &devlink_list, list) {
1441 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1442 !devlink->ops->sb_tc_pool_bind_get)
1443 continue;
1444
1445 mutex_lock(&devlink->lock);
1446 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1447 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1448 devlink,
1449 devlink_sb,
1450 NETLINK_CB(cb->skb).portid,
1451 cb->nlh->nlmsg_seq);
1452 if (err && err != -EOPNOTSUPP) {
1453 mutex_unlock(&devlink->lock);
1454 goto out;
1455 }
1456 }
1457 mutex_unlock(&devlink->lock);
1458 }
1459out:
1460 mutex_unlock(&devlink_mutex);
1461
1462 cb->args[0] = idx;
1463 return msg->len;
1464}
1465
1466static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1467 unsigned int sb_index, u16 tc_index,
1468 enum devlink_sb_pool_type pool_type,
1469 u16 pool_index, u32 threshold)
1470
1471{
1472 const struct devlink_ops *ops = devlink_port->devlink->ops;
1473
1474 if (ops->sb_tc_pool_bind_set)
1475 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1476 tc_index, pool_type,
1477 pool_index, threshold);
1478 return -EOPNOTSUPP;
1479}
1480
1481static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1482 struct genl_info *info)
1483{
1484 struct devlink_port *devlink_port = info->user_ptr[0];
1485 struct devlink_sb *devlink_sb = info->user_ptr[1];
1486 enum devlink_sb_pool_type pool_type;
1487 u16 tc_index;
1488 u16 pool_index;
1489 u32 threshold;
1490 int err;
1491
1492 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1493 if (err)
1494 return err;
1495
1496 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1497 pool_type, &tc_index);
1498 if (err)
1499 return err;
1500
1501 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1502 &pool_index);
1503 if (err)
1504 return err;
1505
1506 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1507 return -EINVAL;
1508
1509 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1510 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1511 tc_index, pool_type,
1512 pool_index, threshold);
1513}
1514
1515static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1516 struct genl_info *info)
1517{
1518 struct devlink *devlink = info->user_ptr[0];
1519 struct devlink_sb *devlink_sb = info->user_ptr[1];
1520 const struct devlink_ops *ops = devlink->ops;
1521
1522 if (ops->sb_occ_snapshot)
1523 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1524 return -EOPNOTSUPP;
1525}
1526
1527static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1528 struct genl_info *info)
1529{
1530 struct devlink *devlink = info->user_ptr[0];
1531 struct devlink_sb *devlink_sb = info->user_ptr[1];
1532 const struct devlink_ops *ops = devlink->ops;
1533
1534 if (ops->sb_occ_max_clear)
1535 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1536 return -EOPNOTSUPP;
1537}
1538
1539static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1540 enum devlink_command cmd, u32 portid,
1541 u32 seq, int flags)
1542{
1543 const struct devlink_ops *ops = devlink->ops;
1544 u8 inline_mode, encap_mode;
1545 void *hdr;
1546 int err = 0;
1547 u16 mode;
1548
1549 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1550 if (!hdr)
1551 return -EMSGSIZE;
1552
1553 err = devlink_nl_put_handle(msg, devlink);
1554 if (err)
1555 goto nla_put_failure;
1556
1557 if (ops->eswitch_mode_get) {
1558 err = ops->eswitch_mode_get(devlink, &mode);
1559 if (err)
1560 goto nla_put_failure;
1561 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1562 if (err)
1563 goto nla_put_failure;
1564 }
1565
1566 if (ops->eswitch_inline_mode_get) {
1567 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1568 if (err)
1569 goto nla_put_failure;
1570 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1571 inline_mode);
1572 if (err)
1573 goto nla_put_failure;
1574 }
1575
1576 if (ops->eswitch_encap_mode_get) {
1577 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1578 if (err)
1579 goto nla_put_failure;
1580 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1581 if (err)
1582 goto nla_put_failure;
1583 }
1584
1585 genlmsg_end(msg, hdr);
1586 return 0;
1587
1588nla_put_failure:
1589 genlmsg_cancel(msg, hdr);
1590 return err;
1591}
1592
1593static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1594 struct genl_info *info)
1595{
1596 struct devlink *devlink = info->user_ptr[0];
1597 struct sk_buff *msg;
1598 int err;
1599
1600 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1601 if (!msg)
1602 return -ENOMEM;
1603
1604 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1605 info->snd_portid, info->snd_seq, 0);
1606
1607 if (err) {
1608 nlmsg_free(msg);
1609 return err;
1610 }
1611
1612 return genlmsg_reply(msg, info);
1613}
1614
1615static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1616 struct genl_info *info)
1617{
1618 struct devlink *devlink = info->user_ptr[0];
1619 const struct devlink_ops *ops = devlink->ops;
1620 u8 inline_mode, encap_mode;
1621 int err = 0;
1622 u16 mode;
1623
1624 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1625 if (!ops->eswitch_mode_set)
1626 return -EOPNOTSUPP;
1627 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1628 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1629 if (err)
1630 return err;
1631 }
1632
1633 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1634 if (!ops->eswitch_inline_mode_set)
1635 return -EOPNOTSUPP;
1636 inline_mode = nla_get_u8(
1637 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1638 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1639 info->extack);
1640 if (err)
1641 return err;
1642 }
1643
1644 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1645 if (!ops->eswitch_encap_mode_set)
1646 return -EOPNOTSUPP;
1647 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1648 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1649 info->extack);
1650 if (err)
1651 return err;
1652 }
1653
1654 return 0;
1655}
1656
1657int devlink_dpipe_match_put(struct sk_buff *skb,
1658 struct devlink_dpipe_match *match)
1659{
1660 struct devlink_dpipe_header *header = match->header;
1661 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1662 struct nlattr *match_attr;
1663
1664 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1665 if (!match_attr)
1666 return -EMSGSIZE;
1667
1668 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1669 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1670 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1671 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1672 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1673 goto nla_put_failure;
1674
1675 nla_nest_end(skb, match_attr);
1676 return 0;
1677
1678nla_put_failure:
1679 nla_nest_cancel(skb, match_attr);
1680 return -EMSGSIZE;
1681}
1682EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1683
1684static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1685 struct sk_buff *skb)
1686{
1687 struct nlattr *matches_attr;
1688
1689 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1690 if (!matches_attr)
1691 return -EMSGSIZE;
1692
1693 if (table->table_ops->matches_dump(table->priv, skb))
1694 goto nla_put_failure;
1695
1696 nla_nest_end(skb, matches_attr);
1697 return 0;
1698
1699nla_put_failure:
1700 nla_nest_cancel(skb, matches_attr);
1701 return -EMSGSIZE;
1702}
1703
1704int devlink_dpipe_action_put(struct sk_buff *skb,
1705 struct devlink_dpipe_action *action)
1706{
1707 struct devlink_dpipe_header *header = action->header;
1708 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1709 struct nlattr *action_attr;
1710
1711 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1712 if (!action_attr)
1713 return -EMSGSIZE;
1714
1715 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1716 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1717 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1718 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1719 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1720 goto nla_put_failure;
1721
1722 nla_nest_end(skb, action_attr);
1723 return 0;
1724
1725nla_put_failure:
1726 nla_nest_cancel(skb, action_attr);
1727 return -EMSGSIZE;
1728}
1729EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1730
1731static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1732 struct sk_buff *skb)
1733{
1734 struct nlattr *actions_attr;
1735
1736 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1737 if (!actions_attr)
1738 return -EMSGSIZE;
1739
1740 if (table->table_ops->actions_dump(table->priv, skb))
1741 goto nla_put_failure;
1742
1743 nla_nest_end(skb, actions_attr);
1744 return 0;
1745
1746nla_put_failure:
1747 nla_nest_cancel(skb, actions_attr);
1748 return -EMSGSIZE;
1749}
1750
1751static int devlink_dpipe_table_put(struct sk_buff *skb,
1752 struct devlink_dpipe_table *table)
1753{
1754 struct nlattr *table_attr;
1755 u64 table_size;
1756
1757 table_size = table->table_ops->size_get(table->priv);
1758 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1759 if (!table_attr)
1760 return -EMSGSIZE;
1761
1762 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1763 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1764 DEVLINK_ATTR_PAD))
1765 goto nla_put_failure;
1766 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1767 table->counters_enabled))
1768 goto nla_put_failure;
1769
1770 if (table->resource_valid) {
1771 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1772 table->resource_id, DEVLINK_ATTR_PAD) ||
1773 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1774 table->resource_units, DEVLINK_ATTR_PAD))
1775 goto nla_put_failure;
1776 }
1777 if (devlink_dpipe_matches_put(table, skb))
1778 goto nla_put_failure;
1779
1780 if (devlink_dpipe_actions_put(table, skb))
1781 goto nla_put_failure;
1782
1783 nla_nest_end(skb, table_attr);
1784 return 0;
1785
1786nla_put_failure:
1787 nla_nest_cancel(skb, table_attr);
1788 return -EMSGSIZE;
1789}
1790
1791static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1792 struct genl_info *info)
1793{
1794 int err;
1795
1796 if (*pskb) {
1797 err = genlmsg_reply(*pskb, info);
1798 if (err)
1799 return err;
1800 }
1801 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1802 if (!*pskb)
1803 return -ENOMEM;
1804 return 0;
1805}
1806
1807static int devlink_dpipe_tables_fill(struct genl_info *info,
1808 enum devlink_command cmd, int flags,
1809 struct list_head *dpipe_tables,
1810 const char *table_name)
1811{
1812 struct devlink *devlink = info->user_ptr[0];
1813 struct devlink_dpipe_table *table;
1814 struct nlattr *tables_attr;
1815 struct sk_buff *skb = NULL;
1816 struct nlmsghdr *nlh;
1817 bool incomplete;
1818 void *hdr;
1819 int i;
1820 int err;
1821
1822 table = list_first_entry(dpipe_tables,
1823 struct devlink_dpipe_table, list);
1824start_again:
1825 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1826 if (err)
1827 return err;
1828
1829 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1830 &devlink_nl_family, NLM_F_MULTI, cmd);
1831 if (!hdr) {
1832 nlmsg_free(skb);
1833 return -EMSGSIZE;
1834 }
1835
1836 if (devlink_nl_put_handle(skb, devlink))
1837 goto nla_put_failure;
1838 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1839 if (!tables_attr)
1840 goto nla_put_failure;
1841
1842 i = 0;
1843 incomplete = false;
1844 list_for_each_entry_from(table, dpipe_tables, list) {
1845 if (!table_name) {
1846 err = devlink_dpipe_table_put(skb, table);
1847 if (err) {
1848 if (!i)
1849 goto err_table_put;
1850 incomplete = true;
1851 break;
1852 }
1853 } else {
1854 if (!strcmp(table->name, table_name)) {
1855 err = devlink_dpipe_table_put(skb, table);
1856 if (err)
1857 break;
1858 }
1859 }
1860 i++;
1861 }
1862
1863 nla_nest_end(skb, tables_attr);
1864 genlmsg_end(skb, hdr);
1865 if (incomplete)
1866 goto start_again;
1867
1868send_done:
1869 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1870 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1871 if (!nlh) {
1872 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1873 if (err)
1874 return err;
1875 goto send_done;
1876 }
1877
1878 return genlmsg_reply(skb, info);
1879
1880nla_put_failure:
1881 err = -EMSGSIZE;
1882err_table_put:
1883 nlmsg_free(skb);
1884 return err;
1885}
1886
1887static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1888 struct genl_info *info)
1889{
1890 struct devlink *devlink = info->user_ptr[0];
1891 const char *table_name = NULL;
1892
1893 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1894 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1895
1896 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1897 &devlink->dpipe_table_list,
1898 table_name);
1899}
1900
1901static int devlink_dpipe_value_put(struct sk_buff *skb,
1902 struct devlink_dpipe_value *value)
1903{
1904 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1905 value->value_size, value->value))
1906 return -EMSGSIZE;
1907 if (value->mask)
1908 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1909 value->value_size, value->mask))
1910 return -EMSGSIZE;
1911 if (value->mapping_valid)
1912 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1913 value->mapping_value))
1914 return -EMSGSIZE;
1915 return 0;
1916}
1917
1918static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1919 struct devlink_dpipe_value *value)
1920{
1921 if (!value->action)
1922 return -EINVAL;
1923 if (devlink_dpipe_action_put(skb, value->action))
1924 return -EMSGSIZE;
1925 if (devlink_dpipe_value_put(skb, value))
1926 return -EMSGSIZE;
1927 return 0;
1928}
1929
1930static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1931 struct devlink_dpipe_value *values,
1932 unsigned int values_count)
1933{
1934 struct nlattr *action_attr;
1935 int i;
1936 int err;
1937
1938 for (i = 0; i < values_count; i++) {
1939 action_attr = nla_nest_start(skb,
1940 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1941 if (!action_attr)
1942 return -EMSGSIZE;
1943 err = devlink_dpipe_action_value_put(skb, &values[i]);
1944 if (err)
1945 goto err_action_value_put;
1946 nla_nest_end(skb, action_attr);
1947 }
1948 return 0;
1949
1950err_action_value_put:
1951 nla_nest_cancel(skb, action_attr);
1952 return err;
1953}
1954
1955static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1956 struct devlink_dpipe_value *value)
1957{
1958 if (!value->match)
1959 return -EINVAL;
1960 if (devlink_dpipe_match_put(skb, value->match))
1961 return -EMSGSIZE;
1962 if (devlink_dpipe_value_put(skb, value))
1963 return -EMSGSIZE;
1964 return 0;
1965}
1966
1967static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1968 struct devlink_dpipe_value *values,
1969 unsigned int values_count)
1970{
1971 struct nlattr *match_attr;
1972 int i;
1973 int err;
1974
1975 for (i = 0; i < values_count; i++) {
1976 match_attr = nla_nest_start(skb,
1977 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1978 if (!match_attr)
1979 return -EMSGSIZE;
1980 err = devlink_dpipe_match_value_put(skb, &values[i]);
1981 if (err)
1982 goto err_match_value_put;
1983 nla_nest_end(skb, match_attr);
1984 }
1985 return 0;
1986
1987err_match_value_put:
1988 nla_nest_cancel(skb, match_attr);
1989 return err;
1990}
1991
1992static int devlink_dpipe_entry_put(struct sk_buff *skb,
1993 struct devlink_dpipe_entry *entry)
1994{
1995 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1996 int err;
1997
1998 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1999 if (!entry_attr)
2000 return -EMSGSIZE;
2001
2002 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2003 DEVLINK_ATTR_PAD))
2004 goto nla_put_failure;
2005 if (entry->counter_valid)
2006 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2007 entry->counter, DEVLINK_ATTR_PAD))
2008 goto nla_put_failure;
2009
2010 matches_attr = nla_nest_start(skb,
2011 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2012 if (!matches_attr)
2013 goto nla_put_failure;
2014
2015 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2016 entry->match_values_count);
2017 if (err) {
2018 nla_nest_cancel(skb, matches_attr);
2019 goto err_match_values_put;
2020 }
2021 nla_nest_end(skb, matches_attr);
2022
2023 actions_attr = nla_nest_start(skb,
2024 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2025 if (!actions_attr)
2026 goto nla_put_failure;
2027
2028 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2029 entry->action_values_count);
2030 if (err) {
2031 nla_nest_cancel(skb, actions_attr);
2032 goto err_action_values_put;
2033 }
2034 nla_nest_end(skb, actions_attr);
2035
2036 nla_nest_end(skb, entry_attr);
2037 return 0;
2038
2039nla_put_failure:
2040 err = -EMSGSIZE;
2041err_match_values_put:
2042err_action_values_put:
2043 nla_nest_cancel(skb, entry_attr);
2044 return err;
2045}
2046
2047static struct devlink_dpipe_table *
2048devlink_dpipe_table_find(struct list_head *dpipe_tables,
2049 const char *table_name)
2050{
2051 struct devlink_dpipe_table *table;
2052
2053 list_for_each_entry_rcu(table, dpipe_tables, list) {
2054 if (!strcmp(table->name, table_name))
2055 return table;
2056 }
2057 return NULL;
2058}
2059
2060int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2061{
2062 struct devlink *devlink;
2063 int err;
2064
2065 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2066 dump_ctx->info);
2067 if (err)
2068 return err;
2069
2070 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2071 dump_ctx->info->snd_portid,
2072 dump_ctx->info->snd_seq,
2073 &devlink_nl_family, NLM_F_MULTI,
2074 dump_ctx->cmd);
2075 if (!dump_ctx->hdr)
2076 goto nla_put_failure;
2077
2078 devlink = dump_ctx->info->user_ptr[0];
2079 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2080 goto nla_put_failure;
2081 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2082 DEVLINK_ATTR_DPIPE_ENTRIES);
2083 if (!dump_ctx->nest)
2084 goto nla_put_failure;
2085 return 0;
2086
2087nla_put_failure:
2088 nlmsg_free(dump_ctx->skb);
2089 return -EMSGSIZE;
2090}
2091EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2092
2093int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2094 struct devlink_dpipe_entry *entry)
2095{
2096 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2097}
2098EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2099
2100int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2101{
2102 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2103 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2104 return 0;
2105}
2106EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2107
2108void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2109
2110{
2111 unsigned int value_count, value_index;
2112 struct devlink_dpipe_value *value;
2113
2114 value = entry->action_values;
2115 value_count = entry->action_values_count;
2116 for (value_index = 0; value_index < value_count; value_index++) {
2117 kfree(value[value_index].value);
2118 kfree(value[value_index].mask);
2119 }
2120
2121 value = entry->match_values;
2122 value_count = entry->match_values_count;
2123 for (value_index = 0; value_index < value_count; value_index++) {
2124 kfree(value[value_index].value);
2125 kfree(value[value_index].mask);
2126 }
2127}
2128EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2129
2130static int devlink_dpipe_entries_fill(struct genl_info *info,
2131 enum devlink_command cmd, int flags,
2132 struct devlink_dpipe_table *table)
2133{
2134 struct devlink_dpipe_dump_ctx dump_ctx;
2135 struct nlmsghdr *nlh;
2136 int err;
2137
2138 dump_ctx.skb = NULL;
2139 dump_ctx.cmd = cmd;
2140 dump_ctx.info = info;
2141
2142 err = table->table_ops->entries_dump(table->priv,
2143 table->counters_enabled,
2144 &dump_ctx);
2145 if (err)
2146 return err;
2147
2148send_done:
2149 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2150 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2151 if (!nlh) {
2152 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2153 if (err)
2154 return err;
2155 goto send_done;
2156 }
2157 return genlmsg_reply(dump_ctx.skb, info);
2158}
2159
2160static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2161 struct genl_info *info)
2162{
2163 struct devlink *devlink = info->user_ptr[0];
2164 struct devlink_dpipe_table *table;
2165 const char *table_name;
2166
2167 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2168 return -EINVAL;
2169
2170 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2171 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2172 table_name);
2173 if (!table)
2174 return -EINVAL;
2175
2176 if (!table->table_ops->entries_dump)
2177 return -EINVAL;
2178
2179 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2180 0, table);
2181}
2182
2183static int devlink_dpipe_fields_put(struct sk_buff *skb,
2184 const struct devlink_dpipe_header *header)
2185{
2186 struct devlink_dpipe_field *field;
2187 struct nlattr *field_attr;
2188 int i;
2189
2190 for (i = 0; i < header->fields_count; i++) {
2191 field = &header->fields[i];
2192 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2193 if (!field_attr)
2194 return -EMSGSIZE;
2195 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2196 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2197 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2198 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2199 goto nla_put_failure;
2200 nla_nest_end(skb, field_attr);
2201 }
2202 return 0;
2203
2204nla_put_failure:
2205 nla_nest_cancel(skb, field_attr);
2206 return -EMSGSIZE;
2207}
2208
2209static int devlink_dpipe_header_put(struct sk_buff *skb,
2210 struct devlink_dpipe_header *header)
2211{
2212 struct nlattr *fields_attr, *header_attr;
2213 int err;
2214
2215 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2216 if (!header_attr)
2217 return -EMSGSIZE;
2218
2219 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2220 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2221 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2222 goto nla_put_failure;
2223
2224 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2225 if (!fields_attr)
2226 goto nla_put_failure;
2227
2228 err = devlink_dpipe_fields_put(skb, header);
2229 if (err) {
2230 nla_nest_cancel(skb, fields_attr);
2231 goto nla_put_failure;
2232 }
2233 nla_nest_end(skb, fields_attr);
2234 nla_nest_end(skb, header_attr);
2235 return 0;
2236
2237nla_put_failure:
2238 err = -EMSGSIZE;
2239 nla_nest_cancel(skb, header_attr);
2240 return err;
2241}
2242
2243static int devlink_dpipe_headers_fill(struct genl_info *info,
2244 enum devlink_command cmd, int flags,
2245 struct devlink_dpipe_headers *
2246 dpipe_headers)
2247{
2248 struct devlink *devlink = info->user_ptr[0];
2249 struct nlattr *headers_attr;
2250 struct sk_buff *skb = NULL;
2251 struct nlmsghdr *nlh;
2252 void *hdr;
2253 int i, j;
2254 int err;
2255
2256 i = 0;
2257start_again:
2258 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2259 if (err)
2260 return err;
2261
2262 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2263 &devlink_nl_family, NLM_F_MULTI, cmd);
2264 if (!hdr) {
2265 nlmsg_free(skb);
2266 return -EMSGSIZE;
2267 }
2268
2269 if (devlink_nl_put_handle(skb, devlink))
2270 goto nla_put_failure;
2271 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2272 if (!headers_attr)
2273 goto nla_put_failure;
2274
2275 j = 0;
2276 for (; i < dpipe_headers->headers_count; i++) {
2277 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2278 if (err) {
2279 if (!j)
2280 goto err_table_put;
2281 break;
2282 }
2283 j++;
2284 }
2285 nla_nest_end(skb, headers_attr);
2286 genlmsg_end(skb, hdr);
2287 if (i != dpipe_headers->headers_count)
2288 goto start_again;
2289
2290send_done:
2291 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2292 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2293 if (!nlh) {
2294 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2295 if (err)
2296 return err;
2297 goto send_done;
2298 }
2299 return genlmsg_reply(skb, info);
2300
2301nla_put_failure:
2302 err = -EMSGSIZE;
2303err_table_put:
2304 nlmsg_free(skb);
2305 return err;
2306}
2307
2308static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2309 struct genl_info *info)
2310{
2311 struct devlink *devlink = info->user_ptr[0];
2312
2313 if (!devlink->dpipe_headers)
2314 return -EOPNOTSUPP;
2315 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2316 0, devlink->dpipe_headers);
2317}
2318
2319static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2320 const char *table_name,
2321 bool enable)
2322{
2323 struct devlink_dpipe_table *table;
2324
2325 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2326 table_name);
2327 if (!table)
2328 return -EINVAL;
2329
2330 if (table->counter_control_extern)
2331 return -EOPNOTSUPP;
2332
2333 if (!(table->counters_enabled ^ enable))
2334 return 0;
2335
2336 table->counters_enabled = enable;
2337 if (table->table_ops->counters_set_update)
2338 table->table_ops->counters_set_update(table->priv, enable);
2339 return 0;
2340}
2341
2342static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2343 struct genl_info *info)
2344{
2345 struct devlink *devlink = info->user_ptr[0];
2346 const char *table_name;
2347 bool counters_enable;
2348
2349 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2350 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2351 return -EINVAL;
2352
2353 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2354 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2355
2356 return devlink_dpipe_table_counters_set(devlink, table_name,
2357 counters_enable);
2358}
2359
2360static struct devlink_resource *
2361devlink_resource_find(struct devlink *devlink,
2362 struct devlink_resource *resource, u64 resource_id)
2363{
2364 struct list_head *resource_list;
2365
2366 if (resource)
2367 resource_list = &resource->resource_list;
2368 else
2369 resource_list = &devlink->resource_list;
2370
2371 list_for_each_entry(resource, resource_list, list) {
2372 struct devlink_resource *child_resource;
2373
2374 if (resource->id == resource_id)
2375 return resource;
2376
2377 child_resource = devlink_resource_find(devlink, resource,
2378 resource_id);
2379 if (child_resource)
2380 return child_resource;
2381 }
2382 return NULL;
2383}
2384
2385static void
2386devlink_resource_validate_children(struct devlink_resource *resource)
2387{
2388 struct devlink_resource *child_resource;
2389 bool size_valid = true;
2390 u64 parts_size = 0;
2391
2392 if (list_empty(&resource->resource_list))
2393 goto out;
2394
2395 list_for_each_entry(child_resource, &resource->resource_list, list)
2396 parts_size += child_resource->size_new;
2397
2398 if (parts_size > resource->size_new)
2399 size_valid = false;
2400out:
2401 resource->size_valid = size_valid;
2402}
2403
2404static int
2405devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2406 struct netlink_ext_ack *extack)
2407{
2408 u64 reminder;
2409 int err = 0;
2410
2411 if (size > resource->size_params.size_max) {
2412 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2413 err = -EINVAL;
2414 }
2415
2416 if (size < resource->size_params.size_min) {
2417 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2418 err = -EINVAL;
2419 }
2420
2421 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2422 if (reminder) {
2423 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2424 err = -EINVAL;
2425 }
2426
2427 return err;
2428}
2429
2430static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2431 struct genl_info *info)
2432{
2433 struct devlink *devlink = info->user_ptr[0];
2434 struct devlink_resource *resource;
2435 u64 resource_id;
2436 u64 size;
2437 int err;
2438
2439 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2440 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2441 return -EINVAL;
2442 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2443
2444 resource = devlink_resource_find(devlink, NULL, resource_id);
2445 if (!resource)
2446 return -EINVAL;
2447
2448 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2449 err = devlink_resource_validate_size(resource, size, info->extack);
2450 if (err)
2451 return err;
2452
2453 resource->size_new = size;
2454 devlink_resource_validate_children(resource);
2455 if (resource->parent)
2456 devlink_resource_validate_children(resource->parent);
2457 return 0;
2458}
2459
2460static int
2461devlink_resource_size_params_put(struct devlink_resource *resource,
2462 struct sk_buff *skb)
2463{
2464 struct devlink_resource_size_params *size_params;
2465
2466 size_params = &resource->size_params;
2467 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2468 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2469 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2470 size_params->size_max, DEVLINK_ATTR_PAD) ||
2471 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2472 size_params->size_min, DEVLINK_ATTR_PAD) ||
2473 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2474 return -EMSGSIZE;
2475 return 0;
2476}
2477
2478static int devlink_resource_occ_put(struct devlink_resource *resource,
2479 struct sk_buff *skb)
2480{
2481 if (!resource->occ_get)
2482 return 0;
2483 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2484 resource->occ_get(resource->occ_get_priv),
2485 DEVLINK_ATTR_PAD);
2486}
2487
2488static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2489 struct devlink_resource *resource)
2490{
2491 struct devlink_resource *child_resource;
2492 struct nlattr *child_resource_attr;
2493 struct nlattr *resource_attr;
2494
2495 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2496 if (!resource_attr)
2497 return -EMSGSIZE;
2498
2499 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2500 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2501 DEVLINK_ATTR_PAD) ||
2502 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2503 DEVLINK_ATTR_PAD))
2504 goto nla_put_failure;
2505 if (resource->size != resource->size_new)
2506 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2507 resource->size_new, DEVLINK_ATTR_PAD);
2508 if (devlink_resource_occ_put(resource, skb))
2509 goto nla_put_failure;
2510 if (devlink_resource_size_params_put(resource, skb))
2511 goto nla_put_failure;
2512 if (list_empty(&resource->resource_list))
2513 goto out;
2514
2515 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2516 resource->size_valid))
2517 goto nla_put_failure;
2518
2519 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2520 if (!child_resource_attr)
2521 goto nla_put_failure;
2522
2523 list_for_each_entry(child_resource, &resource->resource_list, list) {
2524 if (devlink_resource_put(devlink, skb, child_resource))
2525 goto resource_put_failure;
2526 }
2527
2528 nla_nest_end(skb, child_resource_attr);
2529out:
2530 nla_nest_end(skb, resource_attr);
2531 return 0;
2532
2533resource_put_failure:
2534 nla_nest_cancel(skb, child_resource_attr);
2535nla_put_failure:
2536 nla_nest_cancel(skb, resource_attr);
2537 return -EMSGSIZE;
2538}
2539
2540static int devlink_resource_fill(struct genl_info *info,
2541 enum devlink_command cmd, int flags)
2542{
2543 struct devlink *devlink = info->user_ptr[0];
2544 struct devlink_resource *resource;
2545 struct nlattr *resources_attr;
2546 struct sk_buff *skb = NULL;
2547 struct nlmsghdr *nlh;
2548 bool incomplete;
2549 void *hdr;
2550 int i;
2551 int err;
2552
2553 resource = list_first_entry(&devlink->resource_list,
2554 struct devlink_resource, list);
2555start_again:
2556 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2557 if (err)
2558 return err;
2559
2560 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2561 &devlink_nl_family, NLM_F_MULTI, cmd);
2562 if (!hdr) {
2563 nlmsg_free(skb);
2564 return -EMSGSIZE;
2565 }
2566
2567 if (devlink_nl_put_handle(skb, devlink))
2568 goto nla_put_failure;
2569
2570 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2571 if (!resources_attr)
2572 goto nla_put_failure;
2573
2574 incomplete = false;
2575 i = 0;
2576 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2577 err = devlink_resource_put(devlink, skb, resource);
2578 if (err) {
2579 if (!i)
2580 goto err_resource_put;
2581 incomplete = true;
2582 break;
2583 }
2584 i++;
2585 }
2586 nla_nest_end(skb, resources_attr);
2587 genlmsg_end(skb, hdr);
2588 if (incomplete)
2589 goto start_again;
2590send_done:
2591 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2592 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2593 if (!nlh) {
2594 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2595 if (err)
2596 return err;
2597 goto send_done;
2598 }
2599 return genlmsg_reply(skb, info);
2600
2601nla_put_failure:
2602 err = -EMSGSIZE;
2603err_resource_put:
2604 nlmsg_free(skb);
2605 return err;
2606}
2607
2608static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2609 struct genl_info *info)
2610{
2611 struct devlink *devlink = info->user_ptr[0];
2612
2613 if (list_empty(&devlink->resource_list))
2614 return -EOPNOTSUPP;
2615
2616 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2617}
2618
2619static int
2620devlink_resources_validate(struct devlink *devlink,
2621 struct devlink_resource *resource,
2622 struct genl_info *info)
2623{
2624 struct list_head *resource_list;
2625 int err = 0;
2626
2627 if (resource)
2628 resource_list = &resource->resource_list;
2629 else
2630 resource_list = &devlink->resource_list;
2631
2632 list_for_each_entry(resource, resource_list, list) {
2633 if (!resource->size_valid)
2634 return -EINVAL;
2635 err = devlink_resources_validate(devlink, resource, info);
2636 if (err)
2637 return err;
2638 }
2639 return err;
2640}
2641
2642static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2643{
2644 struct devlink *devlink = info->user_ptr[0];
2645 int err;
2646
2647 if (!devlink->ops->reload)
2648 return -EOPNOTSUPP;
2649
2650 err = devlink_resources_validate(devlink, NULL, info);
2651 if (err) {
2652 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2653 return err;
2654 }
2655 return devlink->ops->reload(devlink, info->extack);
2656}
2657
2658static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2659 struct genl_info *info)
2660{
2661 struct devlink *devlink = info->user_ptr[0];
2662 const char *file_name, *component;
2663 struct nlattr *nla_component;
2664
2665 if (!devlink->ops->flash_update)
2666 return -EOPNOTSUPP;
2667
2668 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2669 return -EINVAL;
2670 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2671
2672 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2673 component = nla_component ? nla_data(nla_component) : NULL;
2674
2675 return devlink->ops->flash_update(devlink, file_name, component,
2676 info->extack);
2677}
2678
2679static const struct devlink_param devlink_param_generic[] = {
2680 {
2681 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2682 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2683 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2684 },
2685 {
2686 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2687 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2688 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2689 },
2690 {
2691 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2692 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2693 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2694 },
2695 {
2696 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2697 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2698 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2699 },
2700 {
2701 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2702 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2703 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2704 },
2705 {
2706 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2707 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2708 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2709 },
2710 {
2711 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2712 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2713 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2714 },
2715 {
2716 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2717 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2718 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2719 },
2720};
2721
2722static int devlink_param_generic_verify(const struct devlink_param *param)
2723{
2724 /* verify it match generic parameter by id and name */
2725 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2726 return -EINVAL;
2727 if (strcmp(param->name, devlink_param_generic[param->id].name))
2728 return -ENOENT;
2729
2730 WARN_ON(param->type != devlink_param_generic[param->id].type);
2731
2732 return 0;
2733}
2734
2735static int devlink_param_driver_verify(const struct devlink_param *param)
2736{
2737 int i;
2738
2739 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2740 return -EINVAL;
2741 /* verify no such name in generic params */
2742 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2743 if (!strcmp(param->name, devlink_param_generic[i].name))
2744 return -EEXIST;
2745
2746 return 0;
2747}
2748
2749static struct devlink_param_item *
2750devlink_param_find_by_name(struct list_head *param_list,
2751 const char *param_name)
2752{
2753 struct devlink_param_item *param_item;
2754
2755 list_for_each_entry(param_item, param_list, list)
2756 if (!strcmp(param_item->param->name, param_name))
2757 return param_item;
2758 return NULL;
2759}
2760
2761static struct devlink_param_item *
2762devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2763{
2764 struct devlink_param_item *param_item;
2765
2766 list_for_each_entry(param_item, param_list, list)
2767 if (param_item->param->id == param_id)
2768 return param_item;
2769 return NULL;
2770}
2771
2772static bool
2773devlink_param_cmode_is_supported(const struct devlink_param *param,
2774 enum devlink_param_cmode cmode)
2775{
2776 return test_bit(cmode, &param->supported_cmodes);
2777}
2778
2779static int devlink_param_get(struct devlink *devlink,
2780 const struct devlink_param *param,
2781 struct devlink_param_gset_ctx *ctx)
2782{
2783 if (!param->get)
2784 return -EOPNOTSUPP;
2785 return param->get(devlink, param->id, ctx);
2786}
2787
2788static int devlink_param_set(struct devlink *devlink,
2789 const struct devlink_param *param,
2790 struct devlink_param_gset_ctx *ctx)
2791{
2792 if (!param->set)
2793 return -EOPNOTSUPP;
2794 return param->set(devlink, param->id, ctx);
2795}
2796
2797static int
2798devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2799{
2800 switch (param_type) {
2801 case DEVLINK_PARAM_TYPE_U8:
2802 return NLA_U8;
2803 case DEVLINK_PARAM_TYPE_U16:
2804 return NLA_U16;
2805 case DEVLINK_PARAM_TYPE_U32:
2806 return NLA_U32;
2807 case DEVLINK_PARAM_TYPE_STRING:
2808 return NLA_STRING;
2809 case DEVLINK_PARAM_TYPE_BOOL:
2810 return NLA_FLAG;
2811 default:
2812 return -EINVAL;
2813 }
2814}
2815
2816static int
2817devlink_nl_param_value_fill_one(struct sk_buff *msg,
2818 enum devlink_param_type type,
2819 enum devlink_param_cmode cmode,
2820 union devlink_param_value val)
2821{
2822 struct nlattr *param_value_attr;
2823
2824 param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2825 if (!param_value_attr)
2826 goto nla_put_failure;
2827
2828 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2829 goto value_nest_cancel;
2830
2831 switch (type) {
2832 case DEVLINK_PARAM_TYPE_U8:
2833 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2834 goto value_nest_cancel;
2835 break;
2836 case DEVLINK_PARAM_TYPE_U16:
2837 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2838 goto value_nest_cancel;
2839 break;
2840 case DEVLINK_PARAM_TYPE_U32:
2841 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2842 goto value_nest_cancel;
2843 break;
2844 case DEVLINK_PARAM_TYPE_STRING:
2845 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2846 val.vstr))
2847 goto value_nest_cancel;
2848 break;
2849 case DEVLINK_PARAM_TYPE_BOOL:
2850 if (val.vbool &&
2851 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2852 goto value_nest_cancel;
2853 break;
2854 }
2855
2856 nla_nest_end(msg, param_value_attr);
2857 return 0;
2858
2859value_nest_cancel:
2860 nla_nest_cancel(msg, param_value_attr);
2861nla_put_failure:
2862 return -EMSGSIZE;
2863}
2864
2865static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2866 unsigned int port_index,
2867 struct devlink_param_item *param_item,
2868 enum devlink_command cmd,
2869 u32 portid, u32 seq, int flags)
2870{
2871 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2872 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
2873 const struct devlink_param *param = param_item->param;
2874 struct devlink_param_gset_ctx ctx;
2875 struct nlattr *param_values_list;
2876 struct nlattr *param_attr;
2877 int nla_type;
2878 void *hdr;
2879 int err;
2880 int i;
2881
2882 /* Get value from driver part to driverinit configuration mode */
2883 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2884 if (!devlink_param_cmode_is_supported(param, i))
2885 continue;
2886 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2887 if (!param_item->driverinit_value_valid)
2888 return -EOPNOTSUPP;
2889 param_value[i] = param_item->driverinit_value;
2890 } else {
2891 if (!param_item->published)
2892 continue;
2893 ctx.cmode = i;
2894 err = devlink_param_get(devlink, param, &ctx);
2895 if (err)
2896 return err;
2897 param_value[i] = ctx.val;
2898 }
2899 param_value_set[i] = true;
2900 }
2901
2902 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2903 if (!hdr)
2904 return -EMSGSIZE;
2905
2906 if (devlink_nl_put_handle(msg, devlink))
2907 goto genlmsg_cancel;
2908
2909 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
2910 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
2911 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
2912 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
2913 goto genlmsg_cancel;
2914
2915 param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2916 if (!param_attr)
2917 goto genlmsg_cancel;
2918 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2919 goto param_nest_cancel;
2920 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2921 goto param_nest_cancel;
2922
2923 nla_type = devlink_param_type_to_nla_type(param->type);
2924 if (nla_type < 0)
2925 goto param_nest_cancel;
2926 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2927 goto param_nest_cancel;
2928
2929 param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2930 if (!param_values_list)
2931 goto param_nest_cancel;
2932
2933 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2934 if (!param_value_set[i])
2935 continue;
2936 err = devlink_nl_param_value_fill_one(msg, param->type,
2937 i, param_value[i]);
2938 if (err)
2939 goto values_list_nest_cancel;
2940 }
2941
2942 nla_nest_end(msg, param_values_list);
2943 nla_nest_end(msg, param_attr);
2944 genlmsg_end(msg, hdr);
2945 return 0;
2946
2947values_list_nest_cancel:
2948 nla_nest_end(msg, param_values_list);
2949param_nest_cancel:
2950 nla_nest_cancel(msg, param_attr);
2951genlmsg_cancel:
2952 genlmsg_cancel(msg, hdr);
2953 return -EMSGSIZE;
2954}
2955
2956static void devlink_param_notify(struct devlink *devlink,
2957 unsigned int port_index,
2958 struct devlink_param_item *param_item,
2959 enum devlink_command cmd)
2960{
2961 struct sk_buff *msg;
2962 int err;
2963
2964 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
2965 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
2966 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
2967
2968 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2969 if (!msg)
2970 return;
2971 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
2972 0, 0, 0);
2973 if (err) {
2974 nlmsg_free(msg);
2975 return;
2976 }
2977
2978 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2979 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2980}
2981
2982static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2983 struct netlink_callback *cb)
2984{
2985 struct devlink_param_item *param_item;
2986 struct devlink *devlink;
2987 int start = cb->args[0];
2988 int idx = 0;
2989 int err;
2990
2991 mutex_lock(&devlink_mutex);
2992 list_for_each_entry(devlink, &devlink_list, list) {
2993 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2994 continue;
2995 mutex_lock(&devlink->lock);
2996 list_for_each_entry(param_item, &devlink->param_list, list) {
2997 if (idx < start) {
2998 idx++;
2999 continue;
3000 }
3001 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3002 DEVLINK_CMD_PARAM_GET,
3003 NETLINK_CB(cb->skb).portid,
3004 cb->nlh->nlmsg_seq,
3005 NLM_F_MULTI);
3006 if (err) {
3007 mutex_unlock(&devlink->lock);
3008 goto out;
3009 }
3010 idx++;
3011 }
3012 mutex_unlock(&devlink->lock);
3013 }
3014out:
3015 mutex_unlock(&devlink_mutex);
3016
3017 cb->args[0] = idx;
3018 return msg->len;
3019}
3020
3021static int
3022devlink_param_type_get_from_info(struct genl_info *info,
3023 enum devlink_param_type *param_type)
3024{
3025 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3026 return -EINVAL;
3027
3028 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3029 case NLA_U8:
3030 *param_type = DEVLINK_PARAM_TYPE_U8;
3031 break;
3032 case NLA_U16:
3033 *param_type = DEVLINK_PARAM_TYPE_U16;
3034 break;
3035 case NLA_U32:
3036 *param_type = DEVLINK_PARAM_TYPE_U32;
3037 break;
3038 case NLA_STRING:
3039 *param_type = DEVLINK_PARAM_TYPE_STRING;
3040 break;
3041 case NLA_FLAG:
3042 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3043 break;
3044 default:
3045 return -EINVAL;
3046 }
3047
3048 return 0;
3049}
3050
3051static int
3052devlink_param_value_get_from_info(const struct devlink_param *param,
3053 struct genl_info *info,
3054 union devlink_param_value *value)
3055{
3056 int len;
3057
3058 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3059 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3060 return -EINVAL;
3061
3062 switch (param->type) {
3063 case DEVLINK_PARAM_TYPE_U8:
3064 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3065 break;
3066 case DEVLINK_PARAM_TYPE_U16:
3067 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3068 break;
3069 case DEVLINK_PARAM_TYPE_U32:
3070 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3071 break;
3072 case DEVLINK_PARAM_TYPE_STRING:
3073 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3074 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3075 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3076 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3077 return -EINVAL;
3078 strcpy(value->vstr,
3079 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3080 break;
3081 case DEVLINK_PARAM_TYPE_BOOL:
3082 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3083 true : false;
3084 break;
3085 }
3086 return 0;
3087}
3088
3089static struct devlink_param_item *
3090devlink_param_get_from_info(struct list_head *param_list,
3091 struct genl_info *info)
3092{
3093 char *param_name;
3094
3095 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3096 return NULL;
3097
3098 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3099 return devlink_param_find_by_name(param_list, param_name);
3100}
3101
3102static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3103 struct genl_info *info)
3104{
3105 struct devlink *devlink = info->user_ptr[0];
3106 struct devlink_param_item *param_item;
3107 struct sk_buff *msg;
3108 int err;
3109
3110 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3111 if (!param_item)
3112 return -EINVAL;
3113
3114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3115 if (!msg)
3116 return -ENOMEM;
3117
3118 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3119 DEVLINK_CMD_PARAM_GET,
3120 info->snd_portid, info->snd_seq, 0);
3121 if (err) {
3122 nlmsg_free(msg);
3123 return err;
3124 }
3125
3126 return genlmsg_reply(msg, info);
3127}
3128
3129static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3130 unsigned int port_index,
3131 struct list_head *param_list,
3132 struct genl_info *info,
3133 enum devlink_command cmd)
3134{
3135 enum devlink_param_type param_type;
3136 struct devlink_param_gset_ctx