]> git.proxmox.com Git - mirror_iproute2.git/blame - devlink/devlink.c
devlink: remove unused "jw" field
[mirror_iproute2.git] / devlink / devlink.c
CommitLineData
a3c4b484
JP
1/*
2 * devlink.c Devlink tool
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jiri Pirko <jiri@mellanox.com>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
f7995053 14#include <stdarg.h>
a3c4b484
JP
15#include <string.h>
16#include <stdbool.h>
17#include <unistd.h>
18#include <getopt.h>
19#include <limits.h>
20#include <errno.h>
8b4fbf0b 21#include <inttypes.h>
d0272f54
BS
22#include <sys/sysinfo.h>
23#define _LINUX_SYSINFO_H /* avoid collision with musl header */
a3c4b484
JP
24#include <linux/genetlink.h>
25#include <linux/devlink.h>
26#include <libmnl/libmnl.h>
b2947f8b 27#include <netinet/ether.h>
9b13cddf 28#include <sys/types.h>
a3c4b484
JP
29
30#include "SNAPSHOT.h"
31#include "list.h"
32#include "mnlg.h"
e3d0f0c0 33#include "json_writer.h"
afdc1194 34#include "utils.h"
29993df8 35#include "namespace.h"
a3c4b484 36
f57856fa
OG
37#define ESWITCH_MODE_LEGACY "legacy"
38#define ESWITCH_MODE_SWITCHDEV "switchdev"
6566ca8c
RD
39#define ESWITCH_INLINE_MODE_NONE "none"
40#define ESWITCH_INLINE_MODE_LINK "link"
41#define ESWITCH_INLINE_MODE_NETWORK "network"
42#define ESWITCH_INLINE_MODE_TRANSPORT "transport"
f57856fa 43
13925ae9
MS
44#define PARAM_CMODE_RUNTIME_STR "runtime"
45#define PARAM_CMODE_DRIVERINIT_STR "driverinit"
46#define PARAM_CMODE_PERMANENT_STR "permanent"
ae72e655 47#define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
13925ae9 48
2f1242ef
AL
49#define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
50#define HEALTH_REPORTER_STATE_ERROR_STR "error"
51#define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
52
844646a5 53static int g_new_line_count;
153c1a9b
AS
54static int g_indent_level;
55static bool g_indent_newline;
f7995053 56
153c1a9b
AS
57#define INDENT_STR_STEP 2
58#define INDENT_STR_MAXLEN 32
59static char g_indent_str[INDENT_STR_MAXLEN + 1] = "";
60
f7995053
SH
61static void __attribute__((format(printf, 1, 2)))
62pr_err(const char *fmt, ...)
63{
64 va_list ap;
65
66 va_start(ap, fmt);
67 vfprintf(stderr, fmt, ap);
68 va_end(ap);
69}
70
71static void __attribute__((format(printf, 1, 2)))
72pr_out(const char *fmt, ...)
73{
74 va_list ap;
75
76 if (g_indent_newline) {
77 printf("%s", g_indent_str);
78 g_indent_newline = false;
79 }
80 va_start(ap, fmt);
81 vprintf(fmt, ap);
82 va_end(ap);
83 g_new_line_count = 0;
84}
85
86static void __attribute__((format(printf, 2, 3)))
87pr_out_sp(unsigned int num, const char *fmt, ...)
88{
89 va_list ap;
90 int ret;
91
92 va_start(ap, fmt);
93 ret = vprintf(fmt, ap);
94 va_end(ap);
95
96 if (ret < num)
97 printf("%*s", num - ret, "");
98 g_new_line_count = 0; \
99}
100
9b13cddf
JP
101static void __attribute__((format(printf, 1, 2)))
102pr_out_tty(const char *fmt, ...)
103{
104 va_list ap;
105
106 if (!isatty(STDOUT_FILENO))
107 return;
108 va_start(ap, fmt);
109 vprintf(fmt, ap);
110 va_end(ap);
111}
112
153c1a9b
AS
113static void __pr_out_indent_inc(void)
114{
115 if (g_indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN)
116 return;
117 g_indent_level += INDENT_STR_STEP;
118 memset(g_indent_str, ' ', sizeof(g_indent_str));
119 g_indent_str[g_indent_level] = '\0';
120}
121
122static void __pr_out_indent_dec(void)
123{
124 if (g_indent_level - INDENT_STR_STEP < 0)
125 return;
126 g_indent_level -= INDENT_STR_STEP;
127 g_indent_str[g_indent_level] = '\0';
128}
129
130static void __pr_out_newline(void)
131{
844646a5
AS
132 if (g_new_line_count < 1) {
133 pr_out("\n");
134 g_indent_newline = true;
135 }
136 g_new_line_count++;
153c1a9b
AS
137}
138
a3c4b484
JP
139static int _mnlg_socket_recv_run(struct mnlg_socket *nlg,
140 mnl_cb_t data_cb, void *data)
141{
142 int err;
143
144 err = mnlg_socket_recv_run(nlg, data_cb, data);
145 if (err < 0) {
146 pr_err("devlink answers: %s\n", strerror(errno));
147 return -errno;
148 }
149 return 0;
150}
151
9b13cddf
JP
152static int _mnlg_socket_send(struct mnlg_socket *nlg,
153 const struct nlmsghdr *nlh)
a3c4b484
JP
154{
155 int err;
156
157 err = mnlg_socket_send(nlg, nlh);
158 if (err < 0) {
159 pr_err("Failed to call mnlg_socket_send\n");
160 return -errno;
161 }
9b13cddf
JP
162 return 0;
163}
164
165static int _mnlg_socket_sndrcv(struct mnlg_socket *nlg,
166 const struct nlmsghdr *nlh,
167 mnl_cb_t data_cb, void *data)
168{
169 int err;
170
171 err = _mnlg_socket_send(nlg, nlh);
172 if (err)
173 return err;
a3c4b484
JP
174 return _mnlg_socket_recv_run(nlg, data_cb, data);
175}
176
177static int _mnlg_socket_group_add(struct mnlg_socket *nlg,
178 const char *group_name)
179{
180 int err;
181
182 err = mnlg_socket_group_add(nlg, group_name);
183 if (err < 0) {
184 pr_err("Failed to call mnlg_socket_group_add\n");
185 return -errno;
186 }
187 return 0;
188}
189
190struct ifname_map {
191 struct list_head list;
192 char *bus_name;
193 char *dev_name;
194 uint32_t port_index;
195 char *ifname;
196};
197
198static struct ifname_map *ifname_map_alloc(const char *bus_name,
199 const char *dev_name,
200 uint32_t port_index,
201 const char *ifname)
202{
203 struct ifname_map *ifname_map;
204
205 ifname_map = calloc(1, sizeof(*ifname_map));
206 if (!ifname_map)
207 return NULL;
208 ifname_map->bus_name = strdup(bus_name);
209 ifname_map->dev_name = strdup(dev_name);
210 ifname_map->port_index = port_index;
211 ifname_map->ifname = strdup(ifname);
212 if (!ifname_map->bus_name || !ifname_map->dev_name ||
213 !ifname_map->ifname) {
214 free(ifname_map->ifname);
215 free(ifname_map->dev_name);
216 free(ifname_map->bus_name);
217 free(ifname_map);
218 return NULL;
219 }
220 return ifname_map;
221}
222
223static void ifname_map_free(struct ifname_map *ifname_map)
224{
225 free(ifname_map->ifname);
226 free(ifname_map->dev_name);
227 free(ifname_map->bus_name);
228 free(ifname_map);
229}
230
6563a6eb
JP
231#define DL_OPT_HANDLE BIT(0)
232#define DL_OPT_HANDLEP BIT(1)
233#define DL_OPT_PORT_TYPE BIT(2)
234#define DL_OPT_PORT_COUNT BIT(3)
e6d7367d
JP
235#define DL_OPT_SB BIT(4)
236#define DL_OPT_SB_POOL BIT(5)
237#define DL_OPT_SB_SIZE BIT(6)
238#define DL_OPT_SB_TYPE BIT(7)
239#define DL_OPT_SB_THTYPE BIT(8)
240#define DL_OPT_SB_TH BIT(9)
241#define DL_OPT_SB_TC BIT(10)
f57856fa 242#define DL_OPT_ESWITCH_MODE BIT(11)
6566ca8c 243#define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
153c1a9b
AS
244#define DL_OPT_DPIPE_TABLE_NAME BIT(13)
245#define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
d315b706 246#define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
8cd64409
AS
247#define DL_OPT_RESOURCE_PATH BIT(16)
248#define DL_OPT_RESOURCE_SIZE BIT(17)
13925ae9
MS
249#define DL_OPT_PARAM_NAME BIT(18)
250#define DL_OPT_PARAM_VALUE BIT(19)
251#define DL_OPT_PARAM_CMODE BIT(20)
8b4fbf0b
AV
252#define DL_OPT_HANDLE_REGION BIT(21)
253#define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
254#define DL_OPT_REGION_ADDRESS BIT(23)
255#define DL_OPT_REGION_LENGTH BIT(24)
d326d79c
JK
256#define DL_OPT_FLASH_FILE_NAME BIT(25)
257#define DL_OPT_FLASH_COMPONENT BIT(26)
2f1242ef 258#define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
4fb98f08
AC
259#define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
260#define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
a32692ac
DA
261#define DL_OPT_TRAP_NAME BIT(30)
262#define DL_OPT_TRAP_ACTION BIT(31)
263#define DL_OPT_TRAP_GROUP_NAME BIT(32)
08e8e1ca 264#define DL_OPT_NETNS BIT(33)
a66af556
IS
265#define DL_OPT_TRAP_POLICER_ID BIT(34)
266#define DL_OPT_TRAP_POLICER_RATE BIT(35)
267#define DL_OPT_TRAP_POLICER_BURST BIT(36)
6563a6eb
JP
268
269struct dl_opts {
b83220db 270 uint64_t present; /* flags of present items */
6563a6eb
JP
271 char *bus_name;
272 char *dev_name;
273 uint32_t port_index;
274 enum devlink_port_type port_type;
275 uint32_t port_count;
e6d7367d
JP
276 uint32_t sb_index;
277 uint16_t sb_pool_index;
278 uint32_t sb_pool_size;
279 enum devlink_sb_pool_type sb_pool_type;
280 enum devlink_sb_threshold_type sb_pool_thtype;
281 uint32_t sb_threshold;
282 uint16_t sb_tc_index;
f57856fa 283 enum devlink_eswitch_mode eswitch_mode;
6566ca8c 284 enum devlink_eswitch_inline_mode eswitch_inline_mode;
153c1a9b
AS
285 const char *dpipe_table_name;
286 bool dpipe_counters_enable;
d315b706 287 bool eswitch_encap_mode;
8cd64409 288 const char *resource_path;
c3f69bf9 289 uint64_t resource_size;
8cd64409
AS
290 uint32_t resource_id;
291 bool resource_id_valid;
13925ae9
MS
292 const char *param_name;
293 const char *param_value;
294 enum devlink_param_cmode cmode;
8b4fbf0b
AV
295 char *region_name;
296 uint32_t region_snapshot_id;
297 uint64_t region_address;
298 uint64_t region_length;
d326d79c
JK
299 const char *flash_file_name;
300 const char *flash_component;
2f1242ef 301 const char *reporter_name;
b18d8919
AL
302 uint64_t reporter_graceful_period;
303 bool reporter_auto_recover;
ef12d6da 304 const char *trap_name;
4ede9e9d 305 const char *trap_group_name;
ef12d6da 306 enum devlink_trap_action trap_action;
08e8e1ca
JP
307 bool netns_is_pid;
308 uint32_t netns;
a66af556
IS
309 uint32_t trap_policer_id;
310 uint64_t trap_policer_rate;
311 uint64_t trap_policer_burst;
6563a6eb
JP
312};
313
a3c4b484
JP
314struct dl {
315 struct mnlg_socket *nlg;
316 struct list_head ifname_map_list;
317 int argc;
318 char **argv;
43f35be4 319 bool no_nice_names;
6563a6eb 320 struct dl_opts opts;
e3d0f0c0
JP
321 bool json_output;
322 bool pretty_output;
153c1a9b 323 bool verbose;
ef12d6da 324 bool stats;
e3d0f0c0
JP
325 struct {
326 bool present;
327 char *bus_name;
328 char *dev_name;
329 uint32_t port_index;
330 } arr_last;
a3c4b484
JP
331};
332
333static int dl_argc(struct dl *dl)
334{
335 return dl->argc;
336}
337
338static char *dl_argv(struct dl *dl)
339{
340 if (dl_argc(dl) == 0)
341 return NULL;
342 return *dl->argv;
343}
344
345static void dl_arg_inc(struct dl *dl)
346{
347 if (dl_argc(dl) == 0)
348 return;
349 dl->argc--;
350 dl->argv++;
351}
352
9a0a2fcb
JK
353static void dl_arg_dec(struct dl *dl)
354{
355 dl->argc++;
356 dl->argv--;
357}
358
a3c4b484
JP
359static char *dl_argv_next(struct dl *dl)
360{
361 char *ret;
362
363 if (dl_argc(dl) == 0)
364 return NULL;
365
366 ret = *dl->argv;
367 dl_arg_inc(dl);
368 return ret;
369}
370
371static char *dl_argv_index(struct dl *dl, unsigned int index)
372{
373 if (index >= dl_argc(dl))
374 return NULL;
375 return dl->argv[index];
376}
377
378static int strcmpx(const char *str1, const char *str2)
379{
380 if (strlen(str1) > strlen(str2))
381 return -1;
382 return strncmp(str1, str2, strlen(str1));
383}
384
385static bool dl_argv_match(struct dl *dl, const char *pattern)
386{
387 if (dl_argc(dl) == 0)
388 return false;
389 return strcmpx(dl_argv(dl), pattern) == 0;
390}
391
392static bool dl_no_arg(struct dl *dl)
393{
394 return dl_argc(dl) == 0;
395}
396
56b725a4
AL
397static void __pr_out_indent_newline(struct dl *dl)
398{
399 if (!g_indent_newline && !dl->json_output)
400 pr_out(" ");
401}
402
3666eb0e
RD
403static void check_indent_newline(struct dl *dl)
404{
405 __pr_out_indent_newline(dl);
406
407 if (g_indent_newline && !is_json_context()) {
408 printf("%s", g_indent_str);
409 g_indent_newline = false;
410 }
411 g_new_line_count = 0;
412}
413
4f10cede
AS
414static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
415 [DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING,
416 [DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
417 [DEVLINK_ATTR_PORT_INDEX] = MNL_TYPE_U32,
418 [DEVLINK_ATTR_PORT_TYPE] = MNL_TYPE_U16,
419 [DEVLINK_ATTR_PORT_DESIRED_TYPE] = MNL_TYPE_U16,
420 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX] = MNL_TYPE_U32,
421 [DEVLINK_ATTR_PORT_NETDEV_NAME] = MNL_TYPE_NUL_STRING,
422 [DEVLINK_ATTR_PORT_IBDEV_NAME] = MNL_TYPE_NUL_STRING,
423 [DEVLINK_ATTR_SB_INDEX] = MNL_TYPE_U32,
424 [DEVLINK_ATTR_SB_SIZE] = MNL_TYPE_U32,
425 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] = MNL_TYPE_U16,
426 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] = MNL_TYPE_U16,
427 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT] = MNL_TYPE_U16,
428 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT] = MNL_TYPE_U16,
429 [DEVLINK_ATTR_SB_POOL_INDEX] = MNL_TYPE_U16,
430 [DEVLINK_ATTR_SB_POOL_TYPE] = MNL_TYPE_U8,
431 [DEVLINK_ATTR_SB_POOL_SIZE] = MNL_TYPE_U32,
432 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = MNL_TYPE_U8,
433 [DEVLINK_ATTR_SB_THRESHOLD] = MNL_TYPE_U32,
434 [DEVLINK_ATTR_SB_TC_INDEX] = MNL_TYPE_U16,
435 [DEVLINK_ATTR_SB_OCC_CUR] = MNL_TYPE_U32,
436 [DEVLINK_ATTR_SB_OCC_MAX] = MNL_TYPE_U32,
437 [DEVLINK_ATTR_ESWITCH_MODE] = MNL_TYPE_U16,
438 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = MNL_TYPE_U8,
d315b706 439 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = MNL_TYPE_U8,
153c1a9b
AS
440 [DEVLINK_ATTR_DPIPE_TABLES] = MNL_TYPE_NESTED,
441 [DEVLINK_ATTR_DPIPE_TABLE] = MNL_TYPE_NESTED,
442 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = MNL_TYPE_STRING,
443 [DEVLINK_ATTR_DPIPE_TABLE_SIZE] = MNL_TYPE_U64,
444 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES] = MNL_TYPE_NESTED,
445 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] = MNL_TYPE_NESTED,
446 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = MNL_TYPE_U8,
447 [DEVLINK_ATTR_DPIPE_ENTRIES] = MNL_TYPE_NESTED,
448 [DEVLINK_ATTR_DPIPE_ENTRY] = MNL_TYPE_NESTED,
449 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX] = MNL_TYPE_U64,
450 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] = MNL_TYPE_NESTED,
451 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES] = MNL_TYPE_NESTED,
452 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER] = MNL_TYPE_U64,
453 [DEVLINK_ATTR_DPIPE_MATCH] = MNL_TYPE_NESTED,
454 [DEVLINK_ATTR_DPIPE_MATCH_VALUE] = MNL_TYPE_NESTED,
455 [DEVLINK_ATTR_DPIPE_MATCH_TYPE] = MNL_TYPE_U32,
456 [DEVLINK_ATTR_DPIPE_ACTION] = MNL_TYPE_NESTED,
457 [DEVLINK_ATTR_DPIPE_ACTION_VALUE] = MNL_TYPE_NESTED,
458 [DEVLINK_ATTR_DPIPE_ACTION_TYPE] = MNL_TYPE_U32,
459 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING] = MNL_TYPE_U32,
460 [DEVLINK_ATTR_DPIPE_HEADERS] = MNL_TYPE_NESTED,
461 [DEVLINK_ATTR_DPIPE_HEADER] = MNL_TYPE_NESTED,
462 [DEVLINK_ATTR_DPIPE_HEADER_NAME] = MNL_TYPE_STRING,
463 [DEVLINK_ATTR_DPIPE_HEADER_ID] = MNL_TYPE_U32,
464 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS] = MNL_TYPE_NESTED,
465 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL] = MNL_TYPE_U8,
466 [DEVLINK_ATTR_DPIPE_HEADER_INDEX] = MNL_TYPE_U32,
467 [DEVLINK_ATTR_DPIPE_FIELD] = MNL_TYPE_NESTED,
468 [DEVLINK_ATTR_DPIPE_FIELD_NAME] = MNL_TYPE_STRING,
469 [DEVLINK_ATTR_DPIPE_FIELD_ID] = MNL_TYPE_U32,
470 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] = MNL_TYPE_U32,
471 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE] = MNL_TYPE_U32,
13925ae9
MS
472 [DEVLINK_ATTR_PARAM] = MNL_TYPE_NESTED,
473 [DEVLINK_ATTR_PARAM_NAME] = MNL_TYPE_STRING,
474 [DEVLINK_ATTR_PARAM_TYPE] = MNL_TYPE_U8,
475 [DEVLINK_ATTR_PARAM_VALUES_LIST] = MNL_TYPE_NESTED,
476 [DEVLINK_ATTR_PARAM_VALUE] = MNL_TYPE_NESTED,
477 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = MNL_TYPE_U8,
8b4fbf0b
AV
478 [DEVLINK_ATTR_REGION_NAME] = MNL_TYPE_STRING,
479 [DEVLINK_ATTR_REGION_SIZE] = MNL_TYPE_U64,
480 [DEVLINK_ATTR_REGION_SNAPSHOTS] = MNL_TYPE_NESTED,
481 [DEVLINK_ATTR_REGION_SNAPSHOT] = MNL_TYPE_NESTED,
482 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = MNL_TYPE_U32,
483 [DEVLINK_ATTR_REGION_CHUNKS] = MNL_TYPE_NESTED,
484 [DEVLINK_ATTR_REGION_CHUNK] = MNL_TYPE_NESTED,
485 [DEVLINK_ATTR_REGION_CHUNK_DATA] = MNL_TYPE_BINARY,
486 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = MNL_TYPE_U64,
487 [DEVLINK_ATTR_REGION_CHUNK_LEN] = MNL_TYPE_U64,
05bc89e9
JK
488 [DEVLINK_ATTR_INFO_DRIVER_NAME] = MNL_TYPE_STRING,
489 [DEVLINK_ATTR_INFO_SERIAL_NUMBER] = MNL_TYPE_STRING,
490 [DEVLINK_ATTR_INFO_VERSION_FIXED] = MNL_TYPE_NESTED,
491 [DEVLINK_ATTR_INFO_VERSION_RUNNING] = MNL_TYPE_NESTED,
492 [DEVLINK_ATTR_INFO_VERSION_STORED] = MNL_TYPE_NESTED,
493 [DEVLINK_ATTR_INFO_VERSION_NAME] = MNL_TYPE_STRING,
494 [DEVLINK_ATTR_INFO_VERSION_VALUE] = MNL_TYPE_STRING,
2f1242ef
AL
495 [DEVLINK_ATTR_HEALTH_REPORTER] = MNL_TYPE_NESTED,
496 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = MNL_TYPE_STRING,
497 [DEVLINK_ATTR_HEALTH_REPORTER_STATE] = MNL_TYPE_U8,
498 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT] = MNL_TYPE_U64,
499 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT] = MNL_TYPE_U64,
500 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS] = MNL_TYPE_U64,
501 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = MNL_TYPE_U64,
853be43f
JP
502 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = MNL_TYPE_STRING,
503 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG] = MNL_TYPE_STRING,
504 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE] = MNL_TYPE_U64,
505 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL] = MNL_TYPE_U64,
ef12d6da
IS
506 [DEVLINK_ATTR_STATS] = MNL_TYPE_NESTED,
507 [DEVLINK_ATTR_TRAP_NAME] = MNL_TYPE_STRING,
508 [DEVLINK_ATTR_TRAP_ACTION] = MNL_TYPE_U8,
509 [DEVLINK_ATTR_TRAP_TYPE] = MNL_TYPE_U8,
510 [DEVLINK_ATTR_TRAP_GENERIC] = MNL_TYPE_FLAG,
511 [DEVLINK_ATTR_TRAP_METADATA] = MNL_TYPE_NESTED,
512 [DEVLINK_ATTR_TRAP_GROUP_NAME] = MNL_TYPE_STRING,
88fdbf80 513 [DEVLINK_ATTR_RELOAD_FAILED] = MNL_TYPE_U8,
a66af556
IS
514 [DEVLINK_ATTR_TRAP_POLICER_ID] = MNL_TYPE_U32,
515 [DEVLINK_ATTR_TRAP_POLICER_RATE] = MNL_TYPE_U64,
516 [DEVLINK_ATTR_TRAP_POLICER_BURST] = MNL_TYPE_U64,
ef12d6da
IS
517};
518
519static const enum mnl_attr_data_type
520devlink_stats_policy[DEVLINK_ATTR_STATS_MAX + 1] = {
521 [DEVLINK_ATTR_STATS_RX_PACKETS] = MNL_TYPE_U64,
522 [DEVLINK_ATTR_STATS_RX_BYTES] = MNL_TYPE_U64,
a66af556 523 [DEVLINK_ATTR_STATS_RX_DROPPED] = MNL_TYPE_U64,
4f10cede
AS
524};
525
a3c4b484
JP
526static int attr_cb(const struct nlattr *attr, void *data)
527{
528 const struct nlattr **tb = data;
529 int type;
530
a3c4b484 531 if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0)
c619f2be 532 return MNL_CB_OK;
a3c4b484 533
4f10cede
AS
534 type = mnl_attr_get_type(attr);
535 if (mnl_attr_validate(attr, devlink_policy[type]) < 0)
6566ca8c 536 return MNL_CB_ERROR;
4f10cede 537
a3c4b484
JP
538 tb[type] = attr;
539 return MNL_CB_OK;
540}
541
ef12d6da
IS
542static int attr_stats_cb(const struct nlattr *attr, void *data)
543{
544 const struct nlattr **tb = data;
545 int type;
546
547 /* Allow the tool to work on top of newer kernels that might contain
548 * more attributes.
549 */
550 if (mnl_attr_type_valid(attr, DEVLINK_ATTR_STATS_MAX) < 0)
551 return MNL_CB_OK;
552
553 type = mnl_attr_get_type(attr);
554 if (mnl_attr_validate(attr, devlink_stats_policy[type]) < 0)
555 return MNL_CB_ERROR;
556
557 tb[type] = attr;
558 return MNL_CB_OK;
559}
560
a3c4b484
JP
561static int ifname_map_cb(const struct nlmsghdr *nlh, void *data)
562{
563 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
564 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
565 struct dl *dl = data;
566 struct ifname_map *ifname_map;
567 const char *bus_name;
568 const char *dev_name;
569 uint32_t port_ifindex;
570 const char *port_ifname;
571
572 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
573 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
574 !tb[DEVLINK_ATTR_PORT_INDEX])
575 return MNL_CB_ERROR;
576
577 if (!tb[DEVLINK_ATTR_PORT_NETDEV_NAME])
578 return MNL_CB_OK;
579
580 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
581 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
582 port_ifindex = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
583 port_ifname = mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]);
584 ifname_map = ifname_map_alloc(bus_name, dev_name,
585 port_ifindex, port_ifname);
586 if (!ifname_map)
587 return MNL_CB_ERROR;
588 list_add(&ifname_map->list, &dl->ifname_map_list);
589
590 return MNL_CB_OK;
591}
592
593static void ifname_map_fini(struct dl *dl)
594{
595 struct ifname_map *ifname_map, *tmp;
596
597 list_for_each_entry_safe(ifname_map, tmp,
598 &dl->ifname_map_list, list) {
599 list_del(&ifname_map->list);
600 ifname_map_free(ifname_map);
601 }
602}
603
604static int ifname_map_init(struct dl *dl)
605{
606 struct nlmsghdr *nlh;
607 int err;
608
609 INIT_LIST_HEAD(&dl->ifname_map_list);
610
611 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET,
612 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
613
614 err = _mnlg_socket_sndrcv(dl->nlg, nlh, ifname_map_cb, dl);
615 if (err) {
616 ifname_map_fini(dl);
617 return err;
618 }
619 return 0;
620}
621
622static int ifname_map_lookup(struct dl *dl, const char *ifname,
623 char **p_bus_name, char **p_dev_name,
624 uint32_t *p_port_index)
625{
626 struct ifname_map *ifname_map;
627
628 list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
629 if (strcmp(ifname, ifname_map->ifname) == 0) {
630 *p_bus_name = ifname_map->bus_name;
631 *p_dev_name = ifname_map->dev_name;
632 *p_port_index = ifname_map->port_index;
633 return 0;
634 }
635 }
636 return -ENOENT;
637}
638
e6d7367d
JP
639static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
640 const char *dev_name, uint32_t port_index,
641 char **p_ifname)
642{
643 struct ifname_map *ifname_map;
644
645 list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
646 if (strcmp(bus_name, ifname_map->bus_name) == 0 &&
647 strcmp(dev_name, ifname_map->dev_name) == 0 &&
648 port_index == ifname_map->port_index) {
649 *p_ifname = ifname_map->ifname;
650 return 0;
651 }
652 }
653 return -ENOENT;
654}
655
a3c4b484
JP
656static unsigned int strslashcount(char *str)
657{
658 unsigned int count = 0;
659 char *pos = str;
660
661 while ((pos = strchr(pos, '/'))) {
662 count++;
663 pos++;
664 }
665 return count;
666}
667
668static int strslashrsplit(char *str, char **before, char **after)
669{
670 char *slash;
671
672 slash = strrchr(str, '/');
673 if (!slash)
674 return -EINVAL;
675 *slash = '\0';
676 *before = str;
677 *after = slash + 1;
678 return 0;
679}
680
8b4fbf0b
AV
681static int strtouint64_t(const char *str, uint64_t *p_val)
682{
683 char *endptr;
684 unsigned long long int val;
685
686 val = strtoull(str, &endptr, 10);
687 if (endptr == str || *endptr != '\0')
688 return -EINVAL;
689 if (val > ULONG_MAX)
690 return -ERANGE;
691 *p_val = val;
692 return 0;
693}
694
a3c4b484
JP
695static int strtouint32_t(const char *str, uint32_t *p_val)
696{
697 char *endptr;
698 unsigned long int val;
699
700 val = strtoul(str, &endptr, 10);
701 if (endptr == str || *endptr != '\0')
702 return -EINVAL;
703 if (val > UINT_MAX)
704 return -ERANGE;
705 *p_val = val;
706 return 0;
707}
708
e6d7367d
JP
709static int strtouint16_t(const char *str, uint16_t *p_val)
710{
711 char *endptr;
712 unsigned long int val;
713
714 val = strtoul(str, &endptr, 10);
715 if (endptr == str || *endptr != '\0')
716 return -EINVAL;
717 if (val > USHRT_MAX)
718 return -ERANGE;
719 *p_val = val;
720 return 0;
721}
722
13925ae9
MS
723static int strtouint8_t(const char *str, uint8_t *p_val)
724{
725 char *endptr;
726 unsigned long int val;
727
728 val = strtoul(str, &endptr, 10);
729 if (endptr == str || *endptr != '\0')
730 return -EINVAL;
731 if (val > UCHAR_MAX)
732 return -ERANGE;
733 *p_val = val;
734 return 0;
735}
736
737static int strtobool(const char *str, bool *p_val)
738{
739 bool val;
740
741 if (!strcmp(str, "true") || !strcmp(str, "1"))
742 val = true;
743 else if (!strcmp(str, "false") || !strcmp(str, "0"))
744 val = false;
745 else
746 return -EINVAL;
747 *p_val = val;
748 return 0;
749}
750
2f85a9c5
JP
751static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name)
752{
753 strslashrsplit(str, p_bus_name, p_dev_name);
754 return 0;
755}
756
6563a6eb 757static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name)
a3c4b484
JP
758{
759 char *str = dl_argv_next(dl);
a3c4b484
JP
760
761 if (!str) {
762 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
763 return -EINVAL;
764 }
765 if (strslashcount(str) != 1) {
766 pr_err("Wrong devlink identification string format.\n");
767 pr_err("Expected \"bus_name/dev_name\".\n");
768 return -EINVAL;
769 }
2f85a9c5
JP
770 return __dl_argv_handle(str, p_bus_name, p_dev_name);
771}
a3c4b484 772
2f85a9c5
JP
773static int __dl_argv_handle_port(char *str,
774 char **p_bus_name, char **p_dev_name,
775 uint32_t *p_port_index)
776{
6e33f7b0
PS
777 char *handlestr;
778 char *portstr;
2f85a9c5
JP
779 int err;
780
6e33f7b0
PS
781 err = strslashrsplit(str, &handlestr, &portstr);
782 if (err) {
783 pr_err("Port identification \"%s\" is invalid\n", str);
784 return err;
785 }
2f85a9c5
JP
786 err = strtouint32_t(portstr, p_port_index);
787 if (err) {
788 pr_err("Port index \"%s\" is not a number or not within range\n",
789 portstr);
790 return err;
791 }
6e33f7b0
PS
792 err = strslashrsplit(handlestr, p_bus_name, p_dev_name);
793 if (err) {
794 pr_err("Port identification \"%s\" is invalid\n", str);
795 return err;
796 }
2f85a9c5
JP
797 return 0;
798}
799
800static int __dl_argv_handle_port_ifname(struct dl *dl, char *str,
801 char **p_bus_name, char **p_dev_name,
802 uint32_t *p_port_index)
803{
804 int err;
805
806 err = ifname_map_lookup(dl, str, p_bus_name, p_dev_name,
807 p_port_index);
808 if (err) {
809 pr_err("Netdevice \"%s\" not found\n", str);
810 return err;
811 }
a3c4b484
JP
812 return 0;
813}
814
6563a6eb
JP
815static int dl_argv_handle_port(struct dl *dl, char **p_bus_name,
816 char **p_dev_name, uint32_t *p_port_index)
a3c4b484
JP
817{
818 char *str = dl_argv_next(dl);
819 unsigned int slash_count;
a3c4b484
JP
820
821 if (!str) {
822 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
823 return -EINVAL;
824 }
825 slash_count = strslashcount(str);
7a34b9d0
HL
826 switch (slash_count) {
827 case 0:
828 return __dl_argv_handle_port_ifname(dl, str, p_bus_name,
829 p_dev_name, p_port_index);
830 case 2:
831 return __dl_argv_handle_port(str, p_bus_name,
832 p_dev_name, p_port_index);
833 default:
a3c4b484
JP
834 pr_err("Wrong port identification string format.\n");
835 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
836 return -EINVAL;
837 }
2f85a9c5
JP
838}
839
840static int dl_argv_handle_both(struct dl *dl, char **p_bus_name,
841 char **p_dev_name, uint32_t *p_port_index,
b83220db 842 uint64_t *p_handle_bit)
2f85a9c5
JP
843{
844 char *str = dl_argv_next(dl);
845 unsigned int slash_count;
846 int err;
847
848 if (!str) {
849 pr_err("One of following identifications expected:\n"
850 "Devlink identification (\"bus_name/dev_name\")\n"
851 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
852 return -EINVAL;
853 }
854 slash_count = strslashcount(str);
855 if (slash_count == 1) {
856 err = __dl_argv_handle(str, p_bus_name, p_dev_name);
857 if (err)
a3c4b484 858 return err;
2f85a9c5
JP
859 *p_handle_bit = DL_OPT_HANDLE;
860 } else if (slash_count == 2) {
861 err = __dl_argv_handle_port(str, p_bus_name,
862 p_dev_name, p_port_index);
863 if (err)
864 return err;
865 *p_handle_bit = DL_OPT_HANDLEP;
a3c4b484 866 } else if (slash_count == 0) {
2f85a9c5
JP
867 err = __dl_argv_handle_port_ifname(dl, str, p_bus_name,
868 p_dev_name, p_port_index);
869 if (err)
a3c4b484 870 return err;
2f85a9c5
JP
871 *p_handle_bit = DL_OPT_HANDLEP;
872 } else {
873 pr_err("Wrong port identification string format.\n");
874 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
875 return -EINVAL;
a3c4b484 876 }
a3c4b484
JP
877 return 0;
878}
879
8b4fbf0b
AV
880static int __dl_argv_handle_region(char *str, char **p_bus_name,
881 char **p_dev_name, char **p_region)
882{
883 char *handlestr;
884 int err;
885
886 err = strslashrsplit(str, &handlestr, p_region);
887 if (err) {
888 pr_err("Region identification \"%s\" is invalid\n", str);
889 return err;
890 }
891 err = strslashrsplit(handlestr, p_bus_name, p_dev_name);
892 if (err) {
893 pr_err("Region identification \"%s\" is invalid\n", str);
894 return err;
895 }
896 return 0;
897}
898
899static int dl_argv_handle_region(struct dl *dl, char **p_bus_name,
900 char **p_dev_name, char **p_region)
901{
902 char *str = dl_argv_next(dl);
903 unsigned int slash_count;
904
905 if (!str) {
906 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
907 return -EINVAL;
908 }
909
910 slash_count = strslashcount(str);
911 if (slash_count != 2) {
912 pr_err("Wrong region identification string format.\n");
913 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
914 return -EINVAL;
915 }
916
917 return __dl_argv_handle_region(str, p_bus_name, p_dev_name, p_region);
918}
919
920static int dl_argv_uint64_t(struct dl *dl, uint64_t *p_val)
921{
922 char *str = dl_argv_next(dl);
923 int err;
924
925 if (!str) {
926 pr_err("Unsigned number argument expected\n");
927 return -EINVAL;
928 }
929
930 err = strtouint64_t(str, p_val);
931 if (err) {
932 pr_err("\"%s\" is not a number or not within range\n", str);
933 return err;
934 }
935 return 0;
936}
937
a3c4b484
JP
938static int dl_argv_uint32_t(struct dl *dl, uint32_t *p_val)
939{
940 char *str = dl_argv_next(dl);
941 int err;
942
943 if (!str) {
944 pr_err("Unsigned number argument expected\n");
945 return -EINVAL;
946 }
947
948 err = strtouint32_t(str, p_val);
949 if (err) {
950 pr_err("\"%s\" is not a number or not within range\n", str);
951 return err;
952 }
953 return 0;
954}
955
e6d7367d
JP
956static int dl_argv_uint16_t(struct dl *dl, uint16_t *p_val)
957{
958 char *str = dl_argv_next(dl);
959 int err;
960
961 if (!str) {
962 pr_err("Unsigned number argument expected\n");
963 return -EINVAL;
964 }
965
966 err = strtouint16_t(str, p_val);
967 if (err) {
968 pr_err("\"%s\" is not a number or not within range\n", str);
969 return err;
970 }
971 return 0;
972}
973
b18d8919
AL
974static int dl_argv_bool(struct dl *dl, bool *p_val)
975{
976 char *str = dl_argv_next(dl);
977 int err;
978
979 if (!str) {
980 pr_err("Boolean argument expected\n");
981 return -EINVAL;
982 }
983
984 err = strtobool(str, p_val);
985 if (err) {
986 pr_err("\"%s\" is not a valid boolean value\n", str);
987 return err;
988 }
989 return 0;
990}
991
a3c4b484
JP
992static int dl_argv_str(struct dl *dl, const char **p_str)
993{
994 const char *str = dl_argv_next(dl);
995
996 if (!str) {
997 pr_err("String parameter expected\n");
998 return -EINVAL;
999 }
1000 *p_str = str;
1001 return 0;
1002}
1003
1004static int port_type_get(const char *typestr, enum devlink_port_type *p_type)
1005{
1006 if (strcmp(typestr, "auto") == 0) {
1007 *p_type = DEVLINK_PORT_TYPE_AUTO;
1008 } else if (strcmp(typestr, "eth") == 0) {
1009 *p_type = DEVLINK_PORT_TYPE_ETH;
1010 } else if (strcmp(typestr, "ib") == 0) {
1011 *p_type = DEVLINK_PORT_TYPE_IB;
1012 } else {
1013 pr_err("Unknown port type \"%s\"\n", typestr);
1014 return -EINVAL;
1015 }
1016 return 0;
1017}
1018
e6d7367d
JP
1019static int pool_type_get(const char *typestr, enum devlink_sb_pool_type *p_type)
1020{
1021 if (strcmp(typestr, "ingress") == 0) {
1022 *p_type = DEVLINK_SB_POOL_TYPE_INGRESS;
1023 } else if (strcmp(typestr, "egress") == 0) {
1024 *p_type = DEVLINK_SB_POOL_TYPE_EGRESS;
1025 } else {
1026 pr_err("Unknown pool type \"%s\"\n", typestr);
1027 return -EINVAL;
1028 }
1029 return 0;
1030}
1031
1032static int threshold_type_get(const char *typestr,
1033 enum devlink_sb_threshold_type *p_type)
1034{
1035 if (strcmp(typestr, "static") == 0) {
1036 *p_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC;
1037 } else if (strcmp(typestr, "dynamic") == 0) {
1038 *p_type = DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC;
1039 } else {
1040 pr_err("Unknown threshold type \"%s\"\n", typestr);
1041 return -EINVAL;
1042 }
1043 return 0;
1044}
1045
7c55d770
SH
1046static int eswitch_mode_get(const char *typestr,
1047 enum devlink_eswitch_mode *p_mode)
f57856fa
OG
1048{
1049 if (strcmp(typestr, ESWITCH_MODE_LEGACY) == 0) {
1050 *p_mode = DEVLINK_ESWITCH_MODE_LEGACY;
1051 } else if (strcmp(typestr, ESWITCH_MODE_SWITCHDEV) == 0) {
1052 *p_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
1053 } else {
1054 pr_err("Unknown eswitch mode \"%s\"\n", typestr);
1055 return -EINVAL;
1056 }
1057 return 0;
1058}
1059
6566ca8c
RD
1060static int eswitch_inline_mode_get(const char *typestr,
1061 enum devlink_eswitch_inline_mode *p_mode)
1062{
1063 if (strcmp(typestr, ESWITCH_INLINE_MODE_NONE) == 0) {
1064 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
1065 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_LINK) == 0) {
1066 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
1067 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_NETWORK) == 0) {
1068 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
1069 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_TRANSPORT) == 0) {
1070 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
1071 } else {
1072 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr);
1073 return -EINVAL;
1074 }
1075 return 0;
1076}
1077
153c1a9b
AS
1078static int dpipe_counters_enable_get(const char *typestr,
1079 bool *counters_enable)
1080{
1081 if (strcmp(typestr, "enable") == 0) {
1082 *counters_enable = 1;
1083 } else if (strcmp(typestr, "disable") == 0) {
1084 *counters_enable = 0;
1085 } else {
1086 pr_err("Unknown counter_state \"%s\"\n", typestr);
1087 return -EINVAL;
1088 }
1089 return 0;
1090}
1091
d315b706
RD
1092static int eswitch_encap_mode_get(const char *typestr, bool *p_mode)
1093{
1094 if (strcmp(typestr, "enable") == 0) {
1095 *p_mode = true;
1096 } else if (strcmp(typestr, "disable") == 0) {
1097 *p_mode = false;
1098 } else {
1099 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr);
1100 return -EINVAL;
1101 }
1102 return 0;
1103}
1104
13925ae9
MS
1105static int param_cmode_get(const char *cmodestr,
1106 enum devlink_param_cmode *cmode)
1107{
1108 if (strcmp(cmodestr, PARAM_CMODE_RUNTIME_STR) == 0) {
1109 *cmode = DEVLINK_PARAM_CMODE_RUNTIME;
1110 } else if (strcmp(cmodestr, PARAM_CMODE_DRIVERINIT_STR) == 0) {
1111 *cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1112 } else if (strcmp(cmodestr, PARAM_CMODE_PERMANENT_STR) == 0) {
1113 *cmode = DEVLINK_PARAM_CMODE_PERMANENT;
1114 } else {
1115 pr_err("Unknown configuration mode \"%s\"\n", cmodestr);
1116 return -EINVAL;
1117 }
1118 return 0;
1119}
1120
ef12d6da
IS
1121static int trap_action_get(const char *actionstr,
1122 enum devlink_trap_action *p_action)
1123{
1124 if (strcmp(actionstr, "drop") == 0) {
1125 *p_action = DEVLINK_TRAP_ACTION_DROP;
1126 } else if (strcmp(actionstr, "trap") == 0) {
1127 *p_action = DEVLINK_TRAP_ACTION_TRAP;
1128 } else {
1129 pr_err("Unknown trap action \"%s\"\n", actionstr);
1130 return -EINVAL;
1131 }
1132 return 0;
1133}
1134
ae72e655 1135struct dl_args_metadata {
b83220db 1136 uint64_t o_flag;
ae72e655
AL
1137 char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
1138};
1139
1140static const struct dl_args_metadata dl_args_required[] = {
1141 {DL_OPT_PORT_TYPE, "Port type not set."},
1142 {DL_OPT_PORT_COUNT, "Port split count option expected."},
1143 {DL_OPT_SB_POOL, "Pool index option expected."},
1144 {DL_OPT_SB_SIZE, "Pool size option expected."},
1145 {DL_OPT_SB_TYPE, "Pool type option expected."},
1146 {DL_OPT_SB_THTYPE, "Pool threshold type option expected."},
1147 {DL_OPT_SB_TH, "Threshold option expected."},
1148 {DL_OPT_SB_TC, "TC index option expected."},
1149 {DL_OPT_ESWITCH_MODE, "E-Switch mode option expected."},
1150 {DL_OPT_ESWITCH_INLINE_MODE, "E-Switch inline-mode option expected."},
1151 {DL_OPT_DPIPE_TABLE_NAME, "Dpipe table name expected."},
1152 {DL_OPT_DPIPE_TABLE_COUNTERS, "Dpipe table counter state expected."},
1153 {DL_OPT_ESWITCH_ENCAP_MODE, "E-Switch encapsulation option expected."},
09328144
JK
1154 {DL_OPT_RESOURCE_PATH, "Resource path expected."},
1155 {DL_OPT_RESOURCE_SIZE, "Resource size expected."},
ae72e655
AL
1156 {DL_OPT_PARAM_NAME, "Parameter name expected."},
1157 {DL_OPT_PARAM_VALUE, "Value to set expected."},
1158 {DL_OPT_PARAM_CMODE, "Configuration mode expected."},
1159 {DL_OPT_REGION_SNAPSHOT_ID, "Region snapshot id expected."},
1160 {DL_OPT_REGION_ADDRESS, "Region address value expected."},
1161 {DL_OPT_REGION_LENGTH, "Region length value expected."},
2f1242ef 1162 {DL_OPT_HEALTH_REPORTER_NAME, "Reporter's name is expected."},
ef12d6da 1163 {DL_OPT_TRAP_NAME, "Trap's name is expected."},
4ede9e9d 1164 {DL_OPT_TRAP_GROUP_NAME, "Trap group's name is expected."},
ae72e655
AL
1165};
1166
b83220db
IS
1167static int dl_args_finding_required_validate(uint64_t o_required,
1168 uint64_t o_found)
ae72e655 1169{
b83220db 1170 uint64_t o_flag;
ae72e655
AL
1171 int i;
1172
1173 for (i = 0; i < ARRAY_SIZE(dl_args_required); i++) {
1174 o_flag = dl_args_required[i].o_flag;
1175 if ((o_required & o_flag) && !(o_found & o_flag)) {
1176 pr_err("%s\n", dl_args_required[i].err_msg);
1177 return -EINVAL;
1178 }
1179 }
1896b100
JK
1180 if (o_required & ~o_found) {
1181 pr_err("BUG: unknown argument required but not found\n");
1182 return -EINVAL;
1183 }
ae72e655
AL
1184 return 0;
1185}
1186
b83220db
IS
1187static int dl_argv_parse(struct dl *dl, uint64_t o_required,
1188 uint64_t o_optional)
a3c4b484 1189{
6563a6eb 1190 struct dl_opts *opts = &dl->opts;
b83220db
IS
1191 uint64_t o_all = o_required | o_optional;
1192 uint64_t o_found = 0;
a3c4b484
JP
1193 int err;
1194
2f85a9c5 1195 if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) {
b83220db 1196 uint64_t handle_bit;
2f85a9c5
JP
1197
1198 err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name,
1199 &opts->port_index, &handle_bit);
1200 if (err)
1201 return err;
668bd8d3 1202 o_required &= ~(DL_OPT_HANDLE | DL_OPT_HANDLEP) | handle_bit;
2f85a9c5
JP
1203 o_found |= handle_bit;
1204 } else if (o_required & DL_OPT_HANDLE) {
6563a6eb 1205 err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name);
a3c4b484
JP
1206 if (err)
1207 return err;
6563a6eb 1208 o_found |= DL_OPT_HANDLE;
a3c4b484 1209 } else if (o_required & DL_OPT_HANDLEP) {
6563a6eb
JP
1210 err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name,
1211 &opts->port_index);
a3c4b484
JP
1212 if (err)
1213 return err;
6563a6eb 1214 o_found |= DL_OPT_HANDLEP;
8b4fbf0b
AV
1215 } else if (o_required & DL_OPT_HANDLE_REGION) {
1216 err = dl_argv_handle_region(dl, &opts->bus_name,
1217 &opts->dev_name,
1218 &opts->region_name);
1219 if (err)
1220 return err;
1221 o_found |= DL_OPT_HANDLE_REGION;
a3c4b484
JP
1222 }
1223
1224 while (dl_argc(dl)) {
1225 if (dl_argv_match(dl, "type") &&
1226 (o_all & DL_OPT_PORT_TYPE)) {
a3c4b484
JP
1227 const char *typestr;
1228
1229 dl_arg_inc(dl);
1230 err = dl_argv_str(dl, &typestr);
1231 if (err)
1232 return err;
6563a6eb 1233 err = port_type_get(typestr, &opts->port_type);
a3c4b484
JP
1234 if (err)
1235 return err;
a3c4b484
JP
1236 o_found |= DL_OPT_PORT_TYPE;
1237 } else if (dl_argv_match(dl, "count") &&
1238 (o_all & DL_OPT_PORT_COUNT)) {
a3c4b484 1239 dl_arg_inc(dl);
6563a6eb 1240 err = dl_argv_uint32_t(dl, &opts->port_count);
a3c4b484
JP
1241 if (err)
1242 return err;
a3c4b484 1243 o_found |= DL_OPT_PORT_COUNT;
e6d7367d
JP
1244 } else if (dl_argv_match(dl, "sb") &&
1245 (o_all & DL_OPT_SB)) {
1246 dl_arg_inc(dl);
1247 err = dl_argv_uint32_t(dl, &opts->sb_index);
1248 if (err)
1249 return err;
1250 o_found |= DL_OPT_SB;
1251 } else if (dl_argv_match(dl, "pool") &&
1252 (o_all & DL_OPT_SB_POOL)) {
1253 dl_arg_inc(dl);
1254 err = dl_argv_uint16_t(dl, &opts->sb_pool_index);
1255 if (err)
1256 return err;
1257 o_found |= DL_OPT_SB_POOL;
1258 } else if (dl_argv_match(dl, "size") &&
1259 (o_all & DL_OPT_SB_SIZE)) {
1260 dl_arg_inc(dl);
1261 err = dl_argv_uint32_t(dl, &opts->sb_pool_size);
1262 if (err)
1263 return err;
1264 o_found |= DL_OPT_SB_SIZE;
1265 } else if (dl_argv_match(dl, "type") &&
1266 (o_all & DL_OPT_SB_TYPE)) {
1267 const char *typestr;
1268
1269 dl_arg_inc(dl);
1270 err = dl_argv_str(dl, &typestr);
1271 if (err)
1272 return err;
1273 err = pool_type_get(typestr, &opts->sb_pool_type);
1274 if (err)
1275 return err;
1276 o_found |= DL_OPT_SB_TYPE;
1277 } else if (dl_argv_match(dl, "thtype") &&
1278 (o_all & DL_OPT_SB_THTYPE)) {
1279 const char *typestr;
1280
1281 dl_arg_inc(dl);
1282 err = dl_argv_str(dl, &typestr);
1283 if (err)
1284 return err;
1285 err = threshold_type_get(typestr,
1286 &opts->sb_pool_thtype);
1287 if (err)
1288 return err;
1289 o_found |= DL_OPT_SB_THTYPE;
1290 } else if (dl_argv_match(dl, "th") &&
1291 (o_all & DL_OPT_SB_TH)) {
1292 dl_arg_inc(dl);
1293 err = dl_argv_uint32_t(dl, &opts->sb_threshold);
1294 if (err)
1295 return err;
1296 o_found |= DL_OPT_SB_TH;
1297 } else if (dl_argv_match(dl, "tc") &&
1298 (o_all & DL_OPT_SB_TC)) {
1299 dl_arg_inc(dl);
1300 err = dl_argv_uint16_t(dl, &opts->sb_tc_index);
1301 if (err)
1302 return err;
1303 o_found |= DL_OPT_SB_TC;
f57856fa
OG
1304 } else if (dl_argv_match(dl, "mode") &&
1305 (o_all & DL_OPT_ESWITCH_MODE)) {
1306 const char *typestr;
7c55d770 1307
f57856fa
OG
1308 dl_arg_inc(dl);
1309 err = dl_argv_str(dl, &typestr);
1310 if (err)
1311 return err;
1312 err = eswitch_mode_get(typestr, &opts->eswitch_mode);
1313 if (err)
1314 return err;
1315 o_found |= DL_OPT_ESWITCH_MODE;
6566ca8c
RD
1316 } else if (dl_argv_match(dl, "inline-mode") &&
1317 (o_all & DL_OPT_ESWITCH_INLINE_MODE)) {
1318 const char *typestr;
1319
1320 dl_arg_inc(dl);
1321 err = dl_argv_str(dl, &typestr);
1322 if (err)
1323 return err;
1324 err = eswitch_inline_mode_get(
1325 typestr, &opts->eswitch_inline_mode);
1326 if (err)
1327 return err;
1328 o_found |= DL_OPT_ESWITCH_INLINE_MODE;
153c1a9b
AS
1329 } else if (dl_argv_match(dl, "name") &&
1330 (o_all & DL_OPT_DPIPE_TABLE_NAME)) {
1331 dl_arg_inc(dl);
1332 err = dl_argv_str(dl, &opts->dpipe_table_name);
1333 if (err)
1334 return err;
1335 o_found |= DL_OPT_DPIPE_TABLE_NAME;
1336 } else if (dl_argv_match(dl, "counters") &&
1337 (o_all & DL_OPT_DPIPE_TABLE_COUNTERS)) {
1338 const char *typestr;
1339
1340 dl_arg_inc(dl);
1341 err = dl_argv_str(dl, &typestr);
1342 if (err)
1343 return err;
1344 err = dpipe_counters_enable_get(typestr,
1345 &opts->dpipe_counters_enable);
1346 if (err)
1347 return err;
1348 o_found |= DL_OPT_DPIPE_TABLE_COUNTERS;
d315b706
RD
1349 } else if (dl_argv_match(dl, "encap") &&
1350 (o_all & DL_OPT_ESWITCH_ENCAP_MODE)) {
1351 const char *typestr;
153c1a9b 1352
d315b706
RD
1353 dl_arg_inc(dl);
1354 err = dl_argv_str(dl, &typestr);
1355 if (err)
1356 return err;
1357 err = eswitch_encap_mode_get(typestr,
1358 &opts->eswitch_encap_mode);
1359 if (err)
1360 return err;
1361 o_found |= DL_OPT_ESWITCH_ENCAP_MODE;
8cd64409
AS
1362 } else if (dl_argv_match(dl, "path") &&
1363 (o_all & DL_OPT_RESOURCE_PATH)) {
1364 dl_arg_inc(dl);
1365 err = dl_argv_str(dl, &opts->resource_path);
1366 if (err)
1367 return err;
1368 o_found |= DL_OPT_RESOURCE_PATH;
1369 } else if (dl_argv_match(dl, "size") &&
1370 (o_all & DL_OPT_RESOURCE_SIZE)) {
1371 dl_arg_inc(dl);
c3f69bf9 1372 err = dl_argv_uint64_t(dl, &opts->resource_size);
8cd64409
AS
1373 if (err)
1374 return err;
1375 o_found |= DL_OPT_RESOURCE_SIZE;
13925ae9
MS
1376 } else if (dl_argv_match(dl, "name") &&
1377 (o_all & DL_OPT_PARAM_NAME)) {
1378 dl_arg_inc(dl);
1379 err = dl_argv_str(dl, &opts->param_name);
1380 if (err)
1381 return err;
1382 o_found |= DL_OPT_PARAM_NAME;
1383 } else if (dl_argv_match(dl, "value") &&
1384 (o_all & DL_OPT_PARAM_VALUE)) {
1385 dl_arg_inc(dl);
1386 err = dl_argv_str(dl, &opts->param_value);
1387 if (err)
1388 return err;
1389 o_found |= DL_OPT_PARAM_VALUE;
1390 } else if (dl_argv_match(dl, "cmode") &&
1391 (o_all & DL_OPT_PARAM_CMODE)) {
1392 const char *cmodestr;
1393
1394 dl_arg_inc(dl);
1395 err = dl_argv_str(dl, &cmodestr);
1396 if (err)
1397 return err;
1398 err = param_cmode_get(cmodestr, &opts->cmode);
1399 if (err)
1400 return err;
1401 o_found |= DL_OPT_PARAM_CMODE;
8b4fbf0b
AV
1402 } else if (dl_argv_match(dl, "snapshot") &&
1403 (o_all & DL_OPT_REGION_SNAPSHOT_ID)) {
1404 dl_arg_inc(dl);
1405 err = dl_argv_uint32_t(dl, &opts->region_snapshot_id);
1406 if (err)
1407 return err;
1408 o_found |= DL_OPT_REGION_SNAPSHOT_ID;
1409 } else if (dl_argv_match(dl, "address") &&
1410 (o_all & DL_OPT_REGION_ADDRESS)) {
1411 dl_arg_inc(dl);
1412 err = dl_argv_uint64_t(dl, &opts->region_address);
1413 if (err)
1414 return err;
1415 o_found |= DL_OPT_REGION_ADDRESS;
1416 } else if (dl_argv_match(dl, "length") &&
1417 (o_all & DL_OPT_REGION_LENGTH)) {
1418 dl_arg_inc(dl);
1419 err = dl_argv_uint64_t(dl, &opts->region_length);
1420 if (err)
1421 return err;
1422 o_found |= DL_OPT_REGION_LENGTH;
d326d79c
JK
1423 } else if (dl_argv_match(dl, "file") &&
1424 (o_all & DL_OPT_FLASH_FILE_NAME)) {
1425 dl_arg_inc(dl);
1426 err = dl_argv_str(dl, &opts->flash_file_name);
1427 if (err)
1428 return err;
1429 o_found |= DL_OPT_FLASH_FILE_NAME;
1430 } else if (dl_argv_match(dl, "component") &&
1431 (o_all & DL_OPT_FLASH_COMPONENT)) {
1432 dl_arg_inc(dl);
1433 err = dl_argv_str(dl, &opts->flash_component);
1434 if (err)
1435 return err;
1436 o_found |= DL_OPT_FLASH_COMPONENT;
2f1242ef
AL
1437 } else if (dl_argv_match(dl, "reporter") &&
1438 (o_all & DL_OPT_HEALTH_REPORTER_NAME)) {
1439 dl_arg_inc(dl);
1440 err = dl_argv_str(dl, &opts->reporter_name);
1441 if (err)
1442 return err;
1443 o_found |= DL_OPT_HEALTH_REPORTER_NAME;
b18d8919
AL
1444 } else if (dl_argv_match(dl, "grace_period") &&
1445 (o_all & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)) {
1446 dl_arg_inc(dl);
1447 err = dl_argv_uint64_t(dl,
1448 &opts->reporter_graceful_period);
1449 if (err)
1450 return err;
1451 o_found |= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD;
1452 } else if (dl_argv_match(dl, "auto_recover") &&
1453 (o_all & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)) {
1454 dl_arg_inc(dl);
1455 err = dl_argv_bool(dl, &opts->reporter_auto_recover);
1456 if (err)
1457 return err;
1458 o_found |= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER;
ef12d6da
IS
1459 } else if (dl_argv_match(dl, "trap") &&
1460 (o_all & DL_OPT_TRAP_NAME)) {
1461 dl_arg_inc(dl);
1462 err = dl_argv_str(dl, &opts->trap_name);
1463 if (err)
1464 return err;
1465 o_found |= DL_OPT_TRAP_NAME;
4ede9e9d
IS
1466 } else if (dl_argv_match(dl, "group") &&
1467 (o_all & DL_OPT_TRAP_GROUP_NAME)) {
1468 dl_arg_inc(dl);
1469 err = dl_argv_str(dl, &opts->trap_group_name);
1470 if (err)
1471 return err;
1472 o_found |= DL_OPT_TRAP_GROUP_NAME;
ef12d6da
IS
1473 } else if (dl_argv_match(dl, "action") &&
1474 (o_all & DL_OPT_TRAP_ACTION)) {
1475 const char *actionstr;
1476
1477 dl_arg_inc(dl);
1478 err = dl_argv_str(dl, &actionstr);
1479 if (err)
1480 return err;
1481 err = trap_action_get(actionstr, &opts->trap_action);
1482 if (err)
1483 return err;
1484 o_found |= DL_OPT_TRAP_ACTION;
08e8e1ca
JP
1485 } else if (dl_argv_match(dl, "netns") &&
1486 (o_all & DL_OPT_NETNS)) {
1487 const char *netns_str;
1488
1489 dl_arg_inc(dl);
1490 err = dl_argv_str(dl, &netns_str);
1491 if (err)
1492 return err;
1493 opts->netns = netns_get_fd(netns_str);
9a0a2fcb
JK
1494 if ((int)opts->netns < 0) {
1495 dl_arg_dec(dl);
08e8e1ca
JP
1496 err = dl_argv_uint32_t(dl, &opts->netns);
1497 if (err)
1498 return err;
1499 opts->netns_is_pid = true;
1500 }
1501 o_found |= DL_OPT_NETNS;
a66af556
IS
1502 } else if (dl_argv_match(dl, "policer") &&
1503 (o_all & DL_OPT_TRAP_POLICER_ID)) {
1504 dl_arg_inc(dl);
1505 err = dl_argv_uint32_t(dl, &opts->trap_policer_id);
1506 if (err)
1507 return err;
1508 o_found |= DL_OPT_TRAP_POLICER_ID;
02a2a668
IS
1509 } else if (dl_argv_match(dl, "nopolicer") &&
1510 (o_all & DL_OPT_TRAP_POLICER_ID)) {
1511 dl_arg_inc(dl);
1512 opts->trap_policer_id = 0;
1513 o_found |= DL_OPT_TRAP_POLICER_ID;
a66af556
IS
1514 } else if (dl_argv_match(dl, "rate") &&
1515 (o_all & DL_OPT_TRAP_POLICER_RATE)) {
1516 dl_arg_inc(dl);
1517 err = dl_argv_uint64_t(dl, &opts->trap_policer_rate);
1518 if (err)
1519 return err;
1520 o_found |= DL_OPT_TRAP_POLICER_RATE;
1521 } else if (dl_argv_match(dl, "burst") &&
1522 (o_all & DL_OPT_TRAP_POLICER_BURST)) {
1523 dl_arg_inc(dl);
1524 err = dl_argv_uint64_t(dl, &opts->trap_policer_burst);
1525 if (err)
1526 return err;
1527 o_found |= DL_OPT_TRAP_POLICER_BURST;
a3c4b484
JP
1528 } else {
1529 pr_err("Unknown option \"%s\"\n", dl_argv(dl));
1530 return -EINVAL;
1531 }
1532 }
1533
6563a6eb
JP
1534 opts->present = o_found;
1535
e6d7367d
JP
1536 if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) {
1537 opts->sb_index = 0;
1538 opts->present |= DL_OPT_SB;
1539 }
1540
ae72e655 1541 return dl_args_finding_required_validate(o_required, o_found);
a3c4b484
JP
1542}
1543
6563a6eb
JP
1544static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
1545{
1546 struct dl_opts *opts = &dl->opts;
1547
1548 if (opts->present & DL_OPT_HANDLE) {
1549 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1550 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1551 } else if (opts->present & DL_OPT_HANDLEP) {
1552 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1553 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1554 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_INDEX,
1555 opts->port_index);
8b4fbf0b
AV
1556 } else if (opts->present & DL_OPT_HANDLE_REGION) {
1557 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1558 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1559 mnl_attr_put_strz(nlh, DEVLINK_ATTR_REGION_NAME,
1560 opts->region_name);
6563a6eb
JP
1561 }
1562 if (opts->present & DL_OPT_PORT_TYPE)
1563 mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_TYPE,
1564 opts->port_type);
1565 if (opts->present & DL_OPT_PORT_COUNT)
1566 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_SPLIT_COUNT,
1567 opts->port_count);
e6d7367d
JP
1568 if (opts->present & DL_OPT_SB)
1569 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_INDEX,
1570 opts->sb_index);
1571 if (opts->present & DL_OPT_SB_POOL)
1572 mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_POOL_INDEX,
1573 opts->sb_pool_index);
1574 if (opts->present & DL_OPT_SB_SIZE)
1575 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_POOL_SIZE,
1576 opts->sb_pool_size);
1577 if (opts->present & DL_OPT_SB_TYPE)
1578 mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_TYPE,
1579 opts->sb_pool_type);
1580 if (opts->present & DL_OPT_SB_THTYPE)
1581 mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1582 opts->sb_pool_thtype);
1583 if (opts->present & DL_OPT_SB_TH)
1584 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_THRESHOLD,
1585 opts->sb_threshold);
1586 if (opts->present & DL_OPT_SB_TC)
1587 mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_TC_INDEX,
1588 opts->sb_tc_index);
f57856fa
OG
1589 if (opts->present & DL_OPT_ESWITCH_MODE)
1590 mnl_attr_put_u16(nlh, DEVLINK_ATTR_ESWITCH_MODE,
1591 opts->eswitch_mode);
6566ca8c
RD
1592 if (opts->present & DL_OPT_ESWITCH_INLINE_MODE)
1593 mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1594 opts->eswitch_inline_mode);
153c1a9b
AS
1595 if (opts->present & DL_OPT_DPIPE_TABLE_NAME)
1596 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DPIPE_TABLE_NAME,
1597 opts->dpipe_table_name);
1598 if (opts->present & DL_OPT_DPIPE_TABLE_COUNTERS)
1599 mnl_attr_put_u8(nlh, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1600 opts->dpipe_counters_enable);
d315b706
RD
1601 if (opts->present & DL_OPT_ESWITCH_ENCAP_MODE)
1602 mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_ENCAP_MODE,
1603 opts->eswitch_encap_mode);
8cd64409
AS
1604 if ((opts->present & DL_OPT_RESOURCE_PATH) && opts->resource_id_valid)
1605 mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_ID,
1606 opts->resource_id);
1607 if (opts->present & DL_OPT_RESOURCE_SIZE)
1608 mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE,
1609 opts->resource_size);
13925ae9
MS
1610 if (opts->present & DL_OPT_PARAM_NAME)
1611 mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_NAME,
1612 opts->param_name);
1613 if (opts->present & DL_OPT_PARAM_CMODE)
1614 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE,
1615 opts->cmode);
8b4fbf0b
AV
1616 if (opts->present & DL_OPT_REGION_SNAPSHOT_ID)
1617 mnl_attr_put_u32(nlh, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
1618 opts->region_snapshot_id);
1619 if (opts->present & DL_OPT_REGION_ADDRESS)
1620 mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_ADDR,
1621 opts->region_address);
1622 if (opts->present & DL_OPT_REGION_LENGTH)
1623 mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_LEN,
1624 opts->region_length);
d326d79c
JK
1625 if (opts->present & DL_OPT_FLASH_FILE_NAME)
1626 mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME,
1627 opts->flash_file_name);
1628 if (opts->present & DL_OPT_FLASH_COMPONENT)
1629 mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
1630 opts->flash_component);
2f1242ef
AL
1631 if (opts->present & DL_OPT_HEALTH_REPORTER_NAME)
1632 mnl_attr_put_strz(nlh, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
1633 opts->reporter_name);
b18d8919
AL
1634 if (opts->present & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)
1635 mnl_attr_put_u64(nlh,
1636 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
1637 opts->reporter_graceful_period);
1638 if (opts->present & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)
1639 mnl_attr_put_u8(nlh, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
1640 opts->reporter_auto_recover);
ef12d6da
IS
1641 if (opts->present & DL_OPT_TRAP_NAME)
1642 mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_NAME,
1643 opts->trap_name);
4ede9e9d
IS
1644 if (opts->present & DL_OPT_TRAP_GROUP_NAME)
1645 mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_GROUP_NAME,
1646 opts->trap_group_name);
ef12d6da
IS
1647 if (opts->present & DL_OPT_TRAP_ACTION)
1648 mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION,
1649 opts->trap_action);
08e8e1ca
JP
1650 if (opts->present & DL_OPT_NETNS)
1651 mnl_attr_put_u32(nlh,
1652 opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID :
1653 DEVLINK_ATTR_NETNS_FD,
1654 opts->netns);
a66af556
IS
1655 if (opts->present & DL_OPT_TRAP_POLICER_ID)
1656 mnl_attr_put_u32(nlh, DEVLINK_ATTR_TRAP_POLICER_ID,
1657 opts->trap_policer_id);
1658 if (opts->present & DL_OPT_TRAP_POLICER_RATE)
1659 mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_RATE,
1660 opts->trap_policer_rate);
1661 if (opts->present & DL_OPT_TRAP_POLICER_BURST)
1662 mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_BURST,
1663 opts->trap_policer_burst);
6563a6eb
JP
1664}
1665
1666static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
b83220db 1667 uint64_t o_required, uint64_t o_optional)
6563a6eb
JP
1668{
1669 int err;
1670
1671 err = dl_argv_parse(dl, o_required, o_optional);
1672 if (err)
1673 return err;
1674 dl_opts_put(nlh, dl);
1675 return 0;
1676}
1677
25ec49be
JP
1678static bool dl_dump_filter(struct dl *dl, struct nlattr **tb)
1679{
1680 struct dl_opts *opts = &dl->opts;
1681 struct nlattr *attr_bus_name = tb[DEVLINK_ATTR_BUS_NAME];
1682 struct nlattr *attr_dev_name = tb[DEVLINK_ATTR_DEV_NAME];
1683 struct nlattr *attr_port_index = tb[DEVLINK_ATTR_PORT_INDEX];
1684 struct nlattr *attr_sb_index = tb[DEVLINK_ATTR_SB_INDEX];
1685
1686 if (opts->present & DL_OPT_HANDLE &&
1687 attr_bus_name && attr_dev_name) {
1688 const char *bus_name = mnl_attr_get_str(attr_bus_name);
1689 const char *dev_name = mnl_attr_get_str(attr_dev_name);
1690
1691 if (strcmp(bus_name, opts->bus_name) != 0 ||
1692 strcmp(dev_name, opts->dev_name) != 0)
1693 return false;
1694 }
1695 if (opts->present & DL_OPT_HANDLEP &&
1696 attr_bus_name && attr_dev_name && attr_port_index) {
1697 const char *bus_name = mnl_attr_get_str(attr_bus_name);
1698 const char *dev_name = mnl_attr_get_str(attr_dev_name);
1699 uint32_t port_index = mnl_attr_get_u32(attr_port_index);
1700
1701 if (strcmp(bus_name, opts->bus_name) != 0 ||
1702 strcmp(dev_name, opts->dev_name) != 0 ||
1703 port_index != opts->port_index)
1704 return false;
1705 }
1706 if (opts->present & DL_OPT_SB && attr_sb_index) {
1707 uint32_t sb_index = mnl_attr_get_u32(attr_sb_index);
1708
1709 if (sb_index != opts->sb_index)
1710 return false;
1711 }
1712 return true;
1713}
707a91c5 1714
a3c4b484
JP
1715static void cmd_dev_help(void)
1716{
7a9466db 1717 pr_err("Usage: devlink dev show [ DEV ]\n");
a93b6bb3 1718 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
6566ca8c 1719 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
d315b706 1720 pr_err(" [ encap { disable | enable } ]\n");
a93b6bb3 1721 pr_err(" devlink dev eswitch show DEV\n");
13925ae9
MS
1722 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1723 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
08e8e1ca 1724 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
05bc89e9 1725 pr_err(" devlink dev info [ DEV ]\n");
d326d79c 1726 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
a3c4b484
JP
1727}
1728
e3d0f0c0
JP
1729static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name,
1730 const char *dev_name)
43f35be4 1731{
e3d0f0c0
JP
1732 if (!dl->arr_last.present)
1733 return false;
1734 return strcmp(dl->arr_last.bus_name, bus_name) == 0 &&
1735 strcmp(dl->arr_last.dev_name, dev_name) == 0;
43f35be4
JP
1736}
1737
e3d0f0c0
JP
1738static void arr_last_handle_set(struct dl *dl, const char *bus_name,
1739 const char *dev_name)
a3c4b484 1740{
e3d0f0c0
JP
1741 dl->arr_last.present = true;
1742 free(dl->arr_last.dev_name);
1743 free(dl->arr_last.bus_name);
1744 dl->arr_last.bus_name = strdup(bus_name);
1745 dl->arr_last.dev_name = strdup(dev_name);
a3c4b484
JP
1746}
1747
e3d0f0c0
JP
1748static bool should_arr_last_handle_start(struct dl *dl, const char *bus_name,
1749 const char *dev_name)
43f35be4 1750{
e3d0f0c0 1751 return !cmp_arr_last_handle(dl, bus_name, dev_name);
43f35be4
JP
1752}
1753
e3d0f0c0
JP
1754static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
1755 const char *dev_name)
68cab0ba 1756{
e3d0f0c0
JP
1757 return dl->arr_last.present &&
1758 !cmp_arr_last_handle(dl, bus_name, dev_name);
43f35be4
JP
1759}
1760
e3d0f0c0
JP
1761static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
1762 bool content, bool array)
e6d7367d 1763{
e3d0f0c0
JP
1764 const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
1765 const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
2cc10ce8 1766 char buf[64];
e6d7367d 1767
e3d0f0c0 1768 sprintf(buf, "%s/%s", bus_name, dev_name);
e6d7367d 1769
e3d0f0c0
JP
1770 if (dl->json_output) {
1771 if (array) {
1772 if (should_arr_last_handle_end(dl, bus_name, dev_name))
b20555e4 1773 close_json_array(PRINT_JSON, NULL);
e3d0f0c0
JP
1774 if (should_arr_last_handle_start(dl, bus_name,
1775 dev_name)) {
b20555e4
RD
1776 open_json_array(PRINT_JSON, buf);
1777 open_json_object(NULL);
e3d0f0c0
JP
1778 arr_last_handle_set(dl, bus_name, dev_name);
1779 } else {
b20555e4 1780 open_json_object(NULL);
e3d0f0c0
JP
1781 }
1782 } else {
b20555e4 1783 open_json_object(buf);
e3d0f0c0
JP
1784 }
1785 } else {
153c1a9b
AS
1786 if (array) {
1787 if (should_arr_last_handle_end(dl, bus_name, dev_name))
1788 __pr_out_indent_dec();
1789 if (should_arr_last_handle_start(dl, bus_name,
1790 dev_name)) {
1791 pr_out("%s%s", buf, content ? ":" : "");
1792 __pr_out_newline();
1793 __pr_out_indent_inc();
1794 arr_last_handle_set(dl, bus_name, dev_name);
1795 }
1796 } else {
1797 pr_out("%s%s", buf, content ? ":" : "");
1798 }
e3d0f0c0
JP
1799 }
1800}
e6d7367d 1801
e3d0f0c0
JP
1802static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb)
1803{
1804 __pr_out_handle_start(dl, tb, true, true);
e6d7367d
JP
1805}
1806
e3d0f0c0
JP
1807static void pr_out_handle_end(struct dl *dl)
1808{
1809 if (dl->json_output)
b20555e4 1810 close_json_object();
e3d0f0c0 1811 else
153c1a9b 1812 __pr_out_newline();
e3d0f0c0
JP
1813}
1814
1815static void pr_out_handle(struct dl *dl, struct nlattr **tb)
1816{
1817 __pr_out_handle_start(dl, tb, false, false);
1818 pr_out_handle_end(dl);
1819}
1820
1821static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name,
1822 const char *dev_name, uint32_t port_index)
1823{
1824 return cmp_arr_last_handle(dl, bus_name, dev_name) &&
1825 dl->arr_last.port_index == port_index;
1826}
1827
1828static void arr_last_port_handle_set(struct dl *dl, const char *bus_name,
1829 const char *dev_name, uint32_t port_index)
1830{
1831 arr_last_handle_set(dl, bus_name, dev_name);
1832 dl->arr_last.port_index = port_index;
1833}
1834
1835static bool should_arr_last_port_handle_start(struct dl *dl,
1836 const char *bus_name,
1837 const char *dev_name,
1838 uint32_t port_index)
1839{
1840 return !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
1841}
1842
1843static bool should_arr_last_port_handle_end(struct dl *dl,
1844 const char *bus_name,
1845 const char *dev_name,
1846 uint32_t port_index)
1847{
1848 return dl->arr_last.present &&
1849 !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
1850}
1851
1852static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name,
1853 const char *dev_name,
1854 uint32_t port_index, bool try_nice,
1855 bool array)
1856{
2cc10ce8 1857 static char buf[64];
e3d0f0c0
JP
1858 char *ifname = NULL;
1859
1860 if (dl->no_nice_names || !try_nice ||
1861 ifname_map_rev_lookup(dl, bus_name, dev_name,
1862 port_index, &ifname) != 0)
1863 sprintf(buf, "%s/%s/%d", bus_name, dev_name, port_index);
1864 else
1865 sprintf(buf, "%s", ifname);
1866
1867 if (dl->json_output) {
1868 if (array) {
1869 if (should_arr_last_port_handle_end(dl, bus_name,
1870 dev_name,
1871 port_index))
b20555e4 1872 close_json_array(PRINT_JSON, NULL);
e3d0f0c0
JP
1873 if (should_arr_last_port_handle_start(dl, bus_name,
1874 dev_name,
1875 port_index)) {
b20555e4
RD
1876 open_json_array(PRINT_JSON, buf);
1877 open_json_object(NULL);
e3d0f0c0
JP
1878 arr_last_port_handle_set(dl, bus_name, dev_name,
1879 port_index);
1880 } else {
b20555e4 1881 open_json_object(NULL);
e3d0f0c0
JP
1882 }
1883 } else {
b20555e4 1884 open_json_object(buf);
e3d0f0c0
JP
1885 }
1886 } else {
1887 pr_out("%s:", buf);
1888 }
1889}
1890
1891static void pr_out_port_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice)
e6d7367d
JP
1892{
1893 const char *bus_name;
1894 const char *dev_name;
1895 uint32_t port_index;
1896
1897 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
1898 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
1899 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
e3d0f0c0
JP
1900 __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, false);
1901}
1902
1903static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool try_nice)
1904{
1905 const char *bus_name;
1906 const char *dev_name;
1907 uint32_t port_index;
e6d7367d 1908
e3d0f0c0
JP
1909 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
1910 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
1911 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
1912 __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, true);
e6d7367d
JP
1913}
1914
e3d0f0c0 1915static void pr_out_port_handle_end(struct dl *dl)
a3c4b484 1916{
e3d0f0c0 1917 if (dl->json_output)
b20555e4 1918 close_json_object();
e3d0f0c0
JP
1919 else
1920 pr_out("\n");
1921}
1922
43eb8728
DA
1923static void pr_out_u64(struct dl *dl, const char *name, uint64_t val)
1924{
56b725a4 1925 __pr_out_indent_newline(dl);
43eb8728 1926 if (val == (uint64_t) -1)
3666eb0e 1927 return print_string_name_value(name, "unlimited");
43eb8728 1928
56b725a4 1929 if (dl->json_output)
b20555e4 1930 print_u64(PRINT_JSON, name, NULL, val);
56b725a4
AL
1931 else
1932 pr_out("%s %"PRIu64, name, val);
43eb8728
DA
1933}
1934
1d05cca2
AL
1935static bool is_binary_eol(int i)
1936{
1937 return !(i%16);
1938}
1939
844a6176
AL
1940static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len)
1941{
1d05cca2 1942 int i = 0;
844a6176 1943
844a6176 1944 while (i < len) {
1d05cca2 1945 if (dl->json_output)
b20555e4 1946 print_int(PRINT_JSON, NULL, NULL, data[i]);
1d05cca2
AL
1947 else
1948 pr_out("%02x ", data[i]);
844a6176 1949 i++;
1d05cca2
AL
1950 if (!dl->json_output && is_binary_eol(i))
1951 __pr_out_newline();
844a6176 1952 }
f359942a 1953 if (!dl->json_output && !is_binary_eol(i))
1d05cca2 1954 __pr_out_newline();
844a6176
AL
1955}
1956
844a6176
AL
1957static void pr_out_name(struct dl *dl, const char *name)
1958{
7dd3d51b 1959 __pr_out_indent_newline(dl);
844a6176 1960 if (dl->json_output)
b20555e4 1961 print_string(PRINT_JSON, name, NULL, NULL);
844a6176 1962 else
7dd3d51b 1963 pr_out("%s:", name);
844a6176
AL
1964}
1965
8b4fbf0b
AV
1966static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr)
1967{
1968 if (dl->json_output) {
b20555e4
RD
1969 print_uint(PRINT_JSON, "address", NULL, addr);
1970 open_json_array(PRINT_JSON, "data");
8b4fbf0b
AV
1971 }
1972}
1973
1974static void pr_out_region_chunk_end(struct dl *dl)
1975{
1976 if (dl->json_output)
b20555e4 1977 close_json_array(PRINT_JSON, NULL);
8b4fbf0b
AV
1978}
1979
1980static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len,
1981 uint64_t addr)
1982{
1983 static uint64_t align_val;
1984 uint32_t i = 0;
1985
1986 pr_out_region_chunk_start(dl, addr);
1987 while (i < len) {
1988 if (!dl->json_output)
1989 if (!(align_val % 16))
1990 pr_out("%s%016"PRIx64" ",
1991 align_val ? "\n" : "",
1992 addr);
1993
1994 align_val++;
1995
1996 if (dl->json_output)
b20555e4 1997 print_int(PRINT_JSON, NULL, NULL, data[i]);
8b4fbf0b
AV
1998 else
1999 pr_out("%02x ", data[i]);
2000
2001 addr++;
2002 i++;
2003 }
2004 pr_out_region_chunk_end(dl);
2005}
2006
e3d0f0c0
JP
2007static void pr_out_section_start(struct dl *dl, const char *name)
2008{
2009 if (dl->json_output) {
b20555e4
RD
2010 open_json_object(NULL);
2011 open_json_object(name);
e3d0f0c0
JP
2012 }
2013}
2014
2015static void pr_out_section_end(struct dl *dl)
2016{
2017 if (dl->json_output) {
2018 if (dl->arr_last.present)
b20555e4
RD
2019 close_json_array(PRINT_JSON, NULL);
2020 close_json_object();
2021 close_json_object();
e3d0f0c0 2022 }
a3c4b484
JP
2023}
2024
153c1a9b
AS
2025static void pr_out_array_start(struct dl *dl, const char *name)
2026{
2027 if (dl->json_output) {
b20555e4 2028 open_json_array(PRINT_JSON, name);
153c1a9b 2029 } else {
844646a5 2030 __pr_out_indent_inc();
153c1a9b 2031 __pr_out_newline();
844646a5 2032 pr_out("%s:", name);
153c1a9b 2033 __pr_out_indent_inc();
844646a5 2034 __pr_out_newline();
153c1a9b
AS
2035 }
2036}
2037
2038static void pr_out_array_end(struct dl *dl)
2039{
844646a5 2040 if (dl->json_output) {
b20555e4 2041 close_json_array(PRINT_JSON, NULL);
844646a5
AS
2042 } else {
2043 __pr_out_indent_dec();
153c1a9b 2044 __pr_out_indent_dec();
844646a5 2045 }
153c1a9b
AS
2046}
2047
05bc89e9
JK
2048static void pr_out_object_start(struct dl *dl, const char *name)
2049{
2050 if (dl->json_output) {
b20555e4 2051 open_json_object(name);
05bc89e9
JK
2052 } else {
2053 __pr_out_indent_inc();
2054 __pr_out_newline();
2055 pr_out("%s:", name);
2056 __pr_out_indent_inc();
2057 __pr_out_newline();
2058 }
2059}
2060
2061static void pr_out_object_end(struct dl *dl)
2062{
2063 if (dl->json_output) {
b20555e4 2064 close_json_object();
05bc89e9
JK
2065 } else {
2066 __pr_out_indent_dec();
2067 __pr_out_indent_dec();
2068 }
2069}
2070
153c1a9b
AS
2071static void pr_out_entry_start(struct dl *dl)
2072{
2073 if (dl->json_output)
b20555e4 2074 open_json_object(NULL);
153c1a9b
AS
2075}
2076
2077static void pr_out_entry_end(struct dl *dl)
2078{
2079 if (dl->json_output)
b20555e4 2080 close_json_object();
153c1a9b
AS
2081 else
2082 __pr_out_newline();
2083}
2084
ef12d6da
IS
2085static void pr_out_stats(struct dl *dl, struct nlattr *nla_stats)
2086{
2087 struct nlattr *tb[DEVLINK_ATTR_STATS_MAX + 1] = {};
2088 int err;
2089
2090 if (!dl->stats)
2091 return;
2092
2093 err = mnl_attr_parse_nested(nla_stats, attr_stats_cb, tb);
2094 if (err != MNL_CB_OK)
2095 return;
2096
2097 pr_out_object_start(dl, "stats");
2098 pr_out_object_start(dl, "rx");
2099 if (tb[DEVLINK_ATTR_STATS_RX_BYTES])
2100 pr_out_u64(dl, "bytes",
2101 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_BYTES]));
2102 if (tb[DEVLINK_ATTR_STATS_RX_PACKETS])
2103 pr_out_u64(dl, "packets",
2104 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_PACKETS]));
a66af556
IS
2105 if (tb[DEVLINK_ATTR_STATS_RX_DROPPED])
2106 pr_out_u64(dl, "dropped",
2107 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_DROPPED]));
ef12d6da
IS
2108 pr_out_object_end(dl);
2109 pr_out_object_end(dl);
2110}
2111
13925ae9
MS
2112static const char *param_cmode_name(uint8_t cmode)
2113{
2114 switch (cmode) {
2115 case DEVLINK_PARAM_CMODE_RUNTIME:
2116 return PARAM_CMODE_RUNTIME_STR;
2117 case DEVLINK_PARAM_CMODE_DRIVERINIT:
2118 return PARAM_CMODE_DRIVERINIT_STR;
2119 case DEVLINK_PARAM_CMODE_PERMANENT:
2120 return PARAM_CMODE_PERMANENT_STR;
2121 default: return "<unknown type>";
2122 }
2123}
2124
f57856fa
OG
2125static const char *eswitch_mode_name(uint32_t mode)
2126{
2127 switch (mode) {
2128 case DEVLINK_ESWITCH_MODE_LEGACY: return ESWITCH_MODE_LEGACY;
2129 case DEVLINK_ESWITCH_MODE_SWITCHDEV: return ESWITCH_MODE_SWITCHDEV;
2130 default: return "<unknown mode>";
2131 }
2132}
2133
6566ca8c
RD
2134static const char *eswitch_inline_mode_name(uint32_t mode)
2135{
2136 switch (mode) {
2137 case DEVLINK_ESWITCH_INLINE_MODE_NONE:
2138 return ESWITCH_INLINE_MODE_NONE;
2139 case DEVLINK_ESWITCH_INLINE_MODE_LINK:
2140 return ESWITCH_INLINE_MODE_LINK;
2141 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
2142 return ESWITCH_INLINE_MODE_NETWORK;
2143 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
2144 return ESWITCH_INLINE_MODE_TRANSPORT;
2145 default:
2146 return "<unknown mode>";
2147 }
2148}
2149
f57856fa
OG
2150static void pr_out_eswitch(struct dl *dl, struct nlattr **tb)
2151{
2152 __pr_out_handle_start(dl, tb, true, false);
2153
3666eb0e
RD
2154 if (tb[DEVLINK_ATTR_ESWITCH_MODE]) {
2155 check_indent_newline(dl);
2156 print_string(PRINT_ANY, "mode", "mode %s",
2157 eswitch_mode_name(mnl_attr_get_u16(
2158 tb[DEVLINK_ATTR_ESWITCH_MODE])));
2159 }
2160 if (tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2161 check_indent_newline(dl);
2162 print_string(PRINT_ANY, "inline-mode", "inline-mode %s",
2163 eswitch_inline_mode_name(mnl_attr_get_u8(
2164 tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE])));
2165 }
d315b706
RD
2166 if (tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2167 bool encap_mode = !!mnl_attr_get_u8(tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2168
3666eb0e
RD
2169 check_indent_newline(dl);
2170 print_string(PRINT_ANY, "encap", "encap %s",
2171 encap_mode ? "enable" : "disable");
d315b706
RD
2172 }
2173
f57856fa
OG
2174 pr_out_handle_end(dl);
2175}
2176
2177static int cmd_dev_eswitch_show_cb(const struct nlmsghdr *nlh, void *data)
2178{
2179 struct dl *dl = data;
2180 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2181 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2182
2183 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2184 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
2185 return MNL_CB_ERROR;
2186 pr_out_eswitch(dl, tb);
2187 return MNL_CB_OK;
2188}
2189
2190static int cmd_dev_eswitch_show(struct dl *dl)
2191{
2192 struct nlmsghdr *nlh;
2193 int err;
2194
cdd2f7cc 2195 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_GET,
f57856fa
OG
2196 NLM_F_REQUEST | NLM_F_ACK);
2197
2198 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
2199 if (err)
2200 return err;
2201
2202 pr_out_section_start(dl, "dev");
2203 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_eswitch_show_cb, dl);
2204 pr_out_section_end(dl);
2205 return err;
2206}
2207
2208static int cmd_dev_eswitch_set(struct dl *dl)
2209{
2210 struct nlmsghdr *nlh;
2211 int err;
2212
cdd2f7cc 2213 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_SET,
f57856fa
OG
2214 NLM_F_REQUEST | NLM_F_ACK);
2215
6566ca8c
RD
2216 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE,
2217 DL_OPT_ESWITCH_MODE |
d315b706
RD
2218 DL_OPT_ESWITCH_INLINE_MODE |
2219 DL_OPT_ESWITCH_ENCAP_MODE);
6566ca8c 2220
f57856fa
OG
2221 if (err)
2222 return err;
2223
6566ca8c
RD
2224 if (dl->opts.present == 1) {
2225 pr_err("Need to set at least one option\n");
2226 return -ENOENT;
2227 }
2228
f57856fa
OG
2229 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
2230}
2231
2232static int cmd_dev_eswitch(struct dl *dl)
2233{
a93b6bb3
RD
2234 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
2235 cmd_dev_help();
2236 return 0;
2237 } else if (dl_argv_match(dl, "set")) {
f57856fa
OG
2238 dl_arg_inc(dl);
2239 return cmd_dev_eswitch_set(dl);
2240 } else if (dl_argv_match(dl, "show")) {
2241 dl_arg_inc(dl);
2242 return cmd_dev_eswitch_show(dl);
2243 }
2244 pr_err("Command \"%s\" not found\n", dl_argv(dl));
2245 return -ENOENT;
2246}
2247
2557dca2
ST
2248struct param_val_conv {
2249 const char *name;
2250 const char *vstr;
2251 uint32_t vuint;
2252};
2253
2254static bool param_val_conv_exists(const struct param_val_conv *param_val_conv,
2255 uint32_t len, const char *name)
2256{
2257 uint32_t i;
2258
2259 for (i = 0; i < len; i++)
2260 if (!strcmp(param_val_conv[i].name, name))
2261 return true;
2262
2263 return false;
2264}
2265
2266static int
2267param_val_conv_uint_get(const struct param_val_conv *param_val_conv,
2268 uint32_t len, const char *name, const char *vstr,
2269 uint32_t *vuint)
2270{
2271 uint32_t i;
2272
2273 for (i = 0; i < len; i++)
2274 if (!strcmp(param_val_conv[i].name, name) &&
2275 !strcmp(param_val_conv[i].vstr, vstr)) {
2276 *vuint = param_val_conv[i].vuint;
2277 return 0;
2278 }
2279
2280 return -ENOENT;
2281}
2282
2283static int
2284param_val_conv_str_get(const struct param_val_conv *param_val_conv,
2285 uint32_t len, const char *name, uint32_t vuint,
2286 const char **vstr)
2287{
2288 uint32_t i;
2289
2290 for (i = 0; i < len; i++)
2291 if (!strcmp(param_val_conv[i].name, name) &&
2292 param_val_conv[i].vuint == vuint) {
2293 *vstr = param_val_conv[i].vstr;
2294 return 0;
2295 }
2296
2297 return -ENOENT;
2298}
2299
a463fd4f
ST
2300static const struct param_val_conv param_val_conv[] = {
2301 {
2302 .name = "fw_load_policy",
2303 .vstr = "driver",
2304 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER,
2305 },
2306 {
2307 .name = "fw_load_policy",
2308 .vstr = "flash",
2309 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH,
2310 },
c240e674
DM
2311 {
2312 .name = "reset_dev_on_drv_probe",
2313 .vstr = "unknown",
2314 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN,
2315 },
850de16f
DM
2316 {
2317 .name = "fw_load_policy",
2318 .vstr = "unknown",
2319 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN,
2320 },
c240e674
DM
2321 {
2322 .name = "reset_dev_on_drv_probe",
2323 .vstr = "always",
2324 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS,
2325 },
2326 {
2327 .name = "reset_dev_on_drv_probe",
2328 .vstr = "never",
2329 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER,
2330 },
2331 {
2332 .name = "reset_dev_on_drv_probe",
2333 .vstr = "disk",
2334 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK,
2335 },
a463fd4f 2336};
2557dca2
ST
2337
2338#define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2339
2340static void pr_out_param_value(struct dl *dl, const char *nla_name,
2341 int nla_type, struct nlattr *nl)
13925ae9
MS
2342{
2343 struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
2344 struct nlattr *val_attr;
2557dca2
ST
2345 const char *vstr;
2346 bool conv_exists;
13925ae9
MS
2347 int err;
2348
2349 err = mnl_attr_parse_nested(nl, attr_cb, nla_value);
2350 if (err != MNL_CB_OK)
2351 return;
2352
2353 if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
2354 (nla_type != MNL_TYPE_FLAG &&
2355 !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
2356 return;
2357
3666eb0e
RD
2358 check_indent_newline(dl);
2359 print_string(PRINT_ANY, "cmode", "cmode %s",
2360 param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE])));
2361
13925ae9
MS
2362 val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
2363
2557dca2
ST
2364 conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
2365 nla_name);
2366
13925ae9
MS
2367 switch (nla_type) {
2368 case MNL_TYPE_U8:
2557dca2
ST
2369 if (conv_exists) {
2370 err = param_val_conv_str_get(param_val_conv,
2371 PARAM_VAL_CONV_LEN,
2372 nla_name,
2373 mnl_attr_get_u8(val_attr),
2374 &vstr);
2375 if (err)
2376 return;
3666eb0e 2377 print_string(PRINT_ANY, "value", " value %s", vstr);
2557dca2 2378 } else {
ff360fe9
RD
2379 print_uint(PRINT_ANY, "value", " value %u",
2380 mnl_attr_get_u8(val_attr));
2557dca2 2381 }
13925ae9
MS
2382 break;
2383 case MNL_TYPE_U16:
2557dca2
ST
2384 if (conv_exists) {
2385 err = param_val_conv_str_get(param_val_conv,
2386 PARAM_VAL_CONV_LEN,
2387 nla_name,
2388 mnl_attr_get_u16(val_attr),
2389 &vstr);
2390 if (err)
2391 return;
3666eb0e 2392 print_string(PRINT_ANY, "value", " value %s", vstr);
2557dca2 2393 } else {
ff360fe9
RD
2394 print_uint(PRINT_ANY, "value", " value %u",
2395 mnl_attr_get_u16(val_attr));
2557dca2 2396 }
13925ae9
MS
2397 break;
2398 case MNL_TYPE_U32:
2557dca2
ST
2399 if (conv_exists) {
2400 err = param_val_conv_str_get(param_val_conv,
2401 PARAM_VAL_CONV_LEN,
2402 nla_name,
2403 mnl_attr_get_u32(val_attr),
2404 &vstr);
2405 if (err)
2406 return;
3666eb0e 2407 print_string(PRINT_ANY, "value", " value %s", vstr);
2557dca2 2408 } else {
ff360fe9
RD
2409 print_uint(PRINT_ANY, "value", " value %u",
2410 mnl_attr_get_u32(val_attr));
2557dca2 2411 }
13925ae9
MS
2412 break;
2413 case MNL_TYPE_STRING:
3666eb0e
RD
2414 print_string(PRINT_ANY, "value", " value %s",
2415 mnl_attr_get_str(val_attr));
13925ae9
MS
2416 break;
2417 case MNL_TYPE_FLAG:
ff360fe9 2418 print_bool(PRINT_ANY, "value", " value %s", val_attr);
13925ae9
MS
2419 break;
2420 }
2421}
2422
2423static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array)
2424{
2425 struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {};
2426 struct nlattr *param_value_attr;
2557dca2 2427 const char *nla_name;
13925ae9
MS
2428 int nla_type;
2429 int err;
2430
2431 err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param);
2432 if (err != MNL_CB_OK)
2433 return;
2434 if (!nla_param[DEVLINK_ATTR_PARAM_NAME] ||
2435 !nla_param[DEVLINK_ATTR_PARAM_TYPE] ||
2436 !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST])
2437 return;
2438
2439 if (array)
2440 pr_out_handle_start_arr(dl, tb);
2441 else
2442 __pr_out_handle_start(dl, tb, true, false);
2443
2444 nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]);
2445
2557dca2 2446 nla_name = mnl_attr_get_str(nla_param[DEVLINK_ATTR_PARAM_NAME]);
3666eb0e
RD
2447 check_indent_newline(dl);
2448 print_string(PRINT_ANY, "name", "name %s ", nla_name);
13925ae9 2449 if (!nla_param[DEVLINK_ATTR_PARAM_GENERIC])
3666eb0e 2450 print_string(PRINT_ANY, "type", "type %s", "driver-specific");
13925ae9 2451 else
3666eb0e 2452 print_string(PRINT_ANY, "type", "type %s", "generic");
13925ae9
MS
2453
2454 pr_out_array_start(dl, "values");
2455 mnl_attr_for_each_nested(param_value_attr,
2456 nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) {
2457 pr_out_entry_start(dl);
2557dca2 2458 pr_out_param_value(dl, nla_name, nla_type, param_value_attr);
13925ae9
MS
2459 pr_out_entry_end(dl);
2460 }
2461 pr_out_array_end(dl);
2462 pr_out_handle_end(dl);
2463}
2464
2465static int cmd_dev_param_show_cb(const struct nlmsghdr *nlh, void *data)
2466{
2467 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2468 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2469 struct dl *dl = data;
2470
2471 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2472 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
2473 !tb[DEVLINK_ATTR_PARAM])
2474 return MNL_CB_ERROR;
2475 pr_out_param(dl, tb, true);
2476 return MNL_CB_OK;
2477}
2478
2479struct param_ctx {
2480 struct dl *dl;
2481 int nla_type;
2482 union {
2483 uint8_t vu8;
2484 uint16_t vu16;
2485 uint32_t vu32;
2486 const char *vstr;
2487 bool vbool;
2488 } value;
2489};
2490
2491static int cmd_dev_param_set_cb(const struct nlmsghdr *nlh, void *data)
2492{
2493 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2494 struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {};
2495 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2496 struct nlattr *param_value_attr;
2497 enum devlink_param_cmode cmode;
2498 struct param_ctx *ctx = data;
2499 struct dl *dl = ctx->dl;
2500 int nla_type;
2501 int err;
2502
2503 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2504 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
2505 !tb[DEVLINK_ATTR_PARAM])
2506 return MNL_CB_ERROR;
2507
2508 err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param);
2509 if (err != MNL_CB_OK)
2510 return MNL_CB_ERROR;
2511
2512 if (!nla_param[DEVLINK_ATTR_PARAM_TYPE] ||
2513 !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST])
2514 return MNL_CB_ERROR;
2515
2516 nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]);
2517 mnl_attr_for_each_nested(param_value_attr,
2518 nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) {
2519 struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
2520 struct nlattr *val_attr;
2521
2522 err = mnl_attr_parse_nested(param_value_attr,
2523 attr_cb, nla_value);
2524 if (err != MNL_CB_OK)
2525 return MNL_CB_ERROR;
2526
2527 if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
2528 (nla_type != MNL_TYPE_FLAG &&
2529 !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
2530 return MNL_CB_ERROR;
2531
2532 cmode = mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
2533 if (cmode == dl->opts.cmode) {
2534 val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
2535 switch (nla_type) {
2536 case MNL_TYPE_U8:
2537 ctx->value.vu8 = mnl_attr_get_u8(val_attr);
2538 break;
2539 case MNL_TYPE_U16:
2540 ctx->value.vu16 = mnl_attr_get_u16(val_attr);
2541 break;
2542 case MNL_TYPE_U32:
2543 ctx->value.vu32 = mnl_attr_get_u32(val_attr);
2544 break;
2545 case MNL_TYPE_STRING:
2546 ctx->value.vstr = mnl_attr_get_str(val_attr);
2547 break;
2548 case MNL_TYPE_FLAG:
2549 ctx->value.vbool = val_attr ? true : false;
2550 break;
2551 }
2552 break;
2553 }
2554 }
2555 ctx->nla_type = nla_type;
2556 return MNL_CB_OK;
2557}
2558
2559static int cmd_dev_param_set(struct dl *dl)
2560{
2561 struct param_ctx ctx = {};
2562 struct nlmsghdr *nlh;
2557dca2 2563 bool conv_exists;
13925ae9
MS
2564 uint32_t val_u32;
2565 uint16_t val_u16;
2566 uint8_t val_u8;
2567 bool val_bool;
2568 int err;
2569
2570 err = dl_argv_parse(dl, DL_OPT_HANDLE |
2571 DL_OPT_PARAM_NAME |
2572 DL_OPT_PARAM_VALUE |
2573 DL_OPT_PARAM_CMODE, 0);
2574 if (err)
2575 return err;
2576
2577 /* Get value type */
2578 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET,
2579 NLM_F_REQUEST | NLM_F_ACK);
2580 dl_opts_put(nlh, dl);
2581
2582 ctx.dl = dl;
2583 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_set_cb, &ctx);
2584 if (err)
2585 return err;
2586
2587 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_SET,
2588 NLM_F_REQUEST | NLM_F_ACK);
2589 dl_opts_put(nlh, dl);
2590
2557dca2
ST
2591 conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
2592 dl->opts.param_name);
2593
13925ae9
MS
2594 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_TYPE, ctx.nla_type);
2595 switch (ctx.nla_type) {
2596 case MNL_TYPE_U8:
2557dca2
ST
2597 if (conv_exists) {
2598 err = param_val_conv_uint_get(param_val_conv,
2599 PARAM_VAL_CONV_LEN,
2600 dl->opts.param_name,
2601 dl->opts.param_value,
2602 &val_u32);
2603 val_u8 = val_u32;
2604 } else {
2605 err = strtouint8_t(dl->opts.param_value, &val_u8);
2606 }
13925ae9
MS
2607 if (err)
2608 goto err_param_value_parse;
2609 if (val_u8 == ctx.value.vu8)
2610 return 0;
2611 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u8);
2612 break;
2613 case MNL_TYPE_U16:
2557dca2
ST
2614 if (conv_exists) {
2615 err = param_val_conv_uint_get(param_val_conv,
2616 PARAM_VAL_CONV_LEN,
2617 dl->opts.param_name,
2618 dl->opts.param_value,
2619 &val_u32);
2620 val_u16 = val_u32;
2621 } else {
2622 err = strtouint16_t(dl->opts.param_value, &val_u16);
2623 }
13925ae9
MS
2624 if (err)
2625 goto err_param_value_parse;
2626 if (val_u16 == ctx.value.vu16)
2627 return 0;
2628 mnl_attr_put_u16(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u16);
2629 break;
2630 case MNL_TYPE_U32:
2557dca2
ST
2631 if (conv_exists)
2632 err = param_val_conv_uint_get(param_val_conv,
2633 PARAM_VAL_CONV_LEN,
2634 dl->opts.param_name,
2635 dl->opts.param_value,
2636 &val_u32);
2637 else
2638 err = strtouint32_t(dl->opts.param_value, &val_u32);
13925ae9
MS
2639 if (err)
2640 goto err_param_value_parse;
2641 if (val_u32 == ctx.value.vu32)
2642 return 0;
2643 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32);
2644 break;
2645 case MNL_TYPE_FLAG:
2646 err = strtobool(dl->opts.param_value, &val_bool);
2647 if (err)
2648 goto err_param_value_parse;
2649 if (val_bool == ctx.value.vbool)
2650 return 0;
2651 if (val_bool)
2652 mnl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
2653 0, NULL);
2654 break;
2655 case MNL_TYPE_STRING:
2656 mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
2657 dl->opts.param_value);
2658 if (!strcmp(dl->opts.param_value, ctx.value.vstr))
2659 return 0;
2660 break;
2661 default:
2662 printf("Value type not supported\n");
2663 return -ENOTSUP;
2664 }
2665 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
2666
2667err_param_value_parse:
2668 pr_err("Value \"%s\" is not a number or not within range\n",
2669 dl->opts.param_value);
2670 return err;
2671}
2672
2673static int cmd_dev_param_show(struct dl *dl)
2674{
2675 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
2676 struct nlmsghdr *nlh;
2677 int err;
2678
2679 if (dl_argc(dl) == 0)
2680 flags |= NLM_F_DUMP;
2681
2682 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET, flags);
2683
2684 if (dl_argc(dl) > 0) {
2685 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE |
2686 DL_OPT_PARAM_NAME, 0);
2687 if (err)
2688 return err;
2689 }
2690
2691 pr_out_section_start(dl, "param");
2692 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_show_cb, dl);
2693 pr_out_section_end(dl);
2694 return err;
2695}
2696
2697static int cmd_dev_param(struct dl *dl)
2698{
2699 if (dl_argv_match(dl, "help")) {
2700 cmd_dev_help();
2701 return 0;
2702 } else if (dl_argv_match(dl, "show") ||
2703 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
2704 dl_arg_inc(dl);
2705 return cmd_dev_param_show(dl);
2706 } else if (dl_argv_match(dl, "set")) {
2707 dl_arg_inc(dl);
2708 return cmd_dev_param_set(dl);
2709 }
2710 pr_err("Command \"%s\" not found\n", dl_argv(dl));
2711 return -ENOENT;
2712}
a3c4b484
JP
2713static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)
2714{
e3d0f0c0 2715 struct dl *dl = data;
a3c4b484
JP
2716 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2717 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
88fdbf80 2718 uint8_t reload_failed = 0;
a3c4b484
JP
2719
2720 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2721 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
2722 return MNL_CB_ERROR;
88fdbf80
JP
2723
2724 if (tb[DEVLINK_ATTR_RELOAD_FAILED])
2725 reload_failed = mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED]);
2726
2727 if (reload_failed) {
2728 __pr_out_handle_start(dl, tb, true, false);
ff360fe9
RD
2729 check_indent_newline(dl);
2730 print_bool(PRINT_ANY, "reload_failed", "reload_failed %s", true);
88fdbf80
JP
2731 pr_out_handle_end(dl);
2732 } else {
2733 pr_out_handle(dl, tb);
2734 }
2735
a3c4b484
JP
2736 return MNL_CB_OK;
2737}
2738
2739static int cmd_dev_show(struct dl *dl)
2740{
2741 struct nlmsghdr *nlh;
2742 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
2743 int err;
2744
2745 if (dl_argc(dl) == 0)
2746 flags |= NLM_F_DUMP;
2747
2748 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_GET, flags);
2749
2750 if (dl_argc(dl) > 0) {
2751 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
2752 if (err)
2753 return err;
2754 }
2755
e3d0f0c0
JP
2756 pr_out_section_start(dl, "dev");
2757 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_show_cb, dl);
2758 pr_out_section_end(dl);
2759 return err;
a3c4b484
JP
2760}
2761
06dd94f9
AS
2762static void cmd_dev_reload_help(void)
2763{
08e8e1ca 2764 pr_err("Usage: devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
06dd94f9
AS
2765}
2766
2767static int cmd_dev_reload(struct dl *dl)
2768{
2769 struct nlmsghdr *nlh;
2770 int err;
2771
2772 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
2773 cmd_dev_reload_help();
2774 return 0;
2775 }
2776
2777 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RELOAD,
2778 NLM_F_REQUEST | NLM_F_ACK);
2779
08e8e1ca 2780 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_NETNS);
06dd94f9
AS
2781 if (err)
2782 return err;
2783
2784 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
2785}
2786
05bc89e9
JK
2787static void pr_out_versions_single(struct dl *dl, const struct nlmsghdr *nlh,
2788 const char *name, int type)
2789{
2790 struct nlattr *version;
2791
2792 mnl_attr_for_each(version, nlh, sizeof(struct genlmsghdr)) {
2793 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2794 const char *ver_value;
2795 const char *ver_name;
2796 int err;
2797
2798 if (mnl_attr_get_type(version) != type)
2799 continue;
2800
2801 err = mnl_attr_parse_nested(version, attr_cb, tb);
2802 if (err != MNL_CB_OK)
2803 continue;
2804
2805 if (!tb[DEVLINK_ATTR_INFO_VERSION_NAME] ||
2806 !tb[DEVLINK_ATTR_INFO_VERSION_VALUE])
2807 continue;
2808
2809 if (name) {
2810 pr_out_object_start(dl, name);
2811 name = NULL;
2812 }
2813
2814 ver_name = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_NAME]);
2815 ver_value = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_VALUE]);
2816
3666eb0e
RD
2817 check_indent_newline(dl);
2818 print_string_name_value(ver_name, ver_value);
05bc89e9
JK
2819 if (!dl->json_output)
2820 __pr_out_newline();
2821 }
2822
2823 if (!name)
2824 pr_out_object_end(dl);
2825}
2826
2827static void pr_out_info(struct dl *dl, const struct nlmsghdr *nlh,
2828 struct nlattr **tb, bool has_versions)
2829{
2830 __pr_out_handle_start(dl, tb, true, false);
2831
2832 __pr_out_indent_inc();
2833 if (tb[DEVLINK_ATTR_INFO_DRIVER_NAME]) {
2834 struct nlattr *nla_drv = tb[DEVLINK_ATTR_INFO_DRIVER_NAME];
2835
2836 if (!dl->json_output)
2837 __pr_out_newline();
3666eb0e
RD
2838 check_indent_newline(dl);
2839 print_string(PRINT_ANY, "driver", "driver %s",
2840 mnl_attr_get_str(nla_drv));
05bc89e9
JK
2841 }
2842
2843 if (tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER]) {
2844 struct nlattr *nla_sn = tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER];
2845
2846 if (!dl->json_output)
2847 __pr_out_newline();
3666eb0e
RD
2848 check_indent_newline(dl);
2849 print_string(PRINT_ANY, "serial_number", "serial_number %s",
2850 mnl_attr_get_str(nla_sn));
05bc89e9
JK
2851 }
2852 __pr_out_indent_dec();
2853
2854 if (has_versions) {
2855 pr_out_object_start(dl, "versions");
2856
2857 pr_out_versions_single(dl, nlh, "fixed",
2858 DEVLINK_ATTR_INFO_VERSION_FIXED);
2859 pr_out_versions_single(dl, nlh, "running",
2860 DEVLINK_ATTR_INFO_VERSION_RUNNING);
2861 pr_out_versions_single(dl, nlh, "stored",
2862 DEVLINK_ATTR_INFO_VERSION_STORED);
2863
2864 pr_out_object_end(dl);
2865 }
2866
2867 pr_out_handle_end(dl);
2868}
2869
2870static int cmd_versions_show_cb(const struct nlmsghdr *nlh, void *data)
2871{
2872 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2873 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2874 bool has_versions, has_info;
2875 struct dl *dl = data;
2876
2877 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2878
2879 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
2880 return MNL_CB_ERROR;
2881
2882 has_versions = tb[DEVLINK_ATTR_INFO_VERSION_FIXED] ||
2883 tb[DEVLINK_ATTR_INFO_VERSION_RUNNING] ||
2884 tb[DEVLINK_ATTR_INFO_VERSION_STORED];
2885 has_info = tb[DEVLINK_ATTR_INFO_DRIVER_NAME] ||
2886 tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER] ||
2887 has_versions;
2888
2889 if (has_info)
2890 pr_out_info(dl, nlh, tb, has_versions);
2891
2892 return MNL_CB_OK;
2893}
2894
2895static void cmd_dev_info_help(void)
2896{
2897 pr_err("Usage: devlink dev info [ DEV ]\n");
2898}
2899
2900static int cmd_dev_info(struct dl *dl)
2901{
2902 struct nlmsghdr *nlh;
2903 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
2904 int err;
2905
2906 if (dl_argv_match(dl, "help")) {
2907 cmd_dev_info_help();
2908 return 0;
2909 }
2910
2911 if (dl_argc(dl) == 0)
2912 flags |= NLM_F_DUMP;
2913
2914 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_INFO_GET, flags);
2915
2916 if (dl_argc(dl) > 0) {
2917 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
2918 if (err)
2919 return err;
2920 }
2921
2922 pr_out_section_start(dl, "info");
2923 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_versions_show_cb, dl);
2924 pr_out_section_end(dl);
2925 return err;
2926}
2927
d326d79c
JK
2928static void cmd_dev_flash_help(void)
2929{
2930 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2931}
2932
9b13cddf
JP
2933
2934struct cmd_dev_flash_status_ctx {
2935 struct dl *dl;
2936 char *last_msg;
2937 char *last_component;
2938 uint8_t not_first:1,
2939 last_pc:1,
2940 received_end:1,
2941 flash_done:1;
2942};
2943
2944static int nullstrcmp(const char *str1, const char *str2)
2945{
2946 if (str1 && str2)
2947 return strcmp(str1, str2);
2948 if (!str1 && !str2)
2949 return 0;
2950 return str1 ? 1 : -1;
2951}
2952
2953static int cmd_dev_flash_status_cb(const struct nlmsghdr *nlh, void *data)
2954{
2955 struct cmd_dev_flash_status_ctx *ctx = data;
2956 struct dl_opts *opts = &ctx->dl->opts;
2957 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2958 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2959 const char *component = NULL;
2960 uint64_t done = 0, total = 0;
2961 const char *msg = NULL;
2962 const char *bus_name;
2963 const char *dev_name;
2964
2965 if (genl->cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS &&
2966 genl->cmd != DEVLINK_CMD_FLASH_UPDATE_END)
2967 return MNL_CB_STOP;
2968
2969 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2970 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
2971 return MNL_CB_ERROR;
2972 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
2973 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
2974 if (strcmp(bus_name, opts->bus_name) ||
2975 strcmp(dev_name, opts->dev_name))
2976 return MNL_CB_ERROR;
2977
2978 if (genl->cmd == DEVLINK_CMD_FLASH_UPDATE_END && ctx->not_first) {
2979 pr_out("\n");
2980 free(ctx->last_msg);
2981 free(ctx->last_component);
2982 ctx->received_end = 1;
2983 return MNL_CB_STOP;
2984 }
2985
2986 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG])
2987 msg = mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]);
2988 if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT])
2989 component = mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]);
2990 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE])
2991 done = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]);
2992 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL])
2993 total = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL]);
2994
2995 if (!nullstrcmp(msg, ctx->last_msg) &&
2996 !nullstrcmp(component, ctx->last_component) &&
2997 ctx->last_pc && ctx->not_first) {
2998 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2999 } else {
3000 if (ctx->not_first)
3001 pr_out("\n");
3002 if (component) {
3003 pr_out("[%s] ", component);
3004 free(ctx->last_component);
3005 ctx->last_component = strdup(component);
3006 }
3007 if (msg) {
3008 pr_out("%s", msg);
3009 free(ctx->last_msg);
3010 ctx->last_msg = strdup(msg);
3011 }
3012 }
3013 if (total) {
3014 pr_out_tty(" %3lu%%", (done * 100) / total);
3015 ctx->last_pc = 1;
3016 } else {
3017 ctx->last_pc = 0;
3018 }
3019 fflush(stdout);
3020 ctx->not_first = 1;
3021
3022 return MNL_CB_STOP;
3023}
3024
3025static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx *ctx,
3026 struct mnlg_socket *nlg_ntf,
3027 int pipe_r)
3028{
3029 int nlfd = mnlg_socket_get_fd(nlg_ntf);
3030 fd_set fds[3];
3031 int fdmax;
3032 int i;
3033 int err;
3034 int err2;
3035
3036 for (i = 0; i < 3; i++)
3037 FD_ZERO(&fds[i]);
3038 FD_SET(pipe_r, &fds[0]);
3039 fdmax = pipe_r + 1;
3040 FD_SET(nlfd, &fds[0]);
3041 if (nlfd >= fdmax)
3042 fdmax = nlfd + 1;
3043
3044 while (select(fdmax, &fds[0], &fds[1], &fds[2], NULL) < 0) {
3045 if (errno == EINTR)
3046 continue;
3047 pr_err("select() failed\n");
3048 return -errno;
3049 }
3050 if (FD_ISSET(nlfd, &fds[0])) {
3051 err = _mnlg_socket_recv_run(nlg_ntf,
3052 cmd_dev_flash_status_cb, ctx);
3053 if (err)
3054 return err;
3055 }
3056 if (FD_ISSET(pipe_r, &fds[0])) {
3057 err = read(pipe_r, &err2, sizeof(err2));
3058 if (err == -1) {
3059 pr_err("Failed to read pipe\n");
3060 return -errno;
3061 }
3062 if (err2)
3063 return err2;
3064 ctx->flash_done = 1;
3065 }
3066 return 0;
3067}
3068
3069
d326d79c
JK
3070static int cmd_dev_flash(struct dl *dl)
3071{
9b13cddf
JP
3072 struct cmd_dev_flash_status_ctx ctx = {.dl = dl,};
3073 struct mnlg_socket *nlg_ntf;
d326d79c 3074 struct nlmsghdr *nlh;
9b13cddf
JP
3075 int pipe_r, pipe_w;
3076 int pipe_fds[2];
3077 pid_t pid;
d326d79c
JK
3078 int err;
3079
3080 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3081 cmd_dev_flash_help();
3082 return 0;
3083 }
3084
3085 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_FLASH_UPDATE,
3086 NLM_F_REQUEST | NLM_F_ACK);
3087
3088 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_FLASH_FILE_NAME,
3089 DL_OPT_FLASH_COMPONENT);
3090 if (err)
3091 return err;
3092
9b13cddf
JP
3093 nlg_ntf = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
3094 if (!nlg_ntf)
3095 return err;
3096
3097 err = _mnlg_socket_group_add(nlg_ntf, DEVLINK_GENL_MCGRP_CONFIG_NAME);
3098 if (err)
3099 return err;
3100
3101 err = pipe(pipe_fds);
3102 if (err == -1)
3103 return -errno;
3104 pipe_r = pipe_fds[0];
3105 pipe_w = pipe_fds[1];
3106
3107 pid = fork();
3108 if (pid == -1) {
3109 close(pipe_r);
3110 close(pipe_w);
3111 return -errno;
3112 } else if (!pid) {
3113 /* In child, just execute the flash and pass returned
3114 * value through pipe once it is done.
3115 */
8f9f2b9c
SH
3116 int cc;
3117
9b13cddf
JP
3118 close(pipe_r);
3119 err = _mnlg_socket_send(dl->nlg, nlh);
8f9f2b9c 3120 cc = write(pipe_w, &err, sizeof(err));
9b13cddf 3121 close(pipe_w);
8f9f2b9c 3122 exit(cc != sizeof(err));
9b13cddf
JP
3123 }
3124 close(pipe_w);
3125
3126 do {
3127 err = cmd_dev_flash_fds_process(&ctx, nlg_ntf, pipe_r);
3128 if (err)
3129 goto out;
3130 } while (!ctx.flash_done || (ctx.not_first && !ctx.received_end));
3131
3132 err = _mnlg_socket_recv_run(dl->nlg, NULL, NULL);
3133out:
3134 close(pipe_r);
3135 mnlg_socket_close(nlg_ntf);
3136 return err;
d326d79c
JK
3137}
3138
a3c4b484
JP
3139static int cmd_dev(struct dl *dl)
3140{
3141 if (dl_argv_match(dl, "help")) {
3142 cmd_dev_help();
3143 return 0;
3144 } else if (dl_argv_match(dl, "show") ||
3145 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3146 dl_arg_inc(dl);
3147 return cmd_dev_show(dl);
f57856fa
OG
3148 } else if (dl_argv_match(dl, "eswitch")) {
3149 dl_arg_inc(dl);
3150 return cmd_dev_eswitch(dl);
06dd94f9
AS
3151 } else if (dl_argv_match(dl, "reload")) {
3152 dl_arg_inc(dl);
3153 return cmd_dev_reload(dl);
13925ae9
MS
3154 } else if (dl_argv_match(dl, "param")) {
3155 dl_arg_inc(dl);
3156 return cmd_dev_param(dl);
05bc89e9
JK
3157 } else if (dl_argv_match(dl, "info")) {
3158 dl_arg_inc(dl);
3159 return cmd_dev_info(dl);
d326d79c
JK
3160 } else if (dl_argv_match(dl, "flash")) {
3161 dl_arg_inc(dl);
3162 return cmd_dev_flash(dl);
a3c4b484
JP
3163 }
3164 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3165 return -ENOENT;
3166}
3167
3168static void cmd_port_help(void)
3169{
7a9466db
JP
3170 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3171 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3172 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3173 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
a3c4b484
JP
3174}
3175
3176static const char *port_type_name(uint32_t type)
3177{
3178 switch (type) {
3179 case DEVLINK_PORT_TYPE_NOTSET: return "notset";
3180 case DEVLINK_PORT_TYPE_AUTO: return "auto";
3181 case DEVLINK_PORT_TYPE_ETH: return "eth";
3182 case DEVLINK_PORT_TYPE_IB: return "ib";
3183 default: return "<unknown type>";
3184 }
3185}
3186
852ed605
JP
3187static const char *port_flavour_name(uint16_t flavour)
3188{
3189 switch (flavour) {
3190 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
3191 return "physical";
3192 case DEVLINK_PORT_FLAVOUR_CPU:
3193 return "cpu";
3194 case DEVLINK_PORT_FLAVOUR_DSA:
3195 return "dsa";
05639939
PP
3196 case DEVLINK_PORT_FLAVOUR_PCI_PF:
3197 return "pcipf";
3198 case DEVLINK_PORT_FLAVOUR_PCI_VF:
3199 return "pcivf";
a5c44b82
PP
3200 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
3201 return "virtual";
852ed605
JP
3202 default:
3203 return "<unknown flavour>";
3204 }
3205}
3206
05639939
PP
3207static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
3208{
3209 uint16_t fn_num;
3210
3211 if (tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
3212 fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
ff360fe9 3213 print_uint(PRINT_ANY, "pfnum", " pfnum %u", fn_num);
05639939
PP
3214 }
3215 if (tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]) {
3216 fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]);
ff360fe9 3217 print_uint(PRINT_ANY, "vfnum", " vfnum %u", fn_num);
05639939
PP
3218 }
3219}
3220
e3d0f0c0 3221static void pr_out_port(struct dl *dl, struct nlattr **tb)
a3c4b484
JP
3222{
3223 struct nlattr *pt_attr = tb[DEVLINK_ATTR_PORT_TYPE];
3224 struct nlattr *dpt_attr = tb[DEVLINK_ATTR_PORT_DESIRED_TYPE];
3225
e3d0f0c0 3226 pr_out_port_handle_start(dl, tb, false);
3666eb0e 3227 check_indent_newline(dl);
a3c4b484
JP
3228 if (pt_attr) {
3229 uint16_t port_type = mnl_attr_get_u16(pt_attr);
3230
3666eb0e
RD
3231 print_string(PRINT_ANY, "type", "type %s",
3232 port_type_name(port_type));
a3c4b484
JP
3233 if (dpt_attr) {
3234 uint16_t des_port_type = mnl_attr_get_u16(dpt_attr);
3235
3236 if (port_type != des_port_type)
3666eb0e
RD
3237 print_string(PRINT_ANY, "des_type", " des_type %s",
3238 port_type_name(des_port_type));
a3c4b484
JP
3239 }
3240 }
3666eb0e
RD
3241 if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) {
3242 print_string(PRINT_ANY, "netdev", " netdev %s",
3243 mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]));
3244 }
3245 if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) {
3246 print_string(PRINT_ANY, "ibdev", " ibdev %s",
3247 mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME]));
3248 }
852ed605
JP
3249 if (tb[DEVLINK_ATTR_PORT_FLAVOUR]) {
3250 uint16_t port_flavour =
3251 mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_FLAVOUR]);
3252
3666eb0e
RD
3253 print_string(PRINT_ANY, "flavour", " flavour %s",
3254 port_flavour_name(port_flavour));
05639939
PP
3255
3256 switch (port_flavour) {
3257 case DEVLINK_PORT_FLAVOUR_PCI_PF:
3258 case DEVLINK_PORT_FLAVOUR_PCI_VF:
3259 pr_out_port_pfvf_num(dl, tb);
3260 break;
3261 default:
3262 break;
3263 }
852ed605 3264 }
2eb23f3e
PP
3265 if (tb[DEVLINK_ATTR_PORT_NUMBER]) {
3266 uint32_t port_number;
3267
3268 port_number = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_NUMBER]);
ff360fe9 3269 print_uint(PRINT_ANY, "port", " port %u", port_number);
2eb23f3e 3270 }
a3c4b484 3271 if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])
ff360fe9
RD
3272 print_uint(PRINT_ANY, "split_group", " split_group %u",
3273 mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]));
e3d0f0c0 3274 pr_out_port_handle_end(dl);
a3c4b484
JP
3275}
3276
3277static int cmd_port_show_cb(const struct nlmsghdr *nlh, void *data)
3278{
e3d0f0c0 3279 struct dl *dl = data;
a3c4b484
JP
3280 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3281 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3282
3283 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3284 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3285 !tb[DEVLINK_ATTR_PORT_INDEX])
3286 return MNL_CB_ERROR;
e3d0f0c0 3287 pr_out_port(dl, tb);
a3c4b484
JP
3288 return MNL_CB_OK;
3289}
3290
3291static int cmd_port_show(struct dl *dl)
3292{
3293 struct nlmsghdr *nlh;
3294 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3295 int err;
3296
3297 if (dl_argc(dl) == 0)
3298 flags |= NLM_F_DUMP;
3299
3300 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, flags);
3301
3302 if (dl_argc(dl) > 0) {
3303 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0);
3304 if (err)
3305 return err;
3306 }
3307
e3d0f0c0
JP
3308 pr_out_section_start(dl, "port");
3309 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_port_show_cb, dl);
3310 pr_out_section_end(dl);
3311 return err;
a3c4b484
JP
3312}
3313
3314static int cmd_port_set(struct dl *dl)
3315{
3316 struct nlmsghdr *nlh;
3317 int err;
3318
3319 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SET,
3320 NLM_F_REQUEST | NLM_F_ACK);
3321
3322 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_TYPE, 0);
3323 if (err)
3324 return err;
3325
3326 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3327}
3328
3329static int cmd_port_split(struct dl *dl)
3330{
3331 struct nlmsghdr *nlh;
3332 int err;
3333
3334 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SPLIT,
3335 NLM_F_REQUEST | NLM_F_ACK);
3336
3337 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_COUNT, 0);
3338 if (err)
3339 return err;
3340
3341 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3342}
3343
3344static int cmd_port_unsplit(struct dl *dl)
3345{
3346 struct nlmsghdr *nlh;
3347 int err;
3348
3349 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_UNSPLIT,
3350 NLM_F_REQUEST | NLM_F_ACK);
3351
3352 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0);
3353 if (err)
3354 return err;
3355
3356 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3357}
3358
3359static int cmd_port(struct dl *dl)
3360{
3361 if (dl_argv_match(dl, "help")) {
3362 cmd_port_help();
3363 return 0;
3364 } else if (dl_argv_match(dl, "show") ||
3365 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3366 dl_arg_inc(dl);
3367 return cmd_port_show(dl);
3368 } else if (dl_argv_match(dl, "set")) {
3369 dl_arg_inc(dl);
3370 return cmd_port_set(dl);
3371 } else if (dl_argv_match(dl, "split")) {
3372 dl_arg_inc(dl);
3373 return cmd_port_split(dl);
3374 } else if (dl_argv_match(dl, "unsplit")) {
3375 dl_arg_inc(dl);
3376 return cmd_port_unsplit(dl);
3377 }
3378 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3379 return -ENOENT;
3380}
3381
e6d7367d
JP
3382static void cmd_sb_help(void)
3383{
7a9466db
JP
3384 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3385 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3386 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3387 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3388 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3389 pr_err(" pool POOL_INDEX ]\n");
3390 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3391 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3392 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3393 pr_err(" type { ingress | egress } ]\n");
3394 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3395 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3396 pr_err(" th THRESHOLD\n");
3397 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3398 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3399 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
e6d7367d
JP
3400}
3401
e3d0f0c0 3402static void pr_out_sb(struct dl *dl, struct nlattr **tb)
e6d7367d 3403{
e3d0f0c0 3404 pr_out_handle_start_arr(dl, tb);
3666eb0e 3405 check_indent_newline(dl);
ff360fe9
RD
3406 print_uint(PRINT_ANY, "sb", "sb %u",
3407 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
3408 print_uint(PRINT_ANY, "size", " size %u",
3409 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE]));
3410 print_uint(PRINT_ANY, "ing_pools", " ing_pools %u",
3411 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT]));
3412 print_uint(PRINT_ANY, "eg_pools", " eg_pools %u",
3413 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT]));
3414 print_uint(PRINT_ANY, "ing_tcs", " ing_tcs %u",
3415 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT]));
3416 print_uint(PRINT_ANY, "eg_tcs", " eg_tcs %u",
3417 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT]));
e3d0f0c0 3418 pr_out_handle_end(dl);
e6d7367d
JP
3419}
3420
3421static int cmd_sb_show_cb(const struct nlmsghdr *nlh, void *data)
3422{
e3d0f0c0 3423 struct dl *dl = data;
e6d7367d
JP
3424 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3425 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3426
3427 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3428 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3429 !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_SIZE] ||
3430 !tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] ||
3431 !tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] ||
3432 !tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT] ||
3433 !tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])
3434 return MNL_CB_ERROR;
e3d0f0c0 3435 pr_out_sb(dl, tb);
e6d7367d
JP
3436 return MNL_CB_OK;
3437}
3438
3439static int cmd_sb_show(struct dl *dl)
3440{
3441 struct nlmsghdr *nlh;
3442 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3443 int err;
3444
3445 if (dl_argc(dl) == 0)
3446 flags |= NLM_F_DUMP;
3447
3448 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_GET, flags);
3449
3450 if (dl_argc(dl) > 0) {
3451 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
3452 if (err)
3453 return err;
3454 }
3455
e3d0f0c0
JP
3456 pr_out_section_start(dl, "sb");
3457 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_show_cb, dl);
3458 pr_out_section_end(dl);
3459 return err;
e6d7367d
JP
3460}
3461
3462static const char *pool_type_name(uint8_t type)
3463{
3464 switch (type) {
3465 case DEVLINK_SB_POOL_TYPE_INGRESS: return "ingress";
3466 case DEVLINK_SB_POOL_TYPE_EGRESS: return "egress";
3467 default: return "<unknown type>";
3468 }
3469}
3470
3471static const char *threshold_type_name(uint8_t type)
3472{
3473 switch (type) {
3474 case DEVLINK_SB_THRESHOLD_TYPE_STATIC: return "static";
3475 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC: return "dynamic";
3476 default: return "<unknown type>";
3477 }
3478}
3479
e3d0f0c0 3480static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb)
e6d7367d 3481{
e3d0f0c0 3482 pr_out_handle_start_arr(dl, tb);
ff360fe9
RD
3483 check_indent_newline(dl);
3484 print_uint(PRINT_ANY, "sb", "sb %u",
3485 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
3486 print_uint(PRINT_ANY, "pool", " pool %u",
3487 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
3666eb0e
RD
3488 print_string(PRINT_ANY, "type", " type %s",
3489 pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE])));
ff360fe9
RD
3490 print_uint(PRINT_ANY, "size", " size %u",
3491 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE]));
3666eb0e
RD
3492 print_string(PRINT_ANY, "thtype", " thtype %s",
3493 threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])));
9d886c66 3494 if (tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])
ff360fe9
RD
3495 print_uint(PRINT_ANY, "cell_size", " cell size %u",
3496 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE]));
e3d0f0c0 3497 pr_out_handle_end(dl);
e6d7367d
JP
3498}
3499
3500static int cmd_sb_pool_show_cb(const struct nlmsghdr *nlh, void *data)
3501{
e3d0f0c0 3502 struct dl *dl = data;
e6d7367d
JP
3503 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3504 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3505
3506 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3507 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3508 !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
3509 !tb[DEVLINK_ATTR_SB_POOL_TYPE] || !tb[DEVLINK_ATTR_SB_POOL_SIZE] ||
3510 !tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
3511 return MNL_CB_ERROR;
e3d0f0c0 3512 pr_out_sb_pool(dl, tb);
e6d7367d
JP
3513 return MNL_CB_OK;
3514}
3515
3516static int cmd_sb_pool_show(struct dl *dl)
3517{
3518 struct nlmsghdr *nlh;
3519 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3520 int err;
3521
3522 if (dl_argc(dl) == 0)
3523 flags |= NLM_F_DUMP;
3524
3525 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_GET, flags);
3526
3527 if (dl_argc(dl) > 0) {
3528 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL,
3529 DL_OPT_SB);
3530 if (err)
3531 return err;
3532 }
3533
e3d0f0c0
JP
3534 pr_out_section_start(dl, "pool");
3535 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_pool_show_cb, dl);
3536 pr_out_section_end(dl);
3537 return err;
e6d7367d
JP
3538}
3539
3540static int cmd_sb_pool_set(struct dl *dl)
3541{
3542 struct nlmsghdr *nlh;
3543 int err;
3544
3545 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_SET,
3546 NLM_F_REQUEST | NLM_F_ACK);
3547
3548 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL |
3549 DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, DL_OPT_SB);
3550 if (err)
3551 return err;
3552
3553 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3554}
3555
3556static int cmd_sb_pool(struct dl *dl)
3557{
3558 if (dl_argv_match(dl, "help")) {
3559 cmd_sb_help();
3560 return 0;
3561 } else if (dl_argv_match(dl, "show") ||
3562 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3563 dl_arg_inc(dl);
3564 return cmd_sb_pool_show(dl);
3565 } else if (dl_argv_match(dl, "set")) {
3566 dl_arg_inc(dl);
3567 return cmd_sb_pool_set(dl);
3568 }
3569 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3570 return -ENOENT;
3571}
3572
3573static void pr_out_sb_port_pool(struct dl *dl, struct nlattr **tb)
3574{
e3d0f0c0 3575 pr_out_port_handle_start_arr(dl, tb, true);
ff360fe9
RD
3576 check_indent_newline(dl);
3577 print_uint(PRINT_ANY, "sb", "sb %u",
3578 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
3579 print_uint(PRINT_ANY, "pool", " pool %u",
3580 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
3581 print_uint(PRINT_ANY, "threshold", " threshold %u",
3582 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD]));
e3d0f0c0 3583 pr_out_port_handle_end(dl);
e6d7367d
JP
3584}
3585
3586static int cmd_sb_port_pool_show_cb(const struct nlmsghdr *nlh, void *data)
3587{
3588 struct dl *dl = data;
3589 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3590 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3591
3592 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3593 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3594 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
3595 !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD])
3596 return MNL_CB_ERROR;
3597 pr_out_sb_port_pool(dl, tb);
3598 return MNL_CB_OK;
3599}
3600
3601static int cmd_sb_port_pool_show(struct dl *dl)
3602{
3603 struct nlmsghdr *nlh;
3604 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3605 int err;
3606
3607 if (dl_argc(dl) == 0)
3608 flags |= NLM_F_DUMP;
3609
3610 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags);
3611
3612 if (dl_argc(dl) > 0) {
3613 err = dl_argv_parse_put(nlh, dl,
3614 DL_OPT_HANDLEP | DL_OPT_SB_POOL,
3615 DL_OPT_SB);
3616 if (err)
3617 return err;
3618 }
3619
e3d0f0c0
JP
3620 pr_out_section_start(dl, "port_pool");
3621 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_port_pool_show_cb, dl);
3622 pr_out_section_end(dl);
3623 return 0;
e6d7367d
JP
3624}
3625
3626static int cmd_sb_port_pool_set(struct dl *dl)
3627{
3628 struct nlmsghdr *nlh;
3629 int err;
3630
3631 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_SET,
3632 NLM_F_REQUEST | NLM_F_ACK);
3633
3634 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL |
3635 DL_OPT_SB_TH, DL_OPT_SB);
3636 if (err)
3637 return err;
3638
3639 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3640}
3641
3642static int cmd_sb_port_pool(struct dl *dl)
3643{
3644 if (dl_argv_match(dl, "help")) {
3645 cmd_sb_help();
3646 return 0;
3647 } else if (dl_argv_match(dl, "show") ||
3648 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3649 dl_arg_inc(dl);
3650 return cmd_sb_port_pool_show(dl);
3651 } else if (dl_argv_match(dl, "set")) {
3652 dl_arg_inc(dl);
3653 return cmd_sb_port_pool_set(dl);
3654 }
3655 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3656 return -ENOENT;
3657}
3658
3659static int cmd_sb_port(struct dl *dl)
3660{
3661 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3662 cmd_sb_help();
3663 return 0;
3664 } else if (dl_argv_match(dl, "pool")) {
3665 dl_arg_inc(dl);
3666 return cmd_sb_port_pool(dl);
3667 }
3668 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3669 return -ENOENT;
3670}
3671
3672static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb)
3673{
e3d0f0c0 3674 pr_out_port_handle_start_arr(dl, tb, true);
ff360fe9
RD
3675 check_indent_newline(dl);
3676 print_uint(PRINT_ANY, "sb", "sb %u",
3677 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
3678 print_uint(PRINT_ANY, "tc", " tc %u",
3679 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]));
3666eb0e
RD
3680 print_string(PRINT_ANY, "type", " type %s",
3681 pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE])));
ff360fe9
RD
3682 print_uint(PRINT_ANY, "pool", " pool %u",
3683 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
3684 print_uint(PRINT_ANY, "threshold", " threshold %u",
3685 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD]));
e3d0f0c0 3686 pr_out_port_handle_end(dl);
e6d7367d
JP
3687}
3688
3689static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr *nlh, void *data)
3690{
3691 struct dl *dl = data;
3692 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3693 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3694
3695 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3696 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3697 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
3698 !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] ||
3699 !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD])
3700 return MNL_CB_ERROR;
3701 pr_out_sb_tc_bind(dl, tb);
3702 return MNL_CB_OK;
3703}
3704
3705static int cmd_sb_tc_bind_show(struct dl *dl)
3706{
3707 struct nlmsghdr *nlh;
3708 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3709 int err;
3710
3711 if (dl_argc(dl) == 0)
3712 flags |= NLM_F_DUMP;
3713
3714 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags);
3715
3716 if (dl_argc(dl) > 0) {
3717 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC |
3718 DL_OPT_SB_TYPE, DL_OPT_SB);
3719 if (err)
3720 return err;
3721 }
3722
e3d0f0c0
JP
3723 pr_out_section_start(dl, "tc_bind");
3724 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_tc_bind_show_cb, dl);
3725 pr_out_section_end(dl);
3726 return err;
e6d7367d
JP
3727}
3728
3729static int cmd_sb_tc_bind_set(struct dl *dl)
3730{
3731 struct nlmsghdr *nlh;
3732 int err;
3733
3734 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3735 NLM_F_REQUEST | NLM_F_ACK);
3736
3737 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC |
3738 DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH,
3739 DL_OPT_SB);
3740 if (err)
3741 return err;
3742
3743 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3744}
3745
3746static int cmd_sb_tc_bind(struct dl *dl)
3747{
3748 if (dl_argv_match(dl, "help")) {
3749 cmd_sb_help();
3750 return 0;
3751 } else if (dl_argv_match(dl, "show") ||
3752 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3753 dl_arg_inc(dl);
3754 return cmd_sb_tc_bind_show(dl);
3755 } else if (dl_argv_match(dl, "set")) {
3756 dl_arg_inc(dl);
3757 return cmd_sb_tc_bind_set(dl);
3758 }
3759 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3760 return -ENOENT;
3761}
3762
3763static int cmd_sb_tc(struct dl *dl)
3764{
3765 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3766 cmd_sb_help();
3767 return 0;
3768 } else if (dl_argv_match(dl, "bind")) {
3769 dl_arg_inc(dl);
3770 return cmd_sb_tc_bind(dl);
3771 }
3772 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3773 return -ENOENT;
3774}
3775
25ec49be
JP
3776struct occ_item {
3777 struct list_head list;
3778 uint32_t index;
3779 uint32_t cur;
3780 uint32_t max;
3781 uint32_t bound_pool_index;
3782};
3783
3784struct occ_port {
3785 struct list_head list;
3786 char *bus_name;
3787 char *dev_name;
3788 uint32_t port_index;
3789 uint32_t sb_index;
3790 struct list_head pool_list;
3791 struct list_head ing_tc_list;
3792 struct list_head eg_tc_list;
3793};
3794
3795struct occ_show {
3796 struct dl *dl;
3797 int err;
3798 struct list_head port_list;
3799};
3800
3801static struct occ_item *occ_item_alloc(void)
3802{
3803 return calloc(1, sizeof(struct occ_item));
3804}
3805
3806static void occ_item_free(struct occ_item *occ_item)
3807{
3808 free(occ_item);
3809}
3810
3811static struct occ_port *occ_port_alloc(uint32_t port_index)
3812{
3813 struct occ_port *occ_port;
3814
3815 occ_port = calloc(1, sizeof(*occ_port));
3816 if (!occ_port)
3817 return NULL;
3818 occ_port->port_index = port_index;
3819 INIT_LIST_HEAD(&occ_port->pool_list);
3820 INIT_LIST_HEAD(&occ_port->ing_tc_list);
3821 INIT_LIST_HEAD(&occ_port->eg_tc_list);
3822 return occ_port;
3823}
3824
3825static void occ_port_free(struct occ_port *occ_port)
3826{
3827 struct occ_item *occ_item, *tmp;
3828
3829 list_for_each_entry_safe(occ_item, tmp, &occ_port->pool_list, list)
3830 occ_item_free(occ_item);
3831 list_for_each_entry_safe(occ_item, tmp, &occ_port->ing_tc_list, list)
3832 occ_item_free(occ_item);
3833 list_for_each_entry_safe(occ_item, tmp, &occ_port->eg_tc_list, list)
3834 occ_item_free(occ_item);
3835}
3836
3837static struct occ_show *occ_show_alloc(struct dl *dl)
3838{
3839 struct occ_show *occ_show;
3840
3841 occ_show = calloc(1, sizeof(*occ_show));
3842 if (!occ_show)
3843 return NULL;
3844 occ_show->dl = dl;
3845 INIT_LIST_HEAD(&occ_show->port_list);
3846 return occ_show;
3847}
3848
3849static void occ_show_free(struct occ_show *occ_show)
3850{
3851 struct occ_port *occ_port, *tmp;
3852
3853 list_for_each_entry_safe(occ_port, tmp, &occ_show->port_list, list)
3854 occ_port_free(occ_port);
3855}
3856
3857static struct occ_port *occ_port_get(struct occ_show *occ_show,
3858 struct nlattr **tb)
3859{
3860 struct occ_port *occ_port;
3861 uint32_t port_index;
3862
3863 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
3864
3865 list_for_each_entry_reverse(occ_port, &occ_show->port_list, list) {
3866 if (occ_port->port_index == port_index)
3867 return occ_port;
3868 }
3869 occ_port = occ_port_alloc(port_index);
3870 if (!occ_port)
3871 return NULL;
3872 list_add_tail(&occ_port->list, &occ_show->port_list);
3873 return occ_port;
3874}
3875
3876static void pr_out_occ_show_item_list(const char *label, struct list_head *list,
3877 bool bound_pool)
3878{
3879 struct occ_item *occ_item;
3880 int i = 1;
3881
3882 pr_out_sp(7, " %s:", label);
3883 list_for_each_entry(occ_item, list, list) {
3884 if ((i - 1) % 4 == 0 && i != 1)
3885 pr_out_sp(7, " ");
3886 if (bound_pool)
3887 pr_out_sp(7, "%2u(%u):", occ_item->index,
3888 occ_item->bound_pool_index);
3889 else
3890 pr_out_sp(7, "%2u:", occ_item->index);
517ea57c 3891 pr_out_sp(21, "%10u/%u", occ_item->cur, occ_item->max);
25ec49be
JP
3892 if (i++ % 4 == 0)
3893 pr_out("\n");
3894 }
3895 if ((i - 1) % 4 != 0)
3896 pr_out("\n");
3897}
3898
e3d0f0c0
JP
3899static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label,
3900 struct list_head *list,
3901 bool bound_pool)
3902{
3903 struct occ_item *occ_item;
3904 char buf[32];
3905
b20555e4 3906 open_json_object(label);
e3d0f0c0
JP
3907 list_for_each_entry(occ_item, list, list) {
3908 sprintf(buf, "%u", occ_item->index);
b20555e4 3909 open_json_object(buf);
e3d0f0c0 3910 if (bound_pool)
b20555e4
RD
3911 print_uint(PRINT_JSON, "bound_pool", NULL,
3912 occ_item->bound_pool_index);
3913 print_uint(PRINT_JSON, "current", NULL, occ_item->cur);
3914 print_uint(PRINT_JSON, "max", NULL, occ_item->max);
3915 close_json_object();
e3d0f0c0 3916 }
b20555e4 3917 close_json_object();
e3d0f0c0
JP
3918}
3919
3920static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port)
25ec49be 3921{
e3d0f0c0
JP
3922 if (dl->json_output) {
3923 pr_out_json_occ_show_item_list(dl, "pool",
3924 &occ_port->pool_list, false);
3925 pr_out_json_occ_show_item_list(dl, "itc",
3926 &occ_port->ing_tc_list, true);
3927 pr_out_json_occ_show_item_list(dl, "etc",
3928 &occ_port->eg_tc_list, true);
3929 } else {
3930 pr_out("\n");
3931 pr_out_occ_show_item_list("pool", &occ_port->pool_list, false);
3932 pr_out_occ_show_item_list("itc", &occ_port->ing_tc_list, true);
3933 pr_out_occ_show_item_list("etc", &occ_port->eg_tc_list, true);
3934 }
25ec49be
JP
3935}
3936
3937static void pr_out_occ_show(struct occ_show *occ_show)
3938{
3939 struct dl *dl = occ_show->dl;
3940 struct dl_opts *opts = &dl->opts;
3941 struct occ_port *occ_port;
3942
3943 list_for_each_entry(occ_port, &occ_show->port_list, list) {
e3d0f0c0
JP
3944 __pr_out_port_handle_start(dl, opts->bus_name, opts->dev_name,
3945 occ_port->port_index, true, false);
3946 pr_out_occ_show_port(dl, occ_port);
3947 pr_out_port_handle_end(dl);
25ec49be
JP
3948 }
3949}
3950
3951static void cmd_sb_occ_port_pool_process(struct occ_show *occ_show,
3952 struct nlattr **tb)
3953{
3954 struct occ_port *occ_port;
3955 struct occ_item *occ_item;
3956
3957 if (occ_show->err || !dl_dump_filter(occ_show->dl, tb))
3958 return;
3959
3960 occ_port = occ_port_get(occ_show, tb);
3961 if (!occ_port) {
3962 occ_show->err = -ENOMEM;
3963 return;
3964 }
3965
3966 occ_item = occ_item_alloc();
3967 if (!occ_item) {
3968 occ_show->err = -ENOMEM;
3969 return;
3970 }
3971 occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]);
3972 occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]);
3973 occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]);
3974 list_add_tail(&occ_item->list, &occ_port->pool_list);
3975}
3976
3977static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr *nlh, void *data)
3978{
3979 struct occ_show *occ_show = data;
3980 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3981 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3982
3983 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3984 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3985 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
3986 !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
3987 !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX])
3988 return MNL_CB_ERROR;
3989 cmd_sb_occ_port_pool_process(occ_show, tb);
3990 return MNL_CB_OK;
3991}
3992
3993static void cmd_sb_occ_tc_pool_process(struct occ_show *occ_show,
3994 struct nlattr **tb)
3995{
3996 struct occ_port *occ_port;
3997 struct occ_item *occ_item;
3998 uint8_t pool_type;
3999
4000 if (occ_show->err || !dl_dump_filter(occ_show->dl, tb))
4001 return;
4002
4003 occ_port = occ_port_get(occ_show, tb);
4004 if (!occ_port) {
4005 occ_show->err = -ENOMEM;
4006 return;
4007 }
4008
4009 occ_item = occ_item_alloc();
4010 if (!occ_item) {
4011 occ_show->err = -ENOMEM;
4012 return;
4013 }
4014 occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]);
4015 occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]);
4016 occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]);
4017 occ_item->bound_pool_index =
4018 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]);
4019 pool_type = mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]);
4020 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
4021 list_add_tail(&occ_item->list, &occ_port->ing_tc_list);
4022 else if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS)
4023 list_add_tail(&occ_item->list, &occ_port->eg_tc_list);
4024 else
4025 occ_item_free(occ_item);
4026}
4027
4028static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr *nlh, void *data)
4029{
4030 struct occ_show *occ_show = data;
4031 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4032 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4033
4034 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4035 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4036 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
4037 !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] ||
4038 !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
4039 !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX])
4040 return MNL_CB_ERROR;
4041 cmd_sb_occ_tc_pool_process(occ_show, tb);
4042 return MNL_CB_OK;
4043}
4044
4045static int cmd_sb_occ_show(struct dl *dl)
4046{
4047 struct nlmsghdr *nlh;
4048 struct occ_show *occ_show;
4049 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP;
4050 int err;
4051
4052 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB);
4053 if (err)
4054 return err;
4055
4056 occ_show = occ_show_alloc(dl);
4057 if (!occ_show)
4058 return -ENOMEM;
4059
4060 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags);
4061
4062 err = _mnlg_socket_sndrcv(dl->nlg, nlh,
4063 cmd_sb_occ_port_pool_process_cb, occ_show);
4064 if (err)
4065 goto out;
4066
4067 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags);
4068
4069 err = _mnlg_socket_sndrcv(dl->nlg, nlh,
4070 cmd_sb_occ_tc_pool_process_cb, occ_show);
4071 if (err)
4072 goto out;
4073
e3d0f0c0 4074 pr_out_section_start(dl, "occupancy");
25ec49be 4075 pr_out_occ_show(occ_show);
e3d0f0c0 4076 pr_out_section_end(dl);
25ec49be
JP
4077
4078out:
4079 occ_show_free(occ_show);
4080 return err;
4081}
4082
4083static int cmd_sb_occ_snapshot(struct dl *dl)
4084{
4085 struct nlmsghdr *nlh;
4086 int err;
4087
4088 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_SNAPSHOT,
4089 NLM_F_REQUEST | NLM_F_ACK);
4090
4091 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
4092 if (err)
4093 return err;
4094
4095 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4096}
4097
4098static int cmd_sb_occ_clearmax(struct dl *dl)
4099{
4100 struct nlmsghdr *nlh;
4101 int err;
4102
4103 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_MAX_CLEAR,
4104 NLM_F_REQUEST | NLM_F_ACK);
4105
4106 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
4107 if (err)
4108 return err;
4109
4110 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4111}
4112
4113static int cmd_sb_occ(struct dl *dl)
4114{
4115 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
4116 cmd_sb_help();
4117 return 0;
4118 } else if (dl_argv_match(dl, "show") ||
4119 dl_argv_match(dl, "list")) {
4120 dl_arg_inc(dl);
4121 return cmd_sb_occ_show(dl);
4122 } else if (dl_argv_match(dl, "snapshot")) {
4123 dl_arg_inc(dl);
4124 return cmd_sb_occ_snapshot(dl);
4125 } else if (dl_argv_match(dl, "clearmax")) {
4126 dl_arg_inc(dl);
4127 return cmd_sb_occ_clearmax(dl);
4128 }
4129 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4130 return -ENOENT;
4131}
4132
e6d7367d
JP
4133static int cmd_sb(struct dl *dl)
4134{
4135 if (dl_argv_match(dl, "help")) {
4136 cmd_sb_help();
4137 return 0;
4138 } else if (dl_argv_match(dl, "show") ||
4139 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
4140 dl_arg_inc(dl);
4141 return cmd_sb_show(dl);
4142 } else if (dl_argv_match(dl, "pool")) {
4143 dl_arg_inc(dl);
4144 return cmd_sb_pool(dl);
4145 } else if (dl_argv_match(dl, "port")) {
4146 dl_arg_inc(dl);
4147 return cmd_sb_port(dl);
4148 } else if (dl_argv_match(dl, "tc")) {
4149 dl_arg_inc(dl);
4150 return cmd_sb_tc(dl);
25ec49be
JP
4151 } else if (dl_argv_match(dl, "occupancy")) {
4152 dl_arg_inc(dl);
4153 return cmd_sb_occ(dl);
e6d7367d
JP
4154 }
4155 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4156 return -ENOENT;
4157}
4158
a3c4b484
JP
4159static const char *cmd_name(uint8_t cmd)
4160{
4161 switch (cmd) {
4162 case DEVLINK_CMD_UNSPEC: return "unspec";
4163 case DEVLINK_CMD_GET: return "get";
4164 case DEVLINK_CMD_SET: return "set";
4165 case DEVLINK_CMD_NEW: return "new";
4166 case DEVLINK_CMD_DEL: return "del";
4167 case DEVLINK_CMD_PORT_GET: return "get";
4168 case DEVLINK_CMD_PORT_SET: return "set";
da7a1aa7 4169 case DEVLINK_CMD_PORT_NEW: return "new";
a3c4b484 4170 case DEVLINK_CMD_PORT_DEL: return "del";
13925ae9
MS
4171 case DEVLINK_CMD_PARAM_GET: return "get";
4172 case DEVLINK_CMD_PARAM_SET: return "set";
4173 case DEVLINK_CMD_PARAM_NEW: return "new";
4174 case DEVLINK_CMD_PARAM_DEL: return "del";
8b4fbf0b
AV
4175 case DEVLINK_CMD_REGION_GET: return "get";
4176 case DEVLINK_CMD_REGION_SET: return "set";
4177 case DEVLINK_CMD_REGION_NEW: return "new";
4178 case DEVLINK_CMD_REGION_DEL: return "del";
853be43f
JP
4179 case DEVLINK_CMD_FLASH_UPDATE: return "begin";
4180 case DEVLINK_CMD_FLASH_UPDATE_END: return "end";
4181 case DEVLINK_CMD_FLASH_UPDATE_STATUS: return "status";
5023df6a 4182 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER: return "status";
ef12d6da
IS
4183 case DEVLINK_CMD_TRAP_GET: return "get";
4184 case DEVLINK_CMD_TRAP_SET: return "set";
4185 case DEVLINK_CMD_TRAP_NEW: return "new";
4186 case DEVLINK_CMD_TRAP_DEL: return "del";
4ede9e9d
IS
4187 case DEVLINK_CMD_TRAP_GROUP_GET: return "get";
4188 case DEVLINK_CMD_TRAP_GROUP_SET: return "set";
4189 case DEVLINK_CMD_TRAP_GROUP_NEW: return "new";
4190 case DEVLINK_CMD_TRAP_GROUP_DEL: return "del";
a66af556
IS
4191 case DEVLINK_CMD_TRAP_POLICER_GET: return "get";
4192 case DEVLINK_CMD_TRAP_POLICER_SET: return "set";
4193 case DEVLINK_CMD_TRAP_POLICER_NEW: return "new";
4194 case DEVLINK_CMD_TRAP_POLICER_DEL: return "del";
a3c4b484
JP
4195 default: return "<unknown cmd>";
4196 }
4197}
4198
4199static const char *cmd_obj(uint8_t cmd)
4200{
4201 switch (cmd) {
4202 case DEVLINK_CMD_UNSPEC: return "unspec";
4203 case DEVLINK_CMD_GET:
4204 case DEVLINK_CMD_SET:
4205 case DEVLINK_CMD_NEW:
4206 case DEVLINK_CMD_DEL:
4207 return "dev";
4208 case DEVLINK_CMD_PORT_GET:
4209 case DEVLINK_CMD_PORT_SET:
4210 case DEVLINK_CMD_PORT_NEW:
4211 case DEVLINK_CMD_PORT_DEL:
4212 return "port";
13925ae9
MS
4213 case DEVLINK_CMD_PARAM_GET:
4214 case DEVLINK_CMD_PARAM_SET:
4215 case DEVLINK_CMD_PARAM_NEW:
4216 case DEVLINK_CMD_PARAM_DEL:
4217 return "param";
8b4fbf0b
AV
4218 case DEVLINK_CMD_REGION_GET:
4219 case DEVLINK_CMD_REGION_SET:
4220 case DEVLINK_CMD_REGION_NEW:
4221 case DEVLINK_CMD_REGION_DEL:
4222 return "region";
853be43f
JP
4223 case DEVLINK_CMD_FLASH_UPDATE:
4224 case DEVLINK_CMD_FLASH_UPDATE_END:
4225 case DEVLINK_CMD_FLASH_UPDATE_STATUS:
4226 return "flash";
5023df6a
MS
4227 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER:
4228 return "health";
ef12d6da
IS
4229 case DEVLINK_CMD_TRAP_GET:
4230 case DEVLINK_CMD_TRAP_SET:
4231 case DEVLINK_CMD_TRAP_NEW:
4232 case DEVLINK_CMD_TRAP_DEL:
4233 return "trap";
4ede9e9d
IS
4234 case DEVLINK_CMD_TRAP_GROUP_GET:
4235 case DEVLINK_CMD_TRAP_GROUP_SET:
4236 case DEVLINK_CMD_TRAP_GROUP_NEW:
4237 case DEVLINK_CMD_TRAP_GROUP_DEL:
4238 return "trap-group";
a66af556
IS
4239 case DEVLINK_CMD_TRAP_POLICER_GET:
4240 case DEVLINK_CMD_TRAP_POLICER_SET:
4241 case DEVLINK_CMD_TRAP_POLICER_NEW:
4242 case DEVLINK_CMD_TRAP_POLICER_DEL:
4243 return "trap-policer";
a3c4b484
JP
4244 default: return "<unknown obj>";
4245 }
4246}
4247
4248static void pr_out_mon_header(uint8_t cmd)
4249{
4250 pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd));
4251}
4252
4253static bool cmd_filter_check(struct dl *dl, uint8_t cmd)
4254{
4255 const char *obj = cmd_obj(cmd);
4256 unsigned int index = 0;
4257 const char *cur_obj;
4258
4259 if (dl_no_arg(dl))
4260 return true;
4261 while ((cur_obj = dl_argv_index(dl, index++))) {
4262 if (strcmp(cur_obj, obj) == 0 || strcmp(cur_obj, "all") == 0)
4263 return true;
4264 }
4265 return false;
4266}
4267
853be43f
JP
4268static void pr_out_flash_update(struct dl *dl, struct nlattr **tb)
4269{
4270 __pr_out_handle_start(dl, tb, true, false);
4271
3666eb0e
RD
4272 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]) {
4273 check_indent_newline(dl);
4274 print_string(PRINT_ANY, "msg", "msg %s",
4275 mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]));
4276 }
4277 if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]) {
4278 check_indent_newline(dl);
4279 print_string(PRINT_ANY, "component", "component %s",
4280 mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]));
4281 }
853be43f
JP
4282
4283 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE])
4284 pr_out_u64(dl, "done",
4285 mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]));
4286
4287 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL])
4288 pr_out_u64(dl, "total",
4289 mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL]));
4290
4291 pr_out_handle_end(dl);
4292}
4293
8b4fbf0b 4294static void pr_out_region(struct dl *dl, struct nlattr **tb);
5023df6a 4295static void pr_out_health(struct dl *dl, struct nlattr **tb_health);
ef12d6da 4296static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array);
4ede9e9d 4297static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array);
a66af556 4298static void pr_out_trap_policer(struct dl *dl, struct nlattr **tb, bool array);
8b4fbf0b 4299
a3c4b484
JP
4300static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
4301{
4302 struct dl *dl = data;
4303 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4304 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4305 uint8_t cmd = genl->cmd;
4306
4307 if (!cmd_filter_check(dl, cmd))
4308 return MNL_CB_OK;
4309
4310 switch (cmd) {
4311 case DEVLINK_CMD_GET: /* fall through */
4312 case DEVLINK_CMD_SET: /* fall through */
4313 case DEVLINK_CMD_NEW: /* fall through */
4314 case DEVLINK_CMD_DEL:
4315 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4316 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
4317 return MNL_CB_ERROR;
4318 pr_out_mon_header(genl->cmd);
88fdbf80 4319 pr_out_handle(dl, tb);
a3c4b484
JP
4320 break;
4321 case DEVLINK_CMD_PORT_GET: /* fall through */
4322 case DEVLINK_CMD_PORT_SET: /* fall through */
4323 case DEVLINK_CMD_PORT_NEW: /* fall through */
4324 case DEVLINK_CMD_PORT_DEL:
4325 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4326 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4327 !tb[DEVLINK_ATTR_PORT_INDEX])
4328 return MNL_CB_ERROR;
4329 pr_out_mon_header(genl->cmd);
e3d0f0c0 4330 pr_out_port(dl, tb);
a3c4b484 4331 break;
13925ae9
MS
4332 case DEVLINK_CMD_PARAM_GET: /* fall through */
4333 case DEVLINK_CMD_PARAM_SET: /* fall through */
4334 case DEVLINK_CMD_PARAM_NEW: /* fall through */
4335 case DEVLINK_CMD_PARAM_DEL:
4336 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4337 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4338 !tb[DEVLINK_ATTR_PARAM])
4339 return MNL_CB_ERROR;
4340 pr_out_mon_header(genl->cmd);
4341 pr_out_param(dl, tb, false);
4342 break;
8b4fbf0b
AV
4343 case DEVLINK_CMD_REGION_GET: /* fall through */
4344 case DEVLINK_CMD_REGION_SET: /* fall through */
4345 case DEVLINK_CMD_REGION_NEW: /* fall through */
4346 case DEVLINK_CMD_REGION_DEL:
4347 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4348 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4349 !tb[DEVLINK_ATTR_REGION_NAME])
4350 return MNL_CB_ERROR;
4351 pr_out_mon_header(genl->cmd);
4352 pr_out_region(dl, tb);
4353 break;
853be43f
JP
4354 case DEVLINK_CMD_FLASH_UPDATE: /* fall through */
4355 case DEVLINK_CMD_FLASH_UPDATE_END: /* fall through */
4356 case DEVLINK_CMD_FLASH_UPDATE_STATUS:
4357 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4358 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
4359 return MNL_CB_ERROR;
4360 pr_out_mon_header(genl->cmd);
4361 pr_out_flash_update(dl, tb);
4362 break;
5023df6a
MS
4363 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER:
4364 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4365 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4366 !tb[DEVLINK_ATTR_HEALTH_REPORTER])
4367 return MNL_CB_ERROR;
4368 pr_out_mon_header(genl->cmd);
4369 pr_out_health(dl, tb);
4370 break;
ef12d6da
IS
4371 case DEVLINK_CMD_TRAP_GET: /* fall through */
4372 case DEVLINK_CMD_TRAP_SET: /* fall through */
4373 case DEVLINK_CMD_TRAP_NEW: /* fall through */
4374 case DEVLINK_CMD_TRAP_DEL:
4375 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4376 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4377 !tb[DEVLINK_ATTR_TRAP_NAME] ||
4378 !tb[DEVLINK_ATTR_TRAP_TYPE] ||
4379 !tb[DEVLINK_ATTR_TRAP_ACTION] ||
4380 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
4381 !tb[DEVLINK_ATTR_TRAP_METADATA] ||
4382 !tb[DEVLINK_ATTR_STATS])
4383 return MNL_CB_ERROR;
4384 pr_out_mon_header(genl->cmd);
4385 pr_out_trap(dl, tb, false);
4386 break;
4ede9e9d
IS
4387 case DEVLINK_CMD_TRAP_GROUP_GET: /* fall through */
4388 case DEVLINK_CMD_TRAP_GROUP_SET: /* fall through */
4389 case DEVLINK_CMD_TRAP_GROUP_NEW: /* fall through */
4390 case DEVLINK_CMD_TRAP_GROUP_DEL:
4391 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4392 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4393 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
4394 !tb[DEVLINK_ATTR_STATS])
4395 return MNL_CB_ERROR;
4396 pr_out_mon_header(genl->cmd);
4397 pr_out_trap_group(dl, tb, false);
4398 break;
a66af556
IS
4399 case DEVLINK_CMD_TRAP_POLICER_GET: /* fall through */
4400 case DEVLINK_CMD_TRAP_POLICER_SET: /* fall through */
4401 case DEVLINK_CMD_TRAP_POLICER_NEW: /* fall through */
4402 case DEVLINK_CMD_TRAP_POLICER_DEL: /* fall through */
4403 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4404 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4405 !tb[DEVLINK_ATTR_TRAP_POLICER_ID] ||
4406 !tb[DEVLINK_ATTR_TRAP_POLICER_RATE] ||
4407 !tb[DEVLINK_ATTR_TRAP_POLICER_BURST])
4408 return MNL_CB_ERROR;
4409 pr_out_mon_header(genl->cmd);
4410 pr_out_trap_policer(dl, tb, false);
4411 break;
a3c4b484
JP
4412 }
4413 return MNL_CB_OK;
4414}
4415
4416static int cmd_mon_show(struct dl *dl)
4417{
4418 int err;
4419 unsigned int index = 0;
4420 const char *cur_obj;
4421
4422 while ((cur_obj = dl_argv_index(dl, index++))) {
4423 if (strcmp(cur_obj, "all") != 0 &&
4424 strcmp(cur_obj, "dev") != 0 &&
ef12d6da 4425 strcmp(cur_obj, "port") != 0 &&
5023df6a 4426 strcmp(cur_obj, "health") != 0 &&
4ede9e9d 4427 strcmp(cur_obj, "trap") != 0 &&
a66af556
IS
4428 strcmp(cur_obj, "trap-group") != 0 &&
4429 strcmp(cur_obj, "trap-policer") != 0) {
a3c4b484
JP
4430 pr_err("Unknown object \"%s\"\n", cur_obj);
4431 return -EINVAL;
4432 }
4433 }
4434 err = _mnlg_socket_group_add(dl->nlg, DEVLINK_GENL_MCGRP_CONFIG_NAME);
4435 if (err)
4436 return err;
4437 err = _mnlg_socket_recv_run(dl->nlg, cmd_mon_show_cb, dl);
4438 if (err)
4439 return err;
4440 return 0;
4441}
4442
4443static void cmd_mon_help(void)
4444{
7a9466db 4445 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
a66af556 4446 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
a3c4b484
JP
4447}
4448
4449static int cmd_mon(struct dl *dl)
4450{
4451 if (dl_argv_match(dl, "help")) {
4452 cmd_mon_help();
4453 return 0;
a3c4b484 4454 }
e7330604 4455 return cmd_mon_show(dl);
a3c4b484
JP
4456}
4457
153c1a9b
AS
4458struct dpipe_field {
4459 char *name;
4460 unsigned int id;
4461 unsigned int bitwidth;
4462 enum devlink_dpipe_field_mapping_type mapping_type;
4463};
4464
4465struct dpipe_header {
4466 struct list_head list;
4467 char *name;
4468 unsigned int id;
4469 struct dpipe_field *fields;
4470 unsigned int fields_count;
4471};
4472
ead18027
AS
4473struct dpipe_table {
4474 struct list_head list;
4475 char *name;
4476 unsigned int resource_id;
4477 bool resource_valid;
4478};
4479
4480struct dpipe_tables {
4481 struct list_head table_list;
4482};
4483
8cd64409
AS
4484struct resource {
4485 char *name;
4486 uint64_t size;
4487 uint64_t size_new;
4488 uint64_t size_min;
4489 uint64_t size_max;
4490 uint64_t size_gran;
4491 enum devlink_resource_unit unit;
4492 bool size_valid;
4493 uint64_t size_occ;
4494 bool occ_valid;
4495 uint64_t id;
4496 struct list_head list;
4497 struct list_head resource_list;
4498 struct resource *parent;
4499};
4500
4501struct resources {
4502 struct list_head resource_list;
4503};
4504
4505struct resource_ctx {
4506 struct dl *dl;
4507 int err;
4508 struct resources *resources;
ead18027 4509 struct dpipe_tables *tables;
8cd64409
AS
4510 bool print_resources;
4511 bool pending_change;
4512};
4513
4514static struct resource *resource_alloc(void)
4515{
4516 struct resource *resource;
4517
4518 resource = calloc(1, sizeof(struct resource));
4519 if (!resource)
4520 return NULL;
4521 INIT_LIST_HEAD(&resource->resource_list);
4522 return resource;
4523}
4524
4525static void resource_free(struct resource *resource)
4526{
4527 struct resource *child_resource, *tmp;
4528
4529 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
4530 list) {
4531 free(child_resource->name);
4532 resource_free(child_resource);
4533 }
4534 free(resource);
4535}
4536
4537static struct resources *resources_alloc(void)
4538{
4539 struct resources *resources;
4540
4541 resources = calloc(1, sizeof(struct resources));
4542 if (!resources)
4543 return NULL;
4544 INIT_LIST_HEAD(&resources->resource_list);
4545 return resources;
4546}
4547
4548static void resources_free(struct resources *resources)
4549{
4550 struct resource *resource, *tmp;
4551
4552 list_for_each_entry_safe(resource, tmp, &resources->resource_list, list)
4553 resource_free(resource);
4554}
4555
4556static int resource_ctx_init(struct resource_ctx *ctx, struct dl *dl)
4557{
4558 ctx->resources = resources_alloc();
4559 if (!ctx->resources)
4560 return -ENOMEM;
4561 ctx->dl = dl;
4562 return 0;
4563}
4564
4565static void resource_ctx_fini(struct resource_ctx *ctx)
4566{
4567 resources_free(ctx->resources);
4568}
4569
153c1a9b
AS
4570struct dpipe_ctx {
4571 struct dl *dl;
4572 int err;
4573 struct list_head global_headers;
4574 struct list_head local_headers;
ead18027
AS
4575 struct dpipe_tables *tables;
4576 struct resources *resources;
153c1a9b 4577 bool print_headers;
ead18027 4578 bool print_tables;
153c1a9b
AS
4579};
4580
4581static struct dpipe_header *dpipe_header_alloc(unsigned int fields_count)
a3c4b484 4582{
153c1a9b
AS
4583 struct dpipe_header *header;
4584
4585 header = calloc(1, sizeof(struct dpipe_header));
4586 if (!header)
4587 return NULL;
4588 header->fields = calloc(fields_count, sizeof(struct dpipe_field));
4589 if (!header->fields)
4590 goto err_fields_alloc;
4591 header->fields_count = fields_count;
4592 return header;
4593
4594err_fields_alloc:
4595 free(header);
4596 return NULL;
a3c4b484
JP
4597}
4598
153c1a9b 4599static void dpipe_header_free(struct dpipe_header *header)
a3c4b484 4600{
153c1a9b
AS
4601 free(header->fields);
4602 free(header);
4603}
4604
4605static void dpipe_header_clear(struct dpipe_header *header)
4606{
4607 struct dpipe_field *field;
4608 int i;
4609
4610 for (i = 0; i < header->fields_count; i++) {
4611 field = &header->fields[i];
4612 free(field->name);
a3c4b484 4613 }
153c1a9b 4614 free(header->name);
a3c4b484
JP
4615}
4616
153c1a9b
AS
4617static void dpipe_header_add(struct dpipe_ctx *ctx,
4618 struct dpipe_header *header, bool global)
a3c4b484 4619{
153c1a9b
AS
4620 if (global)
4621 list_add(&header->list, &ctx->global_headers);
4622 else
4623 list_add(&header->list, &ctx->local_headers);
4624}
a3c4b484 4625
153c1a9b
AS
4626static void dpipe_header_del(struct dpipe_header *header)
4627{
4628 list_del(&header->list);
4629}
a3c4b484 4630
ead18027
AS
4631static struct dpipe_table *dpipe_table_alloc(void)
4632{
4633 return calloc(1, sizeof(struct dpipe_table));
4634}
4635
4636static void dpipe_table_free(struct dpipe_table *table)
4637{
4638 free(table);
4639}
4640
4641static struct dpipe_tables *dpipe_tables_alloc(void)
4642{
4643 struct dpipe_tables *tables;
4644
4645 tables = calloc(1, sizeof(struct dpipe_tables));
4646 if (!tables)
4647 return NULL;
4648 INIT_LIST_HEAD(&tables->table_list);
4649 return tables;
4650}
4651
4652static void dpipe_tables_free(struct dpipe_tables *tables)
4653{
4654 struct dpipe_table *table, *tmp;
4655
4656 list_for_each_entry_safe(table, tmp, &tables->table_list, list)
4657 dpipe_table_free(table);
4658 free(tables);
4659}
4660
06a2cda9 4661static int dpipe_ctx_init(struct dpipe_ctx *ctx, struct dl *dl)
153c1a9b 4662{
ead18027
AS
4663 ctx->tables = dpipe_tables_alloc();
4664 if (!ctx->tables)
4665 return -ENOMEM;
4666
153c1a9b
AS
4667 ctx->dl = dl;
4668 INIT_LIST_HEAD(&ctx->global_headers);
4669 INIT_LIST_HEAD(&ctx->local_headers);
06a2cda9 4670 return 0;
153c1a9b
AS
4671}
4672
06a2cda9 4673static void dpipe_ctx_fini(struct dpipe_ctx *ctx)
153c1a9b
AS
4674{
4675 struct dpipe_header *header, *tmp;
4676
4677 list_for_each_entry_safe(header, tmp, &ctx->global_headers,
4678 list) {
4679 dpipe_header_del(header);
4680 dpipe_header_clear(header);
4681 dpipe_header_free(header);
4682 }
4683 list_for_each_entry_safe(header, tmp, &ctx->local_headers,
4684 list) {
4685 dpipe_header_del(header);
4686 dpipe_header_clear(header);
4687 dpipe_header_free(header);
a3c4b484 4688 }
ead18027 4689 dpipe_tables_free(ctx->tables);
153c1a9b 4690}
a3c4b484 4691
153c1a9b
AS
4692static const char *dpipe_header_id2s(struct dpipe_ctx *ctx,
4693 uint32_t header_id, bool global)
4694{
4695 struct list_head *header_list;
4696 struct dpipe_header *header;
4697
4698 if (global)
4699 header_list = &ctx->global_headers;
4700 else
4701 header_list = &ctx->local_headers;
4702 list_for_each_entry(header, header_list, list) {
4703 if (header->id != header_id)
4704 continue;
4705 return header->name;
a3c4b484 4706 }
153c1a9b
AS
4707 return NULL;
4708}
4709
4710static const char *dpipe_field_id2s(struct dpipe_ctx *ctx,
4711 uint32_t header_id,
4712 uint32_t field_id, bool global)
4713{
4714 struct list_head *header_list;
4715 struct dpipe_header *header;
4716
4717 if (global)
4718 header_list = &ctx->global_headers;
4719 else
4720 header_list = &ctx->local_headers;
4721 list_for_each_entry(header, header_list, list) {
4722 if (header->id != header_id)
4723 continue;
4724 return header->fields[field_id].name;
e3d0f0c0 4725 }
153c1a9b
AS
4726 return NULL;
4727}
a3c4b484 4728
153c1a9b
AS
4729static const char *
4730dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type)
4731{
4732 switch (mapping_type) {
4733 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE:
4734 return NULL;
4735 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX:
4736 return "ifindex";
4737 default:
4738 return "<unknown>";
4739 }
a3c4b484
JP
4740}
4741
153c1a9b
AS
4742static const char *
4743dpipe_mapping_get(struct dpipe_ctx *ctx, uint32_t header_id,
4744 uint32_t field_id, bool global)
a3c4b484 4745{
153c1a9b
AS
4746 enum devlink_dpipe_field_mapping_type mapping_type;
4747 struct list_head *header_list;
4748 struct dpipe_header *header;
4749
4750 if (global)
4751 header_list = &ctx->global_headers;
4752 else
4753 header_list = &ctx->local_headers;
4754 list_for_each_entry(header, header_list, list) {
4755 if (header->id != header_id)
4756 continue;
4757 mapping_type = header->fields[field_id].mapping_type;
4758 return dpipe_field_mapping_e2s(mapping_type);
4759 }
4760 return NULL;
a3c4b484
JP
4761}
4762
153c1a9b
AS
4763static void pr_out_dpipe_fields(struct dpipe_ctx *ctx,
4764 struct dpipe_field *fields,
4765 unsigned int field_count)
a3c4b484 4766{
153c1a9b
AS
4767 struct dpipe_field *field;
4768 int i;
a3c4b484 4769
153c1a9b
AS
4770 for (i = 0; i < field_count; i++) {
4771 field = &fields[i];
4772 pr_out_entry_start(ctx->dl);
3666eb0e
RD
4773 check_indent_newline(ctx->dl);
4774 print_string(PRINT_ANY, "name", "name %s", field->name);
153c1a9b 4775 if (ctx->dl->verbose)
ff360fe9
RD
4776 print_uint(PRINT_ANY, "id", " id %u", field->id);
4777 print_uint(PRINT_ANY, "bitwidth", " bitwidth %u", field->bitwidth);
3666eb0e
RD
4778 if (field->mapping_type) {
4779 print_string(PRINT_ANY, "mapping_type", " mapping_type %s",
4780 dpipe_field_mapping_e2s(field->mapping_type));
4781 }
153c1a9b
AS
4782 pr_out_entry_end(ctx->dl);
4783 }
a3c4b484
JP
4784}
4785
153c1a9b
AS
4786static void
4787pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb,
4788 struct dpipe_header *header, bool global)
a3c4b484 4789{
153c1a9b 4790 pr_out_handle_start_arr(ctx->dl, tb);
3666eb0e
RD
4791 check_indent_newline(ctx->dl);
4792 print_string(PRINT_ANY, "name", "name %s", header->name);
153c1a9b 4793 if (ctx->dl->verbose) {
ff360fe9 4794 print_uint(PRINT_ANY, "id", " id %u", header->id);
3666eb0e 4795 print_bool(PRINT_ANY, "global", " global %s", global);
153c1a9b
AS
4796 }
4797 pr_out_array_start(ctx->dl, "field");
4798 pr_out_dpipe_fields(ctx, header->fields,
4799 header->fields_count);
4800 pr_out_array_end(ctx->dl);
4801 pr_out_handle_end(ctx->dl);
a3c4b484
JP
4802}
4803
153c1a9b
AS
4804static void pr_out_dpipe_headers(struct dpipe_ctx *ctx,
4805 struct nlattr **tb)
a3c4b484 4806{
153c1a9b
AS
4807 struct dpipe_header *header;
4808
4809 list_for_each_entry(header, &ctx->local_headers, list)
4810 pr_out_dpipe_header(ctx, tb, header, false);
4811
4812 list_for_each_entry(header, &ctx->global_headers, list)
4813 pr_out_dpipe_header(ctx, tb, header, true);
4814}
4815
4816static int dpipe_header_field_get(struct nlattr *nl, struct dpipe_field *field)
4817{
4818 struct nlattr *nla_field[DEVLINK_ATTR_MAX + 1] = {};
4819 const char *name;
a3c4b484 4820 int err;
a3c4b484 4821
153c1a9b
AS
4822 err = mnl_attr_parse_nested(nl, attr_cb, nla_field);
4823 if (err != MNL_CB_OK)
4824 return -EINVAL;
4825 if (!nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID] ||
4826 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME] ||
4827 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] ||
4828 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE])
4829 return -EINVAL;
4830
4831 name = mnl_attr_get_str(nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME]);
4832 field->id = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID]);
4833 field->bitwidth = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH]);
4834 field->name = strdup(name);
4835 if (!field->name)
4836 return -ENOMEM;
4837 field->mapping_type = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE]);
4838 return 0;
4839}
4840
4841static int dpipe_header_fields_get(struct nlattr *nla_fields,
4842 struct dpipe_field *fields)
4843{
4844 struct nlattr *nla_field;
4845 int count = 0;
4846 int err;
4847
4848 mnl_attr_for_each_nested(nla_field, nla_fields) {
4849 err = dpipe_header_field_get(nla_field, &fields[count]);
4850 if (err)
4851 return err;
4852 count++;
43f35be4 4853 }
153c1a9b
AS
4854 return 0;
4855}
43f35be4 4856
153c1a9b
AS
4857static unsigned int dpipe_header_field_count_get(struct nlattr *nla_fields)
4858{
4859 struct nlattr *nla_field;
4860 unsigned int count = 0;
a3c4b484 4861
153c1a9b
AS
4862 mnl_attr_for_each_nested(nla_field, nla_fields)
4863 count++;
4864 return count;
4865}
4866
4867static int dpipe_header_get(struct dpipe_ctx *ctx, struct nlattr *nl)
4868{
4869 struct nlattr *nla_header[DEVLINK_ATTR_MAX + 1] = {};
4870 struct dpipe_header *header;
4871 unsigned int fields_count;
4872 const char *header_name;
4873 bool global;
4874 int err;
4875
4876 err = mnl_attr_parse_nested(nl, attr_cb, nla_header);
4877 if (err != MNL_CB_OK)
4878 return -EINVAL;
4879
4880 if (!nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME] ||
4881 !nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
4882 !nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS])
4883 return -EINVAL;
4884
4885 fields_count = dpipe_header_field_count_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS]);
4886 header = dpipe_header_alloc(fields_count);
4887 if (!header)
4888 return -ENOMEM;
4889
4890 header_name = mnl_attr_get_str(nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME]);
4891 header->name = strdup(header_name);
4892 header->id = mnl_attr_get_u32(nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID]);
4893 header->fields_count = fields_count;
4894 global = !!mnl_attr_get_u8(nla_header[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
4895
4896 err = dpipe_header_fields_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS],
4897 header->fields);
4898 if (err)
4899 goto err_field_get;
4900 dpipe_header_add(ctx, header, global);
4901 return 0;
4902
4903err_field_get:
4904 dpipe_header_free(header);
4905 return err;
4906}
4907
4908static int dpipe_headers_get(struct dpipe_ctx *ctx, struct nlattr **tb)
4909{
4910 struct nlattr *nla_headers = tb[DEVLINK_ATTR_DPIPE_HEADERS];
4911 struct nlattr *nla_header;
4912 int err;
4913
4914 mnl_attr_for_each_nested(nla_header, nla_headers) {
4915 err = dpipe_header_get(ctx, nla_header);
4916 if (err)
4917 return err;
4918 }
4919 return 0;
4920}
4921
4922static int cmd_dpipe_header_cb(const struct nlmsghdr *nlh, void *data)
4923{
4924 struct dpipe_ctx *ctx = data;
4925 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4926 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4927 int err;
4928
4929 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4930 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4931 !tb[DEVLINK_ATTR_DPIPE_HEADERS])
4932 return MNL_CB_ERROR;
4933 err = dpipe_headers_get(ctx, tb);
4934 if (err) {
4935 ctx->err = err;
4936 return MNL_CB_ERROR;
4937 }
4938
4939 if (ctx->print_headers)
4940 pr_out_dpipe_headers(ctx, tb);
4941 return MNL_CB_OK;
4942}
4943
4944static int cmd_dpipe_headers_show(struct dl *dl)
4945{
4946 struct nlmsghdr *nlh;
06a2cda9 4947 struct dpipe_ctx ctx = {};
153c1a9b
AS
4948 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
4949 int err;
4950
4951 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
4952
4953 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
4954 if (err)
4955 return err;
4956
06a2cda9
AS
4957 err = dpipe_ctx_init(&ctx, dl);
4958 if (err)
4959 return err;
153c1a9b 4960
06a2cda9 4961 ctx.print_headers = true;
153c1a9b
AS
4962
4963 pr_out_section_start(dl, "header");
06a2cda9 4964 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx);
153c1a9b 4965 if (err)
06a2cda9 4966 pr_err("error get headers %s\n", strerror(ctx.err));
153c1a9b
AS
4967 pr_out_section_end(dl);
4968
06a2cda9 4969 dpipe_ctx_fini(&ctx);
153c1a9b
AS
4970 return err;
4971}
4972
4973static void cmd_dpipe_header_help(void)
4974{
4975 pr_err("Usage: devlink dpipe headers show DEV\n");
4976}
4977
4978static int cmd_dpipe_header(struct dl *dl)
4979{
4980 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
4981 cmd_dpipe_header_help();
4982 return 0;
4983 } else if (dl_argv_match(dl, "show")) {
4984 dl_arg_inc(dl);
4985 return cmd_dpipe_headers_show(dl);
4986 }
4987 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4988 return -ENOENT;
4989}
4990
4991static const char
4992*dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type)
4993{
4994 switch (action_type) {
4995 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY:
4996 return "field_modify";
4997 default:
4998 return "<unknown>";
4999 }
5000}
5001
92b2a5bb
AS
5002struct dpipe_op_info {
5003 uint32_t header_id;
5004 uint32_t field_id;
5005 bool header_global;
5006};
5007
5008struct dpipe_action {
5009 struct dpipe_op_info info;
5010 uint32_t type;
5011};
5012
5013static void pr_out_dpipe_action(struct dpipe_action *action,
5014 struct dpipe_ctx *ctx)
153c1a9b 5015{
92b2a5bb 5016 struct dpipe_op_info *op_info = &action->info;
153c1a9b
AS
5017 const char *mapping;
5018
3666eb0e
RD
5019 check_indent_newline(ctx->dl);
5020 print_string(PRINT_ANY, "type", "type %s",
5021 dpipe_action_type_e2s(action->type));
5022 print_string(PRINT_ANY, "header", " header %s",
5023 dpipe_header_id2s(ctx, op_info->header_id,
5024 op_info->header_global));
5025 print_string(PRINT_ANY, "field", " field %s",
5026 dpipe_field_id2s(ctx, op_info->header_id,
5027 op_info->field_id,
5028 op_info->header_global));
92b2a5bb
AS
5029 mapping = dpipe_mapping_get(ctx, op_info->header_id,
5030 op_info->field_id,
5031 op_info->header_global);
153c1a9b 5032 if (mapping)
3666eb0e 5033 print_string(PRINT_ANY, "mapping", " mapping %s", mapping);
153c1a9b
AS
5034}
5035
92b2a5bb 5036static int dpipe_action_parse(struct dpipe_action *action, struct nlattr *nl)
153c1a9b
AS
5037{
5038 struct nlattr *nla_action[DEVLINK_ATTR_MAX + 1] = {};
153c1a9b
AS
5039 int err;
5040
5041 err = mnl_attr_parse_nested(nl, attr_cb, nla_action);
5042 if (err != MNL_CB_OK)
5043 return -EINVAL;
5044
5045 if (!nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE] ||
5046 !nla_action[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
5047 !nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
5048 !nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
5049 return -EINVAL;
5050 }
5051
92b2a5bb
AS
5052 action->type = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE]);
5053 action->info.header_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID]);
5054 action->info.field_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]);
5055 action->info.header_global = !!mnl_attr_get_u8(nla_action[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
153c1a9b 5056
153c1a9b
AS
5057 return 0;
5058}
5059
5060static int dpipe_table_actions_show(struct dpipe_ctx *ctx,
5061 struct nlattr *nla_actions)
5062{
5063 struct nlattr *nla_action;
92b2a5bb 5064 struct dpipe_action action;
153c1a9b
AS
5065
5066 mnl_attr_for_each_nested(nla_action, nla_actions) {
5067 pr_out_entry_start(ctx->dl);
92b2a5bb
AS
5068 if (dpipe_action_parse(&action, nla_action))
5069 goto err_action_parse;
5070 pr_out_dpipe_action(&action, ctx);
153c1a9b
AS
5071 pr_out_entry_end(ctx->dl);
5072 }
5073 return 0;
5074
92b2a5bb 5075err_action_parse:
153c1a9b
AS
5076 pr_out_entry_end(ctx->dl);
5077 return -EINVAL;
5078}
5079
5080static const char *
5081dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type)
5082{
5083 switch (match_type) {
5084 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT:
5085 return "field_exact";
5086 default:
5087 return "<unknown>";
5088 }
5089}
5090
92b2a5bb
AS
5091struct dpipe_match {
5092 struct dpipe_op_info info;
5093 uint32_t type;
5094};
5095
5096static void pr_out_dpipe_match(struct dpipe_match *match,
5097 struct dpipe_ctx *ctx)
153c1a9b 5098{
92b2a5bb 5099 struct dpipe_op_info *op_info = &match->info;
153c1a9b
AS
5100 const char *mapping;
5101
3666eb0e
RD
5102 check_indent_newline(ctx->dl);
5103 print_string(PRINT_ANY, "type", "type %s",
5104 dpipe_match_type_e2s(match->type));
5105 print_string(PRINT_ANY, "header", " header %s",
5106 dpipe_header_id2s(ctx, op_info->header_id,
5107 op_info->header_global));
5108 print_string(PRINT_ANY, "field", " field %s",
5109 dpipe_field_id2s(ctx, op_info->header_id,
5110 op_info->field_id,
5111 op_info->header_global));
92b2a5bb
AS
5112 mapping = dpipe_mapping_get(ctx, op_info->header_id,
5113 op_info->field_id,
5114 op_info->header_global);
153c1a9b 5115 if (mapping)
3666eb0e 5116 print_string(PRINT_ANY, "mapping", " mapping %s", mapping);
153c1a9b
AS
5117}
5118
92b2a5bb
AS
5119static int dpipe_match_parse(struct dpipe_match *match,
5120 struct nlattr *nl)
5121
153c1a9b
AS
5122{
5123 struct nlattr *nla_match[DEVLINK_ATTR_MAX + 1] = {};
153c1a9b
AS
5124 int err;
5125
5126 err = mnl_attr_parse_nested(nl, attr_cb, nla_match);
5127 if (err != MNL_CB_OK)
5128 return -EINVAL;
5129
5130 if (!nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE] ||
5131 !nla_match[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
5132 !nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
5133 !nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
5134 return -EINVAL;
5135 }
5136
92b2a5bb
AS
5137 match->type = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE]);
5138 match->info.header_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID]);
5139 match->info.field_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]);
5140 match->info.header_global = !!mnl_attr_get_u8(nla_match[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
153c1a9b 5141
153c1a9b
AS
5142 return 0;
5143}
5144
5145static int dpipe_table_matches_show(struct dpipe_ctx *ctx,
5146 struct nlattr *nla_matches)
5147{
5148 struct nlattr *nla_match;
92b2a5bb 5149 struct dpipe_match match;
153c1a9b
AS
5150
5151 mnl_attr_for_each_nested(nla_match, nla_matches) {
5152 pr_out_entry_start(ctx->dl);
92b2a5bb
AS
5153 if (dpipe_match_parse(&match, nla_match))
5154 goto err_match_parse;
5155 pr_out_dpipe_match(&match, ctx);
153c1a9b
AS
5156 pr_out_entry_end(ctx->dl);
5157 }
5158 return 0;
5159
92b2a5bb 5160err_match_parse:
153c1a9b
AS
5161 pr_out_entry_end(ctx->dl);
5162 return -EINVAL;
5163}
5164
8cd64409
AS
5165static struct resource *
5166resource_find(struct resources *resources, struct resource *resource,
5167 uint64_t resource_id)
5168{
5169 struct list_head *list_head;
5170
5171 if (!resource)
5172 list_head = &resources->resource_list;
5173 else
5174 list_head = &resource->resource_list;
5175
5176 list_for_each_entry(resource, list_head, list) {
5177 struct resource *child_resource;
5178
5179 if (resource->id == resource_id)
5180 return resource;
5181
5182 child_resource = resource_find(resources, resource,
5183 resource_id);
5184 if (child_resource)
5185 return child_resource;
5186 }
5187 return NULL;
5188}
5189
5190static void
5191resource_path_print(struct dl *dl, struct resources *resources,
5192 uint64_t resource_id)
5193{
5194 struct resource *resource, *parent_resource;
5195 const char del[] = "/";
5196 int path_len = 0;
5197 char *path;
5198
5199 resource = resource_find(resources, NULL, resource_id);
5200 if (!resource)
5201 return;
5202
5203 for (parent_resource = resource; parent_resource;
5204 parent_resource = parent_resource->parent)
5205 path_len += strlen(parent_resource->name) + 1;
5206
5207 path_len++;
5208 path = calloc(1, path_len);
5209 if (!path)
5210 return;
5211
5212 path += path_len - 1;
5213 for (parent_resource = resource; parent_resource;
5214 parent_resource = parent_resource->parent) {
5215 path -= strlen(parent_resource->name);
5216 memcpy(path, parent_resource->name,
5217 strlen(parent_resource->name));
5218 path -= strlen(del);
5219 memcpy(path, del, strlen(del));
5220 }
3666eb0e
RD
5221 check_indent_newline(dl);
5222 print_string(PRINT_ANY, "resource_path", "resource_path %s", path);
8cd64409
AS
5223 free(path);
5224}
5225
153c1a9b
AS
5226static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl)
5227{
5228 struct nlattr *nla_table[DEVLINK_ATTR_MAX + 1] = {};
ead18027
AS
5229 struct dpipe_table *table;
5230 uint32_t resource_units;
153c1a9b 5231 bool counters_enabled;
ead18027 5232 bool resource_valid;
153c1a9b
AS
5233 uint32_t size;
5234 int err;
5235
5236 err = mnl_attr_parse_nested(nl, attr_cb, nla_table);
5237 if (err != MNL_CB_OK)
5238 return -EINVAL;
5239
5240 if (!nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
5241 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE] ||
5242 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] ||
5243 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] ||
5244 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) {
5245 return -EINVAL;
5246 }
5247
ead18027
AS
5248 table = dpipe_table_alloc();
5249 if (!table)
5250 return -ENOMEM;
5251
5252 table->name = strdup(mnl_attr_get_str(nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME]));
153c1a9b
AS
5253 size = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE]);
5254 counters_enabled = !!mnl_attr_get_u8(nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
5255
0e7e1819
JP
5256 resource_valid = nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID] &&
5257 ctx->resources;
ead18027
AS
5258 if (resource_valid) {
5259 table->resource_id = mnl_attr_get_u64(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID]);
5260 table->resource_valid = true;
5261 }
5262
5263 list_add_tail(&table->list, &ctx->tables->table_list);
5264 if (!ctx->print_tables)
5265 return 0;
5266
3666eb0e
RD
5267 check_indent_newline(ctx->dl);
5268 print_string(PRINT_ANY, "name", "name %s", table->name);
ff360fe9 5269 print_uint(PRINT_ANY, "size", " size %u", size);
3666eb0e 5270 print_bool(PRINT_ANY, "counters_enabled", " counters_enabled %s", counters_enabled);
153c1a9b 5271
ead18027
AS
5272 if (resource_valid) {
5273 resource_units = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS]);
5274 resource_path_print(ctx->dl, ctx->resources,
5275 table->resource_id);
ff360fe9
RD
5276 print_uint(PRINT_ANY, "resource_units", " resource_units %u",
5277 resource_units);
ead18027
AS
5278 }
5279
153c1a9b
AS
5280 pr_out_array_start(ctx->dl, "match");
5281 if (dpipe_table_matches_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES]))
5282 goto err_matches_show;
5283 pr_out_array_end(ctx->dl);
5284
5285 pr_out_array_start(ctx->dl, "action");
5286 if (dpipe_table_actions_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS]))
5287 goto err_actions_show;
5288 pr_out_array_end(ctx->dl);
5289
5290 return 0;
5291
5292err_actions_show:
5293err_matches_show:
5294 pr_out_array_end(ctx->dl);
5295 return -EINVAL;
5296}
5297
5298static int dpipe_tables_show(struct dpipe_ctx *ctx, struct nlattr **tb)
5299{
5300 struct nlattr *nla_tables = tb[DEVLINK_ATTR_DPIPE_TABLES];
5301 struct nlattr *nla_table;
5302
5303 mnl_attr_for_each_nested(nla_table, nla_tables) {
ead18027
AS
5304 if (ctx->print_tables)
5305 pr_out_handle_start_arr(ctx->dl, tb);
153c1a9b
AS
5306 if (dpipe_table_show(ctx, nla_table))
5307 goto err_table_show;
ead18027
AS
5308 if (ctx->print_tables)
5309 pr_out_handle_end(ctx->dl);
153c1a9b
AS
5310 }
5311 return 0;
5312
5313err_table_show:
ead18027
AS
5314 if (ctx->print_tables)
5315 pr_out_handle_end(ctx->dl);
153c1a9b
AS
5316 return -EINVAL;
5317}
5318
5319static int cmd_dpipe_table_show_cb(const struct nlmsghdr *nlh, void *data)
5320{
5321 struct dpipe_ctx *ctx = data;
5322 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
5323 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
5324
5325 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5326 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5327 !tb[DEVLINK_ATTR_DPIPE_TABLES])
5328 return MNL_CB_ERROR;
5329
5330 if (dpipe_tables_show(ctx, tb))
5331 return MNL_CB_ERROR;
5332 return MNL_CB_OK;
5333}
5334
ead18027
AS
5335static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data);
5336
153c1a9b
AS
5337static int cmd_dpipe_table_show(struct dl *dl)
5338{
5339 struct nlmsghdr *nlh;
ead18027
AS
5340 struct dpipe_ctx dpipe_ctx = {};
5341 struct resource_ctx resource_ctx = {};
153c1a9b
AS
5342 uint16_t flags = NLM_F_REQUEST;
5343 int err;
5344
ead18027 5345 err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_DPIPE_TABLE_NAME);
06a2cda9
AS
5346 if (err)
5347 return err;
153c1a9b 5348
ead18027
AS
5349 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
5350
5351 err = dpipe_ctx_init(&dpipe_ctx, dl);
153c1a9b 5352 if (err)
ead18027
AS
5353 return err;
5354
5355 dpipe_ctx.print_tables = true;
153c1a9b 5356
153c1a9b 5357 dl_opts_put(nlh, dl);
ead18027
AS
5358 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb,
5359 &dpipe_ctx);
153c1a9b 5360 if (err) {
ead18027
AS
5361 pr_err("error get headers %s\n", strerror(dpipe_ctx.err));
5362 goto err_headers_get;
5363 }
5364
5365 err = resource_ctx_init(&resource_ctx, dl);
5366 if (err)
5367 goto err_resource_ctx_init;
5368
5369 resource_ctx.print_resources = false;
5370 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP, flags);
5371 dl_opts_put(nlh, dl);
5372 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb,
5373 &resource_ctx);
0e7e1819
JP
5374 if (!err)
5375 dpipe_ctx.resources = resource_ctx.resources;
153c1a9b
AS
5376
5377 flags = NLM_F_REQUEST | NLM_F_ACK;
5378 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, flags);
5379 dl_opts_put(nlh, dl);
5380
5381 pr_out_section_start(dl, "table");
ead18027 5382 _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb, &dpipe_ctx);
153c1a9b 5383 pr_out_section_end(dl);
ead18027
AS
5384
5385 resource_ctx_fini(&resource_ctx);
5386 dpipe_ctx_fini(&dpipe_ctx);
5387 return 0;
5388
ead18027
AS
5389err_resource_ctx_init:
5390err_headers_get:
5391 dpipe_ctx_fini(&dpipe_ctx);
153c1a9b
AS
5392 return err;
5393}
5394
5395static int cmd_dpipe_table_set(struct dl *dl)
5396{
5397 struct nlmsghdr *nlh;
5398 int err;
5399
5400 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
5401 NLM_F_REQUEST | NLM_F_ACK);
5402
5403 err = dl_argv_parse_put(nlh, dl,
5404 DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME |
5405 DL_OPT_DPIPE_TABLE_COUNTERS, 0);
5406 if (err)
5407 return err;
5408
5409 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
5410}
5411
31639589
AS
5412enum dpipe_value_type {
5413 DPIPE_VALUE_TYPE_VALUE,
5414 DPIPE_VALUE_TYPE_MASK,
5415};
5416
5417static const char *
5418dpipe_value_type_e2s(enum dpipe_value_type type)
5419{
5420 switch (type) {
5421 case DPIPE_VALUE_TYPE_VALUE:
5422 return "value";
5423 case DPIPE_VALUE_TYPE_MASK:
5424 return "value_mask";
5425 default:
5426 return "<unknown>";
5427 }
5428}
5429
5430struct dpipe_field_printer {
5431 unsigned int field_id;
5432 void (*printer)(struct dpipe_ctx *, enum dpipe_value_type, void *);
5433};
5434
5435struct dpipe_header_printer {
5436 struct dpipe_field_printer *printers;
5437 unsigned int printers_count;
5438 unsigned int header_id;
5439};
5440
b2947f8b
AS
5441static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx *ctx,
5442 enum dpipe_value_type type,
5443 void *value)
5444{
5445 struct in_addr ip_addr;
5446
5447 ip_addr.s_addr = htonl(*(uint32_t *)value);
3666eb0e
RD
5448 check_indent_newline(ctx->dl);
5449 print_string_name_value(dpipe_value_type_e2s(type), inet_ntoa(ip_addr));
b2947f8b
AS
5450}
5451
5452static void
5453dpipe_field_printer_ethernet_addr(struct dpipe_ctx *ctx,
5454 enum dpipe_value_type type,
5455 void *value)
5456{
3666eb0e
RD
5457 check_indent_newline(ctx->dl);
5458 print_string_name_value(dpipe_value_type_e2s(type),
5459 ether_ntoa((struct ether_addr *)value));
b2947f8b
AS
5460}
5461
5462static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx *ctx,
5463 enum dpipe_value_type type,
5464 void *value)
5465{
5466 char str[INET6_ADDRSTRLEN];
5467
5468 inet_ntop(AF_INET6, value, str, INET6_ADDRSTRLEN);
3666eb0e
RD
5469 check_indent_newline(ctx->dl);
5470 print_string_name_value(dpipe_value_type_e2s(type), str);
b2947f8b
AS
5471}
5472
5473static struct dpipe_field_printer dpipe_field_printers_ipv4[] = {
5474 {
5475 .printer = dpipe_field_printer_ipv4_addr,
5476 .field_id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
5477 }
5478};
5479
5480static struct dpipe_header_printer dpipe_header_printer_ipv4 = {
5481 .printers = dpipe_field_printers_ipv4,
5482 .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv4),
5483 .header_id = DEVLINK_DPIPE_HEADER_IPV4,
5484};
5485
5486static struct dpipe_field_printer dpipe_field_printers_ethernet[] = {
5487 {
5488 .printer = dpipe_field_printer_ethernet_addr,
5489 .field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
5490 },
5491};
5492
5493static struct dpipe_header_printer dpipe_header_printer_ethernet = {
5494 .printers = dpipe_field_printers_ethernet,
5495 .printers_count = ARRAY_SIZE(dpipe_field_printers_ethernet),
5496 .header_id = DEVLINK_DPIPE_HEADER_ETHERNET,
5497};
5498
5499static struct dpipe_field_printer dpipe_field_printers_ipv6[] = {
5500 {
5501 .printer = dpipe_field_printer_ipv6_addr,
5502 .field_id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
5503 }
5504};
5505
5506static struct dpipe_header_printer dpipe_header_printer_ipv6 = {
5507 .printers = dpipe_field_printers_ipv6,
5508 .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv6),
5509 .header_id = DEVLINK_DPIPE_HEADER_IPV6,
5510};
5511
5512static struct dpipe_header_printer *dpipe_header_printers[] = {
5513 &dpipe_header_printer_ipv4,
5514 &dpipe_header_printer_ethernet,
5515 &dpipe_header_printer_ipv6,
5516};
31639589
AS
5517
5518static int dpipe_print_prot_header(struct dpipe_ctx *ctx,
5519 struct dpipe_op_info *info,
5520 enum dpipe_value_type type,
5521 void *value)
153c1a9b 5522{
31639589
AS
5523 unsigned int header_printers_count = ARRAY_SIZE(dpipe_header_printers);
5524 struct dpipe_header_printer *header_printer;
5525 struct dpipe_field_printer *field_printer;
5526 unsigned int field_printers_count;
5527 int j;
5528 int i;
5529
5530 for (i = 0; i < header_printers_count; i++) {
5531 header_printer = dpipe_header_printers[i];
5532 if (header_printer->header_id != info->header_id)
5533 continue;
5534 field_printers_count = header_printer->printers_count;
5535 for (j = 0; j < field_printers_count; j++) {
5536 field_printer = &header_printer->printers[j];
5537 if (field_printer->field_id != info->field_id)
5538 continue;
5539 field_printer->printer(ctx, type, value);
5540 return 0;
5541 }
5542 }
5543
5544 return -EINVAL;
5545}
5546
5547static void __pr_out_entry_value(struct dpipe_ctx *ctx,
5548 void *value,
5549 unsigned int value_len,
5550 struct dpipe_op_info *info,
5551 enum dpipe_value_type type)
5552{
5553 if (info->header_global &&
5554 !dpipe_print_prot_header(ctx, info, type, value))
5555 return;
5556
5557 if (value_len == sizeof(uint32_t)) {
5558 uint32_t *value_32 = value;
5559
ff360fe9
RD
5560 check_indent_newline(ctx->dl);
5561 print_uint_name_value(dpipe_value_type_e2s(type), *value_32);
31639589
AS
5562 }
5563}
5564
5565static void pr_out_dpipe_entry_value(struct dpipe_ctx *ctx,
5566 struct nlattr **nla_match_value,
5567 struct dpipe_op_info *info)
5568{
5569 void *value, *value_mask;
5570 uint32_t value_mapping;
153c1a9b
AS
5571 uint16_t value_len;
5572 bool mask, mapping;
5573
5574 mask = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MASK];
5575 mapping = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING];
5576
5577 value_len = mnl_attr_get_payload_len(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
31639589 5578 value = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
153c1a9b 5579
31639589
AS
5580 if (mapping) {
5581 value_mapping = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]);
ff360fe9
RD
5582 check_indent_newline(ctx->dl);
5583 print_uint(PRINT_ANY, "mapping_value", "mapping_value %u", value_mapping);
31639589 5584 }
153c1a9b 5585
31639589
AS
5586 if (mask) {
5587 value_mask = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
5588 __pr_out_entry_value(ctx, value_mask, value_len, info,
5589 DPIPE_VALUE_TYPE_MASK);
153c1a9b
AS
5590 }
5591
31639589 5592 __pr_out_entry_value(ctx, value, value_len, info, DPIPE_VALUE_TYPE_VALUE);
153c1a9b
AS
5593}
5594
5595static int dpipe_entry_match_value_show(struct dpipe_ctx *ctx,
5596 struct nlattr *nl)
5597{
5598 struct nlattr *nla_match_value[DEVLINK_ATTR_MAX + 1] = {};
92b2a5bb 5599 struct dpipe_match match;
153c1a9b
AS
5600 int err;
5601
5602 err = mnl_attr_parse_nested(nl, attr_cb, nla_match_value);
5603 if (err != MNL_CB_OK)
5604 return -EINVAL;
5605
5606 if (!nla_match_value[DEVLINK_ATTR_DPIPE_MATCH] ||
5607 !nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]) {
5608 return -EINVAL;
5609 }
5610
5611 pr_out_entry_start(ctx->dl);
92b2a5bb
AS
5612 if (dpipe_match_parse(&match,
5613 nla_match_value[DEVLINK_ATTR_DPIPE_MATCH]))
5614 goto err_match_parse;
5615 pr_out_dpipe_match(&match, ctx);
31639589 5616 pr_out_dpipe_entry_value(ctx, nla_match_value, &match.info);
153c1a9b
AS
5617 pr_out_entry_end(ctx->dl);
5618
5619 return 0;
5620
92b2a5bb 5621err_match_parse:
153c1a9b
AS
5622 pr_out_entry_end(ctx->dl);
5623 return -EINVAL;
5624}
5625
5626static int dpipe_entry_action_value_show(struct dpipe_ctx *ctx,
5627 struct nlattr *nl)
5628{
5629 struct nlattr *nla_action_value[DEVLINK_ATTR_MAX + 1] = {};
92b2a5bb 5630 struct dpipe_action action;
153c1a9b
AS
5631 int err;
5632
5633 err = mnl_attr_parse_nested(nl, attr_cb, nla_action_value);
5634 if (err != MNL_CB_OK)
5635 return -EINVAL;
5636
5637 if (!nla_action_value[DEVLINK_ATTR_DPIPE_ACTION] ||
5638 !nla_action_value[DEVLINK_ATTR_DPIPE_VALUE]) {
5639 return -EINVAL;
5640 }
5641
5642 pr_out_entry_start(ctx->dl);
92b2a5bb
AS
5643 if (dpipe_action_parse(&action,
5644 nla_action_value[DEVLINK_ATTR_DPIPE_ACTION]))
5645 goto err_action_parse;
5646 pr_out_dpipe_action(&action, ctx);
31639589 5647 pr_out_dpipe_entry_value(ctx, nla_action_value, &action.info);
153c1a9b
AS
5648 pr_out_entry_end(ctx->dl);
5649
5650 return 0;
5651
92b2a5bb 5652err_action_parse:
153c1a9b
AS
5653 pr_out_entry_end(ctx->dl);
5654 return -EINVAL;
5655}
5656
5657static int
5658dpipe_tables_action_values_show(struct dpipe_ctx *ctx,
5659 struct nlattr *nla_action_values)
5660{
5661 struct nlattr *nla_action_value;
5662
5663 mnl_attr_for_each_nested(nla_action_value, nla_action_values) {
5664 if (dpipe_entry_action_value_show(ctx, nla_action_value))
5665 return -EINVAL;
5666 }
5667 return 0;
5668}
5669
5670static int
5671dpipe_tables_match_values_show(struct dpipe_ctx *ctx,
5672 struct nlattr *nla_match_values)
5673{
5674 struct nlattr *nla_match_value;
5675
5676 mnl_attr_for_each_nested(nla_match_value, nla_match_values) {
5677 if (dpipe_entry_match_value_show(ctx, nla_match_value))
5678 return -EINVAL;
5679 }
5680 return 0;
5681}
5682
5683static int dpipe_entry_show(struct dpipe_ctx *ctx, struct nlattr *nl)
5684{
5685 struct nlattr *nla_entry[DEVLINK_ATTR_MAX + 1] = {};
5686 uint32_t entry_index;
5687 uint64_t counter;
5688 int err;
5689
5690 err = mnl_attr_parse_nested(nl, attr_cb, nla_entry);
5691 if (err != MNL_CB_OK)
5692 return -EINVAL;
5693
5694 if (!nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] ||
5695 !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] ||
5696 !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]) {
5697 return -EINVAL;
5698 }
5699
ff360fe9 5700 check_indent_newline(ctx->dl);
153c1a9b 5701 entry_index = mnl_attr_get_u32(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX]);
ff360fe9 5702 print_uint(PRINT_ANY, "index", "index %u", entry_index);
153c1a9b
AS
5703
5704 if (nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]) {
5705 counter = mnl_attr_get_u64(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]);
ff360fe9 5706 print_uint(PRINT_ANY, "counter", " counter %u", counter);
153c1a9b
AS
5707 }
5708
5709 pr_out_array_start(ctx->dl, "match_value");
5710 if (dpipe_tables_match_values_show(ctx,
5711 nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES]))
5712 goto err_match_values_show;
5713 pr_out_array_end(ctx->dl);
5714
5715 pr_out_array_start(ctx->dl, "action_value");
5716 if (dpipe_tables_action_values_show(ctx,
5717 nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]))
5718 goto err_action_values_show;
5719 pr_out_array_end(ctx->dl);
5720 return 0;
5721
5722err_action_values_show:
5723err_match_values_show:
5724 pr_out_array_end(ctx->dl);
5725 return -EINVAL;
5726}
5727
5728static int dpipe_table_entries_show(struct dpipe_ctx *ctx, struct nlattr **tb)
5729{
5730 struct nlattr *nla_entries = tb[DEVLINK_ATTR_DPIPE_ENTRIES];
5731 struct nlattr *nla_entry;
5732
5733 mnl_attr_for_each_nested(nla_entry, nla_entries) {
5734 pr_out_handle_start_arr(ctx->dl, tb);
5735 if (dpipe_entry_show(ctx, nla_entry))
5736 goto err_entry_show;
5737 pr_out_handle_end(ctx->dl);
5738 }
5739 return 0;
5740
5741err_entry_show:
5742 pr_out_handle_end(ctx->dl);
5743 return -EINVAL;
5744}
5745
5746static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr *nlh, void *data)
5747{
5748 struct dpipe_ctx *ctx = data;
5749 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
5750 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
5751
5752 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5753 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5754 !tb[DEVLINK_ATTR_DPIPE_ENTRIES])
5755 return MNL_CB_ERROR;
5756
5757 if (dpipe_table_entries_show(ctx, tb))
5758 return MNL_CB_ERROR;
5759 return MNL_CB_OK;
5760}
5761
5762static int cmd_dpipe_table_dump(struct dl *dl)
5763{
5764 struct nlmsghdr *nlh;
06a2cda9 5765 struct dpipe_ctx ctx = {};
153c1a9b
AS
5766 uint16_t flags = NLM_F_REQUEST;
5767 int err;
5768
06a2cda9
AS
5769 err = dpipe_ctx_init(&ctx, dl);
5770 if (err)
5771 return err;
153c1a9b
AS
5772
5773 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME, 0);
5774 if (err)
5775 goto out;
5776
5777 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
5778 dl_opts_put(nlh, dl);
06a2cda9 5779 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx);
153c1a9b 5780 if (err) {
06a2cda9 5781 pr_err("error get headers %s\n", strerror(ctx.err));
153c1a9b
AS
5782 goto out;
5783 }
5784
5785 flags = NLM_F_REQUEST | NLM_F_ACK;
5786 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_ENTRIES_GET, flags);
5787 dl_opts_put(nlh, dl);
5788
5789 pr_out_section_start(dl, "table_entry");
06a2cda9 5790 _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_entry_dump_cb, &ctx);
153c1a9b
AS
5791 pr_out_section_end(dl);
5792out:
06a2cda9 5793 dpipe_ctx_fini(&ctx);
153c1a9b
AS
5794 return err;
5795}
5796
5797static void cmd_dpipe_table_help(void)
5798{
5799 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5800 "where OBJECT-LIST := { show | set | dump }\n");
5801}
5802
5803static int cmd_dpipe_table(struct dl *dl)
5804{
5805 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
5806 cmd_dpipe_table_help();
5807 return 0;
5808 } else if (dl_argv_match(dl, "show")) {
5809 dl_arg_inc(dl);
5810 return cmd_dpipe_table_show(dl);
5811 } else if (dl_argv_match(dl, "set")) {
5812 dl_arg_inc(dl);
5813 return cmd_dpipe_table_set(dl);
5814 } else if (dl_argv_match(dl, "dump")) {
5815 dl_arg_inc(dl);
5816 return cmd_dpipe_table_dump(dl);
5817 }
5818 pr_err("Command \"%s\" not found\n", dl_argv(dl));
5819 return -ENOENT;
5820}
5821
5822static void cmd_dpipe_help(void)
5823{
5824 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5825 "where OBJECT-LIST := { header | table }\n");
5826}
5827
5828static int cmd_dpipe(struct dl *dl)
5829{
5830 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
5831 cmd_dpipe_help();
5832 return 0;
5833 } else if (dl_argv_match(dl, "header")) {
5834 dl_arg_inc(dl);
5835 return cmd_dpipe_header(dl);
5836 } else if (dl_argv_match(dl, "table")) {
5837 dl_arg_inc(dl);
5838 return cmd_dpipe_table(dl);
5839 }
5840 pr_err("Command \"%s\" not found\n", dl_argv(dl));
5841 return -ENOENT;
5842}
5843
8cd64409
AS
5844static int
5845resource_parse(struct resource_ctx *ctx, struct resource *resource,
5846 struct nlattr **nla_resource)
5847{
5848 if (!nla_resource[DEVLINK_ATTR_RESOURCE_NAME] ||
5849 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE] ||
5850 !nla_resource[DEVLINK_ATTR_RESOURCE_ID] ||
5851 !nla_resource[DEVLINK_ATTR_RESOURCE_UNIT] ||
5852 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN] ||
5853 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX] ||
5854 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]) {
5855 return -EINVAL;
5856 }
5857
5858 resource->name = strdup(mnl_attr_get_str(nla_resource[DEVLINK_ATTR_RESOURCE_NAME]));
5859 resource->size = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE]);
5860 resource->id = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_ID]);
5861 resource->unit = mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_UNIT]);
5862 resource->size_min = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN]);
5863 resource->size_max = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX]);
5864 resource->size_gran = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]);
5865
5866 if (nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW])
5867 resource->size_new = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW]);
5868 else
5869 resource->size_new = resource->size;
5870
5871 if (nla_resource[DEVLINK_ATTR_RESOURCE_OCC]) {
5872 resource->size_occ = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_OCC]);
5873 resource->occ_valid = true;
5874 }
5875
5876 if (resource->size_new != resource->size)
5877 ctx->pending_change = true;
5878
5879 return 0;
5880}
5881
5882static int
5883resource_get(struct resource_ctx *ctx, struct resource *resource,
5884 struct resource *parent_resource, struct nlattr *nl)
5885{
5886 struct nlattr *nla_resource[DEVLINK_ATTR_MAX + 1] = {};
5887 struct nlattr *nla_child_resource;
5888 struct nlattr *nla_resources;
5889 bool top = false;
5890 int err;
5891
5892 if (!resource) {
5893 nla_resources = nl;
5894 top = true;
5895 goto out;
5896 }
5897
5898 err = mnl_attr_parse_nested(nl, attr_cb, nla_resource);
5899 if (err != MNL_CB_OK)
5900 return -EINVAL;
5901
5902 err = resource_parse(ctx, resource, nla_resource);
5903 if (err)
5904 return err;
5905
5906 resource->parent = parent_resource;
5907 if (!nla_resource[DEVLINK_ATTR_RESOURCE_LIST])
5908 return 0;
5909
5910 resource->size_valid = !!mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_VALID]);
5911 nla_resources = nla_resource[DEVLINK_ATTR_RESOURCE_LIST];
5912out:
5913 mnl_attr_for_each_nested(nla_child_resource, nla_resources) {
5914 struct resource *child_resource;
5915 struct list_head *list;
5916
5917 child_resource = resource_alloc();
5918 if (!child_resource)
5919 return -ENOMEM;
5920
5921 if (top)
5922 list = &ctx->resources->resource_list;
5923 else
5924 list = &resource->resource_list;
5925
5926 list_add_tail(&child_resource->list, list);
5927 err = resource_get(ctx, child_resource, resource,
5928 nla_child_resource);
5929 if (err)
5930 return err;
5931 }
5932
5933 return 0;
5934}
5935
5936static const char *resource_unit_str_get(enum devlink_resource_unit unit)
5937{
5938 switch (unit) {
5939 case DEVLINK_RESOURCE_UNIT_ENTRY: return "entry";
5940 default: return "<unknown unit>";
5941 }
5942}
5943
5944static void resource_show(struct resource *resource,
5945 struct resource_ctx *ctx)
5946{
5947 struct resource *child_resource;
ead18027 5948 struct dpipe_table *table;
8cd64409 5949 struct dl *dl = ctx->dl;
ead18027 5950 bool array = false;
8cd64409 5951
3666eb0e
RD
5952 check_indent_newline(dl);
5953 print_string(PRINT_ANY, "name", "name %s", resource->name);
8cd64409
AS
5954 if (dl->verbose)
5955 resource_path_print(dl, ctx->resources, resource->id);
43eb8728 5956 pr_out_u64(dl, "size", resource->size);
8cd64409 5957 if (resource->size != resource->size_new)
43eb8728 5958 pr_out_u64(dl, "size_new", resource->size_new);
8cd64409 5959 if (resource->occ_valid)
ff360fe9 5960 print_uint(PRINT_ANY, "occ", " occ %u", resource->size_occ);
3666eb0e
RD
5961 print_string(PRINT_ANY, "unit", " unit %s",
5962 resource_unit_str_get(resource->unit));
8cd64409
AS
5963
5964 if (resource->size_min != resource->size_max) {
ff360fe9
RD
5965 print_uint(PRINT_ANY, "size_min", " size_min %u",
5966 resource->size_min);
43eb8728 5967 pr_out_u64(dl, "size_max", resource->size_max);
ff360fe9
RD
5968 print_uint(PRINT_ANY, "size_gran", " size_gran %u",
5969 resource->size_gran);
8cd64409
AS
5970 }
5971
ead18027
AS
5972 list_for_each_entry(table, &ctx->tables->table_list, list)
5973 if (table->resource_id == resource->id &&
5974 table->resource_valid)
5975 array = true;
5976
5977 if (array)
5978 pr_out_array_start(dl, "dpipe_tables");
5979 else
3666eb0e
RD
5980 print_string(PRINT_ANY, "dpipe_tables", " dpipe_tables none",
5981 "none");
ead18027
AS
5982
5983 list_for_each_entry(table, &ctx->tables->table_list, list) {
5984 if (table->resource_id != resource->id ||
5985 !table->resource_valid)
5986 continue;
5987 pr_out_entry_start(dl);
3666eb0e
RD
5988 check_indent_newline(dl);
5989 print_string(PRINT_ANY, "table_name", "table_name %s",
5990 table->name);
ead18027
AS
5991 pr_out_entry_end(dl);
5992 }
5993 if (array)
5994 pr_out_array_end(dl);
5995
8cd64409
AS
5996 if (list_empty(&resource->resource_list))
5997 return;
5998
3666eb0e
RD
5999 if (ctx->pending_change) {
6000 check_indent_newline(dl);
6001 print_string(PRINT_ANY, "size_valid", "size_valid %s",
6002 resource->size_valid ? "true" : "false");
6003 }
8cd64409
AS
6004 pr_out_array_start(dl, "resources");
6005 list_for_each_entry(child_resource, &resource->resource_list, list) {
6006 pr_out_entry_start(dl);
6007 resource_show(child_resource, ctx);
6008 pr_out_entry_end(dl);
6009 }
6010 pr_out_array_end(dl);
6011}
6012
6013static void
6014resources_show(struct resource_ctx *ctx, struct nlattr **tb)
6015{
6016 struct resources *resources = ctx->resources;
6017 struct resource *resource;
6018
6019 list_for_each_entry(resource, &resources->resource_list, list) {
6020 pr_out_handle_start_arr(ctx->dl, tb);
6021 resource_show(resource, ctx);
6022 pr_out_handle_end(ctx->dl);
6023 }
6024}
6025
6026static int resources_get(struct resource_ctx *ctx, struct nlattr **tb)
6027{
6028 return resource_get(ctx, NULL, NULL, tb[DEVLINK_ATTR_RESOURCE_LIST]);
6029}
6030
6031static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data)
6032{
6033 struct resource_ctx *ctx = data;
6034 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6035 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6036 int err;
6037
6038 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6039 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6040 !tb[DEVLINK_ATTR_RESOURCE_LIST])
6041 return MNL_CB_ERROR;
6042
6043 err = resources_get(ctx, tb);
6044 if (err) {
6045 ctx->err = err;
6046 return MNL_CB_ERROR;
6047 }
6048
6049 if (ctx->print_resources)
6050 resources_show(ctx, tb);
6051
6052 return MNL_CB_OK;
6053}
6054
6055static int cmd_resource_show(struct dl *dl)
6056{
6057 struct nlmsghdr *nlh;
ead18027
AS
6058 struct dpipe_ctx dpipe_ctx = {};
6059 struct resource_ctx resource_ctx = {};
8cd64409
AS
6060 int err;
6061
ead18027 6062 err = dl_argv_parse(dl, DL_OPT_HANDLE, 0);
8cd64409
AS
6063 if (err)
6064 return err;
6065
ead18027
AS
6066 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET,
6067 NLM_F_REQUEST);
6068 dl_opts_put(nlh, dl);
6069
6070 err = dpipe_ctx_init(&dpipe_ctx, dl);
8cd64409
AS
6071 if (err)
6072 return err;
6073
ead18027
AS
6074 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb,
6075 &dpipe_ctx);
6076 if (err) {
6077 pr_err("error get tables %s\n", strerror(dpipe_ctx.err));
6078 goto out;
6079 }
6080
6081 err = resource_ctx_init(&resource_ctx, dl);
6082 if (err)
6083 goto out;
6084
6085 resource_ctx.print_resources = true;
6086 resource_ctx.tables = dpipe_ctx.tables;
6087 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
6088 NLM_F_REQUEST | NLM_F_ACK);
6089 dl_opts_put(nlh, dl);
8cd64409 6090 pr_out_section_start(dl, "resources");
ead18027
AS
6091 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb,
6092 &resource_ctx);
8cd64409 6093 pr_out_section_end(dl);
ead18027
AS
6094 resource_ctx_fini(&resource_ctx);
6095out:
6096 dpipe_ctx_fini(&dpipe_ctx);
8cd64409
AS
6097 return err;
6098}
6099
6100static void cmd_resource_help(void)
6101{
6102 pr_err("Usage: devlink resource show DEV\n"
6103 " devlink resource set DEV path PATH size SIZE\n");
6104}
6105
6106static struct resource *
6107resource_find_by_name(struct list_head *list, char *name)
6108{
6109 struct resource *resource;
6110
6111 list_for_each_entry(resource, list, list) {
6112 if (!strcmp(resource->name, name))
6113 return resource;
6114 }
6115 return NULL;
6116}
6117
6118static int
6119resource_path_parse(struct resource_ctx *ctx, const char *resource_path,
6120 uint32_t *p_resource_id, bool *p_resource_valid)
6121{
6122 struct resource *resource;
6123 uint32_t resource_id = 0;
6124 char *resource_path_dup;
6125 struct list_head *list;
6126 const char del[] = "/";
6127 char *resource_name;
6128
6129 resource_path_dup = strdup(resource_path);
6130 list = &ctx->resources->resource_list;
6131 resource_name = strtok(resource_path_dup, del);
6132 while (resource_name != NULL) {
6133 resource = resource_find_by_name(list, resource_name);
6134 if (!resource)
6135 goto err_resource_lookup;
6136
6137 list = &resource->resource_list;
6138 resource_name = strtok(NULL, del);
6139 resource_id = resource->id;
6140 }
6141 free(resource_path_dup);
6142 *p_resource_valid = true;
6143 *p_resource_id = resource_id;
6144 return 0;
6145
6146err_resource_lookup:
6147 free(resource_path_dup);
6148 return -EINVAL;
6149}
6150
6151static int cmd_resource_set(struct dl *dl)
6152{
6153 struct nlmsghdr *nlh;
6154 struct resource_ctx ctx = {};
6155 int err;
6156
6157 err = resource_ctx_init(&ctx, dl);
6158 if (err)
6159 return err;
6160
6161 ctx.print_resources = false;
6162 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_RESOURCE_PATH |
6163 DL_OPT_RESOURCE_SIZE, 0);
6164 if (err)
6165 goto out;
6166
6167 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
6168 NLM_F_REQUEST);
6169 dl_opts_put(nlh, dl);
6170 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, &ctx);
6171 if (err) {
6172 pr_err("error getting resources %s\n", strerror(ctx.err));
6173 goto out;
6174 }
6175
6176 err = resource_path_parse(&ctx, dl->opts.resource_path,
6177 &dl->opts.resource_id,
6178 &dl->opts.resource_id_valid);
6179 if (err) {
b1ffc1f4 6180 pr_err("error parsing resource path %s\n", strerror(-err));
8cd64409
AS
6181 goto out;
6182 }
6183
6184 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_SET,
6185 NLM_F_REQUEST | NLM_F_ACK);
6186
6187 dl_opts_put(nlh, dl);
6188 err = _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6189out:
6190 resource_ctx_fini(&ctx);
6191 return err;
6192}
6193
6194static int cmd_resource(struct dl *dl)
6195{
6196 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
6197 cmd_resource_help();
6198 return 0;
6199 } else if (dl_argv_match(dl, "show")) {
6200 dl_arg_inc(dl);
6201 return cmd_resource_show(dl);
6202 } else if (dl_argv_match(dl, "set")) {
6203 dl_arg_inc(dl);
6204 return cmd_resource_set(dl);
6205 }
6206 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6207 return -ENOENT;
6208}
6209
8b4fbf0b
AV
6210static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb)
6211{
6212 const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
6213 const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
6214 const char *region_name = mnl_attr_get_str(tb[DEVLINK_ATTR_REGION_NAME]);
6215 char buf[256];
6216
6217 sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name);
b20555e4
RD
6218 if (dl->json_output)
6219 open_json_object(buf);
6220 else
8b4fbf0b 6221 pr_out("%s:", buf);
8b4fbf0b
AV
6222}
6223
6224static void pr_out_region_handle_end(struct dl *dl)
6225{
6226 if (dl->json_output)
b20555e4 6227 close_json_object();
8b4fbf0b
AV
6228 else
6229 pr_out("\n");
6230}
6231
6232static void pr_out_region_snapshots_start(struct dl *dl, bool array)
6233{
56b725a4 6234 __pr_out_indent_newline(dl);
b20555e4
RD
6235 if (dl->json_output)
6236 open_json_array(PRINT_JSON, "snapshot");
6237 else
56b725a4 6238 pr_out("snapshot %s", array ? "[" : "");
8b4fbf0b
AV
6239}
6240
6241static void pr_out_region_snapshots_end(struct dl *dl, bool array)
6242{
6243 if (dl->json_output)
b20555e4 6244 close_json_array(PRINT_JSON, NULL);
8b4fbf0b
AV
6245 else if (array)
6246 pr_out("]");
6247}
6248
6249static void pr_out_region_snapshots_id(struct dl *dl, struct nlattr **tb, int index)
6250{
6251 uint32_t snapshot_id;
6252
6253 if (!tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
6254 return;
6255
6256 snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6257
6258 if (dl->json_output)
b20555e4 6259 print_uint(PRINT_JSON, NULL, NULL, snapshot_id);
8b4fbf0b
AV
6260 else
6261 pr_out("%s%u", index ? " " : "", snapshot_id);
6262}
6263
6264static void pr_out_snapshots(struct dl *dl, struct nlattr **tb)
6265{
6266 struct nlattr *tb_snapshot[DEVLINK_ATTR_MAX + 1] = {};
6267 struct nlattr *nla_sanpshot;
6268 int err, index = 0;
6269
6270 pr_out_region_snapshots_start(dl, true);
6271 mnl_attr_for_each_nested(nla_sanpshot, tb[DEVLINK_ATTR_REGION_SNAPSHOTS]) {
6272 err = mnl_attr_parse_nested(nla_sanpshot, attr_cb, tb_snapshot);
6273 if (err != MNL_CB_OK)
6274 return;
6275 pr_out_region_snapshots_id(dl, tb_snapshot, index++);
6276 }
6277 pr_out_region_snapshots_end(dl, true);
6278}
6279
6280static void pr_out_snapshot(struct dl *dl, struct nlattr **tb)
6281{
6282 pr_out_region_snapshots_start(dl, false);
6283 pr_out_region_snapshots_id(dl, tb, 0);
6284 pr_out_region_snapshots_end(dl, false);
6285}
6286
6287static void pr_out_region(struct dl *dl, struct nlattr **tb)
6288{
6289 pr_out_region_handle_start(dl, tb);
6290
6291 if (tb[DEVLINK_ATTR_REGION_SIZE])
6292 pr_out_u64(dl, "size",
6293 mnl_attr_get_u64(tb[DEVLINK_ATTR_REGION_SIZE]));
6294
6295 if (tb[DEVLINK_ATTR_REGION_SNAPSHOTS])
6296 pr_out_snapshots(dl, tb);
6297
6298 if (tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
6299 pr_out_snapshot(dl, tb);
6300
6301 pr_out_region_handle_end(dl);
6302}
6303
6304static int cmd_region_show_cb(const struct nlmsghdr *nlh, void *data)
6305{
6306 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6307 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6308 struct dl *dl = data;
6309
6310 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6311 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6312 !tb[DEVLINK_ATTR_REGION_NAME] || !tb[DEVLINK_ATTR_REGION_SIZE])
6313 return MNL_CB_ERROR;
6314
6315 pr_out_region(dl, tb);
6316
6317 return MNL_CB_OK;
6318}
6319
6320static int cmd_region_show(struct dl *dl)
6321{
6322 struct nlmsghdr *nlh;
6323 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
6324 int err;
6325
6326 if (dl_argc(dl) == 0)
6327 flags |= NLM_F_DUMP;
6328
6329 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_GET, flags);
6330
6331 if (dl_argc(dl) > 0) {
6332 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION, 0);
6333 if (err)
6334 return err;
6335 }
6336
6337 pr_out_section_start(dl, "regions");
6338 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_show_cb, dl);
6339 pr_out_section_end(dl);
6340 return err;
6341}
6342
6343static int cmd_region_snapshot_del(struct dl *dl)
6344{
6345 struct nlmsghdr *nlh;
6346 int err;
6347
6348 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_DEL,
6349 NLM_F_REQUEST | NLM_F_ACK);
6350
6351 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
6352 DL_OPT_REGION_SNAPSHOT_ID, 0);
6353 if (err)
6354 return err;
6355
6356 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6357}
6358
6359static int cmd_region_read_cb(const struct nlmsghdr *nlh, void *data)
6360{
6361 struct nlattr *nla_entry, *nla_chunk_data, *nla_chunk_addr;
6362 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6363 struct nlattr *tb_field[DEVLINK_ATTR_MAX + 1] = {};
6364 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6365 struct dl *dl = data;
6366 int err;
6367
6368 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6369 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6370 !tb[DEVLINK_ATTR_REGION_CHUNKS])
6371 return MNL_CB_ERROR;
6372
6373 mnl_attr_for_each_nested(nla_entry, tb[DEVLINK_ATTR_REGION_CHUNKS]) {
6374 err = mnl_attr_parse_nested(nla_entry, attr_cb, tb_field);
6375 if (err != MNL_CB_OK)
6376 return MNL_CB_ERROR;
6377
6378 nla_chunk_data = tb_field[DEVLINK_ATTR_REGION_CHUNK_DATA];
6379 if (!nla_chunk_data)
6380 continue;
6381
6382 nla_chunk_addr = tb_field[DEVLINK_ATTR_REGION_CHUNK_ADDR];
6383 if (!nla_chunk_addr)
6384 continue;
6385
6386 pr_out_region_chunk(dl, mnl_attr_get_payload(nla_chunk_data),
6387 mnl_attr_get_payload_len(nla_chunk_data),
6388 mnl_attr_get_u64(nla_chunk_addr));
6389 }
6390 return MNL_CB_OK;
6391}
6392
6393static int cmd_region_dump(struct dl *dl)
6394{
6395 struct nlmsghdr *nlh;
6396 int err;
6397
6398 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ,
6399 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
6400
6401 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
6402 DL_OPT_REGION_SNAPSHOT_ID, 0);
6403 if (err)
6404 return err;
6405
6406 pr_out_section_start(dl, "dump");
6407 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl);
6408 pr_out_section_end(dl);
6409 if (!dl->json_output)
6410 pr_out("\n");
6411 return err;
6412}
6413
6414static int cmd_region_read(struct dl *dl)
6415{
6416 struct nlmsghdr *nlh;
6417 int err;
6418
6419 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ,
6420 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
6421
6422 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
6423 DL_OPT_REGION_ADDRESS | DL_OPT_REGION_LENGTH |
6424 DL_OPT_REGION_SNAPSHOT_ID, 0);
6425 if (err)
6426 return err;
6427
6428 pr_out_section_start(dl, "read");
6429 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl);
6430 pr_out_section_end(dl);
6431 if (!dl->json_output)
6432 pr_out("\n");
6433 return err;
6434}
6435
6436static void cmd_region_help(void)
6437{
6438 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6439 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6440 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6441 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6442}
6443
6444static int cmd_region(struct dl *dl)
6445{
6446 if (dl_no_arg(dl)) {
6447 return cmd_region_show(dl);
6448 } else if (dl_argv_match(dl, "help")) {
6449 cmd_region_help();
6450 return 0;
6451 } else if (dl_argv_match(dl, "show")) {
6452 dl_arg_inc(dl);
6453 return cmd_region_show(dl);
6454 } else if (dl_argv_match(dl, "del")) {
6455 dl_arg_inc(dl);
6456 return cmd_region_snapshot_del(dl);
6457 } else if (dl_argv_match(dl, "dump")) {
6458 dl_arg_inc(dl);
6459 return cmd_region_dump(dl);
6460 } else if (dl_argv_match(dl, "read")) {
6461 dl_arg_inc(dl);
6462 return cmd_region_read(dl);
6463 }
6464 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6465 return -ENOENT;
6466}
6467
b18d8919
AL
6468static int cmd_health_set_params(struct dl *dl)
6469{
6470 struct nlmsghdr *nlh;
6471 int err;
6472
6473 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_SET,
6474 NLM_F_REQUEST | NLM_F_ACK);
6475 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME,
6476 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD |
6477 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER);
6478 if (err)
6479 return err;
6480
6481 dl_opts_put(nlh, dl);
6482 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6483}
6484
04b583d0
AL
6485static int cmd_health_dump_clear(struct dl *dl)
6486{
6487 struct nlmsghdr *nlh;
6488 int err;
6489
6490 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
6491 NLM_F_REQUEST | NLM_F_ACK);
6492
6493 err = dl_argv_parse_put(nlh, dl,
6494 DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
6495 if (err)
6496 return err;
6497
6498 dl_opts_put(nlh, dl);
6499 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6500}
6501
7b8baf83
AL
6502static int fmsg_value_show(struct dl *dl, int type, struct nlattr *nl_data)
6503{
6504 uint8_t *data;
6505 uint32_t len;
6506
3666eb0e 6507 check_indent_newline(dl);
7b8baf83
AL
6508 switch (type) {
6509 case MNL_TYPE_FLAG:
5a71671a 6510 print_bool(PRINT_ANY, NULL, "%s", mnl_attr_get_u8(nl_data));
7b8baf83
AL
6511 break;
6512 case MNL_TYPE_U8:
5a71671a 6513 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u8(nl_data));
7b8baf83
AL
6514 break;
6515 case MNL_TYPE_U16:
5a71671a 6516 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u16(nl_data));
7b8baf83
AL
6517 break;
6518 case MNL_TYPE_U32:
5a71671a 6519 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u32(nl_data));
7b8baf83
AL
6520 break;
6521 case MNL_TYPE_U64:
5a71671a 6522 print_u64(PRINT_ANY, NULL, "%"PRIu64, mnl_attr_get_u64(nl_data));
7b8baf83
AL
6523 break;
6524 case MNL_TYPE_NUL_STRING:
3666eb0e 6525 print_string(PRINT_ANY, NULL, "%s", mnl_attr_get_str(nl_data));
7b8baf83
AL
6526 break;
6527 case MNL_TYPE_BINARY:
6528 len = mnl_attr_get_payload_len(nl_data);
6529 data = mnl_attr_get_payload(nl_data);
6530 pr_out_binary_value(dl, data, len);
6531 break;
6532 default:
6533 return -EINVAL;
6534 }
6535 return MNL_CB_OK;
6536}
6537
af646bf9
AL
6538static void pr_out_fmsg_name(struct dl *dl, char **name)
6539{
6540 if (!*name)
6541 return;
6542
6543 pr_out_name(dl, *name);
6544 free(*name);
6545 *name = NULL;
6546}
6547
efd12cd2 6548struct nest_entry {
7b8baf83 6549 int attr_type;
efd12cd2 6550 struct list_head list;
7b8baf83
AL
6551};
6552
6553struct fmsg_cb_data {
af646bf9 6554 char *name;
7b8baf83
AL
6555 struct dl *dl;
6556 uint8_t value_type;
efd12cd2 6557 struct list_head entry_list;
7b8baf83
AL
6558};
6559
6560static int cmd_fmsg_nest_queue(struct fmsg_cb_data *fmsg_data,
6561 uint8_t *attr_value, bool insert)
6562{
efd12cd2 6563 struct nest_entry *entry;
7b8baf83
AL
6564
6565 if (insert) {
efd12cd2 6566 entry = malloc(sizeof(struct nest_entry));
7b8baf83
AL
6567 if (!entry)
6568 return -ENOMEM;
6569
6570 entry->attr_type = *attr_value;
efd12cd2 6571 list_add(&entry->list, &fmsg_data->entry_list);
7b8baf83 6572 } else {
efd12cd2 6573 if (list_empty(&fmsg_data->entry_list))
7b8baf83 6574 return MNL_CB_ERROR;
efd12cd2
JP
6575 entry = list_first_entry(&fmsg_data->entry_list,
6576 struct nest_entry, list);
7b8baf83 6577 *attr_value = entry->attr_type;
efd12cd2 6578 list_del(&entry->list);
7b8baf83
AL
6579 free(entry);
6580 }
6581 return MNL_CB_OK;
6582}
6583
af646bf9
AL
6584static void pr_out_fmsg_group_start(struct dl *dl, char **name)
6585{
6586 __pr_out_newline();
6587 pr_out_fmsg_name(dl, name);
6588 __pr_out_newline();
6589 __pr_out_indent_inc();
6590}
6591
6592static void pr_out_fmsg_group_end(struct dl *dl)
6593{
6594 __pr_out_newline();
6595 __pr_out_indent_dec();
6596}
6597
6598static void pr_out_fmsg_start_object(struct dl *dl, char **name)
6599{
6600 if (dl->json_output) {
6601 pr_out_fmsg_name(dl, name);
b20555e4 6602 open_json_object(NULL);
af646bf9
AL
6603 } else {
6604 pr_out_fmsg_group_start(dl, name);
6605 }
6606}
6607
6608static void pr_out_fmsg_end_object(struct dl *dl)
6609{
6610 if (dl->json_output)
b20555e4 6611 close_json_object();
af646bf9
AL
6612 else
6613 pr_out_fmsg_group_end(dl);
6614}
6615
6616static void pr_out_fmsg_start_array(struct dl *dl, char **name)
6617{
6618 if (dl->json_output) {
6619 pr_out_fmsg_name(dl, name);
b20555e4 6620 open_json_array(PRINT_JSON, NULL);
af646bf9
AL
6621 } else {
6622 pr_out_fmsg_group_start(dl, name);
6623 }
6624}
6625
6626static void pr_out_fmsg_end_array(struct dl *dl)
6627{
6628 if (dl->json_output)
b20555e4 6629 close_json_array(PRINT_JSON, NULL);
af646bf9
AL
6630 else
6631 pr_out_fmsg_group_end(dl);
6632}
6633
7b8baf83
AL
6634static int cmd_fmsg_nest(struct fmsg_cb_data *fmsg_data, uint8_t nest_value,
6635 bool start)
6636{
6637 struct dl *dl = fmsg_data->dl;
6638 uint8_t value = nest_value;
6639 int err;
6640
6641 err = cmd_fmsg_nest_queue(fmsg_data, &value, start);
6642 if (err != MNL_CB_OK)
6643 return err;
6644
6645 switch (value) {
6646 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
6647 if (start)
af646bf9 6648 pr_out_fmsg_start_object(dl, &fmsg_data->name);
7b8baf83 6649 else
af646bf9 6650 pr_out_fmsg_end_object(dl);
7b8baf83
AL
6651 break;
6652 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
6653 break;
6654 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
af646bf9
AL
6655 if (start)
6656 pr_out_fmsg_start_array(dl, &fmsg_data->name);
6657 else
6658 pr_out_fmsg_end_array(dl);
7b8baf83
AL
6659 break;
6660 default:
6661 return -EINVAL;
6662 }
6663 return MNL_CB_OK;
6664}
6665
6666static int cmd_fmsg_object_cb(const struct nlmsghdr *nlh, void *data)
6667{
6668 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6669 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6670 struct fmsg_cb_data *fmsg_data = data;
6671 struct dl *dl = fmsg_data->dl;
6672 struct nlattr *nla_object;
6673 int attr_type;
6674 int err;
6675
6676 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6677 if (!tb[DEVLINK_ATTR_FMSG])
6678 return MNL_CB_ERROR;
6679
6680 mnl_attr_for_each_nested(nla_object, tb[DEVLINK_ATTR_FMSG]) {
6681 attr_type = mnl_attr_get_type(nla_object);
6682 switch (attr_type) {
6683 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
6684 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
6685 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
6686 err = cmd_fmsg_nest(fmsg_data, attr_type, true);
6687 if (err != MNL_CB_OK)
6688 return err;
6689 break;
6690 case DEVLINK_ATTR_FMSG_NEST_END:
6691 err = cmd_fmsg_nest(fmsg_data, attr_type, false);
6692 if (err != MNL_CB_OK)
6693 return err;
6694 break;
6695 case DEVLINK_ATTR_FMSG_OBJ_NAME:
af646bf9
AL
6696 free(fmsg_data->name);
6697 fmsg_data->name = strdup(mnl_attr_get_str(nla_object));
6698 if (!fmsg_data->name)
6699 return -ENOMEM;
7b8baf83
AL
6700 break;
6701 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE:
6702 fmsg_data->value_type = mnl_attr_get_u8(nla_object);
6703 break;
6704 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
af646bf9 6705 pr_out_fmsg_name(dl, &fmsg_data->name);
7b8baf83
AL
6706 err = fmsg_value_show(dl, fmsg_data->value_type,
6707 nla_object);
6708 if (err != MNL_CB_OK)
6709 return err;
6710 break;
6711 default:
6712 return -EINVAL;
6713 }
6714 }
6715 return MNL_CB_OK;
6716}
6717
af646bf9
AL
6718static void cmd_fmsg_init(struct dl *dl, struct fmsg_cb_data *data)
6719{
6720 /* FMSG is dynamic: opening of an object or array causes a
6721 * newline. JSON starts with an { or [, but plain text should
6722 * not start with a new line. Ensure this by setting
6723 * g_new_line_count to 1: avoiding newline before the first
6724 * print.
6725 */
6726 g_new_line_count = 1;
6727 data->name = NULL;
6728 data->dl = dl;
6729 INIT_LIST_HEAD(&data->entry_list);
6730}
6731
b4d97ef5 6732static int cmd_health_object_common(struct dl *dl, uint8_t cmd, uint16_t flags)
7b8baf83
AL
6733{
6734 struct fmsg_cb_data data;
6735 struct nlmsghdr *nlh;
6736 int err;
6737
b4d97ef5 6738 nlh = mnlg_msg_prepare(dl->nlg, cmd, flags | NLM_F_REQUEST | NLM_F_ACK);
7b8baf83
AL
6739
6740 err = dl_argv_parse_put(nlh, dl,
6741 DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
6742 if (err)
6743 return err;
6744
af646bf9 6745 cmd_fmsg_init(dl, &data);
7b8baf83 6746 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_fmsg_object_cb, &data);
af646bf9 6747 free(data.name);
7b8baf83
AL
6748 return err;
6749}
6750
041e6e65
AL
6751static int cmd_health_dump_show(struct dl *dl)
6752{
b4d97ef5
AL
6753 return cmd_health_object_common(dl,
6754 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
6755 NLM_F_DUMP);
041e6e65
AL
6756}
6757
7b8baf83
AL
6758static int cmd_health_diagnose(struct dl *dl)
6759{
b4d97ef5
AL
6760 return cmd_health_object_common(dl,
6761 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
6762 0);
7b8baf83
AL
6763}
6764
9083a134
AL
6765static int cmd_health_recover(struct dl *dl)
6766{
6767 struct nlmsghdr *nlh;
6768 int err;
6769
6770 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
6771 NLM_F_REQUEST | NLM_F_ACK);
6772
6773 err = dl_argv_parse_put(nlh, dl,
6774 DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
6775 if (err)
6776 return err;
6777
6778 dl_opts_put(nlh, dl);
6779 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6780}
6781
2f1242ef
AL
6782enum devlink_health_reporter_state {
6783 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
6784 DEVLINK_HEALTH_REPORTER_STATE_ERROR,
6785};
6786
6787static const char *health_state_name(uint8_t state)
6788{
6789 switch (state) {
6790 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY:
6791 return HEALTH_REPORTER_STATE_HEALTHY_STR;
6792 case DEVLINK_HEALTH_REPORTER_STATE_ERROR:
6793 return HEALTH_REPORTER_STATE_ERROR_STR;
6794 default:
6795 return "<unknown state>";
6796 }
6797}
6798
f678a2d0 6799static void pr_out_dump_reporter_format_logtime(struct dl *dl, const struct nlattr *attr)
2f1242ef 6800{
f678a2d0
AL
6801 char dump_date[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
6802 char dump_time[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
6803 uint64_t time_ms = mnl_attr_get_u64(attr);
2f1242ef
AL
6804 struct sysinfo s_info;
6805 struct tm *info;
6806 time_t now, sec;
6807 int err;
6808
6809 time(&now);
6810 info = localtime(&now);
6811 err = sysinfo(&s_info);
6812 if (err)
6813 goto out;
6814 /* Subtract uptime in sec from now yields the time of system
6815 * uptime. To this, add time_ms which is the amount of
6816 * milliseconds elapsed between uptime and the dump taken.
6817 */
6818 sec = now - s_info.uptime + time_ms / 1000;
6819 info = localtime(&sec);
6820out:
f678a2d0
AL
6821 strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", info);
6822 strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", info);
3666eb0e
RD
6823 check_indent_newline(dl);
6824 print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date);
6825 print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time);
2f1242ef
AL
6826}
6827
746b66b0
AL
6828static void pr_out_dump_report_timestamp(struct dl *dl, const struct nlattr *attr)
6829{
6830 char dump_date[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
6831 char dump_time[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
6832 time_t tv_sec;
6833 struct tm *tm;
6834 uint64_t ts;
6835
6836 ts = mnl_attr_get_u64(attr);
6837 tv_sec = ts / 1000000000;
6838 tm = localtime(&tv_sec);
6839
6840 strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", tm);
6841 strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", tm);
6842
3666eb0e
RD
6843 check_indent_newline(dl);
6844 print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date);
6845 print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time);
746b66b0
AL
6846}
6847
2f1242ef
AL
6848static void pr_out_health(struct dl *dl, struct nlattr **tb_health)
6849{
6850 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6851 enum devlink_health_reporter_state state;
2f1242ef
AL
6852 int err;
6853
6854 err = mnl_attr_parse_nested(tb_health[DEVLINK_ATTR_HEALTH_REPORTER],
6855 attr_cb, tb);
6856 if (err != MNL_CB_OK)
6857 return;
6858
6859 if (!tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME] ||
6860 !tb[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT] ||
6861 !tb[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT] ||
6862 !tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE])
6863 return;
6864
6865 pr_out_handle_start_arr(dl, tb_health);
6866
3666eb0e
RD
6867 check_indent_newline(dl);
6868 print_string(PRINT_ANY, "reporter", "reporter %s",
6869 mnl_attr_get_str(tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME]));
2f1242ef
AL
6870 if (!dl->json_output) {
6871 __pr_out_newline();
6872 __pr_out_indent_inc();
6873 }
6874 state = mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE]);
3666eb0e
RD
6875 check_indent_newline(dl);
6876 print_string(PRINT_ANY, "state", "state %s", health_state_name(state));
2f1242ef
AL
6877 pr_out_u64(dl, "error",
6878 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT]));
6879 pr_out_u64(dl, "recover",
6880 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT]));
746b66b0
AL
6881 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS])
6882 pr_out_dump_report_timestamp(dl, tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS]);
6883 else if (tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS])
f678a2d0 6884 pr_out_dump_reporter_format_logtime(dl, tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS]);
2f1242ef
AL
6885 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6886 pr_out_u64(dl, "grace_period",
6887 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]));
6888 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
ff360fe9
RD
6889 print_bool(PRINT_ANY, "auto_recover", " auto_recover %s",
6890 mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]));
2f1242ef
AL
6891
6892 __pr_out_indent_dec();
6893 pr_out_handle_end(dl);
6894}
6895
6896static int cmd_health_show_cb(const struct nlmsghdr *nlh, void *data)
6897{
6898 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6899 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6900 struct dl *dl = data;
6901
6902 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6903 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6904 !tb[DEVLINK_ATTR_HEALTH_REPORTER])
6905 return MNL_CB_ERROR;
6906
6907 pr_out_health(dl, tb);
6908
6909 return MNL_CB_OK;
6910}
6911
6912static int cmd_health_show(struct dl *dl)
6913{
6914 struct nlmsghdr *nlh;
6915 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
6916 int err;
6917
6918 if (dl_argc(dl) == 0)
6919 flags |= NLM_F_DUMP;
6920 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_GET,
6921 flags);
6922
6923 if (dl_argc(dl) > 0) {
6924 err = dl_argv_parse_put(nlh, dl,
6925 DL_OPT_HANDLE |
6926 DL_OPT_HEALTH_REPORTER_NAME, 0);
6927 if (err)
6928 return err;
6929 }
6930 pr_out_section_start(dl, "health");
6931
6932 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_health_show_cb, dl);
6933 pr_out_section_end(dl);
6934 return err;
6935}
6936
6937static void cmd_health_help(void)
6938{
6939 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
9083a134 6940 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
7b8baf83 6941 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
041e6e65 6942 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
04b583d0 6943 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
b18d8919 6944 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
2f1242ef
AL
6945}
6946
6947static int cmd_health(struct dl *dl)
6948{
6949 if (dl_argv_match(dl, "help")) {
6950 cmd_health_help();
6951 return 0;
6952 } else if (dl_argv_match(dl, "show") ||
6953 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
6954 dl_arg_inc(dl);
6955 return cmd_health_show(dl);
9083a134
AL
6956 } else if (dl_argv_match(dl, "recover")) {
6957 dl_arg_inc(dl);
6958 return cmd_health_recover(dl);
7b8baf83
AL
6959 } else if (dl_argv_match(dl, "diagnose")) {
6960 dl_arg_inc(dl);
6961 return cmd_health_diagnose(dl);
041e6e65
AL
6962 } else if (dl_argv_match(dl, "dump")) {
6963 dl_arg_inc(dl);
6964 if (dl_argv_match(dl, "show")) {
6965 dl_arg_inc(dl);
6966 return cmd_health_dump_show(dl);
04b583d0
AL
6967 } else if (dl_argv_match(dl, "clear")) {
6968 dl_arg_inc(dl);
6969 return cmd_health_dump_clear(dl);
041e6e65 6970 }
b18d8919
AL
6971 } else if (dl_argv_match(dl, "set")) {
6972 dl_arg_inc(dl);
6973 return cmd_health_set_params(dl);
2f1242ef
AL
6974 }
6975 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6976 return -ENOENT;
6977}
6978
ef12d6da
IS
6979static const char *trap_type_name(uint8_t type)
6980{
6981 switch (type) {
6982 case DEVLINK_TRAP_TYPE_DROP:
6983 return "drop";
6984 case DEVLINK_TRAP_TYPE_EXCEPTION:
6985 return "exception";
6986 default:
6987 return "<unknown type>";
6988 }
6989}
6990
6991static const char *trap_action_name(uint8_t action)
6992{
6993 switch (action) {
6994 case DEVLINK_TRAP_ACTION_DROP:
6995 return "drop";
6996 case DEVLINK_TRAP_ACTION_TRAP:
6997 return "trap";
6998 default:
6999 return "<unknown action>";
7000 }
7001}
7002
7003static const char *trap_metadata_name(const struct nlattr *attr)
7004{
7005 switch (attr->nla_type) {
7006 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT:
7007 return "input_port";
4fe07b81
JP
7008 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE:
7009 return "flow_action_cookie";
ef12d6da
IS
7010 default:
7011 return "<unknown metadata type>";
7012 }
7013}
7014static void pr_out_trap_metadata(struct dl *dl, struct nlattr *attr)
7015{
7016 struct nlattr *attr_metadata;
7017
7018 pr_out_array_start(dl, "metadata");
3666eb0e
RD
7019 mnl_attr_for_each_nested(attr_metadata, attr) {
7020 check_indent_newline(dl);
7021 print_string(PRINT_ANY, NULL, "%s",
7022 trap_metadata_name(attr_metadata));
7023 }
ef12d6da
IS
7024 pr_out_array_end(dl);
7025}
7026
7027static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array)
7028{
7029 uint8_t action = mnl_attr_get_u8(tb[DEVLINK_ATTR_TRAP_ACTION]);
7030 uint8_t type = mnl_attr_get_u8(tb[DEVLINK_ATTR_TRAP_TYPE]);
7031
7032 if (array)
7033 pr_out_handle_start_arr(dl, tb);
7034 else
7035 __pr_out_handle_start(dl, tb, true, false);
7036
3666eb0e
RD
7037 check_indent_newline(dl);
7038 print_string(PRINT_ANY, "name", "name %s",
7039 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_NAME]));
7040 print_string(PRINT_ANY, "type", " type %s", trap_type_name(type));
ff360fe9 7041 print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]);
3666eb0e
RD
7042 print_string(PRINT_ANY, "action", " action %s", trap_action_name(action));
7043 print_string(PRINT_ANY, "group", " group %s",
7044 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME]));
ef12d6da
IS
7045 if (dl->verbose)
7046 pr_out_trap_metadata(dl, tb[DEVLINK_ATTR_TRAP_METADATA]);
7047 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7048 pr_out_handle_end(dl);
7049}
7050
7051static int cmd_trap_show_cb(const struct nlmsghdr *nlh, void *data)
7052{
7053 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7054 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7055 struct dl *dl = data;
7056
7057 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7058 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7059 !tb[DEVLINK_ATTR_TRAP_NAME] || !tb[DEVLINK_ATTR_TRAP_TYPE] ||
7060 !tb[DEVLINK_ATTR_TRAP_ACTION] ||
7061 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
7062 !tb[DEVLINK_ATTR_TRAP_METADATA] || !tb[DEVLINK_ATTR_STATS])
7063 return MNL_CB_ERROR;
7064
7065 pr_out_trap(dl, tb, true);
7066
7067 return MNL_CB_OK;
7068}
7069
7070static void cmd_trap_help(void)
7071{
7072 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
7073 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
4ede9e9d 7074 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
02a2a668 7075 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
4ede9e9d 7076 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
a66af556
IS
7077 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7078 pr_err(" devlink trap policer show DEV policer POLICER\n");
ef12d6da
IS
7079}
7080
7081static int cmd_trap_show(struct dl *dl)
7082{
7083 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7084 struct nlmsghdr *nlh;
7085 int err;
7086
7087 if (dl_argc(dl) == 0)
7088 flags |= NLM_F_DUMP;
7089
7090 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GET, flags);
7091
7092 if (dl_argc(dl) > 0) {
7093 err = dl_argv_parse_put(nlh, dl,
7094 DL_OPT_HANDLE | DL_OPT_TRAP_NAME, 0);
7095 if (err)
7096 return err;
7097 }
7098
7099 pr_out_section_start(dl, "trap");
7100 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_show_cb, dl);
7101 pr_out_section_end(dl);
7102
7103 return err;
7104}
7105
7106static int cmd_trap_set(struct dl *dl)
7107{
7108 struct nlmsghdr *nlh;
7109 int err;
7110
7111 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_SET,
7112 NLM_F_REQUEST | NLM_F_ACK);
7113
7114 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_TRAP_NAME,
7115 DL_OPT_TRAP_ACTION);
7116 if (err)
7117 return err;
7118
7119 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7120}
7121
4ede9e9d
IS
7122static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array)
7123{
7124 if (array)
7125 pr_out_handle_start_arr(dl, tb);
7126 else
7127 __pr_out_handle_start(dl, tb, true, false);
7128
3666eb0e
RD
7129 check_indent_newline(dl);
7130 print_string(PRINT_ANY, "name", "name %s",
7131 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME]));
ff360fe9 7132 print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]);
02a2a668
IS
7133 if (tb[DEVLINK_ATTR_TRAP_POLICER_ID])
7134 print_uint(PRINT_ANY, "policer", " policer %u",
7135 mnl_attr_get_u32(tb[DEVLINK_ATTR_TRAP_POLICER_ID]));
4ede9e9d
IS
7136 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7137 pr_out_handle_end(dl);
7138}
7139
7140static int cmd_trap_group_show_cb(const struct nlmsghdr *nlh, void *data)
7141{
7142 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7143 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7144 struct dl *dl = data;
7145
7146 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7147 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7148 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] || !tb[DEVLINK_ATTR_STATS])
7149 return MNL_CB_ERROR;
7150
7151 pr_out_trap_group(dl, tb, true);
7152
7153 return MNL_CB_OK;
7154}
7155
7156static int cmd_trap_group_show(struct dl *dl)
7157{
7158 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7159 struct nlmsghdr *nlh;
7160 int err;
7161
7162 if (dl_argc(dl) == 0)
7163 flags |= NLM_F_DUMP;
7164
7165 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GROUP_GET, flags);
7166
7167 if (dl_argc(dl) > 0) {
7168 err = dl_argv_parse_put(nlh, dl,
7169 DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME,
7170 0);
7171 if (err)
7172 return err;
7173 }
7174
7175 pr_out_section_start(dl, "trap_group");
7176 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_group_show_cb, dl);
7177 pr_out_section_end(dl);
7178
7179 return err;
7180}
7181
7182static int cmd_trap_group_set(struct dl *dl)
7183{
7184 struct nlmsghdr *nlh;
7185 int err;
7186
7187 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GROUP_SET,
7188 NLM_F_REQUEST | NLM_F_ACK);
7189
7190 err = dl_argv_parse_put(nlh, dl,
7191 DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME,
02a2a668 7192 DL_OPT_TRAP_ACTION | DL_OPT_TRAP_POLICER_ID);
4ede9e9d
IS
7193 if (err)
7194 return err;
7195
7196 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7197}
7198
7199static int cmd_trap_group(struct dl *dl)
7200{
7201 if (dl_argv_match(dl, "help")) {
7202 cmd_trap_help();
7203 return 0;
7204 } else if (dl_argv_match(dl, "show") ||
7205 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
7206 dl_arg_inc(dl);
7207 return cmd_trap_group_show(dl);
7208 } else if (dl_argv_match(dl, "set")) {
7209 dl_arg_inc(dl);
7210 return cmd_trap_group_set(dl);
7211 }
7212 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7213 return -ENOENT;
7214}
7215
a66af556
IS
7216static void pr_out_trap_policer(struct dl *dl, struct nlattr **tb, bool array)
7217{
7218 if (array)
7219 pr_out_handle_start_arr(dl, tb);
7220 else
7221 __pr_out_handle_start(dl, tb, true, false);
7222
7223 check_indent_newline(dl);
7224 print_uint(PRINT_ANY, "policer", "policer %u",
7225 mnl_attr_get_u32(tb[DEVLINK_ATTR_TRAP_POLICER_ID]));
7226 print_u64(PRINT_ANY, "rate", " rate %llu",
7227 mnl_attr_get_u64(tb[DEVLINK_ATTR_TRAP_POLICER_RATE]));
7228 print_u64(PRINT_ANY, "burst", " burst %llu",
7229 mnl_attr_get_u64(tb[DEVLINK_ATTR_TRAP_POLICER_BURST]));
7230 if (tb[DEVLINK_ATTR_STATS])
7231 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7232 pr_out_handle_end(dl);
7233}
7234
7235static int cmd_trap_policer_show_cb(const struct nlmsghdr *nlh, void *data)
7236{
7237 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7238 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7239 struct dl *dl = data;
7240
7241 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7242 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7243 !tb[DEVLINK_ATTR_TRAP_POLICER_ID] ||
7244 !tb[DEVLINK_ATTR_TRAP_POLICER_RATE] ||
7245 !tb[DEVLINK_ATTR_TRAP_POLICER_BURST])
7246 return MNL_CB_ERROR;
7247
7248 pr_out_trap_policer(dl, tb, true);
7249
7250 return MNL_CB_OK;
7251}
7252
7253static int cmd_trap_policer_show(struct dl *dl)
7254{
7255 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7256 struct nlmsghdr *nlh;
7257 int err;
7258
7259 if (dl_argc(dl) == 0)
7260 flags |= NLM_F_DUMP;
7261
7262 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_POLICER_GET, flags);
7263
7264 if (dl_argc(dl) > 0) {
7265 err = dl_argv_parse_put(nlh, dl,
7266 DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID,
7267 0);
7268 if (err)
7269 return err;
7270 }
7271
7272 pr_out_section_start(dl, "trap_policer");
7273 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_policer_show_cb, dl);
7274 pr_out_section_end(dl);
7275
7276 return err;
7277}
7278
7279static int cmd_trap_policer_set(struct dl *dl)
7280{
7281 struct nlmsghdr *nlh;
7282 int err;
7283
7284 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_POLICER_SET,
7285 NLM_F_REQUEST | NLM_F_ACK);
7286
7287 err = dl_argv_parse_put(nlh, dl,
7288 DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID,
7289 DL_OPT_TRAP_POLICER_RATE |
7290 DL_OPT_TRAP_POLICER_BURST);
7291 if (err)
7292 return err;
7293
7294 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7295}
7296
7297static int cmd_trap_policer(struct dl *dl)
7298{
7299 if (dl_argv_match(dl, "help")) {
7300 cmd_trap_help();
7301 return 0;
7302 } else if (dl_argv_match(dl, "show") ||
7303 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
7304 dl_arg_inc(dl);
7305 return cmd_trap_policer_show(dl);
7306 } else if (dl_argv_match(dl, "set")) {
7307 dl_arg_inc(dl);
7308 return cmd_trap_policer_set(dl);
7309 }
7310 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7311 return -ENOENT;
7312}
7313
ef12d6da
IS
7314static int cmd_trap(struct dl *dl)
7315{
7316 if (dl_argv_match(dl, "help")) {
7317 cmd_trap_help();
7318 return 0;
7319 } else if (dl_argv_match(dl, "show") ||
7320 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
7321 dl_arg_inc(dl);
7322 return cmd_trap_show(dl);
7323 } else if (dl_argv_match(dl, "set")) {
7324 dl_arg_inc(dl);
7325 return cmd_trap_set(dl);
4ede9e9d
IS
7326 } else if (dl_argv_match(dl, "group")) {
7327 dl_arg_inc(dl);
7328 return cmd_trap_group(dl);
a66af556
IS
7329 } else if (dl_argv_match(dl, "policer")) {
7330 dl_arg_inc(dl);
7331 return cmd_trap_policer(dl);
ef12d6da
IS
7332 }
7333 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7334 return -ENOENT;
7335}
7336
153c1a9b
AS
7337static void help(void)
7338{
7339 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
29993df8 7340 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
ef12d6da
IS
7341 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7342 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
153c1a9b
AS
7343}
7344
3e897912 7345static int dl_cmd(struct dl *dl, int argc, char **argv)
153c1a9b 7346{
3e897912
IV
7347 dl->argc = argc;
7348 dl->argv = argv;
7349
153c1a9b
AS
7350 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
7351 help();
7352 return 0;
7353 } else if (dl_argv_match(dl, "dev")) {
7354 dl_arg_inc(dl);
7355 return cmd_dev(dl);
7356 } else if (dl_argv_match(dl, "port")) {
7357 dl_arg_inc(dl);
7358 return cmd_port(dl);
7359 } else if (dl_argv_match(dl, "sb")) {
7360 dl_arg_inc(dl);
7361 return cmd_sb(dl);
7362 } else if (dl_argv_match(dl, "monitor")) {
7363 dl_arg_inc(dl);
7364 return cmd_mon(dl);
7365 } else if (dl_argv_match(dl, "dpipe")) {
7366 dl_arg_inc(dl);
7367 return cmd_dpipe(dl);
8cd64409
AS
7368 } else if (dl_argv_match(dl, "resource")) {
7369 dl_arg_inc(dl);
7370 return cmd_resource(dl);
8b4fbf0b
AV
7371 } else if (dl_argv_match(dl, "region")) {
7372 dl_arg_inc(dl);
7373 return cmd_region(dl);
2f1242ef
AL
7374 } else if (dl_argv_match(dl, "health")) {
7375 dl_arg_inc(dl);
7376 return cmd_health(dl);
ef12d6da
IS
7377 } else if (dl_argv_match(dl, "trap")) {
7378 dl_arg_inc(dl);
7379 return cmd_trap(dl);
153c1a9b
AS
7380 }
7381 pr_err("Object \"%s\" not found\n", dl_argv(dl));
7382 return -ENOENT;
7383}
7384
3e897912 7385static int dl_init(struct dl *dl)
153c1a9b
AS
7386{
7387 int err;
7388
153c1a9b
AS
7389 dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
7390 if (!dl->nlg) {
7391 pr_err("Failed to connect to devlink Netlink\n");
7392 return -errno;
7393 }
7394
7395 err = ifname_map_init(dl);
7396 if (err) {
7397 pr_err("Failed to create index map\n");
7398 goto err_ifname_map_create;
7399 }
b20555e4 7400 new_json_obj_plain(dl->json_output);
153c1a9b
AS
7401 return 0;
7402
153c1a9b
AS
7403err_ifname_map_create:
7404 mnlg_socket_close(dl->nlg);
7405 return err;
7406}
7407
7408static void dl_fini(struct dl *dl)
7409{
b20555e4 7410 delete_json_obj_plain();
153c1a9b
AS
7411 ifname_map_fini(dl);
7412 mnlg_socket_close(dl->nlg);
7413}
7414
7415static struct dl *dl_alloc(void)
7416{
7417 struct dl *dl;
7418
7419 dl = calloc(1, sizeof(*dl));
7420 if (!dl)
7421 return NULL;
7422 return dl;
7423}
7424
7425static void dl_free(struct dl *dl)
7426{
7427 free(dl);
7428}
7429
3e897912
IV
7430static int dl_batch(struct dl *dl, const char *name, bool force)
7431{
7432 char *line = NULL;
7433 size_t len = 0;
7434 int ret = EXIT_SUCCESS;
7435
7436 if (name && strcmp(name, "-") != 0) {
7437 if (freopen(name, "r", stdin) == NULL) {
7438 fprintf(stderr,
7439 "Cannot open file \"%s\" for reading: %s\n",
7440 name, strerror(errno));
7441 return EXIT_FAILURE;
7442 }
7443 }
7444
7445 cmdlineno = 0;
7446 while (getcmdline(&line, &len, stdin) != -1) {
7447 char *largv[100];
7448 int largc;
7449
7450 largc = makeargs(line, largv, 100);
7451 if (!largc)
7452 continue; /* blank line */
7453
7454 if (dl_cmd(dl, largc, largv)) {
7455 fprintf(stderr, "Command failed %s:%d\n",
7456 name, cmdlineno);
7457 ret = EXIT_FAILURE;
7458 if (!force)
7459 break;
7460 }
7461 }
7462
7463 if (line)
7464 free(line);
7465
7466 return ret;
7467}
7468
153c1a9b
AS
7469int main(int argc, char **argv)
7470{
7471 static const struct option long_options[] = {
7472 { "Version", no_argument, NULL, 'V' },
3e897912
IV
7473 { "force", no_argument, NULL, 'f' },
7474 { "batch", required_argument, NULL, 'b' },
153c1a9b
AS
7475 { "no-nice-names", no_argument, NULL, 'n' },
7476 { "json", no_argument, NULL, 'j' },
7477 { "pretty", no_argument, NULL, 'p' },
7478 { "verbose", no_argument, NULL, 'v' },
ef12d6da 7479 { "statistics", no_argument, NULL, 's' },
29993df8 7480 { "Netns", required_argument, NULL, 'N' },
153c1a9b
AS
7481 { NULL, 0, NULL, 0 }
7482 };
3e897912
IV
7483 const char *batch_file = NULL;
7484 bool force = false;
153c1a9b
AS
7485 struct dl *dl;
7486 int opt;
7487 int err;
7488 int ret;
7489
7490 dl = dl_alloc();
7491 if (!dl) {
7492 pr_err("Failed to allocate memory for devlink\n");
7493 return EXIT_FAILURE;
7494 }
7495
29993df8 7496 while ((opt = getopt_long(argc, argv, "Vfb:njpvsN:",
153c1a9b
AS
7497 long_options, NULL)) >= 0) {
7498
7499 switch (opt) {
7500 case 'V':
7501 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
7502 ret = EXIT_SUCCESS;
7503 goto dl_free;
3e897912
IV
7504 case 'f':
7505 force = true;
7506 break;
7507 case 'b':
7508 batch_file = optarg;
7509 break;
153c1a9b
AS
7510 case 'n':
7511 dl->no_nice_names = true;
7512 break;
7513 case 'j':
7514 dl->json_output = true;
7515 break;
7516 case 'p':
b20555e4 7517 pretty = true;
153c1a9b
AS
7518 break;
7519 case 'v':
7520 dl->verbose = true;
e3d0f0c0 7521 break;
ef12d6da
IS
7522 case 's':
7523 dl->stats = true;
7524 break;
29993df8
JP
7525 case 'N':
7526 if (netns_switch(optarg)) {
7527 ret = EXIT_FAILURE;
7528 goto dl_free;
7529 }
7530 break;
a3c4b484
JP
7531 default:
7532 pr_err("Unknown option.\n");
7533 help();
b77c77d2
LR
7534 ret = EXIT_FAILURE;
7535 goto dl_free;
a3c4b484
JP
7536 }
7537 }
7538
7539 argc -= optind;
7540 argv += optind;
7541
3e897912 7542 err = dl_init(dl);
a3c4b484
JP
7543 if (err) {
7544 ret = EXIT_FAILURE;
7545 goto dl_free;
7546 }
7547
3e897912
IV
7548 if (batch_file)
7549 err = dl_batch(dl, batch_file, force);
7550 else
7551 err = dl_cmd(dl, argc, argv);
7552
a3c4b484
JP
7553 if (err) {
7554 ret = EXIT_FAILURE;
7555 goto dl_fini;
7556 }
7557
7558 ret = EXIT_SUCCESS;
7559
7560dl_fini:
7561 dl_fini(dl);
7562dl_free:
7563 dl_free(dl);
7564
7565 return ret;
7566}