blob: d808af7a5c529c1c51a7b0251d69c01756bf1d2a [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
Alex Veskerd7e52722018-07-12 15:13:10 +0300339struct devlink_snapshot {
340 struct list_head list;
341 struct devlink_region *region;
342 devlink_snapshot_data_dest_t *data_destructor;
343 u64 data_len;
344 u8 *data;
345 u32 id;
346};
347
Alex Veskerb16ebe92018-07-12 15:13:08 +0300348static struct devlink_region *
349devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
350{
351 struct devlink_region *region;
352
353 list_for_each_entry(region, &devlink->region_list, list)
354 if (!strcmp(region->name, region_name))
355 return region;
356
357 return NULL;
358}
359
Alex Veskerd7e52722018-07-12 15:13:10 +0300360static struct devlink_snapshot *
361devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
362{
363 struct devlink_snapshot *snapshot;
364
365 list_for_each_entry(snapshot, &region->snapshot_list, list)
366 if (snapshot->id == id)
367 return snapshot;
368
369 return NULL;
370}
371
372static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
373{
374 snapshot->region->cur_snapshots--;
375 list_del(&snapshot->list);
376 (*snapshot->data_destructor)(snapshot->data);
377 kfree(snapshot);
378}
379
Jiri Pirko1fc22572016-04-08 19:12:48 +0200380#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
381#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200382#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100383
384/* The per devlink instance lock is taken by default in the pre-doit
385 * operation, yet several commands do not require this. The global
386 * devlink lock is taken and protects from disruption by user-calls.
387 */
388#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100389
390static int devlink_nl_pre_doit(const struct genl_ops *ops,
391 struct sk_buff *skb, struct genl_info *info)
392{
393 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100394 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100395
396 mutex_lock(&devlink_mutex);
397 devlink = devlink_get_from_info(info);
398 if (IS_ERR(devlink)) {
399 mutex_unlock(&devlink_mutex);
400 return PTR_ERR(devlink);
401 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100402 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
403 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200404 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
405 info->user_ptr[0] = devlink;
406 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100407 struct devlink_port *devlink_port;
408
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100409 devlink_port = devlink_port_get_from_info(devlink, info);
410 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100411 err = PTR_ERR(devlink_port);
412 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100413 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200414 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100415 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200416 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
417 struct devlink_sb *devlink_sb;
418
419 devlink_sb = devlink_sb_get_from_info(devlink, info);
420 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100421 err = PTR_ERR(devlink_sb);
422 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200423 }
424 info->user_ptr[1] = devlink_sb;
425 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100426 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100427
428unlock:
429 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
430 mutex_unlock(&devlink->lock);
431 mutex_unlock(&devlink_mutex);
432 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100433}
434
435static void devlink_nl_post_doit(const struct genl_ops *ops,
436 struct sk_buff *skb, struct genl_info *info)
437{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100438 struct devlink *devlink;
439
440 devlink = devlink_get_from_info(info);
441 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
442 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100443 mutex_unlock(&devlink_mutex);
444}
445
Johannes Berg489111e2016-10-24 14:40:03 +0200446static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100447
448enum devlink_multicast_groups {
449 DEVLINK_MCGRP_CONFIG,
450};
451
452static const struct genl_multicast_group devlink_nl_mcgrps[] = {
453 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
454};
455
456static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
457{
458 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
459 return -EMSGSIZE;
460 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
461 return -EMSGSIZE;
462 return 0;
463}
464
465static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
466 enum devlink_command cmd, u32 portid,
467 u32 seq, int flags)
468{
469 void *hdr;
470
471 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
472 if (!hdr)
473 return -EMSGSIZE;
474
475 if (devlink_nl_put_handle(msg, devlink))
476 goto nla_put_failure;
477
478 genlmsg_end(msg, hdr);
479 return 0;
480
481nla_put_failure:
482 genlmsg_cancel(msg, hdr);
483 return -EMSGSIZE;
484}
485
486static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
487{
488 struct sk_buff *msg;
489 int err;
490
491 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
492
493 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
494 if (!msg)
495 return;
496
497 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
498 if (err) {
499 nlmsg_free(msg);
500 return;
501 }
502
503 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
504 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
505}
506
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200507static int devlink_nl_port_attrs_put(struct sk_buff *msg,
508 struct devlink_port *devlink_port)
509{
510 struct devlink_port_attrs *attrs = &devlink_port->attrs;
511
512 if (!attrs->set)
513 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200514 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
515 return -EMSGSIZE;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200516 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
517 return -EMSGSIZE;
518 if (!attrs->split)
519 return 0;
520 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
521 return -EMSGSIZE;
522 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
523 attrs->split_subport_number))
524 return -EMSGSIZE;
525 return 0;
526}
527
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100528static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
529 struct devlink_port *devlink_port,
530 enum devlink_command cmd, u32 portid,
531 u32 seq, int flags)
532{
533 void *hdr;
534
535 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
536 if (!hdr)
537 return -EMSGSIZE;
538
539 if (devlink_nl_put_handle(msg, devlink))
540 goto nla_put_failure;
541 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
542 goto nla_put_failure;
543 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
544 goto nla_put_failure;
545 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
546 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
547 devlink_port->desired_type))
548 goto nla_put_failure;
549 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
550 struct net_device *netdev = devlink_port->type_dev;
551
552 if (netdev &&
553 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
554 netdev->ifindex) ||
555 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
556 netdev->name)))
557 goto nla_put_failure;
558 }
559 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
560 struct ib_device *ibdev = devlink_port->type_dev;
561
562 if (ibdev &&
563 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
564 ibdev->name))
565 goto nla_put_failure;
566 }
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200567 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100568 goto nla_put_failure;
569
570 genlmsg_end(msg, hdr);
571 return 0;
572
573nla_put_failure:
574 genlmsg_cancel(msg, hdr);
575 return -EMSGSIZE;
576}
577
578static void devlink_port_notify(struct devlink_port *devlink_port,
579 enum devlink_command cmd)
580{
581 struct devlink *devlink = devlink_port->devlink;
582 struct sk_buff *msg;
583 int err;
584
585 if (!devlink_port->registered)
586 return;
587
588 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
589
590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
591 if (!msg)
592 return;
593
594 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
595 if (err) {
596 nlmsg_free(msg);
597 return;
598 }
599
600 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
601 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
602}
603
604static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
605{
606 struct devlink *devlink = info->user_ptr[0];
607 struct sk_buff *msg;
608 int err;
609
610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
611 if (!msg)
612 return -ENOMEM;
613
614 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
615 info->snd_portid, info->snd_seq, 0);
616 if (err) {
617 nlmsg_free(msg);
618 return err;
619 }
620
621 return genlmsg_reply(msg, info);
622}
623
624static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
625 struct netlink_callback *cb)
626{
627 struct devlink *devlink;
628 int start = cb->args[0];
629 int idx = 0;
630 int err;
631
632 mutex_lock(&devlink_mutex);
633 list_for_each_entry(devlink, &devlink_list, list) {
634 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
635 continue;
636 if (idx < start) {
637 idx++;
638 continue;
639 }
640 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
641 NETLINK_CB(cb->skb).portid,
642 cb->nlh->nlmsg_seq, NLM_F_MULTI);
643 if (err)
644 goto out;
645 idx++;
646 }
647out:
648 mutex_unlock(&devlink_mutex);
649
650 cb->args[0] = idx;
651 return msg->len;
652}
653
654static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
655 struct genl_info *info)
656{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200657 struct devlink_port *devlink_port = info->user_ptr[0];
658 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100659 struct sk_buff *msg;
660 int err;
661
662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
663 if (!msg)
664 return -ENOMEM;
665
666 err = devlink_nl_port_fill(msg, devlink, devlink_port,
667 DEVLINK_CMD_PORT_NEW,
668 info->snd_portid, info->snd_seq, 0);
669 if (err) {
670 nlmsg_free(msg);
671 return err;
672 }
673
674 return genlmsg_reply(msg, info);
675}
676
677static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
678 struct netlink_callback *cb)
679{
680 struct devlink *devlink;
681 struct devlink_port *devlink_port;
682 int start = cb->args[0];
683 int idx = 0;
684 int err;
685
686 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100687 list_for_each_entry(devlink, &devlink_list, list) {
688 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
689 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100690 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100691 list_for_each_entry(devlink_port, &devlink->port_list, list) {
692 if (idx < start) {
693 idx++;
694 continue;
695 }
696 err = devlink_nl_port_fill(msg, devlink, devlink_port,
697 DEVLINK_CMD_NEW,
698 NETLINK_CB(cb->skb).portid,
699 cb->nlh->nlmsg_seq,
700 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100701 if (err) {
702 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100703 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100704 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100705 idx++;
706 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100707 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100708 }
709out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100710 mutex_unlock(&devlink_mutex);
711
712 cb->args[0] = idx;
713 return msg->len;
714}
715
716static int devlink_port_type_set(struct devlink *devlink,
717 struct devlink_port *devlink_port,
718 enum devlink_port_type port_type)
719
720{
721 int err;
722
723 if (devlink->ops && devlink->ops->port_type_set) {
724 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
725 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200726 if (port_type == devlink_port->type)
727 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100728 err = devlink->ops->port_type_set(devlink_port, port_type);
729 if (err)
730 return err;
731 devlink_port->desired_type = port_type;
732 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
733 return 0;
734 }
735 return -EOPNOTSUPP;
736}
737
738static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
739 struct genl_info *info)
740{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200741 struct devlink_port *devlink_port = info->user_ptr[0];
742 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100743 int err;
744
745 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
746 enum devlink_port_type port_type;
747
748 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
749 err = devlink_port_type_set(devlink, devlink_port, port_type);
750 if (err)
751 return err;
752 }
753 return 0;
754}
755
David Ahernac0fc8a2018-06-05 08:14:09 -0700756static int devlink_port_split(struct devlink *devlink, u32 port_index,
757 u32 count, struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100758
759{
760 if (devlink->ops && devlink->ops->port_split)
David Ahernac0fc8a2018-06-05 08:14:09 -0700761 return devlink->ops->port_split(devlink, port_index, count,
762 extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100763 return -EOPNOTSUPP;
764}
765
766static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
767 struct genl_info *info)
768{
769 struct devlink *devlink = info->user_ptr[0];
770 u32 port_index;
771 u32 count;
772
773 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
774 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
775 return -EINVAL;
776
777 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
778 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700779 return devlink_port_split(devlink, port_index, count, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100780}
781
David Ahernac0fc8a2018-06-05 08:14:09 -0700782static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
783 struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100784
785{
786 if (devlink->ops && devlink->ops->port_unsplit)
David Ahernac0fc8a2018-06-05 08:14:09 -0700787 return devlink->ops->port_unsplit(devlink, port_index, extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100788 return -EOPNOTSUPP;
789}
790
791static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
792 struct genl_info *info)
793{
794 struct devlink *devlink = info->user_ptr[0];
795 u32 port_index;
796
797 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
798 return -EINVAL;
799
800 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700801 return devlink_port_unsplit(devlink, port_index, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100802}
803
Jiri Pirkobf797472016-04-14 18:19:13 +0200804static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
805 struct devlink_sb *devlink_sb,
806 enum devlink_command cmd, u32 portid,
807 u32 seq, int flags)
808{
809 void *hdr;
810
811 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
812 if (!hdr)
813 return -EMSGSIZE;
814
815 if (devlink_nl_put_handle(msg, devlink))
816 goto nla_put_failure;
817 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
818 goto nla_put_failure;
819 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
820 goto nla_put_failure;
821 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
822 devlink_sb->ingress_pools_count))
823 goto nla_put_failure;
824 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
825 devlink_sb->egress_pools_count))
826 goto nla_put_failure;
827 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
828 devlink_sb->ingress_tc_count))
829 goto nla_put_failure;
830 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
831 devlink_sb->egress_tc_count))
832 goto nla_put_failure;
833
834 genlmsg_end(msg, hdr);
835 return 0;
836
837nla_put_failure:
838 genlmsg_cancel(msg, hdr);
839 return -EMSGSIZE;
840}
841
842static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
843 struct genl_info *info)
844{
845 struct devlink *devlink = info->user_ptr[0];
846 struct devlink_sb *devlink_sb = info->user_ptr[1];
847 struct sk_buff *msg;
848 int err;
849
850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
851 if (!msg)
852 return -ENOMEM;
853
854 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
855 DEVLINK_CMD_SB_NEW,
856 info->snd_portid, info->snd_seq, 0);
857 if (err) {
858 nlmsg_free(msg);
859 return err;
860 }
861
862 return genlmsg_reply(msg, info);
863}
864
865static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
866 struct netlink_callback *cb)
867{
868 struct devlink *devlink;
869 struct devlink_sb *devlink_sb;
870 int start = cb->args[0];
871 int idx = 0;
872 int err;
873
874 mutex_lock(&devlink_mutex);
875 list_for_each_entry(devlink, &devlink_list, list) {
876 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
877 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100878 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200879 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
880 if (idx < start) {
881 idx++;
882 continue;
883 }
884 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
885 DEVLINK_CMD_SB_NEW,
886 NETLINK_CB(cb->skb).portid,
887 cb->nlh->nlmsg_seq,
888 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100889 if (err) {
890 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200891 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100892 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200893 idx++;
894 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100895 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200896 }
897out:
898 mutex_unlock(&devlink_mutex);
899
900 cb->args[0] = idx;
901 return msg->len;
902}
903
904static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
905 struct devlink_sb *devlink_sb,
906 u16 pool_index, enum devlink_command cmd,
907 u32 portid, u32 seq, int flags)
908{
909 struct devlink_sb_pool_info pool_info;
910 void *hdr;
911 int err;
912
913 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
914 pool_index, &pool_info);
915 if (err)
916 return err;
917
918 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
919 if (!hdr)
920 return -EMSGSIZE;
921
922 if (devlink_nl_put_handle(msg, devlink))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
925 goto nla_put_failure;
926 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
927 goto nla_put_failure;
928 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
929 goto nla_put_failure;
930 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
931 goto nla_put_failure;
932 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
933 pool_info.threshold_type))
934 goto nla_put_failure;
935
936 genlmsg_end(msg, hdr);
937 return 0;
938
939nla_put_failure:
940 genlmsg_cancel(msg, hdr);
941 return -EMSGSIZE;
942}
943
944static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
945 struct genl_info *info)
946{
947 struct devlink *devlink = info->user_ptr[0];
948 struct devlink_sb *devlink_sb = info->user_ptr[1];
949 struct sk_buff *msg;
950 u16 pool_index;
951 int err;
952
953 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
954 &pool_index);
955 if (err)
956 return err;
957
958 if (!devlink->ops || !devlink->ops->sb_pool_get)
959 return -EOPNOTSUPP;
960
961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
962 if (!msg)
963 return -ENOMEM;
964
965 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
966 DEVLINK_CMD_SB_POOL_NEW,
967 info->snd_portid, info->snd_seq, 0);
968 if (err) {
969 nlmsg_free(msg);
970 return err;
971 }
972
973 return genlmsg_reply(msg, info);
974}
975
976static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
977 struct devlink *devlink,
978 struct devlink_sb *devlink_sb,
979 u32 portid, u32 seq)
980{
981 u16 pool_count = devlink_sb_pool_count(devlink_sb);
982 u16 pool_index;
983 int err;
984
985 for (pool_index = 0; pool_index < pool_count; pool_index++) {
986 if (*p_idx < start) {
987 (*p_idx)++;
988 continue;
989 }
990 err = devlink_nl_sb_pool_fill(msg, devlink,
991 devlink_sb,
992 pool_index,
993 DEVLINK_CMD_SB_POOL_NEW,
994 portid, seq, NLM_F_MULTI);
995 if (err)
996 return err;
997 (*p_idx)++;
998 }
999 return 0;
1000}
1001
1002static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1003 struct netlink_callback *cb)
1004{
1005 struct devlink *devlink;
1006 struct devlink_sb *devlink_sb;
1007 int start = cb->args[0];
1008 int idx = 0;
1009 int err;
1010
1011 mutex_lock(&devlink_mutex);
1012 list_for_each_entry(devlink, &devlink_list, list) {
1013 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1014 !devlink->ops || !devlink->ops->sb_pool_get)
1015 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001016 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001017 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1018 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1019 devlink_sb,
1020 NETLINK_CB(cb->skb).portid,
1021 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001022 if (err && err != -EOPNOTSUPP) {
1023 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001024 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001025 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001026 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001027 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001028 }
1029out:
1030 mutex_unlock(&devlink_mutex);
1031
1032 cb->args[0] = idx;
1033 return msg->len;
1034}
1035
1036static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1037 u16 pool_index, u32 size,
1038 enum devlink_sb_threshold_type threshold_type)
1039
1040{
1041 const struct devlink_ops *ops = devlink->ops;
1042
1043 if (ops && ops->sb_pool_set)
1044 return ops->sb_pool_set(devlink, sb_index, pool_index,
1045 size, threshold_type);
1046 return -EOPNOTSUPP;
1047}
1048
1049static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1050 struct genl_info *info)
1051{
1052 struct devlink *devlink = info->user_ptr[0];
1053 struct devlink_sb *devlink_sb = info->user_ptr[1];
1054 enum devlink_sb_threshold_type threshold_type;
1055 u16 pool_index;
1056 u32 size;
1057 int err;
1058
1059 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1060 &pool_index);
1061 if (err)
1062 return err;
1063
1064 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1065 if (err)
1066 return err;
1067
1068 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1069 return -EINVAL;
1070
1071 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1072 return devlink_sb_pool_set(devlink, devlink_sb->index,
1073 pool_index, size, threshold_type);
1074}
1075
1076static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1077 struct devlink *devlink,
1078 struct devlink_port *devlink_port,
1079 struct devlink_sb *devlink_sb,
1080 u16 pool_index,
1081 enum devlink_command cmd,
1082 u32 portid, u32 seq, int flags)
1083{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001084 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001085 u32 threshold;
1086 void *hdr;
1087 int err;
1088
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001089 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1090 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001091 if (err)
1092 return err;
1093
1094 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1095 if (!hdr)
1096 return -EMSGSIZE;
1097
1098 if (devlink_nl_put_handle(msg, devlink))
1099 goto nla_put_failure;
1100 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1101 goto nla_put_failure;
1102 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1103 goto nla_put_failure;
1104 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1105 goto nla_put_failure;
1106 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1107 goto nla_put_failure;
1108
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001109 if (ops->sb_occ_port_pool_get) {
1110 u32 cur;
1111 u32 max;
1112
1113 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1114 pool_index, &cur, &max);
1115 if (err && err != -EOPNOTSUPP)
1116 return err;
1117 if (!err) {
1118 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1119 goto nla_put_failure;
1120 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1121 goto nla_put_failure;
1122 }
1123 }
1124
Jiri Pirkobf797472016-04-14 18:19:13 +02001125 genlmsg_end(msg, hdr);
1126 return 0;
1127
1128nla_put_failure:
1129 genlmsg_cancel(msg, hdr);
1130 return -EMSGSIZE;
1131}
1132
1133static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1134 struct genl_info *info)
1135{
1136 struct devlink_port *devlink_port = info->user_ptr[0];
1137 struct devlink *devlink = devlink_port->devlink;
1138 struct devlink_sb *devlink_sb = info->user_ptr[1];
1139 struct sk_buff *msg;
1140 u16 pool_index;
1141 int err;
1142
1143 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1144 &pool_index);
1145 if (err)
1146 return err;
1147
1148 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1149 return -EOPNOTSUPP;
1150
1151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1152 if (!msg)
1153 return -ENOMEM;
1154
1155 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1156 devlink_sb, pool_index,
1157 DEVLINK_CMD_SB_PORT_POOL_NEW,
1158 info->snd_portid, info->snd_seq, 0);
1159 if (err) {
1160 nlmsg_free(msg);
1161 return err;
1162 }
1163
1164 return genlmsg_reply(msg, info);
1165}
1166
1167static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1168 struct devlink *devlink,
1169 struct devlink_sb *devlink_sb,
1170 u32 portid, u32 seq)
1171{
1172 struct devlink_port *devlink_port;
1173 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1174 u16 pool_index;
1175 int err;
1176
1177 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1178 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1179 if (*p_idx < start) {
1180 (*p_idx)++;
1181 continue;
1182 }
1183 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1184 devlink_port,
1185 devlink_sb,
1186 pool_index,
1187 DEVLINK_CMD_SB_PORT_POOL_NEW,
1188 portid, seq,
1189 NLM_F_MULTI);
1190 if (err)
1191 return err;
1192 (*p_idx)++;
1193 }
1194 }
1195 return 0;
1196}
1197
1198static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1199 struct netlink_callback *cb)
1200{
1201 struct devlink *devlink;
1202 struct devlink_sb *devlink_sb;
1203 int start = cb->args[0];
1204 int idx = 0;
1205 int err;
1206
1207 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001208 list_for_each_entry(devlink, &devlink_list, list) {
1209 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1210 !devlink->ops || !devlink->ops->sb_port_pool_get)
1211 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001212 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001213 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1214 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1215 devlink, devlink_sb,
1216 NETLINK_CB(cb->skb).portid,
1217 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001218 if (err && err != -EOPNOTSUPP) {
1219 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001220 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001221 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001222 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001223 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001224 }
1225out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001226 mutex_unlock(&devlink_mutex);
1227
1228 cb->args[0] = idx;
1229 return msg->len;
1230}
1231
1232static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1233 unsigned int sb_index, u16 pool_index,
1234 u32 threshold)
1235
1236{
1237 const struct devlink_ops *ops = devlink_port->devlink->ops;
1238
1239 if (ops && ops->sb_port_pool_set)
1240 return ops->sb_port_pool_set(devlink_port, sb_index,
1241 pool_index, threshold);
1242 return -EOPNOTSUPP;
1243}
1244
1245static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1246 struct genl_info *info)
1247{
1248 struct devlink_port *devlink_port = info->user_ptr[0];
1249 struct devlink_sb *devlink_sb = info->user_ptr[1];
1250 u16 pool_index;
1251 u32 threshold;
1252 int err;
1253
1254 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1255 &pool_index);
1256 if (err)
1257 return err;
1258
1259 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1260 return -EINVAL;
1261
1262 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1263 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1264 pool_index, threshold);
1265}
1266
1267static int
1268devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1269 struct devlink_port *devlink_port,
1270 struct devlink_sb *devlink_sb, u16 tc_index,
1271 enum devlink_sb_pool_type pool_type,
1272 enum devlink_command cmd,
1273 u32 portid, u32 seq, int flags)
1274{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001275 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001276 u16 pool_index;
1277 u32 threshold;
1278 void *hdr;
1279 int err;
1280
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001281 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1282 tc_index, pool_type,
1283 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001284 if (err)
1285 return err;
1286
1287 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1288 if (!hdr)
1289 return -EMSGSIZE;
1290
1291 if (devlink_nl_put_handle(msg, devlink))
1292 goto nla_put_failure;
1293 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1294 goto nla_put_failure;
1295 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1296 goto nla_put_failure;
1297 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1298 goto nla_put_failure;
1299 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1300 goto nla_put_failure;
1301 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1302 goto nla_put_failure;
1303 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1304 goto nla_put_failure;
1305
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001306 if (ops->sb_occ_tc_port_bind_get) {
1307 u32 cur;
1308 u32 max;
1309
1310 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1311 devlink_sb->index,
1312 tc_index, pool_type,
1313 &cur, &max);
1314 if (err && err != -EOPNOTSUPP)
1315 return err;
1316 if (!err) {
1317 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1318 goto nla_put_failure;
1319 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1320 goto nla_put_failure;
1321 }
1322 }
1323
Jiri Pirkobf797472016-04-14 18:19:13 +02001324 genlmsg_end(msg, hdr);
1325 return 0;
1326
1327nla_put_failure:
1328 genlmsg_cancel(msg, hdr);
1329 return -EMSGSIZE;
1330}
1331
1332static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1333 struct genl_info *info)
1334{
1335 struct devlink_port *devlink_port = info->user_ptr[0];
1336 struct devlink *devlink = devlink_port->devlink;
1337 struct devlink_sb *devlink_sb = info->user_ptr[1];
1338 struct sk_buff *msg;
1339 enum devlink_sb_pool_type pool_type;
1340 u16 tc_index;
1341 int err;
1342
1343 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1344 if (err)
1345 return err;
1346
1347 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1348 pool_type, &tc_index);
1349 if (err)
1350 return err;
1351
1352 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1353 return -EOPNOTSUPP;
1354
1355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1356 if (!msg)
1357 return -ENOMEM;
1358
1359 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1360 devlink_sb, tc_index, pool_type,
1361 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1362 info->snd_portid,
1363 info->snd_seq, 0);
1364 if (err) {
1365 nlmsg_free(msg);
1366 return err;
1367 }
1368
1369 return genlmsg_reply(msg, info);
1370}
1371
1372static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1373 int start, int *p_idx,
1374 struct devlink *devlink,
1375 struct devlink_sb *devlink_sb,
1376 u32 portid, u32 seq)
1377{
1378 struct devlink_port *devlink_port;
1379 u16 tc_index;
1380 int err;
1381
1382 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1383 for (tc_index = 0;
1384 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1385 if (*p_idx < start) {
1386 (*p_idx)++;
1387 continue;
1388 }
1389 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1390 devlink_port,
1391 devlink_sb,
1392 tc_index,
1393 DEVLINK_SB_POOL_TYPE_INGRESS,
1394 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1395 portid, seq,
1396 NLM_F_MULTI);
1397 if (err)
1398 return err;
1399 (*p_idx)++;
1400 }
1401 for (tc_index = 0;
1402 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1403 if (*p_idx < start) {
1404 (*p_idx)++;
1405 continue;
1406 }
1407 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1408 devlink_port,
1409 devlink_sb,
1410 tc_index,
1411 DEVLINK_SB_POOL_TYPE_EGRESS,
1412 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1413 portid, seq,
1414 NLM_F_MULTI);
1415 if (err)
1416 return err;
1417 (*p_idx)++;
1418 }
1419 }
1420 return 0;
1421}
1422
1423static int
1424devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1425 struct netlink_callback *cb)
1426{
1427 struct devlink *devlink;
1428 struct devlink_sb *devlink_sb;
1429 int start = cb->args[0];
1430 int idx = 0;
1431 int err;
1432
1433 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001434 list_for_each_entry(devlink, &devlink_list, list) {
1435 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1436 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1437 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001438
1439 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001440 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1441 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1442 devlink,
1443 devlink_sb,
1444 NETLINK_CB(cb->skb).portid,
1445 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001446 if (err && err != -EOPNOTSUPP) {
1447 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001448 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001449 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001450 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001451 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001452 }
1453out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001454 mutex_unlock(&devlink_mutex);
1455
1456 cb->args[0] = idx;
1457 return msg->len;
1458}
1459
1460static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1461 unsigned int sb_index, u16 tc_index,
1462 enum devlink_sb_pool_type pool_type,
1463 u16 pool_index, u32 threshold)
1464
1465{
1466 const struct devlink_ops *ops = devlink_port->devlink->ops;
1467
1468 if (ops && ops->sb_tc_pool_bind_set)
1469 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1470 tc_index, pool_type,
1471 pool_index, threshold);
1472 return -EOPNOTSUPP;
1473}
1474
1475static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1476 struct genl_info *info)
1477{
1478 struct devlink_port *devlink_port = info->user_ptr[0];
1479 struct devlink_sb *devlink_sb = info->user_ptr[1];
1480 enum devlink_sb_pool_type pool_type;
1481 u16 tc_index;
1482 u16 pool_index;
1483 u32 threshold;
1484 int err;
1485
1486 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1487 if (err)
1488 return err;
1489
1490 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1491 pool_type, &tc_index);
1492 if (err)
1493 return err;
1494
1495 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1496 &pool_index);
1497 if (err)
1498 return err;
1499
1500 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1501 return -EINVAL;
1502
1503 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1504 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1505 tc_index, pool_type,
1506 pool_index, threshold);
1507}
1508
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001509static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1510 struct genl_info *info)
1511{
1512 struct devlink *devlink = info->user_ptr[0];
1513 struct devlink_sb *devlink_sb = info->user_ptr[1];
1514 const struct devlink_ops *ops = devlink->ops;
1515
1516 if (ops && ops->sb_occ_snapshot)
1517 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1518 return -EOPNOTSUPP;
1519}
1520
1521static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1522 struct genl_info *info)
1523{
1524 struct devlink *devlink = info->user_ptr[0];
1525 struct devlink_sb *devlink_sb = info->user_ptr[1];
1526 const struct devlink_ops *ops = devlink->ops;
1527
1528 if (ops && ops->sb_occ_max_clear)
1529 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1530 return -EOPNOTSUPP;
1531}
1532
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001533static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1534 enum devlink_command cmd, u32 portid,
1535 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001536{
Roi Dayan59bfde02016-11-22 23:09:57 +02001537 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001538 u8 inline_mode, encap_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001539 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001540 int err = 0;
1541 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001542
1543 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1544 if (!hdr)
1545 return -EMSGSIZE;
1546
Roi Dayan59bfde02016-11-22 23:09:57 +02001547 err = devlink_nl_put_handle(msg, devlink);
1548 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001549 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001550
Jiri Pirko4456f612017-02-09 15:54:36 +01001551 if (ops->eswitch_mode_get) {
1552 err = ops->eswitch_mode_get(devlink, &mode);
1553 if (err)
1554 goto nla_put_failure;
1555 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1556 if (err)
1557 goto nla_put_failure;
1558 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001559
1560 if (ops->eswitch_inline_mode_get) {
1561 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1562 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001563 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001564 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1565 inline_mode);
1566 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001567 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001568 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001569
Roi Dayanf43e9b02016-09-25 13:52:44 +03001570 if (ops->eswitch_encap_mode_get) {
1571 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1572 if (err)
1573 goto nla_put_failure;
1574 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1575 if (err)
1576 goto nla_put_failure;
1577 }
1578
Or Gerlitz08f4b592016-07-01 14:51:01 +03001579 genlmsg_end(msg, hdr);
1580 return 0;
1581
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001582nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001583 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001584 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001585}
1586
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001587static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1588 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001589{
1590 struct devlink *devlink = info->user_ptr[0];
1591 const struct devlink_ops *ops = devlink->ops;
1592 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001593 int err;
1594
Jiri Pirko4456f612017-02-09 15:54:36 +01001595 if (!ops)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001596 return -EOPNOTSUPP;
1597
Or Gerlitz08f4b592016-07-01 14:51:01 +03001598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1599 if (!msg)
1600 return -ENOMEM;
1601
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001602 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1603 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001604
1605 if (err) {
1606 nlmsg_free(msg);
1607 return err;
1608 }
1609
1610 return genlmsg_reply(msg, info);
1611}
1612
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001613static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1614 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001615{
1616 struct devlink *devlink = info->user_ptr[0];
1617 const struct devlink_ops *ops = devlink->ops;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001618 u8 inline_mode, encap_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001619 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001620 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001621
Roi Dayan59bfde02016-11-22 23:09:57 +02001622 if (!ops)
1623 return -EOPNOTSUPP;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001624
Roi Dayan59bfde02016-11-22 23:09:57 +02001625 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1626 if (!ops->eswitch_mode_set)
1627 return -EOPNOTSUPP;
1628 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1629 err = ops->eswitch_mode_set(devlink, mode);
1630 if (err)
1631 return err;
1632 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001633
Roi Dayan59bfde02016-11-22 23:09:57 +02001634 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1635 if (!ops->eswitch_inline_mode_set)
1636 return -EOPNOTSUPP;
1637 inline_mode = nla_get_u8(
1638 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1639 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1640 if (err)
1641 return err;
1642 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001643
1644 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1645 if (!ops->eswitch_encap_mode_set)
1646 return -EOPNOTSUPP;
1647 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1648 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1649 if (err)
1650 return err;
1651 }
1652
Roi Dayan59bfde02016-11-22 23:09:57 +02001653 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001654}
1655
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001656int devlink_dpipe_match_put(struct sk_buff *skb,
1657 struct devlink_dpipe_match *match)
1658{
1659 struct devlink_dpipe_header *header = match->header;
1660 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1661 struct nlattr *match_attr;
1662
1663 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1664 if (!match_attr)
1665 return -EMSGSIZE;
1666
1667 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1668 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1669 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1670 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1671 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1672 goto nla_put_failure;
1673
1674 nla_nest_end(skb, match_attr);
1675 return 0;
1676
1677nla_put_failure:
1678 nla_nest_cancel(skb, match_attr);
1679 return -EMSGSIZE;
1680}
1681EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1682
1683static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1684 struct sk_buff *skb)
1685{
1686 struct nlattr *matches_attr;
1687
1688 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1689 if (!matches_attr)
1690 return -EMSGSIZE;
1691
1692 if (table->table_ops->matches_dump(table->priv, skb))
1693 goto nla_put_failure;
1694
1695 nla_nest_end(skb, matches_attr);
1696 return 0;
1697
1698nla_put_failure:
1699 nla_nest_cancel(skb, matches_attr);
1700 return -EMSGSIZE;
1701}
1702
1703int devlink_dpipe_action_put(struct sk_buff *skb,
1704 struct devlink_dpipe_action *action)
1705{
1706 struct devlink_dpipe_header *header = action->header;
1707 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1708 struct nlattr *action_attr;
1709
1710 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1711 if (!action_attr)
1712 return -EMSGSIZE;
1713
1714 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1715 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1716 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1717 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1718 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1719 goto nla_put_failure;
1720
1721 nla_nest_end(skb, action_attr);
1722 return 0;
1723
1724nla_put_failure:
1725 nla_nest_cancel(skb, action_attr);
1726 return -EMSGSIZE;
1727}
1728EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1729
1730static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1731 struct sk_buff *skb)
1732{
1733 struct nlattr *actions_attr;
1734
1735 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1736 if (!actions_attr)
1737 return -EMSGSIZE;
1738
1739 if (table->table_ops->actions_dump(table->priv, skb))
1740 goto nla_put_failure;
1741
1742 nla_nest_end(skb, actions_attr);
1743 return 0;
1744
1745nla_put_failure:
1746 nla_nest_cancel(skb, actions_attr);
1747 return -EMSGSIZE;
1748}
1749
1750static int devlink_dpipe_table_put(struct sk_buff *skb,
1751 struct devlink_dpipe_table *table)
1752{
1753 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001754 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001755
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001756 table_size = table->table_ops->size_get(table->priv);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001757 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1758 if (!table_attr)
1759 return -EMSGSIZE;
1760
1761 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001762 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001763 DEVLINK_ATTR_PAD))
1764 goto nla_put_failure;
1765 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1766 table->counters_enabled))
1767 goto nla_put_failure;
1768
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001769 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001770 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1771 table->resource_id, DEVLINK_ATTR_PAD) ||
1772 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1773 table->resource_units, DEVLINK_ATTR_PAD))
1774 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001775 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001776 if (devlink_dpipe_matches_put(table, skb))
1777 goto nla_put_failure;
1778
1779 if (devlink_dpipe_actions_put(table, skb))
1780 goto nla_put_failure;
1781
1782 nla_nest_end(skb, table_attr);
1783 return 0;
1784
1785nla_put_failure:
1786 nla_nest_cancel(skb, table_attr);
1787 return -EMSGSIZE;
1788}
1789
1790static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1791 struct genl_info *info)
1792{
1793 int err;
1794
1795 if (*pskb) {
1796 err = genlmsg_reply(*pskb, info);
1797 if (err)
1798 return err;
1799 }
1800 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1801 if (!*pskb)
1802 return -ENOMEM;
1803 return 0;
1804}
1805
1806static int devlink_dpipe_tables_fill(struct genl_info *info,
1807 enum devlink_command cmd, int flags,
1808 struct list_head *dpipe_tables,
1809 const char *table_name)
1810{
1811 struct devlink *devlink = info->user_ptr[0];
1812 struct devlink_dpipe_table *table;
1813 struct nlattr *tables_attr;
1814 struct sk_buff *skb = NULL;
1815 struct nlmsghdr *nlh;
1816 bool incomplete;
1817 void *hdr;
1818 int i;
1819 int err;
1820
1821 table = list_first_entry(dpipe_tables,
1822 struct devlink_dpipe_table, list);
1823start_again:
1824 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1825 if (err)
1826 return err;
1827
1828 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1829 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001830 if (!hdr) {
1831 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001832 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001833 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001834
1835 if (devlink_nl_put_handle(skb, devlink))
1836 goto nla_put_failure;
1837 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1838 if (!tables_attr)
1839 goto nla_put_failure;
1840
1841 i = 0;
1842 incomplete = false;
1843 list_for_each_entry_from(table, dpipe_tables, list) {
1844 if (!table_name) {
1845 err = devlink_dpipe_table_put(skb, table);
1846 if (err) {
1847 if (!i)
1848 goto err_table_put;
1849 incomplete = true;
1850 break;
1851 }
1852 } else {
1853 if (!strcmp(table->name, table_name)) {
1854 err = devlink_dpipe_table_put(skb, table);
1855 if (err)
1856 break;
1857 }
1858 }
1859 i++;
1860 }
1861
1862 nla_nest_end(skb, tables_attr);
1863 genlmsg_end(skb, hdr);
1864 if (incomplete)
1865 goto start_again;
1866
1867send_done:
1868 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1869 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1870 if (!nlh) {
1871 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1872 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001873 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001874 goto send_done;
1875 }
1876
1877 return genlmsg_reply(skb, info);
1878
1879nla_put_failure:
1880 err = -EMSGSIZE;
1881err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001882 nlmsg_free(skb);
1883 return err;
1884}
1885
1886static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1887 struct genl_info *info)
1888{
1889 struct devlink *devlink = info->user_ptr[0];
1890 const char *table_name = NULL;
1891
1892 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1893 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1894
1895 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1896 &devlink->dpipe_table_list,
1897 table_name);
1898}
1899
1900static int devlink_dpipe_value_put(struct sk_buff *skb,
1901 struct devlink_dpipe_value *value)
1902{
1903 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1904 value->value_size, value->value))
1905 return -EMSGSIZE;
1906 if (value->mask)
1907 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1908 value->value_size, value->mask))
1909 return -EMSGSIZE;
1910 if (value->mapping_valid)
1911 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1912 value->mapping_value))
1913 return -EMSGSIZE;
1914 return 0;
1915}
1916
1917static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1918 struct devlink_dpipe_value *value)
1919{
1920 if (!value->action)
1921 return -EINVAL;
1922 if (devlink_dpipe_action_put(skb, value->action))
1923 return -EMSGSIZE;
1924 if (devlink_dpipe_value_put(skb, value))
1925 return -EMSGSIZE;
1926 return 0;
1927}
1928
1929static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1930 struct devlink_dpipe_value *values,
1931 unsigned int values_count)
1932{
1933 struct nlattr *action_attr;
1934 int i;
1935 int err;
1936
1937 for (i = 0; i < values_count; i++) {
1938 action_attr = nla_nest_start(skb,
1939 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1940 if (!action_attr)
1941 return -EMSGSIZE;
1942 err = devlink_dpipe_action_value_put(skb, &values[i]);
1943 if (err)
1944 goto err_action_value_put;
1945 nla_nest_end(skb, action_attr);
1946 }
1947 return 0;
1948
1949err_action_value_put:
1950 nla_nest_cancel(skb, action_attr);
1951 return err;
1952}
1953
1954static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1955 struct devlink_dpipe_value *value)
1956{
1957 if (!value->match)
1958 return -EINVAL;
1959 if (devlink_dpipe_match_put(skb, value->match))
1960 return -EMSGSIZE;
1961 if (devlink_dpipe_value_put(skb, value))
1962 return -EMSGSIZE;
1963 return 0;
1964}
1965
1966static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1967 struct devlink_dpipe_value *values,
1968 unsigned int values_count)
1969{
1970 struct nlattr *match_attr;
1971 int i;
1972 int err;
1973
1974 for (i = 0; i < values_count; i++) {
1975 match_attr = nla_nest_start(skb,
1976 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1977 if (!match_attr)
1978 return -EMSGSIZE;
1979 err = devlink_dpipe_match_value_put(skb, &values[i]);
1980 if (err)
1981 goto err_match_value_put;
1982 nla_nest_end(skb, match_attr);
1983 }
1984 return 0;
1985
1986err_match_value_put:
1987 nla_nest_cancel(skb, match_attr);
1988 return err;
1989}
1990
1991static int devlink_dpipe_entry_put(struct sk_buff *skb,
1992 struct devlink_dpipe_entry *entry)
1993{
1994 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1995 int err;
1996
1997 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1998 if (!entry_attr)
1999 return -EMSGSIZE;
2000
2001 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2002 DEVLINK_ATTR_PAD))
2003 goto nla_put_failure;
2004 if (entry->counter_valid)
2005 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2006 entry->counter, DEVLINK_ATTR_PAD))
2007 goto nla_put_failure;
2008
2009 matches_attr = nla_nest_start(skb,
2010 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2011 if (!matches_attr)
2012 goto nla_put_failure;
2013
2014 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2015 entry->match_values_count);
2016 if (err) {
2017 nla_nest_cancel(skb, matches_attr);
2018 goto err_match_values_put;
2019 }
2020 nla_nest_end(skb, matches_attr);
2021
2022 actions_attr = nla_nest_start(skb,
2023 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2024 if (!actions_attr)
2025 goto nla_put_failure;
2026
2027 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2028 entry->action_values_count);
2029 if (err) {
2030 nla_nest_cancel(skb, actions_attr);
2031 goto err_action_values_put;
2032 }
2033 nla_nest_end(skb, actions_attr);
2034
2035 nla_nest_end(skb, entry_attr);
2036 return 0;
2037
2038nla_put_failure:
2039 err = -EMSGSIZE;
2040err_match_values_put:
2041err_action_values_put:
2042 nla_nest_cancel(skb, entry_attr);
2043 return err;
2044}
2045
2046static struct devlink_dpipe_table *
2047devlink_dpipe_table_find(struct list_head *dpipe_tables,
2048 const char *table_name)
2049{
2050 struct devlink_dpipe_table *table;
2051
2052 list_for_each_entry_rcu(table, dpipe_tables, list) {
2053 if (!strcmp(table->name, table_name))
2054 return table;
2055 }
2056 return NULL;
2057}
2058
2059int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2060{
2061 struct devlink *devlink;
2062 int err;
2063
2064 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2065 dump_ctx->info);
2066 if (err)
2067 return err;
2068
2069 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2070 dump_ctx->info->snd_portid,
2071 dump_ctx->info->snd_seq,
2072 &devlink_nl_family, NLM_F_MULTI,
2073 dump_ctx->cmd);
2074 if (!dump_ctx->hdr)
2075 goto nla_put_failure;
2076
2077 devlink = dump_ctx->info->user_ptr[0];
2078 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2079 goto nla_put_failure;
2080 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2081 DEVLINK_ATTR_DPIPE_ENTRIES);
2082 if (!dump_ctx->nest)
2083 goto nla_put_failure;
2084 return 0;
2085
2086nla_put_failure:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002087 nlmsg_free(dump_ctx->skb);
2088 return -EMSGSIZE;
2089}
2090EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2091
2092int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2093 struct devlink_dpipe_entry *entry)
2094{
2095 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2096}
2097EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2098
2099int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2100{
2101 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2102 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2106
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002107void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2108
2109{
2110 unsigned int value_count, value_index;
2111 struct devlink_dpipe_value *value;
2112
2113 value = entry->action_values;
2114 value_count = entry->action_values_count;
2115 for (value_index = 0; value_index < value_count; value_index++) {
2116 kfree(value[value_index].value);
2117 kfree(value[value_index].mask);
2118 }
2119
2120 value = entry->match_values;
2121 value_count = entry->match_values_count;
2122 for (value_index = 0; value_index < value_count; value_index++) {
2123 kfree(value[value_index].value);
2124 kfree(value[value_index].mask);
2125 }
2126}
2127EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2128
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002129static int devlink_dpipe_entries_fill(struct genl_info *info,
2130 enum devlink_command cmd, int flags,
2131 struct devlink_dpipe_table *table)
2132{
2133 struct devlink_dpipe_dump_ctx dump_ctx;
2134 struct nlmsghdr *nlh;
2135 int err;
2136
2137 dump_ctx.skb = NULL;
2138 dump_ctx.cmd = cmd;
2139 dump_ctx.info = info;
2140
2141 err = table->table_ops->entries_dump(table->priv,
2142 table->counters_enabled,
2143 &dump_ctx);
2144 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002145 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002146
2147send_done:
2148 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2149 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2150 if (!nlh) {
2151 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2152 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002153 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002154 goto send_done;
2155 }
2156 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002157}
2158
2159static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2160 struct genl_info *info)
2161{
2162 struct devlink *devlink = info->user_ptr[0];
2163 struct devlink_dpipe_table *table;
2164 const char *table_name;
2165
2166 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2167 return -EINVAL;
2168
2169 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2170 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2171 table_name);
2172 if (!table)
2173 return -EINVAL;
2174
2175 if (!table->table_ops->entries_dump)
2176 return -EINVAL;
2177
2178 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2179 0, table);
2180}
2181
2182static int devlink_dpipe_fields_put(struct sk_buff *skb,
2183 const struct devlink_dpipe_header *header)
2184{
2185 struct devlink_dpipe_field *field;
2186 struct nlattr *field_attr;
2187 int i;
2188
2189 for (i = 0; i < header->fields_count; i++) {
2190 field = &header->fields[i];
2191 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2192 if (!field_attr)
2193 return -EMSGSIZE;
2194 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2195 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2196 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2197 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2198 goto nla_put_failure;
2199 nla_nest_end(skb, field_attr);
2200 }
2201 return 0;
2202
2203nla_put_failure:
2204 nla_nest_cancel(skb, field_attr);
2205 return -EMSGSIZE;
2206}
2207
2208static int devlink_dpipe_header_put(struct sk_buff *skb,
2209 struct devlink_dpipe_header *header)
2210{
2211 struct nlattr *fields_attr, *header_attr;
2212 int err;
2213
2214 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002215 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002216 return -EMSGSIZE;
2217
2218 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2219 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2220 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2221 goto nla_put_failure;
2222
2223 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2224 if (!fields_attr)
2225 goto nla_put_failure;
2226
2227 err = devlink_dpipe_fields_put(skb, header);
2228 if (err) {
2229 nla_nest_cancel(skb, fields_attr);
2230 goto nla_put_failure;
2231 }
2232 nla_nest_end(skb, fields_attr);
2233 nla_nest_end(skb, header_attr);
2234 return 0;
2235
2236nla_put_failure:
2237 err = -EMSGSIZE;
2238 nla_nest_cancel(skb, header_attr);
2239 return err;
2240}
2241
2242static int devlink_dpipe_headers_fill(struct genl_info *info,
2243 enum devlink_command cmd, int flags,
2244 struct devlink_dpipe_headers *
2245 dpipe_headers)
2246{
2247 struct devlink *devlink = info->user_ptr[0];
2248 struct nlattr *headers_attr;
2249 struct sk_buff *skb = NULL;
2250 struct nlmsghdr *nlh;
2251 void *hdr;
2252 int i, j;
2253 int err;
2254
2255 i = 0;
2256start_again:
2257 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2258 if (err)
2259 return err;
2260
2261 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2262 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002263 if (!hdr) {
2264 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002265 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002266 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002267
2268 if (devlink_nl_put_handle(skb, devlink))
2269 goto nla_put_failure;
2270 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2271 if (!headers_attr)
2272 goto nla_put_failure;
2273
2274 j = 0;
2275 for (; i < dpipe_headers->headers_count; i++) {
2276 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2277 if (err) {
2278 if (!j)
2279 goto err_table_put;
2280 break;
2281 }
2282 j++;
2283 }
2284 nla_nest_end(skb, headers_attr);
2285 genlmsg_end(skb, hdr);
2286 if (i != dpipe_headers->headers_count)
2287 goto start_again;
2288
2289send_done:
2290 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2291 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2292 if (!nlh) {
2293 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2294 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002295 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002296 goto send_done;
2297 }
2298 return genlmsg_reply(skb, info);
2299
2300nla_put_failure:
2301 err = -EMSGSIZE;
2302err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002303 nlmsg_free(skb);
2304 return err;
2305}
2306
2307static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2308 struct genl_info *info)
2309{
2310 struct devlink *devlink = info->user_ptr[0];
2311
2312 if (!devlink->dpipe_headers)
2313 return -EOPNOTSUPP;
2314 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2315 0, devlink->dpipe_headers);
2316}
2317
2318static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2319 const char *table_name,
2320 bool enable)
2321{
2322 struct devlink_dpipe_table *table;
2323
2324 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2325 table_name);
2326 if (!table)
2327 return -EINVAL;
2328
2329 if (table->counter_control_extern)
2330 return -EOPNOTSUPP;
2331
2332 if (!(table->counters_enabled ^ enable))
2333 return 0;
2334
2335 table->counters_enabled = enable;
2336 if (table->table_ops->counters_set_update)
2337 table->table_ops->counters_set_update(table->priv, enable);
2338 return 0;
2339}
2340
2341static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2342 struct genl_info *info)
2343{
2344 struct devlink *devlink = info->user_ptr[0];
2345 const char *table_name;
2346 bool counters_enable;
2347
2348 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2349 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2350 return -EINVAL;
2351
2352 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2353 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2354
2355 return devlink_dpipe_table_counters_set(devlink, table_name,
2356 counters_enable);
2357}
2358
Wei Yongjun43dd7512018-01-17 03:27:42 +00002359static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002360devlink_resource_find(struct devlink *devlink,
2361 struct devlink_resource *resource, u64 resource_id)
2362{
2363 struct list_head *resource_list;
2364
2365 if (resource)
2366 resource_list = &resource->resource_list;
2367 else
2368 resource_list = &devlink->resource_list;
2369
2370 list_for_each_entry(resource, resource_list, list) {
2371 struct devlink_resource *child_resource;
2372
2373 if (resource->id == resource_id)
2374 return resource;
2375
2376 child_resource = devlink_resource_find(devlink, resource,
2377 resource_id);
2378 if (child_resource)
2379 return child_resource;
2380 }
2381 return NULL;
2382}
2383
Wei Yongjun43dd7512018-01-17 03:27:42 +00002384static void
2385devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002386{
2387 struct devlink_resource *child_resource;
2388 bool size_valid = true;
2389 u64 parts_size = 0;
2390
2391 if (list_empty(&resource->resource_list))
2392 goto out;
2393
2394 list_for_each_entry(child_resource, &resource->resource_list, list)
2395 parts_size += child_resource->size_new;
2396
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002397 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002398 size_valid = false;
2399out:
2400 resource->size_valid = size_valid;
2401}
2402
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002403static int
2404devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2405 struct netlink_ext_ack *extack)
2406{
2407 u64 reminder;
2408 int err = 0;
2409
David S. Miller0f3e9c92018-03-06 00:53:44 -05002410 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002411 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2412 err = -EINVAL;
2413 }
2414
David S. Miller0f3e9c92018-03-06 00:53:44 -05002415 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002416 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2417 err = -EINVAL;
2418 }
2419
David S. Miller0f3e9c92018-03-06 00:53:44 -05002420 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002421 if (reminder) {
2422 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2423 err = -EINVAL;
2424 }
2425
2426 return err;
2427}
2428
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002429static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2430 struct genl_info *info)
2431{
2432 struct devlink *devlink = info->user_ptr[0];
2433 struct devlink_resource *resource;
2434 u64 resource_id;
2435 u64 size;
2436 int err;
2437
2438 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2439 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2440 return -EINVAL;
2441 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2442
2443 resource = devlink_resource_find(devlink, NULL, resource_id);
2444 if (!resource)
2445 return -EINVAL;
2446
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002447 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002448 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002449 if (err)
2450 return err;
2451
2452 resource->size_new = size;
2453 devlink_resource_validate_children(resource);
2454 if (resource->parent)
2455 devlink_resource_validate_children(resource->parent);
2456 return 0;
2457}
2458
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002459static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002460devlink_resource_size_params_put(struct devlink_resource *resource,
2461 struct sk_buff *skb)
2462{
2463 struct devlink_resource_size_params *size_params;
2464
Jiri Pirko77d27092018-02-28 13:12:09 +01002465 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002466 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2467 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2468 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2469 size_params->size_max, DEVLINK_ATTR_PAD) ||
2470 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2471 size_params->size_min, DEVLINK_ATTR_PAD) ||
2472 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2473 return -EMSGSIZE;
2474 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002475}
2476
Jiri Pirkofc56be42018-04-05 22:13:21 +02002477static int devlink_resource_occ_put(struct devlink_resource *resource,
2478 struct sk_buff *skb)
2479{
2480 if (!resource->occ_get)
2481 return 0;
2482 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2483 resource->occ_get(resource->occ_get_priv),
2484 DEVLINK_ATTR_PAD);
2485}
2486
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002487static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2488 struct devlink_resource *resource)
2489{
2490 struct devlink_resource *child_resource;
2491 struct nlattr *child_resource_attr;
2492 struct nlattr *resource_attr;
2493
2494 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2495 if (!resource_attr)
2496 return -EMSGSIZE;
2497
2498 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2499 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2500 DEVLINK_ATTR_PAD) ||
2501 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2502 DEVLINK_ATTR_PAD))
2503 goto nla_put_failure;
2504 if (resource->size != resource->size_new)
2505 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2506 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002507 if (devlink_resource_occ_put(resource, skb))
2508 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002509 if (devlink_resource_size_params_put(resource, skb))
2510 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002511 if (list_empty(&resource->resource_list))
2512 goto out;
2513
2514 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2515 resource->size_valid))
2516 goto nla_put_failure;
2517
2518 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2519 if (!child_resource_attr)
2520 goto nla_put_failure;
2521
2522 list_for_each_entry(child_resource, &resource->resource_list, list) {
2523 if (devlink_resource_put(devlink, skb, child_resource))
2524 goto resource_put_failure;
2525 }
2526
2527 nla_nest_end(skb, child_resource_attr);
2528out:
2529 nla_nest_end(skb, resource_attr);
2530 return 0;
2531
2532resource_put_failure:
2533 nla_nest_cancel(skb, child_resource_attr);
2534nla_put_failure:
2535 nla_nest_cancel(skb, resource_attr);
2536 return -EMSGSIZE;
2537}
2538
2539static int devlink_resource_fill(struct genl_info *info,
2540 enum devlink_command cmd, int flags)
2541{
2542 struct devlink *devlink = info->user_ptr[0];
2543 struct devlink_resource *resource;
2544 struct nlattr *resources_attr;
2545 struct sk_buff *skb = NULL;
2546 struct nlmsghdr *nlh;
2547 bool incomplete;
2548 void *hdr;
2549 int i;
2550 int err;
2551
2552 resource = list_first_entry(&devlink->resource_list,
2553 struct devlink_resource, list);
2554start_again:
2555 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2556 if (err)
2557 return err;
2558
2559 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2560 &devlink_nl_family, NLM_F_MULTI, cmd);
2561 if (!hdr) {
2562 nlmsg_free(skb);
2563 return -EMSGSIZE;
2564 }
2565
2566 if (devlink_nl_put_handle(skb, devlink))
2567 goto nla_put_failure;
2568
2569 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2570 if (!resources_attr)
2571 goto nla_put_failure;
2572
2573 incomplete = false;
2574 i = 0;
2575 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2576 err = devlink_resource_put(devlink, skb, resource);
2577 if (err) {
2578 if (!i)
2579 goto err_resource_put;
2580 incomplete = true;
2581 break;
2582 }
2583 i++;
2584 }
2585 nla_nest_end(skb, resources_attr);
2586 genlmsg_end(skb, hdr);
2587 if (incomplete)
2588 goto start_again;
2589send_done:
2590 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2591 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2592 if (!nlh) {
2593 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2594 if (err)
Dan Carpenter83fe9a92018-09-21 11:07:55 +03002595 return err;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002596 goto send_done;
2597 }
2598 return genlmsg_reply(skb, info);
2599
2600nla_put_failure:
2601 err = -EMSGSIZE;
2602err_resource_put:
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002603 nlmsg_free(skb);
2604 return err;
2605}
2606
2607static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2608 struct genl_info *info)
2609{
2610 struct devlink *devlink = info->user_ptr[0];
2611
2612 if (list_empty(&devlink->resource_list))
2613 return -EOPNOTSUPP;
2614
2615 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2616}
2617
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002618static int
2619devlink_resources_validate(struct devlink *devlink,
2620 struct devlink_resource *resource,
2621 struct genl_info *info)
2622{
2623 struct list_head *resource_list;
2624 int err = 0;
2625
2626 if (resource)
2627 resource_list = &resource->resource_list;
2628 else
2629 resource_list = &devlink->resource_list;
2630
2631 list_for_each_entry(resource, resource_list, list) {
2632 if (!resource->size_valid)
2633 return -EINVAL;
2634 err = devlink_resources_validate(devlink, resource, info);
2635 if (err)
2636 return err;
2637 }
2638 return err;
2639}
2640
2641static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2642{
2643 struct devlink *devlink = info->user_ptr[0];
2644 int err;
2645
2646 if (!devlink->ops->reload)
2647 return -EOPNOTSUPP;
2648
2649 err = devlink_resources_validate(devlink, NULL, info);
2650 if (err) {
2651 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2652 return err;
2653 }
David Ahernac0fc8a2018-06-05 08:14:09 -07002654 return devlink->ops->reload(devlink, info->extack);
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002655}
2656
Moshe Shemesh036467c2018-07-04 14:30:33 +03002657static const struct devlink_param devlink_param_generic[] = {
2658 {
2659 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2660 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2661 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2662 },
2663 {
2664 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2665 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2666 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2667 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002668 {
2669 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2670 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2671 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2672 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002673 {
2674 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2675 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2676 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2677 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03002678};
Moshe Shemesheabaef12018-07-04 14:30:28 +03002679
2680static int devlink_param_generic_verify(const struct devlink_param *param)
2681{
2682 /* verify it match generic parameter by id and name */
2683 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2684 return -EINVAL;
2685 if (strcmp(param->name, devlink_param_generic[param->id].name))
2686 return -ENOENT;
2687
2688 WARN_ON(param->type != devlink_param_generic[param->id].type);
2689
2690 return 0;
2691}
2692
2693static int devlink_param_driver_verify(const struct devlink_param *param)
2694{
2695 int i;
2696
2697 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2698 return -EINVAL;
2699 /* verify no such name in generic params */
2700 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2701 if (!strcmp(param->name, devlink_param_generic[i].name))
2702 return -EEXIST;
2703
2704 return 0;
2705}
2706
2707static struct devlink_param_item *
2708devlink_param_find_by_name(struct list_head *param_list,
2709 const char *param_name)
2710{
2711 struct devlink_param_item *param_item;
2712
2713 list_for_each_entry(param_item, param_list, list)
2714 if (!strcmp(param_item->param->name, param_name))
2715 return param_item;
2716 return NULL;
2717}
2718
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03002719static struct devlink_param_item *
2720devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2721{
2722 struct devlink_param_item *param_item;
2723
2724 list_for_each_entry(param_item, param_list, list)
2725 if (param_item->param->id == param_id)
2726 return param_item;
2727 return NULL;
2728}
2729
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002730static bool
2731devlink_param_cmode_is_supported(const struct devlink_param *param,
2732 enum devlink_param_cmode cmode)
2733{
2734 return test_bit(cmode, &param->supported_cmodes);
2735}
2736
2737static int devlink_param_get(struct devlink *devlink,
2738 const struct devlink_param *param,
2739 struct devlink_param_gset_ctx *ctx)
2740{
2741 if (!param->get)
2742 return -EOPNOTSUPP;
2743 return param->get(devlink, param->id, ctx);
2744}
2745
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002746static int devlink_param_set(struct devlink *devlink,
2747 const struct devlink_param *param,
2748 struct devlink_param_gset_ctx *ctx)
2749{
2750 if (!param->set)
2751 return -EOPNOTSUPP;
2752 return param->set(devlink, param->id, ctx);
2753}
2754
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002755static int
2756devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2757{
2758 switch (param_type) {
2759 case DEVLINK_PARAM_TYPE_U8:
2760 return NLA_U8;
2761 case DEVLINK_PARAM_TYPE_U16:
2762 return NLA_U16;
2763 case DEVLINK_PARAM_TYPE_U32:
2764 return NLA_U32;
2765 case DEVLINK_PARAM_TYPE_STRING:
2766 return NLA_STRING;
2767 case DEVLINK_PARAM_TYPE_BOOL:
2768 return NLA_FLAG;
2769 default:
2770 return -EINVAL;
2771 }
2772}
2773
2774static int
2775devlink_nl_param_value_fill_one(struct sk_buff *msg,
2776 enum devlink_param_type type,
2777 enum devlink_param_cmode cmode,
2778 union devlink_param_value val)
2779{
2780 struct nlattr *param_value_attr;
2781
2782 param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2783 if (!param_value_attr)
2784 goto nla_put_failure;
2785
2786 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2787 goto value_nest_cancel;
2788
2789 switch (type) {
2790 case DEVLINK_PARAM_TYPE_U8:
2791 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2792 goto value_nest_cancel;
2793 break;
2794 case DEVLINK_PARAM_TYPE_U16:
2795 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2796 goto value_nest_cancel;
2797 break;
2798 case DEVLINK_PARAM_TYPE_U32:
2799 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2800 goto value_nest_cancel;
2801 break;
2802 case DEVLINK_PARAM_TYPE_STRING:
2803 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2804 val.vstr))
2805 goto value_nest_cancel;
2806 break;
2807 case DEVLINK_PARAM_TYPE_BOOL:
2808 if (val.vbool &&
2809 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2810 goto value_nest_cancel;
2811 break;
2812 }
2813
2814 nla_nest_end(msg, param_value_attr);
2815 return 0;
2816
2817value_nest_cancel:
2818 nla_nest_cancel(msg, param_value_attr);
2819nla_put_failure:
2820 return -EMSGSIZE;
2821}
2822
2823static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2824 struct devlink_param_item *param_item,
2825 enum devlink_command cmd,
2826 u32 portid, u32 seq, int flags)
2827{
2828 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2829 const struct devlink_param *param = param_item->param;
2830 struct devlink_param_gset_ctx ctx;
2831 struct nlattr *param_values_list;
2832 struct nlattr *param_attr;
2833 int nla_type;
2834 void *hdr;
2835 int err;
2836 int i;
2837
2838 /* Get value from driver part to driverinit configuration mode */
2839 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2840 if (!devlink_param_cmode_is_supported(param, i))
2841 continue;
2842 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2843 if (!param_item->driverinit_value_valid)
2844 return -EOPNOTSUPP;
2845 param_value[i] = param_item->driverinit_value;
2846 } else {
2847 ctx.cmode = i;
2848 err = devlink_param_get(devlink, param, &ctx);
2849 if (err)
2850 return err;
2851 param_value[i] = ctx.val;
2852 }
2853 }
2854
2855 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2856 if (!hdr)
2857 return -EMSGSIZE;
2858
2859 if (devlink_nl_put_handle(msg, devlink))
2860 goto genlmsg_cancel;
2861 param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2862 if (!param_attr)
2863 goto genlmsg_cancel;
2864 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2865 goto param_nest_cancel;
2866 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2867 goto param_nest_cancel;
2868
2869 nla_type = devlink_param_type_to_nla_type(param->type);
2870 if (nla_type < 0)
2871 goto param_nest_cancel;
2872 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2873 goto param_nest_cancel;
2874
2875 param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2876 if (!param_values_list)
2877 goto param_nest_cancel;
2878
2879 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2880 if (!devlink_param_cmode_is_supported(param, i))
2881 continue;
2882 err = devlink_nl_param_value_fill_one(msg, param->type,
2883 i, param_value[i]);
2884 if (err)
2885 goto values_list_nest_cancel;
2886 }
2887
2888 nla_nest_end(msg, param_values_list);
2889 nla_nest_end(msg, param_attr);
2890 genlmsg_end(msg, hdr);
2891 return 0;
2892
2893values_list_nest_cancel:
2894 nla_nest_end(msg, param_values_list);
2895param_nest_cancel:
2896 nla_nest_cancel(msg, param_attr);
2897genlmsg_cancel:
2898 genlmsg_cancel(msg, hdr);
2899 return -EMSGSIZE;
2900}
2901
Moshe Shemeshea601e12018-07-04 14:30:32 +03002902static void devlink_param_notify(struct devlink *devlink,
2903 struct devlink_param_item *param_item,
2904 enum devlink_command cmd)
2905{
2906 struct sk_buff *msg;
2907 int err;
2908
2909 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
2910
2911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2912 if (!msg)
2913 return;
2914 err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
2915 if (err) {
2916 nlmsg_free(msg);
2917 return;
2918 }
2919
2920 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2921 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2922}
2923
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002924static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2925 struct netlink_callback *cb)
2926{
2927 struct devlink_param_item *param_item;
2928 struct devlink *devlink;
2929 int start = cb->args[0];
2930 int idx = 0;
2931 int err;
2932
2933 mutex_lock(&devlink_mutex);
2934 list_for_each_entry(devlink, &devlink_list, list) {
2935 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2936 continue;
2937 mutex_lock(&devlink->lock);
2938 list_for_each_entry(param_item, &devlink->param_list, list) {
2939 if (idx < start) {
2940 idx++;
2941 continue;
2942 }
2943 err = devlink_nl_param_fill(msg, devlink, param_item,
2944 DEVLINK_CMD_PARAM_GET,
2945 NETLINK_CB(cb->skb).portid,
2946 cb->nlh->nlmsg_seq,
2947 NLM_F_MULTI);
2948 if (err) {
2949 mutex_unlock(&devlink->lock);
2950 goto out;
2951 }
2952 idx++;
2953 }
2954 mutex_unlock(&devlink->lock);
2955 }
2956out:
2957 mutex_unlock(&devlink_mutex);
2958
2959 cb->args[0] = idx;
2960 return msg->len;
2961}
2962
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002963static int
2964devlink_param_type_get_from_info(struct genl_info *info,
2965 enum devlink_param_type *param_type)
2966{
2967 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
2968 return -EINVAL;
2969
2970 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
2971 case NLA_U8:
2972 *param_type = DEVLINK_PARAM_TYPE_U8;
2973 break;
2974 case NLA_U16:
2975 *param_type = DEVLINK_PARAM_TYPE_U16;
2976 break;
2977 case NLA_U32:
2978 *param_type = DEVLINK_PARAM_TYPE_U32;
2979 break;
2980 case NLA_STRING:
2981 *param_type = DEVLINK_PARAM_TYPE_STRING;
2982 break;
2983 case NLA_FLAG:
2984 *param_type = DEVLINK_PARAM_TYPE_BOOL;
2985 break;
2986 default:
2987 return -EINVAL;
2988 }
2989
2990 return 0;
2991}
2992
2993static int
2994devlink_param_value_get_from_info(const struct devlink_param *param,
2995 struct genl_info *info,
2996 union devlink_param_value *value)
2997{
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03002998 int len;
2999
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003000 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3001 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3002 return -EINVAL;
3003
3004 switch (param->type) {
3005 case DEVLINK_PARAM_TYPE_U8:
3006 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3007 break;
3008 case DEVLINK_PARAM_TYPE_U16:
3009 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3010 break;
3011 case DEVLINK_PARAM_TYPE_U32:
3012 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3013 break;
3014 case DEVLINK_PARAM_TYPE_STRING:
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003015 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3016 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3017 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3018 len >= DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003019 return -EINVAL;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003020 strcpy(value->vstr,
3021 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003022 break;
3023 case DEVLINK_PARAM_TYPE_BOOL:
3024 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3025 true : false;
3026 break;
3027 }
3028 return 0;
3029}
3030
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003031static struct devlink_param_item *
3032devlink_param_get_from_info(struct devlink *devlink,
3033 struct genl_info *info)
3034{
3035 char *param_name;
3036
3037 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3038 return NULL;
3039
3040 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3041 return devlink_param_find_by_name(&devlink->param_list, param_name);
3042}
3043
3044static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3045 struct genl_info *info)
3046{
3047 struct devlink *devlink = info->user_ptr[0];
3048 struct devlink_param_item *param_item;
3049 struct sk_buff *msg;
3050 int err;
3051
3052 param_item = devlink_param_get_from_info(devlink, info);
3053 if (!param_item)
3054 return -EINVAL;
3055
3056 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3057 if (!msg)
3058 return -ENOMEM;
3059
3060 err = devlink_nl_param_fill(msg, devlink, param_item,
3061 DEVLINK_CMD_PARAM_GET,
3062 info->snd_portid, info->snd_seq, 0);
3063 if (err) {
3064 nlmsg_free(msg);
3065 return err;
3066 }
3067
3068 return genlmsg_reply(msg, info);
3069}
3070
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003071static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3072 struct genl_info *info)
3073{
3074 struct devlink *devlink = info->user_ptr[0];
3075 enum devlink_param_type param_type;
3076 struct devlink_param_gset_ctx ctx;
3077 enum devlink_param_cmode cmode;
3078 struct devlink_param_item *param_item;
3079 const struct devlink_param *param;
3080 union devlink_param_value value;
3081 int err = 0;
3082
3083 param_item = devlink_param_get_from_info(devlink, info);
3084 if (!param_item)
3085 return -EINVAL;
3086 param = param_item->param;
3087 err = devlink_param_type_get_from_info(info, &param_type);
3088 if (err)
3089 return err;
3090 if (param_type != param->type)
3091 return -EINVAL;
3092 err = devlink_param_value_get_from_info(param, info, &value);
3093 if (err)
3094 return err;
3095 if (param->validate) {
3096 err = param->validate(devlink, param->id, value, info->extack);
3097 if (err)
3098 return err;
3099 }
3100
3101 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3102 return -EINVAL;
3103 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3104 if (!devlink_param_cmode_is_supported(param, cmode))
3105 return -EOPNOTSUPP;
3106
3107 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3108 param_item->driverinit_value = value;
3109 param_item->driverinit_value_valid = true;
3110 } else {
3111 if (!param->set)
3112 return -EOPNOTSUPP;
3113 ctx.val = value;
3114 ctx.cmode = cmode;
3115 err = devlink_param_set(devlink, param, &ctx);
3116 if (err)
3117 return err;
3118 }
3119
Moshe Shemeshea601e12018-07-04 14:30:32 +03003120 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003121 return 0;
3122}
3123
Moshe Shemesheabaef12018-07-04 14:30:28 +03003124static int devlink_param_register_one(struct devlink *devlink,
3125 const struct devlink_param *param)
3126{
3127 struct devlink_param_item *param_item;
3128
3129 if (devlink_param_find_by_name(&devlink->param_list,
3130 param->name))
3131 return -EEXIST;
3132
3133 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3134 WARN_ON(param->get || param->set);
3135 else
3136 WARN_ON(!param->get || !param->set);
3137
3138 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3139 if (!param_item)
3140 return -ENOMEM;
3141 param_item->param = param;
3142
3143 list_add_tail(&param_item->list, &devlink->param_list);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003144 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003145 return 0;
3146}
3147
3148static void devlink_param_unregister_one(struct devlink *devlink,
3149 const struct devlink_param *param)
3150{
3151 struct devlink_param_item *param_item;
3152
3153 param_item = devlink_param_find_by_name(&devlink->param_list,
3154 param->name);
3155 WARN_ON(!param_item);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003156 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003157 list_del(&param_item->list);
3158 kfree(param_item);
3159}
3160
Alex Veskera006d462018-07-12 15:13:12 +03003161static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3162 struct devlink *devlink,
3163 struct devlink_snapshot *snapshot)
3164{
3165 struct nlattr *snap_attr;
3166 int err;
3167
3168 snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3169 if (!snap_attr)
3170 return -EINVAL;
3171
3172 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3173 if (err)
3174 goto nla_put_failure;
3175
3176 nla_nest_end(msg, snap_attr);
3177 return 0;
3178
3179nla_put_failure:
3180 nla_nest_cancel(msg, snap_attr);
3181 return err;
3182}
3183
3184static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3185 struct devlink *devlink,
3186 struct devlink_region *region)
3187{
3188 struct devlink_snapshot *snapshot;
3189 struct nlattr *snapshots_attr;
3190 int err;
3191
3192 snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
3193 if (!snapshots_attr)
3194 return -EINVAL;
3195
3196 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3197 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3198 if (err)
3199 goto nla_put_failure;
3200 }
3201
3202 nla_nest_end(msg, snapshots_attr);
3203 return 0;
3204
3205nla_put_failure:
3206 nla_nest_cancel(msg, snapshots_attr);
3207 return err;
3208}
3209
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003210static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3211 enum devlink_command cmd, u32 portid,
3212 u32 seq, int flags,
3213 struct devlink_region *region)
3214{
3215 void *hdr;
3216 int err;
3217
3218 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3219 if (!hdr)
3220 return -EMSGSIZE;
3221
3222 err = devlink_nl_put_handle(msg, devlink);
3223 if (err)
3224 goto nla_put_failure;
3225
3226 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3227 if (err)
3228 goto nla_put_failure;
3229
3230 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3231 region->size,
3232 DEVLINK_ATTR_PAD);
3233 if (err)
3234 goto nla_put_failure;
3235
Alex Veskera006d462018-07-12 15:13:12 +03003236 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3237 if (err)
3238 goto nla_put_failure;
3239
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003240 genlmsg_end(msg, hdr);
3241 return 0;
3242
3243nla_put_failure:
3244 genlmsg_cancel(msg, hdr);
3245 return err;
3246}
3247
Alex Vesker866319b2018-07-12 15:13:13 +03003248static void devlink_nl_region_notify(struct devlink_region *region,
3249 struct devlink_snapshot *snapshot,
3250 enum devlink_command cmd)
3251{
3252 struct devlink *devlink = region->devlink;
3253 struct sk_buff *msg;
3254 void *hdr;
3255 int err;
3256
3257 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3258
3259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3260 if (!msg)
3261 return;
3262
3263 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3264 if (!hdr)
3265 goto out_free_msg;
3266
3267 err = devlink_nl_put_handle(msg, devlink);
3268 if (err)
3269 goto out_cancel_msg;
3270
3271 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3272 region->name);
3273 if (err)
3274 goto out_cancel_msg;
3275
3276 if (snapshot) {
3277 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3278 snapshot->id);
3279 if (err)
3280 goto out_cancel_msg;
3281 } else {
3282 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3283 region->size, DEVLINK_ATTR_PAD);
3284 if (err)
3285 goto out_cancel_msg;
3286 }
3287 genlmsg_end(msg, hdr);
3288
3289 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3290 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3291
3292 return;
3293
3294out_cancel_msg:
3295 genlmsg_cancel(msg, hdr);
3296out_free_msg:
3297 nlmsg_free(msg);
3298}
3299
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003300static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3301 struct genl_info *info)
3302{
3303 struct devlink *devlink = info->user_ptr[0];
3304 struct devlink_region *region;
3305 const char *region_name;
3306 struct sk_buff *msg;
3307 int err;
3308
3309 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3310 return -EINVAL;
3311
3312 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3313 region = devlink_region_get_by_name(devlink, region_name);
3314 if (!region)
3315 return -EINVAL;
3316
3317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3318 if (!msg)
3319 return -ENOMEM;
3320
3321 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3322 info->snd_portid, info->snd_seq, 0,
3323 region);
3324 if (err) {
3325 nlmsg_free(msg);
3326 return err;
3327 }
3328
3329 return genlmsg_reply(msg, info);
3330}
3331
3332static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3333 struct netlink_callback *cb)
3334{
3335 struct devlink_region *region;
3336 struct devlink *devlink;
3337 int start = cb->args[0];
3338 int idx = 0;
3339 int err;
3340
3341 mutex_lock(&devlink_mutex);
3342 list_for_each_entry(devlink, &devlink_list, list) {
3343 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3344 continue;
3345
3346 mutex_lock(&devlink->lock);
3347 list_for_each_entry(region, &devlink->region_list, list) {
3348 if (idx < start) {
3349 idx++;
3350 continue;
3351 }
3352 err = devlink_nl_region_fill(msg, devlink,
3353 DEVLINK_CMD_REGION_GET,
3354 NETLINK_CB(cb->skb).portid,
3355 cb->nlh->nlmsg_seq,
3356 NLM_F_MULTI, region);
3357 if (err) {
3358 mutex_unlock(&devlink->lock);
3359 goto out;
3360 }
3361 idx++;
3362 }
3363 mutex_unlock(&devlink->lock);
3364 }
3365out:
3366 mutex_unlock(&devlink_mutex);
3367 cb->args[0] = idx;
3368 return msg->len;
3369}
3370
Alex Vesker866319b2018-07-12 15:13:13 +03003371static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3372 struct genl_info *info)
3373{
3374 struct devlink *devlink = info->user_ptr[0];
3375 struct devlink_snapshot *snapshot;
3376 struct devlink_region *region;
3377 const char *region_name;
3378 u32 snapshot_id;
3379
3380 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3381 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3382 return -EINVAL;
3383
3384 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3385 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3386
3387 region = devlink_region_get_by_name(devlink, region_name);
3388 if (!region)
3389 return -EINVAL;
3390
3391 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3392 if (!snapshot)
3393 return -EINVAL;
3394
3395 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3396 devlink_region_snapshot_del(snapshot);
3397 return 0;
3398}
3399
Alex Vesker4e547952018-07-12 15:13:14 +03003400static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3401 struct devlink *devlink,
3402 u8 *chunk, u32 chunk_size,
3403 u64 addr)
3404{
3405 struct nlattr *chunk_attr;
3406 int err;
3407
3408 chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
3409 if (!chunk_attr)
3410 return -EINVAL;
3411
3412 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3413 if (err)
3414 goto nla_put_failure;
3415
3416 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3417 DEVLINK_ATTR_PAD);
3418 if (err)
3419 goto nla_put_failure;
3420
3421 nla_nest_end(msg, chunk_attr);
3422 return 0;
3423
3424nla_put_failure:
3425 nla_nest_cancel(msg, chunk_attr);
3426 return err;
3427}
3428
3429#define DEVLINK_REGION_READ_CHUNK_SIZE 256
3430
3431static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3432 struct devlink *devlink,
3433 struct devlink_region *region,
3434 struct nlattr **attrs,
3435 u64 start_offset,
3436 u64 end_offset,
3437 bool dump,
3438 u64 *new_offset)
3439{
3440 struct devlink_snapshot *snapshot;
3441 u64 curr_offset = start_offset;
3442 u32 snapshot_id;
3443 int err = 0;
3444
3445 *new_offset = start_offset;
3446
3447 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3448 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3449 if (!snapshot)
3450 return -EINVAL;
3451
3452 if (end_offset > snapshot->data_len || dump)
3453 end_offset = snapshot->data_len;
3454
3455 while (curr_offset < end_offset) {
3456 u32 data_size;
3457 u8 *data;
3458
3459 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3460 data_size = end_offset - curr_offset;
3461 else
3462 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3463
3464 data = &snapshot->data[curr_offset];
3465 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3466 data, data_size,
3467 curr_offset);
3468 if (err)
3469 break;
3470
3471 curr_offset += data_size;
3472 }
3473 *new_offset = curr_offset;
3474
3475 return err;
3476}
3477
3478static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3479 struct netlink_callback *cb)
3480{
3481 u64 ret_offset, start_offset, end_offset = 0;
3482 struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
3483 const struct genl_ops *ops = cb->data;
3484 struct devlink_region *region;
3485 struct nlattr *chunks_attr;
3486 const char *region_name;
3487 struct devlink *devlink;
3488 bool dump = true;
3489 void *hdr;
3490 int err;
3491
3492 start_offset = *((u64 *)&cb->args[0]);
3493
3494 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
3495 attrs, DEVLINK_ATTR_MAX, ops->policy, NULL);
3496 if (err)
3497 goto out;
3498
3499 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3500 if (IS_ERR(devlink))
3501 goto out;
3502
3503 mutex_lock(&devlink_mutex);
3504 mutex_lock(&devlink->lock);
3505
3506 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3507 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3508 goto out_unlock;
3509
3510 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3511 region = devlink_region_get_by_name(devlink, region_name);
3512 if (!region)
3513 goto out_unlock;
3514
3515 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3516 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3517 DEVLINK_CMD_REGION_READ);
3518 if (!hdr)
3519 goto out_unlock;
3520
3521 err = devlink_nl_put_handle(skb, devlink);
3522 if (err)
3523 goto nla_put_failure;
3524
3525 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3526 if (err)
3527 goto nla_put_failure;
3528
3529 chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
3530 if (!chunks_attr)
3531 goto nla_put_failure;
3532
3533 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3534 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3535 if (!start_offset)
3536 start_offset =
3537 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3538
3539 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3540 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3541 dump = false;
3542 }
3543
3544 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3545 region, attrs,
3546 start_offset,
3547 end_offset, dump,
3548 &ret_offset);
3549
3550 if (err && err != -EMSGSIZE)
3551 goto nla_put_failure;
3552
3553 /* Check if there was any progress done to prevent infinite loop */
3554 if (ret_offset == start_offset)
3555 goto nla_put_failure;
3556
3557 *((u64 *)&cb->args[0]) = ret_offset;
3558
3559 nla_nest_end(skb, chunks_attr);
3560 genlmsg_end(skb, hdr);
3561 mutex_unlock(&devlink->lock);
3562 mutex_unlock(&devlink_mutex);
3563
3564 return skb->len;
3565
3566nla_put_failure:
3567 genlmsg_cancel(skb, hdr);
3568out_unlock:
3569 mutex_unlock(&devlink->lock);
3570 mutex_unlock(&devlink_mutex);
3571out:
3572 return 0;
3573}
3574
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003575static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
3576 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
3577 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
3578 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
3579 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
3580 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02003581 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
3582 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
3583 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
3584 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
3585 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
3586 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
3587 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03003588 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02003589 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03003590 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003591 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
3592 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003593 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
3594 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003595 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
3596 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
3597 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003598 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03003599 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003600};
3601
3602static const struct genl_ops devlink_nl_ops[] = {
3603 {
3604 .cmd = DEVLINK_CMD_GET,
3605 .doit = devlink_nl_cmd_get_doit,
3606 .dumpit = devlink_nl_cmd_get_dumpit,
3607 .policy = devlink_nl_policy,
Jiri Pirko1fc22572016-04-08 19:12:48 +02003608 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003609 /* can be retrieved by unprivileged users */
3610 },
3611 {
3612 .cmd = DEVLINK_CMD_PORT_GET,
3613 .doit = devlink_nl_cmd_port_get_doit,
3614 .dumpit = devlink_nl_cmd_port_get_dumpit,
3615 .policy = devlink_nl_policy,
3616 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3617 /* can be retrieved by unprivileged users */
3618 },
3619 {
3620 .cmd = DEVLINK_CMD_PORT_SET,
3621 .doit = devlink_nl_cmd_port_set_doit,
3622 .policy = devlink_nl_policy,
3623 .flags = GENL_ADMIN_PERM,
3624 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3625 },
3626 {
3627 .cmd = DEVLINK_CMD_PORT_SPLIT,
3628 .doit = devlink_nl_cmd_port_split_doit,
3629 .policy = devlink_nl_policy,
3630 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003631 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3632 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003633 },
3634 {
3635 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
3636 .doit = devlink_nl_cmd_port_unsplit_doit,
3637 .policy = devlink_nl_policy,
3638 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003639 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3640 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003641 },
Jiri Pirkobf797472016-04-14 18:19:13 +02003642 {
3643 .cmd = DEVLINK_CMD_SB_GET,
3644 .doit = devlink_nl_cmd_sb_get_doit,
3645 .dumpit = devlink_nl_cmd_sb_get_dumpit,
3646 .policy = devlink_nl_policy,
3647 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3648 DEVLINK_NL_FLAG_NEED_SB,
3649 /* can be retrieved by unprivileged users */
3650 },
3651 {
3652 .cmd = DEVLINK_CMD_SB_POOL_GET,
3653 .doit = devlink_nl_cmd_sb_pool_get_doit,
3654 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
3655 .policy = devlink_nl_policy,
3656 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3657 DEVLINK_NL_FLAG_NEED_SB,
3658 /* can be retrieved by unprivileged users */
3659 },
3660 {
3661 .cmd = DEVLINK_CMD_SB_POOL_SET,
3662 .doit = devlink_nl_cmd_sb_pool_set_doit,
3663 .policy = devlink_nl_policy,
3664 .flags = GENL_ADMIN_PERM,
3665 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3666 DEVLINK_NL_FLAG_NEED_SB,
3667 },
3668 {
3669 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
3670 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
3671 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
3672 .policy = devlink_nl_policy,
3673 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3674 DEVLINK_NL_FLAG_NEED_SB,
3675 /* can be retrieved by unprivileged users */
3676 },
3677 {
3678 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
3679 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
3680 .policy = devlink_nl_policy,
3681 .flags = GENL_ADMIN_PERM,
3682 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3683 DEVLINK_NL_FLAG_NEED_SB,
3684 },
3685 {
3686 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
3687 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
3688 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
3689 .policy = devlink_nl_policy,
3690 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3691 DEVLINK_NL_FLAG_NEED_SB,
3692 /* can be retrieved by unprivileged users */
3693 },
3694 {
3695 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3696 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
3697 .policy = devlink_nl_policy,
3698 .flags = GENL_ADMIN_PERM,
3699 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3700 DEVLINK_NL_FLAG_NEED_SB,
3701 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003702 {
3703 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
3704 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
3705 .policy = devlink_nl_policy,
3706 .flags = GENL_ADMIN_PERM,
3707 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003708 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003709 },
3710 {
3711 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
3712 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
3713 .policy = devlink_nl_policy,
3714 .flags = GENL_ADMIN_PERM,
3715 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003716 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02003717 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03003718 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01003719 .cmd = DEVLINK_CMD_ESWITCH_GET,
3720 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003721 .policy = devlink_nl_policy,
3722 .flags = GENL_ADMIN_PERM,
3723 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3724 },
3725 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01003726 .cmd = DEVLINK_CMD_ESWITCH_SET,
3727 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003728 .policy = devlink_nl_policy,
3729 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07003730 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3731 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03003732 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003733 {
3734 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
3735 .doit = devlink_nl_cmd_dpipe_table_get,
3736 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003737 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003738 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003739 },
3740 {
3741 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
3742 .doit = devlink_nl_cmd_dpipe_entries_get,
3743 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003744 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003745 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003746 },
3747 {
3748 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
3749 .doit = devlink_nl_cmd_dpipe_headers_get,
3750 .policy = devlink_nl_policy,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003751 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003752 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003753 },
3754 {
3755 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
3756 .doit = devlink_nl_cmd_dpipe_table_counters_set,
3757 .policy = devlink_nl_policy,
3758 .flags = GENL_ADMIN_PERM,
3759 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3760 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003761 {
3762 .cmd = DEVLINK_CMD_RESOURCE_SET,
3763 .doit = devlink_nl_cmd_resource_set,
3764 .policy = devlink_nl_policy,
3765 .flags = GENL_ADMIN_PERM,
3766 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3767 },
3768 {
3769 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
3770 .doit = devlink_nl_cmd_resource_dump,
3771 .policy = devlink_nl_policy,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003772 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02003773 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003774 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01003775 {
3776 .cmd = DEVLINK_CMD_RELOAD,
3777 .doit = devlink_nl_cmd_reload,
3778 .policy = devlink_nl_policy,
3779 .flags = GENL_ADMIN_PERM,
3780 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3781 DEVLINK_NL_FLAG_NO_LOCK,
3782 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003783 {
3784 .cmd = DEVLINK_CMD_PARAM_GET,
3785 .doit = devlink_nl_cmd_param_get_doit,
3786 .dumpit = devlink_nl_cmd_param_get_dumpit,
3787 .policy = devlink_nl_policy,
3788 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3789 /* can be retrieved by unprivileged users */
3790 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003791 {
3792 .cmd = DEVLINK_CMD_PARAM_SET,
3793 .doit = devlink_nl_cmd_param_set_doit,
3794 .policy = devlink_nl_policy,
3795 .flags = GENL_ADMIN_PERM,
3796 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3797 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003798 {
3799 .cmd = DEVLINK_CMD_REGION_GET,
3800 .doit = devlink_nl_cmd_region_get_doit,
3801 .dumpit = devlink_nl_cmd_region_get_dumpit,
3802 .policy = devlink_nl_policy,
3803 .flags = GENL_ADMIN_PERM,
3804 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3805 },
Alex Vesker866319b2018-07-12 15:13:13 +03003806 {
3807 .cmd = DEVLINK_CMD_REGION_DEL,
3808 .doit = devlink_nl_cmd_region_del,
3809 .policy = devlink_nl_policy,
3810 .flags = GENL_ADMIN_PERM,
3811 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3812 },
Alex Vesker4e547952018-07-12 15:13:14 +03003813 {
3814 .cmd = DEVLINK_CMD_REGION_READ,
3815 .dumpit = devlink_nl_cmd_region_read_dumpit,
3816 .policy = devlink_nl_policy,
3817 .flags = GENL_ADMIN_PERM,
3818 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3819 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003820};
3821
Johannes Berg56989f62016-10-24 14:40:05 +02003822static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003823 .name = DEVLINK_GENL_NAME,
3824 .version = DEVLINK_GENL_VERSION,
3825 .maxattr = DEVLINK_ATTR_MAX,
3826 .netnsok = true,
3827 .pre_doit = devlink_nl_pre_doit,
3828 .post_doit = devlink_nl_post_doit,
3829 .module = THIS_MODULE,
3830 .ops = devlink_nl_ops,
3831 .n_ops = ARRAY_SIZE(devlink_nl_ops),
3832 .mcgrps = devlink_nl_mcgrps,
3833 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
3834};
3835
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003836/**
3837 * devlink_alloc - Allocate new devlink instance resources
3838 *
3839 * @ops: ops
3840 * @priv_size: size of user private data
3841 *
3842 * Allocate new devlink instance resources, including devlink index
3843 * and name.
3844 */
3845struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
3846{
3847 struct devlink *devlink;
3848
3849 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
3850 if (!devlink)
3851 return NULL;
3852 devlink->ops = ops;
3853 devlink_net_set(devlink, &init_net);
3854 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02003855 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02003856 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01003857 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003858 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03003859 INIT_LIST_HEAD(&devlink->region_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003860 mutex_init(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003861 return devlink;
3862}
3863EXPORT_SYMBOL_GPL(devlink_alloc);
3864
3865/**
3866 * devlink_register - Register devlink instance
3867 *
3868 * @devlink: devlink
3869 */
3870int devlink_register(struct devlink *devlink, struct device *dev)
3871{
3872 mutex_lock(&devlink_mutex);
3873 devlink->dev = dev;
3874 list_add_tail(&devlink->list, &devlink_list);
3875 devlink_notify(devlink, DEVLINK_CMD_NEW);
3876 mutex_unlock(&devlink_mutex);
3877 return 0;
3878}
3879EXPORT_SYMBOL_GPL(devlink_register);
3880
3881/**
3882 * devlink_unregister - Unregister devlink instance
3883 *
3884 * @devlink: devlink
3885 */
3886void devlink_unregister(struct devlink *devlink)
3887{
3888 mutex_lock(&devlink_mutex);
3889 devlink_notify(devlink, DEVLINK_CMD_DEL);
3890 list_del(&devlink->list);
3891 mutex_unlock(&devlink_mutex);
3892}
3893EXPORT_SYMBOL_GPL(devlink_unregister);
3894
3895/**
3896 * devlink_free - Free devlink instance resources
3897 *
3898 * @devlink: devlink
3899 */
3900void devlink_free(struct devlink *devlink)
3901{
3902 kfree(devlink);
3903}
3904EXPORT_SYMBOL_GPL(devlink_free);
3905
3906/**
3907 * devlink_port_register - Register devlink port
3908 *
3909 * @devlink: devlink
3910 * @devlink_port: devlink port
3911 * @port_index
3912 *
3913 * Register devlink port with provided port index. User can use
3914 * any indexing, even hw-related one. devlink_port structure
3915 * is convenient to be embedded inside user driver private structure.
3916 * Note that the caller should take care of zeroing the devlink_port
3917 * structure.
3918 */
3919int devlink_port_register(struct devlink *devlink,
3920 struct devlink_port *devlink_port,
3921 unsigned int port_index)
3922{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003923 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003924 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003925 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003926 return -EEXIST;
3927 }
3928 devlink_port->devlink = devlink;
3929 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003930 devlink_port->registered = true;
3931 list_add_tail(&devlink_port->list, &devlink->port_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003932 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003933 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3934 return 0;
3935}
3936EXPORT_SYMBOL_GPL(devlink_port_register);
3937
3938/**
3939 * devlink_port_unregister - Unregister devlink port
3940 *
3941 * @devlink_port: devlink port
3942 */
3943void devlink_port_unregister(struct devlink_port *devlink_port)
3944{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003945 struct devlink *devlink = devlink_port->devlink;
3946
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003947 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003948 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003949 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01003950 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01003951}
3952EXPORT_SYMBOL_GPL(devlink_port_unregister);
3953
3954static void __devlink_port_type_set(struct devlink_port *devlink_port,
3955 enum devlink_port_type type,
3956 void *type_dev)
3957{
3958 devlink_port->type = type;
3959 devlink_port->type_dev = type_dev;
3960 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3961}
3962
3963/**
3964 * devlink_port_type_eth_set - Set port type to Ethernet
3965 *
3966 * @devlink_port: devlink port
3967 * @netdev: related netdevice
3968 */
3969void devlink_port_type_eth_set(struct devlink_port *devlink_port,
3970 struct net_device *netdev)
3971{
3972 return __devlink_port_type_set(devlink_port,
3973 DEVLINK_PORT_TYPE_ETH, netdev);
3974}
3975EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
3976
3977/**
3978 * devlink_port_type_ib_set - Set port type to InfiniBand
3979 *
3980 * @devlink_port: devlink port
3981 * @ibdev: related IB device
3982 */
3983void devlink_port_type_ib_set(struct devlink_port *devlink_port,
3984 struct ib_device *ibdev)
3985{
3986 return __devlink_port_type_set(devlink_port,
3987 DEVLINK_PORT_TYPE_IB, ibdev);
3988}
3989EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
3990
3991/**
3992 * devlink_port_type_clear - Clear port type
3993 *
3994 * @devlink_port: devlink port
3995 */
3996void devlink_port_type_clear(struct devlink_port *devlink_port)
3997{
3998 return __devlink_port_type_set(devlink_port,
3999 DEVLINK_PORT_TYPE_NOTSET, NULL);
4000}
4001EXPORT_SYMBOL_GPL(devlink_port_type_clear);
4002
4003/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004004 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004005 *
4006 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02004007 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004008 * @port_number: number of the port that is facing user, for example
4009 * the front panel port number
4010 * @split: indicates if this is split port
4011 * @split_subport_number: if the port is split, this is the number
4012 * of subport.
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004013 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004014void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02004015 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004016 u32 port_number, bool split,
4017 u32 split_subport_number)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004018{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004019 struct devlink_port_attrs *attrs = &devlink_port->attrs;
4020
4021 attrs->set = true;
Jiri Pirko5ec13802018-05-18 09:29:01 +02004022 attrs->flavour = flavour;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004023 attrs->port_number = port_number;
4024 attrs->split = split;
4025 attrs->split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004026 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4027}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02004028EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004029
Jiri Pirko08474c12018-05-18 09:29:02 +02004030int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
4031 char *name, size_t len)
4032{
4033 struct devlink_port_attrs *attrs = &devlink_port->attrs;
4034 int n = 0;
4035
4036 if (!attrs->set)
4037 return -EOPNOTSUPP;
4038
4039 switch (attrs->flavour) {
4040 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
4041 if (!attrs->split)
4042 n = snprintf(name, len, "p%u", attrs->port_number);
4043 else
4044 n = snprintf(name, len, "p%us%u", attrs->port_number,
4045 attrs->split_subport_number);
4046 break;
4047 case DEVLINK_PORT_FLAVOUR_CPU:
4048 case DEVLINK_PORT_FLAVOUR_DSA:
4049 /* As CPU and DSA ports do not have a netdevice associated
4050 * case should not ever happen.
4051 */
4052 WARN_ON(1);
4053 return -EINVAL;
4054 }
4055
4056 if (n >= len)
4057 return -EINVAL;
4058
4059 return 0;
4060}
4061EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
4062
Jiri Pirkobf797472016-04-14 18:19:13 +02004063int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
4064 u32 size, u16 ingress_pools_count,
4065 u16 egress_pools_count, u16 ingress_tc_count,
4066 u16 egress_tc_count)
4067{
4068 struct devlink_sb *devlink_sb;
4069 int err = 0;
4070
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004071 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02004072 if (devlink_sb_index_exists(devlink, sb_index)) {
4073 err = -EEXIST;
4074 goto unlock;
4075 }
4076
4077 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
4078 if (!devlink_sb) {
4079 err = -ENOMEM;
4080 goto unlock;
4081 }
4082 devlink_sb->index = sb_index;
4083 devlink_sb->size = size;
4084 devlink_sb->ingress_pools_count = ingress_pools_count;
4085 devlink_sb->egress_pools_count = egress_pools_count;
4086 devlink_sb->ingress_tc_count = ingress_tc_count;
4087 devlink_sb->egress_tc_count = egress_tc_count;
4088 list_add_tail(&devlink_sb->list, &devlink->sb_list);
4089unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004090 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02004091 return err;
4092}
4093EXPORT_SYMBOL_GPL(devlink_sb_register);
4094
4095void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
4096{
4097 struct devlink_sb *devlink_sb;
4098
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004099 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02004100 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
4101 WARN_ON(!devlink_sb);
4102 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004103 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02004104 kfree(devlink_sb);
4105}
4106EXPORT_SYMBOL_GPL(devlink_sb_unregister);
4107
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004108/**
4109 * devlink_dpipe_headers_register - register dpipe headers
4110 *
4111 * @devlink: devlink
4112 * @dpipe_headers: dpipe header array
4113 *
4114 * Register the headers supported by hardware.
4115 */
4116int devlink_dpipe_headers_register(struct devlink *devlink,
4117 struct devlink_dpipe_headers *dpipe_headers)
4118{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004119 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004120 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004121 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004122 return 0;
4123}
4124EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
4125
4126/**
4127 * devlink_dpipe_headers_unregister - unregister dpipe headers
4128 *
4129 * @devlink: devlink
4130 *
4131 * Unregister the headers supported by hardware.
4132 */
4133void devlink_dpipe_headers_unregister(struct devlink *devlink)
4134{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004135 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004136 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004137 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004138}
4139EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
4140
4141/**
4142 * devlink_dpipe_table_counter_enabled - check if counter allocation
4143 * required
4144 * @devlink: devlink
4145 * @table_name: tables name
4146 *
4147 * Used by driver to check if counter allocation is required.
4148 * After counter allocation is turned on the table entries
4149 * are updated to include counter statistics.
4150 *
4151 * After that point on the driver must respect the counter
4152 * state so that each entry added to the table is added
4153 * with a counter.
4154 */
4155bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
4156 const char *table_name)
4157{
4158 struct devlink_dpipe_table *table;
4159 bool enabled;
4160
4161 rcu_read_lock();
4162 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4163 table_name);
4164 enabled = false;
4165 if (table)
4166 enabled = table->counters_enabled;
4167 rcu_read_unlock();
4168 return enabled;
4169}
4170EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
4171
4172/**
4173 * devlink_dpipe_table_register - register dpipe table
4174 *
4175 * @devlink: devlink
4176 * @table_name: table name
4177 * @table_ops: table ops
4178 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004179 * @counter_control_extern: external control for counters
4180 */
4181int devlink_dpipe_table_register(struct devlink *devlink,
4182 const char *table_name,
4183 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02004184 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004185{
4186 struct devlink_dpipe_table *table;
4187
4188 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
4189 return -EEXIST;
4190
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02004191 if (WARN_ON(!table_ops->size_get))
4192 return -EINVAL;
4193
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004194 table = kzalloc(sizeof(*table), GFP_KERNEL);
4195 if (!table)
4196 return -ENOMEM;
4197
4198 table->name = table_name;
4199 table->table_ops = table_ops;
4200 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004201 table->counter_control_extern = counter_control_extern;
4202
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004203 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004204 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004205 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004206 return 0;
4207}
4208EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
4209
4210/**
4211 * devlink_dpipe_table_unregister - unregister dpipe table
4212 *
4213 * @devlink: devlink
4214 * @table_name: table name
4215 */
4216void devlink_dpipe_table_unregister(struct devlink *devlink,
4217 const char *table_name)
4218{
4219 struct devlink_dpipe_table *table;
4220
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004221 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004222 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4223 table_name);
4224 if (!table)
4225 goto unlock;
4226 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004227 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004228 kfree_rcu(table, rcu);
4229 return;
4230unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01004231 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02004232}
4233EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
4234
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004235/**
4236 * devlink_resource_register - devlink resource register
4237 *
4238 * @devlink: devlink
4239 * @resource_name: resource's name
4240 * @top_hierarchy: top hierarchy
4241 * @reload_required: reload is required for new configuration to
4242 * apply
4243 * @resource_size: resource's size
4244 * @resource_id: resource's id
4245 * @parent_reosurce_id: resource's parent id
4246 * @size params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004247 */
4248int devlink_resource_register(struct devlink *devlink,
4249 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004250 u64 resource_size,
4251 u64 resource_id,
4252 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02004253 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004254{
4255 struct devlink_resource *resource;
4256 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07004257 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004258 int err = 0;
4259
David Ahern14530742018-03-20 19:31:14 -07004260 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
4261
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004262 mutex_lock(&devlink->lock);
4263 resource = devlink_resource_find(devlink, NULL, resource_id);
4264 if (resource) {
4265 err = -EINVAL;
4266 goto out;
4267 }
4268
4269 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
4270 if (!resource) {
4271 err = -ENOMEM;
4272 goto out;
4273 }
4274
4275 if (top_hierarchy) {
4276 resource_list = &devlink->resource_list;
4277 } else {
4278 struct devlink_resource *parent_resource;
4279
4280 parent_resource = devlink_resource_find(devlink, NULL,
4281 parent_resource_id);
4282 if (parent_resource) {
4283 resource_list = &parent_resource->resource_list;
4284 resource->parent = parent_resource;
4285 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00004286 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004287 err = -EINVAL;
4288 goto out;
4289 }
4290 }
4291
4292 resource->name = resource_name;
4293 resource->size = resource_size;
4294 resource->size_new = resource_size;
4295 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004296 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01004297 memcpy(&resource->size_params, size_params,
4298 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01004299 INIT_LIST_HEAD(&resource->resource_list);
4300 list_add_tail(&resource->list, resource_list);
4301out:
4302 mutex_unlock(&devlink->lock);
4303 return err;
4304}
4305EXPORT_SYMBOL_GPL(devlink_resource_register);
4306
4307/**
4308 * devlink_resources_unregister - free all resources
4309 *
4310 * @devlink: devlink
4311 * @resource: resource
4312 */
4313void devlink_resources_unregister(struct devlink *devlink,
4314 struct devlink_resource *resource)
4315{
4316 struct devlink_resource *tmp, *child_resource;
4317 struct list_head *resource_list;
4318
4319 if (resource)
4320 resource_list = &resource->resource_list;
4321 else
4322 resource_list = &devlink->resource_list;
4323
4324 if (!resource)
4325 mutex_lock(&devlink->lock);
4326
4327 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
4328 devlink_resources_unregister(devlink, child_resource);
4329 list_del(&child_resource->list);
4330 kfree(child_resource);
4331 }
4332
4333 if (!resource)
4334 mutex_unlock(&devlink->lock);
4335}
4336EXPORT_SYMBOL_GPL(devlink_resources_unregister);
4337
4338/**
4339 * devlink_resource_size_get - get and update size
4340 *
4341 * @devlink: devlink
4342 * @resource_id: the requested resource id
4343 * @p_resource_size: ptr to update
4344 */
4345int devlink_resource_size_get(struct devlink *devlink,
4346 u64 resource_id,
4347 u64 *p_resource_size)
4348{
4349 struct devlink_resource *resource;
4350 int err = 0;
4351
4352 mutex_lock(&devlink->lock);
4353 resource = devlink_resource_find(devlink, NULL, resource_id);
4354 if (!resource) {
4355 err = -EINVAL;
4356 goto out;
4357 }
4358 *p_resource_size = resource->size_new;
4359 resource->size = resource->size_new;
4360out:
4361 mutex_unlock(&devlink->lock);
4362 return err;
4363}
4364EXPORT_SYMBOL_GPL(devlink_resource_size_get);
4365
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01004366/**
4367 * devlink_dpipe_table_resource_set - set the resource id
4368 *
4369 * @devlink: devlink
4370 * @table_name: table name
4371 * @resource_id: resource id
4372 * @resource_units: number of resource's units consumed per table's entry
4373 */
4374int devlink_dpipe_table_resource_set(struct devlink *devlink,
4375 const char *table_name, u64 resource_id,
4376 u64 resource_units)
4377{
4378 struct devlink_dpipe_table *table;
4379 int err = 0;
4380
4381 mutex_lock(&devlink->lock);
4382 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4383 table_name);
4384 if (!table) {
4385 err = -EINVAL;
4386 goto out;
4387 }
4388 table->resource_id = resource_id;
4389 table->resource_units = resource_units;
4390 table->resource_valid = true;
4391out:
4392 mutex_unlock(&devlink->lock);
4393 return err;
4394}
4395EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
4396
Jiri Pirkofc56be42018-04-05 22:13:21 +02004397/**
4398 * devlink_resource_occ_get_register - register occupancy getter
4399 *
4400 * @devlink: devlink
4401 * @resource_id: resource id
4402 * @occ_get: occupancy getter callback
4403 * @occ_get_priv: occupancy getter callback priv
4404 */
4405void devlink_resource_occ_get_register(struct devlink *devlink,
4406 u64 resource_id,
4407 devlink_resource_occ_get_t *occ_get,
4408 void *occ_get_priv)
4409{
4410 struct devlink_resource *resource;
4411
4412 mutex_lock(&devlink->lock);
4413 resource = devlink_resource_find(devlink, NULL, resource_id);
4414 if (WARN_ON(!resource))
4415 goto out;
4416 WARN_ON(resource->occ_get);
4417
4418 resource->occ_get = occ_get;
4419 resource->occ_get_priv = occ_get_priv;
4420out:
4421 mutex_unlock(&devlink->lock);
4422}
4423EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
4424
4425/**
4426 * devlink_resource_occ_get_unregister - unregister occupancy getter
4427 *
4428 * @devlink: devlink
4429 * @resource_id: resource id
4430 */
4431void devlink_resource_occ_get_unregister(struct devlink *devlink,
4432 u64 resource_id)
4433{
4434 struct devlink_resource *resource;
4435
4436 mutex_lock(&devlink->lock);
4437 resource = devlink_resource_find(devlink, NULL, resource_id);
4438 if (WARN_ON(!resource))
4439 goto out;
4440 WARN_ON(!resource->occ_get);
4441
4442 resource->occ_get = NULL;
4443 resource->occ_get_priv = NULL;
4444out:
4445 mutex_unlock(&devlink->lock);
4446}
4447EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
4448
Moshe Shemesheabaef12018-07-04 14:30:28 +03004449/**
4450 * devlink_params_register - register configuration parameters
4451 *
4452 * @devlink: devlink
4453 * @params: configuration parameters array
4454 * @params_count: number of parameters provided
4455 *
4456 * Register the configuration parameters supported by the driver.
4457 */
4458int devlink_params_register(struct devlink *devlink,
4459 const struct devlink_param *params,
4460 size_t params_count)
4461{
4462 const struct devlink_param *param = params;
4463 int i;
4464 int err;
4465
4466 mutex_lock(&devlink->lock);
4467 for (i = 0; i < params_count; i++, param++) {
4468 if (!param || !param->name || !param->supported_cmodes) {
4469 err = -EINVAL;
4470 goto rollback;
4471 }
4472 if (param->generic) {
4473 err = devlink_param_generic_verify(param);
4474 if (err)
4475 goto rollback;
4476 } else {
4477 err = devlink_param_driver_verify(param);
4478 if (err)
4479 goto rollback;
4480 }
4481 err = devlink_param_register_one(devlink, param);
4482 if (err)
4483 goto rollback;
4484 }
4485
4486 mutex_unlock(&devlink->lock);
4487 return 0;
4488
4489rollback:
4490 if (!i)
4491 goto unlock;
4492 for (param--; i > 0; i--, param--)
4493 devlink_param_unregister_one(devlink, param);
4494unlock:
4495 mutex_unlock(&devlink->lock);
4496 return err;
4497}
4498EXPORT_SYMBOL_GPL(devlink_params_register);
4499
4500/**
4501 * devlink_params_unregister - unregister configuration parameters
4502 * @devlink: devlink
4503 * @params: configuration parameters to unregister
4504 * @params_count: number of parameters provided
4505 */
4506void devlink_params_unregister(struct devlink *devlink,
4507 const struct devlink_param *params,
4508 size_t params_count)
4509{
4510 const struct devlink_param *param = params;
4511 int i;
4512
4513 mutex_lock(&devlink->lock);
4514 for (i = 0; i < params_count; i++, param++)
4515 devlink_param_unregister_one(devlink, param);
4516 mutex_unlock(&devlink->lock);
4517}
4518EXPORT_SYMBOL_GPL(devlink_params_unregister);
4519
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03004520/**
4521 * devlink_param_driverinit_value_get - get configuration parameter
4522 * value for driver initializing
4523 *
4524 * @devlink: devlink
4525 * @param_id: parameter ID
4526 * @init_val: value of parameter in driverinit configuration mode
4527 *
4528 * This function should be used by the driver to get driverinit
4529 * configuration for initialization after reload command.
4530 */
4531int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
4532 union devlink_param_value *init_val)
4533{
4534 struct devlink_param_item *param_item;
4535
4536 if (!devlink->ops || !devlink->ops->reload)
4537 return -EOPNOTSUPP;
4538
4539 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4540 if (!param_item)
4541 return -EINVAL;
4542
4543 if (!param_item->driverinit_value_valid ||
4544 !devlink_param_cmode_is_supported(param_item->param,
4545 DEVLINK_PARAM_CMODE_DRIVERINIT))
4546 return -EOPNOTSUPP;
4547
4548 *init_val = param_item->driverinit_value;
4549
4550 return 0;
4551}
4552EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
4553
4554/**
4555 * devlink_param_driverinit_value_set - set value of configuration
4556 * parameter for driverinit
4557 * configuration mode
4558 *
4559 * @devlink: devlink
4560 * @param_id: parameter ID
4561 * @init_val: value of parameter to set for driverinit configuration mode
4562 *
4563 * This function should be used by the driver to set driverinit
4564 * configuration mode default value.
4565 */
4566int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
4567 union devlink_param_value init_val)
4568{
4569 struct devlink_param_item *param_item;
4570
4571 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4572 if (!param_item)
4573 return -EINVAL;
4574
4575 if (!devlink_param_cmode_is_supported(param_item->param,
4576 DEVLINK_PARAM_CMODE_DRIVERINIT))
4577 return -EOPNOTSUPP;
4578
4579 param_item->driverinit_value = init_val;
4580 param_item->driverinit_value_valid = true;
4581
Moshe Shemeshea601e12018-07-04 14:30:32 +03004582 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03004583 return 0;
4584}
4585EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
4586
Moshe Shemeshea601e12018-07-04 14:30:32 +03004587/**
4588 * devlink_param_value_changed - notify devlink on a parameter's value
4589 * change. Should be called by the driver
4590 * right after the change.
4591 *
4592 * @devlink: devlink
4593 * @param_id: parameter ID
4594 *
4595 * This function should be used by the driver to notify devlink on value
4596 * change, excluding driverinit configuration mode.
4597 * For driverinit configuration mode driver should use the function
4598 * devlink_param_driverinit_value_set() instead.
4599 */
4600void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
4601{
4602 struct devlink_param_item *param_item;
4603
4604 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4605 WARN_ON(!param_item);
4606
4607 devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4608}
4609EXPORT_SYMBOL_GPL(devlink_param_value_changed);
4610
Alex Veskerb16ebe92018-07-12 15:13:08 +03004611/**
4612 * devlink_region_create - create a new address region
4613 *
4614 * @devlink: devlink
4615 * @region_name: region name
4616 * @region_max_snapshots: Maximum supported number of snapshots for region
4617 * @region_size: size of region
4618 */
4619struct devlink_region *devlink_region_create(struct devlink *devlink,
4620 const char *region_name,
4621 u32 region_max_snapshots,
4622 u64 region_size)
4623{
4624 struct devlink_region *region;
4625 int err = 0;
4626
4627 mutex_lock(&devlink->lock);
4628
4629 if (devlink_region_get_by_name(devlink, region_name)) {
4630 err = -EEXIST;
4631 goto unlock;
4632 }
4633
4634 region = kzalloc(sizeof(*region), GFP_KERNEL);
4635 if (!region) {
4636 err = -ENOMEM;
4637 goto unlock;
4638 }
4639
4640 region->devlink = devlink;
4641 region->max_snapshots = region_max_snapshots;
4642 region->name = region_name;
4643 region->size = region_size;
4644 INIT_LIST_HEAD(&region->snapshot_list);
4645 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03004646 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03004647
4648 mutex_unlock(&devlink->lock);
4649 return region;
4650
4651unlock:
4652 mutex_unlock(&devlink->lock);
4653 return ERR_PTR(err);
4654}
4655EXPORT_SYMBOL_GPL(devlink_region_create);
4656
4657/**
4658 * devlink_region_destroy - destroy address region
4659 *
4660 * @region: devlink region to destroy
4661 */
4662void devlink_region_destroy(struct devlink_region *region)
4663{
4664 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03004665 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03004666
4667 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03004668
4669 /* Free all snapshots of region */
4670 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
4671 devlink_region_snapshot_del(snapshot);
4672
Alex Veskerb16ebe92018-07-12 15:13:08 +03004673 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03004674
4675 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03004676 mutex_unlock(&devlink->lock);
4677 kfree(region);
4678}
4679EXPORT_SYMBOL_GPL(devlink_region_destroy);
4680
Alex Veskerccadfa42018-07-12 15:13:09 +03004681/**
4682 * devlink_region_shapshot_id_get - get snapshot ID
4683 *
4684 * This callback should be called when adding a new snapshot,
4685 * Driver should use the same id for multiple snapshots taken
4686 * on multiple regions at the same time/by the same trigger.
4687 *
4688 * @devlink: devlink
4689 */
4690u32 devlink_region_shapshot_id_get(struct devlink *devlink)
4691{
4692 u32 id;
4693
4694 mutex_lock(&devlink->lock);
4695 id = ++devlink->snapshot_id;
4696 mutex_unlock(&devlink->lock);
4697
4698 return id;
4699}
4700EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
4701
Alex Veskerd7e52722018-07-12 15:13:10 +03004702/**
4703 * devlink_region_snapshot_create - create a new snapshot
4704 * This will add a new snapshot of a region. The snapshot
4705 * will be stored on the region struct and can be accessed
4706 * from devlink. This is useful for future analyses of snapshots.
4707 * Multiple snapshots can be created on a region.
4708 * The @snapshot_id should be obtained using the getter function.
4709 *
4710 * @devlink_region: devlink region of the snapshot
4711 * @data_len: size of snapshot data
4712 * @data: snapshot data
4713 * @snapshot_id: snapshot id to be created
4714 * @data_destructor: pointer to destructor function to free data
4715 */
4716int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
4717 u8 *data, u32 snapshot_id,
4718 devlink_snapshot_data_dest_t *data_destructor)
4719{
4720 struct devlink *devlink = region->devlink;
4721 struct devlink_snapshot *snapshot;
4722 int err;
4723
4724 mutex_lock(&devlink->lock);
4725
4726 /* check if region can hold one more snapshot */
4727 if (region->cur_snapshots == region->max_snapshots) {
4728 err = -ENOMEM;
4729 goto unlock;
4730 }
4731
4732 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4733 err = -EEXIST;
4734 goto unlock;
4735 }
4736
4737 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4738 if (!snapshot) {
4739 err = -ENOMEM;
4740 goto unlock;
4741 }
4742
4743 snapshot->id = snapshot_id;
4744 snapshot->region = region;
4745 snapshot->data = data;
4746 snapshot->data_len = data_len;
4747 snapshot->data_destructor = data_destructor;
4748
4749 list_add_tail(&snapshot->list, &region->snapshot_list);
4750
4751 region->cur_snapshots++;
4752
Alex Vesker866319b2018-07-12 15:13:13 +03004753 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03004754 mutex_unlock(&devlink->lock);
4755 return 0;
4756
4757unlock:
4758 mutex_unlock(&devlink->lock);
4759 return err;
4760}
4761EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
4762
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004763static int __init devlink_module_init(void)
4764{
Johannes Berg489111e2016-10-24 14:40:03 +02004765 return genl_register_family(&devlink_nl_family);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01004766}
4767
4768static void __exit devlink_module_exit(void)
4769{
4770 genl_unregister_family(&devlink_nl_family);
4771}
4772
4773module_init(devlink_module_init);
4774module_exit(devlink_module_exit);
4775
4776MODULE_LICENSE("GPL v2");
4777MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4778MODULE_DESCRIPTION("Network physical device Netlink interface");
4779MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);