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>
29 #include "json_writer.h"
32 #define ESWITCH_MODE_LEGACY "legacy"
33 #define ESWITCH_MODE_SWITCHDEV "switchdev"
34 #define ESWITCH_INLINE_MODE_NONE "none"
35 #define ESWITCH_INLINE_MODE_LINK "link"
36 #define ESWITCH_INLINE_MODE_NETWORK "network"
37 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
39 #define PARAM_CMODE_RUNTIME_STR "runtime"
40 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
41 #define PARAM_CMODE_PERMANENT_STR "permanent"
42 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
44 static int g_new_line_count
;
46 #define pr_err(args...) fprintf(stderr, ##args)
47 #define pr_out(args...) \
49 if (g_indent_newline) { \
50 fprintf(stdout, "%s", g_indent_str); \
51 g_indent_newline = false; \
53 fprintf(stdout, ##args); \
54 g_new_line_count = 0; \
57 #define pr_out_sp(num, args...) \
59 int ret = fprintf(stdout, ##args); \
61 fprintf(stdout, "%*s", num - ret, ""); \
62 g_new_line_count = 0; \
65 static int g_indent_level
;
66 static bool g_indent_newline
;
67 #define INDENT_STR_STEP 2
68 #define INDENT_STR_MAXLEN 32
69 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
71 static void __pr_out_indent_inc(void)
73 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
75 g_indent_level
+= INDENT_STR_STEP
;
76 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
77 g_indent_str
[g_indent_level
] = '\0';
80 static void __pr_out_indent_dec(void)
82 if (g_indent_level
- INDENT_STR_STEP
< 0)
84 g_indent_level
-= INDENT_STR_STEP
;
85 g_indent_str
[g_indent_level
] = '\0';
88 static void __pr_out_newline(void)
90 if (g_new_line_count
< 1) {
92 g_indent_newline
= true;
97 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
98 mnl_cb_t data_cb
, void *data
)
102 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
104 pr_err("devlink answers: %s\n", strerror(errno
));
110 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
111 const struct nlmsghdr
*nlh
,
112 mnl_cb_t data_cb
, void *data
)
116 err
= mnlg_socket_send(nlg
, nlh
);
118 pr_err("Failed to call mnlg_socket_send\n");
121 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
124 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
125 const char *group_name
)
129 err
= mnlg_socket_group_add(nlg
, group_name
);
131 pr_err("Failed to call mnlg_socket_group_add\n");
138 struct list_head list
;
145 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
146 const char *dev_name
,
150 struct ifname_map
*ifname_map
;
152 ifname_map
= calloc(1, sizeof(*ifname_map
));
155 ifname_map
->bus_name
= strdup(bus_name
);
156 ifname_map
->dev_name
= strdup(dev_name
);
157 ifname_map
->port_index
= port_index
;
158 ifname_map
->ifname
= strdup(ifname
);
159 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
160 !ifname_map
->ifname
) {
161 free(ifname_map
->ifname
);
162 free(ifname_map
->dev_name
);
163 free(ifname_map
->bus_name
);
170 static void ifname_map_free(struct ifname_map
*ifname_map
)
172 free(ifname_map
->ifname
);
173 free(ifname_map
->dev_name
);
174 free(ifname_map
->bus_name
);
178 #define DL_OPT_HANDLE BIT(0)
179 #define DL_OPT_HANDLEP BIT(1)
180 #define DL_OPT_PORT_TYPE BIT(2)
181 #define DL_OPT_PORT_COUNT BIT(3)
182 #define DL_OPT_SB BIT(4)
183 #define DL_OPT_SB_POOL BIT(5)
184 #define DL_OPT_SB_SIZE BIT(6)
185 #define DL_OPT_SB_TYPE BIT(7)
186 #define DL_OPT_SB_THTYPE BIT(8)
187 #define DL_OPT_SB_TH BIT(9)
188 #define DL_OPT_SB_TC BIT(10)
189 #define DL_OPT_ESWITCH_MODE BIT(11)
190 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
191 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
192 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
193 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
194 #define DL_OPT_RESOURCE_PATH BIT(16)
195 #define DL_OPT_RESOURCE_SIZE BIT(17)
196 #define DL_OPT_PARAM_NAME BIT(18)
197 #define DL_OPT_PARAM_VALUE BIT(19)
198 #define DL_OPT_PARAM_CMODE BIT(20)
199 #define DL_OPT_HANDLE_REGION BIT(21)
200 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
201 #define DL_OPT_REGION_ADDRESS BIT(23)
202 #define DL_OPT_REGION_LENGTH BIT(24)
203 #define DL_OPT_FLASH_FILE_NAME BIT(25)
204 #define DL_OPT_FLASH_COMPONENT BIT(26)
207 uint32_t present
; /* flags of present items */
211 enum devlink_port_type port_type
;
214 uint16_t sb_pool_index
;
215 uint32_t sb_pool_size
;
216 enum devlink_sb_pool_type sb_pool_type
;
217 enum devlink_sb_threshold_type sb_pool_thtype
;
218 uint32_t sb_threshold
;
219 uint16_t sb_tc_index
;
220 enum devlink_eswitch_mode eswitch_mode
;
221 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
222 const char *dpipe_table_name
;
223 bool dpipe_counters_enable
;
224 bool eswitch_encap_mode
;
225 const char *resource_path
;
226 uint32_t resource_size
;
227 uint32_t resource_id
;
228 bool resource_id_valid
;
229 const char *param_name
;
230 const char *param_value
;
231 enum devlink_param_cmode cmode
;
233 uint32_t region_snapshot_id
;
234 uint64_t region_address
;
235 uint64_t region_length
;
236 const char *flash_file_name
;
237 const char *flash_component
;
241 struct mnlg_socket
*nlg
;
242 struct list_head ifname_map_list
;
259 static int dl_argc(struct dl
*dl
)
264 static char *dl_argv(struct dl
*dl
)
266 if (dl_argc(dl
) == 0)
271 static void dl_arg_inc(struct dl
*dl
)
273 if (dl_argc(dl
) == 0)
279 static char *dl_argv_next(struct dl
*dl
)
283 if (dl_argc(dl
) == 0)
291 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
293 if (index
>= dl_argc(dl
))
295 return dl
->argv
[index
];
298 static int strcmpx(const char *str1
, const char *str2
)
300 if (strlen(str1
) > strlen(str2
))
302 return strncmp(str1
, str2
, strlen(str1
));
305 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
307 if (dl_argc(dl
) == 0)
309 return strcmpx(dl_argv(dl
), pattern
) == 0;
312 static bool dl_no_arg(struct dl
*dl
)
314 return dl_argc(dl
) == 0;
317 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
318 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
319 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
320 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
321 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
322 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
323 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
324 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
325 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
326 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
327 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
328 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
329 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
330 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
331 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
332 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
333 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
334 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
335 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
336 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
337 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
338 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
339 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
340 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
341 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
342 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
343 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
344 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
345 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
346 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
347 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
348 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
349 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
350 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
351 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
352 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
353 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
354 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
355 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
356 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
357 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
358 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
359 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
360 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
361 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
362 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
363 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
364 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
365 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
366 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
367 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
368 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
369 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
370 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
371 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
372 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
373 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
374 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
375 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
376 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
377 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
378 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
379 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
380 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
381 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
382 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
383 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
384 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
385 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
386 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
387 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
388 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
389 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
390 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
391 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
392 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
393 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
394 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
395 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
396 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
397 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
400 static int attr_cb(const struct nlattr
*attr
, void *data
)
402 const struct nlattr
**tb
= data
;
405 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
408 type
= mnl_attr_get_type(attr
);
409 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
416 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
418 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
419 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
420 struct dl
*dl
= data
;
421 struct ifname_map
*ifname_map
;
422 const char *bus_name
;
423 const char *dev_name
;
424 uint32_t port_ifindex
;
425 const char *port_ifname
;
427 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
428 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
429 !tb
[DEVLINK_ATTR_PORT_INDEX
])
432 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
435 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
436 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
437 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
438 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
439 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
440 port_ifindex
, port_ifname
);
443 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
448 static void ifname_map_fini(struct dl
*dl
)
450 struct ifname_map
*ifname_map
, *tmp
;
452 list_for_each_entry_safe(ifname_map
, tmp
,
453 &dl
->ifname_map_list
, list
) {
454 list_del(&ifname_map
->list
);
455 ifname_map_free(ifname_map
);
459 static int ifname_map_init(struct dl
*dl
)
461 struct nlmsghdr
*nlh
;
464 INIT_LIST_HEAD(&dl
->ifname_map_list
);
466 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
467 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
469 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
477 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
478 char **p_bus_name
, char **p_dev_name
,
479 uint32_t *p_port_index
)
481 struct ifname_map
*ifname_map
;
483 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
484 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
485 *p_bus_name
= ifname_map
->bus_name
;
486 *p_dev_name
= ifname_map
->dev_name
;
487 *p_port_index
= ifname_map
->port_index
;
494 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
495 const char *dev_name
, uint32_t port_index
,
498 struct ifname_map
*ifname_map
;
500 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
501 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
502 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
503 port_index
== ifname_map
->port_index
) {
504 *p_ifname
= ifname_map
->ifname
;
511 static unsigned int strslashcount(char *str
)
513 unsigned int count
= 0;
516 while ((pos
= strchr(pos
, '/'))) {
523 static int strslashrsplit(char *str
, char **before
, char **after
)
527 slash
= strrchr(str
, '/');
536 static int strtouint64_t(const char *str
, uint64_t *p_val
)
539 unsigned long long int val
;
541 val
= strtoull(str
, &endptr
, 10);
542 if (endptr
== str
|| *endptr
!= '\0')
550 static int strtouint32_t(const char *str
, uint32_t *p_val
)
553 unsigned long int val
;
555 val
= strtoul(str
, &endptr
, 10);
556 if (endptr
== str
|| *endptr
!= '\0')
564 static int strtouint16_t(const char *str
, uint16_t *p_val
)
567 unsigned long int val
;
569 val
= strtoul(str
, &endptr
, 10);
570 if (endptr
== str
|| *endptr
!= '\0')
578 static int strtouint8_t(const char *str
, uint8_t *p_val
)
581 unsigned long int val
;
583 val
= strtoul(str
, &endptr
, 10);
584 if (endptr
== str
|| *endptr
!= '\0')
592 static int strtobool(const char *str
, bool *p_val
)
596 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
598 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
606 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
608 strslashrsplit(str
, p_bus_name
, p_dev_name
);
612 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
614 char *str
= dl_argv_next(dl
);
617 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
620 if (strslashcount(str
) != 1) {
621 pr_err("Wrong devlink identification string format.\n");
622 pr_err("Expected \"bus_name/dev_name\".\n");
625 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
628 static int __dl_argv_handle_port(char *str
,
629 char **p_bus_name
, char **p_dev_name
,
630 uint32_t *p_port_index
)
636 err
= strslashrsplit(str
, &handlestr
, &portstr
);
638 pr_err("Port identification \"%s\" is invalid\n", str
);
641 err
= strtouint32_t(portstr
, p_port_index
);
643 pr_err("Port index \"%s\" is not a number or not within range\n",
647 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
649 pr_err("Port identification \"%s\" is invalid\n", str
);
655 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
656 char **p_bus_name
, char **p_dev_name
,
657 uint32_t *p_port_index
)
661 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
664 pr_err("Netdevice \"%s\" not found\n", str
);
670 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
671 char **p_dev_name
, uint32_t *p_port_index
)
673 char *str
= dl_argv_next(dl
);
674 unsigned int slash_count
;
677 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
680 slash_count
= strslashcount(str
);
681 switch (slash_count
) {
683 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
684 p_dev_name
, p_port_index
);
686 return __dl_argv_handle_port(str
, p_bus_name
,
687 p_dev_name
, p_port_index
);
689 pr_err("Wrong port identification string format.\n");
690 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
695 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
696 char **p_dev_name
, uint32_t *p_port_index
,
697 uint32_t *p_handle_bit
)
699 char *str
= dl_argv_next(dl
);
700 unsigned int slash_count
;
704 pr_err("One of following identifications expected:\n"
705 "Devlink identification (\"bus_name/dev_name\")\n"
706 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
709 slash_count
= strslashcount(str
);
710 if (slash_count
== 1) {
711 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
714 *p_handle_bit
= DL_OPT_HANDLE
;
715 } else if (slash_count
== 2) {
716 err
= __dl_argv_handle_port(str
, p_bus_name
,
717 p_dev_name
, p_port_index
);
720 *p_handle_bit
= DL_OPT_HANDLEP
;
721 } else if (slash_count
== 0) {
722 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
723 p_dev_name
, p_port_index
);
726 *p_handle_bit
= DL_OPT_HANDLEP
;
728 pr_err("Wrong port identification string format.\n");
729 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
735 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
736 char **p_dev_name
, char **p_region
)
741 err
= strslashrsplit(str
, &handlestr
, p_region
);
743 pr_err("Region identification \"%s\" is invalid\n", str
);
746 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
748 pr_err("Region identification \"%s\" is invalid\n", str
);
754 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
755 char **p_dev_name
, char **p_region
)
757 char *str
= dl_argv_next(dl
);
758 unsigned int slash_count
;
761 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
765 slash_count
= strslashcount(str
);
766 if (slash_count
!= 2) {
767 pr_err("Wrong region identification string format.\n");
768 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
772 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
775 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
777 char *str
= dl_argv_next(dl
);
781 pr_err("Unsigned number argument expected\n");
785 err
= strtouint64_t(str
, p_val
);
787 pr_err("\"%s\" is not a number or not within range\n", str
);
793 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
795 char *str
= dl_argv_next(dl
);
799 pr_err("Unsigned number argument expected\n");
803 err
= strtouint32_t(str
, p_val
);
805 pr_err("\"%s\" is not a number or not within range\n", str
);
811 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
813 char *str
= dl_argv_next(dl
);
817 pr_err("Unsigned number argument expected\n");
821 err
= strtouint16_t(str
, p_val
);
823 pr_err("\"%s\" is not a number or not within range\n", str
);
829 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
831 const char *str
= dl_argv_next(dl
);
834 pr_err("String parameter expected\n");
841 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
843 if (strcmp(typestr
, "auto") == 0) {
844 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
845 } else if (strcmp(typestr
, "eth") == 0) {
846 *p_type
= DEVLINK_PORT_TYPE_ETH
;
847 } else if (strcmp(typestr
, "ib") == 0) {
848 *p_type
= DEVLINK_PORT_TYPE_IB
;
850 pr_err("Unknown port type \"%s\"\n", typestr
);
856 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
858 if (strcmp(typestr
, "ingress") == 0) {
859 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
860 } else if (strcmp(typestr
, "egress") == 0) {
861 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
863 pr_err("Unknown pool type \"%s\"\n", typestr
);
869 static int threshold_type_get(const char *typestr
,
870 enum devlink_sb_threshold_type
*p_type
)
872 if (strcmp(typestr
, "static") == 0) {
873 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
874 } else if (strcmp(typestr
, "dynamic") == 0) {
875 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
877 pr_err("Unknown threshold type \"%s\"\n", typestr
);
883 static int eswitch_mode_get(const char *typestr
,
884 enum devlink_eswitch_mode
*p_mode
)
886 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
887 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
888 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
889 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
891 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
897 static int eswitch_inline_mode_get(const char *typestr
,
898 enum devlink_eswitch_inline_mode
*p_mode
)
900 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
901 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
902 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
903 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
904 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
905 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
906 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
907 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
909 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
915 static int dpipe_counters_enable_get(const char *typestr
,
916 bool *counters_enable
)
918 if (strcmp(typestr
, "enable") == 0) {
919 *counters_enable
= 1;
920 } else if (strcmp(typestr
, "disable") == 0) {
921 *counters_enable
= 0;
923 pr_err("Unknown counter_state \"%s\"\n", typestr
);
929 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
931 if (strcmp(typestr
, "enable") == 0) {
933 } else if (strcmp(typestr
, "disable") == 0) {
936 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
942 static int param_cmode_get(const char *cmodestr
,
943 enum devlink_param_cmode
*cmode
)
945 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
946 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
947 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
948 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
949 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
950 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
952 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
958 struct dl_args_metadata
{
960 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
963 static const struct dl_args_metadata dl_args_required
[] = {
964 {DL_OPT_PORT_TYPE
, "Port type not set."},
965 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
966 {DL_OPT_SB_POOL
, "Pool index option expected."},
967 {DL_OPT_SB_SIZE
, "Pool size option expected."},
968 {DL_OPT_SB_TYPE
, "Pool type option expected."},
969 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
970 {DL_OPT_SB_TH
, "Threshold option expected."},
971 {DL_OPT_SB_TC
, "TC index option expected."},
972 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
973 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
974 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
975 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
976 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
977 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
978 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
979 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
980 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
981 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
982 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
985 static int dl_args_finding_required_validate(uint32_t o_required
,
991 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
992 o_flag
= dl_args_required
[i
].o_flag
;
993 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
994 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1001 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
1002 uint32_t o_optional
)
1004 struct dl_opts
*opts
= &dl
->opts
;
1005 uint32_t o_all
= o_required
| o_optional
;
1006 uint32_t o_found
= 0;
1009 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1010 uint32_t handle_bit
;
1012 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1013 &opts
->port_index
, &handle_bit
);
1016 o_found
|= handle_bit
;
1017 } else if (o_required
& DL_OPT_HANDLE
) {
1018 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1021 o_found
|= DL_OPT_HANDLE
;
1022 } else if (o_required
& DL_OPT_HANDLEP
) {
1023 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1027 o_found
|= DL_OPT_HANDLEP
;
1028 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1029 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1031 &opts
->region_name
);
1034 o_found
|= DL_OPT_HANDLE_REGION
;
1037 while (dl_argc(dl
)) {
1038 if (dl_argv_match(dl
, "type") &&
1039 (o_all
& DL_OPT_PORT_TYPE
)) {
1040 const char *typestr
;
1043 err
= dl_argv_str(dl
, &typestr
);
1046 err
= port_type_get(typestr
, &opts
->port_type
);
1049 o_found
|= DL_OPT_PORT_TYPE
;
1050 } else if (dl_argv_match(dl
, "count") &&
1051 (o_all
& DL_OPT_PORT_COUNT
)) {
1053 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1056 o_found
|= DL_OPT_PORT_COUNT
;
1057 } else if (dl_argv_match(dl
, "sb") &&
1058 (o_all
& DL_OPT_SB
)) {
1060 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1063 o_found
|= DL_OPT_SB
;
1064 } else if (dl_argv_match(dl
, "pool") &&
1065 (o_all
& DL_OPT_SB_POOL
)) {
1067 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1070 o_found
|= DL_OPT_SB_POOL
;
1071 } else if (dl_argv_match(dl
, "size") &&
1072 (o_all
& DL_OPT_SB_SIZE
)) {
1074 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1077 o_found
|= DL_OPT_SB_SIZE
;
1078 } else if (dl_argv_match(dl
, "type") &&
1079 (o_all
& DL_OPT_SB_TYPE
)) {
1080 const char *typestr
;
1083 err
= dl_argv_str(dl
, &typestr
);
1086 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1089 o_found
|= DL_OPT_SB_TYPE
;
1090 } else if (dl_argv_match(dl
, "thtype") &&
1091 (o_all
& DL_OPT_SB_THTYPE
)) {
1092 const char *typestr
;
1095 err
= dl_argv_str(dl
, &typestr
);
1098 err
= threshold_type_get(typestr
,
1099 &opts
->sb_pool_thtype
);
1102 o_found
|= DL_OPT_SB_THTYPE
;
1103 } else if (dl_argv_match(dl
, "th") &&
1104 (o_all
& DL_OPT_SB_TH
)) {
1106 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1109 o_found
|= DL_OPT_SB_TH
;
1110 } else if (dl_argv_match(dl
, "tc") &&
1111 (o_all
& DL_OPT_SB_TC
)) {
1113 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1116 o_found
|= DL_OPT_SB_TC
;
1117 } else if (dl_argv_match(dl
, "mode") &&
1118 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1119 const char *typestr
;
1122 err
= dl_argv_str(dl
, &typestr
);
1125 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1128 o_found
|= DL_OPT_ESWITCH_MODE
;
1129 } else if (dl_argv_match(dl
, "inline-mode") &&
1130 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1131 const char *typestr
;
1134 err
= dl_argv_str(dl
, &typestr
);
1137 err
= eswitch_inline_mode_get(
1138 typestr
, &opts
->eswitch_inline_mode
);
1141 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1142 } else if (dl_argv_match(dl
, "name") &&
1143 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1145 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1148 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1149 } else if (dl_argv_match(dl
, "counters") &&
1150 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1151 const char *typestr
;
1154 err
= dl_argv_str(dl
, &typestr
);
1157 err
= dpipe_counters_enable_get(typestr
,
1158 &opts
->dpipe_counters_enable
);
1161 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1162 } else if (dl_argv_match(dl
, "encap") &&
1163 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1164 const char *typestr
;
1167 err
= dl_argv_str(dl
, &typestr
);
1170 err
= eswitch_encap_mode_get(typestr
,
1171 &opts
->eswitch_encap_mode
);
1174 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1175 } else if (dl_argv_match(dl
, "path") &&
1176 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1178 err
= dl_argv_str(dl
, &opts
->resource_path
);
1181 o_found
|= DL_OPT_RESOURCE_PATH
;
1182 } else if (dl_argv_match(dl
, "size") &&
1183 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1185 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1188 o_found
|= DL_OPT_RESOURCE_SIZE
;
1189 } else if (dl_argv_match(dl
, "name") &&
1190 (o_all
& DL_OPT_PARAM_NAME
)) {
1192 err
= dl_argv_str(dl
, &opts
->param_name
);
1195 o_found
|= DL_OPT_PARAM_NAME
;
1196 } else if (dl_argv_match(dl
, "value") &&
1197 (o_all
& DL_OPT_PARAM_VALUE
)) {
1199 err
= dl_argv_str(dl
, &opts
->param_value
);
1202 o_found
|= DL_OPT_PARAM_VALUE
;
1203 } else if (dl_argv_match(dl
, "cmode") &&
1204 (o_all
& DL_OPT_PARAM_CMODE
)) {
1205 const char *cmodestr
;
1208 err
= dl_argv_str(dl
, &cmodestr
);
1211 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1214 o_found
|= DL_OPT_PARAM_CMODE
;
1215 } else if (dl_argv_match(dl
, "snapshot") &&
1216 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1218 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1221 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1222 } else if (dl_argv_match(dl
, "address") &&
1223 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1225 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1228 o_found
|= DL_OPT_REGION_ADDRESS
;
1229 } else if (dl_argv_match(dl
, "length") &&
1230 (o_all
& DL_OPT_REGION_LENGTH
)) {
1232 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1235 o_found
|= DL_OPT_REGION_LENGTH
;
1236 } else if (dl_argv_match(dl
, "file") &&
1237 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1239 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1242 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1243 } else if (dl_argv_match(dl
, "component") &&
1244 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1246 err
= dl_argv_str(dl
, &opts
->flash_component
);
1249 o_found
|= DL_OPT_FLASH_COMPONENT
;
1251 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1256 opts
->present
= o_found
;
1258 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1260 opts
->present
|= DL_OPT_SB
;
1263 return dl_args_finding_required_validate(o_required
, o_found
);
1266 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1268 struct dl_opts
*opts
= &dl
->opts
;
1270 if (opts
->present
& DL_OPT_HANDLE
) {
1271 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1272 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1273 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1274 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1275 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1276 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1278 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1279 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1280 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1281 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1284 if (opts
->present
& DL_OPT_PORT_TYPE
)
1285 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1287 if (opts
->present
& DL_OPT_PORT_COUNT
)
1288 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1290 if (opts
->present
& DL_OPT_SB
)
1291 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1293 if (opts
->present
& DL_OPT_SB_POOL
)
1294 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1295 opts
->sb_pool_index
);
1296 if (opts
->present
& DL_OPT_SB_SIZE
)
1297 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1298 opts
->sb_pool_size
);
1299 if (opts
->present
& DL_OPT_SB_TYPE
)
1300 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1301 opts
->sb_pool_type
);
1302 if (opts
->present
& DL_OPT_SB_THTYPE
)
1303 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1304 opts
->sb_pool_thtype
);
1305 if (opts
->present
& DL_OPT_SB_TH
)
1306 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1307 opts
->sb_threshold
);
1308 if (opts
->present
& DL_OPT_SB_TC
)
1309 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1311 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1312 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1313 opts
->eswitch_mode
);
1314 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1315 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1316 opts
->eswitch_inline_mode
);
1317 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1318 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1319 opts
->dpipe_table_name
);
1320 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1321 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1322 opts
->dpipe_counters_enable
);
1323 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1324 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1325 opts
->eswitch_encap_mode
);
1326 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1327 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1329 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1330 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1331 opts
->resource_size
);
1332 if (opts
->present
& DL_OPT_PARAM_NAME
)
1333 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1335 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1336 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1338 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1339 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1340 opts
->region_snapshot_id
);
1341 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1342 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1343 opts
->region_address
);
1344 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1345 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1346 opts
->region_length
);
1347 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1348 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1349 opts
->flash_file_name
);
1350 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1351 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1352 opts
->flash_component
);
1355 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1356 uint32_t o_required
, uint32_t o_optional
)
1360 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1363 dl_opts_put(nlh
, dl
);
1367 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1369 struct dl_opts
*opts
= &dl
->opts
;
1370 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1371 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1372 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1373 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1375 if (opts
->present
& DL_OPT_HANDLE
&&
1376 attr_bus_name
&& attr_dev_name
) {
1377 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1378 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1380 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1381 strcmp(dev_name
, opts
->dev_name
) != 0)
1384 if (opts
->present
& DL_OPT_HANDLEP
&&
1385 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1386 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1387 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1388 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1390 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1391 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1392 port_index
!= opts
->port_index
)
1395 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1396 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1398 if (sb_index
!= opts
->sb_index
)
1404 static void cmd_dev_help(void)
1406 pr_err("Usage: devlink dev show [ DEV ]\n");
1407 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1408 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1409 pr_err(" [ encap { disable | enable } ]\n");
1410 pr_err(" devlink dev eswitch show DEV\n");
1411 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1412 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1413 pr_err(" devlink dev reload DEV\n");
1414 pr_err(" devlink dev info [ DEV ]\n");
1415 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1418 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1419 const char *dev_name
)
1421 if (!dl
->arr_last
.present
)
1423 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1424 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1427 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1428 const char *dev_name
)
1430 dl
->arr_last
.present
= true;
1431 free(dl
->arr_last
.dev_name
);
1432 free(dl
->arr_last
.bus_name
);
1433 dl
->arr_last
.bus_name
= strdup(bus_name
);
1434 dl
->arr_last
.dev_name
= strdup(dev_name
);
1437 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1438 const char *dev_name
)
1440 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1443 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1444 const char *dev_name
)
1446 return dl
->arr_last
.present
&&
1447 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1450 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1451 bool content
, bool array
)
1453 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1454 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1457 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1459 if (dl
->json_output
) {
1461 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1462 jsonw_end_array(dl
->jw
);
1463 if (should_arr_last_handle_start(dl
, bus_name
,
1465 jsonw_name(dl
->jw
, buf
);
1466 jsonw_start_array(dl
->jw
);
1467 jsonw_start_object(dl
->jw
);
1468 arr_last_handle_set(dl
, bus_name
, dev_name
);
1470 jsonw_start_object(dl
->jw
);
1473 jsonw_name(dl
->jw
, buf
);
1474 jsonw_start_object(dl
->jw
);
1478 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1479 __pr_out_indent_dec();
1480 if (should_arr_last_handle_start(dl
, bus_name
,
1482 pr_out("%s%s", buf
, content
? ":" : "");
1484 __pr_out_indent_inc();
1485 arr_last_handle_set(dl
, bus_name
, dev_name
);
1488 pr_out("%s%s", buf
, content
? ":" : "");
1493 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1495 __pr_out_handle_start(dl
, tb
, true, true);
1498 static void pr_out_handle_end(struct dl
*dl
)
1500 if (dl
->json_output
)
1501 jsonw_end_object(dl
->jw
);
1506 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1508 __pr_out_handle_start(dl
, tb
, false, false);
1509 pr_out_handle_end(dl
);
1512 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1513 const char *dev_name
, uint32_t port_index
)
1515 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1516 dl
->arr_last
.port_index
== port_index
;
1519 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1520 const char *dev_name
, uint32_t port_index
)
1522 arr_last_handle_set(dl
, bus_name
, dev_name
);
1523 dl
->arr_last
.port_index
= port_index
;
1526 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1527 const char *bus_name
,
1528 const char *dev_name
,
1529 uint32_t port_index
)
1531 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1534 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1535 const char *bus_name
,
1536 const char *dev_name
,
1537 uint32_t port_index
)
1539 return dl
->arr_last
.present
&&
1540 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1543 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1544 const char *dev_name
,
1545 uint32_t port_index
, bool try_nice
,
1548 static char buf
[32];
1549 char *ifname
= NULL
;
1551 if (dl
->no_nice_names
|| !try_nice
||
1552 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1553 port_index
, &ifname
) != 0)
1554 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1556 sprintf(buf
, "%s", ifname
);
1558 if (dl
->json_output
) {
1560 if (should_arr_last_port_handle_end(dl
, bus_name
,
1563 jsonw_end_array(dl
->jw
);
1564 if (should_arr_last_port_handle_start(dl
, bus_name
,
1567 jsonw_name(dl
->jw
, buf
);
1568 jsonw_start_array(dl
->jw
);
1569 jsonw_start_object(dl
->jw
);
1570 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1573 jsonw_start_object(dl
->jw
);
1576 jsonw_name(dl
->jw
, buf
);
1577 jsonw_start_object(dl
->jw
);
1584 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1586 const char *bus_name
;
1587 const char *dev_name
;
1588 uint32_t port_index
;
1590 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1591 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1592 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1593 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1596 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1598 const char *bus_name
;
1599 const char *dev_name
;
1600 uint32_t port_index
;
1602 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1603 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1604 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1605 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1608 static void pr_out_port_handle_end(struct dl
*dl
)
1610 if (dl
->json_output
)
1611 jsonw_end_object(dl
->jw
);
1617 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1619 if (dl
->json_output
) {
1620 jsonw_string_field(dl
->jw
, name
, val
);
1622 if (g_indent_newline
)
1623 pr_out("%s %s", name
, val
);
1625 pr_out(" %s %s", name
, val
);
1629 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1632 pr_out_str(dl
, name
, "true");
1634 pr_out_str(dl
, name
, "false");
1637 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1639 if (dl
->json_output
) {
1640 jsonw_uint_field(dl
->jw
, name
, val
);
1642 if (g_indent_newline
)
1643 pr_out("%s %u", name
, val
);
1645 pr_out(" %s %u", name
, val
);
1649 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1651 if (val
== (uint64_t) -1)
1652 return pr_out_str(dl
, name
, "unlimited");
1654 return pr_out_uint(dl
, name
, val
);
1657 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1659 if (dl
->json_output
) {
1660 jsonw_name(dl
->jw
, "address");
1661 jsonw_uint(dl
->jw
, addr
);
1662 jsonw_name(dl
->jw
, "data");
1663 jsonw_start_array(dl
->jw
);
1667 static void pr_out_region_chunk_end(struct dl
*dl
)
1669 if (dl
->json_output
)
1670 jsonw_end_array(dl
->jw
);
1673 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1676 static uint64_t align_val
;
1679 pr_out_region_chunk_start(dl
, addr
);
1681 if (!dl
->json_output
)
1682 if (!(align_val
% 16))
1683 pr_out("%s%016"PRIx64
" ",
1684 align_val
? "\n" : "",
1689 if (dl
->json_output
)
1690 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1692 pr_out("%02x ", data
[i
]);
1697 pr_out_region_chunk_end(dl
);
1700 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1702 pr_out_handle(dl
, tb
);
1705 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1707 if (dl
->json_output
) {
1708 jsonw_start_object(dl
->jw
);
1709 jsonw_name(dl
->jw
, name
);
1710 jsonw_start_object(dl
->jw
);
1714 static void pr_out_section_end(struct dl
*dl
)
1716 if (dl
->json_output
) {
1717 if (dl
->arr_last
.present
)
1718 jsonw_end_array(dl
->jw
);
1719 jsonw_end_object(dl
->jw
);
1720 jsonw_end_object(dl
->jw
);
1724 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1726 if (dl
->json_output
) {
1727 jsonw_name(dl
->jw
, name
);
1728 jsonw_start_array(dl
->jw
);
1730 __pr_out_indent_inc();
1732 pr_out("%s:", name
);
1733 __pr_out_indent_inc();
1738 static void pr_out_array_end(struct dl
*dl
)
1740 if (dl
->json_output
) {
1741 jsonw_end_array(dl
->jw
);
1743 __pr_out_indent_dec();
1744 __pr_out_indent_dec();
1748 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1750 if (dl
->json_output
) {
1751 jsonw_name(dl
->jw
, name
);
1752 jsonw_start_object(dl
->jw
);
1754 __pr_out_indent_inc();
1756 pr_out("%s:", name
);
1757 __pr_out_indent_inc();
1762 static void pr_out_object_end(struct dl
*dl
)
1764 if (dl
->json_output
) {
1765 jsonw_end_object(dl
->jw
);
1767 __pr_out_indent_dec();
1768 __pr_out_indent_dec();
1772 static void pr_out_entry_start(struct dl
*dl
)
1774 if (dl
->json_output
)
1775 jsonw_start_object(dl
->jw
);
1778 static void pr_out_entry_end(struct dl
*dl
)
1780 if (dl
->json_output
)
1781 jsonw_end_object(dl
->jw
);
1786 static const char *param_cmode_name(uint8_t cmode
)
1789 case DEVLINK_PARAM_CMODE_RUNTIME
:
1790 return PARAM_CMODE_RUNTIME_STR
;
1791 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1792 return PARAM_CMODE_DRIVERINIT_STR
;
1793 case DEVLINK_PARAM_CMODE_PERMANENT
:
1794 return PARAM_CMODE_PERMANENT_STR
;
1795 default: return "<unknown type>";
1799 static const char *eswitch_mode_name(uint32_t mode
)
1802 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1803 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1804 default: return "<unknown mode>";
1808 static const char *eswitch_inline_mode_name(uint32_t mode
)
1811 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1812 return ESWITCH_INLINE_MODE_NONE
;
1813 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1814 return ESWITCH_INLINE_MODE_LINK
;
1815 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1816 return ESWITCH_INLINE_MODE_NETWORK
;
1817 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1818 return ESWITCH_INLINE_MODE_TRANSPORT
;
1820 return "<unknown mode>";
1824 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1826 __pr_out_handle_start(dl
, tb
, true, false);
1828 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1829 pr_out_str(dl
, "mode",
1830 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1832 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1833 pr_out_str(dl
, "inline-mode",
1834 eswitch_inline_mode_name(mnl_attr_get_u8(
1835 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1837 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1838 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1840 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1843 pr_out_handle_end(dl
);
1846 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1848 struct dl
*dl
= data
;
1849 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1850 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1852 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1853 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1854 return MNL_CB_ERROR
;
1855 pr_out_eswitch(dl
, tb
);
1859 static int cmd_dev_eswitch_show(struct dl
*dl
)
1861 struct nlmsghdr
*nlh
;
1864 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1865 NLM_F_REQUEST
| NLM_F_ACK
);
1867 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1871 pr_out_section_start(dl
, "dev");
1872 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1873 pr_out_section_end(dl
);
1877 static int cmd_dev_eswitch_set(struct dl
*dl
)
1879 struct nlmsghdr
*nlh
;
1882 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1883 NLM_F_REQUEST
| NLM_F_ACK
);
1885 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1886 DL_OPT_ESWITCH_MODE
|
1887 DL_OPT_ESWITCH_INLINE_MODE
|
1888 DL_OPT_ESWITCH_ENCAP_MODE
);
1893 if (dl
->opts
.present
== 1) {
1894 pr_err("Need to set at least one option\n");
1898 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1901 static int cmd_dev_eswitch(struct dl
*dl
)
1903 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1906 } else if (dl_argv_match(dl
, "set")) {
1908 return cmd_dev_eswitch_set(dl
);
1909 } else if (dl_argv_match(dl
, "show")) {
1911 return cmd_dev_eswitch_show(dl
);
1913 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1917 struct param_val_conv
{
1923 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
1924 uint32_t len
, const char *name
)
1928 for (i
= 0; i
< len
; i
++)
1929 if (!strcmp(param_val_conv
[i
].name
, name
))
1936 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
1937 uint32_t len
, const char *name
, const char *vstr
,
1942 for (i
= 0; i
< len
; i
++)
1943 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1944 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
1945 *vuint
= param_val_conv
[i
].vuint
;
1953 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
1954 uint32_t len
, const char *name
, uint32_t vuint
,
1959 for (i
= 0; i
< len
; i
++)
1960 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1961 param_val_conv
[i
].vuint
== vuint
) {
1962 *vstr
= param_val_conv
[i
].vstr
;
1969 static const struct param_val_conv param_val_conv
[] = {
1971 .name
= "fw_load_policy",
1973 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
1976 .name
= "fw_load_policy",
1978 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
1982 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
1984 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
1985 int nla_type
, struct nlattr
*nl
)
1987 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
1988 struct nlattr
*val_attr
;
1993 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
1994 if (err
!= MNL_CB_OK
)
1997 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
1998 (nla_type
!= MNL_TYPE_FLAG
&&
1999 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2002 pr_out_str(dl
, "cmode",
2003 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2004 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2006 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2012 err
= param_val_conv_str_get(param_val_conv
,
2015 mnl_attr_get_u8(val_attr
),
2019 pr_out_str(dl
, "value", vstr
);
2021 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2026 err
= param_val_conv_str_get(param_val_conv
,
2029 mnl_attr_get_u16(val_attr
),
2033 pr_out_str(dl
, "value", vstr
);
2035 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2040 err
= param_val_conv_str_get(param_val_conv
,
2043 mnl_attr_get_u32(val_attr
),
2047 pr_out_str(dl
, "value", vstr
);
2049 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2052 case MNL_TYPE_STRING
:
2053 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2056 pr_out_bool(dl
, "value", val_attr
? true : false);
2061 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2063 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2064 struct nlattr
*param_value_attr
;
2065 const char *nla_name
;
2069 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2070 if (err
!= MNL_CB_OK
)
2072 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2073 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2074 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2078 pr_out_handle_start_arr(dl
, tb
);
2080 __pr_out_handle_start(dl
, tb
, true, false);
2082 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2084 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2085 pr_out_str(dl
, "name", nla_name
);
2087 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2088 pr_out_str(dl
, "type", "driver-specific");
2090 pr_out_str(dl
, "type", "generic");
2092 pr_out_array_start(dl
, "values");
2093 mnl_attr_for_each_nested(param_value_attr
,
2094 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2095 pr_out_entry_start(dl
);
2096 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2097 pr_out_entry_end(dl
);
2099 pr_out_array_end(dl
);
2100 pr_out_handle_end(dl
);
2103 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2105 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2106 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2107 struct dl
*dl
= data
;
2109 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2110 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2111 !tb
[DEVLINK_ATTR_PARAM
])
2112 return MNL_CB_ERROR
;
2113 pr_out_param(dl
, tb
, true);
2129 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2131 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2132 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2133 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2134 struct nlattr
*param_value_attr
;
2135 enum devlink_param_cmode cmode
;
2136 struct param_ctx
*ctx
= data
;
2137 struct dl
*dl
= ctx
->dl
;
2141 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2142 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2143 !tb
[DEVLINK_ATTR_PARAM
])
2144 return MNL_CB_ERROR
;
2146 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2147 if (err
!= MNL_CB_OK
)
2148 return MNL_CB_ERROR
;
2150 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2151 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2152 return MNL_CB_ERROR
;
2154 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2155 mnl_attr_for_each_nested(param_value_attr
,
2156 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2157 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2158 struct nlattr
*val_attr
;
2160 err
= mnl_attr_parse_nested(param_value_attr
,
2161 attr_cb
, nla_value
);
2162 if (err
!= MNL_CB_OK
)
2163 return MNL_CB_ERROR
;
2165 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2166 (nla_type
!= MNL_TYPE_FLAG
&&
2167 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2168 return MNL_CB_ERROR
;
2170 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2171 if (cmode
== dl
->opts
.cmode
) {
2172 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2175 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2178 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2181 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2183 case MNL_TYPE_STRING
:
2184 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2187 ctx
->value
.vbool
= val_attr
? true : false;
2193 ctx
->nla_type
= nla_type
;
2197 static int cmd_dev_param_set(struct dl
*dl
)
2199 struct param_ctx ctx
= {};
2200 struct nlmsghdr
*nlh
;
2208 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2210 DL_OPT_PARAM_VALUE
|
2211 DL_OPT_PARAM_CMODE
, 0);
2215 /* Get value type */
2216 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2217 NLM_F_REQUEST
| NLM_F_ACK
);
2218 dl_opts_put(nlh
, dl
);
2221 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2225 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2226 NLM_F_REQUEST
| NLM_F_ACK
);
2227 dl_opts_put(nlh
, dl
);
2229 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2230 dl
->opts
.param_name
);
2232 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2233 switch (ctx
.nla_type
) {
2236 err
= param_val_conv_uint_get(param_val_conv
,
2238 dl
->opts
.param_name
,
2239 dl
->opts
.param_value
,
2243 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2246 goto err_param_value_parse
;
2247 if (val_u8
== ctx
.value
.vu8
)
2249 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2253 err
= param_val_conv_uint_get(param_val_conv
,
2255 dl
->opts
.param_name
,
2256 dl
->opts
.param_value
,
2260 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2263 goto err_param_value_parse
;
2264 if (val_u16
== ctx
.value
.vu16
)
2266 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2270 err
= param_val_conv_uint_get(param_val_conv
,
2272 dl
->opts
.param_name
,
2273 dl
->opts
.param_value
,
2276 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2278 goto err_param_value_parse
;
2279 if (val_u32
== ctx
.value
.vu32
)
2281 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2284 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2286 goto err_param_value_parse
;
2287 if (val_bool
== ctx
.value
.vbool
)
2290 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2293 case MNL_TYPE_STRING
:
2294 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2295 dl
->opts
.param_value
);
2296 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2300 printf("Value type not supported\n");
2303 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2305 err_param_value_parse
:
2306 pr_err("Value \"%s\" is not a number or not within range\n",
2307 dl
->opts
.param_value
);
2311 static int cmd_dev_param_show(struct dl
*dl
)
2313 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2314 struct nlmsghdr
*nlh
;
2317 if (dl_argc(dl
) == 0)
2318 flags
|= NLM_F_DUMP
;
2320 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2322 if (dl_argc(dl
) > 0) {
2323 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2324 DL_OPT_PARAM_NAME
, 0);
2329 pr_out_section_start(dl
, "param");
2330 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2331 pr_out_section_end(dl
);
2335 static int cmd_dev_param(struct dl
*dl
)
2337 if (dl_argv_match(dl
, "help")) {
2340 } else if (dl_argv_match(dl
, "show") ||
2341 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2343 return cmd_dev_param_show(dl
);
2344 } else if (dl_argv_match(dl
, "set")) {
2346 return cmd_dev_param_set(dl
);
2348 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2351 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2353 struct dl
*dl
= data
;
2354 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2355 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2357 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2358 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2359 return MNL_CB_ERROR
;
2364 static int cmd_dev_show(struct dl
*dl
)
2366 struct nlmsghdr
*nlh
;
2367 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2370 if (dl_argc(dl
) == 0)
2371 flags
|= NLM_F_DUMP
;
2373 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2375 if (dl_argc(dl
) > 0) {
2376 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2381 pr_out_section_start(dl
, "dev");
2382 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2383 pr_out_section_end(dl
);
2387 static void cmd_dev_reload_help(void)
2389 pr_err("Usage: devlink dev reload [ DEV ]\n");
2392 static int cmd_dev_reload(struct dl
*dl
)
2394 struct nlmsghdr
*nlh
;
2397 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2398 cmd_dev_reload_help();
2402 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2403 NLM_F_REQUEST
| NLM_F_ACK
);
2405 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2409 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2412 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2413 const char *name
, int type
)
2415 struct nlattr
*version
;
2417 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2418 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2419 const char *ver_value
;
2420 const char *ver_name
;
2423 if (mnl_attr_get_type(version
) != type
)
2426 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2427 if (err
!= MNL_CB_OK
)
2430 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2431 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2435 pr_out_object_start(dl
, name
);
2439 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2440 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2442 pr_out_str(dl
, ver_name
, ver_value
);
2443 if (!dl
->json_output
)
2448 pr_out_object_end(dl
);
2451 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2452 struct nlattr
**tb
, bool has_versions
)
2454 __pr_out_handle_start(dl
, tb
, true, false);
2456 __pr_out_indent_inc();
2457 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2458 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2460 if (!dl
->json_output
)
2462 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2465 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2466 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2468 if (!dl
->json_output
)
2470 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2472 __pr_out_indent_dec();
2475 pr_out_object_start(dl
, "versions");
2477 pr_out_versions_single(dl
, nlh
, "fixed",
2478 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2479 pr_out_versions_single(dl
, nlh
, "running",
2480 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2481 pr_out_versions_single(dl
, nlh
, "stored",
2482 DEVLINK_ATTR_INFO_VERSION_STORED
);
2484 pr_out_object_end(dl
);
2487 pr_out_handle_end(dl
);
2490 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2492 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2493 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2494 bool has_versions
, has_info
;
2495 struct dl
*dl
= data
;
2497 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2499 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2500 return MNL_CB_ERROR
;
2502 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2503 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2504 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2505 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2506 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2510 pr_out_info(dl
, nlh
, tb
, has_versions
);
2515 static void cmd_dev_info_help(void)
2517 pr_err("Usage: devlink dev info [ DEV ]\n");
2520 static int cmd_dev_info(struct dl
*dl
)
2522 struct nlmsghdr
*nlh
;
2523 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2526 if (dl_argv_match(dl
, "help")) {
2527 cmd_dev_info_help();
2531 if (dl_argc(dl
) == 0)
2532 flags
|= NLM_F_DUMP
;
2534 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2536 if (dl_argc(dl
) > 0) {
2537 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2542 pr_out_section_start(dl
, "info");
2543 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2544 pr_out_section_end(dl
);
2548 static void cmd_dev_flash_help(void)
2550 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2553 static int cmd_dev_flash(struct dl
*dl
)
2555 struct nlmsghdr
*nlh
;
2558 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2559 cmd_dev_flash_help();
2563 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2564 NLM_F_REQUEST
| NLM_F_ACK
);
2566 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2567 DL_OPT_FLASH_COMPONENT
);
2571 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2574 static int cmd_dev(struct dl
*dl
)
2576 if (dl_argv_match(dl
, "help")) {
2579 } else if (dl_argv_match(dl
, "show") ||
2580 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2582 return cmd_dev_show(dl
);
2583 } else if (dl_argv_match(dl
, "eswitch")) {
2585 return cmd_dev_eswitch(dl
);
2586 } else if (dl_argv_match(dl
, "reload")) {
2588 return cmd_dev_reload(dl
);
2589 } else if (dl_argv_match(dl
, "param")) {
2591 return cmd_dev_param(dl
);
2592 } else if (dl_argv_match(dl
, "info")) {
2594 return cmd_dev_info(dl
);
2595 } else if (dl_argv_match(dl
, "flash")) {
2597 return cmd_dev_flash(dl
);
2599 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2603 static void cmd_port_help(void)
2605 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2606 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2607 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2608 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2611 static const char *port_type_name(uint32_t type
)
2614 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2615 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2616 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2617 case DEVLINK_PORT_TYPE_IB
: return "ib";
2618 default: return "<unknown type>";
2622 static const char *port_flavour_name(uint16_t flavour
)
2625 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2627 case DEVLINK_PORT_FLAVOUR_CPU
:
2629 case DEVLINK_PORT_FLAVOUR_DSA
:
2632 return "<unknown flavour>";
2636 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2638 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2639 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2641 pr_out_port_handle_start(dl
, tb
, false);
2643 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2645 pr_out_str(dl
, "type", port_type_name(port_type
));
2647 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2649 if (port_type
!= des_port_type
)
2650 pr_out_str(dl
, "des_type",
2651 port_type_name(des_port_type
));
2654 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2655 pr_out_str(dl
, "netdev",
2656 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2657 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2658 pr_out_str(dl
, "ibdev",
2659 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2660 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2661 uint16_t port_flavour
=
2662 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2664 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2666 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2667 pr_out_uint(dl
, "split_group",
2668 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2669 pr_out_port_handle_end(dl
);
2672 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2674 struct dl
*dl
= data
;
2675 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2676 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2678 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2679 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2680 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2681 return MNL_CB_ERROR
;
2682 pr_out_port(dl
, tb
);
2686 static int cmd_port_show(struct dl
*dl
)
2688 struct nlmsghdr
*nlh
;
2689 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2692 if (dl_argc(dl
) == 0)
2693 flags
|= NLM_F_DUMP
;
2695 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2697 if (dl_argc(dl
) > 0) {
2698 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2703 pr_out_section_start(dl
, "port");
2704 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2705 pr_out_section_end(dl
);
2709 static int cmd_port_set(struct dl
*dl
)
2711 struct nlmsghdr
*nlh
;
2714 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2715 NLM_F_REQUEST
| NLM_F_ACK
);
2717 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2721 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2724 static int cmd_port_split(struct dl
*dl
)
2726 struct nlmsghdr
*nlh
;
2729 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2730 NLM_F_REQUEST
| NLM_F_ACK
);
2732 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2736 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2739 static int cmd_port_unsplit(struct dl
*dl
)
2741 struct nlmsghdr
*nlh
;
2744 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2745 NLM_F_REQUEST
| NLM_F_ACK
);
2747 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2751 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2754 static int cmd_port(struct dl
*dl
)
2756 if (dl_argv_match(dl
, "help")) {
2759 } else if (dl_argv_match(dl
, "show") ||
2760 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2762 return cmd_port_show(dl
);
2763 } else if (dl_argv_match(dl
, "set")) {
2765 return cmd_port_set(dl
);
2766 } else if (dl_argv_match(dl
, "split")) {
2768 return cmd_port_split(dl
);
2769 } else if (dl_argv_match(dl
, "unsplit")) {
2771 return cmd_port_unsplit(dl
);
2773 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2777 static void cmd_sb_help(void)
2779 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2780 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2781 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2782 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2783 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2784 pr_err(" pool POOL_INDEX ]\n");
2785 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2786 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2787 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2788 pr_err(" type { ingress | egress } ]\n");
2789 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2790 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2791 pr_err(" th THRESHOLD\n");
2792 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2793 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2794 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2797 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2799 pr_out_handle_start_arr(dl
, tb
);
2800 pr_out_uint(dl
, "sb",
2801 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2802 pr_out_uint(dl
, "size",
2803 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2804 pr_out_uint(dl
, "ing_pools",
2805 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2806 pr_out_uint(dl
, "eg_pools",
2807 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2808 pr_out_uint(dl
, "ing_tcs",
2809 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2810 pr_out_uint(dl
, "eg_tcs",
2811 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2812 pr_out_handle_end(dl
);
2815 static int cmd_sb_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_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2824 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2825 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2826 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2827 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2828 return MNL_CB_ERROR
;
2833 static int cmd_sb_show(struct dl
*dl
)
2835 struct nlmsghdr
*nlh
;
2836 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2839 if (dl_argc(dl
) == 0)
2840 flags
|= NLM_F_DUMP
;
2842 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2844 if (dl_argc(dl
) > 0) {
2845 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2850 pr_out_section_start(dl
, "sb");
2851 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2852 pr_out_section_end(dl
);
2856 static const char *pool_type_name(uint8_t type
)
2859 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2860 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2861 default: return "<unknown type>";
2865 static const char *threshold_type_name(uint8_t type
)
2868 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2869 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2870 default: return "<unknown type>";
2874 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2876 pr_out_handle_start_arr(dl
, tb
);
2877 pr_out_uint(dl
, "sb",
2878 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2879 pr_out_uint(dl
, "pool",
2880 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2881 pr_out_str(dl
, "type",
2882 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2883 pr_out_uint(dl
, "size",
2884 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2885 pr_out_str(dl
, "thtype",
2886 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2887 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
2888 pr_out_uint(dl
, "cell_size",
2889 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
2890 pr_out_handle_end(dl
);
2893 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2895 struct dl
*dl
= data
;
2896 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2897 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2899 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2900 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2901 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2902 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2903 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2904 return MNL_CB_ERROR
;
2905 pr_out_sb_pool(dl
, tb
);
2909 static int cmd_sb_pool_show(struct dl
*dl
)
2911 struct nlmsghdr
*nlh
;
2912 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2915 if (dl_argc(dl
) == 0)
2916 flags
|= NLM_F_DUMP
;
2918 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2920 if (dl_argc(dl
) > 0) {
2921 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2927 pr_out_section_start(dl
, "pool");
2928 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2929 pr_out_section_end(dl
);
2933 static int cmd_sb_pool_set(struct dl
*dl
)
2935 struct nlmsghdr
*nlh
;
2938 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
2939 NLM_F_REQUEST
| NLM_F_ACK
);
2941 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
2942 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
2946 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2949 static int cmd_sb_pool(struct dl
*dl
)
2951 if (dl_argv_match(dl
, "help")) {
2954 } else if (dl_argv_match(dl
, "show") ||
2955 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2957 return cmd_sb_pool_show(dl
);
2958 } else if (dl_argv_match(dl
, "set")) {
2960 return cmd_sb_pool_set(dl
);
2962 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2966 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
2968 pr_out_port_handle_start_arr(dl
, tb
, true);
2969 pr_out_uint(dl
, "sb",
2970 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2971 pr_out_uint(dl
, "pool",
2972 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2973 pr_out_uint(dl
, "threshold",
2974 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2975 pr_out_port_handle_end(dl
);
2978 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2980 struct dl
*dl
= data
;
2981 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2982 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2984 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2985 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2986 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2987 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2988 return MNL_CB_ERROR
;
2989 pr_out_sb_port_pool(dl
, tb
);
2993 static int cmd_sb_port_pool_show(struct dl
*dl
)
2995 struct nlmsghdr
*nlh
;
2996 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2999 if (dl_argc(dl
) == 0)
3000 flags
|= NLM_F_DUMP
;
3002 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3004 if (dl_argc(dl
) > 0) {
3005 err
= dl_argv_parse_put(nlh
, dl
,
3006 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3012 pr_out_section_start(dl
, "port_pool");
3013 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3014 pr_out_section_end(dl
);
3018 static int cmd_sb_port_pool_set(struct dl
*dl
)
3020 struct nlmsghdr
*nlh
;
3023 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3024 NLM_F_REQUEST
| NLM_F_ACK
);
3026 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3027 DL_OPT_SB_TH
, DL_OPT_SB
);
3031 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3034 static int cmd_sb_port_pool(struct dl
*dl
)
3036 if (dl_argv_match(dl
, "help")) {
3039 } else if (dl_argv_match(dl
, "show") ||
3040 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3042 return cmd_sb_port_pool_show(dl
);
3043 } else if (dl_argv_match(dl
, "set")) {
3045 return cmd_sb_port_pool_set(dl
);
3047 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3051 static int cmd_sb_port(struct dl
*dl
)
3053 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3056 } else if (dl_argv_match(dl
, "pool")) {
3058 return cmd_sb_port_pool(dl
);
3060 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3064 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3066 pr_out_port_handle_start_arr(dl
, tb
, true);
3067 pr_out_uint(dl
, "sb",
3068 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3069 pr_out_uint(dl
, "tc",
3070 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3071 pr_out_str(dl
, "type",
3072 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3073 pr_out_uint(dl
, "pool",
3074 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3075 pr_out_uint(dl
, "threshold",
3076 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3077 pr_out_port_handle_end(dl
);
3080 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3082 struct dl
*dl
= data
;
3083 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3084 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3086 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3087 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3088 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3089 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3090 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3091 return MNL_CB_ERROR
;
3092 pr_out_sb_tc_bind(dl
, tb
);
3096 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3098 struct nlmsghdr
*nlh
;
3099 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3102 if (dl_argc(dl
) == 0)
3103 flags
|= NLM_F_DUMP
;
3105 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3107 if (dl_argc(dl
) > 0) {
3108 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3109 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3114 pr_out_section_start(dl
, "tc_bind");
3115 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3116 pr_out_section_end(dl
);
3120 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3122 struct nlmsghdr
*nlh
;
3125 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3126 NLM_F_REQUEST
| NLM_F_ACK
);
3128 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3129 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3134 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3137 static int cmd_sb_tc_bind(struct dl
*dl
)
3139 if (dl_argv_match(dl
, "help")) {
3142 } else if (dl_argv_match(dl
, "show") ||
3143 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3145 return cmd_sb_tc_bind_show(dl
);
3146 } else if (dl_argv_match(dl
, "set")) {
3148 return cmd_sb_tc_bind_set(dl
);
3150 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3154 static int cmd_sb_tc(struct dl
*dl
)
3156 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3159 } else if (dl_argv_match(dl
, "bind")) {
3161 return cmd_sb_tc_bind(dl
);
3163 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3168 struct list_head list
;
3172 uint32_t bound_pool_index
;
3176 struct list_head list
;
3179 uint32_t port_index
;
3181 struct list_head pool_list
;
3182 struct list_head ing_tc_list
;
3183 struct list_head eg_tc_list
;
3189 struct list_head port_list
;
3192 static struct occ_item
*occ_item_alloc(void)
3194 return calloc(1, sizeof(struct occ_item
));
3197 static void occ_item_free(struct occ_item
*occ_item
)
3202 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3204 struct occ_port
*occ_port
;
3206 occ_port
= calloc(1, sizeof(*occ_port
));
3209 occ_port
->port_index
= port_index
;
3210 INIT_LIST_HEAD(&occ_port
->pool_list
);
3211 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3212 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3216 static void occ_port_free(struct occ_port
*occ_port
)
3218 struct occ_item
*occ_item
, *tmp
;
3220 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3221 occ_item_free(occ_item
);
3222 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3223 occ_item_free(occ_item
);
3224 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3225 occ_item_free(occ_item
);
3228 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3230 struct occ_show
*occ_show
;
3232 occ_show
= calloc(1, sizeof(*occ_show
));
3236 INIT_LIST_HEAD(&occ_show
->port_list
);
3240 static void occ_show_free(struct occ_show
*occ_show
)
3242 struct occ_port
*occ_port
, *tmp
;
3244 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3245 occ_port_free(occ_port
);
3248 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3251 struct occ_port
*occ_port
;
3252 uint32_t port_index
;
3254 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3256 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3257 if (occ_port
->port_index
== port_index
)
3260 occ_port
= occ_port_alloc(port_index
);
3263 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3267 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3270 struct occ_item
*occ_item
;
3273 pr_out_sp(7, " %s:", label
);
3274 list_for_each_entry(occ_item
, list
, list
) {
3275 if ((i
- 1) % 4 == 0 && i
!= 1)
3278 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3279 occ_item
->bound_pool_index
);
3281 pr_out_sp(7, "%2u:", occ_item
->index
);
3282 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
3286 if ((i
- 1) % 4 != 0)
3290 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3291 struct list_head
*list
,
3294 struct occ_item
*occ_item
;
3297 jsonw_name(dl
->jw
, label
);
3298 jsonw_start_object(dl
->jw
);
3299 list_for_each_entry(occ_item
, list
, list
) {
3300 sprintf(buf
, "%u", occ_item
->index
);
3301 jsonw_name(dl
->jw
, buf
);
3302 jsonw_start_object(dl
->jw
);
3304 jsonw_uint_field(dl
->jw
, "bound_pool",
3305 occ_item
->bound_pool_index
);
3306 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3307 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3308 jsonw_end_object(dl
->jw
);
3310 jsonw_end_object(dl
->jw
);
3313 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3315 if (dl
->json_output
) {
3316 pr_out_json_occ_show_item_list(dl
, "pool",
3317 &occ_port
->pool_list
, false);
3318 pr_out_json_occ_show_item_list(dl
, "itc",
3319 &occ_port
->ing_tc_list
, true);
3320 pr_out_json_occ_show_item_list(dl
, "etc",
3321 &occ_port
->eg_tc_list
, true);
3324 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3325 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3326 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3330 static void pr_out_occ_show(struct occ_show
*occ_show
)
3332 struct dl
*dl
= occ_show
->dl
;
3333 struct dl_opts
*opts
= &dl
->opts
;
3334 struct occ_port
*occ_port
;
3336 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3337 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3338 occ_port
->port_index
, true, false);
3339 pr_out_occ_show_port(dl
, occ_port
);
3340 pr_out_port_handle_end(dl
);
3344 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3347 struct occ_port
*occ_port
;
3348 struct occ_item
*occ_item
;
3350 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3353 occ_port
= occ_port_get(occ_show
, tb
);
3355 occ_show
->err
= -ENOMEM
;
3359 occ_item
= occ_item_alloc();
3361 occ_show
->err
= -ENOMEM
;
3364 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3365 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3366 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3367 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3370 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3372 struct occ_show
*occ_show
= data
;
3373 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3374 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3376 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3377 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3378 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3379 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3380 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3381 return MNL_CB_ERROR
;
3382 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3386 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3389 struct occ_port
*occ_port
;
3390 struct occ_item
*occ_item
;
3393 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3396 occ_port
= occ_port_get(occ_show
, tb
);
3398 occ_show
->err
= -ENOMEM
;
3402 occ_item
= occ_item_alloc();
3404 occ_show
->err
= -ENOMEM
;
3407 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3408 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3409 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3410 occ_item
->bound_pool_index
=
3411 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3412 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3413 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3414 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3415 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3416 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3418 occ_item_free(occ_item
);
3421 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3423 struct occ_show
*occ_show
= data
;
3424 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3425 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3427 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3428 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3429 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3430 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3431 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3432 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3433 return MNL_CB_ERROR
;
3434 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3438 static int cmd_sb_occ_show(struct dl
*dl
)
3440 struct nlmsghdr
*nlh
;
3441 struct occ_show
*occ_show
;
3442 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3445 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3449 occ_show
= occ_show_alloc(dl
);
3453 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3455 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3456 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3460 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3462 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3463 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3467 pr_out_section_start(dl
, "occupancy");
3468 pr_out_occ_show(occ_show
);
3469 pr_out_section_end(dl
);
3472 occ_show_free(occ_show
);
3476 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3478 struct nlmsghdr
*nlh
;
3481 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3482 NLM_F_REQUEST
| NLM_F_ACK
);
3484 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3488 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3491 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3493 struct nlmsghdr
*nlh
;
3496 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3497 NLM_F_REQUEST
| NLM_F_ACK
);
3499 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3503 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3506 static int cmd_sb_occ(struct dl
*dl
)
3508 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3511 } else if (dl_argv_match(dl
, "show") ||
3512 dl_argv_match(dl
, "list")) {
3514 return cmd_sb_occ_show(dl
);
3515 } else if (dl_argv_match(dl
, "snapshot")) {
3517 return cmd_sb_occ_snapshot(dl
);
3518 } else if (dl_argv_match(dl
, "clearmax")) {
3520 return cmd_sb_occ_clearmax(dl
);
3522 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3526 static int cmd_sb(struct dl
*dl
)
3528 if (dl_argv_match(dl
, "help")) {
3531 } else if (dl_argv_match(dl
, "show") ||
3532 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3534 return cmd_sb_show(dl
);
3535 } else if (dl_argv_match(dl
, "pool")) {
3537 return cmd_sb_pool(dl
);
3538 } else if (dl_argv_match(dl
, "port")) {
3540 return cmd_sb_port(dl
);
3541 } else if (dl_argv_match(dl
, "tc")) {
3543 return cmd_sb_tc(dl
);
3544 } else if (dl_argv_match(dl
, "occupancy")) {
3546 return cmd_sb_occ(dl
);
3548 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3552 static const char *cmd_name(uint8_t cmd
)
3555 case DEVLINK_CMD_UNSPEC
: return "unspec";
3556 case DEVLINK_CMD_GET
: return "get";
3557 case DEVLINK_CMD_SET
: return "set";
3558 case DEVLINK_CMD_NEW
: return "new";
3559 case DEVLINK_CMD_DEL
: return "del";
3560 case DEVLINK_CMD_PORT_GET
: return "get";
3561 case DEVLINK_CMD_PORT_SET
: return "set";
3562 case DEVLINK_CMD_PORT_NEW
: return "new";
3563 case DEVLINK_CMD_PORT_DEL
: return "del";
3564 case DEVLINK_CMD_PARAM_GET
: return "get";
3565 case DEVLINK_CMD_PARAM_SET
: return "set";
3566 case DEVLINK_CMD_PARAM_NEW
: return "new";
3567 case DEVLINK_CMD_PARAM_DEL
: return "del";
3568 case DEVLINK_CMD_REGION_GET
: return "get";
3569 case DEVLINK_CMD_REGION_SET
: return "set";
3570 case DEVLINK_CMD_REGION_NEW
: return "new";
3571 case DEVLINK_CMD_REGION_DEL
: return "del";
3572 default: return "<unknown cmd>";
3576 static const char *cmd_obj(uint8_t cmd
)
3579 case DEVLINK_CMD_UNSPEC
: return "unspec";
3580 case DEVLINK_CMD_GET
:
3581 case DEVLINK_CMD_SET
:
3582 case DEVLINK_CMD_NEW
:
3583 case DEVLINK_CMD_DEL
:
3585 case DEVLINK_CMD_PORT_GET
:
3586 case DEVLINK_CMD_PORT_SET
:
3587 case DEVLINK_CMD_PORT_NEW
:
3588 case DEVLINK_CMD_PORT_DEL
:
3590 case DEVLINK_CMD_PARAM_GET
:
3591 case DEVLINK_CMD_PARAM_SET
:
3592 case DEVLINK_CMD_PARAM_NEW
:
3593 case DEVLINK_CMD_PARAM_DEL
:
3595 case DEVLINK_CMD_REGION_GET
:
3596 case DEVLINK_CMD_REGION_SET
:
3597 case DEVLINK_CMD_REGION_NEW
:
3598 case DEVLINK_CMD_REGION_DEL
:
3600 default: return "<unknown obj>";
3604 static void pr_out_mon_header(uint8_t cmd
)
3606 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3609 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3611 const char *obj
= cmd_obj(cmd
);
3612 unsigned int index
= 0;
3613 const char *cur_obj
;
3617 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3618 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3624 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3626 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3628 struct dl
*dl
= data
;
3629 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3630 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3631 uint8_t cmd
= genl
->cmd
;
3633 if (!cmd_filter_check(dl
, cmd
))
3637 case DEVLINK_CMD_GET
: /* fall through */
3638 case DEVLINK_CMD_SET
: /* fall through */
3639 case DEVLINK_CMD_NEW
: /* fall through */
3640 case DEVLINK_CMD_DEL
:
3641 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3642 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3643 return MNL_CB_ERROR
;
3644 pr_out_mon_header(genl
->cmd
);
3647 case DEVLINK_CMD_PORT_GET
: /* fall through */
3648 case DEVLINK_CMD_PORT_SET
: /* fall through */
3649 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3650 case DEVLINK_CMD_PORT_DEL
:
3651 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3652 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3653 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3654 return MNL_CB_ERROR
;
3655 pr_out_mon_header(genl
->cmd
);
3656 pr_out_port(dl
, tb
);
3658 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3659 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3660 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3661 case DEVLINK_CMD_PARAM_DEL
:
3662 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3663 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3664 !tb
[DEVLINK_ATTR_PARAM
])
3665 return MNL_CB_ERROR
;
3666 pr_out_mon_header(genl
->cmd
);
3667 pr_out_param(dl
, tb
, false);
3669 case DEVLINK_CMD_REGION_GET
: /* fall through */
3670 case DEVLINK_CMD_REGION_SET
: /* fall through */
3671 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3672 case DEVLINK_CMD_REGION_DEL
:
3673 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3674 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3675 !tb
[DEVLINK_ATTR_REGION_NAME
])
3676 return MNL_CB_ERROR
;
3677 pr_out_mon_header(genl
->cmd
);
3678 pr_out_region(dl
, tb
);
3684 static int cmd_mon_show(struct dl
*dl
)
3687 unsigned int index
= 0;
3688 const char *cur_obj
;
3690 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3691 if (strcmp(cur_obj
, "all") != 0 &&
3692 strcmp(cur_obj
, "dev") != 0 &&
3693 strcmp(cur_obj
, "port") != 0) {
3694 pr_err("Unknown object \"%s\"\n", cur_obj
);
3698 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3701 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3707 static void cmd_mon_help(void)
3709 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3710 "where OBJECT-LIST := { dev | port }\n");
3713 static int cmd_mon(struct dl
*dl
)
3715 if (dl_argv_match(dl
, "help")) {
3718 } else if (dl_no_arg(dl
)) {
3720 return cmd_mon_show(dl
);
3722 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3726 struct dpipe_field
{
3729 unsigned int bitwidth
;
3730 enum devlink_dpipe_field_mapping_type mapping_type
;
3733 struct dpipe_header
{
3734 struct list_head list
;
3737 struct dpipe_field
*fields
;
3738 unsigned int fields_count
;
3741 struct dpipe_table
{
3742 struct list_head list
;
3744 unsigned int resource_id
;
3745 bool resource_valid
;
3748 struct dpipe_tables
{
3749 struct list_head table_list
;
3759 enum devlink_resource_unit unit
;
3764 struct list_head list
;
3765 struct list_head resource_list
;
3766 struct resource
*parent
;
3770 struct list_head resource_list
;
3773 struct resource_ctx
{
3776 struct resources
*resources
;
3777 struct dpipe_tables
*tables
;
3778 bool print_resources
;
3779 bool pending_change
;
3782 static struct resource
*resource_alloc(void)
3784 struct resource
*resource
;
3786 resource
= calloc(1, sizeof(struct resource
));
3789 INIT_LIST_HEAD(&resource
->resource_list
);
3793 static void resource_free(struct resource
*resource
)
3795 struct resource
*child_resource
, *tmp
;
3797 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3799 free(child_resource
->name
);
3800 resource_free(child_resource
);
3805 static struct resources
*resources_alloc(void)
3807 struct resources
*resources
;
3809 resources
= calloc(1, sizeof(struct resources
));
3812 INIT_LIST_HEAD(&resources
->resource_list
);
3816 static void resources_free(struct resources
*resources
)
3818 struct resource
*resource
, *tmp
;
3820 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3821 resource_free(resource
);
3824 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3826 ctx
->resources
= resources_alloc();
3827 if (!ctx
->resources
)
3833 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3835 resources_free(ctx
->resources
);
3841 struct list_head global_headers
;
3842 struct list_head local_headers
;
3843 struct dpipe_tables
*tables
;
3844 struct resources
*resources
;
3849 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3851 struct dpipe_header
*header
;
3853 header
= calloc(1, sizeof(struct dpipe_header
));
3856 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3857 if (!header
->fields
)
3858 goto err_fields_alloc
;
3859 header
->fields_count
= fields_count
;
3867 static void dpipe_header_free(struct dpipe_header
*header
)
3869 free(header
->fields
);
3873 static void dpipe_header_clear(struct dpipe_header
*header
)
3875 struct dpipe_field
*field
;
3878 for (i
= 0; i
< header
->fields_count
; i
++) {
3879 field
= &header
->fields
[i
];
3885 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3886 struct dpipe_header
*header
, bool global
)
3889 list_add(&header
->list
, &ctx
->global_headers
);
3891 list_add(&header
->list
, &ctx
->local_headers
);
3894 static void dpipe_header_del(struct dpipe_header
*header
)
3896 list_del(&header
->list
);
3899 static struct dpipe_table
*dpipe_table_alloc(void)
3901 return calloc(1, sizeof(struct dpipe_table
));
3904 static void dpipe_table_free(struct dpipe_table
*table
)
3909 static struct dpipe_tables
*dpipe_tables_alloc(void)
3911 struct dpipe_tables
*tables
;
3913 tables
= calloc(1, sizeof(struct dpipe_tables
));
3916 INIT_LIST_HEAD(&tables
->table_list
);
3920 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3922 struct dpipe_table
*table
, *tmp
;
3924 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3925 dpipe_table_free(table
);
3929 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
3931 ctx
->tables
= dpipe_tables_alloc();
3936 INIT_LIST_HEAD(&ctx
->global_headers
);
3937 INIT_LIST_HEAD(&ctx
->local_headers
);
3941 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
3943 struct dpipe_header
*header
, *tmp
;
3945 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
3947 dpipe_header_del(header
);
3948 dpipe_header_clear(header
);
3949 dpipe_header_free(header
);
3951 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
3953 dpipe_header_del(header
);
3954 dpipe_header_clear(header
);
3955 dpipe_header_free(header
);
3957 dpipe_tables_free(ctx
->tables
);
3960 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
3961 uint32_t header_id
, bool global
)
3963 struct list_head
*header_list
;
3964 struct dpipe_header
*header
;
3967 header_list
= &ctx
->global_headers
;
3969 header_list
= &ctx
->local_headers
;
3970 list_for_each_entry(header
, header_list
, list
) {
3971 if (header
->id
!= header_id
)
3973 return header
->name
;
3978 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
3980 uint32_t field_id
, bool global
)
3982 struct list_head
*header_list
;
3983 struct dpipe_header
*header
;
3986 header_list
= &ctx
->global_headers
;
3988 header_list
= &ctx
->local_headers
;
3989 list_for_each_entry(header
, header_list
, list
) {
3990 if (header
->id
!= header_id
)
3992 return header
->fields
[field_id
].name
;
3998 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4000 switch (mapping_type
) {
4001 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4003 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4011 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4012 uint32_t field_id
, bool global
)
4014 enum devlink_dpipe_field_mapping_type mapping_type
;
4015 struct list_head
*header_list
;
4016 struct dpipe_header
*header
;
4019 header_list
= &ctx
->global_headers
;
4021 header_list
= &ctx
->local_headers
;
4022 list_for_each_entry(header
, header_list
, list
) {
4023 if (header
->id
!= header_id
)
4025 mapping_type
= header
->fields
[field_id
].mapping_type
;
4026 return dpipe_field_mapping_e2s(mapping_type
);
4031 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4032 struct dpipe_field
*fields
,
4033 unsigned int field_count
)
4035 struct dpipe_field
*field
;
4038 for (i
= 0; i
< field_count
; i
++) {
4040 pr_out_entry_start(ctx
->dl
);
4041 pr_out_str(ctx
->dl
, "name", field
->name
);
4042 if (ctx
->dl
->verbose
)
4043 pr_out_uint(ctx
->dl
, "id", field
->id
);
4044 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4045 if (field
->mapping_type
)
4046 pr_out_str(ctx
->dl
, "mapping_type",
4047 dpipe_field_mapping_e2s(field
->mapping_type
));
4048 pr_out_entry_end(ctx
->dl
);
4053 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4054 struct dpipe_header
*header
, bool global
)
4056 pr_out_handle_start_arr(ctx
->dl
, tb
);
4057 pr_out_str(ctx
->dl
, "name", header
->name
);
4058 if (ctx
->dl
->verbose
) {
4059 pr_out_uint(ctx
->dl
, "id", header
->id
);
4060 pr_out_str(ctx
->dl
, "global",
4061 global
? "true" : "false");
4063 pr_out_array_start(ctx
->dl
, "field");
4064 pr_out_dpipe_fields(ctx
, header
->fields
,
4065 header
->fields_count
);
4066 pr_out_array_end(ctx
->dl
);
4067 pr_out_handle_end(ctx
->dl
);
4070 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4073 struct dpipe_header
*header
;
4075 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4076 pr_out_dpipe_header(ctx
, tb
, header
, false);
4078 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4079 pr_out_dpipe_header(ctx
, tb
, header
, true);
4082 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4084 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4088 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4089 if (err
!= MNL_CB_OK
)
4091 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4092 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4093 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4094 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4097 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4098 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4099 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4100 field
->name
= strdup(name
);
4103 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4107 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4108 struct dpipe_field
*fields
)
4110 struct nlattr
*nla_field
;
4114 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4115 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4123 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4125 struct nlattr
*nla_field
;
4126 unsigned int count
= 0;
4128 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4133 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4135 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4136 struct dpipe_header
*header
;
4137 unsigned int fields_count
;
4138 const char *header_name
;
4142 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4143 if (err
!= MNL_CB_OK
)
4146 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4147 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4148 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4151 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4152 header
= dpipe_header_alloc(fields_count
);
4156 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4157 header
->name
= strdup(header_name
);
4158 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4159 header
->fields_count
= fields_count
;
4160 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4162 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4166 dpipe_header_add(ctx
, header
, global
);
4170 dpipe_header_free(header
);
4174 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4176 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4177 struct nlattr
*nla_header
;
4180 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4181 err
= dpipe_header_get(ctx
, nla_header
);
4188 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4190 struct dpipe_ctx
*ctx
= data
;
4191 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4192 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4195 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4196 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4197 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4198 return MNL_CB_ERROR
;
4199 err
= dpipe_headers_get(ctx
, tb
);
4202 return MNL_CB_ERROR
;
4205 if (ctx
->print_headers
)
4206 pr_out_dpipe_headers(ctx
, tb
);
4210 static int cmd_dpipe_headers_show(struct dl
*dl
)
4212 struct nlmsghdr
*nlh
;
4213 struct dpipe_ctx ctx
= {};
4214 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4217 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4219 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4223 err
= dpipe_ctx_init(&ctx
, dl
);
4227 ctx
.print_headers
= true;
4229 pr_out_section_start(dl
, "header");
4230 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4232 pr_err("error get headers %s\n", strerror(ctx
.err
));
4233 pr_out_section_end(dl
);
4235 dpipe_ctx_fini(&ctx
);
4239 static void cmd_dpipe_header_help(void)
4241 pr_err("Usage: devlink dpipe headers show DEV\n");
4244 static int cmd_dpipe_header(struct dl
*dl
)
4246 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4247 cmd_dpipe_header_help();
4249 } else if (dl_argv_match(dl
, "show")) {
4251 return cmd_dpipe_headers_show(dl
);
4253 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4258 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4260 switch (action_type
) {
4261 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4262 return "field_modify";
4268 struct dpipe_op_info
{
4274 struct dpipe_action
{
4275 struct dpipe_op_info info
;
4279 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4280 struct dpipe_ctx
*ctx
)
4282 struct dpipe_op_info
*op_info
= &action
->info
;
4283 const char *mapping
;
4285 pr_out_str(ctx
->dl
, "type",
4286 dpipe_action_type_e2s(action
->type
));
4287 pr_out_str(ctx
->dl
, "header",
4288 dpipe_header_id2s(ctx
, op_info
->header_id
,
4289 op_info
->header_global
));
4290 pr_out_str(ctx
->dl
, "field",
4291 dpipe_field_id2s(ctx
, op_info
->header_id
,
4293 op_info
->header_global
));
4294 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4296 op_info
->header_global
);
4298 pr_out_str(ctx
->dl
, "mapping", mapping
);
4301 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4303 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4306 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4307 if (err
!= MNL_CB_OK
)
4310 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4311 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4312 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4313 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4317 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4318 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4319 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4320 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4325 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4326 struct nlattr
*nla_actions
)
4328 struct nlattr
*nla_action
;
4329 struct dpipe_action action
;
4331 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4332 pr_out_entry_start(ctx
->dl
);
4333 if (dpipe_action_parse(&action
, nla_action
))
4334 goto err_action_parse
;
4335 pr_out_dpipe_action(&action
, ctx
);
4336 pr_out_entry_end(ctx
->dl
);
4341 pr_out_entry_end(ctx
->dl
);
4346 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4348 switch (match_type
) {
4349 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4350 return "field_exact";
4356 struct dpipe_match
{
4357 struct dpipe_op_info info
;
4361 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4362 struct dpipe_ctx
*ctx
)
4364 struct dpipe_op_info
*op_info
= &match
->info
;
4365 const char *mapping
;
4367 pr_out_str(ctx
->dl
, "type",
4368 dpipe_match_type_e2s(match
->type
));
4369 pr_out_str(ctx
->dl
, "header",
4370 dpipe_header_id2s(ctx
, op_info
->header_id
,
4371 op_info
->header_global
));
4372 pr_out_str(ctx
->dl
, "field",
4373 dpipe_field_id2s(ctx
, op_info
->header_id
,
4375 op_info
->header_global
));
4376 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4378 op_info
->header_global
);
4380 pr_out_str(ctx
->dl
, "mapping", mapping
);
4383 static int dpipe_match_parse(struct dpipe_match
*match
,
4387 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4390 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4391 if (err
!= MNL_CB_OK
)
4394 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4395 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4396 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4397 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4401 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4402 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4403 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4404 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4409 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4410 struct nlattr
*nla_matches
)
4412 struct nlattr
*nla_match
;
4413 struct dpipe_match match
;
4415 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4416 pr_out_entry_start(ctx
->dl
);
4417 if (dpipe_match_parse(&match
, nla_match
))
4418 goto err_match_parse
;
4419 pr_out_dpipe_match(&match
, ctx
);
4420 pr_out_entry_end(ctx
->dl
);
4425 pr_out_entry_end(ctx
->dl
);
4429 static struct resource
*
4430 resource_find(struct resources
*resources
, struct resource
*resource
,
4431 uint64_t resource_id
)
4433 struct list_head
*list_head
;
4436 list_head
= &resources
->resource_list
;
4438 list_head
= &resource
->resource_list
;
4440 list_for_each_entry(resource
, list_head
, list
) {
4441 struct resource
*child_resource
;
4443 if (resource
->id
== resource_id
)
4446 child_resource
= resource_find(resources
, resource
,
4449 return child_resource
;
4455 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4456 uint64_t resource_id
)
4458 struct resource
*resource
, *parent_resource
;
4459 const char del
[] = "/";
4463 resource
= resource_find(resources
, NULL
, resource_id
);
4467 for (parent_resource
= resource
; parent_resource
;
4468 parent_resource
= parent_resource
->parent
)
4469 path_len
+= strlen(parent_resource
->name
) + 1;
4472 path
= calloc(1, path_len
);
4476 path
+= path_len
- 1;
4477 for (parent_resource
= resource
; parent_resource
;
4478 parent_resource
= parent_resource
->parent
) {
4479 path
-= strlen(parent_resource
->name
);
4480 memcpy(path
, parent_resource
->name
,
4481 strlen(parent_resource
->name
));
4482 path
-= strlen(del
);
4483 memcpy(path
, del
, strlen(del
));
4485 pr_out_str(dl
, "resource_path", path
);
4489 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4491 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4492 struct dpipe_table
*table
;
4493 uint32_t resource_units
;
4494 bool counters_enabled
;
4495 bool resource_valid
;
4499 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4500 if (err
!= MNL_CB_OK
)
4503 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4504 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4505 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4506 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4507 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4511 table
= dpipe_table_alloc();
4515 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4516 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4517 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4519 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4521 if (resource_valid
) {
4522 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4523 table
->resource_valid
= true;
4526 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4527 if (!ctx
->print_tables
)
4530 pr_out_str(ctx
->dl
, "name", table
->name
);
4531 pr_out_uint(ctx
->dl
, "size", size
);
4532 pr_out_str(ctx
->dl
, "counters_enabled",
4533 counters_enabled
? "true" : "false");
4535 if (resource_valid
) {
4536 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4537 resource_path_print(ctx
->dl
, ctx
->resources
,
4538 table
->resource_id
);
4539 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4542 pr_out_array_start(ctx
->dl
, "match");
4543 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4544 goto err_matches_show
;
4545 pr_out_array_end(ctx
->dl
);
4547 pr_out_array_start(ctx
->dl
, "action");
4548 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4549 goto err_actions_show
;
4550 pr_out_array_end(ctx
->dl
);
4556 pr_out_array_end(ctx
->dl
);
4560 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4562 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4563 struct nlattr
*nla_table
;
4565 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4566 if (ctx
->print_tables
)
4567 pr_out_handle_start_arr(ctx
->dl
, tb
);
4568 if (dpipe_table_show(ctx
, nla_table
))
4569 goto err_table_show
;
4570 if (ctx
->print_tables
)
4571 pr_out_handle_end(ctx
->dl
);
4576 if (ctx
->print_tables
)
4577 pr_out_handle_end(ctx
->dl
);
4581 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4583 struct dpipe_ctx
*ctx
= data
;
4584 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4585 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4587 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4588 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4589 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4590 return MNL_CB_ERROR
;
4592 if (dpipe_tables_show(ctx
, tb
))
4593 return MNL_CB_ERROR
;
4597 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4599 static int cmd_dpipe_table_show(struct dl
*dl
)
4601 struct nlmsghdr
*nlh
;
4602 struct dpipe_ctx dpipe_ctx
= {};
4603 struct resource_ctx resource_ctx
= {};
4604 uint16_t flags
= NLM_F_REQUEST
;
4607 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4611 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4613 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4617 dpipe_ctx
.print_tables
= true;
4619 dl_opts_put(nlh
, dl
);
4620 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4623 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4624 goto err_headers_get
;
4627 err
= resource_ctx_init(&resource_ctx
, dl
);
4629 goto err_resource_ctx_init
;
4631 resource_ctx
.print_resources
= false;
4632 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4633 dl_opts_put(nlh
, dl
);
4634 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4637 dpipe_ctx
.resources
= resource_ctx
.resources
;
4639 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4640 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4641 dl_opts_put(nlh
, dl
);
4643 pr_out_section_start(dl
, "table");
4644 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4645 pr_out_section_end(dl
);
4647 resource_ctx_fini(&resource_ctx
);
4648 dpipe_ctx_fini(&dpipe_ctx
);
4651 err_resource_ctx_init
:
4653 dpipe_ctx_fini(&dpipe_ctx
);
4657 static int cmd_dpipe_table_set(struct dl
*dl
)
4659 struct nlmsghdr
*nlh
;
4662 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4663 NLM_F_REQUEST
| NLM_F_ACK
);
4665 err
= dl_argv_parse_put(nlh
, dl
,
4666 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4667 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4671 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4674 enum dpipe_value_type
{
4675 DPIPE_VALUE_TYPE_VALUE
,
4676 DPIPE_VALUE_TYPE_MASK
,
4680 dpipe_value_type_e2s(enum dpipe_value_type type
)
4683 case DPIPE_VALUE_TYPE_VALUE
:
4685 case DPIPE_VALUE_TYPE_MASK
:
4686 return "value_mask";
4692 struct dpipe_field_printer
{
4693 unsigned int field_id
;
4694 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4697 struct dpipe_header_printer
{
4698 struct dpipe_field_printer
*printers
;
4699 unsigned int printers_count
;
4700 unsigned int header_id
;
4703 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4704 enum dpipe_value_type type
,
4707 struct in_addr ip_addr
;
4709 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4710 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4714 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4715 enum dpipe_value_type type
,
4718 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4719 ether_ntoa((struct ether_addr
*)value
));
4722 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4723 enum dpipe_value_type type
,
4726 char str
[INET6_ADDRSTRLEN
];
4728 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4729 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4732 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4734 .printer
= dpipe_field_printer_ipv4_addr
,
4735 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4739 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4740 .printers
= dpipe_field_printers_ipv4
,
4741 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4742 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4745 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4747 .printer
= dpipe_field_printer_ethernet_addr
,
4748 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4752 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4753 .printers
= dpipe_field_printers_ethernet
,
4754 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4755 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4758 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4760 .printer
= dpipe_field_printer_ipv6_addr
,
4761 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4765 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4766 .printers
= dpipe_field_printers_ipv6
,
4767 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4768 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4771 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4772 &dpipe_header_printer_ipv4
,
4773 &dpipe_header_printer_ethernet
,
4774 &dpipe_header_printer_ipv6
,
4777 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4778 struct dpipe_op_info
*info
,
4779 enum dpipe_value_type type
,
4782 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4783 struct dpipe_header_printer
*header_printer
;
4784 struct dpipe_field_printer
*field_printer
;
4785 unsigned int field_printers_count
;
4789 for (i
= 0; i
< header_printers_count
; i
++) {
4790 header_printer
= dpipe_header_printers
[i
];
4791 if (header_printer
->header_id
!= info
->header_id
)
4793 field_printers_count
= header_printer
->printers_count
;
4794 for (j
= 0; j
< field_printers_count
; j
++) {
4795 field_printer
= &header_printer
->printers
[j
];
4796 if (field_printer
->field_id
!= info
->field_id
)
4798 field_printer
->printer(ctx
, type
, value
);
4806 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4808 unsigned int value_len
,
4809 struct dpipe_op_info
*info
,
4810 enum dpipe_value_type type
)
4812 if (info
->header_global
&&
4813 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4816 if (value_len
== sizeof(uint32_t)) {
4817 uint32_t *value_32
= value
;
4819 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4823 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4824 struct nlattr
**nla_match_value
,
4825 struct dpipe_op_info
*info
)
4827 void *value
, *value_mask
;
4828 uint32_t value_mapping
;
4832 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4833 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4835 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4836 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4839 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4840 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4844 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4845 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4846 DPIPE_VALUE_TYPE_MASK
);
4849 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4852 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4855 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4856 struct dpipe_match match
;
4859 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4860 if (err
!= MNL_CB_OK
)
4863 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4864 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4868 pr_out_entry_start(ctx
->dl
);
4869 if (dpipe_match_parse(&match
,
4870 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4871 goto err_match_parse
;
4872 pr_out_dpipe_match(&match
, ctx
);
4873 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4874 pr_out_entry_end(ctx
->dl
);
4879 pr_out_entry_end(ctx
->dl
);
4883 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4886 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4887 struct dpipe_action action
;
4890 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4891 if (err
!= MNL_CB_OK
)
4894 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4895 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4899 pr_out_entry_start(ctx
->dl
);
4900 if (dpipe_action_parse(&action
,
4901 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4902 goto err_action_parse
;
4903 pr_out_dpipe_action(&action
, ctx
);
4904 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4905 pr_out_entry_end(ctx
->dl
);
4910 pr_out_entry_end(ctx
->dl
);
4915 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4916 struct nlattr
*nla_action_values
)
4918 struct nlattr
*nla_action_value
;
4920 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4921 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
4928 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
4929 struct nlattr
*nla_match_values
)
4931 struct nlattr
*nla_match_value
;
4933 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
4934 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
4940 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4942 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
4943 uint32_t entry_index
;
4947 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
4948 if (err
!= MNL_CB_OK
)
4951 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
4952 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
4953 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
4957 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
4958 pr_out_uint(ctx
->dl
, "index", entry_index
);
4960 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
4961 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
4962 pr_out_uint(ctx
->dl
, "counter", counter
);
4965 pr_out_array_start(ctx
->dl
, "match_value");
4966 if (dpipe_tables_match_values_show(ctx
,
4967 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
4968 goto err_match_values_show
;
4969 pr_out_array_end(ctx
->dl
);
4971 pr_out_array_start(ctx
->dl
, "action_value");
4972 if (dpipe_tables_action_values_show(ctx
,
4973 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
4974 goto err_action_values_show
;
4975 pr_out_array_end(ctx
->dl
);
4978 err_action_values_show
:
4979 err_match_values_show
:
4980 pr_out_array_end(ctx
->dl
);
4984 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4986 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
4987 struct nlattr
*nla_entry
;
4989 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
4990 pr_out_handle_start_arr(ctx
->dl
, tb
);
4991 if (dpipe_entry_show(ctx
, nla_entry
))
4992 goto err_entry_show
;
4993 pr_out_handle_end(ctx
->dl
);
4998 pr_out_handle_end(ctx
->dl
);
5002 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5004 struct dpipe_ctx
*ctx
= data
;
5005 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5006 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5008 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5009 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5010 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5011 return MNL_CB_ERROR
;
5013 if (dpipe_table_entries_show(ctx
, tb
))
5014 return MNL_CB_ERROR
;
5018 static int cmd_dpipe_table_dump(struct dl
*dl
)
5020 struct nlmsghdr
*nlh
;
5021 struct dpipe_ctx ctx
= {};
5022 uint16_t flags
= NLM_F_REQUEST
;
5025 err
= dpipe_ctx_init(&ctx
, dl
);
5029 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5033 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5034 dl_opts_put(nlh
, dl
);
5035 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5037 pr_err("error get headers %s\n", strerror(ctx
.err
));
5041 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5042 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5043 dl_opts_put(nlh
, dl
);
5045 pr_out_section_start(dl
, "table_entry");
5046 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5047 pr_out_section_end(dl
);
5049 dpipe_ctx_fini(&ctx
);
5053 static void cmd_dpipe_table_help(void)
5055 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5056 "where OBJECT-LIST := { show | set | dump }\n");
5059 static int cmd_dpipe_table(struct dl
*dl
)
5061 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5062 cmd_dpipe_table_help();
5064 } else if (dl_argv_match(dl
, "show")) {
5066 return cmd_dpipe_table_show(dl
);
5067 } else if (dl_argv_match(dl
, "set")) {
5069 return cmd_dpipe_table_set(dl
);
5070 } else if (dl_argv_match(dl
, "dump")) {
5072 return cmd_dpipe_table_dump(dl
);
5074 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5078 static void cmd_dpipe_help(void)
5080 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5081 "where OBJECT-LIST := { header | table }\n");
5084 static int cmd_dpipe(struct dl
*dl
)
5086 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5089 } else if (dl_argv_match(dl
, "header")) {
5091 return cmd_dpipe_header(dl
);
5092 } else if (dl_argv_match(dl
, "table")) {
5094 return cmd_dpipe_table(dl
);
5096 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5101 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5102 struct nlattr
**nla_resource
)
5104 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5105 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5106 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5107 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5108 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5109 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5110 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5114 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5115 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5116 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5117 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5118 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5119 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5120 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5122 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5123 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5125 resource
->size_new
= resource
->size
;
5127 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5128 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5129 resource
->occ_valid
= true;
5132 if (resource
->size_new
!= resource
->size
)
5133 ctx
->pending_change
= true;
5139 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5140 struct resource
*parent_resource
, struct nlattr
*nl
)
5142 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5143 struct nlattr
*nla_child_resource
;
5144 struct nlattr
*nla_resources
;
5154 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5155 if (err
!= MNL_CB_OK
)
5158 err
= resource_parse(ctx
, resource
, nla_resource
);
5162 resource
->parent
= parent_resource
;
5163 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5166 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5167 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5169 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5170 struct resource
*child_resource
;
5171 struct list_head
*list
;
5173 child_resource
= resource_alloc();
5174 if (!child_resource
)
5178 list
= &ctx
->resources
->resource_list
;
5180 list
= &resource
->resource_list
;
5182 list_add_tail(&child_resource
->list
, list
);
5183 err
= resource_get(ctx
, child_resource
, resource
,
5184 nla_child_resource
);
5192 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5195 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5196 default: return "<unknown unit>";
5200 static void resource_show(struct resource
*resource
,
5201 struct resource_ctx
*ctx
)
5203 struct resource
*child_resource
;
5204 struct dpipe_table
*table
;
5205 struct dl
*dl
= ctx
->dl
;
5208 pr_out_str(dl
, "name", resource
->name
);
5210 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5211 pr_out_u64(dl
, "size", resource
->size
);
5212 if (resource
->size
!= resource
->size_new
)
5213 pr_out_u64(dl
, "size_new", resource
->size_new
);
5214 if (resource
->occ_valid
)
5215 pr_out_uint(dl
, "occ", resource
->size_occ
);
5216 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5218 if (resource
->size_min
!= resource
->size_max
) {
5219 pr_out_uint(dl
, "size_min", resource
->size_min
);
5220 pr_out_u64(dl
, "size_max", resource
->size_max
);
5221 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5224 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5225 if (table
->resource_id
== resource
->id
&&
5226 table
->resource_valid
)
5230 pr_out_array_start(dl
, "dpipe_tables");
5232 pr_out_str(dl
, "dpipe_tables", "none");
5234 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5235 if (table
->resource_id
!= resource
->id
||
5236 !table
->resource_valid
)
5238 pr_out_entry_start(dl
);
5239 pr_out_str(dl
, "table_name", table
->name
);
5240 pr_out_entry_end(dl
);
5243 pr_out_array_end(dl
);
5245 if (list_empty(&resource
->resource_list
))
5248 if (ctx
->pending_change
)
5249 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5251 pr_out_array_start(dl
, "resources");
5252 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5253 pr_out_entry_start(dl
);
5254 resource_show(child_resource
, ctx
);
5255 pr_out_entry_end(dl
);
5257 pr_out_array_end(dl
);
5261 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5263 struct resources
*resources
= ctx
->resources
;
5264 struct resource
*resource
;
5266 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5267 pr_out_handle_start_arr(ctx
->dl
, tb
);
5268 resource_show(resource
, ctx
);
5269 pr_out_handle_end(ctx
->dl
);
5273 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5275 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5278 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5280 struct resource_ctx
*ctx
= data
;
5281 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5282 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5285 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5286 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5287 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5288 return MNL_CB_ERROR
;
5290 err
= resources_get(ctx
, tb
);
5293 return MNL_CB_ERROR
;
5296 if (ctx
->print_resources
)
5297 resources_show(ctx
, tb
);
5302 static int cmd_resource_show(struct dl
*dl
)
5304 struct nlmsghdr
*nlh
;
5305 struct dpipe_ctx dpipe_ctx
= {};
5306 struct resource_ctx resource_ctx
= {};
5309 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5313 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5315 dl_opts_put(nlh
, dl
);
5317 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5321 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5324 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5328 err
= resource_ctx_init(&resource_ctx
, dl
);
5332 resource_ctx
.print_resources
= true;
5333 resource_ctx
.tables
= dpipe_ctx
.tables
;
5334 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5335 NLM_F_REQUEST
| NLM_F_ACK
);
5336 dl_opts_put(nlh
, dl
);
5337 pr_out_section_start(dl
, "resources");
5338 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5340 pr_out_section_end(dl
);
5341 resource_ctx_fini(&resource_ctx
);
5343 dpipe_ctx_fini(&dpipe_ctx
);
5347 static void cmd_resource_help(void)
5349 pr_err("Usage: devlink resource show DEV\n"
5350 " devlink resource set DEV path PATH size SIZE\n");
5353 static struct resource
*
5354 resource_find_by_name(struct list_head
*list
, char *name
)
5356 struct resource
*resource
;
5358 list_for_each_entry(resource
, list
, list
) {
5359 if (!strcmp(resource
->name
, name
))
5366 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5367 uint32_t *p_resource_id
, bool *p_resource_valid
)
5369 struct resource
*resource
;
5370 uint32_t resource_id
= 0;
5371 char *resource_path_dup
;
5372 struct list_head
*list
;
5373 const char del
[] = "/";
5374 char *resource_name
;
5376 resource_path_dup
= strdup(resource_path
);
5377 list
= &ctx
->resources
->resource_list
;
5378 resource_name
= strtok(resource_path_dup
, del
);
5379 while (resource_name
!= NULL
) {
5380 resource
= resource_find_by_name(list
, resource_name
);
5382 goto err_resource_lookup
;
5384 list
= &resource
->resource_list
;
5385 resource_name
= strtok(NULL
, del
);
5386 resource_id
= resource
->id
;
5388 free(resource_path_dup
);
5389 *p_resource_valid
= true;
5390 *p_resource_id
= resource_id
;
5393 err_resource_lookup
:
5394 free(resource_path_dup
);
5398 static int cmd_resource_set(struct dl
*dl
)
5400 struct nlmsghdr
*nlh
;
5401 struct resource_ctx ctx
= {};
5404 err
= resource_ctx_init(&ctx
, dl
);
5408 ctx
.print_resources
= false;
5409 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5410 DL_OPT_RESOURCE_SIZE
, 0);
5414 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5416 dl_opts_put(nlh
, dl
);
5417 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5419 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5423 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5424 &dl
->opts
.resource_id
,
5425 &dl
->opts
.resource_id_valid
);
5427 pr_err("error parsing resource path %s\n", strerror(-err
));
5431 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5432 NLM_F_REQUEST
| NLM_F_ACK
);
5434 dl_opts_put(nlh
, dl
);
5435 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5437 resource_ctx_fini(&ctx
);
5441 static int cmd_resource(struct dl
*dl
)
5443 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5444 cmd_resource_help();
5446 } else if (dl_argv_match(dl
, "show")) {
5448 return cmd_resource_show(dl
);
5449 } else if (dl_argv_match(dl
, "set")) {
5451 return cmd_resource_set(dl
);
5453 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5457 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5459 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5460 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5461 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5464 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5465 if (dl
->json_output
) {
5466 jsonw_name(dl
->jw
, buf
);
5467 jsonw_start_object(dl
->jw
);
5473 static void pr_out_region_handle_end(struct dl
*dl
)
5475 if (dl
->json_output
)
5476 jsonw_end_object(dl
->jw
);
5481 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5483 if (dl
->json_output
) {
5484 jsonw_name(dl
->jw
, "snapshot");
5485 jsonw_start_array(dl
->jw
);
5487 if (g_indent_newline
)
5488 pr_out("snapshot %s", array
? "[" : "");
5490 pr_out(" snapshot %s", array
? "[" : "");
5494 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5496 if (dl
->json_output
)
5497 jsonw_end_array(dl
->jw
);
5502 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5504 uint32_t snapshot_id
;
5506 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5509 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5511 if (dl
->json_output
)
5512 jsonw_uint(dl
->jw
, snapshot_id
);
5514 pr_out("%s%u", index
? " " : "", snapshot_id
);
5517 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5519 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5520 struct nlattr
*nla_sanpshot
;
5523 pr_out_region_snapshots_start(dl
, true);
5524 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5525 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5526 if (err
!= MNL_CB_OK
)
5528 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5530 pr_out_region_snapshots_end(dl
, true);
5533 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5535 pr_out_region_snapshots_start(dl
, false);
5536 pr_out_region_snapshots_id(dl
, tb
, 0);
5537 pr_out_region_snapshots_end(dl
, false);
5540 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5542 pr_out_region_handle_start(dl
, tb
);
5544 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5545 pr_out_u64(dl
, "size",
5546 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5548 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5549 pr_out_snapshots(dl
, tb
);
5551 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5552 pr_out_snapshot(dl
, tb
);
5554 pr_out_region_handle_end(dl
);
5557 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5559 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5560 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5561 struct dl
*dl
= data
;
5563 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5564 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5565 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5566 return MNL_CB_ERROR
;
5568 pr_out_region(dl
, tb
);
5573 static int cmd_region_show(struct dl
*dl
)
5575 struct nlmsghdr
*nlh
;
5576 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5579 if (dl_argc(dl
) == 0)
5580 flags
|= NLM_F_DUMP
;
5582 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5584 if (dl_argc(dl
) > 0) {
5585 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5590 pr_out_section_start(dl
, "regions");
5591 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5592 pr_out_section_end(dl
);
5596 static int cmd_region_snapshot_del(struct dl
*dl
)
5598 struct nlmsghdr
*nlh
;
5601 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5602 NLM_F_REQUEST
| NLM_F_ACK
);
5604 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5605 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5609 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5612 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5614 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5615 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5616 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5617 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5618 struct dl
*dl
= data
;
5621 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5622 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5623 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5624 return MNL_CB_ERROR
;
5626 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5627 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5628 if (err
!= MNL_CB_OK
)
5629 return MNL_CB_ERROR
;
5631 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5632 if (!nla_chunk_data
)
5635 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5636 if (!nla_chunk_addr
)
5639 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5640 mnl_attr_get_payload_len(nla_chunk_data
),
5641 mnl_attr_get_u64(nla_chunk_addr
));
5646 static int cmd_region_dump(struct dl
*dl
)
5648 struct nlmsghdr
*nlh
;
5651 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5652 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5654 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5655 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5659 pr_out_section_start(dl
, "dump");
5660 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5661 pr_out_section_end(dl
);
5662 if (!dl
->json_output
)
5667 static int cmd_region_read(struct dl
*dl
)
5669 struct nlmsghdr
*nlh
;
5672 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5673 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5675 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5676 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5677 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5681 pr_out_section_start(dl
, "read");
5682 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5683 pr_out_section_end(dl
);
5684 if (!dl
->json_output
)
5689 static void cmd_region_help(void)
5691 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5692 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5693 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5694 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5697 static int cmd_region(struct dl
*dl
)
5699 if (dl_no_arg(dl
)) {
5700 return cmd_region_show(dl
);
5701 } else if (dl_argv_match(dl
, "help")) {
5704 } else if (dl_argv_match(dl
, "show")) {
5706 return cmd_region_show(dl
);
5707 } else if (dl_argv_match(dl
, "del")) {
5709 return cmd_region_snapshot_del(dl
);
5710 } else if (dl_argv_match(dl
, "dump")) {
5712 return cmd_region_dump(dl
);
5713 } else if (dl_argv_match(dl
, "read")) {
5715 return cmd_region_read(dl
);
5717 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5721 static void help(void)
5723 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5724 " devlink [ -f[orce] ] -b[atch] filename\n"
5725 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5726 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5729 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5734 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5737 } else if (dl_argv_match(dl
, "dev")) {
5740 } else if (dl_argv_match(dl
, "port")) {
5742 return cmd_port(dl
);
5743 } else if (dl_argv_match(dl
, "sb")) {
5746 } else if (dl_argv_match(dl
, "monitor")) {
5749 } else if (dl_argv_match(dl
, "dpipe")) {
5751 return cmd_dpipe(dl
);
5752 } else if (dl_argv_match(dl
, "resource")) {
5754 return cmd_resource(dl
);
5755 } else if (dl_argv_match(dl
, "region")) {
5757 return cmd_region(dl
);
5759 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5763 static int dl_init(struct dl
*dl
)
5767 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5769 pr_err("Failed to connect to devlink Netlink\n");
5773 err
= ifname_map_init(dl
);
5775 pr_err("Failed to create index map\n");
5776 goto err_ifname_map_create
;
5778 if (dl
->json_output
) {
5779 dl
->jw
= jsonw_new(stdout
);
5781 pr_err("Failed to create JSON writer\n");
5784 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5789 ifname_map_fini(dl
);
5790 err_ifname_map_create
:
5791 mnlg_socket_close(dl
->nlg
);
5795 static void dl_fini(struct dl
*dl
)
5797 if (dl
->json_output
)
5798 jsonw_destroy(&dl
->jw
);
5799 ifname_map_fini(dl
);
5800 mnlg_socket_close(dl
->nlg
);
5803 static struct dl
*dl_alloc(void)
5807 dl
= calloc(1, sizeof(*dl
));
5813 static void dl_free(struct dl
*dl
)
5818 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5822 int ret
= EXIT_SUCCESS
;
5824 if (name
&& strcmp(name
, "-") != 0) {
5825 if (freopen(name
, "r", stdin
) == NULL
) {
5827 "Cannot open file \"%s\" for reading: %s\n",
5828 name
, strerror(errno
));
5829 return EXIT_FAILURE
;
5834 while (getcmdline(&line
, &len
, stdin
) != -1) {
5838 largc
= makeargs(line
, largv
, 100);
5840 continue; /* blank line */
5842 if (dl_cmd(dl
, largc
, largv
)) {
5843 fprintf(stderr
, "Command failed %s:%d\n",
5857 int main(int argc
, char **argv
)
5859 static const struct option long_options
[] = {
5860 { "Version", no_argument
, NULL
, 'V' },
5861 { "force", no_argument
, NULL
, 'f' },
5862 { "batch", required_argument
, NULL
, 'b' },
5863 { "no-nice-names", no_argument
, NULL
, 'n' },
5864 { "json", no_argument
, NULL
, 'j' },
5865 { "pretty", no_argument
, NULL
, 'p' },
5866 { "verbose", no_argument
, NULL
, 'v' },
5867 { NULL
, 0, NULL
, 0 }
5869 const char *batch_file
= NULL
;
5878 pr_err("Failed to allocate memory for devlink\n");
5879 return EXIT_FAILURE
;
5882 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5883 long_options
, NULL
)) >= 0) {
5887 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5894 batch_file
= optarg
;
5897 dl
->no_nice_names
= true;
5900 dl
->json_output
= true;
5903 dl
->pretty_output
= true;
5909 pr_err("Unknown option.\n");
5926 err
= dl_batch(dl
, batch_file
, force
);
5928 err
= dl_cmd(dl
, argc
, argv
);