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