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 if (dl
->json_output
) {
1655 jsonw_u64_field(dl
->jw
, name
, val
);
1657 if (g_indent_newline
)
1658 pr_out("%s %lu", name
, val
);
1660 pr_out(" %s %lu", name
, val
);
1664 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1666 if (dl
->json_output
) {
1667 jsonw_name(dl
->jw
, "address");
1668 jsonw_uint(dl
->jw
, addr
);
1669 jsonw_name(dl
->jw
, "data");
1670 jsonw_start_array(dl
->jw
);
1674 static void pr_out_region_chunk_end(struct dl
*dl
)
1676 if (dl
->json_output
)
1677 jsonw_end_array(dl
->jw
);
1680 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1683 static uint64_t align_val
;
1686 pr_out_region_chunk_start(dl
, addr
);
1688 if (!dl
->json_output
)
1689 if (!(align_val
% 16))
1690 pr_out("%s%016"PRIx64
" ",
1691 align_val
? "\n" : "",
1696 if (dl
->json_output
)
1697 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1699 pr_out("%02x ", data
[i
]);
1704 pr_out_region_chunk_end(dl
);
1707 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1709 pr_out_handle(dl
, tb
);
1712 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1714 if (dl
->json_output
) {
1715 jsonw_start_object(dl
->jw
);
1716 jsonw_name(dl
->jw
, name
);
1717 jsonw_start_object(dl
->jw
);
1721 static void pr_out_section_end(struct dl
*dl
)
1723 if (dl
->json_output
) {
1724 if (dl
->arr_last
.present
)
1725 jsonw_end_array(dl
->jw
);
1726 jsonw_end_object(dl
->jw
);
1727 jsonw_end_object(dl
->jw
);
1731 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1733 if (dl
->json_output
) {
1734 jsonw_name(dl
->jw
, name
);
1735 jsonw_start_array(dl
->jw
);
1737 __pr_out_indent_inc();
1739 pr_out("%s:", name
);
1740 __pr_out_indent_inc();
1745 static void pr_out_array_end(struct dl
*dl
)
1747 if (dl
->json_output
) {
1748 jsonw_end_array(dl
->jw
);
1750 __pr_out_indent_dec();
1751 __pr_out_indent_dec();
1755 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1757 if (dl
->json_output
) {
1758 jsonw_name(dl
->jw
, name
);
1759 jsonw_start_object(dl
->jw
);
1761 __pr_out_indent_inc();
1763 pr_out("%s:", name
);
1764 __pr_out_indent_inc();
1769 static void pr_out_object_end(struct dl
*dl
)
1771 if (dl
->json_output
) {
1772 jsonw_end_object(dl
->jw
);
1774 __pr_out_indent_dec();
1775 __pr_out_indent_dec();
1779 static void pr_out_entry_start(struct dl
*dl
)
1781 if (dl
->json_output
)
1782 jsonw_start_object(dl
->jw
);
1785 static void pr_out_entry_end(struct dl
*dl
)
1787 if (dl
->json_output
)
1788 jsonw_end_object(dl
->jw
);
1793 static const char *param_cmode_name(uint8_t cmode
)
1796 case DEVLINK_PARAM_CMODE_RUNTIME
:
1797 return PARAM_CMODE_RUNTIME_STR
;
1798 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1799 return PARAM_CMODE_DRIVERINIT_STR
;
1800 case DEVLINK_PARAM_CMODE_PERMANENT
:
1801 return PARAM_CMODE_PERMANENT_STR
;
1802 default: return "<unknown type>";
1806 static const char *eswitch_mode_name(uint32_t mode
)
1809 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1810 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1811 default: return "<unknown mode>";
1815 static const char *eswitch_inline_mode_name(uint32_t mode
)
1818 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1819 return ESWITCH_INLINE_MODE_NONE
;
1820 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1821 return ESWITCH_INLINE_MODE_LINK
;
1822 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1823 return ESWITCH_INLINE_MODE_NETWORK
;
1824 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1825 return ESWITCH_INLINE_MODE_TRANSPORT
;
1827 return "<unknown mode>";
1831 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1833 __pr_out_handle_start(dl
, tb
, true, false);
1835 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1836 pr_out_str(dl
, "mode",
1837 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1839 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1840 pr_out_str(dl
, "inline-mode",
1841 eswitch_inline_mode_name(mnl_attr_get_u8(
1842 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1844 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1845 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1847 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1850 pr_out_handle_end(dl
);
1853 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1855 struct dl
*dl
= data
;
1856 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1857 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1859 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1860 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1861 return MNL_CB_ERROR
;
1862 pr_out_eswitch(dl
, tb
);
1866 static int cmd_dev_eswitch_show(struct dl
*dl
)
1868 struct nlmsghdr
*nlh
;
1871 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1872 NLM_F_REQUEST
| NLM_F_ACK
);
1874 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1878 pr_out_section_start(dl
, "dev");
1879 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1880 pr_out_section_end(dl
);
1884 static int cmd_dev_eswitch_set(struct dl
*dl
)
1886 struct nlmsghdr
*nlh
;
1889 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1890 NLM_F_REQUEST
| NLM_F_ACK
);
1892 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1893 DL_OPT_ESWITCH_MODE
|
1894 DL_OPT_ESWITCH_INLINE_MODE
|
1895 DL_OPT_ESWITCH_ENCAP_MODE
);
1900 if (dl
->opts
.present
== 1) {
1901 pr_err("Need to set at least one option\n");
1905 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1908 static int cmd_dev_eswitch(struct dl
*dl
)
1910 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1913 } else if (dl_argv_match(dl
, "set")) {
1915 return cmd_dev_eswitch_set(dl
);
1916 } else if (dl_argv_match(dl
, "show")) {
1918 return cmd_dev_eswitch_show(dl
);
1920 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1924 struct param_val_conv
{
1930 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
1931 uint32_t len
, const char *name
)
1935 for (i
= 0; i
< len
; i
++)
1936 if (!strcmp(param_val_conv
[i
].name
, name
))
1943 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
1944 uint32_t len
, const char *name
, const char *vstr
,
1949 for (i
= 0; i
< len
; i
++)
1950 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1951 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
1952 *vuint
= param_val_conv
[i
].vuint
;
1960 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
1961 uint32_t len
, const char *name
, uint32_t vuint
,
1966 for (i
= 0; i
< len
; i
++)
1967 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1968 param_val_conv
[i
].vuint
== vuint
) {
1969 *vstr
= param_val_conv
[i
].vstr
;
1976 static const struct param_val_conv param_val_conv
[] = {
1978 .name
= "fw_load_policy",
1980 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
1983 .name
= "fw_load_policy",
1985 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
1989 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
1991 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
1992 int nla_type
, struct nlattr
*nl
)
1994 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
1995 struct nlattr
*val_attr
;
2000 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2001 if (err
!= MNL_CB_OK
)
2004 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2005 (nla_type
!= MNL_TYPE_FLAG
&&
2006 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2009 pr_out_str(dl
, "cmode",
2010 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2011 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2013 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2019 err
= param_val_conv_str_get(param_val_conv
,
2022 mnl_attr_get_u8(val_attr
),
2026 pr_out_str(dl
, "value", vstr
);
2028 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2033 err
= param_val_conv_str_get(param_val_conv
,
2036 mnl_attr_get_u16(val_attr
),
2040 pr_out_str(dl
, "value", vstr
);
2042 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2047 err
= param_val_conv_str_get(param_val_conv
,
2050 mnl_attr_get_u32(val_attr
),
2054 pr_out_str(dl
, "value", vstr
);
2056 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2059 case MNL_TYPE_STRING
:
2060 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2063 pr_out_bool(dl
, "value", val_attr
? true : false);
2068 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2070 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2071 struct nlattr
*param_value_attr
;
2072 const char *nla_name
;
2076 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2077 if (err
!= MNL_CB_OK
)
2079 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2080 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2081 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2085 pr_out_handle_start_arr(dl
, tb
);
2087 __pr_out_handle_start(dl
, tb
, true, false);
2089 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2091 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2092 pr_out_str(dl
, "name", nla_name
);
2094 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2095 pr_out_str(dl
, "type", "driver-specific");
2097 pr_out_str(dl
, "type", "generic");
2099 pr_out_array_start(dl
, "values");
2100 mnl_attr_for_each_nested(param_value_attr
,
2101 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2102 pr_out_entry_start(dl
);
2103 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2104 pr_out_entry_end(dl
);
2106 pr_out_array_end(dl
);
2107 pr_out_handle_end(dl
);
2110 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2112 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2113 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2114 struct dl
*dl
= data
;
2116 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2117 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2118 !tb
[DEVLINK_ATTR_PARAM
])
2119 return MNL_CB_ERROR
;
2120 pr_out_param(dl
, tb
, true);
2136 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2138 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2139 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2140 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2141 struct nlattr
*param_value_attr
;
2142 enum devlink_param_cmode cmode
;
2143 struct param_ctx
*ctx
= data
;
2144 struct dl
*dl
= ctx
->dl
;
2148 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2149 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2150 !tb
[DEVLINK_ATTR_PARAM
])
2151 return MNL_CB_ERROR
;
2153 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2154 if (err
!= MNL_CB_OK
)
2155 return MNL_CB_ERROR
;
2157 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2158 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2159 return MNL_CB_ERROR
;
2161 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2162 mnl_attr_for_each_nested(param_value_attr
,
2163 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2164 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2165 struct nlattr
*val_attr
;
2167 err
= mnl_attr_parse_nested(param_value_attr
,
2168 attr_cb
, nla_value
);
2169 if (err
!= MNL_CB_OK
)
2170 return MNL_CB_ERROR
;
2172 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2173 (nla_type
!= MNL_TYPE_FLAG
&&
2174 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2175 return MNL_CB_ERROR
;
2177 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2178 if (cmode
== dl
->opts
.cmode
) {
2179 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2182 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2185 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2188 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2190 case MNL_TYPE_STRING
:
2191 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2194 ctx
->value
.vbool
= val_attr
? true : false;
2200 ctx
->nla_type
= nla_type
;
2204 static int cmd_dev_param_set(struct dl
*dl
)
2206 struct param_ctx ctx
= {};
2207 struct nlmsghdr
*nlh
;
2215 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2217 DL_OPT_PARAM_VALUE
|
2218 DL_OPT_PARAM_CMODE
, 0);
2222 /* Get value type */
2223 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2224 NLM_F_REQUEST
| NLM_F_ACK
);
2225 dl_opts_put(nlh
, dl
);
2228 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2232 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2233 NLM_F_REQUEST
| NLM_F_ACK
);
2234 dl_opts_put(nlh
, dl
);
2236 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2237 dl
->opts
.param_name
);
2239 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2240 switch (ctx
.nla_type
) {
2243 err
= param_val_conv_uint_get(param_val_conv
,
2245 dl
->opts
.param_name
,
2246 dl
->opts
.param_value
,
2250 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2253 goto err_param_value_parse
;
2254 if (val_u8
== ctx
.value
.vu8
)
2256 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2260 err
= param_val_conv_uint_get(param_val_conv
,
2262 dl
->opts
.param_name
,
2263 dl
->opts
.param_value
,
2267 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2270 goto err_param_value_parse
;
2271 if (val_u16
== ctx
.value
.vu16
)
2273 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2277 err
= param_val_conv_uint_get(param_val_conv
,
2279 dl
->opts
.param_name
,
2280 dl
->opts
.param_value
,
2283 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2285 goto err_param_value_parse
;
2286 if (val_u32
== ctx
.value
.vu32
)
2288 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2291 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2293 goto err_param_value_parse
;
2294 if (val_bool
== ctx
.value
.vbool
)
2297 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2300 case MNL_TYPE_STRING
:
2301 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2302 dl
->opts
.param_value
);
2303 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2307 printf("Value type not supported\n");
2310 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2312 err_param_value_parse
:
2313 pr_err("Value \"%s\" is not a number or not within range\n",
2314 dl
->opts
.param_value
);
2318 static int cmd_dev_param_show(struct dl
*dl
)
2320 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2321 struct nlmsghdr
*nlh
;
2324 if (dl_argc(dl
) == 0)
2325 flags
|= NLM_F_DUMP
;
2327 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2329 if (dl_argc(dl
) > 0) {
2330 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2331 DL_OPT_PARAM_NAME
, 0);
2336 pr_out_section_start(dl
, "param");
2337 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2338 pr_out_section_end(dl
);
2342 static int cmd_dev_param(struct dl
*dl
)
2344 if (dl_argv_match(dl
, "help")) {
2347 } else if (dl_argv_match(dl
, "show") ||
2348 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2350 return cmd_dev_param_show(dl
);
2351 } else if (dl_argv_match(dl
, "set")) {
2353 return cmd_dev_param_set(dl
);
2355 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2358 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2360 struct dl
*dl
= data
;
2361 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2362 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2364 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2365 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2366 return MNL_CB_ERROR
;
2371 static int cmd_dev_show(struct dl
*dl
)
2373 struct nlmsghdr
*nlh
;
2374 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2377 if (dl_argc(dl
) == 0)
2378 flags
|= NLM_F_DUMP
;
2380 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2382 if (dl_argc(dl
) > 0) {
2383 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2388 pr_out_section_start(dl
, "dev");
2389 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2390 pr_out_section_end(dl
);
2394 static void cmd_dev_reload_help(void)
2396 pr_err("Usage: devlink dev reload [ DEV ]\n");
2399 static int cmd_dev_reload(struct dl
*dl
)
2401 struct nlmsghdr
*nlh
;
2404 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2405 cmd_dev_reload_help();
2409 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2410 NLM_F_REQUEST
| NLM_F_ACK
);
2412 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2416 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2419 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2420 const char *name
, int type
)
2422 struct nlattr
*version
;
2424 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2425 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2426 const char *ver_value
;
2427 const char *ver_name
;
2430 if (mnl_attr_get_type(version
) != type
)
2433 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2434 if (err
!= MNL_CB_OK
)
2437 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2438 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2442 pr_out_object_start(dl
, name
);
2446 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2447 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2449 pr_out_str(dl
, ver_name
, ver_value
);
2450 if (!dl
->json_output
)
2455 pr_out_object_end(dl
);
2458 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2459 struct nlattr
**tb
, bool has_versions
)
2461 __pr_out_handle_start(dl
, tb
, true, false);
2463 __pr_out_indent_inc();
2464 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2465 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2467 if (!dl
->json_output
)
2469 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2472 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2473 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2475 if (!dl
->json_output
)
2477 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2479 __pr_out_indent_dec();
2482 pr_out_object_start(dl
, "versions");
2484 pr_out_versions_single(dl
, nlh
, "fixed",
2485 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2486 pr_out_versions_single(dl
, nlh
, "running",
2487 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2488 pr_out_versions_single(dl
, nlh
, "stored",
2489 DEVLINK_ATTR_INFO_VERSION_STORED
);
2491 pr_out_object_end(dl
);
2494 pr_out_handle_end(dl
);
2497 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2499 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2500 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2501 bool has_versions
, has_info
;
2502 struct dl
*dl
= data
;
2504 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2506 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2507 return MNL_CB_ERROR
;
2509 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2510 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2511 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2512 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2513 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2517 pr_out_info(dl
, nlh
, tb
, has_versions
);
2522 static void cmd_dev_info_help(void)
2524 pr_err("Usage: devlink dev info [ DEV ]\n");
2527 static int cmd_dev_info(struct dl
*dl
)
2529 struct nlmsghdr
*nlh
;
2530 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2533 if (dl_argv_match(dl
, "help")) {
2534 cmd_dev_info_help();
2538 if (dl_argc(dl
) == 0)
2539 flags
|= NLM_F_DUMP
;
2541 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2543 if (dl_argc(dl
) > 0) {
2544 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2549 pr_out_section_start(dl
, "info");
2550 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2551 pr_out_section_end(dl
);
2555 static void cmd_dev_flash_help(void)
2557 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2560 static int cmd_dev_flash(struct dl
*dl
)
2562 struct nlmsghdr
*nlh
;
2565 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2566 cmd_dev_flash_help();
2570 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2571 NLM_F_REQUEST
| NLM_F_ACK
);
2573 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2574 DL_OPT_FLASH_COMPONENT
);
2578 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2581 static int cmd_dev(struct dl
*dl
)
2583 if (dl_argv_match(dl
, "help")) {
2586 } else if (dl_argv_match(dl
, "show") ||
2587 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2589 return cmd_dev_show(dl
);
2590 } else if (dl_argv_match(dl
, "eswitch")) {
2592 return cmd_dev_eswitch(dl
);
2593 } else if (dl_argv_match(dl
, "reload")) {
2595 return cmd_dev_reload(dl
);
2596 } else if (dl_argv_match(dl
, "param")) {
2598 return cmd_dev_param(dl
);
2599 } else if (dl_argv_match(dl
, "info")) {
2601 return cmd_dev_info(dl
);
2602 } else if (dl_argv_match(dl
, "flash")) {
2604 return cmd_dev_flash(dl
);
2606 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2610 static void cmd_port_help(void)
2612 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2613 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2614 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2615 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2618 static const char *port_type_name(uint32_t type
)
2621 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2622 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2623 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2624 case DEVLINK_PORT_TYPE_IB
: return "ib";
2625 default: return "<unknown type>";
2629 static const char *port_flavour_name(uint16_t flavour
)
2632 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2634 case DEVLINK_PORT_FLAVOUR_CPU
:
2636 case DEVLINK_PORT_FLAVOUR_DSA
:
2639 return "<unknown flavour>";
2643 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2645 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2646 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2648 pr_out_port_handle_start(dl
, tb
, false);
2650 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2652 pr_out_str(dl
, "type", port_type_name(port_type
));
2654 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2656 if (port_type
!= des_port_type
)
2657 pr_out_str(dl
, "des_type",
2658 port_type_name(des_port_type
));
2661 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2662 pr_out_str(dl
, "netdev",
2663 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2664 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2665 pr_out_str(dl
, "ibdev",
2666 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2667 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2668 uint16_t port_flavour
=
2669 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2671 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2673 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2674 pr_out_uint(dl
, "split_group",
2675 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2676 pr_out_port_handle_end(dl
);
2679 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2681 struct dl
*dl
= data
;
2682 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2683 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2685 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2686 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2687 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2688 return MNL_CB_ERROR
;
2689 pr_out_port(dl
, tb
);
2693 static int cmd_port_show(struct dl
*dl
)
2695 struct nlmsghdr
*nlh
;
2696 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2699 if (dl_argc(dl
) == 0)
2700 flags
|= NLM_F_DUMP
;
2702 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2704 if (dl_argc(dl
) > 0) {
2705 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2710 pr_out_section_start(dl
, "port");
2711 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2712 pr_out_section_end(dl
);
2716 static int cmd_port_set(struct dl
*dl
)
2718 struct nlmsghdr
*nlh
;
2721 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2722 NLM_F_REQUEST
| NLM_F_ACK
);
2724 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2728 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2731 static int cmd_port_split(struct dl
*dl
)
2733 struct nlmsghdr
*nlh
;
2736 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2737 NLM_F_REQUEST
| NLM_F_ACK
);
2739 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2743 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2746 static int cmd_port_unsplit(struct dl
*dl
)
2748 struct nlmsghdr
*nlh
;
2751 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2752 NLM_F_REQUEST
| NLM_F_ACK
);
2754 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2758 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2761 static int cmd_port(struct dl
*dl
)
2763 if (dl_argv_match(dl
, "help")) {
2766 } else if (dl_argv_match(dl
, "show") ||
2767 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2769 return cmd_port_show(dl
);
2770 } else if (dl_argv_match(dl
, "set")) {
2772 return cmd_port_set(dl
);
2773 } else if (dl_argv_match(dl
, "split")) {
2775 return cmd_port_split(dl
);
2776 } else if (dl_argv_match(dl
, "unsplit")) {
2778 return cmd_port_unsplit(dl
);
2780 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2784 static void cmd_sb_help(void)
2786 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2787 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2788 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2789 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2790 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2791 pr_err(" pool POOL_INDEX ]\n");
2792 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2793 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2794 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2795 pr_err(" type { ingress | egress } ]\n");
2796 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2797 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2798 pr_err(" th THRESHOLD\n");
2799 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2800 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2801 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2804 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2806 pr_out_handle_start_arr(dl
, tb
);
2807 pr_out_uint(dl
, "sb",
2808 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2809 pr_out_uint(dl
, "size",
2810 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2811 pr_out_uint(dl
, "ing_pools",
2812 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2813 pr_out_uint(dl
, "eg_pools",
2814 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2815 pr_out_uint(dl
, "ing_tcs",
2816 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2817 pr_out_uint(dl
, "eg_tcs",
2818 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2819 pr_out_handle_end(dl
);
2822 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2824 struct dl
*dl
= data
;
2825 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2826 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2828 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2829 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2830 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2831 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2832 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2833 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2834 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2835 return MNL_CB_ERROR
;
2840 static int cmd_sb_show(struct dl
*dl
)
2842 struct nlmsghdr
*nlh
;
2843 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2846 if (dl_argc(dl
) == 0)
2847 flags
|= NLM_F_DUMP
;
2849 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2851 if (dl_argc(dl
) > 0) {
2852 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2857 pr_out_section_start(dl
, "sb");
2858 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2859 pr_out_section_end(dl
);
2863 static const char *pool_type_name(uint8_t type
)
2866 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2867 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2868 default: return "<unknown type>";
2872 static const char *threshold_type_name(uint8_t type
)
2875 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2876 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2877 default: return "<unknown type>";
2881 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2883 pr_out_handle_start_arr(dl
, tb
);
2884 pr_out_uint(dl
, "sb",
2885 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2886 pr_out_uint(dl
, "pool",
2887 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2888 pr_out_str(dl
, "type",
2889 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2890 pr_out_uint(dl
, "size",
2891 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2892 pr_out_str(dl
, "thtype",
2893 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2894 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
2895 pr_out_uint(dl
, "cell_size",
2896 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
2897 pr_out_handle_end(dl
);
2900 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2902 struct dl
*dl
= data
;
2903 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2904 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2906 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2907 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2908 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2909 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2910 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2911 return MNL_CB_ERROR
;
2912 pr_out_sb_pool(dl
, tb
);
2916 static int cmd_sb_pool_show(struct dl
*dl
)
2918 struct nlmsghdr
*nlh
;
2919 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2922 if (dl_argc(dl
) == 0)
2923 flags
|= NLM_F_DUMP
;
2925 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2927 if (dl_argc(dl
) > 0) {
2928 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2934 pr_out_section_start(dl
, "pool");
2935 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2936 pr_out_section_end(dl
);
2940 static int cmd_sb_pool_set(struct dl
*dl
)
2942 struct nlmsghdr
*nlh
;
2945 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
2946 NLM_F_REQUEST
| NLM_F_ACK
);
2948 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
2949 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
2953 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2956 static int cmd_sb_pool(struct dl
*dl
)
2958 if (dl_argv_match(dl
, "help")) {
2961 } else if (dl_argv_match(dl
, "show") ||
2962 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2964 return cmd_sb_pool_show(dl
);
2965 } else if (dl_argv_match(dl
, "set")) {
2967 return cmd_sb_pool_set(dl
);
2969 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2973 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
2975 pr_out_port_handle_start_arr(dl
, tb
, true);
2976 pr_out_uint(dl
, "sb",
2977 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2978 pr_out_uint(dl
, "pool",
2979 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2980 pr_out_uint(dl
, "threshold",
2981 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2982 pr_out_port_handle_end(dl
);
2985 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2987 struct dl
*dl
= data
;
2988 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2989 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2991 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2992 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2993 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2994 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2995 return MNL_CB_ERROR
;
2996 pr_out_sb_port_pool(dl
, tb
);
3000 static int cmd_sb_port_pool_show(struct dl
*dl
)
3002 struct nlmsghdr
*nlh
;
3003 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3006 if (dl_argc(dl
) == 0)
3007 flags
|= NLM_F_DUMP
;
3009 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3011 if (dl_argc(dl
) > 0) {
3012 err
= dl_argv_parse_put(nlh
, dl
,
3013 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3019 pr_out_section_start(dl
, "port_pool");
3020 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3021 pr_out_section_end(dl
);
3025 static int cmd_sb_port_pool_set(struct dl
*dl
)
3027 struct nlmsghdr
*nlh
;
3030 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3031 NLM_F_REQUEST
| NLM_F_ACK
);
3033 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3034 DL_OPT_SB_TH
, DL_OPT_SB
);
3038 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3041 static int cmd_sb_port_pool(struct dl
*dl
)
3043 if (dl_argv_match(dl
, "help")) {
3046 } else if (dl_argv_match(dl
, "show") ||
3047 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3049 return cmd_sb_port_pool_show(dl
);
3050 } else if (dl_argv_match(dl
, "set")) {
3052 return cmd_sb_port_pool_set(dl
);
3054 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3058 static int cmd_sb_port(struct dl
*dl
)
3060 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3063 } else if (dl_argv_match(dl
, "pool")) {
3065 return cmd_sb_port_pool(dl
);
3067 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3071 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3073 pr_out_port_handle_start_arr(dl
, tb
, true);
3074 pr_out_uint(dl
, "sb",
3075 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3076 pr_out_uint(dl
, "tc",
3077 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3078 pr_out_str(dl
, "type",
3079 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3080 pr_out_uint(dl
, "pool",
3081 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3082 pr_out_uint(dl
, "threshold",
3083 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3084 pr_out_port_handle_end(dl
);
3087 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3089 struct dl
*dl
= data
;
3090 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3091 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3093 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3094 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3095 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3096 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3097 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3098 return MNL_CB_ERROR
;
3099 pr_out_sb_tc_bind(dl
, tb
);
3103 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3105 struct nlmsghdr
*nlh
;
3106 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3109 if (dl_argc(dl
) == 0)
3110 flags
|= NLM_F_DUMP
;
3112 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3114 if (dl_argc(dl
) > 0) {
3115 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3116 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3121 pr_out_section_start(dl
, "tc_bind");
3122 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3123 pr_out_section_end(dl
);
3127 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3129 struct nlmsghdr
*nlh
;
3132 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3133 NLM_F_REQUEST
| NLM_F_ACK
);
3135 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3136 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3141 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3144 static int cmd_sb_tc_bind(struct dl
*dl
)
3146 if (dl_argv_match(dl
, "help")) {
3149 } else if (dl_argv_match(dl
, "show") ||
3150 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3152 return cmd_sb_tc_bind_show(dl
);
3153 } else if (dl_argv_match(dl
, "set")) {
3155 return cmd_sb_tc_bind_set(dl
);
3157 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3161 static int cmd_sb_tc(struct dl
*dl
)
3163 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3166 } else if (dl_argv_match(dl
, "bind")) {
3168 return cmd_sb_tc_bind(dl
);
3170 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3175 struct list_head list
;
3179 uint32_t bound_pool_index
;
3183 struct list_head list
;
3186 uint32_t port_index
;
3188 struct list_head pool_list
;
3189 struct list_head ing_tc_list
;
3190 struct list_head eg_tc_list
;
3196 struct list_head port_list
;
3199 static struct occ_item
*occ_item_alloc(void)
3201 return calloc(1, sizeof(struct occ_item
));
3204 static void occ_item_free(struct occ_item
*occ_item
)
3209 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3211 struct occ_port
*occ_port
;
3213 occ_port
= calloc(1, sizeof(*occ_port
));
3216 occ_port
->port_index
= port_index
;
3217 INIT_LIST_HEAD(&occ_port
->pool_list
);
3218 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3219 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3223 static void occ_port_free(struct occ_port
*occ_port
)
3225 struct occ_item
*occ_item
, *tmp
;
3227 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3228 occ_item_free(occ_item
);
3229 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3230 occ_item_free(occ_item
);
3231 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3232 occ_item_free(occ_item
);
3235 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3237 struct occ_show
*occ_show
;
3239 occ_show
= calloc(1, sizeof(*occ_show
));
3243 INIT_LIST_HEAD(&occ_show
->port_list
);
3247 static void occ_show_free(struct occ_show
*occ_show
)
3249 struct occ_port
*occ_port
, *tmp
;
3251 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3252 occ_port_free(occ_port
);
3255 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3258 struct occ_port
*occ_port
;
3259 uint32_t port_index
;
3261 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3263 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3264 if (occ_port
->port_index
== port_index
)
3267 occ_port
= occ_port_alloc(port_index
);
3270 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3274 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3277 struct occ_item
*occ_item
;
3280 pr_out_sp(7, " %s:", label
);
3281 list_for_each_entry(occ_item
, list
, list
) {
3282 if ((i
- 1) % 4 == 0 && i
!= 1)
3285 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3286 occ_item
->bound_pool_index
);
3288 pr_out_sp(7, "%2u:", occ_item
->index
);
3289 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
3293 if ((i
- 1) % 4 != 0)
3297 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3298 struct list_head
*list
,
3301 struct occ_item
*occ_item
;
3304 jsonw_name(dl
->jw
, label
);
3305 jsonw_start_object(dl
->jw
);
3306 list_for_each_entry(occ_item
, list
, list
) {
3307 sprintf(buf
, "%u", occ_item
->index
);
3308 jsonw_name(dl
->jw
, buf
);
3309 jsonw_start_object(dl
->jw
);
3311 jsonw_uint_field(dl
->jw
, "bound_pool",
3312 occ_item
->bound_pool_index
);
3313 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3314 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3315 jsonw_end_object(dl
->jw
);
3317 jsonw_end_object(dl
->jw
);
3320 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3322 if (dl
->json_output
) {
3323 pr_out_json_occ_show_item_list(dl
, "pool",
3324 &occ_port
->pool_list
, false);
3325 pr_out_json_occ_show_item_list(dl
, "itc",
3326 &occ_port
->ing_tc_list
, true);
3327 pr_out_json_occ_show_item_list(dl
, "etc",
3328 &occ_port
->eg_tc_list
, true);
3331 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3332 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3333 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3337 static void pr_out_occ_show(struct occ_show
*occ_show
)
3339 struct dl
*dl
= occ_show
->dl
;
3340 struct dl_opts
*opts
= &dl
->opts
;
3341 struct occ_port
*occ_port
;
3343 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3344 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3345 occ_port
->port_index
, true, false);
3346 pr_out_occ_show_port(dl
, occ_port
);
3347 pr_out_port_handle_end(dl
);
3351 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3354 struct occ_port
*occ_port
;
3355 struct occ_item
*occ_item
;
3357 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3360 occ_port
= occ_port_get(occ_show
, tb
);
3362 occ_show
->err
= -ENOMEM
;
3366 occ_item
= occ_item_alloc();
3368 occ_show
->err
= -ENOMEM
;
3371 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3372 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3373 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3374 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3377 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3379 struct occ_show
*occ_show
= data
;
3380 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3381 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3383 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3384 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3385 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3386 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3387 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3388 return MNL_CB_ERROR
;
3389 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3393 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3396 struct occ_port
*occ_port
;
3397 struct occ_item
*occ_item
;
3400 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3403 occ_port
= occ_port_get(occ_show
, tb
);
3405 occ_show
->err
= -ENOMEM
;
3409 occ_item
= occ_item_alloc();
3411 occ_show
->err
= -ENOMEM
;
3414 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3415 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3416 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3417 occ_item
->bound_pool_index
=
3418 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3419 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3420 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3421 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3422 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3423 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3425 occ_item_free(occ_item
);
3428 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3430 struct occ_show
*occ_show
= data
;
3431 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3432 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3434 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3435 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3436 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3437 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3438 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3439 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3440 return MNL_CB_ERROR
;
3441 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3445 static int cmd_sb_occ_show(struct dl
*dl
)
3447 struct nlmsghdr
*nlh
;
3448 struct occ_show
*occ_show
;
3449 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3452 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3456 occ_show
= occ_show_alloc(dl
);
3460 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3462 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3463 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3467 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3469 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3470 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3474 pr_out_section_start(dl
, "occupancy");
3475 pr_out_occ_show(occ_show
);
3476 pr_out_section_end(dl
);
3479 occ_show_free(occ_show
);
3483 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3485 struct nlmsghdr
*nlh
;
3488 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3489 NLM_F_REQUEST
| NLM_F_ACK
);
3491 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3495 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3498 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3500 struct nlmsghdr
*nlh
;
3503 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3504 NLM_F_REQUEST
| NLM_F_ACK
);
3506 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3510 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3513 static int cmd_sb_occ(struct dl
*dl
)
3515 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3518 } else if (dl_argv_match(dl
, "show") ||
3519 dl_argv_match(dl
, "list")) {
3521 return cmd_sb_occ_show(dl
);
3522 } else if (dl_argv_match(dl
, "snapshot")) {
3524 return cmd_sb_occ_snapshot(dl
);
3525 } else if (dl_argv_match(dl
, "clearmax")) {
3527 return cmd_sb_occ_clearmax(dl
);
3529 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3533 static int cmd_sb(struct dl
*dl
)
3535 if (dl_argv_match(dl
, "help")) {
3538 } else if (dl_argv_match(dl
, "show") ||
3539 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3541 return cmd_sb_show(dl
);
3542 } else if (dl_argv_match(dl
, "pool")) {
3544 return cmd_sb_pool(dl
);
3545 } else if (dl_argv_match(dl
, "port")) {
3547 return cmd_sb_port(dl
);
3548 } else if (dl_argv_match(dl
, "tc")) {
3550 return cmd_sb_tc(dl
);
3551 } else if (dl_argv_match(dl
, "occupancy")) {
3553 return cmd_sb_occ(dl
);
3555 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3559 static const char *cmd_name(uint8_t cmd
)
3562 case DEVLINK_CMD_UNSPEC
: return "unspec";
3563 case DEVLINK_CMD_GET
: return "get";
3564 case DEVLINK_CMD_SET
: return "set";
3565 case DEVLINK_CMD_NEW
: return "new";
3566 case DEVLINK_CMD_DEL
: return "del";
3567 case DEVLINK_CMD_PORT_GET
: return "get";
3568 case DEVLINK_CMD_PORT_SET
: return "set";
3569 case DEVLINK_CMD_PORT_NEW
: return "new";
3570 case DEVLINK_CMD_PORT_DEL
: return "del";
3571 case DEVLINK_CMD_PARAM_GET
: return "get";
3572 case DEVLINK_CMD_PARAM_SET
: return "set";
3573 case DEVLINK_CMD_PARAM_NEW
: return "new";
3574 case DEVLINK_CMD_PARAM_DEL
: return "del";
3575 case DEVLINK_CMD_REGION_GET
: return "get";
3576 case DEVLINK_CMD_REGION_SET
: return "set";
3577 case DEVLINK_CMD_REGION_NEW
: return "new";
3578 case DEVLINK_CMD_REGION_DEL
: return "del";
3579 default: return "<unknown cmd>";
3583 static const char *cmd_obj(uint8_t cmd
)
3586 case DEVLINK_CMD_UNSPEC
: return "unspec";
3587 case DEVLINK_CMD_GET
:
3588 case DEVLINK_CMD_SET
:
3589 case DEVLINK_CMD_NEW
:
3590 case DEVLINK_CMD_DEL
:
3592 case DEVLINK_CMD_PORT_GET
:
3593 case DEVLINK_CMD_PORT_SET
:
3594 case DEVLINK_CMD_PORT_NEW
:
3595 case DEVLINK_CMD_PORT_DEL
:
3597 case DEVLINK_CMD_PARAM_GET
:
3598 case DEVLINK_CMD_PARAM_SET
:
3599 case DEVLINK_CMD_PARAM_NEW
:
3600 case DEVLINK_CMD_PARAM_DEL
:
3602 case DEVLINK_CMD_REGION_GET
:
3603 case DEVLINK_CMD_REGION_SET
:
3604 case DEVLINK_CMD_REGION_NEW
:
3605 case DEVLINK_CMD_REGION_DEL
:
3607 default: return "<unknown obj>";
3611 static void pr_out_mon_header(uint8_t cmd
)
3613 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3616 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3618 const char *obj
= cmd_obj(cmd
);
3619 unsigned int index
= 0;
3620 const char *cur_obj
;
3624 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3625 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3631 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3633 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3635 struct dl
*dl
= data
;
3636 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3637 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3638 uint8_t cmd
= genl
->cmd
;
3640 if (!cmd_filter_check(dl
, cmd
))
3644 case DEVLINK_CMD_GET
: /* fall through */
3645 case DEVLINK_CMD_SET
: /* fall through */
3646 case DEVLINK_CMD_NEW
: /* fall through */
3647 case DEVLINK_CMD_DEL
:
3648 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3649 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3650 return MNL_CB_ERROR
;
3651 pr_out_mon_header(genl
->cmd
);
3654 case DEVLINK_CMD_PORT_GET
: /* fall through */
3655 case DEVLINK_CMD_PORT_SET
: /* fall through */
3656 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3657 case DEVLINK_CMD_PORT_DEL
:
3658 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3659 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3660 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3661 return MNL_CB_ERROR
;
3662 pr_out_mon_header(genl
->cmd
);
3663 pr_out_port(dl
, tb
);
3665 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3666 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3667 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3668 case DEVLINK_CMD_PARAM_DEL
:
3669 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3670 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3671 !tb
[DEVLINK_ATTR_PARAM
])
3672 return MNL_CB_ERROR
;
3673 pr_out_mon_header(genl
->cmd
);
3674 pr_out_param(dl
, tb
, false);
3676 case DEVLINK_CMD_REGION_GET
: /* fall through */
3677 case DEVLINK_CMD_REGION_SET
: /* fall through */
3678 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3679 case DEVLINK_CMD_REGION_DEL
:
3680 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3681 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3682 !tb
[DEVLINK_ATTR_REGION_NAME
])
3683 return MNL_CB_ERROR
;
3684 pr_out_mon_header(genl
->cmd
);
3685 pr_out_region(dl
, tb
);
3691 static int cmd_mon_show(struct dl
*dl
)
3694 unsigned int index
= 0;
3695 const char *cur_obj
;
3697 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3698 if (strcmp(cur_obj
, "all") != 0 &&
3699 strcmp(cur_obj
, "dev") != 0 &&
3700 strcmp(cur_obj
, "port") != 0) {
3701 pr_err("Unknown object \"%s\"\n", cur_obj
);
3705 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3708 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3714 static void cmd_mon_help(void)
3716 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3717 "where OBJECT-LIST := { dev | port }\n");
3720 static int cmd_mon(struct dl
*dl
)
3722 if (dl_argv_match(dl
, "help")) {
3725 } else if (dl_no_arg(dl
)) {
3727 return cmd_mon_show(dl
);
3729 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3733 struct dpipe_field
{
3736 unsigned int bitwidth
;
3737 enum devlink_dpipe_field_mapping_type mapping_type
;
3740 struct dpipe_header
{
3741 struct list_head list
;
3744 struct dpipe_field
*fields
;
3745 unsigned int fields_count
;
3748 struct dpipe_table
{
3749 struct list_head list
;
3751 unsigned int resource_id
;
3752 bool resource_valid
;
3755 struct dpipe_tables
{
3756 struct list_head table_list
;
3766 enum devlink_resource_unit unit
;
3771 struct list_head list
;
3772 struct list_head resource_list
;
3773 struct resource
*parent
;
3777 struct list_head resource_list
;
3780 struct resource_ctx
{
3783 struct resources
*resources
;
3784 struct dpipe_tables
*tables
;
3785 bool print_resources
;
3786 bool pending_change
;
3789 static struct resource
*resource_alloc(void)
3791 struct resource
*resource
;
3793 resource
= calloc(1, sizeof(struct resource
));
3796 INIT_LIST_HEAD(&resource
->resource_list
);
3800 static void resource_free(struct resource
*resource
)
3802 struct resource
*child_resource
, *tmp
;
3804 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3806 free(child_resource
->name
);
3807 resource_free(child_resource
);
3812 static struct resources
*resources_alloc(void)
3814 struct resources
*resources
;
3816 resources
= calloc(1, sizeof(struct resources
));
3819 INIT_LIST_HEAD(&resources
->resource_list
);
3823 static void resources_free(struct resources
*resources
)
3825 struct resource
*resource
, *tmp
;
3827 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3828 resource_free(resource
);
3831 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3833 ctx
->resources
= resources_alloc();
3834 if (!ctx
->resources
)
3840 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3842 resources_free(ctx
->resources
);
3848 struct list_head global_headers
;
3849 struct list_head local_headers
;
3850 struct dpipe_tables
*tables
;
3851 struct resources
*resources
;
3856 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3858 struct dpipe_header
*header
;
3860 header
= calloc(1, sizeof(struct dpipe_header
));
3863 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3864 if (!header
->fields
)
3865 goto err_fields_alloc
;
3866 header
->fields_count
= fields_count
;
3874 static void dpipe_header_free(struct dpipe_header
*header
)
3876 free(header
->fields
);
3880 static void dpipe_header_clear(struct dpipe_header
*header
)
3882 struct dpipe_field
*field
;
3885 for (i
= 0; i
< header
->fields_count
; i
++) {
3886 field
= &header
->fields
[i
];
3892 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3893 struct dpipe_header
*header
, bool global
)
3896 list_add(&header
->list
, &ctx
->global_headers
);
3898 list_add(&header
->list
, &ctx
->local_headers
);
3901 static void dpipe_header_del(struct dpipe_header
*header
)
3903 list_del(&header
->list
);
3906 static struct dpipe_table
*dpipe_table_alloc(void)
3908 return calloc(1, sizeof(struct dpipe_table
));
3911 static void dpipe_table_free(struct dpipe_table
*table
)
3916 static struct dpipe_tables
*dpipe_tables_alloc(void)
3918 struct dpipe_tables
*tables
;
3920 tables
= calloc(1, sizeof(struct dpipe_tables
));
3923 INIT_LIST_HEAD(&tables
->table_list
);
3927 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3929 struct dpipe_table
*table
, *tmp
;
3931 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3932 dpipe_table_free(table
);
3936 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
3938 ctx
->tables
= dpipe_tables_alloc();
3943 INIT_LIST_HEAD(&ctx
->global_headers
);
3944 INIT_LIST_HEAD(&ctx
->local_headers
);
3948 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
3950 struct dpipe_header
*header
, *tmp
;
3952 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
3954 dpipe_header_del(header
);
3955 dpipe_header_clear(header
);
3956 dpipe_header_free(header
);
3958 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
3960 dpipe_header_del(header
);
3961 dpipe_header_clear(header
);
3962 dpipe_header_free(header
);
3964 dpipe_tables_free(ctx
->tables
);
3967 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
3968 uint32_t header_id
, bool global
)
3970 struct list_head
*header_list
;
3971 struct dpipe_header
*header
;
3974 header_list
= &ctx
->global_headers
;
3976 header_list
= &ctx
->local_headers
;
3977 list_for_each_entry(header
, header_list
, list
) {
3978 if (header
->id
!= header_id
)
3980 return header
->name
;
3985 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
3987 uint32_t field_id
, bool global
)
3989 struct list_head
*header_list
;
3990 struct dpipe_header
*header
;
3993 header_list
= &ctx
->global_headers
;
3995 header_list
= &ctx
->local_headers
;
3996 list_for_each_entry(header
, header_list
, list
) {
3997 if (header
->id
!= header_id
)
3999 return header
->fields
[field_id
].name
;
4005 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4007 switch (mapping_type
) {
4008 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4010 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4018 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4019 uint32_t field_id
, bool global
)
4021 enum devlink_dpipe_field_mapping_type mapping_type
;
4022 struct list_head
*header_list
;
4023 struct dpipe_header
*header
;
4026 header_list
= &ctx
->global_headers
;
4028 header_list
= &ctx
->local_headers
;
4029 list_for_each_entry(header
, header_list
, list
) {
4030 if (header
->id
!= header_id
)
4032 mapping_type
= header
->fields
[field_id
].mapping_type
;
4033 return dpipe_field_mapping_e2s(mapping_type
);
4038 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4039 struct dpipe_field
*fields
,
4040 unsigned int field_count
)
4042 struct dpipe_field
*field
;
4045 for (i
= 0; i
< field_count
; i
++) {
4047 pr_out_entry_start(ctx
->dl
);
4048 pr_out_str(ctx
->dl
, "name", field
->name
);
4049 if (ctx
->dl
->verbose
)
4050 pr_out_uint(ctx
->dl
, "id", field
->id
);
4051 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4052 if (field
->mapping_type
)
4053 pr_out_str(ctx
->dl
, "mapping_type",
4054 dpipe_field_mapping_e2s(field
->mapping_type
));
4055 pr_out_entry_end(ctx
->dl
);
4060 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4061 struct dpipe_header
*header
, bool global
)
4063 pr_out_handle_start_arr(ctx
->dl
, tb
);
4064 pr_out_str(ctx
->dl
, "name", header
->name
);
4065 if (ctx
->dl
->verbose
) {
4066 pr_out_uint(ctx
->dl
, "id", header
->id
);
4067 pr_out_str(ctx
->dl
, "global",
4068 global
? "true" : "false");
4070 pr_out_array_start(ctx
->dl
, "field");
4071 pr_out_dpipe_fields(ctx
, header
->fields
,
4072 header
->fields_count
);
4073 pr_out_array_end(ctx
->dl
);
4074 pr_out_handle_end(ctx
->dl
);
4077 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4080 struct dpipe_header
*header
;
4082 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4083 pr_out_dpipe_header(ctx
, tb
, header
, false);
4085 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4086 pr_out_dpipe_header(ctx
, tb
, header
, true);
4089 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4091 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4095 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4096 if (err
!= MNL_CB_OK
)
4098 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4099 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4100 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4101 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4104 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4105 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4106 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4107 field
->name
= strdup(name
);
4110 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4114 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4115 struct dpipe_field
*fields
)
4117 struct nlattr
*nla_field
;
4121 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4122 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4130 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4132 struct nlattr
*nla_field
;
4133 unsigned int count
= 0;
4135 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4140 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4142 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4143 struct dpipe_header
*header
;
4144 unsigned int fields_count
;
4145 const char *header_name
;
4149 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4150 if (err
!= MNL_CB_OK
)
4153 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4154 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4155 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4158 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4159 header
= dpipe_header_alloc(fields_count
);
4163 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4164 header
->name
= strdup(header_name
);
4165 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4166 header
->fields_count
= fields_count
;
4167 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4169 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4173 dpipe_header_add(ctx
, header
, global
);
4177 dpipe_header_free(header
);
4181 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4183 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4184 struct nlattr
*nla_header
;
4187 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4188 err
= dpipe_header_get(ctx
, nla_header
);
4195 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4197 struct dpipe_ctx
*ctx
= data
;
4198 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4199 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4202 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4203 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4204 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4205 return MNL_CB_ERROR
;
4206 err
= dpipe_headers_get(ctx
, tb
);
4209 return MNL_CB_ERROR
;
4212 if (ctx
->print_headers
)
4213 pr_out_dpipe_headers(ctx
, tb
);
4217 static int cmd_dpipe_headers_show(struct dl
*dl
)
4219 struct nlmsghdr
*nlh
;
4220 struct dpipe_ctx ctx
= {};
4221 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4224 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4226 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4230 err
= dpipe_ctx_init(&ctx
, dl
);
4234 ctx
.print_headers
= true;
4236 pr_out_section_start(dl
, "header");
4237 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4239 pr_err("error get headers %s\n", strerror(ctx
.err
));
4240 pr_out_section_end(dl
);
4242 dpipe_ctx_fini(&ctx
);
4246 static void cmd_dpipe_header_help(void)
4248 pr_err("Usage: devlink dpipe headers show DEV\n");
4251 static int cmd_dpipe_header(struct dl
*dl
)
4253 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4254 cmd_dpipe_header_help();
4256 } else if (dl_argv_match(dl
, "show")) {
4258 return cmd_dpipe_headers_show(dl
);
4260 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4265 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4267 switch (action_type
) {
4268 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4269 return "field_modify";
4275 struct dpipe_op_info
{
4281 struct dpipe_action
{
4282 struct dpipe_op_info info
;
4286 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4287 struct dpipe_ctx
*ctx
)
4289 struct dpipe_op_info
*op_info
= &action
->info
;
4290 const char *mapping
;
4292 pr_out_str(ctx
->dl
, "type",
4293 dpipe_action_type_e2s(action
->type
));
4294 pr_out_str(ctx
->dl
, "header",
4295 dpipe_header_id2s(ctx
, op_info
->header_id
,
4296 op_info
->header_global
));
4297 pr_out_str(ctx
->dl
, "field",
4298 dpipe_field_id2s(ctx
, op_info
->header_id
,
4300 op_info
->header_global
));
4301 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4303 op_info
->header_global
);
4305 pr_out_str(ctx
->dl
, "mapping", mapping
);
4308 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4310 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4313 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4314 if (err
!= MNL_CB_OK
)
4317 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4318 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4319 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4320 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4324 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4325 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4326 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4327 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4332 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4333 struct nlattr
*nla_actions
)
4335 struct nlattr
*nla_action
;
4336 struct dpipe_action action
;
4338 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4339 pr_out_entry_start(ctx
->dl
);
4340 if (dpipe_action_parse(&action
, nla_action
))
4341 goto err_action_parse
;
4342 pr_out_dpipe_action(&action
, ctx
);
4343 pr_out_entry_end(ctx
->dl
);
4348 pr_out_entry_end(ctx
->dl
);
4353 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4355 switch (match_type
) {
4356 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4357 return "field_exact";
4363 struct dpipe_match
{
4364 struct dpipe_op_info info
;
4368 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4369 struct dpipe_ctx
*ctx
)
4371 struct dpipe_op_info
*op_info
= &match
->info
;
4372 const char *mapping
;
4374 pr_out_str(ctx
->dl
, "type",
4375 dpipe_match_type_e2s(match
->type
));
4376 pr_out_str(ctx
->dl
, "header",
4377 dpipe_header_id2s(ctx
, op_info
->header_id
,
4378 op_info
->header_global
));
4379 pr_out_str(ctx
->dl
, "field",
4380 dpipe_field_id2s(ctx
, op_info
->header_id
,
4382 op_info
->header_global
));
4383 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4385 op_info
->header_global
);
4387 pr_out_str(ctx
->dl
, "mapping", mapping
);
4390 static int dpipe_match_parse(struct dpipe_match
*match
,
4394 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4397 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4398 if (err
!= MNL_CB_OK
)
4401 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4402 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4403 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4404 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4408 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4409 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4410 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4411 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4416 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4417 struct nlattr
*nla_matches
)
4419 struct nlattr
*nla_match
;
4420 struct dpipe_match match
;
4422 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4423 pr_out_entry_start(ctx
->dl
);
4424 if (dpipe_match_parse(&match
, nla_match
))
4425 goto err_match_parse
;
4426 pr_out_dpipe_match(&match
, ctx
);
4427 pr_out_entry_end(ctx
->dl
);
4432 pr_out_entry_end(ctx
->dl
);
4436 static struct resource
*
4437 resource_find(struct resources
*resources
, struct resource
*resource
,
4438 uint64_t resource_id
)
4440 struct list_head
*list_head
;
4443 list_head
= &resources
->resource_list
;
4445 list_head
= &resource
->resource_list
;
4447 list_for_each_entry(resource
, list_head
, list
) {
4448 struct resource
*child_resource
;
4450 if (resource
->id
== resource_id
)
4453 child_resource
= resource_find(resources
, resource
,
4456 return child_resource
;
4462 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4463 uint64_t resource_id
)
4465 struct resource
*resource
, *parent_resource
;
4466 const char del
[] = "/";
4470 resource
= resource_find(resources
, NULL
, resource_id
);
4474 for (parent_resource
= resource
; parent_resource
;
4475 parent_resource
= parent_resource
->parent
)
4476 path_len
+= strlen(parent_resource
->name
) + 1;
4479 path
= calloc(1, path_len
);
4483 path
+= path_len
- 1;
4484 for (parent_resource
= resource
; parent_resource
;
4485 parent_resource
= parent_resource
->parent
) {
4486 path
-= strlen(parent_resource
->name
);
4487 memcpy(path
, parent_resource
->name
,
4488 strlen(parent_resource
->name
));
4489 path
-= strlen(del
);
4490 memcpy(path
, del
, strlen(del
));
4492 pr_out_str(dl
, "resource_path", path
);
4496 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4498 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4499 struct dpipe_table
*table
;
4500 uint32_t resource_units
;
4501 bool counters_enabled
;
4502 bool resource_valid
;
4506 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4507 if (err
!= MNL_CB_OK
)
4510 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4511 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4512 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4513 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4514 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4518 table
= dpipe_table_alloc();
4522 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4523 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4524 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4526 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4528 if (resource_valid
) {
4529 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4530 table
->resource_valid
= true;
4533 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4534 if (!ctx
->print_tables
)
4537 pr_out_str(ctx
->dl
, "name", table
->name
);
4538 pr_out_uint(ctx
->dl
, "size", size
);
4539 pr_out_str(ctx
->dl
, "counters_enabled",
4540 counters_enabled
? "true" : "false");
4542 if (resource_valid
) {
4543 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4544 resource_path_print(ctx
->dl
, ctx
->resources
,
4545 table
->resource_id
);
4546 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4549 pr_out_array_start(ctx
->dl
, "match");
4550 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4551 goto err_matches_show
;
4552 pr_out_array_end(ctx
->dl
);
4554 pr_out_array_start(ctx
->dl
, "action");
4555 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4556 goto err_actions_show
;
4557 pr_out_array_end(ctx
->dl
);
4563 pr_out_array_end(ctx
->dl
);
4567 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4569 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4570 struct nlattr
*nla_table
;
4572 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4573 if (ctx
->print_tables
)
4574 pr_out_handle_start_arr(ctx
->dl
, tb
);
4575 if (dpipe_table_show(ctx
, nla_table
))
4576 goto err_table_show
;
4577 if (ctx
->print_tables
)
4578 pr_out_handle_end(ctx
->dl
);
4583 if (ctx
->print_tables
)
4584 pr_out_handle_end(ctx
->dl
);
4588 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4590 struct dpipe_ctx
*ctx
= data
;
4591 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4592 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4594 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4595 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4596 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4597 return MNL_CB_ERROR
;
4599 if (dpipe_tables_show(ctx
, tb
))
4600 return MNL_CB_ERROR
;
4604 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4606 static int cmd_dpipe_table_show(struct dl
*dl
)
4608 struct nlmsghdr
*nlh
;
4609 struct dpipe_ctx dpipe_ctx
= {};
4610 struct resource_ctx resource_ctx
= {};
4611 uint16_t flags
= NLM_F_REQUEST
;
4614 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4618 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4620 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4624 dpipe_ctx
.print_tables
= true;
4626 dl_opts_put(nlh
, dl
);
4627 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4630 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4631 goto err_headers_get
;
4634 err
= resource_ctx_init(&resource_ctx
, dl
);
4636 goto err_resource_ctx_init
;
4638 resource_ctx
.print_resources
= false;
4639 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4640 dl_opts_put(nlh
, dl
);
4641 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4644 dpipe_ctx
.resources
= resource_ctx
.resources
;
4646 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4647 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4648 dl_opts_put(nlh
, dl
);
4650 pr_out_section_start(dl
, "table");
4651 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4652 pr_out_section_end(dl
);
4654 resource_ctx_fini(&resource_ctx
);
4655 dpipe_ctx_fini(&dpipe_ctx
);
4658 err_resource_ctx_init
:
4660 dpipe_ctx_fini(&dpipe_ctx
);
4664 static int cmd_dpipe_table_set(struct dl
*dl
)
4666 struct nlmsghdr
*nlh
;
4669 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4670 NLM_F_REQUEST
| NLM_F_ACK
);
4672 err
= dl_argv_parse_put(nlh
, dl
,
4673 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4674 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4678 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4681 enum dpipe_value_type
{
4682 DPIPE_VALUE_TYPE_VALUE
,
4683 DPIPE_VALUE_TYPE_MASK
,
4687 dpipe_value_type_e2s(enum dpipe_value_type type
)
4690 case DPIPE_VALUE_TYPE_VALUE
:
4692 case DPIPE_VALUE_TYPE_MASK
:
4693 return "value_mask";
4699 struct dpipe_field_printer
{
4700 unsigned int field_id
;
4701 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4704 struct dpipe_header_printer
{
4705 struct dpipe_field_printer
*printers
;
4706 unsigned int printers_count
;
4707 unsigned int header_id
;
4710 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4711 enum dpipe_value_type type
,
4714 struct in_addr ip_addr
;
4716 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4717 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4721 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4722 enum dpipe_value_type type
,
4725 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4726 ether_ntoa((struct ether_addr
*)value
));
4729 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4730 enum dpipe_value_type type
,
4733 char str
[INET6_ADDRSTRLEN
];
4735 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4736 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4739 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4741 .printer
= dpipe_field_printer_ipv4_addr
,
4742 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4746 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4747 .printers
= dpipe_field_printers_ipv4
,
4748 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4749 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4752 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4754 .printer
= dpipe_field_printer_ethernet_addr
,
4755 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4759 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4760 .printers
= dpipe_field_printers_ethernet
,
4761 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4762 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4765 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4767 .printer
= dpipe_field_printer_ipv6_addr
,
4768 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4772 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4773 .printers
= dpipe_field_printers_ipv6
,
4774 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4775 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4778 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4779 &dpipe_header_printer_ipv4
,
4780 &dpipe_header_printer_ethernet
,
4781 &dpipe_header_printer_ipv6
,
4784 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4785 struct dpipe_op_info
*info
,
4786 enum dpipe_value_type type
,
4789 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4790 struct dpipe_header_printer
*header_printer
;
4791 struct dpipe_field_printer
*field_printer
;
4792 unsigned int field_printers_count
;
4796 for (i
= 0; i
< header_printers_count
; i
++) {
4797 header_printer
= dpipe_header_printers
[i
];
4798 if (header_printer
->header_id
!= info
->header_id
)
4800 field_printers_count
= header_printer
->printers_count
;
4801 for (j
= 0; j
< field_printers_count
; j
++) {
4802 field_printer
= &header_printer
->printers
[j
];
4803 if (field_printer
->field_id
!= info
->field_id
)
4805 field_printer
->printer(ctx
, type
, value
);
4813 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4815 unsigned int value_len
,
4816 struct dpipe_op_info
*info
,
4817 enum dpipe_value_type type
)
4819 if (info
->header_global
&&
4820 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4823 if (value_len
== sizeof(uint32_t)) {
4824 uint32_t *value_32
= value
;
4826 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4830 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4831 struct nlattr
**nla_match_value
,
4832 struct dpipe_op_info
*info
)
4834 void *value
, *value_mask
;
4835 uint32_t value_mapping
;
4839 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4840 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4842 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4843 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4846 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4847 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4851 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4852 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4853 DPIPE_VALUE_TYPE_MASK
);
4856 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4859 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4862 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4863 struct dpipe_match match
;
4866 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4867 if (err
!= MNL_CB_OK
)
4870 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4871 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4875 pr_out_entry_start(ctx
->dl
);
4876 if (dpipe_match_parse(&match
,
4877 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4878 goto err_match_parse
;
4879 pr_out_dpipe_match(&match
, ctx
);
4880 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4881 pr_out_entry_end(ctx
->dl
);
4886 pr_out_entry_end(ctx
->dl
);
4890 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4893 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4894 struct dpipe_action action
;
4897 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4898 if (err
!= MNL_CB_OK
)
4901 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4902 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4906 pr_out_entry_start(ctx
->dl
);
4907 if (dpipe_action_parse(&action
,
4908 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4909 goto err_action_parse
;
4910 pr_out_dpipe_action(&action
, ctx
);
4911 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4912 pr_out_entry_end(ctx
->dl
);
4917 pr_out_entry_end(ctx
->dl
);
4922 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4923 struct nlattr
*nla_action_values
)
4925 struct nlattr
*nla_action_value
;
4927 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4928 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
4935 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
4936 struct nlattr
*nla_match_values
)
4938 struct nlattr
*nla_match_value
;
4940 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
4941 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
4947 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4949 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
4950 uint32_t entry_index
;
4954 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
4955 if (err
!= MNL_CB_OK
)
4958 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
4959 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
4960 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
4964 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
4965 pr_out_uint(ctx
->dl
, "index", entry_index
);
4967 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
4968 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
4969 pr_out_uint(ctx
->dl
, "counter", counter
);
4972 pr_out_array_start(ctx
->dl
, "match_value");
4973 if (dpipe_tables_match_values_show(ctx
,
4974 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
4975 goto err_match_values_show
;
4976 pr_out_array_end(ctx
->dl
);
4978 pr_out_array_start(ctx
->dl
, "action_value");
4979 if (dpipe_tables_action_values_show(ctx
,
4980 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
4981 goto err_action_values_show
;
4982 pr_out_array_end(ctx
->dl
);
4985 err_action_values_show
:
4986 err_match_values_show
:
4987 pr_out_array_end(ctx
->dl
);
4991 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4993 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
4994 struct nlattr
*nla_entry
;
4996 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
4997 pr_out_handle_start_arr(ctx
->dl
, tb
);
4998 if (dpipe_entry_show(ctx
, nla_entry
))
4999 goto err_entry_show
;
5000 pr_out_handle_end(ctx
->dl
);
5005 pr_out_handle_end(ctx
->dl
);
5009 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5011 struct dpipe_ctx
*ctx
= data
;
5012 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5013 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5015 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5016 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5017 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5018 return MNL_CB_ERROR
;
5020 if (dpipe_table_entries_show(ctx
, tb
))
5021 return MNL_CB_ERROR
;
5025 static int cmd_dpipe_table_dump(struct dl
*dl
)
5027 struct nlmsghdr
*nlh
;
5028 struct dpipe_ctx ctx
= {};
5029 uint16_t flags
= NLM_F_REQUEST
;
5032 err
= dpipe_ctx_init(&ctx
, dl
);
5036 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5040 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5041 dl_opts_put(nlh
, dl
);
5042 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5044 pr_err("error get headers %s\n", strerror(ctx
.err
));
5048 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5049 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5050 dl_opts_put(nlh
, dl
);
5052 pr_out_section_start(dl
, "table_entry");
5053 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5054 pr_out_section_end(dl
);
5056 dpipe_ctx_fini(&ctx
);
5060 static void cmd_dpipe_table_help(void)
5062 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5063 "where OBJECT-LIST := { show | set | dump }\n");
5066 static int cmd_dpipe_table(struct dl
*dl
)
5068 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5069 cmd_dpipe_table_help();
5071 } else if (dl_argv_match(dl
, "show")) {
5073 return cmd_dpipe_table_show(dl
);
5074 } else if (dl_argv_match(dl
, "set")) {
5076 return cmd_dpipe_table_set(dl
);
5077 } else if (dl_argv_match(dl
, "dump")) {
5079 return cmd_dpipe_table_dump(dl
);
5081 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5085 static void cmd_dpipe_help(void)
5087 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5088 "where OBJECT-LIST := { header | table }\n");
5091 static int cmd_dpipe(struct dl
*dl
)
5093 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5096 } else if (dl_argv_match(dl
, "header")) {
5098 return cmd_dpipe_header(dl
);
5099 } else if (dl_argv_match(dl
, "table")) {
5101 return cmd_dpipe_table(dl
);
5103 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5108 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5109 struct nlattr
**nla_resource
)
5111 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5112 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5113 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5114 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5115 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5116 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5117 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5121 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5122 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5123 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5124 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5125 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5126 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5127 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5129 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5130 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5132 resource
->size_new
= resource
->size
;
5134 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5135 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5136 resource
->occ_valid
= true;
5139 if (resource
->size_new
!= resource
->size
)
5140 ctx
->pending_change
= true;
5146 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5147 struct resource
*parent_resource
, struct nlattr
*nl
)
5149 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5150 struct nlattr
*nla_child_resource
;
5151 struct nlattr
*nla_resources
;
5161 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5162 if (err
!= MNL_CB_OK
)
5165 err
= resource_parse(ctx
, resource
, nla_resource
);
5169 resource
->parent
= parent_resource
;
5170 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5173 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5174 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5176 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5177 struct resource
*child_resource
;
5178 struct list_head
*list
;
5180 child_resource
= resource_alloc();
5181 if (!child_resource
)
5185 list
= &ctx
->resources
->resource_list
;
5187 list
= &resource
->resource_list
;
5189 list_add_tail(&child_resource
->list
, list
);
5190 err
= resource_get(ctx
, child_resource
, resource
,
5191 nla_child_resource
);
5199 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5202 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5203 default: return "<unknown unit>";
5207 static void resource_show(struct resource
*resource
,
5208 struct resource_ctx
*ctx
)
5210 struct resource
*child_resource
;
5211 struct dpipe_table
*table
;
5212 struct dl
*dl
= ctx
->dl
;
5215 pr_out_str(dl
, "name", resource
->name
);
5217 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5218 pr_out_u64(dl
, "size", resource
->size
);
5219 if (resource
->size
!= resource
->size_new
)
5220 pr_out_u64(dl
, "size_new", resource
->size_new
);
5221 if (resource
->occ_valid
)
5222 pr_out_uint(dl
, "occ", resource
->size_occ
);
5223 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5225 if (resource
->size_min
!= resource
->size_max
) {
5226 pr_out_uint(dl
, "size_min", resource
->size_min
);
5227 pr_out_u64(dl
, "size_max", resource
->size_max
);
5228 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5231 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5232 if (table
->resource_id
== resource
->id
&&
5233 table
->resource_valid
)
5237 pr_out_array_start(dl
, "dpipe_tables");
5239 pr_out_str(dl
, "dpipe_tables", "none");
5241 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5242 if (table
->resource_id
!= resource
->id
||
5243 !table
->resource_valid
)
5245 pr_out_entry_start(dl
);
5246 pr_out_str(dl
, "table_name", table
->name
);
5247 pr_out_entry_end(dl
);
5250 pr_out_array_end(dl
);
5252 if (list_empty(&resource
->resource_list
))
5255 if (ctx
->pending_change
)
5256 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5258 pr_out_array_start(dl
, "resources");
5259 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5260 pr_out_entry_start(dl
);
5261 resource_show(child_resource
, ctx
);
5262 pr_out_entry_end(dl
);
5264 pr_out_array_end(dl
);
5268 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5270 struct resources
*resources
= ctx
->resources
;
5271 struct resource
*resource
;
5273 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5274 pr_out_handle_start_arr(ctx
->dl
, tb
);
5275 resource_show(resource
, ctx
);
5276 pr_out_handle_end(ctx
->dl
);
5280 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5282 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5285 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5287 struct resource_ctx
*ctx
= data
;
5288 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5289 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5292 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5293 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5294 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5295 return MNL_CB_ERROR
;
5297 err
= resources_get(ctx
, tb
);
5300 return MNL_CB_ERROR
;
5303 if (ctx
->print_resources
)
5304 resources_show(ctx
, tb
);
5309 static int cmd_resource_show(struct dl
*dl
)
5311 struct nlmsghdr
*nlh
;
5312 struct dpipe_ctx dpipe_ctx
= {};
5313 struct resource_ctx resource_ctx
= {};
5316 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5320 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5322 dl_opts_put(nlh
, dl
);
5324 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5328 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5331 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5335 err
= resource_ctx_init(&resource_ctx
, dl
);
5339 resource_ctx
.print_resources
= true;
5340 resource_ctx
.tables
= dpipe_ctx
.tables
;
5341 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5342 NLM_F_REQUEST
| NLM_F_ACK
);
5343 dl_opts_put(nlh
, dl
);
5344 pr_out_section_start(dl
, "resources");
5345 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5347 pr_out_section_end(dl
);
5348 resource_ctx_fini(&resource_ctx
);
5350 dpipe_ctx_fini(&dpipe_ctx
);
5354 static void cmd_resource_help(void)
5356 pr_err("Usage: devlink resource show DEV\n"
5357 " devlink resource set DEV path PATH size SIZE\n");
5360 static struct resource
*
5361 resource_find_by_name(struct list_head
*list
, char *name
)
5363 struct resource
*resource
;
5365 list_for_each_entry(resource
, list
, list
) {
5366 if (!strcmp(resource
->name
, name
))
5373 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5374 uint32_t *p_resource_id
, bool *p_resource_valid
)
5376 struct resource
*resource
;
5377 uint32_t resource_id
= 0;
5378 char *resource_path_dup
;
5379 struct list_head
*list
;
5380 const char del
[] = "/";
5381 char *resource_name
;
5383 resource_path_dup
= strdup(resource_path
);
5384 list
= &ctx
->resources
->resource_list
;
5385 resource_name
= strtok(resource_path_dup
, del
);
5386 while (resource_name
!= NULL
) {
5387 resource
= resource_find_by_name(list
, resource_name
);
5389 goto err_resource_lookup
;
5391 list
= &resource
->resource_list
;
5392 resource_name
= strtok(NULL
, del
);
5393 resource_id
= resource
->id
;
5395 free(resource_path_dup
);
5396 *p_resource_valid
= true;
5397 *p_resource_id
= resource_id
;
5400 err_resource_lookup
:
5401 free(resource_path_dup
);
5405 static int cmd_resource_set(struct dl
*dl
)
5407 struct nlmsghdr
*nlh
;
5408 struct resource_ctx ctx
= {};
5411 err
= resource_ctx_init(&ctx
, dl
);
5415 ctx
.print_resources
= false;
5416 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5417 DL_OPT_RESOURCE_SIZE
, 0);
5421 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5423 dl_opts_put(nlh
, dl
);
5424 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5426 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5430 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5431 &dl
->opts
.resource_id
,
5432 &dl
->opts
.resource_id_valid
);
5434 pr_err("error parsing resource path %s\n", strerror(-err
));
5438 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5439 NLM_F_REQUEST
| NLM_F_ACK
);
5441 dl_opts_put(nlh
, dl
);
5442 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5444 resource_ctx_fini(&ctx
);
5448 static int cmd_resource(struct dl
*dl
)
5450 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5451 cmd_resource_help();
5453 } else if (dl_argv_match(dl
, "show")) {
5455 return cmd_resource_show(dl
);
5456 } else if (dl_argv_match(dl
, "set")) {
5458 return cmd_resource_set(dl
);
5460 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5464 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5466 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5467 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5468 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5471 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5472 if (dl
->json_output
) {
5473 jsonw_name(dl
->jw
, buf
);
5474 jsonw_start_object(dl
->jw
);
5480 static void pr_out_region_handle_end(struct dl
*dl
)
5482 if (dl
->json_output
)
5483 jsonw_end_object(dl
->jw
);
5488 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5490 if (dl
->json_output
) {
5491 jsonw_name(dl
->jw
, "snapshot");
5492 jsonw_start_array(dl
->jw
);
5494 if (g_indent_newline
)
5495 pr_out("snapshot %s", array
? "[" : "");
5497 pr_out(" snapshot %s", array
? "[" : "");
5501 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5503 if (dl
->json_output
)
5504 jsonw_end_array(dl
->jw
);
5509 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5511 uint32_t snapshot_id
;
5513 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5516 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5518 if (dl
->json_output
)
5519 jsonw_uint(dl
->jw
, snapshot_id
);
5521 pr_out("%s%u", index
? " " : "", snapshot_id
);
5524 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5526 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5527 struct nlattr
*nla_sanpshot
;
5530 pr_out_region_snapshots_start(dl
, true);
5531 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5532 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5533 if (err
!= MNL_CB_OK
)
5535 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5537 pr_out_region_snapshots_end(dl
, true);
5540 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5542 pr_out_region_snapshots_start(dl
, false);
5543 pr_out_region_snapshots_id(dl
, tb
, 0);
5544 pr_out_region_snapshots_end(dl
, false);
5547 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5549 pr_out_region_handle_start(dl
, tb
);
5551 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5552 pr_out_u64(dl
, "size",
5553 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5555 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5556 pr_out_snapshots(dl
, tb
);
5558 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5559 pr_out_snapshot(dl
, tb
);
5561 pr_out_region_handle_end(dl
);
5564 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5566 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5567 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5568 struct dl
*dl
= data
;
5570 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5571 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5572 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5573 return MNL_CB_ERROR
;
5575 pr_out_region(dl
, tb
);
5580 static int cmd_region_show(struct dl
*dl
)
5582 struct nlmsghdr
*nlh
;
5583 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5586 if (dl_argc(dl
) == 0)
5587 flags
|= NLM_F_DUMP
;
5589 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5591 if (dl_argc(dl
) > 0) {
5592 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5597 pr_out_section_start(dl
, "regions");
5598 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5599 pr_out_section_end(dl
);
5603 static int cmd_region_snapshot_del(struct dl
*dl
)
5605 struct nlmsghdr
*nlh
;
5608 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5609 NLM_F_REQUEST
| NLM_F_ACK
);
5611 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5612 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5616 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5619 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5621 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5622 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5623 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5624 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5625 struct dl
*dl
= data
;
5628 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5629 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5630 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5631 return MNL_CB_ERROR
;
5633 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5634 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5635 if (err
!= MNL_CB_OK
)
5636 return MNL_CB_ERROR
;
5638 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5639 if (!nla_chunk_data
)
5642 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5643 if (!nla_chunk_addr
)
5646 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5647 mnl_attr_get_payload_len(nla_chunk_data
),
5648 mnl_attr_get_u64(nla_chunk_addr
));
5653 static int cmd_region_dump(struct dl
*dl
)
5655 struct nlmsghdr
*nlh
;
5658 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5659 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5661 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5662 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5666 pr_out_section_start(dl
, "dump");
5667 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5668 pr_out_section_end(dl
);
5669 if (!dl
->json_output
)
5674 static int cmd_region_read(struct dl
*dl
)
5676 struct nlmsghdr
*nlh
;
5679 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5680 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5682 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5683 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5684 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5688 pr_out_section_start(dl
, "read");
5689 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5690 pr_out_section_end(dl
);
5691 if (!dl
->json_output
)
5696 static void cmd_region_help(void)
5698 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5699 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5700 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5701 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5704 static int cmd_region(struct dl
*dl
)
5706 if (dl_no_arg(dl
)) {
5707 return cmd_region_show(dl
);
5708 } else if (dl_argv_match(dl
, "help")) {
5711 } else if (dl_argv_match(dl
, "show")) {
5713 return cmd_region_show(dl
);
5714 } else if (dl_argv_match(dl
, "del")) {
5716 return cmd_region_snapshot_del(dl
);
5717 } else if (dl_argv_match(dl
, "dump")) {
5719 return cmd_region_dump(dl
);
5720 } else if (dl_argv_match(dl
, "read")) {
5722 return cmd_region_read(dl
);
5724 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5728 static void help(void)
5730 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5731 " devlink [ -f[orce] ] -b[atch] filename\n"
5732 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5733 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5736 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5741 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5744 } else if (dl_argv_match(dl
, "dev")) {
5747 } else if (dl_argv_match(dl
, "port")) {
5749 return cmd_port(dl
);
5750 } else if (dl_argv_match(dl
, "sb")) {
5753 } else if (dl_argv_match(dl
, "monitor")) {
5756 } else if (dl_argv_match(dl
, "dpipe")) {
5758 return cmd_dpipe(dl
);
5759 } else if (dl_argv_match(dl
, "resource")) {
5761 return cmd_resource(dl
);
5762 } else if (dl_argv_match(dl
, "region")) {
5764 return cmd_region(dl
);
5766 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5770 static int dl_init(struct dl
*dl
)
5774 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5776 pr_err("Failed to connect to devlink Netlink\n");
5780 err
= ifname_map_init(dl
);
5782 pr_err("Failed to create index map\n");
5783 goto err_ifname_map_create
;
5785 if (dl
->json_output
) {
5786 dl
->jw
= jsonw_new(stdout
);
5788 pr_err("Failed to create JSON writer\n");
5791 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5796 ifname_map_fini(dl
);
5797 err_ifname_map_create
:
5798 mnlg_socket_close(dl
->nlg
);
5802 static void dl_fini(struct dl
*dl
)
5804 if (dl
->json_output
)
5805 jsonw_destroy(&dl
->jw
);
5806 ifname_map_fini(dl
);
5807 mnlg_socket_close(dl
->nlg
);
5810 static struct dl
*dl_alloc(void)
5814 dl
= calloc(1, sizeof(*dl
));
5820 static void dl_free(struct dl
*dl
)
5825 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5829 int ret
= EXIT_SUCCESS
;
5831 if (name
&& strcmp(name
, "-") != 0) {
5832 if (freopen(name
, "r", stdin
) == NULL
) {
5834 "Cannot open file \"%s\" for reading: %s\n",
5835 name
, strerror(errno
));
5836 return EXIT_FAILURE
;
5841 while (getcmdline(&line
, &len
, stdin
) != -1) {
5845 largc
= makeargs(line
, largv
, 100);
5847 continue; /* blank line */
5849 if (dl_cmd(dl
, largc
, largv
)) {
5850 fprintf(stderr
, "Command failed %s:%d\n",
5864 int main(int argc
, char **argv
)
5866 static const struct option long_options
[] = {
5867 { "Version", no_argument
, NULL
, 'V' },
5868 { "force", no_argument
, NULL
, 'f' },
5869 { "batch", required_argument
, NULL
, 'b' },
5870 { "no-nice-names", no_argument
, NULL
, 'n' },
5871 { "json", no_argument
, NULL
, 'j' },
5872 { "pretty", no_argument
, NULL
, 'p' },
5873 { "verbose", no_argument
, NULL
, 'v' },
5874 { NULL
, 0, NULL
, 0 }
5876 const char *batch_file
= NULL
;
5885 pr_err("Failed to allocate memory for devlink\n");
5886 return EXIT_FAILURE
;
5889 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5890 long_options
, NULL
)) >= 0) {
5894 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5901 batch_file
= optarg
;
5904 dl
->no_nice_names
= true;
5907 dl
->json_output
= true;
5910 dl
->pretty_output
= true;
5916 pr_err("Unknown option.\n");
5933 err
= dl_batch(dl
, batch_file
, force
);
5935 err
= dl_cmd(dl
, argc
, argv
);