blob: 362cbbcca22524290be2b3a4909d38999eb21e3e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002/*
3 * net/core/devlink.c - Network physical/parent device Netlink interface
4 *
5 * Heavily inspired by net/wireless/
6 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/gfp.h>
15#include <linux/device.h>
16#include <linux/list.h>
17#include <linux/netdevice.h>
Jiri Pirkob8f97552019-03-24 11:14:37 +010018#include <linux/spinlock.h>
Moshe Shemeshb587bda2019-04-29 12:41:45 +030019#include <linux/refcount.h>
Jiri Pirko136bf272019-05-23 10:43:35 +020020#include <linux/workqueue.h>
Ido Schimmel0f420b62019-08-17 16:28:17 +030021#include <linux/u64_stats_sync.h>
22#include <linux/timekeeping.h>
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010023#include <rdma/ib_verbs.h>
24#include <net/netlink.h>
25#include <net/genetlink.h>
26#include <net/rtnetlink.h>
27#include <net/net_namespace.h>
28#include <net/sock.h>
29#include <net/devlink.h>
Ido Schimmel0f420b62019-08-17 16:28:17 +030030#include <net/drop_monitor.h>
Jiri Pirkoe5224f02016-07-12 18:05:03 +020031#define CREATE_TRACE_POINTS
32#include <trace/events/devlink.h>
33
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020034static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
35 {
David Ahern12bdc5e2017-08-30 17:07:30 -070036 .name = "destination mac",
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020037 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
38 .bitwidth = 48,
39 },
40};
41
42struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
43 .name = "ethernet",
44 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
45 .fields = devlink_dpipe_fields_ethernet,
46 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
47 .global = true,
48};
49EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
50
Arkadi Sharshevsky3fb886e2017-08-24 08:40:00 +020051static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
52 {
53 .name = "destination ip",
54 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
55 .bitwidth = 32,
56 },
57};
58
59struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
60 .name = "ipv4",
61 .id = DEVLINK_DPIPE_HEADER_IPV4,
62 .fields = devlink_dpipe_fields_ipv4,
63 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
64 .global = true,
65};
66EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
67
Arkadi Sharshevsky1797f5b2017-08-31 17:59:12 +020068static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
69 {
70 .name = "destination ip",
71 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
72 .bitwidth = 128,
73 },
74};
75
76struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
77 .name = "ipv6",
78 .id = DEVLINK_DPIPE_HEADER_IPV6,
79 .fields = devlink_dpipe_fields_ipv6,
80 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
81 .global = true,
82};
83EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
84
Jiri Pirkoe5224f02016-07-12 18:05:03 +020085EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
Nir Dotan57186a52019-02-04 18:47:45 +000086EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010087
88static LIST_HEAD(devlink_list);
89
90/* devlink_mutex
91 *
92 * An overall lock guarding every operation coming from userspace.
93 * It also guards devlink devices list and it is taken when
94 * driver registers/unregisters it.
95 */
96static DEFINE_MUTEX(devlink_mutex);
97
Jiri Pirko471f8942019-10-03 11:49:31 +020098struct net *devlink_net(const struct devlink *devlink)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010099{
100 return read_pnet(&devlink->_net);
101}
Jiri Pirko471f8942019-10-03 11:49:31 +0200102EXPORT_SYMBOL_GPL(devlink_net);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100103
104static void devlink_net_set(struct devlink *devlink, struct net *net)
105{
106 write_pnet(&devlink->_net, net);
107}
108
109static struct devlink *devlink_get_from_attrs(struct net *net,
110 struct nlattr **attrs)
111{
112 struct devlink *devlink;
113 char *busname;
114 char *devname;
115
116 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
117 return ERR_PTR(-EINVAL);
118
119 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
120 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
121
Parav Panditdac7c082019-02-12 14:24:08 -0600122 lockdep_assert_held(&devlink_mutex);
123
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100124 list_for_each_entry(devlink, &devlink_list, list) {
125 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
126 strcmp(dev_name(devlink->dev), devname) == 0 &&
127 net_eq(devlink_net(devlink), net))
128 return devlink;
129 }
130
131 return ERR_PTR(-ENODEV);
132}
133
134static struct devlink *devlink_get_from_info(struct genl_info *info)
135{
136 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
137}
138
139static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
Parav Panditc7282b52019-08-30 05:39:44 -0500140 unsigned int port_index)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100141{
142 struct devlink_port *devlink_port;
143
144 list_for_each_entry(devlink_port, &devlink->port_list, list) {
145 if (devlink_port->index == port_index)
146 return devlink_port;
147 }
148 return NULL;
149}
150
Parav Panditc7282b52019-08-30 05:39:44 -0500151static bool devlink_port_index_exists(struct devlink *devlink,
152 unsigned int port_index)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100153{
154 return devlink_port_get_by_index(devlink, port_index);
155}
156
157static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
158 struct nlattr **attrs)
159{
160 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
161 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
162 struct devlink_port *devlink_port;
163
164 devlink_port = devlink_port_get_by_index(devlink, port_index);
165 if (!devlink_port)
166 return ERR_PTR(-ENODEV);
167 return devlink_port;
168 }
169 return ERR_PTR(-EINVAL);
170}
171
172static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
173 struct genl_info *info)
174{
175 return devlink_port_get_from_attrs(devlink, info->attrs);
176}
177
Jiri Pirkobf797472016-04-14 18:19:13 +0200178struct devlink_sb {
179 struct list_head list;
180 unsigned int index;
181 u32 size;
182 u16 ingress_pools_count;
183 u16 egress_pools_count;
184 u16 ingress_tc_count;
185 u16 egress_tc_count;
186};
187
188static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
189{
190 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
191}
192
193static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
194 unsigned int sb_index)
195{
196 struct devlink_sb *devlink_sb;
197
198 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
199 if (devlink_sb->index == sb_index)
200 return devlink_sb;
201 }
202 return NULL;
203}
204
205static bool devlink_sb_index_exists(struct devlink *devlink,
206 unsigned int sb_index)
207{
208 return devlink_sb_get_by_index(devlink, sb_index);
209}
210
211static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
212 struct nlattr **attrs)
213{
214 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
215 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
216 struct devlink_sb *devlink_sb;
217
218 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
219 if (!devlink_sb)
220 return ERR_PTR(-ENODEV);
221 return devlink_sb;
222 }
223 return ERR_PTR(-EINVAL);
224}
225
226static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
227 struct genl_info *info)
228{
229 return devlink_sb_get_from_attrs(devlink, info->attrs);
230}
231
232static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
233 struct nlattr **attrs,
234 u16 *p_pool_index)
235{
236 u16 val;
237
238 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
239 return -EINVAL;
240
241 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
242 if (val >= devlink_sb_pool_count(devlink_sb))
243 return -EINVAL;
244 *p_pool_index = val;
245 return 0;
246}
247
248static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
249 struct genl_info *info,
250 u16 *p_pool_index)
251{
252 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
253 p_pool_index);
254}
255
256static int
257devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
258 enum devlink_sb_pool_type *p_pool_type)
259{
260 u8 val;
261
262 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
263 return -EINVAL;
264
265 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
266 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
267 val != DEVLINK_SB_POOL_TYPE_EGRESS)
268 return -EINVAL;
269 *p_pool_type = val;
270 return 0;
271}
272
273static int
274devlink_sb_pool_type_get_from_info(struct genl_info *info,
275 enum devlink_sb_pool_type *p_pool_type)
276{
277 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
278}
279
280static int
281devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
282 enum devlink_sb_threshold_type *p_th_type)
283{
284 u8 val;
285
286 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
287 return -EINVAL;
288
289 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
290 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
291 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
292 return -EINVAL;
293 *p_th_type = val;
294 return 0;
295}
296
297static int
298devlink_sb_th_type_get_from_info(struct genl_info *info,
299 enum devlink_sb_threshold_type *p_th_type)
300{
301 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
302}
303
304static int
305devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
306 struct nlattr **attrs,
307 enum devlink_sb_pool_type pool_type,
308 u16 *p_tc_index)
309{
310 u16 val;
311
312 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
313 return -EINVAL;
314
315 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
316 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
317 val >= devlink_sb->ingress_tc_count)
318 return -EINVAL;
319 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
320 val >= devlink_sb->egress_tc_count)
321 return -EINVAL;
322 *p_tc_index = val;
323 return 0;
324}
325
326static int
327devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
328 struct genl_info *info,
329 enum devlink_sb_pool_type pool_type,
330 u16 *p_tc_index)
331{
332 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
333 pool_type, p_tc_index);
334}
335
Alex Veskerb16ebe92018-07-12 15:13:08 +0300336struct devlink_region {
337 struct devlink *devlink;
338 struct list_head list;
339 const char *name;
340 struct list_head snapshot_list;
341 u32 max_snapshots;
342 u32 cur_snapshots;
343 u64 size;
344};
345
Alex Veskerd7e52722018-07-12 15:13:10 +0300346struct devlink_snapshot {
347 struct list_head list;
348 struct devlink_region *region;
349 devlink_snapshot_data_dest_t *data_destructor;
Alex Veskerd7e52722018-07-12 15:13:10 +0300350 u8 *data;
351 u32 id;
352};
353
Alex Veskerb16ebe92018-07-12 15:13:08 +0300354static struct devlink_region *
355devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
356{
357 struct devlink_region *region;
358
359 list_for_each_entry(region, &devlink->region_list, list)
360 if (!strcmp(region->name, region_name))
361 return region;
362
363 return NULL;
364}
365
Alex Veskerd7e52722018-07-12 15:13:10 +0300366static struct devlink_snapshot *
367devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
368{
369 struct devlink_snapshot *snapshot;
370
371 list_for_each_entry(snapshot, &region->snapshot_list, list)
372 if (snapshot->id == id)
373 return snapshot;
374
375 return NULL;
376}
377
Jiri Pirko1fc22572016-04-08 19:12:48 +0200378#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
379#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200380#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100381
382/* The per devlink instance lock is taken by default in the pre-doit
383 * operation, yet several commands do not require this. The global
384 * devlink lock is taken and protects from disruption by user-calls.
385 */
386#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100387
388static int devlink_nl_pre_doit(const struct genl_ops *ops,
389 struct sk_buff *skb, struct genl_info *info)
390{
391 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100392 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100393
394 mutex_lock(&devlink_mutex);
395 devlink = devlink_get_from_info(info);
396 if (IS_ERR(devlink)) {
397 mutex_unlock(&devlink_mutex);
398 return PTR_ERR(devlink);
399 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100400 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
401 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200402 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
403 info->user_ptr[0] = devlink;
404 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100405 struct devlink_port *devlink_port;
406
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100407 devlink_port = devlink_port_get_from_info(devlink, info);
408 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100409 err = PTR_ERR(devlink_port);
410 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100411 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200412 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100413 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200414 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
415 struct devlink_sb *devlink_sb;
416
417 devlink_sb = devlink_sb_get_from_info(devlink, info);
418 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100419 err = PTR_ERR(devlink_sb);
420 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200421 }
422 info->user_ptr[1] = devlink_sb;
423 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100424 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100425
426unlock:
427 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
428 mutex_unlock(&devlink->lock);
429 mutex_unlock(&devlink_mutex);
430 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100431}
432
433static void devlink_nl_post_doit(const struct genl_ops *ops,
434 struct sk_buff *skb, struct genl_info *info)
435{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100436 struct devlink *devlink;
437
438 devlink = devlink_get_from_info(info);
439 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
440 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100441 mutex_unlock(&devlink_mutex);
442}
443
Johannes Berg489111e2016-10-24 14:40:03 +0200444static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100445
446enum devlink_multicast_groups {
447 DEVLINK_MCGRP_CONFIG,
448};
449
450static const struct genl_multicast_group devlink_nl_mcgrps[] = {
451 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
452};
453
454static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
455{
456 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
457 return -EMSGSIZE;
458 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
459 return -EMSGSIZE;
460 return 0;
461}
462
463static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
464 enum devlink_command cmd, u32 portid,
465 u32 seq, int flags)
466{
467 void *hdr;
468
469 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
470 if (!hdr)
471 return -EMSGSIZE;
472
473 if (devlink_nl_put_handle(msg, devlink))
474 goto nla_put_failure;
Jiri Pirko2670ac22019-09-12 10:49:46 +0200475 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
476 goto nla_put_failure;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100477
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;
Parav Pandit58b6be42019-08-30 05:39:45 -0500516 switch (devlink_port->attrs.flavour) {
517 case DEVLINK_PORT_FLAVOUR_PCI_PF:
Parav Pandit98fd2d62019-07-08 23:17:37 -0500518 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
519 attrs->pci_pf.pf))
520 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500521 break;
522 case DEVLINK_PORT_FLAVOUR_PCI_VF:
Parav Pandite41b6bf2019-07-08 23:17:38 -0500523 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
524 attrs->pci_vf.pf) ||
525 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER,
526 attrs->pci_vf.vf))
527 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500528 break;
529 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
530 case DEVLINK_PORT_FLAVOUR_CPU:
531 case DEVLINK_PORT_FLAVOUR_DSA:
532 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
533 attrs->phys.port_number))
534 return -EMSGSIZE;
535 if (!attrs->split)
536 return 0;
537 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
538 attrs->phys.port_number))
539 return -EMSGSIZE;
540 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
541 attrs->phys.split_subport_number))
542 return -EMSGSIZE;
543 break;
544 default:
545 break;
Parav Pandit98fd2d62019-07-08 23:17:37 -0500546 }
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200547 return 0;
548}
549
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100550static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
551 struct devlink_port *devlink_port,
552 enum devlink_command cmd, u32 portid,
553 u32 seq, int flags)
554{
555 void *hdr;
556
557 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
558 if (!hdr)
559 return -EMSGSIZE;
560
561 if (devlink_nl_put_handle(msg, devlink))
562 goto nla_put_failure;
563 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
564 goto nla_put_failure;
Jiri Pirkob8f97552019-03-24 11:14:37 +0100565
Ido Schimmel0f420b62019-08-17 16:28:17 +0300566 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100567 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100568 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100569 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
570 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
571 devlink_port->desired_type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100572 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100573 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
574 struct net_device *netdev = devlink_port->type_dev;
575
576 if (netdev &&
577 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
578 netdev->ifindex) ||
579 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
580 netdev->name)))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100581 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100582 }
583 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
584 struct ib_device *ibdev = devlink_port->type_dev;
585
586 if (ibdev &&
587 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
588 ibdev->name))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100589 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100590 }
Ido Schimmel0f420b62019-08-17 16:28:17 +0300591 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200592 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100593 goto nla_put_failure;
594
595 genlmsg_end(msg, hdr);
596 return 0;
597
Jiri Pirkob8f97552019-03-24 11:14:37 +0100598nla_put_failure_type_locked:
Ido Schimmel0f420b62019-08-17 16:28:17 +0300599 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100600nla_put_failure:
601 genlmsg_cancel(msg, hdr);
602 return -EMSGSIZE;
603}
604
605static void devlink_port_notify(struct devlink_port *devlink_port,
606 enum devlink_command cmd)
607{
608 struct devlink *devlink = devlink_port->devlink;
609 struct sk_buff *msg;
610 int err;
611
612 if (!devlink_port->registered)
613 return;
614
615 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
616
617 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
618 if (!msg)
619 return;
620
621 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
622 if (err) {
623 nlmsg_free(msg);
624 return;
625 }
626
627 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
628 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
629}
630
631static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
632{
633 struct devlink *devlink = info->user_ptr[0];
634 struct sk_buff *msg;
635 int err;
636
637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
638 if (!msg)
639 return -ENOMEM;
640
641 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
642 info->snd_portid, info->snd_seq, 0);
643 if (err) {
644 nlmsg_free(msg);
645 return err;
646 }
647
648 return genlmsg_reply(msg, info);
649}
650
651static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
652 struct netlink_callback *cb)
653{
654 struct devlink *devlink;
655 int start = cb->args[0];
656 int idx = 0;
657 int err;
658
659 mutex_lock(&devlink_mutex);
660 list_for_each_entry(devlink, &devlink_list, list) {
661 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
662 continue;
663 if (idx < start) {
664 idx++;
665 continue;
666 }
667 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
668 NETLINK_CB(cb->skb).portid,
669 cb->nlh->nlmsg_seq, NLM_F_MULTI);
670 if (err)
671 goto out;
672 idx++;
673 }
674out:
675 mutex_unlock(&devlink_mutex);
676
677 cb->args[0] = idx;
678 return msg->len;
679}
680
681static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
682 struct genl_info *info)
683{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200684 struct devlink_port *devlink_port = info->user_ptr[0];
685 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100686 struct sk_buff *msg;
687 int err;
688
689 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
690 if (!msg)
691 return -ENOMEM;
692
693 err = devlink_nl_port_fill(msg, devlink, devlink_port,
694 DEVLINK_CMD_PORT_NEW,
695 info->snd_portid, info->snd_seq, 0);
696 if (err) {
697 nlmsg_free(msg);
698 return err;
699 }
700
701 return genlmsg_reply(msg, info);
702}
703
704static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
705 struct netlink_callback *cb)
706{
707 struct devlink *devlink;
708 struct devlink_port *devlink_port;
709 int start = cb->args[0];
710 int idx = 0;
711 int err;
712
713 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100714 list_for_each_entry(devlink, &devlink_list, list) {
715 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
716 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100717 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100718 list_for_each_entry(devlink_port, &devlink->port_list, list) {
719 if (idx < start) {
720 idx++;
721 continue;
722 }
723 err = devlink_nl_port_fill(msg, devlink, devlink_port,
724 DEVLINK_CMD_NEW,
725 NETLINK_CB(cb->skb).portid,
726 cb->nlh->nlmsg_seq,
727 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100728 if (err) {
729 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100730 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100731 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100732 idx++;
733 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100734 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100735 }
736out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100737 mutex_unlock(&devlink_mutex);
738
739 cb->args[0] = idx;
740 return msg->len;
741}
742
743static int devlink_port_type_set(struct devlink *devlink,
744 struct devlink_port *devlink_port,
745 enum devlink_port_type port_type)
746
747{
748 int err;
749
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800750 if (devlink->ops->port_type_set) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100751 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
752 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200753 if (port_type == devlink_port->type)
754 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100755 err = devlink->ops->port_type_set(devlink_port, port_type);
756 if (err)
757 return err;
758 devlink_port->desired_type = port_type;
759 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
760 return 0;
761 }
762 return -EOPNOTSUPP;
763}
764
765static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
766 struct genl_info *info)
767{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200768 struct devlink_port *devlink_port = info->user_ptr[0];
769 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100770 int err;
771
772 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
773 enum devlink_port_type port_type;
774
775 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
776 err = devlink_port_type_set(devlink, devlink_port, port_type);
777 if (err)
778 return err;
779 }
780 return 0;
781}
782
David Ahernac0fc8a2018-06-05 08:14:09 -0700783static int devlink_port_split(struct devlink *devlink, u32 port_index,
784 u32 count, struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100785
786{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800787 if (devlink->ops->port_split)
David Ahernac0fc8a2018-06-05 08:14:09 -0700788 return devlink->ops->port_split(devlink, port_index, count,
789 extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100790 return -EOPNOTSUPP;
791}
792
793static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
794 struct genl_info *info)
795{
796 struct devlink *devlink = info->user_ptr[0];
797 u32 port_index;
798 u32 count;
799
800 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
801 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
802 return -EINVAL;
803
804 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
805 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700806 return devlink_port_split(devlink, port_index, count, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100807}
808
David Ahernac0fc8a2018-06-05 08:14:09 -0700809static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
810 struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100811
812{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800813 if (devlink->ops->port_unsplit)
David Ahernac0fc8a2018-06-05 08:14:09 -0700814 return devlink->ops->port_unsplit(devlink, port_index, extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100815 return -EOPNOTSUPP;
816}
817
818static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
819 struct genl_info *info)
820{
821 struct devlink *devlink = info->user_ptr[0];
822 u32 port_index;
823
824 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
825 return -EINVAL;
826
827 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700828 return devlink_port_unsplit(devlink, port_index, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100829}
830
Jiri Pirkobf797472016-04-14 18:19:13 +0200831static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
832 struct devlink_sb *devlink_sb,
833 enum devlink_command cmd, u32 portid,
834 u32 seq, int flags)
835{
836 void *hdr;
837
838 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
839 if (!hdr)
840 return -EMSGSIZE;
841
842 if (devlink_nl_put_handle(msg, devlink))
843 goto nla_put_failure;
844 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
845 goto nla_put_failure;
846 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
847 goto nla_put_failure;
848 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
849 devlink_sb->ingress_pools_count))
850 goto nla_put_failure;
851 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
852 devlink_sb->egress_pools_count))
853 goto nla_put_failure;
854 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
855 devlink_sb->ingress_tc_count))
856 goto nla_put_failure;
857 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
858 devlink_sb->egress_tc_count))
859 goto nla_put_failure;
860
861 genlmsg_end(msg, hdr);
862 return 0;
863
864nla_put_failure:
865 genlmsg_cancel(msg, hdr);
866 return -EMSGSIZE;
867}
868
869static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
870 struct genl_info *info)
871{
872 struct devlink *devlink = info->user_ptr[0];
873 struct devlink_sb *devlink_sb = info->user_ptr[1];
874 struct sk_buff *msg;
875 int err;
876
877 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
878 if (!msg)
879 return -ENOMEM;
880
881 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
882 DEVLINK_CMD_SB_NEW,
883 info->snd_portid, info->snd_seq, 0);
884 if (err) {
885 nlmsg_free(msg);
886 return err;
887 }
888
889 return genlmsg_reply(msg, info);
890}
891
892static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
893 struct netlink_callback *cb)
894{
895 struct devlink *devlink;
896 struct devlink_sb *devlink_sb;
897 int start = cb->args[0];
898 int idx = 0;
899 int err;
900
901 mutex_lock(&devlink_mutex);
902 list_for_each_entry(devlink, &devlink_list, list) {
903 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
904 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100905 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200906 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
907 if (idx < start) {
908 idx++;
909 continue;
910 }
911 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
912 DEVLINK_CMD_SB_NEW,
913 NETLINK_CB(cb->skb).portid,
914 cb->nlh->nlmsg_seq,
915 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100916 if (err) {
917 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200918 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100919 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200920 idx++;
921 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100922 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200923 }
924out:
925 mutex_unlock(&devlink_mutex);
926
927 cb->args[0] = idx;
928 return msg->len;
929}
930
931static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
932 struct devlink_sb *devlink_sb,
933 u16 pool_index, enum devlink_command cmd,
934 u32 portid, u32 seq, int flags)
935{
936 struct devlink_sb_pool_info pool_info;
937 void *hdr;
938 int err;
939
940 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
941 pool_index, &pool_info);
942 if (err)
943 return err;
944
945 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
946 if (!hdr)
947 return -EMSGSIZE;
948
949 if (devlink_nl_put_handle(msg, devlink))
950 goto nla_put_failure;
951 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
952 goto nla_put_failure;
953 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
954 goto nla_put_failure;
955 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
956 goto nla_put_failure;
957 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
958 goto nla_put_failure;
959 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
960 pool_info.threshold_type))
961 goto nla_put_failure;
Jakub Kicinskibff57312019-02-01 17:56:28 -0800962 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
963 pool_info.cell_size))
964 goto nla_put_failure;
Jiri Pirkobf797472016-04-14 18:19:13 +0200965
966 genlmsg_end(msg, hdr);
967 return 0;
968
969nla_put_failure:
970 genlmsg_cancel(msg, hdr);
971 return -EMSGSIZE;
972}
973
974static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
975 struct genl_info *info)
976{
977 struct devlink *devlink = info->user_ptr[0];
978 struct devlink_sb *devlink_sb = info->user_ptr[1];
979 struct sk_buff *msg;
980 u16 pool_index;
981 int err;
982
983 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
984 &pool_index);
985 if (err)
986 return err;
987
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800988 if (!devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +0200989 return -EOPNOTSUPP;
990
991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
992 if (!msg)
993 return -ENOMEM;
994
995 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
996 DEVLINK_CMD_SB_POOL_NEW,
997 info->snd_portid, info->snd_seq, 0);
998 if (err) {
999 nlmsg_free(msg);
1000 return err;
1001 }
1002
1003 return genlmsg_reply(msg, info);
1004}
1005
1006static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1007 struct devlink *devlink,
1008 struct devlink_sb *devlink_sb,
1009 u32 portid, u32 seq)
1010{
1011 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1012 u16 pool_index;
1013 int err;
1014
1015 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1016 if (*p_idx < start) {
1017 (*p_idx)++;
1018 continue;
1019 }
1020 err = devlink_nl_sb_pool_fill(msg, devlink,
1021 devlink_sb,
1022 pool_index,
1023 DEVLINK_CMD_SB_POOL_NEW,
1024 portid, seq, NLM_F_MULTI);
1025 if (err)
1026 return err;
1027 (*p_idx)++;
1028 }
1029 return 0;
1030}
1031
1032static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1033 struct netlink_callback *cb)
1034{
1035 struct devlink *devlink;
1036 struct devlink_sb *devlink_sb;
1037 int start = cb->args[0];
1038 int idx = 0;
1039 int err;
1040
1041 mutex_lock(&devlink_mutex);
1042 list_for_each_entry(devlink, &devlink_list, list) {
1043 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001044 !devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001045 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001046 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001047 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1048 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1049 devlink_sb,
1050 NETLINK_CB(cb->skb).portid,
1051 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001052 if (err && err != -EOPNOTSUPP) {
1053 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001054 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001055 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001056 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001057 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001058 }
1059out:
1060 mutex_unlock(&devlink_mutex);
1061
1062 cb->args[0] = idx;
1063 return msg->len;
1064}
1065
1066static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1067 u16 pool_index, u32 size,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001068 enum devlink_sb_threshold_type threshold_type,
1069 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001070
1071{
1072 const struct devlink_ops *ops = devlink->ops;
1073
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001074 if (ops->sb_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001075 return ops->sb_pool_set(devlink, sb_index, pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001076 size, threshold_type, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001077 return -EOPNOTSUPP;
1078}
1079
1080static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1081 struct genl_info *info)
1082{
1083 struct devlink *devlink = info->user_ptr[0];
1084 struct devlink_sb *devlink_sb = info->user_ptr[1];
1085 enum devlink_sb_threshold_type threshold_type;
1086 u16 pool_index;
1087 u32 size;
1088 int err;
1089
1090 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1091 &pool_index);
1092 if (err)
1093 return err;
1094
1095 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1096 if (err)
1097 return err;
1098
1099 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1100 return -EINVAL;
1101
1102 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1103 return devlink_sb_pool_set(devlink, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001104 pool_index, size, threshold_type,
1105 info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001106}
1107
1108static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1109 struct devlink *devlink,
1110 struct devlink_port *devlink_port,
1111 struct devlink_sb *devlink_sb,
1112 u16 pool_index,
1113 enum devlink_command cmd,
1114 u32 portid, u32 seq, int flags)
1115{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001116 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001117 u32 threshold;
1118 void *hdr;
1119 int err;
1120
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001121 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1122 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001123 if (err)
1124 return err;
1125
1126 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1127 if (!hdr)
1128 return -EMSGSIZE;
1129
1130 if (devlink_nl_put_handle(msg, devlink))
1131 goto nla_put_failure;
1132 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1133 goto nla_put_failure;
1134 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1135 goto nla_put_failure;
1136 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1137 goto nla_put_failure;
1138 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1139 goto nla_put_failure;
1140
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001141 if (ops->sb_occ_port_pool_get) {
1142 u32 cur;
1143 u32 max;
1144
1145 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1146 pool_index, &cur, &max);
1147 if (err && err != -EOPNOTSUPP)
1148 return err;
1149 if (!err) {
1150 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1151 goto nla_put_failure;
1152 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1153 goto nla_put_failure;
1154 }
1155 }
1156
Jiri Pirkobf797472016-04-14 18:19:13 +02001157 genlmsg_end(msg, hdr);
1158 return 0;
1159
1160nla_put_failure:
1161 genlmsg_cancel(msg, hdr);
1162 return -EMSGSIZE;
1163}
1164
1165static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1166 struct genl_info *info)
1167{
1168 struct devlink_port *devlink_port = info->user_ptr[0];
1169 struct devlink *devlink = devlink_port->devlink;
1170 struct devlink_sb *devlink_sb = info->user_ptr[1];
1171 struct sk_buff *msg;
1172 u16 pool_index;
1173 int err;
1174
1175 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1176 &pool_index);
1177 if (err)
1178 return err;
1179
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001180 if (!devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001181 return -EOPNOTSUPP;
1182
1183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1184 if (!msg)
1185 return -ENOMEM;
1186
1187 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1188 devlink_sb, pool_index,
1189 DEVLINK_CMD_SB_PORT_POOL_NEW,
1190 info->snd_portid, info->snd_seq, 0);
1191 if (err) {
1192 nlmsg_free(msg);
1193 return err;
1194 }
1195
1196 return genlmsg_reply(msg, info);
1197}
1198
1199static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1200 struct devlink *devlink,
1201 struct devlink_sb *devlink_sb,
1202 u32 portid, u32 seq)
1203{
1204 struct devlink_port *devlink_port;
1205 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1206 u16 pool_index;
1207 int err;
1208
1209 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1210 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1211 if (*p_idx < start) {
1212 (*p_idx)++;
1213 continue;
1214 }
1215 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1216 devlink_port,
1217 devlink_sb,
1218 pool_index,
1219 DEVLINK_CMD_SB_PORT_POOL_NEW,
1220 portid, seq,
1221 NLM_F_MULTI);
1222 if (err)
1223 return err;
1224 (*p_idx)++;
1225 }
1226 }
1227 return 0;
1228}
1229
1230static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1231 struct netlink_callback *cb)
1232{
1233 struct devlink *devlink;
1234 struct devlink_sb *devlink_sb;
1235 int start = cb->args[0];
1236 int idx = 0;
1237 int err;
1238
1239 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001240 list_for_each_entry(devlink, &devlink_list, list) {
1241 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001242 !devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001243 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001244 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001245 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1246 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1247 devlink, devlink_sb,
1248 NETLINK_CB(cb->skb).portid,
1249 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001250 if (err && err != -EOPNOTSUPP) {
1251 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001252 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001253 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001254 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001255 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001256 }
1257out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001258 mutex_unlock(&devlink_mutex);
1259
1260 cb->args[0] = idx;
1261 return msg->len;
1262}
1263
1264static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1265 unsigned int sb_index, u16 pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001266 u32 threshold,
1267 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001268
1269{
1270 const struct devlink_ops *ops = devlink_port->devlink->ops;
1271
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001272 if (ops->sb_port_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001273 return ops->sb_port_pool_set(devlink_port, sb_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001274 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001275 return -EOPNOTSUPP;
1276}
1277
1278static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1279 struct genl_info *info)
1280{
1281 struct devlink_port *devlink_port = info->user_ptr[0];
1282 struct devlink_sb *devlink_sb = info->user_ptr[1];
1283 u16 pool_index;
1284 u32 threshold;
1285 int err;
1286
1287 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1288 &pool_index);
1289 if (err)
1290 return err;
1291
1292 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1293 return -EINVAL;
1294
1295 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1296 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001297 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001298}
1299
1300static int
1301devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1302 struct devlink_port *devlink_port,
1303 struct devlink_sb *devlink_sb, u16 tc_index,
1304 enum devlink_sb_pool_type pool_type,
1305 enum devlink_command cmd,
1306 u32 portid, u32 seq, int flags)
1307{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001308 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001309 u16 pool_index;
1310 u32 threshold;
1311 void *hdr;
1312 int err;
1313
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001314 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1315 tc_index, pool_type,
1316 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001317 if (err)
1318 return err;
1319
1320 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1321 if (!hdr)
1322 return -EMSGSIZE;
1323
1324 if (devlink_nl_put_handle(msg, devlink))
1325 goto nla_put_failure;
1326 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1327 goto nla_put_failure;
1328 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1329 goto nla_put_failure;
1330 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1331 goto nla_put_failure;
1332 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1333 goto nla_put_failure;
1334 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1335 goto nla_put_failure;
1336 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1337 goto nla_put_failure;
1338
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001339 if (ops->sb_occ_tc_port_bind_get) {
1340 u32 cur;
1341 u32 max;
1342
1343 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1344 devlink_sb->index,
1345 tc_index, pool_type,
1346 &cur, &max);
1347 if (err && err != -EOPNOTSUPP)
1348 return err;
1349 if (!err) {
1350 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1351 goto nla_put_failure;
1352 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1353 goto nla_put_failure;
1354 }
1355 }
1356
Jiri Pirkobf797472016-04-14 18:19:13 +02001357 genlmsg_end(msg, hdr);
1358 return 0;
1359
1360nla_put_failure:
1361 genlmsg_cancel(msg, hdr);
1362 return -EMSGSIZE;
1363}
1364
1365static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1366 struct genl_info *info)
1367{
1368 struct devlink_port *devlink_port = info->user_ptr[0];
1369 struct devlink *devlink = devlink_port->devlink;
1370 struct devlink_sb *devlink_sb = info->user_ptr[1];
1371 struct sk_buff *msg;
1372 enum devlink_sb_pool_type pool_type;
1373 u16 tc_index;
1374 int err;
1375
1376 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1377 if (err)
1378 return err;
1379
1380 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1381 pool_type, &tc_index);
1382 if (err)
1383 return err;
1384
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001385 if (!devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001386 return -EOPNOTSUPP;
1387
1388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1389 if (!msg)
1390 return -ENOMEM;
1391
1392 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1393 devlink_sb, tc_index, pool_type,
1394 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1395 info->snd_portid,
1396 info->snd_seq, 0);
1397 if (err) {
1398 nlmsg_free(msg);
1399 return err;
1400 }
1401
1402 return genlmsg_reply(msg, info);
1403}
1404
1405static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1406 int start, int *p_idx,
1407 struct devlink *devlink,
1408 struct devlink_sb *devlink_sb,
1409 u32 portid, u32 seq)
1410{
1411 struct devlink_port *devlink_port;
1412 u16 tc_index;
1413 int err;
1414
1415 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1416 for (tc_index = 0;
1417 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1418 if (*p_idx < start) {
1419 (*p_idx)++;
1420 continue;
1421 }
1422 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1423 devlink_port,
1424 devlink_sb,
1425 tc_index,
1426 DEVLINK_SB_POOL_TYPE_INGRESS,
1427 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1428 portid, seq,
1429 NLM_F_MULTI);
1430 if (err)
1431 return err;
1432 (*p_idx)++;
1433 }
1434 for (tc_index = 0;
1435 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1436 if (*p_idx < start) {
1437 (*p_idx)++;
1438 continue;
1439 }
1440 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1441 devlink_port,
1442 devlink_sb,
1443 tc_index,
1444 DEVLINK_SB_POOL_TYPE_EGRESS,
1445 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1446 portid, seq,
1447 NLM_F_MULTI);
1448 if (err)
1449 return err;
1450 (*p_idx)++;
1451 }
1452 }
1453 return 0;
1454}
1455
1456static int
1457devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1458 struct netlink_callback *cb)
1459{
1460 struct devlink *devlink;
1461 struct devlink_sb *devlink_sb;
1462 int start = cb->args[0];
1463 int idx = 0;
1464 int err;
1465
1466 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001467 list_for_each_entry(devlink, &devlink_list, list) {
1468 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001469 !devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001470 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001471
1472 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001473 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1474 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1475 devlink,
1476 devlink_sb,
1477 NETLINK_CB(cb->skb).portid,
1478 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001479 if (err && err != -EOPNOTSUPP) {
1480 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001481 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001482 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001483 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001484 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001485 }
1486out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001487 mutex_unlock(&devlink_mutex);
1488
1489 cb->args[0] = idx;
1490 return msg->len;
1491}
1492
1493static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1494 unsigned int sb_index, u16 tc_index,
1495 enum devlink_sb_pool_type pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001496 u16 pool_index, u32 threshold,
1497 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001498
1499{
1500 const struct devlink_ops *ops = devlink_port->devlink->ops;
1501
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001502 if (ops->sb_tc_pool_bind_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001503 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1504 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001505 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001506 return -EOPNOTSUPP;
1507}
1508
1509static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1510 struct genl_info *info)
1511{
1512 struct devlink_port *devlink_port = info->user_ptr[0];
1513 struct devlink_sb *devlink_sb = info->user_ptr[1];
1514 enum devlink_sb_pool_type pool_type;
1515 u16 tc_index;
1516 u16 pool_index;
1517 u32 threshold;
1518 int err;
1519
1520 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1521 if (err)
1522 return err;
1523
1524 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1525 pool_type, &tc_index);
1526 if (err)
1527 return err;
1528
1529 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1530 &pool_index);
1531 if (err)
1532 return err;
1533
1534 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1535 return -EINVAL;
1536
1537 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1538 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1539 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001540 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001541}
1542
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001543static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1544 struct genl_info *info)
1545{
1546 struct devlink *devlink = info->user_ptr[0];
1547 struct devlink_sb *devlink_sb = info->user_ptr[1];
1548 const struct devlink_ops *ops = devlink->ops;
1549
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001550 if (ops->sb_occ_snapshot)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001551 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1552 return -EOPNOTSUPP;
1553}
1554
1555static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1556 struct genl_info *info)
1557{
1558 struct devlink *devlink = info->user_ptr[0];
1559 struct devlink_sb *devlink_sb = info->user_ptr[1];
1560 const struct devlink_ops *ops = devlink->ops;
1561
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001562 if (ops->sb_occ_max_clear)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001563 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1564 return -EOPNOTSUPP;
1565}
1566
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001567static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1568 enum devlink_command cmd, u32 portid,
1569 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001570{
Roi Dayan59bfde02016-11-22 23:09:57 +02001571 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001572 enum devlink_eswitch_encap_mode encap_mode;
1573 u8 inline_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001574 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001575 int err = 0;
1576 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001577
1578 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1579 if (!hdr)
1580 return -EMSGSIZE;
1581
Roi Dayan59bfde02016-11-22 23:09:57 +02001582 err = devlink_nl_put_handle(msg, devlink);
1583 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001584 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001585
Jiri Pirko4456f612017-02-09 15:54:36 +01001586 if (ops->eswitch_mode_get) {
1587 err = ops->eswitch_mode_get(devlink, &mode);
1588 if (err)
1589 goto nla_put_failure;
1590 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1591 if (err)
1592 goto nla_put_failure;
1593 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001594
1595 if (ops->eswitch_inline_mode_get) {
1596 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1597 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001598 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001599 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1600 inline_mode);
1601 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001602 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001603 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001604
Roi Dayanf43e9b02016-09-25 13:52:44 +03001605 if (ops->eswitch_encap_mode_get) {
1606 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1607 if (err)
1608 goto nla_put_failure;
1609 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1610 if (err)
1611 goto nla_put_failure;
1612 }
1613
Or Gerlitz08f4b592016-07-01 14:51:01 +03001614 genlmsg_end(msg, hdr);
1615 return 0;
1616
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001617nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001618 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001619 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001620}
1621
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001622static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1623 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001624{
1625 struct devlink *devlink = info->user_ptr[0];
Or Gerlitz08f4b592016-07-01 14:51:01 +03001626 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001627 int err;
1628
Or Gerlitz08f4b592016-07-01 14:51:01 +03001629 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1630 if (!msg)
1631 return -ENOMEM;
1632
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001633 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1634 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001635
1636 if (err) {
1637 nlmsg_free(msg);
1638 return err;
1639 }
1640
1641 return genlmsg_reply(msg, info);
1642}
1643
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001644static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1645 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001646{
1647 struct devlink *devlink = info->user_ptr[0];
1648 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001649 enum devlink_eswitch_encap_mode encap_mode;
1650 u8 inline_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001651 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001652 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001653
Roi Dayan59bfde02016-11-22 23:09:57 +02001654 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1655 if (!ops->eswitch_mode_set)
1656 return -EOPNOTSUPP;
1657 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001658 err = ops->eswitch_mode_set(devlink, mode, info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001659 if (err)
1660 return err;
1661 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001662
Roi Dayan59bfde02016-11-22 23:09:57 +02001663 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1664 if (!ops->eswitch_inline_mode_set)
1665 return -EOPNOTSUPP;
1666 inline_mode = nla_get_u8(
1667 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001668 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1669 info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001670 if (err)
1671 return err;
1672 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001673
1674 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1675 if (!ops->eswitch_encap_mode_set)
1676 return -EOPNOTSUPP;
1677 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001678 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1679 info->extack);
Roi Dayanf43e9b02016-09-25 13:52:44 +03001680 if (err)
1681 return err;
1682 }
1683
Roi Dayan59bfde02016-11-22 23:09:57 +02001684 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001685}
1686
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001687int devlink_dpipe_match_put(struct sk_buff *skb,
1688 struct devlink_dpipe_match *match)
1689{
1690 struct devlink_dpipe_header *header = match->header;
1691 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1692 struct nlattr *match_attr;
1693
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001694 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001695 if (!match_attr)
1696 return -EMSGSIZE;
1697
1698 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1699 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1700 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1701 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1702 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1703 goto nla_put_failure;
1704
1705 nla_nest_end(skb, match_attr);
1706 return 0;
1707
1708nla_put_failure:
1709 nla_nest_cancel(skb, match_attr);
1710 return -EMSGSIZE;
1711}
1712EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1713
1714static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1715 struct sk_buff *skb)
1716{
1717 struct nlattr *matches_attr;
1718
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001719 matches_attr = nla_nest_start_noflag(skb,
1720 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001721 if (!matches_attr)
1722 return -EMSGSIZE;
1723
1724 if (table->table_ops->matches_dump(table->priv, skb))
1725 goto nla_put_failure;
1726
1727 nla_nest_end(skb, matches_attr);
1728 return 0;
1729
1730nla_put_failure:
1731 nla_nest_cancel(skb, matches_attr);
1732 return -EMSGSIZE;
1733}
1734
1735int devlink_dpipe_action_put(struct sk_buff *skb,
1736 struct devlink_dpipe_action *action)
1737{
1738 struct devlink_dpipe_header *header = action->header;
1739 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1740 struct nlattr *action_attr;
1741
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001742 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001743 if (!action_attr)
1744 return -EMSGSIZE;
1745
1746 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1747 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1748 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1749 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1750 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1751 goto nla_put_failure;
1752
1753 nla_nest_end(skb, action_attr);
1754 return 0;
1755
1756nla_put_failure:
1757 nla_nest_cancel(skb, action_attr);
1758 return -EMSGSIZE;
1759}
1760EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1761
1762static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1763 struct sk_buff *skb)
1764{
1765 struct nlattr *actions_attr;
1766
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001767 actions_attr = nla_nest_start_noflag(skb,
1768 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001769 if (!actions_attr)
1770 return -EMSGSIZE;
1771
1772 if (table->table_ops->actions_dump(table->priv, skb))
1773 goto nla_put_failure;
1774
1775 nla_nest_end(skb, actions_attr);
1776 return 0;
1777
1778nla_put_failure:
1779 nla_nest_cancel(skb, actions_attr);
1780 return -EMSGSIZE;
1781}
1782
1783static int devlink_dpipe_table_put(struct sk_buff *skb,
1784 struct devlink_dpipe_table *table)
1785{
1786 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001787 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001788
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001789 table_size = table->table_ops->size_get(table->priv);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001790 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001791 if (!table_attr)
1792 return -EMSGSIZE;
1793
1794 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001795 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001796 DEVLINK_ATTR_PAD))
1797 goto nla_put_failure;
1798 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1799 table->counters_enabled))
1800 goto nla_put_failure;
1801
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001802 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001803 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1804 table->resource_id, DEVLINK_ATTR_PAD) ||
1805 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1806 table->resource_units, DEVLINK_ATTR_PAD))
1807 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001808 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001809 if (devlink_dpipe_matches_put(table, skb))
1810 goto nla_put_failure;
1811
1812 if (devlink_dpipe_actions_put(table, skb))
1813 goto nla_put_failure;
1814
1815 nla_nest_end(skb, table_attr);
1816 return 0;
1817
1818nla_put_failure:
1819 nla_nest_cancel(skb, table_attr);
1820 return -EMSGSIZE;
1821}
1822
1823static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1824 struct genl_info *info)
1825{
1826 int err;
1827
1828 if (*pskb) {
1829 err = genlmsg_reply(*pskb, info);
1830 if (err)
1831 return err;
1832 }
1833 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1834 if (!*pskb)
1835 return -ENOMEM;
1836 return 0;
1837}
1838
1839static int devlink_dpipe_tables_fill(struct genl_info *info,
1840 enum devlink_command cmd, int flags,
1841 struct list_head *dpipe_tables,
1842 const char *table_name)
1843{
1844 struct devlink *devlink = info->user_ptr[0];
1845 struct devlink_dpipe_table *table;
1846 struct nlattr *tables_attr;
1847 struct sk_buff *skb = NULL;
1848 struct nlmsghdr *nlh;
1849 bool incomplete;
1850 void *hdr;
1851 int i;
1852 int err;
1853
1854 table = list_first_entry(dpipe_tables,
1855 struct devlink_dpipe_table, list);
1856start_again:
1857 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1858 if (err)
1859 return err;
1860
1861 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1862 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001863 if (!hdr) {
1864 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001865 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001866 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001867
1868 if (devlink_nl_put_handle(skb, devlink))
1869 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001870 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001871 if (!tables_attr)
1872 goto nla_put_failure;
1873
1874 i = 0;
1875 incomplete = false;
1876 list_for_each_entry_from(table, dpipe_tables, list) {
1877 if (!table_name) {
1878 err = devlink_dpipe_table_put(skb, table);
1879 if (err) {
1880 if (!i)
1881 goto err_table_put;
1882 incomplete = true;
1883 break;
1884 }
1885 } else {
1886 if (!strcmp(table->name, table_name)) {
1887 err = devlink_dpipe_table_put(skb, table);
1888 if (err)
1889 break;
1890 }
1891 }
1892 i++;
1893 }
1894
1895 nla_nest_end(skb, tables_attr);
1896 genlmsg_end(skb, hdr);
1897 if (incomplete)
1898 goto start_again;
1899
1900send_done:
1901 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1902 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1903 if (!nlh) {
1904 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1905 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001906 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001907 goto send_done;
1908 }
1909
1910 return genlmsg_reply(skb, info);
1911
1912nla_put_failure:
1913 err = -EMSGSIZE;
1914err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001915 nlmsg_free(skb);
1916 return err;
1917}
1918
1919static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1920 struct genl_info *info)
1921{
1922 struct devlink *devlink = info->user_ptr[0];
1923 const char *table_name = NULL;
1924
1925 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1926 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1927
1928 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1929 &devlink->dpipe_table_list,
1930 table_name);
1931}
1932
1933static int devlink_dpipe_value_put(struct sk_buff *skb,
1934 struct devlink_dpipe_value *value)
1935{
1936 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1937 value->value_size, value->value))
1938 return -EMSGSIZE;
1939 if (value->mask)
1940 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1941 value->value_size, value->mask))
1942 return -EMSGSIZE;
1943 if (value->mapping_valid)
1944 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1945 value->mapping_value))
1946 return -EMSGSIZE;
1947 return 0;
1948}
1949
1950static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1951 struct devlink_dpipe_value *value)
1952{
1953 if (!value->action)
1954 return -EINVAL;
1955 if (devlink_dpipe_action_put(skb, value->action))
1956 return -EMSGSIZE;
1957 if (devlink_dpipe_value_put(skb, value))
1958 return -EMSGSIZE;
1959 return 0;
1960}
1961
1962static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1963 struct devlink_dpipe_value *values,
1964 unsigned int values_count)
1965{
1966 struct nlattr *action_attr;
1967 int i;
1968 int err;
1969
1970 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001971 action_attr = nla_nest_start_noflag(skb,
1972 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001973 if (!action_attr)
1974 return -EMSGSIZE;
1975 err = devlink_dpipe_action_value_put(skb, &values[i]);
1976 if (err)
1977 goto err_action_value_put;
1978 nla_nest_end(skb, action_attr);
1979 }
1980 return 0;
1981
1982err_action_value_put:
1983 nla_nest_cancel(skb, action_attr);
1984 return err;
1985}
1986
1987static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1988 struct devlink_dpipe_value *value)
1989{
1990 if (!value->match)
1991 return -EINVAL;
1992 if (devlink_dpipe_match_put(skb, value->match))
1993 return -EMSGSIZE;
1994 if (devlink_dpipe_value_put(skb, value))
1995 return -EMSGSIZE;
1996 return 0;
1997}
1998
1999static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2000 struct devlink_dpipe_value *values,
2001 unsigned int values_count)
2002{
2003 struct nlattr *match_attr;
2004 int i;
2005 int err;
2006
2007 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002008 match_attr = nla_nest_start_noflag(skb,
2009 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002010 if (!match_attr)
2011 return -EMSGSIZE;
2012 err = devlink_dpipe_match_value_put(skb, &values[i]);
2013 if (err)
2014 goto err_match_value_put;
2015 nla_nest_end(skb, match_attr);
2016 }
2017 return 0;
2018
2019err_match_value_put:
2020 nla_nest_cancel(skb, match_attr);
2021 return err;
2022}
2023
2024static int devlink_dpipe_entry_put(struct sk_buff *skb,
2025 struct devlink_dpipe_entry *entry)
2026{
2027 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2028 int err;
2029
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002030 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002031 if (!entry_attr)
2032 return -EMSGSIZE;
2033
2034 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2035 DEVLINK_ATTR_PAD))
2036 goto nla_put_failure;
2037 if (entry->counter_valid)
2038 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2039 entry->counter, DEVLINK_ATTR_PAD))
2040 goto nla_put_failure;
2041
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002042 matches_attr = nla_nest_start_noflag(skb,
2043 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002044 if (!matches_attr)
2045 goto nla_put_failure;
2046
2047 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2048 entry->match_values_count);
2049 if (err) {
2050 nla_nest_cancel(skb, matches_attr);
2051 goto err_match_values_put;
2052 }
2053 nla_nest_end(skb, matches_attr);
2054
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002055 actions_attr = nla_nest_start_noflag(skb,
2056 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002057 if (!actions_attr)
2058 goto nla_put_failure;
2059
2060 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2061 entry->action_values_count);
2062 if (err) {
2063 nla_nest_cancel(skb, actions_attr);
2064 goto err_action_values_put;
2065 }
2066 nla_nest_end(skb, actions_attr);
2067
2068 nla_nest_end(skb, entry_attr);
2069 return 0;
2070
2071nla_put_failure:
2072 err = -EMSGSIZE;
2073err_match_values_put:
2074err_action_values_put:
2075 nla_nest_cancel(skb, entry_attr);
2076 return err;
2077}
2078
2079static struct devlink_dpipe_table *
2080devlink_dpipe_table_find(struct list_head *dpipe_tables,
2081 const char *table_name)
2082{
2083 struct devlink_dpipe_table *table;
2084
2085 list_for_each_entry_rcu(table, dpipe_tables, list) {
2086 if (!strcmp(table->name, table_name))
2087 return table;
2088 }
2089 return NULL;
2090}
2091
2092int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2093{
2094 struct devlink *devlink;
2095 int err;
2096
2097 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2098 dump_ctx->info);
2099 if (err)
2100 return err;
2101
2102 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2103 dump_ctx->info->snd_portid,
2104 dump_ctx->info->snd_seq,
2105 &devlink_nl_family, NLM_F_MULTI,
2106 dump_ctx->cmd);
2107 if (!dump_ctx->hdr)
2108 goto nla_put_failure;
2109
2110 devlink = dump_ctx->info->user_ptr[0];
2111 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2112 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002113 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2114 DEVLINK_ATTR_DPIPE_ENTRIES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002115 if (!dump_ctx->nest)
2116 goto nla_put_failure;
2117 return 0;
2118
2119nla_put_failure:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002120 nlmsg_free(dump_ctx->skb);
2121 return -EMSGSIZE;
2122}
2123EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2124
2125int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2126 struct devlink_dpipe_entry *entry)
2127{
2128 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2129}
2130EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2131
2132int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2133{
2134 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2135 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2136 return 0;
2137}
2138EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2139
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002140void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2141
2142{
2143 unsigned int value_count, value_index;
2144 struct devlink_dpipe_value *value;
2145
2146 value = entry->action_values;
2147 value_count = entry->action_values_count;
2148 for (value_index = 0; value_index < value_count; value_index++) {
2149 kfree(value[value_index].value);
2150 kfree(value[value_index].mask);
2151 }
2152
2153 value = entry->match_values;
2154 value_count = entry->match_values_count;
2155 for (value_index = 0; value_index < value_count; value_index++) {
2156 kfree(value[value_index].value);
2157 kfree(value[value_index].mask);
2158 }
2159}
2160EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2161
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002162static int devlink_dpipe_entries_fill(struct genl_info *info,
2163 enum devlink_command cmd, int flags,
2164 struct devlink_dpipe_table *table)
2165{
2166 struct devlink_dpipe_dump_ctx dump_ctx;
2167 struct nlmsghdr *nlh;
2168 int err;
2169
2170 dump_ctx.skb = NULL;
2171 dump_ctx.cmd = cmd;
2172 dump_ctx.info = info;
2173
2174 err = table->table_ops->entries_dump(table->priv,
2175 table->counters_enabled,
2176 &dump_ctx);
2177 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002178 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002179
2180send_done:
2181 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2182 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2183 if (!nlh) {
2184 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2185 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002186 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002187 goto send_done;
2188 }
2189 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002190}
2191
2192static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2193 struct genl_info *info)
2194{
2195 struct devlink *devlink = info->user_ptr[0];
2196 struct devlink_dpipe_table *table;
2197 const char *table_name;
2198
2199 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2200 return -EINVAL;
2201
2202 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2203 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2204 table_name);
2205 if (!table)
2206 return -EINVAL;
2207
2208 if (!table->table_ops->entries_dump)
2209 return -EINVAL;
2210
2211 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2212 0, table);
2213}
2214
2215static int devlink_dpipe_fields_put(struct sk_buff *skb,
2216 const struct devlink_dpipe_header *header)
2217{
2218 struct devlink_dpipe_field *field;
2219 struct nlattr *field_attr;
2220 int i;
2221
2222 for (i = 0; i < header->fields_count; i++) {
2223 field = &header->fields[i];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002224 field_attr = nla_nest_start_noflag(skb,
2225 DEVLINK_ATTR_DPIPE_FIELD);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002226 if (!field_attr)
2227 return -EMSGSIZE;
2228 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2229 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2230 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2231 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2232 goto nla_put_failure;
2233 nla_nest_end(skb, field_attr);
2234 }
2235 return 0;
2236
2237nla_put_failure:
2238 nla_nest_cancel(skb, field_attr);
2239 return -EMSGSIZE;
2240}
2241
2242static int devlink_dpipe_header_put(struct sk_buff *skb,
2243 struct devlink_dpipe_header *header)
2244{
2245 struct nlattr *fields_attr, *header_attr;
2246 int err;
2247
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002248 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002249 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002250 return -EMSGSIZE;
2251
2252 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2253 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2254 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2255 goto nla_put_failure;
2256
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002257 fields_attr = nla_nest_start_noflag(skb,
2258 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002259 if (!fields_attr)
2260 goto nla_put_failure;
2261
2262 err = devlink_dpipe_fields_put(skb, header);
2263 if (err) {
2264 nla_nest_cancel(skb, fields_attr);
2265 goto nla_put_failure;
2266 }
2267 nla_nest_end(skb, fields_attr);
2268 nla_nest_end(skb, header_attr);
2269 return 0;
2270
2271nla_put_failure:
2272 err = -EMSGSIZE;
2273 nla_nest_cancel(skb, header_attr);
2274 return err;
2275}
2276
2277static int devlink_dpipe_headers_fill(struct genl_info *info,
2278 enum devlink_command cmd, int flags,
2279 struct devlink_dpipe_headers *
2280 dpipe_headers)
2281{
2282 struct devlink *devlink = info->user_ptr[0];
2283 struct nlattr *headers_attr;
2284 struct sk_buff *skb = NULL;
2285 struct nlmsghdr *nlh;
2286 void *hdr;
2287 int i, j;
2288 int err;
2289
2290 i = 0;
2291start_again:
2292 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2293 if (err)
2294 return err;
2295
2296 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2297 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002298 if (!hdr) {
2299 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002300 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002301 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002302
2303 if (devlink_nl_put_handle(skb, devlink))
2304 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002305 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002306 if (!headers_attr)
2307 goto nla_put_failure;
2308
2309 j = 0;
2310 for (; i < dpipe_headers->headers_count; i++) {
2311 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2312 if (err) {
2313 if (!j)
2314 goto err_table_put;
2315 break;
2316 }
2317 j++;
2318 }
2319 nla_nest_end(skb, headers_attr);
2320 genlmsg_end(skb, hdr);
2321 if (i != dpipe_headers->headers_count)
2322 goto start_again;
2323
2324send_done:
2325 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2326 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2327 if (!nlh) {
2328 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2329 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002330 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002331 goto send_done;
2332 }
2333 return genlmsg_reply(skb, info);
2334
2335nla_put_failure:
2336 err = -EMSGSIZE;
2337err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002338 nlmsg_free(skb);
2339 return err;
2340}
2341
2342static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2343 struct genl_info *info)
2344{
2345 struct devlink *devlink = info->user_ptr[0];
2346
2347 if (!devlink->dpipe_headers)
2348 return -EOPNOTSUPP;
2349 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2350 0, devlink->dpipe_headers);
2351}
2352
2353static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2354 const char *table_name,
2355 bool enable)
2356{
2357 struct devlink_dpipe_table *table;
2358
2359 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2360 table_name);
2361 if (!table)
2362 return -EINVAL;
2363
2364 if (table->counter_control_extern)
2365 return -EOPNOTSUPP;
2366
2367 if (!(table->counters_enabled ^ enable))
2368 return 0;
2369
2370 table->counters_enabled = enable;
2371 if (table->table_ops->counters_set_update)
2372 table->table_ops->counters_set_update(table->priv, enable);
2373 return 0;
2374}
2375
2376static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2377 struct genl_info *info)
2378{
2379 struct devlink *devlink = info->user_ptr[0];
2380 const char *table_name;
2381 bool counters_enable;
2382
2383 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2384 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2385 return -EINVAL;
2386
2387 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2388 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2389
2390 return devlink_dpipe_table_counters_set(devlink, table_name,
2391 counters_enable);
2392}
2393
Wei Yongjun43dd7512018-01-17 03:27:42 +00002394static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002395devlink_resource_find(struct devlink *devlink,
2396 struct devlink_resource *resource, u64 resource_id)
2397{
2398 struct list_head *resource_list;
2399
2400 if (resource)
2401 resource_list = &resource->resource_list;
2402 else
2403 resource_list = &devlink->resource_list;
2404
2405 list_for_each_entry(resource, resource_list, list) {
2406 struct devlink_resource *child_resource;
2407
2408 if (resource->id == resource_id)
2409 return resource;
2410
2411 child_resource = devlink_resource_find(devlink, resource,
2412 resource_id);
2413 if (child_resource)
2414 return child_resource;
2415 }
2416 return NULL;
2417}
2418
Wei Yongjun43dd7512018-01-17 03:27:42 +00002419static void
2420devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002421{
2422 struct devlink_resource *child_resource;
2423 bool size_valid = true;
2424 u64 parts_size = 0;
2425
2426 if (list_empty(&resource->resource_list))
2427 goto out;
2428
2429 list_for_each_entry(child_resource, &resource->resource_list, list)
2430 parts_size += child_resource->size_new;
2431
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002432 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002433 size_valid = false;
2434out:
2435 resource->size_valid = size_valid;
2436}
2437
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002438static int
2439devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2440 struct netlink_ext_ack *extack)
2441{
2442 u64 reminder;
2443 int err = 0;
2444
David S. Miller0f3e9c92018-03-06 00:53:44 -05002445 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002446 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2447 err = -EINVAL;
2448 }
2449
David S. Miller0f3e9c92018-03-06 00:53:44 -05002450 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002451 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2452 err = -EINVAL;
2453 }
2454
David S. Miller0f3e9c92018-03-06 00:53:44 -05002455 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002456 if (reminder) {
2457 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2458 err = -EINVAL;
2459 }
2460
2461 return err;
2462}
2463
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002464static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2465 struct genl_info *info)
2466{
2467 struct devlink *devlink = info->user_ptr[0];
2468 struct devlink_resource *resource;
2469 u64 resource_id;
2470 u64 size;
2471 int err;
2472
2473 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2474 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2475 return -EINVAL;
2476 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2477
2478 resource = devlink_resource_find(devlink, NULL, resource_id);
2479 if (!resource)
2480 return -EINVAL;
2481
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002482 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002483 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002484 if (err)
2485 return err;
2486
2487 resource->size_new = size;
2488 devlink_resource_validate_children(resource);
2489 if (resource->parent)
2490 devlink_resource_validate_children(resource->parent);
2491 return 0;
2492}
2493
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002494static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002495devlink_resource_size_params_put(struct devlink_resource *resource,
2496 struct sk_buff *skb)
2497{
2498 struct devlink_resource_size_params *size_params;
2499
Jiri Pirko77d27092018-02-28 13:12:09 +01002500 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002501 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2502 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2503 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2504 size_params->size_max, DEVLINK_ATTR_PAD) ||
2505 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2506 size_params->size_min, DEVLINK_ATTR_PAD) ||
2507 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2508 return -EMSGSIZE;
2509 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002510}
2511
Jiri Pirkofc56be42018-04-05 22:13:21 +02002512static int devlink_resource_occ_put(struct devlink_resource *resource,
2513 struct sk_buff *skb)
2514{
2515 if (!resource->occ_get)
2516 return 0;
2517 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2518 resource->occ_get(resource->occ_get_priv),
2519 DEVLINK_ATTR_PAD);
2520}
2521
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002522static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2523 struct devlink_resource *resource)
2524{
2525 struct devlink_resource *child_resource;
2526 struct nlattr *child_resource_attr;
2527 struct nlattr *resource_attr;
2528
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002529 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002530 if (!resource_attr)
2531 return -EMSGSIZE;
2532
2533 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2534 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2535 DEVLINK_ATTR_PAD) ||
2536 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2537 DEVLINK_ATTR_PAD))
2538 goto nla_put_failure;
2539 if (resource->size != resource->size_new)
2540 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2541 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002542 if (devlink_resource_occ_put(resource, skb))
2543 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002544 if (devlink_resource_size_params_put(resource, skb))
2545 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002546 if (list_empty(&resource->resource_list))
2547 goto out;
2548
2549 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2550 resource->size_valid))
2551 goto nla_put_failure;
2552
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002553 child_resource_attr = nla_nest_start_noflag(skb,
2554 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002555 if (!child_resource_attr)
2556 goto nla_put_failure;
2557
2558 list_for_each_entry(child_resource, &resource->resource_list, list) {
2559 if (devlink_resource_put(devlink, skb, child_resource))
2560 goto resource_put_failure;
2561 }
2562
2563 nla_nest_end(skb, child_resource_attr);
2564out:
2565 nla_nest_end(skb, resource_attr);
2566 return 0;
2567
2568resource_put_failure:
2569 nla_nest_cancel(skb, child_resource_attr);
2570nla_put_failure:
2571 nla_nest_cancel(skb, resource_attr);
2572 return -EMSGSIZE;
2573}
2574
2575static int devlink_resource_fill(struct genl_info *info,
2576 enum devlink_command cmd, int flags)
2577{
2578 struct devlink *devlink = info->user_ptr[0];
2579 struct devlink_resource *resource;
2580 struct nlattr *resources_attr;
2581 struct sk_buff *skb = NULL;
2582 struct nlmsghdr *nlh;
2583 bool incomplete;
2584 void *hdr;
2585 int i;
2586 int err;
2587
2588 resource = list_first_entry(&devlink->resource_list,
2589 struct devlink_resource, list);
2590start_again:
2591 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2592 if (err)
2593 return err;
2594
2595 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2596 &devlink_nl_family, NLM_F_MULTI, cmd);
2597 if (!hdr) {
2598 nlmsg_free(skb);
2599 return -EMSGSIZE;
2600 }
2601
2602 if (devlink_nl_put_handle(skb, devlink))
2603 goto nla_put_failure;
2604
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002605 resources_attr = nla_nest_start_noflag(skb,
2606 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002607 if (!resources_attr)
2608 goto nla_put_failure;
2609
2610 incomplete = false;
2611 i = 0;
2612 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2613 err = devlink_resource_put(devlink, skb, resource);
2614 if (err) {
2615 if (!i)
2616 goto err_resource_put;
2617 incomplete = true;
2618 break;
2619 }
2620 i++;
2621 }
2622 nla_nest_end(skb, resources_attr);
2623 genlmsg_end(skb, hdr);
2624 if (incomplete)
2625 goto start_again;
2626send_done:
2627 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2628 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2629 if (!nlh) {
2630 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2631 if (err)
Dan Carpenter83fe9a92018-09-21 11:07:55 +03002632 return err;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002633 goto send_done;
2634 }
2635 return genlmsg_reply(skb, info);
2636
2637nla_put_failure:
2638 err = -EMSGSIZE;
2639err_resource_put:
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002640 nlmsg_free(skb);
2641 return err;
2642}
2643
2644static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2645 struct genl_info *info)
2646{
2647 struct devlink *devlink = info->user_ptr[0];
2648
2649 if (list_empty(&devlink->resource_list))
2650 return -EOPNOTSUPP;
2651
2652 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2653}
2654
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002655static int
2656devlink_resources_validate(struct devlink *devlink,
2657 struct devlink_resource *resource,
2658 struct genl_info *info)
2659{
2660 struct list_head *resource_list;
2661 int err = 0;
2662
2663 if (resource)
2664 resource_list = &resource->resource_list;
2665 else
2666 resource_list = &devlink->resource_list;
2667
2668 list_for_each_entry(resource, resource_list, list) {
2669 if (!resource->size_valid)
2670 return -EINVAL;
2671 err = devlink_resources_validate(devlink, resource, info);
2672 if (err)
2673 return err;
2674 }
2675 return err;
2676}
2677
Jiri Pirko97691062019-09-12 10:49:45 +02002678static bool devlink_reload_supported(struct devlink *devlink)
2679{
2680 return devlink->ops->reload_down && devlink->ops->reload_up;
2681}
2682
Jiri Pirko2670ac22019-09-12 10:49:46 +02002683static void devlink_reload_failed_set(struct devlink *devlink,
2684 bool reload_failed)
2685{
2686 if (devlink->reload_failed == reload_failed)
2687 return;
2688 devlink->reload_failed = reload_failed;
2689 devlink_notify(devlink, DEVLINK_CMD_NEW);
2690}
2691
2692bool devlink_is_reload_failed(const struct devlink *devlink)
2693{
2694 return devlink->reload_failed;
2695}
2696EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
2697
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002698static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2699{
2700 struct devlink *devlink = info->user_ptr[0];
2701 int err;
2702
Jiri Pirko97691062019-09-12 10:49:45 +02002703 if (!devlink_reload_supported(devlink))
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002704 return -EOPNOTSUPP;
2705
2706 err = devlink_resources_validate(devlink, NULL, info);
2707 if (err) {
2708 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2709 return err;
2710 }
Jiri Pirko97691062019-09-12 10:49:45 +02002711 err = devlink->ops->reload_down(devlink, info->extack);
2712 if (err)
2713 return err;
Jiri Pirko2670ac22019-09-12 10:49:46 +02002714 err = devlink->ops->reload_up(devlink, info->extack);
2715 devlink_reload_failed_set(devlink, !!err);
2716 return err;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002717}
2718
Jiri Pirko191ed202019-06-04 15:40:40 +02002719static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2720 struct devlink *devlink,
2721 enum devlink_command cmd,
2722 const char *status_msg,
2723 const char *component,
2724 unsigned long done, unsigned long total)
2725{
2726 void *hdr;
2727
2728 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2729 if (!hdr)
2730 return -EMSGSIZE;
2731
2732 if (devlink_nl_put_handle(msg, devlink))
2733 goto nla_put_failure;
2734
2735 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2736 goto out;
2737
2738 if (status_msg &&
2739 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2740 status_msg))
2741 goto nla_put_failure;
2742 if (component &&
2743 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2744 component))
2745 goto nla_put_failure;
2746 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2747 done, DEVLINK_ATTR_PAD))
2748 goto nla_put_failure;
2749 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2750 total, DEVLINK_ATTR_PAD))
2751 goto nla_put_failure;
2752
2753out:
2754 genlmsg_end(msg, hdr);
2755 return 0;
2756
2757nla_put_failure:
2758 genlmsg_cancel(msg, hdr);
2759 return -EMSGSIZE;
2760}
2761
2762static void __devlink_flash_update_notify(struct devlink *devlink,
2763 enum devlink_command cmd,
2764 const char *status_msg,
2765 const char *component,
2766 unsigned long done,
2767 unsigned long total)
2768{
2769 struct sk_buff *msg;
2770 int err;
2771
2772 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2773 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2774 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2775
2776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2777 if (!msg)
2778 return;
2779
2780 err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2781 component, done, total);
2782 if (err)
2783 goto out_free_msg;
2784
2785 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2786 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2787 return;
2788
2789out_free_msg:
2790 nlmsg_free(msg);
2791}
2792
2793void devlink_flash_update_begin_notify(struct devlink *devlink)
2794{
2795 __devlink_flash_update_notify(devlink,
2796 DEVLINK_CMD_FLASH_UPDATE,
2797 NULL, NULL, 0, 0);
2798}
2799EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2800
2801void devlink_flash_update_end_notify(struct devlink *devlink)
2802{
2803 __devlink_flash_update_notify(devlink,
2804 DEVLINK_CMD_FLASH_UPDATE_END,
2805 NULL, NULL, 0, 0);
2806}
2807EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2808
2809void devlink_flash_update_status_notify(struct devlink *devlink,
2810 const char *status_msg,
2811 const char *component,
2812 unsigned long done,
2813 unsigned long total)
2814{
2815 __devlink_flash_update_notify(devlink,
2816 DEVLINK_CMD_FLASH_UPDATE_STATUS,
2817 status_msg, component, done, total);
2818}
2819EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2820
Jakub Kicinski76726cc2019-02-14 13:40:44 -08002821static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2822 struct genl_info *info)
2823{
2824 struct devlink *devlink = info->user_ptr[0];
2825 const char *file_name, *component;
2826 struct nlattr *nla_component;
2827
2828 if (!devlink->ops->flash_update)
2829 return -EOPNOTSUPP;
2830
2831 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2832 return -EINVAL;
2833 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2834
2835 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2836 component = nla_component ? nla_data(nla_component) : NULL;
2837
2838 return devlink->ops->flash_update(devlink, file_name, component,
2839 info->extack);
2840}
2841
Moshe Shemesh036467c2018-07-04 14:30:33 +03002842static const struct devlink_param devlink_param_generic[] = {
2843 {
2844 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2845 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2846 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2847 },
2848 {
2849 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2850 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2851 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2852 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002853 {
2854 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2855 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2856 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2857 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002858 {
2859 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2860 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2861 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2862 },
Vasundhara Volame3b51062018-10-04 11:13:44 +05302863 {
2864 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2865 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2866 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2867 },
Vasundhara Volamf61cba42018-10-04 11:13:45 +05302868 {
2869 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2870 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2871 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2872 },
Vasundhara Volam16511782018-10-04 11:13:46 +05302873 {
2874 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2875 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2876 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2877 },
Shalom Toledo846e9802018-12-03 07:58:59 +00002878 {
2879 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2880 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2881 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2882 },
Dirk van der Merwe5bbd21d2019-09-09 00:54:18 +01002883 {
2884 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
2885 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
2886 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
2887 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03002888};
Moshe Shemesheabaef12018-07-04 14:30:28 +03002889
2890static int devlink_param_generic_verify(const struct devlink_param *param)
2891{
2892 /* verify it match generic parameter by id and name */
2893 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2894 return -EINVAL;
2895 if (strcmp(param->name, devlink_param_generic[param->id].name))
2896 return -ENOENT;
2897
2898 WARN_ON(param->type != devlink_param_generic[param->id].type);
2899
2900 return 0;
2901}
2902
2903static int devlink_param_driver_verify(const struct devlink_param *param)
2904{
2905 int i;
2906
2907 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2908 return -EINVAL;
2909 /* verify no such name in generic params */
2910 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2911 if (!strcmp(param->name, devlink_param_generic[i].name))
2912 return -EEXIST;
2913
2914 return 0;
2915}
2916
2917static struct devlink_param_item *
2918devlink_param_find_by_name(struct list_head *param_list,
2919 const char *param_name)
2920{
2921 struct devlink_param_item *param_item;
2922
2923 list_for_each_entry(param_item, param_list, list)
2924 if (!strcmp(param_item->param->name, param_name))
2925 return param_item;
2926 return NULL;
2927}
2928
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03002929static struct devlink_param_item *
2930devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2931{
2932 struct devlink_param_item *param_item;
2933
2934 list_for_each_entry(param_item, param_list, list)
2935 if (param_item->param->id == param_id)
2936 return param_item;
2937 return NULL;
2938}
2939
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002940static bool
2941devlink_param_cmode_is_supported(const struct devlink_param *param,
2942 enum devlink_param_cmode cmode)
2943{
2944 return test_bit(cmode, &param->supported_cmodes);
2945}
2946
2947static int devlink_param_get(struct devlink *devlink,
2948 const struct devlink_param *param,
2949 struct devlink_param_gset_ctx *ctx)
2950{
2951 if (!param->get)
2952 return -EOPNOTSUPP;
2953 return param->get(devlink, param->id, ctx);
2954}
2955
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002956static int devlink_param_set(struct devlink *devlink,
2957 const struct devlink_param *param,
2958 struct devlink_param_gset_ctx *ctx)
2959{
2960 if (!param->set)
2961 return -EOPNOTSUPP;
2962 return param->set(devlink, param->id, ctx);
2963}
2964
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002965static int
2966devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2967{
2968 switch (param_type) {
2969 case DEVLINK_PARAM_TYPE_U8:
2970 return NLA_U8;
2971 case DEVLINK_PARAM_TYPE_U16:
2972 return NLA_U16;
2973 case DEVLINK_PARAM_TYPE_U32:
2974 return NLA_U32;
2975 case DEVLINK_PARAM_TYPE_STRING:
2976 return NLA_STRING;
2977 case DEVLINK_PARAM_TYPE_BOOL:
2978 return NLA_FLAG;
2979 default:
2980 return -EINVAL;
2981 }
2982}
2983
2984static int
2985devlink_nl_param_value_fill_one(struct sk_buff *msg,
2986 enum devlink_param_type type,
2987 enum devlink_param_cmode cmode,
2988 union devlink_param_value val)
2989{
2990 struct nlattr *param_value_attr;
2991
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002992 param_value_attr = nla_nest_start_noflag(msg,
2993 DEVLINK_ATTR_PARAM_VALUE);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002994 if (!param_value_attr)
2995 goto nla_put_failure;
2996
2997 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2998 goto value_nest_cancel;
2999
3000 switch (type) {
3001 case DEVLINK_PARAM_TYPE_U8:
3002 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3003 goto value_nest_cancel;
3004 break;
3005 case DEVLINK_PARAM_TYPE_U16:
3006 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3007 goto value_nest_cancel;
3008 break;
3009 case DEVLINK_PARAM_TYPE_U32:
3010 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3011 goto value_nest_cancel;
3012 break;
3013 case DEVLINK_PARAM_TYPE_STRING:
3014 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3015 val.vstr))
3016 goto value_nest_cancel;
3017 break;
3018 case DEVLINK_PARAM_TYPE_BOOL:
3019 if (val.vbool &&
3020 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3021 goto value_nest_cancel;
3022 break;
3023 }
3024
3025 nla_nest_end(msg, param_value_attr);
3026 return 0;
3027
3028value_nest_cancel:
3029 nla_nest_cancel(msg, param_value_attr);
3030nla_put_failure:
3031 return -EMSGSIZE;
3032}
3033
3034static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303035 unsigned int port_index,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003036 struct devlink_param_item *param_item,
3037 enum devlink_command cmd,
3038 u32 portid, u32 seq, int flags)
3039{
3040 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003041 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003042 const struct devlink_param *param = param_item->param;
3043 struct devlink_param_gset_ctx ctx;
3044 struct nlattr *param_values_list;
3045 struct nlattr *param_attr;
3046 int nla_type;
3047 void *hdr;
3048 int err;
3049 int i;
3050
3051 /* Get value from driver part to driverinit configuration mode */
3052 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3053 if (!devlink_param_cmode_is_supported(param, i))
3054 continue;
3055 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3056 if (!param_item->driverinit_value_valid)
3057 return -EOPNOTSUPP;
3058 param_value[i] = param_item->driverinit_value;
3059 } else {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003060 if (!param_item->published)
3061 continue;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003062 ctx.cmode = i;
3063 err = devlink_param_get(devlink, param, &ctx);
3064 if (err)
3065 return err;
3066 param_value[i] = ctx.val;
3067 }
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003068 param_value_set[i] = true;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003069 }
3070
3071 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3072 if (!hdr)
3073 return -EMSGSIZE;
3074
3075 if (devlink_nl_put_handle(msg, devlink))
3076 goto genlmsg_cancel;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303077
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303078 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3079 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3080 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303081 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3082 goto genlmsg_cancel;
3083
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003084 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003085 if (!param_attr)
3086 goto genlmsg_cancel;
3087 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3088 goto param_nest_cancel;
3089 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3090 goto param_nest_cancel;
3091
3092 nla_type = devlink_param_type_to_nla_type(param->type);
3093 if (nla_type < 0)
3094 goto param_nest_cancel;
3095 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3096 goto param_nest_cancel;
3097
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003098 param_values_list = nla_nest_start_noflag(msg,
3099 DEVLINK_ATTR_PARAM_VALUES_LIST);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003100 if (!param_values_list)
3101 goto param_nest_cancel;
3102
3103 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003104 if (!param_value_set[i])
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003105 continue;
3106 err = devlink_nl_param_value_fill_one(msg, param->type,
3107 i, param_value[i]);
3108 if (err)
3109 goto values_list_nest_cancel;
3110 }
3111
3112 nla_nest_end(msg, param_values_list);
3113 nla_nest_end(msg, param_attr);
3114 genlmsg_end(msg, hdr);
3115 return 0;
3116
3117values_list_nest_cancel:
3118 nla_nest_end(msg, param_values_list);
3119param_nest_cancel:
3120 nla_nest_cancel(msg, param_attr);
3121genlmsg_cancel:
3122 genlmsg_cancel(msg, hdr);
3123 return -EMSGSIZE;
3124}
3125
Moshe Shemeshea601e12018-07-04 14:30:32 +03003126static void devlink_param_notify(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303127 unsigned int port_index,
Moshe Shemeshea601e12018-07-04 14:30:32 +03003128 struct devlink_param_item *param_item,
3129 enum devlink_command cmd)
3130{
3131 struct sk_buff *msg;
3132 int err;
3133
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303134 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3135 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3136 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003137
3138 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3139 if (!msg)
3140 return;
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303141 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3142 0, 0, 0);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003143 if (err) {
3144 nlmsg_free(msg);
3145 return;
3146 }
3147
3148 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3149 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3150}
3151
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003152static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3153 struct netlink_callback *cb)
3154{
3155 struct devlink_param_item *param_item;
3156 struct devlink *devlink;
3157 int start = cb->args[0];
3158 int idx = 0;
3159 int err;
3160
3161 mutex_lock(&devlink_mutex);
3162 list_for_each_entry(devlink, &devlink_list, list) {
3163 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3164 continue;
3165 mutex_lock(&devlink->lock);
3166 list_for_each_entry(param_item, &devlink->param_list, list) {
3167 if (idx < start) {
3168 idx++;
3169 continue;
3170 }
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303171 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003172 DEVLINK_CMD_PARAM_GET,
3173 NETLINK_CB(cb->skb).portid,
3174 cb->nlh->nlmsg_seq,
3175 NLM_F_MULTI);
3176 if (err) {
3177 mutex_unlock(&devlink->lock);
3178 goto out;
3179 }
3180 idx++;
3181 }
3182 mutex_unlock(&devlink->lock);
3183 }
3184out:
3185 mutex_unlock(&devlink_mutex);
3186
3187 cb->args[0] = idx;
3188 return msg->len;
3189}
3190
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003191static int
3192devlink_param_type_get_from_info(struct genl_info *info,
3193 enum devlink_param_type *param_type)
3194{
3195 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3196 return -EINVAL;
3197
3198 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3199 case NLA_U8:
3200 *param_type = DEVLINK_PARAM_TYPE_U8;
3201 break;
3202 case NLA_U16:
3203 *param_type = DEVLINK_PARAM_TYPE_U16;
3204 break;
3205 case NLA_U32:
3206 *param_type = DEVLINK_PARAM_TYPE_U32;
3207 break;
3208 case NLA_STRING:
3209 *param_type = DEVLINK_PARAM_TYPE_STRING;
3210 break;
3211 case NLA_FLAG:
3212 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3213 break;
3214 default:
3215 return -EINVAL;
3216 }
3217
3218 return 0;
3219}
3220
3221static int
3222devlink_param_value_get_from_info(const struct devlink_param *param,
3223 struct genl_info *info,
3224 union devlink_param_value *value)
3225{
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003226 int len;
3227
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003228 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3229 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3230 return -EINVAL;
3231
3232 switch (param->type) {
3233 case DEVLINK_PARAM_TYPE_U8:
3234 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3235 break;
3236 case DEVLINK_PARAM_TYPE_U16:
3237 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3238 break;
3239 case DEVLINK_PARAM_TYPE_U32:
3240 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3241 break;
3242 case DEVLINK_PARAM_TYPE_STRING:
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003243 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3244 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3245 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03003246 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003247 return -EINVAL;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003248 strcpy(value->vstr,
3249 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003250 break;
3251 case DEVLINK_PARAM_TYPE_BOOL:
3252 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3253 true : false;
3254 break;
3255 }
3256 return 0;
3257}
3258
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003259static struct devlink_param_item *
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303260devlink_param_get_from_info(struct list_head *param_list,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003261 struct genl_info *info)
3262{
3263 char *param_name;
3264
3265 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3266 return NULL;
3267
3268 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303269 return devlink_param_find_by_name(param_list, param_name);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003270}
3271
3272static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3273 struct genl_info *info)
3274{
3275 struct devlink *devlink = info->user_ptr[0];
3276 struct devlink_param_item *param_item;
3277 struct sk_buff *msg;
3278 int err;
3279
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303280 param_item = devlink_param_get_from_info(&devlink->param_list, info);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003281 if (!param_item)
3282 return -EINVAL;
3283
3284 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3285 if (!msg)
3286 return -ENOMEM;
3287
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303288 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003289 DEVLINK_CMD_PARAM_GET,
3290 info->snd_portid, info->snd_seq, 0);
3291 if (err) {
3292 nlmsg_free(msg);
3293 return err;
3294 }
3295
3296 return genlmsg_reply(msg, info);
3297}
3298
Vasundhara Volam9c548732019-01-28 18:00:22 +05303299static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303300 unsigned int port_index,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303301 struct list_head *param_list,
3302 struct genl_info *info,
3303 enum devlink_command cmd)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003304{
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003305 enum devlink_param_type param_type;
3306 struct devlink_param_gset_ctx ctx;
3307 enum devlink_param_cmode cmode;
3308 struct devlink_param_item *param_item;
3309 const struct devlink_param *param;
3310 union devlink_param_value value;
3311 int err = 0;
3312
Vasundhara Volam9c548732019-01-28 18:00:22 +05303313 param_item = devlink_param_get_from_info(param_list, info);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003314 if (!param_item)
3315 return -EINVAL;
3316 param = param_item->param;
3317 err = devlink_param_type_get_from_info(info, &param_type);
3318 if (err)
3319 return err;
3320 if (param_type != param->type)
3321 return -EINVAL;
3322 err = devlink_param_value_get_from_info(param, info, &value);
3323 if (err)
3324 return err;
3325 if (param->validate) {
3326 err = param->validate(devlink, param->id, value, info->extack);
3327 if (err)
3328 return err;
3329 }
3330
3331 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3332 return -EINVAL;
3333 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3334 if (!devlink_param_cmode_is_supported(param, cmode))
3335 return -EOPNOTSUPP;
3336
3337 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
Moshe Shemesh12765342018-10-10 16:09:26 +03003338 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3339 strcpy(param_item->driverinit_value.vstr, value.vstr);
3340 else
3341 param_item->driverinit_value = value;
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003342 param_item->driverinit_value_valid = true;
3343 } else {
3344 if (!param->set)
3345 return -EOPNOTSUPP;
3346 ctx.val = value;
3347 ctx.cmode = cmode;
3348 err = devlink_param_set(devlink, param, &ctx);
3349 if (err)
3350 return err;
3351 }
3352
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303353 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003354 return 0;
3355}
3356
Vasundhara Volam9c548732019-01-28 18:00:22 +05303357static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3358 struct genl_info *info)
3359{
3360 struct devlink *devlink = info->user_ptr[0];
3361
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303362 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303363 info, DEVLINK_CMD_PARAM_NEW);
3364}
3365
Moshe Shemesheabaef12018-07-04 14:30:28 +03003366static int devlink_param_register_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303367 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303368 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303369 const struct devlink_param *param,
3370 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003371{
3372 struct devlink_param_item *param_item;
3373
Vasundhara Volam39e61602019-01-28 18:00:20 +05303374 if (devlink_param_find_by_name(param_list, param->name))
Moshe Shemesheabaef12018-07-04 14:30:28 +03003375 return -EEXIST;
3376
3377 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3378 WARN_ON(param->get || param->set);
3379 else
3380 WARN_ON(!param->get || !param->set);
3381
3382 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3383 if (!param_item)
3384 return -ENOMEM;
3385 param_item->param = param;
3386
Vasundhara Volam39e61602019-01-28 18:00:20 +05303387 list_add_tail(&param_item->list, param_list);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303388 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003389 return 0;
3390}
3391
3392static void devlink_param_unregister_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303393 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303394 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303395 const struct devlink_param *param,
3396 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003397{
3398 struct devlink_param_item *param_item;
3399
Vasundhara Volam39e61602019-01-28 18:00:20 +05303400 param_item = devlink_param_find_by_name(param_list, param->name);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003401 WARN_ON(!param_item);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303402 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003403 list_del(&param_item->list);
3404 kfree(param_item);
3405}
3406
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303407static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3408 struct netlink_callback *cb)
3409{
3410 struct devlink_param_item *param_item;
3411 struct devlink_port *devlink_port;
3412 struct devlink *devlink;
3413 int start = cb->args[0];
3414 int idx = 0;
3415 int err;
3416
3417 mutex_lock(&devlink_mutex);
3418 list_for_each_entry(devlink, &devlink_list, list) {
3419 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3420 continue;
3421 mutex_lock(&devlink->lock);
3422 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3423 list_for_each_entry(param_item,
3424 &devlink_port->param_list, list) {
3425 if (idx < start) {
3426 idx++;
3427 continue;
3428 }
3429 err = devlink_nl_param_fill(msg,
3430 devlink_port->devlink,
3431 devlink_port->index, param_item,
3432 DEVLINK_CMD_PORT_PARAM_GET,
3433 NETLINK_CB(cb->skb).portid,
3434 cb->nlh->nlmsg_seq,
3435 NLM_F_MULTI);
3436 if (err) {
3437 mutex_unlock(&devlink->lock);
3438 goto out;
3439 }
3440 idx++;
3441 }
3442 }
3443 mutex_unlock(&devlink->lock);
3444 }
3445out:
3446 mutex_unlock(&devlink_mutex);
3447
3448 cb->args[0] = idx;
3449 return msg->len;
3450}
3451
3452static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3453 struct genl_info *info)
3454{
3455 struct devlink_port *devlink_port = info->user_ptr[0];
3456 struct devlink_param_item *param_item;
3457 struct sk_buff *msg;
3458 int err;
3459
3460 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3461 info);
3462 if (!param_item)
3463 return -EINVAL;
3464
3465 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3466 if (!msg)
3467 return -ENOMEM;
3468
3469 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3470 devlink_port->index, param_item,
3471 DEVLINK_CMD_PORT_PARAM_GET,
3472 info->snd_portid, info->snd_seq, 0);
3473 if (err) {
3474 nlmsg_free(msg);
3475 return err;
3476 }
3477
3478 return genlmsg_reply(msg, info);
3479}
3480
Vasundhara Volam9c548732019-01-28 18:00:22 +05303481static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3482 struct genl_info *info)
3483{
3484 struct devlink_port *devlink_port = info->user_ptr[0];
3485
3486 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303487 devlink_port->index,
3488 &devlink_port->param_list, info,
3489 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam9c548732019-01-28 18:00:22 +05303490}
3491
Alex Veskera006d462018-07-12 15:13:12 +03003492static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3493 struct devlink *devlink,
3494 struct devlink_snapshot *snapshot)
3495{
3496 struct nlattr *snap_attr;
3497 int err;
3498
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003499 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
Alex Veskera006d462018-07-12 15:13:12 +03003500 if (!snap_attr)
3501 return -EINVAL;
3502
3503 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3504 if (err)
3505 goto nla_put_failure;
3506
3507 nla_nest_end(msg, snap_attr);
3508 return 0;
3509
3510nla_put_failure:
3511 nla_nest_cancel(msg, snap_attr);
3512 return err;
3513}
3514
3515static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3516 struct devlink *devlink,
3517 struct devlink_region *region)
3518{
3519 struct devlink_snapshot *snapshot;
3520 struct nlattr *snapshots_attr;
3521 int err;
3522
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003523 snapshots_attr = nla_nest_start_noflag(msg,
3524 DEVLINK_ATTR_REGION_SNAPSHOTS);
Alex Veskera006d462018-07-12 15:13:12 +03003525 if (!snapshots_attr)
3526 return -EINVAL;
3527
3528 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3529 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3530 if (err)
3531 goto nla_put_failure;
3532 }
3533
3534 nla_nest_end(msg, snapshots_attr);
3535 return 0;
3536
3537nla_put_failure:
3538 nla_nest_cancel(msg, snapshots_attr);
3539 return err;
3540}
3541
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003542static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3543 enum devlink_command cmd, u32 portid,
3544 u32 seq, int flags,
3545 struct devlink_region *region)
3546{
3547 void *hdr;
3548 int err;
3549
3550 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3551 if (!hdr)
3552 return -EMSGSIZE;
3553
3554 err = devlink_nl_put_handle(msg, devlink);
3555 if (err)
3556 goto nla_put_failure;
3557
3558 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3559 if (err)
3560 goto nla_put_failure;
3561
3562 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3563 region->size,
3564 DEVLINK_ATTR_PAD);
3565 if (err)
3566 goto nla_put_failure;
3567
Alex Veskera006d462018-07-12 15:13:12 +03003568 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3569 if (err)
3570 goto nla_put_failure;
3571
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003572 genlmsg_end(msg, hdr);
3573 return 0;
3574
3575nla_put_failure:
3576 genlmsg_cancel(msg, hdr);
3577 return err;
3578}
3579
Alex Vesker866319b2018-07-12 15:13:13 +03003580static void devlink_nl_region_notify(struct devlink_region *region,
3581 struct devlink_snapshot *snapshot,
3582 enum devlink_command cmd)
3583{
3584 struct devlink *devlink = region->devlink;
3585 struct sk_buff *msg;
3586 void *hdr;
3587 int err;
3588
3589 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3590
3591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3592 if (!msg)
3593 return;
3594
3595 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3596 if (!hdr)
3597 goto out_free_msg;
3598
3599 err = devlink_nl_put_handle(msg, devlink);
3600 if (err)
3601 goto out_cancel_msg;
3602
3603 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3604 region->name);
3605 if (err)
3606 goto out_cancel_msg;
3607
3608 if (snapshot) {
3609 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3610 snapshot->id);
3611 if (err)
3612 goto out_cancel_msg;
3613 } else {
3614 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3615 region->size, DEVLINK_ATTR_PAD);
3616 if (err)
3617 goto out_cancel_msg;
3618 }
3619 genlmsg_end(msg, hdr);
3620
3621 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3622 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3623
3624 return;
3625
3626out_cancel_msg:
3627 genlmsg_cancel(msg, hdr);
3628out_free_msg:
3629 nlmsg_free(msg);
3630}
3631
Jiri Pirko92b49822019-08-12 14:28:31 +02003632static void devlink_region_snapshot_del(struct devlink_region *region,
3633 struct devlink_snapshot *snapshot)
3634{
3635 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3636 region->cur_snapshots--;
3637 list_del(&snapshot->list);
3638 (*snapshot->data_destructor)(snapshot->data);
3639 kfree(snapshot);
3640}
3641
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003642static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3643 struct genl_info *info)
3644{
3645 struct devlink *devlink = info->user_ptr[0];
3646 struct devlink_region *region;
3647 const char *region_name;
3648 struct sk_buff *msg;
3649 int err;
3650
3651 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3652 return -EINVAL;
3653
3654 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3655 region = devlink_region_get_by_name(devlink, region_name);
3656 if (!region)
3657 return -EINVAL;
3658
3659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3660 if (!msg)
3661 return -ENOMEM;
3662
3663 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3664 info->snd_portid, info->snd_seq, 0,
3665 region);
3666 if (err) {
3667 nlmsg_free(msg);
3668 return err;
3669 }
3670
3671 return genlmsg_reply(msg, info);
3672}
3673
3674static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3675 struct netlink_callback *cb)
3676{
3677 struct devlink_region *region;
3678 struct devlink *devlink;
3679 int start = cb->args[0];
3680 int idx = 0;
3681 int err;
3682
3683 mutex_lock(&devlink_mutex);
3684 list_for_each_entry(devlink, &devlink_list, list) {
3685 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3686 continue;
3687
3688 mutex_lock(&devlink->lock);
3689 list_for_each_entry(region, &devlink->region_list, list) {
3690 if (idx < start) {
3691 idx++;
3692 continue;
3693 }
3694 err = devlink_nl_region_fill(msg, devlink,
3695 DEVLINK_CMD_REGION_GET,
3696 NETLINK_CB(cb->skb).portid,
3697 cb->nlh->nlmsg_seq,
3698 NLM_F_MULTI, region);
3699 if (err) {
3700 mutex_unlock(&devlink->lock);
3701 goto out;
3702 }
3703 idx++;
3704 }
3705 mutex_unlock(&devlink->lock);
3706 }
3707out:
3708 mutex_unlock(&devlink_mutex);
3709 cb->args[0] = idx;
3710 return msg->len;
3711}
3712
Alex Vesker866319b2018-07-12 15:13:13 +03003713static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3714 struct genl_info *info)
3715{
3716 struct devlink *devlink = info->user_ptr[0];
3717 struct devlink_snapshot *snapshot;
3718 struct devlink_region *region;
3719 const char *region_name;
3720 u32 snapshot_id;
3721
3722 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3723 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3724 return -EINVAL;
3725
3726 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3727 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3728
3729 region = devlink_region_get_by_name(devlink, region_name);
3730 if (!region)
3731 return -EINVAL;
3732
3733 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3734 if (!snapshot)
3735 return -EINVAL;
3736
Jiri Pirko92b49822019-08-12 14:28:31 +02003737 devlink_region_snapshot_del(region, snapshot);
Alex Vesker866319b2018-07-12 15:13:13 +03003738 return 0;
3739}
3740
Alex Vesker4e547952018-07-12 15:13:14 +03003741static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3742 struct devlink *devlink,
3743 u8 *chunk, u32 chunk_size,
3744 u64 addr)
3745{
3746 struct nlattr *chunk_attr;
3747 int err;
3748
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003749 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
Alex Vesker4e547952018-07-12 15:13:14 +03003750 if (!chunk_attr)
3751 return -EINVAL;
3752
3753 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3754 if (err)
3755 goto nla_put_failure;
3756
3757 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3758 DEVLINK_ATTR_PAD);
3759 if (err)
3760 goto nla_put_failure;
3761
3762 nla_nest_end(msg, chunk_attr);
3763 return 0;
3764
3765nla_put_failure:
3766 nla_nest_cancel(msg, chunk_attr);
3767 return err;
3768}
3769
3770#define DEVLINK_REGION_READ_CHUNK_SIZE 256
3771
3772static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3773 struct devlink *devlink,
3774 struct devlink_region *region,
3775 struct nlattr **attrs,
3776 u64 start_offset,
3777 u64 end_offset,
3778 bool dump,
3779 u64 *new_offset)
3780{
3781 struct devlink_snapshot *snapshot;
3782 u64 curr_offset = start_offset;
3783 u32 snapshot_id;
3784 int err = 0;
3785
3786 *new_offset = start_offset;
3787
3788 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3789 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3790 if (!snapshot)
3791 return -EINVAL;
3792
Jiri Pirko3a5e5232019-08-09 15:27:15 +02003793 if (end_offset > region->size || dump)
3794 end_offset = region->size;
Alex Vesker4e547952018-07-12 15:13:14 +03003795
3796 while (curr_offset < end_offset) {
3797 u32 data_size;
3798 u8 *data;
3799
3800 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3801 data_size = end_offset - curr_offset;
3802 else
3803 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3804
3805 data = &snapshot->data[curr_offset];
3806 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3807 data, data_size,
3808 curr_offset);
3809 if (err)
3810 break;
3811
3812 curr_offset += data_size;
3813 }
3814 *new_offset = curr_offset;
3815
3816 return err;
3817}
3818
3819static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3820 struct netlink_callback *cb)
3821{
3822 u64 ret_offset, start_offset, end_offset = 0;
Alex Vesker4e547952018-07-12 15:13:14 +03003823 struct devlink_region *region;
3824 struct nlattr *chunks_attr;
3825 const char *region_name;
3826 struct devlink *devlink;
Jakub Kicinski68750562019-02-10 19:35:28 -08003827 struct nlattr **attrs;
Alex Vesker4e547952018-07-12 15:13:14 +03003828 bool dump = true;
3829 void *hdr;
3830 int err;
3831
3832 start_offset = *((u64 *)&cb->args[0]);
3833
Jakub Kicinski68750562019-02-10 19:35:28 -08003834 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
3835 if (!attrs)
3836 return -ENOMEM;
3837
Johannes Berg8cb08172019-04-26 14:07:28 +02003838 err = nlmsg_parse_deprecated(cb->nlh,
3839 GENL_HDRLEN + devlink_nl_family.hdrsize,
3840 attrs, DEVLINK_ATTR_MAX,
3841 devlink_nl_family.policy, cb->extack);
Alex Vesker4e547952018-07-12 15:13:14 +03003842 if (err)
Jakub Kicinski68750562019-02-10 19:35:28 -08003843 goto out_free;
Alex Vesker4e547952018-07-12 15:13:14 +03003844
Parav Panditdac7c082019-02-12 14:24:08 -06003845 mutex_lock(&devlink_mutex);
Alex Vesker4e547952018-07-12 15:13:14 +03003846 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003847 if (IS_ERR(devlink)) {
3848 err = PTR_ERR(devlink);
Parav Panditdac7c082019-02-12 14:24:08 -06003849 goto out_dev;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003850 }
Alex Vesker4e547952018-07-12 15:13:14 +03003851
Alex Vesker4e547952018-07-12 15:13:14 +03003852 mutex_lock(&devlink->lock);
3853
3854 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
Parav Panditfdd41ec2019-02-12 14:23:58 -06003855 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3856 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003857 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003858 }
Alex Vesker4e547952018-07-12 15:13:14 +03003859
3860 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3861 region = devlink_region_get_by_name(devlink, region_name);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003862 if (!region) {
3863 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003864 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003865 }
Alex Vesker4e547952018-07-12 15:13:14 +03003866
3867 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3868 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3869 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003870 if (!hdr) {
3871 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003872 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003873 }
Alex Vesker4e547952018-07-12 15:13:14 +03003874
3875 err = devlink_nl_put_handle(skb, devlink);
3876 if (err)
3877 goto nla_put_failure;
3878
3879 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3880 if (err)
3881 goto nla_put_failure;
3882
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003883 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003884 if (!chunks_attr) {
3885 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003886 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003887 }
Alex Vesker4e547952018-07-12 15:13:14 +03003888
3889 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3890 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3891 if (!start_offset)
3892 start_offset =
3893 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3894
3895 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3896 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3897 dump = false;
3898 }
3899
3900 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3901 region, attrs,
3902 start_offset,
3903 end_offset, dump,
3904 &ret_offset);
3905
3906 if (err && err != -EMSGSIZE)
3907 goto nla_put_failure;
3908
3909 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06003910 if (ret_offset == start_offset) {
3911 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003912 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003913 }
Alex Vesker4e547952018-07-12 15:13:14 +03003914
3915 *((u64 *)&cb->args[0]) = ret_offset;
3916
3917 nla_nest_end(skb, chunks_attr);
3918 genlmsg_end(skb, hdr);
3919 mutex_unlock(&devlink->lock);
3920 mutex_unlock(&devlink_mutex);
Jakub Kicinski68750562019-02-10 19:35:28 -08003921 kfree(attrs);
Alex Vesker4e547952018-07-12 15:13:14 +03003922
3923 return skb->len;
3924
3925nla_put_failure:
3926 genlmsg_cancel(skb, hdr);
3927out_unlock:
3928 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06003929out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03003930 mutex_unlock(&devlink_mutex);
Jakub Kicinski68750562019-02-10 19:35:28 -08003931out_free:
3932 kfree(attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003933 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03003934}
3935
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08003936struct devlink_info_req {
3937 struct sk_buff *msg;
3938};
3939
3940int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3941{
3942 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3943}
3944EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3945
3946int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3947{
3948 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3949}
3950EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3951
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08003952static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3953 const char *version_name,
3954 const char *version_value)
3955{
3956 struct nlattr *nest;
3957 int err;
3958
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003959 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08003960 if (!nest)
3961 return -EMSGSIZE;
3962
3963 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3964 version_name);
3965 if (err)
3966 goto nla_put_failure;
3967
3968 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3969 version_value);
3970 if (err)
3971 goto nla_put_failure;
3972
3973 nla_nest_end(req->msg, nest);
3974
3975 return 0;
3976
3977nla_put_failure:
3978 nla_nest_cancel(req->msg, nest);
3979 return err;
3980}
3981
3982int devlink_info_version_fixed_put(struct devlink_info_req *req,
3983 const char *version_name,
3984 const char *version_value)
3985{
3986 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3987 version_name, version_value);
3988}
3989EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3990
3991int devlink_info_version_stored_put(struct devlink_info_req *req,
3992 const char *version_name,
3993 const char *version_value)
3994{
3995 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3996 version_name, version_value);
3997}
3998EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3999
4000int devlink_info_version_running_put(struct devlink_info_req *req,
4001 const char *version_name,
4002 const char *version_value)
4003{
4004 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4005 version_name, version_value);
4006}
4007EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4008
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004009static int
4010devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4011 enum devlink_command cmd, u32 portid,
4012 u32 seq, int flags, struct netlink_ext_ack *extack)
4013{
4014 struct devlink_info_req req;
4015 void *hdr;
4016 int err;
4017
4018 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4019 if (!hdr)
4020 return -EMSGSIZE;
4021
4022 err = -EMSGSIZE;
4023 if (devlink_nl_put_handle(msg, devlink))
4024 goto err_cancel_msg;
4025
4026 req.msg = msg;
4027 err = devlink->ops->info_get(devlink, &req, extack);
4028 if (err)
4029 goto err_cancel_msg;
4030
4031 genlmsg_end(msg, hdr);
4032 return 0;
4033
4034err_cancel_msg:
4035 genlmsg_cancel(msg, hdr);
4036 return err;
4037}
4038
4039static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4040 struct genl_info *info)
4041{
4042 struct devlink *devlink = info->user_ptr[0];
4043 struct sk_buff *msg;
4044 int err;
4045
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08004046 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004047 return -EOPNOTSUPP;
4048
4049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4050 if (!msg)
4051 return -ENOMEM;
4052
4053 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4054 info->snd_portid, info->snd_seq, 0,
4055 info->extack);
4056 if (err) {
4057 nlmsg_free(msg);
4058 return err;
4059 }
4060
4061 return genlmsg_reply(msg, info);
4062}
4063
4064static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4065 struct netlink_callback *cb)
4066{
4067 struct devlink *devlink;
4068 int start = cb->args[0];
4069 int idx = 0;
4070 int err;
4071
4072 mutex_lock(&devlink_mutex);
4073 list_for_each_entry(devlink, &devlink_list, list) {
4074 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4075 continue;
4076 if (idx < start) {
4077 idx++;
4078 continue;
4079 }
4080
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004081 if (!devlink->ops->info_get) {
4082 idx++;
4083 continue;
4084 }
4085
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004086 mutex_lock(&devlink->lock);
4087 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4088 NETLINK_CB(cb->skb).portid,
4089 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4090 cb->extack);
4091 mutex_unlock(&devlink->lock);
4092 if (err)
4093 break;
4094 idx++;
4095 }
4096 mutex_unlock(&devlink_mutex);
4097
4098 cb->args[0] = idx;
4099 return msg->len;
4100}
4101
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004102struct devlink_fmsg_item {
4103 struct list_head list;
4104 int attrtype;
4105 u8 nla_type;
4106 u16 len;
4107 int value[0];
4108};
4109
4110struct devlink_fmsg {
4111 struct list_head item_list;
4112};
4113
4114static struct devlink_fmsg *devlink_fmsg_alloc(void)
4115{
4116 struct devlink_fmsg *fmsg;
4117
4118 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4119 if (!fmsg)
4120 return NULL;
4121
4122 INIT_LIST_HEAD(&fmsg->item_list);
4123
4124 return fmsg;
4125}
4126
4127static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4128{
4129 struct devlink_fmsg_item *item, *tmp;
4130
4131 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4132 list_del(&item->list);
4133 kfree(item);
4134 }
4135 kfree(fmsg);
4136}
4137
4138static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4139 int attrtype)
4140{
4141 struct devlink_fmsg_item *item;
4142
4143 item = kzalloc(sizeof(*item), GFP_KERNEL);
4144 if (!item)
4145 return -ENOMEM;
4146
4147 item->attrtype = attrtype;
4148 list_add_tail(&item->list, &fmsg->item_list);
4149
4150 return 0;
4151}
4152
4153int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4154{
4155 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4156}
4157EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4158
4159static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4160{
4161 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4162}
4163
4164int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4165{
4166 return devlink_fmsg_nest_end(fmsg);
4167}
4168EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4169
4170#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4171
4172static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4173{
4174 struct devlink_fmsg_item *item;
4175
4176 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4177 return -EMSGSIZE;
4178
4179 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4180 if (!item)
4181 return -ENOMEM;
4182
4183 item->nla_type = NLA_NUL_STRING;
4184 item->len = strlen(name) + 1;
4185 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4186 memcpy(&item->value, name, item->len);
4187 list_add_tail(&item->list, &fmsg->item_list);
4188
4189 return 0;
4190}
4191
4192int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4193{
4194 int err;
4195
4196 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4197 if (err)
4198 return err;
4199
4200 err = devlink_fmsg_put_name(fmsg, name);
4201 if (err)
4202 return err;
4203
4204 return 0;
4205}
4206EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4207
4208int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4209{
4210 return devlink_fmsg_nest_end(fmsg);
4211}
4212EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4213
4214int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4215 const char *name)
4216{
4217 int err;
4218
4219 err = devlink_fmsg_pair_nest_start(fmsg, name);
4220 if (err)
4221 return err;
4222
4223 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4224 if (err)
4225 return err;
4226
4227 return 0;
4228}
4229EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4230
4231int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4232{
4233 int err;
4234
4235 err = devlink_fmsg_nest_end(fmsg);
4236 if (err)
4237 return err;
4238
4239 err = devlink_fmsg_nest_end(fmsg);
4240 if (err)
4241 return err;
4242
4243 return 0;
4244}
4245EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4246
4247static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4248 const void *value, u16 value_len,
4249 u8 value_nla_type)
4250{
4251 struct devlink_fmsg_item *item;
4252
4253 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4254 return -EMSGSIZE;
4255
4256 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4257 if (!item)
4258 return -ENOMEM;
4259
4260 item->nla_type = value_nla_type;
4261 item->len = value_len;
4262 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4263 memcpy(&item->value, value, item->len);
4264 list_add_tail(&item->list, &fmsg->item_list);
4265
4266 return 0;
4267}
4268
4269int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4270{
4271 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4272}
4273EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4274
4275int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4276{
4277 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4278}
4279EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4280
4281int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4282{
4283 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4284}
4285EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4286
4287int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4288{
4289 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4290}
4291EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4292
4293int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4294{
4295 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4296 NLA_NUL_STRING);
4297}
4298EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4299
4300int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4301 u16 value_len)
4302{
4303 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4304}
4305EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4306
4307int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4308 bool value)
4309{
4310 int err;
4311
4312 err = devlink_fmsg_pair_nest_start(fmsg, name);
4313 if (err)
4314 return err;
4315
4316 err = devlink_fmsg_bool_put(fmsg, value);
4317 if (err)
4318 return err;
4319
4320 err = devlink_fmsg_pair_nest_end(fmsg);
4321 if (err)
4322 return err;
4323
4324 return 0;
4325}
4326EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4327
4328int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4329 u8 value)
4330{
4331 int err;
4332
4333 err = devlink_fmsg_pair_nest_start(fmsg, name);
4334 if (err)
4335 return err;
4336
4337 err = devlink_fmsg_u8_put(fmsg, value);
4338 if (err)
4339 return err;
4340
4341 err = devlink_fmsg_pair_nest_end(fmsg);
4342 if (err)
4343 return err;
4344
4345 return 0;
4346}
4347EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4348
4349int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4350 u32 value)
4351{
4352 int err;
4353
4354 err = devlink_fmsg_pair_nest_start(fmsg, name);
4355 if (err)
4356 return err;
4357
4358 err = devlink_fmsg_u32_put(fmsg, value);
4359 if (err)
4360 return err;
4361
4362 err = devlink_fmsg_pair_nest_end(fmsg);
4363 if (err)
4364 return err;
4365
4366 return 0;
4367}
4368EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4369
4370int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4371 u64 value)
4372{
4373 int err;
4374
4375 err = devlink_fmsg_pair_nest_start(fmsg, name);
4376 if (err)
4377 return err;
4378
4379 err = devlink_fmsg_u64_put(fmsg, value);
4380 if (err)
4381 return err;
4382
4383 err = devlink_fmsg_pair_nest_end(fmsg);
4384 if (err)
4385 return err;
4386
4387 return 0;
4388}
4389EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4390
4391int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4392 const char *value)
4393{
4394 int err;
4395
4396 err = devlink_fmsg_pair_nest_start(fmsg, name);
4397 if (err)
4398 return err;
4399
4400 err = devlink_fmsg_string_put(fmsg, value);
4401 if (err)
4402 return err;
4403
4404 err = devlink_fmsg_pair_nest_end(fmsg);
4405 if (err)
4406 return err;
4407
4408 return 0;
4409}
4410EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4411
4412int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4413 const void *value, u16 value_len)
4414{
4415 int err;
4416
4417 err = devlink_fmsg_pair_nest_start(fmsg, name);
4418 if (err)
4419 return err;
4420
4421 err = devlink_fmsg_binary_put(fmsg, value, value_len);
4422 if (err)
4423 return err;
4424
4425 err = devlink_fmsg_pair_nest_end(fmsg);
4426 if (err)
4427 return err;
4428
4429 return 0;
4430}
4431EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4432
4433static int
4434devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4435{
4436 switch (msg->nla_type) {
4437 case NLA_FLAG:
4438 case NLA_U8:
4439 case NLA_U32:
4440 case NLA_U64:
4441 case NLA_NUL_STRING:
4442 case NLA_BINARY:
4443 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4444 msg->nla_type);
4445 default:
4446 return -EINVAL;
4447 }
4448}
4449
4450static int
4451devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4452{
4453 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4454 u8 tmp;
4455
4456 switch (msg->nla_type) {
4457 case NLA_FLAG:
4458 /* Always provide flag data, regardless of its value */
4459 tmp = *(bool *) msg->value;
4460
4461 return nla_put_u8(skb, attrtype, tmp);
4462 case NLA_U8:
4463 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4464 case NLA_U32:
4465 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4466 case NLA_U64:
4467 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4468 DEVLINK_ATTR_PAD);
4469 case NLA_NUL_STRING:
4470 return nla_put_string(skb, attrtype, (char *) &msg->value);
4471 case NLA_BINARY:
4472 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4473 default:
4474 return -EINVAL;
4475 }
4476}
4477
4478static int
4479devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4480 int *start)
4481{
4482 struct devlink_fmsg_item *item;
4483 struct nlattr *fmsg_nlattr;
4484 int i = 0;
4485 int err;
4486
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004487 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004488 if (!fmsg_nlattr)
4489 return -EMSGSIZE;
4490
4491 list_for_each_entry(item, &fmsg->item_list, list) {
4492 if (i < *start) {
4493 i++;
4494 continue;
4495 }
4496
4497 switch (item->attrtype) {
4498 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4499 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4500 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4501 case DEVLINK_ATTR_FMSG_NEST_END:
4502 err = nla_put_flag(skb, item->attrtype);
4503 break;
4504 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4505 err = devlink_fmsg_item_fill_type(item, skb);
4506 if (err)
4507 break;
4508 err = devlink_fmsg_item_fill_data(item, skb);
4509 break;
4510 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4511 err = nla_put_string(skb, item->attrtype,
4512 (char *) &item->value);
4513 break;
4514 default:
4515 err = -EINVAL;
4516 break;
4517 }
4518 if (!err)
4519 *start = ++i;
4520 else
4521 break;
4522 }
4523
4524 nla_nest_end(skb, fmsg_nlattr);
4525 return err;
4526}
4527
4528static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4529 struct genl_info *info,
4530 enum devlink_command cmd, int flags)
4531{
4532 struct nlmsghdr *nlh;
4533 struct sk_buff *skb;
4534 bool last = false;
4535 int index = 0;
4536 void *hdr;
4537 int err;
4538
4539 while (!last) {
4540 int tmp_index = index;
4541
4542 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4543 if (!skb)
4544 return -ENOMEM;
4545
4546 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4547 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4548 if (!hdr) {
4549 err = -EMSGSIZE;
4550 goto nla_put_failure;
4551 }
4552
4553 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4554 if (!err)
4555 last = true;
4556 else if (err != -EMSGSIZE || tmp_index == index)
4557 goto nla_put_failure;
4558
4559 genlmsg_end(skb, hdr);
4560 err = genlmsg_reply(skb, info);
4561 if (err)
4562 return err;
4563 }
4564
4565 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4566 if (!skb)
4567 return -ENOMEM;
4568 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4569 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4570 if (!nlh) {
4571 err = -EMSGSIZE;
4572 goto nla_put_failure;
4573 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004574
Li RongQingfde55ea2019-02-11 19:09:07 +08004575 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004576
4577nla_put_failure:
4578 nlmsg_free(skb);
4579 return err;
4580}
4581
Aya Levine44ef4e2019-05-16 09:49:20 +03004582static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4583 struct netlink_callback *cb,
4584 enum devlink_command cmd)
4585{
4586 int index = cb->args[0];
4587 int tmp_index = index;
4588 void *hdr;
4589 int err;
4590
4591 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4592 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4593 if (!hdr) {
4594 err = -EMSGSIZE;
4595 goto nla_put_failure;
4596 }
4597
4598 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4599 if ((err && err != -EMSGSIZE) || tmp_index == index)
4600 goto nla_put_failure;
4601
4602 cb->args[0] = index;
4603 genlmsg_end(skb, hdr);
4604 return skb->len;
4605
4606nla_put_failure:
4607 genlmsg_cancel(skb, hdr);
4608 return err;
4609}
4610
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004611struct devlink_health_reporter {
4612 struct list_head list;
4613 void *priv;
4614 const struct devlink_health_reporter_ops *ops;
4615 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004616 struct devlink_fmsg *dump_fmsg;
4617 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004618 u64 graceful_period;
4619 bool auto_recover;
4620 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004621 u64 dump_ts;
4622 u64 error_count;
4623 u64 recovery_count;
4624 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004625 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004626};
4627
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004628void *
4629devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4630{
4631 return reporter->priv;
4632}
4633EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4634
4635static struct devlink_health_reporter *
4636devlink_health_reporter_find_by_name(struct devlink *devlink,
4637 const char *reporter_name)
4638{
4639 struct devlink_health_reporter *reporter;
4640
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004641 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004642 list_for_each_entry(reporter, &devlink->reporter_list, list)
4643 if (!strcmp(reporter->ops->name, reporter_name))
4644 return reporter;
4645 return NULL;
4646}
4647
4648/**
4649 * devlink_health_reporter_create - create devlink health reporter
4650 *
4651 * @devlink: devlink
4652 * @ops: ops
4653 * @graceful_period: to avoid recovery loops, in msecs
4654 * @auto_recover: auto recover when error occurs
4655 * @priv: priv
4656 */
4657struct devlink_health_reporter *
4658devlink_health_reporter_create(struct devlink *devlink,
4659 const struct devlink_health_reporter_ops *ops,
4660 u64 graceful_period, bool auto_recover,
4661 void *priv)
4662{
4663 struct devlink_health_reporter *reporter;
4664
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004665 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004666 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4667 reporter = ERR_PTR(-EEXIST);
4668 goto unlock;
4669 }
4670
4671 if (WARN_ON(auto_recover && !ops->recover) ||
4672 WARN_ON(graceful_period && !ops->recover)) {
4673 reporter = ERR_PTR(-EINVAL);
4674 goto unlock;
4675 }
4676
4677 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4678 if (!reporter) {
4679 reporter = ERR_PTR(-ENOMEM);
4680 goto unlock;
4681 }
4682
4683 reporter->priv = priv;
4684 reporter->ops = ops;
4685 reporter->devlink = devlink;
4686 reporter->graceful_period = graceful_period;
4687 reporter->auto_recover = auto_recover;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004688 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004689 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004690 list_add_tail(&reporter->list, &devlink->reporter_list);
4691unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004692 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004693 return reporter;
4694}
4695EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4696
4697/**
4698 * devlink_health_reporter_destroy - destroy devlink health reporter
4699 *
4700 * @reporter: devlink health reporter to destroy
4701 */
4702void
4703devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4704{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004705 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004706 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004707 mutex_unlock(&reporter->devlink->reporters_lock);
4708 while (refcount_read(&reporter->refcount) > 1)
4709 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01004710 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004711 if (reporter->dump_fmsg)
4712 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004713 kfree(reporter);
4714}
4715EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4716
Eran Ben Elisha3167b272019-03-03 10:57:30 +02004717void
4718devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4719 enum devlink_health_reporter_state state)
4720{
4721 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4722 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4723 return;
4724
4725 if (reporter->health_state == state)
4726 return;
4727
4728 reporter->health_state = state;
4729 trace_devlink_health_reporter_state_update(reporter->devlink,
4730 reporter->ops->name, state);
4731}
4732EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4733
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004734static int
4735devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
4736 void *priv_ctx)
4737{
4738 int err;
4739
4740 if (!reporter->ops->recover)
4741 return -EOPNOTSUPP;
4742
4743 err = reporter->ops->recover(reporter, priv_ctx);
4744 if (err)
4745 return err;
4746
4747 reporter->recovery_count++;
4748 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4749 reporter->last_recovery_ts = jiffies;
4750
4751 return 0;
4752}
4753
4754static void
4755devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4756{
4757 if (!reporter->dump_fmsg)
4758 return;
4759 devlink_fmsg_free(reporter->dump_fmsg);
4760 reporter->dump_fmsg = NULL;
4761}
4762
4763static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
4764 void *priv_ctx)
4765{
4766 int err;
4767
4768 if (!reporter->ops->dump)
4769 return 0;
4770
4771 if (reporter->dump_fmsg)
4772 return 0;
4773
4774 reporter->dump_fmsg = devlink_fmsg_alloc();
4775 if (!reporter->dump_fmsg) {
4776 err = -ENOMEM;
4777 return err;
4778 }
4779
4780 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4781 if (err)
4782 goto dump_err;
4783
4784 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
4785 priv_ctx);
4786 if (err)
4787 goto dump_err;
4788
4789 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4790 if (err)
4791 goto dump_err;
4792
4793 reporter->dump_ts = jiffies;
4794
4795 return 0;
4796
4797dump_err:
4798 devlink_health_dump_clear(reporter);
4799 return err;
4800}
4801
4802int devlink_health_report(struct devlink_health_reporter *reporter,
4803 const char *msg, void *priv_ctx)
4804{
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004805 enum devlink_health_reporter_state prev_health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004806 struct devlink *devlink = reporter->devlink;
4807
4808 /* write a log message of the current error */
4809 WARN_ON(!msg);
4810 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4811 reporter->error_count++;
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004812 prev_health_state = reporter->health_state;
4813 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004814
4815 /* abort if the previous error wasn't recovered */
4816 if (reporter->auto_recover &&
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004817 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004818 jiffies - reporter->last_recovery_ts <
4819 msecs_to_jiffies(reporter->graceful_period))) {
4820 trace_devlink_health_recover_aborted(devlink,
4821 reporter->ops->name,
4822 reporter->health_state,
4823 jiffies -
4824 reporter->last_recovery_ts);
4825 return -ECANCELED;
4826 }
4827
4828 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4829
4830 mutex_lock(&reporter->dump_lock);
4831 /* store current dump of current error, for later analysis */
4832 devlink_health_do_dump(reporter, priv_ctx);
4833 mutex_unlock(&reporter->dump_lock);
4834
4835 if (reporter->auto_recover)
4836 return devlink_health_reporter_recover(reporter, priv_ctx);
4837
4838 return 0;
4839}
4840EXPORT_SYMBOL_GPL(devlink_health_report);
4841
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004842static struct devlink_health_reporter *
Aya Levine44ef4e2019-05-16 09:49:20 +03004843devlink_health_reporter_get_from_attrs(struct devlink *devlink,
4844 struct nlattr **attrs)
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004845{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004846 struct devlink_health_reporter *reporter;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004847 char *reporter_name;
4848
Aya Levine44ef4e2019-05-16 09:49:20 +03004849 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004850 return NULL;
4851
Aya Levine44ef4e2019-05-16 09:49:20 +03004852 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004853 mutex_lock(&devlink->reporters_lock);
4854 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4855 if (reporter)
4856 refcount_inc(&reporter->refcount);
4857 mutex_unlock(&devlink->reporters_lock);
4858 return reporter;
4859}
4860
Aya Levine44ef4e2019-05-16 09:49:20 +03004861static struct devlink_health_reporter *
4862devlink_health_reporter_get_from_info(struct devlink *devlink,
4863 struct genl_info *info)
4864{
4865 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
4866}
4867
4868static struct devlink_health_reporter *
4869devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
4870{
4871 struct devlink_health_reporter *reporter;
4872 struct devlink *devlink;
4873 struct nlattr **attrs;
4874 int err;
4875
4876 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
4877 if (!attrs)
4878 return NULL;
4879
4880 err = nlmsg_parse_deprecated(cb->nlh,
4881 GENL_HDRLEN + devlink_nl_family.hdrsize,
4882 attrs, DEVLINK_ATTR_MAX,
4883 devlink_nl_family.policy, cb->extack);
4884 if (err)
4885 goto free;
4886
4887 mutex_lock(&devlink_mutex);
4888 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4889 if (IS_ERR(devlink))
4890 goto unlock;
4891
4892 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
4893 mutex_unlock(&devlink_mutex);
4894 kfree(attrs);
4895 return reporter;
4896unlock:
4897 mutex_unlock(&devlink_mutex);
4898free:
4899 kfree(attrs);
4900 return NULL;
4901}
4902
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004903static void
4904devlink_health_reporter_put(struct devlink_health_reporter *reporter)
4905{
4906 refcount_dec(&reporter->refcount);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004907}
4908
4909static int
4910devlink_nl_health_reporter_fill(struct sk_buff *msg,
4911 struct devlink *devlink,
4912 struct devlink_health_reporter *reporter,
4913 enum devlink_command cmd, u32 portid,
4914 u32 seq, int flags)
4915{
4916 struct nlattr *reporter_attr;
4917 void *hdr;
4918
4919 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4920 if (!hdr)
4921 return -EMSGSIZE;
4922
4923 if (devlink_nl_put_handle(msg, devlink))
4924 goto genlmsg_cancel;
4925
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004926 reporter_attr = nla_nest_start_noflag(msg,
4927 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004928 if (!reporter_attr)
4929 goto genlmsg_cancel;
4930 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4931 reporter->ops->name))
4932 goto reporter_nest_cancel;
4933 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4934 reporter->health_state))
4935 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004936 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004937 reporter->error_count, DEVLINK_ATTR_PAD))
4938 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004939 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004940 reporter->recovery_count, DEVLINK_ATTR_PAD))
4941 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004942 if (reporter->ops->recover &&
4943 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004944 reporter->graceful_period,
4945 DEVLINK_ATTR_PAD))
4946 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004947 if (reporter->ops->recover &&
4948 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004949 reporter->auto_recover))
4950 goto reporter_nest_cancel;
4951 if (reporter->dump_fmsg &&
4952 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4953 jiffies_to_msecs(reporter->dump_ts),
4954 DEVLINK_ATTR_PAD))
4955 goto reporter_nest_cancel;
4956
4957 nla_nest_end(msg, reporter_attr);
4958 genlmsg_end(msg, hdr);
4959 return 0;
4960
4961reporter_nest_cancel:
4962 nla_nest_end(msg, reporter_attr);
4963genlmsg_cancel:
4964 genlmsg_cancel(msg, hdr);
4965 return -EMSGSIZE;
4966}
4967
4968static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
4969 struct genl_info *info)
4970{
4971 struct devlink *devlink = info->user_ptr[0];
4972 struct devlink_health_reporter *reporter;
4973 struct sk_buff *msg;
4974 int err;
4975
4976 reporter = devlink_health_reporter_get_from_info(devlink, info);
4977 if (!reporter)
4978 return -EINVAL;
4979
4980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004981 if (!msg) {
4982 err = -ENOMEM;
4983 goto out;
4984 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004985
4986 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
4987 DEVLINK_CMD_HEALTH_REPORTER_GET,
4988 info->snd_portid, info->snd_seq,
4989 0);
4990 if (err) {
4991 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004992 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004993 }
4994
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004995 err = genlmsg_reply(msg, info);
4996out:
4997 devlink_health_reporter_put(reporter);
4998 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004999}
5000
5001static int
5002devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5003 struct netlink_callback *cb)
5004{
5005 struct devlink_health_reporter *reporter;
5006 struct devlink *devlink;
5007 int start = cb->args[0];
5008 int idx = 0;
5009 int err;
5010
5011 mutex_lock(&devlink_mutex);
5012 list_for_each_entry(devlink, &devlink_list, list) {
5013 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5014 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005015 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005016 list_for_each_entry(reporter, &devlink->reporter_list,
5017 list) {
5018 if (idx < start) {
5019 idx++;
5020 continue;
5021 }
5022 err = devlink_nl_health_reporter_fill(msg, devlink,
5023 reporter,
5024 DEVLINK_CMD_HEALTH_REPORTER_GET,
5025 NETLINK_CB(cb->skb).portid,
5026 cb->nlh->nlmsg_seq,
5027 NLM_F_MULTI);
5028 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005029 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005030 goto out;
5031 }
5032 idx++;
5033 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005034 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005035 }
5036out:
5037 mutex_unlock(&devlink_mutex);
5038
5039 cb->args[0] = idx;
5040 return msg->len;
5041}
5042
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005043static int
5044devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5045 struct genl_info *info)
5046{
5047 struct devlink *devlink = info->user_ptr[0];
5048 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005049 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005050
5051 reporter = devlink_health_reporter_get_from_info(devlink, info);
5052 if (!reporter)
5053 return -EINVAL;
5054
5055 if (!reporter->ops->recover &&
5056 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005057 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5058 err = -EOPNOTSUPP;
5059 goto out;
5060 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005061
5062 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5063 reporter->graceful_period =
5064 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5065
5066 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5067 reporter->auto_recover =
5068 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5069
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005070 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005071 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005072out:
5073 devlink_health_reporter_put(reporter);
5074 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005075}
5076
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005077static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5078 struct genl_info *info)
5079{
5080 struct devlink *devlink = info->user_ptr[0];
5081 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005082 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005083
5084 reporter = devlink_health_reporter_get_from_info(devlink, info);
5085 if (!reporter)
5086 return -EINVAL;
5087
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005088 err = devlink_health_reporter_recover(reporter, NULL);
5089
5090 devlink_health_reporter_put(reporter);
5091 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005092}
5093
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005094static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5095 struct genl_info *info)
5096{
5097 struct devlink *devlink = info->user_ptr[0];
5098 struct devlink_health_reporter *reporter;
5099 struct devlink_fmsg *fmsg;
5100 int err;
5101
5102 reporter = devlink_health_reporter_get_from_info(devlink, info);
5103 if (!reporter)
5104 return -EINVAL;
5105
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005106 if (!reporter->ops->diagnose) {
5107 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005108 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005109 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005110
5111 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005112 if (!fmsg) {
5113 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005114 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005115 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005116
5117 err = devlink_fmsg_obj_nest_start(fmsg);
5118 if (err)
5119 goto out;
5120
5121 err = reporter->ops->diagnose(reporter, fmsg);
5122 if (err)
5123 goto out;
5124
5125 err = devlink_fmsg_obj_nest_end(fmsg);
5126 if (err)
5127 goto out;
5128
5129 err = devlink_fmsg_snd(fmsg, info,
5130 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5131
5132out:
5133 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005134 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005135 return err;
5136}
5137
Aya Levine44ef4e2019-05-16 09:49:20 +03005138static int
5139devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5140 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005141{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005142 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005143 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005144 int err;
5145
Aya Levine44ef4e2019-05-16 09:49:20 +03005146 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005147 if (!reporter)
5148 return -EINVAL;
5149
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005150 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005151 err = -EOPNOTSUPP;
5152 goto out;
5153 }
5154 mutex_lock(&reporter->dump_lock);
5155 if (!start) {
5156 err = devlink_health_do_dump(reporter, NULL);
5157 if (err)
5158 goto unlock;
5159 cb->args[1] = reporter->dump_ts;
5160 }
5161 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5162 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5163 err = -EAGAIN;
5164 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005165 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005166
Aya Levine44ef4e2019-05-16 09:49:20 +03005167 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5168 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5169unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005170 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005171out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005172 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005173 return err;
5174}
5175
5176static int
5177devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5178 struct genl_info *info)
5179{
5180 struct devlink *devlink = info->user_ptr[0];
5181 struct devlink_health_reporter *reporter;
5182
5183 reporter = devlink_health_reporter_get_from_info(devlink, info);
5184 if (!reporter)
5185 return -EINVAL;
5186
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005187 if (!reporter->ops->dump) {
5188 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005189 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005190 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005191
5192 mutex_lock(&reporter->dump_lock);
5193 devlink_health_dump_clear(reporter);
5194 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005195 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005196 return 0;
5197}
5198
Ido Schimmel0f420b62019-08-17 16:28:17 +03005199struct devlink_stats {
5200 u64 rx_bytes;
5201 u64 rx_packets;
5202 struct u64_stats_sync syncp;
5203};
5204
5205/**
5206 * struct devlink_trap_group_item - Packet trap group attributes.
5207 * @group: Immutable packet trap group attributes.
5208 * @refcount: Number of trap items using the group.
5209 * @list: trap_group_list member.
5210 * @stats: Trap group statistics.
5211 *
5212 * Describes packet trap group attributes. Created by devlink during trap
5213 * registration.
5214 */
5215struct devlink_trap_group_item {
5216 const struct devlink_trap_group *group;
5217 refcount_t refcount;
5218 struct list_head list;
5219 struct devlink_stats __percpu *stats;
5220};
5221
5222/**
5223 * struct devlink_trap_item - Packet trap attributes.
5224 * @trap: Immutable packet trap attributes.
5225 * @group_item: Associated group item.
5226 * @list: trap_list member.
5227 * @action: Trap action.
5228 * @stats: Trap statistics.
5229 * @priv: Driver private information.
5230 *
5231 * Describes both mutable and immutable packet trap attributes. Created by
5232 * devlink during trap registration and used for all trap related operations.
5233 */
5234struct devlink_trap_item {
5235 const struct devlink_trap *trap;
5236 struct devlink_trap_group_item *group_item;
5237 struct list_head list;
5238 enum devlink_trap_action action;
5239 struct devlink_stats __percpu *stats;
5240 void *priv;
5241};
5242
5243static struct devlink_trap_item *
5244devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5245{
5246 struct devlink_trap_item *trap_item;
5247
5248 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5249 if (!strcmp(trap_item->trap->name, name))
5250 return trap_item;
5251 }
5252
5253 return NULL;
5254}
5255
5256static struct devlink_trap_item *
5257devlink_trap_item_get_from_info(struct devlink *devlink,
5258 struct genl_info *info)
5259{
5260 struct nlattr *attr;
5261
5262 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5263 return NULL;
5264 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5265
5266 return devlink_trap_item_lookup(devlink, nla_data(attr));
5267}
5268
5269static int
5270devlink_trap_action_get_from_info(struct genl_info *info,
5271 enum devlink_trap_action *p_trap_action)
5272{
5273 u8 val;
5274
5275 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5276 switch (val) {
5277 case DEVLINK_TRAP_ACTION_DROP: /* fall-through */
5278 case DEVLINK_TRAP_ACTION_TRAP:
5279 *p_trap_action = val;
5280 break;
5281 default:
5282 return -EINVAL;
5283 }
5284
5285 return 0;
5286}
5287
5288static int devlink_trap_metadata_put(struct sk_buff *msg,
5289 const struct devlink_trap *trap)
5290{
5291 struct nlattr *attr;
5292
5293 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5294 if (!attr)
5295 return -EMSGSIZE;
5296
5297 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5298 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5299 goto nla_put_failure;
5300
5301 nla_nest_end(msg, attr);
5302
5303 return 0;
5304
5305nla_put_failure:
5306 nla_nest_cancel(msg, attr);
5307 return -EMSGSIZE;
5308}
5309
5310static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5311 struct devlink_stats *stats)
5312{
5313 int i;
5314
5315 memset(stats, 0, sizeof(*stats));
5316 for_each_possible_cpu(i) {
5317 struct devlink_stats *cpu_stats;
5318 u64 rx_packets, rx_bytes;
5319 unsigned int start;
5320
5321 cpu_stats = per_cpu_ptr(trap_stats, i);
5322 do {
5323 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5324 rx_packets = cpu_stats->rx_packets;
5325 rx_bytes = cpu_stats->rx_bytes;
5326 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5327
5328 stats->rx_packets += rx_packets;
5329 stats->rx_bytes += rx_bytes;
5330 }
5331}
5332
5333static int devlink_trap_stats_put(struct sk_buff *msg,
5334 struct devlink_stats __percpu *trap_stats)
5335{
5336 struct devlink_stats stats;
5337 struct nlattr *attr;
5338
5339 devlink_trap_stats_read(trap_stats, &stats);
5340
5341 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5342 if (!attr)
5343 return -EMSGSIZE;
5344
5345 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5346 stats.rx_packets, DEVLINK_ATTR_PAD))
5347 goto nla_put_failure;
5348
5349 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5350 stats.rx_bytes, DEVLINK_ATTR_PAD))
5351 goto nla_put_failure;
5352
5353 nla_nest_end(msg, attr);
5354
5355 return 0;
5356
5357nla_put_failure:
5358 nla_nest_cancel(msg, attr);
5359 return -EMSGSIZE;
5360}
5361
5362static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5363 const struct devlink_trap_item *trap_item,
5364 enum devlink_command cmd, u32 portid, u32 seq,
5365 int flags)
5366{
5367 struct devlink_trap_group_item *group_item = trap_item->group_item;
5368 void *hdr;
5369 int err;
5370
5371 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5372 if (!hdr)
5373 return -EMSGSIZE;
5374
5375 if (devlink_nl_put_handle(msg, devlink))
5376 goto nla_put_failure;
5377
5378 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5379 group_item->group->name))
5380 goto nla_put_failure;
5381
5382 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5383 goto nla_put_failure;
5384
5385 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5386 goto nla_put_failure;
5387
5388 if (trap_item->trap->generic &&
5389 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5390 goto nla_put_failure;
5391
5392 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5393 goto nla_put_failure;
5394
5395 err = devlink_trap_metadata_put(msg, trap_item->trap);
5396 if (err)
5397 goto nla_put_failure;
5398
5399 err = devlink_trap_stats_put(msg, trap_item->stats);
5400 if (err)
5401 goto nla_put_failure;
5402
5403 genlmsg_end(msg, hdr);
5404
5405 return 0;
5406
5407nla_put_failure:
5408 genlmsg_cancel(msg, hdr);
5409 return -EMSGSIZE;
5410}
5411
5412static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
5413 struct genl_info *info)
5414{
5415 struct netlink_ext_ack *extack = info->extack;
5416 struct devlink *devlink = info->user_ptr[0];
5417 struct devlink_trap_item *trap_item;
5418 struct sk_buff *msg;
5419 int err;
5420
5421 if (list_empty(&devlink->trap_list))
5422 return -EOPNOTSUPP;
5423
5424 trap_item = devlink_trap_item_get_from_info(devlink, info);
5425 if (!trap_item) {
5426 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5427 return -ENOENT;
5428 }
5429
5430 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5431 if (!msg)
5432 return -ENOMEM;
5433
5434 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5435 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
5436 info->snd_seq, 0);
5437 if (err)
5438 goto err_trap_fill;
5439
5440 return genlmsg_reply(msg, info);
5441
5442err_trap_fill:
5443 nlmsg_free(msg);
5444 return err;
5445}
5446
5447static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
5448 struct netlink_callback *cb)
5449{
5450 struct devlink_trap_item *trap_item;
5451 struct devlink *devlink;
5452 int start = cb->args[0];
5453 int idx = 0;
5454 int err;
5455
5456 mutex_lock(&devlink_mutex);
5457 list_for_each_entry(devlink, &devlink_list, list) {
5458 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5459 continue;
5460 mutex_lock(&devlink->lock);
5461 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5462 if (idx < start) {
5463 idx++;
5464 continue;
5465 }
5466 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5467 DEVLINK_CMD_TRAP_NEW,
5468 NETLINK_CB(cb->skb).portid,
5469 cb->nlh->nlmsg_seq,
5470 NLM_F_MULTI);
5471 if (err) {
5472 mutex_unlock(&devlink->lock);
5473 goto out;
5474 }
5475 idx++;
5476 }
5477 mutex_unlock(&devlink->lock);
5478 }
5479out:
5480 mutex_unlock(&devlink_mutex);
5481
5482 cb->args[0] = idx;
5483 return msg->len;
5484}
5485
5486static int __devlink_trap_action_set(struct devlink *devlink,
5487 struct devlink_trap_item *trap_item,
5488 enum devlink_trap_action trap_action,
5489 struct netlink_ext_ack *extack)
5490{
5491 int err;
5492
5493 if (trap_item->action != trap_action &&
5494 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
5495 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
5496 return 0;
5497 }
5498
5499 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
5500 trap_action);
5501 if (err)
5502 return err;
5503
5504 trap_item->action = trap_action;
5505
5506 return 0;
5507}
5508
5509static int devlink_trap_action_set(struct devlink *devlink,
5510 struct devlink_trap_item *trap_item,
5511 struct genl_info *info)
5512{
5513 enum devlink_trap_action trap_action;
5514 int err;
5515
5516 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5517 return 0;
5518
5519 err = devlink_trap_action_get_from_info(info, &trap_action);
5520 if (err) {
5521 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5522 return -EINVAL;
5523 }
5524
5525 return __devlink_trap_action_set(devlink, trap_item, trap_action,
5526 info->extack);
5527}
5528
5529static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
5530 struct genl_info *info)
5531{
5532 struct netlink_ext_ack *extack = info->extack;
5533 struct devlink *devlink = info->user_ptr[0];
5534 struct devlink_trap_item *trap_item;
5535 int err;
5536
5537 if (list_empty(&devlink->trap_list))
5538 return -EOPNOTSUPP;
5539
5540 trap_item = devlink_trap_item_get_from_info(devlink, info);
5541 if (!trap_item) {
5542 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5543 return -ENOENT;
5544 }
5545
5546 err = devlink_trap_action_set(devlink, trap_item, info);
5547 if (err)
5548 return err;
5549
5550 return 0;
5551}
5552
5553static struct devlink_trap_group_item *
5554devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
5555{
5556 struct devlink_trap_group_item *group_item;
5557
5558 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5559 if (!strcmp(group_item->group->name, name))
5560 return group_item;
5561 }
5562
5563 return NULL;
5564}
5565
5566static struct devlink_trap_group_item *
5567devlink_trap_group_item_get_from_info(struct devlink *devlink,
5568 struct genl_info *info)
5569{
5570 char *name;
5571
5572 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
5573 return NULL;
5574 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
5575
5576 return devlink_trap_group_item_lookup(devlink, name);
5577}
5578
5579static int
5580devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
5581 const struct devlink_trap_group_item *group_item,
5582 enum devlink_command cmd, u32 portid, u32 seq,
5583 int flags)
5584{
5585 void *hdr;
5586 int err;
5587
5588 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5589 if (!hdr)
5590 return -EMSGSIZE;
5591
5592 if (devlink_nl_put_handle(msg, devlink))
5593 goto nla_put_failure;
5594
5595 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5596 group_item->group->name))
5597 goto nla_put_failure;
5598
5599 if (group_item->group->generic &&
5600 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5601 goto nla_put_failure;
5602
5603 err = devlink_trap_stats_put(msg, group_item->stats);
5604 if (err)
5605 goto nla_put_failure;
5606
5607 genlmsg_end(msg, hdr);
5608
5609 return 0;
5610
5611nla_put_failure:
5612 genlmsg_cancel(msg, hdr);
5613 return -EMSGSIZE;
5614}
5615
5616static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
5617 struct genl_info *info)
5618{
5619 struct netlink_ext_ack *extack = info->extack;
5620 struct devlink *devlink = info->user_ptr[0];
5621 struct devlink_trap_group_item *group_item;
5622 struct sk_buff *msg;
5623 int err;
5624
5625 if (list_empty(&devlink->trap_group_list))
5626 return -EOPNOTSUPP;
5627
5628 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5629 if (!group_item) {
5630 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5631 return -ENOENT;
5632 }
5633
5634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5635 if (!msg)
5636 return -ENOMEM;
5637
5638 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5639 DEVLINK_CMD_TRAP_GROUP_NEW,
5640 info->snd_portid, info->snd_seq, 0);
5641 if (err)
5642 goto err_trap_group_fill;
5643
5644 return genlmsg_reply(msg, info);
5645
5646err_trap_group_fill:
5647 nlmsg_free(msg);
5648 return err;
5649}
5650
5651static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
5652 struct netlink_callback *cb)
5653{
5654 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
5655 struct devlink_trap_group_item *group_item;
5656 u32 portid = NETLINK_CB(cb->skb).portid;
5657 struct devlink *devlink;
5658 int start = cb->args[0];
5659 int idx = 0;
5660 int err;
5661
5662 mutex_lock(&devlink_mutex);
5663 list_for_each_entry(devlink, &devlink_list, list) {
5664 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5665 continue;
5666 mutex_lock(&devlink->lock);
5667 list_for_each_entry(group_item, &devlink->trap_group_list,
5668 list) {
5669 if (idx < start) {
5670 idx++;
5671 continue;
5672 }
5673 err = devlink_nl_trap_group_fill(msg, devlink,
5674 group_item, cmd,
5675 portid,
5676 cb->nlh->nlmsg_seq,
5677 NLM_F_MULTI);
5678 if (err) {
5679 mutex_unlock(&devlink->lock);
5680 goto out;
5681 }
5682 idx++;
5683 }
5684 mutex_unlock(&devlink->lock);
5685 }
5686out:
5687 mutex_unlock(&devlink_mutex);
5688
5689 cb->args[0] = idx;
5690 return msg->len;
5691}
5692
5693static int
5694__devlink_trap_group_action_set(struct devlink *devlink,
5695 struct devlink_trap_group_item *group_item,
5696 enum devlink_trap_action trap_action,
5697 struct netlink_ext_ack *extack)
5698{
5699 const char *group_name = group_item->group->name;
5700 struct devlink_trap_item *trap_item;
5701 int err;
5702
5703 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5704 if (strcmp(trap_item->trap->group.name, group_name))
5705 continue;
5706 err = __devlink_trap_action_set(devlink, trap_item,
5707 trap_action, extack);
5708 if (err)
5709 return err;
5710 }
5711
5712 return 0;
5713}
5714
5715static int
5716devlink_trap_group_action_set(struct devlink *devlink,
5717 struct devlink_trap_group_item *group_item,
5718 struct genl_info *info)
5719{
5720 enum devlink_trap_action trap_action;
5721 int err;
5722
5723 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5724 return 0;
5725
5726 err = devlink_trap_action_get_from_info(info, &trap_action);
5727 if (err) {
5728 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5729 return -EINVAL;
5730 }
5731
5732 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
5733 info->extack);
5734 if (err)
5735 return err;
5736
5737 return 0;
5738}
5739
5740static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
5741 struct genl_info *info)
5742{
5743 struct netlink_ext_ack *extack = info->extack;
5744 struct devlink *devlink = info->user_ptr[0];
5745 struct devlink_trap_group_item *group_item;
5746 int err;
5747
5748 if (list_empty(&devlink->trap_group_list))
5749 return -EOPNOTSUPP;
5750
5751 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5752 if (!group_item) {
5753 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5754 return -ENOENT;
5755 }
5756
5757 err = devlink_trap_group_action_set(devlink, group_item, info);
5758 if (err)
5759 return err;
5760
5761 return 0;
5762}
5763
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005764static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
5765 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
5766 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
5767 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
5768 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
5769 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005770 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
5771 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
5772 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
5773 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
5774 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
5775 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
5776 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005777 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02005778 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03005779 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005780 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
5781 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005782 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
5783 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005784 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
5785 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
5786 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005787 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03005788 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005789 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005790 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
5791 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005792 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
5793 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Ido Schimmel0f420b62019-08-17 16:28:17 +03005794 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
5795 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
5796 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005797};
5798
5799static const struct genl_ops devlink_nl_ops[] = {
5800 {
5801 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005802 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005803 .doit = devlink_nl_cmd_get_doit,
5804 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02005805 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005806 /* can be retrieved by unprivileged users */
5807 },
5808 {
5809 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005810 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005811 .doit = devlink_nl_cmd_port_get_doit,
5812 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005813 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5814 /* can be retrieved by unprivileged users */
5815 },
5816 {
5817 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005818 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005819 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005820 .flags = GENL_ADMIN_PERM,
5821 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5822 },
5823 {
5824 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005825 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005826 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005827 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005828 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5829 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005830 },
5831 {
5832 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005833 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005834 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005835 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005836 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5837 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005838 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005839 {
5840 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005841 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005842 .doit = devlink_nl_cmd_sb_get_doit,
5843 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005844 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5845 DEVLINK_NL_FLAG_NEED_SB,
5846 /* can be retrieved by unprivileged users */
5847 },
5848 {
5849 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005850 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005851 .doit = devlink_nl_cmd_sb_pool_get_doit,
5852 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005853 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5854 DEVLINK_NL_FLAG_NEED_SB,
5855 /* can be retrieved by unprivileged users */
5856 },
5857 {
5858 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005859 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005860 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005861 .flags = GENL_ADMIN_PERM,
5862 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5863 DEVLINK_NL_FLAG_NEED_SB,
5864 },
5865 {
5866 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005867 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005868 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5869 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005870 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5871 DEVLINK_NL_FLAG_NEED_SB,
5872 /* can be retrieved by unprivileged users */
5873 },
5874 {
5875 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005876 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005877 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005878 .flags = GENL_ADMIN_PERM,
5879 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5880 DEVLINK_NL_FLAG_NEED_SB,
5881 },
5882 {
5883 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005884 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005885 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5886 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005887 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5888 DEVLINK_NL_FLAG_NEED_SB,
5889 /* can be retrieved by unprivileged users */
5890 },
5891 {
5892 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005893 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005894 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005895 .flags = GENL_ADMIN_PERM,
5896 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5897 DEVLINK_NL_FLAG_NEED_SB,
5898 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005899 {
5900 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005901 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005902 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005903 .flags = GENL_ADMIN_PERM,
5904 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005905 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005906 },
5907 {
5908 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02005909 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005910 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005911 .flags = GENL_ADMIN_PERM,
5912 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005913 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005914 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005915 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005916 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005917 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005918 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005919 .flags = GENL_ADMIN_PERM,
5920 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5921 },
5922 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005923 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005924 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005925 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005926 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07005927 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5928 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005929 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005930 {
5931 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005932 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005933 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005934 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005935 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005936 },
5937 {
5938 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005939 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005940 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005941 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005942 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005943 },
5944 {
5945 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005946 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005947 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005948 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005949 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005950 },
5951 {
5952 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005953 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005954 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005955 .flags = GENL_ADMIN_PERM,
5956 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5957 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005958 {
5959 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005960 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005961 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005962 .flags = GENL_ADMIN_PERM,
5963 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5964 },
5965 {
5966 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02005967 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005968 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005969 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005970 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005971 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005972 {
5973 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02005974 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005975 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005976 .flags = GENL_ADMIN_PERM,
5977 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5978 DEVLINK_NL_FLAG_NO_LOCK,
5979 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005980 {
5981 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005982 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005983 .doit = devlink_nl_cmd_param_get_doit,
5984 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005985 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5986 /* can be retrieved by unprivileged users */
5987 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005988 {
5989 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005990 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005991 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005992 .flags = GENL_ADMIN_PERM,
5993 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5994 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005995 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05305996 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005997 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05305998 .doit = devlink_nl_cmd_port_param_get_doit,
5999 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306000 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6001 /* can be retrieved by unprivileged users */
6002 },
6003 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05306004 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006005 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306006 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306007 .flags = GENL_ADMIN_PERM,
6008 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6009 },
6010 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006011 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006012 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006013 .doit = devlink_nl_cmd_region_get_doit,
6014 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006015 .flags = GENL_ADMIN_PERM,
6016 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6017 },
Alex Vesker866319b2018-07-12 15:13:13 +03006018 {
6019 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02006020 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03006021 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03006022 .flags = GENL_ADMIN_PERM,
6023 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6024 },
Alex Vesker4e547952018-07-12 15:13:14 +03006025 {
6026 .cmd = DEVLINK_CMD_REGION_READ,
Johannes Bergef6243a2019-04-26 14:07:31 +02006027 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker4e547952018-07-12 15:13:14 +03006028 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03006029 .flags = GENL_ADMIN_PERM,
6030 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6031 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006032 {
6033 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006034 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006035 .doit = devlink_nl_cmd_info_get_doit,
6036 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006037 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6038 /* can be retrieved by unprivileged users */
6039 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006040 {
6041 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006042 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006043 .doit = devlink_nl_cmd_health_reporter_get_doit,
6044 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006045 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6046 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006047 /* can be retrieved by unprivileged users */
6048 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006049 {
6050 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006051 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006052 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006053 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006054 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6055 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006056 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006057 {
6058 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02006059 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006060 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006061 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006062 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6063 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006064 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006065 {
6066 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006067 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006068 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006069 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006070 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6071 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006072 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006073 {
6074 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006075 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Aya Levine44ef4e2019-05-16 09:49:20 +03006076 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006077 .flags = GENL_ADMIN_PERM,
6078 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6079 DEVLINK_NL_FLAG_NO_LOCK,
6080 },
6081 {
6082 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006083 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006084 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006085 .flags = GENL_ADMIN_PERM,
6086 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6087 DEVLINK_NL_FLAG_NO_LOCK,
6088 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006089 {
6090 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006091 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006092 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006093 .flags = GENL_ADMIN_PERM,
6094 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6095 },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006096 {
6097 .cmd = DEVLINK_CMD_TRAP_GET,
6098 .doit = devlink_nl_cmd_trap_get_doit,
6099 .dumpit = devlink_nl_cmd_trap_get_dumpit,
6100 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6101 /* can be retrieved by unprivileged users */
6102 },
6103 {
6104 .cmd = DEVLINK_CMD_TRAP_SET,
6105 .doit = devlink_nl_cmd_trap_set_doit,
6106 .flags = GENL_ADMIN_PERM,
6107 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6108 },
6109 {
6110 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
6111 .doit = devlink_nl_cmd_trap_group_get_doit,
6112 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
6113 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6114 /* can be retrieved by unprivileged users */
6115 },
6116 {
6117 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
6118 .doit = devlink_nl_cmd_trap_group_set_doit,
6119 .flags = GENL_ADMIN_PERM,
6120 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6121 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006122};
6123
Johannes Berg56989f62016-10-24 14:40:05 +02006124static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02006125 .name = DEVLINK_GENL_NAME,
6126 .version = DEVLINK_GENL_VERSION,
6127 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01006128 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02006129 .netnsok = true,
6130 .pre_doit = devlink_nl_pre_doit,
6131 .post_doit = devlink_nl_post_doit,
6132 .module = THIS_MODULE,
6133 .ops = devlink_nl_ops,
6134 .n_ops = ARRAY_SIZE(devlink_nl_ops),
6135 .mcgrps = devlink_nl_mcgrps,
6136 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
6137};
6138
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006139/**
6140 * devlink_alloc - Allocate new devlink instance resources
6141 *
6142 * @ops: ops
6143 * @priv_size: size of user private data
6144 *
6145 * Allocate new devlink instance resources, including devlink index
6146 * and name.
6147 */
6148struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
6149{
6150 struct devlink *devlink;
6151
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006152 if (WARN_ON(!ops))
6153 return NULL;
6154
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006155 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
6156 if (!devlink)
6157 return NULL;
6158 devlink->ops = ops;
6159 devlink_net_set(devlink, &init_net);
6160 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02006161 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006162 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006163 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006164 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006165 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02006166 INIT_LIST_HEAD(&devlink->reporter_list);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006167 INIT_LIST_HEAD(&devlink->trap_list);
6168 INIT_LIST_HEAD(&devlink->trap_group_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006169 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006170 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006171 return devlink;
6172}
6173EXPORT_SYMBOL_GPL(devlink_alloc);
6174
6175/**
6176 * devlink_register - Register devlink instance
6177 *
6178 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006179 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006180 */
6181int devlink_register(struct devlink *devlink, struct device *dev)
6182{
6183 mutex_lock(&devlink_mutex);
6184 devlink->dev = dev;
6185 list_add_tail(&devlink->list, &devlink_list);
6186 devlink_notify(devlink, DEVLINK_CMD_NEW);
6187 mutex_unlock(&devlink_mutex);
6188 return 0;
6189}
6190EXPORT_SYMBOL_GPL(devlink_register);
6191
6192/**
6193 * devlink_unregister - Unregister devlink instance
6194 *
6195 * @devlink: devlink
6196 */
6197void devlink_unregister(struct devlink *devlink)
6198{
6199 mutex_lock(&devlink_mutex);
6200 devlink_notify(devlink, DEVLINK_CMD_DEL);
6201 list_del(&devlink->list);
6202 mutex_unlock(&devlink_mutex);
6203}
6204EXPORT_SYMBOL_GPL(devlink_unregister);
6205
6206/**
6207 * devlink_free - Free devlink instance resources
6208 *
6209 * @devlink: devlink
6210 */
6211void devlink_free(struct devlink *devlink)
6212{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006213 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01006214 mutex_destroy(&devlink->lock);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006215 WARN_ON(!list_empty(&devlink->trap_group_list));
6216 WARN_ON(!list_empty(&devlink->trap_list));
Parav Panditb904aad2019-02-08 15:15:00 -06006217 WARN_ON(!list_empty(&devlink->reporter_list));
6218 WARN_ON(!list_empty(&devlink->region_list));
6219 WARN_ON(!list_empty(&devlink->param_list));
6220 WARN_ON(!list_empty(&devlink->resource_list));
6221 WARN_ON(!list_empty(&devlink->dpipe_table_list));
6222 WARN_ON(!list_empty(&devlink->sb_list));
6223 WARN_ON(!list_empty(&devlink->port_list));
6224
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006225 kfree(devlink);
6226}
6227EXPORT_SYMBOL_GPL(devlink_free);
6228
Jiri Pirko136bf272019-05-23 10:43:35 +02006229static void devlink_port_type_warn(struct work_struct *work)
6230{
6231 WARN(true, "Type was not set for devlink port.");
6232}
6233
6234static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
6235{
6236 /* Ignore CPU and DSA flavours. */
6237 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
6238 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
6239}
6240
6241#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30)
6242
6243static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
6244{
6245 if (!devlink_port_type_should_warn(devlink_port))
6246 return;
6247 /* Schedule a work to WARN in case driver does not set port
6248 * type within timeout.
6249 */
6250 schedule_delayed_work(&devlink_port->type_warn_dw,
6251 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
6252}
6253
6254static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
6255{
6256 if (!devlink_port_type_should_warn(devlink_port))
6257 return;
6258 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
6259}
6260
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006261/**
6262 * devlink_port_register - Register devlink port
6263 *
6264 * @devlink: devlink
6265 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006266 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006267 *
6268 * Register devlink port with provided port index. User can use
6269 * any indexing, even hw-related one. devlink_port structure
6270 * is convenient to be embedded inside user driver private structure.
6271 * Note that the caller should take care of zeroing the devlink_port
6272 * structure.
6273 */
6274int devlink_port_register(struct devlink *devlink,
6275 struct devlink_port *devlink_port,
6276 unsigned int port_index)
6277{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006278 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006279 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006280 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006281 return -EEXIST;
6282 }
6283 devlink_port->devlink = devlink;
6284 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006285 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01006286 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006287 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306288 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006289 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02006290 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
6291 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006292 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6293 return 0;
6294}
6295EXPORT_SYMBOL_GPL(devlink_port_register);
6296
6297/**
6298 * devlink_port_unregister - Unregister devlink port
6299 *
6300 * @devlink_port: devlink port
6301 */
6302void devlink_port_unregister(struct devlink_port *devlink_port)
6303{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006304 struct devlink *devlink = devlink_port->devlink;
6305
Jiri Pirko136bf272019-05-23 10:43:35 +02006306 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006307 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006308 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006309 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006310 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006311}
6312EXPORT_SYMBOL_GPL(devlink_port_unregister);
6313
6314static void __devlink_port_type_set(struct devlink_port *devlink_port,
6315 enum devlink_port_type type,
6316 void *type_dev)
6317{
Jiri Pirko2b239e72019-03-24 11:14:36 +01006318 if (WARN_ON(!devlink_port->registered))
6319 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02006320 devlink_port_type_warn_cancel(devlink_port);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006321 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006322 devlink_port->type = type;
6323 devlink_port->type_dev = type_dev;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006324 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006325 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6326}
6327
6328/**
6329 * devlink_port_type_eth_set - Set port type to Ethernet
6330 *
6331 * @devlink_port: devlink port
6332 * @netdev: related netdevice
6333 */
6334void devlink_port_type_eth_set(struct devlink_port *devlink_port,
6335 struct net_device *netdev)
6336{
Jiri Pirko119c0b52019-04-03 14:24:27 +02006337 const struct net_device_ops *ops = netdev->netdev_ops;
6338
Jiri Pirko746364f2019-03-28 13:56:46 +01006339 /* If driver registers devlink port, it should set devlink port
6340 * attributes accordingly so the compat functions are called
6341 * and the original ops are not used.
6342 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02006343 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01006344 /* Some drivers use the same set of ndos for netdevs
6345 * that have devlink_port registered and also for
6346 * those who don't. Make sure that ndo_get_phys_port_name
6347 * returns -EOPNOTSUPP here in case it is defined.
6348 * Warn if not.
6349 */
Jiri Pirko746364f2019-03-28 13:56:46 +01006350 char name[IFNAMSIZ];
6351 int err;
6352
6353 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
6354 WARN_ON(err != -EOPNOTSUPP);
6355 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02006356 if (ops->ndo_get_port_parent_id) {
6357 /* Some drivers use the same set of ndos for netdevs
6358 * that have devlink_port registered and also for
6359 * those who don't. Make sure that ndo_get_port_parent_id
6360 * returns -EOPNOTSUPP here in case it is defined.
6361 * Warn if not.
6362 */
6363 struct netdev_phys_item_id ppid;
6364 int err;
6365
6366 err = ops->ndo_get_port_parent_id(netdev, &ppid);
6367 WARN_ON(err != -EOPNOTSUPP);
6368 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01006369 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006370}
6371EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
6372
6373/**
6374 * devlink_port_type_ib_set - Set port type to InfiniBand
6375 *
6376 * @devlink_port: devlink port
6377 * @ibdev: related IB device
6378 */
6379void devlink_port_type_ib_set(struct devlink_port *devlink_port,
6380 struct ib_device *ibdev)
6381{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006382 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006383}
6384EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
6385
6386/**
6387 * devlink_port_type_clear - Clear port type
6388 *
6389 * @devlink_port: devlink port
6390 */
6391void devlink_port_type_clear(struct devlink_port *devlink_port)
6392{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006393 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02006394 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006395}
6396EXPORT_SYMBOL_GPL(devlink_port_type_clear);
6397
Parav Pandit378ef012019-07-08 23:17:35 -05006398static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
6399 enum devlink_port_flavour flavour,
6400 const unsigned char *switch_id,
6401 unsigned char switch_id_len)
6402{
6403 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6404
6405 if (WARN_ON(devlink_port->registered))
6406 return -EEXIST;
6407 attrs->set = true;
6408 attrs->flavour = flavour;
6409 if (switch_id) {
6410 attrs->switch_port = true;
6411 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
6412 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
6413 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
6414 attrs->switch_id.id_len = switch_id_len;
6415 } else {
6416 attrs->switch_port = false;
6417 }
6418 return 0;
6419}
6420
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006421/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006422 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006423 *
6424 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02006425 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006426 * @port_number: number of the port that is facing user, for example
6427 * the front panel port number
6428 * @split: indicates if this is split port
6429 * @split_subport_number: if the port is split, this is the number
6430 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02006431 * @switch_id: if the port is part of switch, this is buffer with ID,
6432 * otwerwise this is NULL
6433 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006434 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006435void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02006436 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006437 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02006438 u32 split_subport_number,
6439 const unsigned char *switch_id,
6440 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006441{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006442 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05006443 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006444
Parav Pandit378ef012019-07-08 23:17:35 -05006445 ret = __devlink_port_attrs_set(devlink_port, flavour,
6446 switch_id, switch_id_len);
6447 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01006448 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006449 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05006450 attrs->phys.port_number = port_number;
6451 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006452}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006453EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006454
Parav Pandit98fd2d62019-07-08 23:17:37 -05006455/**
6456 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
6457 *
6458 * @devlink_port: devlink port
6459 * @pf: associated PF for the devlink port instance
6460 * @switch_id: if the port is part of switch, this is buffer with ID,
6461 * otherwise this is NULL
6462 * @switch_id_len: length of the switch_id buffer
6463 */
6464void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
6465 const unsigned char *switch_id,
6466 unsigned char switch_id_len, u16 pf)
6467{
6468 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6469 int ret;
6470
6471 ret = __devlink_port_attrs_set(devlink_port,
6472 DEVLINK_PORT_FLAVOUR_PCI_PF,
6473 switch_id, switch_id_len);
6474 if (ret)
6475 return;
6476
6477 attrs->pci_pf.pf = pf;
6478}
6479EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
6480
Parav Pandite41b6bf2019-07-08 23:17:38 -05006481/**
6482 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
6483 *
6484 * @devlink_port: devlink port
6485 * @pf: associated PF for the devlink port instance
6486 * @vf: associated VF of a PF for the devlink port instance
6487 * @switch_id: if the port is part of switch, this is buffer with ID,
6488 * otherwise this is NULL
6489 * @switch_id_len: length of the switch_id buffer
6490 */
6491void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
6492 const unsigned char *switch_id,
6493 unsigned char switch_id_len,
6494 u16 pf, u16 vf)
6495{
6496 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6497 int ret;
6498
6499 ret = __devlink_port_attrs_set(devlink_port,
6500 DEVLINK_PORT_FLAVOUR_PCI_VF,
6501 switch_id, switch_id_len);
6502 if (ret)
6503 return;
6504 attrs->pci_vf.pf = pf;
6505 attrs->pci_vf.vf = vf;
6506}
6507EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
6508
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006509static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
6510 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02006511{
6512 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6513 int n = 0;
6514
6515 if (!attrs->set)
6516 return -EOPNOTSUPP;
6517
6518 switch (attrs->flavour) {
6519 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
6520 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05006521 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006522 else
Parav Pandit378ef012019-07-08 23:17:35 -05006523 n = snprintf(name, len, "p%us%u",
6524 attrs->phys.port_number,
6525 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006526 break;
6527 case DEVLINK_PORT_FLAVOUR_CPU:
6528 case DEVLINK_PORT_FLAVOUR_DSA:
6529 /* As CPU and DSA ports do not have a netdevice associated
6530 * case should not ever happen.
6531 */
6532 WARN_ON(1);
6533 return -EINVAL;
Parav Pandit98fd2d62019-07-08 23:17:37 -05006534 case DEVLINK_PORT_FLAVOUR_PCI_PF:
6535 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
6536 break;
Parav Pandite41b6bf2019-07-08 23:17:38 -05006537 case DEVLINK_PORT_FLAVOUR_PCI_VF:
6538 n = snprintf(name, len, "pf%uvf%u",
6539 attrs->pci_vf.pf, attrs->pci_vf.vf);
6540 break;
Jiri Pirko08474c12018-05-18 09:29:02 +02006541 }
6542
6543 if (n >= len)
6544 return -EINVAL;
6545
6546 return 0;
6547}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006548
Jiri Pirkobf797472016-04-14 18:19:13 +02006549int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
6550 u32 size, u16 ingress_pools_count,
6551 u16 egress_pools_count, u16 ingress_tc_count,
6552 u16 egress_tc_count)
6553{
6554 struct devlink_sb *devlink_sb;
6555 int err = 0;
6556
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006557 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006558 if (devlink_sb_index_exists(devlink, sb_index)) {
6559 err = -EEXIST;
6560 goto unlock;
6561 }
6562
6563 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
6564 if (!devlink_sb) {
6565 err = -ENOMEM;
6566 goto unlock;
6567 }
6568 devlink_sb->index = sb_index;
6569 devlink_sb->size = size;
6570 devlink_sb->ingress_pools_count = ingress_pools_count;
6571 devlink_sb->egress_pools_count = egress_pools_count;
6572 devlink_sb->ingress_tc_count = ingress_tc_count;
6573 devlink_sb->egress_tc_count = egress_tc_count;
6574 list_add_tail(&devlink_sb->list, &devlink->sb_list);
6575unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006576 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006577 return err;
6578}
6579EXPORT_SYMBOL_GPL(devlink_sb_register);
6580
6581void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
6582{
6583 struct devlink_sb *devlink_sb;
6584
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006585 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006586 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
6587 WARN_ON(!devlink_sb);
6588 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006589 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006590 kfree(devlink_sb);
6591}
6592EXPORT_SYMBOL_GPL(devlink_sb_unregister);
6593
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006594/**
6595 * devlink_dpipe_headers_register - register dpipe headers
6596 *
6597 * @devlink: devlink
6598 * @dpipe_headers: dpipe header array
6599 *
6600 * Register the headers supported by hardware.
6601 */
6602int devlink_dpipe_headers_register(struct devlink *devlink,
6603 struct devlink_dpipe_headers *dpipe_headers)
6604{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006605 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006606 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006607 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006608 return 0;
6609}
6610EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
6611
6612/**
6613 * devlink_dpipe_headers_unregister - unregister dpipe headers
6614 *
6615 * @devlink: devlink
6616 *
6617 * Unregister the headers supported by hardware.
6618 */
6619void devlink_dpipe_headers_unregister(struct devlink *devlink)
6620{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006621 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006622 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006623 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006624}
6625EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
6626
6627/**
6628 * devlink_dpipe_table_counter_enabled - check if counter allocation
6629 * required
6630 * @devlink: devlink
6631 * @table_name: tables name
6632 *
6633 * Used by driver to check if counter allocation is required.
6634 * After counter allocation is turned on the table entries
6635 * are updated to include counter statistics.
6636 *
6637 * After that point on the driver must respect the counter
6638 * state so that each entry added to the table is added
6639 * with a counter.
6640 */
6641bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
6642 const char *table_name)
6643{
6644 struct devlink_dpipe_table *table;
6645 bool enabled;
6646
6647 rcu_read_lock();
6648 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6649 table_name);
6650 enabled = false;
6651 if (table)
6652 enabled = table->counters_enabled;
6653 rcu_read_unlock();
6654 return enabled;
6655}
6656EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
6657
6658/**
6659 * devlink_dpipe_table_register - register dpipe table
6660 *
6661 * @devlink: devlink
6662 * @table_name: table name
6663 * @table_ops: table ops
6664 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006665 * @counter_control_extern: external control for counters
6666 */
6667int devlink_dpipe_table_register(struct devlink *devlink,
6668 const char *table_name,
6669 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006670 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006671{
6672 struct devlink_dpipe_table *table;
6673
6674 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
6675 return -EEXIST;
6676
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006677 if (WARN_ON(!table_ops->size_get))
6678 return -EINVAL;
6679
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006680 table = kzalloc(sizeof(*table), GFP_KERNEL);
6681 if (!table)
6682 return -ENOMEM;
6683
6684 table->name = table_name;
6685 table->table_ops = table_ops;
6686 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006687 table->counter_control_extern = counter_control_extern;
6688
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006689 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006690 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006691 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006692 return 0;
6693}
6694EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
6695
6696/**
6697 * devlink_dpipe_table_unregister - unregister dpipe table
6698 *
6699 * @devlink: devlink
6700 * @table_name: table name
6701 */
6702void devlink_dpipe_table_unregister(struct devlink *devlink,
6703 const char *table_name)
6704{
6705 struct devlink_dpipe_table *table;
6706
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006707 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006708 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6709 table_name);
6710 if (!table)
6711 goto unlock;
6712 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006713 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006714 kfree_rcu(table, rcu);
6715 return;
6716unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006717 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006718}
6719EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
6720
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006721/**
6722 * devlink_resource_register - devlink resource register
6723 *
6724 * @devlink: devlink
6725 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006726 * @resource_size: resource's size
6727 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006728 * @parent_resource_id: resource's parent id
6729 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006730 */
6731int devlink_resource_register(struct devlink *devlink,
6732 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006733 u64 resource_size,
6734 u64 resource_id,
6735 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02006736 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006737{
6738 struct devlink_resource *resource;
6739 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07006740 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006741 int err = 0;
6742
David Ahern14530742018-03-20 19:31:14 -07006743 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
6744
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006745 mutex_lock(&devlink->lock);
6746 resource = devlink_resource_find(devlink, NULL, resource_id);
6747 if (resource) {
6748 err = -EINVAL;
6749 goto out;
6750 }
6751
6752 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
6753 if (!resource) {
6754 err = -ENOMEM;
6755 goto out;
6756 }
6757
6758 if (top_hierarchy) {
6759 resource_list = &devlink->resource_list;
6760 } else {
6761 struct devlink_resource *parent_resource;
6762
6763 parent_resource = devlink_resource_find(devlink, NULL,
6764 parent_resource_id);
6765 if (parent_resource) {
6766 resource_list = &parent_resource->resource_list;
6767 resource->parent = parent_resource;
6768 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00006769 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006770 err = -EINVAL;
6771 goto out;
6772 }
6773 }
6774
6775 resource->name = resource_name;
6776 resource->size = resource_size;
6777 resource->size_new = resource_size;
6778 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006779 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01006780 memcpy(&resource->size_params, size_params,
6781 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006782 INIT_LIST_HEAD(&resource->resource_list);
6783 list_add_tail(&resource->list, resource_list);
6784out:
6785 mutex_unlock(&devlink->lock);
6786 return err;
6787}
6788EXPORT_SYMBOL_GPL(devlink_resource_register);
6789
6790/**
6791 * devlink_resources_unregister - free all resources
6792 *
6793 * @devlink: devlink
6794 * @resource: resource
6795 */
6796void devlink_resources_unregister(struct devlink *devlink,
6797 struct devlink_resource *resource)
6798{
6799 struct devlink_resource *tmp, *child_resource;
6800 struct list_head *resource_list;
6801
6802 if (resource)
6803 resource_list = &resource->resource_list;
6804 else
6805 resource_list = &devlink->resource_list;
6806
6807 if (!resource)
6808 mutex_lock(&devlink->lock);
6809
6810 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
6811 devlink_resources_unregister(devlink, child_resource);
6812 list_del(&child_resource->list);
6813 kfree(child_resource);
6814 }
6815
6816 if (!resource)
6817 mutex_unlock(&devlink->lock);
6818}
6819EXPORT_SYMBOL_GPL(devlink_resources_unregister);
6820
6821/**
6822 * devlink_resource_size_get - get and update size
6823 *
6824 * @devlink: devlink
6825 * @resource_id: the requested resource id
6826 * @p_resource_size: ptr to update
6827 */
6828int devlink_resource_size_get(struct devlink *devlink,
6829 u64 resource_id,
6830 u64 *p_resource_size)
6831{
6832 struct devlink_resource *resource;
6833 int err = 0;
6834
6835 mutex_lock(&devlink->lock);
6836 resource = devlink_resource_find(devlink, NULL, resource_id);
6837 if (!resource) {
6838 err = -EINVAL;
6839 goto out;
6840 }
6841 *p_resource_size = resource->size_new;
6842 resource->size = resource->size_new;
6843out:
6844 mutex_unlock(&devlink->lock);
6845 return err;
6846}
6847EXPORT_SYMBOL_GPL(devlink_resource_size_get);
6848
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01006849/**
6850 * devlink_dpipe_table_resource_set - set the resource id
6851 *
6852 * @devlink: devlink
6853 * @table_name: table name
6854 * @resource_id: resource id
6855 * @resource_units: number of resource's units consumed per table's entry
6856 */
6857int devlink_dpipe_table_resource_set(struct devlink *devlink,
6858 const char *table_name, u64 resource_id,
6859 u64 resource_units)
6860{
6861 struct devlink_dpipe_table *table;
6862 int err = 0;
6863
6864 mutex_lock(&devlink->lock);
6865 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6866 table_name);
6867 if (!table) {
6868 err = -EINVAL;
6869 goto out;
6870 }
6871 table->resource_id = resource_id;
6872 table->resource_units = resource_units;
6873 table->resource_valid = true;
6874out:
6875 mutex_unlock(&devlink->lock);
6876 return err;
6877}
6878EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
6879
Jiri Pirkofc56be42018-04-05 22:13:21 +02006880/**
6881 * devlink_resource_occ_get_register - register occupancy getter
6882 *
6883 * @devlink: devlink
6884 * @resource_id: resource id
6885 * @occ_get: occupancy getter callback
6886 * @occ_get_priv: occupancy getter callback priv
6887 */
6888void devlink_resource_occ_get_register(struct devlink *devlink,
6889 u64 resource_id,
6890 devlink_resource_occ_get_t *occ_get,
6891 void *occ_get_priv)
6892{
6893 struct devlink_resource *resource;
6894
6895 mutex_lock(&devlink->lock);
6896 resource = devlink_resource_find(devlink, NULL, resource_id);
6897 if (WARN_ON(!resource))
6898 goto out;
6899 WARN_ON(resource->occ_get);
6900
6901 resource->occ_get = occ_get;
6902 resource->occ_get_priv = occ_get_priv;
6903out:
6904 mutex_unlock(&devlink->lock);
6905}
6906EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
6907
6908/**
6909 * devlink_resource_occ_get_unregister - unregister occupancy getter
6910 *
6911 * @devlink: devlink
6912 * @resource_id: resource id
6913 */
6914void devlink_resource_occ_get_unregister(struct devlink *devlink,
6915 u64 resource_id)
6916{
6917 struct devlink_resource *resource;
6918
6919 mutex_lock(&devlink->lock);
6920 resource = devlink_resource_find(devlink, NULL, resource_id);
6921 if (WARN_ON(!resource))
6922 goto out;
6923 WARN_ON(!resource->occ_get);
6924
6925 resource->occ_get = NULL;
6926 resource->occ_get_priv = NULL;
6927out:
6928 mutex_unlock(&devlink->lock);
6929}
6930EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
6931
Vasundhara Volam39e61602019-01-28 18:00:20 +05306932static int devlink_param_verify(const struct devlink_param *param)
6933{
6934 if (!param || !param->name || !param->supported_cmodes)
6935 return -EINVAL;
6936 if (param->generic)
6937 return devlink_param_generic_verify(param);
6938 else
6939 return devlink_param_driver_verify(param);
6940}
6941
6942static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306943 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306944 struct list_head *param_list,
6945 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306946 size_t params_count,
6947 enum devlink_command reg_cmd,
6948 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05306949{
6950 const struct devlink_param *param = params;
6951 int i;
6952 int err;
6953
6954 mutex_lock(&devlink->lock);
6955 for (i = 0; i < params_count; i++, param++) {
6956 err = devlink_param_verify(param);
6957 if (err)
6958 goto rollback;
6959
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306960 err = devlink_param_register_one(devlink, port_index,
6961 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306962 if (err)
6963 goto rollback;
6964 }
6965
6966 mutex_unlock(&devlink->lock);
6967 return 0;
6968
6969rollback:
6970 if (!i)
6971 goto unlock;
6972 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306973 devlink_param_unregister_one(devlink, port_index, param_list,
6974 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306975unlock:
6976 mutex_unlock(&devlink->lock);
6977 return err;
6978}
6979
6980static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306981 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306982 struct list_head *param_list,
6983 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306984 size_t params_count,
6985 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05306986{
6987 const struct devlink_param *param = params;
6988 int i;
6989
6990 mutex_lock(&devlink->lock);
6991 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306992 devlink_param_unregister_one(devlink, 0, param_list, param,
6993 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306994 mutex_unlock(&devlink->lock);
6995}
6996
Moshe Shemesheabaef12018-07-04 14:30:28 +03006997/**
6998 * devlink_params_register - register configuration parameters
6999 *
7000 * @devlink: devlink
7001 * @params: configuration parameters array
7002 * @params_count: number of parameters provided
7003 *
7004 * Register the configuration parameters supported by the driver.
7005 */
7006int devlink_params_register(struct devlink *devlink,
7007 const struct devlink_param *params,
7008 size_t params_count)
7009{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307010 return __devlink_params_register(devlink, 0, &devlink->param_list,
7011 params, params_count,
7012 DEVLINK_CMD_PARAM_NEW,
7013 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007014}
7015EXPORT_SYMBOL_GPL(devlink_params_register);
7016
7017/**
7018 * devlink_params_unregister - unregister configuration parameters
7019 * @devlink: devlink
7020 * @params: configuration parameters to unregister
7021 * @params_count: number of parameters provided
7022 */
7023void devlink_params_unregister(struct devlink *devlink,
7024 const struct devlink_param *params,
7025 size_t params_count)
7026{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307027 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
7028 params, params_count,
7029 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007030}
7031EXPORT_SYMBOL_GPL(devlink_params_unregister);
7032
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007033/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00007034 * devlink_params_publish - publish configuration parameters
7035 *
7036 * @devlink: devlink
7037 *
7038 * Publish previously registered configuration parameters.
7039 */
7040void devlink_params_publish(struct devlink *devlink)
7041{
7042 struct devlink_param_item *param_item;
7043
7044 list_for_each_entry(param_item, &devlink->param_list, list) {
7045 if (param_item->published)
7046 continue;
7047 param_item->published = true;
7048 devlink_param_notify(devlink, 0, param_item,
7049 DEVLINK_CMD_PARAM_NEW);
7050 }
7051}
7052EXPORT_SYMBOL_GPL(devlink_params_publish);
7053
7054/**
7055 * devlink_params_unpublish - unpublish configuration parameters
7056 *
7057 * @devlink: devlink
7058 *
7059 * Unpublish previously registered configuration parameters.
7060 */
7061void devlink_params_unpublish(struct devlink *devlink)
7062{
7063 struct devlink_param_item *param_item;
7064
7065 list_for_each_entry(param_item, &devlink->param_list, list) {
7066 if (!param_item->published)
7067 continue;
7068 param_item->published = false;
7069 devlink_param_notify(devlink, 0, param_item,
7070 DEVLINK_CMD_PARAM_DEL);
7071 }
7072}
7073EXPORT_SYMBOL_GPL(devlink_params_unpublish);
7074
7075/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05307076 * devlink_port_params_register - register port configuration parameters
7077 *
7078 * @devlink_port: devlink port
7079 * @params: configuration parameters array
7080 * @params_count: number of parameters provided
7081 *
7082 * Register the configuration parameters supported by the port.
7083 */
7084int devlink_port_params_register(struct devlink_port *devlink_port,
7085 const struct devlink_param *params,
7086 size_t params_count)
7087{
7088 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307089 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307090 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307091 params_count,
7092 DEVLINK_CMD_PORT_PARAM_NEW,
7093 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307094}
7095EXPORT_SYMBOL_GPL(devlink_port_params_register);
7096
7097/**
7098 * devlink_port_params_unregister - unregister port configuration
7099 * parameters
7100 *
7101 * @devlink_port: devlink port
7102 * @params: configuration parameters array
7103 * @params_count: number of parameters provided
7104 */
7105void devlink_port_params_unregister(struct devlink_port *devlink_port,
7106 const struct devlink_param *params,
7107 size_t params_count)
7108{
7109 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307110 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307111 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307112 params, params_count,
7113 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307114}
7115EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
7116
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307117static int
7118__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
7119 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007120{
7121 struct devlink_param_item *param_item;
7122
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307123 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007124 if (!param_item)
7125 return -EINVAL;
7126
7127 if (!param_item->driverinit_value_valid ||
7128 !devlink_param_cmode_is_supported(param_item->param,
7129 DEVLINK_PARAM_CMODE_DRIVERINIT))
7130 return -EOPNOTSUPP;
7131
Moshe Shemesh12765342018-10-10 16:09:26 +03007132 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7133 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
7134 else
7135 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007136
7137 return 0;
7138}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307139
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307140static int
7141__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307142 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307143 struct list_head *param_list, u32 param_id,
7144 union devlink_param_value init_val,
7145 enum devlink_command cmd)
7146{
7147 struct devlink_param_item *param_item;
7148
7149 param_item = devlink_param_find_by_id(param_list, param_id);
7150 if (!param_item)
7151 return -EINVAL;
7152
7153 if (!devlink_param_cmode_is_supported(param_item->param,
7154 DEVLINK_PARAM_CMODE_DRIVERINIT))
7155 return -EOPNOTSUPP;
7156
7157 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7158 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
7159 else
7160 param_item->driverinit_value = init_val;
7161 param_item->driverinit_value_valid = true;
7162
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307163 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307164 return 0;
7165}
7166
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307167/**
7168 * devlink_param_driverinit_value_get - get configuration parameter
7169 * value for driver initializing
7170 *
7171 * @devlink: devlink
7172 * @param_id: parameter ID
7173 * @init_val: value of parameter in driverinit configuration mode
7174 *
7175 * This function should be used by the driver to get driverinit
7176 * configuration for initialization after reload command.
7177 */
7178int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
7179 union devlink_param_value *init_val)
7180{
Jiri Pirko97691062019-09-12 10:49:45 +02007181 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307182 return -EOPNOTSUPP;
7183
7184 return __devlink_param_driverinit_value_get(&devlink->param_list,
7185 param_id, init_val);
7186}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007187EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
7188
7189/**
7190 * devlink_param_driverinit_value_set - set value of configuration
7191 * parameter for driverinit
7192 * configuration mode
7193 *
7194 * @devlink: devlink
7195 * @param_id: parameter ID
7196 * @init_val: value of parameter to set for driverinit configuration mode
7197 *
7198 * This function should be used by the driver to set driverinit
7199 * configuration mode default value.
7200 */
7201int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
7202 union devlink_param_value init_val)
7203{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307204 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307205 &devlink->param_list,
7206 param_id, init_val,
7207 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007208}
7209EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
7210
Moshe Shemeshea601e12018-07-04 14:30:32 +03007211/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307212 * devlink_port_param_driverinit_value_get - get configuration parameter
7213 * value for driver initializing
7214 *
7215 * @devlink_port: devlink_port
7216 * @param_id: parameter ID
7217 * @init_val: value of parameter in driverinit configuration mode
7218 *
7219 * This function should be used by the driver to get driverinit
7220 * configuration for initialization after reload command.
7221 */
7222int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
7223 u32 param_id,
7224 union devlink_param_value *init_val)
7225{
7226 struct devlink *devlink = devlink_port->devlink;
7227
Jiri Pirko97691062019-09-12 10:49:45 +02007228 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307229 return -EOPNOTSUPP;
7230
7231 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
7232 param_id, init_val);
7233}
7234EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
7235
7236/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307237 * devlink_port_param_driverinit_value_set - set value of configuration
7238 * parameter for driverinit
7239 * configuration mode
7240 *
7241 * @devlink_port: devlink_port
7242 * @param_id: parameter ID
7243 * @init_val: value of parameter to set for driverinit configuration mode
7244 *
7245 * This function should be used by the driver to set driverinit
7246 * configuration mode default value.
7247 */
7248int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
7249 u32 param_id,
7250 union devlink_param_value init_val)
7251{
7252 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307253 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307254 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307255 param_id, init_val,
7256 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307257}
7258EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
7259
7260/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03007261 * devlink_param_value_changed - notify devlink on a parameter's value
7262 * change. Should be called by the driver
7263 * right after the change.
7264 *
7265 * @devlink: devlink
7266 * @param_id: parameter ID
7267 *
7268 * This function should be used by the driver to notify devlink on value
7269 * change, excluding driverinit configuration mode.
7270 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03007271 */
7272void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
7273{
7274 struct devlink_param_item *param_item;
7275
7276 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
7277 WARN_ON(!param_item);
7278
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307279 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03007280}
7281EXPORT_SYMBOL_GPL(devlink_param_value_changed);
7282
Alex Veskerb16ebe92018-07-12 15:13:08 +03007283/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307284 * devlink_port_param_value_changed - notify devlink on a parameter's value
7285 * change. Should be called by the driver
7286 * right after the change.
7287 *
7288 * @devlink_port: devlink_port
7289 * @param_id: parameter ID
7290 *
7291 * This function should be used by the driver to notify devlink on value
7292 * change, excluding driverinit configuration mode.
7293 * For driverinit configuration mode driver should use the function
7294 * devlink_port_param_driverinit_value_set() instead.
7295 */
7296void devlink_port_param_value_changed(struct devlink_port *devlink_port,
7297 u32 param_id)
7298{
7299 struct devlink_param_item *param_item;
7300
7301 param_item = devlink_param_find_by_id(&devlink_port->param_list,
7302 param_id);
7303 WARN_ON(!param_item);
7304
7305 devlink_param_notify(devlink_port->devlink, devlink_port->index,
7306 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
7307}
7308EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
7309
7310/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03007311 * devlink_param_value_str_fill - Safely fill-up the string preventing
7312 * from overflow of the preallocated buffer
7313 *
7314 * @dst_val: destination devlink_param_value
7315 * @src: source buffer
7316 */
7317void devlink_param_value_str_fill(union devlink_param_value *dst_val,
7318 const char *src)
7319{
7320 size_t len;
7321
7322 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
7323 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
7324}
7325EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
7326
7327/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03007328 * devlink_region_create - create a new address region
7329 *
7330 * @devlink: devlink
7331 * @region_name: region name
7332 * @region_max_snapshots: Maximum supported number of snapshots for region
7333 * @region_size: size of region
7334 */
7335struct devlink_region *devlink_region_create(struct devlink *devlink,
7336 const char *region_name,
7337 u32 region_max_snapshots,
7338 u64 region_size)
7339{
7340 struct devlink_region *region;
7341 int err = 0;
7342
7343 mutex_lock(&devlink->lock);
7344
7345 if (devlink_region_get_by_name(devlink, region_name)) {
7346 err = -EEXIST;
7347 goto unlock;
7348 }
7349
7350 region = kzalloc(sizeof(*region), GFP_KERNEL);
7351 if (!region) {
7352 err = -ENOMEM;
7353 goto unlock;
7354 }
7355
7356 region->devlink = devlink;
7357 region->max_snapshots = region_max_snapshots;
7358 region->name = region_name;
7359 region->size = region_size;
7360 INIT_LIST_HEAD(&region->snapshot_list);
7361 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03007362 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007363
7364 mutex_unlock(&devlink->lock);
7365 return region;
7366
7367unlock:
7368 mutex_unlock(&devlink->lock);
7369 return ERR_PTR(err);
7370}
7371EXPORT_SYMBOL_GPL(devlink_region_create);
7372
7373/**
7374 * devlink_region_destroy - destroy address region
7375 *
7376 * @region: devlink region to destroy
7377 */
7378void devlink_region_destroy(struct devlink_region *region)
7379{
7380 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03007381 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03007382
7383 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03007384
7385 /* Free all snapshots of region */
7386 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
Jiri Pirko92b49822019-08-12 14:28:31 +02007387 devlink_region_snapshot_del(region, snapshot);
Alex Veskerd7e52722018-07-12 15:13:10 +03007388
Alex Veskerb16ebe92018-07-12 15:13:08 +03007389 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03007390
7391 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007392 mutex_unlock(&devlink->lock);
7393 kfree(region);
7394}
7395EXPORT_SYMBOL_GPL(devlink_region_destroy);
7396
Alex Veskerccadfa42018-07-12 15:13:09 +03007397/**
7398 * devlink_region_shapshot_id_get - get snapshot ID
7399 *
7400 * This callback should be called when adding a new snapshot,
7401 * Driver should use the same id for multiple snapshots taken
7402 * on multiple regions at the same time/by the same trigger.
7403 *
7404 * @devlink: devlink
7405 */
7406u32 devlink_region_shapshot_id_get(struct devlink *devlink)
7407{
7408 u32 id;
7409
7410 mutex_lock(&devlink->lock);
7411 id = ++devlink->snapshot_id;
7412 mutex_unlock(&devlink->lock);
7413
7414 return id;
7415}
7416EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
7417
Alex Veskerd7e52722018-07-12 15:13:10 +03007418/**
7419 * devlink_region_snapshot_create - create a new snapshot
7420 * This will add a new snapshot of a region. The snapshot
7421 * will be stored on the region struct and can be accessed
7422 * from devlink. This is useful for future analyses of snapshots.
7423 * Multiple snapshots can be created on a region.
7424 * The @snapshot_id should be obtained using the getter function.
7425 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007426 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03007427 * @data: snapshot data
7428 * @snapshot_id: snapshot id to be created
7429 * @data_destructor: pointer to destructor function to free data
7430 */
Jiri Pirko3a5e5232019-08-09 15:27:15 +02007431int devlink_region_snapshot_create(struct devlink_region *region,
Alex Veskerd7e52722018-07-12 15:13:10 +03007432 u8 *data, u32 snapshot_id,
7433 devlink_snapshot_data_dest_t *data_destructor)
7434{
7435 struct devlink *devlink = region->devlink;
7436 struct devlink_snapshot *snapshot;
7437 int err;
7438
7439 mutex_lock(&devlink->lock);
7440
7441 /* check if region can hold one more snapshot */
7442 if (region->cur_snapshots == region->max_snapshots) {
7443 err = -ENOMEM;
7444 goto unlock;
7445 }
7446
7447 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
7448 err = -EEXIST;
7449 goto unlock;
7450 }
7451
7452 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
7453 if (!snapshot) {
7454 err = -ENOMEM;
7455 goto unlock;
7456 }
7457
7458 snapshot->id = snapshot_id;
7459 snapshot->region = region;
7460 snapshot->data = data;
Alex Veskerd7e52722018-07-12 15:13:10 +03007461 snapshot->data_destructor = data_destructor;
7462
7463 list_add_tail(&snapshot->list, &region->snapshot_list);
7464
7465 region->cur_snapshots++;
7466
Alex Vesker866319b2018-07-12 15:13:13 +03007467 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03007468 mutex_unlock(&devlink->lock);
7469 return 0;
7470
7471unlock:
7472 mutex_unlock(&devlink->lock);
7473 return err;
7474}
7475EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
7476
Ido Schimmel0f420b62019-08-17 16:28:17 +03007477#define DEVLINK_TRAP(_id, _type) \
7478 { \
7479 .type = DEVLINK_TRAP_TYPE_##_type, \
7480 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
7481 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
7482 }
7483
7484static const struct devlink_trap devlink_trap_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007485 DEVLINK_TRAP(SMAC_MC, DROP),
7486 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
7487 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
7488 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
7489 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
7490 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
7491 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
7492 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
7493 DEVLINK_TRAP(TAIL_DROP, DROP),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007494};
7495
7496#define DEVLINK_TRAP_GROUP(_id) \
7497 { \
7498 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
7499 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
7500 }
7501
7502static const struct devlink_trap_group devlink_trap_group_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007503 DEVLINK_TRAP_GROUP(L2_DROPS),
7504 DEVLINK_TRAP_GROUP(L3_DROPS),
7505 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007506};
7507
7508static int devlink_trap_generic_verify(const struct devlink_trap *trap)
7509{
7510 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
7511 return -EINVAL;
7512
7513 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
7514 return -EINVAL;
7515
7516 if (trap->type != devlink_trap_generic[trap->id].type)
7517 return -EINVAL;
7518
7519 return 0;
7520}
7521
7522static int devlink_trap_driver_verify(const struct devlink_trap *trap)
7523{
7524 int i;
7525
7526 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
7527 return -EINVAL;
7528
7529 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
7530 if (!strcmp(trap->name, devlink_trap_generic[i].name))
7531 return -EEXIST;
7532 }
7533
7534 return 0;
7535}
7536
7537static int devlink_trap_verify(const struct devlink_trap *trap)
7538{
7539 if (!trap || !trap->name || !trap->group.name)
7540 return -EINVAL;
7541
7542 if (trap->generic)
7543 return devlink_trap_generic_verify(trap);
7544 else
7545 return devlink_trap_driver_verify(trap);
7546}
7547
7548static int
7549devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
7550{
7551 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7552 return -EINVAL;
7553
7554 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
7555 return -EINVAL;
7556
7557 return 0;
7558}
7559
7560static int
7561devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
7562{
7563 int i;
7564
7565 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7566 return -EINVAL;
7567
7568 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
7569 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
7570 return -EEXIST;
7571 }
7572
7573 return 0;
7574}
7575
7576static int devlink_trap_group_verify(const struct devlink_trap_group *group)
7577{
7578 if (group->generic)
7579 return devlink_trap_group_generic_verify(group);
7580 else
7581 return devlink_trap_group_driver_verify(group);
7582}
7583
7584static void
7585devlink_trap_group_notify(struct devlink *devlink,
7586 const struct devlink_trap_group_item *group_item,
7587 enum devlink_command cmd)
7588{
7589 struct sk_buff *msg;
7590 int err;
7591
7592 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
7593 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
7594
7595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7596 if (!msg)
7597 return;
7598
7599 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
7600 0);
7601 if (err) {
7602 nlmsg_free(msg);
7603 return;
7604 }
7605
7606 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7607 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7608}
7609
7610static struct devlink_trap_group_item *
7611devlink_trap_group_item_create(struct devlink *devlink,
7612 const struct devlink_trap_group *group)
7613{
7614 struct devlink_trap_group_item *group_item;
7615 int err;
7616
7617 err = devlink_trap_group_verify(group);
7618 if (err)
7619 return ERR_PTR(err);
7620
7621 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
7622 if (!group_item)
7623 return ERR_PTR(-ENOMEM);
7624
7625 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7626 if (!group_item->stats) {
7627 err = -ENOMEM;
7628 goto err_stats_alloc;
7629 }
7630
7631 group_item->group = group;
7632 refcount_set(&group_item->refcount, 1);
7633
7634 if (devlink->ops->trap_group_init) {
7635 err = devlink->ops->trap_group_init(devlink, group);
7636 if (err)
7637 goto err_group_init;
7638 }
7639
7640 list_add_tail(&group_item->list, &devlink->trap_group_list);
7641 devlink_trap_group_notify(devlink, group_item,
7642 DEVLINK_CMD_TRAP_GROUP_NEW);
7643
7644 return group_item;
7645
7646err_group_init:
7647 free_percpu(group_item->stats);
7648err_stats_alloc:
7649 kfree(group_item);
7650 return ERR_PTR(err);
7651}
7652
7653static void
7654devlink_trap_group_item_destroy(struct devlink *devlink,
7655 struct devlink_trap_group_item *group_item)
7656{
7657 devlink_trap_group_notify(devlink, group_item,
7658 DEVLINK_CMD_TRAP_GROUP_DEL);
7659 list_del(&group_item->list);
7660 free_percpu(group_item->stats);
7661 kfree(group_item);
7662}
7663
7664static struct devlink_trap_group_item *
7665devlink_trap_group_item_get(struct devlink *devlink,
7666 const struct devlink_trap_group *group)
7667{
7668 struct devlink_trap_group_item *group_item;
7669
7670 group_item = devlink_trap_group_item_lookup(devlink, group->name);
7671 if (group_item) {
7672 refcount_inc(&group_item->refcount);
7673 return group_item;
7674 }
7675
7676 return devlink_trap_group_item_create(devlink, group);
7677}
7678
7679static void
7680devlink_trap_group_item_put(struct devlink *devlink,
7681 struct devlink_trap_group_item *group_item)
7682{
7683 if (!refcount_dec_and_test(&group_item->refcount))
7684 return;
7685
7686 devlink_trap_group_item_destroy(devlink, group_item);
7687}
7688
7689static int
7690devlink_trap_item_group_link(struct devlink *devlink,
7691 struct devlink_trap_item *trap_item)
7692{
7693 struct devlink_trap_group_item *group_item;
7694
7695 group_item = devlink_trap_group_item_get(devlink,
7696 &trap_item->trap->group);
7697 if (IS_ERR(group_item))
7698 return PTR_ERR(group_item);
7699
7700 trap_item->group_item = group_item;
7701
7702 return 0;
7703}
7704
7705static void
7706devlink_trap_item_group_unlink(struct devlink *devlink,
7707 struct devlink_trap_item *trap_item)
7708{
7709 devlink_trap_group_item_put(devlink, trap_item->group_item);
7710}
7711
7712static void devlink_trap_notify(struct devlink *devlink,
7713 const struct devlink_trap_item *trap_item,
7714 enum devlink_command cmd)
7715{
7716 struct sk_buff *msg;
7717 int err;
7718
7719 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
7720 cmd != DEVLINK_CMD_TRAP_DEL);
7721
7722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7723 if (!msg)
7724 return;
7725
7726 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
7727 if (err) {
7728 nlmsg_free(msg);
7729 return;
7730 }
7731
7732 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7733 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7734}
7735
7736static int
7737devlink_trap_register(struct devlink *devlink,
7738 const struct devlink_trap *trap, void *priv)
7739{
7740 struct devlink_trap_item *trap_item;
7741 int err;
7742
7743 if (devlink_trap_item_lookup(devlink, trap->name))
7744 return -EEXIST;
7745
7746 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
7747 if (!trap_item)
7748 return -ENOMEM;
7749
7750 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7751 if (!trap_item->stats) {
7752 err = -ENOMEM;
7753 goto err_stats_alloc;
7754 }
7755
7756 trap_item->trap = trap;
7757 trap_item->action = trap->init_action;
7758 trap_item->priv = priv;
7759
7760 err = devlink_trap_item_group_link(devlink, trap_item);
7761 if (err)
7762 goto err_group_link;
7763
7764 err = devlink->ops->trap_init(devlink, trap, trap_item);
7765 if (err)
7766 goto err_trap_init;
7767
7768 list_add_tail(&trap_item->list, &devlink->trap_list);
7769 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
7770
7771 return 0;
7772
7773err_trap_init:
7774 devlink_trap_item_group_unlink(devlink, trap_item);
7775err_group_link:
7776 free_percpu(trap_item->stats);
7777err_stats_alloc:
7778 kfree(trap_item);
7779 return err;
7780}
7781
7782static void devlink_trap_unregister(struct devlink *devlink,
7783 const struct devlink_trap *trap)
7784{
7785 struct devlink_trap_item *trap_item;
7786
7787 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7788 if (WARN_ON_ONCE(!trap_item))
7789 return;
7790
7791 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
7792 list_del(&trap_item->list);
7793 if (devlink->ops->trap_fini)
7794 devlink->ops->trap_fini(devlink, trap, trap_item);
7795 devlink_trap_item_group_unlink(devlink, trap_item);
7796 free_percpu(trap_item->stats);
7797 kfree(trap_item);
7798}
7799
7800static void devlink_trap_disable(struct devlink *devlink,
7801 const struct devlink_trap *trap)
7802{
7803 struct devlink_trap_item *trap_item;
7804
7805 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7806 if (WARN_ON_ONCE(!trap_item))
7807 return;
7808
7809 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP);
7810 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
7811}
7812
7813/**
7814 * devlink_traps_register - Register packet traps with devlink.
7815 * @devlink: devlink.
7816 * @traps: Packet traps.
7817 * @traps_count: Count of provided packet traps.
7818 * @priv: Driver private information.
7819 *
7820 * Return: Non-zero value on failure.
7821 */
7822int devlink_traps_register(struct devlink *devlink,
7823 const struct devlink_trap *traps,
7824 size_t traps_count, void *priv)
7825{
7826 int i, err;
7827
7828 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
7829 return -EINVAL;
7830
7831 mutex_lock(&devlink->lock);
7832 for (i = 0; i < traps_count; i++) {
7833 const struct devlink_trap *trap = &traps[i];
7834
7835 err = devlink_trap_verify(trap);
7836 if (err)
7837 goto err_trap_verify;
7838
7839 err = devlink_trap_register(devlink, trap, priv);
7840 if (err)
7841 goto err_trap_register;
7842 }
7843 mutex_unlock(&devlink->lock);
7844
7845 return 0;
7846
7847err_trap_register:
7848err_trap_verify:
7849 for (i--; i >= 0; i--)
7850 devlink_trap_unregister(devlink, &traps[i]);
7851 mutex_unlock(&devlink->lock);
7852 return err;
7853}
7854EXPORT_SYMBOL_GPL(devlink_traps_register);
7855
7856/**
7857 * devlink_traps_unregister - Unregister packet traps from devlink.
7858 * @devlink: devlink.
7859 * @traps: Packet traps.
7860 * @traps_count: Count of provided packet traps.
7861 */
7862void devlink_traps_unregister(struct devlink *devlink,
7863 const struct devlink_trap *traps,
7864 size_t traps_count)
7865{
7866 int i;
7867
7868 mutex_lock(&devlink->lock);
7869 /* Make sure we do not have any packets in-flight while unregistering
7870 * traps by disabling all of them and waiting for a grace period.
7871 */
7872 for (i = traps_count - 1; i >= 0; i--)
7873 devlink_trap_disable(devlink, &traps[i]);
7874 synchronize_rcu();
7875 for (i = traps_count - 1; i >= 0; i--)
7876 devlink_trap_unregister(devlink, &traps[i]);
7877 mutex_unlock(&devlink->lock);
7878}
7879EXPORT_SYMBOL_GPL(devlink_traps_unregister);
7880
7881static void
7882devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
7883 size_t skb_len)
7884{
7885 struct devlink_stats *stats;
7886
7887 stats = this_cpu_ptr(trap_stats);
7888 u64_stats_update_begin(&stats->syncp);
7889 stats->rx_bytes += skb_len;
7890 stats->rx_packets++;
7891 u64_stats_update_end(&stats->syncp);
7892}
7893
7894static void
7895devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata,
7896 const struct devlink_trap_item *trap_item,
7897 struct devlink_port *in_devlink_port)
7898{
7899 struct devlink_trap_group_item *group_item = trap_item->group_item;
7900
7901 hw_metadata->trap_group_name = group_item->group->name;
7902 hw_metadata->trap_name = trap_item->trap->name;
7903
7904 spin_lock(&in_devlink_port->type_lock);
7905 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
7906 hw_metadata->input_dev = in_devlink_port->type_dev;
7907 spin_unlock(&in_devlink_port->type_lock);
7908}
7909
7910/**
7911 * devlink_trap_report - Report trapped packet to drop monitor.
7912 * @devlink: devlink.
7913 * @skb: Trapped packet.
7914 * @trap_ctx: Trap context.
7915 * @in_devlink_port: Input devlink port.
7916 */
7917void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
7918 void *trap_ctx, struct devlink_port *in_devlink_port)
7919{
7920 struct devlink_trap_item *trap_item = trap_ctx;
7921 struct net_dm_hw_metadata hw_metadata = {};
7922
7923 devlink_trap_stats_update(trap_item->stats, skb->len);
7924 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
7925
7926 devlink_trap_report_metadata_fill(&hw_metadata, trap_item,
7927 in_devlink_port);
7928 net_dm_hw_report(skb, &hw_metadata);
7929}
7930EXPORT_SYMBOL_GPL(devlink_trap_report);
7931
7932/**
7933 * devlink_trap_ctx_priv - Trap context to driver private information.
7934 * @trap_ctx: Trap context.
7935 *
7936 * Return: Driver private information passed during registration.
7937 */
7938void *devlink_trap_ctx_priv(void *trap_ctx)
7939{
7940 struct devlink_trap_item *trap_item = trap_ctx;
7941
7942 return trap_item->priv;
7943}
7944EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
7945
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08007946static void __devlink_compat_running_version(struct devlink *devlink,
7947 char *buf, size_t len)
7948{
7949 const struct nlattr *nlattr;
7950 struct devlink_info_req req;
7951 struct sk_buff *msg;
7952 int rem, err;
7953
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08007954 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7955 if (!msg)
7956 return;
7957
7958 req.msg = msg;
7959 err = devlink->ops->info_get(devlink, &req, NULL);
7960 if (err)
7961 goto free_msg;
7962
7963 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
7964 const struct nlattr *kv;
7965 int rem_kv;
7966
7967 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
7968 continue;
7969
7970 nla_for_each_nested(kv, nlattr, rem_kv) {
7971 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
7972 continue;
7973
7974 strlcat(buf, nla_data(kv), len);
7975 strlcat(buf, " ", len);
7976 }
7977 }
7978free_msg:
7979 nlmsg_free(msg);
7980}
7981
7982void devlink_compat_running_version(struct net_device *dev,
7983 char *buf, size_t len)
7984{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08007985 struct devlink *devlink;
7986
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08007987 dev_hold(dev);
7988 rtnl_unlock();
7989
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08007990 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08007991 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01007992 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08007993
7994 mutex_lock(&devlink->lock);
7995 __devlink_compat_running_version(devlink, buf, len);
7996 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08007997
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01007998out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08007999 rtnl_lock();
8000 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008001}
8002
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008003int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
8004{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008005 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008006 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008007
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008008 dev_hold(dev);
8009 rtnl_unlock();
8010
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008011 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008012 if (!devlink || !devlink->ops->flash_update) {
8013 ret = -EOPNOTSUPP;
8014 goto out;
8015 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008016
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008017 mutex_lock(&devlink->lock);
8018 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
8019 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008020
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008021out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008022 rtnl_lock();
8023 dev_put(dev);
8024
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008025 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008026}
8027
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008028int devlink_compat_phys_port_name_get(struct net_device *dev,
8029 char *name, size_t len)
8030{
8031 struct devlink_port *devlink_port;
8032
8033 /* RTNL mutex is held here which ensures that devlink_port
8034 * instance cannot disappear in the middle. No need to take
8035 * any devlink lock as only permanent values are accessed.
8036 */
8037 ASSERT_RTNL();
8038
8039 devlink_port = netdev_to_devlink_port(dev);
8040 if (!devlink_port)
8041 return -EOPNOTSUPP;
8042
8043 return __devlink_port_phys_port_name_get(devlink_port, name, len);
8044}
8045
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008046int devlink_compat_switch_id_get(struct net_device *dev,
8047 struct netdev_phys_item_id *ppid)
8048{
8049 struct devlink_port *devlink_port;
8050
Vlad Buslov043b8412019-08-12 20:02:02 +03008051 /* Caller must hold RTNL mutex or reference to dev, which ensures that
8052 * devlink_port instance cannot disappear in the middle. No need to take
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008053 * any devlink lock as only permanent values are accessed.
8054 */
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008055 devlink_port = netdev_to_devlink_port(dev);
8056 if (!devlink_port || !devlink_port->attrs.switch_port)
8057 return -EOPNOTSUPP;
8058
8059 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
8060
8061 return 0;
8062}
8063
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008064static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008065{
Johannes Berg489111e2016-10-24 14:40:03 +02008066 return genl_register_family(&devlink_nl_family);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008067}
8068
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008069subsys_initcall(devlink_init);