blob: f76219bf0c21d778fe0a5f64d7a1b739bca922ef [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
3989 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3990 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3991 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003992 if (!hdr) {
3993 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003994 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003995 }
Alex Vesker4e547952018-07-12 15:13:14 +03003996
3997 err = devlink_nl_put_handle(skb, devlink);
3998 if (err)
3999 goto nla_put_failure;
4000
4001 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4002 if (err)
4003 goto nla_put_failure;
4004
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004005 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004006 if (!chunks_attr) {
4007 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03004008 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004009 }
Alex Vesker4e547952018-07-12 15:13:14 +03004010
4011 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4012 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4013 if (!start_offset)
4014 start_offset =
4015 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4016
4017 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4018 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4019 dump = false;
4020 }
4021
4022 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4023 region, attrs,
4024 start_offset,
4025 end_offset, dump,
4026 &ret_offset);
4027
4028 if (err && err != -EMSGSIZE)
4029 goto nla_put_failure;
4030
4031 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06004032 if (ret_offset == start_offset) {
4033 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03004034 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06004035 }
Alex Vesker4e547952018-07-12 15:13:14 +03004036
4037 *((u64 *)&cb->args[0]) = ret_offset;
4038
4039 nla_nest_end(skb, chunks_attr);
4040 genlmsg_end(skb, hdr);
4041 mutex_unlock(&devlink->lock);
4042 mutex_unlock(&devlink_mutex);
4043
4044 return skb->len;
4045
4046nla_put_failure:
4047 genlmsg_cancel(skb, hdr);
4048out_unlock:
4049 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06004050out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03004051 mutex_unlock(&devlink_mutex);
Parav Panditfdd41ec2019-02-12 14:23:58 -06004052 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03004053}
4054
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004055struct devlink_info_req {
4056 struct sk_buff *msg;
4057};
4058
4059int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
4060{
4061 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
4062}
4063EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
4064
4065int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
4066{
4067 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
4068}
4069EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
4070
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004071static int devlink_info_version_put(struct devlink_info_req *req, int attr,
4072 const char *version_name,
4073 const char *version_value)
4074{
4075 struct nlattr *nest;
4076 int err;
4077
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004078 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08004079 if (!nest)
4080 return -EMSGSIZE;
4081
4082 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
4083 version_name);
4084 if (err)
4085 goto nla_put_failure;
4086
4087 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
4088 version_value);
4089 if (err)
4090 goto nla_put_failure;
4091
4092 nla_nest_end(req->msg, nest);
4093
4094 return 0;
4095
4096nla_put_failure:
4097 nla_nest_cancel(req->msg, nest);
4098 return err;
4099}
4100
4101int devlink_info_version_fixed_put(struct devlink_info_req *req,
4102 const char *version_name,
4103 const char *version_value)
4104{
4105 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
4106 version_name, version_value);
4107}
4108EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
4109
4110int devlink_info_version_stored_put(struct devlink_info_req *req,
4111 const char *version_name,
4112 const char *version_value)
4113{
4114 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
4115 version_name, version_value);
4116}
4117EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
4118
4119int devlink_info_version_running_put(struct devlink_info_req *req,
4120 const char *version_name,
4121 const char *version_value)
4122{
4123 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4124 version_name, version_value);
4125}
4126EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4127
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004128static int
4129devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4130 enum devlink_command cmd, u32 portid,
4131 u32 seq, int flags, struct netlink_ext_ack *extack)
4132{
4133 struct devlink_info_req req;
4134 void *hdr;
4135 int err;
4136
4137 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4138 if (!hdr)
4139 return -EMSGSIZE;
4140
4141 err = -EMSGSIZE;
4142 if (devlink_nl_put_handle(msg, devlink))
4143 goto err_cancel_msg;
4144
4145 req.msg = msg;
4146 err = devlink->ops->info_get(devlink, &req, extack);
4147 if (err)
4148 goto err_cancel_msg;
4149
4150 genlmsg_end(msg, hdr);
4151 return 0;
4152
4153err_cancel_msg:
4154 genlmsg_cancel(msg, hdr);
4155 return err;
4156}
4157
4158static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4159 struct genl_info *info)
4160{
4161 struct devlink *devlink = info->user_ptr[0];
4162 struct sk_buff *msg;
4163 int err;
4164
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08004165 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004166 return -EOPNOTSUPP;
4167
4168 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4169 if (!msg)
4170 return -ENOMEM;
4171
4172 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4173 info->snd_portid, info->snd_seq, 0,
4174 info->extack);
4175 if (err) {
4176 nlmsg_free(msg);
4177 return err;
4178 }
4179
4180 return genlmsg_reply(msg, info);
4181}
4182
4183static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4184 struct netlink_callback *cb)
4185{
4186 struct devlink *devlink;
4187 int start = cb->args[0];
4188 int idx = 0;
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004189 int err = 0;
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004190
4191 mutex_lock(&devlink_mutex);
4192 list_for_each_entry(devlink, &devlink_list, list) {
4193 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4194 continue;
4195 if (idx < start) {
4196 idx++;
4197 continue;
4198 }
4199
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004200 if (!devlink->ops->info_get) {
4201 idx++;
4202 continue;
4203 }
4204
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004205 mutex_lock(&devlink->lock);
4206 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4207 NETLINK_CB(cb->skb).portid,
4208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4209 cb->extack);
4210 mutex_unlock(&devlink->lock);
Vasundhara Volam93c2fcb2019-09-30 11:52:21 +05304211 if (err && err != -EOPNOTSUPP)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004212 break;
4213 idx++;
4214 }
4215 mutex_unlock(&devlink_mutex);
4216
Jiri Pirkoc62c2cf2019-10-04 11:50:12 +02004217 if (err != -EMSGSIZE)
4218 return err;
4219
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004220 cb->args[0] = idx;
4221 return msg->len;
4222}
4223
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004224struct devlink_fmsg_item {
4225 struct list_head list;
4226 int attrtype;
4227 u8 nla_type;
4228 u16 len;
4229 int value[0];
4230};
4231
4232struct devlink_fmsg {
4233 struct list_head item_list;
4234};
4235
4236static struct devlink_fmsg *devlink_fmsg_alloc(void)
4237{
4238 struct devlink_fmsg *fmsg;
4239
4240 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4241 if (!fmsg)
4242 return NULL;
4243
4244 INIT_LIST_HEAD(&fmsg->item_list);
4245
4246 return fmsg;
4247}
4248
4249static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4250{
4251 struct devlink_fmsg_item *item, *tmp;
4252
4253 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4254 list_del(&item->list);
4255 kfree(item);
4256 }
4257 kfree(fmsg);
4258}
4259
4260static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4261 int attrtype)
4262{
4263 struct devlink_fmsg_item *item;
4264
4265 item = kzalloc(sizeof(*item), GFP_KERNEL);
4266 if (!item)
4267 return -ENOMEM;
4268
4269 item->attrtype = attrtype;
4270 list_add_tail(&item->list, &fmsg->item_list);
4271
4272 return 0;
4273}
4274
4275int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4276{
4277 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4278}
4279EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4280
4281static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4282{
4283 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4284}
4285
4286int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4287{
4288 return devlink_fmsg_nest_end(fmsg);
4289}
4290EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4291
4292#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4293
4294static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4295{
4296 struct devlink_fmsg_item *item;
4297
4298 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4299 return -EMSGSIZE;
4300
4301 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4302 if (!item)
4303 return -ENOMEM;
4304
4305 item->nla_type = NLA_NUL_STRING;
4306 item->len = strlen(name) + 1;
4307 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4308 memcpy(&item->value, name, item->len);
4309 list_add_tail(&item->list, &fmsg->item_list);
4310
4311 return 0;
4312}
4313
4314int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4315{
4316 int err;
4317
4318 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4319 if (err)
4320 return err;
4321
4322 err = devlink_fmsg_put_name(fmsg, name);
4323 if (err)
4324 return err;
4325
4326 return 0;
4327}
4328EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4329
4330int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4331{
4332 return devlink_fmsg_nest_end(fmsg);
4333}
4334EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4335
4336int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4337 const char *name)
4338{
4339 int err;
4340
4341 err = devlink_fmsg_pair_nest_start(fmsg, name);
4342 if (err)
4343 return err;
4344
4345 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4346 if (err)
4347 return err;
4348
4349 return 0;
4350}
4351EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4352
4353int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4354{
4355 int err;
4356
4357 err = devlink_fmsg_nest_end(fmsg);
4358 if (err)
4359 return err;
4360
4361 err = devlink_fmsg_nest_end(fmsg);
4362 if (err)
4363 return err;
4364
4365 return 0;
4366}
4367EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4368
4369static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4370 const void *value, u16 value_len,
4371 u8 value_nla_type)
4372{
4373 struct devlink_fmsg_item *item;
4374
4375 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4376 return -EMSGSIZE;
4377
4378 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4379 if (!item)
4380 return -ENOMEM;
4381
4382 item->nla_type = value_nla_type;
4383 item->len = value_len;
4384 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4385 memcpy(&item->value, value, item->len);
4386 list_add_tail(&item->list, &fmsg->item_list);
4387
4388 return 0;
4389}
4390
4391int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4392{
4393 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4394}
4395EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4396
4397int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4398{
4399 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4400}
4401EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4402
4403int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4404{
4405 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4406}
4407EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4408
4409int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4410{
4411 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4412}
4413EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4414
4415int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4416{
4417 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4418 NLA_NUL_STRING);
4419}
4420EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4421
Aya Levine2cde862019-11-12 14:07:49 +02004422static int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4423 u16 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004424{
4425 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4426}
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004427
4428int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4429 bool value)
4430{
4431 int err;
4432
4433 err = devlink_fmsg_pair_nest_start(fmsg, name);
4434 if (err)
4435 return err;
4436
4437 err = devlink_fmsg_bool_put(fmsg, value);
4438 if (err)
4439 return err;
4440
4441 err = devlink_fmsg_pair_nest_end(fmsg);
4442 if (err)
4443 return err;
4444
4445 return 0;
4446}
4447EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4448
4449int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4450 u8 value)
4451{
4452 int err;
4453
4454 err = devlink_fmsg_pair_nest_start(fmsg, name);
4455 if (err)
4456 return err;
4457
4458 err = devlink_fmsg_u8_put(fmsg, value);
4459 if (err)
4460 return err;
4461
4462 err = devlink_fmsg_pair_nest_end(fmsg);
4463 if (err)
4464 return err;
4465
4466 return 0;
4467}
4468EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4469
4470int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4471 u32 value)
4472{
4473 int err;
4474
4475 err = devlink_fmsg_pair_nest_start(fmsg, name);
4476 if (err)
4477 return err;
4478
4479 err = devlink_fmsg_u32_put(fmsg, value);
4480 if (err)
4481 return err;
4482
4483 err = devlink_fmsg_pair_nest_end(fmsg);
4484 if (err)
4485 return err;
4486
4487 return 0;
4488}
4489EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4490
4491int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4492 u64 value)
4493{
4494 int err;
4495
4496 err = devlink_fmsg_pair_nest_start(fmsg, name);
4497 if (err)
4498 return err;
4499
4500 err = devlink_fmsg_u64_put(fmsg, value);
4501 if (err)
4502 return err;
4503
4504 err = devlink_fmsg_pair_nest_end(fmsg);
4505 if (err)
4506 return err;
4507
4508 return 0;
4509}
4510EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4511
4512int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4513 const char *value)
4514{
4515 int err;
4516
4517 err = devlink_fmsg_pair_nest_start(fmsg, name);
4518 if (err)
4519 return err;
4520
4521 err = devlink_fmsg_string_put(fmsg, value);
4522 if (err)
4523 return err;
4524
4525 err = devlink_fmsg_pair_nest_end(fmsg);
4526 if (err)
4527 return err;
4528
4529 return 0;
4530}
4531EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4532
4533int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
Aya Levine2cde862019-11-12 14:07:49 +02004534 const void *value, u32 value_len)
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004535{
Aya Levine2cde862019-11-12 14:07:49 +02004536 u32 data_size;
4537 u32 offset;
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004538 int err;
4539
Aya Levine2cde862019-11-12 14:07:49 +02004540 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004541 if (err)
4542 return err;
4543
Aya Levine2cde862019-11-12 14:07:49 +02004544 for (offset = 0; offset < value_len; offset += data_size) {
4545 data_size = value_len - offset;
4546 if (data_size > DEVLINK_FMSG_MAX_SIZE)
4547 data_size = DEVLINK_FMSG_MAX_SIZE;
4548 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
4549 if (err)
4550 return err;
4551 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004552
Aya Levine2cde862019-11-12 14:07:49 +02004553 err = devlink_fmsg_arr_pair_nest_end(fmsg);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004554 if (err)
4555 return err;
4556
4557 return 0;
4558}
4559EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4560
4561static int
4562devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4563{
4564 switch (msg->nla_type) {
4565 case NLA_FLAG:
4566 case NLA_U8:
4567 case NLA_U32:
4568 case NLA_U64:
4569 case NLA_NUL_STRING:
4570 case NLA_BINARY:
4571 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4572 msg->nla_type);
4573 default:
4574 return -EINVAL;
4575 }
4576}
4577
4578static int
4579devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4580{
4581 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4582 u8 tmp;
4583
4584 switch (msg->nla_type) {
4585 case NLA_FLAG:
4586 /* Always provide flag data, regardless of its value */
4587 tmp = *(bool *) msg->value;
4588
4589 return nla_put_u8(skb, attrtype, tmp);
4590 case NLA_U8:
4591 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4592 case NLA_U32:
4593 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4594 case NLA_U64:
4595 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4596 DEVLINK_ATTR_PAD);
4597 case NLA_NUL_STRING:
4598 return nla_put_string(skb, attrtype, (char *) &msg->value);
4599 case NLA_BINARY:
4600 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4601 default:
4602 return -EINVAL;
4603 }
4604}
4605
4606static int
4607devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4608 int *start)
4609{
4610 struct devlink_fmsg_item *item;
4611 struct nlattr *fmsg_nlattr;
4612 int i = 0;
4613 int err;
4614
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004615 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004616 if (!fmsg_nlattr)
4617 return -EMSGSIZE;
4618
4619 list_for_each_entry(item, &fmsg->item_list, list) {
4620 if (i < *start) {
4621 i++;
4622 continue;
4623 }
4624
4625 switch (item->attrtype) {
4626 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4627 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4628 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4629 case DEVLINK_ATTR_FMSG_NEST_END:
4630 err = nla_put_flag(skb, item->attrtype);
4631 break;
4632 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4633 err = devlink_fmsg_item_fill_type(item, skb);
4634 if (err)
4635 break;
4636 err = devlink_fmsg_item_fill_data(item, skb);
4637 break;
4638 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4639 err = nla_put_string(skb, item->attrtype,
4640 (char *) &item->value);
4641 break;
4642 default:
4643 err = -EINVAL;
4644 break;
4645 }
4646 if (!err)
4647 *start = ++i;
4648 else
4649 break;
4650 }
4651
4652 nla_nest_end(skb, fmsg_nlattr);
4653 return err;
4654}
4655
4656static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4657 struct genl_info *info,
4658 enum devlink_command cmd, int flags)
4659{
4660 struct nlmsghdr *nlh;
4661 struct sk_buff *skb;
4662 bool last = false;
4663 int index = 0;
4664 void *hdr;
4665 int err;
4666
4667 while (!last) {
4668 int tmp_index = index;
4669
4670 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4671 if (!skb)
4672 return -ENOMEM;
4673
4674 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4675 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4676 if (!hdr) {
4677 err = -EMSGSIZE;
4678 goto nla_put_failure;
4679 }
4680
4681 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4682 if (!err)
4683 last = true;
4684 else if (err != -EMSGSIZE || tmp_index == index)
4685 goto nla_put_failure;
4686
4687 genlmsg_end(skb, hdr);
4688 err = genlmsg_reply(skb, info);
4689 if (err)
4690 return err;
4691 }
4692
4693 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4694 if (!skb)
4695 return -ENOMEM;
4696 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4697 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4698 if (!nlh) {
4699 err = -EMSGSIZE;
4700 goto nla_put_failure;
4701 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004702
Li RongQingfde55ea2019-02-11 19:09:07 +08004703 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004704
4705nla_put_failure:
4706 nlmsg_free(skb);
4707 return err;
4708}
4709
Aya Levine44ef4e2019-05-16 09:49:20 +03004710static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4711 struct netlink_callback *cb,
4712 enum devlink_command cmd)
4713{
4714 int index = cb->args[0];
4715 int tmp_index = index;
4716 void *hdr;
4717 int err;
4718
4719 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4720 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4721 if (!hdr) {
4722 err = -EMSGSIZE;
4723 goto nla_put_failure;
4724 }
4725
4726 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4727 if ((err && err != -EMSGSIZE) || tmp_index == index)
4728 goto nla_put_failure;
4729
4730 cb->args[0] = index;
4731 genlmsg_end(skb, hdr);
4732 return skb->len;
4733
4734nla_put_failure:
4735 genlmsg_cancel(skb, hdr);
4736 return err;
4737}
4738
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004739struct devlink_health_reporter {
4740 struct list_head list;
4741 void *priv;
4742 const struct devlink_health_reporter_ops *ops;
4743 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004744 struct devlink_fmsg *dump_fmsg;
4745 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004746 u64 graceful_period;
4747 bool auto_recover;
4748 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004749 u64 dump_ts;
Aya Levind2795052019-11-10 14:11:56 +02004750 u64 dump_real_ts;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004751 u64 error_count;
4752 u64 recovery_count;
4753 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004754 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004755};
4756
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004757void *
4758devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4759{
4760 return reporter->priv;
4761}
4762EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4763
4764static struct devlink_health_reporter *
4765devlink_health_reporter_find_by_name(struct devlink *devlink,
4766 const char *reporter_name)
4767{
4768 struct devlink_health_reporter *reporter;
4769
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004770 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004771 list_for_each_entry(reporter, &devlink->reporter_list, list)
4772 if (!strcmp(reporter->ops->name, reporter_name))
4773 return reporter;
4774 return NULL;
4775}
4776
4777/**
4778 * devlink_health_reporter_create - create devlink health reporter
4779 *
4780 * @devlink: devlink
4781 * @ops: ops
4782 * @graceful_period: to avoid recovery loops, in msecs
4783 * @auto_recover: auto recover when error occurs
4784 * @priv: priv
4785 */
4786struct devlink_health_reporter *
4787devlink_health_reporter_create(struct devlink *devlink,
4788 const struct devlink_health_reporter_ops *ops,
4789 u64 graceful_period, bool auto_recover,
4790 void *priv)
4791{
4792 struct devlink_health_reporter *reporter;
4793
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004794 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004795 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4796 reporter = ERR_PTR(-EEXIST);
4797 goto unlock;
4798 }
4799
4800 if (WARN_ON(auto_recover && !ops->recover) ||
4801 WARN_ON(graceful_period && !ops->recover)) {
4802 reporter = ERR_PTR(-EINVAL);
4803 goto unlock;
4804 }
4805
4806 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4807 if (!reporter) {
4808 reporter = ERR_PTR(-ENOMEM);
4809 goto unlock;
4810 }
4811
4812 reporter->priv = priv;
4813 reporter->ops = ops;
4814 reporter->devlink = devlink;
4815 reporter->graceful_period = graceful_period;
4816 reporter->auto_recover = auto_recover;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004817 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004818 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004819 list_add_tail(&reporter->list, &devlink->reporter_list);
4820unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004821 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004822 return reporter;
4823}
4824EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4825
4826/**
4827 * devlink_health_reporter_destroy - destroy devlink health reporter
4828 *
4829 * @reporter: devlink health reporter to destroy
4830 */
4831void
4832devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4833{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004834 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004835 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004836 mutex_unlock(&reporter->devlink->reporters_lock);
4837 while (refcount_read(&reporter->refcount) > 1)
4838 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01004839 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004840 if (reporter->dump_fmsg)
4841 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004842 kfree(reporter);
4843}
4844EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4845
Eran Ben Elisha3167b272019-03-03 10:57:30 +02004846void
4847devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4848 enum devlink_health_reporter_state state)
4849{
4850 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4851 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4852 return;
4853
4854 if (reporter->health_state == state)
4855 return;
4856
4857 reporter->health_state = state;
4858 trace_devlink_health_reporter_state_update(reporter->devlink,
4859 reporter->ops->name, state);
4860}
4861EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4862
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004863static int
4864devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004865 void *priv_ctx, struct netlink_ext_ack *extack)
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004866{
4867 int err;
4868
Jiri Pirko40281822019-10-10 15:18:48 +02004869 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
4870 return 0;
4871
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004872 if (!reporter->ops->recover)
4873 return -EOPNOTSUPP;
4874
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004875 err = reporter->ops->recover(reporter, priv_ctx, extack);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004876 if (err)
4877 return err;
4878
4879 reporter->recovery_count++;
4880 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4881 reporter->last_recovery_ts = jiffies;
4882
4883 return 0;
4884}
4885
4886static void
4887devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4888{
4889 if (!reporter->dump_fmsg)
4890 return;
4891 devlink_fmsg_free(reporter->dump_fmsg);
4892 reporter->dump_fmsg = NULL;
4893}
4894
4895static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004896 void *priv_ctx,
4897 struct netlink_ext_ack *extack)
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004898{
4899 int err;
4900
4901 if (!reporter->ops->dump)
4902 return 0;
4903
4904 if (reporter->dump_fmsg)
4905 return 0;
4906
4907 reporter->dump_fmsg = devlink_fmsg_alloc();
4908 if (!reporter->dump_fmsg) {
4909 err = -ENOMEM;
4910 return err;
4911 }
4912
4913 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4914 if (err)
4915 goto dump_err;
4916
4917 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004918 priv_ctx, extack);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004919 if (err)
4920 goto dump_err;
4921
4922 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4923 if (err)
4924 goto dump_err;
4925
4926 reporter->dump_ts = jiffies;
Aya Levind2795052019-11-10 14:11:56 +02004927 reporter->dump_real_ts = ktime_get_real_ns();
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004928
4929 return 0;
4930
4931dump_err:
4932 devlink_health_dump_clear(reporter);
4933 return err;
4934}
4935
4936int devlink_health_report(struct devlink_health_reporter *reporter,
4937 const char *msg, void *priv_ctx)
4938{
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004939 enum devlink_health_reporter_state prev_health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004940 struct devlink *devlink = reporter->devlink;
4941
4942 /* write a log message of the current error */
4943 WARN_ON(!msg);
4944 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4945 reporter->error_count++;
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004946 prev_health_state = reporter->health_state;
4947 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004948
4949 /* abort if the previous error wasn't recovered */
4950 if (reporter->auto_recover &&
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004951 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004952 jiffies - reporter->last_recovery_ts <
4953 msecs_to_jiffies(reporter->graceful_period))) {
4954 trace_devlink_health_recover_aborted(devlink,
4955 reporter->ops->name,
4956 reporter->health_state,
4957 jiffies -
4958 reporter->last_recovery_ts);
4959 return -ECANCELED;
4960 }
4961
4962 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4963
4964 mutex_lock(&reporter->dump_lock);
4965 /* store current dump of current error, for later analysis */
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004966 devlink_health_do_dump(reporter, priv_ctx, NULL);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004967 mutex_unlock(&reporter->dump_lock);
4968
4969 if (reporter->auto_recover)
Jiri Pirkoe7a98102019-10-10 15:18:49 +02004970 return devlink_health_reporter_recover(reporter,
4971 priv_ctx, NULL);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004972
4973 return 0;
4974}
4975EXPORT_SYMBOL_GPL(devlink_health_report);
4976
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004977static struct devlink_health_reporter *
Aya Levine44ef4e2019-05-16 09:49:20 +03004978devlink_health_reporter_get_from_attrs(struct devlink *devlink,
4979 struct nlattr **attrs)
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004980{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004981 struct devlink_health_reporter *reporter;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004982 char *reporter_name;
4983
Aya Levine44ef4e2019-05-16 09:49:20 +03004984 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004985 return NULL;
4986
Aya Levine44ef4e2019-05-16 09:49:20 +03004987 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004988 mutex_lock(&devlink->reporters_lock);
4989 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4990 if (reporter)
4991 refcount_inc(&reporter->refcount);
4992 mutex_unlock(&devlink->reporters_lock);
4993 return reporter;
4994}
4995
Aya Levine44ef4e2019-05-16 09:49:20 +03004996static struct devlink_health_reporter *
4997devlink_health_reporter_get_from_info(struct devlink *devlink,
4998 struct genl_info *info)
4999{
5000 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
5001}
5002
5003static struct devlink_health_reporter *
5004devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
5005{
Jiri Pirkoee85da52019-10-05 20:04:42 +02005006 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Aya Levine44ef4e2019-05-16 09:49:20 +03005007 struct devlink_health_reporter *reporter;
Jiri Pirkoee85da52019-10-05 20:04:42 +02005008 struct nlattr **attrs = info->attrs;
Aya Levine44ef4e2019-05-16 09:49:20 +03005009 struct devlink *devlink;
Aya Levine44ef4e2019-05-16 09:49:20 +03005010
5011 mutex_lock(&devlink_mutex);
5012 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5013 if (IS_ERR(devlink))
5014 goto unlock;
5015
5016 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
5017 mutex_unlock(&devlink_mutex);
Aya Levine44ef4e2019-05-16 09:49:20 +03005018 return reporter;
5019unlock:
5020 mutex_unlock(&devlink_mutex);
Aya Levine44ef4e2019-05-16 09:49:20 +03005021 return NULL;
5022}
5023
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005024static void
5025devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5026{
5027 refcount_dec(&reporter->refcount);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005028}
5029
5030static int
5031devlink_nl_health_reporter_fill(struct sk_buff *msg,
5032 struct devlink *devlink,
5033 struct devlink_health_reporter *reporter,
5034 enum devlink_command cmd, u32 portid,
5035 u32 seq, int flags)
5036{
5037 struct nlattr *reporter_attr;
5038 void *hdr;
5039
5040 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5041 if (!hdr)
5042 return -EMSGSIZE;
5043
5044 if (devlink_nl_put_handle(msg, devlink))
5045 goto genlmsg_cancel;
5046
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005047 reporter_attr = nla_nest_start_noflag(msg,
5048 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005049 if (!reporter_attr)
5050 goto genlmsg_cancel;
5051 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
5052 reporter->ops->name))
5053 goto reporter_nest_cancel;
5054 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
5055 reporter->health_state))
5056 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005057 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005058 reporter->error_count, DEVLINK_ATTR_PAD))
5059 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02005060 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005061 reporter->recovery_count, DEVLINK_ATTR_PAD))
5062 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005063 if (reporter->ops->recover &&
5064 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005065 reporter->graceful_period,
5066 DEVLINK_ATTR_PAD))
5067 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02005068 if (reporter->ops->recover &&
5069 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005070 reporter->auto_recover))
5071 goto reporter_nest_cancel;
5072 if (reporter->dump_fmsg &&
5073 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
5074 jiffies_to_msecs(reporter->dump_ts),
5075 DEVLINK_ATTR_PAD))
5076 goto reporter_nest_cancel;
Aya Levind2795052019-11-10 14:11:56 +02005077 if (reporter->dump_fmsg &&
5078 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
5079 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
5080 goto reporter_nest_cancel;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005081
5082 nla_nest_end(msg, reporter_attr);
5083 genlmsg_end(msg, hdr);
5084 return 0;
5085
5086reporter_nest_cancel:
5087 nla_nest_end(msg, reporter_attr);
5088genlmsg_cancel:
5089 genlmsg_cancel(msg, hdr);
5090 return -EMSGSIZE;
5091}
5092
5093static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
5094 struct genl_info *info)
5095{
5096 struct devlink *devlink = info->user_ptr[0];
5097 struct devlink_health_reporter *reporter;
5098 struct sk_buff *msg;
5099 int err;
5100
5101 reporter = devlink_health_reporter_get_from_info(devlink, info);
5102 if (!reporter)
5103 return -EINVAL;
5104
5105 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005106 if (!msg) {
5107 err = -ENOMEM;
5108 goto out;
5109 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005110
5111 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5112 DEVLINK_CMD_HEALTH_REPORTER_GET,
5113 info->snd_portid, info->snd_seq,
5114 0);
5115 if (err) {
5116 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005117 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005118 }
5119
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005120 err = genlmsg_reply(msg, info);
5121out:
5122 devlink_health_reporter_put(reporter);
5123 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005124}
5125
5126static int
5127devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5128 struct netlink_callback *cb)
5129{
5130 struct devlink_health_reporter *reporter;
5131 struct devlink *devlink;
5132 int start = cb->args[0];
5133 int idx = 0;
5134 int err;
5135
5136 mutex_lock(&devlink_mutex);
5137 list_for_each_entry(devlink, &devlink_list, list) {
5138 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5139 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005140 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005141 list_for_each_entry(reporter, &devlink->reporter_list,
5142 list) {
5143 if (idx < start) {
5144 idx++;
5145 continue;
5146 }
5147 err = devlink_nl_health_reporter_fill(msg, devlink,
5148 reporter,
5149 DEVLINK_CMD_HEALTH_REPORTER_GET,
5150 NETLINK_CB(cb->skb).portid,
5151 cb->nlh->nlmsg_seq,
5152 NLM_F_MULTI);
5153 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005154 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005155 goto out;
5156 }
5157 idx++;
5158 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005159 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005160 }
5161out:
5162 mutex_unlock(&devlink_mutex);
5163
5164 cb->args[0] = idx;
5165 return msg->len;
5166}
5167
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005168static int
5169devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5170 struct genl_info *info)
5171{
5172 struct devlink *devlink = info->user_ptr[0];
5173 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005174 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005175
5176 reporter = devlink_health_reporter_get_from_info(devlink, info);
5177 if (!reporter)
5178 return -EINVAL;
5179
5180 if (!reporter->ops->recover &&
5181 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005182 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5183 err = -EOPNOTSUPP;
5184 goto out;
5185 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005186
5187 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5188 reporter->graceful_period =
5189 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5190
5191 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5192 reporter->auto_recover =
5193 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5194
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005195 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005196 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005197out:
5198 devlink_health_reporter_put(reporter);
5199 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005200}
5201
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005202static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5203 struct genl_info *info)
5204{
5205 struct devlink *devlink = info->user_ptr[0];
5206 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005207 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005208
5209 reporter = devlink_health_reporter_get_from_info(devlink, info);
5210 if (!reporter)
5211 return -EINVAL;
5212
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005213 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005214
5215 devlink_health_reporter_put(reporter);
5216 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005217}
5218
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005219static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5220 struct genl_info *info)
5221{
5222 struct devlink *devlink = info->user_ptr[0];
5223 struct devlink_health_reporter *reporter;
5224 struct devlink_fmsg *fmsg;
5225 int err;
5226
5227 reporter = devlink_health_reporter_get_from_info(devlink, info);
5228 if (!reporter)
5229 return -EINVAL;
5230
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005231 if (!reporter->ops->diagnose) {
5232 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005233 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005234 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005235
5236 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005237 if (!fmsg) {
5238 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005239 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005240 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005241
5242 err = devlink_fmsg_obj_nest_start(fmsg);
5243 if (err)
5244 goto out;
5245
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005246 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005247 if (err)
5248 goto out;
5249
5250 err = devlink_fmsg_obj_nest_end(fmsg);
5251 if (err)
5252 goto out;
5253
5254 err = devlink_fmsg_snd(fmsg, info,
5255 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5256
5257out:
5258 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005259 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005260 return err;
5261}
5262
Aya Levine44ef4e2019-05-16 09:49:20 +03005263static int
5264devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5265 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005266{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005267 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005268 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005269 int err;
5270
Aya Levine44ef4e2019-05-16 09:49:20 +03005271 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005272 if (!reporter)
5273 return -EINVAL;
5274
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005275 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005276 err = -EOPNOTSUPP;
5277 goto out;
5278 }
5279 mutex_lock(&reporter->dump_lock);
5280 if (!start) {
Jiri Pirkoe7a98102019-10-10 15:18:49 +02005281 err = devlink_health_do_dump(reporter, NULL, cb->extack);
Aya Levine44ef4e2019-05-16 09:49:20 +03005282 if (err)
5283 goto unlock;
5284 cb->args[1] = reporter->dump_ts;
5285 }
5286 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5287 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5288 err = -EAGAIN;
5289 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005290 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005291
Aya Levine44ef4e2019-05-16 09:49:20 +03005292 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5293 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5294unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005295 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005296out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005297 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005298 return err;
5299}
5300
5301static int
5302devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5303 struct genl_info *info)
5304{
5305 struct devlink *devlink = info->user_ptr[0];
5306 struct devlink_health_reporter *reporter;
5307
5308 reporter = devlink_health_reporter_get_from_info(devlink, info);
5309 if (!reporter)
5310 return -EINVAL;
5311
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005312 if (!reporter->ops->dump) {
5313 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005314 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005315 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005316
5317 mutex_lock(&reporter->dump_lock);
5318 devlink_health_dump_clear(reporter);
5319 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005320 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005321 return 0;
5322}
5323
Ido Schimmel0f420b62019-08-17 16:28:17 +03005324struct devlink_stats {
5325 u64 rx_bytes;
5326 u64 rx_packets;
5327 struct u64_stats_sync syncp;
5328};
5329
5330/**
5331 * struct devlink_trap_group_item - Packet trap group attributes.
5332 * @group: Immutable packet trap group attributes.
5333 * @refcount: Number of trap items using the group.
5334 * @list: trap_group_list member.
5335 * @stats: Trap group statistics.
5336 *
5337 * Describes packet trap group attributes. Created by devlink during trap
5338 * registration.
5339 */
5340struct devlink_trap_group_item {
5341 const struct devlink_trap_group *group;
5342 refcount_t refcount;
5343 struct list_head list;
5344 struct devlink_stats __percpu *stats;
5345};
5346
5347/**
5348 * struct devlink_trap_item - Packet trap attributes.
5349 * @trap: Immutable packet trap attributes.
5350 * @group_item: Associated group item.
5351 * @list: trap_list member.
5352 * @action: Trap action.
5353 * @stats: Trap statistics.
5354 * @priv: Driver private information.
5355 *
5356 * Describes both mutable and immutable packet trap attributes. Created by
5357 * devlink during trap registration and used for all trap related operations.
5358 */
5359struct devlink_trap_item {
5360 const struct devlink_trap *trap;
5361 struct devlink_trap_group_item *group_item;
5362 struct list_head list;
5363 enum devlink_trap_action action;
5364 struct devlink_stats __percpu *stats;
5365 void *priv;
5366};
5367
5368static struct devlink_trap_item *
5369devlink_trap_item_lookup(struct devlink *devlink, const char *name)
5370{
5371 struct devlink_trap_item *trap_item;
5372
5373 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5374 if (!strcmp(trap_item->trap->name, name))
5375 return trap_item;
5376 }
5377
5378 return NULL;
5379}
5380
5381static struct devlink_trap_item *
5382devlink_trap_item_get_from_info(struct devlink *devlink,
5383 struct genl_info *info)
5384{
5385 struct nlattr *attr;
5386
5387 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
5388 return NULL;
5389 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
5390
5391 return devlink_trap_item_lookup(devlink, nla_data(attr));
5392}
5393
5394static int
5395devlink_trap_action_get_from_info(struct genl_info *info,
5396 enum devlink_trap_action *p_trap_action)
5397{
5398 u8 val;
5399
5400 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
5401 switch (val) {
5402 case DEVLINK_TRAP_ACTION_DROP: /* fall-through */
5403 case DEVLINK_TRAP_ACTION_TRAP:
5404 *p_trap_action = val;
5405 break;
5406 default:
5407 return -EINVAL;
5408 }
5409
5410 return 0;
5411}
5412
5413static int devlink_trap_metadata_put(struct sk_buff *msg,
5414 const struct devlink_trap *trap)
5415{
5416 struct nlattr *attr;
5417
5418 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
5419 if (!attr)
5420 return -EMSGSIZE;
5421
5422 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
5423 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
5424 goto nla_put_failure;
5425
5426 nla_nest_end(msg, attr);
5427
5428 return 0;
5429
5430nla_put_failure:
5431 nla_nest_cancel(msg, attr);
5432 return -EMSGSIZE;
5433}
5434
5435static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
5436 struct devlink_stats *stats)
5437{
5438 int i;
5439
5440 memset(stats, 0, sizeof(*stats));
5441 for_each_possible_cpu(i) {
5442 struct devlink_stats *cpu_stats;
5443 u64 rx_packets, rx_bytes;
5444 unsigned int start;
5445
5446 cpu_stats = per_cpu_ptr(trap_stats, i);
5447 do {
5448 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5449 rx_packets = cpu_stats->rx_packets;
5450 rx_bytes = cpu_stats->rx_bytes;
5451 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5452
5453 stats->rx_packets += rx_packets;
5454 stats->rx_bytes += rx_bytes;
5455 }
5456}
5457
5458static int devlink_trap_stats_put(struct sk_buff *msg,
5459 struct devlink_stats __percpu *trap_stats)
5460{
5461 struct devlink_stats stats;
5462 struct nlattr *attr;
5463
5464 devlink_trap_stats_read(trap_stats, &stats);
5465
5466 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
5467 if (!attr)
5468 return -EMSGSIZE;
5469
5470 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
5471 stats.rx_packets, DEVLINK_ATTR_PAD))
5472 goto nla_put_failure;
5473
5474 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
5475 stats.rx_bytes, DEVLINK_ATTR_PAD))
5476 goto nla_put_failure;
5477
5478 nla_nest_end(msg, attr);
5479
5480 return 0;
5481
5482nla_put_failure:
5483 nla_nest_cancel(msg, attr);
5484 return -EMSGSIZE;
5485}
5486
5487static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
5488 const struct devlink_trap_item *trap_item,
5489 enum devlink_command cmd, u32 portid, u32 seq,
5490 int flags)
5491{
5492 struct devlink_trap_group_item *group_item = trap_item->group_item;
5493 void *hdr;
5494 int err;
5495
5496 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5497 if (!hdr)
5498 return -EMSGSIZE;
5499
5500 if (devlink_nl_put_handle(msg, devlink))
5501 goto nla_put_failure;
5502
5503 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5504 group_item->group->name))
5505 goto nla_put_failure;
5506
5507 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
5508 goto nla_put_failure;
5509
5510 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
5511 goto nla_put_failure;
5512
5513 if (trap_item->trap->generic &&
5514 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5515 goto nla_put_failure;
5516
5517 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
5518 goto nla_put_failure;
5519
5520 err = devlink_trap_metadata_put(msg, trap_item->trap);
5521 if (err)
5522 goto nla_put_failure;
5523
5524 err = devlink_trap_stats_put(msg, trap_item->stats);
5525 if (err)
5526 goto nla_put_failure;
5527
5528 genlmsg_end(msg, hdr);
5529
5530 return 0;
5531
5532nla_put_failure:
5533 genlmsg_cancel(msg, hdr);
5534 return -EMSGSIZE;
5535}
5536
5537static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
5538 struct genl_info *info)
5539{
5540 struct netlink_ext_ack *extack = info->extack;
5541 struct devlink *devlink = info->user_ptr[0];
5542 struct devlink_trap_item *trap_item;
5543 struct sk_buff *msg;
5544 int err;
5545
5546 if (list_empty(&devlink->trap_list))
5547 return -EOPNOTSUPP;
5548
5549 trap_item = devlink_trap_item_get_from_info(devlink, info);
5550 if (!trap_item) {
5551 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5552 return -ENOENT;
5553 }
5554
5555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5556 if (!msg)
5557 return -ENOMEM;
5558
5559 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5560 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
5561 info->snd_seq, 0);
5562 if (err)
5563 goto err_trap_fill;
5564
5565 return genlmsg_reply(msg, info);
5566
5567err_trap_fill:
5568 nlmsg_free(msg);
5569 return err;
5570}
5571
5572static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
5573 struct netlink_callback *cb)
5574{
5575 struct devlink_trap_item *trap_item;
5576 struct devlink *devlink;
5577 int start = cb->args[0];
5578 int idx = 0;
5579 int err;
5580
5581 mutex_lock(&devlink_mutex);
5582 list_for_each_entry(devlink, &devlink_list, list) {
5583 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5584 continue;
5585 mutex_lock(&devlink->lock);
5586 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5587 if (idx < start) {
5588 idx++;
5589 continue;
5590 }
5591 err = devlink_nl_trap_fill(msg, devlink, trap_item,
5592 DEVLINK_CMD_TRAP_NEW,
5593 NETLINK_CB(cb->skb).portid,
5594 cb->nlh->nlmsg_seq,
5595 NLM_F_MULTI);
5596 if (err) {
5597 mutex_unlock(&devlink->lock);
5598 goto out;
5599 }
5600 idx++;
5601 }
5602 mutex_unlock(&devlink->lock);
5603 }
5604out:
5605 mutex_unlock(&devlink_mutex);
5606
5607 cb->args[0] = idx;
5608 return msg->len;
5609}
5610
5611static int __devlink_trap_action_set(struct devlink *devlink,
5612 struct devlink_trap_item *trap_item,
5613 enum devlink_trap_action trap_action,
5614 struct netlink_ext_ack *extack)
5615{
5616 int err;
5617
5618 if (trap_item->action != trap_action &&
5619 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
5620 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
5621 return 0;
5622 }
5623
5624 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
5625 trap_action);
5626 if (err)
5627 return err;
5628
5629 trap_item->action = trap_action;
5630
5631 return 0;
5632}
5633
5634static int devlink_trap_action_set(struct devlink *devlink,
5635 struct devlink_trap_item *trap_item,
5636 struct genl_info *info)
5637{
5638 enum devlink_trap_action trap_action;
5639 int err;
5640
5641 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5642 return 0;
5643
5644 err = devlink_trap_action_get_from_info(info, &trap_action);
5645 if (err) {
5646 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5647 return -EINVAL;
5648 }
5649
5650 return __devlink_trap_action_set(devlink, trap_item, trap_action,
5651 info->extack);
5652}
5653
5654static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
5655 struct genl_info *info)
5656{
5657 struct netlink_ext_ack *extack = info->extack;
5658 struct devlink *devlink = info->user_ptr[0];
5659 struct devlink_trap_item *trap_item;
5660 int err;
5661
5662 if (list_empty(&devlink->trap_list))
5663 return -EOPNOTSUPP;
5664
5665 trap_item = devlink_trap_item_get_from_info(devlink, info);
5666 if (!trap_item) {
5667 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
5668 return -ENOENT;
5669 }
5670
5671 err = devlink_trap_action_set(devlink, trap_item, info);
5672 if (err)
5673 return err;
5674
5675 return 0;
5676}
5677
5678static struct devlink_trap_group_item *
5679devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
5680{
5681 struct devlink_trap_group_item *group_item;
5682
5683 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
5684 if (!strcmp(group_item->group->name, name))
5685 return group_item;
5686 }
5687
5688 return NULL;
5689}
5690
5691static struct devlink_trap_group_item *
5692devlink_trap_group_item_get_from_info(struct devlink *devlink,
5693 struct genl_info *info)
5694{
5695 char *name;
5696
5697 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
5698 return NULL;
5699 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
5700
5701 return devlink_trap_group_item_lookup(devlink, name);
5702}
5703
5704static int
5705devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
5706 const struct devlink_trap_group_item *group_item,
5707 enum devlink_command cmd, u32 portid, u32 seq,
5708 int flags)
5709{
5710 void *hdr;
5711 int err;
5712
5713 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5714 if (!hdr)
5715 return -EMSGSIZE;
5716
5717 if (devlink_nl_put_handle(msg, devlink))
5718 goto nla_put_failure;
5719
5720 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
5721 group_item->group->name))
5722 goto nla_put_failure;
5723
5724 if (group_item->group->generic &&
5725 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
5726 goto nla_put_failure;
5727
5728 err = devlink_trap_stats_put(msg, group_item->stats);
5729 if (err)
5730 goto nla_put_failure;
5731
5732 genlmsg_end(msg, hdr);
5733
5734 return 0;
5735
5736nla_put_failure:
5737 genlmsg_cancel(msg, hdr);
5738 return -EMSGSIZE;
5739}
5740
5741static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
5742 struct genl_info *info)
5743{
5744 struct netlink_ext_ack *extack = info->extack;
5745 struct devlink *devlink = info->user_ptr[0];
5746 struct devlink_trap_group_item *group_item;
5747 struct sk_buff *msg;
5748 int err;
5749
5750 if (list_empty(&devlink->trap_group_list))
5751 return -EOPNOTSUPP;
5752
5753 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5754 if (!group_item) {
5755 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5756 return -ENOENT;
5757 }
5758
5759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5760 if (!msg)
5761 return -ENOMEM;
5762
5763 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
5764 DEVLINK_CMD_TRAP_GROUP_NEW,
5765 info->snd_portid, info->snd_seq, 0);
5766 if (err)
5767 goto err_trap_group_fill;
5768
5769 return genlmsg_reply(msg, info);
5770
5771err_trap_group_fill:
5772 nlmsg_free(msg);
5773 return err;
5774}
5775
5776static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
5777 struct netlink_callback *cb)
5778{
5779 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
5780 struct devlink_trap_group_item *group_item;
5781 u32 portid = NETLINK_CB(cb->skb).portid;
5782 struct devlink *devlink;
5783 int start = cb->args[0];
5784 int idx = 0;
5785 int err;
5786
5787 mutex_lock(&devlink_mutex);
5788 list_for_each_entry(devlink, &devlink_list, list) {
5789 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5790 continue;
5791 mutex_lock(&devlink->lock);
5792 list_for_each_entry(group_item, &devlink->trap_group_list,
5793 list) {
5794 if (idx < start) {
5795 idx++;
5796 continue;
5797 }
5798 err = devlink_nl_trap_group_fill(msg, devlink,
5799 group_item, cmd,
5800 portid,
5801 cb->nlh->nlmsg_seq,
5802 NLM_F_MULTI);
5803 if (err) {
5804 mutex_unlock(&devlink->lock);
5805 goto out;
5806 }
5807 idx++;
5808 }
5809 mutex_unlock(&devlink->lock);
5810 }
5811out:
5812 mutex_unlock(&devlink_mutex);
5813
5814 cb->args[0] = idx;
5815 return msg->len;
5816}
5817
5818static int
5819__devlink_trap_group_action_set(struct devlink *devlink,
5820 struct devlink_trap_group_item *group_item,
5821 enum devlink_trap_action trap_action,
5822 struct netlink_ext_ack *extack)
5823{
5824 const char *group_name = group_item->group->name;
5825 struct devlink_trap_item *trap_item;
5826 int err;
5827
5828 list_for_each_entry(trap_item, &devlink->trap_list, list) {
5829 if (strcmp(trap_item->trap->group.name, group_name))
5830 continue;
5831 err = __devlink_trap_action_set(devlink, trap_item,
5832 trap_action, extack);
5833 if (err)
5834 return err;
5835 }
5836
5837 return 0;
5838}
5839
5840static int
5841devlink_trap_group_action_set(struct devlink *devlink,
5842 struct devlink_trap_group_item *group_item,
5843 struct genl_info *info)
5844{
5845 enum devlink_trap_action trap_action;
5846 int err;
5847
5848 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
5849 return 0;
5850
5851 err = devlink_trap_action_get_from_info(info, &trap_action);
5852 if (err) {
5853 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
5854 return -EINVAL;
5855 }
5856
5857 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
5858 info->extack);
5859 if (err)
5860 return err;
5861
5862 return 0;
5863}
5864
5865static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
5866 struct genl_info *info)
5867{
5868 struct netlink_ext_ack *extack = info->extack;
5869 struct devlink *devlink = info->user_ptr[0];
5870 struct devlink_trap_group_item *group_item;
5871 int err;
5872
5873 if (list_empty(&devlink->trap_group_list))
5874 return -EOPNOTSUPP;
5875
5876 group_item = devlink_trap_group_item_get_from_info(devlink, info);
5877 if (!group_item) {
5878 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
5879 return -ENOENT;
5880 }
5881
5882 err = devlink_trap_group_action_set(devlink, group_item, info);
5883 if (err)
5884 return err;
5885
5886 return 0;
5887}
5888
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005889static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
5890 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
5891 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
5892 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
5893 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
5894 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005895 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
5896 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
5897 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
5898 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
5899 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
5900 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
5901 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005902 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02005903 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03005904 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005905 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
5906 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005907 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
5908 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005909 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
5910 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
5911 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005912 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03005913 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005914 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005915 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
5916 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005917 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
5918 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Ido Schimmel0f420b62019-08-17 16:28:17 +03005919 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
5920 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
5921 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirko070c63f2019-10-03 11:49:39 +02005922 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
5923 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
5924 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005925};
5926
5927static const struct genl_ops devlink_nl_ops[] = {
5928 {
5929 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005930 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005931 .doit = devlink_nl_cmd_get_doit,
5932 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02005933 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005934 /* can be retrieved by unprivileged users */
5935 },
5936 {
5937 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005938 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005939 .doit = devlink_nl_cmd_port_get_doit,
5940 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005941 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5942 /* can be retrieved by unprivileged users */
5943 },
5944 {
5945 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005946 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005947 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005948 .flags = GENL_ADMIN_PERM,
5949 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5950 },
5951 {
5952 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005953 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005954 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005955 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005956 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5957 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005958 },
5959 {
5960 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005961 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005962 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005963 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005964 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5965 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005966 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005967 {
5968 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005969 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005970 .doit = devlink_nl_cmd_sb_get_doit,
5971 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005972 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5973 DEVLINK_NL_FLAG_NEED_SB,
5974 /* can be retrieved by unprivileged users */
5975 },
5976 {
5977 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005978 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005979 .doit = devlink_nl_cmd_sb_pool_get_doit,
5980 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005981 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5982 DEVLINK_NL_FLAG_NEED_SB,
5983 /* can be retrieved by unprivileged users */
5984 },
5985 {
5986 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005987 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005988 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005989 .flags = GENL_ADMIN_PERM,
5990 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5991 DEVLINK_NL_FLAG_NEED_SB,
5992 },
5993 {
5994 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005995 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005996 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5997 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005998 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5999 DEVLINK_NL_FLAG_NEED_SB,
6000 /* can be retrieved by unprivileged users */
6001 },
6002 {
6003 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006004 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006005 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006006 .flags = GENL_ADMIN_PERM,
6007 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6008 DEVLINK_NL_FLAG_NEED_SB,
6009 },
6010 {
6011 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006012 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006013 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
6014 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006015 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6016 DEVLINK_NL_FLAG_NEED_SB,
6017 /* can be retrieved by unprivileged users */
6018 },
6019 {
6020 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006021 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02006022 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02006023 .flags = GENL_ADMIN_PERM,
6024 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
6025 DEVLINK_NL_FLAG_NEED_SB,
6026 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006027 {
6028 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02006029 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006030 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006031 .flags = GENL_ADMIN_PERM,
6032 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006033 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006034 },
6035 {
6036 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006037 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006038 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006039 .flags = GENL_ADMIN_PERM,
6040 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006041 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02006042 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03006043 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006044 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006045 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006046 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006047 .flags = GENL_ADMIN_PERM,
6048 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6049 },
6050 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006051 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006052 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01006053 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006054 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07006055 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6056 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03006057 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006058 {
6059 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006060 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006061 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006062 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006063 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006064 },
6065 {
6066 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006067 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006068 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006069 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006070 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006071 },
6072 {
6073 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006074 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006075 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006076 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006077 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006078 },
6079 {
6080 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006081 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006082 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006083 .flags = GENL_ADMIN_PERM,
6084 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6085 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006086 {
6087 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006088 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006089 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006090 .flags = GENL_ADMIN_PERM,
6091 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6092 },
6093 {
6094 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02006095 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006096 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006097 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02006098 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006099 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006100 {
6101 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02006102 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006103 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01006104 .flags = GENL_ADMIN_PERM,
6105 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6106 DEVLINK_NL_FLAG_NO_LOCK,
6107 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006108 {
6109 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006110 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006111 .doit = devlink_nl_cmd_param_get_doit,
6112 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03006113 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6114 /* can be retrieved by unprivileged users */
6115 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006116 {
6117 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006118 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006119 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03006120 .flags = GENL_ADMIN_PERM,
6121 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6122 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006123 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306124 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006125 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306126 .doit = devlink_nl_cmd_port_param_get_doit,
6127 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05306128 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6129 /* can be retrieved by unprivileged users */
6130 },
6131 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05306132 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006133 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306134 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05306135 .flags = GENL_ADMIN_PERM,
6136 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
6137 },
6138 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006139 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006140 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006141 .doit = devlink_nl_cmd_region_get_doit,
6142 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03006143 .flags = GENL_ADMIN_PERM,
6144 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6145 },
Alex Vesker866319b2018-07-12 15:13:13 +03006146 {
6147 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02006148 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03006149 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03006150 .flags = GENL_ADMIN_PERM,
6151 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6152 },
Alex Vesker4e547952018-07-12 15:13:14 +03006153 {
6154 .cmd = DEVLINK_CMD_REGION_READ,
Jiri Pirkoee85da52019-10-05 20:04:42 +02006155 .validate = GENL_DONT_VALIDATE_STRICT |
6156 GENL_DONT_VALIDATE_DUMP_STRICT,
Alex Vesker4e547952018-07-12 15:13:14 +03006157 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03006158 .flags = GENL_ADMIN_PERM,
6159 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6160 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006161 {
6162 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006163 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006164 .doit = devlink_nl_cmd_info_get_doit,
6165 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08006166 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6167 /* can be retrieved by unprivileged users */
6168 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006169 {
6170 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006171 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006172 .doit = devlink_nl_cmd_health_reporter_get_doit,
6173 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006174 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6175 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02006176 /* can be retrieved by unprivileged users */
6177 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006178 {
6179 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02006180 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006181 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006182 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006183 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6184 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02006185 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006186 {
6187 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02006188 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006189 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006190 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006191 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6192 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02006193 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006194 {
6195 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006196 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006197 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006198 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006199 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6200 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02006201 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006202 {
6203 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Jiri Pirko82a843d2019-10-07 09:28:31 +02006204 .validate = GENL_DONT_VALIDATE_STRICT |
6205 GENL_DONT_VALIDATE_DUMP_STRICT,
Aya Levine44ef4e2019-05-16 09:49:20 +03006206 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006207 .flags = GENL_ADMIN_PERM,
6208 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6209 DEVLINK_NL_FLAG_NO_LOCK,
6210 },
6211 {
6212 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02006213 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006214 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02006215 .flags = GENL_ADMIN_PERM,
6216 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
6217 DEVLINK_NL_FLAG_NO_LOCK,
6218 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006219 {
6220 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02006221 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006222 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08006223 .flags = GENL_ADMIN_PERM,
6224 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6225 },
Ido Schimmel0f420b62019-08-17 16:28:17 +03006226 {
6227 .cmd = DEVLINK_CMD_TRAP_GET,
6228 .doit = devlink_nl_cmd_trap_get_doit,
6229 .dumpit = devlink_nl_cmd_trap_get_dumpit,
6230 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6231 /* can be retrieved by unprivileged users */
6232 },
6233 {
6234 .cmd = DEVLINK_CMD_TRAP_SET,
6235 .doit = devlink_nl_cmd_trap_set_doit,
6236 .flags = GENL_ADMIN_PERM,
6237 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6238 },
6239 {
6240 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
6241 .doit = devlink_nl_cmd_trap_group_get_doit,
6242 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
6243 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6244 /* can be retrieved by unprivileged users */
6245 },
6246 {
6247 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
6248 .doit = devlink_nl_cmd_trap_group_set_doit,
6249 .flags = GENL_ADMIN_PERM,
6250 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6251 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006252};
6253
Johannes Berg56989f62016-10-24 14:40:05 +02006254static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02006255 .name = DEVLINK_GENL_NAME,
6256 .version = DEVLINK_GENL_VERSION,
6257 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01006258 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02006259 .netnsok = true,
6260 .pre_doit = devlink_nl_pre_doit,
6261 .post_doit = devlink_nl_post_doit,
6262 .module = THIS_MODULE,
6263 .ops = devlink_nl_ops,
6264 .n_ops = ARRAY_SIZE(devlink_nl_ops),
6265 .mcgrps = devlink_nl_mcgrps,
6266 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
6267};
6268
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006269/**
6270 * devlink_alloc - Allocate new devlink instance resources
6271 *
6272 * @ops: ops
6273 * @priv_size: size of user private data
6274 *
6275 * Allocate new devlink instance resources, including devlink index
6276 * and name.
6277 */
6278struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
6279{
6280 struct devlink *devlink;
6281
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006282 if (WARN_ON(!ops))
6283 return NULL;
6284
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006285 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
6286 if (!devlink)
6287 return NULL;
6288 devlink->ops = ops;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006289 __devlink_net_set(devlink, &init_net);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006290 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02006291 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006292 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006293 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006294 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006295 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02006296 INIT_LIST_HEAD(&devlink->reporter_list);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006297 INIT_LIST_HEAD(&devlink->trap_list);
6298 INIT_LIST_HEAD(&devlink->trap_group_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006299 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006300 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006301 return devlink;
6302}
6303EXPORT_SYMBOL_GPL(devlink_alloc);
6304
6305/**
6306 * devlink_register - Register devlink instance
6307 *
6308 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006309 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006310 */
6311int devlink_register(struct devlink *devlink, struct device *dev)
6312{
6313 mutex_lock(&devlink_mutex);
6314 devlink->dev = dev;
Jiri Pirko8273fd82019-10-05 08:10:31 +02006315 devlink->registered = true;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006316 list_add_tail(&devlink->list, &devlink_list);
6317 devlink_notify(devlink, DEVLINK_CMD_NEW);
6318 mutex_unlock(&devlink_mutex);
6319 return 0;
6320}
6321EXPORT_SYMBOL_GPL(devlink_register);
6322
6323/**
6324 * devlink_unregister - Unregister devlink instance
6325 *
6326 * @devlink: devlink
6327 */
6328void devlink_unregister(struct devlink *devlink)
6329{
6330 mutex_lock(&devlink_mutex);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01006331 WARN_ON(devlink_reload_supported(devlink) &&
6332 devlink->reload_enabled);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006333 devlink_notify(devlink, DEVLINK_CMD_DEL);
6334 list_del(&devlink->list);
6335 mutex_unlock(&devlink_mutex);
6336}
6337EXPORT_SYMBOL_GPL(devlink_unregister);
6338
6339/**
Jiri Pirkoa0c76342019-11-08 21:42:43 +01006340 * devlink_reload_enable - Enable reload of devlink instance
6341 *
6342 * @devlink: devlink
6343 *
6344 * Should be called at end of device initialization
6345 * process when reload operation is supported.
6346 */
6347void devlink_reload_enable(struct devlink *devlink)
6348{
6349 mutex_lock(&devlink_mutex);
6350 devlink->reload_enabled = true;
6351 mutex_unlock(&devlink_mutex);
6352}
6353EXPORT_SYMBOL_GPL(devlink_reload_enable);
6354
6355/**
6356 * devlink_reload_disable - Disable reload of devlink instance
6357 *
6358 * @devlink: devlink
6359 *
6360 * Should be called at the beginning of device cleanup
6361 * process when reload operation is supported.
6362 */
6363void devlink_reload_disable(struct devlink *devlink)
6364{
6365 mutex_lock(&devlink_mutex);
6366 /* Mutex is taken which ensures that no reload operation is in
6367 * progress while setting up forbidded flag.
6368 */
6369 devlink->reload_enabled = false;
6370 mutex_unlock(&devlink_mutex);
6371}
6372EXPORT_SYMBOL_GPL(devlink_reload_disable);
6373
6374/**
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006375 * devlink_free - Free devlink instance resources
6376 *
6377 * @devlink: devlink
6378 */
6379void devlink_free(struct devlink *devlink)
6380{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03006381 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01006382 mutex_destroy(&devlink->lock);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006383 WARN_ON(!list_empty(&devlink->trap_group_list));
6384 WARN_ON(!list_empty(&devlink->trap_list));
Parav Panditb904aad2019-02-08 15:15:00 -06006385 WARN_ON(!list_empty(&devlink->reporter_list));
6386 WARN_ON(!list_empty(&devlink->region_list));
6387 WARN_ON(!list_empty(&devlink->param_list));
6388 WARN_ON(!list_empty(&devlink->resource_list));
6389 WARN_ON(!list_empty(&devlink->dpipe_table_list));
6390 WARN_ON(!list_empty(&devlink->sb_list));
6391 WARN_ON(!list_empty(&devlink->port_list));
6392
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006393 kfree(devlink);
6394}
6395EXPORT_SYMBOL_GPL(devlink_free);
6396
Jiri Pirko136bf272019-05-23 10:43:35 +02006397static void devlink_port_type_warn(struct work_struct *work)
6398{
6399 WARN(true, "Type was not set for devlink port.");
6400}
6401
6402static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
6403{
6404 /* Ignore CPU and DSA flavours. */
6405 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
6406 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
6407}
6408
Ido Schimmel4c582232020-01-09 19:57:41 +02006409#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
Jiri Pirko136bf272019-05-23 10:43:35 +02006410
6411static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
6412{
6413 if (!devlink_port_type_should_warn(devlink_port))
6414 return;
6415 /* Schedule a work to WARN in case driver does not set port
6416 * type within timeout.
6417 */
6418 schedule_delayed_work(&devlink_port->type_warn_dw,
6419 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
6420}
6421
6422static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
6423{
6424 if (!devlink_port_type_should_warn(devlink_port))
6425 return;
6426 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
6427}
6428
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006429/**
6430 * devlink_port_register - Register devlink port
6431 *
6432 * @devlink: devlink
6433 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006434 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006435 *
6436 * Register devlink port with provided port index. User can use
6437 * any indexing, even hw-related one. devlink_port structure
6438 * is convenient to be embedded inside user driver private structure.
6439 * Note that the caller should take care of zeroing the devlink_port
6440 * structure.
6441 */
6442int devlink_port_register(struct devlink *devlink,
6443 struct devlink_port *devlink_port,
6444 unsigned int port_index)
6445{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006446 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006447 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006448 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006449 return -EEXIST;
6450 }
6451 devlink_port->devlink = devlink;
6452 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006453 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01006454 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006455 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306456 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006457 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02006458 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
6459 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006460 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6461 return 0;
6462}
6463EXPORT_SYMBOL_GPL(devlink_port_register);
6464
6465/**
6466 * devlink_port_unregister - Unregister devlink port
6467 *
6468 * @devlink_port: devlink port
6469 */
6470void devlink_port_unregister(struct devlink_port *devlink_port)
6471{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006472 struct devlink *devlink = devlink_port->devlink;
6473
Jiri Pirko136bf272019-05-23 10:43:35 +02006474 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006475 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006476 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006477 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006478 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006479}
6480EXPORT_SYMBOL_GPL(devlink_port_unregister);
6481
6482static void __devlink_port_type_set(struct devlink_port *devlink_port,
6483 enum devlink_port_type type,
6484 void *type_dev)
6485{
Jiri Pirko2b239e72019-03-24 11:14:36 +01006486 if (WARN_ON(!devlink_port->registered))
6487 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02006488 devlink_port_type_warn_cancel(devlink_port);
Ido Schimmel0f420b62019-08-17 16:28:17 +03006489 spin_lock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006490 devlink_port->type = type;
6491 devlink_port->type_dev = type_dev;
Ido Schimmel0f420b62019-08-17 16:28:17 +03006492 spin_unlock_bh(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006493 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
6494}
6495
6496/**
6497 * devlink_port_type_eth_set - Set port type to Ethernet
6498 *
6499 * @devlink_port: devlink port
6500 * @netdev: related netdevice
6501 */
6502void devlink_port_type_eth_set(struct devlink_port *devlink_port,
6503 struct net_device *netdev)
6504{
Jiri Pirko119c0b52019-04-03 14:24:27 +02006505 const struct net_device_ops *ops = netdev->netdev_ops;
6506
Jiri Pirko746364f2019-03-28 13:56:46 +01006507 /* If driver registers devlink port, it should set devlink port
6508 * attributes accordingly so the compat functions are called
6509 * and the original ops are not used.
6510 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02006511 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01006512 /* Some drivers use the same set of ndos for netdevs
6513 * that have devlink_port registered and also for
6514 * those who don't. Make sure that ndo_get_phys_port_name
6515 * returns -EOPNOTSUPP here in case it is defined.
6516 * Warn if not.
6517 */
Jiri Pirko746364f2019-03-28 13:56:46 +01006518 char name[IFNAMSIZ];
6519 int err;
6520
6521 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
6522 WARN_ON(err != -EOPNOTSUPP);
6523 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02006524 if (ops->ndo_get_port_parent_id) {
6525 /* Some drivers use the same set of ndos for netdevs
6526 * that have devlink_port registered and also for
6527 * those who don't. Make sure that ndo_get_port_parent_id
6528 * returns -EOPNOTSUPP here in case it is defined.
6529 * Warn if not.
6530 */
6531 struct netdev_phys_item_id ppid;
6532 int err;
6533
6534 err = ops->ndo_get_port_parent_id(netdev, &ppid);
6535 WARN_ON(err != -EOPNOTSUPP);
6536 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01006537 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006538}
6539EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
6540
6541/**
6542 * devlink_port_type_ib_set - Set port type to InfiniBand
6543 *
6544 * @devlink_port: devlink port
6545 * @ibdev: related IB device
6546 */
6547void devlink_port_type_ib_set(struct devlink_port *devlink_port,
6548 struct ib_device *ibdev)
6549{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006550 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006551}
6552EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
6553
6554/**
6555 * devlink_port_type_clear - Clear port type
6556 *
6557 * @devlink_port: devlink port
6558 */
6559void devlink_port_type_clear(struct devlink_port *devlink_port)
6560{
Jiri Pirko773b1f32019-03-24 11:14:30 +01006561 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02006562 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006563}
6564EXPORT_SYMBOL_GPL(devlink_port_type_clear);
6565
Parav Pandit378ef012019-07-08 23:17:35 -05006566static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
6567 enum devlink_port_flavour flavour,
6568 const unsigned char *switch_id,
6569 unsigned char switch_id_len)
6570{
6571 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6572
6573 if (WARN_ON(devlink_port->registered))
6574 return -EEXIST;
6575 attrs->set = true;
6576 attrs->flavour = flavour;
6577 if (switch_id) {
6578 attrs->switch_port = true;
6579 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
6580 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
6581 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
6582 attrs->switch_id.id_len = switch_id_len;
6583 } else {
6584 attrs->switch_port = false;
6585 }
6586 return 0;
6587}
6588
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006589/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006590 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006591 *
6592 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02006593 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006594 * @port_number: number of the port that is facing user, for example
6595 * the front panel port number
6596 * @split: indicates if this is split port
6597 * @split_subport_number: if the port is split, this is the number
6598 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02006599 * @switch_id: if the port is part of switch, this is buffer with ID,
6600 * otwerwise this is NULL
6601 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006602 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006603void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02006604 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006605 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02006606 u32 split_subport_number,
6607 const unsigned char *switch_id,
6608 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006609{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006610 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05006611 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006612
Parav Pandit378ef012019-07-08 23:17:35 -05006613 ret = __devlink_port_attrs_set(devlink_port, flavour,
6614 switch_id, switch_id_len);
6615 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01006616 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006617 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05006618 attrs->phys.port_number = port_number;
6619 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006620}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02006621EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006622
Parav Pandit98fd2d62019-07-08 23:17:37 -05006623/**
6624 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
6625 *
6626 * @devlink_port: devlink port
6627 * @pf: associated PF for the devlink port instance
6628 * @switch_id: if the port is part of switch, this is buffer with ID,
6629 * otherwise this is NULL
6630 * @switch_id_len: length of the switch_id buffer
6631 */
6632void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
6633 const unsigned char *switch_id,
6634 unsigned char switch_id_len, u16 pf)
6635{
6636 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6637 int ret;
6638
6639 ret = __devlink_port_attrs_set(devlink_port,
6640 DEVLINK_PORT_FLAVOUR_PCI_PF,
6641 switch_id, switch_id_len);
6642 if (ret)
6643 return;
6644
6645 attrs->pci_pf.pf = pf;
6646}
6647EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
6648
Parav Pandite41b6bf2019-07-08 23:17:38 -05006649/**
6650 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
6651 *
6652 * @devlink_port: devlink port
6653 * @pf: associated PF for the devlink port instance
6654 * @vf: associated VF of a PF for the devlink port instance
6655 * @switch_id: if the port is part of switch, this is buffer with ID,
6656 * otherwise this is NULL
6657 * @switch_id_len: length of the switch_id buffer
6658 */
6659void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
6660 const unsigned char *switch_id,
6661 unsigned char switch_id_len,
6662 u16 pf, u16 vf)
6663{
6664 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6665 int ret;
6666
6667 ret = __devlink_port_attrs_set(devlink_port,
6668 DEVLINK_PORT_FLAVOUR_PCI_VF,
6669 switch_id, switch_id_len);
6670 if (ret)
6671 return;
6672 attrs->pci_vf.pf = pf;
6673 attrs->pci_vf.vf = vf;
6674}
6675EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
6676
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006677static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
6678 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02006679{
6680 struct devlink_port_attrs *attrs = &devlink_port->attrs;
6681 int n = 0;
6682
6683 if (!attrs->set)
6684 return -EOPNOTSUPP;
6685
6686 switch (attrs->flavour) {
6687 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
6688 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05006689 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006690 else
Parav Pandit378ef012019-07-08 23:17:35 -05006691 n = snprintf(name, len, "p%us%u",
6692 attrs->phys.port_number,
6693 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02006694 break;
6695 case DEVLINK_PORT_FLAVOUR_CPU:
6696 case DEVLINK_PORT_FLAVOUR_DSA:
6697 /* As CPU and DSA ports do not have a netdevice associated
6698 * case should not ever happen.
6699 */
6700 WARN_ON(1);
6701 return -EINVAL;
Parav Pandit98fd2d62019-07-08 23:17:37 -05006702 case DEVLINK_PORT_FLAVOUR_PCI_PF:
6703 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
6704 break;
Parav Pandite41b6bf2019-07-08 23:17:38 -05006705 case DEVLINK_PORT_FLAVOUR_PCI_VF:
6706 n = snprintf(name, len, "pf%uvf%u",
6707 attrs->pci_vf.pf, attrs->pci_vf.vf);
6708 break;
Jiri Pirko08474c12018-05-18 09:29:02 +02006709 }
6710
6711 if (n >= len)
6712 return -EINVAL;
6713
6714 return 0;
6715}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006716
Jiri Pirkobf797472016-04-14 18:19:13 +02006717int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
6718 u32 size, u16 ingress_pools_count,
6719 u16 egress_pools_count, u16 ingress_tc_count,
6720 u16 egress_tc_count)
6721{
6722 struct devlink_sb *devlink_sb;
6723 int err = 0;
6724
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006725 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006726 if (devlink_sb_index_exists(devlink, sb_index)) {
6727 err = -EEXIST;
6728 goto unlock;
6729 }
6730
6731 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
6732 if (!devlink_sb) {
6733 err = -ENOMEM;
6734 goto unlock;
6735 }
6736 devlink_sb->index = sb_index;
6737 devlink_sb->size = size;
6738 devlink_sb->ingress_pools_count = ingress_pools_count;
6739 devlink_sb->egress_pools_count = egress_pools_count;
6740 devlink_sb->ingress_tc_count = ingress_tc_count;
6741 devlink_sb->egress_tc_count = egress_tc_count;
6742 list_add_tail(&devlink_sb->list, &devlink->sb_list);
6743unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006744 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006745 return err;
6746}
6747EXPORT_SYMBOL_GPL(devlink_sb_register);
6748
6749void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
6750{
6751 struct devlink_sb *devlink_sb;
6752
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006753 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006754 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
6755 WARN_ON(!devlink_sb);
6756 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006757 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02006758 kfree(devlink_sb);
6759}
6760EXPORT_SYMBOL_GPL(devlink_sb_unregister);
6761
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006762/**
6763 * devlink_dpipe_headers_register - register dpipe headers
6764 *
6765 * @devlink: devlink
6766 * @dpipe_headers: dpipe header array
6767 *
6768 * Register the headers supported by hardware.
6769 */
6770int devlink_dpipe_headers_register(struct devlink *devlink,
6771 struct devlink_dpipe_headers *dpipe_headers)
6772{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006773 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006774 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006775 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006776 return 0;
6777}
6778EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
6779
6780/**
6781 * devlink_dpipe_headers_unregister - unregister dpipe headers
6782 *
6783 * @devlink: devlink
6784 *
6785 * Unregister the headers supported by hardware.
6786 */
6787void devlink_dpipe_headers_unregister(struct devlink *devlink)
6788{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006789 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006790 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006791 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006792}
6793EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
6794
6795/**
6796 * devlink_dpipe_table_counter_enabled - check if counter allocation
6797 * required
6798 * @devlink: devlink
6799 * @table_name: tables name
6800 *
6801 * Used by driver to check if counter allocation is required.
6802 * After counter allocation is turned on the table entries
6803 * are updated to include counter statistics.
6804 *
6805 * After that point on the driver must respect the counter
6806 * state so that each entry added to the table is added
6807 * with a counter.
6808 */
6809bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
6810 const char *table_name)
6811{
6812 struct devlink_dpipe_table *table;
6813 bool enabled;
6814
6815 rcu_read_lock();
6816 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6817 table_name);
6818 enabled = false;
6819 if (table)
6820 enabled = table->counters_enabled;
6821 rcu_read_unlock();
6822 return enabled;
6823}
6824EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
6825
6826/**
6827 * devlink_dpipe_table_register - register dpipe table
6828 *
6829 * @devlink: devlink
6830 * @table_name: table name
6831 * @table_ops: table ops
6832 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006833 * @counter_control_extern: external control for counters
6834 */
6835int devlink_dpipe_table_register(struct devlink *devlink,
6836 const char *table_name,
6837 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006838 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006839{
6840 struct devlink_dpipe_table *table;
6841
6842 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
6843 return -EEXIST;
6844
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02006845 if (WARN_ON(!table_ops->size_get))
6846 return -EINVAL;
6847
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006848 table = kzalloc(sizeof(*table), GFP_KERNEL);
6849 if (!table)
6850 return -ENOMEM;
6851
6852 table->name = table_name;
6853 table->table_ops = table_ops;
6854 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006855 table->counter_control_extern = counter_control_extern;
6856
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006857 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006858 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006859 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006860 return 0;
6861}
6862EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
6863
6864/**
6865 * devlink_dpipe_table_unregister - unregister dpipe table
6866 *
6867 * @devlink: devlink
6868 * @table_name: table name
6869 */
6870void devlink_dpipe_table_unregister(struct devlink *devlink,
6871 const char *table_name)
6872{
6873 struct devlink_dpipe_table *table;
6874
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006875 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006876 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6877 table_name);
6878 if (!table)
6879 goto unlock;
6880 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006881 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006882 kfree_rcu(table, rcu);
6883 return;
6884unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006885 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006886}
6887EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
6888
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006889/**
6890 * devlink_resource_register - devlink resource register
6891 *
6892 * @devlink: devlink
6893 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006894 * @resource_size: resource's size
6895 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006896 * @parent_resource_id: resource's parent id
6897 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006898 */
6899int devlink_resource_register(struct devlink *devlink,
6900 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006901 u64 resource_size,
6902 u64 resource_id,
6903 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02006904 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006905{
6906 struct devlink_resource *resource;
6907 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07006908 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006909 int err = 0;
6910
David Ahern14530742018-03-20 19:31:14 -07006911 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
6912
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006913 mutex_lock(&devlink->lock);
6914 resource = devlink_resource_find(devlink, NULL, resource_id);
6915 if (resource) {
6916 err = -EINVAL;
6917 goto out;
6918 }
6919
6920 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
6921 if (!resource) {
6922 err = -ENOMEM;
6923 goto out;
6924 }
6925
6926 if (top_hierarchy) {
6927 resource_list = &devlink->resource_list;
6928 } else {
6929 struct devlink_resource *parent_resource;
6930
6931 parent_resource = devlink_resource_find(devlink, NULL,
6932 parent_resource_id);
6933 if (parent_resource) {
6934 resource_list = &parent_resource->resource_list;
6935 resource->parent = parent_resource;
6936 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00006937 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006938 err = -EINVAL;
6939 goto out;
6940 }
6941 }
6942
6943 resource->name = resource_name;
6944 resource->size = resource_size;
6945 resource->size_new = resource_size;
6946 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006947 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01006948 memcpy(&resource->size_params, size_params,
6949 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006950 INIT_LIST_HEAD(&resource->resource_list);
6951 list_add_tail(&resource->list, resource_list);
6952out:
6953 mutex_unlock(&devlink->lock);
6954 return err;
6955}
6956EXPORT_SYMBOL_GPL(devlink_resource_register);
6957
6958/**
6959 * devlink_resources_unregister - free all resources
6960 *
6961 * @devlink: devlink
6962 * @resource: resource
6963 */
6964void devlink_resources_unregister(struct devlink *devlink,
6965 struct devlink_resource *resource)
6966{
6967 struct devlink_resource *tmp, *child_resource;
6968 struct list_head *resource_list;
6969
6970 if (resource)
6971 resource_list = &resource->resource_list;
6972 else
6973 resource_list = &devlink->resource_list;
6974
6975 if (!resource)
6976 mutex_lock(&devlink->lock);
6977
6978 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
6979 devlink_resources_unregister(devlink, child_resource);
6980 list_del(&child_resource->list);
6981 kfree(child_resource);
6982 }
6983
6984 if (!resource)
6985 mutex_unlock(&devlink->lock);
6986}
6987EXPORT_SYMBOL_GPL(devlink_resources_unregister);
6988
6989/**
6990 * devlink_resource_size_get - get and update size
6991 *
6992 * @devlink: devlink
6993 * @resource_id: the requested resource id
6994 * @p_resource_size: ptr to update
6995 */
6996int devlink_resource_size_get(struct devlink *devlink,
6997 u64 resource_id,
6998 u64 *p_resource_size)
6999{
7000 struct devlink_resource *resource;
7001 int err = 0;
7002
7003 mutex_lock(&devlink->lock);
7004 resource = devlink_resource_find(devlink, NULL, resource_id);
7005 if (!resource) {
7006 err = -EINVAL;
7007 goto out;
7008 }
7009 *p_resource_size = resource->size_new;
7010 resource->size = resource->size_new;
7011out:
7012 mutex_unlock(&devlink->lock);
7013 return err;
7014}
7015EXPORT_SYMBOL_GPL(devlink_resource_size_get);
7016
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01007017/**
7018 * devlink_dpipe_table_resource_set - set the resource id
7019 *
7020 * @devlink: devlink
7021 * @table_name: table name
7022 * @resource_id: resource id
7023 * @resource_units: number of resource's units consumed per table's entry
7024 */
7025int devlink_dpipe_table_resource_set(struct devlink *devlink,
7026 const char *table_name, u64 resource_id,
7027 u64 resource_units)
7028{
7029 struct devlink_dpipe_table *table;
7030 int err = 0;
7031
7032 mutex_lock(&devlink->lock);
7033 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
7034 table_name);
7035 if (!table) {
7036 err = -EINVAL;
7037 goto out;
7038 }
7039 table->resource_id = resource_id;
7040 table->resource_units = resource_units;
7041 table->resource_valid = true;
7042out:
7043 mutex_unlock(&devlink->lock);
7044 return err;
7045}
7046EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
7047
Jiri Pirkofc56be42018-04-05 22:13:21 +02007048/**
7049 * devlink_resource_occ_get_register - register occupancy getter
7050 *
7051 * @devlink: devlink
7052 * @resource_id: resource id
7053 * @occ_get: occupancy getter callback
7054 * @occ_get_priv: occupancy getter callback priv
7055 */
7056void devlink_resource_occ_get_register(struct devlink *devlink,
7057 u64 resource_id,
7058 devlink_resource_occ_get_t *occ_get,
7059 void *occ_get_priv)
7060{
7061 struct devlink_resource *resource;
7062
7063 mutex_lock(&devlink->lock);
7064 resource = devlink_resource_find(devlink, NULL, resource_id);
7065 if (WARN_ON(!resource))
7066 goto out;
7067 WARN_ON(resource->occ_get);
7068
7069 resource->occ_get = occ_get;
7070 resource->occ_get_priv = occ_get_priv;
7071out:
7072 mutex_unlock(&devlink->lock);
7073}
7074EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
7075
7076/**
7077 * devlink_resource_occ_get_unregister - unregister occupancy getter
7078 *
7079 * @devlink: devlink
7080 * @resource_id: resource id
7081 */
7082void devlink_resource_occ_get_unregister(struct devlink *devlink,
7083 u64 resource_id)
7084{
7085 struct devlink_resource *resource;
7086
7087 mutex_lock(&devlink->lock);
7088 resource = devlink_resource_find(devlink, NULL, resource_id);
7089 if (WARN_ON(!resource))
7090 goto out;
7091 WARN_ON(!resource->occ_get);
7092
7093 resource->occ_get = NULL;
7094 resource->occ_get_priv = NULL;
7095out:
7096 mutex_unlock(&devlink->lock);
7097}
7098EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
7099
Vasundhara Volam39e61602019-01-28 18:00:20 +05307100static int devlink_param_verify(const struct devlink_param *param)
7101{
7102 if (!param || !param->name || !param->supported_cmodes)
7103 return -EINVAL;
7104 if (param->generic)
7105 return devlink_param_generic_verify(param);
7106 else
7107 return devlink_param_driver_verify(param);
7108}
7109
7110static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307111 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307112 struct list_head *param_list,
7113 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307114 size_t params_count,
7115 enum devlink_command reg_cmd,
7116 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307117{
7118 const struct devlink_param *param = params;
7119 int i;
7120 int err;
7121
7122 mutex_lock(&devlink->lock);
7123 for (i = 0; i < params_count; i++, param++) {
7124 err = devlink_param_verify(param);
7125 if (err)
7126 goto rollback;
7127
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307128 err = devlink_param_register_one(devlink, port_index,
7129 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307130 if (err)
7131 goto rollback;
7132 }
7133
7134 mutex_unlock(&devlink->lock);
7135 return 0;
7136
7137rollback:
7138 if (!i)
7139 goto unlock;
7140 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307141 devlink_param_unregister_one(devlink, port_index, param_list,
7142 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307143unlock:
7144 mutex_unlock(&devlink->lock);
7145 return err;
7146}
7147
7148static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307149 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307150 struct list_head *param_list,
7151 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307152 size_t params_count,
7153 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05307154{
7155 const struct devlink_param *param = params;
7156 int i;
7157
7158 mutex_lock(&devlink->lock);
7159 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307160 devlink_param_unregister_one(devlink, 0, param_list, param,
7161 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307162 mutex_unlock(&devlink->lock);
7163}
7164
Moshe Shemesheabaef12018-07-04 14:30:28 +03007165/**
7166 * devlink_params_register - register configuration parameters
7167 *
7168 * @devlink: devlink
7169 * @params: configuration parameters array
7170 * @params_count: number of parameters provided
7171 *
7172 * Register the configuration parameters supported by the driver.
7173 */
7174int devlink_params_register(struct devlink *devlink,
7175 const struct devlink_param *params,
7176 size_t params_count)
7177{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307178 return __devlink_params_register(devlink, 0, &devlink->param_list,
7179 params, params_count,
7180 DEVLINK_CMD_PARAM_NEW,
7181 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007182}
7183EXPORT_SYMBOL_GPL(devlink_params_register);
7184
7185/**
7186 * devlink_params_unregister - unregister configuration parameters
7187 * @devlink: devlink
7188 * @params: configuration parameters to unregister
7189 * @params_count: number of parameters provided
7190 */
7191void devlink_params_unregister(struct devlink *devlink,
7192 const struct devlink_param *params,
7193 size_t params_count)
7194{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307195 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
7196 params, params_count,
7197 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03007198}
7199EXPORT_SYMBOL_GPL(devlink_params_unregister);
7200
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007201/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00007202 * devlink_params_publish - publish configuration parameters
7203 *
7204 * @devlink: devlink
7205 *
7206 * Publish previously registered configuration parameters.
7207 */
7208void devlink_params_publish(struct devlink *devlink)
7209{
7210 struct devlink_param_item *param_item;
7211
7212 list_for_each_entry(param_item, &devlink->param_list, list) {
7213 if (param_item->published)
7214 continue;
7215 param_item->published = true;
7216 devlink_param_notify(devlink, 0, param_item,
7217 DEVLINK_CMD_PARAM_NEW);
7218 }
7219}
7220EXPORT_SYMBOL_GPL(devlink_params_publish);
7221
7222/**
7223 * devlink_params_unpublish - unpublish configuration parameters
7224 *
7225 * @devlink: devlink
7226 *
7227 * Unpublish previously registered configuration parameters.
7228 */
7229void devlink_params_unpublish(struct devlink *devlink)
7230{
7231 struct devlink_param_item *param_item;
7232
7233 list_for_each_entry(param_item, &devlink->param_list, list) {
7234 if (!param_item->published)
7235 continue;
7236 param_item->published = false;
7237 devlink_param_notify(devlink, 0, param_item,
7238 DEVLINK_CMD_PARAM_DEL);
7239 }
7240}
7241EXPORT_SYMBOL_GPL(devlink_params_unpublish);
7242
7243/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05307244 * devlink_port_params_register - register port configuration parameters
7245 *
7246 * @devlink_port: devlink port
7247 * @params: configuration parameters array
7248 * @params_count: number of parameters provided
7249 *
7250 * Register the configuration parameters supported by the port.
7251 */
7252int devlink_port_params_register(struct devlink_port *devlink_port,
7253 const struct devlink_param *params,
7254 size_t params_count)
7255{
7256 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307257 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307258 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307259 params_count,
7260 DEVLINK_CMD_PORT_PARAM_NEW,
7261 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307262}
7263EXPORT_SYMBOL_GPL(devlink_port_params_register);
7264
7265/**
7266 * devlink_port_params_unregister - unregister port configuration
7267 * parameters
7268 *
7269 * @devlink_port: devlink port
7270 * @params: configuration parameters array
7271 * @params_count: number of parameters provided
7272 */
7273void devlink_port_params_unregister(struct devlink_port *devlink_port,
7274 const struct devlink_param *params,
7275 size_t params_count)
7276{
7277 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307278 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05307279 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307280 params, params_count,
7281 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05307282}
7283EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
7284
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307285static int
7286__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
7287 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007288{
7289 struct devlink_param_item *param_item;
7290
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307291 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007292 if (!param_item)
7293 return -EINVAL;
7294
7295 if (!param_item->driverinit_value_valid ||
7296 !devlink_param_cmode_is_supported(param_item->param,
7297 DEVLINK_PARAM_CMODE_DRIVERINIT))
7298 return -EOPNOTSUPP;
7299
Moshe Shemesh12765342018-10-10 16:09:26 +03007300 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7301 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
7302 else
7303 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007304
7305 return 0;
7306}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307307
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307308static int
7309__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307310 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307311 struct list_head *param_list, u32 param_id,
7312 union devlink_param_value init_val,
7313 enum devlink_command cmd)
7314{
7315 struct devlink_param_item *param_item;
7316
7317 param_item = devlink_param_find_by_id(param_list, param_id);
7318 if (!param_item)
7319 return -EINVAL;
7320
7321 if (!devlink_param_cmode_is_supported(param_item->param,
7322 DEVLINK_PARAM_CMODE_DRIVERINIT))
7323 return -EOPNOTSUPP;
7324
7325 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
7326 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
7327 else
7328 param_item->driverinit_value = init_val;
7329 param_item->driverinit_value_valid = true;
7330
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307331 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307332 return 0;
7333}
7334
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307335/**
7336 * devlink_param_driverinit_value_get - get configuration parameter
7337 * value for driver initializing
7338 *
7339 * @devlink: devlink
7340 * @param_id: parameter ID
7341 * @init_val: value of parameter in driverinit configuration mode
7342 *
7343 * This function should be used by the driver to get driverinit
7344 * configuration for initialization after reload command.
7345 */
7346int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
7347 union devlink_param_value *init_val)
7348{
Jiri Pirko97691062019-09-12 10:49:45 +02007349 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307350 return -EOPNOTSUPP;
7351
7352 return __devlink_param_driverinit_value_get(&devlink->param_list,
7353 param_id, init_val);
7354}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007355EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
7356
7357/**
7358 * devlink_param_driverinit_value_set - set value of configuration
7359 * parameter for driverinit
7360 * configuration mode
7361 *
7362 * @devlink: devlink
7363 * @param_id: parameter ID
7364 * @init_val: value of parameter to set for driverinit configuration mode
7365 *
7366 * This function should be used by the driver to set driverinit
7367 * configuration mode default value.
7368 */
7369int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
7370 union devlink_param_value init_val)
7371{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307372 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307373 &devlink->param_list,
7374 param_id, init_val,
7375 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03007376}
7377EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
7378
Moshe Shemeshea601e12018-07-04 14:30:32 +03007379/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307380 * devlink_port_param_driverinit_value_get - get configuration parameter
7381 * value for driver initializing
7382 *
7383 * @devlink_port: devlink_port
7384 * @param_id: parameter ID
7385 * @init_val: value of parameter in driverinit configuration mode
7386 *
7387 * This function should be used by the driver to get driverinit
7388 * configuration for initialization after reload command.
7389 */
7390int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
7391 u32 param_id,
7392 union devlink_param_value *init_val)
7393{
7394 struct devlink *devlink = devlink_port->devlink;
7395
Jiri Pirko97691062019-09-12 10:49:45 +02007396 if (!devlink_reload_supported(devlink))
Vasundhara Volamffd19b92019-01-28 18:00:23 +05307397 return -EOPNOTSUPP;
7398
7399 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
7400 param_id, init_val);
7401}
7402EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
7403
7404/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307405 * devlink_port_param_driverinit_value_set - set value of configuration
7406 * parameter for driverinit
7407 * configuration mode
7408 *
7409 * @devlink_port: devlink_port
7410 * @param_id: parameter ID
7411 * @init_val: value of parameter to set for driverinit configuration mode
7412 *
7413 * This function should be used by the driver to set driverinit
7414 * configuration mode default value.
7415 */
7416int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
7417 u32 param_id,
7418 union devlink_param_value init_val)
7419{
7420 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307421 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307422 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307423 param_id, init_val,
7424 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05307425}
7426EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
7427
7428/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03007429 * devlink_param_value_changed - notify devlink on a parameter's value
7430 * change. Should be called by the driver
7431 * right after the change.
7432 *
7433 * @devlink: devlink
7434 * @param_id: parameter ID
7435 *
7436 * This function should be used by the driver to notify devlink on value
7437 * change, excluding driverinit configuration mode.
7438 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03007439 */
7440void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
7441{
7442 struct devlink_param_item *param_item;
7443
7444 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
7445 WARN_ON(!param_item);
7446
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307447 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03007448}
7449EXPORT_SYMBOL_GPL(devlink_param_value_changed);
7450
Alex Veskerb16ebe92018-07-12 15:13:08 +03007451/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05307452 * devlink_port_param_value_changed - notify devlink on a parameter's value
7453 * change. Should be called by the driver
7454 * right after the change.
7455 *
7456 * @devlink_port: devlink_port
7457 * @param_id: parameter ID
7458 *
7459 * This function should be used by the driver to notify devlink on value
7460 * change, excluding driverinit configuration mode.
7461 * For driverinit configuration mode driver should use the function
7462 * devlink_port_param_driverinit_value_set() instead.
7463 */
7464void devlink_port_param_value_changed(struct devlink_port *devlink_port,
7465 u32 param_id)
7466{
7467 struct devlink_param_item *param_item;
7468
7469 param_item = devlink_param_find_by_id(&devlink_port->param_list,
7470 param_id);
7471 WARN_ON(!param_item);
7472
7473 devlink_param_notify(devlink_port->devlink, devlink_port->index,
7474 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
7475}
7476EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
7477
7478/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03007479 * devlink_param_value_str_fill - Safely fill-up the string preventing
7480 * from overflow of the preallocated buffer
7481 *
7482 * @dst_val: destination devlink_param_value
7483 * @src: source buffer
7484 */
7485void devlink_param_value_str_fill(union devlink_param_value *dst_val,
7486 const char *src)
7487{
7488 size_t len;
7489
7490 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
7491 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
7492}
7493EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
7494
7495/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03007496 * devlink_region_create - create a new address region
7497 *
7498 * @devlink: devlink
7499 * @region_name: region name
7500 * @region_max_snapshots: Maximum supported number of snapshots for region
7501 * @region_size: size of region
7502 */
7503struct devlink_region *devlink_region_create(struct devlink *devlink,
7504 const char *region_name,
7505 u32 region_max_snapshots,
7506 u64 region_size)
7507{
7508 struct devlink_region *region;
7509 int err = 0;
7510
7511 mutex_lock(&devlink->lock);
7512
7513 if (devlink_region_get_by_name(devlink, region_name)) {
7514 err = -EEXIST;
7515 goto unlock;
7516 }
7517
7518 region = kzalloc(sizeof(*region), GFP_KERNEL);
7519 if (!region) {
7520 err = -ENOMEM;
7521 goto unlock;
7522 }
7523
7524 region->devlink = devlink;
7525 region->max_snapshots = region_max_snapshots;
7526 region->name = region_name;
7527 region->size = region_size;
7528 INIT_LIST_HEAD(&region->snapshot_list);
7529 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03007530 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007531
7532 mutex_unlock(&devlink->lock);
7533 return region;
7534
7535unlock:
7536 mutex_unlock(&devlink->lock);
7537 return ERR_PTR(err);
7538}
7539EXPORT_SYMBOL_GPL(devlink_region_create);
7540
7541/**
7542 * devlink_region_destroy - destroy address region
7543 *
7544 * @region: devlink region to destroy
7545 */
7546void devlink_region_destroy(struct devlink_region *region)
7547{
7548 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03007549 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03007550
7551 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03007552
7553 /* Free all snapshots of region */
7554 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
Jiri Pirko92b49822019-08-12 14:28:31 +02007555 devlink_region_snapshot_del(region, snapshot);
Alex Veskerd7e52722018-07-12 15:13:10 +03007556
Alex Veskerb16ebe92018-07-12 15:13:08 +03007557 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03007558
7559 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03007560 mutex_unlock(&devlink->lock);
7561 kfree(region);
7562}
7563EXPORT_SYMBOL_GPL(devlink_region_destroy);
7564
Alex Veskerccadfa42018-07-12 15:13:09 +03007565/**
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007566 * devlink_region_snapshot_id_get - get snapshot ID
Alex Veskerccadfa42018-07-12 15:13:09 +03007567 *
7568 * This callback should be called when adding a new snapshot,
7569 * Driver should use the same id for multiple snapshots taken
7570 * on multiple regions at the same time/by the same trigger.
7571 *
7572 * @devlink: devlink
7573 */
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007574u32 devlink_region_snapshot_id_get(struct devlink *devlink)
Alex Veskerccadfa42018-07-12 15:13:09 +03007575{
7576 u32 id;
7577
7578 mutex_lock(&devlink->lock);
7579 id = ++devlink->snapshot_id;
7580 mutex_unlock(&devlink->lock);
7581
7582 return id;
7583}
Jacob Kellerb0efcae2020-01-09 11:08:20 -08007584EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
Alex Veskerccadfa42018-07-12 15:13:09 +03007585
Alex Veskerd7e52722018-07-12 15:13:10 +03007586/**
7587 * devlink_region_snapshot_create - create a new snapshot
7588 * This will add a new snapshot of a region. The snapshot
7589 * will be stored on the region struct and can be accessed
7590 * from devlink. This is useful for future analyses of snapshots.
7591 * Multiple snapshots can be created on a region.
7592 * The @snapshot_id should be obtained using the getter function.
7593 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08007594 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03007595 * @data: snapshot data
7596 * @snapshot_id: snapshot id to be created
7597 * @data_destructor: pointer to destructor function to free data
7598 */
Jiri Pirko3a5e5232019-08-09 15:27:15 +02007599int devlink_region_snapshot_create(struct devlink_region *region,
Alex Veskerd7e52722018-07-12 15:13:10 +03007600 u8 *data, u32 snapshot_id,
7601 devlink_snapshot_data_dest_t *data_destructor)
7602{
7603 struct devlink *devlink = region->devlink;
7604 struct devlink_snapshot *snapshot;
7605 int err;
7606
7607 mutex_lock(&devlink->lock);
7608
7609 /* check if region can hold one more snapshot */
7610 if (region->cur_snapshots == region->max_snapshots) {
7611 err = -ENOMEM;
7612 goto unlock;
7613 }
7614
7615 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
7616 err = -EEXIST;
7617 goto unlock;
7618 }
7619
7620 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
7621 if (!snapshot) {
7622 err = -ENOMEM;
7623 goto unlock;
7624 }
7625
7626 snapshot->id = snapshot_id;
7627 snapshot->region = region;
7628 snapshot->data = data;
Alex Veskerd7e52722018-07-12 15:13:10 +03007629 snapshot->data_destructor = data_destructor;
7630
7631 list_add_tail(&snapshot->list, &region->snapshot_list);
7632
7633 region->cur_snapshots++;
7634
Alex Vesker866319b2018-07-12 15:13:13 +03007635 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03007636 mutex_unlock(&devlink->lock);
7637 return 0;
7638
7639unlock:
7640 mutex_unlock(&devlink->lock);
7641 return err;
7642}
7643EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
7644
Ido Schimmel0f420b62019-08-17 16:28:17 +03007645#define DEVLINK_TRAP(_id, _type) \
7646 { \
7647 .type = DEVLINK_TRAP_TYPE_##_type, \
7648 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
7649 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
7650 }
7651
7652static const struct devlink_trap devlink_trap_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007653 DEVLINK_TRAP(SMAC_MC, DROP),
7654 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
7655 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
7656 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
7657 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
7658 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
7659 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
7660 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
7661 DEVLINK_TRAP(TAIL_DROP, DROP),
Amit Cohen6896cc42019-11-07 18:42:09 +02007662 DEVLINK_TRAP(NON_IP_PACKET, DROP),
7663 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
7664 DEVLINK_TRAP(DIP_LB, DROP),
7665 DEVLINK_TRAP(SIP_MC, DROP),
7666 DEVLINK_TRAP(SIP_LB, DROP),
7667 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
7668 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
7669 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
7670 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
Amit Cohen3b063ae2019-11-07 18:42:14 +02007671 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
7672 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
7673 DEVLINK_TRAP(RPF, EXCEPTION),
7674 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
7675 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
7676 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007677};
7678
7679#define DEVLINK_TRAP_GROUP(_id) \
7680 { \
7681 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
7682 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
7683 }
7684
7685static const struct devlink_trap_group devlink_trap_group_generic[] = {
Ido Schimmel391203a2019-08-17 16:28:18 +03007686 DEVLINK_TRAP_GROUP(L2_DROPS),
7687 DEVLINK_TRAP_GROUP(L3_DROPS),
7688 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
Ido Schimmel0f420b62019-08-17 16:28:17 +03007689};
7690
7691static int devlink_trap_generic_verify(const struct devlink_trap *trap)
7692{
7693 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
7694 return -EINVAL;
7695
7696 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
7697 return -EINVAL;
7698
7699 if (trap->type != devlink_trap_generic[trap->id].type)
7700 return -EINVAL;
7701
7702 return 0;
7703}
7704
7705static int devlink_trap_driver_verify(const struct devlink_trap *trap)
7706{
7707 int i;
7708
7709 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
7710 return -EINVAL;
7711
7712 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
7713 if (!strcmp(trap->name, devlink_trap_generic[i].name))
7714 return -EEXIST;
7715 }
7716
7717 return 0;
7718}
7719
7720static int devlink_trap_verify(const struct devlink_trap *trap)
7721{
7722 if (!trap || !trap->name || !trap->group.name)
7723 return -EINVAL;
7724
7725 if (trap->generic)
7726 return devlink_trap_generic_verify(trap);
7727 else
7728 return devlink_trap_driver_verify(trap);
7729}
7730
7731static int
7732devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
7733{
7734 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7735 return -EINVAL;
7736
7737 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
7738 return -EINVAL;
7739
7740 return 0;
7741}
7742
7743static int
7744devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
7745{
7746 int i;
7747
7748 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
7749 return -EINVAL;
7750
7751 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
7752 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
7753 return -EEXIST;
7754 }
7755
7756 return 0;
7757}
7758
7759static int devlink_trap_group_verify(const struct devlink_trap_group *group)
7760{
7761 if (group->generic)
7762 return devlink_trap_group_generic_verify(group);
7763 else
7764 return devlink_trap_group_driver_verify(group);
7765}
7766
7767static void
7768devlink_trap_group_notify(struct devlink *devlink,
7769 const struct devlink_trap_group_item *group_item,
7770 enum devlink_command cmd)
7771{
7772 struct sk_buff *msg;
7773 int err;
7774
7775 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
7776 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
7777
7778 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7779 if (!msg)
7780 return;
7781
7782 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
7783 0);
7784 if (err) {
7785 nlmsg_free(msg);
7786 return;
7787 }
7788
7789 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7790 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7791}
7792
7793static struct devlink_trap_group_item *
7794devlink_trap_group_item_create(struct devlink *devlink,
7795 const struct devlink_trap_group *group)
7796{
7797 struct devlink_trap_group_item *group_item;
7798 int err;
7799
7800 err = devlink_trap_group_verify(group);
7801 if (err)
7802 return ERR_PTR(err);
7803
7804 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
7805 if (!group_item)
7806 return ERR_PTR(-ENOMEM);
7807
7808 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7809 if (!group_item->stats) {
7810 err = -ENOMEM;
7811 goto err_stats_alloc;
7812 }
7813
7814 group_item->group = group;
7815 refcount_set(&group_item->refcount, 1);
7816
7817 if (devlink->ops->trap_group_init) {
7818 err = devlink->ops->trap_group_init(devlink, group);
7819 if (err)
7820 goto err_group_init;
7821 }
7822
7823 list_add_tail(&group_item->list, &devlink->trap_group_list);
7824 devlink_trap_group_notify(devlink, group_item,
7825 DEVLINK_CMD_TRAP_GROUP_NEW);
7826
7827 return group_item;
7828
7829err_group_init:
7830 free_percpu(group_item->stats);
7831err_stats_alloc:
7832 kfree(group_item);
7833 return ERR_PTR(err);
7834}
7835
7836static void
7837devlink_trap_group_item_destroy(struct devlink *devlink,
7838 struct devlink_trap_group_item *group_item)
7839{
7840 devlink_trap_group_notify(devlink, group_item,
7841 DEVLINK_CMD_TRAP_GROUP_DEL);
7842 list_del(&group_item->list);
7843 free_percpu(group_item->stats);
7844 kfree(group_item);
7845}
7846
7847static struct devlink_trap_group_item *
7848devlink_trap_group_item_get(struct devlink *devlink,
7849 const struct devlink_trap_group *group)
7850{
7851 struct devlink_trap_group_item *group_item;
7852
7853 group_item = devlink_trap_group_item_lookup(devlink, group->name);
7854 if (group_item) {
7855 refcount_inc(&group_item->refcount);
7856 return group_item;
7857 }
7858
7859 return devlink_trap_group_item_create(devlink, group);
7860}
7861
7862static void
7863devlink_trap_group_item_put(struct devlink *devlink,
7864 struct devlink_trap_group_item *group_item)
7865{
7866 if (!refcount_dec_and_test(&group_item->refcount))
7867 return;
7868
7869 devlink_trap_group_item_destroy(devlink, group_item);
7870}
7871
7872static int
7873devlink_trap_item_group_link(struct devlink *devlink,
7874 struct devlink_trap_item *trap_item)
7875{
7876 struct devlink_trap_group_item *group_item;
7877
7878 group_item = devlink_trap_group_item_get(devlink,
7879 &trap_item->trap->group);
7880 if (IS_ERR(group_item))
7881 return PTR_ERR(group_item);
7882
7883 trap_item->group_item = group_item;
7884
7885 return 0;
7886}
7887
7888static void
7889devlink_trap_item_group_unlink(struct devlink *devlink,
7890 struct devlink_trap_item *trap_item)
7891{
7892 devlink_trap_group_item_put(devlink, trap_item->group_item);
7893}
7894
7895static void devlink_trap_notify(struct devlink *devlink,
7896 const struct devlink_trap_item *trap_item,
7897 enum devlink_command cmd)
7898{
7899 struct sk_buff *msg;
7900 int err;
7901
7902 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
7903 cmd != DEVLINK_CMD_TRAP_DEL);
7904
7905 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7906 if (!msg)
7907 return;
7908
7909 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
7910 if (err) {
7911 nlmsg_free(msg);
7912 return;
7913 }
7914
7915 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
7916 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7917}
7918
7919static int
7920devlink_trap_register(struct devlink *devlink,
7921 const struct devlink_trap *trap, void *priv)
7922{
7923 struct devlink_trap_item *trap_item;
7924 int err;
7925
7926 if (devlink_trap_item_lookup(devlink, trap->name))
7927 return -EEXIST;
7928
7929 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
7930 if (!trap_item)
7931 return -ENOMEM;
7932
7933 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
7934 if (!trap_item->stats) {
7935 err = -ENOMEM;
7936 goto err_stats_alloc;
7937 }
7938
7939 trap_item->trap = trap;
7940 trap_item->action = trap->init_action;
7941 trap_item->priv = priv;
7942
7943 err = devlink_trap_item_group_link(devlink, trap_item);
7944 if (err)
7945 goto err_group_link;
7946
7947 err = devlink->ops->trap_init(devlink, trap, trap_item);
7948 if (err)
7949 goto err_trap_init;
7950
7951 list_add_tail(&trap_item->list, &devlink->trap_list);
7952 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
7953
7954 return 0;
7955
7956err_trap_init:
7957 devlink_trap_item_group_unlink(devlink, trap_item);
7958err_group_link:
7959 free_percpu(trap_item->stats);
7960err_stats_alloc:
7961 kfree(trap_item);
7962 return err;
7963}
7964
7965static void devlink_trap_unregister(struct devlink *devlink,
7966 const struct devlink_trap *trap)
7967{
7968 struct devlink_trap_item *trap_item;
7969
7970 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7971 if (WARN_ON_ONCE(!trap_item))
7972 return;
7973
7974 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
7975 list_del(&trap_item->list);
7976 if (devlink->ops->trap_fini)
7977 devlink->ops->trap_fini(devlink, trap, trap_item);
7978 devlink_trap_item_group_unlink(devlink, trap_item);
7979 free_percpu(trap_item->stats);
7980 kfree(trap_item);
7981}
7982
7983static void devlink_trap_disable(struct devlink *devlink,
7984 const struct devlink_trap *trap)
7985{
7986 struct devlink_trap_item *trap_item;
7987
7988 trap_item = devlink_trap_item_lookup(devlink, trap->name);
7989 if (WARN_ON_ONCE(!trap_item))
7990 return;
7991
7992 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP);
7993 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
7994}
7995
7996/**
7997 * devlink_traps_register - Register packet traps with devlink.
7998 * @devlink: devlink.
7999 * @traps: Packet traps.
8000 * @traps_count: Count of provided packet traps.
8001 * @priv: Driver private information.
8002 *
8003 * Return: Non-zero value on failure.
8004 */
8005int devlink_traps_register(struct devlink *devlink,
8006 const struct devlink_trap *traps,
8007 size_t traps_count, void *priv)
8008{
8009 int i, err;
8010
8011 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
8012 return -EINVAL;
8013
8014 mutex_lock(&devlink->lock);
8015 for (i = 0; i < traps_count; i++) {
8016 const struct devlink_trap *trap = &traps[i];
8017
8018 err = devlink_trap_verify(trap);
8019 if (err)
8020 goto err_trap_verify;
8021
8022 err = devlink_trap_register(devlink, trap, priv);
8023 if (err)
8024 goto err_trap_register;
8025 }
8026 mutex_unlock(&devlink->lock);
8027
8028 return 0;
8029
8030err_trap_register:
8031err_trap_verify:
8032 for (i--; i >= 0; i--)
8033 devlink_trap_unregister(devlink, &traps[i]);
8034 mutex_unlock(&devlink->lock);
8035 return err;
8036}
8037EXPORT_SYMBOL_GPL(devlink_traps_register);
8038
8039/**
8040 * devlink_traps_unregister - Unregister packet traps from devlink.
8041 * @devlink: devlink.
8042 * @traps: Packet traps.
8043 * @traps_count: Count of provided packet traps.
8044 */
8045void devlink_traps_unregister(struct devlink *devlink,
8046 const struct devlink_trap *traps,
8047 size_t traps_count)
8048{
8049 int i;
8050
8051 mutex_lock(&devlink->lock);
8052 /* Make sure we do not have any packets in-flight while unregistering
8053 * traps by disabling all of them and waiting for a grace period.
8054 */
8055 for (i = traps_count - 1; i >= 0; i--)
8056 devlink_trap_disable(devlink, &traps[i]);
8057 synchronize_rcu();
8058 for (i = traps_count - 1; i >= 0; i--)
8059 devlink_trap_unregister(devlink, &traps[i]);
8060 mutex_unlock(&devlink->lock);
8061}
8062EXPORT_SYMBOL_GPL(devlink_traps_unregister);
8063
8064static void
8065devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
8066 size_t skb_len)
8067{
8068 struct devlink_stats *stats;
8069
8070 stats = this_cpu_ptr(trap_stats);
8071 u64_stats_update_begin(&stats->syncp);
8072 stats->rx_bytes += skb_len;
8073 stats->rx_packets++;
8074 u64_stats_update_end(&stats->syncp);
8075}
8076
8077static void
8078devlink_trap_report_metadata_fill(struct net_dm_hw_metadata *hw_metadata,
8079 const struct devlink_trap_item *trap_item,
8080 struct devlink_port *in_devlink_port)
8081{
8082 struct devlink_trap_group_item *group_item = trap_item->group_item;
8083
8084 hw_metadata->trap_group_name = group_item->group->name;
8085 hw_metadata->trap_name = trap_item->trap->name;
8086
8087 spin_lock(&in_devlink_port->type_lock);
8088 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
8089 hw_metadata->input_dev = in_devlink_port->type_dev;
8090 spin_unlock(&in_devlink_port->type_lock);
8091}
8092
8093/**
8094 * devlink_trap_report - Report trapped packet to drop monitor.
8095 * @devlink: devlink.
8096 * @skb: Trapped packet.
8097 * @trap_ctx: Trap context.
8098 * @in_devlink_port: Input devlink port.
8099 */
8100void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
8101 void *trap_ctx, struct devlink_port *in_devlink_port)
8102{
8103 struct devlink_trap_item *trap_item = trap_ctx;
8104 struct net_dm_hw_metadata hw_metadata = {};
8105
8106 devlink_trap_stats_update(trap_item->stats, skb->len);
8107 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
8108
8109 devlink_trap_report_metadata_fill(&hw_metadata, trap_item,
8110 in_devlink_port);
8111 net_dm_hw_report(skb, &hw_metadata);
8112}
8113EXPORT_SYMBOL_GPL(devlink_trap_report);
8114
8115/**
8116 * devlink_trap_ctx_priv - Trap context to driver private information.
8117 * @trap_ctx: Trap context.
8118 *
8119 * Return: Driver private information passed during registration.
8120 */
8121void *devlink_trap_ctx_priv(void *trap_ctx)
8122{
8123 struct devlink_trap_item *trap_item = trap_ctx;
8124
8125 return trap_item->priv;
8126}
8127EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
8128
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008129static void __devlink_compat_running_version(struct devlink *devlink,
8130 char *buf, size_t len)
8131{
8132 const struct nlattr *nlattr;
8133 struct devlink_info_req req;
8134 struct sk_buff *msg;
8135 int rem, err;
8136
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008137 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8138 if (!msg)
8139 return;
8140
8141 req.msg = msg;
8142 err = devlink->ops->info_get(devlink, &req, NULL);
8143 if (err)
8144 goto free_msg;
8145
8146 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
8147 const struct nlattr *kv;
8148 int rem_kv;
8149
8150 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
8151 continue;
8152
8153 nla_for_each_nested(kv, nlattr, rem_kv) {
8154 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
8155 continue;
8156
8157 strlcat(buf, nla_data(kv), len);
8158 strlcat(buf, " ", len);
8159 }
8160 }
8161free_msg:
8162 nlmsg_free(msg);
8163}
8164
8165void devlink_compat_running_version(struct net_device *dev,
8166 char *buf, size_t len)
8167{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008168 struct devlink *devlink;
8169
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008170 dev_hold(dev);
8171 rtnl_unlock();
8172
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008173 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08008174 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008175 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008176
8177 mutex_lock(&devlink->lock);
8178 __devlink_compat_running_version(devlink, buf, len);
8179 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008180
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008181out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008182 rtnl_lock();
8183 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08008184}
8185
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008186int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
8187{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008188 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008189 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008190
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008191 dev_hold(dev);
8192 rtnl_unlock();
8193
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008194 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008195 if (!devlink || !devlink->ops->flash_update) {
8196 ret = -EOPNOTSUPP;
8197 goto out;
8198 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008199
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008200 mutex_lock(&devlink->lock);
8201 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
8202 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008203
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01008204out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08008205 rtnl_lock();
8206 dev_put(dev);
8207
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08008208 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08008209}
8210
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01008211int devlink_compat_phys_port_name_get(struct net_device *dev,
8212 char *name, size_t len)
8213{
8214 struct devlink_port *devlink_port;
8215
8216 /* RTNL mutex is held here which ensures that devlink_port
8217 * instance cannot disappear in the middle. No need to take
8218 * any devlink lock as only permanent values are accessed.
8219 */
8220 ASSERT_RTNL();
8221
8222 devlink_port = netdev_to_devlink_port(dev);
8223 if (!devlink_port)
8224 return -EOPNOTSUPP;
8225
8226 return __devlink_port_phys_port_name_get(devlink_port, name, len);
8227}
8228
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008229int devlink_compat_switch_id_get(struct net_device *dev,
8230 struct netdev_phys_item_id *ppid)
8231{
8232 struct devlink_port *devlink_port;
8233
Vlad Buslov043b8412019-08-12 20:02:02 +03008234 /* Caller must hold RTNL mutex or reference to dev, which ensures that
8235 * devlink_port instance cannot disappear in the middle. No need to take
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008236 * any devlink lock as only permanent values are accessed.
8237 */
Jiri Pirko7e1146e2019-04-03 14:24:17 +02008238 devlink_port = netdev_to_devlink_port(dev);
8239 if (!devlink_port || !devlink_port->attrs.switch_port)
8240 return -EOPNOTSUPP;
8241
8242 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
8243
8244 return 0;
8245}
8246
Jiri Pirko070c63f2019-10-03 11:49:39 +02008247static void __net_exit devlink_pernet_pre_exit(struct net *net)
8248{
8249 struct devlink *devlink;
8250 int err;
8251
8252 /* In case network namespace is getting destroyed, reload
8253 * all devlink instances from this namespace into init_net.
8254 */
8255 mutex_lock(&devlink_mutex);
8256 list_for_each_entry(devlink, &devlink_list, list) {
8257 if (net_eq(devlink_net(devlink), net)) {
8258 if (WARN_ON(!devlink_reload_supported(devlink)))
8259 continue;
8260 err = devlink_reload(devlink, &init_net, NULL);
Jiri Pirkoa0c76342019-11-08 21:42:43 +01008261 if (err && err != -EOPNOTSUPP)
Jiri Pirko070c63f2019-10-03 11:49:39 +02008262 pr_warn("Failed to reload devlink instance into init_net\n");
8263 }
8264 }
8265 mutex_unlock(&devlink_mutex);
8266}
8267
8268static struct pernet_operations devlink_pernet_ops __net_initdata = {
8269 .pre_exit = devlink_pernet_pre_exit,
8270};
8271
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008272static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008273{
Jiri Pirko070c63f2019-10-03 11:49:39 +02008274 int err;
8275
8276 err = genl_register_family(&devlink_nl_family);
8277 if (err)
8278 goto out;
8279 err = register_pernet_subsys(&devlink_pernet_ops);
8280
8281out:
8282 WARN_ON(err);
8283 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008284}
8285
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08008286subsys_initcall(devlink_init);