blob: 0d7c5d3443d26a43765bbcf9c861a27b6d0efb5a [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
Jiri Pirkoa0c76342019-11-08 21:42:43 +01002794 if (!devlink->reload_enabled)
2795 return -EOPNOTSUPP;
2796
Jiri Pirko070c63f2019-10-03 11:49:39 +02002797 err = devlink->ops->reload_down(devlink, !!dest_net, extack);
2798 if (err)
2799 return err;
2800
2801 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
2802 devlink_reload_netns_change(devlink, dest_net);
2803
2804 err = devlink->ops->reload_up(devlink, extack);
2805 devlink_reload_failed_set(devlink, !!err);
2806 return err;
2807}
2808
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002809static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2810{
2811 struct devlink *devlink = info->user_ptr[0];
Jiri Pirko070c63f2019-10-03 11:49:39 +02002812 struct net *dest_net = NULL;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002813 int err;
2814
Jiri Pirko5a508a22019-11-09 11:29:46 +01002815 if (!devlink_reload_supported(devlink) || !devlink->reload_enabled)
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002816 return -EOPNOTSUPP;
2817
2818 err = devlink_resources_validate(devlink, NULL, info);
2819 if (err) {
2820 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2821 return err;
2822 }
Jiri Pirko070c63f2019-10-03 11:49:39 +02002823
2824 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
2825 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
2826 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
2827 dest_net = devlink_netns_get(skb, info);
2828 if (IS_ERR(dest_net))
2829 return PTR_ERR(dest_net);
2830 }
2831
2832 err = devlink_reload(devlink, dest_net, info->extack);
2833
2834 if (dest_net)
2835 put_net(dest_net);
2836
Jiri Pirko2670ac22019-09-12 10:49:46 +02002837 return err;
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002838}
2839
Jiri Pirko191ed202019-06-04 15:40:40 +02002840static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2841 struct devlink *devlink,
2842 enum devlink_command cmd,
2843 const char *status_msg,
2844 const char *component,
2845 unsigned long done, unsigned long total)
2846{
2847 void *hdr;
2848
2849 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2850 if (!hdr)
2851 return -EMSGSIZE;
2852
2853 if (devlink_nl_put_handle(msg, devlink))
2854 goto nla_put_failure;
2855
2856 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2857 goto out;
2858
2859 if (status_msg &&
2860 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2861 status_msg))
2862 goto nla_put_failure;
2863 if (component &&
2864 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2865 component))
2866 goto nla_put_failure;
2867 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2868 done, DEVLINK_ATTR_PAD))
2869 goto nla_put_failure;
2870 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2871 total, DEVLINK_ATTR_PAD))
2872 goto nla_put_failure;
2873
2874out:
2875 genlmsg_end(msg, hdr);
2876 return 0;
2877
2878nla_put_failure:
2879 genlmsg_cancel(msg, hdr);
2880 return -EMSGSIZE;
2881}
2882
2883static void __devlink_flash_update_notify(struct devlink *devlink,
2884 enum devlink_command cmd,
2885 const char *status_msg,
2886 const char *component,
2887 unsigned long done,
2888 unsigned long total)
2889{
2890 struct sk_buff *msg;
2891 int err;
2892
2893 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2894 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2895 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2896
2897 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2898 if (!msg)
2899 return;
2900
2901 err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2902 component, done, total);
2903 if (err)
2904 goto out_free_msg;
2905
2906 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2907 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2908 return;
2909
2910out_free_msg:
2911 nlmsg_free(msg);
2912}
2913
2914void devlink_flash_update_begin_notify(struct devlink *devlink)
2915{
2916 __devlink_flash_update_notify(devlink,
2917 DEVLINK_CMD_FLASH_UPDATE,
2918 NULL, NULL, 0, 0);
2919}
2920EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2921
2922void devlink_flash_update_end_notify(struct devlink *devlink)
2923{
2924 __devlink_flash_update_notify(devlink,
2925 DEVLINK_CMD_FLASH_UPDATE_END,
2926 NULL, NULL, 0, 0);
2927}
2928EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2929
2930void devlink_flash_update_status_notify(struct devlink *devlink,
2931 const char *status_msg,
2932 const char *component,
2933 unsigned long done,
2934 unsigned long total)
2935{
2936 __devlink_flash_update_notify(devlink,
2937 DEVLINK_CMD_FLASH_UPDATE_STATUS,
2938 status_msg, component, done, total);
2939}
2940EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2941
Jakub Kicinski76726cc2019-02-14 13:40:44 -08002942static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2943 struct genl_info *info)
2944{
2945 struct devlink *devlink = info->user_ptr[0];
2946 const char *file_name, *component;
2947 struct nlattr *nla_component;
2948
2949 if (!devlink->ops->flash_update)
2950 return -EOPNOTSUPP;
2951
2952 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2953 return -EINVAL;
2954 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2955
2956 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2957 component = nla_component ? nla_data(nla_component) : NULL;
2958
2959 return devlink->ops->flash_update(devlink, file_name, component,
2960 info->extack);
2961}
2962
Moshe Shemesh036467c2018-07-04 14:30:33 +03002963static const struct devlink_param devlink_param_generic[] = {
2964 {
2965 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2966 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2967 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2968 },
2969 {
2970 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2971 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2972 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2973 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002974 {
2975 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2976 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2977 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2978 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002979 {
2980 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2981 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2982 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2983 },
Vasundhara Volame3b51062018-10-04 11:13:44 +05302984 {
2985 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2986 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2987 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2988 },
Vasundhara Volamf61cba42018-10-04 11:13:45 +05302989 {
2990 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2991 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2992 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2993 },
Vasundhara Volam16511782018-10-04 11:13:46 +05302994 {
2995 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2996 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2997 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2998 },
Shalom Toledo846e9802018-12-03 07:58:59 +00002999 {
3000 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3001 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3002 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3003 },
Dirk van der Merwe5bbd21d2019-09-09 00:54:18 +01003004 {
3005 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3006 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3007 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3008 },
Michael Guralnik6c7295e2019-11-08 23:45:20 +00003009 {
3010 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3011 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3012 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3013 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03003014};
Moshe Shemesheabaef12018-07-04 14:30:28 +03003015
3016static int devlink_param_generic_verify(const struct devlink_param *param)
3017{
3018 /* verify it match generic parameter by id and name */
3019 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3020 return -EINVAL;
3021 if (strcmp(param->name, devlink_param_generic[param->id].name))
3022 return -ENOENT;
3023
3024 WARN_ON(param->type != devlink_param_generic[param->id].type);
3025
3026 return 0;
3027}
3028
3029static int devlink_param_driver_verify(const struct devlink_param *param)
3030{
3031 int i;
3032
3033 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3034 return -EINVAL;
3035 /* verify no such name in generic params */
3036 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3037 if (!strcmp(param->name, devlink_param_generic[i].name))
3038 return -EEXIST;
3039
3040 return 0;
3041}
3042
3043static struct devlink_param_item *
3044devlink_param_find_by_name(struct list_head *param_list,
3045 const char *param_name)
3046{
3047 struct devlink_param_item *param_item;
3048
3049 list_for_each_entry(param_item, param_list, list)
3050 if (!strcmp(param_item->param->name, param_name))
3051 return param_item;
3052 return NULL;
3053}
3054
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03003055static struct devlink_param_item *
3056devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3057{
3058 struct devlink_param_item *param_item;
3059
3060 list_for_each_entry(param_item, param_list, list)
3061 if (param_item->param->id == param_id)
3062 return param_item;
3063 return NULL;
3064}
3065
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003066static bool
3067devlink_param_cmode_is_supported(const struct devlink_param *param,
3068 enum devlink_param_cmode cmode)
3069{
3070 return test_bit(cmode, &param->supported_cmodes);
3071}
3072
3073static int devlink_param_get(struct devlink *devlink,
3074 const struct devlink_param *param,
3075 struct devlink_param_gset_ctx *ctx)
3076{
3077 if (!param->get)
3078 return -EOPNOTSUPP;
3079 return param->get(devlink, param->id, ctx);
3080}
3081
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003082static int devlink_param_set(struct devlink *devlink,
3083 const struct devlink_param *param,
3084 struct devlink_param_gset_ctx *ctx)
3085{
3086 if (!param->set)
3087 return -EOPNOTSUPP;
3088 return param->set(devlink, param->id, ctx);
3089}
3090
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003091static int
3092devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3093{
3094 switch (param_type) {
3095 case DEVLINK_PARAM_TYPE_U8:
3096 return NLA_U8;
3097 case DEVLINK_PARAM_TYPE_U16:
3098 return NLA_U16;
3099 case DEVLINK_PARAM_TYPE_U32:
3100 return NLA_U32;
3101 case DEVLINK_PARAM_TYPE_STRING:
3102 return NLA_STRING;
3103 case DEVLINK_PARAM_TYPE_BOOL:
3104 return NLA_FLAG;
3105 default:
3106 return -EINVAL;
3107 }
3108}
3109
3110static int
3111devlink_nl_param_value_fill_one(struct sk_buff *msg,
3112 enum devlink_param_type type,
3113 enum devlink_param_cmode cmode,
3114 union devlink_param_value val)
3115{
3116 struct nlattr *param_value_attr;
3117
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003118 param_value_attr = nla_nest_start_noflag(msg,
3119 DEVLINK_ATTR_PARAM_VALUE);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003120 if (!param_value_attr)
3121 goto nla_put_failure;
3122
3123 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3124 goto value_nest_cancel;
3125
3126 switch (type) {
3127 case DEVLINK_PARAM_TYPE_U8:
3128 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3129 goto value_nest_cancel;
3130 break;
3131 case DEVLINK_PARAM_TYPE_U16:
3132 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3133 goto value_nest_cancel;
3134 break;
3135 case DEVLINK_PARAM_TYPE_U32:
3136 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3137 goto value_nest_cancel;
3138 break;
3139 case DEVLINK_PARAM_TYPE_STRING:
3140 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3141 val.vstr))
3142 goto value_nest_cancel;
3143 break;
3144 case DEVLINK_PARAM_TYPE_BOOL:
3145 if (val.vbool &&
3146 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3147 goto value_nest_cancel;
3148 break;
3149 }
3150
3151 nla_nest_end(msg, param_value_attr);
3152 return 0;
3153
3154value_nest_cancel:
3155 nla_nest_cancel(msg, param_value_attr);
3156nla_put_failure:
3157 return -EMSGSIZE;
3158}
3159
3160static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303161 unsigned int port_index,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003162 struct devlink_param_item *param_item,
3163 enum devlink_command cmd,
3164 u32 portid, u32 seq, int flags)
3165{
3166 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003167 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003168 const struct devlink_param *param = param_item->param;
3169 struct devlink_param_gset_ctx ctx;
3170 struct nlattr *param_values_list;
3171 struct nlattr *param_attr;
3172 int nla_type;
3173 void *hdr;
3174 int err;
3175 int i;
3176
3177 /* Get value from driver part to driverinit configuration mode */
3178 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3179 if (!devlink_param_cmode_is_supported(param, i))
3180 continue;
3181 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3182 if (!param_item->driverinit_value_valid)
3183 return -EOPNOTSUPP;
3184 param_value[i] = param_item->driverinit_value;
3185 } else {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003186 if (!param_item->published)
3187 continue;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003188 ctx.cmode = i;
3189 err = devlink_param_get(devlink, param, &ctx);
3190 if (err)
3191 return err;
3192 param_value[i] = ctx.val;
3193 }
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003194 param_value_set[i] = true;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003195 }
3196
3197 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3198 if (!hdr)
3199 return -EMSGSIZE;
3200
3201 if (devlink_nl_put_handle(msg, devlink))
3202 goto genlmsg_cancel;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303203
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303204 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3205 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3206 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303207 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3208 goto genlmsg_cancel;
3209
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003210 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003211 if (!param_attr)
3212 goto genlmsg_cancel;
3213 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3214 goto param_nest_cancel;
3215 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3216 goto param_nest_cancel;
3217
3218 nla_type = devlink_param_type_to_nla_type(param->type);
3219 if (nla_type < 0)
3220 goto param_nest_cancel;
3221 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3222 goto param_nest_cancel;
3223
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003224 param_values_list = nla_nest_start_noflag(msg,
3225 DEVLINK_ATTR_PARAM_VALUES_LIST);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003226 if (!param_values_list)
3227 goto param_nest_cancel;
3228
3229 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003230 if (!param_value_set[i])
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003231 continue;
3232 err = devlink_nl_param_value_fill_one(msg, param->type,
3233 i, param_value[i]);
3234 if (err)
3235 goto values_list_nest_cancel;
3236 }
3237
3238 nla_nest_end(msg, param_values_list);
3239 nla_nest_end(msg, param_attr);
3240 genlmsg_end(msg, hdr);
3241 return 0;
3242
3243values_list_nest_cancel:
3244 nla_nest_end(msg, param_values_list);
3245param_nest_cancel:
3246 nla_nest_cancel(msg, param_attr);
3247genlmsg_cancel:
3248 genlmsg_cancel(msg, hdr);
3249 return -EMSGSIZE;
3250}
3251
Moshe Shemeshea601e12018-07-04 14:30:32 +03003252static void devlink_param_notify(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303253 unsigned int port_index,
Moshe Shemeshea601e12018-07-04 14:30:32 +03003254 struct devlink_param_item *param_item,
3255 enum devlink_command cmd)
3256{
3257 struct sk_buff *msg;
3258 int err;
3259
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303260 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3261 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3262 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003263
3264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3265 if (!msg)
3266 return;
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303267 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3268 0, 0, 0);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003269 if (err) {
3270 nlmsg_free(msg);
3271 return;
3272 }
3273
3274 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3275 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3276}
3277
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003278static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3279 struct netlink_callback *cb)
3280{
3281 struct devlink_param_item *param_item;
3282 struct devlink *devlink;
3283 int start = cb->args[0];
3284 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003285 int err = 0;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003286
3287 mutex_lock(&devlink_mutex);
3288 list_for_each_entry(devlink, &devlink_list, list) {
3289 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3290 continue;
3291 mutex_lock(&devlink->lock);
3292 list_for_each_entry(param_item, &devlink->param_list, list) {
3293 if (idx < start) {
3294 idx++;
3295 continue;
3296 }
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303297 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003298 DEVLINK_CMD_PARAM_GET,
3299 NETLINK_CB(cb->skb).portid,
3300 cb->nlh->nlmsg_seq,
3301 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303302 if (err && err != -EOPNOTSUPP) {
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003303 mutex_unlock(&devlink->lock);
3304 goto out;
3305 }
3306 idx++;
3307 }
3308 mutex_unlock(&devlink->lock);
3309 }
3310out:
3311 mutex_unlock(&devlink_mutex);
3312
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003313 if (err != -EMSGSIZE)
3314 return err;
3315
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003316 cb->args[0] = idx;
3317 return msg->len;
3318}
3319
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003320static int
3321devlink_param_type_get_from_info(struct genl_info *info,
3322 enum devlink_param_type *param_type)
3323{
3324 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3325 return -EINVAL;
3326
3327 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3328 case NLA_U8:
3329 *param_type = DEVLINK_PARAM_TYPE_U8;
3330 break;
3331 case NLA_U16:
3332 *param_type = DEVLINK_PARAM_TYPE_U16;
3333 break;
3334 case NLA_U32:
3335 *param_type = DEVLINK_PARAM_TYPE_U32;
3336 break;
3337 case NLA_STRING:
3338 *param_type = DEVLINK_PARAM_TYPE_STRING;
3339 break;
3340 case NLA_FLAG:
3341 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3342 break;
3343 default:
3344 return -EINVAL;
3345 }
3346
3347 return 0;
3348}
3349
3350static int
3351devlink_param_value_get_from_info(const struct devlink_param *param,
3352 struct genl_info *info,
3353 union devlink_param_value *value)
3354{
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003355 int len;
3356
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003357 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3358 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3359 return -EINVAL;
3360
3361 switch (param->type) {
3362 case DEVLINK_PARAM_TYPE_U8:
3363 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3364 break;
3365 case DEVLINK_PARAM_TYPE_U16:
3366 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3367 break;
3368 case DEVLINK_PARAM_TYPE_U32:
3369 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3370 break;
3371 case DEVLINK_PARAM_TYPE_STRING:
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003372 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3373 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3374 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03003375 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003376 return -EINVAL;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003377 strcpy(value->vstr,
3378 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003379 break;
3380 case DEVLINK_PARAM_TYPE_BOOL:
3381 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3382 true : false;
3383 break;
3384 }
3385 return 0;
3386}
3387
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003388static struct devlink_param_item *
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303389devlink_param_get_from_info(struct list_head *param_list,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003390 struct genl_info *info)
3391{
3392 char *param_name;
3393
3394 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3395 return NULL;
3396
3397 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303398 return devlink_param_find_by_name(param_list, param_name);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003399}
3400
3401static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3402 struct genl_info *info)
3403{
3404 struct devlink *devlink = info->user_ptr[0];
3405 struct devlink_param_item *param_item;
3406 struct sk_buff *msg;
3407 int err;
3408
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303409 param_item = devlink_param_get_from_info(&devlink->param_list, info);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003410 if (!param_item)
3411 return -EINVAL;
3412
3413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3414 if (!msg)
3415 return -ENOMEM;
3416
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303417 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003418 DEVLINK_CMD_PARAM_GET,
3419 info->snd_portid, info->snd_seq, 0);
3420 if (err) {
3421 nlmsg_free(msg);
3422 return err;
3423 }
3424
3425 return genlmsg_reply(msg, info);
3426}
3427
Vasundhara Volam9c548732019-01-28 18:00:22 +05303428static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303429 unsigned int port_index,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303430 struct list_head *param_list,
3431 struct genl_info *info,
3432 enum devlink_command cmd)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003433{
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003434 enum devlink_param_type param_type;
3435 struct devlink_param_gset_ctx ctx;
3436 enum devlink_param_cmode cmode;
3437 struct devlink_param_item *param_item;
3438 const struct devlink_param *param;
3439 union devlink_param_value value;
3440 int err = 0;
3441
Vasundhara Volam9c548732019-01-28 18:00:22 +05303442 param_item = devlink_param_get_from_info(param_list, info);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003443 if (!param_item)
3444 return -EINVAL;
3445 param = param_item->param;
3446 err = devlink_param_type_get_from_info(info, &param_type);
3447 if (err)
3448 return err;
3449 if (param_type != param->type)
3450 return -EINVAL;
3451 err = devlink_param_value_get_from_info(param, info, &value);
3452 if (err)
3453 return err;
3454 if (param->validate) {
3455 err = param->validate(devlink, param->id, value, info->extack);
3456 if (err)
3457 return err;
3458 }
3459
3460 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3461 return -EINVAL;
3462 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3463 if (!devlink_param_cmode_is_supported(param, cmode))
3464 return -EOPNOTSUPP;
3465
3466 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
Moshe Shemesh12765342018-10-10 16:09:26 +03003467 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3468 strcpy(param_item->driverinit_value.vstr, value.vstr);
3469 else
3470 param_item->driverinit_value = value;
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003471 param_item->driverinit_value_valid = true;
3472 } else {
3473 if (!param->set)
3474 return -EOPNOTSUPP;
3475 ctx.val = value;
3476 ctx.cmode = cmode;
3477 err = devlink_param_set(devlink, param, &ctx);
3478 if (err)
3479 return err;
3480 }
3481
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303482 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003483 return 0;
3484}
3485
Vasundhara Volam9c548732019-01-28 18:00:22 +05303486static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3487 struct genl_info *info)
3488{
3489 struct devlink *devlink = info->user_ptr[0];
3490
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303491 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303492 info, DEVLINK_CMD_PARAM_NEW);
3493}
3494
Moshe Shemesheabaef12018-07-04 14:30:28 +03003495static int devlink_param_register_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303496 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303497 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303498 const struct devlink_param *param,
3499 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003500{
3501 struct devlink_param_item *param_item;
3502
Vasundhara Volam39e61602019-01-28 18:00:20 +05303503 if (devlink_param_find_by_name(param_list, param->name))
Moshe Shemesheabaef12018-07-04 14:30:28 +03003504 return -EEXIST;
3505
3506 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3507 WARN_ON(param->get || param->set);
3508 else
3509 WARN_ON(!param->get || !param->set);
3510
3511 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3512 if (!param_item)
3513 return -ENOMEM;
3514 param_item->param = param;
3515
Vasundhara Volam39e61602019-01-28 18:00:20 +05303516 list_add_tail(&param_item->list, param_list);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303517 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003518 return 0;
3519}
3520
3521static void devlink_param_unregister_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303522 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303523 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303524 const struct devlink_param *param,
3525 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003526{
3527 struct devlink_param_item *param_item;
3528
Vasundhara Volam39e61602019-01-28 18:00:20 +05303529 param_item = devlink_param_find_by_name(param_list, param->name);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003530 WARN_ON(!param_item);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303531 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003532 list_del(&param_item->list);
3533 kfree(param_item);
3534}
3535
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303536static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3537 struct netlink_callback *cb)
3538{
3539 struct devlink_param_item *param_item;
3540 struct devlink_port *devlink_port;
3541 struct devlink *devlink;
3542 int start = cb->args[0];
3543 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003544 int err = 0;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303545
3546 mutex_lock(&devlink_mutex);
3547 list_for_each_entry(devlink, &devlink_list, list) {
3548 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3549 continue;
3550 mutex_lock(&devlink->lock);
3551 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3552 list_for_each_entry(param_item,
3553 &devlink_port->param_list, list) {
3554 if (idx < start) {
3555 idx++;
3556 continue;
3557 }
3558 err = devlink_nl_param_fill(msg,
3559 devlink_port->devlink,
3560 devlink_port->index, param_item,
3561 DEVLINK_CMD_PORT_PARAM_GET,
3562 NETLINK_CB(cb->skb).portid,
3563 cb->nlh->nlmsg_seq,
3564 NLM_F_MULTI);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05303565 if (err && err != -EOPNOTSUPP) {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303566 mutex_unlock(&devlink->lock);
3567 goto out;
3568 }
3569 idx++;
3570 }
3571 }
3572 mutex_unlock(&devlink->lock);
3573 }
3574out:
3575 mutex_unlock(&devlink_mutex);
3576
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02003577 if (err != -EMSGSIZE)
3578 return err;
3579
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303580 cb->args[0] = idx;
3581 return msg->len;
3582}
3583
3584static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3585 struct genl_info *info)
3586{
3587 struct devlink_port *devlink_port = info->user_ptr[0];
3588 struct devlink_param_item *param_item;
3589 struct sk_buff *msg;
3590 int err;
3591
3592 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3593 info);
3594 if (!param_item)
3595 return -EINVAL;
3596
3597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3598 if (!msg)
3599 return -ENOMEM;
3600
3601 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3602 devlink_port->index, param_item,
3603 DEVLINK_CMD_PORT_PARAM_GET,
3604 info->snd_portid, info->snd_seq, 0);
3605 if (err) {
3606 nlmsg_free(msg);
3607 return err;
3608 }
3609
3610 return genlmsg_reply(msg, info);
3611}
3612
Vasundhara Volam9c548732019-01-28 18:00:22 +05303613static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3614 struct genl_info *info)
3615{
3616 struct devlink_port *devlink_port = info->user_ptr[0];
3617
3618 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303619 devlink_port->index,
3620 &devlink_port->param_list, info,
3621 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam9c548732019-01-28 18:00:22 +05303622}
3623
Alex Veskera006d462018-07-12 15:13:12 +03003624static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3625 struct devlink *devlink,
3626 struct devlink_snapshot *snapshot)
3627{
3628 struct nlattr *snap_attr;
3629 int err;
3630
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003631 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
Alex Veskera006d462018-07-12 15:13:12 +03003632 if (!snap_attr)
3633 return -EINVAL;
3634
3635 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3636 if (err)
3637 goto nla_put_failure;
3638
3639 nla_nest_end(msg, snap_attr);
3640 return 0;
3641
3642nla_put_failure:
3643 nla_nest_cancel(msg, snap_attr);
3644 return err;
3645}
3646
3647static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3648 struct devlink *devlink,
3649 struct devlink_region *region)
3650{
3651 struct devlink_snapshot *snapshot;
3652 struct nlattr *snapshots_attr;
3653 int err;
3654
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003655 snapshots_attr = nla_nest_start_noflag(msg,
3656 DEVLINK_ATTR_REGION_SNAPSHOTS);
Alex Veskera006d462018-07-12 15:13:12 +03003657 if (!snapshots_attr)
3658 return -EINVAL;
3659
3660 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3661 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3662 if (err)
3663 goto nla_put_failure;
3664 }
3665
3666 nla_nest_end(msg, snapshots_attr);
3667 return 0;
3668
3669nla_put_failure:
3670 nla_nest_cancel(msg, snapshots_attr);
3671 return err;
3672}
3673
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003674static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3675 enum devlink_command cmd, u32 portid,
3676 u32 seq, int flags,
3677 struct devlink_region *region)
3678{
3679 void *hdr;
3680 int err;
3681
3682 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3683 if (!hdr)
3684 return -EMSGSIZE;
3685
3686 err = devlink_nl_put_handle(msg, devlink);
3687 if (err)
3688 goto nla_put_failure;
3689
3690 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3691 if (err)
3692 goto nla_put_failure;
3693
3694 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3695 region->size,
3696 DEVLINK_ATTR_PAD);
3697 if (err)
3698 goto nla_put_failure;
3699
Alex Veskera006d462018-07-12 15:13:12 +03003700 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3701 if (err)
3702 goto nla_put_failure;
3703
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003704 genlmsg_end(msg, hdr);
3705 return 0;
3706
3707nla_put_failure:
3708 genlmsg_cancel(msg, hdr);
3709 return err;
3710}
3711
Alex Vesker866319b2018-07-12 15:13:13 +03003712static void devlink_nl_region_notify(struct devlink_region *region,
3713 struct devlink_snapshot *snapshot,
3714 enum devlink_command cmd)
3715{
3716 struct devlink *devlink = region->devlink;
3717 struct sk_buff *msg;
3718 void *hdr;
3719 int err;
3720
3721 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3722
3723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3724 if (!msg)
3725 return;
3726
3727 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3728 if (!hdr)
3729 goto out_free_msg;
3730
3731 err = devlink_nl_put_handle(msg, devlink);
3732 if (err)
3733 goto out_cancel_msg;
3734
3735 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3736 region->name);
3737 if (err)
3738 goto out_cancel_msg;
3739
3740 if (snapshot) {
3741 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3742 snapshot->id);
3743 if (err)
3744 goto out_cancel_msg;
3745 } else {
3746 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3747 region->size, DEVLINK_ATTR_PAD);
3748 if (err)
3749 goto out_cancel_msg;
3750 }
3751 genlmsg_end(msg, hdr);
3752
3753 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3754 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3755
3756 return;
3757
3758out_cancel_msg:
3759 genlmsg_cancel(msg, hdr);
3760out_free_msg:
3761 nlmsg_free(msg);
3762}
3763
Jiri Pirko92b49822019-08-12 14:28:31 +02003764static void devlink_region_snapshot_del(struct devlink_region *region,
3765 struct devlink_snapshot *snapshot)
3766{
3767 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3768 region->cur_snapshots--;
3769 list_del(&snapshot->list);
3770 (*snapshot->data_destructor)(snapshot->data);
3771 kfree(snapshot);
3772}
3773
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003774static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3775 struct genl_info *info)
3776{
3777 struct devlink *devlink = info->user_ptr[0];
3778 struct devlink_region *region;
3779 const char *region_name;
3780 struct sk_buff *msg;
3781 int err;
3782
3783 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3784 return -EINVAL;
3785
3786 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3787 region = devlink_region_get_by_name(devlink, region_name);
3788 if (!region)
3789 return -EINVAL;
3790
3791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3792 if (!msg)
3793 return -ENOMEM;
3794
3795 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3796 info->snd_portid, info->snd_seq, 0,
3797 region);
3798 if (err) {
3799 nlmsg_free(msg);
3800 return err;
3801 }
3802
3803 return genlmsg_reply(msg, info);
3804}
3805
3806static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3807 struct netlink_callback *cb)
3808{
3809 struct devlink_region *region;
3810 struct devlink *devlink;
3811 int start = cb->args[0];
3812 int idx = 0;
3813 int err;
3814
3815 mutex_lock(&devlink_mutex);
3816 list_for_each_entry(devlink, &devlink_list, list) {
3817 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3818 continue;
3819
3820 mutex_lock(&devlink->lock);
3821 list_for_each_entry(region, &devlink->region_list, list) {
3822 if (idx < start) {
3823 idx++;
3824 continue;
3825 }
3826 err = devlink_nl_region_fill(msg, devlink,
3827 DEVLINK_CMD_REGION_GET,
3828 NETLINK_CB(cb->skb).portid,
3829 cb->nlh->nlmsg_seq,
3830 NLM_F_MULTI, region);
3831 if (err) {
3832 mutex_unlock(&devlink->lock);
3833 goto out;
3834 }
3835 idx++;
3836 }
3837 mutex_unlock(&devlink->lock);
3838 }
3839out:
3840 mutex_unlock(&devlink_mutex);
3841 cb->args[0] = idx;
3842 return msg->len;
3843}
3844
Alex Vesker866319b2018-07-12 15:13:13 +03003845static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3846 struct genl_info *info)
3847{
3848 struct devlink *devlink = info->user_ptr[0];
3849 struct devlink_snapshot *snapshot;
3850 struct devlink_region *region;
3851 const char *region_name;
3852 u32 snapshot_id;
3853
3854 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3855 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3856 return -EINVAL;
3857
3858 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3859 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3860
3861 region = devlink_region_get_by_name(devlink, region_name);
3862 if (!region)
3863 return -EINVAL;
3864
3865 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3866 if (!snapshot)
3867 return -EINVAL;
3868
Jiri Pirko92b49822019-08-12 14:28:31 +02003869 devlink_region_snapshot_del(region, snapshot);
Alex Vesker866319b2018-07-12 15:13:13 +03003870 return 0;
3871}
3872
Alex Vesker4e547952018-07-12 15:13:14 +03003873static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3874 struct devlink *devlink,
3875 u8 *chunk, u32 chunk_size,
3876 u64 addr)
3877{
3878 struct nlattr *chunk_attr;
3879 int err;
3880
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003881 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
Alex Vesker4e547952018-07-12 15:13:14 +03003882 if (!chunk_attr)
3883 return -EINVAL;
3884
3885 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3886 if (err)
3887 goto nla_put_failure;
3888
3889 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3890 DEVLINK_ATTR_PAD);
3891 if (err)
3892 goto nla_put_failure;
3893
3894 nla_nest_end(msg, chunk_attr);
3895 return 0;
3896
3897nla_put_failure:
3898 nla_nest_cancel(msg, chunk_attr);
3899 return err;
3900}
3901
3902#define DEVLINK_REGION_READ_CHUNK_SIZE 256
3903
3904static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3905 struct devlink *devlink,
3906 struct devlink_region *region,
3907 struct nlattr **attrs,
3908 u64 start_offset,
3909 u64 end_offset,
3910 bool dump,
3911 u64 *new_offset)
3912{
3913 struct devlink_snapshot *snapshot;
3914 u64 curr_offset = start_offset;
3915 u32 snapshot_id;
3916 int err = 0;
3917
3918 *new_offset = start_offset;
3919
3920 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3921 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3922 if (!snapshot)
3923 return -EINVAL;
3924
Jiri Pirko3a5e5232019-08-09 15:27:15 +02003925 if (end_offset > region->size || dump)
3926 end_offset = region->size;
Alex Vesker4e547952018-07-12 15:13:14 +03003927
3928 while (curr_offset < end_offset) {
3929 u32 data_size;
3930 u8 *data;
3931
3932 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3933 data_size = end_offset - curr_offset;
3934 else
3935 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3936
3937 data = &snapshot->data[curr_offset];
3938 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3939 data, data_size,
3940 curr_offset);
3941 if (err)
3942 break;
3943
3944 curr_offset += data_size;
3945 }
3946 *new_offset = curr_offset;
3947
3948 return err;
3949}
3950
3951static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3952 struct netlink_callback *cb)
3953{
Jiri Pirkoee85da52019-10-05 20:04:42 +02003954 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Alex Vesker4e547952018-07-12 15:13:14 +03003955 u64 ret_offset, start_offset, end_offset = 0;
Jiri Pirkoee85da52019-10-05 20:04:42 +02003956 struct nlattr **attrs = info->attrs;
Alex Vesker4e547952018-07-12 15:13:14 +03003957 struct devlink_region *region;
3958 struct nlattr *chunks_attr;
3959 const char *region_name;
3960 struct devlink *devlink;
3961 bool dump = true;
3962 void *hdr;
3963 int err;
3964
3965 start_offset = *((u64 *)&cb->args[0]);
3966
Parav Panditdac7c082019-02-12 14:24:08 -06003967 mutex_lock(&devlink_mutex);
Alex Vesker4e547952018-07-12 15:13:14 +03003968 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003969 if (IS_ERR(devlink)) {
3970 err = PTR_ERR(devlink);
Parav Panditdac7c082019-02-12 14:24:08 -06003971 goto out_dev;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003972 }
Alex Vesker4e547952018-07-12 15:13:14 +03003973
Alex Vesker4e547952018-07-12 15:13:14 +03003974 mutex_lock(&devlink->lock);
3975
3976 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
Parav Panditfdd41ec2019-02-12 14:23:58 -06003977 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3978 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003979 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003980 }
Alex Vesker4e547952018-07-12 15:13:14 +03003981
3982 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3983 region = devlink_region_get_by_name(devlink, region_name);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003984 if (!region) {
3985 err = -EINVAL;
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
Jacob Kellerd5b90e92020-02-04 15:59:50 -08003989 /* return 0 if there is no further data to read */
3990 if (start_offset >= region->size) {
3991 err = 0;
3992 goto out_unlock;
3993 }
3994
Alex Vesker4e547952018-07-12 15:13:14 +03003995 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3996 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3997 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003998 if (!hdr) {
3999 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004000 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004001 }
Alex Vesker4e547952018-07-12 15:13:14 +03004002
4003 err = devlink_nl_put_handle(skb, devlink);
4004 if (err)
4005 goto nla_put_failure;
4006
4007 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4008 if (err)
4009 goto nla_put_failure;
4010
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004011 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004012 if (!chunks_attr) {
4013 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004014 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004015 }
Alex Vesker4e547952018-07-12 15:13:14 +03004016
4017 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4018 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4019 if (!start_offset)
4020 start_offset =
4021 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4022
4023 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4024 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4025 dump = false;
4026 }
4027
4028 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4029 region, attrs,
4030 start_offset,
4031 end_offset, dump,
4032 &ret_offset);
4033
4034 if (err && err != -EMSGSIZE)
4035 goto nla_put_failure;
4036
4037 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06004038 if (ret_offset == start_offset) {
4039 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004040 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004041 }
Alex Vesker4e547952018-07-12 15:13:14 +03004042
4043 *((u64 *)&cb->args[0]) = ret_offset;
4044
4045 nla_nest_end(skb, chunks_attr);
4046 genlmsg_end(skb, hdr);
4047 mutex_unlock(&devlink->lock);
4048 mutex_unlock(&devlink_mutex);
4049
4050 return skb->len;
4051
4052nla_put_failure:
4053 genlmsg_cancel(skb, hdr);
4054out_unlock:
4055 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06004056out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03004057 mutex_unlock(&devlink_mutex);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004058 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03004059}
4060
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004061struct devlink_info_req {
4062 struct sk_buff *msg;
4063};
4064
4065int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
4066{
4067 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
4068}
4069EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
4070
4071int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
4072{
4073 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
4074}
4075EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
4076
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004077static int devlink_info_version_put(struct devlink_info_req *req, int attr,
4078 const char *version_name,
4079 const char *version_value)
4080{
4081 struct nlattr *nest;
4082 int err;
4083
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004084 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004085 if (!nest)
4086 return -EMSGSIZE;
4087
4088 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
4089 version_name);
4090 if (err)
4091 goto nla_put_failure;
4092
4093 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
4094 version_value);
4095 if (err)
4096 goto nla_put_failure;
4097
4098 nla_nest_end(req->msg, nest);
4099
4100 return 0;
4101
4102nla_put_failure:
4103 nla_nest_cancel(req->msg, nest);
4104 return err;
4105}
4106
4107int devlink_info_version_fixed_put(struct devlink_info_req *req,
4108 const char *version_name,
4109 const char *version_value)
4110{
4111 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
4112 version_name, version_value);
4113}
4114EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
4115
4116int devlink_info_version_stored_put(struct devlink_info_req *req,
4117 const char *version_name,
4118 const char *version_value)
4119{
4120 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
4121 version_name, version_value);
4122}
4123EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
4124
4125int devlink_info_version_running_put(struct devlink_info_req *req,
4126 const char *version_name,
4127 const char *version_value)
4128{
4129 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4130 version_name, version_value);
4131}
4132EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4133
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004134static int
4135devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4136 enum devlink_command cmd, u32 portid,
4137 u32 seq, int flags, struct netlink_ext_ack *extack)
4138{
4139 struct devlink_info_req req;
4140 void *hdr;
4141 int err;
4142
4143 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4144 if (!hdr)
4145 return -EMSGSIZE;
4146
4147 err = -EMSGSIZE;
4148 if (devlink_nl_put_handle(msg, devlink))
4149 goto err_cancel_msg;
4150
4151 req.msg = msg;
4152 err = devlink->ops->info_get(devlink, &req, extack);
4153 if (err)
4154 goto err_cancel_msg;
4155
4156 genlmsg_end(msg, hdr);
4157 return 0;
4158
4159err_cancel_msg:
4160 genlmsg_cancel(msg, hdr);
4161 return err;
4162}
4163
4164static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4165 struct genl_info *info)
4166{
4167 struct devlink *devlink = info->user_ptr[0];
4168 struct sk_buff *msg;
4169 int err;
4170
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08004171 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004172 return -EOPNOTSUPP;
4173
4174 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4175 if (!msg)
4176 return -ENOMEM;
4177
4178 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4179 info->snd_portid, info->snd_seq, 0,
4180 info->extack);
4181 if (err) {
4182 nlmsg_free(msg);
4183 return err;
4184 }
4185
4186 return genlmsg_reply(msg, info);
4187}
4188
4189static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4190 struct netlink_callback *cb)
4191{
4192 struct devlink *devlink;
4193 int start = cb->args[0];
4194 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004195 int err = 0;
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004196
4197 mutex_lock(&devlink_mutex);
4198 list_for_each_entry(devlink, &devlink_list, list) {
4199 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4200 continue;
4201 if (idx < start) {
4202 idx++;
4203 continue;
4204 }
4205
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004206 if (!devlink->ops->info_get) {
4207 idx++;
4208 continue;
4209 }
4210
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004211 mutex_lock(&devlink->lock);
4212 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4213 NETLINK_CB(cb->skb).portid,
4214 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4215 cb->extack);
4216 mutex_unlock(&devlink->lock);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05304217 if (err && err != -EOPNOTSUPP)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004218 break;
4219 idx++;
4220 }
4221 mutex_unlock(&devlink_mutex);
4222
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004223 if (err != -EMSGSIZE)
4224 return err;
4225
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004226 cb->args[0] = idx;
4227 return msg->len;
4228}
4229
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004230struct devlink_fmsg_item {
4231 struct list_head list;
4232 int attrtype;
4233 u8 nla_type;
4234 u16 len;
4235 int value[0];
4236};
4237
4238struct devlink_fmsg {
4239 struct list_head item_list;
Aya Levin573ed902020-02-11 14:32:42 -08004240 bool putting_binary; /* This flag forces enclosing of binary data
4241 * in an array brackets. It forces using
4242 * of designated API:
4243 * devlink_fmsg_binary_pair_nest_start()
4244 * devlink_fmsg_binary_pair_nest_end()
4245 */
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004246};
4247
4248static struct devlink_fmsg *devlink_fmsg_alloc(void)
4249{
4250 struct devlink_fmsg *fmsg;
4251
4252 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4253 if (!fmsg)
4254 return NULL;
4255
4256 INIT_LIST_HEAD(&fmsg->item_list);
4257
4258 return fmsg;
4259}
4260
4261static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4262{
4263 struct devlink_fmsg_item *item, *tmp;
4264
4265 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4266 list_del(&item->list);
4267 kfree(item);
4268 }
4269 kfree(fmsg);
4270}
4271
4272static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4273 int attrtype)
4274{
4275 struct devlink_fmsg_item *item;
4276
4277 item = kzalloc(sizeof(*item), GFP_KERNEL);
4278 if (!item)
4279 return -ENOMEM;
4280
4281 item->attrtype = attrtype;
4282 list_add_tail(&item->list, &fmsg->item_list);
4283
4284 return 0;
4285}
4286
4287int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4288{
Aya Levin573ed902020-02-11 14:32:42 -08004289 if (fmsg->putting_binary)
4290 return -EINVAL;
4291
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004292 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4293}
4294EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4295
4296static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4297{
Aya Levin573ed902020-02-11 14:32:42 -08004298 if (fmsg->putting_binary)
4299 return -EINVAL;
4300
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004301 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4302}
4303
4304int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4305{
Aya Levin573ed902020-02-11 14:32:42 -08004306 if (fmsg->putting_binary)
4307 return -EINVAL;
4308
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004309 return devlink_fmsg_nest_end(fmsg);
4310}
4311EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4312
4313#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4314
4315static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4316{
4317 struct devlink_fmsg_item *item;
4318
Aya Levin573ed902020-02-11 14:32:42 -08004319 if (fmsg->putting_binary)
4320 return -EINVAL;
4321
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004322 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4323 return -EMSGSIZE;
4324
4325 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4326 if (!item)
4327 return -ENOMEM;
4328
4329 item->nla_type = NLA_NUL_STRING;
4330 item->len = strlen(name) + 1;
4331 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4332 memcpy(&item->value, name, item->len);
4333 list_add_tail(&item->list, &fmsg->item_list);
4334
4335 return 0;
4336}
4337
4338int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4339{
4340 int err;
4341
Aya Levin573ed902020-02-11 14:32:42 -08004342 if (fmsg->putting_binary)
4343 return -EINVAL;
4344
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004345 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4346 if (err)
4347 return err;
4348
4349 err = devlink_fmsg_put_name(fmsg, name);
4350 if (err)
4351 return err;
4352
4353 return 0;
4354}
4355EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4356
4357int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4358{
Aya Levin573ed902020-02-11 14:32:42 -08004359 if (fmsg->putting_binary)
4360 return -EINVAL;
4361
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004362 return devlink_fmsg_nest_end(fmsg);
4363}
4364EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4365
4366int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4367 const char *name)
4368{
4369 int err;
4370
Aya Levin573ed902020-02-11 14:32:42 -08004371 if (fmsg->putting_binary)
4372 return -EINVAL;
4373
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004374 err = devlink_fmsg_pair_nest_start(fmsg, name);
4375 if (err)
4376 return err;
4377
4378 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4379 if (err)
4380 return err;
4381
4382 return 0;
4383}
4384EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4385
4386int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4387{
4388 int err;
4389
Aya Levin573ed902020-02-11 14:32:42 -08004390 if (fmsg->putting_binary)
4391 return -EINVAL;
4392
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004393 err = devlink_fmsg_nest_end(fmsg);
4394 if (err)
4395 return err;
4396
4397 err = devlink_fmsg_nest_end(fmsg);
4398 if (err)
4399 return err;
4400
4401 return 0;
4402}
4403EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4404
Aya Levin573ed902020-02-11 14:32:42 -08004405int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
4406 const char *name)
4407{
4408 int err;
4409
4410 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
4411 if (err)
4412 return err;
4413
4414 fmsg->putting_binary = true;
4415 return err;
4416}
4417EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
4418
4419int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
4420{
4421 if (!fmsg->putting_binary)
4422 return -EINVAL;
4423
4424 fmsg->putting_binary = false;
4425 return devlink_fmsg_arr_pair_nest_end(fmsg);
4426}
4427EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
4428
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004429static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4430 const void *value, u16 value_len,
4431 u8 value_nla_type)
4432{
4433 struct devlink_fmsg_item *item;
4434
4435 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4436 return -EMSGSIZE;
4437
4438 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4439 if (!item)
4440 return -ENOMEM;
4441
4442 item->nla_type = value_nla_type;
4443 item->len = value_len;
4444 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4445 memcpy(&item->value, value, item->len);
4446 list_add_tail(&item->list, &fmsg->item_list);
4447
4448 return 0;
4449}
4450
4451int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4452{
Aya Levin573ed902020-02-11 14:32:42 -08004453 if (fmsg->putting_binary)
4454 return -EINVAL;
4455
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004456 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4457}
4458EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4459
4460int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4461{
Aya Levin573ed902020-02-11 14:32:42 -08004462 if (fmsg->putting_binary)
4463 return -EINVAL;
4464
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004465 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4466}
4467EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4468
4469int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4470{
Aya Levin573ed902020-02-11 14:32:42 -08004471 if (fmsg->putting_binary)
4472 return -EINVAL;
4473
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004474 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4475}
4476EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4477
4478int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4479{
Aya Levin573ed902020-02-11 14:32:42 -08004480 if (fmsg->putting_binary)
4481 return -EINVAL;
4482
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004483 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4484}
4485EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4486
4487int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4488{
Aya Levin573ed902020-02-11 14:32:42 -08004489 if (fmsg->putting_binary)
4490 return -EINVAL;
4491
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004492 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4493 NLA_NUL_STRING);
4494}
4495EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4496
Aya Levin573ed902020-02-11 14:32:42 -08004497int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4498 u16 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004499{
Aya Levin573ed902020-02-11 14:32:42 -08004500 if (!fmsg->putting_binary)
4501 return -EINVAL;
4502
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004503 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4504}
Aya Levin573ed902020-02-11 14:32:42 -08004505EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004506
4507int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4508 bool value)
4509{
4510 int err;
4511
4512 err = devlink_fmsg_pair_nest_start(fmsg, name);
4513 if (err)
4514 return err;
4515
4516 err = devlink_fmsg_bool_put(fmsg, value);
4517 if (err)
4518 return err;
4519
4520 err = devlink_fmsg_pair_nest_end(fmsg);
4521 if (err)
4522 return err;
4523
4524 return 0;
4525}
4526EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4527
4528int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4529 u8 value)
4530{
4531 int err;
4532
4533 err = devlink_fmsg_pair_nest_start(fmsg, name);
4534 if (err)
4535 return err;
4536
4537 err = devlink_fmsg_u8_put(fmsg, value);
4538 if (err)
4539 return err;
4540
4541 err = devlink_fmsg_pair_nest_end(fmsg);
4542 if (err)
4543 return err;
4544
4545 return 0;
4546}
4547EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4548
4549int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4550 u32 value)
4551{
4552 int err;
4553
4554 err = devlink_fmsg_pair_nest_start(fmsg, name);
4555 if (err)
4556 return err;
4557
4558 err = devlink_fmsg_u32_put(fmsg, value);
4559 if (err)
4560 return err;
4561
4562 err = devlink_fmsg_pair_nest_end(fmsg);
4563 if (err)
4564 return err;
4565
4566 return 0;
4567}
4568EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4569
4570int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4571 u64 value)
4572{
4573 int err;
4574
4575 err = devlink_fmsg_pair_nest_start(fmsg, name);
4576 if (err)
4577 return err;
4578
4579 err = devlink_fmsg_u64_put(fmsg, value);
4580 if (err)
4581 return err;
4582
4583 err = devlink_fmsg_pair_nest_end(fmsg);
4584 if (err)
4585 return err;
4586
4587 return 0;
4588}
4589EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4590
4591int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4592 const char *value)
4593{
4594 int err;
4595
4596 err = devlink_fmsg_pair_nest_start(fmsg, name);
4597 if (err)
4598 return err;
4599
4600 err = devlink_fmsg_string_put(fmsg, value);
4601 if (err)
4602 return err;
4603
4604 err = devlink_fmsg_pair_nest_end(fmsg);
4605 if (err)
4606 return err;
4607
4608 return 0;
4609}
4610EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4611
4612int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
Aya Levine2cde862019-11-12 14:07:49 +02004613 const void *value, u32 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004614{
Aya Levine2cde862019-11-12 14:07:49 +02004615 u32 data_size;
Aya Levin573ed902020-02-11 14:32:42 -08004616 int end_err;
Aya Levine2cde862019-11-12 14:07:49 +02004617 u32 offset;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004618 int err;
4619
Aya Levin573ed902020-02-11 14:32:42 -08004620 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004621 if (err)
4622 return err;
4623
Aya Levine2cde862019-11-12 14:07:49 +02004624 for (offset = 0; offset < value_len; offset += data_size) {
4625 data_size = value_len - offset;
4626 if (data_size > DEVLINK_FMSG_MAX_SIZE)
4627 data_size = DEVLINK_FMSG_MAX_SIZE;
4628 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
4629 if (err)
Aya Levin573ed902020-02-11 14:32:42 -08004630 break;
4631 /* Exit from loop with a break (instead of
4632 * return) to make sure putting_binary is turned off in
4633 * devlink_fmsg_binary_pair_nest_end
4634 */
Aya Levine2cde862019-11-12 14:07:49 +02004635 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004636
Aya Levin573ed902020-02-11 14:32:42 -08004637 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
4638 if (end_err)
4639 err = end_err;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004640
Aya Levin573ed902020-02-11 14:32:42 -08004641 return err;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004642}
4643EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4644
4645static int
4646devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4647{
4648 switch (msg->nla_type) {
4649 case NLA_FLAG:
4650 case NLA_U8:
4651 case NLA_U32:
4652 case NLA_U64:
4653 case NLA_NUL_STRING:
4654 case NLA_BINARY:
4655 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4656 msg->nla_type);
4657 default:
4658 return -EINVAL;
4659 }
4660}
4661
4662static int
4663devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4664{
4665 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4666 u8 tmp;
4667
4668 switch (msg->nla_type) {
4669 case NLA_FLAG:
4670 /* Always provide flag data, regardless of its value */
4671 tmp = *(bool *) msg->value;
4672
4673 return nla_put_u8(skb, attrtype, tmp);
4674 case NLA_U8:
4675 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4676 case NLA_U32:
4677 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4678 case NLA_U64:
4679 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4680 DEVLINK_ATTR_PAD);
4681 case NLA_NUL_STRING:
4682 return nla_put_string(skb, attrtype, (char *) &msg->value);
4683 case NLA_BINARY:
4684 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4685 default:
4686 return -EINVAL;
4687 }
4688}
4689
4690static int
4691devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4692 int *start)
4693{
4694 struct devlink_fmsg_item *item;
4695 struct nlattr *fmsg_nlattr;
4696 int i = 0;
4697 int err;
4698
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004699 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004700 if (!fmsg_nlattr)
4701 return -EMSGSIZE;
4702
4703 list_for_each_entry(item, &fmsg->item_list, list) {
4704 if (i < *start) {
4705 i++;
4706 continue;
4707 }
4708
4709 switch (item->attrtype) {
4710 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4711 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4712 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4713 case DEVLINK_ATTR_FMSG_NEST_END:
4714 err = nla_put_flag(skb, item->attrtype);
4715 break;
4716 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4717 err = devlink_fmsg_item_fill_type(item, skb);
4718 if (err)
4719 break;
4720 err = devlink_fmsg_item_fill_data(item, skb);
4721 break;
4722 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4723 err = nla_put_string(skb, item->attrtype,
4724 (char *) &item->value);
4725 break;
4726 default:
4727 err = -EINVAL;
4728 break;
4729 }
4730 if (!err)
4731 *start = ++i;
4732 else
4733 break;
4734 }
4735
4736 nla_nest_end(skb, fmsg_nlattr);
4737 return err;
4738}
4739
4740static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4741 struct genl_info *info,
4742 enum devlink_command cmd, int flags)
4743{
4744 struct nlmsghdr *nlh;
4745 struct sk_buff *skb;
4746 bool last = false;
4747 int index = 0;
4748 void *hdr;
4749 int err;
4750
4751 while (!last) {
4752 int tmp_index = index;
4753
4754 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4755 if (!skb)
4756 return -ENOMEM;
4757
4758 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4759 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4760 if (!hdr) {
4761 err = -EMSGSIZE;
4762 goto nla_put_failure;
4763 }
4764
4765 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4766 if (!err)
4767 last = true;
4768 else if (err != -EMSGSIZE || tmp_index == index)
4769 goto nla_put_failure;
4770
4771 genlmsg_end(skb, hdr);
4772 err = genlmsg_reply(skb, info);
4773 if (err)
4774 return err;
4775 }
4776
4777 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4778 if (!skb)
4779 return -ENOMEM;
4780 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4781 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4782 if (!nlh) {
4783 err = -EMSGSIZE;
4784 goto nla_put_failure;
4785 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004786
Li RongQingfde55ea2019-02-11 19:09:07 +08004787 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004788
4789nla_put_failure:
4790 nlmsg_free(skb);
4791 return err;
4792}
4793
Aya Levine44ef4e2019-05-16 09:49:20 +03004794static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4795 struct netlink_callback *cb,
4796 enum devlink_command cmd)
4797{
4798 int index = cb->args[0];
4799 int tmp_index = index;
4800 void *hdr;
4801 int err;
4802
4803 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4804 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4805 if (!hdr) {
4806 err = -EMSGSIZE;
4807 goto nla_put_failure;
4808 }
4809
4810 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4811 if ((err && err != -EMSGSIZE) || tmp_index == index)
4812 goto nla_put_failure;
4813
4814 cb->args[0] = index;
4815 genlmsg_end(skb, hdr);
4816 return skb->len;
4817
4818nla_put_failure:
4819 genlmsg_cancel(skb, hdr);
4820 return err;
4821}
4822
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004823struct devlink_health_reporter {
4824 struct list_head list;
4825 void *priv;
4826 const struct devlink_health_reporter_ops *ops;
4827 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004828 struct devlink_fmsg *dump_fmsg;
4829 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004830 u64 graceful_period;
4831 bool auto_recover;
4832 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004833 u64 dump_ts;
Aya Levind2795052019-11-10 14:11:56 +02004834 u64 dump_real_ts;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004835 u64 error_count;
4836 u64 recovery_count;
4837 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004838 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004839};
4840
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004841void *
4842devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4843{
4844 return reporter->priv;
4845}
4846EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4847
4848static struct devlink_health_reporter *
4849devlink_health_reporter_find_by_name(struct devlink *devlink,
4850 const char *reporter_name)
4851{
4852 struct devlink_health_reporter *reporter;
4853
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004854 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004855 list_for_each_entry(reporter, &devlink->reporter_list, list)
4856 if (!strcmp(reporter->ops->name, reporter_name))
4857 return reporter;
4858 return NULL;
4859}
4860
4861/**
4862 * devlink_health_reporter_create - create devlink health reporter
4863 *
4864 * @devlink: devlink
4865 * @ops: ops
4866 * @graceful_period: to avoid recovery loops, in msecs
4867 * @auto_recover: auto recover when error occurs
4868 * @priv: priv
4869 */
4870struct devlink_health_reporter *
4871devlink_health_reporter_create(struct devlink *devlink,
4872 const struct devlink_health_reporter_ops *ops,
4873 u64 graceful_period, bool auto_recover,
4874 void *priv)
4875{
4876 struct devlink_health_reporter *reporter;
4877
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004878 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004879 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4880 reporter = ERR_PTR(-EEXIST);
4881 goto unlock;
4882 }
4883
4884 if (WARN_ON(auto_recover && !ops->recover) ||
4885 WARN_ON(graceful_period && !ops->recover)) {
4886 reporter = ERR_PTR(-EINVAL);
4887 goto unlock;
4888 }
4889
4890 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4891 if (!reporter) {
4892 reporter = ERR_PTR(-ENOMEM);
4893 goto unlock;
4894 }
4895
4896 reporter->priv = priv;
4897 reporter->ops = ops;
4898 reporter->devlink = devlink;
4899 reporter->graceful_period = graceful_period;
4900 reporter->auto_recover = auto_recover;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004901 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004902 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004903 list_add_tail(&reporter->list, &devlink->reporter_list);
4904unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004905 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004906 return reporter;
4907}
4908EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4909
4910/**
4911 * devlink_health_reporter_destroy - destroy devlink health reporter
4912 *
4913 * @reporter: devlink health reporter to destroy
4914 */
4915void
4916devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4917{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004918 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004919 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004920 mutex_unlock(&reporter->devlink->reporters_lock);
4921 while (refcount_read(&reporter->refcount) > 1)
4922 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01004923 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004924 if (reporter->dump_fmsg)
4925 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004926 kfree(reporter);
4927}
4928EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4929
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004930static int
4931devlink_nl_health_reporter_fill(struct sk_buff *msg,
4932 struct devlink *devlink,
4933 struct devlink_health_reporter *reporter,
4934 enum devlink_command cmd, u32 portid,
4935 u32 seq, int flags)
4936{
4937 struct nlattr *reporter_attr;
4938 void *hdr;
4939
4940 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4941 if (!hdr)
4942 return -EMSGSIZE;
4943
4944 if (devlink_nl_put_handle(msg, devlink))
4945 goto genlmsg_cancel;
4946
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004947 reporter_attr = nla_nest_start_noflag(msg,
4948 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004949 if (!reporter_attr)
4950 goto genlmsg_cancel;
4951 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4952 reporter->ops->name))
4953 goto reporter_nest_cancel;
4954 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4955 reporter->health_state))
4956 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004957 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004958 reporter->error_count, DEVLINK_ATTR_PAD))
4959 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004960 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004961 reporter->recovery_count, DEVLINK_ATTR_PAD))
4962 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004963 if (reporter->ops->recover &&
4964 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004965 reporter->graceful_period,
4966 DEVLINK_ATTR_PAD))
4967 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004968 if (reporter->ops->recover &&
4969 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004970 reporter->auto_recover))
4971 goto reporter_nest_cancel;
4972 if (reporter->dump_fmsg &&
4973 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4974 jiffies_to_msecs(reporter->dump_ts),
4975 DEVLINK_ATTR_PAD))
4976 goto reporter_nest_cancel;
Aya Levind2795052019-11-10 14:11:56 +02004977 if (reporter->dump_fmsg &&
4978 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
4979 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
4980 goto reporter_nest_cancel;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004981
4982 nla_nest_end(msg, reporter_attr);
4983 genlmsg_end(msg, hdr);
4984 return 0;
4985
4986reporter_nest_cancel:
4987 nla_nest_end(msg, reporter_attr);
4988genlmsg_cancel:
4989 genlmsg_cancel(msg, hdr);
4990 return -EMSGSIZE;
4991}
4992
Vikas Gupta97ff3bd2020-01-02 21:18:10 +05304993static void devlink_recover_notify(struct devlink_health_reporter *reporter,
4994 enum devlink_command cmd)
4995{
4996 struct sk_buff *msg;
4997 int err;
4998
4999 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5000
5001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5002 if (!msg)
5003 return;
5004
5005 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
5006 reporter, cmd, 0, 0, 0);
5007 if (err) {
5008 nlmsg_free(msg);
5009 return;
5010 }
5011
5012 genlmsg_multicast_netns(&devlink_nl_family,
5013 devlink_net(reporter->devlink),
5014 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5015}
5016
5017void
Moshe Shemesh6ec8b6c2020-01-23 19:57:13 +02005018devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
5019{
5020 reporter->recovery_count++;
5021 reporter->last_recovery_ts = jiffies;
5022}
5023EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
5024
5025static int
5026devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
5027 void *priv_ctx, struct netlink_ext_ack *extack)
5028{
5029 int err;
5030
5031 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
5032 return 0;
5033
5034 if (!reporter->ops->recover)
5035 return -EOPNOTSUPP;
5036
5037 err = reporter->ops->recover(reporter, priv_ctx, extack);
5038 if (err)
5039 return err;
5040
5041 devlink_health_reporter_recovery_done(reporter);
5042 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
5043 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5044
5045 return 0;
5046}
5047
5048static void
5049devlink_health_dump_clear(struct devlink_health_reporter *reporter)
5050{
5051 if (!reporter->dump_fmsg)
5052 return;
5053 devlink_fmsg_free(reporter->dump_fmsg);
5054 reporter->dump_fmsg = NULL;
5055}
5056
5057static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
5058 void *priv_ctx,
5059 struct netlink_ext_ack *extack)
5060{
5061 int err;
5062
5063 if (!reporter->ops->dump)
5064 return 0;
5065
5066 if (reporter->dump_fmsg)
5067 return 0;
5068
5069 reporter->dump_fmsg = devlink_fmsg_alloc();
5070 if (!reporter->dump_fmsg) {
5071 err = -ENOMEM;
5072 return err;
5073 }
5074
5075 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
5076 if (err)
5077 goto dump_err;
5078
5079 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
5080 priv_ctx, extack);
5081 if (err)
5082 goto dump_err;
5083
5084 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
5085 if (err)
5086 goto dump_err;
5087
5088 reporter->dump_ts = jiffies;
5089 reporter->dump_real_ts = ktime_get_real_ns();
5090
5091 return 0;
5092
5093dump_err:
5094 devlink_health_dump_clear(reporter);
5095 return err;
5096}
5097
5098int devlink_health_report(struct devlink_health_reporter *reporter,
5099 const char *msg, void *priv_ctx)
5100{
5101 enum devlink_health_reporter_state prev_health_state;
5102 struct devlink *devlink = reporter->devlink;
5103
5104 /* write a log message of the current error */
5105 WARN_ON(!msg);
5106 trace_devlink_health_report(devlink, reporter->ops->name, msg);
5107 reporter->error_count++;
5108 prev_health_state = reporter->health_state;
5109 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5110 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5111
5112 /* abort if the previous error wasn't recovered */
5113 if (reporter->auto_recover &&
5114 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
5115 jiffies - reporter->last_recovery_ts <
5116 msecs_to_jiffies(reporter->graceful_period))) {
5117 trace_devlink_health_recover_aborted(devlink,
5118 reporter->ops->name,
5119 reporter->health_state,
5120 jiffies -
5121 reporter->last_recovery_ts);
5122 return -ECANCELED;
5123 }
5124
5125 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5126
5127 mutex_lock(&reporter->dump_lock);
5128 /* store current dump of current error, for later analysis */
5129 devlink_health_do_dump(reporter, priv_ctx, NULL);
5130 mutex_unlock(&reporter->dump_lock);
5131
5132 if (reporter->auto_recover)
5133 return devlink_health_reporter_recover(reporter,
5134 priv_ctx, NULL);
5135
5136 return 0;
5137}
5138EXPORT_SYMBOL_GPL(devlink_health_report);
5139
5140static struct devlink_health_reporter *
5141devlink_health_reporter_get_from_attrs(struct devlink *devlink,
5142 struct nlattr **attrs)
5143{
5144 struct devlink_health_reporter *reporter;
5145 char *reporter_name;
5146
5147 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
5148 return NULL;
5149
5150 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
5151 mutex_lock(&devlink->reporters_lock);
5152 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
5153 if (reporter)
5154 refcount_inc(&reporter->refcount);
5155 mutex_unlock(&devlink->reporters_lock);
5156 return reporter;
5157}
5158
5159static struct devlink_health_reporter *
5160devlink_health_reporter_get_from_info(struct devlink *devlink,
5161 struct genl_info *info)
5162{
5163 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
5164}
5165
5166static struct devlink_health_reporter *
5167devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
5168{
5169 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5170 struct devlink_health_reporter *reporter;
5171 struct nlattr **attrs = info->attrs;
5172 struct devlink *devlink;
5173
5174 mutex_lock(&devlink_mutex);
5175 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5176 if (IS_ERR(devlink))
5177 goto unlock;
5178
5179 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
5180 mutex_unlock(&devlink_mutex);
5181 return reporter;
5182unlock:
5183 mutex_unlock(&devlink_mutex);
5184 return NULL;
5185}
5186
5187static void
5188devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5189{
5190 refcount_dec(&reporter->refcount);
5191}
5192
5193void
Vikas Gupta97ff3bd2020-01-02 21:18:10 +05305194devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
5195 enum devlink_health_reporter_state state)
5196{
5197 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
5198 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
5199 return;
5200
5201 if (reporter->health_state == state)
5202 return;
5203
5204 reporter->health_state = state;
5205 trace_devlink_health_reporter_state_update(reporter->devlink,
5206 reporter->ops->name, state);
5207 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5208}
5209EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
5210
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005211static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
5212 struct genl_info *info)
5213{
5214 struct devlink *devlink = info->user_ptr[0];
5215 struct devlink_health_reporter *reporter;
5216 struct sk_buff *msg;
5217 int err;
5218
5219 reporter = devlink_health_reporter_get_from_info(devlink, info);
5220 if (!reporter)
5221 return -EINVAL;
5222
5223 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005224 if (!msg) {
5225 err = -ENOMEM;
5226 goto out;
5227 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005228
5229 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5230 DEVLINK_CMD_HEALTH_REPORTER_GET,
5231 info->snd_portid, info->snd_seq,
5232 0);
5233 if (err) {
5234 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005235 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005236 }
5237
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005238 err = genlmsg_reply(msg, info);
5239out:
5240 devlink_health_reporter_put(reporter);
5241 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005242}
5243
5244static int
5245devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5246 struct netlink_callback *cb)
5247{
5248 struct devlink_health_reporter *reporter;
5249 struct devlink *devlink;
5250 int start = cb->args[0];
5251 int idx = 0;
5252 int err;
5253
5254 mutex_lock(&devlink_mutex);
5255 list_for_each_entry(devlink, &devlink_list, list) {
5256 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5257 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005258 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005259 list_for_each_entry(reporter, &devlink->reporter_list,
5260 list) {
5261 if (idx < start) {
5262 idx++;
5263 continue;
5264 }
5265 err = devlink_nl_health_reporter_fill(msg, devlink,
5266 reporter,
5267 DEVLINK_CMD_HEALTH_REPORTER_GET,
5268 NETLINK_CB(cb->skb).portid,
5269 cb->nlh->nlmsg_seq,
5270 NLM_F_MULTI);
5271 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005272 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005273 goto out;
5274 }
5275 idx++;
5276 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005277 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005278 }
5279out:
5280 mutex_unlock(&devlink_mutex);
5281
5282 cb->args[0] = idx;
5283 return msg->len;
5284}
5285
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005286static int
5287devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5288 struct genl_info *info)
5289{
5290 struct devlink *devlink = info->user_ptr[0];
5291 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005292 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005293
5294 reporter = devlink_health_reporter_get_from_info(devlink, info);
5295 if (!reporter)
5296 return -EINVAL;
5297
5298 if (!reporter->ops->recover &&
5299 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005300 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5301 err = -EOPNOTSUPP;
5302 goto out;
5303 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005304
5305 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5306 reporter->graceful_period =
5307 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5308
5309 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5310 reporter->auto_recover =
5311 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5312
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005313 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005314 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005315out:
5316 devlink_health_reporter_put(reporter);
5317 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005318}
5319
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005320static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5321 struct genl_info *info)
5322{
5323 struct devlink *devlink = info->user_ptr[0];
5324 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005325 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005326
5327 reporter = devlink_health_reporter_get_from_info(devlink, info);
5328 if (!reporter)
5329 return -EINVAL;
5330
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005331 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005332
5333 devlink_health_reporter_put(reporter);
5334 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005335}
5336
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005337static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5338 struct genl_info *info)
5339{
5340 struct devlink *devlink = info->user_ptr[0];
5341 struct devlink_health_reporter *reporter;
5342 struct devlink_fmsg *fmsg;
5343 int err;
5344
5345 reporter = devlink_health_reporter_get_from_info(devlink, info);
5346 if (!reporter)
5347 return -EINVAL;
5348
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005349 if (!reporter->ops->diagnose) {
5350 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005351 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005352 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005353
5354 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005355 if (!fmsg) {
5356 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005357 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005358 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005359
5360 err = devlink_fmsg_obj_nest_start(fmsg);
5361 if (err)
5362 goto out;
5363
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005364 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005365 if (err)
5366 goto out;
5367
5368 err = devlink_fmsg_obj_nest_end(fmsg);
5369 if (err)
5370 goto out;
5371
5372 err = devlink_fmsg_snd(fmsg, info,
5373 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5374
5375out:
5376 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005377 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005378 return err;
5379}
5380
Aya Levine44ef4e2019-05-16 09:49:20 +03005381static int
5382devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5383 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005384{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005385 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005386 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005387 int err;
5388
Aya Levine44ef4e2019-05-16 09:49:20 +03005389 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005390 if (!reporter)
5391 return -EINVAL;
5392
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005393 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005394 err = -EOPNOTSUPP;
5395 goto out;
5396 }
5397 mutex_lock(&reporter->dump_lock);
5398 if (!start) {
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005399 err = devlink_health_do_dump(reporter, NULL, cb->extack);
Aya Levine44ef4e2019-05-16 09:49:20 +03005400 if (err)
5401 goto unlock;
5402 cb->args[1] = reporter->dump_ts;
5403 }
5404 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5405 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5406 err = -EAGAIN;
5407 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005408 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005409
Aya Levine44ef4e2019-05-16 09:49:20 +03005410 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5411 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5412unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005413 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005414out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005415 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005416 return err;
5417}
5418
5419static int
5420devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5421 struct genl_info *info)
5422{
5423 struct devlink *devlink = info->user_ptr[0];
5424 struct devlink_health_reporter *reporter;
5425
5426 reporter = devlink_health_reporter_get_from_info(devlink, info);
5427 if (!reporter)
5428 return -EINVAL;
5429
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005430 if (!reporter->ops->dump) {
5431 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005432 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005433 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005434
5435 mutex_lock(&reporter->dump_lock);
5436 devlink_health_dump_clear(reporter);
5437 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005438 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005439 return 0;
5440}
5441
Ido Schimmel0f420b62019-08-17 16:28:17 +03005442struct devlink_stats {
5443 u64 rx_bytes;
5444 u64 rx_packets;
5445 struct u64_stats_sync syncp;
5446};
5447
5448/**
5449 * struct devlink_trap_group_item - Packet trap group attributes.
5450 * @group: Immutable packet trap group attributes.
5451 * @refcount: Number of trap items using the group.
5452 * @list: trap_group_list member.
5453 * @stats: Trap group statistics.
5454 *
5455 * Describes packet trap group attributes. Created by devlink during trap
5456 * registration.
5457 */
5458struct devlink_trap_group_item {
5459 const struct devlink_trap_group *group;
5460 refcount_t refcount;
5461 struct list_head list;
5462 struct devlink_stats __percpu *stats;
5463};
5464
5465/**
5466 * struct devlink_trap_item - Packet trap attributes.
5467 * @trap: Immutable packet trap attributes.
5468 * @group_item: Associated group item.
5469 * @list: trap_list member.
5470 * @action: Trap action.
5471 * @stats: Trap statistics.
5472 * @priv: Driver private information.
5473 *
5474 * Describes both mutable and immutable packet trap attributes. Created by
5475 * devlink during trap registration and used for all trap related operations.
5476 */
5477struct devlink_trap_item {
5478 const struct devlink_trap *trap;
5479 struct devlink_trap_group_item *group_item;
5480 struct list_head list;
5481 enum devlink_trap_action action;
5482 struct devlink_stats __percpu *stats;
5483 void *priv;
5484};
5485
5486static struct devlink_trap_item *
5487devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5488{
5489 struct devlink_trap_item *trap_item;
5490
5491 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5492 if (!strcmp(trap_item->trap->name, name))
5493 return trap_item;
5494 }
5495
5496 return NULL;
5497}
5498
5499static struct devlink_trap_item *
5500devlink_trap_item_get_from_info(struct devlink *devlink,
5501 struct genl_info *info)
5502{
5503 struct nlattr *attr;
5504
5505 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5506 return NULL;
5507 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5508
5509 return devlink_trap_item_lookup(devlink, nla_data(attr));
5510}
5511
5512static int
5513devlink_trap_action_get_from_info(struct genl_info *info,
5514 enum devlink_trap_action *p_trap_action)
5515{
5516 u8 val;
5517
5518 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5519 switch (val) {
5520 case DEVLINK_TRAP_ACTION_DROP: /* fall-through */
5521 case DEVLINK_TRAP_ACTION_TRAP:
5522 *p_trap_action = val;
5523 break;
5524 default:
5525 return -EINVAL;
5526 }
5527
5528 return 0;
5529}
5530
5531static int devlink_trap_metadata_put(struct sk_buff *msg,
5532 const struct devlink_trap *trap)
5533{
5534 struct nlattr *attr;
5535
5536 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5537 if (!attr)
5538 return -EMSGSIZE;
5539
5540 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5541 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5542 goto nla_put_failure;
5543
5544 nla_nest_end(msg, attr);
5545
5546 return 0;
5547
5548nla_put_failure:
5549 nla_nest_cancel(msg, attr);
5550 return -EMSGSIZE;
5551}
5552
5553static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5554 struct devlink_stats *stats)
5555{
5556 int i;
5557
5558 memset(stats, 0, sizeof(*stats));
5559 for_each_possible_cpu(i) {
5560 struct devlink_stats *cpu_stats;
5561 u64 rx_packets, rx_bytes;
5562 unsigned int start;
5563
5564 cpu_stats = per_cpu_ptr(trap_stats, i);
5565 do {
5566 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5567 rx_packets = cpu_stats->rx_packets;
5568 rx_bytes = cpu_stats->rx_bytes;
5569 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5570
5571 stats->rx_packets += rx_packets;
5572 stats->rx_bytes += rx_bytes;
5573 }
5574}
5575
5576static int devlink_trap_stats_put(struct sk_buff *msg,
5577 struct devlink_stats __percpu *trap_stats)
5578{
5579 struct devlink_stats stats;
5580 struct nlattr *attr;
5581
5582 devlink_trap_stats_read(trap_stats, &stats);
5583
5584 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5585 if (!attr)
5586 return -EMSGSIZE;
5587
5588 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5589 stats.rx_packets, DEVLINK_ATTR_PAD))
5590 goto nla_put_failure;
5591
5592 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5593 stats.rx_bytes, DEVLINK_ATTR_PAD))
5594 goto nla_put_failure;
5595
5596 nla_nest_end(msg, attr);
5597
5598 return 0;
5599
5600nla_put_failure:
5601 nla_nest_cancel(msg, attr);
5602 return -EMSGSIZE;
5603}
5604
5605static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5606 const struct devlink_trap_item *trap_item,
5607 enum devlink_command cmd, u32 portid, u32 seq,
5608 int flags)
5609{
5610 struct devlink_trap_group_item *group_item = trap_item->group_item;
5611 void *hdr;
5612 int err;
5613
5614 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5615 if (!hdr)
5616 return -EMSGSIZE;
5617
5618 if (devlink_nl_put_handle(msg, devlink))
5619 goto nla_put_failure;
5620
5621 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5622 group_item->group->name))
5623 goto nla_put_failure;
5624
5625 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5626 goto nla_put_failure;
5627
5628 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5629 goto nla_put_failure;
5630
5631 if (trap_item->trap->generic &&
5632 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5633 goto nla_put_failure;
5634
5635 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5636 goto nla_put_failure;
5637
5638 err = devlink_trap_metadata_put(msg, trap_item->trap);
5639 if (err)
5640 goto nla_put_failure;
5641
5642 err = devlink_trap_stats_put(msg, trap_item->stats);
5643 if (err)
5644 goto nla_put_failure;
5645
5646 genlmsg_end(msg, hdr);
5647
5648 return 0;
5649
5650nla_put_failure:
5651 genlmsg_cancel(msg, hdr);
5652 return -EMSGSIZE;
5653}
5654
5655static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
5656 struct genl_info *info)
5657{
5658 struct netlink_ext_ack *extack = info->extack;
5659 struct devlink *devlink = info->user_ptr[0];
5660 struct devlink_trap_item *trap_item;
5661 struct sk_buff *msg;
5662 int err;
5663
5664 if (list_empty(&devlink->trap_list))
5665 return -EOPNOTSUPP;
5666
5667 trap_item = devlink_trap_item_get_from_info(devlink, info);
5668 if (!trap_item) {
5669 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5670 return -ENOENT;
5671 }
5672
5673 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5674 if (!msg)
5675 return -ENOMEM;
5676
5677 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5678 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
5679 info->snd_seq, 0);
5680 if (err)
5681 goto err_trap_fill;
5682
5683 return genlmsg_reply(msg, info);
5684
5685err_trap_fill:
5686 nlmsg_free(msg);
5687 return err;
5688}
5689
5690static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
5691 struct netlink_callback *cb)
5692{
5693 struct devlink_trap_item *trap_item;
5694 struct devlink *devlink;
5695 int start = cb->args[0];
5696 int idx = 0;
5697 int err;
5698
5699 mutex_lock(&devlink_mutex);
5700 list_for_each_entry(devlink, &devlink_list, list) {
5701 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5702 continue;
5703 mutex_lock(&devlink->lock);
5704 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5705 if (idx < start) {
5706 idx++;
5707 continue;
5708 }
5709 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5710 DEVLINK_CMD_TRAP_NEW,
5711 NETLINK_CB(cb->skb).portid,
5712 cb->nlh->nlmsg_seq,
5713 NLM_F_MULTI);
5714 if (err) {
5715 mutex_unlock(&devlink->lock);
5716 goto out;
5717 }
5718 idx++;
5719 }
5720 mutex_unlock(&devlink->lock);
5721 }
5722out:
5723 mutex_unlock(&devlink_mutex);
5724
5725 cb->args[0] = idx;
5726 return msg->len;
5727}
5728
5729static int __devlink_trap_action_set(struct devlink *devlink,
5730 struct devlink_trap_item *trap_item,
5731 enum devlink_trap_action trap_action,
5732 struct netlink_ext_ack *extack)
5733{
5734 int err;
5735
5736 if (trap_item->action != trap_action &&
5737 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
5738 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
5739 return 0;
5740 }
5741
5742 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
5743 trap_action);
5744 if (err)
5745 return err;
5746
5747 trap_item->action = trap_action;
5748
5749 return 0;
5750}
5751
5752static int devlink_trap_action_set(struct devlink *devlink,
5753 struct devlink_trap_item *trap_item,
5754 struct genl_info *info)
5755{
5756 enum devlink_trap_action trap_action;
5757 int err;
5758
5759 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5760 return 0;
5761
5762 err = devlink_trap_action_get_from_info(info, &trap_action);
5763 if (err) {
5764 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5765 return -EINVAL;
5766 }
5767
5768 return __devlink_trap_action_set(devlink, trap_item, trap_action,
5769 info->extack);
5770}
5771
5772static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
5773 struct genl_info *info)
5774{
5775 struct netlink_ext_ack *extack = info->extack;
5776 struct devlink *devlink = info->user_ptr[0];
5777 struct devlink_trap_item *trap_item;
5778 int err;
5779
5780 if (list_empty(&devlink->trap_list))
5781 return -EOPNOTSUPP;
5782
5783 trap_item = devlink_trap_item_get_from_info(devlink, info);
5784 if (!trap_item) {
5785 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5786 return -ENOENT;
5787 }
5788
5789 err = devlink_trap_action_set(devlink, trap_item, info);
5790 if (err)
5791 return err;
5792
5793 return 0;
5794}
5795
5796static struct devlink_trap_group_item *
5797devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
5798{
5799 struct devlink_trap_group_item *group_item;
5800
5801 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5802 if (!strcmp(group_item->group->name, name))
5803 return group_item;
5804 }
5805
5806 return NULL;
5807}
5808
5809static struct devlink_trap_group_item *
5810devlink_trap_group_item_get_from_info(struct devlink *devlink,
5811 struct genl_info *info)
5812{
5813 char *name;
5814
5815 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
5816 return NULL;
5817 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
5818
5819 return devlink_trap_group_item_lookup(devlink, name);
5820}
5821
5822static int
5823devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
5824 const struct devlink_trap_group_item *group_item,
5825 enum devlink_command cmd, u32 portid, u32 seq,
5826 int flags)
5827{
5828 void *hdr;
5829 int err;
5830
5831 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5832 if (!hdr)
5833 return -EMSGSIZE;
5834
5835 if (devlink_nl_put_handle(msg, devlink))
5836 goto nla_put_failure;
5837
5838 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5839 group_item->group->name))
5840 goto nla_put_failure;
5841
5842 if (group_item->group->generic &&
5843 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5844 goto nla_put_failure;
5845
5846 err = devlink_trap_stats_put(msg, group_item->stats);
5847 if (err)
5848 goto nla_put_failure;
5849
5850 genlmsg_end(msg, hdr);
5851
5852 return 0;
5853
5854nla_put_failure:
5855 genlmsg_cancel(msg, hdr);
5856 return -EMSGSIZE;
5857}
5858
5859static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
5860 struct genl_info *info)
5861{
5862 struct netlink_ext_ack *extack = info->extack;
5863 struct devlink *devlink = info->user_ptr[0];
5864 struct devlink_trap_group_item *group_item;
5865 struct sk_buff *msg;
5866 int err;
5867
5868 if (list_empty(&devlink->trap_group_list))
5869 return -EOPNOTSUPP;
5870
5871 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5872 if (!group_item) {
5873 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5874 return -ENOENT;
5875 }
5876
5877 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5878 if (!msg)
5879 return -ENOMEM;
5880
5881 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5882 DEVLINK_CMD_TRAP_GROUP_NEW,
5883 info->snd_portid, info->snd_seq, 0);
5884 if (err)
5885 goto err_trap_group_fill;
5886
5887 return genlmsg_reply(msg, info);
5888
5889err_trap_group_fill:
5890 nlmsg_free(msg);
5891 return err;
5892}
5893
5894static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
5895 struct netlink_callback *cb)
5896{
5897 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
5898 struct devlink_trap_group_item *group_item;
5899 u32 portid = NETLINK_CB(cb->skb).portid;
5900 struct devlink *devlink;
5901 int start = cb->args[0];
5902 int idx = 0;
5903 int err;
5904
5905 mutex_lock(&devlink_mutex);
5906 list_for_each_entry(devlink, &devlink_list, list) {
5907 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5908 continue;
5909 mutex_lock(&devlink->lock);
5910 list_for_each_entry(group_item, &devlink->trap_group_list,
5911 list) {
5912 if (idx < start) {
5913 idx++;
5914 continue;
5915 }
5916 err = devlink_nl_trap_group_fill(msg, devlink,
5917 group_item, cmd,
5918 portid,
5919 cb->nlh->nlmsg_seq,
5920 NLM_F_MULTI);
5921 if (err) {
5922 mutex_unlock(&devlink->lock);
5923 goto out;
5924 }
5925 idx++;
5926 }
5927 mutex_unlock(&devlink->lock);
5928 }
5929out:
5930 mutex_unlock(&devlink_mutex);
5931
5932 cb->args[0] = idx;
5933 return msg->len;
5934}
5935
5936static int
5937__devlink_trap_group_action_set(struct devlink *devlink,
5938 struct devlink_trap_group_item *group_item,
5939 enum devlink_trap_action trap_action,
5940 struct netlink_ext_ack *extack)
5941{
5942 const char *group_name = group_item->group->name;
5943 struct devlink_trap_item *trap_item;
5944 int err;
5945
5946 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5947 if (strcmp(trap_item->trap->group.name, group_name))
5948 continue;
5949 err = __devlink_trap_action_set(devlink, trap_item,
5950 trap_action, extack);
5951 if (err)
5952 return err;
5953 }
5954
5955 return 0;
5956}
5957
5958static int
5959devlink_trap_group_action_set(struct devlink *devlink,
5960 struct devlink_trap_group_item *group_item,
5961 struct genl_info *info)
5962{
5963 enum devlink_trap_action trap_action;
5964 int err;
5965
5966 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5967 return 0;
5968
5969 err = devlink_trap_action_get_from_info(info, &trap_action);
5970 if (err) {
5971 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5972 return -EINVAL;
5973 }
5974
5975 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
5976 info->extack);
5977 if (err)
5978 return err;
5979
5980 return 0;
5981}
5982
5983static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
5984 struct genl_info *info)
5985{
5986 struct netlink_ext_ack *extack = info->extack;
5987 struct devlink *devlink = info->user_ptr[0];
5988 struct devlink_trap_group_item *group_item;
5989 int err;
5990
5991 if (list_empty(&devlink->trap_group_list))
5992 return -EOPNOTSUPP;
5993
5994 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5995 if (!group_item) {
5996 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5997 return -ENOENT;
5998 }
5999
6000 err = devlink_trap_group_action_set(devlink, group_item, info);
6001 if (err)
6002 return err;
6003
6004 return 0;
6005}
6006
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006007static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
6008 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
6009 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
6010 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
6011 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
6012 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02006013 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
6014 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
6015 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
6016 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
6017 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
6018 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
6019 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006020 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02006021 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03006022 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006023 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
6024 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006025 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
6026 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006027 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
6028 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
6029 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006030 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03006031 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006032 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006033 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
6034 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006035 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
6036 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006037 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
6038 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
6039 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirko070c63f2019-10-03 11:49:39 +02006040 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
6041 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
6042 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006043};
6044
6045static const struct genl_ops devlink_nl_ops[] = {
6046 {
6047 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006048 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006049 .doit = devlink_nl_cmd_get_doit,
6050 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02006051 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006052 /* can be retrieved by unprivileged users */
6053 },
6054 {
6055 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006056 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006057 .doit = devlink_nl_cmd_port_get_doit,
6058 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006059 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6060 /* can be retrieved by unprivileged users */
6061 },
6062 {
6063 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006064 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006065 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006066 .flags = GENL_ADMIN_PERM,
6067 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6068 },
6069 {
6070 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006071 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006072 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006073 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006074 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6075 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006076 },
6077 {
6078 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006079 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006080 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006081 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006082 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6083 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006084 },
Jiri Pirkobf797472016-04-14 18:19:13 +02006085 {
6086 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006087 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006088 .doit = devlink_nl_cmd_sb_get_doit,
6089 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006090 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6091 DEVLINK_NL_FLAG_NEED_SB,
6092 /* can be retrieved by unprivileged users */
6093 },
6094 {
6095 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006096 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006097 .doit = devlink_nl_cmd_sb_pool_get_doit,
6098 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006099 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6100 DEVLINK_NL_FLAG_NEED_SB,
6101 /* can be retrieved by unprivileged users */
6102 },
6103 {
6104 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006105 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006106 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006107 .flags = GENL_ADMIN_PERM,
6108 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6109 DEVLINK_NL_FLAG_NEED_SB,
6110 },
6111 {
6112 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006113 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006114 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
6115 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006116 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6117 DEVLINK_NL_FLAG_NEED_SB,
6118 /* can be retrieved by unprivileged users */
6119 },
6120 {
6121 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006122 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006123 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006124 .flags = GENL_ADMIN_PERM,
6125 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6126 DEVLINK_NL_FLAG_NEED_SB,
6127 },
6128 {
6129 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006130 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006131 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
6132 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006133 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6134 DEVLINK_NL_FLAG_NEED_SB,
6135 /* can be retrieved by unprivileged users */
6136 },
6137 {
6138 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006139 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006140 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006141 .flags = GENL_ADMIN_PERM,
6142 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6143 DEVLINK_NL_FLAG_NEED_SB,
6144 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006145 {
6146 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006147 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006148 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006149 .flags = GENL_ADMIN_PERM,
6150 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006151 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006152 },
6153 {
6154 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006155 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006156 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006157 .flags = GENL_ADMIN_PERM,
6158 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006159 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006160 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006161 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006162 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006163 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006164 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006165 .flags = GENL_ADMIN_PERM,
6166 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6167 },
6168 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006169 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006170 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006171 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006172 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07006173 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6174 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006175 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006176 {
6177 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006178 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006179 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006180 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006181 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006182 },
6183 {
6184 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006185 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006186 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006187 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006188 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006189 },
6190 {
6191 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006192 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006193 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006194 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006195 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006196 },
6197 {
6198 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006199 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006200 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006201 .flags = GENL_ADMIN_PERM,
6202 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6203 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006204 {
6205 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006206 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006207 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006208 .flags = GENL_ADMIN_PERM,
6209 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6210 },
6211 {
6212 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02006213 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006214 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006215 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006216 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006217 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006218 {
6219 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02006220 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006221 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006222 .flags = GENL_ADMIN_PERM,
6223 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6224 DEVLINK_NL_FLAG_NO_LOCK,
6225 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006226 {
6227 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006228 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006229 .doit = devlink_nl_cmd_param_get_doit,
6230 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006231 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6232 /* can be retrieved by unprivileged users */
6233 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006234 {
6235 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006236 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006237 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006238 .flags = GENL_ADMIN_PERM,
6239 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6240 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006241 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306242 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006243 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306244 .doit = devlink_nl_cmd_port_param_get_doit,
6245 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306246 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6247 /* can be retrieved by unprivileged users */
6248 },
6249 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05306250 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006251 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306252 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306253 .flags = GENL_ADMIN_PERM,
6254 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6255 },
6256 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006257 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006258 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006259 .doit = devlink_nl_cmd_region_get_doit,
6260 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006261 .flags = GENL_ADMIN_PERM,
6262 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6263 },
Alex Vesker866319b2018-07-12 15:13:13 +03006264 {
6265 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02006266 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03006267 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03006268 .flags = GENL_ADMIN_PERM,
6269 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6270 },
Alex Vesker4e547952018-07-12 15:13:14 +03006271 {
6272 .cmd = DEVLINK_CMD_REGION_READ,
Jiri Pirkoee85da52019-10-05 20:04:42 +02006273 .validate = GENL_DONT_VALIDATE_STRICT |
6274 GENL_DONT_VALIDATE_DUMP_STRICT,
Alex Vesker4e547952018-07-12 15:13:14 +03006275 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03006276 .flags = GENL_ADMIN_PERM,
6277 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6278 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006279 {
6280 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006281 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006282 .doit = devlink_nl_cmd_info_get_doit,
6283 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006284 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6285 /* can be retrieved by unprivileged users */
6286 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006287 {
6288 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006289 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006290 .doit = devlink_nl_cmd_health_reporter_get_doit,
6291 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006292 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6293 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006294 /* can be retrieved by unprivileged users */
6295 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006296 {
6297 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006299 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006300 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006301 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6302 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006303 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006304 {
6305 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02006306 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006307 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006308 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006309 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6310 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006311 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006312 {
6313 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006314 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006315 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006316 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006317 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6318 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006319 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006320 {
6321 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Jiri Pirko82a843d2019-10-07 09:28:31 +02006322 .validate = GENL_DONT_VALIDATE_STRICT |
6323 GENL_DONT_VALIDATE_DUMP_STRICT,
Aya Levine44ef4e2019-05-16 09:49:20 +03006324 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006325 .flags = GENL_ADMIN_PERM,
6326 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6327 DEVLINK_NL_FLAG_NO_LOCK,
6328 },
6329 {
6330 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006331 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006332 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006333 .flags = GENL_ADMIN_PERM,
6334 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6335 DEVLINK_NL_FLAG_NO_LOCK,
6336 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006337 {
6338 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006339 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006340 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006341 .flags = GENL_ADMIN_PERM,
6342 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6343 },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006344 {
6345 .cmd = DEVLINK_CMD_TRAP_GET,
6346 .doit = devlink_nl_cmd_trap_get_doit,
6347 .dumpit = devlink_nl_cmd_trap_get_dumpit,
6348 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6349 /* can be retrieved by unprivileged users */
6350 },
6351 {
6352 .cmd = DEVLINK_CMD_TRAP_SET,
6353 .doit = devlink_nl_cmd_trap_set_doit,
6354 .flags = GENL_ADMIN_PERM,
6355 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6356 },
6357 {
6358 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
6359 .doit = devlink_nl_cmd_trap_group_get_doit,
6360 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
6361 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6362 /* can be retrieved by unprivileged users */
6363 },
6364 {
6365 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
6366 .doit = devlink_nl_cmd_trap_group_set_doit,
6367 .flags = GENL_ADMIN_PERM,
6368 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6369 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006370};
6371
Johannes Berg56989f62016-10-24 14:40:05 +02006372static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02006373 .name = DEVLINK_GENL_NAME,
6374 .version = DEVLINK_GENL_VERSION,
6375 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01006376 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02006377 .netnsok = true,
6378 .pre_doit = devlink_nl_pre_doit,
6379 .post_doit = devlink_nl_post_doit,
6380 .module = THIS_MODULE,
6381 .ops = devlink_nl_ops,
6382 .n_ops = ARRAY_SIZE(devlink_nl_ops),
6383 .mcgrps = devlink_nl_mcgrps,
6384 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
6385};
6386
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006387/**
6388 * devlink_alloc - Allocate new devlink instance resources
6389 *
6390 * @ops: ops
6391 * @priv_size: size of user private data
6392 *
6393 * Allocate new devlink instance resources, including devlink index
6394 * and name.
6395 */
6396struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
6397{
6398 struct devlink *devlink;
6399
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006400 if (WARN_ON(!ops))
6401 return NULL;
6402
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006403 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
6404 if (!devlink)
6405 return NULL;
6406 devlink->ops = ops;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006407 __devlink_net_set(devlink, &init_net);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006408 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02006409 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006410 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006411 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006412 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006413 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02006414 INIT_LIST_HEAD(&devlink->reporter_list);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006415 INIT_LIST_HEAD(&devlink->trap_list);
6416 INIT_LIST_HEAD(&devlink->trap_group_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006417 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006418 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006419 return devlink;
6420}
6421EXPORT_SYMBOL_GPL(devlink_alloc);
6422
6423/**
6424 * devlink_register - Register devlink instance
6425 *
6426 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006427 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006428 */
6429int devlink_register(struct devlink *devlink, struct device *dev)
6430{
6431 mutex_lock(&devlink_mutex);
6432 devlink->dev = dev;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006433 devlink->registered = true;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006434 list_add_tail(&devlink->list, &devlink_list);
6435 devlink_notify(devlink, DEVLINK_CMD_NEW);
6436 mutex_unlock(&devlink_mutex);
6437 return 0;
6438}
6439EXPORT_SYMBOL_GPL(devlink_register);
6440
6441/**
6442 * devlink_unregister - Unregister devlink instance
6443 *
6444 * @devlink: devlink
6445 */
6446void devlink_unregister(struct devlink *devlink)
6447{
6448 mutex_lock(&devlink_mutex);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01006449 WARN_ON(devlink_reload_supported(devlink) &&
6450 devlink->reload_enabled);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006451 devlink_notify(devlink, DEVLINK_CMD_DEL);
6452 list_del(&devlink->list);
6453 mutex_unlock(&devlink_mutex);
6454}
6455EXPORT_SYMBOL_GPL(devlink_unregister);
6456
6457/**
Jiri Pirkoa0c76342019-11-08 21:42:43 +01006458 * devlink_reload_enable - Enable reload of devlink instance
6459 *
6460 * @devlink: devlink
6461 *
6462 * Should be called at end of device initialization
6463 * process when reload operation is supported.
6464 */
6465void devlink_reload_enable(struct devlink *devlink)
6466{
6467 mutex_lock(&devlink_mutex);
6468 devlink->reload_enabled = true;
6469 mutex_unlock(&devlink_mutex);
6470}
6471EXPORT_SYMBOL_GPL(devlink_reload_enable);
6472
6473/**
6474 * devlink_reload_disable - Disable reload of devlink instance
6475 *
6476 * @devlink: devlink
6477 *
6478 * Should be called at the beginning of device cleanup
6479 * process when reload operation is supported.
6480 */
6481void devlink_reload_disable(struct devlink *devlink)
6482{
6483 mutex_lock(&devlink_mutex);
6484 /* Mutex is taken which ensures that no reload operation is in
6485 * progress while setting up forbidded flag.
6486 */
6487 devlink->reload_enabled = false;
6488 mutex_unlock(&devlink_mutex);
6489}
6490EXPORT_SYMBOL_GPL(devlink_reload_disable);
6491
6492/**
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006493 * devlink_free - Free devlink instance resources
6494 *
6495 * @devlink: devlink
6496 */
6497void devlink_free(struct devlink *devlink)
6498{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006499 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01006500 mutex_destroy(&devlink->lock);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006501 WARN_ON(!list_empty(&devlink->trap_group_list));
6502 WARN_ON(!list_empty(&devlink->trap_list));
Parav Panditb904aad2019-02-08 15:15:00 -06006503 WARN_ON(!list_empty(&devlink->reporter_list));
6504 WARN_ON(!list_empty(&devlink->region_list));
6505 WARN_ON(!list_empty(&devlink->param_list));
6506 WARN_ON(!list_empty(&devlink->resource_list));
6507 WARN_ON(!list_empty(&devlink->dpipe_table_list));
6508 WARN_ON(!list_empty(&devlink->sb_list));
6509 WARN_ON(!list_empty(&devlink->port_list));
6510
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006511 kfree(devlink);
6512}
6513EXPORT_SYMBOL_GPL(devlink_free);
6514
Jiri Pirko136bf272019-05-23 10:43:35 +02006515static void devlink_port_type_warn(struct work_struct *work)
6516{
6517 WARN(true, "Type was not set for devlink port.");
6518}
6519
6520static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
6521{
6522 /* Ignore CPU and DSA flavours. */
6523 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
6524 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
6525}
6526
Ido Schimmel4c582232020-01-09 19:57:41 +02006527#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
Jiri Pirko136bf272019-05-23 10:43:35 +02006528
6529static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
6530{
6531 if (!devlink_port_type_should_warn(devlink_port))
6532 return;
6533 /* Schedule a work to WARN in case driver does not set port
6534 * type within timeout.
6535 */
6536 schedule_delayed_work(&devlink_port->type_warn_dw,
6537 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
6538}
6539
6540static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
6541{
6542 if (!devlink_port_type_should_warn(devlink_port))
6543 return;
6544 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
6545}
6546
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006547/**
6548 * devlink_port_register - Register devlink port
6549 *
6550 * @devlink: devlink
6551 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006552 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006553 *
6554 * Register devlink port with provided port index. User can use
6555 * any indexing, even hw-related one. devlink_port structure
6556 * is convenient to be embedded inside user driver private structure.
6557 * Note that the caller should take care of zeroing the devlink_port
6558 * structure.
6559 */
6560int devlink_port_register(struct devlink *devlink,
6561 struct devlink_port *devlink_port,
6562 unsigned int port_index)
6563{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006564 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006565 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006566 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006567 return -EEXIST;
6568 }
6569 devlink_port->devlink = devlink;
6570 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006571 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01006572 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006573 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306574 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006575 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02006576 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
6577 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006578 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6579 return 0;
6580}
6581EXPORT_SYMBOL_GPL(devlink_port_register);
6582
6583/**
6584 * devlink_port_unregister - Unregister devlink port
6585 *
6586 * @devlink_port: devlink port
6587 */
6588void devlink_port_unregister(struct devlink_port *devlink_port)
6589{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006590 struct devlink *devlink = devlink_port->devlink;
6591
Jiri Pirko136bf272019-05-23 10:43:35 +02006592 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006593 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006594 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006595 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006596 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006597}
6598EXPORT_SYMBOL_GPL(devlink_port_unregister);
6599
6600static void __devlink_port_type_set(struct devlink_port *devlink_port,
6601 enum devlink_port_type type,
6602 void *type_dev)
6603{
Jiri Pirko2b239e72019-03-24 11:14:36 +01006604 if (WARN_ON(!devlink_port->registered))
6605 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02006606 devlink_port_type_warn_cancel(devlink_port);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006607 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006608 devlink_port->type = type;
6609 devlink_port->type_dev = type_dev;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006610 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006611 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6612}
6613
6614/**
6615 * devlink_port_type_eth_set - Set port type to Ethernet
6616 *
6617 * @devlink_port: devlink port
6618 * @netdev: related netdevice
6619 */
6620void devlink_port_type_eth_set(struct devlink_port *devlink_port,
6621 struct net_device *netdev)
6622{
Jiri Pirko119c0b52019-04-03 14:24:27 +02006623 const struct net_device_ops *ops = netdev->netdev_ops;
6624
Jiri Pirko746364f2019-03-28 13:56:46 +01006625 /* If driver registers devlink port, it should set devlink port
6626 * attributes accordingly so the compat functions are called
6627 * and the original ops are not used.
6628 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02006629 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01006630 /* Some drivers use the same set of ndos for netdevs
6631 * that have devlink_port registered and also for
6632 * those who don't. Make sure that ndo_get_phys_port_name
6633 * returns -EOPNOTSUPP here in case it is defined.
6634 * Warn if not.
6635 */
Jiri Pirko746364f2019-03-28 13:56:46 +01006636 char name[IFNAMSIZ];
6637 int err;
6638
6639 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
6640 WARN_ON(err != -EOPNOTSUPP);
6641 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02006642 if (ops->ndo_get_port_parent_id) {
6643 /* Some drivers use the same set of ndos for netdevs
6644 * that have devlink_port registered and also for
6645 * those who don't. Make sure that ndo_get_port_parent_id
6646 * returns -EOPNOTSUPP here in case it is defined.
6647 * Warn if not.
6648 */
6649 struct netdev_phys_item_id ppid;
6650 int err;
6651
6652 err = ops->ndo_get_port_parent_id(netdev, &ppid);
6653 WARN_ON(err != -EOPNOTSUPP);
6654 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01006655 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006656}
6657EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
6658
6659/**
6660 * devlink_port_type_ib_set - Set port type to InfiniBand
6661 *
6662 * @devlink_port: devlink port
6663 * @ibdev: related IB device
6664 */
6665void devlink_port_type_ib_set(struct devlink_port *devlink_port,
6666 struct ib_device *ibdev)
6667{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006668 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006669}
6670EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
6671
6672/**
6673 * devlink_port_type_clear - Clear port type
6674 *
6675 * @devlink_port: devlink port
6676 */
6677void devlink_port_type_clear(struct devlink_port *devlink_port)
6678{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006679 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02006680 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006681}
6682EXPORT_SYMBOL_GPL(devlink_port_type_clear);
6683
Parav Pandit378ef012019-07-08 23:17:35 -05006684static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
6685 enum devlink_port_flavour flavour,
6686 const unsigned char *switch_id,
6687 unsigned char switch_id_len)
6688{
6689 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6690
6691 if (WARN_ON(devlink_port->registered))
6692 return -EEXIST;
6693 attrs->set = true;
6694 attrs->flavour = flavour;
6695 if (switch_id) {
6696 attrs->switch_port = true;
6697 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
6698 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
6699 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
6700 attrs->switch_id.id_len = switch_id_len;
6701 } else {
6702 attrs->switch_port = false;
6703 }
6704 return 0;
6705}
6706
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006707/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006708 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006709 *
6710 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02006711 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006712 * @port_number: number of the port that is facing user, for example
6713 * the front panel port number
6714 * @split: indicates if this is split port
6715 * @split_subport_number: if the port is split, this is the number
6716 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02006717 * @switch_id: if the port is part of switch, this is buffer with ID,
6718 * otwerwise this is NULL
6719 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006720 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006721void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02006722 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006723 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02006724 u32 split_subport_number,
6725 const unsigned char *switch_id,
6726 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006727{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006728 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05006729 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006730
Parav Pandit378ef012019-07-08 23:17:35 -05006731 ret = __devlink_port_attrs_set(devlink_port, flavour,
6732 switch_id, switch_id_len);
6733 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01006734 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006735 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05006736 attrs->phys.port_number = port_number;
6737 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006738}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006739EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006740
Parav Pandit98fd2d62019-07-08 23:17:37 -05006741/**
6742 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
6743 *
6744 * @devlink_port: devlink port
6745 * @pf: associated PF for the devlink port instance
6746 * @switch_id: if the port is part of switch, this is buffer with ID,
6747 * otherwise this is NULL
6748 * @switch_id_len: length of the switch_id buffer
6749 */
6750void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
6751 const unsigned char *switch_id,
6752 unsigned char switch_id_len, u16 pf)
6753{
6754 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6755 int ret;
6756
6757 ret = __devlink_port_attrs_set(devlink_port,
6758 DEVLINK_PORT_FLAVOUR_PCI_PF,
6759 switch_id, switch_id_len);
6760 if (ret)
6761 return;
6762
6763 attrs->pci_pf.pf = pf;
6764}
6765EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
6766
Parav Pandite41b6bf2019-07-08 23:17:38 -05006767/**
6768 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
6769 *
6770 * @devlink_port: devlink port
6771 * @pf: associated PF for the devlink port instance
6772 * @vf: associated VF of a PF for the devlink port instance
6773 * @switch_id: if the port is part of switch, this is buffer with ID,
6774 * otherwise this is NULL
6775 * @switch_id_len: length of the switch_id buffer
6776 */
6777void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
6778 const unsigned char *switch_id,
6779 unsigned char switch_id_len,
6780 u16 pf, u16 vf)
6781{
6782 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6783 int ret;
6784
6785 ret = __devlink_port_attrs_set(devlink_port,
6786 DEVLINK_PORT_FLAVOUR_PCI_VF,
6787 switch_id, switch_id_len);
6788 if (ret)
6789 return;
6790 attrs->pci_vf.pf = pf;
6791 attrs->pci_vf.vf = vf;
6792}
6793EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
6794
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006795static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
6796 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02006797{
6798 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6799 int n = 0;
6800
6801 if (!attrs->set)
6802 return -EOPNOTSUPP;
6803
6804 switch (attrs->flavour) {
6805 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
6806 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05006807 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006808 else
Parav Pandit378ef012019-07-08 23:17:35 -05006809 n = snprintf(name, len, "p%us%u",
6810 attrs->phys.port_number,
6811 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006812 break;
6813 case DEVLINK_PORT_FLAVOUR_CPU:
6814 case DEVLINK_PORT_FLAVOUR_DSA:
6815 /* As CPU and DSA ports do not have a netdevice associated
6816 * case should not ever happen.
6817 */
6818 WARN_ON(1);
6819 return -EINVAL;
Parav Pandit98fd2d62019-07-08 23:17:37 -05006820 case DEVLINK_PORT_FLAVOUR_PCI_PF:
6821 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
6822 break;
Parav Pandite41b6bf2019-07-08 23:17:38 -05006823 case DEVLINK_PORT_FLAVOUR_PCI_VF:
6824 n = snprintf(name, len, "pf%uvf%u",
6825 attrs->pci_vf.pf, attrs->pci_vf.vf);
6826 break;
Jiri Pirko08474c12018-05-18 09:29:02 +02006827 }
6828
6829 if (n >= len)
6830 return -EINVAL;
6831
6832 return 0;
6833}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006834
Jiri Pirkobf797472016-04-14 18:19:13 +02006835int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
6836 u32 size, u16 ingress_pools_count,
6837 u16 egress_pools_count, u16 ingress_tc_count,
6838 u16 egress_tc_count)
6839{
6840 struct devlink_sb *devlink_sb;
6841 int err = 0;
6842
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006843 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006844 if (devlink_sb_index_exists(devlink, sb_index)) {
6845 err = -EEXIST;
6846 goto unlock;
6847 }
6848
6849 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
6850 if (!devlink_sb) {
6851 err = -ENOMEM;
6852 goto unlock;
6853 }
6854 devlink_sb->index = sb_index;
6855 devlink_sb->size = size;
6856 devlink_sb->ingress_pools_count = ingress_pools_count;
6857 devlink_sb->egress_pools_count = egress_pools_count;
6858 devlink_sb->ingress_tc_count = ingress_tc_count;
6859 devlink_sb->egress_tc_count = egress_tc_count;
6860 list_add_tail(&devlink_sb->list, &devlink->sb_list);
6861unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006862 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006863 return err;
6864}
6865EXPORT_SYMBOL_GPL(devlink_sb_register);
6866
6867void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
6868{
6869 struct devlink_sb *devlink_sb;
6870
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006871 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006872 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
6873 WARN_ON(!devlink_sb);
6874 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006875 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006876 kfree(devlink_sb);
6877}
6878EXPORT_SYMBOL_GPL(devlink_sb_unregister);
6879
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006880/**
6881 * devlink_dpipe_headers_register - register dpipe headers
6882 *
6883 * @devlink: devlink
6884 * @dpipe_headers: dpipe header array
6885 *
6886 * Register the headers supported by hardware.
6887 */
6888int devlink_dpipe_headers_register(struct devlink *devlink,
6889 struct devlink_dpipe_headers *dpipe_headers)
6890{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006891 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006892 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006893 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006894 return 0;
6895}
6896EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
6897
6898/**
6899 * devlink_dpipe_headers_unregister - unregister dpipe headers
6900 *
6901 * @devlink: devlink
6902 *
6903 * Unregister the headers supported by hardware.
6904 */
6905void devlink_dpipe_headers_unregister(struct devlink *devlink)
6906{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006907 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006908 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006909 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006910}
6911EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
6912
6913/**
6914 * devlink_dpipe_table_counter_enabled - check if counter allocation
6915 * required
6916 * @devlink: devlink
6917 * @table_name: tables name
6918 *
6919 * Used by driver to check if counter allocation is required.
6920 * After counter allocation is turned on the table entries
6921 * are updated to include counter statistics.
6922 *
6923 * After that point on the driver must respect the counter
6924 * state so that each entry added to the table is added
6925 * with a counter.
6926 */
6927bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
6928 const char *table_name)
6929{
6930 struct devlink_dpipe_table *table;
6931 bool enabled;
6932
6933 rcu_read_lock();
6934 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6935 table_name);
6936 enabled = false;
6937 if (table)
6938 enabled = table->counters_enabled;
6939 rcu_read_unlock();
6940 return enabled;
6941}
6942EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
6943
6944/**
6945 * devlink_dpipe_table_register - register dpipe table
6946 *
6947 * @devlink: devlink
6948 * @table_name: table name
6949 * @table_ops: table ops
6950 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006951 * @counter_control_extern: external control for counters
6952 */
6953int devlink_dpipe_table_register(struct devlink *devlink,
6954 const char *table_name,
6955 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006956 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006957{
6958 struct devlink_dpipe_table *table;
6959
6960 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
6961 return -EEXIST;
6962
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006963 if (WARN_ON(!table_ops->size_get))
6964 return -EINVAL;
6965
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006966 table = kzalloc(sizeof(*table), GFP_KERNEL);
6967 if (!table)
6968 return -ENOMEM;
6969
6970 table->name = table_name;
6971 table->table_ops = table_ops;
6972 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006973 table->counter_control_extern = counter_control_extern;
6974
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006975 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006976 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006977 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006978 return 0;
6979}
6980EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
6981
6982/**
6983 * devlink_dpipe_table_unregister - unregister dpipe table
6984 *
6985 * @devlink: devlink
6986 * @table_name: table name
6987 */
6988void devlink_dpipe_table_unregister(struct devlink *devlink,
6989 const char *table_name)
6990{
6991 struct devlink_dpipe_table *table;
6992
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006993 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006994 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6995 table_name);
6996 if (!table)
6997 goto unlock;
6998 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006999 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007000 kfree_rcu(table, rcu);
7001 return;
7002unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01007003 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02007004}
7005EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
7006
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007007/**
7008 * devlink_resource_register - devlink resource register
7009 *
7010 * @devlink: devlink
7011 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007012 * @resource_size: resource's size
7013 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007014 * @parent_resource_id: resource's parent id
7015 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007016 */
7017int devlink_resource_register(struct devlink *devlink,
7018 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007019 u64 resource_size,
7020 u64 resource_id,
7021 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02007022 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007023{
7024 struct devlink_resource *resource;
7025 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07007026 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007027 int err = 0;
7028
David Ahern14530742018-03-20 19:31:14 -07007029 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
7030
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007031 mutex_lock(&devlink->lock);
7032 resource = devlink_resource_find(devlink, NULL, resource_id);
7033 if (resource) {
7034 err = -EINVAL;
7035 goto out;
7036 }
7037
7038 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
7039 if (!resource) {
7040 err = -ENOMEM;
7041 goto out;
7042 }
7043
7044 if (top_hierarchy) {
7045 resource_list = &devlink->resource_list;
7046 } else {
7047 struct devlink_resource *parent_resource;
7048
7049 parent_resource = devlink_resource_find(devlink, NULL,
7050 parent_resource_id);
7051 if (parent_resource) {
7052 resource_list = &parent_resource->resource_list;
7053 resource->parent = parent_resource;
7054 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00007055 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007056 err = -EINVAL;
7057 goto out;
7058 }
7059 }
7060
7061 resource->name = resource_name;
7062 resource->size = resource_size;
7063 resource->size_new = resource_size;
7064 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007065 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01007066 memcpy(&resource->size_params, size_params,
7067 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01007068 INIT_LIST_HEAD(&resource->resource_list);
7069 list_add_tail(&resource->list, resource_list);
7070out:
7071 mutex_unlock(&devlink->lock);
7072 return err;
7073}
7074EXPORT_SYMBOL_GPL(devlink_resource_register);
7075
7076/**
7077 * devlink_resources_unregister - free all resources
7078 *
7079 * @devlink: devlink
7080 * @resource: resource
7081 */
7082void devlink_resources_unregister(struct devlink *devlink,
7083 struct devlink_resource *resource)
7084{
7085 struct devlink_resource *tmp, *child_resource;
7086 struct list_head *resource_list;
7087
7088 if (resource)
7089 resource_list = &resource->resource_list;
7090 else
7091 resource_list = &devlink->resource_list;
7092
7093 if (!resource)
7094 mutex_lock(&devlink->lock);
7095
7096 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
7097 devlink_resources_unregister(devlink, child_resource);
7098 list_del(&child_resource->list);
7099 kfree(child_resource);
7100 }
7101
7102 if (!resource)
7103 mutex_unlock(&devlink->lock);
7104}
7105EXPORT_SYMBOL_GPL(devlink_resources_unregister);
7106
7107/**
7108 * devlink_resource_size_get - get and update size
7109 *
7110 * @devlink: devlink
7111 * @resource_id: the requested resource id
7112 * @p_resource_size: ptr to update
7113 */
7114int devlink_resource_size_get(struct devlink *devlink,
7115 u64 resource_id,
7116 u64 *p_resource_size)
7117{
7118 struct devlink_resource *resource;
7119 int err = 0;
7120
7121 mutex_lock(&devlink->lock);
7122 resource = devlink_resource_find(devlink, NULL, resource_id);
7123 if (!resource) {
7124 err = -EINVAL;
7125 goto out;
7126 }
7127 *p_resource_size = resource->size_new;
7128 resource->size = resource->size_new;
7129out:
7130 mutex_unlock(&devlink->lock);
7131 return err;
7132}
7133EXPORT_SYMBOL_GPL(devlink_resource_size_get);
7134
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01007135/**
7136 * devlink_dpipe_table_resource_set - set the resource id
7137 *
7138 * @devlink: devlink
7139 * @table_name: table name
7140 * @resource_id: resource id
7141 * @resource_units: number of resource's units consumed per table's entry
7142 */
7143int devlink_dpipe_table_resource_set(struct devlink *devlink,
7144 const char *table_name, u64 resource_id,
7145 u64 resource_units)
7146{
7147 struct devlink_dpipe_table *table;
7148 int err = 0;
7149
7150 mutex_lock(&devlink->lock);
7151 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
7152 table_name);
7153 if (!table) {
7154 err = -EINVAL;
7155 goto out;
7156 }
7157 table->resource_id = resource_id;
7158 table->resource_units = resource_units;
7159 table->resource_valid = true;
7160out:
7161 mutex_unlock(&devlink->lock);
7162 return err;
7163}
7164EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
7165
Jiri Pirkofc56be42018-04-05 22:13:21 +02007166/**
7167 * devlink_resource_occ_get_register - register occupancy getter
7168 *
7169 * @devlink: devlink
7170 * @resource_id: resource id
7171 * @occ_get: occupancy getter callback
7172 * @occ_get_priv: occupancy getter callback priv
7173 */
7174void devlink_resource_occ_get_register(struct devlink *devlink,
7175 u64 resource_id,
7176 devlink_resource_occ_get_t *occ_get,
7177 void *occ_get_priv)
7178{
7179 struct devlink_resource *resource;
7180
7181 mutex_lock(&devlink->lock);
7182 resource = devlink_resource_find(devlink, NULL, resource_id);
7183 if (WARN_ON(!resource))
7184 goto out;
7185 WARN_ON(resource->occ_get);
7186
7187 resource->occ_get = occ_get;
7188 resource->occ_get_priv = occ_get_priv;
7189out:
7190 mutex_unlock(&devlink->lock);
7191}
7192EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
7193
7194/**
7195 * devlink_resource_occ_get_unregister - unregister occupancy getter
7196 *
7197 * @devlink: devlink
7198 * @resource_id: resource id
7199 */
7200void devlink_resource_occ_get_unregister(struct devlink *devlink,
7201 u64 resource_id)
7202{
7203 struct devlink_resource *resource;
7204
7205 mutex_lock(&devlink->lock);
7206 resource = devlink_resource_find(devlink, NULL, resource_id);
7207 if (WARN_ON(!resource))
7208 goto out;
7209 WARN_ON(!resource->occ_get);
7210
7211 resource->occ_get = NULL;
7212 resource->occ_get_priv = NULL;
7213out:
7214 mutex_unlock(&devlink->lock);
7215}
7216EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
7217
Vasundhara Volam39e61602019-01-28 18:00:20 +05307218static int devlink_param_verify(const struct devlink_param *param)
7219{
7220 if (!param || !param->name || !param->supported_cmodes)
7221 return -EINVAL;
7222 if (param->generic)
7223 return devlink_param_generic_verify(param);
7224 else
7225 return devlink_param_driver_verify(param);
7226}
7227
7228static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307229 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307230 struct list_head *param_list,
7231 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307232 size_t params_count,
7233 enum devlink_command reg_cmd,
7234 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307235{
7236 const struct devlink_param *param = params;
7237 int i;
7238 int err;
7239
7240 mutex_lock(&devlink->lock);
7241 for (i = 0; i < params_count; i++, param++) {
7242 err = devlink_param_verify(param);
7243 if (err)
7244 goto rollback;
7245
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307246 err = devlink_param_register_one(devlink, port_index,
7247 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307248 if (err)
7249 goto rollback;
7250 }
7251
7252 mutex_unlock(&devlink->lock);
7253 return 0;
7254
7255rollback:
7256 if (!i)
7257 goto unlock;
7258 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307259 devlink_param_unregister_one(devlink, port_index, param_list,
7260 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307261unlock:
7262 mutex_unlock(&devlink->lock);
7263 return err;
7264}
7265
7266static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307267 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307268 struct list_head *param_list,
7269 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307270 size_t params_count,
7271 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307272{
7273 const struct devlink_param *param = params;
7274 int i;
7275
7276 mutex_lock(&devlink->lock);
7277 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307278 devlink_param_unregister_one(devlink, 0, param_list, param,
7279 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307280 mutex_unlock(&devlink->lock);
7281}
7282
Moshe Shemesheabaef12018-07-04 14:30:28 +03007283/**
7284 * devlink_params_register - register configuration parameters
7285 *
7286 * @devlink: devlink
7287 * @params: configuration parameters array
7288 * @params_count: number of parameters provided
7289 *
7290 * Register the configuration parameters supported by the driver.
7291 */
7292int devlink_params_register(struct devlink *devlink,
7293 const struct devlink_param *params,
7294 size_t params_count)
7295{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307296 return __devlink_params_register(devlink, 0, &devlink->param_list,
7297 params, params_count,
7298 DEVLINK_CMD_PARAM_NEW,
7299 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007300}
7301EXPORT_SYMBOL_GPL(devlink_params_register);
7302
7303/**
7304 * devlink_params_unregister - unregister configuration parameters
7305 * @devlink: devlink
7306 * @params: configuration parameters to unregister
7307 * @params_count: number of parameters provided
7308 */
7309void devlink_params_unregister(struct devlink *devlink,
7310 const struct devlink_param *params,
7311 size_t params_count)
7312{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307313 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
7314 params, params_count,
7315 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007316}
7317EXPORT_SYMBOL_GPL(devlink_params_unregister);
7318
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007319/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00007320 * devlink_params_publish - publish configuration parameters
7321 *
7322 * @devlink: devlink
7323 *
7324 * Publish previously registered configuration parameters.
7325 */
7326void devlink_params_publish(struct devlink *devlink)
7327{
7328 struct devlink_param_item *param_item;
7329
7330 list_for_each_entry(param_item, &devlink->param_list, list) {
7331 if (param_item->published)
7332 continue;
7333 param_item->published = true;
7334 devlink_param_notify(devlink, 0, param_item,
7335 DEVLINK_CMD_PARAM_NEW);
7336 }
7337}
7338EXPORT_SYMBOL_GPL(devlink_params_publish);
7339
7340/**
7341 * devlink_params_unpublish - unpublish configuration parameters
7342 *
7343 * @devlink: devlink
7344 *
7345 * Unpublish previously registered configuration parameters.
7346 */
7347void devlink_params_unpublish(struct devlink *devlink)
7348{
7349 struct devlink_param_item *param_item;
7350
7351 list_for_each_entry(param_item, &devlink->param_list, list) {
7352 if (!param_item->published)
7353 continue;
7354 param_item->published = false;
7355 devlink_param_notify(devlink, 0, param_item,
7356 DEVLINK_CMD_PARAM_DEL);
7357 }
7358}
7359EXPORT_SYMBOL_GPL(devlink_params_unpublish);
7360
7361/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05307362 * devlink_port_params_register - register port configuration parameters
7363 *
7364 * @devlink_port: devlink port
7365 * @params: configuration parameters array
7366 * @params_count: number of parameters provided
7367 *
7368 * Register the configuration parameters supported by the port.
7369 */
7370int devlink_port_params_register(struct devlink_port *devlink_port,
7371 const struct devlink_param *params,
7372 size_t params_count)
7373{
7374 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307375 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307376 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307377 params_count,
7378 DEVLINK_CMD_PORT_PARAM_NEW,
7379 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307380}
7381EXPORT_SYMBOL_GPL(devlink_port_params_register);
7382
7383/**
7384 * devlink_port_params_unregister - unregister port configuration
7385 * parameters
7386 *
7387 * @devlink_port: devlink port
7388 * @params: configuration parameters array
7389 * @params_count: number of parameters provided
7390 */
7391void devlink_port_params_unregister(struct devlink_port *devlink_port,
7392 const struct devlink_param *params,
7393 size_t params_count)
7394{
7395 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307396 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307397 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307398 params, params_count,
7399 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307400}
7401EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
7402
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307403static int
7404__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
7405 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007406{
7407 struct devlink_param_item *param_item;
7408
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307409 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007410 if (!param_item)
7411 return -EINVAL;
7412
7413 if (!param_item->driverinit_value_valid ||
7414 !devlink_param_cmode_is_supported(param_item->param,
7415 DEVLINK_PARAM_CMODE_DRIVERINIT))
7416 return -EOPNOTSUPP;
7417
Moshe Shemesh12765342018-10-10 16:09:26 +03007418 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7419 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
7420 else
7421 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007422
7423 return 0;
7424}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307425
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307426static int
7427__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307428 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307429 struct list_head *param_list, u32 param_id,
7430 union devlink_param_value init_val,
7431 enum devlink_command cmd)
7432{
7433 struct devlink_param_item *param_item;
7434
7435 param_item = devlink_param_find_by_id(param_list, param_id);
7436 if (!param_item)
7437 return -EINVAL;
7438
7439 if (!devlink_param_cmode_is_supported(param_item->param,
7440 DEVLINK_PARAM_CMODE_DRIVERINIT))
7441 return -EOPNOTSUPP;
7442
7443 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7444 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
7445 else
7446 param_item->driverinit_value = init_val;
7447 param_item->driverinit_value_valid = true;
7448
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307449 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307450 return 0;
7451}
7452
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307453/**
7454 * devlink_param_driverinit_value_get - get configuration parameter
7455 * value for driver initializing
7456 *
7457 * @devlink: devlink
7458 * @param_id: parameter ID
7459 * @init_val: value of parameter in driverinit configuration mode
7460 *
7461 * This function should be used by the driver to get driverinit
7462 * configuration for initialization after reload command.
7463 */
7464int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
7465 union devlink_param_value *init_val)
7466{
Jiri Pirko97691062019-09-12 10:49:45 +02007467 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307468 return -EOPNOTSUPP;
7469
7470 return __devlink_param_driverinit_value_get(&devlink->param_list,
7471 param_id, init_val);
7472}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007473EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
7474
7475/**
7476 * devlink_param_driverinit_value_set - set value of configuration
7477 * parameter for driverinit
7478 * configuration mode
7479 *
7480 * @devlink: devlink
7481 * @param_id: parameter ID
7482 * @init_val: value of parameter to set for driverinit configuration mode
7483 *
7484 * This function should be used by the driver to set driverinit
7485 * configuration mode default value.
7486 */
7487int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
7488 union devlink_param_value init_val)
7489{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307490 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307491 &devlink->param_list,
7492 param_id, init_val,
7493 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007494}
7495EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
7496
Moshe Shemeshea601e12018-07-04 14:30:32 +03007497/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307498 * devlink_port_param_driverinit_value_get - get configuration parameter
7499 * value for driver initializing
7500 *
7501 * @devlink_port: devlink_port
7502 * @param_id: parameter ID
7503 * @init_val: value of parameter in driverinit configuration mode
7504 *
7505 * This function should be used by the driver to get driverinit
7506 * configuration for initialization after reload command.
7507 */
7508int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
7509 u32 param_id,
7510 union devlink_param_value *init_val)
7511{
7512 struct devlink *devlink = devlink_port->devlink;
7513
Jiri Pirko97691062019-09-12 10:49:45 +02007514 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307515 return -EOPNOTSUPP;
7516
7517 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
7518 param_id, init_val);
7519}
7520EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
7521
7522/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307523 * devlink_port_param_driverinit_value_set - set value of configuration
7524 * parameter for driverinit
7525 * configuration mode
7526 *
7527 * @devlink_port: devlink_port
7528 * @param_id: parameter ID
7529 * @init_val: value of parameter to set for driverinit configuration mode
7530 *
7531 * This function should be used by the driver to set driverinit
7532 * configuration mode default value.
7533 */
7534int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
7535 u32 param_id,
7536 union devlink_param_value init_val)
7537{
7538 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307539 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307540 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307541 param_id, init_val,
7542 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307543}
7544EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
7545
7546/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03007547 * devlink_param_value_changed - notify devlink on a parameter's value
7548 * change. Should be called by the driver
7549 * right after the change.
7550 *
7551 * @devlink: devlink
7552 * @param_id: parameter ID
7553 *
7554 * This function should be used by the driver to notify devlink on value
7555 * change, excluding driverinit configuration mode.
7556 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03007557 */
7558void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
7559{
7560 struct devlink_param_item *param_item;
7561
7562 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
7563 WARN_ON(!param_item);
7564
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307565 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03007566}
7567EXPORT_SYMBOL_GPL(devlink_param_value_changed);
7568
Alex Veskerb16ebe92018-07-12 15:13:08 +03007569/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307570 * devlink_port_param_value_changed - notify devlink on a parameter's value
7571 * change. Should be called by the driver
7572 * right after the change.
7573 *
7574 * @devlink_port: devlink_port
7575 * @param_id: parameter ID
7576 *
7577 * This function should be used by the driver to notify devlink on value
7578 * change, excluding driverinit configuration mode.
7579 * For driverinit configuration mode driver should use the function
7580 * devlink_port_param_driverinit_value_set() instead.
7581 */
7582void devlink_port_param_value_changed(struct devlink_port *devlink_port,
7583 u32 param_id)
7584{
7585 struct devlink_param_item *param_item;
7586
7587 param_item = devlink_param_find_by_id(&devlink_port->param_list,
7588 param_id);
7589 WARN_ON(!param_item);
7590
7591 devlink_param_notify(devlink_port->devlink, devlink_port->index,
7592 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
7593}
7594EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
7595
7596/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03007597 * devlink_param_value_str_fill - Safely fill-up the string preventing
7598 * from overflow of the preallocated buffer
7599 *
7600 * @dst_val: destination devlink_param_value
7601 * @src: source buffer
7602 */
7603void devlink_param_value_str_fill(union devlink_param_value *dst_val,
7604 const char *src)
7605{
7606 size_t len;
7607
7608 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
7609 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
7610}
7611EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
7612
7613/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03007614 * devlink_region_create - create a new address region
7615 *
7616 * @devlink: devlink
7617 * @region_name: region name
7618 * @region_max_snapshots: Maximum supported number of snapshots for region
7619 * @region_size: size of region
7620 */
7621struct devlink_region *devlink_region_create(struct devlink *devlink,
7622 const char *region_name,
7623 u32 region_max_snapshots,
7624 u64 region_size)
7625{
7626 struct devlink_region *region;
7627 int err = 0;
7628
7629 mutex_lock(&devlink->lock);
7630
7631 if (devlink_region_get_by_name(devlink, region_name)) {
7632 err = -EEXIST;
7633 goto unlock;
7634 }
7635
7636 region = kzalloc(sizeof(*region), GFP_KERNEL);
7637 if (!region) {
7638 err = -ENOMEM;
7639 goto unlock;
7640 }
7641
7642 region->devlink = devlink;
7643 region->max_snapshots = region_max_snapshots;
7644 region->name = region_name;
7645 region->size = region_size;
7646 INIT_LIST_HEAD(&region->snapshot_list);
7647 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03007648 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007649
7650 mutex_unlock(&devlink->lock);
7651 return region;
7652
7653unlock:
7654 mutex_unlock(&devlink->lock);
7655 return ERR_PTR(err);
7656}
7657EXPORT_SYMBOL_GPL(devlink_region_create);
7658
7659/**
7660 * devlink_region_destroy - destroy address region
7661 *
7662 * @region: devlink region to destroy
7663 */
7664void devlink_region_destroy(struct devlink_region *region)
7665{
7666 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03007667 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03007668
7669 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03007670
7671 /* Free all snapshots of region */
7672 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
Jiri Pirko92b49822019-08-12 14:28:31 +02007673 devlink_region_snapshot_del(region, snapshot);
Alex Veskerd7e52722018-07-12 15:13:10 +03007674
Alex Veskerb16ebe92018-07-12 15:13:08 +03007675 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03007676
7677 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007678 mutex_unlock(&devlink->lock);
7679 kfree(region);
7680}
7681EXPORT_SYMBOL_GPL(devlink_region_destroy);
7682
Alex Veskerccadfa42018-07-12 15:13:09 +03007683/**
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007684 * devlink_region_snapshot_id_get - get snapshot ID
Alex Veskerccadfa42018-07-12 15:13:09 +03007685 *
7686 * This callback should be called when adding a new snapshot,
7687 * Driver should use the same id for multiple snapshots taken
7688 * on multiple regions at the same time/by the same trigger.
7689 *
7690 * @devlink: devlink
7691 */
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007692u32 devlink_region_snapshot_id_get(struct devlink *devlink)
Alex Veskerccadfa42018-07-12 15:13:09 +03007693{
7694 u32 id;
7695
7696 mutex_lock(&devlink->lock);
7697 id = ++devlink->snapshot_id;
7698 mutex_unlock(&devlink->lock);
7699
7700 return id;
7701}
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007702EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
Alex Veskerccadfa42018-07-12 15:13:09 +03007703
Alex Veskerd7e52722018-07-12 15:13:10 +03007704/**
7705 * devlink_region_snapshot_create - create a new snapshot
7706 * This will add a new snapshot of a region. The snapshot
7707 * will be stored on the region struct and can be accessed
7708 * from devlink. This is useful for future analyses of snapshots.
7709 * Multiple snapshots can be created on a region.
7710 * The @snapshot_id should be obtained using the getter function.
7711 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007712 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03007713 * @data: snapshot data
7714 * @snapshot_id: snapshot id to be created
7715 * @data_destructor: pointer to destructor function to free data
7716 */
Jiri Pirko3a5e5232019-08-09 15:27:15 +02007717int devlink_region_snapshot_create(struct devlink_region *region,
Alex Veskerd7e52722018-07-12 15:13:10 +03007718 u8 *data, u32 snapshot_id,
7719 devlink_snapshot_data_dest_t *data_destructor)
7720{
7721 struct devlink *devlink = region->devlink;
7722 struct devlink_snapshot *snapshot;
7723 int err;
7724
7725 mutex_lock(&devlink->lock);
7726
7727 /* check if region can hold one more snapshot */
7728 if (region->cur_snapshots == region->max_snapshots) {
7729 err = -ENOMEM;
7730 goto unlock;
7731 }
7732
7733 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
7734 err = -EEXIST;
7735 goto unlock;
7736 }
7737
7738 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
7739 if (!snapshot) {
7740 err = -ENOMEM;
7741 goto unlock;
7742 }
7743
7744 snapshot->id = snapshot_id;
7745 snapshot->region = region;
7746 snapshot->data = data;
Alex Veskerd7e52722018-07-12 15:13:10 +03007747 snapshot->data_destructor = data_destructor;
7748
7749 list_add_tail(&snapshot->list, &region->snapshot_list);
7750
7751 region->cur_snapshots++;
7752
Alex Vesker866319b2018-07-12 15:13:13 +03007753 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03007754 mutex_unlock(&devlink->lock);
7755 return 0;
7756
7757unlock:
7758 mutex_unlock(&devlink->lock);
7759 return err;
7760}
7761EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
7762
Ido Schimmel0f420b62019-08-17 16:28:17 +03007763#define DEVLINK_TRAP(_id, _type) \
7764 { \
7765 .type = DEVLINK_TRAP_TYPE_##_type, \
7766 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
7767 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
7768 }
7769
7770static const struct devlink_trap devlink_trap_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007771 DEVLINK_TRAP(SMAC_MC, DROP),
7772 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
7773 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
7774 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
7775 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
7776 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
7777 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
7778 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
7779 DEVLINK_TRAP(TAIL_DROP, DROP),
Amit Cohen6896cc42019-11-07 18:42:09 +02007780 DEVLINK_TRAP(NON_IP_PACKET, DROP),
7781 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
7782 DEVLINK_TRAP(DIP_LB, DROP),
7783 DEVLINK_TRAP(SIP_MC, DROP),
7784 DEVLINK_TRAP(SIP_LB, DROP),
7785 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
7786 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
7787 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
7788 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
Amit Cohen3b063ae2019-11-07 18:42:14 +02007789 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
7790 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
7791 DEVLINK_TRAP(RPF, EXCEPTION),
7792 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
7793 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
7794 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
Amit Cohen95f0ead2020-01-19 15:00:48 +02007795 DEVLINK_TRAP(NON_ROUTABLE, DROP),
Amit Cohen13c056e2020-01-19 15:00:54 +02007796 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
Amit Cohenc3cae492020-01-19 15:00:58 +02007797 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
Jiri Pirkoecd942a2020-02-24 08:35:47 +01007798 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
7799 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007800};
7801
7802#define DEVLINK_TRAP_GROUP(_id) \
7803 { \
7804 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
7805 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
7806 }
7807
7808static const struct devlink_trap_group devlink_trap_group_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007809 DEVLINK_TRAP_GROUP(L2_DROPS),
7810 DEVLINK_TRAP_GROUP(L3_DROPS),
7811 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
Amit Cohen13c056e2020-01-19 15:00:54 +02007812 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
Jiri Pirkoecd942a2020-02-24 08:35:47 +01007813 DEVLINK_TRAP_GROUP(ACL_DROPS),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007814};
7815
7816static int devlink_trap_generic_verify(const struct devlink_trap *trap)
7817{
7818 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
7819 return -EINVAL;
7820
7821 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
7822 return -EINVAL;
7823
7824 if (trap->type != devlink_trap_generic[trap->id].type)
7825 return -EINVAL;
7826
7827 return 0;
7828}
7829
7830static int devlink_trap_driver_verify(const struct devlink_trap *trap)
7831{
7832 int i;
7833
7834 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
7835 return -EINVAL;
7836
7837 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
7838 if (!strcmp(trap->name, devlink_trap_generic[i].name))
7839 return -EEXIST;
7840 }
7841
7842 return 0;
7843}
7844
7845static int devlink_trap_verify(const struct devlink_trap *trap)
7846{
7847 if (!trap || !trap->name || !trap->group.name)
7848 return -EINVAL;
7849
7850 if (trap->generic)
7851 return devlink_trap_generic_verify(trap);
7852 else
7853 return devlink_trap_driver_verify(trap);
7854}
7855
7856static int
7857devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
7858{
7859 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7860 return -EINVAL;
7861
7862 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
7863 return -EINVAL;
7864
7865 return 0;
7866}
7867
7868static int
7869devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
7870{
7871 int i;
7872
7873 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7874 return -EINVAL;
7875
7876 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
7877 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
7878 return -EEXIST;
7879 }
7880
7881 return 0;
7882}
7883
7884static int devlink_trap_group_verify(const struct devlink_trap_group *group)
7885{
7886 if (group->generic)
7887 return devlink_trap_group_generic_verify(group);
7888 else
7889 return devlink_trap_group_driver_verify(group);
7890}
7891
7892static void
7893devlink_trap_group_notify(struct devlink *devlink,
7894 const struct devlink_trap_group_item *group_item,
7895 enum devlink_command cmd)
7896{
7897 struct sk_buff *msg;
7898 int err;
7899
7900 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
7901 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
7902
7903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7904 if (!msg)
7905 return;
7906
7907 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
7908 0);
7909 if (err) {
7910 nlmsg_free(msg);
7911 return;
7912 }
7913
7914 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7915 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7916}
7917
7918static struct devlink_trap_group_item *
7919devlink_trap_group_item_create(struct devlink *devlink,
7920 const struct devlink_trap_group *group)
7921{
7922 struct devlink_trap_group_item *group_item;
7923 int err;
7924
7925 err = devlink_trap_group_verify(group);
7926 if (err)
7927 return ERR_PTR(err);
7928
7929 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
7930 if (!group_item)
7931 return ERR_PTR(-ENOMEM);
7932
7933 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7934 if (!group_item->stats) {
7935 err = -ENOMEM;
7936 goto err_stats_alloc;
7937 }
7938
7939 group_item->group = group;
7940 refcount_set(&group_item->refcount, 1);
7941
7942 if (devlink->ops->trap_group_init) {
7943 err = devlink->ops->trap_group_init(devlink, group);
7944 if (err)
7945 goto err_group_init;
7946 }
7947
7948 list_add_tail(&group_item->list, &devlink->trap_group_list);
7949 devlink_trap_group_notify(devlink, group_item,
7950 DEVLINK_CMD_TRAP_GROUP_NEW);
7951
7952 return group_item;
7953
7954err_group_init:
7955 free_percpu(group_item->stats);
7956err_stats_alloc:
7957 kfree(group_item);
7958 return ERR_PTR(err);
7959}
7960
7961static void
7962devlink_trap_group_item_destroy(struct devlink *devlink,
7963 struct devlink_trap_group_item *group_item)
7964{
7965 devlink_trap_group_notify(devlink, group_item,
7966 DEVLINK_CMD_TRAP_GROUP_DEL);
7967 list_del(&group_item->list);
7968 free_percpu(group_item->stats);
7969 kfree(group_item);
7970}
7971
7972static struct devlink_trap_group_item *
7973devlink_trap_group_item_get(struct devlink *devlink,
7974 const struct devlink_trap_group *group)
7975{
7976 struct devlink_trap_group_item *group_item;
7977
7978 group_item = devlink_trap_group_item_lookup(devlink, group->name);
7979 if (group_item) {
7980 refcount_inc(&group_item->refcount);
7981 return group_item;
7982 }
7983
7984 return devlink_trap_group_item_create(devlink, group);
7985}
7986
7987static void
7988devlink_trap_group_item_put(struct devlink *devlink,
7989 struct devlink_trap_group_item *group_item)
7990{
7991 if (!refcount_dec_and_test(&group_item->refcount))
7992 return;
7993
7994 devlink_trap_group_item_destroy(devlink, group_item);
7995}
7996
7997static int
7998devlink_trap_item_group_link(struct devlink *devlink,
7999 struct devlink_trap_item *trap_item)
8000{
8001 struct devlink_trap_group_item *group_item;
8002
8003 group_item = devlink_trap_group_item_get(devlink,
8004 &trap_item->trap->group);
8005 if (IS_ERR(group_item))
8006 return PTR_ERR(group_item);
8007
8008 trap_item->group_item = group_item;
8009
8010 return 0;
8011}
8012
8013static void
8014devlink_trap_item_group_unlink(struct devlink *devlink,
8015 struct devlink_trap_item *trap_item)
8016{
8017 devlink_trap_group_item_put(devlink, trap_item->group_item);
8018}
8019
8020static void devlink_trap_notify(struct devlink *devlink,
8021 const struct devlink_trap_item *trap_item,
8022 enum devlink_command cmd)
8023{
8024 struct sk_buff *msg;
8025 int err;
8026
8027 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
8028 cmd != DEVLINK_CMD_TRAP_DEL);
8029
8030 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8031 if (!msg)
8032 return;
8033
8034 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
8035 if (err) {
8036 nlmsg_free(msg);
8037 return;
8038 }
8039
8040 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
8041 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
8042}
8043
8044static int
8045devlink_trap_register(struct devlink *devlink,
8046 const struct devlink_trap *trap, void *priv)
8047{
8048 struct devlink_trap_item *trap_item;
8049 int err;
8050
8051 if (devlink_trap_item_lookup(devlink, trap->name))
8052 return -EEXIST;
8053
8054 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
8055 if (!trap_item)
8056 return -ENOMEM;
8057
8058 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
8059 if (!trap_item->stats) {
8060 err = -ENOMEM;
8061 goto err_stats_alloc;
8062 }
8063
8064 trap_item->trap = trap;
8065 trap_item->action = trap->init_action;
8066 trap_item->priv = priv;
8067
8068 err = devlink_trap_item_group_link(devlink, trap_item);
8069 if (err)
8070 goto err_group_link;
8071
8072 err = devlink->ops->trap_init(devlink, trap, trap_item);
8073 if (err)
8074 goto err_trap_init;
8075
8076 list_add_tail(&trap_item->list, &devlink->trap_list);
8077 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
8078
8079 return 0;
8080
8081err_trap_init:
8082 devlink_trap_item_group_unlink(devlink, trap_item);
8083err_group_link:
8084 free_percpu(trap_item->stats);
8085err_stats_alloc:
8086 kfree(trap_item);
8087 return err;
8088}
8089
8090static void devlink_trap_unregister(struct devlink *devlink,
8091 const struct devlink_trap *trap)
8092{
8093 struct devlink_trap_item *trap_item;
8094
8095 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8096 if (WARN_ON_ONCE(!trap_item))
8097 return;
8098
8099 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
8100 list_del(&trap_item->list);
8101 if (devlink->ops->trap_fini)
8102 devlink->ops->trap_fini(devlink, trap, trap_item);
8103 devlink_trap_item_group_unlink(devlink, trap_item);
8104 free_percpu(trap_item->stats);
8105 kfree(trap_item);
8106}
8107
8108static void devlink_trap_disable(struct devlink *devlink,
8109 const struct devlink_trap *trap)
8110{
8111 struct devlink_trap_item *trap_item;
8112
8113 trap_item = devlink_trap_item_lookup(devlink, trap->name);
8114 if (WARN_ON_ONCE(!trap_item))
8115 return;
8116
8117 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP);
8118 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
8119}
8120
8121/**
8122 * devlink_traps_register - Register packet traps with devlink.
8123 * @devlink: devlink.
8124 * @traps: Packet traps.
8125 * @traps_count: Count of provided packet traps.
8126 * @priv: Driver private information.
8127 *
8128 * Return: Non-zero value on failure.
8129 */
8130int devlink_traps_register(struct devlink *devlink,
8131 const struct devlink_trap *traps,
8132 size_t traps_count, void *priv)
8133{
8134 int i, err;
8135
8136 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
8137 return -EINVAL;
8138
8139 mutex_lock(&devlink->lock);
8140 for (i = 0; i < traps_count; i++) {
8141 const struct devlink_trap *trap = &traps[i];
8142
8143 err = devlink_trap_verify(trap);
8144 if (err)
8145 goto err_trap_verify;
8146
8147 err = devlink_trap_register(devlink, trap, priv);
8148 if (err)
8149 goto err_trap_register;
8150 }
8151 mutex_unlock(&devlink->lock);
8152
8153 return 0;
8154
8155err_trap_register:
8156err_trap_verify:
8157 for (i--; i >= 0; i--)
8158 devlink_trap_unregister(devlink, &traps[i]);
8159 mutex_unlock(&devlink->lock);
8160 return err;
8161}
8162EXPORT_SYMBOL_GPL(devlink_traps_register);
8163
8164/**
8165 * devlink_traps_unregister - Unregister packet traps from devlink.
8166 * @devlink: devlink.
8167 * @traps: Packet traps.
8168 * @traps_count: Count of provided packet traps.
8169 */
8170void devlink_traps_unregister(struct devlink *devlink,
8171 const struct devlink_trap *traps,
8172 size_t traps_count)
8173{
8174 int i;
8175
8176 mutex_lock(&devlink->lock);
8177 /* Make sure we do not have any packets in-flight while unregistering
8178 * traps by disabling all of them and waiting for a grace period.
8179 */
8180 for (i = traps_count - 1; i >= 0; i--)
8181 devlink_trap_disable(devlink, &traps[i]);
8182 synchronize_rcu();
8183 for (i = traps_count - 1; i >= 0; i--)
8184 devlink_trap_unregister(devlink, &traps[i]);
8185 mutex_unlock(&devlink->lock);
8186}
8187EXPORT_SYMBOL_GPL(devlink_traps_unregister);
8188
8189static void
8190devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
8191 size_t skb_len)
8192{
8193 struct devlink_stats *stats;
8194
8195 stats = this_cpu_ptr(trap_stats);
8196 u64_stats_update_begin(&stats->syncp);
8197 stats->rx_bytes += skb_len;
8198 stats->rx_packets++;
8199 u64_stats_update_end(&stats->syncp);
8200}
8201
8202static void
8203devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata,
8204 const struct devlink_trap_item *trap_item,
8205 struct devlink_port *in_devlink_port)
8206{
8207 struct devlink_trap_group_item *group_item = trap_item->group_item;
8208
8209 hw_metadata->trap_group_name = group_item->group->name;
8210 hw_metadata->trap_name = trap_item->trap->name;
8211
8212 spin_lock(&in_devlink_port->type_lock);
8213 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
8214 hw_metadata->input_dev = in_devlink_port->type_dev;
8215 spin_unlock(&in_devlink_port->type_lock);
8216}
8217
8218/**
8219 * devlink_trap_report - Report trapped packet to drop monitor.
8220 * @devlink: devlink.
8221 * @skb: Trapped packet.
8222 * @trap_ctx: Trap context.
8223 * @in_devlink_port: Input devlink port.
8224 */
8225void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
8226 void *trap_ctx, struct devlink_port *in_devlink_port)
8227{
8228 struct devlink_trap_item *trap_item = trap_ctx;
8229 struct net_dm_hw_metadata hw_metadata = {};
8230
8231 devlink_trap_stats_update(trap_item->stats, skb->len);
8232 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
8233
8234 devlink_trap_report_metadata_fill(&hw_metadata, trap_item,
8235 in_devlink_port);
8236 net_dm_hw_report(skb, &hw_metadata);
8237}
8238EXPORT_SYMBOL_GPL(devlink_trap_report);
8239
8240/**
8241 * devlink_trap_ctx_priv - Trap context to driver private information.
8242 * @trap_ctx: Trap context.
8243 *
8244 * Return: Driver private information passed during registration.
8245 */
8246void *devlink_trap_ctx_priv(void *trap_ctx)
8247{
8248 struct devlink_trap_item *trap_item = trap_ctx;
8249
8250 return trap_item->priv;
8251}
8252EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
8253
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008254static void __devlink_compat_running_version(struct devlink *devlink,
8255 char *buf, size_t len)
8256{
8257 const struct nlattr *nlattr;
8258 struct devlink_info_req req;
8259 struct sk_buff *msg;
8260 int rem, err;
8261
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8263 if (!msg)
8264 return;
8265
8266 req.msg = msg;
8267 err = devlink->ops->info_get(devlink, &req, NULL);
8268 if (err)
8269 goto free_msg;
8270
8271 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
8272 const struct nlattr *kv;
8273 int rem_kv;
8274
8275 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
8276 continue;
8277
8278 nla_for_each_nested(kv, nlattr, rem_kv) {
8279 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
8280 continue;
8281
8282 strlcat(buf, nla_data(kv), len);
8283 strlcat(buf, " ", len);
8284 }
8285 }
8286free_msg:
8287 nlmsg_free(msg);
8288}
8289
8290void devlink_compat_running_version(struct net_device *dev,
8291 char *buf, size_t len)
8292{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008293 struct devlink *devlink;
8294
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008295 dev_hold(dev);
8296 rtnl_unlock();
8297
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008298 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08008299 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008300 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008301
8302 mutex_lock(&devlink->lock);
8303 __devlink_compat_running_version(devlink, buf, len);
8304 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008305
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008306out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008307 rtnl_lock();
8308 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008309}
8310
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008311int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
8312{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008313 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008314 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008315
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008316 dev_hold(dev);
8317 rtnl_unlock();
8318
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008319 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008320 if (!devlink || !devlink->ops->flash_update) {
8321 ret = -EOPNOTSUPP;
8322 goto out;
8323 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008324
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008325 mutex_lock(&devlink->lock);
8326 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
8327 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008328
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008329out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008330 rtnl_lock();
8331 dev_put(dev);
8332
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008333 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008334}
8335
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008336int devlink_compat_phys_port_name_get(struct net_device *dev,
8337 char *name, size_t len)
8338{
8339 struct devlink_port *devlink_port;
8340
8341 /* RTNL mutex is held here which ensures that devlink_port
8342 * instance cannot disappear in the middle. No need to take
8343 * any devlink lock as only permanent values are accessed.
8344 */
8345 ASSERT_RTNL();
8346
8347 devlink_port = netdev_to_devlink_port(dev);
8348 if (!devlink_port)
8349 return -EOPNOTSUPP;
8350
8351 return __devlink_port_phys_port_name_get(devlink_port, name, len);
8352}
8353
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008354int devlink_compat_switch_id_get(struct net_device *dev,
8355 struct netdev_phys_item_id *ppid)
8356{
8357 struct devlink_port *devlink_port;
8358
Vlad Buslov043b8412019-08-12 20:02:02 +03008359 /* Caller must hold RTNL mutex or reference to dev, which ensures that
8360 * devlink_port instance cannot disappear in the middle. No need to take
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008361 * any devlink lock as only permanent values are accessed.
8362 */
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008363 devlink_port = netdev_to_devlink_port(dev);
8364 if (!devlink_port || !devlink_port->attrs.switch_port)
8365 return -EOPNOTSUPP;
8366
8367 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
8368
8369 return 0;
8370}
8371
Jiri Pirko070c63f2019-10-03 11:49:39 +02008372static void __net_exit devlink_pernet_pre_exit(struct net *net)
8373{
8374 struct devlink *devlink;
8375 int err;
8376
8377 /* In case network namespace is getting destroyed, reload
8378 * all devlink instances from this namespace into init_net.
8379 */
8380 mutex_lock(&devlink_mutex);
8381 list_for_each_entry(devlink, &devlink_list, list) {
8382 if (net_eq(devlink_net(devlink), net)) {
8383 if (WARN_ON(!devlink_reload_supported(devlink)))
8384 continue;
8385 err = devlink_reload(devlink, &init_net, NULL);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01008386 if (err && err != -EOPNOTSUPP)
Jiri Pirko070c63f2019-10-03 11:49:39 +02008387 pr_warn("Failed to reload devlink instance into init_net\n");
8388 }
8389 }
8390 mutex_unlock(&devlink_mutex);
8391}
8392
8393static struct pernet_operations devlink_pernet_ops __net_initdata = {
8394 .pre_exit = devlink_pernet_pre_exit,
8395};
8396
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008397static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008398{
Jiri Pirko070c63f2019-10-03 11:49:39 +02008399 int err;
8400
8401 err = genl_register_family(&devlink_nl_family);
8402 if (err)
8403 goto out;
8404 err = register_pernet_subsys(&devlink_pernet_ops);
8405
8406out:
8407 WARN_ON(err);
8408 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008409}
8410
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008411subsys_initcall(devlink_init);