GNU Linux-libre 4.14.332-gnu1
[releases.git] / net / core / devlink.c
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
32 static 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
40 struct 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 };
47 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
48
49 static 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
57 struct 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 };
64 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
65
66 static 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
74 struct 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 };
81 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
82
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
84
85 static LIST_HEAD(devlink_list);
86
87 /* devlink_mutex
88  *
89  * An overall lock guarding every operation coming from userspace.
90  * It also guards devlink devices list and it is taken when
91  * driver registers/unregisters it.
92  */
93 static DEFINE_MUTEX(devlink_mutex);
94
95 /* devlink_port_mutex
96  *
97  * Shared lock to guard lists of ports in all devlink devices.
98  */
99 static DEFINE_MUTEX(devlink_port_mutex);
100
101 static struct net *devlink_net(const struct devlink *devlink)
102 {
103         return read_pnet(&devlink->_net);
104 }
105
106 static void devlink_net_set(struct devlink *devlink, struct net *net)
107 {
108         write_pnet(&devlink->_net, net);
109 }
110
111 static struct devlink *devlink_get_from_attrs(struct net *net,
112                                               struct nlattr **attrs)
113 {
114         struct devlink *devlink;
115         char *busname;
116         char *devname;
117
118         if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
119                 return ERR_PTR(-EINVAL);
120
121         busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
122         devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
123
124         list_for_each_entry(devlink, &devlink_list, list) {
125                 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
126                     strcmp(dev_name(devlink->dev), devname) == 0 &&
127                     net_eq(devlink_net(devlink), net))
128                         return devlink;
129         }
130
131         return ERR_PTR(-ENODEV);
132 }
133
134 static struct devlink *devlink_get_from_info(struct genl_info *info)
135 {
136         return devlink_get_from_attrs(genl_info_net(info), info->attrs);
137 }
138
139 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
140                                                       int port_index)
141 {
142         struct devlink_port *devlink_port;
143
144         list_for_each_entry(devlink_port, &devlink->port_list, list) {
145                 if (devlink_port->index == port_index)
146                         return devlink_port;
147         }
148         return NULL;
149 }
150
151 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
152 {
153         return devlink_port_get_by_index(devlink, port_index);
154 }
155
156 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
157                                                         struct nlattr **attrs)
158 {
159         if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
160                 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
161                 struct devlink_port *devlink_port;
162
163                 devlink_port = devlink_port_get_by_index(devlink, port_index);
164                 if (!devlink_port)
165                         return ERR_PTR(-ENODEV);
166                 return devlink_port;
167         }
168         return ERR_PTR(-EINVAL);
169 }
170
171 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
172                                                        struct genl_info *info)
173 {
174         return devlink_port_get_from_attrs(devlink, info->attrs);
175 }
176
177 struct devlink_sb {
178         struct list_head list;
179         unsigned int index;
180         u32 size;
181         u16 ingress_pools_count;
182         u16 egress_pools_count;
183         u16 ingress_tc_count;
184         u16 egress_tc_count;
185 };
186
187 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
188 {
189         return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
190 }
191
192 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
193                                                   unsigned int sb_index)
194 {
195         struct devlink_sb *devlink_sb;
196
197         list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
198                 if (devlink_sb->index == sb_index)
199                         return devlink_sb;
200         }
201         return NULL;
202 }
203
204 static bool devlink_sb_index_exists(struct devlink *devlink,
205                                     unsigned int sb_index)
206 {
207         return devlink_sb_get_by_index(devlink, sb_index);
208 }
209
210 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
211                                                     struct nlattr **attrs)
212 {
213         if (attrs[DEVLINK_ATTR_SB_INDEX]) {
214                 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
215                 struct devlink_sb *devlink_sb;
216
217                 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
218                 if (!devlink_sb)
219                         return ERR_PTR(-ENODEV);
220                 return devlink_sb;
221         }
222         return ERR_PTR(-EINVAL);
223 }
224
225 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
226                                                    struct genl_info *info)
227 {
228         return devlink_sb_get_from_attrs(devlink, info->attrs);
229 }
230
231 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
232                                                 struct nlattr **attrs,
233                                                 u16 *p_pool_index)
234 {
235         u16 val;
236
237         if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
238                 return -EINVAL;
239
240         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
241         if (val >= devlink_sb_pool_count(devlink_sb))
242                 return -EINVAL;
243         *p_pool_index = val;
244         return 0;
245 }
246
247 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
248                                                struct genl_info *info,
249                                                u16 *p_pool_index)
250 {
251         return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
252                                                     p_pool_index);
253 }
254
255 static int
256 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
257                                     enum devlink_sb_pool_type *p_pool_type)
258 {
259         u8 val;
260
261         if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
262                 return -EINVAL;
263
264         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
265         if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
266             val != DEVLINK_SB_POOL_TYPE_EGRESS)
267                 return -EINVAL;
268         *p_pool_type = val;
269         return 0;
270 }
271
272 static int
273 devlink_sb_pool_type_get_from_info(struct genl_info *info,
274                                    enum devlink_sb_pool_type *p_pool_type)
275 {
276         return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
277 }
278
279 static int
280 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
281                                   enum devlink_sb_threshold_type *p_th_type)
282 {
283         u8 val;
284
285         if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
286                 return -EINVAL;
287
288         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
289         if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
290             val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
291                 return -EINVAL;
292         *p_th_type = val;
293         return 0;
294 }
295
296 static int
297 devlink_sb_th_type_get_from_info(struct genl_info *info,
298                                  enum devlink_sb_threshold_type *p_th_type)
299 {
300         return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
301 }
302
303 static int
304 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
305                                    struct nlattr **attrs,
306                                    enum devlink_sb_pool_type pool_type,
307                                    u16 *p_tc_index)
308 {
309         u16 val;
310
311         if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
312                 return -EINVAL;
313
314         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
315         if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
316             val >= devlink_sb->ingress_tc_count)
317                 return -EINVAL;
318         if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
319             val >= devlink_sb->egress_tc_count)
320                 return -EINVAL;
321         *p_tc_index = val;
322         return 0;
323 }
324
325 static int
326 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
327                                   struct genl_info *info,
328                                   enum devlink_sb_pool_type pool_type,
329                                   u16 *p_tc_index)
330 {
331         return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
332                                                   pool_type, p_tc_index);
333 }
334
335 #define DEVLINK_NL_FLAG_NEED_DEVLINK    BIT(0)
336 #define DEVLINK_NL_FLAG_NEED_PORT       BIT(1)
337 #define DEVLINK_NL_FLAG_NEED_SB         BIT(2)
338 #define DEVLINK_NL_FLAG_LOCK_PORTS      BIT(3)
339         /* port is not needed but we need to ensure they don't
340          * change in the middle of command
341          */
342
343 static int devlink_nl_pre_doit(const struct genl_ops *ops,
344                                struct sk_buff *skb, struct genl_info *info)
345 {
346         struct devlink *devlink;
347
348         mutex_lock(&devlink_mutex);
349         devlink = devlink_get_from_info(info);
350         if (IS_ERR(devlink)) {
351                 mutex_unlock(&devlink_mutex);
352                 return PTR_ERR(devlink);
353         }
354         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
355                 info->user_ptr[0] = devlink;
356         } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
357                 struct devlink_port *devlink_port;
358
359                 mutex_lock(&devlink_port_mutex);
360                 devlink_port = devlink_port_get_from_info(devlink, info);
361                 if (IS_ERR(devlink_port)) {
362                         mutex_unlock(&devlink_port_mutex);
363                         mutex_unlock(&devlink_mutex);
364                         return PTR_ERR(devlink_port);
365                 }
366                 info->user_ptr[0] = devlink_port;
367         }
368         if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) {
369                 mutex_lock(&devlink_port_mutex);
370         }
371         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
372                 struct devlink_sb *devlink_sb;
373
374                 devlink_sb = devlink_sb_get_from_info(devlink, info);
375                 if (IS_ERR(devlink_sb)) {
376                         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT)
377                                 mutex_unlock(&devlink_port_mutex);
378                         mutex_unlock(&devlink_mutex);
379                         return PTR_ERR(devlink_sb);
380                 }
381                 info->user_ptr[1] = devlink_sb;
382         }
383         return 0;
384 }
385
386 static void devlink_nl_post_doit(const struct genl_ops *ops,
387                                  struct sk_buff *skb, struct genl_info *info)
388 {
389         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT ||
390             ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS)
391                 mutex_unlock(&devlink_port_mutex);
392         mutex_unlock(&devlink_mutex);
393 }
394
395 static struct genl_family devlink_nl_family;
396
397 enum devlink_multicast_groups {
398         DEVLINK_MCGRP_CONFIG,
399 };
400
401 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
402         [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
403 };
404
405 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
406 {
407         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
408                 return -EMSGSIZE;
409         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
410                 return -EMSGSIZE;
411         return 0;
412 }
413
414 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
415                            enum devlink_command cmd, u32 portid,
416                            u32 seq, int flags)
417 {
418         void *hdr;
419
420         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
421         if (!hdr)
422                 return -EMSGSIZE;
423
424         if (devlink_nl_put_handle(msg, devlink))
425                 goto nla_put_failure;
426
427         genlmsg_end(msg, hdr);
428         return 0;
429
430 nla_put_failure:
431         genlmsg_cancel(msg, hdr);
432         return -EMSGSIZE;
433 }
434
435 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
436 {
437         struct sk_buff *msg;
438         int err;
439
440         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
441
442         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443         if (!msg)
444                 return;
445
446         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
447         if (err) {
448                 nlmsg_free(msg);
449                 return;
450         }
451
452         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
453                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
454 }
455
456 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
457                                 struct devlink_port *devlink_port,
458                                 enum devlink_command cmd, u32 portid,
459                                 u32 seq, int flags)
460 {
461         void *hdr;
462
463         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
464         if (!hdr)
465                 return -EMSGSIZE;
466
467         if (devlink_nl_put_handle(msg, devlink))
468                 goto nla_put_failure;
469         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
470                 goto nla_put_failure;
471         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
472                 goto nla_put_failure;
473         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
474             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
475                         devlink_port->desired_type))
476                 goto nla_put_failure;
477         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
478                 struct net_device *netdev = devlink_port->type_dev;
479
480                 if (netdev &&
481                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
482                                  netdev->ifindex) ||
483                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
484                                     netdev->name)))
485                         goto nla_put_failure;
486         }
487         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
488                 struct ib_device *ibdev = devlink_port->type_dev;
489
490                 if (ibdev &&
491                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
492                                    ibdev->name))
493                         goto nla_put_failure;
494         }
495         if (devlink_port->split &&
496             nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
497                         devlink_port->split_group))
498                 goto nla_put_failure;
499
500         genlmsg_end(msg, hdr);
501         return 0;
502
503 nla_put_failure:
504         genlmsg_cancel(msg, hdr);
505         return -EMSGSIZE;
506 }
507
508 static void devlink_port_notify(struct devlink_port *devlink_port,
509                                 enum devlink_command cmd)
510 {
511         struct devlink *devlink = devlink_port->devlink;
512         struct sk_buff *msg;
513         int err;
514
515         if (!devlink_port->registered)
516                 return;
517
518         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
519
520         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
521         if (!msg)
522                 return;
523
524         err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
525         if (err) {
526                 nlmsg_free(msg);
527                 return;
528         }
529
530         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
531                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
532 }
533
534 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
535 {
536         struct devlink *devlink = info->user_ptr[0];
537         struct sk_buff *msg;
538         int err;
539
540         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
541         if (!msg)
542                 return -ENOMEM;
543
544         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
545                               info->snd_portid, info->snd_seq, 0);
546         if (err) {
547                 nlmsg_free(msg);
548                 return err;
549         }
550
551         return genlmsg_reply(msg, info);
552 }
553
554 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
555                                      struct netlink_callback *cb)
556 {
557         struct devlink *devlink;
558         int start = cb->args[0];
559         int idx = 0;
560         int err;
561
562         mutex_lock(&devlink_mutex);
563         list_for_each_entry(devlink, &devlink_list, list) {
564                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
565                         continue;
566                 if (idx < start) {
567                         idx++;
568                         continue;
569                 }
570                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
571                                       NETLINK_CB(cb->skb).portid,
572                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
573                 if (err)
574                         goto out;
575                 idx++;
576         }
577 out:
578         mutex_unlock(&devlink_mutex);
579
580         cb->args[0] = idx;
581         return msg->len;
582 }
583
584 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
585                                         struct genl_info *info)
586 {
587         struct devlink_port *devlink_port = info->user_ptr[0];
588         struct devlink *devlink = devlink_port->devlink;
589         struct sk_buff *msg;
590         int err;
591
592         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
593         if (!msg)
594                 return -ENOMEM;
595
596         err = devlink_nl_port_fill(msg, devlink, devlink_port,
597                                    DEVLINK_CMD_PORT_NEW,
598                                    info->snd_portid, info->snd_seq, 0);
599         if (err) {
600                 nlmsg_free(msg);
601                 return err;
602         }
603
604         return genlmsg_reply(msg, info);
605 }
606
607 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
608                                           struct netlink_callback *cb)
609 {
610         struct devlink *devlink;
611         struct devlink_port *devlink_port;
612         int start = cb->args[0];
613         int idx = 0;
614         int err;
615
616         mutex_lock(&devlink_mutex);
617         mutex_lock(&devlink_port_mutex);
618         list_for_each_entry(devlink, &devlink_list, list) {
619                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
620                         continue;
621                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
622                         if (idx < start) {
623                                 idx++;
624                                 continue;
625                         }
626                         err = devlink_nl_port_fill(msg, devlink, devlink_port,
627                                                    DEVLINK_CMD_NEW,
628                                                    NETLINK_CB(cb->skb).portid,
629                                                    cb->nlh->nlmsg_seq,
630                                                    NLM_F_MULTI);
631                         if (err)
632                                 goto out;
633                         idx++;
634                 }
635         }
636 out:
637         mutex_unlock(&devlink_port_mutex);
638         mutex_unlock(&devlink_mutex);
639
640         cb->args[0] = idx;
641         return msg->len;
642 }
643
644 static int devlink_port_type_set(struct devlink *devlink,
645                                  struct devlink_port *devlink_port,
646                                  enum devlink_port_type port_type)
647
648 {
649         int err;
650
651         if (devlink->ops && devlink->ops->port_type_set) {
652                 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
653                         return -EINVAL;
654                 if (port_type == devlink_port->type)
655                         return 0;
656                 err = devlink->ops->port_type_set(devlink_port, port_type);
657                 if (err)
658                         return err;
659                 devlink_port->desired_type = port_type;
660                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
661                 return 0;
662         }
663         return -EOPNOTSUPP;
664 }
665
666 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
667                                         struct genl_info *info)
668 {
669         struct devlink_port *devlink_port = info->user_ptr[0];
670         struct devlink *devlink = devlink_port->devlink;
671         int err;
672
673         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
674                 enum devlink_port_type port_type;
675
676                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
677                 err = devlink_port_type_set(devlink, devlink_port, port_type);
678                 if (err)
679                         return err;
680         }
681         return 0;
682 }
683
684 static int devlink_port_split(struct devlink *devlink,
685                               u32 port_index, u32 count)
686
687 {
688         if (devlink->ops && devlink->ops->port_split)
689                 return devlink->ops->port_split(devlink, port_index, count);
690         return -EOPNOTSUPP;
691 }
692
693 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
694                                           struct genl_info *info)
695 {
696         struct devlink *devlink = info->user_ptr[0];
697         u32 port_index;
698         u32 count;
699
700         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
701             !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
702                 return -EINVAL;
703
704         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
705         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
706         return devlink_port_split(devlink, port_index, count);
707 }
708
709 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
710
711 {
712         if (devlink->ops && devlink->ops->port_unsplit)
713                 return devlink->ops->port_unsplit(devlink, port_index);
714         return -EOPNOTSUPP;
715 }
716
717 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
718                                             struct genl_info *info)
719 {
720         struct devlink *devlink = info->user_ptr[0];
721         u32 port_index;
722
723         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
724                 return -EINVAL;
725
726         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
727         return devlink_port_unsplit(devlink, port_index);
728 }
729
730 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
731                               struct devlink_sb *devlink_sb,
732                               enum devlink_command cmd, u32 portid,
733                               u32 seq, int flags)
734 {
735         void *hdr;
736
737         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
738         if (!hdr)
739                 return -EMSGSIZE;
740
741         if (devlink_nl_put_handle(msg, devlink))
742                 goto nla_put_failure;
743         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
744                 goto nla_put_failure;
745         if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
746                 goto nla_put_failure;
747         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
748                         devlink_sb->ingress_pools_count))
749                 goto nla_put_failure;
750         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
751                         devlink_sb->egress_pools_count))
752                 goto nla_put_failure;
753         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
754                         devlink_sb->ingress_tc_count))
755                 goto nla_put_failure;
756         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
757                         devlink_sb->egress_tc_count))
758                 goto nla_put_failure;
759
760         genlmsg_end(msg, hdr);
761         return 0;
762
763 nla_put_failure:
764         genlmsg_cancel(msg, hdr);
765         return -EMSGSIZE;
766 }
767
768 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
769                                       struct genl_info *info)
770 {
771         struct devlink *devlink = info->user_ptr[0];
772         struct devlink_sb *devlink_sb = info->user_ptr[1];
773         struct sk_buff *msg;
774         int err;
775
776         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
777         if (!msg)
778                 return -ENOMEM;
779
780         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
781                                  DEVLINK_CMD_SB_NEW,
782                                  info->snd_portid, info->snd_seq, 0);
783         if (err) {
784                 nlmsg_free(msg);
785                 return err;
786         }
787
788         return genlmsg_reply(msg, info);
789 }
790
791 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
792                                         struct netlink_callback *cb)
793 {
794         struct devlink *devlink;
795         struct devlink_sb *devlink_sb;
796         int start = cb->args[0];
797         int idx = 0;
798         int err;
799
800         mutex_lock(&devlink_mutex);
801         list_for_each_entry(devlink, &devlink_list, list) {
802                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
803                         continue;
804                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
805                         if (idx < start) {
806                                 idx++;
807                                 continue;
808                         }
809                         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
810                                                  DEVLINK_CMD_SB_NEW,
811                                                  NETLINK_CB(cb->skb).portid,
812                                                  cb->nlh->nlmsg_seq,
813                                                  NLM_F_MULTI);
814                         if (err)
815                                 goto out;
816                         idx++;
817                 }
818         }
819 out:
820         mutex_unlock(&devlink_mutex);
821
822         cb->args[0] = idx;
823         return msg->len;
824 }
825
826 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
827                                    struct devlink_sb *devlink_sb,
828                                    u16 pool_index, enum devlink_command cmd,
829                                    u32 portid, u32 seq, int flags)
830 {
831         struct devlink_sb_pool_info pool_info;
832         void *hdr;
833         int err;
834
835         err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
836                                         pool_index, &pool_info);
837         if (err)
838                 return err;
839
840         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
841         if (!hdr)
842                 return -EMSGSIZE;
843
844         if (devlink_nl_put_handle(msg, devlink))
845                 goto nla_put_failure;
846         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
847                 goto nla_put_failure;
848         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
849                 goto nla_put_failure;
850         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
851                 goto nla_put_failure;
852         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
853                 goto nla_put_failure;
854         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
855                        pool_info.threshold_type))
856                 goto nla_put_failure;
857
858         genlmsg_end(msg, hdr);
859         return 0;
860
861 nla_put_failure:
862         genlmsg_cancel(msg, hdr);
863         return -EMSGSIZE;
864 }
865
866 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
867                                            struct genl_info *info)
868 {
869         struct devlink *devlink = info->user_ptr[0];
870         struct devlink_sb *devlink_sb = info->user_ptr[1];
871         struct sk_buff *msg;
872         u16 pool_index;
873         int err;
874
875         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
876                                                   &pool_index);
877         if (err)
878                 return err;
879
880         if (!devlink->ops || !devlink->ops->sb_pool_get)
881                 return -EOPNOTSUPP;
882
883         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
884         if (!msg)
885                 return -ENOMEM;
886
887         err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
888                                       DEVLINK_CMD_SB_POOL_NEW,
889                                       info->snd_portid, info->snd_seq, 0);
890         if (err) {
891                 nlmsg_free(msg);
892                 return err;
893         }
894
895         return genlmsg_reply(msg, info);
896 }
897
898 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
899                                 struct devlink *devlink,
900                                 struct devlink_sb *devlink_sb,
901                                 u32 portid, u32 seq)
902 {
903         u16 pool_count = devlink_sb_pool_count(devlink_sb);
904         u16 pool_index;
905         int err;
906
907         for (pool_index = 0; pool_index < pool_count; pool_index++) {
908                 if (*p_idx < start) {
909                         (*p_idx)++;
910                         continue;
911                 }
912                 err = devlink_nl_sb_pool_fill(msg, devlink,
913                                               devlink_sb,
914                                               pool_index,
915                                               DEVLINK_CMD_SB_POOL_NEW,
916                                               portid, seq, NLM_F_MULTI);
917                 if (err)
918                         return err;
919                 (*p_idx)++;
920         }
921         return 0;
922 }
923
924 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
925                                              struct netlink_callback *cb)
926 {
927         struct devlink *devlink;
928         struct devlink_sb *devlink_sb;
929         int start = cb->args[0];
930         int idx = 0;
931         int err;
932
933         mutex_lock(&devlink_mutex);
934         list_for_each_entry(devlink, &devlink_list, list) {
935                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
936                     !devlink->ops || !devlink->ops->sb_pool_get)
937                         continue;
938                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
939                         err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
940                                                    devlink_sb,
941                                                    NETLINK_CB(cb->skb).portid,
942                                                    cb->nlh->nlmsg_seq);
943                         if (err && err != -EOPNOTSUPP)
944                                 goto out;
945                 }
946         }
947 out:
948         mutex_unlock(&devlink_mutex);
949
950         cb->args[0] = idx;
951         return msg->len;
952 }
953
954 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
955                                u16 pool_index, u32 size,
956                                enum devlink_sb_threshold_type threshold_type)
957
958 {
959         const struct devlink_ops *ops = devlink->ops;
960
961         if (ops && ops->sb_pool_set)
962                 return ops->sb_pool_set(devlink, sb_index, pool_index,
963                                         size, threshold_type);
964         return -EOPNOTSUPP;
965 }
966
967 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
968                                            struct genl_info *info)
969 {
970         struct devlink *devlink = info->user_ptr[0];
971         struct devlink_sb *devlink_sb = info->user_ptr[1];
972         enum devlink_sb_threshold_type threshold_type;
973         u16 pool_index;
974         u32 size;
975         int err;
976
977         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
978                                                   &pool_index);
979         if (err)
980                 return err;
981
982         err = devlink_sb_th_type_get_from_info(info, &threshold_type);
983         if (err)
984                 return err;
985
986         if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
987                 return -EINVAL;
988
989         size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
990         return devlink_sb_pool_set(devlink, devlink_sb->index,
991                                    pool_index, size, threshold_type);
992 }
993
994 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
995                                         struct devlink *devlink,
996                                         struct devlink_port *devlink_port,
997                                         struct devlink_sb *devlink_sb,
998                                         u16 pool_index,
999                                         enum devlink_command cmd,
1000                                         u32 portid, u32 seq, int flags)
1001 {
1002         const struct devlink_ops *ops = devlink->ops;
1003         u32 threshold;
1004         void *hdr;
1005         int err;
1006
1007         err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1008                                     pool_index, &threshold);
1009         if (err)
1010                 return err;
1011
1012         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1013         if (!hdr)
1014                 return -EMSGSIZE;
1015
1016         if (devlink_nl_put_handle(msg, devlink))
1017                 goto nla_put_failure;
1018         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1019                 goto nla_put_failure;
1020         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1021                 goto nla_put_failure;
1022         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1023                 goto nla_put_failure;
1024         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1025                 goto nla_put_failure;
1026
1027         if (ops->sb_occ_port_pool_get) {
1028                 u32 cur;
1029                 u32 max;
1030
1031                 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1032                                                 pool_index, &cur, &max);
1033                 if (err && err != -EOPNOTSUPP)
1034                         goto sb_occ_get_failure;
1035                 if (!err) {
1036                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1037                                 goto nla_put_failure;
1038                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1039                                 goto nla_put_failure;
1040                 }
1041         }
1042
1043         genlmsg_end(msg, hdr);
1044         return 0;
1045
1046 nla_put_failure:
1047         err = -EMSGSIZE;
1048 sb_occ_get_failure:
1049         genlmsg_cancel(msg, hdr);
1050         return err;
1051 }
1052
1053 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1054                                                 struct genl_info *info)
1055 {
1056         struct devlink_port *devlink_port = info->user_ptr[0];
1057         struct devlink *devlink = devlink_port->devlink;
1058         struct devlink_sb *devlink_sb = info->user_ptr[1];
1059         struct sk_buff *msg;
1060         u16 pool_index;
1061         int err;
1062
1063         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1064                                                   &pool_index);
1065         if (err)
1066                 return err;
1067
1068         if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1069                 return -EOPNOTSUPP;
1070
1071         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1072         if (!msg)
1073                 return -ENOMEM;
1074
1075         err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1076                                            devlink_sb, pool_index,
1077                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1078                                            info->snd_portid, info->snd_seq, 0);
1079         if (err) {
1080                 nlmsg_free(msg);
1081                 return err;
1082         }
1083
1084         return genlmsg_reply(msg, info);
1085 }
1086
1087 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1088                                      struct devlink *devlink,
1089                                      struct devlink_sb *devlink_sb,
1090                                      u32 portid, u32 seq)
1091 {
1092         struct devlink_port *devlink_port;
1093         u16 pool_count = devlink_sb_pool_count(devlink_sb);
1094         u16 pool_index;
1095         int err;
1096
1097         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1098                 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1099                         if (*p_idx < start) {
1100                                 (*p_idx)++;
1101                                 continue;
1102                         }
1103                         err = devlink_nl_sb_port_pool_fill(msg, devlink,
1104                                                            devlink_port,
1105                                                            devlink_sb,
1106                                                            pool_index,
1107                                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1108                                                            portid, seq,
1109                                                            NLM_F_MULTI);
1110                         if (err)
1111                                 return err;
1112                         (*p_idx)++;
1113                 }
1114         }
1115         return 0;
1116 }
1117
1118 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1119                                                   struct netlink_callback *cb)
1120 {
1121         struct devlink *devlink;
1122         struct devlink_sb *devlink_sb;
1123         int start = cb->args[0];
1124         int idx = 0;
1125         int err;
1126
1127         mutex_lock(&devlink_mutex);
1128         mutex_lock(&devlink_port_mutex);
1129         list_for_each_entry(devlink, &devlink_list, list) {
1130                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1131                     !devlink->ops || !devlink->ops->sb_port_pool_get)
1132                         continue;
1133                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1134                         err = __sb_port_pool_get_dumpit(msg, start, &idx,
1135                                                         devlink, devlink_sb,
1136                                                         NETLINK_CB(cb->skb).portid,
1137                                                         cb->nlh->nlmsg_seq);
1138                         if (err && err != -EOPNOTSUPP)
1139                                 goto out;
1140                 }
1141         }
1142 out:
1143         mutex_unlock(&devlink_port_mutex);
1144         mutex_unlock(&devlink_mutex);
1145
1146         cb->args[0] = idx;
1147         return msg->len;
1148 }
1149
1150 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1151                                     unsigned int sb_index, u16 pool_index,
1152                                     u32 threshold)
1153
1154 {
1155         const struct devlink_ops *ops = devlink_port->devlink->ops;
1156
1157         if (ops && ops->sb_port_pool_set)
1158                 return ops->sb_port_pool_set(devlink_port, sb_index,
1159                                              pool_index, threshold);
1160         return -EOPNOTSUPP;
1161 }
1162
1163 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1164                                                 struct genl_info *info)
1165 {
1166         struct devlink_port *devlink_port = info->user_ptr[0];
1167         struct devlink_sb *devlink_sb = info->user_ptr[1];
1168         u16 pool_index;
1169         u32 threshold;
1170         int err;
1171
1172         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1173                                                   &pool_index);
1174         if (err)
1175                 return err;
1176
1177         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1178                 return -EINVAL;
1179
1180         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1181         return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1182                                         pool_index, threshold);
1183 }
1184
1185 static int
1186 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1187                                 struct devlink_port *devlink_port,
1188                                 struct devlink_sb *devlink_sb, u16 tc_index,
1189                                 enum devlink_sb_pool_type pool_type,
1190                                 enum devlink_command cmd,
1191                                 u32 portid, u32 seq, int flags)
1192 {
1193         const struct devlink_ops *ops = devlink->ops;
1194         u16 pool_index;
1195         u32 threshold;
1196         void *hdr;
1197         int err;
1198
1199         err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1200                                        tc_index, pool_type,
1201                                        &pool_index, &threshold);
1202         if (err)
1203                 return err;
1204
1205         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1206         if (!hdr)
1207                 return -EMSGSIZE;
1208
1209         if (devlink_nl_put_handle(msg, devlink))
1210                 goto nla_put_failure;
1211         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1212                 goto nla_put_failure;
1213         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1214                 goto nla_put_failure;
1215         if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1216                 goto nla_put_failure;
1217         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1218                 goto nla_put_failure;
1219         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1220                 goto nla_put_failure;
1221         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1222                 goto nla_put_failure;
1223
1224         if (ops->sb_occ_tc_port_bind_get) {
1225                 u32 cur;
1226                 u32 max;
1227
1228                 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1229                                                    devlink_sb->index,
1230                                                    tc_index, pool_type,
1231                                                    &cur, &max);
1232                 if (err && err != -EOPNOTSUPP)
1233                         return err;
1234                 if (!err) {
1235                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1236                                 goto nla_put_failure;
1237                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1238                                 goto nla_put_failure;
1239                 }
1240         }
1241
1242         genlmsg_end(msg, hdr);
1243         return 0;
1244
1245 nla_put_failure:
1246         genlmsg_cancel(msg, hdr);
1247         return -EMSGSIZE;
1248 }
1249
1250 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1251                                                    struct genl_info *info)
1252 {
1253         struct devlink_port *devlink_port = info->user_ptr[0];
1254         struct devlink *devlink = devlink_port->devlink;
1255         struct devlink_sb *devlink_sb = info->user_ptr[1];
1256         struct sk_buff *msg;
1257         enum devlink_sb_pool_type pool_type;
1258         u16 tc_index;
1259         int err;
1260
1261         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1262         if (err)
1263                 return err;
1264
1265         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1266                                                 pool_type, &tc_index);
1267         if (err)
1268                 return err;
1269
1270         if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1271                 return -EOPNOTSUPP;
1272
1273         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1274         if (!msg)
1275                 return -ENOMEM;
1276
1277         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1278                                               devlink_sb, tc_index, pool_type,
1279                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1280                                               info->snd_portid,
1281                                               info->snd_seq, 0);
1282         if (err) {
1283                 nlmsg_free(msg);
1284                 return err;
1285         }
1286
1287         return genlmsg_reply(msg, info);
1288 }
1289
1290 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1291                                         int start, int *p_idx,
1292                                         struct devlink *devlink,
1293                                         struct devlink_sb *devlink_sb,
1294                                         u32 portid, u32 seq)
1295 {
1296         struct devlink_port *devlink_port;
1297         u16 tc_index;
1298         int err;
1299
1300         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1301                 for (tc_index = 0;
1302                      tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1303                         if (*p_idx < start) {
1304                                 (*p_idx)++;
1305                                 continue;
1306                         }
1307                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1308                                                               devlink_port,
1309                                                               devlink_sb,
1310                                                               tc_index,
1311                                                               DEVLINK_SB_POOL_TYPE_INGRESS,
1312                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1313                                                               portid, seq,
1314                                                               NLM_F_MULTI);
1315                         if (err)
1316                                 return err;
1317                         (*p_idx)++;
1318                 }
1319                 for (tc_index = 0;
1320                      tc_index < devlink_sb->egress_tc_count; tc_index++) {
1321                         if (*p_idx < start) {
1322                                 (*p_idx)++;
1323                                 continue;
1324                         }
1325                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1326                                                               devlink_port,
1327                                                               devlink_sb,
1328                                                               tc_index,
1329                                                               DEVLINK_SB_POOL_TYPE_EGRESS,
1330                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1331                                                               portid, seq,
1332                                                               NLM_F_MULTI);
1333                         if (err)
1334                                 return err;
1335                         (*p_idx)++;
1336                 }
1337         }
1338         return 0;
1339 }
1340
1341 static int
1342 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1343                                           struct netlink_callback *cb)
1344 {
1345         struct devlink *devlink;
1346         struct devlink_sb *devlink_sb;
1347         int start = cb->args[0];
1348         int idx = 0;
1349         int err;
1350
1351         mutex_lock(&devlink_mutex);
1352         mutex_lock(&devlink_port_mutex);
1353         list_for_each_entry(devlink, &devlink_list, list) {
1354                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1355                     !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1356                         continue;
1357                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1358                         err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1359                                                            devlink,
1360                                                            devlink_sb,
1361                                                            NETLINK_CB(cb->skb).portid,
1362                                                            cb->nlh->nlmsg_seq);
1363                         if (err && err != -EOPNOTSUPP)
1364                                 goto out;
1365                 }
1366         }
1367 out:
1368         mutex_unlock(&devlink_port_mutex);
1369         mutex_unlock(&devlink_mutex);
1370
1371         cb->args[0] = idx;
1372         return msg->len;
1373 }
1374
1375 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1376                                        unsigned int sb_index, u16 tc_index,
1377                                        enum devlink_sb_pool_type pool_type,
1378                                        u16 pool_index, u32 threshold)
1379
1380 {
1381         const struct devlink_ops *ops = devlink_port->devlink->ops;
1382
1383         if (ops && ops->sb_tc_pool_bind_set)
1384                 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1385                                                 tc_index, pool_type,
1386                                                 pool_index, threshold);
1387         return -EOPNOTSUPP;
1388 }
1389
1390 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1391                                                    struct genl_info *info)
1392 {
1393         struct devlink_port *devlink_port = info->user_ptr[0];
1394         struct devlink_sb *devlink_sb = info->user_ptr[1];
1395         enum devlink_sb_pool_type pool_type;
1396         u16 tc_index;
1397         u16 pool_index;
1398         u32 threshold;
1399         int err;
1400
1401         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1402         if (err)
1403                 return err;
1404
1405         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1406                                                 pool_type, &tc_index);
1407         if (err)
1408                 return err;
1409
1410         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1411                                                   &pool_index);
1412         if (err)
1413                 return err;
1414
1415         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1416                 return -EINVAL;
1417
1418         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1419         return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1420                                            tc_index, pool_type,
1421                                            pool_index, threshold);
1422 }
1423
1424 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1425                                                struct genl_info *info)
1426 {
1427         struct devlink *devlink = info->user_ptr[0];
1428         struct devlink_sb *devlink_sb = info->user_ptr[1];
1429         const struct devlink_ops *ops = devlink->ops;
1430
1431         if (ops && ops->sb_occ_snapshot)
1432                 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1433         return -EOPNOTSUPP;
1434 }
1435
1436 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1437                                                 struct genl_info *info)
1438 {
1439         struct devlink *devlink = info->user_ptr[0];
1440         struct devlink_sb *devlink_sb = info->user_ptr[1];
1441         const struct devlink_ops *ops = devlink->ops;
1442
1443         if (ops && ops->sb_occ_max_clear)
1444                 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1445         return -EOPNOTSUPP;
1446 }
1447
1448 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1449                                    enum devlink_command cmd, u32 portid,
1450                                    u32 seq, int flags)
1451 {
1452         const struct devlink_ops *ops = devlink->ops;
1453         u8 inline_mode, encap_mode;
1454         void *hdr;
1455         int err = 0;
1456         u16 mode;
1457
1458         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1459         if (!hdr)
1460                 return -EMSGSIZE;
1461
1462         err = devlink_nl_put_handle(msg, devlink);
1463         if (err)
1464                 goto nla_put_failure;
1465
1466         if (ops->eswitch_mode_get) {
1467                 err = ops->eswitch_mode_get(devlink, &mode);
1468                 if (err)
1469                         goto nla_put_failure;
1470                 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1471                 if (err)
1472                         goto nla_put_failure;
1473         }
1474
1475         if (ops->eswitch_inline_mode_get) {
1476                 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1477                 if (err)
1478                         goto nla_put_failure;
1479                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1480                                  inline_mode);
1481                 if (err)
1482                         goto nla_put_failure;
1483         }
1484
1485         if (ops->eswitch_encap_mode_get) {
1486                 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1487                 if (err)
1488                         goto nla_put_failure;
1489                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1490                 if (err)
1491                         goto nla_put_failure;
1492         }
1493
1494         genlmsg_end(msg, hdr);
1495         return 0;
1496
1497 nla_put_failure:
1498         genlmsg_cancel(msg, hdr);
1499         return err;
1500 }
1501
1502 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1503                                            struct genl_info *info)
1504 {
1505         struct devlink *devlink = info->user_ptr[0];
1506         const struct devlink_ops *ops = devlink->ops;
1507         struct sk_buff *msg;
1508         int err;
1509
1510         if (!ops)
1511                 return -EOPNOTSUPP;
1512
1513         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1514         if (!msg)
1515                 return -ENOMEM;
1516
1517         err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1518                                       info->snd_portid, info->snd_seq, 0);
1519
1520         if (err) {
1521                 nlmsg_free(msg);
1522                 return err;
1523         }
1524
1525         return genlmsg_reply(msg, info);
1526 }
1527
1528 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1529                                            struct genl_info *info)
1530 {
1531         struct devlink *devlink = info->user_ptr[0];
1532         const struct devlink_ops *ops = devlink->ops;
1533         u8 inline_mode, encap_mode;
1534         int err = 0;
1535         u16 mode;
1536
1537         if (!ops)
1538                 return -EOPNOTSUPP;
1539
1540         if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1541                 if (!ops->eswitch_mode_set)
1542                         return -EOPNOTSUPP;
1543                 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1544                 err = ops->eswitch_mode_set(devlink, mode);
1545                 if (err)
1546                         return err;
1547         }
1548
1549         if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1550                 if (!ops->eswitch_inline_mode_set)
1551                         return -EOPNOTSUPP;
1552                 inline_mode = nla_get_u8(
1553                                 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1554                 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1555                 if (err)
1556                         return err;
1557         }
1558
1559         if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1560                 if (!ops->eswitch_encap_mode_set)
1561                         return -EOPNOTSUPP;
1562                 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1563                 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1564                 if (err)
1565                         return err;
1566         }
1567
1568         return 0;
1569 }
1570
1571 int devlink_dpipe_match_put(struct sk_buff *skb,
1572                             struct devlink_dpipe_match *match)
1573 {
1574         struct devlink_dpipe_header *header = match->header;
1575         struct devlink_dpipe_field *field = &header->fields[match->field_id];
1576         struct nlattr *match_attr;
1577
1578         match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1579         if (!match_attr)
1580                 return -EMSGSIZE;
1581
1582         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1583             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1584             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1585             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1586             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1587                 goto nla_put_failure;
1588
1589         nla_nest_end(skb, match_attr);
1590         return 0;
1591
1592 nla_put_failure:
1593         nla_nest_cancel(skb, match_attr);
1594         return -EMSGSIZE;
1595 }
1596 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1597
1598 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1599                                      struct sk_buff *skb)
1600 {
1601         struct nlattr *matches_attr;
1602
1603         matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1604         if (!matches_attr)
1605                 return -EMSGSIZE;
1606
1607         if (table->table_ops->matches_dump(table->priv, skb))
1608                 goto nla_put_failure;
1609
1610         nla_nest_end(skb, matches_attr);
1611         return 0;
1612
1613 nla_put_failure:
1614         nla_nest_cancel(skb, matches_attr);
1615         return -EMSGSIZE;
1616 }
1617
1618 int devlink_dpipe_action_put(struct sk_buff *skb,
1619                              struct devlink_dpipe_action *action)
1620 {
1621         struct devlink_dpipe_header *header = action->header;
1622         struct devlink_dpipe_field *field = &header->fields[action->field_id];
1623         struct nlattr *action_attr;
1624
1625         action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1626         if (!action_attr)
1627                 return -EMSGSIZE;
1628
1629         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1630             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1631             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1632             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1633             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1634                 goto nla_put_failure;
1635
1636         nla_nest_end(skb, action_attr);
1637         return 0;
1638
1639 nla_put_failure:
1640         nla_nest_cancel(skb, action_attr);
1641         return -EMSGSIZE;
1642 }
1643 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1644
1645 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1646                                      struct sk_buff *skb)
1647 {
1648         struct nlattr *actions_attr;
1649
1650         actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1651         if (!actions_attr)
1652                 return -EMSGSIZE;
1653
1654         if (table->table_ops->actions_dump(table->priv, skb))
1655                 goto nla_put_failure;
1656
1657         nla_nest_end(skb, actions_attr);
1658         return 0;
1659
1660 nla_put_failure:
1661         nla_nest_cancel(skb, actions_attr);
1662         return -EMSGSIZE;
1663 }
1664
1665 static int devlink_dpipe_table_put(struct sk_buff *skb,
1666                                    struct devlink_dpipe_table *table)
1667 {
1668         struct nlattr *table_attr;
1669         u64 table_size;
1670
1671         table_size = table->table_ops->size_get(table->priv);
1672         table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1673         if (!table_attr)
1674                 return -EMSGSIZE;
1675
1676         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1677             nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1678                               DEVLINK_ATTR_PAD))
1679                 goto nla_put_failure;
1680         if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1681                        table->counters_enabled))
1682                 goto nla_put_failure;
1683
1684         if (devlink_dpipe_matches_put(table, skb))
1685                 goto nla_put_failure;
1686
1687         if (devlink_dpipe_actions_put(table, skb))
1688                 goto nla_put_failure;
1689
1690         nla_nest_end(skb, table_attr);
1691         return 0;
1692
1693 nla_put_failure:
1694         nla_nest_cancel(skb, table_attr);
1695         return -EMSGSIZE;
1696 }
1697
1698 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1699                                             struct genl_info *info)
1700 {
1701         int err;
1702
1703         if (*pskb) {
1704                 err = genlmsg_reply(*pskb, info);
1705                 if (err)
1706                         return err;
1707         }
1708         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1709         if (!*pskb)
1710                 return -ENOMEM;
1711         return 0;
1712 }
1713
1714 static int devlink_dpipe_tables_fill(struct genl_info *info,
1715                                      enum devlink_command cmd, int flags,
1716                                      struct list_head *dpipe_tables,
1717                                      const char *table_name)
1718 {
1719         struct devlink *devlink = info->user_ptr[0];
1720         struct devlink_dpipe_table *table;
1721         struct nlattr *tables_attr;
1722         struct sk_buff *skb = NULL;
1723         struct nlmsghdr *nlh;
1724         bool incomplete;
1725         void *hdr;
1726         int i;
1727         int err;
1728
1729         table = list_first_entry(dpipe_tables,
1730                                  struct devlink_dpipe_table, list);
1731 start_again:
1732         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1733         if (err)
1734                 return err;
1735
1736         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1737                           &devlink_nl_family, NLM_F_MULTI, cmd);
1738         if (!hdr) {
1739                 nlmsg_free(skb);
1740                 return -EMSGSIZE;
1741         }
1742
1743         if (devlink_nl_put_handle(skb, devlink))
1744                 goto nla_put_failure;
1745         tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1746         if (!tables_attr)
1747                 goto nla_put_failure;
1748
1749         i = 0;
1750         incomplete = false;
1751         list_for_each_entry_from(table, dpipe_tables, list) {
1752                 if (!table_name) {
1753                         err = devlink_dpipe_table_put(skb, table);
1754                         if (err) {
1755                                 if (!i)
1756                                         goto err_table_put;
1757                                 incomplete = true;
1758                                 break;
1759                         }
1760                 } else {
1761                         if (!strcmp(table->name, table_name)) {
1762                                 err = devlink_dpipe_table_put(skb, table);
1763                                 if (err)
1764                                         break;
1765                         }
1766                 }
1767                 i++;
1768         }
1769
1770         nla_nest_end(skb, tables_attr);
1771         genlmsg_end(skb, hdr);
1772         if (incomplete)
1773                 goto start_again;
1774
1775 send_done:
1776         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1777                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
1778         if (!nlh) {
1779                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1780                 if (err)
1781                         return err;
1782                 goto send_done;
1783         }
1784
1785         return genlmsg_reply(skb, info);
1786
1787 nla_put_failure:
1788         err = -EMSGSIZE;
1789 err_table_put:
1790         genlmsg_cancel(skb, hdr);
1791         nlmsg_free(skb);
1792         return err;
1793 }
1794
1795 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1796                                           struct genl_info *info)
1797 {
1798         struct devlink *devlink = info->user_ptr[0];
1799         const char *table_name =  NULL;
1800
1801         if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1802                 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1803
1804         return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1805                                          &devlink->dpipe_table_list,
1806                                          table_name);
1807 }
1808
1809 static int devlink_dpipe_value_put(struct sk_buff *skb,
1810                                    struct devlink_dpipe_value *value)
1811 {
1812         if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1813                     value->value_size, value->value))
1814                 return -EMSGSIZE;
1815         if (value->mask)
1816                 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1817                             value->value_size, value->mask))
1818                         return -EMSGSIZE;
1819         if (value->mapping_valid)
1820                 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1821                                 value->mapping_value))
1822                         return -EMSGSIZE;
1823         return 0;
1824 }
1825
1826 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1827                                           struct devlink_dpipe_value *value)
1828 {
1829         if (!value->action)
1830                 return -EINVAL;
1831         if (devlink_dpipe_action_put(skb, value->action))
1832                 return -EMSGSIZE;
1833         if (devlink_dpipe_value_put(skb, value))
1834                 return -EMSGSIZE;
1835         return 0;
1836 }
1837
1838 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1839                                            struct devlink_dpipe_value *values,
1840                                            unsigned int values_count)
1841 {
1842         struct nlattr *action_attr;
1843         int i;
1844         int err;
1845
1846         for (i = 0; i < values_count; i++) {
1847                 action_attr = nla_nest_start(skb,
1848                                              DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1849                 if (!action_attr)
1850                         return -EMSGSIZE;
1851                 err = devlink_dpipe_action_value_put(skb, &values[i]);
1852                 if (err)
1853                         goto err_action_value_put;
1854                 nla_nest_end(skb, action_attr);
1855         }
1856         return 0;
1857
1858 err_action_value_put:
1859         nla_nest_cancel(skb, action_attr);
1860         return err;
1861 }
1862
1863 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1864                                          struct devlink_dpipe_value *value)
1865 {
1866         if (!value->match)
1867                 return -EINVAL;
1868         if (devlink_dpipe_match_put(skb, value->match))
1869                 return -EMSGSIZE;
1870         if (devlink_dpipe_value_put(skb, value))
1871                 return -EMSGSIZE;
1872         return 0;
1873 }
1874
1875 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1876                                           struct devlink_dpipe_value *values,
1877                                           unsigned int values_count)
1878 {
1879         struct nlattr *match_attr;
1880         int i;
1881         int err;
1882
1883         for (i = 0; i < values_count; i++) {
1884                 match_attr = nla_nest_start(skb,
1885                                             DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1886                 if (!match_attr)
1887                         return -EMSGSIZE;
1888                 err = devlink_dpipe_match_value_put(skb, &values[i]);
1889                 if (err)
1890                         goto err_match_value_put;
1891                 nla_nest_end(skb, match_attr);
1892         }
1893         return 0;
1894
1895 err_match_value_put:
1896         nla_nest_cancel(skb, match_attr);
1897         return err;
1898 }
1899
1900 static int devlink_dpipe_entry_put(struct sk_buff *skb,
1901                                    struct devlink_dpipe_entry *entry)
1902 {
1903         struct nlattr *entry_attr, *matches_attr, *actions_attr;
1904         int err;
1905
1906         entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1907         if (!entry_attr)
1908                 return  -EMSGSIZE;
1909
1910         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
1911                               DEVLINK_ATTR_PAD))
1912                 goto nla_put_failure;
1913         if (entry->counter_valid)
1914                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
1915                                       entry->counter, DEVLINK_ATTR_PAD))
1916                         goto nla_put_failure;
1917
1918         matches_attr = nla_nest_start(skb,
1919                                       DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
1920         if (!matches_attr)
1921                 goto nla_put_failure;
1922
1923         err = devlink_dpipe_match_values_put(skb, entry->match_values,
1924                                              entry->match_values_count);
1925         if (err) {
1926                 nla_nest_cancel(skb, matches_attr);
1927                 goto err_match_values_put;
1928         }
1929         nla_nest_end(skb, matches_attr);
1930
1931         actions_attr = nla_nest_start(skb,
1932                                       DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
1933         if (!actions_attr)
1934                 goto nla_put_failure;
1935
1936         err = devlink_dpipe_action_values_put(skb, entry->action_values,
1937                                               entry->action_values_count);
1938         if (err) {
1939                 nla_nest_cancel(skb, actions_attr);
1940                 goto err_action_values_put;
1941         }
1942         nla_nest_end(skb, actions_attr);
1943
1944         nla_nest_end(skb, entry_attr);
1945         return 0;
1946
1947 nla_put_failure:
1948         err = -EMSGSIZE;
1949 err_match_values_put:
1950 err_action_values_put:
1951         nla_nest_cancel(skb, entry_attr);
1952         return err;
1953 }
1954
1955 static struct devlink_dpipe_table *
1956 devlink_dpipe_table_find(struct list_head *dpipe_tables,
1957                          const char *table_name)
1958 {
1959         struct devlink_dpipe_table *table;
1960
1961         list_for_each_entry_rcu(table, dpipe_tables, list) {
1962                 if (!strcmp(table->name, table_name))
1963                         return table;
1964         }
1965         return NULL;
1966 }
1967
1968 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
1969 {
1970         struct devlink *devlink;
1971         int err;
1972
1973         err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
1974                                                dump_ctx->info);
1975         if (err)
1976                 return err;
1977
1978         dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
1979                                     dump_ctx->info->snd_portid,
1980                                     dump_ctx->info->snd_seq,
1981                                     &devlink_nl_family, NLM_F_MULTI,
1982                                     dump_ctx->cmd);
1983         if (!dump_ctx->hdr)
1984                 goto nla_put_failure;
1985
1986         devlink = dump_ctx->info->user_ptr[0];
1987         if (devlink_nl_put_handle(dump_ctx->skb, devlink))
1988                 goto nla_put_failure;
1989         dump_ctx->nest = nla_nest_start(dump_ctx->skb,
1990                                         DEVLINK_ATTR_DPIPE_ENTRIES);
1991         if (!dump_ctx->nest)
1992                 goto nla_put_failure;
1993         return 0;
1994
1995 nla_put_failure:
1996         genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
1997         nlmsg_free(dump_ctx->skb);
1998         return -EMSGSIZE;
1999 }
2000 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2001
2002 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2003                                    struct devlink_dpipe_entry *entry)
2004 {
2005         return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2006 }
2007 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2008
2009 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2010 {
2011         nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2012         genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2013         return 0;
2014 }
2015 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2016
2017 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2018
2019 {
2020         unsigned int value_count, value_index;
2021         struct devlink_dpipe_value *value;
2022
2023         value = entry->action_values;
2024         value_count = entry->action_values_count;
2025         for (value_index = 0; value_index < value_count; value_index++) {
2026                 kfree(value[value_index].value);
2027                 kfree(value[value_index].mask);
2028         }
2029
2030         value = entry->match_values;
2031         value_count = entry->match_values_count;
2032         for (value_index = 0; value_index < value_count; value_index++) {
2033                 kfree(value[value_index].value);
2034                 kfree(value[value_index].mask);
2035         }
2036 }
2037 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2038
2039 static int devlink_dpipe_entries_fill(struct genl_info *info,
2040                                       enum devlink_command cmd, int flags,
2041                                       struct devlink_dpipe_table *table)
2042 {
2043         struct devlink_dpipe_dump_ctx dump_ctx;
2044         struct nlmsghdr *nlh;
2045         int err;
2046
2047         dump_ctx.skb = NULL;
2048         dump_ctx.cmd = cmd;
2049         dump_ctx.info = info;
2050
2051         err = table->table_ops->entries_dump(table->priv,
2052                                              table->counters_enabled,
2053                                              &dump_ctx);
2054         if (err)
2055                 return err;
2056
2057 send_done:
2058         nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2059                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2060         if (!nlh) {
2061                 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2062                 if (err)
2063                         return err;
2064                 goto send_done;
2065         }
2066         return genlmsg_reply(dump_ctx.skb, info);
2067 }
2068
2069 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2070                                             struct genl_info *info)
2071 {
2072         struct devlink *devlink = info->user_ptr[0];
2073         struct devlink_dpipe_table *table;
2074         const char *table_name;
2075
2076         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2077                 return -EINVAL;
2078
2079         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2080         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2081                                          table_name);
2082         if (!table)
2083                 return -EINVAL;
2084
2085         if (!table->table_ops->entries_dump)
2086                 return -EINVAL;
2087
2088         return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2089                                           0, table);
2090 }
2091
2092 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2093                                     const struct devlink_dpipe_header *header)
2094 {
2095         struct devlink_dpipe_field *field;
2096         struct nlattr *field_attr;
2097         int i;
2098
2099         for (i = 0; i < header->fields_count; i++) {
2100                 field = &header->fields[i];
2101                 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2102                 if (!field_attr)
2103                         return -EMSGSIZE;
2104                 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2105                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2106                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2107                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2108                         goto nla_put_failure;
2109                 nla_nest_end(skb, field_attr);
2110         }
2111         return 0;
2112
2113 nla_put_failure:
2114         nla_nest_cancel(skb, field_attr);
2115         return -EMSGSIZE;
2116 }
2117
2118 static int devlink_dpipe_header_put(struct sk_buff *skb,
2119                                     struct devlink_dpipe_header *header)
2120 {
2121         struct nlattr *fields_attr, *header_attr;
2122         int err;
2123
2124         header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2125         if (!header_attr)
2126                 return -EMSGSIZE;
2127
2128         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2129             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2130             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2131                 goto nla_put_failure;
2132
2133         fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2134         if (!fields_attr)
2135                 goto nla_put_failure;
2136
2137         err = devlink_dpipe_fields_put(skb, header);
2138         if (err) {
2139                 nla_nest_cancel(skb, fields_attr);
2140                 goto nla_put_failure;
2141         }
2142         nla_nest_end(skb, fields_attr);
2143         nla_nest_end(skb, header_attr);
2144         return 0;
2145
2146 nla_put_failure:
2147         err = -EMSGSIZE;
2148         nla_nest_cancel(skb, header_attr);
2149         return err;
2150 }
2151
2152 static int devlink_dpipe_headers_fill(struct genl_info *info,
2153                                       enum devlink_command cmd, int flags,
2154                                       struct devlink_dpipe_headers *
2155                                       dpipe_headers)
2156 {
2157         struct devlink *devlink = info->user_ptr[0];
2158         struct nlattr *headers_attr;
2159         struct sk_buff *skb = NULL;
2160         struct nlmsghdr *nlh;
2161         void *hdr;
2162         int i, j;
2163         int err;
2164
2165         i = 0;
2166 start_again:
2167         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2168         if (err)
2169                 return err;
2170
2171         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2172                           &devlink_nl_family, NLM_F_MULTI, cmd);
2173         if (!hdr) {
2174                 nlmsg_free(skb);
2175                 return -EMSGSIZE;
2176         }
2177
2178         if (devlink_nl_put_handle(skb, devlink))
2179                 goto nla_put_failure;
2180         headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2181         if (!headers_attr)
2182                 goto nla_put_failure;
2183
2184         j = 0;
2185         for (; i < dpipe_headers->headers_count; i++) {
2186                 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2187                 if (err) {
2188                         if (!j)
2189                                 goto err_table_put;
2190                         break;
2191                 }
2192                 j++;
2193         }
2194         nla_nest_end(skb, headers_attr);
2195         genlmsg_end(skb, hdr);
2196         if (i != dpipe_headers->headers_count)
2197                 goto start_again;
2198
2199 send_done:
2200         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2201                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2202         if (!nlh) {
2203                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2204                 if (err)
2205                         return err;
2206                 goto send_done;
2207         }
2208         return genlmsg_reply(skb, info);
2209
2210 nla_put_failure:
2211         err = -EMSGSIZE;
2212 err_table_put:
2213         genlmsg_cancel(skb, hdr);
2214         nlmsg_free(skb);
2215         return err;
2216 }
2217
2218 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2219                                             struct genl_info *info)
2220 {
2221         struct devlink *devlink = info->user_ptr[0];
2222
2223         if (!devlink->dpipe_headers)
2224                 return -EOPNOTSUPP;
2225         return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2226                                           0, devlink->dpipe_headers);
2227 }
2228
2229 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2230                                             const char *table_name,
2231                                             bool enable)
2232 {
2233         struct devlink_dpipe_table *table;
2234
2235         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2236                                          table_name);
2237         if (!table)
2238                 return -EINVAL;
2239
2240         if (table->counter_control_extern)
2241                 return -EOPNOTSUPP;
2242
2243         if (!(table->counters_enabled ^ enable))
2244                 return 0;
2245
2246         table->counters_enabled = enable;
2247         if (table->table_ops->counters_set_update)
2248                 table->table_ops->counters_set_update(table->priv, enable);
2249         return 0;
2250 }
2251
2252 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2253                                                    struct genl_info *info)
2254 {
2255         struct devlink *devlink = info->user_ptr[0];
2256         const char *table_name;
2257         bool counters_enable;
2258
2259         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2260             !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2261                 return -EINVAL;
2262
2263         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2264         counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2265
2266         return devlink_dpipe_table_counters_set(devlink, table_name,
2267                                                 counters_enable);
2268 }
2269
2270 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
2271         [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
2272         [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
2273         [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
2274         [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
2275         [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
2276         [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
2277         [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
2278         [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
2279         [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
2280         [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
2281         [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
2282         [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
2283         [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
2284         [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
2285         [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
2286         [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
2287         [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
2288 };
2289
2290 static const struct genl_ops devlink_nl_ops[] = {
2291         {
2292                 .cmd = DEVLINK_CMD_GET,
2293                 .doit = devlink_nl_cmd_get_doit,
2294                 .dumpit = devlink_nl_cmd_get_dumpit,
2295                 .policy = devlink_nl_policy,
2296                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2297                 /* can be retrieved by unprivileged users */
2298         },
2299         {
2300                 .cmd = DEVLINK_CMD_PORT_GET,
2301                 .doit = devlink_nl_cmd_port_get_doit,
2302                 .dumpit = devlink_nl_cmd_port_get_dumpit,
2303                 .policy = devlink_nl_policy,
2304                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2305                 /* can be retrieved by unprivileged users */
2306         },
2307         {
2308                 .cmd = DEVLINK_CMD_PORT_SET,
2309                 .doit = devlink_nl_cmd_port_set_doit,
2310                 .policy = devlink_nl_policy,
2311                 .flags = GENL_ADMIN_PERM,
2312                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2313         },
2314         {
2315                 .cmd = DEVLINK_CMD_PORT_SPLIT,
2316                 .doit = devlink_nl_cmd_port_split_doit,
2317                 .policy = devlink_nl_policy,
2318                 .flags = GENL_ADMIN_PERM,
2319                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2320         },
2321         {
2322                 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
2323                 .doit = devlink_nl_cmd_port_unsplit_doit,
2324                 .policy = devlink_nl_policy,
2325                 .flags = GENL_ADMIN_PERM,
2326                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2327         },
2328         {
2329                 .cmd = DEVLINK_CMD_SB_GET,
2330                 .doit = devlink_nl_cmd_sb_get_doit,
2331                 .dumpit = devlink_nl_cmd_sb_get_dumpit,
2332                 .policy = devlink_nl_policy,
2333                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2334                                   DEVLINK_NL_FLAG_NEED_SB,
2335                 /* can be retrieved by unprivileged users */
2336         },
2337         {
2338                 .cmd = DEVLINK_CMD_SB_POOL_GET,
2339                 .doit = devlink_nl_cmd_sb_pool_get_doit,
2340                 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
2341                 .policy = devlink_nl_policy,
2342                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2343                                   DEVLINK_NL_FLAG_NEED_SB,
2344                 /* can be retrieved by unprivileged users */
2345         },
2346         {
2347                 .cmd = DEVLINK_CMD_SB_POOL_SET,
2348                 .doit = devlink_nl_cmd_sb_pool_set_doit,
2349                 .policy = devlink_nl_policy,
2350                 .flags = GENL_ADMIN_PERM,
2351                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2352                                   DEVLINK_NL_FLAG_NEED_SB,
2353         },
2354         {
2355                 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
2356                 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
2357                 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
2358                 .policy = devlink_nl_policy,
2359                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2360                                   DEVLINK_NL_FLAG_NEED_SB,
2361                 /* can be retrieved by unprivileged users */
2362         },
2363         {
2364                 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
2365                 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
2366                 .policy = devlink_nl_policy,
2367                 .flags = GENL_ADMIN_PERM,
2368                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2369                                   DEVLINK_NL_FLAG_NEED_SB,
2370         },
2371         {
2372                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
2373                 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
2374                 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
2375                 .policy = devlink_nl_policy,
2376                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2377                                   DEVLINK_NL_FLAG_NEED_SB,
2378                 /* can be retrieved by unprivileged users */
2379         },
2380         {
2381                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
2382                 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
2383                 .policy = devlink_nl_policy,
2384                 .flags = GENL_ADMIN_PERM,
2385                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2386                                   DEVLINK_NL_FLAG_NEED_SB,
2387         },
2388         {
2389                 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
2390                 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
2391                 .policy = devlink_nl_policy,
2392                 .flags = GENL_ADMIN_PERM,
2393                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2394                                   DEVLINK_NL_FLAG_NEED_SB |
2395                                   DEVLINK_NL_FLAG_LOCK_PORTS,
2396         },
2397         {
2398                 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
2399                 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
2400                 .policy = devlink_nl_policy,
2401                 .flags = GENL_ADMIN_PERM,
2402                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2403                                   DEVLINK_NL_FLAG_NEED_SB |
2404                                   DEVLINK_NL_FLAG_LOCK_PORTS,
2405         },
2406         {
2407                 .cmd = DEVLINK_CMD_ESWITCH_GET,
2408                 .doit = devlink_nl_cmd_eswitch_get_doit,
2409                 .policy = devlink_nl_policy,
2410                 .flags = GENL_ADMIN_PERM,
2411                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2412         },
2413         {
2414                 .cmd = DEVLINK_CMD_ESWITCH_SET,
2415                 .doit = devlink_nl_cmd_eswitch_set_doit,
2416                 .policy = devlink_nl_policy,
2417                 .flags = GENL_ADMIN_PERM,
2418                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2419         },
2420         {
2421                 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
2422                 .doit = devlink_nl_cmd_dpipe_table_get,
2423                 .policy = devlink_nl_policy,
2424                 .flags = GENL_ADMIN_PERM,
2425                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2426         },
2427         {
2428                 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
2429                 .doit = devlink_nl_cmd_dpipe_entries_get,
2430                 .policy = devlink_nl_policy,
2431                 .flags = GENL_ADMIN_PERM,
2432                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2433         },
2434         {
2435                 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
2436                 .doit = devlink_nl_cmd_dpipe_headers_get,
2437                 .policy = devlink_nl_policy,
2438                 .flags = GENL_ADMIN_PERM,
2439                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2440         },
2441         {
2442                 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
2443                 .doit = devlink_nl_cmd_dpipe_table_counters_set,
2444                 .policy = devlink_nl_policy,
2445                 .flags = GENL_ADMIN_PERM,
2446                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2447         },
2448 };
2449
2450 static struct genl_family devlink_nl_family __ro_after_init = {
2451         .name           = DEVLINK_GENL_NAME,
2452         .version        = DEVLINK_GENL_VERSION,
2453         .maxattr        = DEVLINK_ATTR_MAX,
2454         .netnsok        = true,
2455         .pre_doit       = devlink_nl_pre_doit,
2456         .post_doit      = devlink_nl_post_doit,
2457         .module         = THIS_MODULE,
2458         .ops            = devlink_nl_ops,
2459         .n_ops          = ARRAY_SIZE(devlink_nl_ops),
2460         .mcgrps         = devlink_nl_mcgrps,
2461         .n_mcgrps       = ARRAY_SIZE(devlink_nl_mcgrps),
2462 };
2463
2464 /**
2465  *      devlink_alloc - Allocate new devlink instance resources
2466  *
2467  *      @ops: ops
2468  *      @priv_size: size of user private data
2469  *
2470  *      Allocate new devlink instance resources, including devlink index
2471  *      and name.
2472  */
2473 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
2474 {
2475         struct devlink *devlink;
2476
2477         devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
2478         if (!devlink)
2479                 return NULL;
2480         devlink->ops = ops;
2481         devlink_net_set(devlink, &init_net);
2482         INIT_LIST_HEAD(&devlink->port_list);
2483         INIT_LIST_HEAD(&devlink->sb_list);
2484         INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
2485         return devlink;
2486 }
2487 EXPORT_SYMBOL_GPL(devlink_alloc);
2488
2489 /**
2490  *      devlink_register - Register devlink instance
2491  *
2492  *      @devlink: devlink
2493  */
2494 int devlink_register(struct devlink *devlink, struct device *dev)
2495 {
2496         mutex_lock(&devlink_mutex);
2497         devlink->dev = dev;
2498         list_add_tail(&devlink->list, &devlink_list);
2499         devlink_notify(devlink, DEVLINK_CMD_NEW);
2500         mutex_unlock(&devlink_mutex);
2501         return 0;
2502 }
2503 EXPORT_SYMBOL_GPL(devlink_register);
2504
2505 /**
2506  *      devlink_unregister - Unregister devlink instance
2507  *
2508  *      @devlink: devlink
2509  */
2510 void devlink_unregister(struct devlink *devlink)
2511 {
2512         mutex_lock(&devlink_mutex);
2513         devlink_notify(devlink, DEVLINK_CMD_DEL);
2514         list_del(&devlink->list);
2515         mutex_unlock(&devlink_mutex);
2516 }
2517 EXPORT_SYMBOL_GPL(devlink_unregister);
2518
2519 /**
2520  *      devlink_free - Free devlink instance resources
2521  *
2522  *      @devlink: devlink
2523  */
2524 void devlink_free(struct devlink *devlink)
2525 {
2526         kfree(devlink);
2527 }
2528 EXPORT_SYMBOL_GPL(devlink_free);
2529
2530 /**
2531  *      devlink_port_register - Register devlink port
2532  *
2533  *      @devlink: devlink
2534  *      @devlink_port: devlink port
2535  *      @port_index
2536  *
2537  *      Register devlink port with provided port index. User can use
2538  *      any indexing, even hw-related one. devlink_port structure
2539  *      is convenient to be embedded inside user driver private structure.
2540  *      Note that the caller should take care of zeroing the devlink_port
2541  *      structure.
2542  */
2543 int devlink_port_register(struct devlink *devlink,
2544                           struct devlink_port *devlink_port,
2545                           unsigned int port_index)
2546 {
2547         mutex_lock(&devlink_port_mutex);
2548         if (devlink_port_index_exists(devlink, port_index)) {
2549                 mutex_unlock(&devlink_port_mutex);
2550                 return -EEXIST;
2551         }
2552         devlink_port->devlink = devlink;
2553         devlink_port->index = port_index;
2554         devlink_port->registered = true;
2555         list_add_tail(&devlink_port->list, &devlink->port_list);
2556         mutex_unlock(&devlink_port_mutex);
2557         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2558         return 0;
2559 }
2560 EXPORT_SYMBOL_GPL(devlink_port_register);
2561
2562 /**
2563  *      devlink_port_unregister - Unregister devlink port
2564  *
2565  *      @devlink_port: devlink port
2566  */
2567 void devlink_port_unregister(struct devlink_port *devlink_port)
2568 {
2569         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
2570         mutex_lock(&devlink_port_mutex);
2571         list_del(&devlink_port->list);
2572         mutex_unlock(&devlink_port_mutex);
2573 }
2574 EXPORT_SYMBOL_GPL(devlink_port_unregister);
2575
2576 static void __devlink_port_type_set(struct devlink_port *devlink_port,
2577                                     enum devlink_port_type type,
2578                                     void *type_dev)
2579 {
2580         devlink_port->type = type;
2581         devlink_port->type_dev = type_dev;
2582         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2583 }
2584
2585 /**
2586  *      devlink_port_type_eth_set - Set port type to Ethernet
2587  *
2588  *      @devlink_port: devlink port
2589  *      @netdev: related netdevice
2590  */
2591 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
2592                                struct net_device *netdev)
2593 {
2594         return __devlink_port_type_set(devlink_port,
2595                                        DEVLINK_PORT_TYPE_ETH, netdev);
2596 }
2597 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
2598
2599 /**
2600  *      devlink_port_type_ib_set - Set port type to InfiniBand
2601  *
2602  *      @devlink_port: devlink port
2603  *      @ibdev: related IB device
2604  */
2605 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
2606                               struct ib_device *ibdev)
2607 {
2608         return __devlink_port_type_set(devlink_port,
2609                                        DEVLINK_PORT_TYPE_IB, ibdev);
2610 }
2611 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
2612
2613 /**
2614  *      devlink_port_type_clear - Clear port type
2615  *
2616  *      @devlink_port: devlink port
2617  */
2618 void devlink_port_type_clear(struct devlink_port *devlink_port)
2619 {
2620         return __devlink_port_type_set(devlink_port,
2621                                        DEVLINK_PORT_TYPE_NOTSET, NULL);
2622 }
2623 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
2624
2625 /**
2626  *      devlink_port_split_set - Set port is split
2627  *
2628  *      @devlink_port: devlink port
2629  *      @split_group: split group - identifies group split port is part of
2630  */
2631 void devlink_port_split_set(struct devlink_port *devlink_port,
2632                             u32 split_group)
2633 {
2634         devlink_port->split = true;
2635         devlink_port->split_group = split_group;
2636         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2637 }
2638 EXPORT_SYMBOL_GPL(devlink_port_split_set);
2639
2640 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
2641                         u32 size, u16 ingress_pools_count,
2642                         u16 egress_pools_count, u16 ingress_tc_count,
2643                         u16 egress_tc_count)
2644 {
2645         struct devlink_sb *devlink_sb;
2646         int err = 0;
2647
2648         mutex_lock(&devlink_mutex);
2649         if (devlink_sb_index_exists(devlink, sb_index)) {
2650                 err = -EEXIST;
2651                 goto unlock;
2652         }
2653
2654         devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
2655         if (!devlink_sb) {
2656                 err = -ENOMEM;
2657                 goto unlock;
2658         }
2659         devlink_sb->index = sb_index;
2660         devlink_sb->size = size;
2661         devlink_sb->ingress_pools_count = ingress_pools_count;
2662         devlink_sb->egress_pools_count = egress_pools_count;
2663         devlink_sb->ingress_tc_count = ingress_tc_count;
2664         devlink_sb->egress_tc_count = egress_tc_count;
2665         list_add_tail(&devlink_sb->list, &devlink->sb_list);
2666 unlock:
2667         mutex_unlock(&devlink_mutex);
2668         return err;
2669 }
2670 EXPORT_SYMBOL_GPL(devlink_sb_register);
2671
2672 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
2673 {
2674         struct devlink_sb *devlink_sb;
2675
2676         mutex_lock(&devlink_mutex);
2677         devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
2678         WARN_ON(!devlink_sb);
2679         list_del(&devlink_sb->list);
2680         mutex_unlock(&devlink_mutex);
2681         kfree(devlink_sb);
2682 }
2683 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
2684
2685 /**
2686  *      devlink_dpipe_headers_register - register dpipe headers
2687  *
2688  *      @devlink: devlink
2689  *      @dpipe_headers: dpipe header array
2690  *
2691  *      Register the headers supported by hardware.
2692  */
2693 int devlink_dpipe_headers_register(struct devlink *devlink,
2694                                    struct devlink_dpipe_headers *dpipe_headers)
2695 {
2696         mutex_lock(&devlink_mutex);
2697         devlink->dpipe_headers = dpipe_headers;
2698         mutex_unlock(&devlink_mutex);
2699         return 0;
2700 }
2701 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
2702
2703 /**
2704  *      devlink_dpipe_headers_unregister - unregister dpipe headers
2705  *
2706  *      @devlink: devlink
2707  *
2708  *      Unregister the headers supported by hardware.
2709  */
2710 void devlink_dpipe_headers_unregister(struct devlink *devlink)
2711 {
2712         mutex_lock(&devlink_mutex);
2713         devlink->dpipe_headers = NULL;
2714         mutex_unlock(&devlink_mutex);
2715 }
2716 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
2717
2718 /**
2719  *      devlink_dpipe_table_counter_enabled - check if counter allocation
2720  *                                            required
2721  *      @devlink: devlink
2722  *      @table_name: tables name
2723  *
2724  *      Used by driver to check if counter allocation is required.
2725  *      After counter allocation is turned on the table entries
2726  *      are updated to include counter statistics.
2727  *
2728  *      After that point on the driver must respect the counter
2729  *      state so that each entry added to the table is added
2730  *      with a counter.
2731  */
2732 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
2733                                          const char *table_name)
2734 {
2735         struct devlink_dpipe_table *table;
2736         bool enabled;
2737
2738         rcu_read_lock();
2739         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2740                                          table_name);
2741         enabled = false;
2742         if (table)
2743                 enabled = table->counters_enabled;
2744         rcu_read_unlock();
2745         return enabled;
2746 }
2747 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
2748
2749 /**
2750  *      devlink_dpipe_table_register - register dpipe table
2751  *
2752  *      @devlink: devlink
2753  *      @table_name: table name
2754  *      @table_ops: table ops
2755  *      @priv: priv
2756  *      @counter_control_extern: external control for counters
2757  */
2758 int devlink_dpipe_table_register(struct devlink *devlink,
2759                                  const char *table_name,
2760                                  struct devlink_dpipe_table_ops *table_ops,
2761                                  void *priv, bool counter_control_extern)
2762 {
2763         struct devlink_dpipe_table *table;
2764
2765         if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
2766                 return -EEXIST;
2767
2768         if (WARN_ON(!table_ops->size_get))
2769                 return -EINVAL;
2770
2771         table = kzalloc(sizeof(*table), GFP_KERNEL);
2772         if (!table)
2773                 return -ENOMEM;
2774
2775         table->name = table_name;
2776         table->table_ops = table_ops;
2777         table->priv = priv;
2778         table->counter_control_extern = counter_control_extern;
2779
2780         mutex_lock(&devlink_mutex);
2781         list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
2782         mutex_unlock(&devlink_mutex);
2783         return 0;
2784 }
2785 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
2786
2787 /**
2788  *      devlink_dpipe_table_unregister - unregister dpipe table
2789  *
2790  *      @devlink: devlink
2791  *      @table_name: table name
2792  */
2793 void devlink_dpipe_table_unregister(struct devlink *devlink,
2794                                     const char *table_name)
2795 {
2796         struct devlink_dpipe_table *table;
2797
2798         mutex_lock(&devlink_mutex);
2799         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2800                                          table_name);
2801         if (!table)
2802                 goto unlock;
2803         list_del_rcu(&table->list);
2804         mutex_unlock(&devlink_mutex);
2805         kfree_rcu(table, rcu);
2806         return;
2807 unlock:
2808         mutex_unlock(&devlink_mutex);
2809 }
2810 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
2811
2812 static int __init devlink_module_init(void)
2813 {
2814         return genl_register_family(&devlink_nl_family);
2815 }
2816
2817 static void __exit devlink_module_exit(void)
2818 {
2819         genl_unregister_family(&devlink_nl_family);
2820 }
2821
2822 module_init(devlink_module_init);
2823 module_exit(devlink_module_exit);
2824
2825 MODULE_LICENSE("GPL v2");
2826 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2827 MODULE_DESCRIPTION("Network physical device Netlink interface");
2828 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);