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(15, "%7u/%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")) {
3861 } else if (dl_no_arg(dl
)) {
3863 return cmd_mon_show(dl
);
3865 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3869 struct dpipe_field
{
3872 unsigned int bitwidth
;
3873 enum devlink_dpipe_field_mapping_type mapping_type
;
3876 struct dpipe_header
{
3877 struct list_head list
;
3880 struct dpipe_field
*fields
;
3881 unsigned int fields_count
;
3884 struct dpipe_table
{
3885 struct list_head list
;
3887 unsigned int resource_id
;
3888 bool resource_valid
;
3891 struct dpipe_tables
{
3892 struct list_head table_list
;
3902 enum devlink_resource_unit unit
;
3907 struct list_head list
;
3908 struct list_head resource_list
;
3909 struct resource
*parent
;
3913 struct list_head resource_list
;
3916 struct resource_ctx
{
3919 struct resources
*resources
;
3920 struct dpipe_tables
*tables
;
3921 bool print_resources
;
3922 bool pending_change
;
3925 static struct resource
*resource_alloc(void)
3927 struct resource
*resource
;
3929 resource
= calloc(1, sizeof(struct resource
));
3932 INIT_LIST_HEAD(&resource
->resource_list
);
3936 static void resource_free(struct resource
*resource
)
3938 struct resource
*child_resource
, *tmp
;
3940 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3942 free(child_resource
->name
);
3943 resource_free(child_resource
);
3948 static struct resources
*resources_alloc(void)
3950 struct resources
*resources
;
3952 resources
= calloc(1, sizeof(struct resources
));
3955 INIT_LIST_HEAD(&resources
->resource_list
);
3959 static void resources_free(struct resources
*resources
)
3961 struct resource
*resource
, *tmp
;
3963 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3964 resource_free(resource
);
3967 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3969 ctx
->resources
= resources_alloc();
3970 if (!ctx
->resources
)
3976 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3978 resources_free(ctx
->resources
);
3984 struct list_head global_headers
;
3985 struct list_head local_headers
;
3986 struct dpipe_tables
*tables
;
3987 struct resources
*resources
;
3992 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3994 struct dpipe_header
*header
;
3996 header
= calloc(1, sizeof(struct dpipe_header
));
3999 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4000 if (!header
->fields
)
4001 goto err_fields_alloc
;
4002 header
->fields_count
= fields_count
;
4010 static void dpipe_header_free(struct dpipe_header
*header
)
4012 free(header
->fields
);
4016 static void dpipe_header_clear(struct dpipe_header
*header
)
4018 struct dpipe_field
*field
;
4021 for (i
= 0; i
< header
->fields_count
; i
++) {
4022 field
= &header
->fields
[i
];
4028 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4029 struct dpipe_header
*header
, bool global
)
4032 list_add(&header
->list
, &ctx
->global_headers
);
4034 list_add(&header
->list
, &ctx
->local_headers
);
4037 static void dpipe_header_del(struct dpipe_header
*header
)
4039 list_del(&header
->list
);
4042 static struct dpipe_table
*dpipe_table_alloc(void)
4044 return calloc(1, sizeof(struct dpipe_table
));
4047 static void dpipe_table_free(struct dpipe_table
*table
)
4052 static struct dpipe_tables
*dpipe_tables_alloc(void)
4054 struct dpipe_tables
*tables
;
4056 tables
= calloc(1, sizeof(struct dpipe_tables
));
4059 INIT_LIST_HEAD(&tables
->table_list
);
4063 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4065 struct dpipe_table
*table
, *tmp
;
4067 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4068 dpipe_table_free(table
);
4072 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4074 ctx
->tables
= dpipe_tables_alloc();
4079 INIT_LIST_HEAD(&ctx
->global_headers
);
4080 INIT_LIST_HEAD(&ctx
->local_headers
);
4084 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4086 struct dpipe_header
*header
, *tmp
;
4088 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4090 dpipe_header_del(header
);
4091 dpipe_header_clear(header
);
4092 dpipe_header_free(header
);
4094 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4096 dpipe_header_del(header
);
4097 dpipe_header_clear(header
);
4098 dpipe_header_free(header
);
4100 dpipe_tables_free(ctx
->tables
);
4103 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4104 uint32_t header_id
, bool global
)
4106 struct list_head
*header_list
;
4107 struct dpipe_header
*header
;
4110 header_list
= &ctx
->global_headers
;
4112 header_list
= &ctx
->local_headers
;
4113 list_for_each_entry(header
, header_list
, list
) {
4114 if (header
->id
!= header_id
)
4116 return header
->name
;
4121 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4123 uint32_t field_id
, bool global
)
4125 struct list_head
*header_list
;
4126 struct dpipe_header
*header
;
4129 header_list
= &ctx
->global_headers
;
4131 header_list
= &ctx
->local_headers
;
4132 list_for_each_entry(header
, header_list
, list
) {
4133 if (header
->id
!= header_id
)
4135 return header
->fields
[field_id
].name
;
4141 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4143 switch (mapping_type
) {
4144 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4146 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4154 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4155 uint32_t field_id
, bool global
)
4157 enum devlink_dpipe_field_mapping_type mapping_type
;
4158 struct list_head
*header_list
;
4159 struct dpipe_header
*header
;
4162 header_list
= &ctx
->global_headers
;
4164 header_list
= &ctx
->local_headers
;
4165 list_for_each_entry(header
, header_list
, list
) {
4166 if (header
->id
!= header_id
)
4168 mapping_type
= header
->fields
[field_id
].mapping_type
;
4169 return dpipe_field_mapping_e2s(mapping_type
);
4174 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4175 struct dpipe_field
*fields
,
4176 unsigned int field_count
)
4178 struct dpipe_field
*field
;
4181 for (i
= 0; i
< field_count
; i
++) {
4183 pr_out_entry_start(ctx
->dl
);
4184 pr_out_str(ctx
->dl
, "name", field
->name
);
4185 if (ctx
->dl
->verbose
)
4186 pr_out_uint(ctx
->dl
, "id", field
->id
);
4187 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4188 if (field
->mapping_type
)
4189 pr_out_str(ctx
->dl
, "mapping_type",
4190 dpipe_field_mapping_e2s(field
->mapping_type
));
4191 pr_out_entry_end(ctx
->dl
);
4196 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4197 struct dpipe_header
*header
, bool global
)
4199 pr_out_handle_start_arr(ctx
->dl
, tb
);
4200 pr_out_str(ctx
->dl
, "name", header
->name
);
4201 if (ctx
->dl
->verbose
) {
4202 pr_out_uint(ctx
->dl
, "id", header
->id
);
4203 pr_out_str(ctx
->dl
, "global",
4204 global
? "true" : "false");
4206 pr_out_array_start(ctx
->dl
, "field");
4207 pr_out_dpipe_fields(ctx
, header
->fields
,
4208 header
->fields_count
);
4209 pr_out_array_end(ctx
->dl
);
4210 pr_out_handle_end(ctx
->dl
);
4213 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4216 struct dpipe_header
*header
;
4218 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4219 pr_out_dpipe_header(ctx
, tb
, header
, false);
4221 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4222 pr_out_dpipe_header(ctx
, tb
, header
, true);
4225 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4227 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4231 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4232 if (err
!= MNL_CB_OK
)
4234 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4235 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4236 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4237 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4240 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4241 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4242 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4243 field
->name
= strdup(name
);
4246 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4250 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4251 struct dpipe_field
*fields
)
4253 struct nlattr
*nla_field
;
4257 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4258 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4266 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4268 struct nlattr
*nla_field
;
4269 unsigned int count
= 0;
4271 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4276 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4278 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4279 struct dpipe_header
*header
;
4280 unsigned int fields_count
;
4281 const char *header_name
;
4285 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4286 if (err
!= MNL_CB_OK
)
4289 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4290 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4291 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4294 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4295 header
= dpipe_header_alloc(fields_count
);
4299 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4300 header
->name
= strdup(header_name
);
4301 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4302 header
->fields_count
= fields_count
;
4303 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4305 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4309 dpipe_header_add(ctx
, header
, global
);
4313 dpipe_header_free(header
);
4317 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4319 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4320 struct nlattr
*nla_header
;
4323 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4324 err
= dpipe_header_get(ctx
, nla_header
);
4331 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4333 struct dpipe_ctx
*ctx
= data
;
4334 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4335 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4338 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4339 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4340 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4341 return MNL_CB_ERROR
;
4342 err
= dpipe_headers_get(ctx
, tb
);
4345 return MNL_CB_ERROR
;
4348 if (ctx
->print_headers
)
4349 pr_out_dpipe_headers(ctx
, tb
);
4353 static int cmd_dpipe_headers_show(struct dl
*dl
)
4355 struct nlmsghdr
*nlh
;
4356 struct dpipe_ctx ctx
= {};
4357 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4360 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4362 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4366 err
= dpipe_ctx_init(&ctx
, dl
);
4370 ctx
.print_headers
= true;
4372 pr_out_section_start(dl
, "header");
4373 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4375 pr_err("error get headers %s\n", strerror(ctx
.err
));
4376 pr_out_section_end(dl
);
4378 dpipe_ctx_fini(&ctx
);
4382 static void cmd_dpipe_header_help(void)
4384 pr_err("Usage: devlink dpipe headers show DEV\n");
4387 static int cmd_dpipe_header(struct dl
*dl
)
4389 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4390 cmd_dpipe_header_help();
4392 } else if (dl_argv_match(dl
, "show")) {
4394 return cmd_dpipe_headers_show(dl
);
4396 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4401 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4403 switch (action_type
) {
4404 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4405 return "field_modify";
4411 struct dpipe_op_info
{
4417 struct dpipe_action
{
4418 struct dpipe_op_info info
;
4422 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4423 struct dpipe_ctx
*ctx
)
4425 struct dpipe_op_info
*op_info
= &action
->info
;
4426 const char *mapping
;
4428 pr_out_str(ctx
->dl
, "type",
4429 dpipe_action_type_e2s(action
->type
));
4430 pr_out_str(ctx
->dl
, "header",
4431 dpipe_header_id2s(ctx
, op_info
->header_id
,
4432 op_info
->header_global
));
4433 pr_out_str(ctx
->dl
, "field",
4434 dpipe_field_id2s(ctx
, op_info
->header_id
,
4436 op_info
->header_global
));
4437 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4439 op_info
->header_global
);
4441 pr_out_str(ctx
->dl
, "mapping", mapping
);
4444 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4446 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4449 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4450 if (err
!= MNL_CB_OK
)
4453 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4454 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4455 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4456 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4460 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4461 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4462 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4463 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4468 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4469 struct nlattr
*nla_actions
)
4471 struct nlattr
*nla_action
;
4472 struct dpipe_action action
;
4474 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4475 pr_out_entry_start(ctx
->dl
);
4476 if (dpipe_action_parse(&action
, nla_action
))
4477 goto err_action_parse
;
4478 pr_out_dpipe_action(&action
, ctx
);
4479 pr_out_entry_end(ctx
->dl
);
4484 pr_out_entry_end(ctx
->dl
);
4489 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4491 switch (match_type
) {
4492 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4493 return "field_exact";
4499 struct dpipe_match
{
4500 struct dpipe_op_info info
;
4504 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4505 struct dpipe_ctx
*ctx
)
4507 struct dpipe_op_info
*op_info
= &match
->info
;
4508 const char *mapping
;
4510 pr_out_str(ctx
->dl
, "type",
4511 dpipe_match_type_e2s(match
->type
));
4512 pr_out_str(ctx
->dl
, "header",
4513 dpipe_header_id2s(ctx
, op_info
->header_id
,
4514 op_info
->header_global
));
4515 pr_out_str(ctx
->dl
, "field",
4516 dpipe_field_id2s(ctx
, op_info
->header_id
,
4518 op_info
->header_global
));
4519 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4521 op_info
->header_global
);
4523 pr_out_str(ctx
->dl
, "mapping", mapping
);
4526 static int dpipe_match_parse(struct dpipe_match
*match
,
4530 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4533 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4534 if (err
!= MNL_CB_OK
)
4537 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4538 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4539 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4540 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4544 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4545 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4546 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4547 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4552 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4553 struct nlattr
*nla_matches
)
4555 struct nlattr
*nla_match
;
4556 struct dpipe_match match
;
4558 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4559 pr_out_entry_start(ctx
->dl
);
4560 if (dpipe_match_parse(&match
, nla_match
))
4561 goto err_match_parse
;
4562 pr_out_dpipe_match(&match
, ctx
);
4563 pr_out_entry_end(ctx
->dl
);
4568 pr_out_entry_end(ctx
->dl
);
4572 static struct resource
*
4573 resource_find(struct resources
*resources
, struct resource
*resource
,
4574 uint64_t resource_id
)
4576 struct list_head
*list_head
;
4579 list_head
= &resources
->resource_list
;
4581 list_head
= &resource
->resource_list
;
4583 list_for_each_entry(resource
, list_head
, list
) {
4584 struct resource
*child_resource
;
4586 if (resource
->id
== resource_id
)
4589 child_resource
= resource_find(resources
, resource
,
4592 return child_resource
;
4598 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4599 uint64_t resource_id
)
4601 struct resource
*resource
, *parent_resource
;
4602 const char del
[] = "/";
4606 resource
= resource_find(resources
, NULL
, resource_id
);
4610 for (parent_resource
= resource
; parent_resource
;
4611 parent_resource
= parent_resource
->parent
)
4612 path_len
+= strlen(parent_resource
->name
) + 1;
4615 path
= calloc(1, path_len
);
4619 path
+= path_len
- 1;
4620 for (parent_resource
= resource
; parent_resource
;
4621 parent_resource
= parent_resource
->parent
) {
4622 path
-= strlen(parent_resource
->name
);
4623 memcpy(path
, parent_resource
->name
,
4624 strlen(parent_resource
->name
));
4625 path
-= strlen(del
);
4626 memcpy(path
, del
, strlen(del
));
4628 pr_out_str(dl
, "resource_path", path
);
4632 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4634 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4635 struct dpipe_table
*table
;
4636 uint32_t resource_units
;
4637 bool counters_enabled
;
4638 bool resource_valid
;
4642 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4643 if (err
!= MNL_CB_OK
)
4646 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4647 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4648 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4649 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4650 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4654 table
= dpipe_table_alloc();
4658 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4659 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4660 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4662 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4664 if (resource_valid
) {
4665 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4666 table
->resource_valid
= true;
4669 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4670 if (!ctx
->print_tables
)
4673 pr_out_str(ctx
->dl
, "name", table
->name
);
4674 pr_out_uint(ctx
->dl
, "size", size
);
4675 pr_out_str(ctx
->dl
, "counters_enabled",
4676 counters_enabled
? "true" : "false");
4678 if (resource_valid
) {
4679 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4680 resource_path_print(ctx
->dl
, ctx
->resources
,
4681 table
->resource_id
);
4682 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4685 pr_out_array_start(ctx
->dl
, "match");
4686 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4687 goto err_matches_show
;
4688 pr_out_array_end(ctx
->dl
);
4690 pr_out_array_start(ctx
->dl
, "action");
4691 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4692 goto err_actions_show
;
4693 pr_out_array_end(ctx
->dl
);
4699 pr_out_array_end(ctx
->dl
);
4703 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4705 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4706 struct nlattr
*nla_table
;
4708 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4709 if (ctx
->print_tables
)
4710 pr_out_handle_start_arr(ctx
->dl
, tb
);
4711 if (dpipe_table_show(ctx
, nla_table
))
4712 goto err_table_show
;
4713 if (ctx
->print_tables
)
4714 pr_out_handle_end(ctx
->dl
);
4719 if (ctx
->print_tables
)
4720 pr_out_handle_end(ctx
->dl
);
4724 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4726 struct dpipe_ctx
*ctx
= data
;
4727 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4728 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4730 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4731 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4732 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4733 return MNL_CB_ERROR
;
4735 if (dpipe_tables_show(ctx
, tb
))
4736 return MNL_CB_ERROR
;
4740 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4742 static int cmd_dpipe_table_show(struct dl
*dl
)
4744 struct nlmsghdr
*nlh
;
4745 struct dpipe_ctx dpipe_ctx
= {};
4746 struct resource_ctx resource_ctx
= {};
4747 uint16_t flags
= NLM_F_REQUEST
;
4750 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4754 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4756 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4760 dpipe_ctx
.print_tables
= true;
4762 dl_opts_put(nlh
, dl
);
4763 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4766 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4767 goto err_headers_get
;
4770 err
= resource_ctx_init(&resource_ctx
, dl
);
4772 goto err_resource_ctx_init
;
4774 resource_ctx
.print_resources
= false;
4775 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4776 dl_opts_put(nlh
, dl
);
4777 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4780 dpipe_ctx
.resources
= resource_ctx
.resources
;
4782 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4783 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4784 dl_opts_put(nlh
, dl
);
4786 pr_out_section_start(dl
, "table");
4787 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4788 pr_out_section_end(dl
);
4790 resource_ctx_fini(&resource_ctx
);
4791 dpipe_ctx_fini(&dpipe_ctx
);
4794 err_resource_ctx_init
:
4796 dpipe_ctx_fini(&dpipe_ctx
);
4800 static int cmd_dpipe_table_set(struct dl
*dl
)
4802 struct nlmsghdr
*nlh
;
4805 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4806 NLM_F_REQUEST
| NLM_F_ACK
);
4808 err
= dl_argv_parse_put(nlh
, dl
,
4809 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4810 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4814 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4817 enum dpipe_value_type
{
4818 DPIPE_VALUE_TYPE_VALUE
,
4819 DPIPE_VALUE_TYPE_MASK
,
4823 dpipe_value_type_e2s(enum dpipe_value_type type
)
4826 case DPIPE_VALUE_TYPE_VALUE
:
4828 case DPIPE_VALUE_TYPE_MASK
:
4829 return "value_mask";
4835 struct dpipe_field_printer
{
4836 unsigned int field_id
;
4837 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4840 struct dpipe_header_printer
{
4841 struct dpipe_field_printer
*printers
;
4842 unsigned int printers_count
;
4843 unsigned int header_id
;
4846 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4847 enum dpipe_value_type type
,
4850 struct in_addr ip_addr
;
4852 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4853 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4857 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4858 enum dpipe_value_type type
,
4861 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4862 ether_ntoa((struct ether_addr
*)value
));
4865 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4866 enum dpipe_value_type type
,
4869 char str
[INET6_ADDRSTRLEN
];
4871 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4872 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4875 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4877 .printer
= dpipe_field_printer_ipv4_addr
,
4878 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4882 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4883 .printers
= dpipe_field_printers_ipv4
,
4884 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4885 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4888 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4890 .printer
= dpipe_field_printer_ethernet_addr
,
4891 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4895 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4896 .printers
= dpipe_field_printers_ethernet
,
4897 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4898 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4901 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4903 .printer
= dpipe_field_printer_ipv6_addr
,
4904 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4908 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4909 .printers
= dpipe_field_printers_ipv6
,
4910 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4911 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4914 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4915 &dpipe_header_printer_ipv4
,
4916 &dpipe_header_printer_ethernet
,
4917 &dpipe_header_printer_ipv6
,
4920 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4921 struct dpipe_op_info
*info
,
4922 enum dpipe_value_type type
,
4925 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4926 struct dpipe_header_printer
*header_printer
;
4927 struct dpipe_field_printer
*field_printer
;
4928 unsigned int field_printers_count
;
4932 for (i
= 0; i
< header_printers_count
; i
++) {
4933 header_printer
= dpipe_header_printers
[i
];
4934 if (header_printer
->header_id
!= info
->header_id
)
4936 field_printers_count
= header_printer
->printers_count
;
4937 for (j
= 0; j
< field_printers_count
; j
++) {
4938 field_printer
= &header_printer
->printers
[j
];
4939 if (field_printer
->field_id
!= info
->field_id
)
4941 field_printer
->printer(ctx
, type
, value
);
4949 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4951 unsigned int value_len
,
4952 struct dpipe_op_info
*info
,
4953 enum dpipe_value_type type
)
4955 if (info
->header_global
&&
4956 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4959 if (value_len
== sizeof(uint32_t)) {
4960 uint32_t *value_32
= value
;
4962 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4966 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4967 struct nlattr
**nla_match_value
,
4968 struct dpipe_op_info
*info
)
4970 void *value
, *value_mask
;
4971 uint32_t value_mapping
;
4975 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4976 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4978 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4979 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4982 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4983 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4987 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4988 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4989 DPIPE_VALUE_TYPE_MASK
);
4992 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4995 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4998 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4999 struct dpipe_match match
;
5002 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5003 if (err
!= MNL_CB_OK
)
5006 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5007 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5011 pr_out_entry_start(ctx
->dl
);
5012 if (dpipe_match_parse(&match
,
5013 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5014 goto err_match_parse
;
5015 pr_out_dpipe_match(&match
, ctx
);
5016 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5017 pr_out_entry_end(ctx
->dl
);
5022 pr_out_entry_end(ctx
->dl
);
5026 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5029 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5030 struct dpipe_action action
;
5033 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5034 if (err
!= MNL_CB_OK
)
5037 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5038 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5042 pr_out_entry_start(ctx
->dl
);
5043 if (dpipe_action_parse(&action
,
5044 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5045 goto err_action_parse
;
5046 pr_out_dpipe_action(&action
, ctx
);
5047 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5048 pr_out_entry_end(ctx
->dl
);
5053 pr_out_entry_end(ctx
->dl
);
5058 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5059 struct nlattr
*nla_action_values
)
5061 struct nlattr
*nla_action_value
;
5063 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5064 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5071 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5072 struct nlattr
*nla_match_values
)
5074 struct nlattr
*nla_match_value
;
5076 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5077 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5083 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5085 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5086 uint32_t entry_index
;
5090 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5091 if (err
!= MNL_CB_OK
)
5094 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5095 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5096 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5100 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5101 pr_out_uint(ctx
->dl
, "index", entry_index
);
5103 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5104 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5105 pr_out_uint(ctx
->dl
, "counter", counter
);
5108 pr_out_array_start(ctx
->dl
, "match_value");
5109 if (dpipe_tables_match_values_show(ctx
,
5110 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5111 goto err_match_values_show
;
5112 pr_out_array_end(ctx
->dl
);
5114 pr_out_array_start(ctx
->dl
, "action_value");
5115 if (dpipe_tables_action_values_show(ctx
,
5116 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5117 goto err_action_values_show
;
5118 pr_out_array_end(ctx
->dl
);
5121 err_action_values_show
:
5122 err_match_values_show
:
5123 pr_out_array_end(ctx
->dl
);
5127 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5129 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5130 struct nlattr
*nla_entry
;
5132 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5133 pr_out_handle_start_arr(ctx
->dl
, tb
);
5134 if (dpipe_entry_show(ctx
, nla_entry
))
5135 goto err_entry_show
;
5136 pr_out_handle_end(ctx
->dl
);
5141 pr_out_handle_end(ctx
->dl
);
5145 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5147 struct dpipe_ctx
*ctx
= data
;
5148 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5149 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5151 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5152 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5153 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5154 return MNL_CB_ERROR
;
5156 if (dpipe_table_entries_show(ctx
, tb
))
5157 return MNL_CB_ERROR
;
5161 static int cmd_dpipe_table_dump(struct dl
*dl
)
5163 struct nlmsghdr
*nlh
;
5164 struct dpipe_ctx ctx
= {};
5165 uint16_t flags
= NLM_F_REQUEST
;
5168 err
= dpipe_ctx_init(&ctx
, dl
);
5172 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5176 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5177 dl_opts_put(nlh
, dl
);
5178 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5180 pr_err("error get headers %s\n", strerror(ctx
.err
));
5184 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5185 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5186 dl_opts_put(nlh
, dl
);
5188 pr_out_section_start(dl
, "table_entry");
5189 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5190 pr_out_section_end(dl
);
5192 dpipe_ctx_fini(&ctx
);
5196 static void cmd_dpipe_table_help(void)
5198 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5199 "where OBJECT-LIST := { show | set | dump }\n");
5202 static int cmd_dpipe_table(struct dl
*dl
)
5204 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5205 cmd_dpipe_table_help();
5207 } else if (dl_argv_match(dl
, "show")) {
5209 return cmd_dpipe_table_show(dl
);
5210 } else if (dl_argv_match(dl
, "set")) {
5212 return cmd_dpipe_table_set(dl
);
5213 } else if (dl_argv_match(dl
, "dump")) {
5215 return cmd_dpipe_table_dump(dl
);
5217 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5221 static void cmd_dpipe_help(void)
5223 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5224 "where OBJECT-LIST := { header | table }\n");
5227 static int cmd_dpipe(struct dl
*dl
)
5229 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5232 } else if (dl_argv_match(dl
, "header")) {
5234 return cmd_dpipe_header(dl
);
5235 } else if (dl_argv_match(dl
, "table")) {
5237 return cmd_dpipe_table(dl
);
5239 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5244 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5245 struct nlattr
**nla_resource
)
5247 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5248 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5249 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5250 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5251 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5252 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5253 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5257 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5258 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5259 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5260 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5261 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5262 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5263 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5265 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5266 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5268 resource
->size_new
= resource
->size
;
5270 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5271 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5272 resource
->occ_valid
= true;
5275 if (resource
->size_new
!= resource
->size
)
5276 ctx
->pending_change
= true;
5282 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5283 struct resource
*parent_resource
, struct nlattr
*nl
)
5285 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5286 struct nlattr
*nla_child_resource
;
5287 struct nlattr
*nla_resources
;
5297 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5298 if (err
!= MNL_CB_OK
)
5301 err
= resource_parse(ctx
, resource
, nla_resource
);
5305 resource
->parent
= parent_resource
;
5306 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5309 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5310 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5312 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5313 struct resource
*child_resource
;
5314 struct list_head
*list
;
5316 child_resource
= resource_alloc();
5317 if (!child_resource
)
5321 list
= &ctx
->resources
->resource_list
;
5323 list
= &resource
->resource_list
;
5325 list_add_tail(&child_resource
->list
, list
);
5326 err
= resource_get(ctx
, child_resource
, resource
,
5327 nla_child_resource
);
5335 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5338 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5339 default: return "<unknown unit>";
5343 static void resource_show(struct resource
*resource
,
5344 struct resource_ctx
*ctx
)
5346 struct resource
*child_resource
;
5347 struct dpipe_table
*table
;
5348 struct dl
*dl
= ctx
->dl
;
5351 pr_out_str(dl
, "name", resource
->name
);
5353 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5354 pr_out_u64(dl
, "size", resource
->size
);
5355 if (resource
->size
!= resource
->size_new
)
5356 pr_out_u64(dl
, "size_new", resource
->size_new
);
5357 if (resource
->occ_valid
)
5358 pr_out_uint(dl
, "occ", resource
->size_occ
);
5359 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5361 if (resource
->size_min
!= resource
->size_max
) {
5362 pr_out_uint(dl
, "size_min", resource
->size_min
);
5363 pr_out_u64(dl
, "size_max", resource
->size_max
);
5364 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5367 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5368 if (table
->resource_id
== resource
->id
&&
5369 table
->resource_valid
)
5373 pr_out_array_start(dl
, "dpipe_tables");
5375 pr_out_str(dl
, "dpipe_tables", "none");
5377 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5378 if (table
->resource_id
!= resource
->id
||
5379 !table
->resource_valid
)
5381 pr_out_entry_start(dl
);
5382 pr_out_str(dl
, "table_name", table
->name
);
5383 pr_out_entry_end(dl
);
5386 pr_out_array_end(dl
);
5388 if (list_empty(&resource
->resource_list
))
5391 if (ctx
->pending_change
)
5392 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5394 pr_out_array_start(dl
, "resources");
5395 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5396 pr_out_entry_start(dl
);
5397 resource_show(child_resource
, ctx
);
5398 pr_out_entry_end(dl
);
5400 pr_out_array_end(dl
);
5404 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5406 struct resources
*resources
= ctx
->resources
;
5407 struct resource
*resource
;
5409 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5410 pr_out_handle_start_arr(ctx
->dl
, tb
);
5411 resource_show(resource
, ctx
);
5412 pr_out_handle_end(ctx
->dl
);
5416 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5418 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5421 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5423 struct resource_ctx
*ctx
= data
;
5424 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5425 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5428 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5429 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5430 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5431 return MNL_CB_ERROR
;
5433 err
= resources_get(ctx
, tb
);
5436 return MNL_CB_ERROR
;
5439 if (ctx
->print_resources
)
5440 resources_show(ctx
, tb
);
5445 static int cmd_resource_show(struct dl
*dl
)
5447 struct nlmsghdr
*nlh
;
5448 struct dpipe_ctx dpipe_ctx
= {};
5449 struct resource_ctx resource_ctx
= {};
5452 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5456 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5458 dl_opts_put(nlh
, dl
);
5460 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5464 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5467 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5471 err
= resource_ctx_init(&resource_ctx
, dl
);
5475 resource_ctx
.print_resources
= true;
5476 resource_ctx
.tables
= dpipe_ctx
.tables
;
5477 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5478 NLM_F_REQUEST
| NLM_F_ACK
);
5479 dl_opts_put(nlh
, dl
);
5480 pr_out_section_start(dl
, "resources");
5481 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5483 pr_out_section_end(dl
);
5484 resource_ctx_fini(&resource_ctx
);
5486 dpipe_ctx_fini(&dpipe_ctx
);
5490 static void cmd_resource_help(void)
5492 pr_err("Usage: devlink resource show DEV\n"
5493 " devlink resource set DEV path PATH size SIZE\n");
5496 static struct resource
*
5497 resource_find_by_name(struct list_head
*list
, char *name
)
5499 struct resource
*resource
;
5501 list_for_each_entry(resource
, list
, list
) {
5502 if (!strcmp(resource
->name
, name
))
5509 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5510 uint32_t *p_resource_id
, bool *p_resource_valid
)
5512 struct resource
*resource
;
5513 uint32_t resource_id
= 0;
5514 char *resource_path_dup
;
5515 struct list_head
*list
;
5516 const char del
[] = "/";
5517 char *resource_name
;
5519 resource_path_dup
= strdup(resource_path
);
5520 list
= &ctx
->resources
->resource_list
;
5521 resource_name
= strtok(resource_path_dup
, del
);
5522 while (resource_name
!= NULL
) {
5523 resource
= resource_find_by_name(list
, resource_name
);
5525 goto err_resource_lookup
;
5527 list
= &resource
->resource_list
;
5528 resource_name
= strtok(NULL
, del
);
5529 resource_id
= resource
->id
;
5531 free(resource_path_dup
);
5532 *p_resource_valid
= true;
5533 *p_resource_id
= resource_id
;
5536 err_resource_lookup
:
5537 free(resource_path_dup
);
5541 static int cmd_resource_set(struct dl
*dl
)
5543 struct nlmsghdr
*nlh
;
5544 struct resource_ctx ctx
= {};
5547 err
= resource_ctx_init(&ctx
, dl
);
5551 ctx
.print_resources
= false;
5552 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5553 DL_OPT_RESOURCE_SIZE
, 0);
5557 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5559 dl_opts_put(nlh
, dl
);
5560 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5562 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5566 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5567 &dl
->opts
.resource_id
,
5568 &dl
->opts
.resource_id_valid
);
5570 pr_err("error parsing resource path %s\n", strerror(-err
));
5574 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5575 NLM_F_REQUEST
| NLM_F_ACK
);
5577 dl_opts_put(nlh
, dl
);
5578 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5580 resource_ctx_fini(&ctx
);
5584 static int cmd_resource(struct dl
*dl
)
5586 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5587 cmd_resource_help();
5589 } else if (dl_argv_match(dl
, "show")) {
5591 return cmd_resource_show(dl
);
5592 } else if (dl_argv_match(dl
, "set")) {
5594 return cmd_resource_set(dl
);
5596 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5600 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5602 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5603 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5604 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5607 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5608 if (dl
->json_output
) {
5609 jsonw_name(dl
->jw
, buf
);
5610 jsonw_start_object(dl
->jw
);
5616 static void pr_out_region_handle_end(struct dl
*dl
)
5618 if (dl
->json_output
)
5619 jsonw_end_object(dl
->jw
);
5624 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5626 if (dl
->json_output
) {
5627 jsonw_name(dl
->jw
, "snapshot");
5628 jsonw_start_array(dl
->jw
);
5630 if (g_indent_newline
)
5631 pr_out("snapshot %s", array
? "[" : "");
5633 pr_out(" snapshot %s", array
? "[" : "");
5637 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5639 if (dl
->json_output
)
5640 jsonw_end_array(dl
->jw
);
5645 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5647 uint32_t snapshot_id
;
5649 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5652 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5654 if (dl
->json_output
)
5655 jsonw_uint(dl
->jw
, snapshot_id
);
5657 pr_out("%s%u", index
? " " : "", snapshot_id
);
5660 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5662 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5663 struct nlattr
*nla_sanpshot
;
5666 pr_out_region_snapshots_start(dl
, true);
5667 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5668 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5669 if (err
!= MNL_CB_OK
)
5671 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5673 pr_out_region_snapshots_end(dl
, true);
5676 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5678 pr_out_region_snapshots_start(dl
, false);
5679 pr_out_region_snapshots_id(dl
, tb
, 0);
5680 pr_out_region_snapshots_end(dl
, false);
5683 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5685 pr_out_region_handle_start(dl
, tb
);
5687 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5688 pr_out_u64(dl
, "size",
5689 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5691 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5692 pr_out_snapshots(dl
, tb
);
5694 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5695 pr_out_snapshot(dl
, tb
);
5697 pr_out_region_handle_end(dl
);
5700 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5702 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5703 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5704 struct dl
*dl
= data
;
5706 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5707 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5708 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5709 return MNL_CB_ERROR
;
5711 pr_out_region(dl
, tb
);
5716 static int cmd_region_show(struct dl
*dl
)
5718 struct nlmsghdr
*nlh
;
5719 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5722 if (dl_argc(dl
) == 0)
5723 flags
|= NLM_F_DUMP
;
5725 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5727 if (dl_argc(dl
) > 0) {
5728 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5733 pr_out_section_start(dl
, "regions");
5734 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5735 pr_out_section_end(dl
);
5739 static int cmd_region_snapshot_del(struct dl
*dl
)
5741 struct nlmsghdr
*nlh
;
5744 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5745 NLM_F_REQUEST
| NLM_F_ACK
);
5747 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5748 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5752 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5755 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5757 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5758 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5759 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5760 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5761 struct dl
*dl
= data
;
5764 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5765 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5766 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5767 return MNL_CB_ERROR
;
5769 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5770 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5771 if (err
!= MNL_CB_OK
)
5772 return MNL_CB_ERROR
;
5774 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5775 if (!nla_chunk_data
)
5778 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5779 if (!nla_chunk_addr
)
5782 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5783 mnl_attr_get_payload_len(nla_chunk_data
),
5784 mnl_attr_get_u64(nla_chunk_addr
));
5789 static int cmd_region_dump(struct dl
*dl
)
5791 struct nlmsghdr
*nlh
;
5794 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5795 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5797 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5798 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5802 pr_out_section_start(dl
, "dump");
5803 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5804 pr_out_section_end(dl
);
5805 if (!dl
->json_output
)
5810 static int cmd_region_read(struct dl
*dl
)
5812 struct nlmsghdr
*nlh
;
5815 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5816 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5818 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5819 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5820 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5824 pr_out_section_start(dl
, "read");
5825 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5826 pr_out_section_end(dl
);
5827 if (!dl
->json_output
)
5832 static void cmd_region_help(void)
5834 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5835 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5836 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5837 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5840 static int cmd_region(struct dl
*dl
)
5842 if (dl_no_arg(dl
)) {
5843 return cmd_region_show(dl
);
5844 } else if (dl_argv_match(dl
, "help")) {
5847 } else if (dl_argv_match(dl
, "show")) {
5849 return cmd_region_show(dl
);
5850 } else if (dl_argv_match(dl
, "del")) {
5852 return cmd_region_snapshot_del(dl
);
5853 } else if (dl_argv_match(dl
, "dump")) {
5855 return cmd_region_dump(dl
);
5856 } else if (dl_argv_match(dl
, "read")) {
5858 return cmd_region_read(dl
);
5860 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5864 static int cmd_health_set_params(struct dl
*dl
)
5866 struct nlmsghdr
*nlh
;
5869 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
5870 NLM_F_REQUEST
| NLM_F_ACK
);
5871 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
5872 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
5873 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
5877 dl_opts_put(nlh
, dl
);
5878 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5881 static int cmd_health_dump_clear(struct dl
*dl
)
5883 struct nlmsghdr
*nlh
;
5886 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
5887 NLM_F_REQUEST
| NLM_F_ACK
);
5889 err
= dl_argv_parse_put(nlh
, dl
,
5890 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
5894 dl_opts_put(nlh
, dl
);
5895 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5898 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
5905 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
5908 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
5911 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
5914 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
5917 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
5919 case MNL_TYPE_NUL_STRING
:
5920 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
5922 case MNL_TYPE_BINARY
:
5923 len
= mnl_attr_get_payload_len(nl_data
);
5924 data
= mnl_attr_get_payload(nl_data
);
5925 pr_out_binary_value(dl
, data
, len
);
5933 struct nest_qentry
{
5935 TAILQ_ENTRY(nest_qentry
) nest_entries
;
5938 struct fmsg_cb_data
{
5941 TAILQ_HEAD(, nest_qentry
) qhead
;
5944 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
5945 uint8_t *attr_value
, bool insert
)
5947 struct nest_qentry
*entry
= NULL
;
5950 entry
= malloc(sizeof(struct nest_qentry
));
5954 entry
->attr_type
= *attr_value
;
5955 TAILQ_INSERT_HEAD(&fmsg_data
->qhead
, entry
, nest_entries
);
5957 if (TAILQ_EMPTY(&fmsg_data
->qhead
))
5958 return MNL_CB_ERROR
;
5959 entry
= TAILQ_FIRST(&fmsg_data
->qhead
);
5960 *attr_value
= entry
->attr_type
;
5961 TAILQ_REMOVE(&fmsg_data
->qhead
, entry
, nest_entries
);
5967 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
5970 struct dl
*dl
= fmsg_data
->dl
;
5971 uint8_t value
= nest_value
;
5974 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
5975 if (err
!= MNL_CB_OK
)
5979 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
5981 pr_out_entry_start(dl
);
5983 pr_out_entry_end(dl
);
5985 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
5987 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
5988 if (dl
->json_output
) {
5990 jsonw_start_array(dl
->jw
);
5992 jsonw_end_array(dl
->jw
);
5996 __pr_out_indent_inc();
5998 __pr_out_indent_dec();
6008 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6010 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6011 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6012 struct fmsg_cb_data
*fmsg_data
= data
;
6013 struct dl
*dl
= fmsg_data
->dl
;
6014 struct nlattr
*nla_object
;
6018 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6019 if (!tb
[DEVLINK_ATTR_FMSG
])
6020 return MNL_CB_ERROR
;
6022 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6023 attr_type
= mnl_attr_get_type(nla_object
);
6024 switch (attr_type
) {
6025 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6026 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6027 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6028 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6029 if (err
!= MNL_CB_OK
)
6032 case DEVLINK_ATTR_FMSG_NEST_END
:
6033 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6034 if (err
!= MNL_CB_OK
)
6037 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6038 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6040 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6041 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6043 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6044 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6046 if (err
!= MNL_CB_OK
)
6056 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
)
6058 struct fmsg_cb_data data
;
6059 struct nlmsghdr
*nlh
;
6062 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, NLM_F_REQUEST
| NLM_F_ACK
);
6064 err
= dl_argv_parse_put(nlh
, dl
,
6065 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6070 TAILQ_INIT(&data
.qhead
);
6071 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6075 static int cmd_health_dump_show(struct dl
*dl
)
6077 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
6080 static int cmd_health_diagnose(struct dl
*dl
)
6082 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
);
6085 static int cmd_health_recover(struct dl
*dl
)
6087 struct nlmsghdr
*nlh
;
6090 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6091 NLM_F_REQUEST
| NLM_F_ACK
);
6093 err
= dl_argv_parse_put(nlh
, dl
,
6094 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6098 dl_opts_put(nlh
, dl
);
6099 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6102 enum devlink_health_reporter_state
{
6103 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6104 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6107 static const char *health_state_name(uint8_t state
)
6110 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6111 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6112 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6113 return HEALTH_REPORTER_STATE_ERROR_STR
;
6115 return "<unknown state>";
6119 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6121 struct sysinfo s_info
;
6127 info
= localtime(&now
);
6128 err
= sysinfo(&s_info
);
6131 /* Subtract uptime in sec from now yields the time of system
6132 * uptime. To this, add time_ms which is the amount of
6133 * milliseconds elapsed between uptime and the dump taken.
6135 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6136 info
= localtime(&sec
);
6138 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6139 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6142 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6144 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6145 enum devlink_health_reporter_state state
;
6146 const struct nlattr
*attr
;
6150 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6152 if (err
!= MNL_CB_OK
)
6155 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6156 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6157 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6158 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6161 pr_out_handle_start_arr(dl
, tb_health
);
6163 pr_out_str(dl
, "name",
6164 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6165 if (!dl
->json_output
) {
6167 __pr_out_indent_inc();
6169 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6170 pr_out_str(dl
, "state", health_state_name(state
));
6171 pr_out_u64(dl
, "error",
6172 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6173 pr_out_u64(dl
, "recover",
6174 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6175 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6176 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6177 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6179 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6180 time_ms
= mnl_attr_get_u64(attr
);
6181 format_logtime(time_ms
, dump_date
, dump_time
);
6183 pr_out_str(dl
, "last_dump_date", dump_date
);
6184 pr_out_str(dl
, "last_dump_time", dump_time
);
6186 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6187 pr_out_u64(dl
, "grace_period",
6188 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6189 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6190 pr_out_bool(dl
, "auto_recover",
6191 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6193 __pr_out_indent_dec();
6194 pr_out_handle_end(dl
);
6197 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6199 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6200 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6201 struct dl
*dl
= data
;
6203 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6204 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6205 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6206 return MNL_CB_ERROR
;
6208 pr_out_health(dl
, tb
);
6213 static int cmd_health_show(struct dl
*dl
)
6215 struct nlmsghdr
*nlh
;
6216 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6219 if (dl_argc(dl
) == 0)
6220 flags
|= NLM_F_DUMP
;
6221 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6224 if (dl_argc(dl
) > 0) {
6225 err
= dl_argv_parse_put(nlh
, dl
,
6227 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6231 pr_out_section_start(dl
, "health");
6233 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6234 pr_out_section_end(dl
);
6238 static void cmd_health_help(void)
6240 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6241 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6242 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6243 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6244 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6245 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6248 static int cmd_health(struct dl
*dl
)
6250 if (dl_argv_match(dl
, "help")) {
6253 } else if (dl_argv_match(dl
, "show") ||
6254 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6256 return cmd_health_show(dl
);
6257 } else if (dl_argv_match(dl
, "recover")) {
6259 return cmd_health_recover(dl
);
6260 } else if (dl_argv_match(dl
, "diagnose")) {
6262 return cmd_health_diagnose(dl
);
6263 } else if (dl_argv_match(dl
, "dump")) {
6265 if (dl_argv_match(dl
, "show")) {
6267 return cmd_health_dump_show(dl
);
6268 } else if (dl_argv_match(dl
, "clear")) {
6270 return cmd_health_dump_clear(dl
);
6272 } else if (dl_argv_match(dl
, "set")) {
6274 return cmd_health_set_params(dl
);
6276 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6280 static void help(void)
6282 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
6283 " devlink [ -f[orce] ] -b[atch] filename\n"
6284 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health }\n"
6285 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
6288 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
6293 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6296 } else if (dl_argv_match(dl
, "dev")) {
6299 } else if (dl_argv_match(dl
, "port")) {
6301 return cmd_port(dl
);
6302 } else if (dl_argv_match(dl
, "sb")) {
6305 } else if (dl_argv_match(dl
, "monitor")) {
6308 } else if (dl_argv_match(dl
, "dpipe")) {
6310 return cmd_dpipe(dl
);
6311 } else if (dl_argv_match(dl
, "resource")) {
6313 return cmd_resource(dl
);
6314 } else if (dl_argv_match(dl
, "region")) {
6316 return cmd_region(dl
);
6317 } else if (dl_argv_match(dl
, "health")) {
6319 return cmd_health(dl
);
6321 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
6325 static int dl_init(struct dl
*dl
)
6329 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
6331 pr_err("Failed to connect to devlink Netlink\n");
6335 err
= ifname_map_init(dl
);
6337 pr_err("Failed to create index map\n");
6338 goto err_ifname_map_create
;
6340 if (dl
->json_output
) {
6341 dl
->jw
= jsonw_new(stdout
);
6343 pr_err("Failed to create JSON writer\n");
6346 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
6351 ifname_map_fini(dl
);
6352 err_ifname_map_create
:
6353 mnlg_socket_close(dl
->nlg
);
6357 static void dl_fini(struct dl
*dl
)
6359 if (dl
->json_output
)
6360 jsonw_destroy(&dl
->jw
);
6361 ifname_map_fini(dl
);
6362 mnlg_socket_close(dl
->nlg
);
6365 static struct dl
*dl_alloc(void)
6369 dl
= calloc(1, sizeof(*dl
));
6375 static void dl_free(struct dl
*dl
)
6380 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
6384 int ret
= EXIT_SUCCESS
;
6386 if (name
&& strcmp(name
, "-") != 0) {
6387 if (freopen(name
, "r", stdin
) == NULL
) {
6389 "Cannot open file \"%s\" for reading: %s\n",
6390 name
, strerror(errno
));
6391 return EXIT_FAILURE
;
6396 while (getcmdline(&line
, &len
, stdin
) != -1) {
6400 largc
= makeargs(line
, largv
, 100);
6402 continue; /* blank line */
6404 if (dl_cmd(dl
, largc
, largv
)) {
6405 fprintf(stderr
, "Command failed %s:%d\n",
6419 int main(int argc
, char **argv
)
6421 static const struct option long_options
[] = {
6422 { "Version", no_argument
, NULL
, 'V' },
6423 { "force", no_argument
, NULL
, 'f' },
6424 { "batch", required_argument
, NULL
, 'b' },
6425 { "no-nice-names", no_argument
, NULL
, 'n' },
6426 { "json", no_argument
, NULL
, 'j' },
6427 { "pretty", no_argument
, NULL
, 'p' },
6428 { "verbose", no_argument
, NULL
, 'v' },
6429 { NULL
, 0, NULL
, 0 }
6431 const char *batch_file
= NULL
;
6440 pr_err("Failed to allocate memory for devlink\n");
6441 return EXIT_FAILURE
;
6444 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
6445 long_options
, NULL
)) >= 0) {
6449 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
6456 batch_file
= optarg
;
6459 dl
->no_nice_names
= true;
6462 dl
->json_output
= true;
6465 dl
->pretty_output
= true;
6471 pr_err("Unknown option.\n");
6488 err
= dl_batch(dl
, batch_file
, force
);
6490 err
= dl_cmd(dl
, argc
, argv
);