blob: 9bbe2162f22ff3c2e68933805f0c6d475423bc8d [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;
347 const char *name;
348 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;
357 devlink_snapshot_data_dest_t *data_destructor;
Alex Veskerd7e52722018-07-12 15:13:10 +0300358 u8 *data;
359 u32 id;
360};
361
Alex Veskerb16ebe92018-07-12 15:13:08 +0300362static struct devlink_region *
363devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
364{
365 struct devlink_region *region;
366
367 list_for_each_entry(region, &devlink->region_list, list)
368 if (!strcmp(region->name, region_name))
369 return region;
370
371 return NULL;
372}
373
Alex Veskerd7e52722018-07-12 15:13:10 +0300374static struct devlink_snapshot *
375devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
376{
377 struct devlink_snapshot *snapshot;
378
379 list_for_each_entry(snapshot, &region->snapshot_list, list)
380 if (snapshot->id == id)
381 return snapshot;
382
383 return NULL;
384}
385
Jiri Pirko1fc22572016-04-08 19:12:48 +0200386#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
387#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200388#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100389
390/* The per devlink instance lock is taken by default in the pre-doit
391 * operation, yet several commands do not require this. The global
392 * devlink lock is taken and protects from disruption by user-calls.
393 */
394#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100395
396static int devlink_nl_pre_doit(const struct genl_ops *ops,
397 struct sk_buff *skb, struct genl_info *info)
398{
399 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100400 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100401
402 mutex_lock(&devlink_mutex);
403 devlink = devlink_get_from_info(info);
404 if (IS_ERR(devlink)) {
405 mutex_unlock(&devlink_mutex);
406 return PTR_ERR(devlink);
407 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100408 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
409 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200410 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
411 info->user_ptr[0] = devlink;
412 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100413 struct devlink_port *devlink_port;
414
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100415 devlink_port = devlink_port_get_from_info(devlink, info);
416 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100417 err = PTR_ERR(devlink_port);
418 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100419 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200420 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100421 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200422 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
423 struct devlink_sb *devlink_sb;
424
425 devlink_sb = devlink_sb_get_from_info(devlink, info);
426 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100427 err = PTR_ERR(devlink_sb);
428 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200429 }
430 info->user_ptr[1] = devlink_sb;
431 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100432 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100433
434unlock:
435 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
436 mutex_unlock(&devlink->lock);
437 mutex_unlock(&devlink_mutex);
438 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100439}
440
441static void devlink_nl_post_doit(const struct genl_ops *ops,
442 struct sk_buff *skb, struct genl_info *info)
443{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100444 struct devlink *devlink;
445
Jiri Pirko070c63f2019-10-03 11:49:39 +0200446 /* When devlink changes netns, it would not be found
447 * by devlink_get_from_info(). So try if it is stored first.
448 */
449 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
450 devlink = info->user_ptr[0];
451 } else {
452 devlink = devlink_get_from_info(info);
453 WARN_ON(IS_ERR(devlink));
454 }
455 if (!IS_ERR(devlink) && ~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100456 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100457 mutex_unlock(&devlink_mutex);
458}
459
Johannes Berg489111e2016-10-24 14:40:03 +0200460static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100461
462enum devlink_multicast_groups {
463 DEVLINK_MCGRP_CONFIG,
464};
465
466static const struct genl_multicast_group devlink_nl_mcgrps[] = {
467 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
468};
469
470static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
471{
472 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
473 return -EMSGSIZE;
474 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
475 return -EMSGSIZE;
476 return 0;
477}
478
479static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
480 enum devlink_command cmd, u32 portid,
481 u32 seq, int flags)
482{
483 void *hdr;
484
485 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
486 if (!hdr)
487 return -EMSGSIZE;
488
489 if (devlink_nl_put_handle(msg, devlink))
490 goto nla_put_failure;
Jiri Pirko2670ac22019-09-12 10:49:46 +0200491 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
492 goto nla_put_failure;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100493
494 genlmsg_end(msg, hdr);
495 return 0;
496
497nla_put_failure:
498 genlmsg_cancel(msg, hdr);
499 return -EMSGSIZE;
500}
501
502static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
503{
504 struct sk_buff *msg;
505 int err;
506
507 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
508
509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
510 if (!msg)
511 return;
512
513 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
514 if (err) {
515 nlmsg_free(msg);
516 return;
517 }
518
519 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
520 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
521}
522
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200523static int devlink_nl_port_attrs_put(struct sk_buff *msg,
524 struct devlink_port *devlink_port)
525{
526 struct devlink_port_attrs *attrs = &devlink_port->attrs;
527
528 if (!attrs->set)
529 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200530 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
531 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500532 switch (devlink_port->attrs.flavour) {
533 case DEVLINK_PORT_FLAVOUR_PCI_PF:
Parav Pandit98fd2d62019-07-08 23:17:37 -0500534 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
535 attrs->pci_pf.pf))
536 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500537 break;
538 case DEVLINK_PORT_FLAVOUR_PCI_VF:
Parav Pandite41b6bf2019-07-08 23:17:38 -0500539 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
540 attrs->pci_vf.pf) ||
541 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER,
542 attrs->pci_vf.vf))
543 return -EMSGSIZE;
Parav Pandit58b6be42019-08-30 05:39:45 -0500544 break;
545 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
546 case DEVLINK_PORT_FLAVOUR_CPU:
547 case DEVLINK_PORT_FLAVOUR_DSA:
548 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,
2106 const char *table_name)
2107{
2108 struct devlink_dpipe_table *table;
2109
2110 list_for_each_entry_rcu(table, dpipe_tables, list) {
2111 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,
2229 table_name);
2230 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,
2385 table_name);
2386 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) {
2712 NL_SET_ERR_MSG(info->extack, "multiple netns identifying attributes specified");
2713 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)) {
2730 NL_SET_ERR_MSG(info->extack, "Unknown network namespace");
2731 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
2794 err = devlink->ops->reload_down(devlink, !!dest_net, extack);
2795 if (err)
2796 return err;
2797
2798 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
2799 devlink_reload_netns_change(devlink, dest_net);
2800
2801 err = devlink->ops->reload_up(devlink, extack);
2802 devlink_reload_failed_set(devlink, !!err);
2803 return err;
2804}
2805
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002806static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2807{
2808 struct devlink *devlink = info->user_ptr[0];
Jiri Pirko070c63f2019-10-03 11:49:39 +02002809 struct net *dest_net = NULL;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002810 int err;
2811
Jiri Pirko97691062019-09-12 10:49:45 +02002812 if (!devlink_reload_supported(devlink))
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002813 return -EOPNOTSUPP;
2814
2815 err = devlink_resources_validate(devlink, NULL, info);
2816 if (err) {
2817 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2818 return err;
2819 }
Jiri Pirko070c63f2019-10-03 11:49:39 +02002820
2821 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
2822 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
2823 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
2824 dest_net = devlink_netns_get(skb, info);
2825 if (IS_ERR(dest_net))
2826 return PTR_ERR(dest_net);
2827 }
2828
2829 err = devlink_reload(devlink, dest_net, info->extack);
2830
2831 if (dest_net)
2832 put_net(dest_net);
2833
Jiri Pirko2670ac22019-09-12 10:49:46 +02002834 return err;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002835}
2836
Jiri Pirko191ed202019-06-04 15:40:40 +02002837static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2838 struct devlink *devlink,
2839 enum devlink_command cmd,
2840 const char *status_msg,
2841 const char *component,
2842 unsigned long done, unsigned long total)
2843{
2844 void *hdr;
2845
2846 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2847 if (!hdr)
2848 return -EMSGSIZE;
2849
2850 if (devlink_nl_put_handle(msg, devlink))
2851 goto nla_put_failure;
2852
2853 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2854 goto out;
2855
2856 if (status_msg &&
2857 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2858 status_msg))
2859 goto nla_put_failure;
2860 if (component &&
2861 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2862 component))
2863 goto nla_put_failure;
2864 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2865 done, DEVLINK_ATTR_PAD))
2866 goto nla_put_failure;
2867 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2868 total, DEVLINK_ATTR_PAD))
2869 goto nla_put_failure;
2870
2871out:
2872 genlmsg_end(msg, hdr);
2873 return 0;
2874
2875nla_put_failure:
2876 genlmsg_cancel(msg, hdr);
2877 return -EMSGSIZE;
2878}
2879
2880static void __devlink_flash_update_notify(struct devlink *devlink,
2881 enum devlink_command cmd,
2882 const char *status_msg,
2883 const char *component,
2884 unsigned long done,
2885 unsigned long total)
2886{
2887 struct sk_buff *msg;
2888 int err;
2889
2890 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2891 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2892 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2893
2894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2895 if (!msg)
2896 return;
2897
2898 err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2899 component, done, total);
2900 if (err)
2901 goto out_free_msg;
2902
2903 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2904 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2905 return;
2906
2907out_free_msg:
2908 nlmsg_free(msg);
2909}
2910
2911void devlink_flash_update_begin_notify(struct devlink *devlink)
2912{
2913 __devlink_flash_update_notify(devlink,
2914 DEVLINK_CMD_FLASH_UPDATE,
2915 NULL, NULL, 0, 0);
2916}
2917EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2918
2919void devlink_flash_update_end_notify(struct devlink *devlink)
2920{
2921 __devlink_flash_update_notify(devlink,
2922 DEVLINK_CMD_FLASH_UPDATE_END,
2923 NULL, NULL, 0, 0);
2924}
2925EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2926
2927void devlink_flash_update_status_notify(struct devlink *devlink,
2928 const char *status_msg,
2929 const char *component,
2930 unsigned long done,
2931 unsigned long total)
2932{
2933 __devlink_flash_update_notify(devlink,
2934 DEVLINK_CMD_FLASH_UPDATE_STATUS,
2935 status_msg, component, done, total);
2936}
2937EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2938
Jakub Kicinski76726cc2019-02-14 13:40:44 -08002939static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2940 struct genl_info *info)
2941{
2942 struct devlink *devlink = info->user_ptr[0];
2943 const char *file_name, *component;
2944 struct nlattr *nla_component;
2945
2946 if (!devlink->ops->flash_update)
2947 return -EOPNOTSUPP;
2948
2949 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2950 return -EINVAL;
2951 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2952
2953 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2954 component = nla_component ? nla_data(nla_component) : NULL;
2955
2956 return devlink->ops->flash_update(devlink, file_name, component,
2957 info->extack);
2958}
2959
Moshe Shemesh036467c2018-07-04 14:30:33 +03002960static const struct devlink_param devlink_param_generic[] = {
2961 {
2962 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2963 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2964 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2965 },
2966 {
2967 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2968 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2969 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2970 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002971 {
2972 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2973 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2974 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2975 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002976 {
2977 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2978 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2979 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2980 },
Vasundhara Volame3b51062018-10-04 11:13:44 +05302981 {
2982 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2983 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2984 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2985 },
Vasundhara Volamf61cba42018-10-04 11:13:45 +05302986 {
2987 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2988 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2989 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2990 },
Vasundhara Volam16511782018-10-04 11:13:46 +05302991 {
2992 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2993 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2994 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2995 },
Shalom Toledo846e9802018-12-03 07:58:59 +00002996 {
2997 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2998 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2999 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3000 },
Dirk van der Merwe5bbd21d2019-09-09 00:54:18 +01003001 {
3002 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3003 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3004 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3005 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03003006};
Moshe Shemesheabaef12018-07-04 14:30:28 +03003007
3008static int devlink_param_generic_verify(const struct devlink_param *param)
3009{
3010 /* verify it match generic parameter by id and name */
3011 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3012 return -EINVAL;
3013 if (strcmp(param->name, devlink_param_generic[param->id].name))
3014 return -ENOENT;
3015
3016 WARN_ON(param->type != devlink_param_generic[param->id].type);
3017
3018 return 0;
3019}
3020
3021static int devlink_param_driver_verify(const struct devlink_param *param)
3022{
3023 int i;
3024
3025 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3026 return -EINVAL;
3027 /* verify no such name in generic params */
3028 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3029 if (!strcmp(param->name, devlink_param_generic[i].name))
3030 return -EEXIST;
3031
3032 return 0;
3033}
3034
3035static struct devlink_param_item *
3036devlink_param_find_by_name(struct list_head *param_list,
3037 const char *param_name)
3038{
3039 struct devlink_param_item *param_item;
3040
3041 list_for_each_entry(param_item, param_list, list)
3042 if (!strcmp(param_item->param->name, param_name))
3043 return param_item;
3044 return NULL;
3045}
3046
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03003047static struct devlink_param_item *
3048devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3049{
3050 struct devlink_param_item *param_item;
3051
3052 list_for_each_entry(param_item, param_list, list)
3053 if (param_item->param->id == param_id)
3054 return param_item;
3055 return NULL;
3056}
3057
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003058static bool
3059devlink_param_cmode_is_supported(const struct devlink_param *param,
3060 enum devlink_param_cmode cmode)
3061{
3062 return test_bit(cmode, &param->supported_cmodes);
3063}
3064
3065static int devlink_param_get(struct devlink *devlink,
3066 const struct devlink_param *param,
3067 struct devlink_param_gset_ctx *ctx)
3068{
3069 if (!param->get)
3070 return -EOPNOTSUPP;
3071 return param->get(devlink, param->id, ctx);
3072}
3073
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003074static int devlink_param_set(struct devlink *devlink,
3075 const struct devlink_param *param,
3076 struct devlink_param_gset_ctx *ctx)
3077{
3078 if (!param->set)
3079 return -EOPNOTSUPP;
3080 return param->set(devlink, param->id, ctx);
3081}
3082
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003083static int
3084devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3085{
3086 switch (param_type) {
3087 case DEVLINK_PARAM_TYPE_U8:
3088 return NLA_U8;
3089 case DEVLINK_PARAM_TYPE_U16:
3090 return NLA_U16;
3091 case DEVLINK_PARAM_TYPE_U32:
3092 return NLA_U32;
3093 case DEVLINK_PARAM_TYPE_STRING:
3094 return NLA_STRING;
3095 case DEVLINK_PARAM_TYPE_BOOL:
3096 return NLA_FLAG;
3097 default:
3098 return -EINVAL;
3099 }
3100}
3101
3102static int
3103devlink_nl_param_value_fill_one(struct sk_buff *msg,
3104 enum devlink_param_type type,
3105 enum devlink_param_cmode cmode,
3106 union devlink_param_value val)
3107{
3108 struct nlattr *param_value_attr;
3109
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003110 param_value_attr = nla_nest_start_noflag(msg,
3111 DEVLINK_ATTR_PARAM_VALUE);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003112 if (!param_value_attr)
3113 goto nla_put_failure;
3114
3115 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3116 goto value_nest_cancel;
3117
3118 switch (type) {
3119 case DEVLINK_PARAM_TYPE_U8:
3120 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3121 goto value_nest_cancel;
3122 break;
3123 case DEVLINK_PARAM_TYPE_U16:
3124 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3125 goto value_nest_cancel;
3126 break;
3127 case DEVLINK_PARAM_TYPE_U32:
3128 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3129 goto value_nest_cancel;
3130 break;
3131 case DEVLINK_PARAM_TYPE_STRING:
3132 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3133 val.vstr))
3134 goto value_nest_cancel;
3135 break;
3136 case DEVLINK_PARAM_TYPE_BOOL:
3137 if (val.vbool &&
3138 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3139 goto value_nest_cancel;
3140 break;
3141 }
3142
3143 nla_nest_end(msg, param_value_attr);
3144 return 0;
3145
3146value_nest_cancel:
3147 nla_nest_cancel(msg, param_value_attr);
3148nla_put_failure:
3149 return -EMSGSIZE;
3150}
3151
3152static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303153 unsigned int port_index,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003154 struct devlink_param_item *param_item,
3155 enum devlink_command cmd,
3156 u32 portid, u32 seq, int flags)
3157{
3158 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003159 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003160 const struct devlink_param *param = param_item->param;
3161 struct devlink_param_gset_ctx ctx;
3162 struct nlattr *param_values_list;
3163 struct nlattr *param_attr;
3164 int nla_type;
3165 void *hdr;
3166 int err;
3167 int i;
3168
3169 /* Get value from driver part to driverinit configuration mode */
3170 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3171 if (!devlink_param_cmode_is_supported(param, i))
3172 continue;
3173 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3174 if (!param_item->driverinit_value_valid)
3175 return -EOPNOTSUPP;
3176 param_value[i] = param_item->driverinit_value;
3177 } else {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003178 if (!param_item->published)
3179 continue;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003180 ctx.cmode = i;
3181 err = devlink_param_get(devlink, param, &ctx);
3182 if (err)
3183 return err;
3184 param_value[i] = ctx.val;
3185 }
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003186 param_value_set[i] = true;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003187 }
3188
3189 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3190 if (!hdr)
3191 return -EMSGSIZE;
3192
3193 if (devlink_nl_put_handle(msg, devlink))
3194 goto genlmsg_cancel;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303195
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303196 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3197 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3198 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303199 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3200 goto genlmsg_cancel;
3201
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003202 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003203 if (!param_attr)
3204 goto genlmsg_cancel;
3205 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3206 goto param_nest_cancel;
3207 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3208 goto param_nest_cancel;
3209
3210 nla_type = devlink_param_type_to_nla_type(param->type);
3211 if (nla_type < 0)
3212 goto param_nest_cancel;
3213 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3214 goto param_nest_cancel;
3215
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003216 param_values_list = nla_nest_start_noflag(msg,
3217 DEVLINK_ATTR_PARAM_VALUES_LIST);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003218 if (!param_values_list)
3219 goto param_nest_cancel;
3220
3221 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003222 if (!param_value_set[i])
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003223 continue;
3224 err = devlink_nl_param_value_fill_one(msg, param->type,
3225 i, param_value[i]);
3226 if (err)
3227 goto values_list_nest_cancel;
3228 }
3229
3230 nla_nest_end(msg, param_values_list);
3231 nla_nest_end(msg, param_attr);
3232 genlmsg_end(msg, hdr);
3233 return 0;
3234
3235values_list_nest_cancel:
3236 nla_nest_end(msg, param_values_list);
3237param_nest_cancel:
3238 nla_nest_cancel(msg, param_attr);
3239genlmsg_cancel:
3240 genlmsg_cancel(msg, hdr);
3241 return -EMSGSIZE;
3242}
3243
Moshe Shemeshea601e12018-07-04 14:30:32 +03003244static void devlink_param_notify(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303245 unsigned int port_index,
Moshe Shemeshea601e12018-07-04 14:30:32 +03003246 struct devlink_param_item *param_item,
3247 enum devlink_command cmd)
3248{
3249 struct sk_buff *msg;
3250 int err;
3251
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303252 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3253 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3254 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003255
3256 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3257 if (!msg)
3258 return;
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303259 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3260 0, 0, 0);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003261 if (err) {
3262 nlmsg_free(msg);
3263 return;
3264 }
3265
3266 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3267 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3268}
3269
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003270static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3271 struct netlink_callback *cb)
3272{
3273 struct devlink_param_item *param_item;
3274 struct devlink *devlink;
3275 int start = cb->args[0];
3276 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003277 int err = 0;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003278
3279 mutex_lock(&devlink_mutex);
3280 list_for_each_entry(devlink, &devlink_list, list) {
3281 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3282 continue;
3283 mutex_lock(&devlink->lock);
3284 list_for_each_entry(param_item, &devlink->param_list, list) {
3285 if (idx < start) {
3286 idx++;
3287 continue;
3288 }
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303289 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003290 DEVLINK_CMD_PARAM_GET,
3291 NETLINK_CB(cb->skb).portid,
3292 cb->nlh->nlmsg_seq,
3293 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303294 if (err && err != -EOPNOTSUPP) {
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003295 mutex_unlock(&devlink->lock);
3296 goto out;
3297 }
3298 idx++;
3299 }
3300 mutex_unlock(&devlink->lock);
3301 }
3302out:
3303 mutex_unlock(&devlink_mutex);
3304
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003305 if (err != -EMSGSIZE)
3306 return err;
3307
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003308 cb->args[0] = idx;
3309 return msg->len;
3310}
3311
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003312static int
3313devlink_param_type_get_from_info(struct genl_info *info,
3314 enum devlink_param_type *param_type)
3315{
3316 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3317 return -EINVAL;
3318
3319 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3320 case NLA_U8:
3321 *param_type = DEVLINK_PARAM_TYPE_U8;
3322 break;
3323 case NLA_U16:
3324 *param_type = DEVLINK_PARAM_TYPE_U16;
3325 break;
3326 case NLA_U32:
3327 *param_type = DEVLINK_PARAM_TYPE_U32;
3328 break;
3329 case NLA_STRING:
3330 *param_type = DEVLINK_PARAM_TYPE_STRING;
3331 break;
3332 case NLA_FLAG:
3333 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3334 break;
3335 default:
3336 return -EINVAL;
3337 }
3338
3339 return 0;
3340}
3341
3342static int
3343devlink_param_value_get_from_info(const struct devlink_param *param,
3344 struct genl_info *info,
3345 union devlink_param_value *value)
3346{
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003347 int len;
3348
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003349 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3350 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3351 return -EINVAL;
3352
3353 switch (param->type) {
3354 case DEVLINK_PARAM_TYPE_U8:
3355 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3356 break;
3357 case DEVLINK_PARAM_TYPE_U16:
3358 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3359 break;
3360 case DEVLINK_PARAM_TYPE_U32:
3361 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3362 break;
3363 case DEVLINK_PARAM_TYPE_STRING:
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003364 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3365 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3366 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03003367 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003368 return -EINVAL;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003369 strcpy(value->vstr,
3370 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003371 break;
3372 case DEVLINK_PARAM_TYPE_BOOL:
3373 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3374 true : false;
3375 break;
3376 }
3377 return 0;
3378}
3379
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003380static struct devlink_param_item *
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303381devlink_param_get_from_info(struct list_head *param_list,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003382 struct genl_info *info)
3383{
3384 char *param_name;
3385
3386 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3387 return NULL;
3388
3389 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303390 return devlink_param_find_by_name(param_list, param_name);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003391}
3392
3393static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3394 struct genl_info *info)
3395{
3396 struct devlink *devlink = info->user_ptr[0];
3397 struct devlink_param_item *param_item;
3398 struct sk_buff *msg;
3399 int err;
3400
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303401 param_item = devlink_param_get_from_info(&devlink->param_list, info);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003402 if (!param_item)
3403 return -EINVAL;
3404
3405 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3406 if (!msg)
3407 return -ENOMEM;
3408
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303409 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003410 DEVLINK_CMD_PARAM_GET,
3411 info->snd_portid, info->snd_seq, 0);
3412 if (err) {
3413 nlmsg_free(msg);
3414 return err;
3415 }
3416
3417 return genlmsg_reply(msg, info);
3418}
3419
Vasundhara Volam9c548732019-01-28 18:00:22 +05303420static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303421 unsigned int port_index,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303422 struct list_head *param_list,
3423 struct genl_info *info,
3424 enum devlink_command cmd)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003425{
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003426 enum devlink_param_type param_type;
3427 struct devlink_param_gset_ctx ctx;
3428 enum devlink_param_cmode cmode;
3429 struct devlink_param_item *param_item;
3430 const struct devlink_param *param;
3431 union devlink_param_value value;
3432 int err = 0;
3433
Vasundhara Volam9c548732019-01-28 18:00:22 +05303434 param_item = devlink_param_get_from_info(param_list, info);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003435 if (!param_item)
3436 return -EINVAL;
3437 param = param_item->param;
3438 err = devlink_param_type_get_from_info(info, &param_type);
3439 if (err)
3440 return err;
3441 if (param_type != param->type)
3442 return -EINVAL;
3443 err = devlink_param_value_get_from_info(param, info, &value);
3444 if (err)
3445 return err;
3446 if (param->validate) {
3447 err = param->validate(devlink, param->id, value, info->extack);
3448 if (err)
3449 return err;
3450 }
3451
3452 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3453 return -EINVAL;
3454 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3455 if (!devlink_param_cmode_is_supported(param, cmode))
3456 return -EOPNOTSUPP;
3457
3458 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
Moshe Shemesh12765342018-10-10 16:09:26 +03003459 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3460 strcpy(param_item->driverinit_value.vstr, value.vstr);
3461 else
3462 param_item->driverinit_value = value;
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003463 param_item->driverinit_value_valid = true;
3464 } else {
3465 if (!param->set)
3466 return -EOPNOTSUPP;
3467 ctx.val = value;
3468 ctx.cmode = cmode;
3469 err = devlink_param_set(devlink, param, &ctx);
3470 if (err)
3471 return err;
3472 }
3473
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303474 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003475 return 0;
3476}
3477
Vasundhara Volam9c548732019-01-28 18:00:22 +05303478static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3479 struct genl_info *info)
3480{
3481 struct devlink *devlink = info->user_ptr[0];
3482
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303483 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303484 info, DEVLINK_CMD_PARAM_NEW);
3485}
3486
Moshe Shemesheabaef12018-07-04 14:30:28 +03003487static int devlink_param_register_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303488 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303489 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303490 const struct devlink_param *param,
3491 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003492{
3493 struct devlink_param_item *param_item;
3494
Vasundhara Volam39e61602019-01-28 18:00:20 +05303495 if (devlink_param_find_by_name(param_list, param->name))
Moshe Shemesheabaef12018-07-04 14:30:28 +03003496 return -EEXIST;
3497
3498 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3499 WARN_ON(param->get || param->set);
3500 else
3501 WARN_ON(!param->get || !param->set);
3502
3503 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3504 if (!param_item)
3505 return -ENOMEM;
3506 param_item->param = param;
3507
Vasundhara Volam39e61602019-01-28 18:00:20 +05303508 list_add_tail(&param_item->list, param_list);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303509 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003510 return 0;
3511}
3512
3513static void devlink_param_unregister_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303514 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303515 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303516 const struct devlink_param *param,
3517 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003518{
3519 struct devlink_param_item *param_item;
3520
Vasundhara Volam39e61602019-01-28 18:00:20 +05303521 param_item = devlink_param_find_by_name(param_list, param->name);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003522 WARN_ON(!param_item);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303523 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003524 list_del(&param_item->list);
3525 kfree(param_item);
3526}
3527
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303528static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3529 struct netlink_callback *cb)
3530{
3531 struct devlink_param_item *param_item;
3532 struct devlink_port *devlink_port;
3533 struct devlink *devlink;
3534 int start = cb->args[0];
3535 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003536 int err = 0;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303537
3538 mutex_lock(&devlink_mutex);
3539 list_for_each_entry(devlink, &devlink_list, list) {
3540 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3541 continue;
3542 mutex_lock(&devlink->lock);
3543 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3544 list_for_each_entry(param_item,
3545 &devlink_port->param_list, list) {
3546 if (idx < start) {
3547 idx++;
3548 continue;
3549 }
3550 err = devlink_nl_param_fill(msg,
3551 devlink_port->devlink,
3552 devlink_port->index, param_item,
3553 DEVLINK_CMD_PORT_PARAM_GET,
3554 NETLINK_CB(cb->skb).portid,
3555 cb->nlh->nlmsg_seq,
3556 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303557 if (err && err != -EOPNOTSUPP) {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303558 mutex_unlock(&devlink->lock);
3559 goto out;
3560 }
3561 idx++;
3562 }
3563 }
3564 mutex_unlock(&devlink->lock);
3565 }
3566out:
3567 mutex_unlock(&devlink_mutex);
3568
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003569 if (err != -EMSGSIZE)
3570 return err;
3571
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303572 cb->args[0] = idx;
3573 return msg->len;
3574}
3575
3576static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3577 struct genl_info *info)
3578{
3579 struct devlink_port *devlink_port = info->user_ptr[0];
3580 struct devlink_param_item *param_item;
3581 struct sk_buff *msg;
3582 int err;
3583
3584 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3585 info);
3586 if (!param_item)
3587 return -EINVAL;
3588
3589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3590 if (!msg)
3591 return -ENOMEM;
3592
3593 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3594 devlink_port->index, param_item,
3595 DEVLINK_CMD_PORT_PARAM_GET,
3596 info->snd_portid, info->snd_seq, 0);
3597 if (err) {
3598 nlmsg_free(msg);
3599 return err;
3600 }
3601
3602 return genlmsg_reply(msg, info);
3603}
3604
Vasundhara Volam9c548732019-01-28 18:00:22 +05303605static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3606 struct genl_info *info)
3607{
3608 struct devlink_port *devlink_port = info->user_ptr[0];
3609
3610 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303611 devlink_port->index,
3612 &devlink_port->param_list, info,
3613 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam9c548732019-01-28 18:00:22 +05303614}
3615
Alex Veskera006d462018-07-12 15:13:12 +03003616static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3617 struct devlink *devlink,
3618 struct devlink_snapshot *snapshot)
3619{
3620 struct nlattr *snap_attr;
3621 int err;
3622
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003623 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
Alex Veskera006d462018-07-12 15:13:12 +03003624 if (!snap_attr)
3625 return -EINVAL;
3626
3627 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3628 if (err)
3629 goto nla_put_failure;
3630
3631 nla_nest_end(msg, snap_attr);
3632 return 0;
3633
3634nla_put_failure:
3635 nla_nest_cancel(msg, snap_attr);
3636 return err;
3637}
3638
3639static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3640 struct devlink *devlink,
3641 struct devlink_region *region)
3642{
3643 struct devlink_snapshot *snapshot;
3644 struct nlattr *snapshots_attr;
3645 int err;
3646
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003647 snapshots_attr = nla_nest_start_noflag(msg,
3648 DEVLINK_ATTR_REGION_SNAPSHOTS);
Alex Veskera006d462018-07-12 15:13:12 +03003649 if (!snapshots_attr)
3650 return -EINVAL;
3651
3652 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3653 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3654 if (err)
3655 goto nla_put_failure;
3656 }
3657
3658 nla_nest_end(msg, snapshots_attr);
3659 return 0;
3660
3661nla_put_failure:
3662 nla_nest_cancel(msg, snapshots_attr);
3663 return err;
3664}
3665
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003666static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3667 enum devlink_command cmd, u32 portid,
3668 u32 seq, int flags,
3669 struct devlink_region *region)
3670{
3671 void *hdr;
3672 int err;
3673
3674 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3675 if (!hdr)
3676 return -EMSGSIZE;
3677
3678 err = devlink_nl_put_handle(msg, devlink);
3679 if (err)
3680 goto nla_put_failure;
3681
3682 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3683 if (err)
3684 goto nla_put_failure;
3685
3686 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3687 region->size,
3688 DEVLINK_ATTR_PAD);
3689 if (err)
3690 goto nla_put_failure;
3691
Alex Veskera006d462018-07-12 15:13:12 +03003692 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3693 if (err)
3694 goto nla_put_failure;
3695
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003696 genlmsg_end(msg, hdr);
3697 return 0;
3698
3699nla_put_failure:
3700 genlmsg_cancel(msg, hdr);
3701 return err;
3702}
3703
Alex Vesker866319b2018-07-12 15:13:13 +03003704static void devlink_nl_region_notify(struct devlink_region *region,
3705 struct devlink_snapshot *snapshot,
3706 enum devlink_command cmd)
3707{
3708 struct devlink *devlink = region->devlink;
3709 struct sk_buff *msg;
3710 void *hdr;
3711 int err;
3712
3713 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3714
3715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3716 if (!msg)
3717 return;
3718
3719 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3720 if (!hdr)
3721 goto out_free_msg;
3722
3723 err = devlink_nl_put_handle(msg, devlink);
3724 if (err)
3725 goto out_cancel_msg;
3726
3727 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3728 region->name);
3729 if (err)
3730 goto out_cancel_msg;
3731
3732 if (snapshot) {
3733 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3734 snapshot->id);
3735 if (err)
3736 goto out_cancel_msg;
3737 } else {
3738 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3739 region->size, DEVLINK_ATTR_PAD);
3740 if (err)
3741 goto out_cancel_msg;
3742 }
3743 genlmsg_end(msg, hdr);
3744
3745 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3746 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3747
3748 return;
3749
3750out_cancel_msg:
3751 genlmsg_cancel(msg, hdr);
3752out_free_msg:
3753 nlmsg_free(msg);
3754}
3755
Jiri Pirko92b49822019-08-12 14:28:31 +02003756static void devlink_region_snapshot_del(struct devlink_region *region,
3757 struct devlink_snapshot *snapshot)
3758{
3759 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3760 region->cur_snapshots--;
3761 list_del(&snapshot->list);
3762 (*snapshot->data_destructor)(snapshot->data);
3763 kfree(snapshot);
3764}
3765
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003766static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3767 struct genl_info *info)
3768{
3769 struct devlink *devlink = info->user_ptr[0];
3770 struct devlink_region *region;
3771 const char *region_name;
3772 struct sk_buff *msg;
3773 int err;
3774
3775 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3776 return -EINVAL;
3777
3778 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3779 region = devlink_region_get_by_name(devlink, region_name);
3780 if (!region)
3781 return -EINVAL;
3782
3783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3784 if (!msg)
3785 return -ENOMEM;
3786
3787 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3788 info->snd_portid, info->snd_seq, 0,
3789 region);
3790 if (err) {
3791 nlmsg_free(msg);
3792 return err;
3793 }
3794
3795 return genlmsg_reply(msg, info);
3796}
3797
3798static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3799 struct netlink_callback *cb)
3800{
3801 struct devlink_region *region;
3802 struct devlink *devlink;
3803 int start = cb->args[0];
3804 int idx = 0;
3805 int err;
3806
3807 mutex_lock(&devlink_mutex);
3808 list_for_each_entry(devlink, &devlink_list, list) {
3809 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3810 continue;
3811
3812 mutex_lock(&devlink->lock);
3813 list_for_each_entry(region, &devlink->region_list, list) {
3814 if (idx < start) {
3815 idx++;
3816 continue;
3817 }
3818 err = devlink_nl_region_fill(msg, devlink,
3819 DEVLINK_CMD_REGION_GET,
3820 NETLINK_CB(cb->skb).portid,
3821 cb->nlh->nlmsg_seq,
3822 NLM_F_MULTI, region);
3823 if (err) {
3824 mutex_unlock(&devlink->lock);
3825 goto out;
3826 }
3827 idx++;
3828 }
3829 mutex_unlock(&devlink->lock);
3830 }
3831out:
3832 mutex_unlock(&devlink_mutex);
3833 cb->args[0] = idx;
3834 return msg->len;
3835}
3836
Alex Vesker866319b2018-07-12 15:13:13 +03003837static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3838 struct genl_info *info)
3839{
3840 struct devlink *devlink = info->user_ptr[0];
3841 struct devlink_snapshot *snapshot;
3842 struct devlink_region *region;
3843 const char *region_name;
3844 u32 snapshot_id;
3845
3846 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3847 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3848 return -EINVAL;
3849
3850 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3851 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3852
3853 region = devlink_region_get_by_name(devlink, region_name);
3854 if (!region)
3855 return -EINVAL;
3856
3857 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3858 if (!snapshot)
3859 return -EINVAL;
3860
Jiri Pirko92b49822019-08-12 14:28:31 +02003861 devlink_region_snapshot_del(region, snapshot);
Alex Vesker866319b2018-07-12 15:13:13 +03003862 return 0;
3863}
3864
Alex Vesker4e547952018-07-12 15:13:14 +03003865static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3866 struct devlink *devlink,
3867 u8 *chunk, u32 chunk_size,
3868 u64 addr)
3869{
3870 struct nlattr *chunk_attr;
3871 int err;
3872
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003873 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
Alex Vesker4e547952018-07-12 15:13:14 +03003874 if (!chunk_attr)
3875 return -EINVAL;
3876
3877 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3878 if (err)
3879 goto nla_put_failure;
3880
3881 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3882 DEVLINK_ATTR_PAD);
3883 if (err)
3884 goto nla_put_failure;
3885
3886 nla_nest_end(msg, chunk_attr);
3887 return 0;
3888
3889nla_put_failure:
3890 nla_nest_cancel(msg, chunk_attr);
3891 return err;
3892}
3893
3894#define DEVLINK_REGION_READ_CHUNK_SIZE 256
3895
3896static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3897 struct devlink *devlink,
3898 struct devlink_region *region,
3899 struct nlattr **attrs,
3900 u64 start_offset,
3901 u64 end_offset,
3902 bool dump,
3903 u64 *new_offset)
3904{
3905 struct devlink_snapshot *snapshot;
3906 u64 curr_offset = start_offset;
3907 u32 snapshot_id;
3908 int err = 0;
3909
3910 *new_offset = start_offset;
3911
3912 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3913 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3914 if (!snapshot)
3915 return -EINVAL;
3916
Jiri Pirko3a5e5232019-08-09 15:27:15 +02003917 if (end_offset > region->size || dump)
3918 end_offset = region->size;
Alex Vesker4e547952018-07-12 15:13:14 +03003919
3920 while (curr_offset < end_offset) {
3921 u32 data_size;
3922 u8 *data;
3923
3924 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3925 data_size = end_offset - curr_offset;
3926 else
3927 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3928
3929 data = &snapshot->data[curr_offset];
3930 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3931 data, data_size,
3932 curr_offset);
3933 if (err)
3934 break;
3935
3936 curr_offset += data_size;
3937 }
3938 *new_offset = curr_offset;
3939
3940 return err;
3941}
3942
3943static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3944 struct netlink_callback *cb)
3945{
Jiri Pirkoee85da52019-10-05 20:04:42 +02003946 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Alex Vesker4e547952018-07-12 15:13:14 +03003947 u64 ret_offset, start_offset, end_offset = 0;
Jiri Pirkoee85da52019-10-05 20:04:42 +02003948 struct nlattr **attrs = info->attrs;
Alex Vesker4e547952018-07-12 15:13:14 +03003949 struct devlink_region *region;
3950 struct nlattr *chunks_attr;
3951 const char *region_name;
3952 struct devlink *devlink;
3953 bool dump = true;
3954 void *hdr;
3955 int err;
3956
3957 start_offset = *((u64 *)&cb->args[0]);
3958
Parav Panditdac7c082019-02-12 14:24:08 -06003959 mutex_lock(&devlink_mutex);
Alex Vesker4e547952018-07-12 15:13:14 +03003960 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003961 if (IS_ERR(devlink)) {
3962 err = PTR_ERR(devlink);
Parav Panditdac7c082019-02-12 14:24:08 -06003963 goto out_dev;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003964 }
Alex Vesker4e547952018-07-12 15:13:14 +03003965
Alex Vesker4e547952018-07-12 15:13:14 +03003966 mutex_lock(&devlink->lock);
3967
3968 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
Parav Panditfdd41ec2019-02-12 14:23:58 -06003969 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3970 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003971 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003972 }
Alex Vesker4e547952018-07-12 15:13:14 +03003973
3974 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3975 region = devlink_region_get_by_name(devlink, region_name);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003976 if (!region) {
3977 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003978 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003979 }
Alex Vesker4e547952018-07-12 15:13:14 +03003980
3981 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3982 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3983 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003984 if (!hdr) {
3985 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003986 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003987 }
Alex Vesker4e547952018-07-12 15:13:14 +03003988
3989 err = devlink_nl_put_handle(skb, devlink);
3990 if (err)
3991 goto nla_put_failure;
3992
3993 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3994 if (err)
3995 goto nla_put_failure;
3996
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003997 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003998 if (!chunks_attr) {
3999 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004000 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004001 }
Alex Vesker4e547952018-07-12 15:13:14 +03004002
4003 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4004 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4005 if (!start_offset)
4006 start_offset =
4007 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4008
4009 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4010 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4011 dump = false;
4012 }
4013
4014 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4015 region, attrs,
4016 start_offset,
4017 end_offset, dump,
4018 &ret_offset);
4019
4020 if (err && err != -EMSGSIZE)
4021 goto nla_put_failure;
4022
4023 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06004024 if (ret_offset == start_offset) {
4025 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004026 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004027 }
Alex Vesker4e547952018-07-12 15:13:14 +03004028
4029 *((u64 *)&cb->args[0]) = ret_offset;
4030
4031 nla_nest_end(skb, chunks_attr);
4032 genlmsg_end(skb, hdr);
4033 mutex_unlock(&devlink->lock);
4034 mutex_unlock(&devlink_mutex);
4035
4036 return skb->len;
4037
4038nla_put_failure:
4039 genlmsg_cancel(skb, hdr);
4040out_unlock:
4041 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06004042out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03004043 mutex_unlock(&devlink_mutex);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004044 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03004045}
4046
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004047struct devlink_info_req {
4048 struct sk_buff *msg;
4049};
4050
4051int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
4052{
4053 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
4054}
4055EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
4056
4057int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
4058{
4059 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
4060}
4061EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
4062
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004063static int devlink_info_version_put(struct devlink_info_req *req, int attr,
4064 const char *version_name,
4065 const char *version_value)
4066{
4067 struct nlattr *nest;
4068 int err;
4069
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004070 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004071 if (!nest)
4072 return -EMSGSIZE;
4073
4074 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
4075 version_name);
4076 if (err)
4077 goto nla_put_failure;
4078
4079 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
4080 version_value);
4081 if (err)
4082 goto nla_put_failure;
4083
4084 nla_nest_end(req->msg, nest);
4085
4086 return 0;
4087
4088nla_put_failure:
4089 nla_nest_cancel(req->msg, nest);
4090 return err;
4091}
4092
4093int devlink_info_version_fixed_put(struct devlink_info_req *req,
4094 const char *version_name,
4095 const char *version_value)
4096{
4097 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
4098 version_name, version_value);
4099}
4100EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
4101
4102int devlink_info_version_stored_put(struct devlink_info_req *req,
4103 const char *version_name,
4104 const char *version_value)
4105{
4106 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
4107 version_name, version_value);
4108}
4109EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
4110
4111int devlink_info_version_running_put(struct devlink_info_req *req,
4112 const char *version_name,
4113 const char *version_value)
4114{
4115 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4116 version_name, version_value);
4117}
4118EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4119
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004120static int
4121devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4122 enum devlink_command cmd, u32 portid,
4123 u32 seq, int flags, struct netlink_ext_ack *extack)
4124{
4125 struct devlink_info_req req;
4126 void *hdr;
4127 int err;
4128
4129 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4130 if (!hdr)
4131 return -EMSGSIZE;
4132
4133 err = -EMSGSIZE;
4134 if (devlink_nl_put_handle(msg, devlink))
4135 goto err_cancel_msg;
4136
4137 req.msg = msg;
4138 err = devlink->ops->info_get(devlink, &req, extack);
4139 if (err)
4140 goto err_cancel_msg;
4141
4142 genlmsg_end(msg, hdr);
4143 return 0;
4144
4145err_cancel_msg:
4146 genlmsg_cancel(msg, hdr);
4147 return err;
4148}
4149
4150static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4151 struct genl_info *info)
4152{
4153 struct devlink *devlink = info->user_ptr[0];
4154 struct sk_buff *msg;
4155 int err;
4156
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08004157 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004158 return -EOPNOTSUPP;
4159
4160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4161 if (!msg)
4162 return -ENOMEM;
4163
4164 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4165 info->snd_portid, info->snd_seq, 0,
4166 info->extack);
4167 if (err) {
4168 nlmsg_free(msg);
4169 return err;
4170 }
4171
4172 return genlmsg_reply(msg, info);
4173}
4174
4175static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4176 struct netlink_callback *cb)
4177{
4178 struct devlink *devlink;
4179 int start = cb->args[0];
4180 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004181 int err = 0;
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004182
4183 mutex_lock(&devlink_mutex);
4184 list_for_each_entry(devlink, &devlink_list, list) {
4185 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4186 continue;
4187 if (idx < start) {
4188 idx++;
4189 continue;
4190 }
4191
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004192 if (!devlink->ops->info_get) {
4193 idx++;
4194 continue;
4195 }
4196
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004197 mutex_lock(&devlink->lock);
4198 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4199 NETLINK_CB(cb->skb).portid,
4200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4201 cb->extack);
4202 mutex_unlock(&devlink->lock);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05304203 if (err && err != -EOPNOTSUPP)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004204 break;
4205 idx++;
4206 }
4207 mutex_unlock(&devlink_mutex);
4208
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004209 if (err != -EMSGSIZE)
4210 return err;
4211
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004212 cb->args[0] = idx;
4213 return msg->len;
4214}
4215
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004216struct devlink_fmsg_item {
4217 struct list_head list;
4218 int attrtype;
4219 u8 nla_type;
4220 u16 len;
4221 int value[0];
4222};
4223
4224struct devlink_fmsg {
4225 struct list_head item_list;
4226};
4227
4228static struct devlink_fmsg *devlink_fmsg_alloc(void)
4229{
4230 struct devlink_fmsg *fmsg;
4231
4232 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4233 if (!fmsg)
4234 return NULL;
4235
4236 INIT_LIST_HEAD(&fmsg->item_list);
4237
4238 return fmsg;
4239}
4240
4241static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4242{
4243 struct devlink_fmsg_item *item, *tmp;
4244
4245 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4246 list_del(&item->list);
4247 kfree(item);
4248 }
4249 kfree(fmsg);
4250}
4251
4252static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4253 int attrtype)
4254{
4255 struct devlink_fmsg_item *item;
4256
4257 item = kzalloc(sizeof(*item), GFP_KERNEL);
4258 if (!item)
4259 return -ENOMEM;
4260
4261 item->attrtype = attrtype;
4262 list_add_tail(&item->list, &fmsg->item_list);
4263
4264 return 0;
4265}
4266
4267int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4268{
4269 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4270}
4271EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4272
4273static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4274{
4275 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4276}
4277
4278int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4279{
4280 return devlink_fmsg_nest_end(fmsg);
4281}
4282EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4283
4284#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4285
4286static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4287{
4288 struct devlink_fmsg_item *item;
4289
4290 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4291 return -EMSGSIZE;
4292
4293 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4294 if (!item)
4295 return -ENOMEM;
4296
4297 item->nla_type = NLA_NUL_STRING;
4298 item->len = strlen(name) + 1;
4299 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4300 memcpy(&item->value, name, item->len);
4301 list_add_tail(&item->list, &fmsg->item_list);
4302
4303 return 0;
4304}
4305
4306int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4307{
4308 int err;
4309
4310 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4311 if (err)
4312 return err;
4313
4314 err = devlink_fmsg_put_name(fmsg, name);
4315 if (err)
4316 return err;
4317
4318 return 0;
4319}
4320EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4321
4322int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4323{
4324 return devlink_fmsg_nest_end(fmsg);
4325}
4326EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4327
4328int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4329 const char *name)
4330{
4331 int err;
4332
4333 err = devlink_fmsg_pair_nest_start(fmsg, name);
4334 if (err)
4335 return err;
4336
4337 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4338 if (err)
4339 return err;
4340
4341 return 0;
4342}
4343EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4344
4345int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4346{
4347 int err;
4348
4349 err = devlink_fmsg_nest_end(fmsg);
4350 if (err)
4351 return err;
4352
4353 err = devlink_fmsg_nest_end(fmsg);
4354 if (err)
4355 return err;
4356
4357 return 0;
4358}
4359EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4360
4361static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4362 const void *value, u16 value_len,
4363 u8 value_nla_type)
4364{
4365 struct devlink_fmsg_item *item;
4366
4367 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4368 return -EMSGSIZE;
4369
4370 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4371 if (!item)
4372 return -ENOMEM;
4373
4374 item->nla_type = value_nla_type;
4375 item->len = value_len;
4376 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4377 memcpy(&item->value, value, item->len);
4378 list_add_tail(&item->list, &fmsg->item_list);
4379
4380 return 0;
4381}
4382
4383int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4384{
4385 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4386}
4387EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4388
4389int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4390{
4391 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4392}
4393EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4394
4395int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4396{
4397 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4398}
4399EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4400
4401int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4402{
4403 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4404}
4405EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4406
4407int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4408{
4409 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4410 NLA_NUL_STRING);
4411}
4412EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4413
4414int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4415 u16 value_len)
4416{
4417 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4418}
4419EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4420
4421int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4422 bool value)
4423{
4424 int err;
4425
4426 err = devlink_fmsg_pair_nest_start(fmsg, name);
4427 if (err)
4428 return err;
4429
4430 err = devlink_fmsg_bool_put(fmsg, value);
4431 if (err)
4432 return err;
4433
4434 err = devlink_fmsg_pair_nest_end(fmsg);
4435 if (err)
4436 return err;
4437
4438 return 0;
4439}
4440EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4441
4442int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4443 u8 value)
4444{
4445 int err;
4446
4447 err = devlink_fmsg_pair_nest_start(fmsg, name);
4448 if (err)
4449 return err;
4450
4451 err = devlink_fmsg_u8_put(fmsg, value);
4452 if (err)
4453 return err;
4454
4455 err = devlink_fmsg_pair_nest_end(fmsg);
4456 if (err)
4457 return err;
4458
4459 return 0;
4460}
4461EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4462
4463int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4464 u32 value)
4465{
4466 int err;
4467
4468 err = devlink_fmsg_pair_nest_start(fmsg, name);
4469 if (err)
4470 return err;
4471
4472 err = devlink_fmsg_u32_put(fmsg, value);
4473 if (err)
4474 return err;
4475
4476 err = devlink_fmsg_pair_nest_end(fmsg);
4477 if (err)
4478 return err;
4479
4480 return 0;
4481}
4482EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4483
4484int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4485 u64 value)
4486{
4487 int err;
4488
4489 err = devlink_fmsg_pair_nest_start(fmsg, name);
4490 if (err)
4491 return err;
4492
4493 err = devlink_fmsg_u64_put(fmsg, value);
4494 if (err)
4495 return err;
4496
4497 err = devlink_fmsg_pair_nest_end(fmsg);
4498 if (err)
4499 return err;
4500
4501 return 0;
4502}
4503EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4504
4505int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4506 const char *value)
4507{
4508 int err;
4509
4510 err = devlink_fmsg_pair_nest_start(fmsg, name);
4511 if (err)
4512 return err;
4513
4514 err = devlink_fmsg_string_put(fmsg, value);
4515 if (err)
4516 return err;
4517
4518 err = devlink_fmsg_pair_nest_end(fmsg);
4519 if (err)
4520 return err;
4521
4522 return 0;
4523}
4524EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4525
4526int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4527 const void *value, u16 value_len)
4528{
4529 int err;
4530
4531 err = devlink_fmsg_pair_nest_start(fmsg, name);
4532 if (err)
4533 return err;
4534
4535 err = devlink_fmsg_binary_put(fmsg, value, value_len);
4536 if (err)
4537 return err;
4538
4539 err = devlink_fmsg_pair_nest_end(fmsg);
4540 if (err)
4541 return err;
4542
4543 return 0;
4544}
4545EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4546
4547static int
4548devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4549{
4550 switch (msg->nla_type) {
4551 case NLA_FLAG:
4552 case NLA_U8:
4553 case NLA_U32:
4554 case NLA_U64:
4555 case NLA_NUL_STRING:
4556 case NLA_BINARY:
4557 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4558 msg->nla_type);
4559 default:
4560 return -EINVAL;
4561 }
4562}
4563
4564static int
4565devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4566{
4567 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4568 u8 tmp;
4569
4570 switch (msg->nla_type) {
4571 case NLA_FLAG:
4572 /* Always provide flag data, regardless of its value */
4573 tmp = *(bool *) msg->value;
4574
4575 return nla_put_u8(skb, attrtype, tmp);
4576 case NLA_U8:
4577 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4578 case NLA_U32:
4579 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4580 case NLA_U64:
4581 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4582 DEVLINK_ATTR_PAD);
4583 case NLA_NUL_STRING:
4584 return nla_put_string(skb, attrtype, (char *) &msg->value);
4585 case NLA_BINARY:
4586 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4587 default:
4588 return -EINVAL;
4589 }
4590}
4591
4592static int
4593devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4594 int *start)
4595{
4596 struct devlink_fmsg_item *item;
4597 struct nlattr *fmsg_nlattr;
4598 int i = 0;
4599 int err;
4600
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004601 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004602 if (!fmsg_nlattr)
4603 return -EMSGSIZE;
4604
4605 list_for_each_entry(item, &fmsg->item_list, list) {
4606 if (i < *start) {
4607 i++;
4608 continue;
4609 }
4610
4611 switch (item->attrtype) {
4612 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4613 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4614 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4615 case DEVLINK_ATTR_FMSG_NEST_END:
4616 err = nla_put_flag(skb, item->attrtype);
4617 break;
4618 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4619 err = devlink_fmsg_item_fill_type(item, skb);
4620 if (err)
4621 break;
4622 err = devlink_fmsg_item_fill_data(item, skb);
4623 break;
4624 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4625 err = nla_put_string(skb, item->attrtype,
4626 (char *) &item->value);
4627 break;
4628 default:
4629 err = -EINVAL;
4630 break;
4631 }
4632 if (!err)
4633 *start = ++i;
4634 else
4635 break;
4636 }
4637
4638 nla_nest_end(skb, fmsg_nlattr);
4639 return err;
4640}
4641
4642static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4643 struct genl_info *info,
4644 enum devlink_command cmd, int flags)
4645{
4646 struct nlmsghdr *nlh;
4647 struct sk_buff *skb;
4648 bool last = false;
4649 int index = 0;
4650 void *hdr;
4651 int err;
4652
4653 while (!last) {
4654 int tmp_index = index;
4655
4656 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4657 if (!skb)
4658 return -ENOMEM;
4659
4660 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4661 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4662 if (!hdr) {
4663 err = -EMSGSIZE;
4664 goto nla_put_failure;
4665 }
4666
4667 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4668 if (!err)
4669 last = true;
4670 else if (err != -EMSGSIZE || tmp_index == index)
4671 goto nla_put_failure;
4672
4673 genlmsg_end(skb, hdr);
4674 err = genlmsg_reply(skb, info);
4675 if (err)
4676 return err;
4677 }
4678
4679 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4680 if (!skb)
4681 return -ENOMEM;
4682 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4683 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4684 if (!nlh) {
4685 err = -EMSGSIZE;
4686 goto nla_put_failure;
4687 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004688
Li RongQingfde55ea2019-02-11 19:09:07 +08004689 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004690
4691nla_put_failure:
4692 nlmsg_free(skb);
4693 return err;
4694}
4695
Aya Levine44ef4e2019-05-16 09:49:20 +03004696static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4697 struct netlink_callback *cb,
4698 enum devlink_command cmd)
4699{
4700 int index = cb->args[0];
4701 int tmp_index = index;
4702 void *hdr;
4703 int err;
4704
4705 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4706 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4707 if (!hdr) {
4708 err = -EMSGSIZE;
4709 goto nla_put_failure;
4710 }
4711
4712 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4713 if ((err && err != -EMSGSIZE) || tmp_index == index)
4714 goto nla_put_failure;
4715
4716 cb->args[0] = index;
4717 genlmsg_end(skb, hdr);
4718 return skb->len;
4719
4720nla_put_failure:
4721 genlmsg_cancel(skb, hdr);
4722 return err;
4723}
4724
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004725struct devlink_health_reporter {
4726 struct list_head list;
4727 void *priv;
4728 const struct devlink_health_reporter_ops *ops;
4729 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004730 struct devlink_fmsg *dump_fmsg;
4731 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004732 u64 graceful_period;
4733 bool auto_recover;
4734 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004735 u64 dump_ts;
4736 u64 error_count;
4737 u64 recovery_count;
4738 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004739 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004740};
4741
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004742void *
4743devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4744{
4745 return reporter->priv;
4746}
4747EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4748
4749static struct devlink_health_reporter *
4750devlink_health_reporter_find_by_name(struct devlink *devlink,
4751 const char *reporter_name)
4752{
4753 struct devlink_health_reporter *reporter;
4754
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004755 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004756 list_for_each_entry(reporter, &devlink->reporter_list, list)
4757 if (!strcmp(reporter->ops->name, reporter_name))
4758 return reporter;
4759 return NULL;
4760}
4761
4762/**
4763 * devlink_health_reporter_create - create devlink health reporter
4764 *
4765 * @devlink: devlink
4766 * @ops: ops
4767 * @graceful_period: to avoid recovery loops, in msecs
4768 * @auto_recover: auto recover when error occurs
4769 * @priv: priv
4770 */
4771struct devlink_health_reporter *
4772devlink_health_reporter_create(struct devlink *devlink,
4773 const struct devlink_health_reporter_ops *ops,
4774 u64 graceful_period, bool auto_recover,
4775 void *priv)
4776{
4777 struct devlink_health_reporter *reporter;
4778
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004779 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004780 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4781 reporter = ERR_PTR(-EEXIST);
4782 goto unlock;
4783 }
4784
4785 if (WARN_ON(auto_recover && !ops->recover) ||
4786 WARN_ON(graceful_period && !ops->recover)) {
4787 reporter = ERR_PTR(-EINVAL);
4788 goto unlock;
4789 }
4790
4791 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4792 if (!reporter) {
4793 reporter = ERR_PTR(-ENOMEM);
4794 goto unlock;
4795 }
4796
4797 reporter->priv = priv;
4798 reporter->ops = ops;
4799 reporter->devlink = devlink;
4800 reporter->graceful_period = graceful_period;
4801 reporter->auto_recover = auto_recover;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004802 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004803 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004804 list_add_tail(&reporter->list, &devlink->reporter_list);
4805unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004806 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004807 return reporter;
4808}
4809EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4810
4811/**
4812 * devlink_health_reporter_destroy - destroy devlink health reporter
4813 *
4814 * @reporter: devlink health reporter to destroy
4815 */
4816void
4817devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4818{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004819 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004820 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004821 mutex_unlock(&reporter->devlink->reporters_lock);
4822 while (refcount_read(&reporter->refcount) > 1)
4823 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01004824 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004825 if (reporter->dump_fmsg)
4826 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004827 kfree(reporter);
4828}
4829EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4830
Eran Ben Elisha3167b272019-03-03 10:57:30 +02004831void
4832devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4833 enum devlink_health_reporter_state state)
4834{
4835 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4836 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4837 return;
4838
4839 if (reporter->health_state == state)
4840 return;
4841
4842 reporter->health_state = state;
4843 trace_devlink_health_reporter_state_update(reporter->devlink,
4844 reporter->ops->name, state);
4845}
4846EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4847
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004848static int
4849devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004850 void *priv_ctx, struct netlink_ext_ack *extack)
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004851{
4852 int err;
4853
Jiri Pirko40281822019-10-10 15:18:48 +02004854 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
4855 return 0;
4856
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004857 if (!reporter->ops->recover)
4858 return -EOPNOTSUPP;
4859
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004860 err = reporter->ops->recover(reporter, priv_ctx, extack);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004861 if (err)
4862 return err;
4863
4864 reporter->recovery_count++;
4865 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4866 reporter->last_recovery_ts = jiffies;
4867
4868 return 0;
4869}
4870
4871static void
4872devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4873{
4874 if (!reporter->dump_fmsg)
4875 return;
4876 devlink_fmsg_free(reporter->dump_fmsg);
4877 reporter->dump_fmsg = NULL;
4878}
4879
4880static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004881 void *priv_ctx,
4882 struct netlink_ext_ack *extack)
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004883{
4884 int err;
4885
4886 if (!reporter->ops->dump)
4887 return 0;
4888
4889 if (reporter->dump_fmsg)
4890 return 0;
4891
4892 reporter->dump_fmsg = devlink_fmsg_alloc();
4893 if (!reporter->dump_fmsg) {
4894 err = -ENOMEM;
4895 return err;
4896 }
4897
4898 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4899 if (err)
4900 goto dump_err;
4901
4902 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004903 priv_ctx, extack);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004904 if (err)
4905 goto dump_err;
4906
4907 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4908 if (err)
4909 goto dump_err;
4910
4911 reporter->dump_ts = jiffies;
4912
4913 return 0;
4914
4915dump_err:
4916 devlink_health_dump_clear(reporter);
4917 return err;
4918}
4919
4920int devlink_health_report(struct devlink_health_reporter *reporter,
4921 const char *msg, void *priv_ctx)
4922{
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004923 enum devlink_health_reporter_state prev_health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004924 struct devlink *devlink = reporter->devlink;
4925
4926 /* write a log message of the current error */
4927 WARN_ON(!msg);
4928 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4929 reporter->error_count++;
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004930 prev_health_state = reporter->health_state;
4931 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004932
4933 /* abort if the previous error wasn't recovered */
4934 if (reporter->auto_recover &&
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004935 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004936 jiffies - reporter->last_recovery_ts <
4937 msecs_to_jiffies(reporter->graceful_period))) {
4938 trace_devlink_health_recover_aborted(devlink,
4939 reporter->ops->name,
4940 reporter->health_state,
4941 jiffies -
4942 reporter->last_recovery_ts);
4943 return -ECANCELED;
4944 }
4945
4946 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4947
4948 mutex_lock(&reporter->dump_lock);
4949 /* store current dump of current error, for later analysis */
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004950 devlink_health_do_dump(reporter, priv_ctx, NULL);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004951 mutex_unlock(&reporter->dump_lock);
4952
4953 if (reporter->auto_recover)
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004954 return devlink_health_reporter_recover(reporter,
4955 priv_ctx, NULL);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004956
4957 return 0;
4958}
4959EXPORT_SYMBOL_GPL(devlink_health_report);
4960
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004961static struct devlink_health_reporter *
Aya Levine44ef4e2019-05-16 09:49:20 +03004962devlink_health_reporter_get_from_attrs(struct devlink *devlink,
4963 struct nlattr **attrs)
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004964{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004965 struct devlink_health_reporter *reporter;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004966 char *reporter_name;
4967
Aya Levine44ef4e2019-05-16 09:49:20 +03004968 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004969 return NULL;
4970
Aya Levine44ef4e2019-05-16 09:49:20 +03004971 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004972 mutex_lock(&devlink->reporters_lock);
4973 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4974 if (reporter)
4975 refcount_inc(&reporter->refcount);
4976 mutex_unlock(&devlink->reporters_lock);
4977 return reporter;
4978}
4979
Aya Levine44ef4e2019-05-16 09:49:20 +03004980static struct devlink_health_reporter *
4981devlink_health_reporter_get_from_info(struct devlink *devlink,
4982 struct genl_info *info)
4983{
4984 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
4985}
4986
4987static struct devlink_health_reporter *
4988devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
4989{
Jiri Pirkoee85da52019-10-05 20:04:42 +02004990 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Aya Levine44ef4e2019-05-16 09:49:20 +03004991 struct devlink_health_reporter *reporter;
Jiri Pirkoee85da52019-10-05 20:04:42 +02004992 struct nlattr **attrs = info->attrs;
Aya Levine44ef4e2019-05-16 09:49:20 +03004993 struct devlink *devlink;
Aya Levine44ef4e2019-05-16 09:49:20 +03004994
4995 mutex_lock(&devlink_mutex);
4996 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4997 if (IS_ERR(devlink))
4998 goto unlock;
4999
5000 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
5001 mutex_unlock(&devlink_mutex);
Aya Levine44ef4e2019-05-16 09:49:20 +03005002 return reporter;
5003unlock:
5004 mutex_unlock(&devlink_mutex);
Aya Levine44ef4e2019-05-16 09:49:20 +03005005 return NULL;
5006}
5007
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005008static void
5009devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5010{
5011 refcount_dec(&reporter->refcount);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005012}
5013
5014static int
5015devlink_nl_health_reporter_fill(struct sk_buff *msg,
5016 struct devlink *devlink,
5017 struct devlink_health_reporter *reporter,
5018 enum devlink_command cmd, u32 portid,
5019 u32 seq, int flags)
5020{
5021 struct nlattr *reporter_attr;
5022 void *hdr;
5023
5024 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5025 if (!hdr)
5026 return -EMSGSIZE;
5027
5028 if (devlink_nl_put_handle(msg, devlink))
5029 goto genlmsg_cancel;
5030
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005031 reporter_attr = nla_nest_start_noflag(msg,
5032 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005033 if (!reporter_attr)
5034 goto genlmsg_cancel;
5035 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
5036 reporter->ops->name))
5037 goto reporter_nest_cancel;
5038 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
5039 reporter->health_state))
5040 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005041 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005042 reporter->error_count, DEVLINK_ATTR_PAD))
5043 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005044 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005045 reporter->recovery_count, DEVLINK_ATTR_PAD))
5046 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005047 if (reporter->ops->recover &&
5048 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005049 reporter->graceful_period,
5050 DEVLINK_ATTR_PAD))
5051 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005052 if (reporter->ops->recover &&
5053 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005054 reporter->auto_recover))
5055 goto reporter_nest_cancel;
5056 if (reporter->dump_fmsg &&
5057 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
5058 jiffies_to_msecs(reporter->dump_ts),
5059 DEVLINK_ATTR_PAD))
5060 goto reporter_nest_cancel;
5061
5062 nla_nest_end(msg, reporter_attr);
5063 genlmsg_end(msg, hdr);
5064 return 0;
5065
5066reporter_nest_cancel:
5067 nla_nest_end(msg, reporter_attr);
5068genlmsg_cancel:
5069 genlmsg_cancel(msg, hdr);
5070 return -EMSGSIZE;
5071}
5072
5073static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
5074 struct genl_info *info)
5075{
5076 struct devlink *devlink = info->user_ptr[0];
5077 struct devlink_health_reporter *reporter;
5078 struct sk_buff *msg;
5079 int err;
5080
5081 reporter = devlink_health_reporter_get_from_info(devlink, info);
5082 if (!reporter)
5083 return -EINVAL;
5084
5085 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005086 if (!msg) {
5087 err = -ENOMEM;
5088 goto out;
5089 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005090
5091 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5092 DEVLINK_CMD_HEALTH_REPORTER_GET,
5093 info->snd_portid, info->snd_seq,
5094 0);
5095 if (err) {
5096 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005097 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005098 }
5099
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005100 err = genlmsg_reply(msg, info);
5101out:
5102 devlink_health_reporter_put(reporter);
5103 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005104}
5105
5106static int
5107devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5108 struct netlink_callback *cb)
5109{
5110 struct devlink_health_reporter *reporter;
5111 struct devlink *devlink;
5112 int start = cb->args[0];
5113 int idx = 0;
5114 int err;
5115
5116 mutex_lock(&devlink_mutex);
5117 list_for_each_entry(devlink, &devlink_list, list) {
5118 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5119 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005120 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005121 list_for_each_entry(reporter, &devlink->reporter_list,
5122 list) {
5123 if (idx < start) {
5124 idx++;
5125 continue;
5126 }
5127 err = devlink_nl_health_reporter_fill(msg, devlink,
5128 reporter,
5129 DEVLINK_CMD_HEALTH_REPORTER_GET,
5130 NETLINK_CB(cb->skb).portid,
5131 cb->nlh->nlmsg_seq,
5132 NLM_F_MULTI);
5133 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005134 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005135 goto out;
5136 }
5137 idx++;
5138 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005139 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005140 }
5141out:
5142 mutex_unlock(&devlink_mutex);
5143
5144 cb->args[0] = idx;
5145 return msg->len;
5146}
5147
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005148static int
5149devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5150 struct genl_info *info)
5151{
5152 struct devlink *devlink = info->user_ptr[0];
5153 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005154 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005155
5156 reporter = devlink_health_reporter_get_from_info(devlink, info);
5157 if (!reporter)
5158 return -EINVAL;
5159
5160 if (!reporter->ops->recover &&
5161 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005162 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5163 err = -EOPNOTSUPP;
5164 goto out;
5165 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005166
5167 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5168 reporter->graceful_period =
5169 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5170
5171 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5172 reporter->auto_recover =
5173 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5174
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005175 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005176 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005177out:
5178 devlink_health_reporter_put(reporter);
5179 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005180}
5181
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005182static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5183 struct genl_info *info)
5184{
5185 struct devlink *devlink = info->user_ptr[0];
5186 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005187 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005188
5189 reporter = devlink_health_reporter_get_from_info(devlink, info);
5190 if (!reporter)
5191 return -EINVAL;
5192
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005193 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005194
5195 devlink_health_reporter_put(reporter);
5196 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005197}
5198
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005199static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5200 struct genl_info *info)
5201{
5202 struct devlink *devlink = info->user_ptr[0];
5203 struct devlink_health_reporter *reporter;
5204 struct devlink_fmsg *fmsg;
5205 int err;
5206
5207 reporter = devlink_health_reporter_get_from_info(devlink, info);
5208 if (!reporter)
5209 return -EINVAL;
5210
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005211 if (!reporter->ops->diagnose) {
5212 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005213 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005214 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005215
5216 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005217 if (!fmsg) {
5218 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005219 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005220 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005221
5222 err = devlink_fmsg_obj_nest_start(fmsg);
5223 if (err)
5224 goto out;
5225
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005226 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005227 if (err)
5228 goto out;
5229
5230 err = devlink_fmsg_obj_nest_end(fmsg);
5231 if (err)
5232 goto out;
5233
5234 err = devlink_fmsg_snd(fmsg, info,
5235 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5236
5237out:
5238 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005239 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005240 return err;
5241}
5242
Aya Levine44ef4e2019-05-16 09:49:20 +03005243static int
5244devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5245 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005246{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005247 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005248 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005249 int err;
5250
Aya Levine44ef4e2019-05-16 09:49:20 +03005251 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005252 if (!reporter)
5253 return -EINVAL;
5254
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005255 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005256 err = -EOPNOTSUPP;
5257 goto out;
5258 }
5259 mutex_lock(&reporter->dump_lock);
5260 if (!start) {
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005261 err = devlink_health_do_dump(reporter, NULL, cb->extack);
Aya Levine44ef4e2019-05-16 09:49:20 +03005262 if (err)
5263 goto unlock;
5264 cb->args[1] = reporter->dump_ts;
5265 }
5266 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5267 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5268 err = -EAGAIN;
5269 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005270 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005271
Aya Levine44ef4e2019-05-16 09:49:20 +03005272 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5273 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5274unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005275 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005276out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005277 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005278 return err;
5279}
5280
5281static int
5282devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5283 struct genl_info *info)
5284{
5285 struct devlink *devlink = info->user_ptr[0];
5286 struct devlink_health_reporter *reporter;
5287
5288 reporter = devlink_health_reporter_get_from_info(devlink, info);
5289 if (!reporter)
5290 return -EINVAL;
5291
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005292 if (!reporter->ops->dump) {
5293 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005294 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005295 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005296
5297 mutex_lock(&reporter->dump_lock);
5298 devlink_health_dump_clear(reporter);
5299 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005300 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005301 return 0;
5302}
5303
Ido Schimmel0f420b62019-08-17 16:28:17 +03005304struct devlink_stats {
5305 u64 rx_bytes;
5306 u64 rx_packets;
5307 struct u64_stats_sync syncp;
5308};
5309
5310/**
5311 * struct devlink_trap_group_item - Packet trap group attributes.
5312 * @group: Immutable packet trap group attributes.
5313 * @refcount: Number of trap items using the group.
5314 * @list: trap_group_list member.
5315 * @stats: Trap group statistics.
5316 *
5317 * Describes packet trap group attributes. Created by devlink during trap
5318 * registration.
5319 */
5320struct devlink_trap_group_item {
5321 const struct devlink_trap_group *group;
5322 refcount_t refcount;
5323 struct list_head list;
5324 struct devlink_stats __percpu *stats;
5325};
5326
5327/**
5328 * struct devlink_trap_item - Packet trap attributes.
5329 * @trap: Immutable packet trap attributes.
5330 * @group_item: Associated group item.
5331 * @list: trap_list member.
5332 * @action: Trap action.
5333 * @stats: Trap statistics.
5334 * @priv: Driver private information.
5335 *
5336 * Describes both mutable and immutable packet trap attributes. Created by
5337 * devlink during trap registration and used for all trap related operations.
5338 */
5339struct devlink_trap_item {
5340 const struct devlink_trap *trap;
5341 struct devlink_trap_group_item *group_item;
5342 struct list_head list;
5343 enum devlink_trap_action action;
5344 struct devlink_stats __percpu *stats;
5345 void *priv;
5346};
5347
5348static struct devlink_trap_item *
5349devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5350{
5351 struct devlink_trap_item *trap_item;
5352
5353 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5354 if (!strcmp(trap_item->trap->name, name))
5355 return trap_item;
5356 }
5357
5358 return NULL;
5359}
5360
5361static struct devlink_trap_item *
5362devlink_trap_item_get_from_info(struct devlink *devlink,
5363 struct genl_info *info)
5364{
5365 struct nlattr *attr;
5366
5367 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5368 return NULL;
5369 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5370
5371 return devlink_trap_item_lookup(devlink, nla_data(attr));
5372}
5373
5374static int
5375devlink_trap_action_get_from_info(struct genl_info *info,
5376 enum devlink_trap_action *p_trap_action)
5377{
5378 u8 val;
5379
5380 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5381 switch (val) {
5382 case DEVLINK_TRAP_ACTION_DROP: /* fall-through */
5383 case DEVLINK_TRAP_ACTION_TRAP:
5384 *p_trap_action = val;
5385 break;
5386 default:
5387 return -EINVAL;
5388 }
5389
5390 return 0;
5391}
5392
5393static int devlink_trap_metadata_put(struct sk_buff *msg,
5394 const struct devlink_trap *trap)
5395{
5396 struct nlattr *attr;
5397
5398 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5399 if (!attr)
5400 return -EMSGSIZE;
5401
5402 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5403 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5404 goto nla_put_failure;
5405
5406 nla_nest_end(msg, attr);
5407
5408 return 0;
5409
5410nla_put_failure:
5411 nla_nest_cancel(msg, attr);
5412 return -EMSGSIZE;
5413}
5414
5415static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5416 struct devlink_stats *stats)
5417{
5418 int i;
5419
5420 memset(stats, 0, sizeof(*stats));
5421 for_each_possible_cpu(i) {
5422 struct devlink_stats *cpu_stats;
5423 u64 rx_packets, rx_bytes;
5424 unsigned int start;
5425
5426 cpu_stats = per_cpu_ptr(trap_stats, i);
5427 do {
5428 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5429 rx_packets = cpu_stats->rx_packets;
5430 rx_bytes = cpu_stats->rx_bytes;
5431 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5432
5433 stats->rx_packets += rx_packets;
5434 stats->rx_bytes += rx_bytes;
5435 }
5436}
5437
5438static int devlink_trap_stats_put(struct sk_buff *msg,
5439 struct devlink_stats __percpu *trap_stats)
5440{
5441 struct devlink_stats stats;
5442 struct nlattr *attr;
5443
5444 devlink_trap_stats_read(trap_stats, &stats);
5445
5446 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5447 if (!attr)
5448 return -EMSGSIZE;
5449
5450 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5451 stats.rx_packets, DEVLINK_ATTR_PAD))
5452 goto nla_put_failure;
5453
5454 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5455 stats.rx_bytes, DEVLINK_ATTR_PAD))
5456 goto nla_put_failure;
5457
5458 nla_nest_end(msg, attr);
5459
5460 return 0;
5461
5462nla_put_failure:
5463 nla_nest_cancel(msg, attr);
5464 return -EMSGSIZE;
5465}
5466
5467static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5468 const struct devlink_trap_item *trap_item,
5469 enum devlink_command cmd, u32 portid, u32 seq,
5470 int flags)
5471{
5472 struct devlink_trap_group_item *group_item = trap_item->group_item;
5473 void *hdr;
5474 int err;
5475
5476 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5477 if (!hdr)
5478 return -EMSGSIZE;
5479
5480 if (devlink_nl_put_handle(msg, devlink))
5481 goto nla_put_failure;
5482
5483 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5484 group_item->group->name))
5485 goto nla_put_failure;
5486
5487 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5488 goto nla_put_failure;
5489
5490 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5491 goto nla_put_failure;
5492
5493 if (trap_item->trap->generic &&
5494 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5495 goto nla_put_failure;
5496
5497 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5498 goto nla_put_failure;
5499
5500 err = devlink_trap_metadata_put(msg, trap_item->trap);
5501 if (err)
5502 goto nla_put_failure;
5503
5504 err = devlink_trap_stats_put(msg, trap_item->stats);
5505 if (err)
5506 goto nla_put_failure;
5507
5508 genlmsg_end(msg, hdr);
5509
5510 return 0;
5511
5512nla_put_failure:
5513 genlmsg_cancel(msg, hdr);
5514 return -EMSGSIZE;
5515}
5516
5517static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
5518 struct genl_info *info)
5519{
5520 struct netlink_ext_ack *extack = info->extack;
5521 struct devlink *devlink = info->user_ptr[0];
5522 struct devlink_trap_item *trap_item;
5523 struct sk_buff *msg;
5524 int err;
5525
5526 if (list_empty(&devlink->trap_list))
5527 return -EOPNOTSUPP;
5528
5529 trap_item = devlink_trap_item_get_from_info(devlink, info);
5530 if (!trap_item) {
5531 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5532 return -ENOENT;
5533 }
5534
5535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5536 if (!msg)
5537 return -ENOMEM;
5538
5539 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5540 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
5541 info->snd_seq, 0);
5542 if (err)
5543 goto err_trap_fill;
5544
5545 return genlmsg_reply(msg, info);
5546
5547err_trap_fill:
5548 nlmsg_free(msg);
5549 return err;
5550}
5551
5552static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
5553 struct netlink_callback *cb)
5554{
5555 struct devlink_trap_item *trap_item;
5556 struct devlink *devlink;
5557 int start = cb->args[0];
5558 int idx = 0;
5559 int err;
5560
5561 mutex_lock(&devlink_mutex);
5562 list_for_each_entry(devlink, &devlink_list, list) {
5563 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5564 continue;
5565 mutex_lock(&devlink->lock);
5566 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5567 if (idx < start) {
5568 idx++;
5569 continue;
5570 }
5571 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5572 DEVLINK_CMD_TRAP_NEW,
5573 NETLINK_CB(cb->skb).portid,
5574 cb->nlh->nlmsg_seq,
5575 NLM_F_MULTI);
5576 if (err) {
5577 mutex_unlock(&devlink->lock);
5578 goto out;
5579 }
5580 idx++;
5581 }
5582 mutex_unlock(&devlink->lock);
5583 }
5584out:
5585 mutex_unlock(&devlink_mutex);
5586
5587 cb->args[0] = idx;
5588 return msg->len;
5589}
5590
5591static int __devlink_trap_action_set(struct devlink *devlink,
5592 struct devlink_trap_item *trap_item,
5593 enum devlink_trap_action trap_action,
5594 struct netlink_ext_ack *extack)
5595{
5596 int err;
5597
5598 if (trap_item->action != trap_action &&
5599 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
5600 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
5601 return 0;
5602 }
5603
5604 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
5605 trap_action);
5606 if (err)
5607 return err;
5608
5609 trap_item->action = trap_action;
5610
5611 return 0;
5612}
5613
5614static int devlink_trap_action_set(struct devlink *devlink,
5615 struct devlink_trap_item *trap_item,
5616 struct genl_info *info)
5617{
5618 enum devlink_trap_action trap_action;
5619 int err;
5620
5621 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5622 return 0;
5623
5624 err = devlink_trap_action_get_from_info(info, &trap_action);
5625 if (err) {
5626 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5627 return -EINVAL;
5628 }
5629
5630 return __devlink_trap_action_set(devlink, trap_item, trap_action,
5631 info->extack);
5632}
5633
5634static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
5635 struct genl_info *info)
5636{
5637 struct netlink_ext_ack *extack = info->extack;
5638 struct devlink *devlink = info->user_ptr[0];
5639 struct devlink_trap_item *trap_item;
5640 int err;
5641
5642 if (list_empty(&devlink->trap_list))
5643 return -EOPNOTSUPP;
5644
5645 trap_item = devlink_trap_item_get_from_info(devlink, info);
5646 if (!trap_item) {
5647 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5648 return -ENOENT;
5649 }
5650
5651 err = devlink_trap_action_set(devlink, trap_item, info);
5652 if (err)
5653 return err;
5654
5655 return 0;
5656}
5657
5658static struct devlink_trap_group_item *
5659devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
5660{
5661 struct devlink_trap_group_item *group_item;
5662
5663 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5664 if (!strcmp(group_item->group->name, name))
5665 return group_item;
5666 }
5667
5668 return NULL;
5669}
5670
5671static struct devlink_trap_group_item *
5672devlink_trap_group_item_get_from_info(struct devlink *devlink,
5673 struct genl_info *info)
5674{
5675 char *name;
5676
5677 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
5678 return NULL;
5679 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
5680
5681 return devlink_trap_group_item_lookup(devlink, name);
5682}
5683
5684static int
5685devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
5686 const struct devlink_trap_group_item *group_item,
5687 enum devlink_command cmd, u32 portid, u32 seq,
5688 int flags)
5689{
5690 void *hdr;
5691 int err;
5692
5693 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5694 if (!hdr)
5695 return -EMSGSIZE;
5696
5697 if (devlink_nl_put_handle(msg, devlink))
5698 goto nla_put_failure;
5699
5700 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5701 group_item->group->name))
5702 goto nla_put_failure;
5703
5704 if (group_item->group->generic &&
5705 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5706 goto nla_put_failure;
5707
5708 err = devlink_trap_stats_put(msg, group_item->stats);
5709 if (err)
5710 goto nla_put_failure;
5711
5712 genlmsg_end(msg, hdr);
5713
5714 return 0;
5715
5716nla_put_failure:
5717 genlmsg_cancel(msg, hdr);
5718 return -EMSGSIZE;
5719}
5720
5721static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
5722 struct genl_info *info)
5723{
5724 struct netlink_ext_ack *extack = info->extack;
5725 struct devlink *devlink = info->user_ptr[0];
5726 struct devlink_trap_group_item *group_item;
5727 struct sk_buff *msg;
5728 int err;
5729
5730 if (list_empty(&devlink->trap_group_list))
5731 return -EOPNOTSUPP;
5732
5733 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5734 if (!group_item) {
5735 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5736 return -ENOENT;
5737 }
5738
5739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5740 if (!msg)
5741 return -ENOMEM;
5742
5743 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5744 DEVLINK_CMD_TRAP_GROUP_NEW,
5745 info->snd_portid, info->snd_seq, 0);
5746 if (err)
5747 goto err_trap_group_fill;
5748
5749 return genlmsg_reply(msg, info);
5750
5751err_trap_group_fill:
5752 nlmsg_free(msg);
5753 return err;
5754}
5755
5756static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
5757 struct netlink_callback *cb)
5758{
5759 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
5760 struct devlink_trap_group_item *group_item;
5761 u32 portid = NETLINK_CB(cb->skb).portid;
5762 struct devlink *devlink;
5763 int start = cb->args[0];
5764 int idx = 0;
5765 int err;
5766
5767 mutex_lock(&devlink_mutex);
5768 list_for_each_entry(devlink, &devlink_list, list) {
5769 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5770 continue;
5771 mutex_lock(&devlink->lock);
5772 list_for_each_entry(group_item, &devlink->trap_group_list,
5773 list) {
5774 if (idx < start) {
5775 idx++;
5776 continue;
5777 }
5778 err = devlink_nl_trap_group_fill(msg, devlink,
5779 group_item, cmd,
5780 portid,
5781 cb->nlh->nlmsg_seq,
5782 NLM_F_MULTI);
5783 if (err) {
5784 mutex_unlock(&devlink->lock);
5785 goto out;
5786 }
5787 idx++;
5788 }
5789 mutex_unlock(&devlink->lock);
5790 }
5791out:
5792 mutex_unlock(&devlink_mutex);
5793
5794 cb->args[0] = idx;
5795 return msg->len;
5796}
5797
5798static int
5799__devlink_trap_group_action_set(struct devlink *devlink,
5800 struct devlink_trap_group_item *group_item,
5801 enum devlink_trap_action trap_action,
5802 struct netlink_ext_ack *extack)
5803{
5804 const char *group_name = group_item->group->name;
5805 struct devlink_trap_item *trap_item;
5806 int err;
5807
5808 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5809 if (strcmp(trap_item->trap->group.name, group_name))
5810 continue;
5811 err = __devlink_trap_action_set(devlink, trap_item,
5812 trap_action, extack);
5813 if (err)
5814 return err;
5815 }
5816
5817 return 0;
5818}
5819
5820static int
5821devlink_trap_group_action_set(struct devlink *devlink,
5822 struct devlink_trap_group_item *group_item,
5823 struct genl_info *info)
5824{
5825 enum devlink_trap_action trap_action;
5826 int err;
5827
5828 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5829 return 0;
5830
5831 err = devlink_trap_action_get_from_info(info, &trap_action);
5832 if (err) {
5833 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5834 return -EINVAL;
5835 }
5836
5837 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
5838 info->extack);
5839 if (err)
5840 return err;
5841
5842 return 0;
5843}
5844
5845static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
5846 struct genl_info *info)
5847{
5848 struct netlink_ext_ack *extack = info->extack;
5849 struct devlink *devlink = info->user_ptr[0];
5850 struct devlink_trap_group_item *group_item;
5851 int err;
5852
5853 if (list_empty(&devlink->trap_group_list))
5854 return -EOPNOTSUPP;
5855
5856 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5857 if (!group_item) {
5858 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5859 return -ENOENT;
5860 }
5861
5862 err = devlink_trap_group_action_set(devlink, group_item, info);
5863 if (err)
5864 return err;
5865
5866 return 0;
5867}
5868
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005869static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
5870 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
5871 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
5872 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
5873 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
5874 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005875 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
5876 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
5877 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
5878 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
5879 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
5880 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
5881 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005882 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02005883 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03005884 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005885 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
5886 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005887 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
5888 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005889 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
5890 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
5891 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005892 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03005893 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005894 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005895 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
5896 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005897 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
5898 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Ido Schimmel0f420b62019-08-17 16:28:17 +03005899 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
5900 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
5901 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirko070c63f2019-10-03 11:49:39 +02005902 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
5903 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
5904 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005905};
5906
5907static const struct genl_ops devlink_nl_ops[] = {
5908 {
5909 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005910 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005911 .doit = devlink_nl_cmd_get_doit,
5912 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02005913 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005914 /* can be retrieved by unprivileged users */
5915 },
5916 {
5917 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005918 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005919 .doit = devlink_nl_cmd_port_get_doit,
5920 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005921 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5922 /* can be retrieved by unprivileged users */
5923 },
5924 {
5925 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005926 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005927 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005928 .flags = GENL_ADMIN_PERM,
5929 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5930 },
5931 {
5932 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005933 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005934 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005935 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005936 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5937 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005938 },
5939 {
5940 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005941 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005942 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005943 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005944 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5945 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005946 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005947 {
5948 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005949 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005950 .doit = devlink_nl_cmd_sb_get_doit,
5951 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005952 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5953 DEVLINK_NL_FLAG_NEED_SB,
5954 /* can be retrieved by unprivileged users */
5955 },
5956 {
5957 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005958 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005959 .doit = devlink_nl_cmd_sb_pool_get_doit,
5960 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005961 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5962 DEVLINK_NL_FLAG_NEED_SB,
5963 /* can be retrieved by unprivileged users */
5964 },
5965 {
5966 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005967 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005968 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005969 .flags = GENL_ADMIN_PERM,
5970 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5971 DEVLINK_NL_FLAG_NEED_SB,
5972 },
5973 {
5974 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005975 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005976 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5977 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005978 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5979 DEVLINK_NL_FLAG_NEED_SB,
5980 /* can be retrieved by unprivileged users */
5981 },
5982 {
5983 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005984 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005985 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005986 .flags = GENL_ADMIN_PERM,
5987 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5988 DEVLINK_NL_FLAG_NEED_SB,
5989 },
5990 {
5991 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005992 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005993 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5994 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005995 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5996 DEVLINK_NL_FLAG_NEED_SB,
5997 /* can be retrieved by unprivileged users */
5998 },
5999 {
6000 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006001 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006002 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006003 .flags = GENL_ADMIN_PERM,
6004 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6005 DEVLINK_NL_FLAG_NEED_SB,
6006 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006007 {
6008 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006009 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006010 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006011 .flags = GENL_ADMIN_PERM,
6012 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006013 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006014 },
6015 {
6016 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006017 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006018 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006019 .flags = GENL_ADMIN_PERM,
6020 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006021 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006022 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006023 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006024 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006025 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006026 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006027 .flags = GENL_ADMIN_PERM,
6028 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6029 },
6030 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006031 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006032 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006033 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006034 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07006035 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6036 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006037 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006038 {
6039 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006040 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006041 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006042 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006043 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006044 },
6045 {
6046 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006047 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006048 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006049 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006050 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006051 },
6052 {
6053 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006054 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006055 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006056 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006057 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006058 },
6059 {
6060 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006061 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006062 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006063 .flags = GENL_ADMIN_PERM,
6064 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6065 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006066 {
6067 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006068 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006069 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006070 .flags = GENL_ADMIN_PERM,
6071 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6072 },
6073 {
6074 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02006075 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006076 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006077 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006078 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006079 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006080 {
6081 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02006082 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006083 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006084 .flags = GENL_ADMIN_PERM,
6085 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6086 DEVLINK_NL_FLAG_NO_LOCK,
6087 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006088 {
6089 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006090 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006091 .doit = devlink_nl_cmd_param_get_doit,
6092 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006093 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6094 /* can be retrieved by unprivileged users */
6095 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006096 {
6097 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006098 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006099 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006100 .flags = GENL_ADMIN_PERM,
6101 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6102 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006103 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306104 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006105 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306106 .doit = devlink_nl_cmd_port_param_get_doit,
6107 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306108 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6109 /* can be retrieved by unprivileged users */
6110 },
6111 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05306112 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006113 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306114 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306115 .flags = GENL_ADMIN_PERM,
6116 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6117 },
6118 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006119 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006120 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006121 .doit = devlink_nl_cmd_region_get_doit,
6122 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006123 .flags = GENL_ADMIN_PERM,
6124 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6125 },
Alex Vesker866319b2018-07-12 15:13:13 +03006126 {
6127 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02006128 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03006129 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03006130 .flags = GENL_ADMIN_PERM,
6131 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6132 },
Alex Vesker4e547952018-07-12 15:13:14 +03006133 {
6134 .cmd = DEVLINK_CMD_REGION_READ,
Jiri Pirkoee85da52019-10-05 20:04:42 +02006135 .validate = GENL_DONT_VALIDATE_STRICT |
6136 GENL_DONT_VALIDATE_DUMP_STRICT,
Alex Vesker4e547952018-07-12 15:13:14 +03006137 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03006138 .flags = GENL_ADMIN_PERM,
6139 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6140 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006141 {
6142 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006143 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006144 .doit = devlink_nl_cmd_info_get_doit,
6145 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006146 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6147 /* can be retrieved by unprivileged users */
6148 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006149 {
6150 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006151 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006152 .doit = devlink_nl_cmd_health_reporter_get_doit,
6153 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006154 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6155 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006156 /* can be retrieved by unprivileged users */
6157 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006158 {
6159 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006160 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006161 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006162 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006163 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6164 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006165 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006166 {
6167 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02006168 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006169 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006170 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006171 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6172 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006173 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006174 {
6175 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006176 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006177 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006178 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006179 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6180 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006181 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006182 {
6183 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Jiri Pirko82a843d2019-10-07 09:28:31 +02006184 .validate = GENL_DONT_VALIDATE_STRICT |
6185 GENL_DONT_VALIDATE_DUMP_STRICT,
Aya Levine44ef4e2019-05-16 09:49:20 +03006186 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006187 .flags = GENL_ADMIN_PERM,
6188 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6189 DEVLINK_NL_FLAG_NO_LOCK,
6190 },
6191 {
6192 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006193 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006194 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006195 .flags = GENL_ADMIN_PERM,
6196 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6197 DEVLINK_NL_FLAG_NO_LOCK,
6198 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006199 {
6200 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006201 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006202 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006203 .flags = GENL_ADMIN_PERM,
6204 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6205 },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006206 {
6207 .cmd = DEVLINK_CMD_TRAP_GET,
6208 .doit = devlink_nl_cmd_trap_get_doit,
6209 .dumpit = devlink_nl_cmd_trap_get_dumpit,
6210 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6211 /* can be retrieved by unprivileged users */
6212 },
6213 {
6214 .cmd = DEVLINK_CMD_TRAP_SET,
6215 .doit = devlink_nl_cmd_trap_set_doit,
6216 .flags = GENL_ADMIN_PERM,
6217 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6218 },
6219 {
6220 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
6221 .doit = devlink_nl_cmd_trap_group_get_doit,
6222 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
6223 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6224 /* can be retrieved by unprivileged users */
6225 },
6226 {
6227 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
6228 .doit = devlink_nl_cmd_trap_group_set_doit,
6229 .flags = GENL_ADMIN_PERM,
6230 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6231 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006232};
6233
Johannes Berg56989f62016-10-24 14:40:05 +02006234static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02006235 .name = DEVLINK_GENL_NAME,
6236 .version = DEVLINK_GENL_VERSION,
6237 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01006238 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02006239 .netnsok = true,
6240 .pre_doit = devlink_nl_pre_doit,
6241 .post_doit = devlink_nl_post_doit,
6242 .module = THIS_MODULE,
6243 .ops = devlink_nl_ops,
6244 .n_ops = ARRAY_SIZE(devlink_nl_ops),
6245 .mcgrps = devlink_nl_mcgrps,
6246 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
6247};
6248
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006249/**
6250 * devlink_alloc - Allocate new devlink instance resources
6251 *
6252 * @ops: ops
6253 * @priv_size: size of user private data
6254 *
6255 * Allocate new devlink instance resources, including devlink index
6256 * and name.
6257 */
6258struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
6259{
6260 struct devlink *devlink;
6261
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006262 if (WARN_ON(!ops))
6263 return NULL;
6264
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006265 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
6266 if (!devlink)
6267 return NULL;
6268 devlink->ops = ops;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006269 __devlink_net_set(devlink, &init_net);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006270 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02006271 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006272 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006273 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006274 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006275 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02006276 INIT_LIST_HEAD(&devlink->reporter_list);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006277 INIT_LIST_HEAD(&devlink->trap_list);
6278 INIT_LIST_HEAD(&devlink->trap_group_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006279 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006280 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006281 return devlink;
6282}
6283EXPORT_SYMBOL_GPL(devlink_alloc);
6284
6285/**
6286 * devlink_register - Register devlink instance
6287 *
6288 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006289 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006290 */
6291int devlink_register(struct devlink *devlink, struct device *dev)
6292{
6293 mutex_lock(&devlink_mutex);
6294 devlink->dev = dev;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006295 devlink->registered = true;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006296 list_add_tail(&devlink->list, &devlink_list);
6297 devlink_notify(devlink, DEVLINK_CMD_NEW);
6298 mutex_unlock(&devlink_mutex);
6299 return 0;
6300}
6301EXPORT_SYMBOL_GPL(devlink_register);
6302
6303/**
6304 * devlink_unregister - Unregister devlink instance
6305 *
6306 * @devlink: devlink
6307 */
6308void devlink_unregister(struct devlink *devlink)
6309{
6310 mutex_lock(&devlink_mutex);
6311 devlink_notify(devlink, DEVLINK_CMD_DEL);
6312 list_del(&devlink->list);
6313 mutex_unlock(&devlink_mutex);
6314}
6315EXPORT_SYMBOL_GPL(devlink_unregister);
6316
6317/**
6318 * devlink_free - Free devlink instance resources
6319 *
6320 * @devlink: devlink
6321 */
6322void devlink_free(struct devlink *devlink)
6323{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006324 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01006325 mutex_destroy(&devlink->lock);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006326 WARN_ON(!list_empty(&devlink->trap_group_list));
6327 WARN_ON(!list_empty(&devlink->trap_list));
Parav Panditb904aad2019-02-08 15:15:00 -06006328 WARN_ON(!list_empty(&devlink->reporter_list));
6329 WARN_ON(!list_empty(&devlink->region_list));
6330 WARN_ON(!list_empty(&devlink->param_list));
6331 WARN_ON(!list_empty(&devlink->resource_list));
6332 WARN_ON(!list_empty(&devlink->dpipe_table_list));
6333 WARN_ON(!list_empty(&devlink->sb_list));
6334 WARN_ON(!list_empty(&devlink->port_list));
6335
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006336 kfree(devlink);
6337}
6338EXPORT_SYMBOL_GPL(devlink_free);
6339
Jiri Pirko136bf272019-05-23 10:43:35 +02006340static void devlink_port_type_warn(struct work_struct *work)
6341{
6342 WARN(true, "Type was not set for devlink port.");
6343}
6344
6345static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
6346{
6347 /* Ignore CPU and DSA flavours. */
6348 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
6349 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
6350}
6351
6352#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30)
6353
6354static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
6355{
6356 if (!devlink_port_type_should_warn(devlink_port))
6357 return;
6358 /* Schedule a work to WARN in case driver does not set port
6359 * type within timeout.
6360 */
6361 schedule_delayed_work(&devlink_port->type_warn_dw,
6362 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
6363}
6364
6365static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
6366{
6367 if (!devlink_port_type_should_warn(devlink_port))
6368 return;
6369 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
6370}
6371
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006372/**
6373 * devlink_port_register - Register devlink port
6374 *
6375 * @devlink: devlink
6376 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006377 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006378 *
6379 * Register devlink port with provided port index. User can use
6380 * any indexing, even hw-related one. devlink_port structure
6381 * is convenient to be embedded inside user driver private structure.
6382 * Note that the caller should take care of zeroing the devlink_port
6383 * structure.
6384 */
6385int devlink_port_register(struct devlink *devlink,
6386 struct devlink_port *devlink_port,
6387 unsigned int port_index)
6388{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006389 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006390 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006391 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006392 return -EEXIST;
6393 }
6394 devlink_port->devlink = devlink;
6395 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006396 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01006397 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006398 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306399 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006400 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02006401 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
6402 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006403 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6404 return 0;
6405}
6406EXPORT_SYMBOL_GPL(devlink_port_register);
6407
6408/**
6409 * devlink_port_unregister - Unregister devlink port
6410 *
6411 * @devlink_port: devlink port
6412 */
6413void devlink_port_unregister(struct devlink_port *devlink_port)
6414{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006415 struct devlink *devlink = devlink_port->devlink;
6416
Jiri Pirko136bf272019-05-23 10:43:35 +02006417 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006418 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006419 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006420 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006421 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006422}
6423EXPORT_SYMBOL_GPL(devlink_port_unregister);
6424
6425static void __devlink_port_type_set(struct devlink_port *devlink_port,
6426 enum devlink_port_type type,
6427 void *type_dev)
6428{
Jiri Pirko2b239e72019-03-24 11:14:36 +01006429 if (WARN_ON(!devlink_port->registered))
6430 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02006431 devlink_port_type_warn_cancel(devlink_port);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006432 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006433 devlink_port->type = type;
6434 devlink_port->type_dev = type_dev;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006435 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006436 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6437}
6438
6439/**
6440 * devlink_port_type_eth_set - Set port type to Ethernet
6441 *
6442 * @devlink_port: devlink port
6443 * @netdev: related netdevice
6444 */
6445void devlink_port_type_eth_set(struct devlink_port *devlink_port,
6446 struct net_device *netdev)
6447{
Jiri Pirko119c0b52019-04-03 14:24:27 +02006448 const struct net_device_ops *ops = netdev->netdev_ops;
6449
Jiri Pirko746364f2019-03-28 13:56:46 +01006450 /* If driver registers devlink port, it should set devlink port
6451 * attributes accordingly so the compat functions are called
6452 * and the original ops are not used.
6453 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02006454 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01006455 /* Some drivers use the same set of ndos for netdevs
6456 * that have devlink_port registered and also for
6457 * those who don't. Make sure that ndo_get_phys_port_name
6458 * returns -EOPNOTSUPP here in case it is defined.
6459 * Warn if not.
6460 */
Jiri Pirko746364f2019-03-28 13:56:46 +01006461 char name[IFNAMSIZ];
6462 int err;
6463
6464 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
6465 WARN_ON(err != -EOPNOTSUPP);
6466 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02006467 if (ops->ndo_get_port_parent_id) {
6468 /* Some drivers use the same set of ndos for netdevs
6469 * that have devlink_port registered and also for
6470 * those who don't. Make sure that ndo_get_port_parent_id
6471 * returns -EOPNOTSUPP here in case it is defined.
6472 * Warn if not.
6473 */
6474 struct netdev_phys_item_id ppid;
6475 int err;
6476
6477 err = ops->ndo_get_port_parent_id(netdev, &ppid);
6478 WARN_ON(err != -EOPNOTSUPP);
6479 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01006480 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006481}
6482EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
6483
6484/**
6485 * devlink_port_type_ib_set - Set port type to InfiniBand
6486 *
6487 * @devlink_port: devlink port
6488 * @ibdev: related IB device
6489 */
6490void devlink_port_type_ib_set(struct devlink_port *devlink_port,
6491 struct ib_device *ibdev)
6492{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006493 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006494}
6495EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
6496
6497/**
6498 * devlink_port_type_clear - Clear port type
6499 *
6500 * @devlink_port: devlink port
6501 */
6502void devlink_port_type_clear(struct devlink_port *devlink_port)
6503{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006504 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02006505 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006506}
6507EXPORT_SYMBOL_GPL(devlink_port_type_clear);
6508
Parav Pandit378ef012019-07-08 23:17:35 -05006509static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
6510 enum devlink_port_flavour flavour,
6511 const unsigned char *switch_id,
6512 unsigned char switch_id_len)
6513{
6514 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6515
6516 if (WARN_ON(devlink_port->registered))
6517 return -EEXIST;
6518 attrs->set = true;
6519 attrs->flavour = flavour;
6520 if (switch_id) {
6521 attrs->switch_port = true;
6522 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
6523 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
6524 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
6525 attrs->switch_id.id_len = switch_id_len;
6526 } else {
6527 attrs->switch_port = false;
6528 }
6529 return 0;
6530}
6531
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006532/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006533 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006534 *
6535 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02006536 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006537 * @port_number: number of the port that is facing user, for example
6538 * the front panel port number
6539 * @split: indicates if this is split port
6540 * @split_subport_number: if the port is split, this is the number
6541 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02006542 * @switch_id: if the port is part of switch, this is buffer with ID,
6543 * otwerwise this is NULL
6544 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006545 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006546void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02006547 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006548 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02006549 u32 split_subport_number,
6550 const unsigned char *switch_id,
6551 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006552{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006553 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05006554 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006555
Parav Pandit378ef012019-07-08 23:17:35 -05006556 ret = __devlink_port_attrs_set(devlink_port, flavour,
6557 switch_id, switch_id_len);
6558 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01006559 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006560 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05006561 attrs->phys.port_number = port_number;
6562 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006563}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006564EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006565
Parav Pandit98fd2d62019-07-08 23:17:37 -05006566/**
6567 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
6568 *
6569 * @devlink_port: devlink port
6570 * @pf: associated PF for the devlink port instance
6571 * @switch_id: if the port is part of switch, this is buffer with ID,
6572 * otherwise this is NULL
6573 * @switch_id_len: length of the switch_id buffer
6574 */
6575void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
6576 const unsigned char *switch_id,
6577 unsigned char switch_id_len, u16 pf)
6578{
6579 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6580 int ret;
6581
6582 ret = __devlink_port_attrs_set(devlink_port,
6583 DEVLINK_PORT_FLAVOUR_PCI_PF,
6584 switch_id, switch_id_len);
6585 if (ret)
6586 return;
6587
6588 attrs->pci_pf.pf = pf;
6589}
6590EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
6591
Parav Pandite41b6bf2019-07-08 23:17:38 -05006592/**
6593 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
6594 *
6595 * @devlink_port: devlink port
6596 * @pf: associated PF for the devlink port instance
6597 * @vf: associated VF of a PF for the devlink port instance
6598 * @switch_id: if the port is part of switch, this is buffer with ID,
6599 * otherwise this is NULL
6600 * @switch_id_len: length of the switch_id buffer
6601 */
6602void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
6603 const unsigned char *switch_id,
6604 unsigned char switch_id_len,
6605 u16 pf, u16 vf)
6606{
6607 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6608 int ret;
6609
6610 ret = __devlink_port_attrs_set(devlink_port,
6611 DEVLINK_PORT_FLAVOUR_PCI_VF,
6612 switch_id, switch_id_len);
6613 if (ret)
6614 return;
6615 attrs->pci_vf.pf = pf;
6616 attrs->pci_vf.vf = vf;
6617}
6618EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
6619
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006620static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
6621 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02006622{
6623 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6624 int n = 0;
6625
6626 if (!attrs->set)
6627 return -EOPNOTSUPP;
6628
6629 switch (attrs->flavour) {
6630 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
6631 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05006632 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006633 else
Parav Pandit378ef012019-07-08 23:17:35 -05006634 n = snprintf(name, len, "p%us%u",
6635 attrs->phys.port_number,
6636 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006637 break;
6638 case DEVLINK_PORT_FLAVOUR_CPU:
6639 case DEVLINK_PORT_FLAVOUR_DSA:
6640 /* As CPU and DSA ports do not have a netdevice associated
6641 * case should not ever happen.
6642 */
6643 WARN_ON(1);
6644 return -EINVAL;
Parav Pandit98fd2d62019-07-08 23:17:37 -05006645 case DEVLINK_PORT_FLAVOUR_PCI_PF:
6646 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
6647 break;
Parav Pandite41b6bf2019-07-08 23:17:38 -05006648 case DEVLINK_PORT_FLAVOUR_PCI_VF:
6649 n = snprintf(name, len, "pf%uvf%u",
6650 attrs->pci_vf.pf, attrs->pci_vf.vf);
6651 break;
Jiri Pirko08474c12018-05-18 09:29:02 +02006652 }
6653
6654 if (n >= len)
6655 return -EINVAL;
6656
6657 return 0;
6658}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006659
Jiri Pirkobf797472016-04-14 18:19:13 +02006660int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
6661 u32 size, u16 ingress_pools_count,
6662 u16 egress_pools_count, u16 ingress_tc_count,
6663 u16 egress_tc_count)
6664{
6665 struct devlink_sb *devlink_sb;
6666 int err = 0;
6667
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006668 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006669 if (devlink_sb_index_exists(devlink, sb_index)) {
6670 err = -EEXIST;
6671 goto unlock;
6672 }
6673
6674 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
6675 if (!devlink_sb) {
6676 err = -ENOMEM;
6677 goto unlock;
6678 }
6679 devlink_sb->index = sb_index;
6680 devlink_sb->size = size;
6681 devlink_sb->ingress_pools_count = ingress_pools_count;
6682 devlink_sb->egress_pools_count = egress_pools_count;
6683 devlink_sb->ingress_tc_count = ingress_tc_count;
6684 devlink_sb->egress_tc_count = egress_tc_count;
6685 list_add_tail(&devlink_sb->list, &devlink->sb_list);
6686unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006687 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006688 return err;
6689}
6690EXPORT_SYMBOL_GPL(devlink_sb_register);
6691
6692void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
6693{
6694 struct devlink_sb *devlink_sb;
6695
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006696 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006697 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
6698 WARN_ON(!devlink_sb);
6699 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006700 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006701 kfree(devlink_sb);
6702}
6703EXPORT_SYMBOL_GPL(devlink_sb_unregister);
6704
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006705/**
6706 * devlink_dpipe_headers_register - register dpipe headers
6707 *
6708 * @devlink: devlink
6709 * @dpipe_headers: dpipe header array
6710 *
6711 * Register the headers supported by hardware.
6712 */
6713int devlink_dpipe_headers_register(struct devlink *devlink,
6714 struct devlink_dpipe_headers *dpipe_headers)
6715{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006716 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006717 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006718 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006719 return 0;
6720}
6721EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
6722
6723/**
6724 * devlink_dpipe_headers_unregister - unregister dpipe headers
6725 *
6726 * @devlink: devlink
6727 *
6728 * Unregister the headers supported by hardware.
6729 */
6730void devlink_dpipe_headers_unregister(struct devlink *devlink)
6731{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006732 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006733 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006734 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006735}
6736EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
6737
6738/**
6739 * devlink_dpipe_table_counter_enabled - check if counter allocation
6740 * required
6741 * @devlink: devlink
6742 * @table_name: tables name
6743 *
6744 * Used by driver to check if counter allocation is required.
6745 * After counter allocation is turned on the table entries
6746 * are updated to include counter statistics.
6747 *
6748 * After that point on the driver must respect the counter
6749 * state so that each entry added to the table is added
6750 * with a counter.
6751 */
6752bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
6753 const char *table_name)
6754{
6755 struct devlink_dpipe_table *table;
6756 bool enabled;
6757
6758 rcu_read_lock();
6759 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6760 table_name);
6761 enabled = false;
6762 if (table)
6763 enabled = table->counters_enabled;
6764 rcu_read_unlock();
6765 return enabled;
6766}
6767EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
6768
6769/**
6770 * devlink_dpipe_table_register - register dpipe table
6771 *
6772 * @devlink: devlink
6773 * @table_name: table name
6774 * @table_ops: table ops
6775 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006776 * @counter_control_extern: external control for counters
6777 */
6778int devlink_dpipe_table_register(struct devlink *devlink,
6779 const char *table_name,
6780 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006781 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006782{
6783 struct devlink_dpipe_table *table;
6784
6785 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
6786 return -EEXIST;
6787
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006788 if (WARN_ON(!table_ops->size_get))
6789 return -EINVAL;
6790
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006791 table = kzalloc(sizeof(*table), GFP_KERNEL);
6792 if (!table)
6793 return -ENOMEM;
6794
6795 table->name = table_name;
6796 table->table_ops = table_ops;
6797 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006798 table->counter_control_extern = counter_control_extern;
6799
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006800 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006801 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006802 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006803 return 0;
6804}
6805EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
6806
6807/**
6808 * devlink_dpipe_table_unregister - unregister dpipe table
6809 *
6810 * @devlink: devlink
6811 * @table_name: table name
6812 */
6813void devlink_dpipe_table_unregister(struct devlink *devlink,
6814 const char *table_name)
6815{
6816 struct devlink_dpipe_table *table;
6817
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006818 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006819 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6820 table_name);
6821 if (!table)
6822 goto unlock;
6823 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006824 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006825 kfree_rcu(table, rcu);
6826 return;
6827unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006828 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006829}
6830EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
6831
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006832/**
6833 * devlink_resource_register - devlink resource register
6834 *
6835 * @devlink: devlink
6836 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006837 * @resource_size: resource's size
6838 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006839 * @parent_resource_id: resource's parent id
6840 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006841 */
6842int devlink_resource_register(struct devlink *devlink,
6843 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006844 u64 resource_size,
6845 u64 resource_id,
6846 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02006847 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006848{
6849 struct devlink_resource *resource;
6850 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07006851 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006852 int err = 0;
6853
David Ahern14530742018-03-20 19:31:14 -07006854 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
6855
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006856 mutex_lock(&devlink->lock);
6857 resource = devlink_resource_find(devlink, NULL, resource_id);
6858 if (resource) {
6859 err = -EINVAL;
6860 goto out;
6861 }
6862
6863 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
6864 if (!resource) {
6865 err = -ENOMEM;
6866 goto out;
6867 }
6868
6869 if (top_hierarchy) {
6870 resource_list = &devlink->resource_list;
6871 } else {
6872 struct devlink_resource *parent_resource;
6873
6874 parent_resource = devlink_resource_find(devlink, NULL,
6875 parent_resource_id);
6876 if (parent_resource) {
6877 resource_list = &parent_resource->resource_list;
6878 resource->parent = parent_resource;
6879 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00006880 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006881 err = -EINVAL;
6882 goto out;
6883 }
6884 }
6885
6886 resource->name = resource_name;
6887 resource->size = resource_size;
6888 resource->size_new = resource_size;
6889 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006890 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01006891 memcpy(&resource->size_params, size_params,
6892 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006893 INIT_LIST_HEAD(&resource->resource_list);
6894 list_add_tail(&resource->list, resource_list);
6895out:
6896 mutex_unlock(&devlink->lock);
6897 return err;
6898}
6899EXPORT_SYMBOL_GPL(devlink_resource_register);
6900
6901/**
6902 * devlink_resources_unregister - free all resources
6903 *
6904 * @devlink: devlink
6905 * @resource: resource
6906 */
6907void devlink_resources_unregister(struct devlink *devlink,
6908 struct devlink_resource *resource)
6909{
6910 struct devlink_resource *tmp, *child_resource;
6911 struct list_head *resource_list;
6912
6913 if (resource)
6914 resource_list = &resource->resource_list;
6915 else
6916 resource_list = &devlink->resource_list;
6917
6918 if (!resource)
6919 mutex_lock(&devlink->lock);
6920
6921 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
6922 devlink_resources_unregister(devlink, child_resource);
6923 list_del(&child_resource->list);
6924 kfree(child_resource);
6925 }
6926
6927 if (!resource)
6928 mutex_unlock(&devlink->lock);
6929}
6930EXPORT_SYMBOL_GPL(devlink_resources_unregister);
6931
6932/**
6933 * devlink_resource_size_get - get and update size
6934 *
6935 * @devlink: devlink
6936 * @resource_id: the requested resource id
6937 * @p_resource_size: ptr to update
6938 */
6939int devlink_resource_size_get(struct devlink *devlink,
6940 u64 resource_id,
6941 u64 *p_resource_size)
6942{
6943 struct devlink_resource *resource;
6944 int err = 0;
6945
6946 mutex_lock(&devlink->lock);
6947 resource = devlink_resource_find(devlink, NULL, resource_id);
6948 if (!resource) {
6949 err = -EINVAL;
6950 goto out;
6951 }
6952 *p_resource_size = resource->size_new;
6953 resource->size = resource->size_new;
6954out:
6955 mutex_unlock(&devlink->lock);
6956 return err;
6957}
6958EXPORT_SYMBOL_GPL(devlink_resource_size_get);
6959
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01006960/**
6961 * devlink_dpipe_table_resource_set - set the resource id
6962 *
6963 * @devlink: devlink
6964 * @table_name: table name
6965 * @resource_id: resource id
6966 * @resource_units: number of resource's units consumed per table's entry
6967 */
6968int devlink_dpipe_table_resource_set(struct devlink *devlink,
6969 const char *table_name, u64 resource_id,
6970 u64 resource_units)
6971{
6972 struct devlink_dpipe_table *table;
6973 int err = 0;
6974
6975 mutex_lock(&devlink->lock);
6976 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6977 table_name);
6978 if (!table) {
6979 err = -EINVAL;
6980 goto out;
6981 }
6982 table->resource_id = resource_id;
6983 table->resource_units = resource_units;
6984 table->resource_valid = true;
6985out:
6986 mutex_unlock(&devlink->lock);
6987 return err;
6988}
6989EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
6990
Jiri Pirkofc56be42018-04-05 22:13:21 +02006991/**
6992 * devlink_resource_occ_get_register - register occupancy getter
6993 *
6994 * @devlink: devlink
6995 * @resource_id: resource id
6996 * @occ_get: occupancy getter callback
6997 * @occ_get_priv: occupancy getter callback priv
6998 */
6999void devlink_resource_occ_get_register(struct devlink *devlink,
7000 u64 resource_id,
7001 devlink_resource_occ_get_t *occ_get,
7002 void *occ_get_priv)
7003{
7004 struct devlink_resource *resource;
7005
7006 mutex_lock(&devlink->lock);
7007 resource = devlink_resource_find(devlink, NULL, resource_id);
7008 if (WARN_ON(!resource))
7009 goto out;
7010 WARN_ON(resource->occ_get);
7011
7012 resource->occ_get = occ_get;
7013 resource->occ_get_priv = occ_get_priv;
7014out:
7015 mutex_unlock(&devlink->lock);
7016}
7017EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
7018
7019/**
7020 * devlink_resource_occ_get_unregister - unregister occupancy getter
7021 *
7022 * @devlink: devlink
7023 * @resource_id: resource id
7024 */
7025void devlink_resource_occ_get_unregister(struct devlink *devlink,
7026 u64 resource_id)
7027{
7028 struct devlink_resource *resource;
7029
7030 mutex_lock(&devlink->lock);
7031 resource = devlink_resource_find(devlink, NULL, resource_id);
7032 if (WARN_ON(!resource))
7033 goto out;
7034 WARN_ON(!resource->occ_get);
7035
7036 resource->occ_get = NULL;
7037 resource->occ_get_priv = NULL;
7038out:
7039 mutex_unlock(&devlink->lock);
7040}
7041EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
7042
Vasundhara Volam39e61602019-01-28 18:00:20 +05307043static int devlink_param_verify(const struct devlink_param *param)
7044{
7045 if (!param || !param->name || !param->supported_cmodes)
7046 return -EINVAL;
7047 if (param->generic)
7048 return devlink_param_generic_verify(param);
7049 else
7050 return devlink_param_driver_verify(param);
7051}
7052
7053static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307054 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307055 struct list_head *param_list,
7056 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307057 size_t params_count,
7058 enum devlink_command reg_cmd,
7059 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307060{
7061 const struct devlink_param *param = params;
7062 int i;
7063 int err;
7064
7065 mutex_lock(&devlink->lock);
7066 for (i = 0; i < params_count; i++, param++) {
7067 err = devlink_param_verify(param);
7068 if (err)
7069 goto rollback;
7070
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307071 err = devlink_param_register_one(devlink, port_index,
7072 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307073 if (err)
7074 goto rollback;
7075 }
7076
7077 mutex_unlock(&devlink->lock);
7078 return 0;
7079
7080rollback:
7081 if (!i)
7082 goto unlock;
7083 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307084 devlink_param_unregister_one(devlink, port_index, param_list,
7085 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307086unlock:
7087 mutex_unlock(&devlink->lock);
7088 return err;
7089}
7090
7091static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307092 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307093 struct list_head *param_list,
7094 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307095 size_t params_count,
7096 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307097{
7098 const struct devlink_param *param = params;
7099 int i;
7100
7101 mutex_lock(&devlink->lock);
7102 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307103 devlink_param_unregister_one(devlink, 0, param_list, param,
7104 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307105 mutex_unlock(&devlink->lock);
7106}
7107
Moshe Shemesheabaef12018-07-04 14:30:28 +03007108/**
7109 * devlink_params_register - register configuration parameters
7110 *
7111 * @devlink: devlink
7112 * @params: configuration parameters array
7113 * @params_count: number of parameters provided
7114 *
7115 * Register the configuration parameters supported by the driver.
7116 */
7117int devlink_params_register(struct devlink *devlink,
7118 const struct devlink_param *params,
7119 size_t params_count)
7120{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307121 return __devlink_params_register(devlink, 0, &devlink->param_list,
7122 params, params_count,
7123 DEVLINK_CMD_PARAM_NEW,
7124 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007125}
7126EXPORT_SYMBOL_GPL(devlink_params_register);
7127
7128/**
7129 * devlink_params_unregister - unregister configuration parameters
7130 * @devlink: devlink
7131 * @params: configuration parameters to unregister
7132 * @params_count: number of parameters provided
7133 */
7134void devlink_params_unregister(struct devlink *devlink,
7135 const struct devlink_param *params,
7136 size_t params_count)
7137{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307138 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
7139 params, params_count,
7140 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007141}
7142EXPORT_SYMBOL_GPL(devlink_params_unregister);
7143
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007144/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00007145 * devlink_params_publish - publish configuration parameters
7146 *
7147 * @devlink: devlink
7148 *
7149 * Publish previously registered configuration parameters.
7150 */
7151void devlink_params_publish(struct devlink *devlink)
7152{
7153 struct devlink_param_item *param_item;
7154
7155 list_for_each_entry(param_item, &devlink->param_list, list) {
7156 if (param_item->published)
7157 continue;
7158 param_item->published = true;
7159 devlink_param_notify(devlink, 0, param_item,
7160 DEVLINK_CMD_PARAM_NEW);
7161 }
7162}
7163EXPORT_SYMBOL_GPL(devlink_params_publish);
7164
7165/**
7166 * devlink_params_unpublish - unpublish configuration parameters
7167 *
7168 * @devlink: devlink
7169 *
7170 * Unpublish previously registered configuration parameters.
7171 */
7172void devlink_params_unpublish(struct devlink *devlink)
7173{
7174 struct devlink_param_item *param_item;
7175
7176 list_for_each_entry(param_item, &devlink->param_list, list) {
7177 if (!param_item->published)
7178 continue;
7179 param_item->published = false;
7180 devlink_param_notify(devlink, 0, param_item,
7181 DEVLINK_CMD_PARAM_DEL);
7182 }
7183}
7184EXPORT_SYMBOL_GPL(devlink_params_unpublish);
7185
7186/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05307187 * devlink_port_params_register - register port configuration parameters
7188 *
7189 * @devlink_port: devlink port
7190 * @params: configuration parameters array
7191 * @params_count: number of parameters provided
7192 *
7193 * Register the configuration parameters supported by the port.
7194 */
7195int devlink_port_params_register(struct devlink_port *devlink_port,
7196 const struct devlink_param *params,
7197 size_t params_count)
7198{
7199 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307200 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307201 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307202 params_count,
7203 DEVLINK_CMD_PORT_PARAM_NEW,
7204 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307205}
7206EXPORT_SYMBOL_GPL(devlink_port_params_register);
7207
7208/**
7209 * devlink_port_params_unregister - unregister port configuration
7210 * parameters
7211 *
7212 * @devlink_port: devlink port
7213 * @params: configuration parameters array
7214 * @params_count: number of parameters provided
7215 */
7216void devlink_port_params_unregister(struct devlink_port *devlink_port,
7217 const struct devlink_param *params,
7218 size_t params_count)
7219{
7220 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307221 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307222 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307223 params, params_count,
7224 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307225}
7226EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
7227
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307228static int
7229__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
7230 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007231{
7232 struct devlink_param_item *param_item;
7233
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307234 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007235 if (!param_item)
7236 return -EINVAL;
7237
7238 if (!param_item->driverinit_value_valid ||
7239 !devlink_param_cmode_is_supported(param_item->param,
7240 DEVLINK_PARAM_CMODE_DRIVERINIT))
7241 return -EOPNOTSUPP;
7242
Moshe Shemesh12765342018-10-10 16:09:26 +03007243 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7244 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
7245 else
7246 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007247
7248 return 0;
7249}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307250
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307251static int
7252__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307253 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307254 struct list_head *param_list, u32 param_id,
7255 union devlink_param_value init_val,
7256 enum devlink_command cmd)
7257{
7258 struct devlink_param_item *param_item;
7259
7260 param_item = devlink_param_find_by_id(param_list, param_id);
7261 if (!param_item)
7262 return -EINVAL;
7263
7264 if (!devlink_param_cmode_is_supported(param_item->param,
7265 DEVLINK_PARAM_CMODE_DRIVERINIT))
7266 return -EOPNOTSUPP;
7267
7268 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7269 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
7270 else
7271 param_item->driverinit_value = init_val;
7272 param_item->driverinit_value_valid = true;
7273
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307274 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307275 return 0;
7276}
7277
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307278/**
7279 * devlink_param_driverinit_value_get - get configuration parameter
7280 * value for driver initializing
7281 *
7282 * @devlink: devlink
7283 * @param_id: parameter ID
7284 * @init_val: value of parameter in driverinit configuration mode
7285 *
7286 * This function should be used by the driver to get driverinit
7287 * configuration for initialization after reload command.
7288 */
7289int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
7290 union devlink_param_value *init_val)
7291{
Jiri Pirko97691062019-09-12 10:49:45 +02007292 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307293 return -EOPNOTSUPP;
7294
7295 return __devlink_param_driverinit_value_get(&devlink->param_list,
7296 param_id, init_val);
7297}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007298EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
7299
7300/**
7301 * devlink_param_driverinit_value_set - set value of configuration
7302 * parameter for driverinit
7303 * configuration mode
7304 *
7305 * @devlink: devlink
7306 * @param_id: parameter ID
7307 * @init_val: value of parameter to set for driverinit configuration mode
7308 *
7309 * This function should be used by the driver to set driverinit
7310 * configuration mode default value.
7311 */
7312int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
7313 union devlink_param_value init_val)
7314{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307315 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307316 &devlink->param_list,
7317 param_id, init_val,
7318 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007319}
7320EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
7321
Moshe Shemeshea601e12018-07-04 14:30:32 +03007322/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307323 * devlink_port_param_driverinit_value_get - get configuration parameter
7324 * value for driver initializing
7325 *
7326 * @devlink_port: devlink_port
7327 * @param_id: parameter ID
7328 * @init_val: value of parameter in driverinit configuration mode
7329 *
7330 * This function should be used by the driver to get driverinit
7331 * configuration for initialization after reload command.
7332 */
7333int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
7334 u32 param_id,
7335 union devlink_param_value *init_val)
7336{
7337 struct devlink *devlink = devlink_port->devlink;
7338
Jiri Pirko97691062019-09-12 10:49:45 +02007339 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307340 return -EOPNOTSUPP;
7341
7342 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
7343 param_id, init_val);
7344}
7345EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
7346
7347/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307348 * devlink_port_param_driverinit_value_set - set value of configuration
7349 * parameter for driverinit
7350 * configuration mode
7351 *
7352 * @devlink_port: devlink_port
7353 * @param_id: parameter ID
7354 * @init_val: value of parameter to set for driverinit configuration mode
7355 *
7356 * This function should be used by the driver to set driverinit
7357 * configuration mode default value.
7358 */
7359int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
7360 u32 param_id,
7361 union devlink_param_value init_val)
7362{
7363 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307364 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307365 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307366 param_id, init_val,
7367 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307368}
7369EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
7370
7371/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03007372 * devlink_param_value_changed - notify devlink on a parameter's value
7373 * change. Should be called by the driver
7374 * right after the change.
7375 *
7376 * @devlink: devlink
7377 * @param_id: parameter ID
7378 *
7379 * This function should be used by the driver to notify devlink on value
7380 * change, excluding driverinit configuration mode.
7381 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03007382 */
7383void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
7384{
7385 struct devlink_param_item *param_item;
7386
7387 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
7388 WARN_ON(!param_item);
7389
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307390 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03007391}
7392EXPORT_SYMBOL_GPL(devlink_param_value_changed);
7393
Alex Veskerb16ebe92018-07-12 15:13:08 +03007394/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307395 * devlink_port_param_value_changed - notify devlink on a parameter's value
7396 * change. Should be called by the driver
7397 * right after the change.
7398 *
7399 * @devlink_port: devlink_port
7400 * @param_id: parameter ID
7401 *
7402 * This function should be used by the driver to notify devlink on value
7403 * change, excluding driverinit configuration mode.
7404 * For driverinit configuration mode driver should use the function
7405 * devlink_port_param_driverinit_value_set() instead.
7406 */
7407void devlink_port_param_value_changed(struct devlink_port *devlink_port,
7408 u32 param_id)
7409{
7410 struct devlink_param_item *param_item;
7411
7412 param_item = devlink_param_find_by_id(&devlink_port->param_list,
7413 param_id);
7414 WARN_ON(!param_item);
7415
7416 devlink_param_notify(devlink_port->devlink, devlink_port->index,
7417 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
7418}
7419EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
7420
7421/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03007422 * devlink_param_value_str_fill - Safely fill-up the string preventing
7423 * from overflow of the preallocated buffer
7424 *
7425 * @dst_val: destination devlink_param_value
7426 * @src: source buffer
7427 */
7428void devlink_param_value_str_fill(union devlink_param_value *dst_val,
7429 const char *src)
7430{
7431 size_t len;
7432
7433 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
7434 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
7435}
7436EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
7437
7438/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03007439 * devlink_region_create - create a new address region
7440 *
7441 * @devlink: devlink
7442 * @region_name: region name
7443 * @region_max_snapshots: Maximum supported number of snapshots for region
7444 * @region_size: size of region
7445 */
7446struct devlink_region *devlink_region_create(struct devlink *devlink,
7447 const char *region_name,
7448 u32 region_max_snapshots,
7449 u64 region_size)
7450{
7451 struct devlink_region *region;
7452 int err = 0;
7453
7454 mutex_lock(&devlink->lock);
7455
7456 if (devlink_region_get_by_name(devlink, region_name)) {
7457 err = -EEXIST;
7458 goto unlock;
7459 }
7460
7461 region = kzalloc(sizeof(*region), GFP_KERNEL);
7462 if (!region) {
7463 err = -ENOMEM;
7464 goto unlock;
7465 }
7466
7467 region->devlink = devlink;
7468 region->max_snapshots = region_max_snapshots;
7469 region->name = region_name;
7470 region->size = region_size;
7471 INIT_LIST_HEAD(&region->snapshot_list);
7472 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03007473 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007474
7475 mutex_unlock(&devlink->lock);
7476 return region;
7477
7478unlock:
7479 mutex_unlock(&devlink->lock);
7480 return ERR_PTR(err);
7481}
7482EXPORT_SYMBOL_GPL(devlink_region_create);
7483
7484/**
7485 * devlink_region_destroy - destroy address region
7486 *
7487 * @region: devlink region to destroy
7488 */
7489void devlink_region_destroy(struct devlink_region *region)
7490{
7491 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03007492 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03007493
7494 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03007495
7496 /* Free all snapshots of region */
7497 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
Jiri Pirko92b49822019-08-12 14:28:31 +02007498 devlink_region_snapshot_del(region, snapshot);
Alex Veskerd7e52722018-07-12 15:13:10 +03007499
Alex Veskerb16ebe92018-07-12 15:13:08 +03007500 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03007501
7502 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007503 mutex_unlock(&devlink->lock);
7504 kfree(region);
7505}
7506EXPORT_SYMBOL_GPL(devlink_region_destroy);
7507
Alex Veskerccadfa42018-07-12 15:13:09 +03007508/**
7509 * devlink_region_shapshot_id_get - get snapshot ID
7510 *
7511 * This callback should be called when adding a new snapshot,
7512 * Driver should use the same id for multiple snapshots taken
7513 * on multiple regions at the same time/by the same trigger.
7514 *
7515 * @devlink: devlink
7516 */
7517u32 devlink_region_shapshot_id_get(struct devlink *devlink)
7518{
7519 u32 id;
7520
7521 mutex_lock(&devlink->lock);
7522 id = ++devlink->snapshot_id;
7523 mutex_unlock(&devlink->lock);
7524
7525 return id;
7526}
7527EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
7528
Alex Veskerd7e52722018-07-12 15:13:10 +03007529/**
7530 * devlink_region_snapshot_create - create a new snapshot
7531 * This will add a new snapshot of a region. The snapshot
7532 * will be stored on the region struct and can be accessed
7533 * from devlink. This is useful for future analyses of snapshots.
7534 * Multiple snapshots can be created on a region.
7535 * The @snapshot_id should be obtained using the getter function.
7536 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007537 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03007538 * @data: snapshot data
7539 * @snapshot_id: snapshot id to be created
7540 * @data_destructor: pointer to destructor function to free data
7541 */
Jiri Pirko3a5e5232019-08-09 15:27:15 +02007542int devlink_region_snapshot_create(struct devlink_region *region,
Alex Veskerd7e52722018-07-12 15:13:10 +03007543 u8 *data, u32 snapshot_id,
7544 devlink_snapshot_data_dest_t *data_destructor)
7545{
7546 struct devlink *devlink = region->devlink;
7547 struct devlink_snapshot *snapshot;
7548 int err;
7549
7550 mutex_lock(&devlink->lock);
7551
7552 /* check if region can hold one more snapshot */
7553 if (region->cur_snapshots == region->max_snapshots) {
7554 err = -ENOMEM;
7555 goto unlock;
7556 }
7557
7558 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
7559 err = -EEXIST;
7560 goto unlock;
7561 }
7562
7563 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
7564 if (!snapshot) {
7565 err = -ENOMEM;
7566 goto unlock;
7567 }
7568
7569 snapshot->id = snapshot_id;
7570 snapshot->region = region;
7571 snapshot->data = data;
Alex Veskerd7e52722018-07-12 15:13:10 +03007572 snapshot->data_destructor = data_destructor;
7573
7574 list_add_tail(&snapshot->list, &region->snapshot_list);
7575
7576 region->cur_snapshots++;
7577
Alex Vesker866319b2018-07-12 15:13:13 +03007578 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03007579 mutex_unlock(&devlink->lock);
7580 return 0;
7581
7582unlock:
7583 mutex_unlock(&devlink->lock);
7584 return err;
7585}
7586EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
7587
Ido Schimmel0f420b62019-08-17 16:28:17 +03007588#define DEVLINK_TRAP(_id, _type) \
7589 { \
7590 .type = DEVLINK_TRAP_TYPE_##_type, \
7591 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
7592 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
7593 }
7594
7595static const struct devlink_trap devlink_trap_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007596 DEVLINK_TRAP(SMAC_MC, DROP),
7597 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
7598 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
7599 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
7600 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
7601 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
7602 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
7603 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
7604 DEVLINK_TRAP(TAIL_DROP, DROP),
Amit Cohen6896cc42019-11-07 18:42:09 +02007605 DEVLINK_TRAP(NON_IP_PACKET, DROP),
7606 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
7607 DEVLINK_TRAP(DIP_LB, DROP),
7608 DEVLINK_TRAP(SIP_MC, DROP),
7609 DEVLINK_TRAP(SIP_LB, DROP),
7610 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
7611 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
7612 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
7613 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007614};
7615
7616#define DEVLINK_TRAP_GROUP(_id) \
7617 { \
7618 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
7619 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
7620 }
7621
7622static const struct devlink_trap_group devlink_trap_group_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007623 DEVLINK_TRAP_GROUP(L2_DROPS),
7624 DEVLINK_TRAP_GROUP(L3_DROPS),
7625 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007626};
7627
7628static int devlink_trap_generic_verify(const struct devlink_trap *trap)
7629{
7630 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
7631 return -EINVAL;
7632
7633 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
7634 return -EINVAL;
7635
7636 if (trap->type != devlink_trap_generic[trap->id].type)
7637 return -EINVAL;
7638
7639 return 0;
7640}
7641
7642static int devlink_trap_driver_verify(const struct devlink_trap *trap)
7643{
7644 int i;
7645
7646 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
7647 return -EINVAL;
7648
7649 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
7650 if (!strcmp(trap->name, devlink_trap_generic[i].name))
7651 return -EEXIST;
7652 }
7653
7654 return 0;
7655}
7656
7657static int devlink_trap_verify(const struct devlink_trap *trap)
7658{
7659 if (!trap || !trap->name || !trap->group.name)
7660 return -EINVAL;
7661
7662 if (trap->generic)
7663 return devlink_trap_generic_verify(trap);
7664 else
7665 return devlink_trap_driver_verify(trap);
7666}
7667
7668static int
7669devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
7670{
7671 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7672 return -EINVAL;
7673
7674 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
7675 return -EINVAL;
7676
7677 return 0;
7678}
7679
7680static int
7681devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
7682{
7683 int i;
7684
7685 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7686 return -EINVAL;
7687
7688 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
7689 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
7690 return -EEXIST;
7691 }
7692
7693 return 0;
7694}
7695
7696static int devlink_trap_group_verify(const struct devlink_trap_group *group)
7697{
7698 if (group->generic)
7699 return devlink_trap_group_generic_verify(group);
7700 else
7701 return devlink_trap_group_driver_verify(group);
7702}
7703
7704static void
7705devlink_trap_group_notify(struct devlink *devlink,
7706 const struct devlink_trap_group_item *group_item,
7707 enum devlink_command cmd)
7708{
7709 struct sk_buff *msg;
7710 int err;
7711
7712 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
7713 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
7714
7715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7716 if (!msg)
7717 return;
7718
7719 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
7720 0);
7721 if (err) {
7722 nlmsg_free(msg);
7723 return;
7724 }
7725
7726 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7727 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7728}
7729
7730static struct devlink_trap_group_item *
7731devlink_trap_group_item_create(struct devlink *devlink,
7732 const struct devlink_trap_group *group)
7733{
7734 struct devlink_trap_group_item *group_item;
7735 int err;
7736
7737 err = devlink_trap_group_verify(group);
7738 if (err)
7739 return ERR_PTR(err);
7740
7741 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
7742 if (!group_item)
7743 return ERR_PTR(-ENOMEM);
7744
7745 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7746 if (!group_item->stats) {
7747 err = -ENOMEM;
7748 goto err_stats_alloc;
7749 }
7750
7751 group_item->group = group;
7752 refcount_set(&group_item->refcount, 1);
7753
7754 if (devlink->ops->trap_group_init) {
7755 err = devlink->ops->trap_group_init(devlink, group);
7756 if (err)
7757 goto err_group_init;
7758 }
7759
7760 list_add_tail(&group_item->list, &devlink->trap_group_list);
7761 devlink_trap_group_notify(devlink, group_item,
7762 DEVLINK_CMD_TRAP_GROUP_NEW);
7763
7764 return group_item;
7765
7766err_group_init:
7767 free_percpu(group_item->stats);
7768err_stats_alloc:
7769 kfree(group_item);
7770 return ERR_PTR(err);
7771}
7772
7773static void
7774devlink_trap_group_item_destroy(struct devlink *devlink,
7775 struct devlink_trap_group_item *group_item)
7776{
7777 devlink_trap_group_notify(devlink, group_item,
7778 DEVLINK_CMD_TRAP_GROUP_DEL);
7779 list_del(&group_item->list);
7780 free_percpu(group_item->stats);
7781 kfree(group_item);
7782}
7783
7784static struct devlink_trap_group_item *
7785devlink_trap_group_item_get(struct devlink *devlink,
7786 const struct devlink_trap_group *group)
7787{
7788 struct devlink_trap_group_item *group_item;
7789
7790 group_item = devlink_trap_group_item_lookup(devlink, group->name);
7791 if (group_item) {
7792 refcount_inc(&group_item->refcount);
7793 return group_item;
7794 }
7795
7796 return devlink_trap_group_item_create(devlink, group);
7797}
7798
7799static void
7800devlink_trap_group_item_put(struct devlink *devlink,
7801 struct devlink_trap_group_item *group_item)
7802{
7803 if (!refcount_dec_and_test(&group_item->refcount))
7804 return;
7805
7806 devlink_trap_group_item_destroy(devlink, group_item);
7807}
7808
7809static int
7810devlink_trap_item_group_link(struct devlink *devlink,
7811 struct devlink_trap_item *trap_item)
7812{
7813 struct devlink_trap_group_item *group_item;
7814
7815 group_item = devlink_trap_group_item_get(devlink,
7816 &trap_item->trap->group);
7817 if (IS_ERR(group_item))
7818 return PTR_ERR(group_item);
7819
7820 trap_item->group_item = group_item;
7821
7822 return 0;
7823}
7824
7825static void
7826devlink_trap_item_group_unlink(struct devlink *devlink,
7827 struct devlink_trap_item *trap_item)
7828{
7829 devlink_trap_group_item_put(devlink, trap_item->group_item);
7830}
7831
7832static void devlink_trap_notify(struct devlink *devlink,
7833 const struct devlink_trap_item *trap_item,
7834 enum devlink_command cmd)
7835{
7836 struct sk_buff *msg;
7837 int err;
7838
7839 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
7840 cmd != DEVLINK_CMD_TRAP_DEL);
7841
7842 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7843 if (!msg)
7844 return;
7845
7846 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
7847 if (err) {
7848 nlmsg_free(msg);
7849 return;
7850 }
7851
7852 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7853 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7854}
7855
7856static int
7857devlink_trap_register(struct devlink *devlink,
7858 const struct devlink_trap *trap, void *priv)
7859{
7860 struct devlink_trap_item *trap_item;
7861 int err;
7862
7863 if (devlink_trap_item_lookup(devlink, trap->name))
7864 return -EEXIST;
7865
7866 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
7867 if (!trap_item)
7868 return -ENOMEM;
7869
7870 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7871 if (!trap_item->stats) {
7872 err = -ENOMEM;
7873 goto err_stats_alloc;
7874 }
7875
7876 trap_item->trap = trap;
7877 trap_item->action = trap->init_action;
7878 trap_item->priv = priv;
7879
7880 err = devlink_trap_item_group_link(devlink, trap_item);
7881 if (err)
7882 goto err_group_link;
7883
7884 err = devlink->ops->trap_init(devlink, trap, trap_item);
7885 if (err)
7886 goto err_trap_init;
7887
7888 list_add_tail(&trap_item->list, &devlink->trap_list);
7889 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
7890
7891 return 0;
7892
7893err_trap_init:
7894 devlink_trap_item_group_unlink(devlink, trap_item);
7895err_group_link:
7896 free_percpu(trap_item->stats);
7897err_stats_alloc:
7898 kfree(trap_item);
7899 return err;
7900}
7901
7902static void devlink_trap_unregister(struct devlink *devlink,
7903 const struct devlink_trap *trap)
7904{
7905 struct devlink_trap_item *trap_item;
7906
7907 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7908 if (WARN_ON_ONCE(!trap_item))
7909 return;
7910
7911 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
7912 list_del(&trap_item->list);
7913 if (devlink->ops->trap_fini)
7914 devlink->ops->trap_fini(devlink, trap, trap_item);
7915 devlink_trap_item_group_unlink(devlink, trap_item);
7916 free_percpu(trap_item->stats);
7917 kfree(trap_item);
7918}
7919
7920static void devlink_trap_disable(struct devlink *devlink,
7921 const struct devlink_trap *trap)
7922{
7923 struct devlink_trap_item *trap_item;
7924
7925 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7926 if (WARN_ON_ONCE(!trap_item))
7927 return;
7928
7929 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP);
7930 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
7931}
7932
7933/**
7934 * devlink_traps_register - Register packet traps with devlink.
7935 * @devlink: devlink.
7936 * @traps: Packet traps.
7937 * @traps_count: Count of provided packet traps.
7938 * @priv: Driver private information.
7939 *
7940 * Return: Non-zero value on failure.
7941 */
7942int devlink_traps_register(struct devlink *devlink,
7943 const struct devlink_trap *traps,
7944 size_t traps_count, void *priv)
7945{
7946 int i, err;
7947
7948 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
7949 return -EINVAL;
7950
7951 mutex_lock(&devlink->lock);
7952 for (i = 0; i < traps_count; i++) {
7953 const struct devlink_trap *trap = &traps[i];
7954
7955 err = devlink_trap_verify(trap);
7956 if (err)
7957 goto err_trap_verify;
7958
7959 err = devlink_trap_register(devlink, trap, priv);
7960 if (err)
7961 goto err_trap_register;
7962 }
7963 mutex_unlock(&devlink->lock);
7964
7965 return 0;
7966
7967err_trap_register:
7968err_trap_verify:
7969 for (i--; i >= 0; i--)
7970 devlink_trap_unregister(devlink, &traps[i]);
7971 mutex_unlock(&devlink->lock);
7972 return err;
7973}
7974EXPORT_SYMBOL_GPL(devlink_traps_register);
7975
7976/**
7977 * devlink_traps_unregister - Unregister packet traps from devlink.
7978 * @devlink: devlink.
7979 * @traps: Packet traps.
7980 * @traps_count: Count of provided packet traps.
7981 */
7982void devlink_traps_unregister(struct devlink *devlink,
7983 const struct devlink_trap *traps,
7984 size_t traps_count)
7985{
7986 int i;
7987
7988 mutex_lock(&devlink->lock);
7989 /* Make sure we do not have any packets in-flight while unregistering
7990 * traps by disabling all of them and waiting for a grace period.
7991 */
7992 for (i = traps_count - 1; i >= 0; i--)
7993 devlink_trap_disable(devlink, &traps[i]);
7994 synchronize_rcu();
7995 for (i = traps_count - 1; i >= 0; i--)
7996 devlink_trap_unregister(devlink, &traps[i]);
7997 mutex_unlock(&devlink->lock);
7998}
7999EXPORT_SYMBOL_GPL(devlink_traps_unregister);
8000
8001static void
8002devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
8003 size_t skb_len)
8004{
8005 struct devlink_stats *stats;
8006
8007 stats = this_cpu_ptr(trap_stats);
8008 u64_stats_update_begin(&stats->syncp);
8009 stats->rx_bytes += skb_len;
8010 stats->rx_packets++;
8011 u64_stats_update_end(&stats->syncp);
8012}
8013
8014static void
8015devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata,
8016 const struct devlink_trap_item *trap_item,
8017 struct devlink_port *in_devlink_port)
8018{
8019 struct devlink_trap_group_item *group_item = trap_item->group_item;
8020
8021 hw_metadata->trap_group_name = group_item->group->name;
8022 hw_metadata->trap_name = trap_item->trap->name;
8023
8024 spin_lock(&in_devlink_port->type_lock);
8025 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
8026 hw_metadata->input_dev = in_devlink_port->type_dev;
8027 spin_unlock(&in_devlink_port->type_lock);
8028}
8029
8030/**
8031 * devlink_trap_report - Report trapped packet to drop monitor.
8032 * @devlink: devlink.
8033 * @skb: Trapped packet.
8034 * @trap_ctx: Trap context.
8035 * @in_devlink_port: Input devlink port.
8036 */
8037void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
8038 void *trap_ctx, struct devlink_port *in_devlink_port)
8039{
8040 struct devlink_trap_item *trap_item = trap_ctx;
8041 struct net_dm_hw_metadata hw_metadata = {};
8042
8043 devlink_trap_stats_update(trap_item->stats, skb->len);
8044 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
8045
8046 devlink_trap_report_metadata_fill(&hw_metadata, trap_item,
8047 in_devlink_port);
8048 net_dm_hw_report(skb, &hw_metadata);
8049}
8050EXPORT_SYMBOL_GPL(devlink_trap_report);
8051
8052/**
8053 * devlink_trap_ctx_priv - Trap context to driver private information.
8054 * @trap_ctx: Trap context.
8055 *
8056 * Return: Driver private information passed during registration.
8057 */
8058void *devlink_trap_ctx_priv(void *trap_ctx)
8059{
8060 struct devlink_trap_item *trap_item = trap_ctx;
8061
8062 return trap_item->priv;
8063}
8064EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
8065
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008066static void __devlink_compat_running_version(struct devlink *devlink,
8067 char *buf, size_t len)
8068{
8069 const struct nlattr *nlattr;
8070 struct devlink_info_req req;
8071 struct sk_buff *msg;
8072 int rem, err;
8073
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008074 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8075 if (!msg)
8076 return;
8077
8078 req.msg = msg;
8079 err = devlink->ops->info_get(devlink, &req, NULL);
8080 if (err)
8081 goto free_msg;
8082
8083 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
8084 const struct nlattr *kv;
8085 int rem_kv;
8086
8087 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
8088 continue;
8089
8090 nla_for_each_nested(kv, nlattr, rem_kv) {
8091 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
8092 continue;
8093
8094 strlcat(buf, nla_data(kv), len);
8095 strlcat(buf, " ", len);
8096 }
8097 }
8098free_msg:
8099 nlmsg_free(msg);
8100}
8101
8102void devlink_compat_running_version(struct net_device *dev,
8103 char *buf, size_t len)
8104{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008105 struct devlink *devlink;
8106
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008107 dev_hold(dev);
8108 rtnl_unlock();
8109
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008110 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08008111 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008112 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008113
8114 mutex_lock(&devlink->lock);
8115 __devlink_compat_running_version(devlink, buf, len);
8116 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008117
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008118out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008119 rtnl_lock();
8120 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008121}
8122
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008123int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
8124{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008125 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008126 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008127
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008128 dev_hold(dev);
8129 rtnl_unlock();
8130
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008131 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008132 if (!devlink || !devlink->ops->flash_update) {
8133 ret = -EOPNOTSUPP;
8134 goto out;
8135 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008136
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008137 mutex_lock(&devlink->lock);
8138 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
8139 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008140
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008141out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008142 rtnl_lock();
8143 dev_put(dev);
8144
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008145 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008146}
8147
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008148int devlink_compat_phys_port_name_get(struct net_device *dev,
8149 char *name, size_t len)
8150{
8151 struct devlink_port *devlink_port;
8152
8153 /* RTNL mutex is held here which ensures that devlink_port
8154 * instance cannot disappear in the middle. No need to take
8155 * any devlink lock as only permanent values are accessed.
8156 */
8157 ASSERT_RTNL();
8158
8159 devlink_port = netdev_to_devlink_port(dev);
8160 if (!devlink_port)
8161 return -EOPNOTSUPP;
8162
8163 return __devlink_port_phys_port_name_get(devlink_port, name, len);
8164}
8165
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008166int devlink_compat_switch_id_get(struct net_device *dev,
8167 struct netdev_phys_item_id *ppid)
8168{
8169 struct devlink_port *devlink_port;
8170
Vlad Buslov043b8412019-08-12 20:02:02 +03008171 /* Caller must hold RTNL mutex or reference to dev, which ensures that
8172 * devlink_port instance cannot disappear in the middle. No need to take
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008173 * any devlink lock as only permanent values are accessed.
8174 */
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008175 devlink_port = netdev_to_devlink_port(dev);
8176 if (!devlink_port || !devlink_port->attrs.switch_port)
8177 return -EOPNOTSUPP;
8178
8179 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
8180
8181 return 0;
8182}
8183
Jiri Pirko070c63f2019-10-03 11:49:39 +02008184static void __net_exit devlink_pernet_pre_exit(struct net *net)
8185{
8186 struct devlink *devlink;
8187 int err;
8188
8189 /* In case network namespace is getting destroyed, reload
8190 * all devlink instances from this namespace into init_net.
8191 */
8192 mutex_lock(&devlink_mutex);
8193 list_for_each_entry(devlink, &devlink_list, list) {
8194 if (net_eq(devlink_net(devlink), net)) {
8195 if (WARN_ON(!devlink_reload_supported(devlink)))
8196 continue;
8197 err = devlink_reload(devlink, &init_net, NULL);
8198 if (err)
8199 pr_warn("Failed to reload devlink instance into init_net\n");
8200 }
8201 }
8202 mutex_unlock(&devlink_mutex);
8203}
8204
8205static struct pernet_operations devlink_pernet_ops __net_initdata = {
8206 .pre_exit = devlink_pernet_pre_exit,
8207};
8208
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008209static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008210{
Jiri Pirko070c63f2019-10-03 11:49:39 +02008211 int err;
8212
8213 err = genl_register_family(&devlink_nl_family);
8214 if (err)
8215 goto out;
8216 err = register_pernet_subsys(&devlink_pernet_ops);
8217
8218out:
8219 WARN_ON(err);
8220 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008221}
8222
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008223subsys_initcall(devlink_init);