blob: c91ef1b5f7384a428789f28e2cffc172d60ff9e6 [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
Jiri Pirko8273fd82019-10-05 08:10:31 +0200104static void __devlink_net_set(struct devlink *devlink, struct net *net)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100105{
106 write_pnet(&devlink->_net, net);
107}
108
Jiri Pirko8273fd82019-10-05 08:10:31 +0200109void devlink_net_set(struct devlink *devlink, struct net *net)
110{
111 if (WARN_ON(devlink->registered))
112 return;
113 __devlink_net_set(devlink, net);
114}
115EXPORT_SYMBOL_GPL(devlink_net_set);
116
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100117static struct devlink *devlink_get_from_attrs(struct net *net,
118 struct nlattr **attrs)
119{
120 struct devlink *devlink;
121 char *busname;
122 char *devname;
123
124 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
125 return ERR_PTR(-EINVAL);
126
127 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
128 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
129
Parav Panditdac7c082019-02-12 14:24:08 -0600130 lockdep_assert_held(&devlink_mutex);
131
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100132 list_for_each_entry(devlink, &devlink_list, list) {
133 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
134 strcmp(dev_name(devlink->dev), devname) == 0 &&
135 net_eq(devlink_net(devlink), net))
136 return devlink;
137 }
138
139 return ERR_PTR(-ENODEV);
140}
141
142static struct devlink *devlink_get_from_info(struct genl_info *info)
143{
144 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
145}
146
147static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
Parav Panditc7282b52019-08-30 05:39:44 -0500148 unsigned int port_index)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100149{
150 struct devlink_port *devlink_port;
151
152 list_for_each_entry(devlink_port, &devlink->port_list, list) {
153 if (devlink_port->index == port_index)
154 return devlink_port;
155 }
156 return NULL;
157}
158
Parav Panditc7282b52019-08-30 05:39:44 -0500159static bool devlink_port_index_exists(struct devlink *devlink,
160 unsigned int port_index)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100161{
162 return devlink_port_get_by_index(devlink, port_index);
163}
164
165static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
166 struct nlattr **attrs)
167{
168 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
169 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
170 struct devlink_port *devlink_port;
171
172 devlink_port = devlink_port_get_by_index(devlink, port_index);
173 if (!devlink_port)
174 return ERR_PTR(-ENODEV);
175 return devlink_port;
176 }
177 return ERR_PTR(-EINVAL);
178}
179
180static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
181 struct genl_info *info)
182{
183 return devlink_port_get_from_attrs(devlink, info->attrs);
184}
185
Jiri Pirkobf797472016-04-14 18:19:13 +0200186struct devlink_sb {
187 struct list_head list;
188 unsigned int index;
189 u32 size;
190 u16 ingress_pools_count;
191 u16 egress_pools_count;
192 u16 ingress_tc_count;
193 u16 egress_tc_count;
194};
195
196static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
197{
198 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
199}
200
201static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
202 unsigned int sb_index)
203{
204 struct devlink_sb *devlink_sb;
205
206 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
207 if (devlink_sb->index == sb_index)
208 return devlink_sb;
209 }
210 return NULL;
211}
212
213static bool devlink_sb_index_exists(struct devlink *devlink,
214 unsigned int sb_index)
215{
216 return devlink_sb_get_by_index(devlink, sb_index);
217}
218
219static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
220 struct nlattr **attrs)
221{
222 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
223 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
224 struct devlink_sb *devlink_sb;
225
226 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
227 if (!devlink_sb)
228 return ERR_PTR(-ENODEV);
229 return devlink_sb;
230 }
231 return ERR_PTR(-EINVAL);
232}
233
234static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
235 struct genl_info *info)
236{
237 return devlink_sb_get_from_attrs(devlink, info->attrs);
238}
239
240static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
241 struct nlattr **attrs,
242 u16 *p_pool_index)
243{
244 u16 val;
245
246 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
247 return -EINVAL;
248
249 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
250 if (val >= devlink_sb_pool_count(devlink_sb))
251 return -EINVAL;
252 *p_pool_index = val;
253 return 0;
254}
255
256static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
257 struct genl_info *info,
258 u16 *p_pool_index)
259{
260 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
261 p_pool_index);
262}
263
264static int
265devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
266 enum devlink_sb_pool_type *p_pool_type)
267{
268 u8 val;
269
270 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
271 return -EINVAL;
272
273 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
274 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
275 val != DEVLINK_SB_POOL_TYPE_EGRESS)
276 return -EINVAL;
277 *p_pool_type = val;
278 return 0;
279}
280
281static int
282devlink_sb_pool_type_get_from_info(struct genl_info *info,
283 enum devlink_sb_pool_type *p_pool_type)
284{
285 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
286}
287
288static int
289devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
290 enum devlink_sb_threshold_type *p_th_type)
291{
292 u8 val;
293
294 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
295 return -EINVAL;
296
297 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
298 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
299 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
300 return -EINVAL;
301 *p_th_type = val;
302 return 0;
303}
304
305static int
306devlink_sb_th_type_get_from_info(struct genl_info *info,
307 enum devlink_sb_threshold_type *p_th_type)
308{
309 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
310}
311
312static int
313devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
314 struct nlattr **attrs,
315 enum devlink_sb_pool_type pool_type,
316 u16 *p_tc_index)
317{
318 u16 val;
319
320 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
321 return -EINVAL;
322
323 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
324 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
325 val >= devlink_sb->ingress_tc_count)
326 return -EINVAL;
327 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
328 val >= devlink_sb->egress_tc_count)
329 return -EINVAL;
330 *p_tc_index = val;
331 return 0;
332}
333
334static int
335devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
336 struct genl_info *info,
337 enum devlink_sb_pool_type pool_type,
338 u16 *p_tc_index)
339{
340 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
341 pool_type, p_tc_index);
342}
343
Alex Veskerb16ebe92018-07-12 15:13:08 +0300344struct devlink_region {
345 struct devlink *devlink;
346 struct list_head list;
Jacob Kellere8937682020-03-26 11:37:08 -0700347 const struct devlink_region_ops *ops;
Alex Veskerb16ebe92018-07-12 15:13:08 +0300348 struct list_head snapshot_list;
349 u32 max_snapshots;
350 u32 cur_snapshots;
351 u64 size;
352};
353
Alex Veskerd7e52722018-07-12 15:13:10 +0300354struct devlink_snapshot {
355 struct list_head list;
356 struct devlink_region *region;
Alex Veskerd7e52722018-07-12 15:13:10 +0300357 u8 *data;
358 u32 id;
359};
360
Alex Veskerb16ebe92018-07-12 15:13:08 +0300361static struct devlink_region *
362devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
363{
364 struct devlink_region *region;
365
366 list_for_each_entry(region, &devlink->region_list, list)
Jacob Kellere8937682020-03-26 11:37:08 -0700367 if (!strcmp(region->ops->name, region_name))
Alex Veskerb16ebe92018-07-12 15:13:08 +0300368 return region;
369
370 return NULL;
371}
372
Alex Veskerd7e52722018-07-12 15:13:10 +0300373static struct devlink_snapshot *
374devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
375{
376 struct devlink_snapshot *snapshot;
377
378 list_for_each_entry(snapshot, &region->snapshot_list, list)
379 if (snapshot->id == id)
380 return snapshot;
381
382 return NULL;
383}
384
Jiri Pirko1fc22572016-04-08 19:12:48 +0200385#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
386#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200387#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100388
389/* The per devlink instance lock is taken by default in the pre-doit
390 * operation, yet several commands do not require this. The global
391 * devlink lock is taken and protects from disruption by user-calls.
392 */
393#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100394
395static int devlink_nl_pre_doit(const struct genl_ops *ops,
396 struct sk_buff *skb, struct genl_info *info)
397{
398 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100399 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100400
401 mutex_lock(&devlink_mutex);
402 devlink = devlink_get_from_info(info);
403 if (IS_ERR(devlink)) {
404 mutex_unlock(&devlink_mutex);
405 return PTR_ERR(devlink);
406 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100407 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
408 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200409 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
410 info->user_ptr[0] = devlink;
411 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100412 struct devlink_port *devlink_port;
413
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100414 devlink_port = devlink_port_get_from_info(devlink, info);
415 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100416 err = PTR_ERR(devlink_port);
417 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100418 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200419 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100420 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200421 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
422 struct devlink_sb *devlink_sb;
423
424 devlink_sb = devlink_sb_get_from_info(devlink, info);
425 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100426 err = PTR_ERR(devlink_sb);
427 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200428 }
429 info->user_ptr[1] = devlink_sb;
430 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100431 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100432
433unlock:
434 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
435 mutex_unlock(&devlink->lock);
436 mutex_unlock(&devlink_mutex);
437 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100438}
439
440static void devlink_nl_post_doit(const struct genl_ops *ops,
441 struct sk_buff *skb, struct genl_info *info)
442{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100443 struct devlink *devlink;
444
Jiri Pirko070c63f2019-10-03 11:49:39 +0200445 /* When devlink changes netns, it would not be found
446 * by devlink_get_from_info(). So try if it is stored first.
447 */
448 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
449 devlink = info->user_ptr[0];
450 } else {
451 devlink = devlink_get_from_info(info);
452 WARN_ON(IS_ERR(devlink));
453 }
454 if (!IS_ERR(devlink) && ~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100455 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100456 mutex_unlock(&devlink_mutex);
457}
458
Johannes Berg489111e2016-10-24 14:40:03 +0200459static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100460
461enum devlink_multicast_groups {
462 DEVLINK_MCGRP_CONFIG,
463};
464
465static const struct genl_multicast_group devlink_nl_mcgrps[] = {
466 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
467};
468
469static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
470{
471 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
472 return -EMSGSIZE;
473 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
474 return -EMSGSIZE;
475 return 0;
476}
477
478static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
479 enum devlink_command cmd, u32 portid,
480 u32 seq, int flags)
481{
482 void *hdr;
483
484 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
485 if (!hdr)
486 return -EMSGSIZE;
487
488 if (devlink_nl_put_handle(msg, devlink))
489 goto nla_put_failure;
Jiri Pirko2670ac22019-09-12 10:49:46 +0200490 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
491 goto nla_put_failure;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100492
493 genlmsg_end(msg, hdr);
494 return 0;
495
496nla_put_failure:
497 genlmsg_cancel(msg, hdr);
498 return -EMSGSIZE;
499}
500
501static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
502{
503 struct sk_buff *msg;
504 int err;
505
506 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
507
508 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
509 if (!msg)
510 return;
511
512 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
513 if (err) {
514 nlmsg_free(msg);
515 return;
516 }
517
518 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
519 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
520}
521
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200522static int devlink_nl_port_attrs_put(struct sk_buff *msg,
523 struct devlink_port *devlink_port)
524{
525 struct devlink_port_attrs *attrs = &devlink_port->attrs;
526
527 if (!attrs->set)
528 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200529 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
530 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500531 switch (devlink_port->attrs.flavour) {
532 case DEVLINK_PORT_FLAVOUR_PCI_PF:
Parav Pandit98fd2d62019-07-08 23:17:37 -0500533 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
534 attrs->pci_pf.pf))
535 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500536 break;
537 case DEVLINK_PORT_FLAVOUR_PCI_VF:
Parav Pandite41b6bf2019-07-08 23:17:38 -0500538 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
539 attrs->pci_vf.pf) ||
540 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER,
541 attrs->pci_vf.vf))
542 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500543 break;
544 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
545 case DEVLINK_PORT_FLAVOUR_CPU:
546 case DEVLINK_PORT_FLAVOUR_DSA:
Parav Panditacf1ee42020-03-03 08:12:42 -0600547 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
Parav Pandit58b6be42019-08-30 05:39:45 -0500548 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
549 attrs->phys.port_number))
550 return -EMSGSIZE;
551 if (!attrs->split)
552 return 0;
553 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
554 attrs->phys.port_number))
555 return -EMSGSIZE;
556 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
557 attrs->phys.split_subport_number))
558 return -EMSGSIZE;
559 break;
560 default:
561 break;
Parav Pandit98fd2d62019-07-08 23:17:37 -0500562 }
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200563 return 0;
564}
565
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100566static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
567 struct devlink_port *devlink_port,
568 enum devlink_command cmd, u32 portid,
569 u32 seq, int flags)
570{
571 void *hdr;
572
573 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
574 if (!hdr)
575 return -EMSGSIZE;
576
577 if (devlink_nl_put_handle(msg, devlink))
578 goto nla_put_failure;
579 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
580 goto nla_put_failure;
Jiri Pirkob8f97552019-03-24 11:14:37 +0100581
Ido Schimmel0f420b62019-08-17 16:28:17 +0300582 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100583 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100584 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100585 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
586 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
587 devlink_port->desired_type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100588 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100589 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
590 struct net_device *netdev = devlink_port->type_dev;
591
592 if (netdev &&
593 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
594 netdev->ifindex) ||
595 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
596 netdev->name)))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100597 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100598 }
599 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
600 struct ib_device *ibdev = devlink_port->type_dev;
601
602 if (ibdev &&
603 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
604 ibdev->name))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100605 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100606 }
Ido Schimmel0f420b62019-08-17 16:28:17 +0300607 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200608 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100609 goto nla_put_failure;
610
611 genlmsg_end(msg, hdr);
612 return 0;
613
Jiri Pirkob8f97552019-03-24 11:14:37 +0100614nla_put_failure_type_locked:
Ido Schimmel0f420b62019-08-17 16:28:17 +0300615 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100616nla_put_failure:
617 genlmsg_cancel(msg, hdr);
618 return -EMSGSIZE;
619}
620
621static void devlink_port_notify(struct devlink_port *devlink_port,
622 enum devlink_command cmd)
623{
624 struct devlink *devlink = devlink_port->devlink;
625 struct sk_buff *msg;
626 int err;
627
628 if (!devlink_port->registered)
629 return;
630
631 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
632
633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
634 if (!msg)
635 return;
636
637 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
638 if (err) {
639 nlmsg_free(msg);
640 return;
641 }
642
643 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
644 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
645}
646
647static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
648{
649 struct devlink *devlink = info->user_ptr[0];
650 struct sk_buff *msg;
651 int err;
652
653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
654 if (!msg)
655 return -ENOMEM;
656
657 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
658 info->snd_portid, info->snd_seq, 0);
659 if (err) {
660 nlmsg_free(msg);
661 return err;
662 }
663
664 return genlmsg_reply(msg, info);
665}
666
667static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
668 struct netlink_callback *cb)
669{
670 struct devlink *devlink;
671 int start = cb->args[0];
672 int idx = 0;
673 int err;
674
675 mutex_lock(&devlink_mutex);
676 list_for_each_entry(devlink, &devlink_list, list) {
677 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
678 continue;
679 if (idx < start) {
680 idx++;
681 continue;
682 }
683 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
684 NETLINK_CB(cb->skb).portid,
685 cb->nlh->nlmsg_seq, NLM_F_MULTI);
686 if (err)
687 goto out;
688 idx++;
689 }
690out:
691 mutex_unlock(&devlink_mutex);
692
693 cb->args[0] = idx;
694 return msg->len;
695}
696
697static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
698 struct genl_info *info)
699{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200700 struct devlink_port *devlink_port = info->user_ptr[0];
701 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100702 struct sk_buff *msg;
703 int err;
704
705 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
706 if (!msg)
707 return -ENOMEM;
708
709 err = devlink_nl_port_fill(msg, devlink, devlink_port,
710 DEVLINK_CMD_PORT_NEW,
711 info->snd_portid, info->snd_seq, 0);
712 if (err) {
713 nlmsg_free(msg);
714 return err;
715 }
716
717 return genlmsg_reply(msg, info);
718}
719
720static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
721 struct netlink_callback *cb)
722{
723 struct devlink *devlink;
724 struct devlink_port *devlink_port;
725 int start = cb->args[0];
726 int idx = 0;
727 int err;
728
729 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100730 list_for_each_entry(devlink, &devlink_list, list) {
731 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
732 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100733 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100734 list_for_each_entry(devlink_port, &devlink->port_list, list) {
735 if (idx < start) {
736 idx++;
737 continue;
738 }
739 err = devlink_nl_port_fill(msg, devlink, devlink_port,
740 DEVLINK_CMD_NEW,
741 NETLINK_CB(cb->skb).portid,
742 cb->nlh->nlmsg_seq,
743 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100744 if (err) {
745 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100746 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100747 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100748 idx++;
749 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100750 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100751 }
752out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100753 mutex_unlock(&devlink_mutex);
754
755 cb->args[0] = idx;
756 return msg->len;
757}
758
759static int devlink_port_type_set(struct devlink *devlink,
760 struct devlink_port *devlink_port,
761 enum devlink_port_type port_type)
762
763{
764 int err;
765
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800766 if (devlink->ops->port_type_set) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100767 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
768 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200769 if (port_type == devlink_port->type)
770 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100771 err = devlink->ops->port_type_set(devlink_port, port_type);
772 if (err)
773 return err;
774 devlink_port->desired_type = port_type;
775 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
776 return 0;
777 }
778 return -EOPNOTSUPP;
779}
780
781static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
782 struct genl_info *info)
783{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200784 struct devlink_port *devlink_port = info->user_ptr[0];
785 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100786 int err;
787
788 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
789 enum devlink_port_type port_type;
790
791 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
792 err = devlink_port_type_set(devlink, devlink_port, port_type);
793 if (err)
794 return err;
795 }
796 return 0;
797}
798
David Ahernac0fc8a2018-06-05 08:14:09 -0700799static int devlink_port_split(struct devlink *devlink, u32 port_index,
800 u32 count, struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100801
802{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800803 if (devlink->ops->port_split)
David Ahernac0fc8a2018-06-05 08:14:09 -0700804 return devlink->ops->port_split(devlink, port_index, count,
805 extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100806 return -EOPNOTSUPP;
807}
808
809static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
810 struct genl_info *info)
811{
812 struct devlink *devlink = info->user_ptr[0];
813 u32 port_index;
814 u32 count;
815
816 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
817 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
818 return -EINVAL;
819
820 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
821 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700822 return devlink_port_split(devlink, port_index, count, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100823}
824
David Ahernac0fc8a2018-06-05 08:14:09 -0700825static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
826 struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100827
828{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800829 if (devlink->ops->port_unsplit)
David Ahernac0fc8a2018-06-05 08:14:09 -0700830 return devlink->ops->port_unsplit(devlink, port_index, extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100831 return -EOPNOTSUPP;
832}
833
834static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
835 struct genl_info *info)
836{
837 struct devlink *devlink = info->user_ptr[0];
838 u32 port_index;
839
840 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
841 return -EINVAL;
842
843 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700844 return devlink_port_unsplit(devlink, port_index, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100845}
846
Jiri Pirkobf797472016-04-14 18:19:13 +0200847static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
848 struct devlink_sb *devlink_sb,
849 enum devlink_command cmd, u32 portid,
850 u32 seq, int flags)
851{
852 void *hdr;
853
854 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
855 if (!hdr)
856 return -EMSGSIZE;
857
858 if (devlink_nl_put_handle(msg, devlink))
859 goto nla_put_failure;
860 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
861 goto nla_put_failure;
862 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
863 goto nla_put_failure;
864 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
865 devlink_sb->ingress_pools_count))
866 goto nla_put_failure;
867 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
868 devlink_sb->egress_pools_count))
869 goto nla_put_failure;
870 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
871 devlink_sb->ingress_tc_count))
872 goto nla_put_failure;
873 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
874 devlink_sb->egress_tc_count))
875 goto nla_put_failure;
876
877 genlmsg_end(msg, hdr);
878 return 0;
879
880nla_put_failure:
881 genlmsg_cancel(msg, hdr);
882 return -EMSGSIZE;
883}
884
885static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
886 struct genl_info *info)
887{
888 struct devlink *devlink = info->user_ptr[0];
889 struct devlink_sb *devlink_sb = info->user_ptr[1];
890 struct sk_buff *msg;
891 int err;
892
893 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
894 if (!msg)
895 return -ENOMEM;
896
897 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
898 DEVLINK_CMD_SB_NEW,
899 info->snd_portid, info->snd_seq, 0);
900 if (err) {
901 nlmsg_free(msg);
902 return err;
903 }
904
905 return genlmsg_reply(msg, info);
906}
907
908static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
909 struct netlink_callback *cb)
910{
911 struct devlink *devlink;
912 struct devlink_sb *devlink_sb;
913 int start = cb->args[0];
914 int idx = 0;
915 int err;
916
917 mutex_lock(&devlink_mutex);
918 list_for_each_entry(devlink, &devlink_list, list) {
919 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
920 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100921 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200922 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
923 if (idx < start) {
924 idx++;
925 continue;
926 }
927 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
928 DEVLINK_CMD_SB_NEW,
929 NETLINK_CB(cb->skb).portid,
930 cb->nlh->nlmsg_seq,
931 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100932 if (err) {
933 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200934 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100935 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200936 idx++;
937 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100938 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200939 }
940out:
941 mutex_unlock(&devlink_mutex);
942
943 cb->args[0] = idx;
944 return msg->len;
945}
946
947static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
948 struct devlink_sb *devlink_sb,
949 u16 pool_index, enum devlink_command cmd,
950 u32 portid, u32 seq, int flags)
951{
952 struct devlink_sb_pool_info pool_info;
953 void *hdr;
954 int err;
955
956 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
957 pool_index, &pool_info);
958 if (err)
959 return err;
960
961 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
962 if (!hdr)
963 return -EMSGSIZE;
964
965 if (devlink_nl_put_handle(msg, devlink))
966 goto nla_put_failure;
967 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
968 goto nla_put_failure;
969 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
970 goto nla_put_failure;
971 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
972 goto nla_put_failure;
973 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
974 goto nla_put_failure;
975 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
976 pool_info.threshold_type))
977 goto nla_put_failure;
Jakub Kicinskibff57312019-02-01 17:56:28 -0800978 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
979 pool_info.cell_size))
980 goto nla_put_failure;
Jiri Pirkobf797472016-04-14 18:19:13 +0200981
982 genlmsg_end(msg, hdr);
983 return 0;
984
985nla_put_failure:
986 genlmsg_cancel(msg, hdr);
987 return -EMSGSIZE;
988}
989
990static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
991 struct genl_info *info)
992{
993 struct devlink *devlink = info->user_ptr[0];
994 struct devlink_sb *devlink_sb = info->user_ptr[1];
995 struct sk_buff *msg;
996 u16 pool_index;
997 int err;
998
999 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1000 &pool_index);
1001 if (err)
1002 return err;
1003
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001004 if (!devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001005 return -EOPNOTSUPP;
1006
1007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1008 if (!msg)
1009 return -ENOMEM;
1010
1011 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
1012 DEVLINK_CMD_SB_POOL_NEW,
1013 info->snd_portid, info->snd_seq, 0);
1014 if (err) {
1015 nlmsg_free(msg);
1016 return err;
1017 }
1018
1019 return genlmsg_reply(msg, info);
1020}
1021
1022static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1023 struct devlink *devlink,
1024 struct devlink_sb *devlink_sb,
1025 u32 portid, u32 seq)
1026{
1027 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1028 u16 pool_index;
1029 int err;
1030
1031 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1032 if (*p_idx < start) {
1033 (*p_idx)++;
1034 continue;
1035 }
1036 err = devlink_nl_sb_pool_fill(msg, devlink,
1037 devlink_sb,
1038 pool_index,
1039 DEVLINK_CMD_SB_POOL_NEW,
1040 portid, seq, NLM_F_MULTI);
1041 if (err)
1042 return err;
1043 (*p_idx)++;
1044 }
1045 return 0;
1046}
1047
1048static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1049 struct netlink_callback *cb)
1050{
1051 struct devlink *devlink;
1052 struct devlink_sb *devlink_sb;
1053 int start = cb->args[0];
1054 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001055 int err = 0;
Jiri Pirkobf797472016-04-14 18:19:13 +02001056
1057 mutex_lock(&devlink_mutex);
1058 list_for_each_entry(devlink, &devlink_list, list) {
1059 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001060 !devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001061 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001062 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001063 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1064 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1065 devlink_sb,
1066 NETLINK_CB(cb->skb).portid,
1067 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001068 if (err && err != -EOPNOTSUPP) {
1069 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001070 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001071 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001072 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001073 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001074 }
1075out:
1076 mutex_unlock(&devlink_mutex);
1077
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001078 if (err != -EMSGSIZE)
1079 return err;
1080
Jiri Pirkobf797472016-04-14 18:19:13 +02001081 cb->args[0] = idx;
1082 return msg->len;
1083}
1084
1085static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1086 u16 pool_index, u32 size,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001087 enum devlink_sb_threshold_type threshold_type,
1088 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001089
1090{
1091 const struct devlink_ops *ops = devlink->ops;
1092
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001093 if (ops->sb_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001094 return ops->sb_pool_set(devlink, sb_index, pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001095 size, threshold_type, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001096 return -EOPNOTSUPP;
1097}
1098
1099static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1100 struct genl_info *info)
1101{
1102 struct devlink *devlink = info->user_ptr[0];
1103 struct devlink_sb *devlink_sb = info->user_ptr[1];
1104 enum devlink_sb_threshold_type threshold_type;
1105 u16 pool_index;
1106 u32 size;
1107 int err;
1108
1109 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1110 &pool_index);
1111 if (err)
1112 return err;
1113
1114 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1115 if (err)
1116 return err;
1117
1118 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1119 return -EINVAL;
1120
1121 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1122 return devlink_sb_pool_set(devlink, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001123 pool_index, size, threshold_type,
1124 info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001125}
1126
1127static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1128 struct devlink *devlink,
1129 struct devlink_port *devlink_port,
1130 struct devlink_sb *devlink_sb,
1131 u16 pool_index,
1132 enum devlink_command cmd,
1133 u32 portid, u32 seq, int flags)
1134{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001135 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001136 u32 threshold;
1137 void *hdr;
1138 int err;
1139
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001140 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1141 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001142 if (err)
1143 return err;
1144
1145 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1146 if (!hdr)
1147 return -EMSGSIZE;
1148
1149 if (devlink_nl_put_handle(msg, devlink))
1150 goto nla_put_failure;
1151 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1152 goto nla_put_failure;
1153 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1154 goto nla_put_failure;
1155 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1156 goto nla_put_failure;
1157 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1158 goto nla_put_failure;
1159
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001160 if (ops->sb_occ_port_pool_get) {
1161 u32 cur;
1162 u32 max;
1163
1164 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1165 pool_index, &cur, &max);
1166 if (err && err != -EOPNOTSUPP)
1167 return err;
1168 if (!err) {
1169 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1170 goto nla_put_failure;
1171 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1172 goto nla_put_failure;
1173 }
1174 }
1175
Jiri Pirkobf797472016-04-14 18:19:13 +02001176 genlmsg_end(msg, hdr);
1177 return 0;
1178
1179nla_put_failure:
1180 genlmsg_cancel(msg, hdr);
1181 return -EMSGSIZE;
1182}
1183
1184static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1185 struct genl_info *info)
1186{
1187 struct devlink_port *devlink_port = info->user_ptr[0];
1188 struct devlink *devlink = devlink_port->devlink;
1189 struct devlink_sb *devlink_sb = info->user_ptr[1];
1190 struct sk_buff *msg;
1191 u16 pool_index;
1192 int err;
1193
1194 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1195 &pool_index);
1196 if (err)
1197 return err;
1198
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001199 if (!devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001200 return -EOPNOTSUPP;
1201
1202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1203 if (!msg)
1204 return -ENOMEM;
1205
1206 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1207 devlink_sb, pool_index,
1208 DEVLINK_CMD_SB_PORT_POOL_NEW,
1209 info->snd_portid, info->snd_seq, 0);
1210 if (err) {
1211 nlmsg_free(msg);
1212 return err;
1213 }
1214
1215 return genlmsg_reply(msg, info);
1216}
1217
1218static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1219 struct devlink *devlink,
1220 struct devlink_sb *devlink_sb,
1221 u32 portid, u32 seq)
1222{
1223 struct devlink_port *devlink_port;
1224 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1225 u16 pool_index;
1226 int err;
1227
1228 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1229 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1230 if (*p_idx < start) {
1231 (*p_idx)++;
1232 continue;
1233 }
1234 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1235 devlink_port,
1236 devlink_sb,
1237 pool_index,
1238 DEVLINK_CMD_SB_PORT_POOL_NEW,
1239 portid, seq,
1240 NLM_F_MULTI);
1241 if (err)
1242 return err;
1243 (*p_idx)++;
1244 }
1245 }
1246 return 0;
1247}
1248
1249static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1250 struct netlink_callback *cb)
1251{
1252 struct devlink *devlink;
1253 struct devlink_sb *devlink_sb;
1254 int start = cb->args[0];
1255 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001256 int err = 0;
Jiri Pirkobf797472016-04-14 18:19:13 +02001257
1258 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001259 list_for_each_entry(devlink, &devlink_list, list) {
1260 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001261 !devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001262 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001263 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001264 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1265 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1266 devlink, devlink_sb,
1267 NETLINK_CB(cb->skb).portid,
1268 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001269 if (err && err != -EOPNOTSUPP) {
1270 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001271 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001272 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001273 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001274 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001275 }
1276out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001277 mutex_unlock(&devlink_mutex);
1278
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001279 if (err != -EMSGSIZE)
1280 return err;
1281
Jiri Pirkobf797472016-04-14 18:19:13 +02001282 cb->args[0] = idx;
1283 return msg->len;
1284}
1285
1286static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1287 unsigned int sb_index, u16 pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001288 u32 threshold,
1289 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001290
1291{
1292 const struct devlink_ops *ops = devlink_port->devlink->ops;
1293
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001294 if (ops->sb_port_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001295 return ops->sb_port_pool_set(devlink_port, sb_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001296 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001297 return -EOPNOTSUPP;
1298}
1299
1300static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1301 struct genl_info *info)
1302{
1303 struct devlink_port *devlink_port = info->user_ptr[0];
1304 struct devlink_sb *devlink_sb = info->user_ptr[1];
1305 u16 pool_index;
1306 u32 threshold;
1307 int err;
1308
1309 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1310 &pool_index);
1311 if (err)
1312 return err;
1313
1314 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1315 return -EINVAL;
1316
1317 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1318 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001319 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001320}
1321
1322static int
1323devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1324 struct devlink_port *devlink_port,
1325 struct devlink_sb *devlink_sb, u16 tc_index,
1326 enum devlink_sb_pool_type pool_type,
1327 enum devlink_command cmd,
1328 u32 portid, u32 seq, int flags)
1329{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001330 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001331 u16 pool_index;
1332 u32 threshold;
1333 void *hdr;
1334 int err;
1335
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001336 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1337 tc_index, pool_type,
1338 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001339 if (err)
1340 return err;
1341
1342 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1343 if (!hdr)
1344 return -EMSGSIZE;
1345
1346 if (devlink_nl_put_handle(msg, devlink))
1347 goto nla_put_failure;
1348 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1349 goto nla_put_failure;
1350 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1351 goto nla_put_failure;
1352 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1353 goto nla_put_failure;
1354 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1355 goto nla_put_failure;
1356 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1357 goto nla_put_failure;
1358 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1359 goto nla_put_failure;
1360
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001361 if (ops->sb_occ_tc_port_bind_get) {
1362 u32 cur;
1363 u32 max;
1364
1365 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1366 devlink_sb->index,
1367 tc_index, pool_type,
1368 &cur, &max);
1369 if (err && err != -EOPNOTSUPP)
1370 return err;
1371 if (!err) {
1372 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1373 goto nla_put_failure;
1374 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1375 goto nla_put_failure;
1376 }
1377 }
1378
Jiri Pirkobf797472016-04-14 18:19:13 +02001379 genlmsg_end(msg, hdr);
1380 return 0;
1381
1382nla_put_failure:
1383 genlmsg_cancel(msg, hdr);
1384 return -EMSGSIZE;
1385}
1386
1387static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1388 struct genl_info *info)
1389{
1390 struct devlink_port *devlink_port = info->user_ptr[0];
1391 struct devlink *devlink = devlink_port->devlink;
1392 struct devlink_sb *devlink_sb = info->user_ptr[1];
1393 struct sk_buff *msg;
1394 enum devlink_sb_pool_type pool_type;
1395 u16 tc_index;
1396 int err;
1397
1398 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1399 if (err)
1400 return err;
1401
1402 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1403 pool_type, &tc_index);
1404 if (err)
1405 return err;
1406
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001407 if (!devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001408 return -EOPNOTSUPP;
1409
1410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1411 if (!msg)
1412 return -ENOMEM;
1413
1414 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1415 devlink_sb, tc_index, pool_type,
1416 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1417 info->snd_portid,
1418 info->snd_seq, 0);
1419 if (err) {
1420 nlmsg_free(msg);
1421 return err;
1422 }
1423
1424 return genlmsg_reply(msg, info);
1425}
1426
1427static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1428 int start, int *p_idx,
1429 struct devlink *devlink,
1430 struct devlink_sb *devlink_sb,
1431 u32 portid, u32 seq)
1432{
1433 struct devlink_port *devlink_port;
1434 u16 tc_index;
1435 int err;
1436
1437 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1438 for (tc_index = 0;
1439 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1440 if (*p_idx < start) {
1441 (*p_idx)++;
1442 continue;
1443 }
1444 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1445 devlink_port,
1446 devlink_sb,
1447 tc_index,
1448 DEVLINK_SB_POOL_TYPE_INGRESS,
1449 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1450 portid, seq,
1451 NLM_F_MULTI);
1452 if (err)
1453 return err;
1454 (*p_idx)++;
1455 }
1456 for (tc_index = 0;
1457 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1458 if (*p_idx < start) {
1459 (*p_idx)++;
1460 continue;
1461 }
1462 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1463 devlink_port,
1464 devlink_sb,
1465 tc_index,
1466 DEVLINK_SB_POOL_TYPE_EGRESS,
1467 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1468 portid, seq,
1469 NLM_F_MULTI);
1470 if (err)
1471 return err;
1472 (*p_idx)++;
1473 }
1474 }
1475 return 0;
1476}
1477
1478static int
1479devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1480 struct netlink_callback *cb)
1481{
1482 struct devlink *devlink;
1483 struct devlink_sb *devlink_sb;
1484 int start = cb->args[0];
1485 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001486 int err = 0;
Jiri Pirkobf797472016-04-14 18:19:13 +02001487
1488 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001489 list_for_each_entry(devlink, &devlink_list, list) {
1490 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001491 !devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001492 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001493
1494 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001495 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1496 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1497 devlink,
1498 devlink_sb,
1499 NETLINK_CB(cb->skb).portid,
1500 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001501 if (err && err != -EOPNOTSUPP) {
1502 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001503 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001504 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001505 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001506 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001507 }
1508out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001509 mutex_unlock(&devlink_mutex);
1510
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02001511 if (err != -EMSGSIZE)
1512 return err;
1513
Jiri Pirkobf797472016-04-14 18:19:13 +02001514 cb->args[0] = idx;
1515 return msg->len;
1516}
1517
1518static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1519 unsigned int sb_index, u16 tc_index,
1520 enum devlink_sb_pool_type pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001521 u16 pool_index, u32 threshold,
1522 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001523
1524{
1525 const struct devlink_ops *ops = devlink_port->devlink->ops;
1526
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001527 if (ops->sb_tc_pool_bind_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001528 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1529 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001530 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001531 return -EOPNOTSUPP;
1532}
1533
1534static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1535 struct genl_info *info)
1536{
1537 struct devlink_port *devlink_port = info->user_ptr[0];
1538 struct devlink_sb *devlink_sb = info->user_ptr[1];
1539 enum devlink_sb_pool_type pool_type;
1540 u16 tc_index;
1541 u16 pool_index;
1542 u32 threshold;
1543 int err;
1544
1545 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1546 if (err)
1547 return err;
1548
1549 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1550 pool_type, &tc_index);
1551 if (err)
1552 return err;
1553
1554 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1555 &pool_index);
1556 if (err)
1557 return err;
1558
1559 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1560 return -EINVAL;
1561
1562 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1563 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1564 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001565 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001566}
1567
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001568static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1569 struct genl_info *info)
1570{
1571 struct devlink *devlink = info->user_ptr[0];
1572 struct devlink_sb *devlink_sb = info->user_ptr[1];
1573 const struct devlink_ops *ops = devlink->ops;
1574
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001575 if (ops->sb_occ_snapshot)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001576 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1577 return -EOPNOTSUPP;
1578}
1579
1580static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1581 struct genl_info *info)
1582{
1583 struct devlink *devlink = info->user_ptr[0];
1584 struct devlink_sb *devlink_sb = info->user_ptr[1];
1585 const struct devlink_ops *ops = devlink->ops;
1586
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001587 if (ops->sb_occ_max_clear)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001588 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1589 return -EOPNOTSUPP;
1590}
1591
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001592static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1593 enum devlink_command cmd, u32 portid,
1594 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001595{
Roi Dayan59bfde02016-11-22 23:09:57 +02001596 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001597 enum devlink_eswitch_encap_mode encap_mode;
1598 u8 inline_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001599 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001600 int err = 0;
1601 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001602
1603 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1604 if (!hdr)
1605 return -EMSGSIZE;
1606
Roi Dayan59bfde02016-11-22 23:09:57 +02001607 err = devlink_nl_put_handle(msg, devlink);
1608 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001609 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001610
Jiri Pirko4456f612017-02-09 15:54:36 +01001611 if (ops->eswitch_mode_get) {
1612 err = ops->eswitch_mode_get(devlink, &mode);
1613 if (err)
1614 goto nla_put_failure;
1615 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1616 if (err)
1617 goto nla_put_failure;
1618 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001619
1620 if (ops->eswitch_inline_mode_get) {
1621 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1622 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001623 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001624 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1625 inline_mode);
1626 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001627 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001628 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001629
Roi Dayanf43e9b02016-09-25 13:52:44 +03001630 if (ops->eswitch_encap_mode_get) {
1631 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1632 if (err)
1633 goto nla_put_failure;
1634 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1635 if (err)
1636 goto nla_put_failure;
1637 }
1638
Or Gerlitz08f4b592016-07-01 14:51:01 +03001639 genlmsg_end(msg, hdr);
1640 return 0;
1641
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001642nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001643 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001644 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001645}
1646
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001647static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1648 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001649{
1650 struct devlink *devlink = info->user_ptr[0];
Or Gerlitz08f4b592016-07-01 14:51:01 +03001651 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001652 int err;
1653
Or Gerlitz08f4b592016-07-01 14:51:01 +03001654 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1655 if (!msg)
1656 return -ENOMEM;
1657
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001658 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1659 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001660
1661 if (err) {
1662 nlmsg_free(msg);
1663 return err;
1664 }
1665
1666 return genlmsg_reply(msg, info);
1667}
1668
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001669static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1670 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001671{
1672 struct devlink *devlink = info->user_ptr[0];
1673 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001674 enum devlink_eswitch_encap_mode encap_mode;
1675 u8 inline_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001676 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001677 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001678
Roi Dayan59bfde02016-11-22 23:09:57 +02001679 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1680 if (!ops->eswitch_mode_set)
1681 return -EOPNOTSUPP;
1682 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001683 err = ops->eswitch_mode_set(devlink, mode, info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001684 if (err)
1685 return err;
1686 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001687
Roi Dayan59bfde02016-11-22 23:09:57 +02001688 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1689 if (!ops->eswitch_inline_mode_set)
1690 return -EOPNOTSUPP;
1691 inline_mode = nla_get_u8(
1692 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001693 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1694 info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001695 if (err)
1696 return err;
1697 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001698
1699 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1700 if (!ops->eswitch_encap_mode_set)
1701 return -EOPNOTSUPP;
1702 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001703 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1704 info->extack);
Roi Dayanf43e9b02016-09-25 13:52:44 +03001705 if (err)
1706 return err;
1707 }
1708
Roi Dayan59bfde02016-11-22 23:09:57 +02001709 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001710}
1711
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001712int devlink_dpipe_match_put(struct sk_buff *skb,
1713 struct devlink_dpipe_match *match)
1714{
1715 struct devlink_dpipe_header *header = match->header;
1716 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1717 struct nlattr *match_attr;
1718
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001719 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001720 if (!match_attr)
1721 return -EMSGSIZE;
1722
1723 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1724 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1725 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1726 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1727 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1728 goto nla_put_failure;
1729
1730 nla_nest_end(skb, match_attr);
1731 return 0;
1732
1733nla_put_failure:
1734 nla_nest_cancel(skb, match_attr);
1735 return -EMSGSIZE;
1736}
1737EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1738
1739static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1740 struct sk_buff *skb)
1741{
1742 struct nlattr *matches_attr;
1743
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001744 matches_attr = nla_nest_start_noflag(skb,
1745 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001746 if (!matches_attr)
1747 return -EMSGSIZE;
1748
1749 if (table->table_ops->matches_dump(table->priv, skb))
1750 goto nla_put_failure;
1751
1752 nla_nest_end(skb, matches_attr);
1753 return 0;
1754
1755nla_put_failure:
1756 nla_nest_cancel(skb, matches_attr);
1757 return -EMSGSIZE;
1758}
1759
1760int devlink_dpipe_action_put(struct sk_buff *skb,
1761 struct devlink_dpipe_action *action)
1762{
1763 struct devlink_dpipe_header *header = action->header;
1764 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1765 struct nlattr *action_attr;
1766
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001767 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001768 if (!action_attr)
1769 return -EMSGSIZE;
1770
1771 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1772 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1773 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1774 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1775 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1776 goto nla_put_failure;
1777
1778 nla_nest_end(skb, action_attr);
1779 return 0;
1780
1781nla_put_failure:
1782 nla_nest_cancel(skb, action_attr);
1783 return -EMSGSIZE;
1784}
1785EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1786
1787static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1788 struct sk_buff *skb)
1789{
1790 struct nlattr *actions_attr;
1791
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001792 actions_attr = nla_nest_start_noflag(skb,
1793 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001794 if (!actions_attr)
1795 return -EMSGSIZE;
1796
1797 if (table->table_ops->actions_dump(table->priv, skb))
1798 goto nla_put_failure;
1799
1800 nla_nest_end(skb, actions_attr);
1801 return 0;
1802
1803nla_put_failure:
1804 nla_nest_cancel(skb, actions_attr);
1805 return -EMSGSIZE;
1806}
1807
1808static int devlink_dpipe_table_put(struct sk_buff *skb,
1809 struct devlink_dpipe_table *table)
1810{
1811 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001812 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001813
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001814 table_size = table->table_ops->size_get(table->priv);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001815 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001816 if (!table_attr)
1817 return -EMSGSIZE;
1818
1819 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001820 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001821 DEVLINK_ATTR_PAD))
1822 goto nla_put_failure;
1823 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1824 table->counters_enabled))
1825 goto nla_put_failure;
1826
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001827 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001828 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1829 table->resource_id, DEVLINK_ATTR_PAD) ||
1830 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1831 table->resource_units, DEVLINK_ATTR_PAD))
1832 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001833 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001834 if (devlink_dpipe_matches_put(table, skb))
1835 goto nla_put_failure;
1836
1837 if (devlink_dpipe_actions_put(table, skb))
1838 goto nla_put_failure;
1839
1840 nla_nest_end(skb, table_attr);
1841 return 0;
1842
1843nla_put_failure:
1844 nla_nest_cancel(skb, table_attr);
1845 return -EMSGSIZE;
1846}
1847
1848static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1849 struct genl_info *info)
1850{
1851 int err;
1852
1853 if (*pskb) {
1854 err = genlmsg_reply(*pskb, info);
1855 if (err)
1856 return err;
1857 }
1858 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1859 if (!*pskb)
1860 return -ENOMEM;
1861 return 0;
1862}
1863
1864static int devlink_dpipe_tables_fill(struct genl_info *info,
1865 enum devlink_command cmd, int flags,
1866 struct list_head *dpipe_tables,
1867 const char *table_name)
1868{
1869 struct devlink *devlink = info->user_ptr[0];
1870 struct devlink_dpipe_table *table;
1871 struct nlattr *tables_attr;
1872 struct sk_buff *skb = NULL;
1873 struct nlmsghdr *nlh;
1874 bool incomplete;
1875 void *hdr;
1876 int i;
1877 int err;
1878
1879 table = list_first_entry(dpipe_tables,
1880 struct devlink_dpipe_table, list);
1881start_again:
1882 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1883 if (err)
1884 return err;
1885
1886 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1887 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001888 if (!hdr) {
1889 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001890 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001891 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001892
1893 if (devlink_nl_put_handle(skb, devlink))
1894 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001895 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001896 if (!tables_attr)
1897 goto nla_put_failure;
1898
1899 i = 0;
1900 incomplete = false;
1901 list_for_each_entry_from(table, dpipe_tables, list) {
1902 if (!table_name) {
1903 err = devlink_dpipe_table_put(skb, table);
1904 if (err) {
1905 if (!i)
1906 goto err_table_put;
1907 incomplete = true;
1908 break;
1909 }
1910 } else {
1911 if (!strcmp(table->name, table_name)) {
1912 err = devlink_dpipe_table_put(skb, table);
1913 if (err)
1914 break;
1915 }
1916 }
1917 i++;
1918 }
1919
1920 nla_nest_end(skb, tables_attr);
1921 genlmsg_end(skb, hdr);
1922 if (incomplete)
1923 goto start_again;
1924
1925send_done:
1926 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1927 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1928 if (!nlh) {
1929 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1930 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001931 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001932 goto send_done;
1933 }
1934
1935 return genlmsg_reply(skb, info);
1936
1937nla_put_failure:
1938 err = -EMSGSIZE;
1939err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001940 nlmsg_free(skb);
1941 return err;
1942}
1943
1944static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1945 struct genl_info *info)
1946{
1947 struct devlink *devlink = info->user_ptr[0];
1948 const char *table_name = NULL;
1949
1950 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1951 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1952
1953 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1954 &devlink->dpipe_table_list,
1955 table_name);
1956}
1957
1958static int devlink_dpipe_value_put(struct sk_buff *skb,
1959 struct devlink_dpipe_value *value)
1960{
1961 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1962 value->value_size, value->value))
1963 return -EMSGSIZE;
1964 if (value->mask)
1965 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1966 value->value_size, value->mask))
1967 return -EMSGSIZE;
1968 if (value->mapping_valid)
1969 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1970 value->mapping_value))
1971 return -EMSGSIZE;
1972 return 0;
1973}
1974
1975static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1976 struct devlink_dpipe_value *value)
1977{
1978 if (!value->action)
1979 return -EINVAL;
1980 if (devlink_dpipe_action_put(skb, value->action))
1981 return -EMSGSIZE;
1982 if (devlink_dpipe_value_put(skb, value))
1983 return -EMSGSIZE;
1984 return 0;
1985}
1986
1987static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1988 struct devlink_dpipe_value *values,
1989 unsigned int values_count)
1990{
1991 struct nlattr *action_attr;
1992 int i;
1993 int err;
1994
1995 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001996 action_attr = nla_nest_start_noflag(skb,
1997 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001998 if (!action_attr)
1999 return -EMSGSIZE;
2000 err = devlink_dpipe_action_value_put(skb, &values[i]);
2001 if (err)
2002 goto err_action_value_put;
2003 nla_nest_end(skb, action_attr);
2004 }
2005 return 0;
2006
2007err_action_value_put:
2008 nla_nest_cancel(skb, action_attr);
2009 return err;
2010}
2011
2012static int devlink_dpipe_match_value_put(struct sk_buff *skb,
2013 struct devlink_dpipe_value *value)
2014{
2015 if (!value->match)
2016 return -EINVAL;
2017 if (devlink_dpipe_match_put(skb, value->match))
2018 return -EMSGSIZE;
2019 if (devlink_dpipe_value_put(skb, value))
2020 return -EMSGSIZE;
2021 return 0;
2022}
2023
2024static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2025 struct devlink_dpipe_value *values,
2026 unsigned int values_count)
2027{
2028 struct nlattr *match_attr;
2029 int i;
2030 int err;
2031
2032 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002033 match_attr = nla_nest_start_noflag(skb,
2034 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002035 if (!match_attr)
2036 return -EMSGSIZE;
2037 err = devlink_dpipe_match_value_put(skb, &values[i]);
2038 if (err)
2039 goto err_match_value_put;
2040 nla_nest_end(skb, match_attr);
2041 }
2042 return 0;
2043
2044err_match_value_put:
2045 nla_nest_cancel(skb, match_attr);
2046 return err;
2047}
2048
2049static int devlink_dpipe_entry_put(struct sk_buff *skb,
2050 struct devlink_dpipe_entry *entry)
2051{
2052 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2053 int err;
2054
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002055 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002056 if (!entry_attr)
2057 return -EMSGSIZE;
2058
2059 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2060 DEVLINK_ATTR_PAD))
2061 goto nla_put_failure;
2062 if (entry->counter_valid)
2063 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2064 entry->counter, DEVLINK_ATTR_PAD))
2065 goto nla_put_failure;
2066
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002067 matches_attr = nla_nest_start_noflag(skb,
2068 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002069 if (!matches_attr)
2070 goto nla_put_failure;
2071
2072 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2073 entry->match_values_count);
2074 if (err) {
2075 nla_nest_cancel(skb, matches_attr);
2076 goto err_match_values_put;
2077 }
2078 nla_nest_end(skb, matches_attr);
2079
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002080 actions_attr = nla_nest_start_noflag(skb,
2081 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002082 if (!actions_attr)
2083 goto nla_put_failure;
2084
2085 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2086 entry->action_values_count);
2087 if (err) {
2088 nla_nest_cancel(skb, actions_attr);
2089 goto err_action_values_put;
2090 }
2091 nla_nest_end(skb, actions_attr);
2092
2093 nla_nest_end(skb, entry_attr);
2094 return 0;
2095
2096nla_put_failure:
2097 err = -EMSGSIZE;
2098err_match_values_put:
2099err_action_values_put:
2100 nla_nest_cancel(skb, entry_attr);
2101 return err;
2102}
2103
2104static struct devlink_dpipe_table *
2105devlink_dpipe_table_find(struct list_head *dpipe_tables,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05302106 const char *table_name, struct devlink *devlink)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002107{
2108 struct devlink_dpipe_table *table;
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05302109 list_for_each_entry_rcu(table, dpipe_tables, list,
2110 lockdep_is_held(&devlink->lock)) {
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002111 if (!strcmp(table->name, table_name))
2112 return table;
2113 }
2114 return NULL;
2115}
2116
2117int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2118{
2119 struct devlink *devlink;
2120 int err;
2121
2122 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2123 dump_ctx->info);
2124 if (err)
2125 return err;
2126
2127 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2128 dump_ctx->info->snd_portid,
2129 dump_ctx->info->snd_seq,
2130 &devlink_nl_family, NLM_F_MULTI,
2131 dump_ctx->cmd);
2132 if (!dump_ctx->hdr)
2133 goto nla_put_failure;
2134
2135 devlink = dump_ctx->info->user_ptr[0];
2136 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2137 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002138 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2139 DEVLINK_ATTR_DPIPE_ENTRIES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002140 if (!dump_ctx->nest)
2141 goto nla_put_failure;
2142 return 0;
2143
2144nla_put_failure:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002145 nlmsg_free(dump_ctx->skb);
2146 return -EMSGSIZE;
2147}
2148EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2149
2150int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2151 struct devlink_dpipe_entry *entry)
2152{
2153 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2154}
2155EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2156
2157int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2158{
2159 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2160 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2164
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002165void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2166
2167{
2168 unsigned int value_count, value_index;
2169 struct devlink_dpipe_value *value;
2170
2171 value = entry->action_values;
2172 value_count = entry->action_values_count;
2173 for (value_index = 0; value_index < value_count; value_index++) {
2174 kfree(value[value_index].value);
2175 kfree(value[value_index].mask);
2176 }
2177
2178 value = entry->match_values;
2179 value_count = entry->match_values_count;
2180 for (value_index = 0; value_index < value_count; value_index++) {
2181 kfree(value[value_index].value);
2182 kfree(value[value_index].mask);
2183 }
2184}
2185EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2186
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002187static int devlink_dpipe_entries_fill(struct genl_info *info,
2188 enum devlink_command cmd, int flags,
2189 struct devlink_dpipe_table *table)
2190{
2191 struct devlink_dpipe_dump_ctx dump_ctx;
2192 struct nlmsghdr *nlh;
2193 int err;
2194
2195 dump_ctx.skb = NULL;
2196 dump_ctx.cmd = cmd;
2197 dump_ctx.info = info;
2198
2199 err = table->table_ops->entries_dump(table->priv,
2200 table->counters_enabled,
2201 &dump_ctx);
2202 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002203 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002204
2205send_done:
2206 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2207 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2208 if (!nlh) {
2209 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2210 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002211 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002212 goto send_done;
2213 }
2214 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002215}
2216
2217static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2218 struct genl_info *info)
2219{
2220 struct devlink *devlink = info->user_ptr[0];
2221 struct devlink_dpipe_table *table;
2222 const char *table_name;
2223
2224 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2225 return -EINVAL;
2226
2227 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2228 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05302229 table_name, devlink);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002230 if (!table)
2231 return -EINVAL;
2232
2233 if (!table->table_ops->entries_dump)
2234 return -EINVAL;
2235
2236 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2237 0, table);
2238}
2239
2240static int devlink_dpipe_fields_put(struct sk_buff *skb,
2241 const struct devlink_dpipe_header *header)
2242{
2243 struct devlink_dpipe_field *field;
2244 struct nlattr *field_attr;
2245 int i;
2246
2247 for (i = 0; i < header->fields_count; i++) {
2248 field = &header->fields[i];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002249 field_attr = nla_nest_start_noflag(skb,
2250 DEVLINK_ATTR_DPIPE_FIELD);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002251 if (!field_attr)
2252 return -EMSGSIZE;
2253 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2254 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2255 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2256 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2257 goto nla_put_failure;
2258 nla_nest_end(skb, field_attr);
2259 }
2260 return 0;
2261
2262nla_put_failure:
2263 nla_nest_cancel(skb, field_attr);
2264 return -EMSGSIZE;
2265}
2266
2267static int devlink_dpipe_header_put(struct sk_buff *skb,
2268 struct devlink_dpipe_header *header)
2269{
2270 struct nlattr *fields_attr, *header_attr;
2271 int err;
2272
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002273 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002274 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002275 return -EMSGSIZE;
2276
2277 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2278 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2279 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2280 goto nla_put_failure;
2281
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002282 fields_attr = nla_nest_start_noflag(skb,
2283 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002284 if (!fields_attr)
2285 goto nla_put_failure;
2286
2287 err = devlink_dpipe_fields_put(skb, header);
2288 if (err) {
2289 nla_nest_cancel(skb, fields_attr);
2290 goto nla_put_failure;
2291 }
2292 nla_nest_end(skb, fields_attr);
2293 nla_nest_end(skb, header_attr);
2294 return 0;
2295
2296nla_put_failure:
2297 err = -EMSGSIZE;
2298 nla_nest_cancel(skb, header_attr);
2299 return err;
2300}
2301
2302static int devlink_dpipe_headers_fill(struct genl_info *info,
2303 enum devlink_command cmd, int flags,
2304 struct devlink_dpipe_headers *
2305 dpipe_headers)
2306{
2307 struct devlink *devlink = info->user_ptr[0];
2308 struct nlattr *headers_attr;
2309 struct sk_buff *skb = NULL;
2310 struct nlmsghdr *nlh;
2311 void *hdr;
2312 int i, j;
2313 int err;
2314
2315 i = 0;
2316start_again:
2317 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2318 if (err)
2319 return err;
2320
2321 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2322 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002323 if (!hdr) {
2324 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002325 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002326 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002327
2328 if (devlink_nl_put_handle(skb, devlink))
2329 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002330 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002331 if (!headers_attr)
2332 goto nla_put_failure;
2333
2334 j = 0;
2335 for (; i < dpipe_headers->headers_count; i++) {
2336 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2337 if (err) {
2338 if (!j)
2339 goto err_table_put;
2340 break;
2341 }
2342 j++;
2343 }
2344 nla_nest_end(skb, headers_attr);
2345 genlmsg_end(skb, hdr);
2346 if (i != dpipe_headers->headers_count)
2347 goto start_again;
2348
2349send_done:
2350 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2351 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2352 if (!nlh) {
2353 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2354 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002355 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002356 goto send_done;
2357 }
2358 return genlmsg_reply(skb, info);
2359
2360nla_put_failure:
2361 err = -EMSGSIZE;
2362err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002363 nlmsg_free(skb);
2364 return err;
2365}
2366
2367static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2368 struct genl_info *info)
2369{
2370 struct devlink *devlink = info->user_ptr[0];
2371
2372 if (!devlink->dpipe_headers)
2373 return -EOPNOTSUPP;
2374 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2375 0, devlink->dpipe_headers);
2376}
2377
2378static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2379 const char *table_name,
2380 bool enable)
2381{
2382 struct devlink_dpipe_table *table;
2383
2384 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05302385 table_name, devlink);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002386 if (!table)
2387 return -EINVAL;
2388
2389 if (table->counter_control_extern)
2390 return -EOPNOTSUPP;
2391
2392 if (!(table->counters_enabled ^ enable))
2393 return 0;
2394
2395 table->counters_enabled = enable;
2396 if (table->table_ops->counters_set_update)
2397 table->table_ops->counters_set_update(table->priv, enable);
2398 return 0;
2399}
2400
2401static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2402 struct genl_info *info)
2403{
2404 struct devlink *devlink = info->user_ptr[0];
2405 const char *table_name;
2406 bool counters_enable;
2407
2408 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2409 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2410 return -EINVAL;
2411
2412 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2413 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2414
2415 return devlink_dpipe_table_counters_set(devlink, table_name,
2416 counters_enable);
2417}
2418
Wei Yongjun43dd7512018-01-17 03:27:42 +00002419static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002420devlink_resource_find(struct devlink *devlink,
2421 struct devlink_resource *resource, u64 resource_id)
2422{
2423 struct list_head *resource_list;
2424
2425 if (resource)
2426 resource_list = &resource->resource_list;
2427 else
2428 resource_list = &devlink->resource_list;
2429
2430 list_for_each_entry(resource, resource_list, list) {
2431 struct devlink_resource *child_resource;
2432
2433 if (resource->id == resource_id)
2434 return resource;
2435
2436 child_resource = devlink_resource_find(devlink, resource,
2437 resource_id);
2438 if (child_resource)
2439 return child_resource;
2440 }
2441 return NULL;
2442}
2443
Wei Yongjun43dd7512018-01-17 03:27:42 +00002444static void
2445devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002446{
2447 struct devlink_resource *child_resource;
2448 bool size_valid = true;
2449 u64 parts_size = 0;
2450
2451 if (list_empty(&resource->resource_list))
2452 goto out;
2453
2454 list_for_each_entry(child_resource, &resource->resource_list, list)
2455 parts_size += child_resource->size_new;
2456
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002457 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002458 size_valid = false;
2459out:
2460 resource->size_valid = size_valid;
2461}
2462
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002463static int
2464devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2465 struct netlink_ext_ack *extack)
2466{
2467 u64 reminder;
2468 int err = 0;
2469
David S. Miller0f3e9c92018-03-06 00:53:44 -05002470 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002471 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2472 err = -EINVAL;
2473 }
2474
David S. Miller0f3e9c92018-03-06 00:53:44 -05002475 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002476 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2477 err = -EINVAL;
2478 }
2479
David S. Miller0f3e9c92018-03-06 00:53:44 -05002480 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002481 if (reminder) {
2482 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2483 err = -EINVAL;
2484 }
2485
2486 return err;
2487}
2488
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002489static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2490 struct genl_info *info)
2491{
2492 struct devlink *devlink = info->user_ptr[0];
2493 struct devlink_resource *resource;
2494 u64 resource_id;
2495 u64 size;
2496 int err;
2497
2498 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2499 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2500 return -EINVAL;
2501 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2502
2503 resource = devlink_resource_find(devlink, NULL, resource_id);
2504 if (!resource)
2505 return -EINVAL;
2506
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002507 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002508 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002509 if (err)
2510 return err;
2511
2512 resource->size_new = size;
2513 devlink_resource_validate_children(resource);
2514 if (resource->parent)
2515 devlink_resource_validate_children(resource->parent);
2516 return 0;
2517}
2518
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002519static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002520devlink_resource_size_params_put(struct devlink_resource *resource,
2521 struct sk_buff *skb)
2522{
2523 struct devlink_resource_size_params *size_params;
2524
Jiri Pirko77d27092018-02-28 13:12:09 +01002525 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002526 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2527 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2528 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2529 size_params->size_max, DEVLINK_ATTR_PAD) ||
2530 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2531 size_params->size_min, DEVLINK_ATTR_PAD) ||
2532 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2533 return -EMSGSIZE;
2534 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002535}
2536
Jiri Pirkofc56be42018-04-05 22:13:21 +02002537static int devlink_resource_occ_put(struct devlink_resource *resource,
2538 struct sk_buff *skb)
2539{
2540 if (!resource->occ_get)
2541 return 0;
2542 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2543 resource->occ_get(resource->occ_get_priv),
2544 DEVLINK_ATTR_PAD);
2545}
2546
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002547static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2548 struct devlink_resource *resource)
2549{
2550 struct devlink_resource *child_resource;
2551 struct nlattr *child_resource_attr;
2552 struct nlattr *resource_attr;
2553
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002554 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002555 if (!resource_attr)
2556 return -EMSGSIZE;
2557
2558 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2559 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2560 DEVLINK_ATTR_PAD) ||
2561 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2562 DEVLINK_ATTR_PAD))
2563 goto nla_put_failure;
2564 if (resource->size != resource->size_new)
2565 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2566 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002567 if (devlink_resource_occ_put(resource, skb))
2568 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002569 if (devlink_resource_size_params_put(resource, skb))
2570 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002571 if (list_empty(&resource->resource_list))
2572 goto out;
2573
2574 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2575 resource->size_valid))
2576 goto nla_put_failure;
2577
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002578 child_resource_attr = nla_nest_start_noflag(skb,
2579 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002580 if (!child_resource_attr)
2581 goto nla_put_failure;
2582
2583 list_for_each_entry(child_resource, &resource->resource_list, list) {
2584 if (devlink_resource_put(devlink, skb, child_resource))
2585 goto resource_put_failure;
2586 }
2587
2588 nla_nest_end(skb, child_resource_attr);
2589out:
2590 nla_nest_end(skb, resource_attr);
2591 return 0;
2592
2593resource_put_failure:
2594 nla_nest_cancel(skb, child_resource_attr);
2595nla_put_failure:
2596 nla_nest_cancel(skb, resource_attr);
2597 return -EMSGSIZE;
2598}
2599
2600static int devlink_resource_fill(struct genl_info *info,
2601 enum devlink_command cmd, int flags)
2602{
2603 struct devlink *devlink = info->user_ptr[0];
2604 struct devlink_resource *resource;
2605 struct nlattr *resources_attr;
2606 struct sk_buff *skb = NULL;
2607 struct nlmsghdr *nlh;
2608 bool incomplete;
2609 void *hdr;
2610 int i;
2611 int err;
2612
2613 resource = list_first_entry(&devlink->resource_list,
2614 struct devlink_resource, list);
2615start_again:
2616 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2617 if (err)
2618 return err;
2619
2620 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2621 &devlink_nl_family, NLM_F_MULTI, cmd);
2622 if (!hdr) {
2623 nlmsg_free(skb);
2624 return -EMSGSIZE;
2625 }
2626
2627 if (devlink_nl_put_handle(skb, devlink))
2628 goto nla_put_failure;
2629
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002630 resources_attr = nla_nest_start_noflag(skb,
2631 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002632 if (!resources_attr)
2633 goto nla_put_failure;
2634
2635 incomplete = false;
2636 i = 0;
2637 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2638 err = devlink_resource_put(devlink, skb, resource);
2639 if (err) {
2640 if (!i)
2641 goto err_resource_put;
2642 incomplete = true;
2643 break;
2644 }
2645 i++;
2646 }
2647 nla_nest_end(skb, resources_attr);
2648 genlmsg_end(skb, hdr);
2649 if (incomplete)
2650 goto start_again;
2651send_done:
2652 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2653 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2654 if (!nlh) {
2655 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2656 if (err)
Dan Carpenter83fe9a92018-09-21 11:07:55 +03002657 return err;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002658 goto send_done;
2659 }
2660 return genlmsg_reply(skb, info);
2661
2662nla_put_failure:
2663 err = -EMSGSIZE;
2664err_resource_put:
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002665 nlmsg_free(skb);
2666 return err;
2667}
2668
2669static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2670 struct genl_info *info)
2671{
2672 struct devlink *devlink = info->user_ptr[0];
2673
2674 if (list_empty(&devlink->resource_list))
2675 return -EOPNOTSUPP;
2676
2677 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2678}
2679
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002680static int
2681devlink_resources_validate(struct devlink *devlink,
2682 struct devlink_resource *resource,
2683 struct genl_info *info)
2684{
2685 struct list_head *resource_list;
2686 int err = 0;
2687
2688 if (resource)
2689 resource_list = &resource->resource_list;
2690 else
2691 resource_list = &devlink->resource_list;
2692
2693 list_for_each_entry(resource, resource_list, list) {
2694 if (!resource->size_valid)
2695 return -EINVAL;
2696 err = devlink_resources_validate(devlink, resource, info);
2697 if (err)
2698 return err;
2699 }
2700 return err;
2701}
2702
Jiri Pirko070c63f2019-10-03 11:49:39 +02002703static struct net *devlink_netns_get(struct sk_buff *skb,
2704 struct genl_info *info)
2705{
2706 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
2707 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
2708 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
2709 struct net *net;
2710
2711 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
Jiri Pirko054eae82020-03-28 19:25:29 +01002712 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
Jiri Pirko070c63f2019-10-03 11:49:39 +02002713 return ERR_PTR(-EINVAL);
2714 }
2715
2716 if (netns_pid_attr) {
2717 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
2718 } else if (netns_fd_attr) {
2719 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
2720 } else if (netns_id_attr) {
2721 net = get_net_ns_by_id(sock_net(skb->sk),
2722 nla_get_u32(netns_id_attr));
2723 if (!net)
2724 net = ERR_PTR(-EINVAL);
2725 } else {
2726 WARN_ON(1);
2727 net = ERR_PTR(-EINVAL);
2728 }
2729 if (IS_ERR(net)) {
Jiri Pirko054eae82020-03-28 19:25:29 +01002730 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
Jiri Pirko070c63f2019-10-03 11:49:39 +02002731 return ERR_PTR(-EINVAL);
2732 }
2733 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
2734 put_net(net);
2735 return ERR_PTR(-EPERM);
2736 }
2737 return net;
2738}
2739
2740static void devlink_param_notify(struct devlink *devlink,
2741 unsigned int port_index,
2742 struct devlink_param_item *param_item,
2743 enum devlink_command cmd);
2744
2745static void devlink_reload_netns_change(struct devlink *devlink,
2746 struct net *dest_net)
2747{
2748 struct devlink_param_item *param_item;
2749
2750 /* Userspace needs to be notified about devlink objects
2751 * removed from original and entering new network namespace.
2752 * The rest of the devlink objects are re-created during
2753 * reload process so the notifications are generated separatelly.
2754 */
2755
2756 list_for_each_entry(param_item, &devlink->param_list, list)
2757 devlink_param_notify(devlink, 0, param_item,
2758 DEVLINK_CMD_PARAM_DEL);
2759 devlink_notify(devlink, DEVLINK_CMD_DEL);
2760
Jiri Pirko8273fd82019-10-05 08:10:31 +02002761 __devlink_net_set(devlink, dest_net);
Jiri Pirko070c63f2019-10-03 11:49:39 +02002762
2763 devlink_notify(devlink, DEVLINK_CMD_NEW);
2764 list_for_each_entry(param_item, &devlink->param_list, list)
2765 devlink_param_notify(devlink, 0, param_item,
2766 DEVLINK_CMD_PARAM_NEW);
2767}
2768
Jiri Pirko97691062019-09-12 10:49:45 +02002769static bool devlink_reload_supported(struct devlink *devlink)
2770{
2771 return devlink->ops->reload_down && devlink->ops->reload_up;
2772}
2773
Jiri Pirko2670ac22019-09-12 10:49:46 +02002774static void devlink_reload_failed_set(struct devlink *devlink,
2775 bool reload_failed)
2776{
2777 if (devlink->reload_failed == reload_failed)
2778 return;
2779 devlink->reload_failed = reload_failed;
2780 devlink_notify(devlink, DEVLINK_CMD_NEW);
2781}
2782
2783bool devlink_is_reload_failed(const struct devlink *devlink)
2784{
2785 return devlink->reload_failed;
2786}
2787EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
2788
Jiri Pirko070c63f2019-10-03 11:49:39 +02002789static int devlink_reload(struct devlink *devlink, struct net *dest_net,
2790 struct netlink_ext_ack *extack)
2791{
2792 int err;
2793
Jiri Pirkoa0c76342019-11-08 21:42:43 +01002794 if (!devlink->reload_enabled)
2795 return -EOPNOTSUPP;
2796
Jiri Pirko070c63f2019-10-03 11:49:39 +02002797 err = devlink->ops->reload_down(devlink, !!dest_net, extack);
2798 if (err)
2799 return err;
2800
2801 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
2802 devlink_reload_netns_change(devlink, dest_net);
2803
2804 err = devlink->ops->reload_up(devlink, extack);
2805 devlink_reload_failed_set(devlink, !!err);
2806 return err;
2807}
2808
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002809static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2810{
2811 struct devlink *devlink = info->user_ptr[0];
Jiri Pirko070c63f2019-10-03 11:49:39 +02002812 struct net *dest_net = NULL;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002813 int err;
2814
Jiri Pirko5a508a22019-11-09 11:29:46 +01002815 if (!devlink_reload_supported(devlink) || !devlink->reload_enabled)
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002816 return -EOPNOTSUPP;
2817
2818 err = devlink_resources_validate(devlink, NULL, info);
2819 if (err) {
2820 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2821 return err;
2822 }
Jiri Pirko070c63f2019-10-03 11:49:39 +02002823
2824 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
2825 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
2826 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
2827 dest_net = devlink_netns_get(skb, info);
2828 if (IS_ERR(dest_net))
2829 return PTR_ERR(dest_net);
2830 }
2831
2832 err = devlink_reload(devlink, dest_net, info->extack);
2833
2834 if (dest_net)
2835 put_net(dest_net);
2836
Jiri Pirko2670ac22019-09-12 10:49:46 +02002837 return err;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002838}
2839
Jiri Pirko191ed202019-06-04 15:40:40 +02002840static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2841 struct devlink *devlink,
2842 enum devlink_command cmd,
2843 const char *status_msg,
2844 const char *component,
2845 unsigned long done, unsigned long total)
2846{
2847 void *hdr;
2848
2849 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2850 if (!hdr)
2851 return -EMSGSIZE;
2852
2853 if (devlink_nl_put_handle(msg, devlink))
2854 goto nla_put_failure;
2855
2856 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2857 goto out;
2858
2859 if (status_msg &&
2860 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2861 status_msg))
2862 goto nla_put_failure;
2863 if (component &&
2864 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2865 component))
2866 goto nla_put_failure;
2867 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2868 done, DEVLINK_ATTR_PAD))
2869 goto nla_put_failure;
2870 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2871 total, DEVLINK_ATTR_PAD))
2872 goto nla_put_failure;
2873
2874out:
2875 genlmsg_end(msg, hdr);
2876 return 0;
2877
2878nla_put_failure:
2879 genlmsg_cancel(msg, hdr);
2880 return -EMSGSIZE;
2881}
2882
2883static void __devlink_flash_update_notify(struct devlink *devlink,
2884 enum devlink_command cmd,
2885 const char *status_msg,
2886 const char *component,
2887 unsigned long done,
2888 unsigned long total)
2889{
2890 struct sk_buff *msg;
2891 int err;
2892
2893 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2894 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2895 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2896
2897 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2898 if (!msg)
2899 return;
2900
2901 err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2902 component, done, total);
2903 if (err)
2904 goto out_free_msg;
2905
2906 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2907 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2908 return;
2909
2910out_free_msg:
2911 nlmsg_free(msg);
2912}
2913
2914void devlink_flash_update_begin_notify(struct devlink *devlink)
2915{
2916 __devlink_flash_update_notify(devlink,
2917 DEVLINK_CMD_FLASH_UPDATE,
2918 NULL, NULL, 0, 0);
2919}
2920EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2921
2922void devlink_flash_update_end_notify(struct devlink *devlink)
2923{
2924 __devlink_flash_update_notify(devlink,
2925 DEVLINK_CMD_FLASH_UPDATE_END,
2926 NULL, NULL, 0, 0);
2927}
2928EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2929
2930void devlink_flash_update_status_notify(struct devlink *devlink,
2931 const char *status_msg,
2932 const char *component,
2933 unsigned long done,
2934 unsigned long total)
2935{
2936 __devlink_flash_update_notify(devlink,
2937 DEVLINK_CMD_FLASH_UPDATE_STATUS,
2938 status_msg, component, done, total);
2939}
2940EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2941
Jakub Kicinski76726cc2019-02-14 13:40:44 -08002942static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2943 struct genl_info *info)
2944{
2945 struct devlink *devlink = info->user_ptr[0];
2946 const char *file_name, *component;
2947 struct nlattr *nla_component;
2948
2949 if (!devlink->ops->flash_update)
2950 return -EOPNOTSUPP;
2951
2952 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2953 return -EINVAL;
2954 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2955
2956 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2957 component = nla_component ? nla_data(nla_component) : NULL;
2958
2959 return devlink->ops->flash_update(devlink, file_name, component,
2960 info->extack);
2961}
2962
Moshe Shemesh036467c2018-07-04 14:30:33 +03002963static const struct devlink_param devlink_param_generic[] = {
2964 {
2965 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2966 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2967 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2968 },
2969 {
2970 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2971 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2972 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2973 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002974 {
2975 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2976 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2977 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2978 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002979 {
2980 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2981 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2982 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2983 },
Vasundhara Volame3b51062018-10-04 11:13:44 +05302984 {
2985 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2986 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2987 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2988 },
Vasundhara Volamf61cba42018-10-04 11:13:45 +05302989 {
2990 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2991 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2992 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2993 },
Vasundhara Volam16511782018-10-04 11:13:46 +05302994 {
2995 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2996 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2997 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2998 },
Shalom Toledo846e9802018-12-03 07:58:59 +00002999 {
3000 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3001 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3002 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3003 },
Dirk van der Merwe5bbd21d2019-09-09 00:54:18 +01003004 {
3005 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3006 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3007 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3008 },
Michael Guralnik6c7295e2019-11-08 23:45:20 +00003009 {
3010 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3011 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3012 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3013 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03003014};
Moshe Shemesheabaef12018-07-04 14:30:28 +03003015
3016static int devlink_param_generic_verify(const struct devlink_param *param)
3017{
3018 /* verify it match generic parameter by id and name */
3019 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3020 return -EINVAL;
3021 if (strcmp(param->name, devlink_param_generic[param->id].name))
3022 return -ENOENT;
3023
3024 WARN_ON(param->type != devlink_param_generic[param->id].type);
3025
3026 return 0;
3027}
3028
3029static int devlink_param_driver_verify(const struct devlink_param *param)
3030{
3031 int i;
3032
3033 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3034 return -EINVAL;
3035 /* verify no such name in generic params */
3036 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3037 if (!strcmp(param->name, devlink_param_generic[i].name))
3038 return -EEXIST;
3039
3040 return 0;
3041}
3042
3043static struct devlink_param_item *
3044devlink_param_find_by_name(struct list_head *param_list,
3045 const char *param_name)
3046{
3047 struct devlink_param_item *param_item;
3048
3049 list_for_each_entry(param_item, param_list, list)
3050 if (!strcmp(param_item->param->name, param_name))
3051 return param_item;
3052 return NULL;
3053}
3054
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03003055static struct devlink_param_item *
3056devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3057{
3058 struct devlink_param_item *param_item;
3059
3060 list_for_each_entry(param_item, param_list, list)
3061 if (param_item->param->id == param_id)
3062 return param_item;
3063 return NULL;
3064}
3065
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003066static bool
3067devlink_param_cmode_is_supported(const struct devlink_param *param,
3068 enum devlink_param_cmode cmode)
3069{
3070 return test_bit(cmode, &param->supported_cmodes);
3071}
3072
3073static int devlink_param_get(struct devlink *devlink,
3074 const struct devlink_param *param,
3075 struct devlink_param_gset_ctx *ctx)
3076{
3077 if (!param->get)
3078 return -EOPNOTSUPP;
3079 return param->get(devlink, param->id, ctx);
3080}
3081
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003082static int devlink_param_set(struct devlink *devlink,
3083 const struct devlink_param *param,
3084 struct devlink_param_gset_ctx *ctx)
3085{
3086 if (!param->set)
3087 return -EOPNOTSUPP;
3088 return param->set(devlink, param->id, ctx);
3089}
3090
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003091static int
3092devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3093{
3094 switch (param_type) {
3095 case DEVLINK_PARAM_TYPE_U8:
3096 return NLA_U8;
3097 case DEVLINK_PARAM_TYPE_U16:
3098 return NLA_U16;
3099 case DEVLINK_PARAM_TYPE_U32:
3100 return NLA_U32;
3101 case DEVLINK_PARAM_TYPE_STRING:
3102 return NLA_STRING;
3103 case DEVLINK_PARAM_TYPE_BOOL:
3104 return NLA_FLAG;
3105 default:
3106 return -EINVAL;
3107 }
3108}
3109
3110static int
3111devlink_nl_param_value_fill_one(struct sk_buff *msg,
3112 enum devlink_param_type type,
3113 enum devlink_param_cmode cmode,
3114 union devlink_param_value val)
3115{
3116 struct nlattr *param_value_attr;
3117
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003118 param_value_attr = nla_nest_start_noflag(msg,
3119 DEVLINK_ATTR_PARAM_VALUE);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003120 if (!param_value_attr)
3121 goto nla_put_failure;
3122
3123 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3124 goto value_nest_cancel;
3125
3126 switch (type) {
3127 case DEVLINK_PARAM_TYPE_U8:
3128 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3129 goto value_nest_cancel;
3130 break;
3131 case DEVLINK_PARAM_TYPE_U16:
3132 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3133 goto value_nest_cancel;
3134 break;
3135 case DEVLINK_PARAM_TYPE_U32:
3136 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3137 goto value_nest_cancel;
3138 break;
3139 case DEVLINK_PARAM_TYPE_STRING:
3140 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3141 val.vstr))
3142 goto value_nest_cancel;
3143 break;
3144 case DEVLINK_PARAM_TYPE_BOOL:
3145 if (val.vbool &&
3146 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3147 goto value_nest_cancel;
3148 break;
3149 }
3150
3151 nla_nest_end(msg, param_value_attr);
3152 return 0;
3153
3154value_nest_cancel:
3155 nla_nest_cancel(msg, param_value_attr);
3156nla_put_failure:
3157 return -EMSGSIZE;
3158}
3159
3160static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303161 unsigned int port_index,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003162 struct devlink_param_item *param_item,
3163 enum devlink_command cmd,
3164 u32 portid, u32 seq, int flags)
3165{
3166 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003167 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003168 const struct devlink_param *param = param_item->param;
3169 struct devlink_param_gset_ctx ctx;
3170 struct nlattr *param_values_list;
3171 struct nlattr *param_attr;
3172 int nla_type;
3173 void *hdr;
3174 int err;
3175 int i;
3176
3177 /* Get value from driver part to driverinit configuration mode */
3178 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3179 if (!devlink_param_cmode_is_supported(param, i))
3180 continue;
3181 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3182 if (!param_item->driverinit_value_valid)
3183 return -EOPNOTSUPP;
3184 param_value[i] = param_item->driverinit_value;
3185 } else {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003186 if (!param_item->published)
3187 continue;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003188 ctx.cmode = i;
3189 err = devlink_param_get(devlink, param, &ctx);
3190 if (err)
3191 return err;
3192 param_value[i] = ctx.val;
3193 }
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003194 param_value_set[i] = true;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003195 }
3196
3197 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3198 if (!hdr)
3199 return -EMSGSIZE;
3200
3201 if (devlink_nl_put_handle(msg, devlink))
3202 goto genlmsg_cancel;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303203
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303204 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3205 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3206 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303207 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3208 goto genlmsg_cancel;
3209
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003210 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003211 if (!param_attr)
3212 goto genlmsg_cancel;
3213 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3214 goto param_nest_cancel;
3215 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3216 goto param_nest_cancel;
3217
3218 nla_type = devlink_param_type_to_nla_type(param->type);
3219 if (nla_type < 0)
3220 goto param_nest_cancel;
3221 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3222 goto param_nest_cancel;
3223
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003224 param_values_list = nla_nest_start_noflag(msg,
3225 DEVLINK_ATTR_PARAM_VALUES_LIST);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003226 if (!param_values_list)
3227 goto param_nest_cancel;
3228
3229 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003230 if (!param_value_set[i])
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003231 continue;
3232 err = devlink_nl_param_value_fill_one(msg, param->type,
3233 i, param_value[i]);
3234 if (err)
3235 goto values_list_nest_cancel;
3236 }
3237
3238 nla_nest_end(msg, param_values_list);
3239 nla_nest_end(msg, param_attr);
3240 genlmsg_end(msg, hdr);
3241 return 0;
3242
3243values_list_nest_cancel:
3244 nla_nest_end(msg, param_values_list);
3245param_nest_cancel:
3246 nla_nest_cancel(msg, param_attr);
3247genlmsg_cancel:
3248 genlmsg_cancel(msg, hdr);
3249 return -EMSGSIZE;
3250}
3251
Moshe Shemeshea601e12018-07-04 14:30:32 +03003252static void devlink_param_notify(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303253 unsigned int port_index,
Moshe Shemeshea601e12018-07-04 14:30:32 +03003254 struct devlink_param_item *param_item,
3255 enum devlink_command cmd)
3256{
3257 struct sk_buff *msg;
3258 int err;
3259
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303260 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3261 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3262 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003263
3264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3265 if (!msg)
3266 return;
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303267 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3268 0, 0, 0);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003269 if (err) {
3270 nlmsg_free(msg);
3271 return;
3272 }
3273
3274 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3275 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3276}
3277
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003278static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3279 struct netlink_callback *cb)
3280{
3281 struct devlink_param_item *param_item;
3282 struct devlink *devlink;
3283 int start = cb->args[0];
3284 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003285 int err = 0;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003286
3287 mutex_lock(&devlink_mutex);
3288 list_for_each_entry(devlink, &devlink_list, list) {
3289 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3290 continue;
3291 mutex_lock(&devlink->lock);
3292 list_for_each_entry(param_item, &devlink->param_list, list) {
3293 if (idx < start) {
3294 idx++;
3295 continue;
3296 }
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303297 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003298 DEVLINK_CMD_PARAM_GET,
3299 NETLINK_CB(cb->skb).portid,
3300 cb->nlh->nlmsg_seq,
3301 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303302 if (err && err != -EOPNOTSUPP) {
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003303 mutex_unlock(&devlink->lock);
3304 goto out;
3305 }
3306 idx++;
3307 }
3308 mutex_unlock(&devlink->lock);
3309 }
3310out:
3311 mutex_unlock(&devlink_mutex);
3312
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003313 if (err != -EMSGSIZE)
3314 return err;
3315
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003316 cb->args[0] = idx;
3317 return msg->len;
3318}
3319
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003320static int
3321devlink_param_type_get_from_info(struct genl_info *info,
3322 enum devlink_param_type *param_type)
3323{
3324 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3325 return -EINVAL;
3326
3327 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3328 case NLA_U8:
3329 *param_type = DEVLINK_PARAM_TYPE_U8;
3330 break;
3331 case NLA_U16:
3332 *param_type = DEVLINK_PARAM_TYPE_U16;
3333 break;
3334 case NLA_U32:
3335 *param_type = DEVLINK_PARAM_TYPE_U32;
3336 break;
3337 case NLA_STRING:
3338 *param_type = DEVLINK_PARAM_TYPE_STRING;
3339 break;
3340 case NLA_FLAG:
3341 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3342 break;
3343 default:
3344 return -EINVAL;
3345 }
3346
3347 return 0;
3348}
3349
3350static int
3351devlink_param_value_get_from_info(const struct devlink_param *param,
3352 struct genl_info *info,
3353 union devlink_param_value *value)
3354{
Jakub Kicinski87509392020-03-02 21:05:11 -08003355 struct nlattr *param_data;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003356 int len;
3357
Jakub Kicinski87509392020-03-02 21:05:11 -08003358 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3359
3360 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003361 return -EINVAL;
3362
3363 switch (param->type) {
3364 case DEVLINK_PARAM_TYPE_U8:
Jakub Kicinski87509392020-03-02 21:05:11 -08003365 if (nla_len(param_data) != sizeof(u8))
3366 return -EINVAL;
3367 value->vu8 = nla_get_u8(param_data);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003368 break;
3369 case DEVLINK_PARAM_TYPE_U16:
Jakub Kicinski87509392020-03-02 21:05:11 -08003370 if (nla_len(param_data) != sizeof(u16))
3371 return -EINVAL;
3372 value->vu16 = nla_get_u16(param_data);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003373 break;
3374 case DEVLINK_PARAM_TYPE_U32:
Jakub Kicinski87509392020-03-02 21:05:11 -08003375 if (nla_len(param_data) != sizeof(u32))
3376 return -EINVAL;
3377 value->vu32 = nla_get_u32(param_data);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003378 break;
3379 case DEVLINK_PARAM_TYPE_STRING:
Jakub Kicinski87509392020-03-02 21:05:11 -08003380 len = strnlen(nla_data(param_data), nla_len(param_data));
3381 if (len == nla_len(param_data) ||
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03003382 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003383 return -EINVAL;
Jakub Kicinski87509392020-03-02 21:05:11 -08003384 strcpy(value->vstr, nla_data(param_data));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003385 break;
3386 case DEVLINK_PARAM_TYPE_BOOL:
Jakub Kicinski87509392020-03-02 21:05:11 -08003387 if (param_data && nla_len(param_data))
3388 return -EINVAL;
3389 value->vbool = nla_get_flag(param_data);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003390 break;
3391 }
3392 return 0;
3393}
3394
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003395static struct devlink_param_item *
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303396devlink_param_get_from_info(struct list_head *param_list,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003397 struct genl_info *info)
3398{
3399 char *param_name;
3400
3401 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3402 return NULL;
3403
3404 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303405 return devlink_param_find_by_name(param_list, param_name);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003406}
3407
3408static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3409 struct genl_info *info)
3410{
3411 struct devlink *devlink = info->user_ptr[0];
3412 struct devlink_param_item *param_item;
3413 struct sk_buff *msg;
3414 int err;
3415
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303416 param_item = devlink_param_get_from_info(&devlink->param_list, info);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003417 if (!param_item)
3418 return -EINVAL;
3419
3420 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3421 if (!msg)
3422 return -ENOMEM;
3423
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303424 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003425 DEVLINK_CMD_PARAM_GET,
3426 info->snd_portid, info->snd_seq, 0);
3427 if (err) {
3428 nlmsg_free(msg);
3429 return err;
3430 }
3431
3432 return genlmsg_reply(msg, info);
3433}
3434
Vasundhara Volam9c548732019-01-28 18:00:22 +05303435static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303436 unsigned int port_index,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303437 struct list_head *param_list,
3438 struct genl_info *info,
3439 enum devlink_command cmd)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003440{
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003441 enum devlink_param_type param_type;
3442 struct devlink_param_gset_ctx ctx;
3443 enum devlink_param_cmode cmode;
3444 struct devlink_param_item *param_item;
3445 const struct devlink_param *param;
3446 union devlink_param_value value;
3447 int err = 0;
3448
Vasundhara Volam9c548732019-01-28 18:00:22 +05303449 param_item = devlink_param_get_from_info(param_list, info);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003450 if (!param_item)
3451 return -EINVAL;
3452 param = param_item->param;
3453 err = devlink_param_type_get_from_info(info, &param_type);
3454 if (err)
3455 return err;
3456 if (param_type != param->type)
3457 return -EINVAL;
3458 err = devlink_param_value_get_from_info(param, info, &value);
3459 if (err)
3460 return err;
3461 if (param->validate) {
3462 err = param->validate(devlink, param->id, value, info->extack);
3463 if (err)
3464 return err;
3465 }
3466
3467 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3468 return -EINVAL;
3469 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3470 if (!devlink_param_cmode_is_supported(param, cmode))
3471 return -EOPNOTSUPP;
3472
3473 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
Moshe Shemesh12765342018-10-10 16:09:26 +03003474 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3475 strcpy(param_item->driverinit_value.vstr, value.vstr);
3476 else
3477 param_item->driverinit_value = value;
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003478 param_item->driverinit_value_valid = true;
3479 } else {
3480 if (!param->set)
3481 return -EOPNOTSUPP;
3482 ctx.val = value;
3483 ctx.cmode = cmode;
3484 err = devlink_param_set(devlink, param, &ctx);
3485 if (err)
3486 return err;
3487 }
3488
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303489 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003490 return 0;
3491}
3492
Vasundhara Volam9c548732019-01-28 18:00:22 +05303493static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3494 struct genl_info *info)
3495{
3496 struct devlink *devlink = info->user_ptr[0];
3497
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303498 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303499 info, DEVLINK_CMD_PARAM_NEW);
3500}
3501
Moshe Shemesheabaef12018-07-04 14:30:28 +03003502static int devlink_param_register_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303503 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303504 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303505 const struct devlink_param *param,
3506 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003507{
3508 struct devlink_param_item *param_item;
3509
Vasundhara Volam39e61602019-01-28 18:00:20 +05303510 if (devlink_param_find_by_name(param_list, param->name))
Moshe Shemesheabaef12018-07-04 14:30:28 +03003511 return -EEXIST;
3512
3513 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3514 WARN_ON(param->get || param->set);
3515 else
3516 WARN_ON(!param->get || !param->set);
3517
3518 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3519 if (!param_item)
3520 return -ENOMEM;
3521 param_item->param = param;
3522
Vasundhara Volam39e61602019-01-28 18:00:20 +05303523 list_add_tail(&param_item->list, param_list);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303524 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003525 return 0;
3526}
3527
3528static void devlink_param_unregister_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303529 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303530 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303531 const struct devlink_param *param,
3532 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003533{
3534 struct devlink_param_item *param_item;
3535
Vasundhara Volam39e61602019-01-28 18:00:20 +05303536 param_item = devlink_param_find_by_name(param_list, param->name);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003537 WARN_ON(!param_item);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303538 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003539 list_del(&param_item->list);
3540 kfree(param_item);
3541}
3542
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303543static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3544 struct netlink_callback *cb)
3545{
3546 struct devlink_param_item *param_item;
3547 struct devlink_port *devlink_port;
3548 struct devlink *devlink;
3549 int start = cb->args[0];
3550 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003551 int err = 0;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303552
3553 mutex_lock(&devlink_mutex);
3554 list_for_each_entry(devlink, &devlink_list, list) {
3555 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3556 continue;
3557 mutex_lock(&devlink->lock);
3558 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3559 list_for_each_entry(param_item,
3560 &devlink_port->param_list, list) {
3561 if (idx < start) {
3562 idx++;
3563 continue;
3564 }
3565 err = devlink_nl_param_fill(msg,
3566 devlink_port->devlink,
3567 devlink_port->index, param_item,
3568 DEVLINK_CMD_PORT_PARAM_GET,
3569 NETLINK_CB(cb->skb).portid,
3570 cb->nlh->nlmsg_seq,
3571 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303572 if (err && err != -EOPNOTSUPP) {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303573 mutex_unlock(&devlink->lock);
3574 goto out;
3575 }
3576 idx++;
3577 }
3578 }
3579 mutex_unlock(&devlink->lock);
3580 }
3581out:
3582 mutex_unlock(&devlink_mutex);
3583
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003584 if (err != -EMSGSIZE)
3585 return err;
3586
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303587 cb->args[0] = idx;
3588 return msg->len;
3589}
3590
3591static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3592 struct genl_info *info)
3593{
3594 struct devlink_port *devlink_port = info->user_ptr[0];
3595 struct devlink_param_item *param_item;
3596 struct sk_buff *msg;
3597 int err;
3598
3599 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3600 info);
3601 if (!param_item)
3602 return -EINVAL;
3603
3604 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3605 if (!msg)
3606 return -ENOMEM;
3607
3608 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3609 devlink_port->index, param_item,
3610 DEVLINK_CMD_PORT_PARAM_GET,
3611 info->snd_portid, info->snd_seq, 0);
3612 if (err) {
3613 nlmsg_free(msg);
3614 return err;
3615 }
3616
3617 return genlmsg_reply(msg, info);
3618}
3619
Vasundhara Volam9c548732019-01-28 18:00:22 +05303620static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3621 struct genl_info *info)
3622{
3623 struct devlink_port *devlink_port = info->user_ptr[0];
3624
3625 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303626 devlink_port->index,
3627 &devlink_port->param_list, info,
3628 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam9c548732019-01-28 18:00:22 +05303629}
3630
Alex Veskera006d462018-07-12 15:13:12 +03003631static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3632 struct devlink *devlink,
3633 struct devlink_snapshot *snapshot)
3634{
3635 struct nlattr *snap_attr;
3636 int err;
3637
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003638 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
Alex Veskera006d462018-07-12 15:13:12 +03003639 if (!snap_attr)
3640 return -EINVAL;
3641
3642 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3643 if (err)
3644 goto nla_put_failure;
3645
3646 nla_nest_end(msg, snap_attr);
3647 return 0;
3648
3649nla_put_failure:
3650 nla_nest_cancel(msg, snap_attr);
3651 return err;
3652}
3653
3654static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3655 struct devlink *devlink,
3656 struct devlink_region *region)
3657{
3658 struct devlink_snapshot *snapshot;
3659 struct nlattr *snapshots_attr;
3660 int err;
3661
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003662 snapshots_attr = nla_nest_start_noflag(msg,
3663 DEVLINK_ATTR_REGION_SNAPSHOTS);
Alex Veskera006d462018-07-12 15:13:12 +03003664 if (!snapshots_attr)
3665 return -EINVAL;
3666
3667 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3668 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3669 if (err)
3670 goto nla_put_failure;
3671 }
3672
3673 nla_nest_end(msg, snapshots_attr);
3674 return 0;
3675
3676nla_put_failure:
3677 nla_nest_cancel(msg, snapshots_attr);
3678 return err;
3679}
3680
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003681static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3682 enum devlink_command cmd, u32 portid,
3683 u32 seq, int flags,
3684 struct devlink_region *region)
3685{
3686 void *hdr;
3687 int err;
3688
3689 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3690 if (!hdr)
3691 return -EMSGSIZE;
3692
3693 err = devlink_nl_put_handle(msg, devlink);
3694 if (err)
3695 goto nla_put_failure;
3696
Jacob Kellere8937682020-03-26 11:37:08 -07003697 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003698 if (err)
3699 goto nla_put_failure;
3700
3701 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3702 region->size,
3703 DEVLINK_ATTR_PAD);
3704 if (err)
3705 goto nla_put_failure;
3706
Alex Veskera006d462018-07-12 15:13:12 +03003707 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3708 if (err)
3709 goto nla_put_failure;
3710
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003711 genlmsg_end(msg, hdr);
3712 return 0;
3713
3714nla_put_failure:
3715 genlmsg_cancel(msg, hdr);
3716 return err;
3717}
3718
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003719static struct sk_buff *
3720devlink_nl_region_notify_build(struct devlink_region *region,
3721 struct devlink_snapshot *snapshot,
3722 enum devlink_command cmd, u32 portid, u32 seq)
Alex Vesker866319b2018-07-12 15:13:13 +03003723{
3724 struct devlink *devlink = region->devlink;
3725 struct sk_buff *msg;
3726 void *hdr;
3727 int err;
3728
Alex Vesker866319b2018-07-12 15:13:13 +03003729
3730 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3731 if (!msg)
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003732 return ERR_PTR(-ENOMEM);
Alex Vesker866319b2018-07-12 15:13:13 +03003733
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003734 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
3735 if (!hdr) {
3736 err = -EMSGSIZE;
Alex Vesker866319b2018-07-12 15:13:13 +03003737 goto out_free_msg;
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003738 }
Alex Vesker866319b2018-07-12 15:13:13 +03003739
3740 err = devlink_nl_put_handle(msg, devlink);
3741 if (err)
3742 goto out_cancel_msg;
3743
3744 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
Jacob Kellere8937682020-03-26 11:37:08 -07003745 region->ops->name);
Alex Vesker866319b2018-07-12 15:13:13 +03003746 if (err)
3747 goto out_cancel_msg;
3748
3749 if (snapshot) {
3750 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3751 snapshot->id);
3752 if (err)
3753 goto out_cancel_msg;
3754 } else {
3755 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3756 region->size, DEVLINK_ATTR_PAD);
3757 if (err)
3758 goto out_cancel_msg;
3759 }
3760 genlmsg_end(msg, hdr);
3761
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003762 return msg;
Alex Vesker866319b2018-07-12 15:13:13 +03003763
3764out_cancel_msg:
3765 genlmsg_cancel(msg, hdr);
3766out_free_msg:
3767 nlmsg_free(msg);
Jakub Kicinskidd86fec2020-05-01 09:40:40 -07003768 return ERR_PTR(err);
3769}
3770
3771static void devlink_nl_region_notify(struct devlink_region *region,
3772 struct devlink_snapshot *snapshot,
3773 enum devlink_command cmd)
3774{
3775 struct devlink *devlink = region->devlink;
3776 struct sk_buff *msg;
3777
3778 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3779
3780 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
3781 if (IS_ERR(msg))
3782 return;
3783
3784 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3785 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
Alex Vesker866319b2018-07-12 15:13:13 +03003786}
3787
Jacob Kellercf80fae2020-03-26 11:37:11 -07003788/**
Jacob Keller12102432020-03-26 11:37:15 -07003789 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
3790 * @devlink: devlink instance
3791 * @id: the snapshot id
3792 *
3793 * Track when a new snapshot begins using an id. Load the count for the
3794 * given id from the snapshot xarray, increment it, and store it back.
3795 *
3796 * Called when a new snapshot is created with the given id.
3797 *
3798 * The id *must* have been previously allocated by
3799 * devlink_region_snapshot_id_get().
3800 *
3801 * Returns 0 on success, or an error on failure.
3802 */
3803static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
3804{
3805 unsigned long count;
3806 void *p;
3807
3808 lockdep_assert_held(&devlink->lock);
3809
3810 p = xa_load(&devlink->snapshot_ids, id);
3811 if (WARN_ON(!p))
3812 return -EINVAL;
3813
3814 if (WARN_ON(!xa_is_value(p)))
3815 return -EINVAL;
3816
3817 count = xa_to_value(p);
3818 count++;
3819
3820 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
3821 GFP_KERNEL));
3822}
3823
3824/**
3825 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
3826 * @devlink: devlink instance
3827 * @id: the snapshot id
3828 *
3829 * Track when a snapshot is deleted and stops using an id. Load the count
3830 * for the given id from the snapshot xarray, decrement it, and store it
3831 * back.
3832 *
3833 * If the count reaches zero, erase this id from the xarray, freeing it
3834 * up for future re-use by devlink_region_snapshot_id_get().
3835 *
3836 * Called when a snapshot using the given id is deleted, and when the
3837 * initial allocator of the id is finished using it.
3838 */
3839static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
3840{
3841 unsigned long count;
3842 void *p;
3843
3844 lockdep_assert_held(&devlink->lock);
3845
3846 p = xa_load(&devlink->snapshot_ids, id);
3847 if (WARN_ON(!p))
3848 return;
3849
3850 if (WARN_ON(!xa_is_value(p)))
3851 return;
3852
3853 count = xa_to_value(p);
3854
3855 if (count > 1) {
3856 count--;
3857 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
3858 GFP_KERNEL);
3859 } else {
3860 /* If this was the last user, we can erase this id */
3861 xa_erase(&devlink->snapshot_ids, id);
3862 }
3863}
3864
3865/**
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07003866 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
3867 * @devlink: devlink instance
3868 * @id: the snapshot id
3869 *
3870 * Mark the given snapshot id as used by inserting a zero value into the
3871 * snapshot xarray.
3872 *
3873 * This must be called while holding the devlink instance lock. Unlike
3874 * devlink_snapshot_id_get, the initial reference count is zero, not one.
3875 * It is expected that the id will immediately be used before
3876 * releasing the devlink instance lock.
3877 *
3878 * Returns zero on success, or an error code if the snapshot id could not
3879 * be inserted.
3880 */
3881static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
3882{
3883 lockdep_assert_held(&devlink->lock);
3884
3885 if (WARN_ON(xa_load(&devlink->snapshot_ids, id)))
3886 return -EEXIST;
3887
3888 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
3889 GFP_KERNEL));
3890}
3891
3892/**
Jacob Keller70001082020-03-26 11:37:13 -07003893 * __devlink_region_snapshot_id_get - get snapshot ID
3894 * @devlink: devlink instance
Jacob Keller7ef19d32020-03-26 11:37:14 -07003895 * @id: storage to return snapshot id
Jacob Keller70001082020-03-26 11:37:13 -07003896 *
Jacob Keller7ef19d32020-03-26 11:37:14 -07003897 * Allocates a new snapshot id. Returns zero on success, or a negative
3898 * error on failure. Must be called while holding the devlink instance
3899 * lock.
Jacob Keller12102432020-03-26 11:37:15 -07003900 *
3901 * Snapshot IDs are tracked using an xarray which stores the number of
3902 * users of the snapshot id.
3903 *
3904 * Note that the caller of this function counts as a 'user', in order to
3905 * avoid race conditions. The caller must release its hold on the
3906 * snapshot by using devlink_region_snapshot_id_put.
Jacob Keller70001082020-03-26 11:37:13 -07003907 */
Jacob Keller7ef19d32020-03-26 11:37:14 -07003908static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
Jacob Keller70001082020-03-26 11:37:13 -07003909{
3910 lockdep_assert_held(&devlink->lock);
Jacob Keller7ef19d32020-03-26 11:37:14 -07003911
Jacob Keller12102432020-03-26 11:37:15 -07003912 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
3913 xa_limit_32b, GFP_KERNEL);
Jacob Keller70001082020-03-26 11:37:13 -07003914}
3915
3916/**
Jacob Kellercf80fae2020-03-26 11:37:11 -07003917 * __devlink_region_snapshot_create - create a new snapshot
3918 * This will add a new snapshot of a region. The snapshot
3919 * will be stored on the region struct and can be accessed
3920 * from devlink. This is useful for future analyses of snapshots.
3921 * Multiple snapshots can be created on a region.
3922 * The @snapshot_id should be obtained using the getter function.
3923 *
3924 * Must be called only while holding the devlink instance lock.
3925 *
3926 * @region: devlink region of the snapshot
3927 * @data: snapshot data
3928 * @snapshot_id: snapshot id to be created
3929 */
3930static int
3931__devlink_region_snapshot_create(struct devlink_region *region,
3932 u8 *data, u32 snapshot_id)
3933{
3934 struct devlink *devlink = region->devlink;
3935 struct devlink_snapshot *snapshot;
Jacob Keller12102432020-03-26 11:37:15 -07003936 int err;
Jacob Kellercf80fae2020-03-26 11:37:11 -07003937
3938 lockdep_assert_held(&devlink->lock);
3939
3940 /* check if region can hold one more snapshot */
3941 if (region->cur_snapshots == region->max_snapshots)
Jacob Keller47a39f62020-03-26 11:37:12 -07003942 return -ENOSPC;
Jacob Kellercf80fae2020-03-26 11:37:11 -07003943
3944 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
3945 return -EEXIST;
3946
3947 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
3948 if (!snapshot)
3949 return -ENOMEM;
3950
Jacob Keller12102432020-03-26 11:37:15 -07003951 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
3952 if (err)
3953 goto err_snapshot_id_increment;
3954
Jacob Kellercf80fae2020-03-26 11:37:11 -07003955 snapshot->id = snapshot_id;
3956 snapshot->region = region;
3957 snapshot->data = data;
3958
3959 list_add_tail(&snapshot->list, &region->snapshot_list);
3960
3961 region->cur_snapshots++;
3962
3963 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
3964 return 0;
Jacob Keller12102432020-03-26 11:37:15 -07003965
3966err_snapshot_id_increment:
3967 kfree(snapshot);
3968 return err;
Jacob Kellercf80fae2020-03-26 11:37:11 -07003969}
3970
Jiri Pirko92b49822019-08-12 14:28:31 +02003971static void devlink_region_snapshot_del(struct devlink_region *region,
3972 struct devlink_snapshot *snapshot)
3973{
Jacob Keller12102432020-03-26 11:37:15 -07003974 struct devlink *devlink = region->devlink;
3975
3976 lockdep_assert_held(&devlink->lock);
3977
Jiri Pirko92b49822019-08-12 14:28:31 +02003978 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3979 region->cur_snapshots--;
3980 list_del(&snapshot->list);
Jacob Kellera0a09f62020-03-26 11:37:09 -07003981 region->ops->destructor(snapshot->data);
Jacob Keller12102432020-03-26 11:37:15 -07003982 __devlink_snapshot_id_decrement(devlink, snapshot->id);
Jiri Pirko92b49822019-08-12 14:28:31 +02003983 kfree(snapshot);
3984}
3985
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003986static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3987 struct genl_info *info)
3988{
3989 struct devlink *devlink = info->user_ptr[0];
3990 struct devlink_region *region;
3991 const char *region_name;
3992 struct sk_buff *msg;
3993 int err;
3994
3995 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3996 return -EINVAL;
3997
3998 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3999 region = devlink_region_get_by_name(devlink, region_name);
4000 if (!region)
4001 return -EINVAL;
4002
4003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4004 if (!msg)
4005 return -ENOMEM;
4006
4007 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4008 info->snd_portid, info->snd_seq, 0,
4009 region);
4010 if (err) {
4011 nlmsg_free(msg);
4012 return err;
4013 }
4014
4015 return genlmsg_reply(msg, info);
4016}
4017
4018static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4019 struct netlink_callback *cb)
4020{
4021 struct devlink_region *region;
4022 struct devlink *devlink;
4023 int start = cb->args[0];
4024 int idx = 0;
4025 int err;
4026
4027 mutex_lock(&devlink_mutex);
4028 list_for_each_entry(devlink, &devlink_list, list) {
4029 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4030 continue;
4031
4032 mutex_lock(&devlink->lock);
4033 list_for_each_entry(region, &devlink->region_list, list) {
4034 if (idx < start) {
4035 idx++;
4036 continue;
4037 }
4038 err = devlink_nl_region_fill(msg, devlink,
4039 DEVLINK_CMD_REGION_GET,
4040 NETLINK_CB(cb->skb).portid,
4041 cb->nlh->nlmsg_seq,
4042 NLM_F_MULTI, region);
4043 if (err) {
4044 mutex_unlock(&devlink->lock);
4045 goto out;
4046 }
4047 idx++;
4048 }
4049 mutex_unlock(&devlink->lock);
4050 }
4051out:
4052 mutex_unlock(&devlink_mutex);
4053 cb->args[0] = idx;
4054 return msg->len;
4055}
4056
Alex Vesker866319b2018-07-12 15:13:13 +03004057static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4058 struct genl_info *info)
4059{
4060 struct devlink *devlink = info->user_ptr[0];
4061 struct devlink_snapshot *snapshot;
4062 struct devlink_region *region;
4063 const char *region_name;
4064 u32 snapshot_id;
4065
4066 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4067 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4068 return -EINVAL;
4069
4070 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4071 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4072
4073 region = devlink_region_get_by_name(devlink, region_name);
4074 if (!region)
4075 return -EINVAL;
4076
4077 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4078 if (!snapshot)
4079 return -EINVAL;
4080
Jiri Pirko92b49822019-08-12 14:28:31 +02004081 devlink_region_snapshot_del(region, snapshot);
Alex Vesker866319b2018-07-12 15:13:13 +03004082 return 0;
4083}
4084
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004085static int
4086devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4087{
4088 struct devlink *devlink = info->user_ptr[0];
Jakub Kicinski043b3e22020-05-01 09:40:41 -07004089 struct devlink_snapshot *snapshot;
4090 struct nlattr *snapshot_id_attr;
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004091 struct devlink_region *region;
4092 const char *region_name;
4093 u32 snapshot_id;
4094 u8 *data;
4095 int err;
4096
4097 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4098 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4099 return -EINVAL;
4100 }
4101
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004102 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4103 region = devlink_region_get_by_name(devlink, region_name);
4104 if (!region) {
4105 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4106 return -EINVAL;
4107 }
4108
4109 if (!region->ops->snapshot) {
4110 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4111 return -EOPNOTSUPP;
4112 }
4113
4114 if (region->cur_snapshots == region->max_snapshots) {
4115 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4116 return -ENOSPC;
4117 }
4118
Jakub Kicinski043b3e22020-05-01 09:40:41 -07004119 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4120 if (snapshot_id_attr) {
4121 snapshot_id = nla_get_u32(snapshot_id_attr);
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004122
Jakub Kicinski043b3e22020-05-01 09:40:41 -07004123 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4124 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4125 return -EEXIST;
4126 }
4127
4128 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4129 if (err)
4130 return err;
4131 } else {
4132 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4133 if (err) {
4134 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4135 return err;
4136 }
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004137 }
4138
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004139 err = region->ops->snapshot(devlink, info->extack, &data);
4140 if (err)
4141 goto err_snapshot_capture;
4142
4143 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4144 if (err)
4145 goto err_snapshot_create;
4146
Jakub Kicinski043b3e22020-05-01 09:40:41 -07004147 if (!snapshot_id_attr) {
4148 struct sk_buff *msg;
4149
4150 snapshot = devlink_region_snapshot_get_by_id(region,
4151 snapshot_id);
4152 if (WARN_ON(!snapshot))
4153 return -EINVAL;
4154
4155 msg = devlink_nl_region_notify_build(region, snapshot,
4156 DEVLINK_CMD_REGION_NEW,
4157 info->snd_portid,
4158 info->snd_seq);
4159 err = PTR_ERR_OR_ZERO(msg);
4160 if (err)
4161 goto err_notify;
4162
4163 err = genlmsg_reply(msg, info);
4164 if (err)
4165 goto err_notify;
4166 }
4167
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004168 return 0;
4169
4170err_snapshot_create:
4171 region->ops->destructor(data);
4172err_snapshot_capture:
4173 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4174 return err;
Jakub Kicinski043b3e22020-05-01 09:40:41 -07004175
4176err_notify:
4177 devlink_region_snapshot_del(region, snapshot);
4178 return err;
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07004179}
4180
Alex Vesker4e547952018-07-12 15:13:14 +03004181static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4182 struct devlink *devlink,
4183 u8 *chunk, u32 chunk_size,
4184 u64 addr)
4185{
4186 struct nlattr *chunk_attr;
4187 int err;
4188
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004189 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
Alex Vesker4e547952018-07-12 15:13:14 +03004190 if (!chunk_attr)
4191 return -EINVAL;
4192
4193 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4194 if (err)
4195 goto nla_put_failure;
4196
4197 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4198 DEVLINK_ATTR_PAD);
4199 if (err)
4200 goto nla_put_failure;
4201
4202 nla_nest_end(msg, chunk_attr);
4203 return 0;
4204
4205nla_put_failure:
4206 nla_nest_cancel(msg, chunk_attr);
4207 return err;
4208}
4209
4210#define DEVLINK_REGION_READ_CHUNK_SIZE 256
4211
4212static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4213 struct devlink *devlink,
4214 struct devlink_region *region,
4215 struct nlattr **attrs,
4216 u64 start_offset,
4217 u64 end_offset,
Alex Vesker4e547952018-07-12 15:13:14 +03004218 u64 *new_offset)
4219{
4220 struct devlink_snapshot *snapshot;
4221 u64 curr_offset = start_offset;
4222 u32 snapshot_id;
4223 int err = 0;
4224
4225 *new_offset = start_offset;
4226
4227 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4228 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4229 if (!snapshot)
4230 return -EINVAL;
4231
Alex Vesker4e547952018-07-12 15:13:14 +03004232 while (curr_offset < end_offset) {
4233 u32 data_size;
4234 u8 *data;
4235
4236 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4237 data_size = end_offset - curr_offset;
4238 else
4239 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4240
4241 data = &snapshot->data[curr_offset];
4242 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4243 data, data_size,
4244 curr_offset);
4245 if (err)
4246 break;
4247
4248 curr_offset += data_size;
4249 }
4250 *new_offset = curr_offset;
4251
4252 return err;
4253}
4254
4255static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4256 struct netlink_callback *cb)
4257{
Jiri Pirkoee85da52019-10-05 20:04:42 +02004258 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Jakub Kicinski5a46b062020-05-13 10:28:22 -07004259 u64 ret_offset, start_offset, end_offset = U64_MAX;
Jiri Pirkoee85da52019-10-05 20:04:42 +02004260 struct nlattr **attrs = info->attrs;
Alex Vesker4e547952018-07-12 15:13:14 +03004261 struct devlink_region *region;
4262 struct nlattr *chunks_attr;
4263 const char *region_name;
4264 struct devlink *devlink;
Alex Vesker4e547952018-07-12 15:13:14 +03004265 void *hdr;
4266 int err;
4267
4268 start_offset = *((u64 *)&cb->args[0]);
4269
Parav Panditdac7c082019-02-12 14:24:08 -06004270 mutex_lock(&devlink_mutex);
Alex Vesker4e547952018-07-12 15:13:14 +03004271 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004272 if (IS_ERR(devlink)) {
4273 err = PTR_ERR(devlink);
Parav Panditdac7c082019-02-12 14:24:08 -06004274 goto out_dev;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004275 }
Alex Vesker4e547952018-07-12 15:13:14 +03004276
Alex Vesker4e547952018-07-12 15:13:14 +03004277 mutex_lock(&devlink->lock);
4278
4279 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
Parav Panditfdd41ec2019-02-12 14:23:58 -06004280 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4281 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004282 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004283 }
Alex Vesker4e547952018-07-12 15:13:14 +03004284
4285 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4286 region = devlink_region_get_by_name(devlink, region_name);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004287 if (!region) {
4288 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004289 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004290 }
Alex Vesker4e547952018-07-12 15:13:14 +03004291
Jakub Kicinski5a46b062020-05-13 10:28:22 -07004292 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4293 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4294 if (!start_offset)
4295 start_offset =
4296 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4297
4298 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4299 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4300 }
4301
4302 if (end_offset > region->size)
4303 end_offset = region->size;
4304
Jacob Kellerd5b90e92020-02-04 15:59:50 -08004305 /* return 0 if there is no further data to read */
Jakub Kicinski5a46b062020-05-13 10:28:22 -07004306 if (start_offset == end_offset) {
Jacob Kellerd5b90e92020-02-04 15:59:50 -08004307 err = 0;
4308 goto out_unlock;
4309 }
4310
Alex Vesker4e547952018-07-12 15:13:14 +03004311 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4312 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4313 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004314 if (!hdr) {
4315 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004316 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004317 }
Alex Vesker4e547952018-07-12 15:13:14 +03004318
4319 err = devlink_nl_put_handle(skb, devlink);
4320 if (err)
4321 goto nla_put_failure;
4322
4323 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4324 if (err)
4325 goto nla_put_failure;
4326
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004327 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004328 if (!chunks_attr) {
4329 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004330 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004331 }
Alex Vesker4e547952018-07-12 15:13:14 +03004332
Alex Vesker4e547952018-07-12 15:13:14 +03004333 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4334 region, attrs,
4335 start_offset,
Jakub Kicinski5a46b062020-05-13 10:28:22 -07004336 end_offset, &ret_offset);
Alex Vesker4e547952018-07-12 15:13:14 +03004337
4338 if (err && err != -EMSGSIZE)
4339 goto nla_put_failure;
4340
4341 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06004342 if (ret_offset == start_offset) {
4343 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004344 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004345 }
Alex Vesker4e547952018-07-12 15:13:14 +03004346
4347 *((u64 *)&cb->args[0]) = ret_offset;
4348
4349 nla_nest_end(skb, chunks_attr);
4350 genlmsg_end(skb, hdr);
4351 mutex_unlock(&devlink->lock);
4352 mutex_unlock(&devlink_mutex);
4353
4354 return skb->len;
4355
4356nla_put_failure:
4357 genlmsg_cancel(skb, hdr);
4358out_unlock:
4359 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06004360out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03004361 mutex_unlock(&devlink_mutex);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004362 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03004363}
4364
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004365struct devlink_info_req {
4366 struct sk_buff *msg;
4367};
4368
4369int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
4370{
4371 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
4372}
4373EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
4374
4375int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
4376{
4377 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
4378}
4379EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
4380
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004381static int devlink_info_version_put(struct devlink_info_req *req, int attr,
4382 const char *version_name,
4383 const char *version_value)
4384{
4385 struct nlattr *nest;
4386 int err;
4387
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004388 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004389 if (!nest)
4390 return -EMSGSIZE;
4391
4392 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
4393 version_name);
4394 if (err)
4395 goto nla_put_failure;
4396
4397 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
4398 version_value);
4399 if (err)
4400 goto nla_put_failure;
4401
4402 nla_nest_end(req->msg, nest);
4403
4404 return 0;
4405
4406nla_put_failure:
4407 nla_nest_cancel(req->msg, nest);
4408 return err;
4409}
4410
4411int devlink_info_version_fixed_put(struct devlink_info_req *req,
4412 const char *version_name,
4413 const char *version_value)
4414{
4415 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
4416 version_name, version_value);
4417}
4418EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
4419
4420int devlink_info_version_stored_put(struct devlink_info_req *req,
4421 const char *version_name,
4422 const char *version_value)
4423{
4424 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
4425 version_name, version_value);
4426}
4427EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
4428
4429int devlink_info_version_running_put(struct devlink_info_req *req,
4430 const char *version_name,
4431 const char *version_value)
4432{
4433 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4434 version_name, version_value);
4435}
4436EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4437
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004438static int
4439devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4440 enum devlink_command cmd, u32 portid,
4441 u32 seq, int flags, struct netlink_ext_ack *extack)
4442{
4443 struct devlink_info_req req;
4444 void *hdr;
4445 int err;
4446
4447 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4448 if (!hdr)
4449 return -EMSGSIZE;
4450
4451 err = -EMSGSIZE;
4452 if (devlink_nl_put_handle(msg, devlink))
4453 goto err_cancel_msg;
4454
4455 req.msg = msg;
4456 err = devlink->ops->info_get(devlink, &req, extack);
4457 if (err)
4458 goto err_cancel_msg;
4459
4460 genlmsg_end(msg, hdr);
4461 return 0;
4462
4463err_cancel_msg:
4464 genlmsg_cancel(msg, hdr);
4465 return err;
4466}
4467
4468static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4469 struct genl_info *info)
4470{
4471 struct devlink *devlink = info->user_ptr[0];
4472 struct sk_buff *msg;
4473 int err;
4474
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08004475 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004476 return -EOPNOTSUPP;
4477
4478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4479 if (!msg)
4480 return -ENOMEM;
4481
4482 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4483 info->snd_portid, info->snd_seq, 0,
4484 info->extack);
4485 if (err) {
4486 nlmsg_free(msg);
4487 return err;
4488 }
4489
4490 return genlmsg_reply(msg, info);
4491}
4492
4493static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4494 struct netlink_callback *cb)
4495{
4496 struct devlink *devlink;
4497 int start = cb->args[0];
4498 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004499 int err = 0;
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004500
4501 mutex_lock(&devlink_mutex);
4502 list_for_each_entry(devlink, &devlink_list, list) {
4503 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4504 continue;
4505 if (idx < start) {
4506 idx++;
4507 continue;
4508 }
4509
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004510 if (!devlink->ops->info_get) {
4511 idx++;
4512 continue;
4513 }
4514
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004515 mutex_lock(&devlink->lock);
4516 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4517 NETLINK_CB(cb->skb).portid,
4518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4519 cb->extack);
4520 mutex_unlock(&devlink->lock);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05304521 if (err && err != -EOPNOTSUPP)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004522 break;
4523 idx++;
4524 }
4525 mutex_unlock(&devlink_mutex);
4526
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004527 if (err != -EMSGSIZE)
4528 return err;
4529
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004530 cb->args[0] = idx;
4531 return msg->len;
4532}
4533
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004534struct devlink_fmsg_item {
4535 struct list_head list;
4536 int attrtype;
4537 u8 nla_type;
4538 u16 len;
Gustavo A. R. Silvad2afb412020-02-28 07:43:24 -06004539 int value[];
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004540};
4541
4542struct devlink_fmsg {
4543 struct list_head item_list;
Aya Levin573ed902020-02-11 14:32:42 -08004544 bool putting_binary; /* This flag forces enclosing of binary data
4545 * in an array brackets. It forces using
4546 * of designated API:
4547 * devlink_fmsg_binary_pair_nest_start()
4548 * devlink_fmsg_binary_pair_nest_end()
4549 */
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004550};
4551
4552static struct devlink_fmsg *devlink_fmsg_alloc(void)
4553{
4554 struct devlink_fmsg *fmsg;
4555
4556 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4557 if (!fmsg)
4558 return NULL;
4559
4560 INIT_LIST_HEAD(&fmsg->item_list);
4561
4562 return fmsg;
4563}
4564
4565static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4566{
4567 struct devlink_fmsg_item *item, *tmp;
4568
4569 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4570 list_del(&item->list);
4571 kfree(item);
4572 }
4573 kfree(fmsg);
4574}
4575
4576static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4577 int attrtype)
4578{
4579 struct devlink_fmsg_item *item;
4580
4581 item = kzalloc(sizeof(*item), GFP_KERNEL);
4582 if (!item)
4583 return -ENOMEM;
4584
4585 item->attrtype = attrtype;
4586 list_add_tail(&item->list, &fmsg->item_list);
4587
4588 return 0;
4589}
4590
4591int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4592{
Aya Levin573ed902020-02-11 14:32:42 -08004593 if (fmsg->putting_binary)
4594 return -EINVAL;
4595
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004596 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4597}
4598EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4599
4600static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4601{
Aya Levin573ed902020-02-11 14:32:42 -08004602 if (fmsg->putting_binary)
4603 return -EINVAL;
4604
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004605 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4606}
4607
4608int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4609{
Aya Levin573ed902020-02-11 14:32:42 -08004610 if (fmsg->putting_binary)
4611 return -EINVAL;
4612
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004613 return devlink_fmsg_nest_end(fmsg);
4614}
4615EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4616
4617#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4618
4619static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4620{
4621 struct devlink_fmsg_item *item;
4622
Aya Levin573ed902020-02-11 14:32:42 -08004623 if (fmsg->putting_binary)
4624 return -EINVAL;
4625
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004626 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4627 return -EMSGSIZE;
4628
4629 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4630 if (!item)
4631 return -ENOMEM;
4632
4633 item->nla_type = NLA_NUL_STRING;
4634 item->len = strlen(name) + 1;
4635 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4636 memcpy(&item->value, name, item->len);
4637 list_add_tail(&item->list, &fmsg->item_list);
4638
4639 return 0;
4640}
4641
4642int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4643{
4644 int err;
4645
Aya Levin573ed902020-02-11 14:32:42 -08004646 if (fmsg->putting_binary)
4647 return -EINVAL;
4648
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004649 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4650 if (err)
4651 return err;
4652
4653 err = devlink_fmsg_put_name(fmsg, name);
4654 if (err)
4655 return err;
4656
4657 return 0;
4658}
4659EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4660
4661int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4662{
Aya Levin573ed902020-02-11 14:32:42 -08004663 if (fmsg->putting_binary)
4664 return -EINVAL;
4665
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004666 return devlink_fmsg_nest_end(fmsg);
4667}
4668EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4669
4670int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4671 const char *name)
4672{
4673 int err;
4674
Aya Levin573ed902020-02-11 14:32:42 -08004675 if (fmsg->putting_binary)
4676 return -EINVAL;
4677
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004678 err = devlink_fmsg_pair_nest_start(fmsg, name);
4679 if (err)
4680 return err;
4681
4682 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4683 if (err)
4684 return err;
4685
4686 return 0;
4687}
4688EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4689
4690int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4691{
4692 int err;
4693
Aya Levin573ed902020-02-11 14:32:42 -08004694 if (fmsg->putting_binary)
4695 return -EINVAL;
4696
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004697 err = devlink_fmsg_nest_end(fmsg);
4698 if (err)
4699 return err;
4700
4701 err = devlink_fmsg_nest_end(fmsg);
4702 if (err)
4703 return err;
4704
4705 return 0;
4706}
4707EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4708
Aya Levin573ed902020-02-11 14:32:42 -08004709int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
4710 const char *name)
4711{
4712 int err;
4713
4714 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
4715 if (err)
4716 return err;
4717
4718 fmsg->putting_binary = true;
4719 return err;
4720}
4721EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
4722
4723int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
4724{
4725 if (!fmsg->putting_binary)
4726 return -EINVAL;
4727
4728 fmsg->putting_binary = false;
4729 return devlink_fmsg_arr_pair_nest_end(fmsg);
4730}
4731EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
4732
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004733static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4734 const void *value, u16 value_len,
4735 u8 value_nla_type)
4736{
4737 struct devlink_fmsg_item *item;
4738
4739 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4740 return -EMSGSIZE;
4741
4742 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4743 if (!item)
4744 return -ENOMEM;
4745
4746 item->nla_type = value_nla_type;
4747 item->len = value_len;
4748 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4749 memcpy(&item->value, value, item->len);
4750 list_add_tail(&item->list, &fmsg->item_list);
4751
4752 return 0;
4753}
4754
4755int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4756{
Aya Levin573ed902020-02-11 14:32:42 -08004757 if (fmsg->putting_binary)
4758 return -EINVAL;
4759
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004760 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4761}
4762EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4763
4764int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4765{
Aya Levin573ed902020-02-11 14:32:42 -08004766 if (fmsg->putting_binary)
4767 return -EINVAL;
4768
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004769 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4770}
4771EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4772
4773int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4774{
Aya Levin573ed902020-02-11 14:32:42 -08004775 if (fmsg->putting_binary)
4776 return -EINVAL;
4777
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004778 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4779}
4780EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4781
4782int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4783{
Aya Levin573ed902020-02-11 14:32:42 -08004784 if (fmsg->putting_binary)
4785 return -EINVAL;
4786
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004787 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4788}
4789EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4790
4791int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4792{
Aya Levin573ed902020-02-11 14:32:42 -08004793 if (fmsg->putting_binary)
4794 return -EINVAL;
4795
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004796 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4797 NLA_NUL_STRING);
4798}
4799EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4800
Aya Levin573ed902020-02-11 14:32:42 -08004801int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4802 u16 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004803{
Aya Levin573ed902020-02-11 14:32:42 -08004804 if (!fmsg->putting_binary)
4805 return -EINVAL;
4806
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004807 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4808}
Aya Levin573ed902020-02-11 14:32:42 -08004809EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004810
4811int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4812 bool value)
4813{
4814 int err;
4815
4816 err = devlink_fmsg_pair_nest_start(fmsg, name);
4817 if (err)
4818 return err;
4819
4820 err = devlink_fmsg_bool_put(fmsg, value);
4821 if (err)
4822 return err;
4823
4824 err = devlink_fmsg_pair_nest_end(fmsg);
4825 if (err)
4826 return err;
4827
4828 return 0;
4829}
4830EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4831
4832int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4833 u8 value)
4834{
4835 int err;
4836
4837 err = devlink_fmsg_pair_nest_start(fmsg, name);
4838 if (err)
4839 return err;
4840
4841 err = devlink_fmsg_u8_put(fmsg, value);
4842 if (err)
4843 return err;
4844
4845 err = devlink_fmsg_pair_nest_end(fmsg);
4846 if (err)
4847 return err;
4848
4849 return 0;
4850}
4851EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4852
4853int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4854 u32 value)
4855{
4856 int err;
4857
4858 err = devlink_fmsg_pair_nest_start(fmsg, name);
4859 if (err)
4860 return err;
4861
4862 err = devlink_fmsg_u32_put(fmsg, value);
4863 if (err)
4864 return err;
4865
4866 err = devlink_fmsg_pair_nest_end(fmsg);
4867 if (err)
4868 return err;
4869
4870 return 0;
4871}
4872EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4873
4874int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4875 u64 value)
4876{
4877 int err;
4878
4879 err = devlink_fmsg_pair_nest_start(fmsg, name);
4880 if (err)
4881 return err;
4882
4883 err = devlink_fmsg_u64_put(fmsg, value);
4884 if (err)
4885 return err;
4886
4887 err = devlink_fmsg_pair_nest_end(fmsg);
4888 if (err)
4889 return err;
4890
4891 return 0;
4892}
4893EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4894
4895int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4896 const char *value)
4897{
4898 int err;
4899
4900 err = devlink_fmsg_pair_nest_start(fmsg, name);
4901 if (err)
4902 return err;
4903
4904 err = devlink_fmsg_string_put(fmsg, value);
4905 if (err)
4906 return err;
4907
4908 err = devlink_fmsg_pair_nest_end(fmsg);
4909 if (err)
4910 return err;
4911
4912 return 0;
4913}
4914EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4915
4916int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
Aya Levine2cde862019-11-12 14:07:49 +02004917 const void *value, u32 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004918{
Aya Levine2cde862019-11-12 14:07:49 +02004919 u32 data_size;
Aya Levin573ed902020-02-11 14:32:42 -08004920 int end_err;
Aya Levine2cde862019-11-12 14:07:49 +02004921 u32 offset;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004922 int err;
4923
Aya Levin573ed902020-02-11 14:32:42 -08004924 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004925 if (err)
4926 return err;
4927
Aya Levine2cde862019-11-12 14:07:49 +02004928 for (offset = 0; offset < value_len; offset += data_size) {
4929 data_size = value_len - offset;
4930 if (data_size > DEVLINK_FMSG_MAX_SIZE)
4931 data_size = DEVLINK_FMSG_MAX_SIZE;
4932 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
4933 if (err)
Aya Levin573ed902020-02-11 14:32:42 -08004934 break;
4935 /* Exit from loop with a break (instead of
4936 * return) to make sure putting_binary is turned off in
4937 * devlink_fmsg_binary_pair_nest_end
4938 */
Aya Levine2cde862019-11-12 14:07:49 +02004939 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004940
Aya Levin573ed902020-02-11 14:32:42 -08004941 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
4942 if (end_err)
4943 err = end_err;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004944
Aya Levin573ed902020-02-11 14:32:42 -08004945 return err;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004946}
4947EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4948
4949static int
4950devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4951{
4952 switch (msg->nla_type) {
4953 case NLA_FLAG:
4954 case NLA_U8:
4955 case NLA_U32:
4956 case NLA_U64:
4957 case NLA_NUL_STRING:
4958 case NLA_BINARY:
4959 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4960 msg->nla_type);
4961 default:
4962 return -EINVAL;
4963 }
4964}
4965
4966static int
4967devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4968{
4969 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4970 u8 tmp;
4971
4972 switch (msg->nla_type) {
4973 case NLA_FLAG:
4974 /* Always provide flag data, regardless of its value */
4975 tmp = *(bool *) msg->value;
4976
4977 return nla_put_u8(skb, attrtype, tmp);
4978 case NLA_U8:
4979 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4980 case NLA_U32:
4981 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4982 case NLA_U64:
4983 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4984 DEVLINK_ATTR_PAD);
4985 case NLA_NUL_STRING:
4986 return nla_put_string(skb, attrtype, (char *) &msg->value);
4987 case NLA_BINARY:
4988 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4989 default:
4990 return -EINVAL;
4991 }
4992}
4993
4994static int
4995devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4996 int *start)
4997{
4998 struct devlink_fmsg_item *item;
4999 struct nlattr *fmsg_nlattr;
5000 int i = 0;
5001 int err;
5002
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005003 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02005004 if (!fmsg_nlattr)
5005 return -EMSGSIZE;
5006
5007 list_for_each_entry(item, &fmsg->item_list, list) {
5008 if (i < *start) {
5009 i++;
5010 continue;
5011 }
5012
5013 switch (item->attrtype) {
5014 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5015 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5016 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5017 case DEVLINK_ATTR_FMSG_NEST_END:
5018 err = nla_put_flag(skb, item->attrtype);
5019 break;
5020 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5021 err = devlink_fmsg_item_fill_type(item, skb);
5022 if (err)
5023 break;
5024 err = devlink_fmsg_item_fill_data(item, skb);
5025 break;
5026 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5027 err = nla_put_string(skb, item->attrtype,
5028 (char *) &item->value);
5029 break;
5030 default:
5031 err = -EINVAL;
5032 break;
5033 }
5034 if (!err)
5035 *start = ++i;
5036 else
5037 break;
5038 }
5039
5040 nla_nest_end(skb, fmsg_nlattr);
5041 return err;
5042}
5043
5044static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5045 struct genl_info *info,
5046 enum devlink_command cmd, int flags)
5047{
5048 struct nlmsghdr *nlh;
5049 struct sk_buff *skb;
5050 bool last = false;
5051 int index = 0;
5052 void *hdr;
5053 int err;
5054
5055 while (!last) {
5056 int tmp_index = index;
5057
5058 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5059 if (!skb)
5060 return -ENOMEM;
5061
5062 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5063 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5064 if (!hdr) {
5065 err = -EMSGSIZE;
5066 goto nla_put_failure;
5067 }
5068
5069 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5070 if (!err)
5071 last = true;
5072 else if (err != -EMSGSIZE || tmp_index == index)
5073 goto nla_put_failure;
5074
5075 genlmsg_end(skb, hdr);
5076 err = genlmsg_reply(skb, info);
5077 if (err)
5078 return err;
5079 }
5080
5081 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5082 if (!skb)
5083 return -ENOMEM;
5084 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5085 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5086 if (!nlh) {
5087 err = -EMSGSIZE;
5088 goto nla_put_failure;
5089 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02005090
Li RongQingfde55ea2019-02-11 19:09:07 +08005091 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02005092
5093nla_put_failure:
5094 nlmsg_free(skb);
5095 return err;
5096}
5097
Aya Levine44ef4e2019-05-16 09:49:20 +03005098static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5099 struct netlink_callback *cb,
5100 enum devlink_command cmd)
5101{
5102 int index = cb->args[0];
5103 int tmp_index = index;
5104 void *hdr;
5105 int err;
5106
5107 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5108 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5109 if (!hdr) {
5110 err = -EMSGSIZE;
5111 goto nla_put_failure;
5112 }
5113
5114 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5115 if ((err && err != -EMSGSIZE) || tmp_index == index)
5116 goto nla_put_failure;
5117
5118 cb->args[0] = index;
5119 genlmsg_end(skb, hdr);
5120 return skb->len;
5121
5122nla_put_failure:
5123 genlmsg_cancel(skb, hdr);
5124 return err;
5125}
5126
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005127struct devlink_health_reporter {
5128 struct list_head list;
5129 void *priv;
5130 const struct devlink_health_reporter_ops *ops;
5131 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005132 struct devlink_fmsg *dump_fmsg;
5133 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005134 u64 graceful_period;
5135 bool auto_recover;
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005136 bool auto_dump;
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005137 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005138 u64 dump_ts;
Aya Levind2795052019-11-10 14:11:56 +02005139 u64 dump_real_ts;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005140 u64 error_count;
5141 u64 recovery_count;
5142 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005143 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005144};
5145
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005146void *
5147devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5148{
5149 return reporter->priv;
5150}
5151EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5152
5153static struct devlink_health_reporter *
5154devlink_health_reporter_find_by_name(struct devlink *devlink,
5155 const char *reporter_name)
5156{
5157 struct devlink_health_reporter *reporter;
5158
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005159 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005160 list_for_each_entry(reporter, &devlink->reporter_list, list)
5161 if (!strcmp(reporter->ops->name, reporter_name))
5162 return reporter;
5163 return NULL;
5164}
5165
5166/**
5167 * devlink_health_reporter_create - create devlink health reporter
5168 *
5169 * @devlink: devlink
5170 * @ops: ops
5171 * @graceful_period: to avoid recovery loops, in msecs
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005172 * @priv: priv
5173 */
5174struct devlink_health_reporter *
5175devlink_health_reporter_create(struct devlink *devlink,
5176 const struct devlink_health_reporter_ops *ops,
Eran Ben Elishaba7d16c2020-03-29 14:05:54 +03005177 u64 graceful_period, void *priv)
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005178{
5179 struct devlink_health_reporter *reporter;
5180
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005181 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005182 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5183 reporter = ERR_PTR(-EEXIST);
5184 goto unlock;
5185 }
5186
Eran Ben Elishaba7d16c2020-03-29 14:05:54 +03005187 if (WARN_ON(graceful_period && !ops->recover)) {
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005188 reporter = ERR_PTR(-EINVAL);
5189 goto unlock;
5190 }
5191
5192 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5193 if (!reporter) {
5194 reporter = ERR_PTR(-ENOMEM);
5195 goto unlock;
5196 }
5197
5198 reporter->priv = priv;
5199 reporter->ops = ops;
5200 reporter->devlink = devlink;
5201 reporter->graceful_period = graceful_period;
Eran Ben Elishaba7d16c2020-03-29 14:05:54 +03005202 reporter->auto_recover = !!ops->recover;
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005203 reporter->auto_dump = !!ops->dump;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005204 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005205 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005206 list_add_tail(&reporter->list, &devlink->reporter_list);
5207unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005208 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005209 return reporter;
5210}
5211EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5212
5213/**
5214 * devlink_health_reporter_destroy - destroy devlink health reporter
5215 *
5216 * @reporter: devlink health reporter to destroy
5217 */
5218void
5219devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5220{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005221 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005222 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005223 mutex_unlock(&reporter->devlink->reporters_lock);
5224 while (refcount_read(&reporter->refcount) > 1)
5225 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01005226 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02005227 if (reporter->dump_fmsg)
5228 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005229 kfree(reporter);
5230}
5231EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
5232
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005233static int
5234devlink_nl_health_reporter_fill(struct sk_buff *msg,
5235 struct devlink *devlink,
5236 struct devlink_health_reporter *reporter,
5237 enum devlink_command cmd, u32 portid,
5238 u32 seq, int flags)
5239{
5240 struct nlattr *reporter_attr;
5241 void *hdr;
5242
5243 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5244 if (!hdr)
5245 return -EMSGSIZE;
5246
5247 if (devlink_nl_put_handle(msg, devlink))
5248 goto genlmsg_cancel;
5249
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005250 reporter_attr = nla_nest_start_noflag(msg,
5251 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005252 if (!reporter_attr)
5253 goto genlmsg_cancel;
5254 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
5255 reporter->ops->name))
5256 goto reporter_nest_cancel;
5257 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
5258 reporter->health_state))
5259 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005260 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005261 reporter->error_count, DEVLINK_ATTR_PAD))
5262 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005263 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005264 reporter->recovery_count, DEVLINK_ATTR_PAD))
5265 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005266 if (reporter->ops->recover &&
5267 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005268 reporter->graceful_period,
5269 DEVLINK_ATTR_PAD))
5270 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005271 if (reporter->ops->recover &&
5272 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005273 reporter->auto_recover))
5274 goto reporter_nest_cancel;
5275 if (reporter->dump_fmsg &&
5276 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
5277 jiffies_to_msecs(reporter->dump_ts),
5278 DEVLINK_ATTR_PAD))
5279 goto reporter_nest_cancel;
Aya Levind2795052019-11-10 14:11:56 +02005280 if (reporter->dump_fmsg &&
5281 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
5282 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
5283 goto reporter_nest_cancel;
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005284 if (reporter->ops->dump &&
5285 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
5286 reporter->auto_dump))
5287 goto reporter_nest_cancel;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005288
5289 nla_nest_end(msg, reporter_attr);
5290 genlmsg_end(msg, hdr);
5291 return 0;
5292
5293reporter_nest_cancel:
5294 nla_nest_end(msg, reporter_attr);
5295genlmsg_cancel:
5296 genlmsg_cancel(msg, hdr);
5297 return -EMSGSIZE;
5298}
5299
Vikas Gupta97ff3bd2020-01-02 21:18:10 +05305300static void devlink_recover_notify(struct devlink_health_reporter *reporter,
5301 enum devlink_command cmd)
5302{
5303 struct sk_buff *msg;
5304 int err;
5305
5306 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5307
5308 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5309 if (!msg)
5310 return;
5311
5312 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
5313 reporter, cmd, 0, 0, 0);
5314 if (err) {
5315 nlmsg_free(msg);
5316 return;
5317 }
5318
5319 genlmsg_multicast_netns(&devlink_nl_family,
5320 devlink_net(reporter->devlink),
5321 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5322}
5323
5324void
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005325devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
5326{
5327 reporter->recovery_count++;
5328 reporter->last_recovery_ts = jiffies;
5329}
5330EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
5331
5332static int
5333devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
5334 void *priv_ctx, struct netlink_ext_ack *extack)
5335{
5336 int err;
5337
5338 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
5339 return 0;
5340
5341 if (!reporter->ops->recover)
5342 return -EOPNOTSUPP;
5343
5344 err = reporter->ops->recover(reporter, priv_ctx, extack);
5345 if (err)
5346 return err;
5347
5348 devlink_health_reporter_recovery_done(reporter);
5349 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
5350 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5351
5352 return 0;
5353}
5354
5355static void
5356devlink_health_dump_clear(struct devlink_health_reporter *reporter)
5357{
5358 if (!reporter->dump_fmsg)
5359 return;
5360 devlink_fmsg_free(reporter->dump_fmsg);
5361 reporter->dump_fmsg = NULL;
5362}
5363
5364static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
5365 void *priv_ctx,
5366 struct netlink_ext_ack *extack)
5367{
5368 int err;
5369
5370 if (!reporter->ops->dump)
5371 return 0;
5372
5373 if (reporter->dump_fmsg)
5374 return 0;
5375
5376 reporter->dump_fmsg = devlink_fmsg_alloc();
5377 if (!reporter->dump_fmsg) {
5378 err = -ENOMEM;
5379 return err;
5380 }
5381
5382 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
5383 if (err)
5384 goto dump_err;
5385
5386 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
5387 priv_ctx, extack);
5388 if (err)
5389 goto dump_err;
5390
5391 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
5392 if (err)
5393 goto dump_err;
5394
5395 reporter->dump_ts = jiffies;
5396 reporter->dump_real_ts = ktime_get_real_ns();
5397
5398 return 0;
5399
5400dump_err:
5401 devlink_health_dump_clear(reporter);
5402 return err;
5403}
5404
5405int devlink_health_report(struct devlink_health_reporter *reporter,
5406 const char *msg, void *priv_ctx)
5407{
5408 enum devlink_health_reporter_state prev_health_state;
5409 struct devlink *devlink = reporter->devlink;
Aya Levinbea0c5c2020-05-04 11:27:46 +03005410 unsigned long recover_ts_threshold;
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005411
5412 /* write a log message of the current error */
5413 WARN_ON(!msg);
5414 trace_devlink_health_report(devlink, reporter->ops->name, msg);
5415 reporter->error_count++;
5416 prev_health_state = reporter->health_state;
5417 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5418 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5419
5420 /* abort if the previous error wasn't recovered */
Aya Levinbea0c5c2020-05-04 11:27:46 +03005421 recover_ts_threshold = reporter->last_recovery_ts +
5422 msecs_to_jiffies(reporter->graceful_period);
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005423 if (reporter->auto_recover &&
5424 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
Aya Levinbea0c5c2020-05-04 11:27:46 +03005425 (reporter->last_recovery_ts && reporter->recovery_count &&
5426 time_is_after_jiffies(recover_ts_threshold)))) {
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005427 trace_devlink_health_recover_aborted(devlink,
5428 reporter->ops->name,
5429 reporter->health_state,
5430 jiffies -
5431 reporter->last_recovery_ts);
5432 return -ECANCELED;
5433 }
5434
5435 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5436
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005437 if (reporter->auto_dump) {
5438 mutex_lock(&reporter->dump_lock);
5439 /* store current dump of current error, for later analysis */
5440 devlink_health_do_dump(reporter, priv_ctx, NULL);
5441 mutex_unlock(&reporter->dump_lock);
5442 }
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005443
5444 if (reporter->auto_recover)
5445 return devlink_health_reporter_recover(reporter,
5446 priv_ctx, NULL);
5447
5448 return 0;
5449}
5450EXPORT_SYMBOL_GPL(devlink_health_report);
5451
5452static struct devlink_health_reporter *
5453devlink_health_reporter_get_from_attrs(struct devlink *devlink,
5454 struct nlattr **attrs)
5455{
5456 struct devlink_health_reporter *reporter;
5457 char *reporter_name;
5458
5459 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
5460 return NULL;
5461
5462 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
5463 mutex_lock(&devlink->reporters_lock);
5464 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
5465 if (reporter)
5466 refcount_inc(&reporter->refcount);
5467 mutex_unlock(&devlink->reporters_lock);
5468 return reporter;
5469}
5470
5471static struct devlink_health_reporter *
5472devlink_health_reporter_get_from_info(struct devlink *devlink,
5473 struct genl_info *info)
5474{
5475 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
5476}
5477
5478static struct devlink_health_reporter *
5479devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
5480{
5481 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5482 struct devlink_health_reporter *reporter;
5483 struct nlattr **attrs = info->attrs;
5484 struct devlink *devlink;
5485
5486 mutex_lock(&devlink_mutex);
5487 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5488 if (IS_ERR(devlink))
5489 goto unlock;
5490
5491 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
5492 mutex_unlock(&devlink_mutex);
5493 return reporter;
5494unlock:
5495 mutex_unlock(&devlink_mutex);
5496 return NULL;
5497}
5498
5499static void
5500devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5501{
5502 refcount_dec(&reporter->refcount);
5503}
5504
5505void
Vikas Gupta97ff3bd2020-01-02 21:18:10 +05305506devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
5507 enum devlink_health_reporter_state state)
5508{
5509 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
5510 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
5511 return;
5512
5513 if (reporter->health_state == state)
5514 return;
5515
5516 reporter->health_state = state;
5517 trace_devlink_health_reporter_state_update(reporter->devlink,
5518 reporter->ops->name, state);
5519 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5520}
5521EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
5522
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005523static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
5524 struct genl_info *info)
5525{
5526 struct devlink *devlink = info->user_ptr[0];
5527 struct devlink_health_reporter *reporter;
5528 struct sk_buff *msg;
5529 int err;
5530
5531 reporter = devlink_health_reporter_get_from_info(devlink, info);
5532 if (!reporter)
5533 return -EINVAL;
5534
5535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005536 if (!msg) {
5537 err = -ENOMEM;
5538 goto out;
5539 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005540
5541 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5542 DEVLINK_CMD_HEALTH_REPORTER_GET,
5543 info->snd_portid, info->snd_seq,
5544 0);
5545 if (err) {
5546 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005547 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005548 }
5549
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005550 err = genlmsg_reply(msg, info);
5551out:
5552 devlink_health_reporter_put(reporter);
5553 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005554}
5555
5556static int
5557devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5558 struct netlink_callback *cb)
5559{
5560 struct devlink_health_reporter *reporter;
5561 struct devlink *devlink;
5562 int start = cb->args[0];
5563 int idx = 0;
5564 int err;
5565
5566 mutex_lock(&devlink_mutex);
5567 list_for_each_entry(devlink, &devlink_list, list) {
5568 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5569 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005570 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005571 list_for_each_entry(reporter, &devlink->reporter_list,
5572 list) {
5573 if (idx < start) {
5574 idx++;
5575 continue;
5576 }
5577 err = devlink_nl_health_reporter_fill(msg, devlink,
5578 reporter,
5579 DEVLINK_CMD_HEALTH_REPORTER_GET,
5580 NETLINK_CB(cb->skb).portid,
5581 cb->nlh->nlmsg_seq,
5582 NLM_F_MULTI);
5583 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005584 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005585 goto out;
5586 }
5587 idx++;
5588 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005589 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005590 }
5591out:
5592 mutex_unlock(&devlink_mutex);
5593
5594 cb->args[0] = idx;
5595 return msg->len;
5596}
5597
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005598static int
5599devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5600 struct genl_info *info)
5601{
5602 struct devlink *devlink = info->user_ptr[0];
5603 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005604 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005605
5606 reporter = devlink_health_reporter_get_from_info(devlink, info);
5607 if (!reporter)
5608 return -EINVAL;
5609
5610 if (!reporter->ops->recover &&
5611 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005612 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5613 err = -EOPNOTSUPP;
5614 goto out;
5615 }
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005616 if (!reporter->ops->dump &&
5617 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
5618 err = -EOPNOTSUPP;
5619 goto out;
5620 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005621
5622 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5623 reporter->graceful_period =
5624 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5625
5626 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5627 reporter->auto_recover =
5628 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5629
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03005630 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
5631 reporter->auto_dump =
5632 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
5633
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005634 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005635 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005636out:
5637 devlink_health_reporter_put(reporter);
5638 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005639}
5640
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005641static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5642 struct genl_info *info)
5643{
5644 struct devlink *devlink = info->user_ptr[0];
5645 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005646 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005647
5648 reporter = devlink_health_reporter_get_from_info(devlink, info);
5649 if (!reporter)
5650 return -EINVAL;
5651
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005652 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005653
5654 devlink_health_reporter_put(reporter);
5655 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005656}
5657
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005658static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5659 struct genl_info *info)
5660{
5661 struct devlink *devlink = info->user_ptr[0];
5662 struct devlink_health_reporter *reporter;
5663 struct devlink_fmsg *fmsg;
5664 int err;
5665
5666 reporter = devlink_health_reporter_get_from_info(devlink, info);
5667 if (!reporter)
5668 return -EINVAL;
5669
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005670 if (!reporter->ops->diagnose) {
5671 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005672 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005673 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005674
5675 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005676 if (!fmsg) {
5677 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005678 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005679 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005680
5681 err = devlink_fmsg_obj_nest_start(fmsg);
5682 if (err)
5683 goto out;
5684
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005685 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005686 if (err)
5687 goto out;
5688
5689 err = devlink_fmsg_obj_nest_end(fmsg);
5690 if (err)
5691 goto out;
5692
5693 err = devlink_fmsg_snd(fmsg, info,
5694 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5695
5696out:
5697 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005698 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005699 return err;
5700}
5701
Aya Levine44ef4e2019-05-16 09:49:20 +03005702static int
5703devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5704 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005705{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005706 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005707 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005708 int err;
5709
Aya Levine44ef4e2019-05-16 09:49:20 +03005710 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005711 if (!reporter)
5712 return -EINVAL;
5713
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005714 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005715 err = -EOPNOTSUPP;
5716 goto out;
5717 }
5718 mutex_lock(&reporter->dump_lock);
5719 if (!start) {
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005720 err = devlink_health_do_dump(reporter, NULL, cb->extack);
Aya Levine44ef4e2019-05-16 09:49:20 +03005721 if (err)
5722 goto unlock;
5723 cb->args[1] = reporter->dump_ts;
5724 }
5725 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5726 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5727 err = -EAGAIN;
5728 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005729 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005730
Aya Levine44ef4e2019-05-16 09:49:20 +03005731 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5732 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5733unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005734 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005735out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005736 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005737 return err;
5738}
5739
5740static int
5741devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5742 struct genl_info *info)
5743{
5744 struct devlink *devlink = info->user_ptr[0];
5745 struct devlink_health_reporter *reporter;
5746
5747 reporter = devlink_health_reporter_get_from_info(devlink, info);
5748 if (!reporter)
5749 return -EINVAL;
5750
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005751 if (!reporter->ops->dump) {
5752 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005753 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005754 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005755
5756 mutex_lock(&reporter->dump_lock);
5757 devlink_health_dump_clear(reporter);
5758 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005759 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005760 return 0;
5761}
5762
Ido Schimmel0f420b62019-08-17 16:28:17 +03005763struct devlink_stats {
5764 u64 rx_bytes;
5765 u64 rx_packets;
5766 struct u64_stats_sync syncp;
5767};
5768
5769/**
Ido Schimmel1e8c6612020-03-30 22:38:18 +03005770 * struct devlink_trap_policer_item - Packet trap policer attributes.
5771 * @policer: Immutable packet trap policer attributes.
5772 * @rate: Rate in packets / sec.
5773 * @burst: Burst size in packets.
5774 * @list: trap_policer_list member.
5775 *
5776 * Describes packet trap policer attributes. Created by devlink during trap
5777 * policer registration.
5778 */
5779struct devlink_trap_policer_item {
5780 const struct devlink_trap_policer *policer;
5781 u64 rate;
5782 u64 burst;
5783 struct list_head list;
5784};
5785
5786/**
Ido Schimmel0f420b62019-08-17 16:28:17 +03005787 * struct devlink_trap_group_item - Packet trap group attributes.
5788 * @group: Immutable packet trap group attributes.
Ido Schimmelf9f54392020-03-30 22:38:21 +03005789 * @policer_item: Associated policer item. Can be NULL.
Ido Schimmel0f420b62019-08-17 16:28:17 +03005790 * @list: trap_group_list member.
5791 * @stats: Trap group statistics.
5792 *
5793 * Describes packet trap group attributes. Created by devlink during trap
Ido Schimmela09b37f2020-03-22 20:48:29 +02005794 * group registration.
Ido Schimmel0f420b62019-08-17 16:28:17 +03005795 */
5796struct devlink_trap_group_item {
5797 const struct devlink_trap_group *group;
Ido Schimmelf9f54392020-03-30 22:38:21 +03005798 struct devlink_trap_policer_item *policer_item;
Ido Schimmel0f420b62019-08-17 16:28:17 +03005799 struct list_head list;
5800 struct devlink_stats __percpu *stats;
5801};
5802
5803/**
5804 * struct devlink_trap_item - Packet trap attributes.
5805 * @trap: Immutable packet trap attributes.
5806 * @group_item: Associated group item.
5807 * @list: trap_list member.
5808 * @action: Trap action.
5809 * @stats: Trap statistics.
5810 * @priv: Driver private information.
5811 *
5812 * Describes both mutable and immutable packet trap attributes. Created by
5813 * devlink during trap registration and used for all trap related operations.
5814 */
5815struct devlink_trap_item {
5816 const struct devlink_trap *trap;
5817 struct devlink_trap_group_item *group_item;
5818 struct list_head list;
5819 enum devlink_trap_action action;
5820 struct devlink_stats __percpu *stats;
5821 void *priv;
5822};
5823
Ido Schimmel1e8c6612020-03-30 22:38:18 +03005824static struct devlink_trap_policer_item *
5825devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
5826{
5827 struct devlink_trap_policer_item *policer_item;
5828
5829 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
5830 if (policer_item->policer->id == id)
5831 return policer_item;
5832 }
5833
5834 return NULL;
5835}
5836
Ido Schimmel0f420b62019-08-17 16:28:17 +03005837static struct devlink_trap_item *
5838devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5839{
5840 struct devlink_trap_item *trap_item;
5841
5842 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5843 if (!strcmp(trap_item->trap->name, name))
5844 return trap_item;
5845 }
5846
5847 return NULL;
5848}
5849
5850static struct devlink_trap_item *
5851devlink_trap_item_get_from_info(struct devlink *devlink,
5852 struct genl_info *info)
5853{
5854 struct nlattr *attr;
5855
5856 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5857 return NULL;
5858 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5859
5860 return devlink_trap_item_lookup(devlink, nla_data(attr));
5861}
5862
5863static int
5864devlink_trap_action_get_from_info(struct genl_info *info,
5865 enum devlink_trap_action *p_trap_action)
5866{
5867 u8 val;
5868
5869 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5870 switch (val) {
5871 case DEVLINK_TRAP_ACTION_DROP: /* fall-through */
Ido Schimmel9eefeab2020-05-29 21:36:39 +03005872 case DEVLINK_TRAP_ACTION_TRAP: /* fall-through */
5873 case DEVLINK_TRAP_ACTION_MIRROR:
Ido Schimmel0f420b62019-08-17 16:28:17 +03005874 *p_trap_action = val;
5875 break;
5876 default:
5877 return -EINVAL;
5878 }
5879
5880 return 0;
5881}
5882
5883static int devlink_trap_metadata_put(struct sk_buff *msg,
5884 const struct devlink_trap *trap)
5885{
5886 struct nlattr *attr;
5887
5888 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5889 if (!attr)
5890 return -EMSGSIZE;
5891
5892 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5893 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5894 goto nla_put_failure;
Jiri Pirko85b05892020-02-25 11:45:19 +01005895 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
5896 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
5897 goto nla_put_failure;
Ido Schimmel0f420b62019-08-17 16:28:17 +03005898
5899 nla_nest_end(msg, attr);
5900
5901 return 0;
5902
5903nla_put_failure:
5904 nla_nest_cancel(msg, attr);
5905 return -EMSGSIZE;
5906}
5907
5908static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5909 struct devlink_stats *stats)
5910{
5911 int i;
5912
5913 memset(stats, 0, sizeof(*stats));
5914 for_each_possible_cpu(i) {
5915 struct devlink_stats *cpu_stats;
5916 u64 rx_packets, rx_bytes;
5917 unsigned int start;
5918
5919 cpu_stats = per_cpu_ptr(trap_stats, i);
5920 do {
5921 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5922 rx_packets = cpu_stats->rx_packets;
5923 rx_bytes = cpu_stats->rx_bytes;
5924 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5925
5926 stats->rx_packets += rx_packets;
5927 stats->rx_bytes += rx_bytes;
5928 }
5929}
5930
5931static int devlink_trap_stats_put(struct sk_buff *msg,
5932 struct devlink_stats __percpu *trap_stats)
5933{
5934 struct devlink_stats stats;
5935 struct nlattr *attr;
5936
5937 devlink_trap_stats_read(trap_stats, &stats);
5938
5939 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5940 if (!attr)
5941 return -EMSGSIZE;
5942
5943 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5944 stats.rx_packets, DEVLINK_ATTR_PAD))
5945 goto nla_put_failure;
5946
5947 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5948 stats.rx_bytes, DEVLINK_ATTR_PAD))
5949 goto nla_put_failure;
5950
5951 nla_nest_end(msg, attr);
5952
5953 return 0;
5954
5955nla_put_failure:
5956 nla_nest_cancel(msg, attr);
5957 return -EMSGSIZE;
5958}
5959
5960static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5961 const struct devlink_trap_item *trap_item,
5962 enum devlink_command cmd, u32 portid, u32 seq,
5963 int flags)
5964{
5965 struct devlink_trap_group_item *group_item = trap_item->group_item;
5966 void *hdr;
5967 int err;
5968
5969 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5970 if (!hdr)
5971 return -EMSGSIZE;
5972
5973 if (devlink_nl_put_handle(msg, devlink))
5974 goto nla_put_failure;
5975
5976 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5977 group_item->group->name))
5978 goto nla_put_failure;
5979
5980 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5981 goto nla_put_failure;
5982
5983 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5984 goto nla_put_failure;
5985
5986 if (trap_item->trap->generic &&
5987 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5988 goto nla_put_failure;
5989
5990 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5991 goto nla_put_failure;
5992
5993 err = devlink_trap_metadata_put(msg, trap_item->trap);
5994 if (err)
5995 goto nla_put_failure;
5996
5997 err = devlink_trap_stats_put(msg, trap_item->stats);
5998 if (err)
5999 goto nla_put_failure;
6000
6001 genlmsg_end(msg, hdr);
6002
6003 return 0;
6004
6005nla_put_failure:
6006 genlmsg_cancel(msg, hdr);
6007 return -EMSGSIZE;
6008}
6009
6010static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6011 struct genl_info *info)
6012{
6013 struct netlink_ext_ack *extack = info->extack;
6014 struct devlink *devlink = info->user_ptr[0];
6015 struct devlink_trap_item *trap_item;
6016 struct sk_buff *msg;
6017 int err;
6018
6019 if (list_empty(&devlink->trap_list))
6020 return -EOPNOTSUPP;
6021
6022 trap_item = devlink_trap_item_get_from_info(devlink, info);
6023 if (!trap_item) {
6024 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6025 return -ENOENT;
6026 }
6027
6028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6029 if (!msg)
6030 return -ENOMEM;
6031
6032 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6033 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6034 info->snd_seq, 0);
6035 if (err)
6036 goto err_trap_fill;
6037
6038 return genlmsg_reply(msg, info);
6039
6040err_trap_fill:
6041 nlmsg_free(msg);
6042 return err;
6043}
6044
6045static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6046 struct netlink_callback *cb)
6047{
6048 struct devlink_trap_item *trap_item;
6049 struct devlink *devlink;
6050 int start = cb->args[0];
6051 int idx = 0;
6052 int err;
6053
6054 mutex_lock(&devlink_mutex);
6055 list_for_each_entry(devlink, &devlink_list, list) {
6056 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6057 continue;
6058 mutex_lock(&devlink->lock);
6059 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6060 if (idx < start) {
6061 idx++;
6062 continue;
6063 }
6064 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6065 DEVLINK_CMD_TRAP_NEW,
6066 NETLINK_CB(cb->skb).portid,
6067 cb->nlh->nlmsg_seq,
6068 NLM_F_MULTI);
6069 if (err) {
6070 mutex_unlock(&devlink->lock);
6071 goto out;
6072 }
6073 idx++;
6074 }
6075 mutex_unlock(&devlink->lock);
6076 }
6077out:
6078 mutex_unlock(&devlink_mutex);
6079
6080 cb->args[0] = idx;
6081 return msg->len;
6082}
6083
6084static int __devlink_trap_action_set(struct devlink *devlink,
6085 struct devlink_trap_item *trap_item,
6086 enum devlink_trap_action trap_action,
6087 struct netlink_ext_ack *extack)
6088{
6089 int err;
6090
6091 if (trap_item->action != trap_action &&
6092 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6093 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6094 return 0;
6095 }
6096
6097 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6098 trap_action);
6099 if (err)
6100 return err;
6101
6102 trap_item->action = trap_action;
6103
6104 return 0;
6105}
6106
6107static int devlink_trap_action_set(struct devlink *devlink,
6108 struct devlink_trap_item *trap_item,
6109 struct genl_info *info)
6110{
6111 enum devlink_trap_action trap_action;
6112 int err;
6113
6114 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6115 return 0;
6116
6117 err = devlink_trap_action_get_from_info(info, &trap_action);
6118 if (err) {
6119 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6120 return -EINVAL;
6121 }
6122
6123 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6124 info->extack);
6125}
6126
6127static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6128 struct genl_info *info)
6129{
6130 struct netlink_ext_ack *extack = info->extack;
6131 struct devlink *devlink = info->user_ptr[0];
6132 struct devlink_trap_item *trap_item;
6133 int err;
6134
6135 if (list_empty(&devlink->trap_list))
6136 return -EOPNOTSUPP;
6137
6138 trap_item = devlink_trap_item_get_from_info(devlink, info);
6139 if (!trap_item) {
6140 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6141 return -ENOENT;
6142 }
6143
6144 err = devlink_trap_action_set(devlink, trap_item, info);
6145 if (err)
6146 return err;
6147
6148 return 0;
6149}
6150
6151static struct devlink_trap_group_item *
6152devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
6153{
6154 struct devlink_trap_group_item *group_item;
6155
6156 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6157 if (!strcmp(group_item->group->name, name))
6158 return group_item;
6159 }
6160
6161 return NULL;
6162}
6163
6164static struct devlink_trap_group_item *
Ido Schimmel107f1672020-03-22 20:48:30 +02006165devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
6166{
6167 struct devlink_trap_group_item *group_item;
6168
6169 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6170 if (group_item->group->id == id)
6171 return group_item;
6172 }
6173
6174 return NULL;
6175}
6176
6177static struct devlink_trap_group_item *
Ido Schimmel0f420b62019-08-17 16:28:17 +03006178devlink_trap_group_item_get_from_info(struct devlink *devlink,
6179 struct genl_info *info)
6180{
6181 char *name;
6182
6183 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
6184 return NULL;
6185 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
6186
6187 return devlink_trap_group_item_lookup(devlink, name);
6188}
6189
6190static int
6191devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
6192 const struct devlink_trap_group_item *group_item,
6193 enum devlink_command cmd, u32 portid, u32 seq,
6194 int flags)
6195{
6196 void *hdr;
6197 int err;
6198
6199 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6200 if (!hdr)
6201 return -EMSGSIZE;
6202
6203 if (devlink_nl_put_handle(msg, devlink))
6204 goto nla_put_failure;
6205
6206 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6207 group_item->group->name))
6208 goto nla_put_failure;
6209
6210 if (group_item->group->generic &&
6211 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6212 goto nla_put_failure;
6213
Ido Schimmelf9f54392020-03-30 22:38:21 +03006214 if (group_item->policer_item &&
6215 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
6216 group_item->policer_item->policer->id))
6217 goto nla_put_failure;
6218
Ido Schimmel0f420b62019-08-17 16:28:17 +03006219 err = devlink_trap_stats_put(msg, group_item->stats);
6220 if (err)
6221 goto nla_put_failure;
6222
6223 genlmsg_end(msg, hdr);
6224
6225 return 0;
6226
6227nla_put_failure:
6228 genlmsg_cancel(msg, hdr);
6229 return -EMSGSIZE;
6230}
6231
6232static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
6233 struct genl_info *info)
6234{
6235 struct netlink_ext_ack *extack = info->extack;
6236 struct devlink *devlink = info->user_ptr[0];
6237 struct devlink_trap_group_item *group_item;
6238 struct sk_buff *msg;
6239 int err;
6240
6241 if (list_empty(&devlink->trap_group_list))
6242 return -EOPNOTSUPP;
6243
6244 group_item = devlink_trap_group_item_get_from_info(devlink, info);
6245 if (!group_item) {
6246 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
6247 return -ENOENT;
6248 }
6249
6250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6251 if (!msg)
6252 return -ENOMEM;
6253
6254 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
6255 DEVLINK_CMD_TRAP_GROUP_NEW,
6256 info->snd_portid, info->snd_seq, 0);
6257 if (err)
6258 goto err_trap_group_fill;
6259
6260 return genlmsg_reply(msg, info);
6261
6262err_trap_group_fill:
6263 nlmsg_free(msg);
6264 return err;
6265}
6266
6267static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
6268 struct netlink_callback *cb)
6269{
6270 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
6271 struct devlink_trap_group_item *group_item;
6272 u32 portid = NETLINK_CB(cb->skb).portid;
6273 struct devlink *devlink;
6274 int start = cb->args[0];
6275 int idx = 0;
6276 int err;
6277
6278 mutex_lock(&devlink_mutex);
6279 list_for_each_entry(devlink, &devlink_list, list) {
6280 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6281 continue;
6282 mutex_lock(&devlink->lock);
6283 list_for_each_entry(group_item, &devlink->trap_group_list,
6284 list) {
6285 if (idx < start) {
6286 idx++;
6287 continue;
6288 }
6289 err = devlink_nl_trap_group_fill(msg, devlink,
6290 group_item, cmd,
6291 portid,
6292 cb->nlh->nlmsg_seq,
6293 NLM_F_MULTI);
6294 if (err) {
6295 mutex_unlock(&devlink->lock);
6296 goto out;
6297 }
6298 idx++;
6299 }
6300 mutex_unlock(&devlink->lock);
6301 }
6302out:
6303 mutex_unlock(&devlink_mutex);
6304
6305 cb->args[0] = idx;
6306 return msg->len;
6307}
6308
6309static int
6310__devlink_trap_group_action_set(struct devlink *devlink,
6311 struct devlink_trap_group_item *group_item,
6312 enum devlink_trap_action trap_action,
6313 struct netlink_ext_ack *extack)
6314{
6315 const char *group_name = group_item->group->name;
6316 struct devlink_trap_item *trap_item;
6317 int err;
6318
6319 list_for_each_entry(trap_item, &devlink->trap_list, list) {
Ido Schimmel107f1672020-03-22 20:48:30 +02006320 if (strcmp(trap_item->group_item->group->name, group_name))
Ido Schimmel0f420b62019-08-17 16:28:17 +03006321 continue;
6322 err = __devlink_trap_action_set(devlink, trap_item,
6323 trap_action, extack);
6324 if (err)
6325 return err;
6326 }
6327
6328 return 0;
6329}
6330
6331static int
6332devlink_trap_group_action_set(struct devlink *devlink,
6333 struct devlink_trap_group_item *group_item,
Ido Schimmelc0648752020-03-30 22:38:22 +03006334 struct genl_info *info, bool *p_modified)
Ido Schimmel0f420b62019-08-17 16:28:17 +03006335{
6336 enum devlink_trap_action trap_action;
6337 int err;
6338
6339 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6340 return 0;
6341
6342 err = devlink_trap_action_get_from_info(info, &trap_action);
6343 if (err) {
6344 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6345 return -EINVAL;
6346 }
6347
6348 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
6349 info->extack);
6350 if (err)
6351 return err;
6352
Ido Schimmelc0648752020-03-30 22:38:22 +03006353 *p_modified = true;
6354
6355 return 0;
6356}
6357
6358static int devlink_trap_group_set(struct devlink *devlink,
6359 struct devlink_trap_group_item *group_item,
6360 struct genl_info *info)
6361{
6362 struct devlink_trap_policer_item *policer_item;
6363 struct netlink_ext_ack *extack = info->extack;
6364 const struct devlink_trap_policer *policer;
6365 struct nlattr **attrs = info->attrs;
6366 int err;
6367
6368 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6369 return 0;
6370
6371 if (!devlink->ops->trap_group_set)
6372 return -EOPNOTSUPP;
6373
6374 policer_item = group_item->policer_item;
6375 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
6376 u32 policer_id;
6377
6378 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6379 policer_item = devlink_trap_policer_item_lookup(devlink,
6380 policer_id);
6381 if (policer_id && !policer_item) {
6382 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
6383 return -ENOENT;
6384 }
6385 }
6386 policer = policer_item ? policer_item->policer : NULL;
6387
6388 err = devlink->ops->trap_group_set(devlink, group_item->group, policer);
6389 if (err)
6390 return err;
6391
6392 group_item->policer_item = policer_item;
6393
Ido Schimmel0f420b62019-08-17 16:28:17 +03006394 return 0;
6395}
6396
6397static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
6398 struct genl_info *info)
6399{
6400 struct netlink_ext_ack *extack = info->extack;
6401 struct devlink *devlink = info->user_ptr[0];
6402 struct devlink_trap_group_item *group_item;
Ido Schimmelc0648752020-03-30 22:38:22 +03006403 bool modified = false;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006404 int err;
6405
6406 if (list_empty(&devlink->trap_group_list))
6407 return -EOPNOTSUPP;
6408
6409 group_item = devlink_trap_group_item_get_from_info(devlink, info);
6410 if (!group_item) {
6411 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
6412 return -ENOENT;
6413 }
6414
Ido Schimmelc0648752020-03-30 22:38:22 +03006415 err = devlink_trap_group_action_set(devlink, group_item, info,
6416 &modified);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006417 if (err)
6418 return err;
6419
Ido Schimmelc0648752020-03-30 22:38:22 +03006420 err = devlink_trap_group_set(devlink, group_item, info);
6421 if (err)
6422 goto err_trap_group_set;
6423
Ido Schimmel0f420b62019-08-17 16:28:17 +03006424 return 0;
Ido Schimmelc0648752020-03-30 22:38:22 +03006425
6426err_trap_group_set:
6427 if (modified)
6428 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
6429 return err;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006430}
6431
Ido Schimmel1e8c6612020-03-30 22:38:18 +03006432static struct devlink_trap_policer_item *
6433devlink_trap_policer_item_get_from_info(struct devlink *devlink,
6434 struct genl_info *info)
6435{
6436 u32 id;
6437
6438 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6439 return NULL;
6440 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6441
6442 return devlink_trap_policer_item_lookup(devlink, id);
6443}
6444
6445static int
6446devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
6447 const struct devlink_trap_policer *policer)
6448{
6449 struct nlattr *attr;
6450 u64 drops;
6451 int err;
6452
6453 if (!devlink->ops->trap_policer_counter_get)
6454 return 0;
6455
6456 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
6457 if (err)
6458 return err;
6459
6460 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6461 if (!attr)
6462 return -EMSGSIZE;
6463
6464 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
6465 DEVLINK_ATTR_PAD))
6466 goto nla_put_failure;
6467
6468 nla_nest_end(msg, attr);
6469
6470 return 0;
6471
6472nla_put_failure:
6473 nla_nest_cancel(msg, attr);
6474 return -EMSGSIZE;
6475}
6476
6477static int
6478devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
6479 const struct devlink_trap_policer_item *policer_item,
6480 enum devlink_command cmd, u32 portid, u32 seq,
6481 int flags)
6482{
6483 void *hdr;
6484 int err;
6485
6486 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6487 if (!hdr)
6488 return -EMSGSIZE;
6489
6490 if (devlink_nl_put_handle(msg, devlink))
6491 goto nla_put_failure;
6492
6493 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
6494 policer_item->policer->id))
6495 goto nla_put_failure;
6496
6497 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
6498 policer_item->rate, DEVLINK_ATTR_PAD))
6499 goto nla_put_failure;
6500
6501 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
6502 policer_item->burst, DEVLINK_ATTR_PAD))
6503 goto nla_put_failure;
6504
6505 err = devlink_trap_policer_stats_put(msg, devlink,
6506 policer_item->policer);
6507 if (err)
6508 goto nla_put_failure;
6509
6510 genlmsg_end(msg, hdr);
6511
6512 return 0;
6513
6514nla_put_failure:
6515 genlmsg_cancel(msg, hdr);
6516 return -EMSGSIZE;
6517}
6518
6519static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
6520 struct genl_info *info)
6521{
6522 struct devlink_trap_policer_item *policer_item;
6523 struct netlink_ext_ack *extack = info->extack;
6524 struct devlink *devlink = info->user_ptr[0];
6525 struct sk_buff *msg;
6526 int err;
6527
6528 if (list_empty(&devlink->trap_policer_list))
6529 return -EOPNOTSUPP;
6530
6531 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
6532 if (!policer_item) {
6533 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
6534 return -ENOENT;
6535 }
6536
6537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6538 if (!msg)
6539 return -ENOMEM;
6540
6541 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
6542 DEVLINK_CMD_TRAP_POLICER_NEW,
6543 info->snd_portid, info->snd_seq, 0);
6544 if (err)
6545 goto err_trap_policer_fill;
6546
6547 return genlmsg_reply(msg, info);
6548
6549err_trap_policer_fill:
6550 nlmsg_free(msg);
6551 return err;
6552}
6553
6554static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
6555 struct netlink_callback *cb)
6556{
6557 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
6558 struct devlink_trap_policer_item *policer_item;
6559 u32 portid = NETLINK_CB(cb->skb).portid;
6560 struct devlink *devlink;
6561 int start = cb->args[0];
6562 int idx = 0;
6563 int err;
6564
6565 mutex_lock(&devlink_mutex);
6566 list_for_each_entry(devlink, &devlink_list, list) {
6567 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6568 continue;
6569 mutex_lock(&devlink->lock);
6570 list_for_each_entry(policer_item, &devlink->trap_policer_list,
6571 list) {
6572 if (idx < start) {
6573 idx++;
6574 continue;
6575 }
6576 err = devlink_nl_trap_policer_fill(msg, devlink,
6577 policer_item, cmd,
6578 portid,
6579 cb->nlh->nlmsg_seq,
6580 NLM_F_MULTI);
6581 if (err) {
6582 mutex_unlock(&devlink->lock);
6583 goto out;
6584 }
6585 idx++;
6586 }
6587 mutex_unlock(&devlink->lock);
6588 }
6589out:
6590 mutex_unlock(&devlink_mutex);
6591
6592 cb->args[0] = idx;
6593 return msg->len;
6594}
6595
6596static int
6597devlink_trap_policer_set(struct devlink *devlink,
6598 struct devlink_trap_policer_item *policer_item,
6599 struct genl_info *info)
6600{
6601 struct netlink_ext_ack *extack = info->extack;
6602 struct nlattr **attrs = info->attrs;
6603 u64 rate, burst;
6604 int err;
6605
6606 rate = policer_item->rate;
6607 burst = policer_item->burst;
6608
6609 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
6610 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
6611
6612 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
6613 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
6614
6615 if (rate < policer_item->policer->min_rate) {
6616 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
6617 return -EINVAL;
6618 }
6619
6620 if (rate > policer_item->policer->max_rate) {
6621 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
6622 return -EINVAL;
6623 }
6624
6625 if (burst < policer_item->policer->min_burst) {
6626 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
6627 return -EINVAL;
6628 }
6629
6630 if (burst > policer_item->policer->max_burst) {
6631 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
6632 return -EINVAL;
6633 }
6634
6635 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
6636 rate, burst, info->extack);
6637 if (err)
6638 return err;
6639
6640 policer_item->rate = rate;
6641 policer_item->burst = burst;
6642
6643 return 0;
6644}
6645
6646static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
6647 struct genl_info *info)
6648{
6649 struct devlink_trap_policer_item *policer_item;
6650 struct netlink_ext_ack *extack = info->extack;
6651 struct devlink *devlink = info->user_ptr[0];
6652
6653 if (list_empty(&devlink->trap_policer_list))
6654 return -EOPNOTSUPP;
6655
6656 if (!devlink->ops->trap_policer_set)
6657 return -EOPNOTSUPP;
6658
6659 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
6660 if (!policer_item) {
6661 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
6662 return -ENOENT;
6663 }
6664
6665 return devlink_trap_policer_set(devlink, policer_item, info);
6666}
6667
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006668static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
Ido Schimmel1e8c6612020-03-30 22:38:18 +03006669 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
6670 DEVLINK_ATTR_TRAP_POLICER_ID },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006671 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
6672 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
6673 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
6674 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
6675 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02006676 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
6677 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
6678 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
6679 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
6680 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
6681 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
6682 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006683 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02006684 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03006685 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006686 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
6687 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006688 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
6689 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006690 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
6691 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
6692 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006693 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03006694 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Jakub Kicinskiff3b63b2020-03-02 21:05:12 -08006695 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
6696 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006697 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006698 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
6699 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006700 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
6701 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006702 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
6703 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
6704 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirko070c63f2019-10-03 11:49:39 +02006705 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
6706 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
6707 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
Eran Ben Elisha48bb52c2020-03-29 14:05:55 +03006708 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
Ido Schimmel1e8c6612020-03-30 22:38:18 +03006709 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
6710 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
6711 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006712};
6713
6714static const struct genl_ops devlink_nl_ops[] = {
6715 {
6716 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006717 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006718 .doit = devlink_nl_cmd_get_doit,
6719 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02006720 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006721 /* can be retrieved by unprivileged users */
6722 },
6723 {
6724 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006725 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006726 .doit = devlink_nl_cmd_port_get_doit,
6727 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006728 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6729 /* can be retrieved by unprivileged users */
6730 },
6731 {
6732 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006733 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006734 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006735 .flags = GENL_ADMIN_PERM,
6736 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6737 },
6738 {
6739 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006740 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006741 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006742 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006743 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6744 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006745 },
6746 {
6747 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006748 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006749 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006750 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006751 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6752 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006753 },
Jiri Pirkobf797472016-04-14 18:19:13 +02006754 {
6755 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006756 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006757 .doit = devlink_nl_cmd_sb_get_doit,
6758 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006759 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6760 DEVLINK_NL_FLAG_NEED_SB,
6761 /* can be retrieved by unprivileged users */
6762 },
6763 {
6764 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006765 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006766 .doit = devlink_nl_cmd_sb_pool_get_doit,
6767 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006768 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6769 DEVLINK_NL_FLAG_NEED_SB,
6770 /* can be retrieved by unprivileged users */
6771 },
6772 {
6773 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006774 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006775 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006776 .flags = GENL_ADMIN_PERM,
6777 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6778 DEVLINK_NL_FLAG_NEED_SB,
6779 },
6780 {
6781 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006782 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006783 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
6784 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006785 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6786 DEVLINK_NL_FLAG_NEED_SB,
6787 /* can be retrieved by unprivileged users */
6788 },
6789 {
6790 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006791 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006792 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006793 .flags = GENL_ADMIN_PERM,
6794 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6795 DEVLINK_NL_FLAG_NEED_SB,
6796 },
6797 {
6798 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006799 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006800 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
6801 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006802 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6803 DEVLINK_NL_FLAG_NEED_SB,
6804 /* can be retrieved by unprivileged users */
6805 },
6806 {
6807 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006808 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006809 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006810 .flags = GENL_ADMIN_PERM,
6811 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6812 DEVLINK_NL_FLAG_NEED_SB,
6813 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006814 {
6815 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006816 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006817 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006818 .flags = GENL_ADMIN_PERM,
6819 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006820 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006821 },
6822 {
6823 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006824 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006825 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006826 .flags = GENL_ADMIN_PERM,
6827 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006828 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006829 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006830 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006831 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006832 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006833 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006834 .flags = GENL_ADMIN_PERM,
Parav Pandit98fed6e2020-02-23 19:06:56 -06006835 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6836 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006837 },
6838 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006839 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006840 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006841 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006842 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07006843 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6844 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006845 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006846 {
6847 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006848 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006849 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006850 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006851 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006852 },
6853 {
6854 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006855 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006856 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006857 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006858 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006859 },
6860 {
6861 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006862 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006863 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006864 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006865 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006866 },
6867 {
6868 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006869 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006870 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006871 .flags = GENL_ADMIN_PERM,
6872 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6873 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006874 {
6875 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006876 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006877 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006878 .flags = GENL_ADMIN_PERM,
6879 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6880 },
6881 {
6882 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02006883 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006884 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006885 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006886 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006887 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006888 {
6889 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02006890 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006891 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006892 .flags = GENL_ADMIN_PERM,
6893 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6894 DEVLINK_NL_FLAG_NO_LOCK,
6895 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006896 {
6897 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006898 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006899 .doit = devlink_nl_cmd_param_get_doit,
6900 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006901 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6902 /* can be retrieved by unprivileged users */
6903 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006904 {
6905 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006906 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006907 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006908 .flags = GENL_ADMIN_PERM,
6909 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6910 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006911 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306912 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006913 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306914 .doit = devlink_nl_cmd_port_param_get_doit,
6915 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306916 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6917 /* can be retrieved by unprivileged users */
6918 },
6919 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05306920 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006921 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306922 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306923 .flags = GENL_ADMIN_PERM,
6924 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6925 },
6926 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006927 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006928 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006929 .doit = devlink_nl_cmd_region_get_doit,
6930 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006931 .flags = GENL_ADMIN_PERM,
6932 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6933 },
Alex Vesker866319b2018-07-12 15:13:13 +03006934 {
Jacob Kellerb9a17ab2020-03-26 11:37:16 -07006935 .cmd = DEVLINK_CMD_REGION_NEW,
6936 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6937 .doit = devlink_nl_cmd_region_new,
6938 .flags = GENL_ADMIN_PERM,
6939 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6940 },
6941 {
Alex Vesker866319b2018-07-12 15:13:13 +03006942 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02006943 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03006944 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03006945 .flags = GENL_ADMIN_PERM,
6946 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6947 },
Alex Vesker4e547952018-07-12 15:13:14 +03006948 {
6949 .cmd = DEVLINK_CMD_REGION_READ,
Jiri Pirkoee85da52019-10-05 20:04:42 +02006950 .validate = GENL_DONT_VALIDATE_STRICT |
6951 GENL_DONT_VALIDATE_DUMP_STRICT,
Alex Vesker4e547952018-07-12 15:13:14 +03006952 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03006953 .flags = GENL_ADMIN_PERM,
6954 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6955 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006956 {
6957 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006958 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006959 .doit = devlink_nl_cmd_info_get_doit,
6960 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006961 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6962 /* can be retrieved by unprivileged users */
6963 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006964 {
6965 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006966 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006967 .doit = devlink_nl_cmd_health_reporter_get_doit,
6968 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006969 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6970 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006971 /* can be retrieved by unprivileged users */
6972 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006973 {
6974 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006975 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006976 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006977 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006978 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6979 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006980 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006981 {
6982 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02006983 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006984 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006985 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006986 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6987 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006988 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006989 {
6990 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006991 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006992 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006993 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006994 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6995 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006996 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006997 {
6998 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Jiri Pirko82a843d2019-10-07 09:28:31 +02006999 .validate = GENL_DONT_VALIDATE_STRICT |
7000 GENL_DONT_VALIDATE_DUMP_STRICT,
Aya Levine44ef4e2019-05-16 09:49:20 +03007001 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02007002 .flags = GENL_ADMIN_PERM,
7003 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
7004 DEVLINK_NL_FLAG_NO_LOCK,
7005 },
7006 {
7007 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02007008 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02007009 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02007010 .flags = GENL_ADMIN_PERM,
7011 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
7012 DEVLINK_NL_FLAG_NO_LOCK,
7013 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08007014 {
7015 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02007016 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08007017 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08007018 .flags = GENL_ADMIN_PERM,
7019 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7020 },
Ido Schimmel0f420b62019-08-17 16:28:17 +03007021 {
7022 .cmd = DEVLINK_CMD_TRAP_GET,
7023 .doit = devlink_nl_cmd_trap_get_doit,
7024 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7025 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7026 /* can be retrieved by unprivileged users */
7027 },
7028 {
7029 .cmd = DEVLINK_CMD_TRAP_SET,
7030 .doit = devlink_nl_cmd_trap_set_doit,
7031 .flags = GENL_ADMIN_PERM,
7032 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7033 },
7034 {
7035 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7036 .doit = devlink_nl_cmd_trap_group_get_doit,
7037 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7038 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7039 /* can be retrieved by unprivileged users */
7040 },
7041 {
7042 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7043 .doit = devlink_nl_cmd_trap_group_set_doit,
7044 .flags = GENL_ADMIN_PERM,
7045 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7046 },
Ido Schimmel1e8c6612020-03-30 22:38:18 +03007047 {
7048 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7049 .doit = devlink_nl_cmd_trap_policer_get_doit,
7050 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7051 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7052 /* can be retrieved by unprivileged users */
7053 },
7054 {
7055 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7056 .doit = devlink_nl_cmd_trap_policer_set_doit,
7057 .flags = GENL_ADMIN_PERM,
7058 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
7059 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007060};
7061
Johannes Berg56989f62016-10-24 14:40:05 +02007062static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02007063 .name = DEVLINK_GENL_NAME,
7064 .version = DEVLINK_GENL_VERSION,
7065 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01007066 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02007067 .netnsok = true,
7068 .pre_doit = devlink_nl_pre_doit,
7069 .post_doit = devlink_nl_post_doit,
7070 .module = THIS_MODULE,
7071 .ops = devlink_nl_ops,
7072 .n_ops = ARRAY_SIZE(devlink_nl_ops),
7073 .mcgrps = devlink_nl_mcgrps,
7074 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7075};
7076
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007077/**
7078 * devlink_alloc - Allocate new devlink instance resources
7079 *
7080 * @ops: ops
7081 * @priv_size: size of user private data
7082 *
7083 * Allocate new devlink instance resources, including devlink index
7084 * and name.
7085 */
7086struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7087{
7088 struct devlink *devlink;
7089
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08007090 if (WARN_ON(!ops))
7091 return NULL;
7092
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007093 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7094 if (!devlink)
7095 return NULL;
7096 devlink->ops = ops;
Jacob Keller12102432020-03-26 11:37:15 -07007097 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
Jiri Pirko8273fd82019-10-05 08:10:31 +02007098 __devlink_net_set(devlink, &init_net);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007099 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02007100 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007101 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007102 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007103 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007104 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02007105 INIT_LIST_HEAD(&devlink->reporter_list);
Ido Schimmel0f420b62019-08-17 16:28:17 +03007106 INIT_LIST_HEAD(&devlink->trap_list);
7107 INIT_LIST_HEAD(&devlink->trap_group_list);
Ido Schimmel1e8c6612020-03-30 22:38:18 +03007108 INIT_LIST_HEAD(&devlink->trap_policer_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007109 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03007110 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007111 return devlink;
7112}
7113EXPORT_SYMBOL_GPL(devlink_alloc);
7114
7115/**
7116 * devlink_register - Register devlink instance
7117 *
7118 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007119 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007120 */
7121int devlink_register(struct devlink *devlink, struct device *dev)
7122{
7123 mutex_lock(&devlink_mutex);
7124 devlink->dev = dev;
Jiri Pirko8273fd82019-10-05 08:10:31 +02007125 devlink->registered = true;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007126 list_add_tail(&devlink->list, &devlink_list);
7127 devlink_notify(devlink, DEVLINK_CMD_NEW);
7128 mutex_unlock(&devlink_mutex);
7129 return 0;
7130}
7131EXPORT_SYMBOL_GPL(devlink_register);
7132
7133/**
7134 * devlink_unregister - Unregister devlink instance
7135 *
7136 * @devlink: devlink
7137 */
7138void devlink_unregister(struct devlink *devlink)
7139{
7140 mutex_lock(&devlink_mutex);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01007141 WARN_ON(devlink_reload_supported(devlink) &&
7142 devlink->reload_enabled);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007143 devlink_notify(devlink, DEVLINK_CMD_DEL);
7144 list_del(&devlink->list);
7145 mutex_unlock(&devlink_mutex);
7146}
7147EXPORT_SYMBOL_GPL(devlink_unregister);
7148
7149/**
Jiri Pirkoa0c76342019-11-08 21:42:43 +01007150 * devlink_reload_enable - Enable reload of devlink instance
7151 *
7152 * @devlink: devlink
7153 *
7154 * Should be called at end of device initialization
7155 * process when reload operation is supported.
7156 */
7157void devlink_reload_enable(struct devlink *devlink)
7158{
7159 mutex_lock(&devlink_mutex);
7160 devlink->reload_enabled = true;
7161 mutex_unlock(&devlink_mutex);
7162}
7163EXPORT_SYMBOL_GPL(devlink_reload_enable);
7164
7165/**
7166 * devlink_reload_disable - Disable reload of devlink instance
7167 *
7168 * @devlink: devlink
7169 *
7170 * Should be called at the beginning of device cleanup
7171 * process when reload operation is supported.
7172 */
7173void devlink_reload_disable(struct devlink *devlink)
7174{
7175 mutex_lock(&devlink_mutex);
7176 /* Mutex is taken which ensures that no reload operation is in
7177 * progress while setting up forbidded flag.
7178 */
7179 devlink->reload_enabled = false;
7180 mutex_unlock(&devlink_mutex);
7181}
7182EXPORT_SYMBOL_GPL(devlink_reload_disable);
7183
7184/**
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007185 * devlink_free - Free devlink instance resources
7186 *
7187 * @devlink: devlink
7188 */
7189void devlink_free(struct devlink *devlink)
7190{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03007191 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01007192 mutex_destroy(&devlink->lock);
Ido Schimmel1e8c6612020-03-30 22:38:18 +03007193 WARN_ON(!list_empty(&devlink->trap_policer_list));
Ido Schimmel0f420b62019-08-17 16:28:17 +03007194 WARN_ON(!list_empty(&devlink->trap_group_list));
7195 WARN_ON(!list_empty(&devlink->trap_list));
Parav Panditb904aad2019-02-08 15:15:00 -06007196 WARN_ON(!list_empty(&devlink->reporter_list));
7197 WARN_ON(!list_empty(&devlink->region_list));
7198 WARN_ON(!list_empty(&devlink->param_list));
7199 WARN_ON(!list_empty(&devlink->resource_list));
7200 WARN_ON(!list_empty(&devlink->dpipe_table_list));
7201 WARN_ON(!list_empty(&devlink->sb_list));
7202 WARN_ON(!list_empty(&devlink->port_list));
7203
Jacob Keller12102432020-03-26 11:37:15 -07007204 xa_destroy(&devlink->snapshot_ids);
7205
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007206 kfree(devlink);
7207}
7208EXPORT_SYMBOL_GPL(devlink_free);
7209
Jiri Pirko136bf272019-05-23 10:43:35 +02007210static void devlink_port_type_warn(struct work_struct *work)
7211{
7212 WARN(true, "Type was not set for devlink port.");
7213}
7214
7215static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
7216{
7217 /* Ignore CPU and DSA flavours. */
7218 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
7219 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
7220}
7221
Ido Schimmel4c582232020-01-09 19:57:41 +02007222#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
Jiri Pirko136bf272019-05-23 10:43:35 +02007223
7224static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
7225{
7226 if (!devlink_port_type_should_warn(devlink_port))
7227 return;
7228 /* Schedule a work to WARN in case driver does not set port
7229 * type within timeout.
7230 */
7231 schedule_delayed_work(&devlink_port->type_warn_dw,
7232 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
7233}
7234
7235static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
7236{
7237 if (!devlink_port_type_should_warn(devlink_port))
7238 return;
7239 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
7240}
7241
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007242/**
7243 * devlink_port_register - Register devlink port
7244 *
7245 * @devlink: devlink
7246 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007247 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007248 *
7249 * Register devlink port with provided port index. User can use
7250 * any indexing, even hw-related one. devlink_port structure
7251 * is convenient to be embedded inside user driver private structure.
7252 * Note that the caller should take care of zeroing the devlink_port
7253 * structure.
7254 */
7255int devlink_port_register(struct devlink *devlink,
7256 struct devlink_port *devlink_port,
7257 unsigned int port_index)
7258{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007259 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007260 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007261 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007262 return -EEXIST;
7263 }
7264 devlink_port->devlink = devlink;
7265 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007266 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01007267 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007268 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307269 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007270 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02007271 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
7272 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007273 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
7274 return 0;
7275}
7276EXPORT_SYMBOL_GPL(devlink_port_register);
7277
7278/**
7279 * devlink_port_unregister - Unregister devlink port
7280 *
7281 * @devlink_port: devlink port
7282 */
7283void devlink_port_unregister(struct devlink_port *devlink_port)
7284{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007285 struct devlink *devlink = devlink_port->devlink;
7286
Jiri Pirko136bf272019-05-23 10:43:35 +02007287 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007288 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007289 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007290 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007291 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007292}
7293EXPORT_SYMBOL_GPL(devlink_port_unregister);
7294
7295static void __devlink_port_type_set(struct devlink_port *devlink_port,
7296 enum devlink_port_type type,
7297 void *type_dev)
7298{
Jiri Pirko2b239e72019-03-24 11:14:36 +01007299 if (WARN_ON(!devlink_port->registered))
7300 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02007301 devlink_port_type_warn_cancel(devlink_port);
Ido Schimmel0f420b62019-08-17 16:28:17 +03007302 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007303 devlink_port->type = type;
7304 devlink_port->type_dev = type_dev;
Ido Schimmel0f420b62019-08-17 16:28:17 +03007305 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007306 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
7307}
7308
7309/**
7310 * devlink_port_type_eth_set - Set port type to Ethernet
7311 *
7312 * @devlink_port: devlink port
7313 * @netdev: related netdevice
7314 */
7315void devlink_port_type_eth_set(struct devlink_port *devlink_port,
7316 struct net_device *netdev)
7317{
Jiri Pirko119c0b52019-04-03 14:24:27 +02007318 const struct net_device_ops *ops = netdev->netdev_ops;
7319
Jiri Pirko746364f2019-03-28 13:56:46 +01007320 /* If driver registers devlink port, it should set devlink port
7321 * attributes accordingly so the compat functions are called
7322 * and the original ops are not used.
7323 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02007324 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01007325 /* Some drivers use the same set of ndos for netdevs
7326 * that have devlink_port registered and also for
7327 * those who don't. Make sure that ndo_get_phys_port_name
7328 * returns -EOPNOTSUPP here in case it is defined.
7329 * Warn if not.
7330 */
Jiri Pirko746364f2019-03-28 13:56:46 +01007331 char name[IFNAMSIZ];
7332 int err;
7333
7334 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
7335 WARN_ON(err != -EOPNOTSUPP);
7336 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02007337 if (ops->ndo_get_port_parent_id) {
7338 /* Some drivers use the same set of ndos for netdevs
7339 * that have devlink_port registered and also for
7340 * those who don't. Make sure that ndo_get_port_parent_id
7341 * returns -EOPNOTSUPP here in case it is defined.
7342 * Warn if not.
7343 */
7344 struct netdev_phys_item_id ppid;
7345 int err;
7346
7347 err = ops->ndo_get_port_parent_id(netdev, &ppid);
7348 WARN_ON(err != -EOPNOTSUPP);
7349 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01007350 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007351}
7352EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
7353
7354/**
7355 * devlink_port_type_ib_set - Set port type to InfiniBand
7356 *
7357 * @devlink_port: devlink port
7358 * @ibdev: related IB device
7359 */
7360void devlink_port_type_ib_set(struct devlink_port *devlink_port,
7361 struct ib_device *ibdev)
7362{
Jiri Pirko773b1f32019-03-24 11:14:30 +01007363 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007364}
7365EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
7366
7367/**
7368 * devlink_port_type_clear - Clear port type
7369 *
7370 * @devlink_port: devlink port
7371 */
7372void devlink_port_type_clear(struct devlink_port *devlink_port)
7373{
Jiri Pirko773b1f32019-03-24 11:14:30 +01007374 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02007375 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007376}
7377EXPORT_SYMBOL_GPL(devlink_port_type_clear);
7378
Parav Pandit378ef012019-07-08 23:17:35 -05007379static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
7380 enum devlink_port_flavour flavour,
7381 const unsigned char *switch_id,
7382 unsigned char switch_id_len)
7383{
7384 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7385
7386 if (WARN_ON(devlink_port->registered))
7387 return -EEXIST;
7388 attrs->set = true;
7389 attrs->flavour = flavour;
7390 if (switch_id) {
7391 attrs->switch_port = true;
7392 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
7393 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
7394 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
7395 attrs->switch_id.id_len = switch_id_len;
7396 } else {
7397 attrs->switch_port = false;
7398 }
7399 return 0;
7400}
7401
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007402/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007403 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007404 *
7405 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02007406 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007407 * @port_number: number of the port that is facing user, for example
7408 * the front panel port number
7409 * @split: indicates if this is split port
7410 * @split_subport_number: if the port is split, this is the number
7411 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02007412 * @switch_id: if the port is part of switch, this is buffer with ID,
7413 * otwerwise this is NULL
7414 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007415 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007416void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02007417 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007418 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02007419 u32 split_subport_number,
7420 const unsigned char *switch_id,
7421 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007422{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007423 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05007424 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007425
Parav Pandit378ef012019-07-08 23:17:35 -05007426 ret = __devlink_port_attrs_set(devlink_port, flavour,
7427 switch_id, switch_id_len);
7428 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01007429 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007430 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05007431 attrs->phys.port_number = port_number;
7432 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007433}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02007434EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01007435
Parav Pandit98fd2d62019-07-08 23:17:37 -05007436/**
7437 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
7438 *
7439 * @devlink_port: devlink port
7440 * @pf: associated PF for the devlink port instance
7441 * @switch_id: if the port is part of switch, this is buffer with ID,
7442 * otherwise this is NULL
7443 * @switch_id_len: length of the switch_id buffer
7444 */
7445void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
7446 const unsigned char *switch_id,
7447 unsigned char switch_id_len, u16 pf)
7448{
7449 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7450 int ret;
7451
7452 ret = __devlink_port_attrs_set(devlink_port,
7453 DEVLINK_PORT_FLAVOUR_PCI_PF,
7454 switch_id, switch_id_len);
7455 if (ret)
7456 return;
7457
7458 attrs->pci_pf.pf = pf;
7459}
7460EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
7461
Parav Pandite41b6bf2019-07-08 23:17:38 -05007462/**
7463 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
7464 *
7465 * @devlink_port: devlink port
7466 * @pf: associated PF for the devlink port instance
7467 * @vf: associated VF of a PF for the devlink port instance
7468 * @switch_id: if the port is part of switch, this is buffer with ID,
7469 * otherwise this is NULL
7470 * @switch_id_len: length of the switch_id buffer
7471 */
7472void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
7473 const unsigned char *switch_id,
7474 unsigned char switch_id_len,
7475 u16 pf, u16 vf)
7476{
7477 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7478 int ret;
7479
7480 ret = __devlink_port_attrs_set(devlink_port,
7481 DEVLINK_PORT_FLAVOUR_PCI_VF,
7482 switch_id, switch_id_len);
7483 if (ret)
7484 return;
7485 attrs->pci_vf.pf = pf;
7486 attrs->pci_vf.vf = vf;
7487}
7488EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
7489
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01007490static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
7491 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02007492{
7493 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7494 int n = 0;
7495
7496 if (!attrs->set)
7497 return -EOPNOTSUPP;
7498
7499 switch (attrs->flavour) {
7500 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
Parav Panditacf1ee42020-03-03 08:12:42 -06007501 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
Jiri Pirko08474c12018-05-18 09:29:02 +02007502 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05007503 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02007504 else
Parav Pandit378ef012019-07-08 23:17:35 -05007505 n = snprintf(name, len, "p%us%u",
7506 attrs->phys.port_number,
7507 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02007508 break;
7509 case DEVLINK_PORT_FLAVOUR_CPU:
7510 case DEVLINK_PORT_FLAVOUR_DSA:
7511 /* As CPU and DSA ports do not have a netdevice associated
7512 * case should not ever happen.
7513 */
7514 WARN_ON(1);
7515 return -EINVAL;
Parav Pandit98fd2d62019-07-08 23:17:37 -05007516 case DEVLINK_PORT_FLAVOUR_PCI_PF:
7517 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
7518 break;
Parav Pandite41b6bf2019-07-08 23:17:38 -05007519 case DEVLINK_PORT_FLAVOUR_PCI_VF:
7520 n = snprintf(name, len, "pf%uvf%u",
7521 attrs->pci_vf.pf, attrs->pci_vf.vf);
7522 break;
Jiri Pirko08474c12018-05-18 09:29:02 +02007523 }
7524
7525 if (n >= len)
7526 return -EINVAL;
7527
7528 return 0;
7529}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01007530
Jiri Pirkobf797472016-04-14 18:19:13 +02007531int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
7532 u32 size, u16 ingress_pools_count,
7533 u16 egress_pools_count, u16 ingress_tc_count,
7534 u16 egress_tc_count)
7535{
7536 struct devlink_sb *devlink_sb;
7537 int err = 0;
7538
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007539 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02007540 if (devlink_sb_index_exists(devlink, sb_index)) {
7541 err = -EEXIST;
7542 goto unlock;
7543 }
7544
7545 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
7546 if (!devlink_sb) {
7547 err = -ENOMEM;
7548 goto unlock;
7549 }
7550 devlink_sb->index = sb_index;
7551 devlink_sb->size = size;
7552 devlink_sb->ingress_pools_count = ingress_pools_count;
7553 devlink_sb->egress_pools_count = egress_pools_count;
7554 devlink_sb->ingress_tc_count = ingress_tc_count;
7555 devlink_sb->egress_tc_count = egress_tc_count;
7556 list_add_tail(&devlink_sb->list, &devlink->sb_list);
7557unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007558 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02007559 return err;
7560}
7561EXPORT_SYMBOL_GPL(devlink_sb_register);
7562
7563void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
7564{
7565 struct devlink_sb *devlink_sb;
7566
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007567 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02007568 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
7569 WARN_ON(!devlink_sb);
7570 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007571 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02007572 kfree(devlink_sb);
7573}
7574EXPORT_SYMBOL_GPL(devlink_sb_unregister);
7575
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007576/**
7577 * devlink_dpipe_headers_register - register dpipe headers
7578 *
7579 * @devlink: devlink
7580 * @dpipe_headers: dpipe header array
7581 *
7582 * Register the headers supported by hardware.
7583 */
7584int devlink_dpipe_headers_register(struct devlink *devlink,
7585 struct devlink_dpipe_headers *dpipe_headers)
7586{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007587 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007588 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007589 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007590 return 0;
7591}
7592EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
7593
7594/**
7595 * devlink_dpipe_headers_unregister - unregister dpipe headers
7596 *
7597 * @devlink: devlink
7598 *
7599 * Unregister the headers supported by hardware.
7600 */
7601void devlink_dpipe_headers_unregister(struct devlink *devlink)
7602{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007603 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007604 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007605 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007606}
7607EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
7608
7609/**
7610 * devlink_dpipe_table_counter_enabled - check if counter allocation
7611 * required
7612 * @devlink: devlink
7613 * @table_name: tables name
7614 *
7615 * Used by driver to check if counter allocation is required.
7616 * After counter allocation is turned on the table entries
7617 * are updated to include counter statistics.
7618 *
7619 * After that point on the driver must respect the counter
7620 * state so that each entry added to the table is added
7621 * with a counter.
7622 */
7623bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
7624 const char *table_name)
7625{
7626 struct devlink_dpipe_table *table;
7627 bool enabled;
7628
7629 rcu_read_lock();
7630 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05307631 table_name, devlink);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007632 enabled = false;
7633 if (table)
7634 enabled = table->counters_enabled;
7635 rcu_read_unlock();
7636 return enabled;
7637}
7638EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
7639
7640/**
7641 * devlink_dpipe_table_register - register dpipe table
7642 *
7643 * @devlink: devlink
7644 * @table_name: table name
7645 * @table_ops: table ops
7646 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007647 * @counter_control_extern: external control for counters
7648 */
7649int devlink_dpipe_table_register(struct devlink *devlink,
7650 const char *table_name,
7651 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02007652 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007653{
7654 struct devlink_dpipe_table *table;
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307655 int err = 0;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007656
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02007657 if (WARN_ON(!table_ops->size_get))
7658 return -EINVAL;
7659
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307660 mutex_lock(&devlink->lock);
7661
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05307662 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
7663 devlink)) {
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307664 err = -EEXIST;
7665 goto unlock;
7666 }
7667
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007668 table = kzalloc(sizeof(*table), GFP_KERNEL);
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307669 if (!table) {
7670 err = -ENOMEM;
7671 goto unlock;
7672 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007673
7674 table->name = table_name;
7675 table->table_ops = table_ops;
7676 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007677 table->counter_control_extern = counter_control_extern;
7678
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007679 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307680unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007681 mutex_unlock(&devlink->lock);
Madhuparna Bhowmik6132c1d2020-02-23 16:52:33 +05307682 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007683}
7684EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
7685
7686/**
7687 * devlink_dpipe_table_unregister - unregister dpipe table
7688 *
7689 * @devlink: devlink
7690 * @table_name: table name
7691 */
7692void devlink_dpipe_table_unregister(struct devlink *devlink,
7693 const char *table_name)
7694{
7695 struct devlink_dpipe_table *table;
7696
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007697 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007698 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05307699 table_name, devlink);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007700 if (!table)
7701 goto unlock;
7702 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007703 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007704 kfree_rcu(table, rcu);
7705 return;
7706unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007707 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007708}
7709EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
7710
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007711/**
7712 * devlink_resource_register - devlink resource register
7713 *
7714 * @devlink: devlink
7715 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007716 * @resource_size: resource's size
7717 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007718 * @parent_resource_id: resource's parent id
7719 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007720 */
7721int devlink_resource_register(struct devlink *devlink,
7722 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007723 u64 resource_size,
7724 u64 resource_id,
7725 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02007726 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007727{
7728 struct devlink_resource *resource;
7729 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07007730 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007731 int err = 0;
7732
David Ahern14530742018-03-20 19:31:14 -07007733 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
7734
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007735 mutex_lock(&devlink->lock);
7736 resource = devlink_resource_find(devlink, NULL, resource_id);
7737 if (resource) {
7738 err = -EINVAL;
7739 goto out;
7740 }
7741
7742 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
7743 if (!resource) {
7744 err = -ENOMEM;
7745 goto out;
7746 }
7747
7748 if (top_hierarchy) {
7749 resource_list = &devlink->resource_list;
7750 } else {
7751 struct devlink_resource *parent_resource;
7752
7753 parent_resource = devlink_resource_find(devlink, NULL,
7754 parent_resource_id);
7755 if (parent_resource) {
7756 resource_list = &parent_resource->resource_list;
7757 resource->parent = parent_resource;
7758 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00007759 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007760 err = -EINVAL;
7761 goto out;
7762 }
7763 }
7764
7765 resource->name = resource_name;
7766 resource->size = resource_size;
7767 resource->size_new = resource_size;
7768 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007769 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01007770 memcpy(&resource->size_params, size_params,
7771 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007772 INIT_LIST_HEAD(&resource->resource_list);
7773 list_add_tail(&resource->list, resource_list);
7774out:
7775 mutex_unlock(&devlink->lock);
7776 return err;
7777}
7778EXPORT_SYMBOL_GPL(devlink_resource_register);
7779
7780/**
7781 * devlink_resources_unregister - free all resources
7782 *
7783 * @devlink: devlink
7784 * @resource: resource
7785 */
7786void devlink_resources_unregister(struct devlink *devlink,
7787 struct devlink_resource *resource)
7788{
7789 struct devlink_resource *tmp, *child_resource;
7790 struct list_head *resource_list;
7791
7792 if (resource)
7793 resource_list = &resource->resource_list;
7794 else
7795 resource_list = &devlink->resource_list;
7796
7797 if (!resource)
7798 mutex_lock(&devlink->lock);
7799
7800 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
7801 devlink_resources_unregister(devlink, child_resource);
7802 list_del(&child_resource->list);
7803 kfree(child_resource);
7804 }
7805
7806 if (!resource)
7807 mutex_unlock(&devlink->lock);
7808}
7809EXPORT_SYMBOL_GPL(devlink_resources_unregister);
7810
7811/**
7812 * devlink_resource_size_get - get and update size
7813 *
7814 * @devlink: devlink
7815 * @resource_id: the requested resource id
7816 * @p_resource_size: ptr to update
7817 */
7818int devlink_resource_size_get(struct devlink *devlink,
7819 u64 resource_id,
7820 u64 *p_resource_size)
7821{
7822 struct devlink_resource *resource;
7823 int err = 0;
7824
7825 mutex_lock(&devlink->lock);
7826 resource = devlink_resource_find(devlink, NULL, resource_id);
7827 if (!resource) {
7828 err = -EINVAL;
7829 goto out;
7830 }
7831 *p_resource_size = resource->size_new;
7832 resource->size = resource->size_new;
7833out:
7834 mutex_unlock(&devlink->lock);
7835 return err;
7836}
7837EXPORT_SYMBOL_GPL(devlink_resource_size_get);
7838
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01007839/**
7840 * devlink_dpipe_table_resource_set - set the resource id
7841 *
7842 * @devlink: devlink
7843 * @table_name: table name
7844 * @resource_id: resource id
7845 * @resource_units: number of resource's units consumed per table's entry
7846 */
7847int devlink_dpipe_table_resource_set(struct devlink *devlink,
7848 const char *table_name, u64 resource_id,
7849 u64 resource_units)
7850{
7851 struct devlink_dpipe_table *table;
7852 int err = 0;
7853
7854 mutex_lock(&devlink->lock);
7855 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
Madhuparna Bhowmik2eb51c72020-02-25 17:57:45 +05307856 table_name, devlink);
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01007857 if (!table) {
7858 err = -EINVAL;
7859 goto out;
7860 }
7861 table->resource_id = resource_id;
7862 table->resource_units = resource_units;
7863 table->resource_valid = true;
7864out:
7865 mutex_unlock(&devlink->lock);
7866 return err;
7867}
7868EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
7869
Jiri Pirkofc56be42018-04-05 22:13:21 +02007870/**
7871 * devlink_resource_occ_get_register - register occupancy getter
7872 *
7873 * @devlink: devlink
7874 * @resource_id: resource id
7875 * @occ_get: occupancy getter callback
7876 * @occ_get_priv: occupancy getter callback priv
7877 */
7878void devlink_resource_occ_get_register(struct devlink *devlink,
7879 u64 resource_id,
7880 devlink_resource_occ_get_t *occ_get,
7881 void *occ_get_priv)
7882{
7883 struct devlink_resource *resource;
7884
7885 mutex_lock(&devlink->lock);
7886 resource = devlink_resource_find(devlink, NULL, resource_id);
7887 if (WARN_ON(!resource))
7888 goto out;
7889 WARN_ON(resource->occ_get);
7890
7891 resource->occ_get = occ_get;
7892 resource->occ_get_priv = occ_get_priv;
7893out:
7894 mutex_unlock(&devlink->lock);
7895}
7896EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
7897
7898/**
7899 * devlink_resource_occ_get_unregister - unregister occupancy getter
7900 *
7901 * @devlink: devlink
7902 * @resource_id: resource id
7903 */
7904void devlink_resource_occ_get_unregister(struct devlink *devlink,
7905 u64 resource_id)
7906{
7907 struct devlink_resource *resource;
7908
7909 mutex_lock(&devlink->lock);
7910 resource = devlink_resource_find(devlink, NULL, resource_id);
7911 if (WARN_ON(!resource))
7912 goto out;
7913 WARN_ON(!resource->occ_get);
7914
7915 resource->occ_get = NULL;
7916 resource->occ_get_priv = NULL;
7917out:
7918 mutex_unlock(&devlink->lock);
7919}
7920EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
7921
Vasundhara Volam39e61602019-01-28 18:00:20 +05307922static int devlink_param_verify(const struct devlink_param *param)
7923{
7924 if (!param || !param->name || !param->supported_cmodes)
7925 return -EINVAL;
7926 if (param->generic)
7927 return devlink_param_generic_verify(param);
7928 else
7929 return devlink_param_driver_verify(param);
7930}
7931
7932static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307933 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307934 struct list_head *param_list,
7935 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307936 size_t params_count,
7937 enum devlink_command reg_cmd,
7938 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307939{
7940 const struct devlink_param *param = params;
7941 int i;
7942 int err;
7943
7944 mutex_lock(&devlink->lock);
7945 for (i = 0; i < params_count; i++, param++) {
7946 err = devlink_param_verify(param);
7947 if (err)
7948 goto rollback;
7949
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307950 err = devlink_param_register_one(devlink, port_index,
7951 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307952 if (err)
7953 goto rollback;
7954 }
7955
7956 mutex_unlock(&devlink->lock);
7957 return 0;
7958
7959rollback:
7960 if (!i)
7961 goto unlock;
7962 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307963 devlink_param_unregister_one(devlink, port_index, param_list,
7964 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307965unlock:
7966 mutex_unlock(&devlink->lock);
7967 return err;
7968}
7969
7970static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307971 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307972 struct list_head *param_list,
7973 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307974 size_t params_count,
7975 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307976{
7977 const struct devlink_param *param = params;
7978 int i;
7979
7980 mutex_lock(&devlink->lock);
7981 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307982 devlink_param_unregister_one(devlink, 0, param_list, param,
7983 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307984 mutex_unlock(&devlink->lock);
7985}
7986
Moshe Shemesheabaef12018-07-04 14:30:28 +03007987/**
7988 * devlink_params_register - register configuration parameters
7989 *
7990 * @devlink: devlink
7991 * @params: configuration parameters array
7992 * @params_count: number of parameters provided
7993 *
7994 * Register the configuration parameters supported by the driver.
7995 */
7996int devlink_params_register(struct devlink *devlink,
7997 const struct devlink_param *params,
7998 size_t params_count)
7999{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308000 return __devlink_params_register(devlink, 0, &devlink->param_list,
8001 params, params_count,
8002 DEVLINK_CMD_PARAM_NEW,
8003 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03008004}
8005EXPORT_SYMBOL_GPL(devlink_params_register);
8006
8007/**
8008 * devlink_params_unregister - unregister configuration parameters
8009 * @devlink: devlink
8010 * @params: configuration parameters to unregister
8011 * @params_count: number of parameters provided
8012 */
8013void devlink_params_unregister(struct devlink *devlink,
8014 const struct devlink_param *params,
8015 size_t params_count)
8016{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308017 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8018 params, params_count,
8019 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03008020}
8021EXPORT_SYMBOL_GPL(devlink_params_unregister);
8022
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008023/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00008024 * devlink_params_publish - publish configuration parameters
8025 *
8026 * @devlink: devlink
8027 *
8028 * Publish previously registered configuration parameters.
8029 */
8030void devlink_params_publish(struct devlink *devlink)
8031{
8032 struct devlink_param_item *param_item;
8033
8034 list_for_each_entry(param_item, &devlink->param_list, list) {
8035 if (param_item->published)
8036 continue;
8037 param_item->published = true;
8038 devlink_param_notify(devlink, 0, param_item,
8039 DEVLINK_CMD_PARAM_NEW);
8040 }
8041}
8042EXPORT_SYMBOL_GPL(devlink_params_publish);
8043
8044/**
8045 * devlink_params_unpublish - unpublish configuration parameters
8046 *
8047 * @devlink: devlink
8048 *
8049 * Unpublish previously registered configuration parameters.
8050 */
8051void devlink_params_unpublish(struct devlink *devlink)
8052{
8053 struct devlink_param_item *param_item;
8054
8055 list_for_each_entry(param_item, &devlink->param_list, list) {
8056 if (!param_item->published)
8057 continue;
8058 param_item->published = false;
8059 devlink_param_notify(devlink, 0, param_item,
8060 DEVLINK_CMD_PARAM_DEL);
8061 }
8062}
8063EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8064
8065/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05308066 * devlink_port_params_register - register port configuration parameters
8067 *
8068 * @devlink_port: devlink port
8069 * @params: configuration parameters array
8070 * @params_count: number of parameters provided
8071 *
8072 * Register the configuration parameters supported by the port.
8073 */
8074int devlink_port_params_register(struct devlink_port *devlink_port,
8075 const struct devlink_param *params,
8076 size_t params_count)
8077{
8078 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308079 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05308080 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308081 params_count,
8082 DEVLINK_CMD_PORT_PARAM_NEW,
8083 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05308084}
8085EXPORT_SYMBOL_GPL(devlink_port_params_register);
8086
8087/**
8088 * devlink_port_params_unregister - unregister port configuration
8089 * parameters
8090 *
8091 * @devlink_port: devlink port
8092 * @params: configuration parameters array
8093 * @params_count: number of parameters provided
8094 */
8095void devlink_port_params_unregister(struct devlink_port *devlink_port,
8096 const struct devlink_param *params,
8097 size_t params_count)
8098{
8099 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308100 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05308101 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308102 params, params_count,
8103 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05308104}
8105EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
8106
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308107static int
8108__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
8109 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008110{
8111 struct devlink_param_item *param_item;
8112
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308113 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008114 if (!param_item)
8115 return -EINVAL;
8116
8117 if (!param_item->driverinit_value_valid ||
8118 !devlink_param_cmode_is_supported(param_item->param,
8119 DEVLINK_PARAM_CMODE_DRIVERINIT))
8120 return -EOPNOTSUPP;
8121
Moshe Shemesh12765342018-10-10 16:09:26 +03008122 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8123 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
8124 else
8125 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008126
8127 return 0;
8128}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308129
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308130static int
8131__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308132 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308133 struct list_head *param_list, u32 param_id,
8134 union devlink_param_value init_val,
8135 enum devlink_command cmd)
8136{
8137 struct devlink_param_item *param_item;
8138
8139 param_item = devlink_param_find_by_id(param_list, param_id);
8140 if (!param_item)
8141 return -EINVAL;
8142
8143 if (!devlink_param_cmode_is_supported(param_item->param,
8144 DEVLINK_PARAM_CMODE_DRIVERINIT))
8145 return -EOPNOTSUPP;
8146
8147 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8148 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
8149 else
8150 param_item->driverinit_value = init_val;
8151 param_item->driverinit_value_valid = true;
8152
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308153 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308154 return 0;
8155}
8156
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308157/**
8158 * devlink_param_driverinit_value_get - get configuration parameter
8159 * value for driver initializing
8160 *
8161 * @devlink: devlink
8162 * @param_id: parameter ID
8163 * @init_val: value of parameter in driverinit configuration mode
8164 *
8165 * This function should be used by the driver to get driverinit
8166 * configuration for initialization after reload command.
8167 */
8168int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
8169 union devlink_param_value *init_val)
8170{
Jiri Pirko97691062019-09-12 10:49:45 +02008171 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308172 return -EOPNOTSUPP;
8173
8174 return __devlink_param_driverinit_value_get(&devlink->param_list,
8175 param_id, init_val);
8176}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008177EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
8178
8179/**
8180 * devlink_param_driverinit_value_set - set value of configuration
8181 * parameter for driverinit
8182 * configuration mode
8183 *
8184 * @devlink: devlink
8185 * @param_id: parameter ID
8186 * @init_val: value of parameter to set for driverinit configuration mode
8187 *
8188 * This function should be used by the driver to set driverinit
8189 * configuration mode default value.
8190 */
8191int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
8192 union devlink_param_value init_val)
8193{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308194 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308195 &devlink->param_list,
8196 param_id, init_val,
8197 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03008198}
8199EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
8200
Moshe Shemeshea601e12018-07-04 14:30:32 +03008201/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308202 * devlink_port_param_driverinit_value_get - get configuration parameter
8203 * value for driver initializing
8204 *
8205 * @devlink_port: devlink_port
8206 * @param_id: parameter ID
8207 * @init_val: value of parameter in driverinit configuration mode
8208 *
8209 * This function should be used by the driver to get driverinit
8210 * configuration for initialization after reload command.
8211 */
8212int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
8213 u32 param_id,
8214 union devlink_param_value *init_val)
8215{
8216 struct devlink *devlink = devlink_port->devlink;
8217
Jiri Pirko97691062019-09-12 10:49:45 +02008218 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05308219 return -EOPNOTSUPP;
8220
8221 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
8222 param_id, init_val);
8223}
8224EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
8225
8226/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308227 * devlink_port_param_driverinit_value_set - set value of configuration
8228 * parameter for driverinit
8229 * configuration mode
8230 *
8231 * @devlink_port: devlink_port
8232 * @param_id: parameter ID
8233 * @init_val: value of parameter to set for driverinit configuration mode
8234 *
8235 * This function should be used by the driver to set driverinit
8236 * configuration mode default value.
8237 */
8238int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
8239 u32 param_id,
8240 union devlink_param_value init_val)
8241{
8242 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308243 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308244 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308245 param_id, init_val,
8246 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05308247}
8248EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
8249
8250/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03008251 * devlink_param_value_changed - notify devlink on a parameter's value
8252 * change. Should be called by the driver
8253 * right after the change.
8254 *
8255 * @devlink: devlink
8256 * @param_id: parameter ID
8257 *
8258 * This function should be used by the driver to notify devlink on value
8259 * change, excluding driverinit configuration mode.
8260 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03008261 */
8262void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
8263{
8264 struct devlink_param_item *param_item;
8265
8266 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
8267 WARN_ON(!param_item);
8268
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308269 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03008270}
8271EXPORT_SYMBOL_GPL(devlink_param_value_changed);
8272
Alex Veskerb16ebe92018-07-12 15:13:08 +03008273/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05308274 * devlink_port_param_value_changed - notify devlink on a parameter's value
8275 * change. Should be called by the driver
8276 * right after the change.
8277 *
8278 * @devlink_port: devlink_port
8279 * @param_id: parameter ID
8280 *
8281 * This function should be used by the driver to notify devlink on value
8282 * change, excluding driverinit configuration mode.
8283 * For driverinit configuration mode driver should use the function
8284 * devlink_port_param_driverinit_value_set() instead.
8285 */
8286void devlink_port_param_value_changed(struct devlink_port *devlink_port,
8287 u32 param_id)
8288{
8289 struct devlink_param_item *param_item;
8290
8291 param_item = devlink_param_find_by_id(&devlink_port->param_list,
8292 param_id);
8293 WARN_ON(!param_item);
8294
8295 devlink_param_notify(devlink_port->devlink, devlink_port->index,
8296 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
8297}
8298EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
8299
8300/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03008301 * devlink_param_value_str_fill - Safely fill-up the string preventing
8302 * from overflow of the preallocated buffer
8303 *
8304 * @dst_val: destination devlink_param_value
8305 * @src: source buffer
8306 */
8307void devlink_param_value_str_fill(union devlink_param_value *dst_val,
8308 const char *src)
8309{
8310 size_t len;
8311
8312 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
8313 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
8314}
8315EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
8316
8317/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03008318 * devlink_region_create - create a new address region
8319 *
8320 * @devlink: devlink
Jacob Kellere8937682020-03-26 11:37:08 -07008321 * @ops: region operations and name
Alex Veskerb16ebe92018-07-12 15:13:08 +03008322 * @region_max_snapshots: Maximum supported number of snapshots for region
8323 * @region_size: size of region
8324 */
Jacob Kellere8937682020-03-26 11:37:08 -07008325struct devlink_region *
8326devlink_region_create(struct devlink *devlink,
8327 const struct devlink_region_ops *ops,
8328 u32 region_max_snapshots, u64 region_size)
Alex Veskerb16ebe92018-07-12 15:13:08 +03008329{
8330 struct devlink_region *region;
8331 int err = 0;
8332
Jacob Kellera0a09f62020-03-26 11:37:09 -07008333 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
8334 return ERR_PTR(-EINVAL);
8335
Alex Veskerb16ebe92018-07-12 15:13:08 +03008336 mutex_lock(&devlink->lock);
8337
Jacob Kellere8937682020-03-26 11:37:08 -07008338 if (devlink_region_get_by_name(devlink, ops->name)) {
Alex Veskerb16ebe92018-07-12 15:13:08 +03008339 err = -EEXIST;
8340 goto unlock;
8341 }
8342
8343 region = kzalloc(sizeof(*region), GFP_KERNEL);
8344 if (!region) {
8345 err = -ENOMEM;
8346 goto unlock;
8347 }
8348
8349 region->devlink = devlink;
8350 region->max_snapshots = region_max_snapshots;
Jacob Kellere8937682020-03-26 11:37:08 -07008351 region->ops = ops;
Alex Veskerb16ebe92018-07-12 15:13:08 +03008352 region->size = region_size;
8353 INIT_LIST_HEAD(&region->snapshot_list);
8354 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03008355 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03008356
8357 mutex_unlock(&devlink->lock);
8358 return region;
8359
8360unlock:
8361 mutex_unlock(&devlink->lock);
8362 return ERR_PTR(err);
8363}
8364EXPORT_SYMBOL_GPL(devlink_region_create);
8365
8366/**
8367 * devlink_region_destroy - destroy address region
8368 *
8369 * @region: devlink region to destroy
8370 */
8371void devlink_region_destroy(struct devlink_region *region)
8372{
8373 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03008374 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03008375
8376 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03008377
8378 /* Free all snapshots of region */
8379 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
Jiri Pirko92b49822019-08-12 14:28:31 +02008380 devlink_region_snapshot_del(region, snapshot);
Alex Veskerd7e52722018-07-12 15:13:10 +03008381
Alex Veskerb16ebe92018-07-12 15:13:08 +03008382 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03008383
8384 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03008385 mutex_unlock(&devlink->lock);
8386 kfree(region);
8387}
8388EXPORT_SYMBOL_GPL(devlink_region_destroy);
8389
Alex Veskerccadfa42018-07-12 15:13:09 +03008390/**
Jacob Kellerb0efcae2020-01-09 11:08:20 -08008391 * devlink_region_snapshot_id_get - get snapshot ID
Alex Veskerccadfa42018-07-12 15:13:09 +03008392 *
8393 * This callback should be called when adding a new snapshot,
8394 * Driver should use the same id for multiple snapshots taken
8395 * on multiple regions at the same time/by the same trigger.
8396 *
Jacob Keller12102432020-03-26 11:37:15 -07008397 * The caller of this function must use devlink_region_snapshot_id_put
8398 * when finished creating regions using this id.
8399 *
Jacob Keller7ef19d32020-03-26 11:37:14 -07008400 * Returns zero on success, or a negative error code on failure.
8401 *
Alex Veskerccadfa42018-07-12 15:13:09 +03008402 * @devlink: devlink
Jacob Keller7ef19d32020-03-26 11:37:14 -07008403 * @id: storage to return id
Alex Veskerccadfa42018-07-12 15:13:09 +03008404 */
Jacob Keller7ef19d32020-03-26 11:37:14 -07008405int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
Alex Veskerccadfa42018-07-12 15:13:09 +03008406{
Jacob Keller7ef19d32020-03-26 11:37:14 -07008407 int err;
Alex Veskerccadfa42018-07-12 15:13:09 +03008408
8409 mutex_lock(&devlink->lock);
Jacob Keller7ef19d32020-03-26 11:37:14 -07008410 err = __devlink_region_snapshot_id_get(devlink, id);
Alex Veskerccadfa42018-07-12 15:13:09 +03008411 mutex_unlock(&devlink->lock);
8412
Jacob Keller7ef19d32020-03-26 11:37:14 -07008413 return err;
Alex Veskerccadfa42018-07-12 15:13:09 +03008414}
Jacob Kellerb0efcae2020-01-09 11:08:20 -08008415EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
Alex Veskerccadfa42018-07-12 15:13:09 +03008416
Alex Veskerd7e52722018-07-12 15:13:10 +03008417/**
Jacob Keller12102432020-03-26 11:37:15 -07008418 * devlink_region_snapshot_id_put - put snapshot ID reference
8419 *
8420 * This should be called by a driver after finishing creating snapshots
8421 * with an id. Doing so ensures that the ID can later be released in the
8422 * event that all snapshots using it have been destroyed.
8423 *
8424 * @devlink: devlink
8425 * @id: id to release reference on
8426 */
8427void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
8428{
8429 mutex_lock(&devlink->lock);
8430 __devlink_snapshot_id_decrement(devlink, id);
8431 mutex_unlock(&devlink->lock);
8432}
8433EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
8434
8435/**
Alex Veskerd7e52722018-07-12 15:13:10 +03008436 * devlink_region_snapshot_create - create a new snapshot
8437 * This will add a new snapshot of a region. The snapshot
8438 * will be stored on the region struct and can be accessed
Jacob Keller6d82f672020-03-26 11:37:10 -07008439 * from devlink. This is useful for future analyses of snapshots.
Alex Veskerd7e52722018-07-12 15:13:10 +03008440 * Multiple snapshots can be created on a region.
8441 * The @snapshot_id should be obtained using the getter function.
8442 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08008443 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03008444 * @data: snapshot data
8445 * @snapshot_id: snapshot id to be created
Alex Veskerd7e52722018-07-12 15:13:10 +03008446 */
Jiri Pirko3a5e5232019-08-09 15:27:15 +02008447int devlink_region_snapshot_create(struct devlink_region *region,
Jacob Kellera0a09f62020-03-26 11:37:09 -07008448 u8 *data, u32 snapshot_id)
Alex Veskerd7e52722018-07-12 15:13:10 +03008449{
8450 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03008451 int err;
8452
8453 mutex_lock(&devlink->lock);
Jacob Kellercf80fae2020-03-26 11:37:11 -07008454 err = __devlink_region_snapshot_create(region, data, snapshot_id);
Alex Veskerd7e52722018-07-12 15:13:10 +03008455 mutex_unlock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03008456
Alex Veskerd7e52722018-07-12 15:13:10 +03008457 return err;
8458}
8459EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
8460
Ido Schimmel0f420b62019-08-17 16:28:17 +03008461#define DEVLINK_TRAP(_id, _type) \
8462 { \
8463 .type = DEVLINK_TRAP_TYPE_##_type, \
8464 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
8465 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
8466 }
8467
8468static const struct devlink_trap devlink_trap_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03008469 DEVLINK_TRAP(SMAC_MC, DROP),
8470 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
8471 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
8472 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
8473 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
8474 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
8475 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
8476 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
8477 DEVLINK_TRAP(TAIL_DROP, DROP),
Amit Cohen6896cc42019-11-07 18:42:09 +02008478 DEVLINK_TRAP(NON_IP_PACKET, DROP),
8479 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
8480 DEVLINK_TRAP(DIP_LB, DROP),
8481 DEVLINK_TRAP(SIP_MC, DROP),
8482 DEVLINK_TRAP(SIP_LB, DROP),
8483 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
8484 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
8485 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
8486 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
Amit Cohen3b063ae2019-11-07 18:42:14 +02008487 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
8488 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
8489 DEVLINK_TRAP(RPF, EXCEPTION),
8490 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
8491 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
8492 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
Amit Cohen95f0ead2020-01-19 15:00:48 +02008493 DEVLINK_TRAP(NON_ROUTABLE, DROP),
Amit Cohen13c056e2020-01-19 15:00:54 +02008494 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
Amit Cohenc3cae492020-01-19 15:00:58 +02008495 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
Jiri Pirkoecd942a2020-02-24 08:35:47 +01008496 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
8497 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
Ido Schimmel515eac62020-05-29 21:36:41 +03008498 DEVLINK_TRAP(STP, CONTROL),
8499 DEVLINK_TRAP(LACP, CONTROL),
8500 DEVLINK_TRAP(LLDP, CONTROL),
8501 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
8502 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
8503 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
8504 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
8505 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
8506 DEVLINK_TRAP(MLD_QUERY, CONTROL),
8507 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
8508 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
8509 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
Ido Schimmel0f420b62019-08-17 16:28:17 +03008510};
8511
8512#define DEVLINK_TRAP_GROUP(_id) \
8513 { \
8514 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
8515 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
8516 }
8517
8518static const struct devlink_trap_group devlink_trap_group_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03008519 DEVLINK_TRAP_GROUP(L2_DROPS),
8520 DEVLINK_TRAP_GROUP(L3_DROPS),
Ido Schimmel678eb192020-05-29 21:36:36 +03008521 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
Ido Schimmel391203a2019-08-17 16:28:18 +03008522 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
Amit Cohen13c056e2020-01-19 15:00:54 +02008523 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
Jiri Pirkoecd942a2020-02-24 08:35:47 +01008524 DEVLINK_TRAP_GROUP(ACL_DROPS),
Ido Schimmel515eac62020-05-29 21:36:41 +03008525 DEVLINK_TRAP_GROUP(STP),
8526 DEVLINK_TRAP_GROUP(LACP),
8527 DEVLINK_TRAP_GROUP(LLDP),
8528 DEVLINK_TRAP_GROUP(MC_SNOOPING),
Ido Schimmel0f420b62019-08-17 16:28:17 +03008529};
8530
8531static int devlink_trap_generic_verify(const struct devlink_trap *trap)
8532{
8533 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
8534 return -EINVAL;
8535
8536 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
8537 return -EINVAL;
8538
8539 if (trap->type != devlink_trap_generic[trap->id].type)
8540 return -EINVAL;
8541
8542 return 0;
8543}
8544
8545static int devlink_trap_driver_verify(const struct devlink_trap *trap)
8546{
8547 int i;
8548
8549 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
8550 return -EINVAL;
8551
8552 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
8553 if (!strcmp(trap->name, devlink_trap_generic[i].name))
8554 return -EEXIST;
8555 }
8556
8557 return 0;
8558}
8559
8560static int devlink_trap_verify(const struct devlink_trap *trap)
8561{
Ido Schimmel107f1672020-03-22 20:48:30 +02008562 if (!trap || !trap->name)
Ido Schimmel0f420b62019-08-17 16:28:17 +03008563 return -EINVAL;
8564
8565 if (trap->generic)
8566 return devlink_trap_generic_verify(trap);
8567 else
8568 return devlink_trap_driver_verify(trap);
8569}
8570
8571static int
8572devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
8573{
8574 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
8575 return -EINVAL;
8576
8577 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
8578 return -EINVAL;
8579
8580 return 0;
8581}
8582
8583static int
8584devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
8585{
8586 int i;
8587
8588 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
8589 return -EINVAL;
8590
8591 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
8592 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
8593 return -EEXIST;
8594 }
8595
8596 return 0;
8597}
8598
8599static int devlink_trap_group_verify(const struct devlink_trap_group *group)
8600{
8601 if (group->generic)
8602 return devlink_trap_group_generic_verify(group);
8603 else
8604 return devlink_trap_group_driver_verify(group);
8605}
8606
8607static void
8608devlink_trap_group_notify(struct devlink *devlink,
8609 const struct devlink_trap_group_item *group_item,
8610 enum devlink_command cmd)
8611{
8612 struct sk_buff *msg;
8613 int err;
8614
8615 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
8616 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
8617
8618 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8619 if (!msg)
8620 return;
8621
8622 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
8623 0);
8624 if (err) {
8625 nlmsg_free(msg);
8626 return;
8627 }
8628
8629 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
8630 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
8631}
8632
Ido Schimmel0f420b62019-08-17 16:28:17 +03008633static int
8634devlink_trap_item_group_link(struct devlink *devlink,
8635 struct devlink_trap_item *trap_item)
8636{
Ido Schimmel107f1672020-03-22 20:48:30 +02008637 u16 group_id = trap_item->trap->init_group_id;
Ido Schimmel0f420b62019-08-17 16:28:17 +03008638 struct devlink_trap_group_item *group_item;
8639
Ido Schimmel107f1672020-03-22 20:48:30 +02008640 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
Ido Schimmela09b37f2020-03-22 20:48:29 +02008641 if (WARN_ON_ONCE(!group_item))
8642 return -EINVAL;
Ido Schimmel0f420b62019-08-17 16:28:17 +03008643
8644 trap_item->group_item = group_item;
8645
8646 return 0;
8647}
8648
Ido Schimmel0f420b62019-08-17 16:28:17 +03008649static void devlink_trap_notify(struct devlink *devlink,
8650 const struct devlink_trap_item *trap_item,
8651 enum devlink_command cmd)
8652{
8653 struct sk_buff *msg;
8654 int err;
8655
8656 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
8657 cmd != DEVLINK_CMD_TRAP_DEL);
8658
8659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8660 if (!msg)
8661 return;
8662
8663 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
8664 if (err) {
8665 nlmsg_free(msg);
8666 return;
8667 }
8668
8669 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
8670 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
8671}
8672
8673static int
8674devlink_trap_register(struct devlink *devlink,
8675 const struct devlink_trap *trap, void *priv)
8676{
8677 struct devlink_trap_item *trap_item;
8678 int err;
8679
8680 if (devlink_trap_item_lookup(devlink, trap->name))
8681 return -EEXIST;
8682
8683 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
8684 if (!trap_item)
8685 return -ENOMEM;
8686
8687 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
8688 if (!trap_item->stats) {
8689 err = -ENOMEM;
8690 goto err_stats_alloc;
8691 }
8692
8693 trap_item->trap = trap;
8694 trap_item->action = trap->init_action;
8695 trap_item->priv = priv;
8696
8697 err = devlink_trap_item_group_link(devlink, trap_item);
8698 if (err)
8699 goto err_group_link;
8700
8701 err = devlink->ops->trap_init(devlink, trap, trap_item);
8702 if (err)
8703 goto err_trap_init;
8704
8705 list_add_tail(&trap_item->list, &devlink->trap_list);
8706 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
8707
8708 return 0;
8709
8710err_trap_init:
Ido Schimmel0f420b62019-08-17 16:28:17 +03008711err_group_link:
8712 free_percpu(trap_item->stats);
8713err_stats_alloc:
8714 kfree(trap_item);
8715 return err;
8716}
8717
8718static void devlink_trap_unregister(struct devlink *devlink,
8719 const struct devlink_trap *trap)
8720{
8721 struct devlink_trap_item *trap_item;
8722
8723 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8724 if (WARN_ON_ONCE(!trap_item))
8725 return;
8726
8727 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
8728 list_del(&trap_item->list);
8729 if (devlink->ops->trap_fini)
8730 devlink->ops->trap_fini(devlink, trap, trap_item);
Ido Schimmel0f420b62019-08-17 16:28:17 +03008731 free_percpu(trap_item->stats);
8732 kfree(trap_item);
8733}
8734
8735static void devlink_trap_disable(struct devlink *devlink,
8736 const struct devlink_trap *trap)
8737{
8738 struct devlink_trap_item *trap_item;
8739
8740 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8741 if (WARN_ON_ONCE(!trap_item))
8742 return;
8743
8744 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP);
8745 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
8746}
8747
8748/**
8749 * devlink_traps_register - Register packet traps with devlink.
8750 * @devlink: devlink.
8751 * @traps: Packet traps.
8752 * @traps_count: Count of provided packet traps.
8753 * @priv: Driver private information.
8754 *
8755 * Return: Non-zero value on failure.
8756 */
8757int devlink_traps_register(struct devlink *devlink,
8758 const struct devlink_trap *traps,
8759 size_t traps_count, void *priv)
8760{
8761 int i, err;
8762
8763 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
8764 return -EINVAL;
8765
8766 mutex_lock(&devlink->lock);
8767 for (i = 0; i < traps_count; i++) {
8768 const struct devlink_trap *trap = &traps[i];
8769
8770 err = devlink_trap_verify(trap);
8771 if (err)
8772 goto err_trap_verify;
8773
8774 err = devlink_trap_register(devlink, trap, priv);
8775 if (err)
8776 goto err_trap_register;
8777 }
8778 mutex_unlock(&devlink->lock);
8779
8780 return 0;
8781
8782err_trap_register:
8783err_trap_verify:
8784 for (i--; i >= 0; i--)
8785 devlink_trap_unregister(devlink, &traps[i]);
8786 mutex_unlock(&devlink->lock);
8787 return err;
8788}
8789EXPORT_SYMBOL_GPL(devlink_traps_register);
8790
8791/**
8792 * devlink_traps_unregister - Unregister packet traps from devlink.
8793 * @devlink: devlink.
8794 * @traps: Packet traps.
8795 * @traps_count: Count of provided packet traps.
8796 */
8797void devlink_traps_unregister(struct devlink *devlink,
8798 const struct devlink_trap *traps,
8799 size_t traps_count)
8800{
8801 int i;
8802
8803 mutex_lock(&devlink->lock);
8804 /* Make sure we do not have any packets in-flight while unregistering
8805 * traps by disabling all of them and waiting for a grace period.
8806 */
8807 for (i = traps_count - 1; i >= 0; i--)
8808 devlink_trap_disable(devlink, &traps[i]);
8809 synchronize_rcu();
8810 for (i = traps_count - 1; i >= 0; i--)
8811 devlink_trap_unregister(devlink, &traps[i]);
8812 mutex_unlock(&devlink->lock);
8813}
8814EXPORT_SYMBOL_GPL(devlink_traps_unregister);
8815
8816static void
8817devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
8818 size_t skb_len)
8819{
8820 struct devlink_stats *stats;
8821
8822 stats = this_cpu_ptr(trap_stats);
8823 u64_stats_update_begin(&stats->syncp);
8824 stats->rx_bytes += skb_len;
8825 stats->rx_packets++;
8826 u64_stats_update_end(&stats->syncp);
8827}
8828
8829static void
8830devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata,
8831 const struct devlink_trap_item *trap_item,
Jiri Pirko5a2e1062020-02-25 11:45:21 +01008832 struct devlink_port *in_devlink_port,
8833 const struct flow_action_cookie *fa_cookie)
Ido Schimmel0f420b62019-08-17 16:28:17 +03008834{
8835 struct devlink_trap_group_item *group_item = trap_item->group_item;
8836
8837 hw_metadata->trap_group_name = group_item->group->name;
8838 hw_metadata->trap_name = trap_item->trap->name;
Jiri Pirko5a2e1062020-02-25 11:45:21 +01008839 hw_metadata->fa_cookie = fa_cookie;
Ido Schimmel0f420b62019-08-17 16:28:17 +03008840
8841 spin_lock(&in_devlink_port->type_lock);
8842 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
8843 hw_metadata->input_dev = in_devlink_port->type_dev;
8844 spin_unlock(&in_devlink_port->type_lock);
8845}
8846
8847/**
8848 * devlink_trap_report - Report trapped packet to drop monitor.
8849 * @devlink: devlink.
8850 * @skb: Trapped packet.
8851 * @trap_ctx: Trap context.
8852 * @in_devlink_port: Input devlink port.
Jiri Pirko5a2e1062020-02-25 11:45:21 +01008853 * @fa_cookie: Flow action cookie. Could be NULL.
Ido Schimmel0f420b62019-08-17 16:28:17 +03008854 */
8855void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
Jiri Pirko5a2e1062020-02-25 11:45:21 +01008856 void *trap_ctx, struct devlink_port *in_devlink_port,
8857 const struct flow_action_cookie *fa_cookie)
8858
Ido Schimmel0f420b62019-08-17 16:28:17 +03008859{
8860 struct devlink_trap_item *trap_item = trap_ctx;
8861 struct net_dm_hw_metadata hw_metadata = {};
8862
8863 devlink_trap_stats_update(trap_item->stats, skb->len);
8864 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
8865
Ido Schimmel30a4e9a2020-05-29 21:36:40 +03008866 /* Control packets were not dropped by the device or encountered an
8867 * exception during forwarding and therefore should not be reported to
8868 * the kernel's drop monitor.
8869 */
8870 if (trap_item->trap->type == DEVLINK_TRAP_TYPE_CONTROL)
8871 return;
8872
Ido Schimmel0f420b62019-08-17 16:28:17 +03008873 devlink_trap_report_metadata_fill(&hw_metadata, trap_item,
Jiri Pirko5a2e1062020-02-25 11:45:21 +01008874 in_devlink_port, fa_cookie);
Ido Schimmel0f420b62019-08-17 16:28:17 +03008875 net_dm_hw_report(skb, &hw_metadata);
8876}
8877EXPORT_SYMBOL_GPL(devlink_trap_report);
8878
8879/**
8880 * devlink_trap_ctx_priv - Trap context to driver private information.
8881 * @trap_ctx: Trap context.
8882 *
8883 * Return: Driver private information passed during registration.
8884 */
8885void *devlink_trap_ctx_priv(void *trap_ctx)
8886{
8887 struct devlink_trap_item *trap_item = trap_ctx;
8888
8889 return trap_item->priv;
8890}
8891EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
8892
Ido Schimmel95ad9552020-03-22 20:48:26 +02008893static int
Ido Schimmelf9f54392020-03-30 22:38:21 +03008894devlink_trap_group_item_policer_link(struct devlink *devlink,
8895 struct devlink_trap_group_item *group_item)
8896{
8897 u32 policer_id = group_item->group->init_policer_id;
8898 struct devlink_trap_policer_item *policer_item;
8899
8900 if (policer_id == 0)
8901 return 0;
8902
8903 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
8904 if (WARN_ON_ONCE(!policer_item))
8905 return -EINVAL;
8906
8907 group_item->policer_item = policer_item;
8908
8909 return 0;
8910}
8911
8912static int
Ido Schimmel95ad9552020-03-22 20:48:26 +02008913devlink_trap_group_register(struct devlink *devlink,
8914 const struct devlink_trap_group *group)
8915{
8916 struct devlink_trap_group_item *group_item;
8917 int err;
8918
8919 if (devlink_trap_group_item_lookup(devlink, group->name))
8920 return -EEXIST;
8921
8922 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
8923 if (!group_item)
8924 return -ENOMEM;
8925
8926 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
8927 if (!group_item->stats) {
8928 err = -ENOMEM;
8929 goto err_stats_alloc;
8930 }
8931
8932 group_item->group = group;
Ido Schimmel95ad9552020-03-22 20:48:26 +02008933
Ido Schimmelf9f54392020-03-30 22:38:21 +03008934 err = devlink_trap_group_item_policer_link(devlink, group_item);
8935 if (err)
8936 goto err_policer_link;
8937
Ido Schimmel95ad9552020-03-22 20:48:26 +02008938 if (devlink->ops->trap_group_init) {
8939 err = devlink->ops->trap_group_init(devlink, group);
8940 if (err)
8941 goto err_group_init;
8942 }
8943
8944 list_add_tail(&group_item->list, &devlink->trap_group_list);
8945 devlink_trap_group_notify(devlink, group_item,
8946 DEVLINK_CMD_TRAP_GROUP_NEW);
8947
8948 return 0;
8949
8950err_group_init:
Ido Schimmelf9f54392020-03-30 22:38:21 +03008951err_policer_link:
Ido Schimmel95ad9552020-03-22 20:48:26 +02008952 free_percpu(group_item->stats);
8953err_stats_alloc:
8954 kfree(group_item);
8955 return err;
8956}
8957
8958static void
8959devlink_trap_group_unregister(struct devlink *devlink,
8960 const struct devlink_trap_group *group)
8961{
8962 struct devlink_trap_group_item *group_item;
8963
8964 group_item = devlink_trap_group_item_lookup(devlink, group->name);
8965 if (WARN_ON_ONCE(!group_item))
8966 return;
8967
8968 devlink_trap_group_notify(devlink, group_item,
8969 DEVLINK_CMD_TRAP_GROUP_DEL);
8970 list_del(&group_item->list);
8971 free_percpu(group_item->stats);
8972 kfree(group_item);
8973}
8974
8975/**
8976 * devlink_trap_groups_register - Register packet trap groups with devlink.
8977 * @devlink: devlink.
8978 * @groups: Packet trap groups.
8979 * @groups_count: Count of provided packet trap groups.
8980 *
8981 * Return: Non-zero value on failure.
8982 */
8983int devlink_trap_groups_register(struct devlink *devlink,
8984 const struct devlink_trap_group *groups,
8985 size_t groups_count)
8986{
8987 int i, err;
8988
8989 mutex_lock(&devlink->lock);
8990 for (i = 0; i < groups_count; i++) {
8991 const struct devlink_trap_group *group = &groups[i];
8992
8993 err = devlink_trap_group_verify(group);
8994 if (err)
8995 goto err_trap_group_verify;
8996
8997 err = devlink_trap_group_register(devlink, group);
8998 if (err)
8999 goto err_trap_group_register;
9000 }
9001 mutex_unlock(&devlink->lock);
9002
9003 return 0;
9004
9005err_trap_group_register:
9006err_trap_group_verify:
9007 for (i--; i >= 0; i--)
9008 devlink_trap_group_unregister(devlink, &groups[i]);
9009 mutex_unlock(&devlink->lock);
9010 return err;
9011}
9012EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9013
9014/**
9015 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9016 * @devlink: devlink.
9017 * @groups: Packet trap groups.
9018 * @groups_count: Count of provided packet trap groups.
9019 */
9020void devlink_trap_groups_unregister(struct devlink *devlink,
9021 const struct devlink_trap_group *groups,
9022 size_t groups_count)
9023{
9024 int i;
9025
9026 mutex_lock(&devlink->lock);
9027 for (i = groups_count - 1; i >= 0; i--)
9028 devlink_trap_group_unregister(devlink, &groups[i]);
9029 mutex_unlock(&devlink->lock);
9030}
9031EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
9032
Ido Schimmel1e8c6612020-03-30 22:38:18 +03009033static void
9034devlink_trap_policer_notify(struct devlink *devlink,
9035 const struct devlink_trap_policer_item *policer_item,
9036 enum devlink_command cmd)
9037{
9038 struct sk_buff *msg;
9039 int err;
9040
9041 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
9042 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
9043
9044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9045 if (!msg)
9046 return;
9047
9048 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
9049 0, 0);
9050 if (err) {
9051 nlmsg_free(msg);
9052 return;
9053 }
9054
9055 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9056 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9057}
9058
9059static int
9060devlink_trap_policer_register(struct devlink *devlink,
9061 const struct devlink_trap_policer *policer)
9062{
9063 struct devlink_trap_policer_item *policer_item;
9064 int err;
9065
9066 if (devlink_trap_policer_item_lookup(devlink, policer->id))
9067 return -EEXIST;
9068
9069 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
9070 if (!policer_item)
9071 return -ENOMEM;
9072
9073 policer_item->policer = policer;
9074 policer_item->rate = policer->init_rate;
9075 policer_item->burst = policer->init_burst;
9076
9077 if (devlink->ops->trap_policer_init) {
9078 err = devlink->ops->trap_policer_init(devlink, policer);
9079 if (err)
9080 goto err_policer_init;
9081 }
9082
9083 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
9084 devlink_trap_policer_notify(devlink, policer_item,
9085 DEVLINK_CMD_TRAP_POLICER_NEW);
9086
9087 return 0;
9088
9089err_policer_init:
9090 kfree(policer_item);
9091 return err;
9092}
9093
9094static void
9095devlink_trap_policer_unregister(struct devlink *devlink,
9096 const struct devlink_trap_policer *policer)
9097{
9098 struct devlink_trap_policer_item *policer_item;
9099
9100 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
9101 if (WARN_ON_ONCE(!policer_item))
9102 return;
9103
9104 devlink_trap_policer_notify(devlink, policer_item,
9105 DEVLINK_CMD_TRAP_POLICER_DEL);
9106 list_del(&policer_item->list);
9107 if (devlink->ops->trap_policer_fini)
9108 devlink->ops->trap_policer_fini(devlink, policer);
9109 kfree(policer_item);
9110}
9111
9112/**
9113 * devlink_trap_policers_register - Register packet trap policers with devlink.
9114 * @devlink: devlink.
9115 * @policers: Packet trap policers.
9116 * @policers_count: Count of provided packet trap policers.
9117 *
9118 * Return: Non-zero value on failure.
9119 */
9120int
9121devlink_trap_policers_register(struct devlink *devlink,
9122 const struct devlink_trap_policer *policers,
9123 size_t policers_count)
9124{
9125 int i, err;
9126
9127 mutex_lock(&devlink->lock);
9128 for (i = 0; i < policers_count; i++) {
9129 const struct devlink_trap_policer *policer = &policers[i];
9130
9131 if (WARN_ON(policer->id == 0 ||
9132 policer->max_rate < policer->min_rate ||
9133 policer->max_burst < policer->min_burst)) {
9134 err = -EINVAL;
9135 goto err_trap_policer_verify;
9136 }
9137
9138 err = devlink_trap_policer_register(devlink, policer);
9139 if (err)
9140 goto err_trap_policer_register;
9141 }
9142 mutex_unlock(&devlink->lock);
9143
9144 return 0;
9145
9146err_trap_policer_register:
9147err_trap_policer_verify:
9148 for (i--; i >= 0; i--)
9149 devlink_trap_policer_unregister(devlink, &policers[i]);
9150 mutex_unlock(&devlink->lock);
9151 return err;
9152}
9153EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
9154
9155/**
9156 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
9157 * @devlink: devlink.
9158 * @policers: Packet trap policers.
9159 * @policers_count: Count of provided packet trap policers.
9160 */
9161void
9162devlink_trap_policers_unregister(struct devlink *devlink,
9163 const struct devlink_trap_policer *policers,
9164 size_t policers_count)
9165{
9166 int i;
9167
9168 mutex_lock(&devlink->lock);
9169 for (i = policers_count - 1; i >= 0; i--)
9170 devlink_trap_policer_unregister(devlink, &policers[i]);
9171 mutex_unlock(&devlink->lock);
9172}
9173EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
9174
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08009175static void __devlink_compat_running_version(struct devlink *devlink,
9176 char *buf, size_t len)
9177{
9178 const struct nlattr *nlattr;
9179 struct devlink_info_req req;
9180 struct sk_buff *msg;
9181 int rem, err;
9182
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08009183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9184 if (!msg)
9185 return;
9186
9187 req.msg = msg;
9188 err = devlink->ops->info_get(devlink, &req, NULL);
9189 if (err)
9190 goto free_msg;
9191
9192 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
9193 const struct nlattr *kv;
9194 int rem_kv;
9195
9196 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
9197 continue;
9198
9199 nla_for_each_nested(kv, nlattr, rem_kv) {
9200 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
9201 continue;
9202
9203 strlcat(buf, nla_data(kv), len);
9204 strlcat(buf, " ", len);
9205 }
9206 }
9207free_msg:
9208 nlmsg_free(msg);
9209}
9210
9211void devlink_compat_running_version(struct net_device *dev,
9212 char *buf, size_t len)
9213{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08009214 struct devlink *devlink;
9215
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009216 dev_hold(dev);
9217 rtnl_unlock();
9218
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08009219 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08009220 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01009221 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08009222
9223 mutex_lock(&devlink->lock);
9224 __devlink_compat_running_version(devlink, buf, len);
9225 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009226
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01009227out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009228 rtnl_lock();
9229 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08009230}
9231
Jakub Kicinski4eceba12019-02-14 13:40:45 -08009232int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
9233{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08009234 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01009235 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08009236
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009237 dev_hold(dev);
9238 rtnl_unlock();
9239
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08009240 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01009241 if (!devlink || !devlink->ops->flash_update) {
9242 ret = -EOPNOTSUPP;
9243 goto out;
9244 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08009245
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08009246 mutex_lock(&devlink->lock);
9247 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
9248 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009249
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01009250out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08009251 rtnl_lock();
9252 dev_put(dev);
9253
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08009254 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08009255}
9256
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01009257int devlink_compat_phys_port_name_get(struct net_device *dev,
9258 char *name, size_t len)
9259{
9260 struct devlink_port *devlink_port;
9261
9262 /* RTNL mutex is held here which ensures that devlink_port
9263 * instance cannot disappear in the middle. No need to take
9264 * any devlink lock as only permanent values are accessed.
9265 */
9266 ASSERT_RTNL();
9267
9268 devlink_port = netdev_to_devlink_port(dev);
9269 if (!devlink_port)
9270 return -EOPNOTSUPP;
9271
9272 return __devlink_port_phys_port_name_get(devlink_port, name, len);
9273}
9274
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009275int devlink_compat_switch_id_get(struct net_device *dev,
9276 struct netdev_phys_item_id *ppid)
9277{
9278 struct devlink_port *devlink_port;
9279
Vlad Buslov043b8412019-08-12 20:02:02 +03009280 /* Caller must hold RTNL mutex or reference to dev, which ensures that
9281 * devlink_port instance cannot disappear in the middle. No need to take
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009282 * any devlink lock as only permanent values are accessed.
9283 */
Jiri Pirko7e1146e2019-04-03 14:24:17 +02009284 devlink_port = netdev_to_devlink_port(dev);
9285 if (!devlink_port || !devlink_port->attrs.switch_port)
9286 return -EOPNOTSUPP;
9287
9288 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
9289
9290 return 0;
9291}
9292
Jiri Pirko070c63f2019-10-03 11:49:39 +02009293static void __net_exit devlink_pernet_pre_exit(struct net *net)
9294{
9295 struct devlink *devlink;
9296 int err;
9297
9298 /* In case network namespace is getting destroyed, reload
9299 * all devlink instances from this namespace into init_net.
9300 */
9301 mutex_lock(&devlink_mutex);
9302 list_for_each_entry(devlink, &devlink_list, list) {
9303 if (net_eq(devlink_net(devlink), net)) {
9304 if (WARN_ON(!devlink_reload_supported(devlink)))
9305 continue;
9306 err = devlink_reload(devlink, &init_net, NULL);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01009307 if (err && err != -EOPNOTSUPP)
Jiri Pirko070c63f2019-10-03 11:49:39 +02009308 pr_warn("Failed to reload devlink instance into init_net\n");
9309 }
9310 }
9311 mutex_unlock(&devlink_mutex);
9312}
9313
9314static struct pernet_operations devlink_pernet_ops __net_initdata = {
9315 .pre_exit = devlink_pernet_pre_exit,
9316};
9317
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08009318static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01009319{
Jiri Pirko070c63f2019-10-03 11:49:39 +02009320 int err;
9321
9322 err = genl_register_family(&devlink_nl_family);
9323 if (err)
9324 goto out;
9325 err = register_pernet_subsys(&devlink_pernet_ops);
9326
9327out:
9328 WARN_ON(err);
9329 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01009330}
9331
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08009332subsys_initcall(devlink_init);