blob: 6c92ddd2465da9c3dd8fb52091b8d7f8076e465c [file] [log] [blame]
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001/*
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>
Jiri Pirkoe5224f02016-07-12 18:05:03 +020029#define CREATE_TRACE_POINTS
30#include <trace/events/devlink.h>
31
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020032static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
33 {
David Ahern12bdc5e2017-08-30 17:07:30 -070034 .name = "destination mac",
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020035 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
36 .bitwidth = 48,
37 },
38};
39
40struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
41 .name = "ethernet",
42 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
43 .fields = devlink_dpipe_fields_ethernet,
44 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
45 .global = true,
46};
47EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
48
Arkadi Sharshevsky3fb886e2017-08-24 08:40:00 +020049static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
50 {
51 .name = "destination ip",
52 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
53 .bitwidth = 32,
54 },
55};
56
57struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
58 .name = "ipv4",
59 .id = DEVLINK_DPIPE_HEADER_IPV4,
60 .fields = devlink_dpipe_fields_ipv4,
61 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
62 .global = true,
63};
64EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
65
Arkadi Sharshevsky1797f5b2017-08-31 17:59:12 +020066static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
67 {
68 .name = "destination ip",
69 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
70 .bitwidth = 128,
71 },
72};
73
74struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
75 .name = "ipv6",
76 .id = DEVLINK_DPIPE_HEADER_IPV6,
77 .fields = devlink_dpipe_fields_ipv6,
78 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
79 .global = true,
80};
81EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
82
Jiri Pirkoe5224f02016-07-12 18:05:03 +020083EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010084
85static 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 */
93static DEFINE_MUTEX(devlink_mutex);
94
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010095static struct net *devlink_net(const struct devlink *devlink)
96{
97 return read_pnet(&devlink->_net);
98}
99
100static void devlink_net_set(struct devlink *devlink, struct net *net)
101{
102 write_pnet(&devlink->_net, net);
103}
104
105static struct devlink *devlink_get_from_attrs(struct net *net,
106 struct nlattr **attrs)
107{
108 struct devlink *devlink;
109 char *busname;
110 char *devname;
111
112 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
113 return ERR_PTR(-EINVAL);
114
115 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
116 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
117
118 list_for_each_entry(devlink, &devlink_list, list) {
119 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
120 strcmp(dev_name(devlink->dev), devname) == 0 &&
121 net_eq(devlink_net(devlink), net))
122 return devlink;
123 }
124
125 return ERR_PTR(-ENODEV);
126}
127
128static struct devlink *devlink_get_from_info(struct genl_info *info)
129{
130 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
131}
132
133static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
134 int port_index)
135{
136 struct devlink_port *devlink_port;
137
138 list_for_each_entry(devlink_port, &devlink->port_list, list) {
139 if (devlink_port->index == port_index)
140 return devlink_port;
141 }
142 return NULL;
143}
144
145static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
146{
147 return devlink_port_get_by_index(devlink, port_index);
148}
149
150static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
151 struct nlattr **attrs)
152{
153 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
154 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
155 struct devlink_port *devlink_port;
156
157 devlink_port = devlink_port_get_by_index(devlink, port_index);
158 if (!devlink_port)
159 return ERR_PTR(-ENODEV);
160 return devlink_port;
161 }
162 return ERR_PTR(-EINVAL);
163}
164
165static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
166 struct genl_info *info)
167{
168 return devlink_port_get_from_attrs(devlink, info->attrs);
169}
170
Jiri Pirkobf797472016-04-14 18:19:13 +0200171struct devlink_sb {
172 struct list_head list;
173 unsigned int index;
174 u32 size;
175 u16 ingress_pools_count;
176 u16 egress_pools_count;
177 u16 ingress_tc_count;
178 u16 egress_tc_count;
179};
180
181static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
182{
183 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
184}
185
186static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
187 unsigned int sb_index)
188{
189 struct devlink_sb *devlink_sb;
190
191 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
192 if (devlink_sb->index == sb_index)
193 return devlink_sb;
194 }
195 return NULL;
196}
197
198static bool devlink_sb_index_exists(struct devlink *devlink,
199 unsigned int sb_index)
200{
201 return devlink_sb_get_by_index(devlink, sb_index);
202}
203
204static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
205 struct nlattr **attrs)
206{
207 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
208 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
209 struct devlink_sb *devlink_sb;
210
211 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
212 if (!devlink_sb)
213 return ERR_PTR(-ENODEV);
214 return devlink_sb;
215 }
216 return ERR_PTR(-EINVAL);
217}
218
219static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
220 struct genl_info *info)
221{
222 return devlink_sb_get_from_attrs(devlink, info->attrs);
223}
224
225static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
226 struct nlattr **attrs,
227 u16 *p_pool_index)
228{
229 u16 val;
230
231 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
232 return -EINVAL;
233
234 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
235 if (val >= devlink_sb_pool_count(devlink_sb))
236 return -EINVAL;
237 *p_pool_index = val;
238 return 0;
239}
240
241static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
242 struct genl_info *info,
243 u16 *p_pool_index)
244{
245 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
246 p_pool_index);
247}
248
249static int
250devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
251 enum devlink_sb_pool_type *p_pool_type)
252{
253 u8 val;
254
255 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
256 return -EINVAL;
257
258 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
259 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
260 val != DEVLINK_SB_POOL_TYPE_EGRESS)
261 return -EINVAL;
262 *p_pool_type = val;
263 return 0;
264}
265
266static int
267devlink_sb_pool_type_get_from_info(struct genl_info *info,
268 enum devlink_sb_pool_type *p_pool_type)
269{
270 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
271}
272
273static int
274devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
275 enum devlink_sb_threshold_type *p_th_type)
276{
277 u8 val;
278
279 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
280 return -EINVAL;
281
282 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
283 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
284 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
285 return -EINVAL;
286 *p_th_type = val;
287 return 0;
288}
289
290static int
291devlink_sb_th_type_get_from_info(struct genl_info *info,
292 enum devlink_sb_threshold_type *p_th_type)
293{
294 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
295}
296
297static int
298devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
299 struct nlattr **attrs,
300 enum devlink_sb_pool_type pool_type,
301 u16 *p_tc_index)
302{
303 u16 val;
304
305 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
306 return -EINVAL;
307
308 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
309 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
310 val >= devlink_sb->ingress_tc_count)
311 return -EINVAL;
312 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
313 val >= devlink_sb->egress_tc_count)
314 return -EINVAL;
315 *p_tc_index = val;
316 return 0;
317}
318
319static int
320devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
321 struct genl_info *info,
322 enum devlink_sb_pool_type pool_type,
323 u16 *p_tc_index)
324{
325 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
326 pool_type, p_tc_index);
327}
328
Alex Veskerb16ebe92018-07-12 15:13:08 +0300329struct devlink_region {
330 struct devlink *devlink;
331 struct list_head list;
332 const char *name;
333 struct list_head snapshot_list;
334 u32 max_snapshots;
335 u32 cur_snapshots;
336 u64 size;
337};
338
339static struct devlink_region *
340devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
341{
342 struct devlink_region *region;
343
344 list_for_each_entry(region, &devlink->region_list, list)
345 if (!strcmp(region->name, region_name))
346 return region;
347
348 return NULL;
349}
350
Jiri Pirko1fc22572016-04-08 19:12:48 +0200351#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
352#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200353#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100354
355/* The per devlink instance lock is taken by default in the pre-doit
356 * operation, yet several commands do not require this. The global
357 * devlink lock is taken and protects from disruption by user-calls.
358 */
359#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100360
361static int devlink_nl_pre_doit(const struct genl_ops *ops,
362 struct sk_buff *skb, struct genl_info *info)
363{
364 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100365 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100366
367 mutex_lock(&devlink_mutex);
368 devlink = devlink_get_from_info(info);
369 if (IS_ERR(devlink)) {
370 mutex_unlock(&devlink_mutex);
371 return PTR_ERR(devlink);
372 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100373 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
374 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200375 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
376 info->user_ptr[0] = devlink;
377 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100378 struct devlink_port *devlink_port;
379
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100380 devlink_port = devlink_port_get_from_info(devlink, info);
381 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100382 err = PTR_ERR(devlink_port);
383 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100384 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200385 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100386 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200387 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
388 struct devlink_sb *devlink_sb;
389
390 devlink_sb = devlink_sb_get_from_info(devlink, info);
391 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100392 err = PTR_ERR(devlink_sb);
393 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200394 }
395 info->user_ptr[1] = devlink_sb;
396 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100397 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100398
399unlock:
400 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
401 mutex_unlock(&devlink->lock);
402 mutex_unlock(&devlink_mutex);
403 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100404}
405
406static void devlink_nl_post_doit(const struct genl_ops *ops,
407 struct sk_buff *skb, struct genl_info *info)
408{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100409 struct devlink *devlink;
410
411 devlink = devlink_get_from_info(info);
412 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
413 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100414 mutex_unlock(&devlink_mutex);
415}
416
Johannes Berg489111e2016-10-24 14:40:03 +0200417static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100418
419enum devlink_multicast_groups {
420 DEVLINK_MCGRP_CONFIG,
421};
422
423static const struct genl_multicast_group devlink_nl_mcgrps[] = {
424 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
425};
426
427static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
428{
429 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
430 return -EMSGSIZE;
431 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
432 return -EMSGSIZE;
433 return 0;
434}
435
436static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
437 enum devlink_command cmd, u32 portid,
438 u32 seq, int flags)
439{
440 void *hdr;
441
442 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
443 if (!hdr)
444 return -EMSGSIZE;
445
446 if (devlink_nl_put_handle(msg, devlink))
447 goto nla_put_failure;
448
449 genlmsg_end(msg, hdr);
450 return 0;
451
452nla_put_failure:
453 genlmsg_cancel(msg, hdr);
454 return -EMSGSIZE;
455}
456
457static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
458{
459 struct sk_buff *msg;
460 int err;
461
462 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
463
464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
465 if (!msg)
466 return;
467
468 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
469 if (err) {
470 nlmsg_free(msg);
471 return;
472 }
473
474 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
475 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
476}
477
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200478static int devlink_nl_port_attrs_put(struct sk_buff *msg,
479 struct devlink_port *devlink_port)
480{
481 struct devlink_port_attrs *attrs = &devlink_port->attrs;
482
483 if (!attrs->set)
484 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200485 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
486 return -EMSGSIZE;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200487 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
488 return -EMSGSIZE;
489 if (!attrs->split)
490 return 0;
491 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
492 return -EMSGSIZE;
493 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
494 attrs->split_subport_number))
495 return -EMSGSIZE;
496 return 0;
497}
498
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100499static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
500 struct devlink_port *devlink_port,
501 enum devlink_command cmd, u32 portid,
502 u32 seq, int flags)
503{
504 void *hdr;
505
506 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
507 if (!hdr)
508 return -EMSGSIZE;
509
510 if (devlink_nl_put_handle(msg, devlink))
511 goto nla_put_failure;
512 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
513 goto nla_put_failure;
514 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
515 goto nla_put_failure;
516 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
517 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
518 devlink_port->desired_type))
519 goto nla_put_failure;
520 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
521 struct net_device *netdev = devlink_port->type_dev;
522
523 if (netdev &&
524 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
525 netdev->ifindex) ||
526 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
527 netdev->name)))
528 goto nla_put_failure;
529 }
530 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
531 struct ib_device *ibdev = devlink_port->type_dev;
532
533 if (ibdev &&
534 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
535 ibdev->name))
536 goto nla_put_failure;
537 }
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200538 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100539 goto nla_put_failure;
540
541 genlmsg_end(msg, hdr);
542 return 0;
543
544nla_put_failure:
545 genlmsg_cancel(msg, hdr);
546 return -EMSGSIZE;
547}
548
549static void devlink_port_notify(struct devlink_port *devlink_port,
550 enum devlink_command cmd)
551{
552 struct devlink *devlink = devlink_port->devlink;
553 struct sk_buff *msg;
554 int err;
555
556 if (!devlink_port->registered)
557 return;
558
559 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
560
561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
562 if (!msg)
563 return;
564
565 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
566 if (err) {
567 nlmsg_free(msg);
568 return;
569 }
570
571 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
572 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
573}
574
575static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
576{
577 struct devlink *devlink = info->user_ptr[0];
578 struct sk_buff *msg;
579 int err;
580
581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
582 if (!msg)
583 return -ENOMEM;
584
585 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
586 info->snd_portid, info->snd_seq, 0);
587 if (err) {
588 nlmsg_free(msg);
589 return err;
590 }
591
592 return genlmsg_reply(msg, info);
593}
594
595static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
596 struct netlink_callback *cb)
597{
598 struct devlink *devlink;
599 int start = cb->args[0];
600 int idx = 0;
601 int err;
602
603 mutex_lock(&devlink_mutex);
604 list_for_each_entry(devlink, &devlink_list, list) {
605 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
606 continue;
607 if (idx < start) {
608 idx++;
609 continue;
610 }
611 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
612 NETLINK_CB(cb->skb).portid,
613 cb->nlh->nlmsg_seq, NLM_F_MULTI);
614 if (err)
615 goto out;
616 idx++;
617 }
618out:
619 mutex_unlock(&devlink_mutex);
620
621 cb->args[0] = idx;
622 return msg->len;
623}
624
625static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
626 struct genl_info *info)
627{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200628 struct devlink_port *devlink_port = info->user_ptr[0];
629 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100630 struct sk_buff *msg;
631 int err;
632
633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
634 if (!msg)
635 return -ENOMEM;
636
637 err = devlink_nl_port_fill(msg, devlink, devlink_port,
638 DEVLINK_CMD_PORT_NEW,
639 info->snd_portid, info->snd_seq, 0);
640 if (err) {
641 nlmsg_free(msg);
642 return err;
643 }
644
645 return genlmsg_reply(msg, info);
646}
647
648static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
649 struct netlink_callback *cb)
650{
651 struct devlink *devlink;
652 struct devlink_port *devlink_port;
653 int start = cb->args[0];
654 int idx = 0;
655 int err;
656
657 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100658 list_for_each_entry(devlink, &devlink_list, list) {
659 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
660 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100661 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100662 list_for_each_entry(devlink_port, &devlink->port_list, list) {
663 if (idx < start) {
664 idx++;
665 continue;
666 }
667 err = devlink_nl_port_fill(msg, devlink, devlink_port,
668 DEVLINK_CMD_NEW,
669 NETLINK_CB(cb->skb).portid,
670 cb->nlh->nlmsg_seq,
671 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100672 if (err) {
673 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100674 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100675 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100676 idx++;
677 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100678 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100679 }
680out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100681 mutex_unlock(&devlink_mutex);
682
683 cb->args[0] = idx;
684 return msg->len;
685}
686
687static int devlink_port_type_set(struct devlink *devlink,
688 struct devlink_port *devlink_port,
689 enum devlink_port_type port_type)
690
691{
692 int err;
693
694 if (devlink->ops && devlink->ops->port_type_set) {
695 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
696 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200697 if (port_type == devlink_port->type)
698 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100699 err = devlink->ops->port_type_set(devlink_port, port_type);
700 if (err)
701 return err;
702 devlink_port->desired_type = port_type;
703 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
704 return 0;
705 }
706 return -EOPNOTSUPP;
707}
708
709static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
710 struct genl_info *info)
711{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200712 struct devlink_port *devlink_port = info->user_ptr[0];
713 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100714 int err;
715
716 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
717 enum devlink_port_type port_type;
718
719 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
720 err = devlink_port_type_set(devlink, devlink_port, port_type);
721 if (err)
722 return err;
723 }
724 return 0;
725}
726
David Ahernac0fc8a2018-06-05 08:14:09 -0700727static int devlink_port_split(struct devlink *devlink, u32 port_index,
728 u32 count, struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100729
730{
731 if (devlink->ops && devlink->ops->port_split)
David Ahernac0fc8a2018-06-05 08:14:09 -0700732 return devlink->ops->port_split(devlink, port_index, count,
733 extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100734 return -EOPNOTSUPP;
735}
736
737static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
738 struct genl_info *info)
739{
740 struct devlink *devlink = info->user_ptr[0];
741 u32 port_index;
742 u32 count;
743
744 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
745 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
746 return -EINVAL;
747
748 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
749 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700750 return devlink_port_split(devlink, port_index, count, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100751}
752
David Ahernac0fc8a2018-06-05 08:14:09 -0700753static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
754 struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100755
756{
757 if (devlink->ops && devlink->ops->port_unsplit)
David Ahernac0fc8a2018-06-05 08:14:09 -0700758 return devlink->ops->port_unsplit(devlink, port_index, extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100759 return -EOPNOTSUPP;
760}
761
762static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
763 struct genl_info *info)
764{
765 struct devlink *devlink = info->user_ptr[0];
766 u32 port_index;
767
768 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
769 return -EINVAL;
770
771 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700772 return devlink_port_unsplit(devlink, port_index, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100773}
774
Jiri Pirkobf797472016-04-14 18:19:13 +0200775static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
776 struct devlink_sb *devlink_sb,
777 enum devlink_command cmd, u32 portid,
778 u32 seq, int flags)
779{
780 void *hdr;
781
782 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
783 if (!hdr)
784 return -EMSGSIZE;
785
786 if (devlink_nl_put_handle(msg, devlink))
787 goto nla_put_failure;
788 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
789 goto nla_put_failure;
790 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
791 goto nla_put_failure;
792 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
793 devlink_sb->ingress_pools_count))
794 goto nla_put_failure;
795 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
796 devlink_sb->egress_pools_count))
797 goto nla_put_failure;
798 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
799 devlink_sb->ingress_tc_count))
800 goto nla_put_failure;
801 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
802 devlink_sb->egress_tc_count))
803 goto nla_put_failure;
804
805 genlmsg_end(msg, hdr);
806 return 0;
807
808nla_put_failure:
809 genlmsg_cancel(msg, hdr);
810 return -EMSGSIZE;
811}
812
813static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
814 struct genl_info *info)
815{
816 struct devlink *devlink = info->user_ptr[0];
817 struct devlink_sb *devlink_sb = info->user_ptr[1];
818 struct sk_buff *msg;
819 int err;
820
821 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
822 if (!msg)
823 return -ENOMEM;
824
825 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
826 DEVLINK_CMD_SB_NEW,
827 info->snd_portid, info->snd_seq, 0);
828 if (err) {
829 nlmsg_free(msg);
830 return err;
831 }
832
833 return genlmsg_reply(msg, info);
834}
835
836static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
837 struct netlink_callback *cb)
838{
839 struct devlink *devlink;
840 struct devlink_sb *devlink_sb;
841 int start = cb->args[0];
842 int idx = 0;
843 int err;
844
845 mutex_lock(&devlink_mutex);
846 list_for_each_entry(devlink, &devlink_list, list) {
847 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
848 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100849 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200850 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
851 if (idx < start) {
852 idx++;
853 continue;
854 }
855 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
856 DEVLINK_CMD_SB_NEW,
857 NETLINK_CB(cb->skb).portid,
858 cb->nlh->nlmsg_seq,
859 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100860 if (err) {
861 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200862 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100863 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200864 idx++;
865 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100866 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200867 }
868out:
869 mutex_unlock(&devlink_mutex);
870
871 cb->args[0] = idx;
872 return msg->len;
873}
874
875static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
876 struct devlink_sb *devlink_sb,
877 u16 pool_index, enum devlink_command cmd,
878 u32 portid, u32 seq, int flags)
879{
880 struct devlink_sb_pool_info pool_info;
881 void *hdr;
882 int err;
883
884 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
885 pool_index, &pool_info);
886 if (err)
887 return err;
888
889 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
890 if (!hdr)
891 return -EMSGSIZE;
892
893 if (devlink_nl_put_handle(msg, devlink))
894 goto nla_put_failure;
895 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
896 goto nla_put_failure;
897 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
898 goto nla_put_failure;
899 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
900 goto nla_put_failure;
901 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
902 goto nla_put_failure;
903 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
904 pool_info.threshold_type))
905 goto nla_put_failure;
906
907 genlmsg_end(msg, hdr);
908 return 0;
909
910nla_put_failure:
911 genlmsg_cancel(msg, hdr);
912 return -EMSGSIZE;
913}
914
915static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
916 struct genl_info *info)
917{
918 struct devlink *devlink = info->user_ptr[0];
919 struct devlink_sb *devlink_sb = info->user_ptr[1];
920 struct sk_buff *msg;
921 u16 pool_index;
922 int err;
923
924 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
925 &pool_index);
926 if (err)
927 return err;
928
929 if (!devlink->ops || !devlink->ops->sb_pool_get)
930 return -EOPNOTSUPP;
931
932 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
933 if (!msg)
934 return -ENOMEM;
935
936 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
937 DEVLINK_CMD_SB_POOL_NEW,
938 info->snd_portid, info->snd_seq, 0);
939 if (err) {
940 nlmsg_free(msg);
941 return err;
942 }
943
944 return genlmsg_reply(msg, info);
945}
946
947static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
948 struct devlink *devlink,
949 struct devlink_sb *devlink_sb,
950 u32 portid, u32 seq)
951{
952 u16 pool_count = devlink_sb_pool_count(devlink_sb);
953 u16 pool_index;
954 int err;
955
956 for (pool_index = 0; pool_index < pool_count; pool_index++) {
957 if (*p_idx < start) {
958 (*p_idx)++;
959 continue;
960 }
961 err = devlink_nl_sb_pool_fill(msg, devlink,
962 devlink_sb,
963 pool_index,
964 DEVLINK_CMD_SB_POOL_NEW,
965 portid, seq, NLM_F_MULTI);
966 if (err)
967 return err;
968 (*p_idx)++;
969 }
970 return 0;
971}
972
973static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
974 struct netlink_callback *cb)
975{
976 struct devlink *devlink;
977 struct devlink_sb *devlink_sb;
978 int start = cb->args[0];
979 int idx = 0;
980 int err;
981
982 mutex_lock(&devlink_mutex);
983 list_for_each_entry(devlink, &devlink_list, list) {
984 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
985 !devlink->ops || !devlink->ops->sb_pool_get)
986 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100987 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200988 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
989 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
990 devlink_sb,
991 NETLINK_CB(cb->skb).portid,
992 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100993 if (err && err != -EOPNOTSUPP) {
994 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200995 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100996 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200997 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100998 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200999 }
1000out:
1001 mutex_unlock(&devlink_mutex);
1002
1003 cb->args[0] = idx;
1004 return msg->len;
1005}
1006
1007static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1008 u16 pool_index, u32 size,
1009 enum devlink_sb_threshold_type threshold_type)
1010
1011{
1012 const struct devlink_ops *ops = devlink->ops;
1013
1014 if (ops && ops->sb_pool_set)
1015 return ops->sb_pool_set(devlink, sb_index, pool_index,
1016 size, threshold_type);
1017 return -EOPNOTSUPP;
1018}
1019
1020static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1021 struct genl_info *info)
1022{
1023 struct devlink *devlink = info->user_ptr[0];
1024 struct devlink_sb *devlink_sb = info->user_ptr[1];
1025 enum devlink_sb_threshold_type threshold_type;
1026 u16 pool_index;
1027 u32 size;
1028 int err;
1029
1030 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1031 &pool_index);
1032 if (err)
1033 return err;
1034
1035 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1036 if (err)
1037 return err;
1038
1039 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1040 return -EINVAL;
1041
1042 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1043 return devlink_sb_pool_set(devlink, devlink_sb->index,
1044 pool_index, size, threshold_type);
1045}
1046
1047static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1048 struct devlink *devlink,
1049 struct devlink_port *devlink_port,
1050 struct devlink_sb *devlink_sb,
1051 u16 pool_index,
1052 enum devlink_command cmd,
1053 u32 portid, u32 seq, int flags)
1054{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001055 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001056 u32 threshold;
1057 void *hdr;
1058 int err;
1059
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001060 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1061 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001062 if (err)
1063 return err;
1064
1065 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1066 if (!hdr)
1067 return -EMSGSIZE;
1068
1069 if (devlink_nl_put_handle(msg, devlink))
1070 goto nla_put_failure;
1071 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1072 goto nla_put_failure;
1073 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1074 goto nla_put_failure;
1075 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1076 goto nla_put_failure;
1077 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1078 goto nla_put_failure;
1079
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001080 if (ops->sb_occ_port_pool_get) {
1081 u32 cur;
1082 u32 max;
1083
1084 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1085 pool_index, &cur, &max);
1086 if (err && err != -EOPNOTSUPP)
1087 return err;
1088 if (!err) {
1089 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1090 goto nla_put_failure;
1091 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1092 goto nla_put_failure;
1093 }
1094 }
1095
Jiri Pirkobf797472016-04-14 18:19:13 +02001096 genlmsg_end(msg, hdr);
1097 return 0;
1098
1099nla_put_failure:
1100 genlmsg_cancel(msg, hdr);
1101 return -EMSGSIZE;
1102}
1103
1104static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1105 struct genl_info *info)
1106{
1107 struct devlink_port *devlink_port = info->user_ptr[0];
1108 struct devlink *devlink = devlink_port->devlink;
1109 struct devlink_sb *devlink_sb = info->user_ptr[1];
1110 struct sk_buff *msg;
1111 u16 pool_index;
1112 int err;
1113
1114 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1115 &pool_index);
1116 if (err)
1117 return err;
1118
1119 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1120 return -EOPNOTSUPP;
1121
1122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1123 if (!msg)
1124 return -ENOMEM;
1125
1126 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1127 devlink_sb, pool_index,
1128 DEVLINK_CMD_SB_PORT_POOL_NEW,
1129 info->snd_portid, info->snd_seq, 0);
1130 if (err) {
1131 nlmsg_free(msg);
1132 return err;
1133 }
1134
1135 return genlmsg_reply(msg, info);
1136}
1137
1138static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1139 struct devlink *devlink,
1140 struct devlink_sb *devlink_sb,
1141 u32 portid, u32 seq)
1142{
1143 struct devlink_port *devlink_port;
1144 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1145 u16 pool_index;
1146 int err;
1147
1148 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1149 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1150 if (*p_idx < start) {
1151 (*p_idx)++;
1152 continue;
1153 }
1154 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1155 devlink_port,
1156 devlink_sb,
1157 pool_index,
1158 DEVLINK_CMD_SB_PORT_POOL_NEW,
1159 portid, seq,
1160 NLM_F_MULTI);
1161 if (err)
1162 return err;
1163 (*p_idx)++;
1164 }
1165 }
1166 return 0;
1167}
1168
1169static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1170 struct netlink_callback *cb)
1171{
1172 struct devlink *devlink;
1173 struct devlink_sb *devlink_sb;
1174 int start = cb->args[0];
1175 int idx = 0;
1176 int err;
1177
1178 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001179 list_for_each_entry(devlink, &devlink_list, list) {
1180 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1181 !devlink->ops || !devlink->ops->sb_port_pool_get)
1182 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001183 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001184 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1185 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1186 devlink, devlink_sb,
1187 NETLINK_CB(cb->skb).portid,
1188 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001189 if (err && err != -EOPNOTSUPP) {
1190 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001191 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001192 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001193 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001194 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001195 }
1196out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001197 mutex_unlock(&devlink_mutex);
1198
1199 cb->args[0] = idx;
1200 return msg->len;
1201}
1202
1203static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1204 unsigned int sb_index, u16 pool_index,
1205 u32 threshold)
1206
1207{
1208 const struct devlink_ops *ops = devlink_port->devlink->ops;
1209
1210 if (ops && ops->sb_port_pool_set)
1211 return ops->sb_port_pool_set(devlink_port, sb_index,
1212 pool_index, threshold);
1213 return -EOPNOTSUPP;
1214}
1215
1216static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1217 struct genl_info *info)
1218{
1219 struct devlink_port *devlink_port = info->user_ptr[0];
1220 struct devlink_sb *devlink_sb = info->user_ptr[1];
1221 u16 pool_index;
1222 u32 threshold;
1223 int err;
1224
1225 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1226 &pool_index);
1227 if (err)
1228 return err;
1229
1230 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1231 return -EINVAL;
1232
1233 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1234 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1235 pool_index, threshold);
1236}
1237
1238static int
1239devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1240 struct devlink_port *devlink_port,
1241 struct devlink_sb *devlink_sb, u16 tc_index,
1242 enum devlink_sb_pool_type pool_type,
1243 enum devlink_command cmd,
1244 u32 portid, u32 seq, int flags)
1245{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001246 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001247 u16 pool_index;
1248 u32 threshold;
1249 void *hdr;
1250 int err;
1251
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001252 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1253 tc_index, pool_type,
1254 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001255 if (err)
1256 return err;
1257
1258 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1259 if (!hdr)
1260 return -EMSGSIZE;
1261
1262 if (devlink_nl_put_handle(msg, devlink))
1263 goto nla_put_failure;
1264 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1265 goto nla_put_failure;
1266 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1267 goto nla_put_failure;
1268 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1269 goto nla_put_failure;
1270 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1271 goto nla_put_failure;
1272 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1273 goto nla_put_failure;
1274 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1275 goto nla_put_failure;
1276
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001277 if (ops->sb_occ_tc_port_bind_get) {
1278 u32 cur;
1279 u32 max;
1280
1281 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1282 devlink_sb->index,
1283 tc_index, pool_type,
1284 &cur, &max);
1285 if (err && err != -EOPNOTSUPP)
1286 return err;
1287 if (!err) {
1288 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1289 goto nla_put_failure;
1290 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1291 goto nla_put_failure;
1292 }
1293 }
1294
Jiri Pirkobf797472016-04-14 18:19:13 +02001295 genlmsg_end(msg, hdr);
1296 return 0;
1297
1298nla_put_failure:
1299 genlmsg_cancel(msg, hdr);
1300 return -EMSGSIZE;
1301}
1302
1303static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1304 struct genl_info *info)
1305{
1306 struct devlink_port *devlink_port = info->user_ptr[0];
1307 struct devlink *devlink = devlink_port->devlink;
1308 struct devlink_sb *devlink_sb = info->user_ptr[1];
1309 struct sk_buff *msg;
1310 enum devlink_sb_pool_type pool_type;
1311 u16 tc_index;
1312 int err;
1313
1314 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1315 if (err)
1316 return err;
1317
1318 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1319 pool_type, &tc_index);
1320 if (err)
1321 return err;
1322
1323 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1324 return -EOPNOTSUPP;
1325
1326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1327 if (!msg)
1328 return -ENOMEM;
1329
1330 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1331 devlink_sb, tc_index, pool_type,
1332 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1333 info->snd_portid,
1334 info->snd_seq, 0);
1335 if (err) {
1336 nlmsg_free(msg);
1337 return err;
1338 }
1339
1340 return genlmsg_reply(msg, info);
1341}
1342
1343static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1344 int start, int *p_idx,
1345 struct devlink *devlink,
1346 struct devlink_sb *devlink_sb,
1347 u32 portid, u32 seq)
1348{
1349 struct devlink_port *devlink_port;
1350 u16 tc_index;
1351 int err;
1352
1353 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1354 for (tc_index = 0;
1355 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1356 if (*p_idx < start) {
1357 (*p_idx)++;
1358 continue;
1359 }
1360 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1361 devlink_port,
1362 devlink_sb,
1363 tc_index,
1364 DEVLINK_SB_POOL_TYPE_INGRESS,
1365 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1366 portid, seq,
1367 NLM_F_MULTI);
1368 if (err)
1369 return err;
1370 (*p_idx)++;
1371 }
1372 for (tc_index = 0;
1373 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1374 if (*p_idx < start) {
1375 (*p_idx)++;
1376 continue;
1377 }
1378 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1379 devlink_port,
1380 devlink_sb,
1381 tc_index,
1382 DEVLINK_SB_POOL_TYPE_EGRESS,
1383 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1384 portid, seq,
1385 NLM_F_MULTI);
1386 if (err)
1387 return err;
1388 (*p_idx)++;
1389 }
1390 }
1391 return 0;
1392}
1393
1394static int
1395devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1396 struct netlink_callback *cb)
1397{
1398 struct devlink *devlink;
1399 struct devlink_sb *devlink_sb;
1400 int start = cb->args[0];
1401 int idx = 0;
1402 int err;
1403
1404 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001405 list_for_each_entry(devlink, &devlink_list, list) {
1406 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1407 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1408 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001409
1410 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001411 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1412 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1413 devlink,
1414 devlink_sb,
1415 NETLINK_CB(cb->skb).portid,
1416 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001417 if (err && err != -EOPNOTSUPP) {
1418 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001419 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001420 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001421 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001422 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001423 }
1424out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001425 mutex_unlock(&devlink_mutex);
1426
1427 cb->args[0] = idx;
1428 return msg->len;
1429}
1430
1431static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1432 unsigned int sb_index, u16 tc_index,
1433 enum devlink_sb_pool_type pool_type,
1434 u16 pool_index, u32 threshold)
1435
1436{
1437 const struct devlink_ops *ops = devlink_port->devlink->ops;
1438
1439 if (ops && ops->sb_tc_pool_bind_set)
1440 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1441 tc_index, pool_type,
1442 pool_index, threshold);
1443 return -EOPNOTSUPP;
1444}
1445
1446static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1447 struct genl_info *info)
1448{
1449 struct devlink_port *devlink_port = info->user_ptr[0];
1450 struct devlink_sb *devlink_sb = info->user_ptr[1];
1451 enum devlink_sb_pool_type pool_type;
1452 u16 tc_index;
1453 u16 pool_index;
1454 u32 threshold;
1455 int err;
1456
1457 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1458 if (err)
1459 return err;
1460
1461 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1462 pool_type, &tc_index);
1463 if (err)
1464 return err;
1465
1466 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1467 &pool_index);
1468 if (err)
1469 return err;
1470
1471 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1472 return -EINVAL;
1473
1474 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1475 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1476 tc_index, pool_type,
1477 pool_index, threshold);
1478}
1479
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001480static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1481 struct genl_info *info)
1482{
1483 struct devlink *devlink = info->user_ptr[0];
1484 struct devlink_sb *devlink_sb = info->user_ptr[1];
1485 const struct devlink_ops *ops = devlink->ops;
1486
1487 if (ops && ops->sb_occ_snapshot)
1488 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1489 return -EOPNOTSUPP;
1490}
1491
1492static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1493 struct genl_info *info)
1494{
1495 struct devlink *devlink = info->user_ptr[0];
1496 struct devlink_sb *devlink_sb = info->user_ptr[1];
1497 const struct devlink_ops *ops = devlink->ops;
1498
1499 if (ops && ops->sb_occ_max_clear)
1500 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1501 return -EOPNOTSUPP;
1502}
1503
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001504static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1505 enum devlink_command cmd, u32 portid,
1506 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001507{
Roi Dayan59bfde02016-11-22 23:09:57 +02001508 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001509 u8 inline_mode, encap_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001510 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001511 int err = 0;
1512 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001513
1514 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1515 if (!hdr)
1516 return -EMSGSIZE;
1517
Roi Dayan59bfde02016-11-22 23:09:57 +02001518 err = devlink_nl_put_handle(msg, devlink);
1519 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001520 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001521
Jiri Pirko4456f612017-02-09 15:54:36 +01001522 if (ops->eswitch_mode_get) {
1523 err = ops->eswitch_mode_get(devlink, &mode);
1524 if (err)
1525 goto nla_put_failure;
1526 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1527 if (err)
1528 goto nla_put_failure;
1529 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001530
1531 if (ops->eswitch_inline_mode_get) {
1532 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1533 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001534 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001535 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1536 inline_mode);
1537 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001538 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001539 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001540
Roi Dayanf43e9b02016-09-25 13:52:44 +03001541 if (ops->eswitch_encap_mode_get) {
1542 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1543 if (err)
1544 goto nla_put_failure;
1545 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1546 if (err)
1547 goto nla_put_failure;
1548 }
1549
Or Gerlitz08f4b592016-07-01 14:51:01 +03001550 genlmsg_end(msg, hdr);
1551 return 0;
1552
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001553nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001554 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001555 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001556}
1557
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001558static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1559 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001560{
1561 struct devlink *devlink = info->user_ptr[0];
1562 const struct devlink_ops *ops = devlink->ops;
1563 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001564 int err;
1565
Jiri Pirko4456f612017-02-09 15:54:36 +01001566 if (!ops)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001567 return -EOPNOTSUPP;
1568
Or Gerlitz08f4b592016-07-01 14:51:01 +03001569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1570 if (!msg)
1571 return -ENOMEM;
1572
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001573 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1574 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001575
1576 if (err) {
1577 nlmsg_free(msg);
1578 return err;
1579 }
1580
1581 return genlmsg_reply(msg, info);
1582}
1583
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001584static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1585 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001586{
1587 struct devlink *devlink = info->user_ptr[0];
1588 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001589 u8 inline_mode, encap_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001590 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001591 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001592
Roi Dayan59bfde02016-11-22 23:09:57 +02001593 if (!ops)
1594 return -EOPNOTSUPP;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001595
Roi Dayan59bfde02016-11-22 23:09:57 +02001596 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1597 if (!ops->eswitch_mode_set)
1598 return -EOPNOTSUPP;
1599 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1600 err = ops->eswitch_mode_set(devlink, mode);
1601 if (err)
1602 return err;
1603 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001604
Roi Dayan59bfde02016-11-22 23:09:57 +02001605 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1606 if (!ops->eswitch_inline_mode_set)
1607 return -EOPNOTSUPP;
1608 inline_mode = nla_get_u8(
1609 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1610 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1611 if (err)
1612 return err;
1613 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001614
1615 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1616 if (!ops->eswitch_encap_mode_set)
1617 return -EOPNOTSUPP;
1618 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1619 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1620 if (err)
1621 return err;
1622 }
1623
Roi Dayan59bfde02016-11-22 23:09:57 +02001624 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001625}
1626
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001627int devlink_dpipe_match_put(struct sk_buff *skb,
1628 struct devlink_dpipe_match *match)
1629{
1630 struct devlink_dpipe_header *header = match->header;
1631 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1632 struct nlattr *match_attr;
1633
1634 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1635 if (!match_attr)
1636 return -EMSGSIZE;
1637
1638 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1639 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1640 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1641 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1642 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1643 goto nla_put_failure;
1644
1645 nla_nest_end(skb, match_attr);
1646 return 0;
1647
1648nla_put_failure:
1649 nla_nest_cancel(skb, match_attr);
1650 return -EMSGSIZE;
1651}
1652EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1653
1654static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1655 struct sk_buff *skb)
1656{
1657 struct nlattr *matches_attr;
1658
1659 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1660 if (!matches_attr)
1661 return -EMSGSIZE;
1662
1663 if (table->table_ops->matches_dump(table->priv, skb))
1664 goto nla_put_failure;
1665
1666 nla_nest_end(skb, matches_attr);
1667 return 0;
1668
1669nla_put_failure:
1670 nla_nest_cancel(skb, matches_attr);
1671 return -EMSGSIZE;
1672}
1673
1674int devlink_dpipe_action_put(struct sk_buff *skb,
1675 struct devlink_dpipe_action *action)
1676{
1677 struct devlink_dpipe_header *header = action->header;
1678 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1679 struct nlattr *action_attr;
1680
1681 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1682 if (!action_attr)
1683 return -EMSGSIZE;
1684
1685 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1686 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1687 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1688 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1689 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1690 goto nla_put_failure;
1691
1692 nla_nest_end(skb, action_attr);
1693 return 0;
1694
1695nla_put_failure:
1696 nla_nest_cancel(skb, action_attr);
1697 return -EMSGSIZE;
1698}
1699EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1700
1701static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1702 struct sk_buff *skb)
1703{
1704 struct nlattr *actions_attr;
1705
1706 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1707 if (!actions_attr)
1708 return -EMSGSIZE;
1709
1710 if (table->table_ops->actions_dump(table->priv, skb))
1711 goto nla_put_failure;
1712
1713 nla_nest_end(skb, actions_attr);
1714 return 0;
1715
1716nla_put_failure:
1717 nla_nest_cancel(skb, actions_attr);
1718 return -EMSGSIZE;
1719}
1720
1721static int devlink_dpipe_table_put(struct sk_buff *skb,
1722 struct devlink_dpipe_table *table)
1723{
1724 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001725 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001726
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001727 table_size = table->table_ops->size_get(table->priv);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001728 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1729 if (!table_attr)
1730 return -EMSGSIZE;
1731
1732 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001733 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001734 DEVLINK_ATTR_PAD))
1735 goto nla_put_failure;
1736 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1737 table->counters_enabled))
1738 goto nla_put_failure;
1739
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001740 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001741 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1742 table->resource_id, DEVLINK_ATTR_PAD) ||
1743 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1744 table->resource_units, DEVLINK_ATTR_PAD))
1745 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001746 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001747 if (devlink_dpipe_matches_put(table, skb))
1748 goto nla_put_failure;
1749
1750 if (devlink_dpipe_actions_put(table, skb))
1751 goto nla_put_failure;
1752
1753 nla_nest_end(skb, table_attr);
1754 return 0;
1755
1756nla_put_failure:
1757 nla_nest_cancel(skb, table_attr);
1758 return -EMSGSIZE;
1759}
1760
1761static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1762 struct genl_info *info)
1763{
1764 int err;
1765
1766 if (*pskb) {
1767 err = genlmsg_reply(*pskb, info);
1768 if (err)
1769 return err;
1770 }
1771 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1772 if (!*pskb)
1773 return -ENOMEM;
1774 return 0;
1775}
1776
1777static int devlink_dpipe_tables_fill(struct genl_info *info,
1778 enum devlink_command cmd, int flags,
1779 struct list_head *dpipe_tables,
1780 const char *table_name)
1781{
1782 struct devlink *devlink = info->user_ptr[0];
1783 struct devlink_dpipe_table *table;
1784 struct nlattr *tables_attr;
1785 struct sk_buff *skb = NULL;
1786 struct nlmsghdr *nlh;
1787 bool incomplete;
1788 void *hdr;
1789 int i;
1790 int err;
1791
1792 table = list_first_entry(dpipe_tables,
1793 struct devlink_dpipe_table, list);
1794start_again:
1795 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1796 if (err)
1797 return err;
1798
1799 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1800 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001801 if (!hdr) {
1802 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001803 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001804 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001805
1806 if (devlink_nl_put_handle(skb, devlink))
1807 goto nla_put_failure;
1808 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1809 if (!tables_attr)
1810 goto nla_put_failure;
1811
1812 i = 0;
1813 incomplete = false;
1814 list_for_each_entry_from(table, dpipe_tables, list) {
1815 if (!table_name) {
1816 err = devlink_dpipe_table_put(skb, table);
1817 if (err) {
1818 if (!i)
1819 goto err_table_put;
1820 incomplete = true;
1821 break;
1822 }
1823 } else {
1824 if (!strcmp(table->name, table_name)) {
1825 err = devlink_dpipe_table_put(skb, table);
1826 if (err)
1827 break;
1828 }
1829 }
1830 i++;
1831 }
1832
1833 nla_nest_end(skb, tables_attr);
1834 genlmsg_end(skb, hdr);
1835 if (incomplete)
1836 goto start_again;
1837
1838send_done:
1839 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1840 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1841 if (!nlh) {
1842 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1843 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001844 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001845 goto send_done;
1846 }
1847
1848 return genlmsg_reply(skb, info);
1849
1850nla_put_failure:
1851 err = -EMSGSIZE;
1852err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001853 nlmsg_free(skb);
1854 return err;
1855}
1856
1857static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1858 struct genl_info *info)
1859{
1860 struct devlink *devlink = info->user_ptr[0];
1861 const char *table_name = NULL;
1862
1863 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1864 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1865
1866 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1867 &devlink->dpipe_table_list,
1868 table_name);
1869}
1870
1871static int devlink_dpipe_value_put(struct sk_buff *skb,
1872 struct devlink_dpipe_value *value)
1873{
1874 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1875 value->value_size, value->value))
1876 return -EMSGSIZE;
1877 if (value->mask)
1878 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1879 value->value_size, value->mask))
1880 return -EMSGSIZE;
1881 if (value->mapping_valid)
1882 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1883 value->mapping_value))
1884 return -EMSGSIZE;
1885 return 0;
1886}
1887
1888static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1889 struct devlink_dpipe_value *value)
1890{
1891 if (!value->action)
1892 return -EINVAL;
1893 if (devlink_dpipe_action_put(skb, value->action))
1894 return -EMSGSIZE;
1895 if (devlink_dpipe_value_put(skb, value))
1896 return -EMSGSIZE;
1897 return 0;
1898}
1899
1900static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1901 struct devlink_dpipe_value *values,
1902 unsigned int values_count)
1903{
1904 struct nlattr *action_attr;
1905 int i;
1906 int err;
1907
1908 for (i = 0; i < values_count; i++) {
1909 action_attr = nla_nest_start(skb,
1910 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1911 if (!action_attr)
1912 return -EMSGSIZE;
1913 err = devlink_dpipe_action_value_put(skb, &values[i]);
1914 if (err)
1915 goto err_action_value_put;
1916 nla_nest_end(skb, action_attr);
1917 }
1918 return 0;
1919
1920err_action_value_put:
1921 nla_nest_cancel(skb, action_attr);
1922 return err;
1923}
1924
1925static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1926 struct devlink_dpipe_value *value)
1927{
1928 if (!value->match)
1929 return -EINVAL;
1930 if (devlink_dpipe_match_put(skb, value->match))
1931 return -EMSGSIZE;
1932 if (devlink_dpipe_value_put(skb, value))
1933 return -EMSGSIZE;
1934 return 0;
1935}
1936
1937static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1938 struct devlink_dpipe_value *values,
1939 unsigned int values_count)
1940{
1941 struct nlattr *match_attr;
1942 int i;
1943 int err;
1944
1945 for (i = 0; i < values_count; i++) {
1946 match_attr = nla_nest_start(skb,
1947 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1948 if (!match_attr)
1949 return -EMSGSIZE;
1950 err = devlink_dpipe_match_value_put(skb, &values[i]);
1951 if (err)
1952 goto err_match_value_put;
1953 nla_nest_end(skb, match_attr);
1954 }
1955 return 0;
1956
1957err_match_value_put:
1958 nla_nest_cancel(skb, match_attr);
1959 return err;
1960}
1961
1962static int devlink_dpipe_entry_put(struct sk_buff *skb,
1963 struct devlink_dpipe_entry *entry)
1964{
1965 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1966 int err;
1967
1968 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1969 if (!entry_attr)
1970 return -EMSGSIZE;
1971
1972 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
1973 DEVLINK_ATTR_PAD))
1974 goto nla_put_failure;
1975 if (entry->counter_valid)
1976 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
1977 entry->counter, DEVLINK_ATTR_PAD))
1978 goto nla_put_failure;
1979
1980 matches_attr = nla_nest_start(skb,
1981 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
1982 if (!matches_attr)
1983 goto nla_put_failure;
1984
1985 err = devlink_dpipe_match_values_put(skb, entry->match_values,
1986 entry->match_values_count);
1987 if (err) {
1988 nla_nest_cancel(skb, matches_attr);
1989 goto err_match_values_put;
1990 }
1991 nla_nest_end(skb, matches_attr);
1992
1993 actions_attr = nla_nest_start(skb,
1994 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
1995 if (!actions_attr)
1996 goto nla_put_failure;
1997
1998 err = devlink_dpipe_action_values_put(skb, entry->action_values,
1999 entry->action_values_count);
2000 if (err) {
2001 nla_nest_cancel(skb, actions_attr);
2002 goto err_action_values_put;
2003 }
2004 nla_nest_end(skb, actions_attr);
2005
2006 nla_nest_end(skb, entry_attr);
2007 return 0;
2008
2009nla_put_failure:
2010 err = -EMSGSIZE;
2011err_match_values_put:
2012err_action_values_put:
2013 nla_nest_cancel(skb, entry_attr);
2014 return err;
2015}
2016
2017static struct devlink_dpipe_table *
2018devlink_dpipe_table_find(struct list_head *dpipe_tables,
2019 const char *table_name)
2020{
2021 struct devlink_dpipe_table *table;
2022
2023 list_for_each_entry_rcu(table, dpipe_tables, list) {
2024 if (!strcmp(table->name, table_name))
2025 return table;
2026 }
2027 return NULL;
2028}
2029
2030int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2031{
2032 struct devlink *devlink;
2033 int err;
2034
2035 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2036 dump_ctx->info);
2037 if (err)
2038 return err;
2039
2040 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2041 dump_ctx->info->snd_portid,
2042 dump_ctx->info->snd_seq,
2043 &devlink_nl_family, NLM_F_MULTI,
2044 dump_ctx->cmd);
2045 if (!dump_ctx->hdr)
2046 goto nla_put_failure;
2047
2048 devlink = dump_ctx->info->user_ptr[0];
2049 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2050 goto nla_put_failure;
2051 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2052 DEVLINK_ATTR_DPIPE_ENTRIES);
2053 if (!dump_ctx->nest)
2054 goto nla_put_failure;
2055 return 0;
2056
2057nla_put_failure:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002058 nlmsg_free(dump_ctx->skb);
2059 return -EMSGSIZE;
2060}
2061EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2062
2063int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2064 struct devlink_dpipe_entry *entry)
2065{
2066 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2067}
2068EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2069
2070int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2071{
2072 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2073 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2074 return 0;
2075}
2076EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2077
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002078void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2079
2080{
2081 unsigned int value_count, value_index;
2082 struct devlink_dpipe_value *value;
2083
2084 value = entry->action_values;
2085 value_count = entry->action_values_count;
2086 for (value_index = 0; value_index < value_count; value_index++) {
2087 kfree(value[value_index].value);
2088 kfree(value[value_index].mask);
2089 }
2090
2091 value = entry->match_values;
2092 value_count = entry->match_values_count;
2093 for (value_index = 0; value_index < value_count; value_index++) {
2094 kfree(value[value_index].value);
2095 kfree(value[value_index].mask);
2096 }
2097}
2098EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2099
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002100static int devlink_dpipe_entries_fill(struct genl_info *info,
2101 enum devlink_command cmd, int flags,
2102 struct devlink_dpipe_table *table)
2103{
2104 struct devlink_dpipe_dump_ctx dump_ctx;
2105 struct nlmsghdr *nlh;
2106 int err;
2107
2108 dump_ctx.skb = NULL;
2109 dump_ctx.cmd = cmd;
2110 dump_ctx.info = info;
2111
2112 err = table->table_ops->entries_dump(table->priv,
2113 table->counters_enabled,
2114 &dump_ctx);
2115 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002116 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002117
2118send_done:
2119 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2120 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2121 if (!nlh) {
2122 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2123 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002124 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002125 goto send_done;
2126 }
2127 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002128}
2129
2130static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2131 struct genl_info *info)
2132{
2133 struct devlink *devlink = info->user_ptr[0];
2134 struct devlink_dpipe_table *table;
2135 const char *table_name;
2136
2137 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2138 return -EINVAL;
2139
2140 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2141 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2142 table_name);
2143 if (!table)
2144 return -EINVAL;
2145
2146 if (!table->table_ops->entries_dump)
2147 return -EINVAL;
2148
2149 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2150 0, table);
2151}
2152
2153static int devlink_dpipe_fields_put(struct sk_buff *skb,
2154 const struct devlink_dpipe_header *header)
2155{
2156 struct devlink_dpipe_field *field;
2157 struct nlattr *field_attr;
2158 int i;
2159
2160 for (i = 0; i < header->fields_count; i++) {
2161 field = &header->fields[i];
2162 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2163 if (!field_attr)
2164 return -EMSGSIZE;
2165 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2166 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2167 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2168 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2169 goto nla_put_failure;
2170 nla_nest_end(skb, field_attr);
2171 }
2172 return 0;
2173
2174nla_put_failure:
2175 nla_nest_cancel(skb, field_attr);
2176 return -EMSGSIZE;
2177}
2178
2179static int devlink_dpipe_header_put(struct sk_buff *skb,
2180 struct devlink_dpipe_header *header)
2181{
2182 struct nlattr *fields_attr, *header_attr;
2183 int err;
2184
2185 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002186 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002187 return -EMSGSIZE;
2188
2189 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2190 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2191 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2192 goto nla_put_failure;
2193
2194 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2195 if (!fields_attr)
2196 goto nla_put_failure;
2197
2198 err = devlink_dpipe_fields_put(skb, header);
2199 if (err) {
2200 nla_nest_cancel(skb, fields_attr);
2201 goto nla_put_failure;
2202 }
2203 nla_nest_end(skb, fields_attr);
2204 nla_nest_end(skb, header_attr);
2205 return 0;
2206
2207nla_put_failure:
2208 err = -EMSGSIZE;
2209 nla_nest_cancel(skb, header_attr);
2210 return err;
2211}
2212
2213static int devlink_dpipe_headers_fill(struct genl_info *info,
2214 enum devlink_command cmd, int flags,
2215 struct devlink_dpipe_headers *
2216 dpipe_headers)
2217{
2218 struct devlink *devlink = info->user_ptr[0];
2219 struct nlattr *headers_attr;
2220 struct sk_buff *skb = NULL;
2221 struct nlmsghdr *nlh;
2222 void *hdr;
2223 int i, j;
2224 int err;
2225
2226 i = 0;
2227start_again:
2228 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2229 if (err)
2230 return err;
2231
2232 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2233 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002234 if (!hdr) {
2235 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002236 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002237 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002238
2239 if (devlink_nl_put_handle(skb, devlink))
2240 goto nla_put_failure;
2241 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2242 if (!headers_attr)
2243 goto nla_put_failure;
2244
2245 j = 0;
2246 for (; i < dpipe_headers->headers_count; i++) {
2247 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2248 if (err) {
2249 if (!j)
2250 goto err_table_put;
2251 break;
2252 }
2253 j++;
2254 }
2255 nla_nest_end(skb, headers_attr);
2256 genlmsg_end(skb, hdr);
2257 if (i != dpipe_headers->headers_count)
2258 goto start_again;
2259
2260send_done:
2261 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2262 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2263 if (!nlh) {
2264 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2265 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002266 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002267 goto send_done;
2268 }
2269 return genlmsg_reply(skb, info);
2270
2271nla_put_failure:
2272 err = -EMSGSIZE;
2273err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002274 nlmsg_free(skb);
2275 return err;
2276}
2277
2278static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2279 struct genl_info *info)
2280{
2281 struct devlink *devlink = info->user_ptr[0];
2282
2283 if (!devlink->dpipe_headers)
2284 return -EOPNOTSUPP;
2285 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2286 0, devlink->dpipe_headers);
2287}
2288
2289static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2290 const char *table_name,
2291 bool enable)
2292{
2293 struct devlink_dpipe_table *table;
2294
2295 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2296 table_name);
2297 if (!table)
2298 return -EINVAL;
2299
2300 if (table->counter_control_extern)
2301 return -EOPNOTSUPP;
2302
2303 if (!(table->counters_enabled ^ enable))
2304 return 0;
2305
2306 table->counters_enabled = enable;
2307 if (table->table_ops->counters_set_update)
2308 table->table_ops->counters_set_update(table->priv, enable);
2309 return 0;
2310}
2311
2312static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2313 struct genl_info *info)
2314{
2315 struct devlink *devlink = info->user_ptr[0];
2316 const char *table_name;
2317 bool counters_enable;
2318
2319 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2320 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2321 return -EINVAL;
2322
2323 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2324 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2325
2326 return devlink_dpipe_table_counters_set(devlink, table_name,
2327 counters_enable);
2328}
2329
Wei Yongjun43dd7512018-01-17 03:27:42 +00002330static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002331devlink_resource_find(struct devlink *devlink,
2332 struct devlink_resource *resource, u64 resource_id)
2333{
2334 struct list_head *resource_list;
2335
2336 if (resource)
2337 resource_list = &resource->resource_list;
2338 else
2339 resource_list = &devlink->resource_list;
2340
2341 list_for_each_entry(resource, resource_list, list) {
2342 struct devlink_resource *child_resource;
2343
2344 if (resource->id == resource_id)
2345 return resource;
2346
2347 child_resource = devlink_resource_find(devlink, resource,
2348 resource_id);
2349 if (child_resource)
2350 return child_resource;
2351 }
2352 return NULL;
2353}
2354
Wei Yongjun43dd7512018-01-17 03:27:42 +00002355static void
2356devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002357{
2358 struct devlink_resource *child_resource;
2359 bool size_valid = true;
2360 u64 parts_size = 0;
2361
2362 if (list_empty(&resource->resource_list))
2363 goto out;
2364
2365 list_for_each_entry(child_resource, &resource->resource_list, list)
2366 parts_size += child_resource->size_new;
2367
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002368 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002369 size_valid = false;
2370out:
2371 resource->size_valid = size_valid;
2372}
2373
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002374static int
2375devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2376 struct netlink_ext_ack *extack)
2377{
2378 u64 reminder;
2379 int err = 0;
2380
David S. Miller0f3e9c92018-03-06 00:53:44 -05002381 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002382 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2383 err = -EINVAL;
2384 }
2385
David S. Miller0f3e9c92018-03-06 00:53:44 -05002386 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002387 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2388 err = -EINVAL;
2389 }
2390
David S. Miller0f3e9c92018-03-06 00:53:44 -05002391 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002392 if (reminder) {
2393 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2394 err = -EINVAL;
2395 }
2396
2397 return err;
2398}
2399
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002400static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2401 struct genl_info *info)
2402{
2403 struct devlink *devlink = info->user_ptr[0];
2404 struct devlink_resource *resource;
2405 u64 resource_id;
2406 u64 size;
2407 int err;
2408
2409 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2410 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2411 return -EINVAL;
2412 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2413
2414 resource = devlink_resource_find(devlink, NULL, resource_id);
2415 if (!resource)
2416 return -EINVAL;
2417
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002418 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002419 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002420 if (err)
2421 return err;
2422
2423 resource->size_new = size;
2424 devlink_resource_validate_children(resource);
2425 if (resource->parent)
2426 devlink_resource_validate_children(resource->parent);
2427 return 0;
2428}
2429
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002430static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002431devlink_resource_size_params_put(struct devlink_resource *resource,
2432 struct sk_buff *skb)
2433{
2434 struct devlink_resource_size_params *size_params;
2435
Jiri Pirko77d27092018-02-28 13:12:09 +01002436 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002437 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2438 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2439 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2440 size_params->size_max, DEVLINK_ATTR_PAD) ||
2441 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2442 size_params->size_min, DEVLINK_ATTR_PAD) ||
2443 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2444 return -EMSGSIZE;
2445 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002446}
2447
Jiri Pirkofc56be42018-04-05 22:13:21 +02002448static int devlink_resource_occ_put(struct devlink_resource *resource,
2449 struct sk_buff *skb)
2450{
2451 if (!resource->occ_get)
2452 return 0;
2453 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2454 resource->occ_get(resource->occ_get_priv),
2455 DEVLINK_ATTR_PAD);
2456}
2457
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002458static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2459 struct devlink_resource *resource)
2460{
2461 struct devlink_resource *child_resource;
2462 struct nlattr *child_resource_attr;
2463 struct nlattr *resource_attr;
2464
2465 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2466 if (!resource_attr)
2467 return -EMSGSIZE;
2468
2469 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2470 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2471 DEVLINK_ATTR_PAD) ||
2472 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2473 DEVLINK_ATTR_PAD))
2474 goto nla_put_failure;
2475 if (resource->size != resource->size_new)
2476 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2477 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002478 if (devlink_resource_occ_put(resource, skb))
2479 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002480 if (devlink_resource_size_params_put(resource, skb))
2481 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002482 if (list_empty(&resource->resource_list))
2483 goto out;
2484
2485 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2486 resource->size_valid))
2487 goto nla_put_failure;
2488
2489 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2490 if (!child_resource_attr)
2491 goto nla_put_failure;
2492
2493 list_for_each_entry(child_resource, &resource->resource_list, list) {
2494 if (devlink_resource_put(devlink, skb, child_resource))
2495 goto resource_put_failure;
2496 }
2497
2498 nla_nest_end(skb, child_resource_attr);
2499out:
2500 nla_nest_end(skb, resource_attr);
2501 return 0;
2502
2503resource_put_failure:
2504 nla_nest_cancel(skb, child_resource_attr);
2505nla_put_failure:
2506 nla_nest_cancel(skb, resource_attr);
2507 return -EMSGSIZE;
2508}
2509
2510static int devlink_resource_fill(struct genl_info *info,
2511 enum devlink_command cmd, int flags)
2512{
2513 struct devlink *devlink = info->user_ptr[0];
2514 struct devlink_resource *resource;
2515 struct nlattr *resources_attr;
2516 struct sk_buff *skb = NULL;
2517 struct nlmsghdr *nlh;
2518 bool incomplete;
2519 void *hdr;
2520 int i;
2521 int err;
2522
2523 resource = list_first_entry(&devlink->resource_list,
2524 struct devlink_resource, list);
2525start_again:
2526 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2527 if (err)
2528 return err;
2529
2530 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2531 &devlink_nl_family, NLM_F_MULTI, cmd);
2532 if (!hdr) {
2533 nlmsg_free(skb);
2534 return -EMSGSIZE;
2535 }
2536
2537 if (devlink_nl_put_handle(skb, devlink))
2538 goto nla_put_failure;
2539
2540 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2541 if (!resources_attr)
2542 goto nla_put_failure;
2543
2544 incomplete = false;
2545 i = 0;
2546 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2547 err = devlink_resource_put(devlink, skb, resource);
2548 if (err) {
2549 if (!i)
2550 goto err_resource_put;
2551 incomplete = true;
2552 break;
2553 }
2554 i++;
2555 }
2556 nla_nest_end(skb, resources_attr);
2557 genlmsg_end(skb, hdr);
2558 if (incomplete)
2559 goto start_again;
2560send_done:
2561 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2562 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2563 if (!nlh) {
2564 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2565 if (err)
2566 goto err_skb_send_alloc;
2567 goto send_done;
2568 }
2569 return genlmsg_reply(skb, info);
2570
2571nla_put_failure:
2572 err = -EMSGSIZE;
2573err_resource_put:
2574err_skb_send_alloc:
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002575 nlmsg_free(skb);
2576 return err;
2577}
2578
2579static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2580 struct genl_info *info)
2581{
2582 struct devlink *devlink = info->user_ptr[0];
2583
2584 if (list_empty(&devlink->resource_list))
2585 return -EOPNOTSUPP;
2586
2587 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2588}
2589
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002590static int
2591devlink_resources_validate(struct devlink *devlink,
2592 struct devlink_resource *resource,
2593 struct genl_info *info)
2594{
2595 struct list_head *resource_list;
2596 int err = 0;
2597
2598 if (resource)
2599 resource_list = &resource->resource_list;
2600 else
2601 resource_list = &devlink->resource_list;
2602
2603 list_for_each_entry(resource, resource_list, list) {
2604 if (!resource->size_valid)
2605 return -EINVAL;
2606 err = devlink_resources_validate(devlink, resource, info);
2607 if (err)
2608 return err;
2609 }
2610 return err;
2611}
2612
2613static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2614{
2615 struct devlink *devlink = info->user_ptr[0];
2616 int err;
2617
2618 if (!devlink->ops->reload)
2619 return -EOPNOTSUPP;
2620
2621 err = devlink_resources_validate(devlink, NULL, info);
2622 if (err) {
2623 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2624 return err;
2625 }
David Ahernac0fc8a2018-06-05 08:14:09 -07002626 return devlink->ops->reload(devlink, info->extack);
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002627}
2628
Moshe Shemesh036467c2018-07-04 14:30:33 +03002629static const struct devlink_param devlink_param_generic[] = {
2630 {
2631 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2632 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2633 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2634 },
2635 {
2636 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2637 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2638 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2639 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002640 {
2641 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2642 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2643 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2644 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03002645};
Moshe Shemesheabaef12018-07-04 14:30:28 +03002646
2647static int devlink_param_generic_verify(const struct devlink_param *param)
2648{
2649 /* verify it match generic parameter by id and name */
2650 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2651 return -EINVAL;
2652 if (strcmp(param->name, devlink_param_generic[param->id].name))
2653 return -ENOENT;
2654
2655 WARN_ON(param->type != devlink_param_generic[param->id].type);
2656
2657 return 0;
2658}
2659
2660static int devlink_param_driver_verify(const struct devlink_param *param)
2661{
2662 int i;
2663
2664 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2665 return -EINVAL;
2666 /* verify no such name in generic params */
2667 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2668 if (!strcmp(param->name, devlink_param_generic[i].name))
2669 return -EEXIST;
2670
2671 return 0;
2672}
2673
2674static struct devlink_param_item *
2675devlink_param_find_by_name(struct list_head *param_list,
2676 const char *param_name)
2677{
2678 struct devlink_param_item *param_item;
2679
2680 list_for_each_entry(param_item, param_list, list)
2681 if (!strcmp(param_item->param->name, param_name))
2682 return param_item;
2683 return NULL;
2684}
2685
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03002686static struct devlink_param_item *
2687devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2688{
2689 struct devlink_param_item *param_item;
2690
2691 list_for_each_entry(param_item, param_list, list)
2692 if (param_item->param->id == param_id)
2693 return param_item;
2694 return NULL;
2695}
2696
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002697static bool
2698devlink_param_cmode_is_supported(const struct devlink_param *param,
2699 enum devlink_param_cmode cmode)
2700{
2701 return test_bit(cmode, &param->supported_cmodes);
2702}
2703
2704static int devlink_param_get(struct devlink *devlink,
2705 const struct devlink_param *param,
2706 struct devlink_param_gset_ctx *ctx)
2707{
2708 if (!param->get)
2709 return -EOPNOTSUPP;
2710 return param->get(devlink, param->id, ctx);
2711}
2712
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002713static int devlink_param_set(struct devlink *devlink,
2714 const struct devlink_param *param,
2715 struct devlink_param_gset_ctx *ctx)
2716{
2717 if (!param->set)
2718 return -EOPNOTSUPP;
2719 return param->set(devlink, param->id, ctx);
2720}
2721
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002722static int
2723devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2724{
2725 switch (param_type) {
2726 case DEVLINK_PARAM_TYPE_U8:
2727 return NLA_U8;
2728 case DEVLINK_PARAM_TYPE_U16:
2729 return NLA_U16;
2730 case DEVLINK_PARAM_TYPE_U32:
2731 return NLA_U32;
2732 case DEVLINK_PARAM_TYPE_STRING:
2733 return NLA_STRING;
2734 case DEVLINK_PARAM_TYPE_BOOL:
2735 return NLA_FLAG;
2736 default:
2737 return -EINVAL;
2738 }
2739}
2740
2741static int
2742devlink_nl_param_value_fill_one(struct sk_buff *msg,
2743 enum devlink_param_type type,
2744 enum devlink_param_cmode cmode,
2745 union devlink_param_value val)
2746{
2747 struct nlattr *param_value_attr;
2748
2749 param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2750 if (!param_value_attr)
2751 goto nla_put_failure;
2752
2753 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2754 goto value_nest_cancel;
2755
2756 switch (type) {
2757 case DEVLINK_PARAM_TYPE_U8:
2758 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2759 goto value_nest_cancel;
2760 break;
2761 case DEVLINK_PARAM_TYPE_U16:
2762 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2763 goto value_nest_cancel;
2764 break;
2765 case DEVLINK_PARAM_TYPE_U32:
2766 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2767 goto value_nest_cancel;
2768 break;
2769 case DEVLINK_PARAM_TYPE_STRING:
2770 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2771 val.vstr))
2772 goto value_nest_cancel;
2773 break;
2774 case DEVLINK_PARAM_TYPE_BOOL:
2775 if (val.vbool &&
2776 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2777 goto value_nest_cancel;
2778 break;
2779 }
2780
2781 nla_nest_end(msg, param_value_attr);
2782 return 0;
2783
2784value_nest_cancel:
2785 nla_nest_cancel(msg, param_value_attr);
2786nla_put_failure:
2787 return -EMSGSIZE;
2788}
2789
2790static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2791 struct devlink_param_item *param_item,
2792 enum devlink_command cmd,
2793 u32 portid, u32 seq, int flags)
2794{
2795 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2796 const struct devlink_param *param = param_item->param;
2797 struct devlink_param_gset_ctx ctx;
2798 struct nlattr *param_values_list;
2799 struct nlattr *param_attr;
2800 int nla_type;
2801 void *hdr;
2802 int err;
2803 int i;
2804
2805 /* Get value from driver part to driverinit configuration mode */
2806 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2807 if (!devlink_param_cmode_is_supported(param, i))
2808 continue;
2809 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2810 if (!param_item->driverinit_value_valid)
2811 return -EOPNOTSUPP;
2812 param_value[i] = param_item->driverinit_value;
2813 } else {
2814 ctx.cmode = i;
2815 err = devlink_param_get(devlink, param, &ctx);
2816 if (err)
2817 return err;
2818 param_value[i] = ctx.val;
2819 }
2820 }
2821
2822 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2823 if (!hdr)
2824 return -EMSGSIZE;
2825
2826 if (devlink_nl_put_handle(msg, devlink))
2827 goto genlmsg_cancel;
2828 param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2829 if (!param_attr)
2830 goto genlmsg_cancel;
2831 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2832 goto param_nest_cancel;
2833 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2834 goto param_nest_cancel;
2835
2836 nla_type = devlink_param_type_to_nla_type(param->type);
2837 if (nla_type < 0)
2838 goto param_nest_cancel;
2839 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2840 goto param_nest_cancel;
2841
2842 param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2843 if (!param_values_list)
2844 goto param_nest_cancel;
2845
2846 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2847 if (!devlink_param_cmode_is_supported(param, i))
2848 continue;
2849 err = devlink_nl_param_value_fill_one(msg, param->type,
2850 i, param_value[i]);
2851 if (err)
2852 goto values_list_nest_cancel;
2853 }
2854
2855 nla_nest_end(msg, param_values_list);
2856 nla_nest_end(msg, param_attr);
2857 genlmsg_end(msg, hdr);
2858 return 0;
2859
2860values_list_nest_cancel:
2861 nla_nest_end(msg, param_values_list);
2862param_nest_cancel:
2863 nla_nest_cancel(msg, param_attr);
2864genlmsg_cancel:
2865 genlmsg_cancel(msg, hdr);
2866 return -EMSGSIZE;
2867}
2868
Moshe Shemeshea601e12018-07-04 14:30:32 +03002869static void devlink_param_notify(struct devlink *devlink,
2870 struct devlink_param_item *param_item,
2871 enum devlink_command cmd)
2872{
2873 struct sk_buff *msg;
2874 int err;
2875
2876 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
2877
2878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2879 if (!msg)
2880 return;
2881 err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
2882 if (err) {
2883 nlmsg_free(msg);
2884 return;
2885 }
2886
2887 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2888 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2889}
2890
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002891static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2892 struct netlink_callback *cb)
2893{
2894 struct devlink_param_item *param_item;
2895 struct devlink *devlink;
2896 int start = cb->args[0];
2897 int idx = 0;
2898 int err;
2899
2900 mutex_lock(&devlink_mutex);
2901 list_for_each_entry(devlink, &devlink_list, list) {
2902 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2903 continue;
2904 mutex_lock(&devlink->lock);
2905 list_for_each_entry(param_item, &devlink->param_list, list) {
2906 if (idx < start) {
2907 idx++;
2908 continue;
2909 }
2910 err = devlink_nl_param_fill(msg, devlink, param_item,
2911 DEVLINK_CMD_PARAM_GET,
2912 NETLINK_CB(cb->skb).portid,
2913 cb->nlh->nlmsg_seq,
2914 NLM_F_MULTI);
2915 if (err) {
2916 mutex_unlock(&devlink->lock);
2917 goto out;
2918 }
2919 idx++;
2920 }
2921 mutex_unlock(&devlink->lock);
2922 }
2923out:
2924 mutex_unlock(&devlink_mutex);
2925
2926 cb->args[0] = idx;
2927 return msg->len;
2928}
2929
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002930static int
2931devlink_param_type_get_from_info(struct genl_info *info,
2932 enum devlink_param_type *param_type)
2933{
2934 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
2935 return -EINVAL;
2936
2937 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
2938 case NLA_U8:
2939 *param_type = DEVLINK_PARAM_TYPE_U8;
2940 break;
2941 case NLA_U16:
2942 *param_type = DEVLINK_PARAM_TYPE_U16;
2943 break;
2944 case NLA_U32:
2945 *param_type = DEVLINK_PARAM_TYPE_U32;
2946 break;
2947 case NLA_STRING:
2948 *param_type = DEVLINK_PARAM_TYPE_STRING;
2949 break;
2950 case NLA_FLAG:
2951 *param_type = DEVLINK_PARAM_TYPE_BOOL;
2952 break;
2953 default:
2954 return -EINVAL;
2955 }
2956
2957 return 0;
2958}
2959
2960static int
2961devlink_param_value_get_from_info(const struct devlink_param *param,
2962 struct genl_info *info,
2963 union devlink_param_value *value)
2964{
2965 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
2966 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
2967 return -EINVAL;
2968
2969 switch (param->type) {
2970 case DEVLINK_PARAM_TYPE_U8:
2971 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
2972 break;
2973 case DEVLINK_PARAM_TYPE_U16:
2974 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
2975 break;
2976 case DEVLINK_PARAM_TYPE_U32:
2977 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
2978 break;
2979 case DEVLINK_PARAM_TYPE_STRING:
2980 if (nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) >
2981 DEVLINK_PARAM_MAX_STRING_VALUE)
2982 return -EINVAL;
2983 value->vstr = nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
2984 break;
2985 case DEVLINK_PARAM_TYPE_BOOL:
2986 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
2987 true : false;
2988 break;
2989 }
2990 return 0;
2991}
2992
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002993static struct devlink_param_item *
2994devlink_param_get_from_info(struct devlink *devlink,
2995 struct genl_info *info)
2996{
2997 char *param_name;
2998
2999 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3000 return NULL;
3001
3002 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3003 return devlink_param_find_by_name(&devlink->param_list, param_name);
3004}
3005
3006static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3007 struct genl_info *info)
3008{
3009 struct devlink *devlink = info->user_ptr[0];
3010 struct devlink_param_item *param_item;
3011 struct sk_buff *msg;
3012 int err;
3013
3014 param_item = devlink_param_get_from_info(devlink, info);
3015 if (!param_item)
3016 return -EINVAL;
3017
3018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3019 if (!msg)
3020 return -ENOMEM;
3021
3022 err = devlink_nl_param_fill(msg, devlink, param_item,
3023 DEVLINK_CMD_PARAM_GET,
3024 info->snd_portid, info->snd_seq, 0);
3025 if (err) {
3026 nlmsg_free(msg);
3027 return err;
3028 }
3029
3030 return genlmsg_reply(msg, info);
3031}
3032
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003033static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3034 struct genl_info *info)
3035{
3036 struct devlink *devlink = info->user_ptr[0];
3037 enum devlink_param_type param_type;
3038 struct devlink_param_gset_ctx ctx;
3039 enum devlink_param_cmode cmode;
3040 struct devlink_param_item *param_item;
3041 const struct devlink_param *param;
3042 union devlink_param_value value;
3043 int err = 0;
3044
3045 param_item = devlink_param_get_from_info(devlink, info);
3046 if (!param_item)
3047 return -EINVAL;
3048 param = param_item->param;
3049 err = devlink_param_type_get_from_info(info, &param_type);
3050 if (err)
3051 return err;
3052 if (param_type != param->type)
3053 return -EINVAL;
3054 err = devlink_param_value_get_from_info(param, info, &value);
3055 if (err)
3056 return err;
3057 if (param->validate) {
3058 err = param->validate(devlink, param->id, value, info->extack);
3059 if (err)
3060 return err;
3061 }
3062
3063 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3064 return -EINVAL;
3065 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3066 if (!devlink_param_cmode_is_supported(param, cmode))
3067 return -EOPNOTSUPP;
3068
3069 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3070 param_item->driverinit_value = value;
3071 param_item->driverinit_value_valid = true;
3072 } else {
3073 if (!param->set)
3074 return -EOPNOTSUPP;
3075 ctx.val = value;
3076 ctx.cmode = cmode;
3077 err = devlink_param_set(devlink, param, &ctx);
3078 if (err)
3079 return err;
3080 }
3081
Moshe Shemeshea601e12018-07-04 14:30:32 +03003082 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003083 return 0;
3084}
3085
Moshe Shemesheabaef12018-07-04 14:30:28 +03003086static int devlink_param_register_one(struct devlink *devlink,
3087 const struct devlink_param *param)
3088{
3089 struct devlink_param_item *param_item;
3090
3091 if (devlink_param_find_by_name(&devlink->param_list,
3092 param->name))
3093 return -EEXIST;
3094
3095 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3096 WARN_ON(param->get || param->set);
3097 else
3098 WARN_ON(!param->get || !param->set);
3099
3100 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3101 if (!param_item)
3102 return -ENOMEM;
3103 param_item->param = param;
3104
3105 list_add_tail(&param_item->list, &devlink->param_list);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003106 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003107 return 0;
3108}
3109
3110static void devlink_param_unregister_one(struct devlink *devlink,
3111 const struct devlink_param *param)
3112{
3113 struct devlink_param_item *param_item;
3114
3115 param_item = devlink_param_find_by_name(&devlink->param_list,
3116 param->name);
3117 WARN_ON(!param_item);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003118 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003119 list_del(&param_item->list);
3120 kfree(param_item);
3121}
3122
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003123static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
3124 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
3125 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
3126 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
3127 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
3128 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02003129 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
3130 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
3131 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
3132 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
3133 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
3134 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
3135 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03003136 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02003137 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03003138 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003139 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
3140 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003141 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
3142 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003143 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
3144 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
3145 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003146};
3147
3148static const struct genl_ops devlink_nl_ops[] = {
3149 {
3150 .cmd = DEVLINK_CMD_GET,
3151 .doit = devlink_nl_cmd_get_doit,
3152 .dumpit = devlink_nl_cmd_get_dumpit,
3153 .policy = devlink_nl_policy,
Jiri Pirko1fc22572016-04-08 19:12:48 +02003154 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003155 /* can be retrieved by unprivileged users */
3156 },
3157 {
3158 .cmd = DEVLINK_CMD_PORT_GET,
3159 .doit = devlink_nl_cmd_port_get_doit,
3160 .dumpit = devlink_nl_cmd_port_get_dumpit,
3161 .policy = devlink_nl_policy,
3162 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3163 /* can be retrieved by unprivileged users */
3164 },
3165 {
3166 .cmd = DEVLINK_CMD_PORT_SET,
3167 .doit = devlink_nl_cmd_port_set_doit,
3168 .policy = devlink_nl_policy,
3169 .flags = GENL_ADMIN_PERM,
3170 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3171 },
3172 {
3173 .cmd = DEVLINK_CMD_PORT_SPLIT,
3174 .doit = devlink_nl_cmd_port_split_doit,
3175 .policy = devlink_nl_policy,
3176 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003177 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3178 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003179 },
3180 {
3181 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
3182 .doit = devlink_nl_cmd_port_unsplit_doit,
3183 .policy = devlink_nl_policy,
3184 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003185 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3186 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003187 },
Jiri Pirkobf797472016-04-14 18:19:13 +02003188 {
3189 .cmd = DEVLINK_CMD_SB_GET,
3190 .doit = devlink_nl_cmd_sb_get_doit,
3191 .dumpit = devlink_nl_cmd_sb_get_dumpit,
3192 .policy = devlink_nl_policy,
3193 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3194 DEVLINK_NL_FLAG_NEED_SB,
3195 /* can be retrieved by unprivileged users */
3196 },
3197 {
3198 .cmd = DEVLINK_CMD_SB_POOL_GET,
3199 .doit = devlink_nl_cmd_sb_pool_get_doit,
3200 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
3201 .policy = devlink_nl_policy,
3202 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3203 DEVLINK_NL_FLAG_NEED_SB,
3204 /* can be retrieved by unprivileged users */
3205 },
3206 {
3207 .cmd = DEVLINK_CMD_SB_POOL_SET,
3208 .doit = devlink_nl_cmd_sb_pool_set_doit,
3209 .policy = devlink_nl_policy,
3210 .flags = GENL_ADMIN_PERM,
3211 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3212 DEVLINK_NL_FLAG_NEED_SB,
3213 },
3214 {
3215 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
3216 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
3217 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
3218 .policy = devlink_nl_policy,
3219 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3220 DEVLINK_NL_FLAG_NEED_SB,
3221 /* can be retrieved by unprivileged users */
3222 },
3223 {
3224 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
3225 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
3226 .policy = devlink_nl_policy,
3227 .flags = GENL_ADMIN_PERM,
3228 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3229 DEVLINK_NL_FLAG_NEED_SB,
3230 },
3231 {
3232 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
3233 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
3234 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
3235 .policy = devlink_nl_policy,
3236 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3237 DEVLINK_NL_FLAG_NEED_SB,
3238 /* can be retrieved by unprivileged users */
3239 },
3240 {
3241 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3242 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
3243 .policy = devlink_nl_policy,
3244 .flags = GENL_ADMIN_PERM,
3245 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3246 DEVLINK_NL_FLAG_NEED_SB,
3247 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003248 {
3249 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
3250 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
3251 .policy = devlink_nl_policy,
3252 .flags = GENL_ADMIN_PERM,
3253 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003254 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003255 },
3256 {
3257 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
3258 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
3259 .policy = devlink_nl_policy,
3260 .flags = GENL_ADMIN_PERM,
3261 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003262 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003263 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03003264 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01003265 .cmd = DEVLINK_CMD_ESWITCH_GET,
3266 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003267 .policy = devlink_nl_policy,
3268 .flags = GENL_ADMIN_PERM,
3269 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3270 },
3271 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01003272 .cmd = DEVLINK_CMD_ESWITCH_SET,
3273 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003274 .policy = devlink_nl_policy,
3275 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07003276 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3277 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003278 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003279 {
3280 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
3281 .doit = devlink_nl_cmd_dpipe_table_get,
3282 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003283 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003284 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003285 },
3286 {
3287 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
3288 .doit = devlink_nl_cmd_dpipe_entries_get,
3289 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003290 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003291 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003292 },
3293 {
3294 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
3295 .doit = devlink_nl_cmd_dpipe_headers_get,
3296 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003297 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003298 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003299 },
3300 {
3301 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
3302 .doit = devlink_nl_cmd_dpipe_table_counters_set,
3303 .policy = devlink_nl_policy,
3304 .flags = GENL_ADMIN_PERM,
3305 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3306 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003307 {
3308 .cmd = DEVLINK_CMD_RESOURCE_SET,
3309 .doit = devlink_nl_cmd_resource_set,
3310 .policy = devlink_nl_policy,
3311 .flags = GENL_ADMIN_PERM,
3312 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3313 },
3314 {
3315 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
3316 .doit = devlink_nl_cmd_resource_dump,
3317 .policy = devlink_nl_policy,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003318 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003319 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003320 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01003321 {
3322 .cmd = DEVLINK_CMD_RELOAD,
3323 .doit = devlink_nl_cmd_reload,
3324 .policy = devlink_nl_policy,
3325 .flags = GENL_ADMIN_PERM,
3326 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3327 DEVLINK_NL_FLAG_NO_LOCK,
3328 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003329 {
3330 .cmd = DEVLINK_CMD_PARAM_GET,
3331 .doit = devlink_nl_cmd_param_get_doit,
3332 .dumpit = devlink_nl_cmd_param_get_dumpit,
3333 .policy = devlink_nl_policy,
3334 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3335 /* can be retrieved by unprivileged users */
3336 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003337 {
3338 .cmd = DEVLINK_CMD_PARAM_SET,
3339 .doit = devlink_nl_cmd_param_set_doit,
3340 .policy = devlink_nl_policy,
3341 .flags = GENL_ADMIN_PERM,
3342 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3343 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003344};
3345
Johannes Berg56989f62016-10-24 14:40:05 +02003346static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003347 .name = DEVLINK_GENL_NAME,
3348 .version = DEVLINK_GENL_VERSION,
3349 .maxattr = DEVLINK_ATTR_MAX,
3350 .netnsok = true,
3351 .pre_doit = devlink_nl_pre_doit,
3352 .post_doit = devlink_nl_post_doit,
3353 .module = THIS_MODULE,
3354 .ops = devlink_nl_ops,
3355 .n_ops = ARRAY_SIZE(devlink_nl_ops),
3356 .mcgrps = devlink_nl_mcgrps,
3357 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
3358};
3359
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003360/**
3361 * devlink_alloc - Allocate new devlink instance resources
3362 *
3363 * @ops: ops
3364 * @priv_size: size of user private data
3365 *
3366 * Allocate new devlink instance resources, including devlink index
3367 * and name.
3368 */
3369struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
3370{
3371 struct devlink *devlink;
3372
3373 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
3374 if (!devlink)
3375 return NULL;
3376 devlink->ops = ops;
3377 devlink_net_set(devlink, &init_net);
3378 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02003379 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003380 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003381 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003382 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03003383 INIT_LIST_HEAD(&devlink->region_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003384 mutex_init(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003385 return devlink;
3386}
3387EXPORT_SYMBOL_GPL(devlink_alloc);
3388
3389/**
3390 * devlink_register - Register devlink instance
3391 *
3392 * @devlink: devlink
3393 */
3394int devlink_register(struct devlink *devlink, struct device *dev)
3395{
3396 mutex_lock(&devlink_mutex);
3397 devlink->dev = dev;
3398 list_add_tail(&devlink->list, &devlink_list);
3399 devlink_notify(devlink, DEVLINK_CMD_NEW);
3400 mutex_unlock(&devlink_mutex);
3401 return 0;
3402}
3403EXPORT_SYMBOL_GPL(devlink_register);
3404
3405/**
3406 * devlink_unregister - Unregister devlink instance
3407 *
3408 * @devlink: devlink
3409 */
3410void devlink_unregister(struct devlink *devlink)
3411{
3412 mutex_lock(&devlink_mutex);
3413 devlink_notify(devlink, DEVLINK_CMD_DEL);
3414 list_del(&devlink->list);
3415 mutex_unlock(&devlink_mutex);
3416}
3417EXPORT_SYMBOL_GPL(devlink_unregister);
3418
3419/**
3420 * devlink_free - Free devlink instance resources
3421 *
3422 * @devlink: devlink
3423 */
3424void devlink_free(struct devlink *devlink)
3425{
3426 kfree(devlink);
3427}
3428EXPORT_SYMBOL_GPL(devlink_free);
3429
3430/**
3431 * devlink_port_register - Register devlink port
3432 *
3433 * @devlink: devlink
3434 * @devlink_port: devlink port
3435 * @port_index
3436 *
3437 * Register devlink port with provided port index. User can use
3438 * any indexing, even hw-related one. devlink_port structure
3439 * is convenient to be embedded inside user driver private structure.
3440 * Note that the caller should take care of zeroing the devlink_port
3441 * structure.
3442 */
3443int devlink_port_register(struct devlink *devlink,
3444 struct devlink_port *devlink_port,
3445 unsigned int port_index)
3446{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003447 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003448 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003449 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003450 return -EEXIST;
3451 }
3452 devlink_port->devlink = devlink;
3453 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003454 devlink_port->registered = true;
3455 list_add_tail(&devlink_port->list, &devlink->port_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003456 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003457 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3458 return 0;
3459}
3460EXPORT_SYMBOL_GPL(devlink_port_register);
3461
3462/**
3463 * devlink_port_unregister - Unregister devlink port
3464 *
3465 * @devlink_port: devlink port
3466 */
3467void devlink_port_unregister(struct devlink_port *devlink_port)
3468{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003469 struct devlink *devlink = devlink_port->devlink;
3470
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003471 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003472 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003473 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003474 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003475}
3476EXPORT_SYMBOL_GPL(devlink_port_unregister);
3477
3478static void __devlink_port_type_set(struct devlink_port *devlink_port,
3479 enum devlink_port_type type,
3480 void *type_dev)
3481{
3482 devlink_port->type = type;
3483 devlink_port->type_dev = type_dev;
3484 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3485}
3486
3487/**
3488 * devlink_port_type_eth_set - Set port type to Ethernet
3489 *
3490 * @devlink_port: devlink port
3491 * @netdev: related netdevice
3492 */
3493void devlink_port_type_eth_set(struct devlink_port *devlink_port,
3494 struct net_device *netdev)
3495{
3496 return __devlink_port_type_set(devlink_port,
3497 DEVLINK_PORT_TYPE_ETH, netdev);
3498}
3499EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
3500
3501/**
3502 * devlink_port_type_ib_set - Set port type to InfiniBand
3503 *
3504 * @devlink_port: devlink port
3505 * @ibdev: related IB device
3506 */
3507void devlink_port_type_ib_set(struct devlink_port *devlink_port,
3508 struct ib_device *ibdev)
3509{
3510 return __devlink_port_type_set(devlink_port,
3511 DEVLINK_PORT_TYPE_IB, ibdev);
3512}
3513EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
3514
3515/**
3516 * devlink_port_type_clear - Clear port type
3517 *
3518 * @devlink_port: devlink port
3519 */
3520void devlink_port_type_clear(struct devlink_port *devlink_port)
3521{
3522 return __devlink_port_type_set(devlink_port,
3523 DEVLINK_PORT_TYPE_NOTSET, NULL);
3524}
3525EXPORT_SYMBOL_GPL(devlink_port_type_clear);
3526
3527/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003528 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003529 *
3530 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02003531 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003532 * @port_number: number of the port that is facing user, for example
3533 * the front panel port number
3534 * @split: indicates if this is split port
3535 * @split_subport_number: if the port is split, this is the number
3536 * of subport.
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003537 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003538void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02003539 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003540 u32 port_number, bool split,
3541 u32 split_subport_number)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003542{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003543 struct devlink_port_attrs *attrs = &devlink_port->attrs;
3544
3545 attrs->set = true;
Jiri Pirko5ec13802018-05-18 09:29:01 +02003546 attrs->flavour = flavour;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003547 attrs->port_number = port_number;
3548 attrs->split = split;
3549 attrs->split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003550 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3551}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02003552EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003553
Jiri Pirko08474c12018-05-18 09:29:02 +02003554int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
3555 char *name, size_t len)
3556{
3557 struct devlink_port_attrs *attrs = &devlink_port->attrs;
3558 int n = 0;
3559
3560 if (!attrs->set)
3561 return -EOPNOTSUPP;
3562
3563 switch (attrs->flavour) {
3564 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
3565 if (!attrs->split)
3566 n = snprintf(name, len, "p%u", attrs->port_number);
3567 else
3568 n = snprintf(name, len, "p%us%u", attrs->port_number,
3569 attrs->split_subport_number);
3570 break;
3571 case DEVLINK_PORT_FLAVOUR_CPU:
3572 case DEVLINK_PORT_FLAVOUR_DSA:
3573 /* As CPU and DSA ports do not have a netdevice associated
3574 * case should not ever happen.
3575 */
3576 WARN_ON(1);
3577 return -EINVAL;
3578 }
3579
3580 if (n >= len)
3581 return -EINVAL;
3582
3583 return 0;
3584}
3585EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
3586
Jiri Pirkobf797472016-04-14 18:19:13 +02003587int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
3588 u32 size, u16 ingress_pools_count,
3589 u16 egress_pools_count, u16 ingress_tc_count,
3590 u16 egress_tc_count)
3591{
3592 struct devlink_sb *devlink_sb;
3593 int err = 0;
3594
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003595 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003596 if (devlink_sb_index_exists(devlink, sb_index)) {
3597 err = -EEXIST;
3598 goto unlock;
3599 }
3600
3601 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
3602 if (!devlink_sb) {
3603 err = -ENOMEM;
3604 goto unlock;
3605 }
3606 devlink_sb->index = sb_index;
3607 devlink_sb->size = size;
3608 devlink_sb->ingress_pools_count = ingress_pools_count;
3609 devlink_sb->egress_pools_count = egress_pools_count;
3610 devlink_sb->ingress_tc_count = ingress_tc_count;
3611 devlink_sb->egress_tc_count = egress_tc_count;
3612 list_add_tail(&devlink_sb->list, &devlink->sb_list);
3613unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003614 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003615 return err;
3616}
3617EXPORT_SYMBOL_GPL(devlink_sb_register);
3618
3619void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
3620{
3621 struct devlink_sb *devlink_sb;
3622
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003623 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003624 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
3625 WARN_ON(!devlink_sb);
3626 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003627 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02003628 kfree(devlink_sb);
3629}
3630EXPORT_SYMBOL_GPL(devlink_sb_unregister);
3631
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003632/**
3633 * devlink_dpipe_headers_register - register dpipe headers
3634 *
3635 * @devlink: devlink
3636 * @dpipe_headers: dpipe header array
3637 *
3638 * Register the headers supported by hardware.
3639 */
3640int devlink_dpipe_headers_register(struct devlink *devlink,
3641 struct devlink_dpipe_headers *dpipe_headers)
3642{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003643 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003644 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003645 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003646 return 0;
3647}
3648EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
3649
3650/**
3651 * devlink_dpipe_headers_unregister - unregister dpipe headers
3652 *
3653 * @devlink: devlink
3654 *
3655 * Unregister the headers supported by hardware.
3656 */
3657void devlink_dpipe_headers_unregister(struct devlink *devlink)
3658{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003659 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003660 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003661 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003662}
3663EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
3664
3665/**
3666 * devlink_dpipe_table_counter_enabled - check if counter allocation
3667 * required
3668 * @devlink: devlink
3669 * @table_name: tables name
3670 *
3671 * Used by driver to check if counter allocation is required.
3672 * After counter allocation is turned on the table entries
3673 * are updated to include counter statistics.
3674 *
3675 * After that point on the driver must respect the counter
3676 * state so that each entry added to the table is added
3677 * with a counter.
3678 */
3679bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
3680 const char *table_name)
3681{
3682 struct devlink_dpipe_table *table;
3683 bool enabled;
3684
3685 rcu_read_lock();
3686 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3687 table_name);
3688 enabled = false;
3689 if (table)
3690 enabled = table->counters_enabled;
3691 rcu_read_unlock();
3692 return enabled;
3693}
3694EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
3695
3696/**
3697 * devlink_dpipe_table_register - register dpipe table
3698 *
3699 * @devlink: devlink
3700 * @table_name: table name
3701 * @table_ops: table ops
3702 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003703 * @counter_control_extern: external control for counters
3704 */
3705int devlink_dpipe_table_register(struct devlink *devlink,
3706 const char *table_name,
3707 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02003708 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003709{
3710 struct devlink_dpipe_table *table;
3711
3712 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
3713 return -EEXIST;
3714
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02003715 if (WARN_ON(!table_ops->size_get))
3716 return -EINVAL;
3717
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003718 table = kzalloc(sizeof(*table), GFP_KERNEL);
3719 if (!table)
3720 return -ENOMEM;
3721
3722 table->name = table_name;
3723 table->table_ops = table_ops;
3724 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003725 table->counter_control_extern = counter_control_extern;
3726
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003727 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003728 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003729 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003730 return 0;
3731}
3732EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
3733
3734/**
3735 * devlink_dpipe_table_unregister - unregister dpipe table
3736 *
3737 * @devlink: devlink
3738 * @table_name: table name
3739 */
3740void devlink_dpipe_table_unregister(struct devlink *devlink,
3741 const char *table_name)
3742{
3743 struct devlink_dpipe_table *table;
3744
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003745 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003746 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3747 table_name);
3748 if (!table)
3749 goto unlock;
3750 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003751 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003752 kfree_rcu(table, rcu);
3753 return;
3754unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003755 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003756}
3757EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
3758
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003759/**
3760 * devlink_resource_register - devlink resource register
3761 *
3762 * @devlink: devlink
3763 * @resource_name: resource's name
3764 * @top_hierarchy: top hierarchy
3765 * @reload_required: reload is required for new configuration to
3766 * apply
3767 * @resource_size: resource's size
3768 * @resource_id: resource's id
3769 * @parent_reosurce_id: resource's parent id
3770 * @size params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003771 */
3772int devlink_resource_register(struct devlink *devlink,
3773 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003774 u64 resource_size,
3775 u64 resource_id,
3776 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02003777 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003778{
3779 struct devlink_resource *resource;
3780 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07003781 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003782 int err = 0;
3783
David Ahern14530742018-03-20 19:31:14 -07003784 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
3785
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003786 mutex_lock(&devlink->lock);
3787 resource = devlink_resource_find(devlink, NULL, resource_id);
3788 if (resource) {
3789 err = -EINVAL;
3790 goto out;
3791 }
3792
3793 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
3794 if (!resource) {
3795 err = -ENOMEM;
3796 goto out;
3797 }
3798
3799 if (top_hierarchy) {
3800 resource_list = &devlink->resource_list;
3801 } else {
3802 struct devlink_resource *parent_resource;
3803
3804 parent_resource = devlink_resource_find(devlink, NULL,
3805 parent_resource_id);
3806 if (parent_resource) {
3807 resource_list = &parent_resource->resource_list;
3808 resource->parent = parent_resource;
3809 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00003810 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003811 err = -EINVAL;
3812 goto out;
3813 }
3814 }
3815
3816 resource->name = resource_name;
3817 resource->size = resource_size;
3818 resource->size_new = resource_size;
3819 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003820 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01003821 memcpy(&resource->size_params, size_params,
3822 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003823 INIT_LIST_HEAD(&resource->resource_list);
3824 list_add_tail(&resource->list, resource_list);
3825out:
3826 mutex_unlock(&devlink->lock);
3827 return err;
3828}
3829EXPORT_SYMBOL_GPL(devlink_resource_register);
3830
3831/**
3832 * devlink_resources_unregister - free all resources
3833 *
3834 * @devlink: devlink
3835 * @resource: resource
3836 */
3837void devlink_resources_unregister(struct devlink *devlink,
3838 struct devlink_resource *resource)
3839{
3840 struct devlink_resource *tmp, *child_resource;
3841 struct list_head *resource_list;
3842
3843 if (resource)
3844 resource_list = &resource->resource_list;
3845 else
3846 resource_list = &devlink->resource_list;
3847
3848 if (!resource)
3849 mutex_lock(&devlink->lock);
3850
3851 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
3852 devlink_resources_unregister(devlink, child_resource);
3853 list_del(&child_resource->list);
3854 kfree(child_resource);
3855 }
3856
3857 if (!resource)
3858 mutex_unlock(&devlink->lock);
3859}
3860EXPORT_SYMBOL_GPL(devlink_resources_unregister);
3861
3862/**
3863 * devlink_resource_size_get - get and update size
3864 *
3865 * @devlink: devlink
3866 * @resource_id: the requested resource id
3867 * @p_resource_size: ptr to update
3868 */
3869int devlink_resource_size_get(struct devlink *devlink,
3870 u64 resource_id,
3871 u64 *p_resource_size)
3872{
3873 struct devlink_resource *resource;
3874 int err = 0;
3875
3876 mutex_lock(&devlink->lock);
3877 resource = devlink_resource_find(devlink, NULL, resource_id);
3878 if (!resource) {
3879 err = -EINVAL;
3880 goto out;
3881 }
3882 *p_resource_size = resource->size_new;
3883 resource->size = resource->size_new;
3884out:
3885 mutex_unlock(&devlink->lock);
3886 return err;
3887}
3888EXPORT_SYMBOL_GPL(devlink_resource_size_get);
3889
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01003890/**
3891 * devlink_dpipe_table_resource_set - set the resource id
3892 *
3893 * @devlink: devlink
3894 * @table_name: table name
3895 * @resource_id: resource id
3896 * @resource_units: number of resource's units consumed per table's entry
3897 */
3898int devlink_dpipe_table_resource_set(struct devlink *devlink,
3899 const char *table_name, u64 resource_id,
3900 u64 resource_units)
3901{
3902 struct devlink_dpipe_table *table;
3903 int err = 0;
3904
3905 mutex_lock(&devlink->lock);
3906 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3907 table_name);
3908 if (!table) {
3909 err = -EINVAL;
3910 goto out;
3911 }
3912 table->resource_id = resource_id;
3913 table->resource_units = resource_units;
3914 table->resource_valid = true;
3915out:
3916 mutex_unlock(&devlink->lock);
3917 return err;
3918}
3919EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
3920
Jiri Pirkofc56be42018-04-05 22:13:21 +02003921/**
3922 * devlink_resource_occ_get_register - register occupancy getter
3923 *
3924 * @devlink: devlink
3925 * @resource_id: resource id
3926 * @occ_get: occupancy getter callback
3927 * @occ_get_priv: occupancy getter callback priv
3928 */
3929void devlink_resource_occ_get_register(struct devlink *devlink,
3930 u64 resource_id,
3931 devlink_resource_occ_get_t *occ_get,
3932 void *occ_get_priv)
3933{
3934 struct devlink_resource *resource;
3935
3936 mutex_lock(&devlink->lock);
3937 resource = devlink_resource_find(devlink, NULL, resource_id);
3938 if (WARN_ON(!resource))
3939 goto out;
3940 WARN_ON(resource->occ_get);
3941
3942 resource->occ_get = occ_get;
3943 resource->occ_get_priv = occ_get_priv;
3944out:
3945 mutex_unlock(&devlink->lock);
3946}
3947EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
3948
3949/**
3950 * devlink_resource_occ_get_unregister - unregister occupancy getter
3951 *
3952 * @devlink: devlink
3953 * @resource_id: resource id
3954 */
3955void devlink_resource_occ_get_unregister(struct devlink *devlink,
3956 u64 resource_id)
3957{
3958 struct devlink_resource *resource;
3959
3960 mutex_lock(&devlink->lock);
3961 resource = devlink_resource_find(devlink, NULL, resource_id);
3962 if (WARN_ON(!resource))
3963 goto out;
3964 WARN_ON(!resource->occ_get);
3965
3966 resource->occ_get = NULL;
3967 resource->occ_get_priv = NULL;
3968out:
3969 mutex_unlock(&devlink->lock);
3970}
3971EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
3972
Moshe Shemesheabaef12018-07-04 14:30:28 +03003973/**
3974 * devlink_params_register - register configuration parameters
3975 *
3976 * @devlink: devlink
3977 * @params: configuration parameters array
3978 * @params_count: number of parameters provided
3979 *
3980 * Register the configuration parameters supported by the driver.
3981 */
3982int devlink_params_register(struct devlink *devlink,
3983 const struct devlink_param *params,
3984 size_t params_count)
3985{
3986 const struct devlink_param *param = params;
3987 int i;
3988 int err;
3989
3990 mutex_lock(&devlink->lock);
3991 for (i = 0; i < params_count; i++, param++) {
3992 if (!param || !param->name || !param->supported_cmodes) {
3993 err = -EINVAL;
3994 goto rollback;
3995 }
3996 if (param->generic) {
3997 err = devlink_param_generic_verify(param);
3998 if (err)
3999 goto rollback;
4000 } else {
4001 err = devlink_param_driver_verify(param);
4002 if (err)
4003 goto rollback;
4004 }
4005 err = devlink_param_register_one(devlink, param);
4006 if (err)
4007 goto rollback;
4008 }
4009
4010 mutex_unlock(&devlink->lock);
4011 return 0;
4012
4013rollback:
4014 if (!i)
4015 goto unlock;
4016 for (param--; i > 0; i--, param--)
4017 devlink_param_unregister_one(devlink, param);
4018unlock:
4019 mutex_unlock(&devlink->lock);
4020 return err;
4021}
4022EXPORT_SYMBOL_GPL(devlink_params_register);
4023
4024/**
4025 * devlink_params_unregister - unregister configuration parameters
4026 * @devlink: devlink
4027 * @params: configuration parameters to unregister
4028 * @params_count: number of parameters provided
4029 */
4030void devlink_params_unregister(struct devlink *devlink,
4031 const struct devlink_param *params,
4032 size_t params_count)
4033{
4034 const struct devlink_param *param = params;
4035 int i;
4036
4037 mutex_lock(&devlink->lock);
4038 for (i = 0; i < params_count; i++, param++)
4039 devlink_param_unregister_one(devlink, param);
4040 mutex_unlock(&devlink->lock);
4041}
4042EXPORT_SYMBOL_GPL(devlink_params_unregister);
4043
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03004044/**
4045 * devlink_param_driverinit_value_get - get configuration parameter
4046 * value for driver initializing
4047 *
4048 * @devlink: devlink
4049 * @param_id: parameter ID
4050 * @init_val: value of parameter in driverinit configuration mode
4051 *
4052 * This function should be used by the driver to get driverinit
4053 * configuration for initialization after reload command.
4054 */
4055int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
4056 union devlink_param_value *init_val)
4057{
4058 struct devlink_param_item *param_item;
4059
4060 if (!devlink->ops || !devlink->ops->reload)
4061 return -EOPNOTSUPP;
4062
4063 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4064 if (!param_item)
4065 return -EINVAL;
4066
4067 if (!param_item->driverinit_value_valid ||
4068 !devlink_param_cmode_is_supported(param_item->param,
4069 DEVLINK_PARAM_CMODE_DRIVERINIT))
4070 return -EOPNOTSUPP;
4071
4072 *init_val = param_item->driverinit_value;
4073
4074 return 0;
4075}
4076EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
4077
4078/**
4079 * devlink_param_driverinit_value_set - set value of configuration
4080 * parameter for driverinit
4081 * configuration mode
4082 *
4083 * @devlink: devlink
4084 * @param_id: parameter ID
4085 * @init_val: value of parameter to set for driverinit configuration mode
4086 *
4087 * This function should be used by the driver to set driverinit
4088 * configuration mode default value.
4089 */
4090int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
4091 union devlink_param_value init_val)
4092{
4093 struct devlink_param_item *param_item;
4094
4095 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4096 if (!param_item)
4097 return -EINVAL;
4098
4099 if (!devlink_param_cmode_is_supported(param_item->param,
4100 DEVLINK_PARAM_CMODE_DRIVERINIT))
4101 return -EOPNOTSUPP;
4102
4103 param_item->driverinit_value = init_val;
4104 param_item->driverinit_value_valid = true;
4105
Moshe Shemeshea601e12018-07-04 14:30:32 +03004106 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03004107 return 0;
4108}
4109EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
4110
Moshe Shemeshea601e12018-07-04 14:30:32 +03004111/**
4112 * devlink_param_value_changed - notify devlink on a parameter's value
4113 * change. Should be called by the driver
4114 * right after the change.
4115 *
4116 * @devlink: devlink
4117 * @param_id: parameter ID
4118 *
4119 * This function should be used by the driver to notify devlink on value
4120 * change, excluding driverinit configuration mode.
4121 * For driverinit configuration mode driver should use the function
4122 * devlink_param_driverinit_value_set() instead.
4123 */
4124void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
4125{
4126 struct devlink_param_item *param_item;
4127
4128 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4129 WARN_ON(!param_item);
4130
4131 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4132}
4133EXPORT_SYMBOL_GPL(devlink_param_value_changed);
4134
Alex Veskerb16ebe92018-07-12 15:13:08 +03004135/**
4136 * devlink_region_create - create a new address region
4137 *
4138 * @devlink: devlink
4139 * @region_name: region name
4140 * @region_max_snapshots: Maximum supported number of snapshots for region
4141 * @region_size: size of region
4142 */
4143struct devlink_region *devlink_region_create(struct devlink *devlink,
4144 const char *region_name,
4145 u32 region_max_snapshots,
4146 u64 region_size)
4147{
4148 struct devlink_region *region;
4149 int err = 0;
4150
4151 mutex_lock(&devlink->lock);
4152
4153 if (devlink_region_get_by_name(devlink, region_name)) {
4154 err = -EEXIST;
4155 goto unlock;
4156 }
4157
4158 region = kzalloc(sizeof(*region), GFP_KERNEL);
4159 if (!region) {
4160 err = -ENOMEM;
4161 goto unlock;
4162 }
4163
4164 region->devlink = devlink;
4165 region->max_snapshots = region_max_snapshots;
4166 region->name = region_name;
4167 region->size = region_size;
4168 INIT_LIST_HEAD(&region->snapshot_list);
4169 list_add_tail(&region->list, &devlink->region_list);
4170
4171 mutex_unlock(&devlink->lock);
4172 return region;
4173
4174unlock:
4175 mutex_unlock(&devlink->lock);
4176 return ERR_PTR(err);
4177}
4178EXPORT_SYMBOL_GPL(devlink_region_create);
4179
4180/**
4181 * devlink_region_destroy - destroy address region
4182 *
4183 * @region: devlink region to destroy
4184 */
4185void devlink_region_destroy(struct devlink_region *region)
4186{
4187 struct devlink *devlink = region->devlink;
4188
4189 mutex_lock(&devlink->lock);
4190 list_del(&region->list);
4191 mutex_unlock(&devlink->lock);
4192 kfree(region);
4193}
4194EXPORT_SYMBOL_GPL(devlink_region_destroy);
4195
Alex Veskerccadfa42018-07-12 15:13:09 +03004196/**
4197 * devlink_region_shapshot_id_get - get snapshot ID
4198 *
4199 * This callback should be called when adding a new snapshot,
4200 * Driver should use the same id for multiple snapshots taken
4201 * on multiple regions at the same time/by the same trigger.
4202 *
4203 * @devlink: devlink
4204 */
4205u32 devlink_region_shapshot_id_get(struct devlink *devlink)
4206{
4207 u32 id;
4208
4209 mutex_lock(&devlink->lock);
4210 id = ++devlink->snapshot_id;
4211 mutex_unlock(&devlink->lock);
4212
4213 return id;
4214}
4215EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
4216
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004217static int __init devlink_module_init(void)
4218{
Johannes Berg489111e2016-10-24 14:40:03 +02004219 return genl_register_family(&devlink_nl_family);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004220}
4221
4222static void __exit devlink_module_exit(void)
4223{
4224 genl_unregister_family(&devlink_nl_family);
4225}
4226
4227module_init(devlink_module_init);
4228module_exit(devlink_module_exit);
4229
4230MODULE_LICENSE("GPL v2");
4231MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4232MODULE_DESCRIPTION("Network physical device Netlink interface");
4233MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);