2 * devlink.c Devlink tool
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.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
21 #include <linux/genetlink.h>
22 #include <linux/devlink.h>
23 #include <libmnl/libmnl.h>
24 #include <netinet/ether.h>
25 #include <sys/sysinfo.h>
26 #include <sys/queue.h>
31 #include "json_writer.h"
34 #define ESWITCH_MODE_LEGACY "legacy"
35 #define ESWITCH_MODE_SWITCHDEV "switchdev"
36 #define ESWITCH_INLINE_MODE_NONE "none"
37 #define ESWITCH_INLINE_MODE_LINK "link"
38 #define ESWITCH_INLINE_MODE_NETWORK "network"
39 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
41 #define PARAM_CMODE_RUNTIME_STR "runtime"
42 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
43 #define PARAM_CMODE_PERMANENT_STR "permanent"
44 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
46 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
47 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
48 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
50 static int g_new_line_count
;
52 #define pr_err(args...) fprintf(stderr, ##args)
53 #define pr_out(args...) \
55 if (g_indent_newline) { \
56 fprintf(stdout, "%s", g_indent_str); \
57 g_indent_newline = false; \
59 fprintf(stdout, ##args); \
60 g_new_line_count = 0; \
63 #define pr_out_sp(num, args...) \
65 int ret = fprintf(stdout, ##args); \
67 fprintf(stdout, "%*s", num - ret, ""); \
68 g_new_line_count = 0; \
71 static int g_indent_level
;
72 static bool g_indent_newline
;
73 #define INDENT_STR_STEP 2
74 #define INDENT_STR_MAXLEN 32
75 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
77 static void __pr_out_indent_inc(void)
79 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
81 g_indent_level
+= INDENT_STR_STEP
;
82 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
83 g_indent_str
[g_indent_level
] = '\0';
86 static void __pr_out_indent_dec(void)
88 if (g_indent_level
- INDENT_STR_STEP
< 0)
90 g_indent_level
-= INDENT_STR_STEP
;
91 g_indent_str
[g_indent_level
] = '\0';
94 static void __pr_out_newline(void)
96 if (g_new_line_count
< 1) {
98 g_indent_newline
= true;
103 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
104 mnl_cb_t data_cb
, void *data
)
108 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
110 pr_err("devlink answers: %s\n", strerror(errno
));
116 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
117 const struct nlmsghdr
*nlh
,
118 mnl_cb_t data_cb
, void *data
)
122 err
= mnlg_socket_send(nlg
, nlh
);
124 pr_err("Failed to call mnlg_socket_send\n");
127 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
130 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
131 const char *group_name
)
135 err
= mnlg_socket_group_add(nlg
, group_name
);
137 pr_err("Failed to call mnlg_socket_group_add\n");
144 struct list_head list
;
151 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
152 const char *dev_name
,
156 struct ifname_map
*ifname_map
;
158 ifname_map
= calloc(1, sizeof(*ifname_map
));
161 ifname_map
->bus_name
= strdup(bus_name
);
162 ifname_map
->dev_name
= strdup(dev_name
);
163 ifname_map
->port_index
= port_index
;
164 ifname_map
->ifname
= strdup(ifname
);
165 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
166 !ifname_map
->ifname
) {
167 free(ifname_map
->ifname
);
168 free(ifname_map
->dev_name
);
169 free(ifname_map
->bus_name
);
176 static void ifname_map_free(struct ifname_map
*ifname_map
)
178 free(ifname_map
->ifname
);
179 free(ifname_map
->dev_name
);
180 free(ifname_map
->bus_name
);
184 #define DL_OPT_HANDLE BIT(0)
185 #define DL_OPT_HANDLEP BIT(1)
186 #define DL_OPT_PORT_TYPE BIT(2)
187 #define DL_OPT_PORT_COUNT BIT(3)
188 #define DL_OPT_SB BIT(4)
189 #define DL_OPT_SB_POOL BIT(5)
190 #define DL_OPT_SB_SIZE BIT(6)
191 #define DL_OPT_SB_TYPE BIT(7)
192 #define DL_OPT_SB_THTYPE BIT(8)
193 #define DL_OPT_SB_TH BIT(9)
194 #define DL_OPT_SB_TC BIT(10)
195 #define DL_OPT_ESWITCH_MODE BIT(11)
196 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
197 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
198 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
199 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
200 #define DL_OPT_RESOURCE_PATH BIT(16)
201 #define DL_OPT_RESOURCE_SIZE BIT(17)
202 #define DL_OPT_PARAM_NAME BIT(18)
203 #define DL_OPT_PARAM_VALUE BIT(19)
204 #define DL_OPT_PARAM_CMODE BIT(20)
205 #define DL_OPT_HANDLE_REGION BIT(21)
206 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
207 #define DL_OPT_REGION_ADDRESS BIT(23)
208 #define DL_OPT_REGION_LENGTH BIT(24)
209 #define DL_OPT_FLASH_FILE_NAME BIT(25)
210 #define DL_OPT_FLASH_COMPONENT BIT(26)
211 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
212 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(27)
213 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(28)
216 uint32_t present
; /* flags of present items */
220 enum devlink_port_type port_type
;
223 uint16_t sb_pool_index
;
224 uint32_t sb_pool_size
;
225 enum devlink_sb_pool_type sb_pool_type
;
226 enum devlink_sb_threshold_type sb_pool_thtype
;
227 uint32_t sb_threshold
;
228 uint16_t sb_tc_index
;
229 enum devlink_eswitch_mode eswitch_mode
;
230 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
231 const char *dpipe_table_name
;
232 bool dpipe_counters_enable
;
233 bool eswitch_encap_mode
;
234 const char *resource_path
;
235 uint32_t resource_size
;
236 uint32_t resource_id
;
237 bool resource_id_valid
;
238 const char *param_name
;
239 const char *param_value
;
240 enum devlink_param_cmode cmode
;
242 uint32_t region_snapshot_id
;
243 uint64_t region_address
;
244 uint64_t region_length
;
245 const char *flash_file_name
;
246 const char *flash_component
;
247 const char *reporter_name
;
248 uint64_t reporter_graceful_period
;
249 bool reporter_auto_recover
;
253 struct mnlg_socket
*nlg
;
254 struct list_head ifname_map_list
;
271 static int dl_argc(struct dl
*dl
)
276 static char *dl_argv(struct dl
*dl
)
278 if (dl_argc(dl
) == 0)
283 static void dl_arg_inc(struct dl
*dl
)
285 if (dl_argc(dl
) == 0)
291 static char *dl_argv_next(struct dl
*dl
)
295 if (dl_argc(dl
) == 0)
303 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
305 if (index
>= dl_argc(dl
))
307 return dl
->argv
[index
];
310 static int strcmpx(const char *str1
, const char *str2
)
312 if (strlen(str1
) > strlen(str2
))
314 return strncmp(str1
, str2
, strlen(str1
));
317 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
319 if (dl_argc(dl
) == 0)
321 return strcmpx(dl_argv(dl
), pattern
) == 0;
324 static bool dl_no_arg(struct dl
*dl
)
326 return dl_argc(dl
) == 0;
329 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
330 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
331 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
332 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
333 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
334 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
335 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
336 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
337 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
338 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
339 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
340 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
341 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
342 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
343 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
344 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
345 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
346 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
347 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
348 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
349 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
350 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
351 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
352 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
353 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
354 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
355 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
356 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
357 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
358 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
359 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
360 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
361 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
362 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
363 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
364 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
365 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
366 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
367 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
368 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
369 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
370 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
371 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
372 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
373 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
374 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
375 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
376 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
377 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
378 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
379 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
380 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
381 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
382 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
383 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
384 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
385 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
386 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
387 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
388 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
389 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
390 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
391 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
392 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
393 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
394 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
395 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
396 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
397 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
398 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
399 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
400 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
401 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
402 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
403 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
404 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
405 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
406 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
407 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
408 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
409 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
410 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
411 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
412 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
413 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
414 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
415 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
416 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
419 static int attr_cb(const struct nlattr
*attr
, void *data
)
421 const struct nlattr
**tb
= data
;
424 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
427 type
= mnl_attr_get_type(attr
);
428 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
435 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
437 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
438 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
439 struct dl
*dl
= data
;
440 struct ifname_map
*ifname_map
;
441 const char *bus_name
;
442 const char *dev_name
;
443 uint32_t port_ifindex
;
444 const char *port_ifname
;
446 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
447 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
448 !tb
[DEVLINK_ATTR_PORT_INDEX
])
451 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
454 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
455 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
456 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
457 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
458 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
459 port_ifindex
, port_ifname
);
462 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
467 static void ifname_map_fini(struct dl
*dl
)
469 struct ifname_map
*ifname_map
, *tmp
;
471 list_for_each_entry_safe(ifname_map
, tmp
,
472 &dl
->ifname_map_list
, list
) {
473 list_del(&ifname_map
->list
);
474 ifname_map_free(ifname_map
);
478 static int ifname_map_init(struct dl
*dl
)
480 struct nlmsghdr
*nlh
;
483 INIT_LIST_HEAD(&dl
->ifname_map_list
);
485 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
486 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
488 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
496 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
497 char **p_bus_name
, char **p_dev_name
,
498 uint32_t *p_port_index
)
500 struct ifname_map
*ifname_map
;
502 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
503 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
504 *p_bus_name
= ifname_map
->bus_name
;
505 *p_dev_name
= ifname_map
->dev_name
;
506 *p_port_index
= ifname_map
->port_index
;
513 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
514 const char *dev_name
, uint32_t port_index
,
517 struct ifname_map
*ifname_map
;
519 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
520 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
521 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
522 port_index
== ifname_map
->port_index
) {
523 *p_ifname
= ifname_map
->ifname
;
530 static unsigned int strslashcount(char *str
)
532 unsigned int count
= 0;
535 while ((pos
= strchr(pos
, '/'))) {
542 static int strslashrsplit(char *str
, char **before
, char **after
)
546 slash
= strrchr(str
, '/');
555 static int strtouint64_t(const char *str
, uint64_t *p_val
)
558 unsigned long long int val
;
560 val
= strtoull(str
, &endptr
, 10);
561 if (endptr
== str
|| *endptr
!= '\0')
569 static int strtouint32_t(const char *str
, uint32_t *p_val
)
572 unsigned long int val
;
574 val
= strtoul(str
, &endptr
, 10);
575 if (endptr
== str
|| *endptr
!= '\0')
583 static int strtouint16_t(const char *str
, uint16_t *p_val
)
586 unsigned long int val
;
588 val
= strtoul(str
, &endptr
, 10);
589 if (endptr
== str
|| *endptr
!= '\0')
597 static int strtouint8_t(const char *str
, uint8_t *p_val
)
600 unsigned long int val
;
602 val
= strtoul(str
, &endptr
, 10);
603 if (endptr
== str
|| *endptr
!= '\0')
611 static int strtobool(const char *str
, bool *p_val
)
615 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
617 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
625 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
627 strslashrsplit(str
, p_bus_name
, p_dev_name
);
631 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
633 char *str
= dl_argv_next(dl
);
636 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
639 if (strslashcount(str
) != 1) {
640 pr_err("Wrong devlink identification string format.\n");
641 pr_err("Expected \"bus_name/dev_name\".\n");
644 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
647 static int __dl_argv_handle_port(char *str
,
648 char **p_bus_name
, char **p_dev_name
,
649 uint32_t *p_port_index
)
655 err
= strslashrsplit(str
, &handlestr
, &portstr
);
657 pr_err("Port identification \"%s\" is invalid\n", str
);
660 err
= strtouint32_t(portstr
, p_port_index
);
662 pr_err("Port index \"%s\" is not a number or not within range\n",
666 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
668 pr_err("Port identification \"%s\" is invalid\n", str
);
674 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
675 char **p_bus_name
, char **p_dev_name
,
676 uint32_t *p_port_index
)
680 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
683 pr_err("Netdevice \"%s\" not found\n", str
);
689 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
690 char **p_dev_name
, uint32_t *p_port_index
)
692 char *str
= dl_argv_next(dl
);
693 unsigned int slash_count
;
696 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
699 slash_count
= strslashcount(str
);
700 switch (slash_count
) {
702 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
703 p_dev_name
, p_port_index
);
705 return __dl_argv_handle_port(str
, p_bus_name
,
706 p_dev_name
, p_port_index
);
708 pr_err("Wrong port identification string format.\n");
709 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
714 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
715 char **p_dev_name
, uint32_t *p_port_index
,
716 uint32_t *p_handle_bit
)
718 char *str
= dl_argv_next(dl
);
719 unsigned int slash_count
;
723 pr_err("One of following identifications expected:\n"
724 "Devlink identification (\"bus_name/dev_name\")\n"
725 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
728 slash_count
= strslashcount(str
);
729 if (slash_count
== 1) {
730 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
733 *p_handle_bit
= DL_OPT_HANDLE
;
734 } else if (slash_count
== 2) {
735 err
= __dl_argv_handle_port(str
, p_bus_name
,
736 p_dev_name
, p_port_index
);
739 *p_handle_bit
= DL_OPT_HANDLEP
;
740 } else if (slash_count
== 0) {
741 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
742 p_dev_name
, p_port_index
);
745 *p_handle_bit
= DL_OPT_HANDLEP
;
747 pr_err("Wrong port identification string format.\n");
748 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
754 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
755 char **p_dev_name
, char **p_region
)
760 err
= strslashrsplit(str
, &handlestr
, p_region
);
762 pr_err("Region identification \"%s\" is invalid\n", str
);
765 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
767 pr_err("Region identification \"%s\" is invalid\n", str
);
773 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
774 char **p_dev_name
, char **p_region
)
776 char *str
= dl_argv_next(dl
);
777 unsigned int slash_count
;
780 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
784 slash_count
= strslashcount(str
);
785 if (slash_count
!= 2) {
786 pr_err("Wrong region identification string format.\n");
787 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
791 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
794 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
796 char *str
= dl_argv_next(dl
);
800 pr_err("Unsigned number argument expected\n");
804 err
= strtouint64_t(str
, p_val
);
806 pr_err("\"%s\" is not a number or not within range\n", str
);
812 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
814 char *str
= dl_argv_next(dl
);
818 pr_err("Unsigned number argument expected\n");
822 err
= strtouint32_t(str
, p_val
);
824 pr_err("\"%s\" is not a number or not within range\n", str
);
830 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
832 char *str
= dl_argv_next(dl
);
836 pr_err("Unsigned number argument expected\n");
840 err
= strtouint16_t(str
, p_val
);
842 pr_err("\"%s\" is not a number or not within range\n", str
);
848 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
850 char *str
= dl_argv_next(dl
);
854 pr_err("Boolean argument expected\n");
858 err
= strtobool(str
, p_val
);
860 pr_err("\"%s\" is not a valid boolean value\n", str
);
866 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
868 const char *str
= dl_argv_next(dl
);
871 pr_err("String parameter expected\n");
878 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
880 if (strcmp(typestr
, "auto") == 0) {
881 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
882 } else if (strcmp(typestr
, "eth") == 0) {
883 *p_type
= DEVLINK_PORT_TYPE_ETH
;
884 } else if (strcmp(typestr
, "ib") == 0) {
885 *p_type
= DEVLINK_PORT_TYPE_IB
;
887 pr_err("Unknown port type \"%s\"\n", typestr
);
893 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
895 if (strcmp(typestr
, "ingress") == 0) {
896 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
897 } else if (strcmp(typestr
, "egress") == 0) {
898 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
900 pr_err("Unknown pool type \"%s\"\n", typestr
);
906 static int threshold_type_get(const char *typestr
,
907 enum devlink_sb_threshold_type
*p_type
)
909 if (strcmp(typestr
, "static") == 0) {
910 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
911 } else if (strcmp(typestr
, "dynamic") == 0) {
912 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
914 pr_err("Unknown threshold type \"%s\"\n", typestr
);
920 static int eswitch_mode_get(const char *typestr
,
921 enum devlink_eswitch_mode
*p_mode
)
923 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
924 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
925 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
926 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
928 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
934 static int eswitch_inline_mode_get(const char *typestr
,
935 enum devlink_eswitch_inline_mode
*p_mode
)
937 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
938 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
939 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
940 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
941 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
942 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
943 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
944 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
946 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
952 static int dpipe_counters_enable_get(const char *typestr
,
953 bool *counters_enable
)
955 if (strcmp(typestr
, "enable") == 0) {
956 *counters_enable
= 1;
957 } else if (strcmp(typestr
, "disable") == 0) {
958 *counters_enable
= 0;
960 pr_err("Unknown counter_state \"%s\"\n", typestr
);
966 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
968 if (strcmp(typestr
, "enable") == 0) {
970 } else if (strcmp(typestr
, "disable") == 0) {
973 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
979 static int param_cmode_get(const char *cmodestr
,
980 enum devlink_param_cmode
*cmode
)
982 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
983 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
984 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
985 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
986 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
987 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
989 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
995 struct dl_args_metadata
{
997 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1000 static const struct dl_args_metadata dl_args_required
[] = {
1001 {DL_OPT_PORT_TYPE
, "Port type not set."},
1002 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1003 {DL_OPT_SB_POOL
, "Pool index option expected."},
1004 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1005 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1006 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1007 {DL_OPT_SB_TH
, "Threshold option expected."},
1008 {DL_OPT_SB_TC
, "TC index option expected."},
1009 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1010 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1011 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1012 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1013 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1014 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1015 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1016 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1017 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1018 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1019 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1020 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1023 static int dl_args_finding_required_validate(uint32_t o_required
,
1029 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1030 o_flag
= dl_args_required
[i
].o_flag
;
1031 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1032 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1039 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
1040 uint32_t o_optional
)
1042 struct dl_opts
*opts
= &dl
->opts
;
1043 uint32_t o_all
= o_required
| o_optional
;
1044 uint32_t o_found
= 0;
1047 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1048 uint32_t handle_bit
;
1050 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1051 &opts
->port_index
, &handle_bit
);
1054 o_found
|= handle_bit
;
1055 } else if (o_required
& DL_OPT_HANDLE
) {
1056 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1059 o_found
|= DL_OPT_HANDLE
;
1060 } else if (o_required
& DL_OPT_HANDLEP
) {
1061 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1065 o_found
|= DL_OPT_HANDLEP
;
1066 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1067 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1069 &opts
->region_name
);
1072 o_found
|= DL_OPT_HANDLE_REGION
;
1075 while (dl_argc(dl
)) {
1076 if (dl_argv_match(dl
, "type") &&
1077 (o_all
& DL_OPT_PORT_TYPE
)) {
1078 const char *typestr
;
1081 err
= dl_argv_str(dl
, &typestr
);
1084 err
= port_type_get(typestr
, &opts
->port_type
);
1087 o_found
|= DL_OPT_PORT_TYPE
;
1088 } else if (dl_argv_match(dl
, "count") &&
1089 (o_all
& DL_OPT_PORT_COUNT
)) {
1091 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1094 o_found
|= DL_OPT_PORT_COUNT
;
1095 } else if (dl_argv_match(dl
, "sb") &&
1096 (o_all
& DL_OPT_SB
)) {
1098 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1101 o_found
|= DL_OPT_SB
;
1102 } else if (dl_argv_match(dl
, "pool") &&
1103 (o_all
& DL_OPT_SB_POOL
)) {
1105 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1108 o_found
|= DL_OPT_SB_POOL
;
1109 } else if (dl_argv_match(dl
, "size") &&
1110 (o_all
& DL_OPT_SB_SIZE
)) {
1112 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1115 o_found
|= DL_OPT_SB_SIZE
;
1116 } else if (dl_argv_match(dl
, "type") &&
1117 (o_all
& DL_OPT_SB_TYPE
)) {
1118 const char *typestr
;
1121 err
= dl_argv_str(dl
, &typestr
);
1124 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1127 o_found
|= DL_OPT_SB_TYPE
;
1128 } else if (dl_argv_match(dl
, "thtype") &&
1129 (o_all
& DL_OPT_SB_THTYPE
)) {
1130 const char *typestr
;
1133 err
= dl_argv_str(dl
, &typestr
);
1136 err
= threshold_type_get(typestr
,
1137 &opts
->sb_pool_thtype
);
1140 o_found
|= DL_OPT_SB_THTYPE
;
1141 } else if (dl_argv_match(dl
, "th") &&
1142 (o_all
& DL_OPT_SB_TH
)) {
1144 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1147 o_found
|= DL_OPT_SB_TH
;
1148 } else if (dl_argv_match(dl
, "tc") &&
1149 (o_all
& DL_OPT_SB_TC
)) {
1151 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1154 o_found
|= DL_OPT_SB_TC
;
1155 } else if (dl_argv_match(dl
, "mode") &&
1156 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1157 const char *typestr
;
1160 err
= dl_argv_str(dl
, &typestr
);
1163 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1166 o_found
|= DL_OPT_ESWITCH_MODE
;
1167 } else if (dl_argv_match(dl
, "inline-mode") &&
1168 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1169 const char *typestr
;
1172 err
= dl_argv_str(dl
, &typestr
);
1175 err
= eswitch_inline_mode_get(
1176 typestr
, &opts
->eswitch_inline_mode
);
1179 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1180 } else if (dl_argv_match(dl
, "name") &&
1181 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1183 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1186 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1187 } else if (dl_argv_match(dl
, "counters") &&
1188 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1189 const char *typestr
;
1192 err
= dl_argv_str(dl
, &typestr
);
1195 err
= dpipe_counters_enable_get(typestr
,
1196 &opts
->dpipe_counters_enable
);
1199 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1200 } else if (dl_argv_match(dl
, "encap") &&
1201 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1202 const char *typestr
;
1205 err
= dl_argv_str(dl
, &typestr
);
1208 err
= eswitch_encap_mode_get(typestr
,
1209 &opts
->eswitch_encap_mode
);
1212 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1213 } else if (dl_argv_match(dl
, "path") &&
1214 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1216 err
= dl_argv_str(dl
, &opts
->resource_path
);
1219 o_found
|= DL_OPT_RESOURCE_PATH
;
1220 } else if (dl_argv_match(dl
, "size") &&
1221 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1223 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1226 o_found
|= DL_OPT_RESOURCE_SIZE
;
1227 } else if (dl_argv_match(dl
, "name") &&
1228 (o_all
& DL_OPT_PARAM_NAME
)) {
1230 err
= dl_argv_str(dl
, &opts
->param_name
);
1233 o_found
|= DL_OPT_PARAM_NAME
;
1234 } else if (dl_argv_match(dl
, "value") &&
1235 (o_all
& DL_OPT_PARAM_VALUE
)) {
1237 err
= dl_argv_str(dl
, &opts
->param_value
);
1240 o_found
|= DL_OPT_PARAM_VALUE
;
1241 } else if (dl_argv_match(dl
, "cmode") &&
1242 (o_all
& DL_OPT_PARAM_CMODE
)) {
1243 const char *cmodestr
;
1246 err
= dl_argv_str(dl
, &cmodestr
);
1249 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1252 o_found
|= DL_OPT_PARAM_CMODE
;
1253 } else if (dl_argv_match(dl
, "snapshot") &&
1254 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1256 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1259 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1260 } else if (dl_argv_match(dl
, "address") &&
1261 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1263 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1266 o_found
|= DL_OPT_REGION_ADDRESS
;
1267 } else if (dl_argv_match(dl
, "length") &&
1268 (o_all
& DL_OPT_REGION_LENGTH
)) {
1270 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1273 o_found
|= DL_OPT_REGION_LENGTH
;
1274 } else if (dl_argv_match(dl
, "file") &&
1275 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1277 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1280 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1281 } else if (dl_argv_match(dl
, "component") &&
1282 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1284 err
= dl_argv_str(dl
, &opts
->flash_component
);
1287 o_found
|= DL_OPT_FLASH_COMPONENT
;
1288 } else if (dl_argv_match(dl
, "reporter") &&
1289 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1291 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1294 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1295 } else if (dl_argv_match(dl
, "grace_period") &&
1296 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1298 err
= dl_argv_uint64_t(dl
,
1299 &opts
->reporter_graceful_period
);
1302 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1303 } else if (dl_argv_match(dl
, "auto_recover") &&
1304 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1306 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1309 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1311 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1316 opts
->present
= o_found
;
1318 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1320 opts
->present
|= DL_OPT_SB
;
1323 return dl_args_finding_required_validate(o_required
, o_found
);
1326 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1328 struct dl_opts
*opts
= &dl
->opts
;
1330 if (opts
->present
& DL_OPT_HANDLE
) {
1331 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1332 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1333 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1334 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1335 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1336 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1338 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1339 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1340 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1341 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1344 if (opts
->present
& DL_OPT_PORT_TYPE
)
1345 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1347 if (opts
->present
& DL_OPT_PORT_COUNT
)
1348 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1350 if (opts
->present
& DL_OPT_SB
)
1351 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1353 if (opts
->present
& DL_OPT_SB_POOL
)
1354 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1355 opts
->sb_pool_index
);
1356 if (opts
->present
& DL_OPT_SB_SIZE
)
1357 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1358 opts
->sb_pool_size
);
1359 if (opts
->present
& DL_OPT_SB_TYPE
)
1360 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1361 opts
->sb_pool_type
);
1362 if (opts
->present
& DL_OPT_SB_THTYPE
)
1363 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1364 opts
->sb_pool_thtype
);
1365 if (opts
->present
& DL_OPT_SB_TH
)
1366 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1367 opts
->sb_threshold
);
1368 if (opts
->present
& DL_OPT_SB_TC
)
1369 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1371 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1372 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1373 opts
->eswitch_mode
);
1374 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1375 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1376 opts
->eswitch_inline_mode
);
1377 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1378 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1379 opts
->dpipe_table_name
);
1380 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1381 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1382 opts
->dpipe_counters_enable
);
1383 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1384 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1385 opts
->eswitch_encap_mode
);
1386 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1387 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1389 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1390 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1391 opts
->resource_size
);
1392 if (opts
->present
& DL_OPT_PARAM_NAME
)
1393 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1395 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1396 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1398 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1399 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1400 opts
->region_snapshot_id
);
1401 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1402 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1403 opts
->region_address
);
1404 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1405 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1406 opts
->region_length
);
1407 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1408 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1409 opts
->flash_file_name
);
1410 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1411 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1412 opts
->flash_component
);
1413 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1414 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1415 opts
->reporter_name
);
1416 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1417 mnl_attr_put_u64(nlh
,
1418 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1419 opts
->reporter_graceful_period
);
1420 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1421 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1422 opts
->reporter_auto_recover
);
1426 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1427 uint32_t o_required
, uint32_t o_optional
)
1431 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1434 dl_opts_put(nlh
, dl
);
1438 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1440 struct dl_opts
*opts
= &dl
->opts
;
1441 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1442 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1443 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1444 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1446 if (opts
->present
& DL_OPT_HANDLE
&&
1447 attr_bus_name
&& attr_dev_name
) {
1448 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1449 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1451 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1452 strcmp(dev_name
, opts
->dev_name
) != 0)
1455 if (opts
->present
& DL_OPT_HANDLEP
&&
1456 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1457 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1458 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1459 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1461 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1462 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1463 port_index
!= opts
->port_index
)
1466 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1467 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1469 if (sb_index
!= opts
->sb_index
)
1475 static void cmd_dev_help(void)
1477 pr_err("Usage: devlink dev show [ DEV ]\n");
1478 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1479 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1480 pr_err(" [ encap { disable | enable } ]\n");
1481 pr_err(" devlink dev eswitch show DEV\n");
1482 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1483 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1484 pr_err(" devlink dev reload DEV\n");
1485 pr_err(" devlink dev info [ DEV ]\n");
1486 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1489 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1490 const char *dev_name
)
1492 if (!dl
->arr_last
.present
)
1494 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1495 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1498 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1499 const char *dev_name
)
1501 dl
->arr_last
.present
= true;
1502 free(dl
->arr_last
.dev_name
);
1503 free(dl
->arr_last
.bus_name
);
1504 dl
->arr_last
.bus_name
= strdup(bus_name
);
1505 dl
->arr_last
.dev_name
= strdup(dev_name
);
1508 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1509 const char *dev_name
)
1511 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1514 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1515 const char *dev_name
)
1517 return dl
->arr_last
.present
&&
1518 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1521 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1522 bool content
, bool array
)
1524 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1525 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1528 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1530 if (dl
->json_output
) {
1532 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1533 jsonw_end_array(dl
->jw
);
1534 if (should_arr_last_handle_start(dl
, bus_name
,
1536 jsonw_name(dl
->jw
, buf
);
1537 jsonw_start_array(dl
->jw
);
1538 jsonw_start_object(dl
->jw
);
1539 arr_last_handle_set(dl
, bus_name
, dev_name
);
1541 jsonw_start_object(dl
->jw
);
1544 jsonw_name(dl
->jw
, buf
);
1545 jsonw_start_object(dl
->jw
);
1549 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1550 __pr_out_indent_dec();
1551 if (should_arr_last_handle_start(dl
, bus_name
,
1553 pr_out("%s%s", buf
, content
? ":" : "");
1555 __pr_out_indent_inc();
1556 arr_last_handle_set(dl
, bus_name
, dev_name
);
1559 pr_out("%s%s", buf
, content
? ":" : "");
1564 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1566 __pr_out_handle_start(dl
, tb
, true, true);
1569 static void pr_out_handle_end(struct dl
*dl
)
1571 if (dl
->json_output
)
1572 jsonw_end_object(dl
->jw
);
1577 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1579 __pr_out_handle_start(dl
, tb
, false, false);
1580 pr_out_handle_end(dl
);
1583 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1584 const char *dev_name
, uint32_t port_index
)
1586 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1587 dl
->arr_last
.port_index
== port_index
;
1590 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1591 const char *dev_name
, uint32_t port_index
)
1593 arr_last_handle_set(dl
, bus_name
, dev_name
);
1594 dl
->arr_last
.port_index
= port_index
;
1597 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1598 const char *bus_name
,
1599 const char *dev_name
,
1600 uint32_t port_index
)
1602 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1605 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1606 const char *bus_name
,
1607 const char *dev_name
,
1608 uint32_t port_index
)
1610 return dl
->arr_last
.present
&&
1611 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1614 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1615 const char *dev_name
,
1616 uint32_t port_index
, bool try_nice
,
1619 static char buf
[32];
1620 char *ifname
= NULL
;
1622 if (dl
->no_nice_names
|| !try_nice
||
1623 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1624 port_index
, &ifname
) != 0)
1625 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1627 sprintf(buf
, "%s", ifname
);
1629 if (dl
->json_output
) {
1631 if (should_arr_last_port_handle_end(dl
, bus_name
,
1634 jsonw_end_array(dl
->jw
);
1635 if (should_arr_last_port_handle_start(dl
, bus_name
,
1638 jsonw_name(dl
->jw
, buf
);
1639 jsonw_start_array(dl
->jw
);
1640 jsonw_start_object(dl
->jw
);
1641 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1644 jsonw_start_object(dl
->jw
);
1647 jsonw_name(dl
->jw
, buf
);
1648 jsonw_start_object(dl
->jw
);
1655 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1657 const char *bus_name
;
1658 const char *dev_name
;
1659 uint32_t port_index
;
1661 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1662 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1663 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1664 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1667 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1669 const char *bus_name
;
1670 const char *dev_name
;
1671 uint32_t port_index
;
1673 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1674 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1675 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1676 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1679 static void pr_out_port_handle_end(struct dl
*dl
)
1681 if (dl
->json_output
)
1682 jsonw_end_object(dl
->jw
);
1688 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1690 if (dl
->json_output
) {
1691 jsonw_string_field(dl
->jw
, name
, val
);
1693 if (g_indent_newline
)
1694 pr_out("%s %s", name
, val
);
1696 pr_out(" %s %s", name
, val
);
1700 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1702 if (dl
->json_output
)
1703 jsonw_bool_field(dl
->jw
, name
, val
);
1705 pr_out_str(dl
, name
, val
? "true" : "false");
1708 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1710 if (dl
->json_output
) {
1711 jsonw_uint_field(dl
->jw
, name
, val
);
1713 if (g_indent_newline
)
1714 pr_out("%s %u", name
, val
);
1716 pr_out(" %s %u", name
, val
);
1720 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1722 if (val
== (uint64_t) -1)
1723 return pr_out_str(dl
, name
, "unlimited");
1725 if (dl
->json_output
) {
1726 jsonw_u64_field(dl
->jw
, name
, val
);
1728 if (g_indent_newline
)
1729 pr_out("%s %lu", name
, val
);
1731 pr_out(" %s %lu", name
, val
);
1735 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1737 if (dl
->json_output
)
1738 jsonw_bool(dl
->jw
, value
);
1740 pr_out(" %s", value
? "true" : "false");
1743 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1745 if (dl
->json_output
)
1746 jsonw_uint(dl
->jw
, value
);
1748 pr_out(" %u", value
);
1751 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1753 if (dl
->json_output
)
1754 jsonw_u64(dl
->jw
, value
);
1756 pr_out(" %lu", value
);
1759 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1763 if (dl
->json_output
)
1764 jsonw_start_array(dl
->jw
);
1769 if (dl
->json_output
) {
1770 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1772 pr_out(" %02x", data
[i
]);
1778 if (dl
->json_output
)
1779 jsonw_end_array(dl
->jw
);
1780 else if ((i
- 1) % 16)
1784 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1786 if (dl
->json_output
)
1787 jsonw_string(dl
->jw
, value
);
1789 pr_out(" %s", value
);
1792 static void pr_out_name(struct dl
*dl
, const char *name
)
1794 if (dl
->json_output
)
1795 jsonw_name(dl
->jw
, name
);
1797 pr_out(" %s:", name
);
1800 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1802 if (dl
->json_output
) {
1803 jsonw_name(dl
->jw
, "address");
1804 jsonw_uint(dl
->jw
, addr
);
1805 jsonw_name(dl
->jw
, "data");
1806 jsonw_start_array(dl
->jw
);
1810 static void pr_out_region_chunk_end(struct dl
*dl
)
1812 if (dl
->json_output
)
1813 jsonw_end_array(dl
->jw
);
1816 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1819 static uint64_t align_val
;
1822 pr_out_region_chunk_start(dl
, addr
);
1824 if (!dl
->json_output
)
1825 if (!(align_val
% 16))
1826 pr_out("%s%016"PRIx64
" ",
1827 align_val
? "\n" : "",
1832 if (dl
->json_output
)
1833 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1835 pr_out("%02x ", data
[i
]);
1840 pr_out_region_chunk_end(dl
);
1843 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1845 pr_out_handle(dl
, tb
);
1848 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1850 if (dl
->json_output
) {
1851 jsonw_start_object(dl
->jw
);
1852 jsonw_name(dl
->jw
, name
);
1853 jsonw_start_object(dl
->jw
);
1857 static void pr_out_section_end(struct dl
*dl
)
1859 if (dl
->json_output
) {
1860 if (dl
->arr_last
.present
)
1861 jsonw_end_array(dl
->jw
);
1862 jsonw_end_object(dl
->jw
);
1863 jsonw_end_object(dl
->jw
);
1867 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1869 if (dl
->json_output
) {
1870 jsonw_name(dl
->jw
, name
);
1871 jsonw_start_array(dl
->jw
);
1873 __pr_out_indent_inc();
1875 pr_out("%s:", name
);
1876 __pr_out_indent_inc();
1881 static void pr_out_array_end(struct dl
*dl
)
1883 if (dl
->json_output
) {
1884 jsonw_end_array(dl
->jw
);
1886 __pr_out_indent_dec();
1887 __pr_out_indent_dec();
1891 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1893 if (dl
->json_output
) {
1894 jsonw_name(dl
->jw
, name
);
1895 jsonw_start_object(dl
->jw
);
1897 __pr_out_indent_inc();
1899 pr_out("%s:", name
);
1900 __pr_out_indent_inc();
1905 static void pr_out_object_end(struct dl
*dl
)
1907 if (dl
->json_output
) {
1908 jsonw_end_object(dl
->jw
);
1910 __pr_out_indent_dec();
1911 __pr_out_indent_dec();
1915 static void pr_out_entry_start(struct dl
*dl
)
1917 if (dl
->json_output
)
1918 jsonw_start_object(dl
->jw
);
1921 static void pr_out_entry_end(struct dl
*dl
)
1923 if (dl
->json_output
)
1924 jsonw_end_object(dl
->jw
);
1929 static const char *param_cmode_name(uint8_t cmode
)
1932 case DEVLINK_PARAM_CMODE_RUNTIME
:
1933 return PARAM_CMODE_RUNTIME_STR
;
1934 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1935 return PARAM_CMODE_DRIVERINIT_STR
;
1936 case DEVLINK_PARAM_CMODE_PERMANENT
:
1937 return PARAM_CMODE_PERMANENT_STR
;
1938 default: return "<unknown type>";
1942 static const char *eswitch_mode_name(uint32_t mode
)
1945 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1946 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1947 default: return "<unknown mode>";
1951 static const char *eswitch_inline_mode_name(uint32_t mode
)
1954 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1955 return ESWITCH_INLINE_MODE_NONE
;
1956 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1957 return ESWITCH_INLINE_MODE_LINK
;
1958 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1959 return ESWITCH_INLINE_MODE_NETWORK
;
1960 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1961 return ESWITCH_INLINE_MODE_TRANSPORT
;
1963 return "<unknown mode>";
1967 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1969 __pr_out_handle_start(dl
, tb
, true, false);
1971 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1972 pr_out_str(dl
, "mode",
1973 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1975 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1976 pr_out_str(dl
, "inline-mode",
1977 eswitch_inline_mode_name(mnl_attr_get_u8(
1978 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1980 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1981 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1983 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1986 pr_out_handle_end(dl
);
1989 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1991 struct dl
*dl
= data
;
1992 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1993 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1995 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1996 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1997 return MNL_CB_ERROR
;
1998 pr_out_eswitch(dl
, tb
);
2002 static int cmd_dev_eswitch_show(struct dl
*dl
)
2004 struct nlmsghdr
*nlh
;
2007 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2008 NLM_F_REQUEST
| NLM_F_ACK
);
2010 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2014 pr_out_section_start(dl
, "dev");
2015 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2016 pr_out_section_end(dl
);
2020 static int cmd_dev_eswitch_set(struct dl
*dl
)
2022 struct nlmsghdr
*nlh
;
2025 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2026 NLM_F_REQUEST
| NLM_F_ACK
);
2028 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2029 DL_OPT_ESWITCH_MODE
|
2030 DL_OPT_ESWITCH_INLINE_MODE
|
2031 DL_OPT_ESWITCH_ENCAP_MODE
);
2036 if (dl
->opts
.present
== 1) {
2037 pr_err("Need to set at least one option\n");
2041 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2044 static int cmd_dev_eswitch(struct dl
*dl
)
2046 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2049 } else if (dl_argv_match(dl
, "set")) {
2051 return cmd_dev_eswitch_set(dl
);
2052 } else if (dl_argv_match(dl
, "show")) {
2054 return cmd_dev_eswitch_show(dl
);
2056 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2060 struct param_val_conv
{
2066 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2067 uint32_t len
, const char *name
)
2071 for (i
= 0; i
< len
; i
++)
2072 if (!strcmp(param_val_conv
[i
].name
, name
))
2079 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2080 uint32_t len
, const char *name
, const char *vstr
,
2085 for (i
= 0; i
< len
; i
++)
2086 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2087 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2088 *vuint
= param_val_conv
[i
].vuint
;
2096 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2097 uint32_t len
, const char *name
, uint32_t vuint
,
2102 for (i
= 0; i
< len
; i
++)
2103 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2104 param_val_conv
[i
].vuint
== vuint
) {
2105 *vstr
= param_val_conv
[i
].vstr
;
2112 static const struct param_val_conv param_val_conv
[] = {
2114 .name
= "fw_load_policy",
2116 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2119 .name
= "fw_load_policy",
2121 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2125 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2127 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2128 int nla_type
, struct nlattr
*nl
)
2130 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2131 struct nlattr
*val_attr
;
2136 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2137 if (err
!= MNL_CB_OK
)
2140 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2141 (nla_type
!= MNL_TYPE_FLAG
&&
2142 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2145 pr_out_str(dl
, "cmode",
2146 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2147 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2149 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2155 err
= param_val_conv_str_get(param_val_conv
,
2158 mnl_attr_get_u8(val_attr
),
2162 pr_out_str(dl
, "value", vstr
);
2164 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2169 err
= param_val_conv_str_get(param_val_conv
,
2172 mnl_attr_get_u16(val_attr
),
2176 pr_out_str(dl
, "value", vstr
);
2178 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2183 err
= param_val_conv_str_get(param_val_conv
,
2186 mnl_attr_get_u32(val_attr
),
2190 pr_out_str(dl
, "value", vstr
);
2192 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2195 case MNL_TYPE_STRING
:
2196 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2199 pr_out_bool(dl
, "value", val_attr
? true : false);
2204 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2206 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2207 struct nlattr
*param_value_attr
;
2208 const char *nla_name
;
2212 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2213 if (err
!= MNL_CB_OK
)
2215 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2216 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2217 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2221 pr_out_handle_start_arr(dl
, tb
);
2223 __pr_out_handle_start(dl
, tb
, true, false);
2225 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2227 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2228 pr_out_str(dl
, "name", nla_name
);
2230 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2231 pr_out_str(dl
, "type", "driver-specific");
2233 pr_out_str(dl
, "type", "generic");
2235 pr_out_array_start(dl
, "values");
2236 mnl_attr_for_each_nested(param_value_attr
,
2237 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2238 pr_out_entry_start(dl
);
2239 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2240 pr_out_entry_end(dl
);
2242 pr_out_array_end(dl
);
2243 pr_out_handle_end(dl
);
2246 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2248 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2249 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2250 struct dl
*dl
= data
;
2252 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2253 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2254 !tb
[DEVLINK_ATTR_PARAM
])
2255 return MNL_CB_ERROR
;
2256 pr_out_param(dl
, tb
, true);
2272 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2274 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2275 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2276 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2277 struct nlattr
*param_value_attr
;
2278 enum devlink_param_cmode cmode
;
2279 struct param_ctx
*ctx
= data
;
2280 struct dl
*dl
= ctx
->dl
;
2284 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2285 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2286 !tb
[DEVLINK_ATTR_PARAM
])
2287 return MNL_CB_ERROR
;
2289 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2290 if (err
!= MNL_CB_OK
)
2291 return MNL_CB_ERROR
;
2293 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2294 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2295 return MNL_CB_ERROR
;
2297 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2298 mnl_attr_for_each_nested(param_value_attr
,
2299 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2300 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2301 struct nlattr
*val_attr
;
2303 err
= mnl_attr_parse_nested(param_value_attr
,
2304 attr_cb
, nla_value
);
2305 if (err
!= MNL_CB_OK
)
2306 return MNL_CB_ERROR
;
2308 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2309 (nla_type
!= MNL_TYPE_FLAG
&&
2310 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2311 return MNL_CB_ERROR
;
2313 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2314 if (cmode
== dl
->opts
.cmode
) {
2315 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2318 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2321 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2324 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2326 case MNL_TYPE_STRING
:
2327 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2330 ctx
->value
.vbool
= val_attr
? true : false;
2336 ctx
->nla_type
= nla_type
;
2340 static int cmd_dev_param_set(struct dl
*dl
)
2342 struct param_ctx ctx
= {};
2343 struct nlmsghdr
*nlh
;
2351 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2353 DL_OPT_PARAM_VALUE
|
2354 DL_OPT_PARAM_CMODE
, 0);
2358 /* Get value type */
2359 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2360 NLM_F_REQUEST
| NLM_F_ACK
);
2361 dl_opts_put(nlh
, dl
);
2364 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2368 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2369 NLM_F_REQUEST
| NLM_F_ACK
);
2370 dl_opts_put(nlh
, dl
);
2372 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2373 dl
->opts
.param_name
);
2375 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2376 switch (ctx
.nla_type
) {
2379 err
= param_val_conv_uint_get(param_val_conv
,
2381 dl
->opts
.param_name
,
2382 dl
->opts
.param_value
,
2386 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2389 goto err_param_value_parse
;
2390 if (val_u8
== ctx
.value
.vu8
)
2392 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2396 err
= param_val_conv_uint_get(param_val_conv
,
2398 dl
->opts
.param_name
,
2399 dl
->opts
.param_value
,
2403 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2406 goto err_param_value_parse
;
2407 if (val_u16
== ctx
.value
.vu16
)
2409 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2413 err
= param_val_conv_uint_get(param_val_conv
,
2415 dl
->opts
.param_name
,
2416 dl
->opts
.param_value
,
2419 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2421 goto err_param_value_parse
;
2422 if (val_u32
== ctx
.value
.vu32
)
2424 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2427 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2429 goto err_param_value_parse
;
2430 if (val_bool
== ctx
.value
.vbool
)
2433 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2436 case MNL_TYPE_STRING
:
2437 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2438 dl
->opts
.param_value
);
2439 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2443 printf("Value type not supported\n");
2446 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2448 err_param_value_parse
:
2449 pr_err("Value \"%s\" is not a number or not within range\n",
2450 dl
->opts
.param_value
);
2454 static int cmd_dev_param_show(struct dl
*dl
)
2456 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2457 struct nlmsghdr
*nlh
;
2460 if (dl_argc(dl
) == 0)
2461 flags
|= NLM_F_DUMP
;
2463 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2465 if (dl_argc(dl
) > 0) {
2466 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2467 DL_OPT_PARAM_NAME
, 0);
2472 pr_out_section_start(dl
, "param");
2473 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2474 pr_out_section_end(dl
);
2478 static int cmd_dev_param(struct dl
*dl
)
2480 if (dl_argv_match(dl
, "help")) {
2483 } else if (dl_argv_match(dl
, "show") ||
2484 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2486 return cmd_dev_param_show(dl
);
2487 } else if (dl_argv_match(dl
, "set")) {
2489 return cmd_dev_param_set(dl
);
2491 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2494 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2496 struct dl
*dl
= data
;
2497 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2498 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2500 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2501 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2502 return MNL_CB_ERROR
;
2507 static int cmd_dev_show(struct dl
*dl
)
2509 struct nlmsghdr
*nlh
;
2510 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2513 if (dl_argc(dl
) == 0)
2514 flags
|= NLM_F_DUMP
;
2516 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2518 if (dl_argc(dl
) > 0) {
2519 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2524 pr_out_section_start(dl
, "dev");
2525 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2526 pr_out_section_end(dl
);
2530 static void cmd_dev_reload_help(void)
2532 pr_err("Usage: devlink dev reload [ DEV ]\n");
2535 static int cmd_dev_reload(struct dl
*dl
)
2537 struct nlmsghdr
*nlh
;
2540 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2541 cmd_dev_reload_help();
2545 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2546 NLM_F_REQUEST
| NLM_F_ACK
);
2548 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2552 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2555 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2556 const char *name
, int type
)
2558 struct nlattr
*version
;
2560 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2561 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2562 const char *ver_value
;
2563 const char *ver_name
;
2566 if (mnl_attr_get_type(version
) != type
)
2569 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2570 if (err
!= MNL_CB_OK
)
2573 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2574 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2578 pr_out_object_start(dl
, name
);
2582 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2583 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2585 pr_out_str(dl
, ver_name
, ver_value
);
2586 if (!dl
->json_output
)
2591 pr_out_object_end(dl
);
2594 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2595 struct nlattr
**tb
, bool has_versions
)
2597 __pr_out_handle_start(dl
, tb
, true, false);
2599 __pr_out_indent_inc();
2600 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2601 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2603 if (!dl
->json_output
)
2605 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2608 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2609 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2611 if (!dl
->json_output
)
2613 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2615 __pr_out_indent_dec();
2618 pr_out_object_start(dl
, "versions");
2620 pr_out_versions_single(dl
, nlh
, "fixed",
2621 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2622 pr_out_versions_single(dl
, nlh
, "running",
2623 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2624 pr_out_versions_single(dl
, nlh
, "stored",
2625 DEVLINK_ATTR_INFO_VERSION_STORED
);
2627 pr_out_object_end(dl
);
2630 pr_out_handle_end(dl
);
2633 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2635 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2636 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2637 bool has_versions
, has_info
;
2638 struct dl
*dl
= data
;
2640 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2642 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2643 return MNL_CB_ERROR
;
2645 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2646 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2647 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2648 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2649 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2653 pr_out_info(dl
, nlh
, tb
, has_versions
);
2658 static void cmd_dev_info_help(void)
2660 pr_err("Usage: devlink dev info [ DEV ]\n");
2663 static int cmd_dev_info(struct dl
*dl
)
2665 struct nlmsghdr
*nlh
;
2666 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2669 if (dl_argv_match(dl
, "help")) {
2670 cmd_dev_info_help();
2674 if (dl_argc(dl
) == 0)
2675 flags
|= NLM_F_DUMP
;
2677 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2679 if (dl_argc(dl
) > 0) {
2680 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2685 pr_out_section_start(dl
, "info");
2686 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2687 pr_out_section_end(dl
);
2691 static void cmd_dev_flash_help(void)
2693 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2696 static int cmd_dev_flash(struct dl
*dl
)
2698 struct nlmsghdr
*nlh
;
2701 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2702 cmd_dev_flash_help();
2706 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2707 NLM_F_REQUEST
| NLM_F_ACK
);
2709 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2710 DL_OPT_FLASH_COMPONENT
);
2714 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2717 static int cmd_dev(struct dl
*dl
)
2719 if (dl_argv_match(dl
, "help")) {
2722 } else if (dl_argv_match(dl
, "show") ||
2723 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2725 return cmd_dev_show(dl
);
2726 } else if (dl_argv_match(dl
, "eswitch")) {
2728 return cmd_dev_eswitch(dl
);
2729 } else if (dl_argv_match(dl
, "reload")) {
2731 return cmd_dev_reload(dl
);
2732 } else if (dl_argv_match(dl
, "param")) {
2734 return cmd_dev_param(dl
);
2735 } else if (dl_argv_match(dl
, "info")) {
2737 return cmd_dev_info(dl
);
2738 } else if (dl_argv_match(dl
, "flash")) {
2740 return cmd_dev_flash(dl
);
2742 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2746 static void cmd_port_help(void)
2748 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2749 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2750 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2751 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2754 static const char *port_type_name(uint32_t type
)
2757 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2758 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2759 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2760 case DEVLINK_PORT_TYPE_IB
: return "ib";
2761 default: return "<unknown type>";
2765 static const char *port_flavour_name(uint16_t flavour
)
2768 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2770 case DEVLINK_PORT_FLAVOUR_CPU
:
2772 case DEVLINK_PORT_FLAVOUR_DSA
:
2775 return "<unknown flavour>";
2779 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2781 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2782 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2784 pr_out_port_handle_start(dl
, tb
, false);
2786 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2788 pr_out_str(dl
, "type", port_type_name(port_type
));
2790 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2792 if (port_type
!= des_port_type
)
2793 pr_out_str(dl
, "des_type",
2794 port_type_name(des_port_type
));
2797 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2798 pr_out_str(dl
, "netdev",
2799 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2800 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2801 pr_out_str(dl
, "ibdev",
2802 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2803 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2804 uint16_t port_flavour
=
2805 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2807 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2809 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2810 pr_out_uint(dl
, "split_group",
2811 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2812 pr_out_port_handle_end(dl
);
2815 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2817 struct dl
*dl
= data
;
2818 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2819 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2821 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2822 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2823 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2824 return MNL_CB_ERROR
;
2825 pr_out_port(dl
, tb
);
2829 static int cmd_port_show(struct dl
*dl
)
2831 struct nlmsghdr
*nlh
;
2832 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2835 if (dl_argc(dl
) == 0)
2836 flags
|= NLM_F_DUMP
;
2838 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2840 if (dl_argc(dl
) > 0) {
2841 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2846 pr_out_section_start(dl
, "port");
2847 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2848 pr_out_section_end(dl
);
2852 static int cmd_port_set(struct dl
*dl
)
2854 struct nlmsghdr
*nlh
;
2857 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2858 NLM_F_REQUEST
| NLM_F_ACK
);
2860 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2864 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2867 static int cmd_port_split(struct dl
*dl
)
2869 struct nlmsghdr
*nlh
;
2872 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2873 NLM_F_REQUEST
| NLM_F_ACK
);
2875 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2879 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2882 static int cmd_port_unsplit(struct dl
*dl
)
2884 struct nlmsghdr
*nlh
;
2887 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2888 NLM_F_REQUEST
| NLM_F_ACK
);
2890 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2894 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2897 static int cmd_port(struct dl
*dl
)
2899 if (dl_argv_match(dl
, "help")) {
2902 } else if (dl_argv_match(dl
, "show") ||
2903 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2905 return cmd_port_show(dl
);
2906 } else if (dl_argv_match(dl
, "set")) {
2908 return cmd_port_set(dl
);
2909 } else if (dl_argv_match(dl
, "split")) {
2911 return cmd_port_split(dl
);
2912 } else if (dl_argv_match(dl
, "unsplit")) {
2914 return cmd_port_unsplit(dl
);
2916 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2920 static void cmd_sb_help(void)
2922 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2923 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2924 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2925 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2926 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2927 pr_err(" pool POOL_INDEX ]\n");
2928 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2929 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2930 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2931 pr_err(" type { ingress | egress } ]\n");
2932 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2933 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2934 pr_err(" th THRESHOLD\n");
2935 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2936 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2937 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2940 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2942 pr_out_handle_start_arr(dl
, tb
);
2943 pr_out_uint(dl
, "sb",
2944 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2945 pr_out_uint(dl
, "size",
2946 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2947 pr_out_uint(dl
, "ing_pools",
2948 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2949 pr_out_uint(dl
, "eg_pools",
2950 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2951 pr_out_uint(dl
, "ing_tcs",
2952 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2953 pr_out_uint(dl
, "eg_tcs",
2954 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2955 pr_out_handle_end(dl
);
2958 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2960 struct dl
*dl
= data
;
2961 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2962 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2964 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2965 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2966 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2967 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2968 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2969 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2970 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2971 return MNL_CB_ERROR
;
2976 static int cmd_sb_show(struct dl
*dl
)
2978 struct nlmsghdr
*nlh
;
2979 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2982 if (dl_argc(dl
) == 0)
2983 flags
|= NLM_F_DUMP
;
2985 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2987 if (dl_argc(dl
) > 0) {
2988 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2993 pr_out_section_start(dl
, "sb");
2994 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2995 pr_out_section_end(dl
);
2999 static const char *pool_type_name(uint8_t type
)
3002 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3003 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3004 default: return "<unknown type>";
3008 static const char *threshold_type_name(uint8_t type
)
3011 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3012 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3013 default: return "<unknown type>";
3017 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3019 pr_out_handle_start_arr(dl
, tb
);
3020 pr_out_uint(dl
, "sb",
3021 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3022 pr_out_uint(dl
, "pool",
3023 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3024 pr_out_str(dl
, "type",
3025 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3026 pr_out_uint(dl
, "size",
3027 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3028 pr_out_str(dl
, "thtype",
3029 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3030 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3031 pr_out_uint(dl
, "cell_size",
3032 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3033 pr_out_handle_end(dl
);
3036 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3038 struct dl
*dl
= data
;
3039 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3040 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3042 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3043 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3044 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3045 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3046 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3047 return MNL_CB_ERROR
;
3048 pr_out_sb_pool(dl
, tb
);
3052 static int cmd_sb_pool_show(struct dl
*dl
)
3054 struct nlmsghdr
*nlh
;
3055 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3058 if (dl_argc(dl
) == 0)
3059 flags
|= NLM_F_DUMP
;
3061 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3063 if (dl_argc(dl
) > 0) {
3064 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3070 pr_out_section_start(dl
, "pool");
3071 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3072 pr_out_section_end(dl
);
3076 static int cmd_sb_pool_set(struct dl
*dl
)
3078 struct nlmsghdr
*nlh
;
3081 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3082 NLM_F_REQUEST
| NLM_F_ACK
);
3084 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3085 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3089 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3092 static int cmd_sb_pool(struct dl
*dl
)
3094 if (dl_argv_match(dl
, "help")) {
3097 } else if (dl_argv_match(dl
, "show") ||
3098 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3100 return cmd_sb_pool_show(dl
);
3101 } else if (dl_argv_match(dl
, "set")) {
3103 return cmd_sb_pool_set(dl
);
3105 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3109 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3111 pr_out_port_handle_start_arr(dl
, tb
, true);
3112 pr_out_uint(dl
, "sb",
3113 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3114 pr_out_uint(dl
, "pool",
3115 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3116 pr_out_uint(dl
, "threshold",
3117 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3118 pr_out_port_handle_end(dl
);
3121 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3123 struct dl
*dl
= data
;
3124 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3125 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3127 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3128 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3129 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3130 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3131 return MNL_CB_ERROR
;
3132 pr_out_sb_port_pool(dl
, tb
);
3136 static int cmd_sb_port_pool_show(struct dl
*dl
)
3138 struct nlmsghdr
*nlh
;
3139 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3142 if (dl_argc(dl
) == 0)
3143 flags
|= NLM_F_DUMP
;
3145 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3147 if (dl_argc(dl
) > 0) {
3148 err
= dl_argv_parse_put(nlh
, dl
,
3149 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3155 pr_out_section_start(dl
, "port_pool");
3156 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3157 pr_out_section_end(dl
);
3161 static int cmd_sb_port_pool_set(struct dl
*dl
)
3163 struct nlmsghdr
*nlh
;
3166 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3167 NLM_F_REQUEST
| NLM_F_ACK
);
3169 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3170 DL_OPT_SB_TH
, DL_OPT_SB
);
3174 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3177 static int cmd_sb_port_pool(struct dl
*dl
)
3179 if (dl_argv_match(dl
, "help")) {
3182 } else if (dl_argv_match(dl
, "show") ||
3183 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3185 return cmd_sb_port_pool_show(dl
);
3186 } else if (dl_argv_match(dl
, "set")) {
3188 return cmd_sb_port_pool_set(dl
);
3190 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3194 static int cmd_sb_port(struct dl
*dl
)
3196 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3199 } else if (dl_argv_match(dl
, "pool")) {
3201 return cmd_sb_port_pool(dl
);
3203 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3207 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3209 pr_out_port_handle_start_arr(dl
, tb
, true);
3210 pr_out_uint(dl
, "sb",
3211 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3212 pr_out_uint(dl
, "tc",
3213 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3214 pr_out_str(dl
, "type",
3215 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3216 pr_out_uint(dl
, "pool",
3217 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3218 pr_out_uint(dl
, "threshold",
3219 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3220 pr_out_port_handle_end(dl
);
3223 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3225 struct dl
*dl
= data
;
3226 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3227 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3229 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3230 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3231 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3232 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3233 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3234 return MNL_CB_ERROR
;
3235 pr_out_sb_tc_bind(dl
, tb
);
3239 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3241 struct nlmsghdr
*nlh
;
3242 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3245 if (dl_argc(dl
) == 0)
3246 flags
|= NLM_F_DUMP
;
3248 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3250 if (dl_argc(dl
) > 0) {
3251 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3252 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3257 pr_out_section_start(dl
, "tc_bind");
3258 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3259 pr_out_section_end(dl
);
3263 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3265 struct nlmsghdr
*nlh
;
3268 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3269 NLM_F_REQUEST
| NLM_F_ACK
);
3271 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3272 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3277 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3280 static int cmd_sb_tc_bind(struct dl
*dl
)
3282 if (dl_argv_match(dl
, "help")) {
3285 } else if (dl_argv_match(dl
, "show") ||
3286 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3288 return cmd_sb_tc_bind_show(dl
);
3289 } else if (dl_argv_match(dl
, "set")) {
3291 return cmd_sb_tc_bind_set(dl
);
3293 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3297 static int cmd_sb_tc(struct dl
*dl
)
3299 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3302 } else if (dl_argv_match(dl
, "bind")) {
3304 return cmd_sb_tc_bind(dl
);
3306 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3311 struct list_head list
;
3315 uint32_t bound_pool_index
;
3319 struct list_head list
;
3322 uint32_t port_index
;
3324 struct list_head pool_list
;
3325 struct list_head ing_tc_list
;
3326 struct list_head eg_tc_list
;
3332 struct list_head port_list
;
3335 static struct occ_item
*occ_item_alloc(void)
3337 return calloc(1, sizeof(struct occ_item
));
3340 static void occ_item_free(struct occ_item
*occ_item
)
3345 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3347 struct occ_port
*occ_port
;
3349 occ_port
= calloc(1, sizeof(*occ_port
));
3352 occ_port
->port_index
= port_index
;
3353 INIT_LIST_HEAD(&occ_port
->pool_list
);
3354 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3355 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3359 static void occ_port_free(struct occ_port
*occ_port
)
3361 struct occ_item
*occ_item
, *tmp
;
3363 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3364 occ_item_free(occ_item
);
3365 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3366 occ_item_free(occ_item
);
3367 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3368 occ_item_free(occ_item
);
3371 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3373 struct occ_show
*occ_show
;
3375 occ_show
= calloc(1, sizeof(*occ_show
));
3379 INIT_LIST_HEAD(&occ_show
->port_list
);
3383 static void occ_show_free(struct occ_show
*occ_show
)
3385 struct occ_port
*occ_port
, *tmp
;
3387 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3388 occ_port_free(occ_port
);
3391 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3394 struct occ_port
*occ_port
;
3395 uint32_t port_index
;
3397 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3399 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3400 if (occ_port
->port_index
== port_index
)
3403 occ_port
= occ_port_alloc(port_index
);
3406 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3410 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3413 struct occ_item
*occ_item
;
3416 pr_out_sp(7, " %s:", label
);
3417 list_for_each_entry(occ_item
, list
, list
) {
3418 if ((i
- 1) % 4 == 0 && i
!= 1)
3421 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3422 occ_item
->bound_pool_index
);
3424 pr_out_sp(7, "%2u:", occ_item
->index
);
3425 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3429 if ((i
- 1) % 4 != 0)
3433 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3434 struct list_head
*list
,
3437 struct occ_item
*occ_item
;
3440 jsonw_name(dl
->jw
, label
);
3441 jsonw_start_object(dl
->jw
);
3442 list_for_each_entry(occ_item
, list
, list
) {
3443 sprintf(buf
, "%u", occ_item
->index
);
3444 jsonw_name(dl
->jw
, buf
);
3445 jsonw_start_object(dl
->jw
);
3447 jsonw_uint_field(dl
->jw
, "bound_pool",
3448 occ_item
->bound_pool_index
);
3449 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3450 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3451 jsonw_end_object(dl
->jw
);
3453 jsonw_end_object(dl
->jw
);
3456 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3458 if (dl
->json_output
) {
3459 pr_out_json_occ_show_item_list(dl
, "pool",
3460 &occ_port
->pool_list
, false);
3461 pr_out_json_occ_show_item_list(dl
, "itc",
3462 &occ_port
->ing_tc_list
, true);
3463 pr_out_json_occ_show_item_list(dl
, "etc",
3464 &occ_port
->eg_tc_list
, true);
3467 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3468 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3469 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3473 static void pr_out_occ_show(struct occ_show
*occ_show
)
3475 struct dl
*dl
= occ_show
->dl
;
3476 struct dl_opts
*opts
= &dl
->opts
;
3477 struct occ_port
*occ_port
;
3479 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3480 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3481 occ_port
->port_index
, true, false);
3482 pr_out_occ_show_port(dl
, occ_port
);
3483 pr_out_port_handle_end(dl
);
3487 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3490 struct occ_port
*occ_port
;
3491 struct occ_item
*occ_item
;
3493 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3496 occ_port
= occ_port_get(occ_show
, tb
);
3498 occ_show
->err
= -ENOMEM
;
3502 occ_item
= occ_item_alloc();
3504 occ_show
->err
= -ENOMEM
;
3507 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3508 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3509 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3510 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3513 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3515 struct occ_show
*occ_show
= data
;
3516 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3517 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3519 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3520 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3521 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3522 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3523 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3524 return MNL_CB_ERROR
;
3525 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3529 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3532 struct occ_port
*occ_port
;
3533 struct occ_item
*occ_item
;
3536 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3539 occ_port
= occ_port_get(occ_show
, tb
);
3541 occ_show
->err
= -ENOMEM
;
3545 occ_item
= occ_item_alloc();
3547 occ_show
->err
= -ENOMEM
;
3550 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3551 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3552 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3553 occ_item
->bound_pool_index
=
3554 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3555 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3556 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3557 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3558 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3559 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3561 occ_item_free(occ_item
);
3564 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3566 struct occ_show
*occ_show
= data
;
3567 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3568 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3570 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3571 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3572 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3573 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3574 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3575 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3576 return MNL_CB_ERROR
;
3577 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3581 static int cmd_sb_occ_show(struct dl
*dl
)
3583 struct nlmsghdr
*nlh
;
3584 struct occ_show
*occ_show
;
3585 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3588 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3592 occ_show
= occ_show_alloc(dl
);
3596 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3598 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3599 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3603 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3605 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3606 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3610 pr_out_section_start(dl
, "occupancy");
3611 pr_out_occ_show(occ_show
);
3612 pr_out_section_end(dl
);
3615 occ_show_free(occ_show
);
3619 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3621 struct nlmsghdr
*nlh
;
3624 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3625 NLM_F_REQUEST
| NLM_F_ACK
);
3627 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3631 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3634 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3636 struct nlmsghdr
*nlh
;
3639 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3640 NLM_F_REQUEST
| NLM_F_ACK
);
3642 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3646 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3649 static int cmd_sb_occ(struct dl
*dl
)
3651 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3654 } else if (dl_argv_match(dl
, "show") ||
3655 dl_argv_match(dl
, "list")) {
3657 return cmd_sb_occ_show(dl
);
3658 } else if (dl_argv_match(dl
, "snapshot")) {
3660 return cmd_sb_occ_snapshot(dl
);
3661 } else if (dl_argv_match(dl
, "clearmax")) {
3663 return cmd_sb_occ_clearmax(dl
);
3665 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3669 static int cmd_sb(struct dl
*dl
)
3671 if (dl_argv_match(dl
, "help")) {
3674 } else if (dl_argv_match(dl
, "show") ||
3675 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3677 return cmd_sb_show(dl
);
3678 } else if (dl_argv_match(dl
, "pool")) {
3680 return cmd_sb_pool(dl
);
3681 } else if (dl_argv_match(dl
, "port")) {
3683 return cmd_sb_port(dl
);
3684 } else if (dl_argv_match(dl
, "tc")) {
3686 return cmd_sb_tc(dl
);
3687 } else if (dl_argv_match(dl
, "occupancy")) {
3689 return cmd_sb_occ(dl
);
3691 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3695 static const char *cmd_name(uint8_t cmd
)
3698 case DEVLINK_CMD_UNSPEC
: return "unspec";
3699 case DEVLINK_CMD_GET
: return "get";
3700 case DEVLINK_CMD_SET
: return "set";
3701 case DEVLINK_CMD_NEW
: return "new";
3702 case DEVLINK_CMD_DEL
: return "del";
3703 case DEVLINK_CMD_PORT_GET
: return "get";
3704 case DEVLINK_CMD_PORT_SET
: return "set";
3705 case DEVLINK_CMD_PORT_NEW
: return "new";
3706 case DEVLINK_CMD_PORT_DEL
: return "del";
3707 case DEVLINK_CMD_PARAM_GET
: return "get";
3708 case DEVLINK_CMD_PARAM_SET
: return "set";
3709 case DEVLINK_CMD_PARAM_NEW
: return "new";
3710 case DEVLINK_CMD_PARAM_DEL
: return "del";
3711 case DEVLINK_CMD_REGION_GET
: return "get";
3712 case DEVLINK_CMD_REGION_SET
: return "set";
3713 case DEVLINK_CMD_REGION_NEW
: return "new";
3714 case DEVLINK_CMD_REGION_DEL
: return "del";
3715 default: return "<unknown cmd>";
3719 static const char *cmd_obj(uint8_t cmd
)
3722 case DEVLINK_CMD_UNSPEC
: return "unspec";
3723 case DEVLINK_CMD_GET
:
3724 case DEVLINK_CMD_SET
:
3725 case DEVLINK_CMD_NEW
:
3726 case DEVLINK_CMD_DEL
:
3728 case DEVLINK_CMD_PORT_GET
:
3729 case DEVLINK_CMD_PORT_SET
:
3730 case DEVLINK_CMD_PORT_NEW
:
3731 case DEVLINK_CMD_PORT_DEL
:
3733 case DEVLINK_CMD_PARAM_GET
:
3734 case DEVLINK_CMD_PARAM_SET
:
3735 case DEVLINK_CMD_PARAM_NEW
:
3736 case DEVLINK_CMD_PARAM_DEL
:
3738 case DEVLINK_CMD_REGION_GET
:
3739 case DEVLINK_CMD_REGION_SET
:
3740 case DEVLINK_CMD_REGION_NEW
:
3741 case DEVLINK_CMD_REGION_DEL
:
3743 default: return "<unknown obj>";
3747 static void pr_out_mon_header(uint8_t cmd
)
3749 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3752 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3754 const char *obj
= cmd_obj(cmd
);
3755 unsigned int index
= 0;
3756 const char *cur_obj
;
3760 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3761 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3767 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3769 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3771 struct dl
*dl
= data
;
3772 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3773 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3774 uint8_t cmd
= genl
->cmd
;
3776 if (!cmd_filter_check(dl
, cmd
))
3780 case DEVLINK_CMD_GET
: /* fall through */
3781 case DEVLINK_CMD_SET
: /* fall through */
3782 case DEVLINK_CMD_NEW
: /* fall through */
3783 case DEVLINK_CMD_DEL
:
3784 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3785 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3786 return MNL_CB_ERROR
;
3787 pr_out_mon_header(genl
->cmd
);
3790 case DEVLINK_CMD_PORT_GET
: /* fall through */
3791 case DEVLINK_CMD_PORT_SET
: /* fall through */
3792 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3793 case DEVLINK_CMD_PORT_DEL
:
3794 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3795 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3796 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3797 return MNL_CB_ERROR
;
3798 pr_out_mon_header(genl
->cmd
);
3799 pr_out_port(dl
, tb
);
3801 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3802 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3803 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3804 case DEVLINK_CMD_PARAM_DEL
:
3805 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3806 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3807 !tb
[DEVLINK_ATTR_PARAM
])
3808 return MNL_CB_ERROR
;
3809 pr_out_mon_header(genl
->cmd
);
3810 pr_out_param(dl
, tb
, false);
3812 case DEVLINK_CMD_REGION_GET
: /* fall through */
3813 case DEVLINK_CMD_REGION_SET
: /* fall through */
3814 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3815 case DEVLINK_CMD_REGION_DEL
:
3816 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3817 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3818 !tb
[DEVLINK_ATTR_REGION_NAME
])
3819 return MNL_CB_ERROR
;
3820 pr_out_mon_header(genl
->cmd
);
3821 pr_out_region(dl
, tb
);
3827 static int cmd_mon_show(struct dl
*dl
)
3830 unsigned int index
= 0;
3831 const char *cur_obj
;
3833 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3834 if (strcmp(cur_obj
, "all") != 0 &&
3835 strcmp(cur_obj
, "dev") != 0 &&
3836 strcmp(cur_obj
, "port") != 0) {
3837 pr_err("Unknown object \"%s\"\n", cur_obj
);
3841 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3844 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3850 static void cmd_mon_help(void)
3852 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3853 "where OBJECT-LIST := { dev | port }\n");
3856 static int cmd_mon(struct dl
*dl
)
3858 if (dl_argv_match(dl
, "help")) {
3862 return cmd_mon_show(dl
);
3865 struct dpipe_field
{
3868 unsigned int bitwidth
;
3869 enum devlink_dpipe_field_mapping_type mapping_type
;
3872 struct dpipe_header
{
3873 struct list_head list
;
3876 struct dpipe_field
*fields
;
3877 unsigned int fields_count
;
3880 struct dpipe_table
{
3881 struct list_head list
;
3883 unsigned int resource_id
;
3884 bool resource_valid
;
3887 struct dpipe_tables
{
3888 struct list_head table_list
;
3898 enum devlink_resource_unit unit
;
3903 struct list_head list
;
3904 struct list_head resource_list
;
3905 struct resource
*parent
;
3909 struct list_head resource_list
;
3912 struct resource_ctx
{
3915 struct resources
*resources
;
3916 struct dpipe_tables
*tables
;
3917 bool print_resources
;
3918 bool pending_change
;
3921 static struct resource
*resource_alloc(void)
3923 struct resource
*resource
;
3925 resource
= calloc(1, sizeof(struct resource
));
3928 INIT_LIST_HEAD(&resource
->resource_list
);
3932 static void resource_free(struct resource
*resource
)
3934 struct resource
*child_resource
, *tmp
;
3936 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3938 free(child_resource
->name
);
3939 resource_free(child_resource
);
3944 static struct resources
*resources_alloc(void)
3946 struct resources
*resources
;
3948 resources
= calloc(1, sizeof(struct resources
));
3951 INIT_LIST_HEAD(&resources
->resource_list
);
3955 static void resources_free(struct resources
*resources
)
3957 struct resource
*resource
, *tmp
;
3959 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3960 resource_free(resource
);
3963 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3965 ctx
->resources
= resources_alloc();
3966 if (!ctx
->resources
)
3972 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3974 resources_free(ctx
->resources
);
3980 struct list_head global_headers
;
3981 struct list_head local_headers
;
3982 struct dpipe_tables
*tables
;
3983 struct resources
*resources
;
3988 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3990 struct dpipe_header
*header
;
3992 header
= calloc(1, sizeof(struct dpipe_header
));
3995 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3996 if (!header
->fields
)
3997 goto err_fields_alloc
;
3998 header
->fields_count
= fields_count
;
4006 static void dpipe_header_free(struct dpipe_header
*header
)
4008 free(header
->fields
);
4012 static void dpipe_header_clear(struct dpipe_header
*header
)
4014 struct dpipe_field
*field
;
4017 for (i
= 0; i
< header
->fields_count
; i
++) {
4018 field
= &header
->fields
[i
];
4024 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4025 struct dpipe_header
*header
, bool global
)
4028 list_add(&header
->list
, &ctx
->global_headers
);
4030 list_add(&header
->list
, &ctx
->local_headers
);
4033 static void dpipe_header_del(struct dpipe_header
*header
)
4035 list_del(&header
->list
);
4038 static struct dpipe_table
*dpipe_table_alloc(void)
4040 return calloc(1, sizeof(struct dpipe_table
));
4043 static void dpipe_table_free(struct dpipe_table
*table
)
4048 static struct dpipe_tables
*dpipe_tables_alloc(void)
4050 struct dpipe_tables
*tables
;
4052 tables
= calloc(1, sizeof(struct dpipe_tables
));
4055 INIT_LIST_HEAD(&tables
->table_list
);
4059 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4061 struct dpipe_table
*table
, *tmp
;
4063 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4064 dpipe_table_free(table
);
4068 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4070 ctx
->tables
= dpipe_tables_alloc();
4075 INIT_LIST_HEAD(&ctx
->global_headers
);
4076 INIT_LIST_HEAD(&ctx
->local_headers
);
4080 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4082 struct dpipe_header
*header
, *tmp
;
4084 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4086 dpipe_header_del(header
);
4087 dpipe_header_clear(header
);
4088 dpipe_header_free(header
);
4090 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4092 dpipe_header_del(header
);
4093 dpipe_header_clear(header
);
4094 dpipe_header_free(header
);
4096 dpipe_tables_free(ctx
->tables
);
4099 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4100 uint32_t header_id
, bool global
)
4102 struct list_head
*header_list
;
4103 struct dpipe_header
*header
;
4106 header_list
= &ctx
->global_headers
;
4108 header_list
= &ctx
->local_headers
;
4109 list_for_each_entry(header
, header_list
, list
) {
4110 if (header
->id
!= header_id
)
4112 return header
->name
;
4117 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4119 uint32_t field_id
, bool global
)
4121 struct list_head
*header_list
;
4122 struct dpipe_header
*header
;
4125 header_list
= &ctx
->global_headers
;
4127 header_list
= &ctx
->local_headers
;
4128 list_for_each_entry(header
, header_list
, list
) {
4129 if (header
->id
!= header_id
)
4131 return header
->fields
[field_id
].name
;
4137 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4139 switch (mapping_type
) {
4140 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4142 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4150 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4151 uint32_t field_id
, bool global
)
4153 enum devlink_dpipe_field_mapping_type mapping_type
;
4154 struct list_head
*header_list
;
4155 struct dpipe_header
*header
;
4158 header_list
= &ctx
->global_headers
;
4160 header_list
= &ctx
->local_headers
;
4161 list_for_each_entry(header
, header_list
, list
) {
4162 if (header
->id
!= header_id
)
4164 mapping_type
= header
->fields
[field_id
].mapping_type
;
4165 return dpipe_field_mapping_e2s(mapping_type
);
4170 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4171 struct dpipe_field
*fields
,
4172 unsigned int field_count
)
4174 struct dpipe_field
*field
;
4177 for (i
= 0; i
< field_count
; i
++) {
4179 pr_out_entry_start(ctx
->dl
);
4180 pr_out_str(ctx
->dl
, "name", field
->name
);
4181 if (ctx
->dl
->verbose
)
4182 pr_out_uint(ctx
->dl
, "id", field
->id
);
4183 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4184 if (field
->mapping_type
)
4185 pr_out_str(ctx
->dl
, "mapping_type",
4186 dpipe_field_mapping_e2s(field
->mapping_type
));
4187 pr_out_entry_end(ctx
->dl
);
4192 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4193 struct dpipe_header
*header
, bool global
)
4195 pr_out_handle_start_arr(ctx
->dl
, tb
);
4196 pr_out_str(ctx
->dl
, "name", header
->name
);
4197 if (ctx
->dl
->verbose
) {
4198 pr_out_uint(ctx
->dl
, "id", header
->id
);
4199 pr_out_str(ctx
->dl
, "global",
4200 global
? "true" : "false");
4202 pr_out_array_start(ctx
->dl
, "field");
4203 pr_out_dpipe_fields(ctx
, header
->fields
,
4204 header
->fields_count
);
4205 pr_out_array_end(ctx
->dl
);
4206 pr_out_handle_end(ctx
->dl
);
4209 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4212 struct dpipe_header
*header
;
4214 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4215 pr_out_dpipe_header(ctx
, tb
, header
, false);
4217 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4218 pr_out_dpipe_header(ctx
, tb
, header
, true);
4221 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4223 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4227 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4228 if (err
!= MNL_CB_OK
)
4230 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4231 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4232 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4233 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4236 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4237 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4238 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4239 field
->name
= strdup(name
);
4242 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4246 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4247 struct dpipe_field
*fields
)
4249 struct nlattr
*nla_field
;
4253 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4254 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4262 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4264 struct nlattr
*nla_field
;
4265 unsigned int count
= 0;
4267 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4272 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4274 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4275 struct dpipe_header
*header
;
4276 unsigned int fields_count
;
4277 const char *header_name
;
4281 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4282 if (err
!= MNL_CB_OK
)
4285 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4286 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4287 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4290 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4291 header
= dpipe_header_alloc(fields_count
);
4295 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4296 header
->name
= strdup(header_name
);
4297 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4298 header
->fields_count
= fields_count
;
4299 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4301 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4305 dpipe_header_add(ctx
, header
, global
);
4309 dpipe_header_free(header
);
4313 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4315 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4316 struct nlattr
*nla_header
;
4319 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4320 err
= dpipe_header_get(ctx
, nla_header
);
4327 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4329 struct dpipe_ctx
*ctx
= data
;
4330 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4331 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4334 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4335 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4336 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4337 return MNL_CB_ERROR
;
4338 err
= dpipe_headers_get(ctx
, tb
);
4341 return MNL_CB_ERROR
;
4344 if (ctx
->print_headers
)
4345 pr_out_dpipe_headers(ctx
, tb
);
4349 static int cmd_dpipe_headers_show(struct dl
*dl
)
4351 struct nlmsghdr
*nlh
;
4352 struct dpipe_ctx ctx
= {};
4353 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4356 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4358 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4362 err
= dpipe_ctx_init(&ctx
, dl
);
4366 ctx
.print_headers
= true;
4368 pr_out_section_start(dl
, "header");
4369 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4371 pr_err("error get headers %s\n", strerror(ctx
.err
));
4372 pr_out_section_end(dl
);
4374 dpipe_ctx_fini(&ctx
);
4378 static void cmd_dpipe_header_help(void)
4380 pr_err("Usage: devlink dpipe headers show DEV\n");
4383 static int cmd_dpipe_header(struct dl
*dl
)
4385 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4386 cmd_dpipe_header_help();
4388 } else if (dl_argv_match(dl
, "show")) {
4390 return cmd_dpipe_headers_show(dl
);
4392 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4397 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4399 switch (action_type
) {
4400 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4401 return "field_modify";
4407 struct dpipe_op_info
{
4413 struct dpipe_action
{
4414 struct dpipe_op_info info
;
4418 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4419 struct dpipe_ctx
*ctx
)
4421 struct dpipe_op_info
*op_info
= &action
->info
;
4422 const char *mapping
;
4424 pr_out_str(ctx
->dl
, "type",
4425 dpipe_action_type_e2s(action
->type
));
4426 pr_out_str(ctx
->dl
, "header",
4427 dpipe_header_id2s(ctx
, op_info
->header_id
,
4428 op_info
->header_global
));
4429 pr_out_str(ctx
->dl
, "field",
4430 dpipe_field_id2s(ctx
, op_info
->header_id
,
4432 op_info
->header_global
));
4433 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4435 op_info
->header_global
);
4437 pr_out_str(ctx
->dl
, "mapping", mapping
);
4440 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4442 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4445 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4446 if (err
!= MNL_CB_OK
)
4449 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4450 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4451 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4452 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4456 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4457 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4458 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4459 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4464 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4465 struct nlattr
*nla_actions
)
4467 struct nlattr
*nla_action
;
4468 struct dpipe_action action
;
4470 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4471 pr_out_entry_start(ctx
->dl
);
4472 if (dpipe_action_parse(&action
, nla_action
))
4473 goto err_action_parse
;
4474 pr_out_dpipe_action(&action
, ctx
);
4475 pr_out_entry_end(ctx
->dl
);
4480 pr_out_entry_end(ctx
->dl
);
4485 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4487 switch (match_type
) {
4488 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4489 return "field_exact";
4495 struct dpipe_match
{
4496 struct dpipe_op_info info
;
4500 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4501 struct dpipe_ctx
*ctx
)
4503 struct dpipe_op_info
*op_info
= &match
->info
;
4504 const char *mapping
;
4506 pr_out_str(ctx
->dl
, "type",
4507 dpipe_match_type_e2s(match
->type
));
4508 pr_out_str(ctx
->dl
, "header",
4509 dpipe_header_id2s(ctx
, op_info
->header_id
,
4510 op_info
->header_global
));
4511 pr_out_str(ctx
->dl
, "field",
4512 dpipe_field_id2s(ctx
, op_info
->header_id
,
4514 op_info
->header_global
));
4515 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4517 op_info
->header_global
);
4519 pr_out_str(ctx
->dl
, "mapping", mapping
);
4522 static int dpipe_match_parse(struct dpipe_match
*match
,
4526 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4529 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4530 if (err
!= MNL_CB_OK
)
4533 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4534 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4535 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4536 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4540 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4541 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4542 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4543 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4548 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4549 struct nlattr
*nla_matches
)
4551 struct nlattr
*nla_match
;
4552 struct dpipe_match match
;
4554 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4555 pr_out_entry_start(ctx
->dl
);
4556 if (dpipe_match_parse(&match
, nla_match
))
4557 goto err_match_parse
;
4558 pr_out_dpipe_match(&match
, ctx
);
4559 pr_out_entry_end(ctx
->dl
);
4564 pr_out_entry_end(ctx
->dl
);
4568 static struct resource
*
4569 resource_find(struct resources
*resources
, struct resource
*resource
,
4570 uint64_t resource_id
)
4572 struct list_head
*list_head
;
4575 list_head
= &resources
->resource_list
;
4577 list_head
= &resource
->resource_list
;
4579 list_for_each_entry(resource
, list_head
, list
) {
4580 struct resource
*child_resource
;
4582 if (resource
->id
== resource_id
)
4585 child_resource
= resource_find(resources
, resource
,
4588 return child_resource
;
4594 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4595 uint64_t resource_id
)
4597 struct resource
*resource
, *parent_resource
;
4598 const char del
[] = "/";
4602 resource
= resource_find(resources
, NULL
, resource_id
);
4606 for (parent_resource
= resource
; parent_resource
;
4607 parent_resource
= parent_resource
->parent
)
4608 path_len
+= strlen(parent_resource
->name
) + 1;
4611 path
= calloc(1, path_len
);
4615 path
+= path_len
- 1;
4616 for (parent_resource
= resource
; parent_resource
;
4617 parent_resource
= parent_resource
->parent
) {
4618 path
-= strlen(parent_resource
->name
);
4619 memcpy(path
, parent_resource
->name
,
4620 strlen(parent_resource
->name
));
4621 path
-= strlen(del
);
4622 memcpy(path
, del
, strlen(del
));
4624 pr_out_str(dl
, "resource_path", path
);
4628 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4630 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4631 struct dpipe_table
*table
;
4632 uint32_t resource_units
;
4633 bool counters_enabled
;
4634 bool resource_valid
;
4638 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4639 if (err
!= MNL_CB_OK
)
4642 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4643 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4644 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4645 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4646 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4650 table
= dpipe_table_alloc();
4654 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4655 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4656 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4658 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4660 if (resource_valid
) {
4661 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4662 table
->resource_valid
= true;
4665 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4666 if (!ctx
->print_tables
)
4669 pr_out_str(ctx
->dl
, "name", table
->name
);
4670 pr_out_uint(ctx
->dl
, "size", size
);
4671 pr_out_str(ctx
->dl
, "counters_enabled",
4672 counters_enabled
? "true" : "false");
4674 if (resource_valid
) {
4675 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4676 resource_path_print(ctx
->dl
, ctx
->resources
,
4677 table
->resource_id
);
4678 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4681 pr_out_array_start(ctx
->dl
, "match");
4682 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4683 goto err_matches_show
;
4684 pr_out_array_end(ctx
->dl
);
4686 pr_out_array_start(ctx
->dl
, "action");
4687 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4688 goto err_actions_show
;
4689 pr_out_array_end(ctx
->dl
);
4695 pr_out_array_end(ctx
->dl
);
4699 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4701 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4702 struct nlattr
*nla_table
;
4704 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4705 if (ctx
->print_tables
)
4706 pr_out_handle_start_arr(ctx
->dl
, tb
);
4707 if (dpipe_table_show(ctx
, nla_table
))
4708 goto err_table_show
;
4709 if (ctx
->print_tables
)
4710 pr_out_handle_end(ctx
->dl
);
4715 if (ctx
->print_tables
)
4716 pr_out_handle_end(ctx
->dl
);
4720 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4722 struct dpipe_ctx
*ctx
= data
;
4723 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4724 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4726 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4727 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4728 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4729 return MNL_CB_ERROR
;
4731 if (dpipe_tables_show(ctx
, tb
))
4732 return MNL_CB_ERROR
;
4736 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4738 static int cmd_dpipe_table_show(struct dl
*dl
)
4740 struct nlmsghdr
*nlh
;
4741 struct dpipe_ctx dpipe_ctx
= {};
4742 struct resource_ctx resource_ctx
= {};
4743 uint16_t flags
= NLM_F_REQUEST
;
4746 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4750 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4752 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4756 dpipe_ctx
.print_tables
= true;
4758 dl_opts_put(nlh
, dl
);
4759 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4762 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4763 goto err_headers_get
;
4766 err
= resource_ctx_init(&resource_ctx
, dl
);
4768 goto err_resource_ctx_init
;
4770 resource_ctx
.print_resources
= false;
4771 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4772 dl_opts_put(nlh
, dl
);
4773 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4776 dpipe_ctx
.resources
= resource_ctx
.resources
;
4778 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4779 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4780 dl_opts_put(nlh
, dl
);
4782 pr_out_section_start(dl
, "table");
4783 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4784 pr_out_section_end(dl
);
4786 resource_ctx_fini(&resource_ctx
);
4787 dpipe_ctx_fini(&dpipe_ctx
);
4790 err_resource_ctx_init
:
4792 dpipe_ctx_fini(&dpipe_ctx
);
4796 static int cmd_dpipe_table_set(struct dl
*dl
)
4798 struct nlmsghdr
*nlh
;
4801 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4802 NLM_F_REQUEST
| NLM_F_ACK
);
4804 err
= dl_argv_parse_put(nlh
, dl
,
4805 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4806 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4810 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4813 enum dpipe_value_type
{
4814 DPIPE_VALUE_TYPE_VALUE
,
4815 DPIPE_VALUE_TYPE_MASK
,
4819 dpipe_value_type_e2s(enum dpipe_value_type type
)
4822 case DPIPE_VALUE_TYPE_VALUE
:
4824 case DPIPE_VALUE_TYPE_MASK
:
4825 return "value_mask";
4831 struct dpipe_field_printer
{
4832 unsigned int field_id
;
4833 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4836 struct dpipe_header_printer
{
4837 struct dpipe_field_printer
*printers
;
4838 unsigned int printers_count
;
4839 unsigned int header_id
;
4842 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4843 enum dpipe_value_type type
,
4846 struct in_addr ip_addr
;
4848 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4849 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4853 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4854 enum dpipe_value_type type
,
4857 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4858 ether_ntoa((struct ether_addr
*)value
));
4861 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4862 enum dpipe_value_type type
,
4865 char str
[INET6_ADDRSTRLEN
];
4867 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4868 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4871 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4873 .printer
= dpipe_field_printer_ipv4_addr
,
4874 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4878 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4879 .printers
= dpipe_field_printers_ipv4
,
4880 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4881 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4884 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4886 .printer
= dpipe_field_printer_ethernet_addr
,
4887 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4891 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4892 .printers
= dpipe_field_printers_ethernet
,
4893 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4894 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4897 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4899 .printer
= dpipe_field_printer_ipv6_addr
,
4900 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4904 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4905 .printers
= dpipe_field_printers_ipv6
,
4906 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4907 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4910 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4911 &dpipe_header_printer_ipv4
,
4912 &dpipe_header_printer_ethernet
,
4913 &dpipe_header_printer_ipv6
,
4916 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4917 struct dpipe_op_info
*info
,
4918 enum dpipe_value_type type
,
4921 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4922 struct dpipe_header_printer
*header_printer
;
4923 struct dpipe_field_printer
*field_printer
;
4924 unsigned int field_printers_count
;
4928 for (i
= 0; i
< header_printers_count
; i
++) {
4929 header_printer
= dpipe_header_printers
[i
];
4930 if (header_printer
->header_id
!= info
->header_id
)
4932 field_printers_count
= header_printer
->printers_count
;
4933 for (j
= 0; j
< field_printers_count
; j
++) {
4934 field_printer
= &header_printer
->printers
[j
];
4935 if (field_printer
->field_id
!= info
->field_id
)
4937 field_printer
->printer(ctx
, type
, value
);
4945 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4947 unsigned int value_len
,
4948 struct dpipe_op_info
*info
,
4949 enum dpipe_value_type type
)
4951 if (info
->header_global
&&
4952 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4955 if (value_len
== sizeof(uint32_t)) {
4956 uint32_t *value_32
= value
;
4958 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4962 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4963 struct nlattr
**nla_match_value
,
4964 struct dpipe_op_info
*info
)
4966 void *value
, *value_mask
;
4967 uint32_t value_mapping
;
4971 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4972 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4974 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4975 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4978 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4979 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4983 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4984 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4985 DPIPE_VALUE_TYPE_MASK
);
4988 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4991 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4994 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4995 struct dpipe_match match
;
4998 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4999 if (err
!= MNL_CB_OK
)
5002 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5003 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5007 pr_out_entry_start(ctx
->dl
);
5008 if (dpipe_match_parse(&match
,
5009 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5010 goto err_match_parse
;
5011 pr_out_dpipe_match(&match
, ctx
);
5012 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5013 pr_out_entry_end(ctx
->dl
);
5018 pr_out_entry_end(ctx
->dl
);
5022 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5025 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5026 struct dpipe_action action
;
5029 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5030 if (err
!= MNL_CB_OK
)
5033 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5034 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5038 pr_out_entry_start(ctx
->dl
);
5039 if (dpipe_action_parse(&action
,
5040 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5041 goto err_action_parse
;
5042 pr_out_dpipe_action(&action
, ctx
);
5043 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5044 pr_out_entry_end(ctx
->dl
);
5049 pr_out_entry_end(ctx
->dl
);
5054 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5055 struct nlattr
*nla_action_values
)
5057 struct nlattr
*nla_action_value
;
5059 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5060 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5067 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5068 struct nlattr
*nla_match_values
)
5070 struct nlattr
*nla_match_value
;
5072 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5073 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5079 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5081 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5082 uint32_t entry_index
;
5086 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5087 if (err
!= MNL_CB_OK
)
5090 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5091 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5092 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5096 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5097 pr_out_uint(ctx
->dl
, "index", entry_index
);
5099 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5100 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5101 pr_out_uint(ctx
->dl
, "counter", counter
);
5104 pr_out_array_start(ctx
->dl
, "match_value");
5105 if (dpipe_tables_match_values_show(ctx
,
5106 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5107 goto err_match_values_show
;
5108 pr_out_array_end(ctx
->dl
);
5110 pr_out_array_start(ctx
->dl
, "action_value");
5111 if (dpipe_tables_action_values_show(ctx
,
5112 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5113 goto err_action_values_show
;
5114 pr_out_array_end(ctx
->dl
);
5117 err_action_values_show
:
5118 err_match_values_show
:
5119 pr_out_array_end(ctx
->dl
);
5123 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5125 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5126 struct nlattr
*nla_entry
;
5128 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5129 pr_out_handle_start_arr(ctx
->dl
, tb
);
5130 if (dpipe_entry_show(ctx
, nla_entry
))
5131 goto err_entry_show
;
5132 pr_out_handle_end(ctx
->dl
);
5137 pr_out_handle_end(ctx
->dl
);
5141 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5143 struct dpipe_ctx
*ctx
= data
;
5144 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5145 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5147 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5148 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5149 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5150 return MNL_CB_ERROR
;
5152 if (dpipe_table_entries_show(ctx
, tb
))
5153 return MNL_CB_ERROR
;
5157 static int cmd_dpipe_table_dump(struct dl
*dl
)
5159 struct nlmsghdr
*nlh
;
5160 struct dpipe_ctx ctx
= {};
5161 uint16_t flags
= NLM_F_REQUEST
;
5164 err
= dpipe_ctx_init(&ctx
, dl
);
5168 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5172 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5173 dl_opts_put(nlh
, dl
);
5174 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5176 pr_err("error get headers %s\n", strerror(ctx
.err
));
5180 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5181 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5182 dl_opts_put(nlh
, dl
);
5184 pr_out_section_start(dl
, "table_entry");
5185 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5186 pr_out_section_end(dl
);
5188 dpipe_ctx_fini(&ctx
);
5192 static void cmd_dpipe_table_help(void)
5194 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5195 "where OBJECT-LIST := { show | set | dump }\n");
5198 static int cmd_dpipe_table(struct dl
*dl
)
5200 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5201 cmd_dpipe_table_help();
5203 } else if (dl_argv_match(dl
, "show")) {
5205 return cmd_dpipe_table_show(dl
);
5206 } else if (dl_argv_match(dl
, "set")) {
5208 return cmd_dpipe_table_set(dl
);
5209 } else if (dl_argv_match(dl
, "dump")) {
5211 return cmd_dpipe_table_dump(dl
);
5213 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5217 static void cmd_dpipe_help(void)
5219 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5220 "where OBJECT-LIST := { header | table }\n");
5223 static int cmd_dpipe(struct dl
*dl
)
5225 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5228 } else if (dl_argv_match(dl
, "header")) {
5230 return cmd_dpipe_header(dl
);
5231 } else if (dl_argv_match(dl
, "table")) {
5233 return cmd_dpipe_table(dl
);
5235 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5240 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5241 struct nlattr
**nla_resource
)
5243 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5244 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5245 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5246 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5247 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5248 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5249 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5253 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5254 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5255 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5256 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5257 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5258 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5259 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5261 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5262 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5264 resource
->size_new
= resource
->size
;
5266 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5267 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5268 resource
->occ_valid
= true;
5271 if (resource
->size_new
!= resource
->size
)
5272 ctx
->pending_change
= true;
5278 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5279 struct resource
*parent_resource
, struct nlattr
*nl
)
5281 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5282 struct nlattr
*nla_child_resource
;
5283 struct nlattr
*nla_resources
;
5293 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5294 if (err
!= MNL_CB_OK
)
5297 err
= resource_parse(ctx
, resource
, nla_resource
);
5301 resource
->parent
= parent_resource
;
5302 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5305 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5306 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5308 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5309 struct resource
*child_resource
;
5310 struct list_head
*list
;
5312 child_resource
= resource_alloc();
5313 if (!child_resource
)
5317 list
= &ctx
->resources
->resource_list
;
5319 list
= &resource
->resource_list
;
5321 list_add_tail(&child_resource
->list
, list
);
5322 err
= resource_get(ctx
, child_resource
, resource
,
5323 nla_child_resource
);
5331 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5334 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5335 default: return "<unknown unit>";
5339 static void resource_show(struct resource
*resource
,
5340 struct resource_ctx
*ctx
)
5342 struct resource
*child_resource
;
5343 struct dpipe_table
*table
;
5344 struct dl
*dl
= ctx
->dl
;
5347 pr_out_str(dl
, "name", resource
->name
);
5349 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5350 pr_out_u64(dl
, "size", resource
->size
);
5351 if (resource
->size
!= resource
->size_new
)
5352 pr_out_u64(dl
, "size_new", resource
->size_new
);
5353 if (resource
->occ_valid
)
5354 pr_out_uint(dl
, "occ", resource
->size_occ
);
5355 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5357 if (resource
->size_min
!= resource
->size_max
) {
5358 pr_out_uint(dl
, "size_min", resource
->size_min
);
5359 pr_out_u64(dl
, "size_max", resource
->size_max
);
5360 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5363 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5364 if (table
->resource_id
== resource
->id
&&
5365 table
->resource_valid
)
5369 pr_out_array_start(dl
, "dpipe_tables");
5371 pr_out_str(dl
, "dpipe_tables", "none");
5373 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5374 if (table
->resource_id
!= resource
->id
||
5375 !table
->resource_valid
)
5377 pr_out_entry_start(dl
);
5378 pr_out_str(dl
, "table_name", table
->name
);
5379 pr_out_entry_end(dl
);
5382 pr_out_array_end(dl
);
5384 if (list_empty(&resource
->resource_list
))
5387 if (ctx
->pending_change
)
5388 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5390 pr_out_array_start(dl
, "resources");
5391 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5392 pr_out_entry_start(dl
);
5393 resource_show(child_resource
, ctx
);
5394 pr_out_entry_end(dl
);
5396 pr_out_array_end(dl
);
5400 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5402 struct resources
*resources
= ctx
->resources
;
5403 struct resource
*resource
;
5405 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5406 pr_out_handle_start_arr(ctx
->dl
, tb
);
5407 resource_show(resource
, ctx
);
5408 pr_out_handle_end(ctx
->dl
);
5412 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5414 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5417 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5419 struct resource_ctx
*ctx
= data
;
5420 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5421 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5424 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5425 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5426 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5427 return MNL_CB_ERROR
;
5429 err
= resources_get(ctx
, tb
);
5432 return MNL_CB_ERROR
;
5435 if (ctx
->print_resources
)
5436 resources_show(ctx
, tb
);
5441 static int cmd_resource_show(struct dl
*dl
)
5443 struct nlmsghdr
*nlh
;
5444 struct dpipe_ctx dpipe_ctx
= {};
5445 struct resource_ctx resource_ctx
= {};
5448 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5452 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5454 dl_opts_put(nlh
, dl
);
5456 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5460 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5463 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5467 err
= resource_ctx_init(&resource_ctx
, dl
);
5471 resource_ctx
.print_resources
= true;
5472 resource_ctx
.tables
= dpipe_ctx
.tables
;
5473 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5474 NLM_F_REQUEST
| NLM_F_ACK
);
5475 dl_opts_put(nlh
, dl
);
5476 pr_out_section_start(dl
, "resources");
5477 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5479 pr_out_section_end(dl
);
5480 resource_ctx_fini(&resource_ctx
);
5482 dpipe_ctx_fini(&dpipe_ctx
);
5486 static void cmd_resource_help(void)
5488 pr_err("Usage: devlink resource show DEV\n"
5489 " devlink resource set DEV path PATH size SIZE\n");
5492 static struct resource
*
5493 resource_find_by_name(struct list_head
*list
, char *name
)
5495 struct resource
*resource
;
5497 list_for_each_entry(resource
, list
, list
) {
5498 if (!strcmp(resource
->name
, name
))
5505 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5506 uint32_t *p_resource_id
, bool *p_resource_valid
)
5508 struct resource
*resource
;
5509 uint32_t resource_id
= 0;
5510 char *resource_path_dup
;
5511 struct list_head
*list
;
5512 const char del
[] = "/";
5513 char *resource_name
;
5515 resource_path_dup
= strdup(resource_path
);
5516 list
= &ctx
->resources
->resource_list
;
5517 resource_name
= strtok(resource_path_dup
, del
);
5518 while (resource_name
!= NULL
) {
5519 resource
= resource_find_by_name(list
, resource_name
);
5521 goto err_resource_lookup
;
5523 list
= &resource
->resource_list
;
5524 resource_name
= strtok(NULL
, del
);
5525 resource_id
= resource
->id
;
5527 free(resource_path_dup
);
5528 *p_resource_valid
= true;
5529 *p_resource_id
= resource_id
;
5532 err_resource_lookup
:
5533 free(resource_path_dup
);
5537 static int cmd_resource_set(struct dl
*dl
)
5539 struct nlmsghdr
*nlh
;
5540 struct resource_ctx ctx
= {};
5543 err
= resource_ctx_init(&ctx
, dl
);
5547 ctx
.print_resources
= false;
5548 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5549 DL_OPT_RESOURCE_SIZE
, 0);
5553 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5555 dl_opts_put(nlh
, dl
);
5556 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5558 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5562 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5563 &dl
->opts
.resource_id
,
5564 &dl
->opts
.resource_id_valid
);
5566 pr_err("error parsing resource path %s\n", strerror(-err
));
5570 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5571 NLM_F_REQUEST
| NLM_F_ACK
);
5573 dl_opts_put(nlh
, dl
);
5574 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5576 resource_ctx_fini(&ctx
);
5580 static int cmd_resource(struct dl
*dl
)
5582 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5583 cmd_resource_help();
5585 } else if (dl_argv_match(dl
, "show")) {
5587 return cmd_resource_show(dl
);
5588 } else if (dl_argv_match(dl
, "set")) {
5590 return cmd_resource_set(dl
);
5592 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5596 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5598 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5599 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5600 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5603 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5604 if (dl
->json_output
) {
5605 jsonw_name(dl
->jw
, buf
);
5606 jsonw_start_object(dl
->jw
);
5612 static void pr_out_region_handle_end(struct dl
*dl
)
5614 if (dl
->json_output
)
5615 jsonw_end_object(dl
->jw
);
5620 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5622 if (dl
->json_output
) {
5623 jsonw_name(dl
->jw
, "snapshot");
5624 jsonw_start_array(dl
->jw
);
5626 if (g_indent_newline
)
5627 pr_out("snapshot %s", array
? "[" : "");
5629 pr_out(" snapshot %s", array
? "[" : "");
5633 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5635 if (dl
->json_output
)
5636 jsonw_end_array(dl
->jw
);
5641 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5643 uint32_t snapshot_id
;
5645 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5648 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5650 if (dl
->json_output
)
5651 jsonw_uint(dl
->jw
, snapshot_id
);
5653 pr_out("%s%u", index
? " " : "", snapshot_id
);
5656 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5658 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5659 struct nlattr
*nla_sanpshot
;
5662 pr_out_region_snapshots_start(dl
, true);
5663 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5664 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5665 if (err
!= MNL_CB_OK
)
5667 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5669 pr_out_region_snapshots_end(dl
, true);
5672 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5674 pr_out_region_snapshots_start(dl
, false);
5675 pr_out_region_snapshots_id(dl
, tb
, 0);
5676 pr_out_region_snapshots_end(dl
, false);
5679 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5681 pr_out_region_handle_start(dl
, tb
);
5683 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5684 pr_out_u64(dl
, "size",
5685 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5687 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5688 pr_out_snapshots(dl
, tb
);
5690 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5691 pr_out_snapshot(dl
, tb
);
5693 pr_out_region_handle_end(dl
);
5696 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5698 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5699 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5700 struct dl
*dl
= data
;
5702 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5703 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5704 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5705 return MNL_CB_ERROR
;
5707 pr_out_region(dl
, tb
);
5712 static int cmd_region_show(struct dl
*dl
)
5714 struct nlmsghdr
*nlh
;
5715 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5718 if (dl_argc(dl
) == 0)
5719 flags
|= NLM_F_DUMP
;
5721 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5723 if (dl_argc(dl
) > 0) {
5724 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5729 pr_out_section_start(dl
, "regions");
5730 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5731 pr_out_section_end(dl
);
5735 static int cmd_region_snapshot_del(struct dl
*dl
)
5737 struct nlmsghdr
*nlh
;
5740 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5741 NLM_F_REQUEST
| NLM_F_ACK
);
5743 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5744 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5748 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5751 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5753 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5754 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5755 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5756 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5757 struct dl
*dl
= data
;
5760 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5761 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5762 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5763 return MNL_CB_ERROR
;
5765 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5766 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5767 if (err
!= MNL_CB_OK
)
5768 return MNL_CB_ERROR
;
5770 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5771 if (!nla_chunk_data
)
5774 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5775 if (!nla_chunk_addr
)
5778 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5779 mnl_attr_get_payload_len(nla_chunk_data
),
5780 mnl_attr_get_u64(nla_chunk_addr
));
5785 static int cmd_region_dump(struct dl
*dl
)
5787 struct nlmsghdr
*nlh
;
5790 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5791 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5793 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5794 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5798 pr_out_section_start(dl
, "dump");
5799 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5800 pr_out_section_end(dl
);
5801 if (!dl
->json_output
)
5806 static int cmd_region_read(struct dl
*dl
)
5808 struct nlmsghdr
*nlh
;
5811 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5812 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5814 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5815 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5816 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5820 pr_out_section_start(dl
, "read");
5821 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5822 pr_out_section_end(dl
);
5823 if (!dl
->json_output
)
5828 static void cmd_region_help(void)
5830 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5831 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5832 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5833 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5836 static int cmd_region(struct dl
*dl
)
5838 if (dl_no_arg(dl
)) {
5839 return cmd_region_show(dl
);
5840 } else if (dl_argv_match(dl
, "help")) {
5843 } else if (dl_argv_match(dl
, "show")) {
5845 return cmd_region_show(dl
);
5846 } else if (dl_argv_match(dl
, "del")) {
5848 return cmd_region_snapshot_del(dl
);
5849 } else if (dl_argv_match(dl
, "dump")) {
5851 return cmd_region_dump(dl
);
5852 } else if (dl_argv_match(dl
, "read")) {
5854 return cmd_region_read(dl
);
5856 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5860 static int cmd_health_set_params(struct dl
*dl
)
5862 struct nlmsghdr
*nlh
;
5865 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
5866 NLM_F_REQUEST
| NLM_F_ACK
);
5867 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
5868 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
5869 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
5873 dl_opts_put(nlh
, dl
);
5874 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5877 static int cmd_health_dump_clear(struct dl
*dl
)
5879 struct nlmsghdr
*nlh
;
5882 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
5883 NLM_F_REQUEST
| NLM_F_ACK
);
5885 err
= dl_argv_parse_put(nlh
, dl
,
5886 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
5890 dl_opts_put(nlh
, dl
);
5891 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5894 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
5901 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
5904 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
5907 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
5910 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
5913 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
5915 case MNL_TYPE_NUL_STRING
:
5916 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
5918 case MNL_TYPE_BINARY
:
5919 len
= mnl_attr_get_payload_len(nl_data
);
5920 data
= mnl_attr_get_payload(nl_data
);
5921 pr_out_binary_value(dl
, data
, len
);
5929 struct nest_qentry
{
5931 TAILQ_ENTRY(nest_qentry
) nest_entries
;
5934 struct fmsg_cb_data
{
5937 TAILQ_HEAD(, nest_qentry
) qhead
;
5940 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
5941 uint8_t *attr_value
, bool insert
)
5943 struct nest_qentry
*entry
= NULL
;
5946 entry
= malloc(sizeof(struct nest_qentry
));
5950 entry
->attr_type
= *attr_value
;
5951 TAILQ_INSERT_HEAD(&fmsg_data
->qhead
, entry
, nest_entries
);
5953 if (TAILQ_EMPTY(&fmsg_data
->qhead
))
5954 return MNL_CB_ERROR
;
5955 entry
= TAILQ_FIRST(&fmsg_data
->qhead
);
5956 *attr_value
= entry
->attr_type
;
5957 TAILQ_REMOVE(&fmsg_data
->qhead
, entry
, nest_entries
);
5963 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
5966 struct dl
*dl
= fmsg_data
->dl
;
5967 uint8_t value
= nest_value
;
5970 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
5971 if (err
!= MNL_CB_OK
)
5975 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
5977 pr_out_entry_start(dl
);
5979 pr_out_entry_end(dl
);
5981 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
5983 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
5984 if (dl
->json_output
) {
5986 jsonw_start_array(dl
->jw
);
5988 jsonw_end_array(dl
->jw
);
5992 __pr_out_indent_inc();
5994 __pr_out_indent_dec();
6004 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6006 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6007 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6008 struct fmsg_cb_data
*fmsg_data
= data
;
6009 struct dl
*dl
= fmsg_data
->dl
;
6010 struct nlattr
*nla_object
;
6014 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6015 if (!tb
[DEVLINK_ATTR_FMSG
])
6016 return MNL_CB_ERROR
;
6018 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6019 attr_type
= mnl_attr_get_type(nla_object
);
6020 switch (attr_type
) {
6021 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6022 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6023 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6024 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6025 if (err
!= MNL_CB_OK
)
6028 case DEVLINK_ATTR_FMSG_NEST_END
:
6029 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6030 if (err
!= MNL_CB_OK
)
6033 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6034 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6036 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6037 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6039 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6040 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6042 if (err
!= MNL_CB_OK
)
6052 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
)
6054 struct fmsg_cb_data data
;
6055 struct nlmsghdr
*nlh
;
6058 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, NLM_F_REQUEST
| NLM_F_ACK
);
6060 err
= dl_argv_parse_put(nlh
, dl
,
6061 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6066 TAILQ_INIT(&data
.qhead
);
6067 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6071 static int cmd_health_dump_show(struct dl
*dl
)
6073 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
6076 static int cmd_health_diagnose(struct dl
*dl
)
6078 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
);
6081 static int cmd_health_recover(struct dl
*dl
)
6083 struct nlmsghdr
*nlh
;
6086 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6087 NLM_F_REQUEST
| NLM_F_ACK
);
6089 err
= dl_argv_parse_put(nlh
, dl
,
6090 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6094 dl_opts_put(nlh
, dl
);
6095 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6098 enum devlink_health_reporter_state
{
6099 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6100 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6103 static const char *health_state_name(uint8_t state
)
6106 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6107 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6108 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6109 return HEALTH_REPORTER_STATE_ERROR_STR
;
6111 return "<unknown state>";
6115 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6117 struct sysinfo s_info
;
6123 info
= localtime(&now
);
6124 err
= sysinfo(&s_info
);
6127 /* Subtract uptime in sec from now yields the time of system
6128 * uptime. To this, add time_ms which is the amount of
6129 * milliseconds elapsed between uptime and the dump taken.
6131 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6132 info
= localtime(&sec
);
6134 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6135 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6138 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6140 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6141 enum devlink_health_reporter_state state
;
6142 const struct nlattr
*attr
;
6146 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6148 if (err
!= MNL_CB_OK
)
6151 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6152 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6153 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6154 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6157 pr_out_handle_start_arr(dl
, tb_health
);
6159 pr_out_str(dl
, "name",
6160 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6161 if (!dl
->json_output
) {
6163 __pr_out_indent_inc();
6165 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6166 pr_out_str(dl
, "state", health_state_name(state
));
6167 pr_out_u64(dl
, "error",
6168 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6169 pr_out_u64(dl
, "recover",
6170 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6171 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6172 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6173 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6175 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6176 time_ms
= mnl_attr_get_u64(attr
);
6177 format_logtime(time_ms
, dump_date
, dump_time
);
6179 pr_out_str(dl
, "last_dump_date", dump_date
);
6180 pr_out_str(dl
, "last_dump_time", dump_time
);
6182 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6183 pr_out_u64(dl
, "grace_period",
6184 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6185 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6186 pr_out_bool(dl
, "auto_recover",
6187 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6189 __pr_out_indent_dec();
6190 pr_out_handle_end(dl
);
6193 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6195 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6196 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6197 struct dl
*dl
= data
;
6199 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6200 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6201 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6202 return MNL_CB_ERROR
;
6204 pr_out_health(dl
, tb
);
6209 static int cmd_health_show(struct dl
*dl
)
6211 struct nlmsghdr
*nlh
;
6212 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6215 if (dl_argc(dl
) == 0)
6216 flags
|= NLM_F_DUMP
;
6217 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6220 if (dl_argc(dl
) > 0) {
6221 err
= dl_argv_parse_put(nlh
, dl
,
6223 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6227 pr_out_section_start(dl
, "health");
6229 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6230 pr_out_section_end(dl
);
6234 static void cmd_health_help(void)
6236 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6237 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6238 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6239 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6240 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6241 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6244 static int cmd_health(struct dl
*dl
)
6246 if (dl_argv_match(dl
, "help")) {
6249 } else if (dl_argv_match(dl
, "show") ||
6250 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6252 return cmd_health_show(dl
);
6253 } else if (dl_argv_match(dl
, "recover")) {
6255 return cmd_health_recover(dl
);
6256 } else if (dl_argv_match(dl
, "diagnose")) {
6258 return cmd_health_diagnose(dl
);
6259 } else if (dl_argv_match(dl
, "dump")) {
6261 if (dl_argv_match(dl
, "show")) {
6263 return cmd_health_dump_show(dl
);
6264 } else if (dl_argv_match(dl
, "clear")) {
6266 return cmd_health_dump_clear(dl
);
6268 } else if (dl_argv_match(dl
, "set")) {
6270 return cmd_health_set_params(dl
);
6272 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6276 static void help(void)
6278 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
6279 " devlink [ -f[orce] ] -b[atch] filename\n"
6280 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health }\n"
6281 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
6284 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
6289 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6292 } else if (dl_argv_match(dl
, "dev")) {
6295 } else if (dl_argv_match(dl
, "port")) {
6297 return cmd_port(dl
);
6298 } else if (dl_argv_match(dl
, "sb")) {
6301 } else if (dl_argv_match(dl
, "monitor")) {
6304 } else if (dl_argv_match(dl
, "dpipe")) {
6306 return cmd_dpipe(dl
);
6307 } else if (dl_argv_match(dl
, "resource")) {
6309 return cmd_resource(dl
);
6310 } else if (dl_argv_match(dl
, "region")) {
6312 return cmd_region(dl
);
6313 } else if (dl_argv_match(dl
, "health")) {
6315 return cmd_health(dl
);
6317 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
6321 static int dl_init(struct dl
*dl
)
6325 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
6327 pr_err("Failed to connect to devlink Netlink\n");
6331 err
= ifname_map_init(dl
);
6333 pr_err("Failed to create index map\n");
6334 goto err_ifname_map_create
;
6336 if (dl
->json_output
) {
6337 dl
->jw
= jsonw_new(stdout
);
6339 pr_err("Failed to create JSON writer\n");
6342 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
6347 ifname_map_fini(dl
);
6348 err_ifname_map_create
:
6349 mnlg_socket_close(dl
->nlg
);
6353 static void dl_fini(struct dl
*dl
)
6355 if (dl
->json_output
)
6356 jsonw_destroy(&dl
->jw
);
6357 ifname_map_fini(dl
);
6358 mnlg_socket_close(dl
->nlg
);
6361 static struct dl
*dl_alloc(void)
6365 dl
= calloc(1, sizeof(*dl
));
6371 static void dl_free(struct dl
*dl
)
6376 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
6380 int ret
= EXIT_SUCCESS
;
6382 if (name
&& strcmp(name
, "-") != 0) {
6383 if (freopen(name
, "r", stdin
) == NULL
) {
6385 "Cannot open file \"%s\" for reading: %s\n",
6386 name
, strerror(errno
));
6387 return EXIT_FAILURE
;
6392 while (getcmdline(&line
, &len
, stdin
) != -1) {
6396 largc
= makeargs(line
, largv
, 100);
6398 continue; /* blank line */
6400 if (dl_cmd(dl
, largc
, largv
)) {
6401 fprintf(stderr
, "Command failed %s:%d\n",
6415 int main(int argc
, char **argv
)
6417 static const struct option long_options
[] = {
6418 { "Version", no_argument
, NULL
, 'V' },
6419 { "force", no_argument
, NULL
, 'f' },
6420 { "batch", required_argument
, NULL
, 'b' },
6421 { "no-nice-names", no_argument
, NULL
, 'n' },
6422 { "json", no_argument
, NULL
, 'j' },
6423 { "pretty", no_argument
, NULL
, 'p' },
6424 { "verbose", no_argument
, NULL
, 'v' },
6425 { NULL
, 0, NULL
, 0 }
6427 const char *batch_file
= NULL
;
6436 pr_err("Failed to allocate memory for devlink\n");
6437 return EXIT_FAILURE
;
6440 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
6441 long_options
, NULL
)) >= 0) {
6445 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
6452 batch_file
= optarg
;
6455 dl
->no_nice_names
= true;
6458 dl
->json_output
= true;
6461 dl
->pretty_output
= true;
6467 pr_err("Unknown option.\n");
6484 err
= dl_batch(dl
, batch_file
, force
);
6486 err
= dl_cmd(dl
, argc
, argv
);