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
)
1631 if (dl
->json_output
)
1632 jsonw_bool_field(dl
->jw
, name
, val
);
1634 pr_out_str(dl
, name
, val
? "true" : "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_bool_value(struct dl
*dl
, bool value
)
1666 if (dl
->json_output
)
1667 jsonw_bool(dl
->jw
, value
);
1669 pr_out(" %s", value
? "true" : "false");
1672 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1674 if (dl
->json_output
)
1675 jsonw_uint(dl
->jw
, value
);
1677 pr_out(" %u", value
);
1680 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1682 if (dl
->json_output
)
1683 jsonw_u64(dl
->jw
, value
);
1685 pr_out(" %lu", value
);
1688 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1692 if (dl
->json_output
)
1693 jsonw_start_array(dl
->jw
);
1698 if (dl
->json_output
) {
1699 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1701 pr_out(" %02x", data
[i
]);
1707 if (dl
->json_output
)
1708 jsonw_end_array(dl
->jw
);
1709 else if ((i
- 1) % 16)
1713 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1715 if (dl
->json_output
)
1716 jsonw_string(dl
->jw
, value
);
1718 pr_out(" %s", value
);
1721 static void pr_out_name(struct dl
*dl
, const char *name
)
1723 if (dl
->json_output
)
1724 jsonw_name(dl
->jw
, name
);
1726 pr_out(" %s:", name
);
1729 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1731 if (dl
->json_output
) {
1732 jsonw_name(dl
->jw
, "address");
1733 jsonw_uint(dl
->jw
, addr
);
1734 jsonw_name(dl
->jw
, "data");
1735 jsonw_start_array(dl
->jw
);
1739 static void pr_out_region_chunk_end(struct dl
*dl
)
1741 if (dl
->json_output
)
1742 jsonw_end_array(dl
->jw
);
1745 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1748 static uint64_t align_val
;
1751 pr_out_region_chunk_start(dl
, addr
);
1753 if (!dl
->json_output
)
1754 if (!(align_val
% 16))
1755 pr_out("%s%016"PRIx64
" ",
1756 align_val
? "\n" : "",
1761 if (dl
->json_output
)
1762 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1764 pr_out("%02x ", data
[i
]);
1769 pr_out_region_chunk_end(dl
);
1772 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1774 pr_out_handle(dl
, tb
);
1777 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1779 if (dl
->json_output
) {
1780 jsonw_start_object(dl
->jw
);
1781 jsonw_name(dl
->jw
, name
);
1782 jsonw_start_object(dl
->jw
);
1786 static void pr_out_section_end(struct dl
*dl
)
1788 if (dl
->json_output
) {
1789 if (dl
->arr_last
.present
)
1790 jsonw_end_array(dl
->jw
);
1791 jsonw_end_object(dl
->jw
);
1792 jsonw_end_object(dl
->jw
);
1796 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1798 if (dl
->json_output
) {
1799 jsonw_name(dl
->jw
, name
);
1800 jsonw_start_array(dl
->jw
);
1802 __pr_out_indent_inc();
1804 pr_out("%s:", name
);
1805 __pr_out_indent_inc();
1810 static void pr_out_array_end(struct dl
*dl
)
1812 if (dl
->json_output
) {
1813 jsonw_end_array(dl
->jw
);
1815 __pr_out_indent_dec();
1816 __pr_out_indent_dec();
1820 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1822 if (dl
->json_output
) {
1823 jsonw_name(dl
->jw
, name
);
1824 jsonw_start_object(dl
->jw
);
1826 __pr_out_indent_inc();
1828 pr_out("%s:", name
);
1829 __pr_out_indent_inc();
1834 static void pr_out_object_end(struct dl
*dl
)
1836 if (dl
->json_output
) {
1837 jsonw_end_object(dl
->jw
);
1839 __pr_out_indent_dec();
1840 __pr_out_indent_dec();
1844 static void pr_out_entry_start(struct dl
*dl
)
1846 if (dl
->json_output
)
1847 jsonw_start_object(dl
->jw
);
1850 static void pr_out_entry_end(struct dl
*dl
)
1852 if (dl
->json_output
)
1853 jsonw_end_object(dl
->jw
);
1858 static const char *param_cmode_name(uint8_t cmode
)
1861 case DEVLINK_PARAM_CMODE_RUNTIME
:
1862 return PARAM_CMODE_RUNTIME_STR
;
1863 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1864 return PARAM_CMODE_DRIVERINIT_STR
;
1865 case DEVLINK_PARAM_CMODE_PERMANENT
:
1866 return PARAM_CMODE_PERMANENT_STR
;
1867 default: return "<unknown type>";
1871 static const char *eswitch_mode_name(uint32_t mode
)
1874 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1875 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1876 default: return "<unknown mode>";
1880 static const char *eswitch_inline_mode_name(uint32_t mode
)
1883 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1884 return ESWITCH_INLINE_MODE_NONE
;
1885 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1886 return ESWITCH_INLINE_MODE_LINK
;
1887 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1888 return ESWITCH_INLINE_MODE_NETWORK
;
1889 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1890 return ESWITCH_INLINE_MODE_TRANSPORT
;
1892 return "<unknown mode>";
1896 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1898 __pr_out_handle_start(dl
, tb
, true, false);
1900 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1901 pr_out_str(dl
, "mode",
1902 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1904 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1905 pr_out_str(dl
, "inline-mode",
1906 eswitch_inline_mode_name(mnl_attr_get_u8(
1907 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1909 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1910 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1912 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1915 pr_out_handle_end(dl
);
1918 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1920 struct dl
*dl
= data
;
1921 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1922 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1924 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1925 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1926 return MNL_CB_ERROR
;
1927 pr_out_eswitch(dl
, tb
);
1931 static int cmd_dev_eswitch_show(struct dl
*dl
)
1933 struct nlmsghdr
*nlh
;
1936 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1937 NLM_F_REQUEST
| NLM_F_ACK
);
1939 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1943 pr_out_section_start(dl
, "dev");
1944 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1945 pr_out_section_end(dl
);
1949 static int cmd_dev_eswitch_set(struct dl
*dl
)
1951 struct nlmsghdr
*nlh
;
1954 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1955 NLM_F_REQUEST
| NLM_F_ACK
);
1957 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1958 DL_OPT_ESWITCH_MODE
|
1959 DL_OPT_ESWITCH_INLINE_MODE
|
1960 DL_OPT_ESWITCH_ENCAP_MODE
);
1965 if (dl
->opts
.present
== 1) {
1966 pr_err("Need to set at least one option\n");
1970 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1973 static int cmd_dev_eswitch(struct dl
*dl
)
1975 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1978 } else if (dl_argv_match(dl
, "set")) {
1980 return cmd_dev_eswitch_set(dl
);
1981 } else if (dl_argv_match(dl
, "show")) {
1983 return cmd_dev_eswitch_show(dl
);
1985 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1989 struct param_val_conv
{
1995 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
1996 uint32_t len
, const char *name
)
2000 for (i
= 0; i
< len
; i
++)
2001 if (!strcmp(param_val_conv
[i
].name
, name
))
2008 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2009 uint32_t len
, const char *name
, const char *vstr
,
2014 for (i
= 0; i
< len
; i
++)
2015 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2016 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2017 *vuint
= param_val_conv
[i
].vuint
;
2025 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2026 uint32_t len
, const char *name
, uint32_t vuint
,
2031 for (i
= 0; i
< len
; i
++)
2032 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2033 param_val_conv
[i
].vuint
== vuint
) {
2034 *vstr
= param_val_conv
[i
].vstr
;
2041 static const struct param_val_conv param_val_conv
[] = {
2043 .name
= "fw_load_policy",
2045 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2048 .name
= "fw_load_policy",
2050 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2054 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2056 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2057 int nla_type
, struct nlattr
*nl
)
2059 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2060 struct nlattr
*val_attr
;
2065 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2066 if (err
!= MNL_CB_OK
)
2069 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2070 (nla_type
!= MNL_TYPE_FLAG
&&
2071 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2074 pr_out_str(dl
, "cmode",
2075 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2076 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2078 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2084 err
= param_val_conv_str_get(param_val_conv
,
2087 mnl_attr_get_u8(val_attr
),
2091 pr_out_str(dl
, "value", vstr
);
2093 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2098 err
= param_val_conv_str_get(param_val_conv
,
2101 mnl_attr_get_u16(val_attr
),
2105 pr_out_str(dl
, "value", vstr
);
2107 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2112 err
= param_val_conv_str_get(param_val_conv
,
2115 mnl_attr_get_u32(val_attr
),
2119 pr_out_str(dl
, "value", vstr
);
2121 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2124 case MNL_TYPE_STRING
:
2125 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2128 pr_out_bool(dl
, "value", val_attr
? true : false);
2133 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2135 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2136 struct nlattr
*param_value_attr
;
2137 const char *nla_name
;
2141 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2142 if (err
!= MNL_CB_OK
)
2144 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2145 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2146 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2150 pr_out_handle_start_arr(dl
, tb
);
2152 __pr_out_handle_start(dl
, tb
, true, false);
2154 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2156 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2157 pr_out_str(dl
, "name", nla_name
);
2159 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2160 pr_out_str(dl
, "type", "driver-specific");
2162 pr_out_str(dl
, "type", "generic");
2164 pr_out_array_start(dl
, "values");
2165 mnl_attr_for_each_nested(param_value_attr
,
2166 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2167 pr_out_entry_start(dl
);
2168 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2169 pr_out_entry_end(dl
);
2171 pr_out_array_end(dl
);
2172 pr_out_handle_end(dl
);
2175 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2177 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2178 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2179 struct dl
*dl
= data
;
2181 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2182 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2183 !tb
[DEVLINK_ATTR_PARAM
])
2184 return MNL_CB_ERROR
;
2185 pr_out_param(dl
, tb
, true);
2201 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2203 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2204 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2205 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2206 struct nlattr
*param_value_attr
;
2207 enum devlink_param_cmode cmode
;
2208 struct param_ctx
*ctx
= data
;
2209 struct dl
*dl
= ctx
->dl
;
2213 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2214 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2215 !tb
[DEVLINK_ATTR_PARAM
])
2216 return MNL_CB_ERROR
;
2218 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2219 if (err
!= MNL_CB_OK
)
2220 return MNL_CB_ERROR
;
2222 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2223 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2224 return MNL_CB_ERROR
;
2226 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2227 mnl_attr_for_each_nested(param_value_attr
,
2228 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2229 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2230 struct nlattr
*val_attr
;
2232 err
= mnl_attr_parse_nested(param_value_attr
,
2233 attr_cb
, nla_value
);
2234 if (err
!= MNL_CB_OK
)
2235 return MNL_CB_ERROR
;
2237 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2238 (nla_type
!= MNL_TYPE_FLAG
&&
2239 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2240 return MNL_CB_ERROR
;
2242 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2243 if (cmode
== dl
->opts
.cmode
) {
2244 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2247 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2250 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2253 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2255 case MNL_TYPE_STRING
:
2256 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2259 ctx
->value
.vbool
= val_attr
? true : false;
2265 ctx
->nla_type
= nla_type
;
2269 static int cmd_dev_param_set(struct dl
*dl
)
2271 struct param_ctx ctx
= {};
2272 struct nlmsghdr
*nlh
;
2280 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2282 DL_OPT_PARAM_VALUE
|
2283 DL_OPT_PARAM_CMODE
, 0);
2287 /* Get value type */
2288 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2289 NLM_F_REQUEST
| NLM_F_ACK
);
2290 dl_opts_put(nlh
, dl
);
2293 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2297 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2298 NLM_F_REQUEST
| NLM_F_ACK
);
2299 dl_opts_put(nlh
, dl
);
2301 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2302 dl
->opts
.param_name
);
2304 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2305 switch (ctx
.nla_type
) {
2308 err
= param_val_conv_uint_get(param_val_conv
,
2310 dl
->opts
.param_name
,
2311 dl
->opts
.param_value
,
2315 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2318 goto err_param_value_parse
;
2319 if (val_u8
== ctx
.value
.vu8
)
2321 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2325 err
= param_val_conv_uint_get(param_val_conv
,
2327 dl
->opts
.param_name
,
2328 dl
->opts
.param_value
,
2332 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2335 goto err_param_value_parse
;
2336 if (val_u16
== ctx
.value
.vu16
)
2338 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2342 err
= param_val_conv_uint_get(param_val_conv
,
2344 dl
->opts
.param_name
,
2345 dl
->opts
.param_value
,
2348 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2350 goto err_param_value_parse
;
2351 if (val_u32
== ctx
.value
.vu32
)
2353 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2356 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2358 goto err_param_value_parse
;
2359 if (val_bool
== ctx
.value
.vbool
)
2362 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2365 case MNL_TYPE_STRING
:
2366 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2367 dl
->opts
.param_value
);
2368 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2372 printf("Value type not supported\n");
2375 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2377 err_param_value_parse
:
2378 pr_err("Value \"%s\" is not a number or not within range\n",
2379 dl
->opts
.param_value
);
2383 static int cmd_dev_param_show(struct dl
*dl
)
2385 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2386 struct nlmsghdr
*nlh
;
2389 if (dl_argc(dl
) == 0)
2390 flags
|= NLM_F_DUMP
;
2392 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2394 if (dl_argc(dl
) > 0) {
2395 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2396 DL_OPT_PARAM_NAME
, 0);
2401 pr_out_section_start(dl
, "param");
2402 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2403 pr_out_section_end(dl
);
2407 static int cmd_dev_param(struct dl
*dl
)
2409 if (dl_argv_match(dl
, "help")) {
2412 } else if (dl_argv_match(dl
, "show") ||
2413 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2415 return cmd_dev_param_show(dl
);
2416 } else if (dl_argv_match(dl
, "set")) {
2418 return cmd_dev_param_set(dl
);
2420 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2423 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2425 struct dl
*dl
= data
;
2426 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2427 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2429 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2430 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2431 return MNL_CB_ERROR
;
2436 static int cmd_dev_show(struct dl
*dl
)
2438 struct nlmsghdr
*nlh
;
2439 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2442 if (dl_argc(dl
) == 0)
2443 flags
|= NLM_F_DUMP
;
2445 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2447 if (dl_argc(dl
) > 0) {
2448 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2453 pr_out_section_start(dl
, "dev");
2454 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2455 pr_out_section_end(dl
);
2459 static void cmd_dev_reload_help(void)
2461 pr_err("Usage: devlink dev reload [ DEV ]\n");
2464 static int cmd_dev_reload(struct dl
*dl
)
2466 struct nlmsghdr
*nlh
;
2469 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2470 cmd_dev_reload_help();
2474 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2475 NLM_F_REQUEST
| NLM_F_ACK
);
2477 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2481 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2484 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2485 const char *name
, int type
)
2487 struct nlattr
*version
;
2489 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2490 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2491 const char *ver_value
;
2492 const char *ver_name
;
2495 if (mnl_attr_get_type(version
) != type
)
2498 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2499 if (err
!= MNL_CB_OK
)
2502 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2503 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2507 pr_out_object_start(dl
, name
);
2511 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2512 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2514 pr_out_str(dl
, ver_name
, ver_value
);
2515 if (!dl
->json_output
)
2520 pr_out_object_end(dl
);
2523 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2524 struct nlattr
**tb
, bool has_versions
)
2526 __pr_out_handle_start(dl
, tb
, true, false);
2528 __pr_out_indent_inc();
2529 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2530 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2532 if (!dl
->json_output
)
2534 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2537 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2538 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2540 if (!dl
->json_output
)
2542 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2544 __pr_out_indent_dec();
2547 pr_out_object_start(dl
, "versions");
2549 pr_out_versions_single(dl
, nlh
, "fixed",
2550 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2551 pr_out_versions_single(dl
, nlh
, "running",
2552 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2553 pr_out_versions_single(dl
, nlh
, "stored",
2554 DEVLINK_ATTR_INFO_VERSION_STORED
);
2556 pr_out_object_end(dl
);
2559 pr_out_handle_end(dl
);
2562 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2564 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2565 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2566 bool has_versions
, has_info
;
2567 struct dl
*dl
= data
;
2569 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2571 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2572 return MNL_CB_ERROR
;
2574 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2575 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2576 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2577 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2578 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2582 pr_out_info(dl
, nlh
, tb
, has_versions
);
2587 static void cmd_dev_info_help(void)
2589 pr_err("Usage: devlink dev info [ DEV ]\n");
2592 static int cmd_dev_info(struct dl
*dl
)
2594 struct nlmsghdr
*nlh
;
2595 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2598 if (dl_argv_match(dl
, "help")) {
2599 cmd_dev_info_help();
2603 if (dl_argc(dl
) == 0)
2604 flags
|= NLM_F_DUMP
;
2606 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2608 if (dl_argc(dl
) > 0) {
2609 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2614 pr_out_section_start(dl
, "info");
2615 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2616 pr_out_section_end(dl
);
2620 static void cmd_dev_flash_help(void)
2622 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2625 static int cmd_dev_flash(struct dl
*dl
)
2627 struct nlmsghdr
*nlh
;
2630 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2631 cmd_dev_flash_help();
2635 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2636 NLM_F_REQUEST
| NLM_F_ACK
);
2638 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2639 DL_OPT_FLASH_COMPONENT
);
2643 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2646 static int cmd_dev(struct dl
*dl
)
2648 if (dl_argv_match(dl
, "help")) {
2651 } else if (dl_argv_match(dl
, "show") ||
2652 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2654 return cmd_dev_show(dl
);
2655 } else if (dl_argv_match(dl
, "eswitch")) {
2657 return cmd_dev_eswitch(dl
);
2658 } else if (dl_argv_match(dl
, "reload")) {
2660 return cmd_dev_reload(dl
);
2661 } else if (dl_argv_match(dl
, "param")) {
2663 return cmd_dev_param(dl
);
2664 } else if (dl_argv_match(dl
, "info")) {
2666 return cmd_dev_info(dl
);
2667 } else if (dl_argv_match(dl
, "flash")) {
2669 return cmd_dev_flash(dl
);
2671 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2675 static void cmd_port_help(void)
2677 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2678 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2679 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2680 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2683 static const char *port_type_name(uint32_t type
)
2686 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2687 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2688 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2689 case DEVLINK_PORT_TYPE_IB
: return "ib";
2690 default: return "<unknown type>";
2694 static const char *port_flavour_name(uint16_t flavour
)
2697 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2699 case DEVLINK_PORT_FLAVOUR_CPU
:
2701 case DEVLINK_PORT_FLAVOUR_DSA
:
2704 return "<unknown flavour>";
2708 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2710 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2711 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2713 pr_out_port_handle_start(dl
, tb
, false);
2715 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2717 pr_out_str(dl
, "type", port_type_name(port_type
));
2719 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2721 if (port_type
!= des_port_type
)
2722 pr_out_str(dl
, "des_type",
2723 port_type_name(des_port_type
));
2726 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2727 pr_out_str(dl
, "netdev",
2728 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2729 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2730 pr_out_str(dl
, "ibdev",
2731 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2732 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2733 uint16_t port_flavour
=
2734 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2736 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2738 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2739 pr_out_uint(dl
, "split_group",
2740 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2741 pr_out_port_handle_end(dl
);
2744 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2746 struct dl
*dl
= data
;
2747 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2748 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2750 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2751 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2752 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2753 return MNL_CB_ERROR
;
2754 pr_out_port(dl
, tb
);
2758 static int cmd_port_show(struct dl
*dl
)
2760 struct nlmsghdr
*nlh
;
2761 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2764 if (dl_argc(dl
) == 0)
2765 flags
|= NLM_F_DUMP
;
2767 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2769 if (dl_argc(dl
) > 0) {
2770 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2775 pr_out_section_start(dl
, "port");
2776 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2777 pr_out_section_end(dl
);
2781 static int cmd_port_set(struct dl
*dl
)
2783 struct nlmsghdr
*nlh
;
2786 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2787 NLM_F_REQUEST
| NLM_F_ACK
);
2789 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2793 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2796 static int cmd_port_split(struct dl
*dl
)
2798 struct nlmsghdr
*nlh
;
2801 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2802 NLM_F_REQUEST
| NLM_F_ACK
);
2804 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2808 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2811 static int cmd_port_unsplit(struct dl
*dl
)
2813 struct nlmsghdr
*nlh
;
2816 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2817 NLM_F_REQUEST
| NLM_F_ACK
);
2819 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2823 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2826 static int cmd_port(struct dl
*dl
)
2828 if (dl_argv_match(dl
, "help")) {
2831 } else if (dl_argv_match(dl
, "show") ||
2832 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2834 return cmd_port_show(dl
);
2835 } else if (dl_argv_match(dl
, "set")) {
2837 return cmd_port_set(dl
);
2838 } else if (dl_argv_match(dl
, "split")) {
2840 return cmd_port_split(dl
);
2841 } else if (dl_argv_match(dl
, "unsplit")) {
2843 return cmd_port_unsplit(dl
);
2845 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2849 static void cmd_sb_help(void)
2851 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2852 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2853 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2854 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2855 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2856 pr_err(" pool POOL_INDEX ]\n");
2857 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2858 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2859 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2860 pr_err(" type { ingress | egress } ]\n");
2861 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2862 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2863 pr_err(" th THRESHOLD\n");
2864 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2865 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2866 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2869 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2871 pr_out_handle_start_arr(dl
, tb
);
2872 pr_out_uint(dl
, "sb",
2873 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2874 pr_out_uint(dl
, "size",
2875 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2876 pr_out_uint(dl
, "ing_pools",
2877 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2878 pr_out_uint(dl
, "eg_pools",
2879 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2880 pr_out_uint(dl
, "ing_tcs",
2881 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2882 pr_out_uint(dl
, "eg_tcs",
2883 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2884 pr_out_handle_end(dl
);
2887 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2889 struct dl
*dl
= data
;
2890 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2891 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2893 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2894 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2895 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2896 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2897 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2898 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2899 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2900 return MNL_CB_ERROR
;
2905 static int cmd_sb_show(struct dl
*dl
)
2907 struct nlmsghdr
*nlh
;
2908 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2911 if (dl_argc(dl
) == 0)
2912 flags
|= NLM_F_DUMP
;
2914 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2916 if (dl_argc(dl
) > 0) {
2917 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2922 pr_out_section_start(dl
, "sb");
2923 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2924 pr_out_section_end(dl
);
2928 static const char *pool_type_name(uint8_t type
)
2931 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2932 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2933 default: return "<unknown type>";
2937 static const char *threshold_type_name(uint8_t type
)
2940 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2941 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2942 default: return "<unknown type>";
2946 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2948 pr_out_handle_start_arr(dl
, tb
);
2949 pr_out_uint(dl
, "sb",
2950 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2951 pr_out_uint(dl
, "pool",
2952 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2953 pr_out_str(dl
, "type",
2954 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2955 pr_out_uint(dl
, "size",
2956 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2957 pr_out_str(dl
, "thtype",
2958 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2959 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
2960 pr_out_uint(dl
, "cell_size",
2961 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
2962 pr_out_handle_end(dl
);
2965 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2967 struct dl
*dl
= data
;
2968 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2969 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2971 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2972 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2973 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2974 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2975 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2976 return MNL_CB_ERROR
;
2977 pr_out_sb_pool(dl
, tb
);
2981 static int cmd_sb_pool_show(struct dl
*dl
)
2983 struct nlmsghdr
*nlh
;
2984 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2987 if (dl_argc(dl
) == 0)
2988 flags
|= NLM_F_DUMP
;
2990 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2992 if (dl_argc(dl
) > 0) {
2993 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2999 pr_out_section_start(dl
, "pool");
3000 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3001 pr_out_section_end(dl
);
3005 static int cmd_sb_pool_set(struct dl
*dl
)
3007 struct nlmsghdr
*nlh
;
3010 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3011 NLM_F_REQUEST
| NLM_F_ACK
);
3013 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3014 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3018 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3021 static int cmd_sb_pool(struct dl
*dl
)
3023 if (dl_argv_match(dl
, "help")) {
3026 } else if (dl_argv_match(dl
, "show") ||
3027 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3029 return cmd_sb_pool_show(dl
);
3030 } else if (dl_argv_match(dl
, "set")) {
3032 return cmd_sb_pool_set(dl
);
3034 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3038 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3040 pr_out_port_handle_start_arr(dl
, tb
, true);
3041 pr_out_uint(dl
, "sb",
3042 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3043 pr_out_uint(dl
, "pool",
3044 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3045 pr_out_uint(dl
, "threshold",
3046 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3047 pr_out_port_handle_end(dl
);
3050 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3052 struct dl
*dl
= data
;
3053 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3054 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3056 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3057 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3058 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3059 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3060 return MNL_CB_ERROR
;
3061 pr_out_sb_port_pool(dl
, tb
);
3065 static int cmd_sb_port_pool_show(struct dl
*dl
)
3067 struct nlmsghdr
*nlh
;
3068 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3071 if (dl_argc(dl
) == 0)
3072 flags
|= NLM_F_DUMP
;
3074 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3076 if (dl_argc(dl
) > 0) {
3077 err
= dl_argv_parse_put(nlh
, dl
,
3078 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3084 pr_out_section_start(dl
, "port_pool");
3085 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3086 pr_out_section_end(dl
);
3090 static int cmd_sb_port_pool_set(struct dl
*dl
)
3092 struct nlmsghdr
*nlh
;
3095 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3096 NLM_F_REQUEST
| NLM_F_ACK
);
3098 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3099 DL_OPT_SB_TH
, DL_OPT_SB
);
3103 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3106 static int cmd_sb_port_pool(struct dl
*dl
)
3108 if (dl_argv_match(dl
, "help")) {
3111 } else if (dl_argv_match(dl
, "show") ||
3112 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3114 return cmd_sb_port_pool_show(dl
);
3115 } else if (dl_argv_match(dl
, "set")) {
3117 return cmd_sb_port_pool_set(dl
);
3119 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3123 static int cmd_sb_port(struct dl
*dl
)
3125 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3128 } else if (dl_argv_match(dl
, "pool")) {
3130 return cmd_sb_port_pool(dl
);
3132 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3136 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3138 pr_out_port_handle_start_arr(dl
, tb
, true);
3139 pr_out_uint(dl
, "sb",
3140 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3141 pr_out_uint(dl
, "tc",
3142 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3143 pr_out_str(dl
, "type",
3144 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3145 pr_out_uint(dl
, "pool",
3146 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3147 pr_out_uint(dl
, "threshold",
3148 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3149 pr_out_port_handle_end(dl
);
3152 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3154 struct dl
*dl
= data
;
3155 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3156 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3158 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3159 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3160 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3161 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3162 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3163 return MNL_CB_ERROR
;
3164 pr_out_sb_tc_bind(dl
, tb
);
3168 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3170 struct nlmsghdr
*nlh
;
3171 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3174 if (dl_argc(dl
) == 0)
3175 flags
|= NLM_F_DUMP
;
3177 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3179 if (dl_argc(dl
) > 0) {
3180 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3181 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3186 pr_out_section_start(dl
, "tc_bind");
3187 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3188 pr_out_section_end(dl
);
3192 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3194 struct nlmsghdr
*nlh
;
3197 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3198 NLM_F_REQUEST
| NLM_F_ACK
);
3200 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3201 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3206 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3209 static int cmd_sb_tc_bind(struct dl
*dl
)
3211 if (dl_argv_match(dl
, "help")) {
3214 } else if (dl_argv_match(dl
, "show") ||
3215 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3217 return cmd_sb_tc_bind_show(dl
);
3218 } else if (dl_argv_match(dl
, "set")) {
3220 return cmd_sb_tc_bind_set(dl
);
3222 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3226 static int cmd_sb_tc(struct dl
*dl
)
3228 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3231 } else if (dl_argv_match(dl
, "bind")) {
3233 return cmd_sb_tc_bind(dl
);
3235 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3240 struct list_head list
;
3244 uint32_t bound_pool_index
;
3248 struct list_head list
;
3251 uint32_t port_index
;
3253 struct list_head pool_list
;
3254 struct list_head ing_tc_list
;
3255 struct list_head eg_tc_list
;
3261 struct list_head port_list
;
3264 static struct occ_item
*occ_item_alloc(void)
3266 return calloc(1, sizeof(struct occ_item
));
3269 static void occ_item_free(struct occ_item
*occ_item
)
3274 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3276 struct occ_port
*occ_port
;
3278 occ_port
= calloc(1, sizeof(*occ_port
));
3281 occ_port
->port_index
= port_index
;
3282 INIT_LIST_HEAD(&occ_port
->pool_list
);
3283 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3284 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3288 static void occ_port_free(struct occ_port
*occ_port
)
3290 struct occ_item
*occ_item
, *tmp
;
3292 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3293 occ_item_free(occ_item
);
3294 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3295 occ_item_free(occ_item
);
3296 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3297 occ_item_free(occ_item
);
3300 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3302 struct occ_show
*occ_show
;
3304 occ_show
= calloc(1, sizeof(*occ_show
));
3308 INIT_LIST_HEAD(&occ_show
->port_list
);
3312 static void occ_show_free(struct occ_show
*occ_show
)
3314 struct occ_port
*occ_port
, *tmp
;
3316 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3317 occ_port_free(occ_port
);
3320 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3323 struct occ_port
*occ_port
;
3324 uint32_t port_index
;
3326 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3328 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3329 if (occ_port
->port_index
== port_index
)
3332 occ_port
= occ_port_alloc(port_index
);
3335 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3339 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3342 struct occ_item
*occ_item
;
3345 pr_out_sp(7, " %s:", label
);
3346 list_for_each_entry(occ_item
, list
, list
) {
3347 if ((i
- 1) % 4 == 0 && i
!= 1)
3350 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3351 occ_item
->bound_pool_index
);
3353 pr_out_sp(7, "%2u:", occ_item
->index
);
3354 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
3358 if ((i
- 1) % 4 != 0)
3362 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3363 struct list_head
*list
,
3366 struct occ_item
*occ_item
;
3369 jsonw_name(dl
->jw
, label
);
3370 jsonw_start_object(dl
->jw
);
3371 list_for_each_entry(occ_item
, list
, list
) {
3372 sprintf(buf
, "%u", occ_item
->index
);
3373 jsonw_name(dl
->jw
, buf
);
3374 jsonw_start_object(dl
->jw
);
3376 jsonw_uint_field(dl
->jw
, "bound_pool",
3377 occ_item
->bound_pool_index
);
3378 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3379 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3380 jsonw_end_object(dl
->jw
);
3382 jsonw_end_object(dl
->jw
);
3385 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3387 if (dl
->json_output
) {
3388 pr_out_json_occ_show_item_list(dl
, "pool",
3389 &occ_port
->pool_list
, false);
3390 pr_out_json_occ_show_item_list(dl
, "itc",
3391 &occ_port
->ing_tc_list
, true);
3392 pr_out_json_occ_show_item_list(dl
, "etc",
3393 &occ_port
->eg_tc_list
, true);
3396 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3397 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3398 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3402 static void pr_out_occ_show(struct occ_show
*occ_show
)
3404 struct dl
*dl
= occ_show
->dl
;
3405 struct dl_opts
*opts
= &dl
->opts
;
3406 struct occ_port
*occ_port
;
3408 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3409 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3410 occ_port
->port_index
, true, false);
3411 pr_out_occ_show_port(dl
, occ_port
);
3412 pr_out_port_handle_end(dl
);
3416 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3419 struct occ_port
*occ_port
;
3420 struct occ_item
*occ_item
;
3422 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3425 occ_port
= occ_port_get(occ_show
, tb
);
3427 occ_show
->err
= -ENOMEM
;
3431 occ_item
= occ_item_alloc();
3433 occ_show
->err
= -ENOMEM
;
3436 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3437 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3438 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3439 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3442 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3444 struct occ_show
*occ_show
= data
;
3445 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3446 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3448 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3449 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3450 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3451 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3452 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3453 return MNL_CB_ERROR
;
3454 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3458 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3461 struct occ_port
*occ_port
;
3462 struct occ_item
*occ_item
;
3465 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3468 occ_port
= occ_port_get(occ_show
, tb
);
3470 occ_show
->err
= -ENOMEM
;
3474 occ_item
= occ_item_alloc();
3476 occ_show
->err
= -ENOMEM
;
3479 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3480 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3481 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3482 occ_item
->bound_pool_index
=
3483 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3484 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3485 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3486 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3487 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3488 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3490 occ_item_free(occ_item
);
3493 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3495 struct occ_show
*occ_show
= data
;
3496 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3497 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3499 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3500 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3501 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3502 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3503 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3504 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3505 return MNL_CB_ERROR
;
3506 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3510 static int cmd_sb_occ_show(struct dl
*dl
)
3512 struct nlmsghdr
*nlh
;
3513 struct occ_show
*occ_show
;
3514 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3517 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3521 occ_show
= occ_show_alloc(dl
);
3525 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3527 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3528 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3532 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3534 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3535 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3539 pr_out_section_start(dl
, "occupancy");
3540 pr_out_occ_show(occ_show
);
3541 pr_out_section_end(dl
);
3544 occ_show_free(occ_show
);
3548 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3550 struct nlmsghdr
*nlh
;
3553 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3554 NLM_F_REQUEST
| NLM_F_ACK
);
3556 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3560 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3563 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3565 struct nlmsghdr
*nlh
;
3568 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3569 NLM_F_REQUEST
| NLM_F_ACK
);
3571 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3575 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3578 static int cmd_sb_occ(struct dl
*dl
)
3580 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3583 } else if (dl_argv_match(dl
, "show") ||
3584 dl_argv_match(dl
, "list")) {
3586 return cmd_sb_occ_show(dl
);
3587 } else if (dl_argv_match(dl
, "snapshot")) {
3589 return cmd_sb_occ_snapshot(dl
);
3590 } else if (dl_argv_match(dl
, "clearmax")) {
3592 return cmd_sb_occ_clearmax(dl
);
3594 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3598 static int cmd_sb(struct dl
*dl
)
3600 if (dl_argv_match(dl
, "help")) {
3603 } else if (dl_argv_match(dl
, "show") ||
3604 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3606 return cmd_sb_show(dl
);
3607 } else if (dl_argv_match(dl
, "pool")) {
3609 return cmd_sb_pool(dl
);
3610 } else if (dl_argv_match(dl
, "port")) {
3612 return cmd_sb_port(dl
);
3613 } else if (dl_argv_match(dl
, "tc")) {
3615 return cmd_sb_tc(dl
);
3616 } else if (dl_argv_match(dl
, "occupancy")) {
3618 return cmd_sb_occ(dl
);
3620 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3624 static const char *cmd_name(uint8_t cmd
)
3627 case DEVLINK_CMD_UNSPEC
: return "unspec";
3628 case DEVLINK_CMD_GET
: return "get";
3629 case DEVLINK_CMD_SET
: return "set";
3630 case DEVLINK_CMD_NEW
: return "new";
3631 case DEVLINK_CMD_DEL
: return "del";
3632 case DEVLINK_CMD_PORT_GET
: return "get";
3633 case DEVLINK_CMD_PORT_SET
: return "set";
3634 case DEVLINK_CMD_PORT_NEW
: return "new";
3635 case DEVLINK_CMD_PORT_DEL
: return "del";
3636 case DEVLINK_CMD_PARAM_GET
: return "get";
3637 case DEVLINK_CMD_PARAM_SET
: return "set";
3638 case DEVLINK_CMD_PARAM_NEW
: return "new";
3639 case DEVLINK_CMD_PARAM_DEL
: return "del";
3640 case DEVLINK_CMD_REGION_GET
: return "get";
3641 case DEVLINK_CMD_REGION_SET
: return "set";
3642 case DEVLINK_CMD_REGION_NEW
: return "new";
3643 case DEVLINK_CMD_REGION_DEL
: return "del";
3644 default: return "<unknown cmd>";
3648 static const char *cmd_obj(uint8_t cmd
)
3651 case DEVLINK_CMD_UNSPEC
: return "unspec";
3652 case DEVLINK_CMD_GET
:
3653 case DEVLINK_CMD_SET
:
3654 case DEVLINK_CMD_NEW
:
3655 case DEVLINK_CMD_DEL
:
3657 case DEVLINK_CMD_PORT_GET
:
3658 case DEVLINK_CMD_PORT_SET
:
3659 case DEVLINK_CMD_PORT_NEW
:
3660 case DEVLINK_CMD_PORT_DEL
:
3662 case DEVLINK_CMD_PARAM_GET
:
3663 case DEVLINK_CMD_PARAM_SET
:
3664 case DEVLINK_CMD_PARAM_NEW
:
3665 case DEVLINK_CMD_PARAM_DEL
:
3667 case DEVLINK_CMD_REGION_GET
:
3668 case DEVLINK_CMD_REGION_SET
:
3669 case DEVLINK_CMD_REGION_NEW
:
3670 case DEVLINK_CMD_REGION_DEL
:
3672 default: return "<unknown obj>";
3676 static void pr_out_mon_header(uint8_t cmd
)
3678 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3681 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3683 const char *obj
= cmd_obj(cmd
);
3684 unsigned int index
= 0;
3685 const char *cur_obj
;
3689 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3690 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3696 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3698 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3700 struct dl
*dl
= data
;
3701 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3702 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3703 uint8_t cmd
= genl
->cmd
;
3705 if (!cmd_filter_check(dl
, cmd
))
3709 case DEVLINK_CMD_GET
: /* fall through */
3710 case DEVLINK_CMD_SET
: /* fall through */
3711 case DEVLINK_CMD_NEW
: /* fall through */
3712 case DEVLINK_CMD_DEL
:
3713 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3714 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3715 return MNL_CB_ERROR
;
3716 pr_out_mon_header(genl
->cmd
);
3719 case DEVLINK_CMD_PORT_GET
: /* fall through */
3720 case DEVLINK_CMD_PORT_SET
: /* fall through */
3721 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3722 case DEVLINK_CMD_PORT_DEL
:
3723 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3724 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3725 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3726 return MNL_CB_ERROR
;
3727 pr_out_mon_header(genl
->cmd
);
3728 pr_out_port(dl
, tb
);
3730 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3731 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3732 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3733 case DEVLINK_CMD_PARAM_DEL
:
3734 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3735 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3736 !tb
[DEVLINK_ATTR_PARAM
])
3737 return MNL_CB_ERROR
;
3738 pr_out_mon_header(genl
->cmd
);
3739 pr_out_param(dl
, tb
, false);
3741 case DEVLINK_CMD_REGION_GET
: /* fall through */
3742 case DEVLINK_CMD_REGION_SET
: /* fall through */
3743 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3744 case DEVLINK_CMD_REGION_DEL
:
3745 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3746 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3747 !tb
[DEVLINK_ATTR_REGION_NAME
])
3748 return MNL_CB_ERROR
;
3749 pr_out_mon_header(genl
->cmd
);
3750 pr_out_region(dl
, tb
);
3756 static int cmd_mon_show(struct dl
*dl
)
3759 unsigned int index
= 0;
3760 const char *cur_obj
;
3762 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3763 if (strcmp(cur_obj
, "all") != 0 &&
3764 strcmp(cur_obj
, "dev") != 0 &&
3765 strcmp(cur_obj
, "port") != 0) {
3766 pr_err("Unknown object \"%s\"\n", cur_obj
);
3770 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3773 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3779 static void cmd_mon_help(void)
3781 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3782 "where OBJECT-LIST := { dev | port }\n");
3785 static int cmd_mon(struct dl
*dl
)
3787 if (dl_argv_match(dl
, "help")) {
3790 } else if (dl_no_arg(dl
)) {
3792 return cmd_mon_show(dl
);
3794 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3798 struct dpipe_field
{
3801 unsigned int bitwidth
;
3802 enum devlink_dpipe_field_mapping_type mapping_type
;
3805 struct dpipe_header
{
3806 struct list_head list
;
3809 struct dpipe_field
*fields
;
3810 unsigned int fields_count
;
3813 struct dpipe_table
{
3814 struct list_head list
;
3816 unsigned int resource_id
;
3817 bool resource_valid
;
3820 struct dpipe_tables
{
3821 struct list_head table_list
;
3831 enum devlink_resource_unit unit
;
3836 struct list_head list
;
3837 struct list_head resource_list
;
3838 struct resource
*parent
;
3842 struct list_head resource_list
;
3845 struct resource_ctx
{
3848 struct resources
*resources
;
3849 struct dpipe_tables
*tables
;
3850 bool print_resources
;
3851 bool pending_change
;
3854 static struct resource
*resource_alloc(void)
3856 struct resource
*resource
;
3858 resource
= calloc(1, sizeof(struct resource
));
3861 INIT_LIST_HEAD(&resource
->resource_list
);
3865 static void resource_free(struct resource
*resource
)
3867 struct resource
*child_resource
, *tmp
;
3869 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3871 free(child_resource
->name
);
3872 resource_free(child_resource
);
3877 static struct resources
*resources_alloc(void)
3879 struct resources
*resources
;
3881 resources
= calloc(1, sizeof(struct resources
));
3884 INIT_LIST_HEAD(&resources
->resource_list
);
3888 static void resources_free(struct resources
*resources
)
3890 struct resource
*resource
, *tmp
;
3892 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3893 resource_free(resource
);
3896 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3898 ctx
->resources
= resources_alloc();
3899 if (!ctx
->resources
)
3905 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3907 resources_free(ctx
->resources
);
3913 struct list_head global_headers
;
3914 struct list_head local_headers
;
3915 struct dpipe_tables
*tables
;
3916 struct resources
*resources
;
3921 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3923 struct dpipe_header
*header
;
3925 header
= calloc(1, sizeof(struct dpipe_header
));
3928 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3929 if (!header
->fields
)
3930 goto err_fields_alloc
;
3931 header
->fields_count
= fields_count
;
3939 static void dpipe_header_free(struct dpipe_header
*header
)
3941 free(header
->fields
);
3945 static void dpipe_header_clear(struct dpipe_header
*header
)
3947 struct dpipe_field
*field
;
3950 for (i
= 0; i
< header
->fields_count
; i
++) {
3951 field
= &header
->fields
[i
];
3957 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3958 struct dpipe_header
*header
, bool global
)
3961 list_add(&header
->list
, &ctx
->global_headers
);
3963 list_add(&header
->list
, &ctx
->local_headers
);
3966 static void dpipe_header_del(struct dpipe_header
*header
)
3968 list_del(&header
->list
);
3971 static struct dpipe_table
*dpipe_table_alloc(void)
3973 return calloc(1, sizeof(struct dpipe_table
));
3976 static void dpipe_table_free(struct dpipe_table
*table
)
3981 static struct dpipe_tables
*dpipe_tables_alloc(void)
3983 struct dpipe_tables
*tables
;
3985 tables
= calloc(1, sizeof(struct dpipe_tables
));
3988 INIT_LIST_HEAD(&tables
->table_list
);
3992 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3994 struct dpipe_table
*table
, *tmp
;
3996 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3997 dpipe_table_free(table
);
4001 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4003 ctx
->tables
= dpipe_tables_alloc();
4008 INIT_LIST_HEAD(&ctx
->global_headers
);
4009 INIT_LIST_HEAD(&ctx
->local_headers
);
4013 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4015 struct dpipe_header
*header
, *tmp
;
4017 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4019 dpipe_header_del(header
);
4020 dpipe_header_clear(header
);
4021 dpipe_header_free(header
);
4023 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4025 dpipe_header_del(header
);
4026 dpipe_header_clear(header
);
4027 dpipe_header_free(header
);
4029 dpipe_tables_free(ctx
->tables
);
4032 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4033 uint32_t header_id
, bool global
)
4035 struct list_head
*header_list
;
4036 struct dpipe_header
*header
;
4039 header_list
= &ctx
->global_headers
;
4041 header_list
= &ctx
->local_headers
;
4042 list_for_each_entry(header
, header_list
, list
) {
4043 if (header
->id
!= header_id
)
4045 return header
->name
;
4050 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4052 uint32_t field_id
, bool global
)
4054 struct list_head
*header_list
;
4055 struct dpipe_header
*header
;
4058 header_list
= &ctx
->global_headers
;
4060 header_list
= &ctx
->local_headers
;
4061 list_for_each_entry(header
, header_list
, list
) {
4062 if (header
->id
!= header_id
)
4064 return header
->fields
[field_id
].name
;
4070 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4072 switch (mapping_type
) {
4073 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4075 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4083 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4084 uint32_t field_id
, bool global
)
4086 enum devlink_dpipe_field_mapping_type mapping_type
;
4087 struct list_head
*header_list
;
4088 struct dpipe_header
*header
;
4091 header_list
= &ctx
->global_headers
;
4093 header_list
= &ctx
->local_headers
;
4094 list_for_each_entry(header
, header_list
, list
) {
4095 if (header
->id
!= header_id
)
4097 mapping_type
= header
->fields
[field_id
].mapping_type
;
4098 return dpipe_field_mapping_e2s(mapping_type
);
4103 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4104 struct dpipe_field
*fields
,
4105 unsigned int field_count
)
4107 struct dpipe_field
*field
;
4110 for (i
= 0; i
< field_count
; i
++) {
4112 pr_out_entry_start(ctx
->dl
);
4113 pr_out_str(ctx
->dl
, "name", field
->name
);
4114 if (ctx
->dl
->verbose
)
4115 pr_out_uint(ctx
->dl
, "id", field
->id
);
4116 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4117 if (field
->mapping_type
)
4118 pr_out_str(ctx
->dl
, "mapping_type",
4119 dpipe_field_mapping_e2s(field
->mapping_type
));
4120 pr_out_entry_end(ctx
->dl
);
4125 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4126 struct dpipe_header
*header
, bool global
)
4128 pr_out_handle_start_arr(ctx
->dl
, tb
);
4129 pr_out_str(ctx
->dl
, "name", header
->name
);
4130 if (ctx
->dl
->verbose
) {
4131 pr_out_uint(ctx
->dl
, "id", header
->id
);
4132 pr_out_str(ctx
->dl
, "global",
4133 global
? "true" : "false");
4135 pr_out_array_start(ctx
->dl
, "field");
4136 pr_out_dpipe_fields(ctx
, header
->fields
,
4137 header
->fields_count
);
4138 pr_out_array_end(ctx
->dl
);
4139 pr_out_handle_end(ctx
->dl
);
4142 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4145 struct dpipe_header
*header
;
4147 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4148 pr_out_dpipe_header(ctx
, tb
, header
, false);
4150 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4151 pr_out_dpipe_header(ctx
, tb
, header
, true);
4154 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4156 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4160 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4161 if (err
!= MNL_CB_OK
)
4163 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4164 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4165 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4166 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4169 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4170 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4171 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4172 field
->name
= strdup(name
);
4175 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4179 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4180 struct dpipe_field
*fields
)
4182 struct nlattr
*nla_field
;
4186 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4187 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4195 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4197 struct nlattr
*nla_field
;
4198 unsigned int count
= 0;
4200 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4205 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4207 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4208 struct dpipe_header
*header
;
4209 unsigned int fields_count
;
4210 const char *header_name
;
4214 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4215 if (err
!= MNL_CB_OK
)
4218 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4219 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4220 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4223 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4224 header
= dpipe_header_alloc(fields_count
);
4228 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4229 header
->name
= strdup(header_name
);
4230 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4231 header
->fields_count
= fields_count
;
4232 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4234 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4238 dpipe_header_add(ctx
, header
, global
);
4242 dpipe_header_free(header
);
4246 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4248 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4249 struct nlattr
*nla_header
;
4252 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4253 err
= dpipe_header_get(ctx
, nla_header
);
4260 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4262 struct dpipe_ctx
*ctx
= data
;
4263 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4264 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4267 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4268 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4269 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4270 return MNL_CB_ERROR
;
4271 err
= dpipe_headers_get(ctx
, tb
);
4274 return MNL_CB_ERROR
;
4277 if (ctx
->print_headers
)
4278 pr_out_dpipe_headers(ctx
, tb
);
4282 static int cmd_dpipe_headers_show(struct dl
*dl
)
4284 struct nlmsghdr
*nlh
;
4285 struct dpipe_ctx ctx
= {};
4286 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4289 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4291 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4295 err
= dpipe_ctx_init(&ctx
, dl
);
4299 ctx
.print_headers
= true;
4301 pr_out_section_start(dl
, "header");
4302 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4304 pr_err("error get headers %s\n", strerror(ctx
.err
));
4305 pr_out_section_end(dl
);
4307 dpipe_ctx_fini(&ctx
);
4311 static void cmd_dpipe_header_help(void)
4313 pr_err("Usage: devlink dpipe headers show DEV\n");
4316 static int cmd_dpipe_header(struct dl
*dl
)
4318 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4319 cmd_dpipe_header_help();
4321 } else if (dl_argv_match(dl
, "show")) {
4323 return cmd_dpipe_headers_show(dl
);
4325 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4330 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4332 switch (action_type
) {
4333 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4334 return "field_modify";
4340 struct dpipe_op_info
{
4346 struct dpipe_action
{
4347 struct dpipe_op_info info
;
4351 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4352 struct dpipe_ctx
*ctx
)
4354 struct dpipe_op_info
*op_info
= &action
->info
;
4355 const char *mapping
;
4357 pr_out_str(ctx
->dl
, "type",
4358 dpipe_action_type_e2s(action
->type
));
4359 pr_out_str(ctx
->dl
, "header",
4360 dpipe_header_id2s(ctx
, op_info
->header_id
,
4361 op_info
->header_global
));
4362 pr_out_str(ctx
->dl
, "field",
4363 dpipe_field_id2s(ctx
, op_info
->header_id
,
4365 op_info
->header_global
));
4366 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4368 op_info
->header_global
);
4370 pr_out_str(ctx
->dl
, "mapping", mapping
);
4373 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4375 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4378 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4379 if (err
!= MNL_CB_OK
)
4382 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4383 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4384 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4385 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4389 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4390 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4391 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4392 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4397 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4398 struct nlattr
*nla_actions
)
4400 struct nlattr
*nla_action
;
4401 struct dpipe_action action
;
4403 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4404 pr_out_entry_start(ctx
->dl
);
4405 if (dpipe_action_parse(&action
, nla_action
))
4406 goto err_action_parse
;
4407 pr_out_dpipe_action(&action
, ctx
);
4408 pr_out_entry_end(ctx
->dl
);
4413 pr_out_entry_end(ctx
->dl
);
4418 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4420 switch (match_type
) {
4421 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4422 return "field_exact";
4428 struct dpipe_match
{
4429 struct dpipe_op_info info
;
4433 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4434 struct dpipe_ctx
*ctx
)
4436 struct dpipe_op_info
*op_info
= &match
->info
;
4437 const char *mapping
;
4439 pr_out_str(ctx
->dl
, "type",
4440 dpipe_match_type_e2s(match
->type
));
4441 pr_out_str(ctx
->dl
, "header",
4442 dpipe_header_id2s(ctx
, op_info
->header_id
,
4443 op_info
->header_global
));
4444 pr_out_str(ctx
->dl
, "field",
4445 dpipe_field_id2s(ctx
, op_info
->header_id
,
4447 op_info
->header_global
));
4448 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4450 op_info
->header_global
);
4452 pr_out_str(ctx
->dl
, "mapping", mapping
);
4455 static int dpipe_match_parse(struct dpipe_match
*match
,
4459 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4462 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4463 if (err
!= MNL_CB_OK
)
4466 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4467 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4468 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4469 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4473 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4474 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4475 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4476 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4481 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4482 struct nlattr
*nla_matches
)
4484 struct nlattr
*nla_match
;
4485 struct dpipe_match match
;
4487 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4488 pr_out_entry_start(ctx
->dl
);
4489 if (dpipe_match_parse(&match
, nla_match
))
4490 goto err_match_parse
;
4491 pr_out_dpipe_match(&match
, ctx
);
4492 pr_out_entry_end(ctx
->dl
);
4497 pr_out_entry_end(ctx
->dl
);
4501 static struct resource
*
4502 resource_find(struct resources
*resources
, struct resource
*resource
,
4503 uint64_t resource_id
)
4505 struct list_head
*list_head
;
4508 list_head
= &resources
->resource_list
;
4510 list_head
= &resource
->resource_list
;
4512 list_for_each_entry(resource
, list_head
, list
) {
4513 struct resource
*child_resource
;
4515 if (resource
->id
== resource_id
)
4518 child_resource
= resource_find(resources
, resource
,
4521 return child_resource
;
4527 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4528 uint64_t resource_id
)
4530 struct resource
*resource
, *parent_resource
;
4531 const char del
[] = "/";
4535 resource
= resource_find(resources
, NULL
, resource_id
);
4539 for (parent_resource
= resource
; parent_resource
;
4540 parent_resource
= parent_resource
->parent
)
4541 path_len
+= strlen(parent_resource
->name
) + 1;
4544 path
= calloc(1, path_len
);
4548 path
+= path_len
- 1;
4549 for (parent_resource
= resource
; parent_resource
;
4550 parent_resource
= parent_resource
->parent
) {
4551 path
-= strlen(parent_resource
->name
);
4552 memcpy(path
, parent_resource
->name
,
4553 strlen(parent_resource
->name
));
4554 path
-= strlen(del
);
4555 memcpy(path
, del
, strlen(del
));
4557 pr_out_str(dl
, "resource_path", path
);
4561 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4563 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4564 struct dpipe_table
*table
;
4565 uint32_t resource_units
;
4566 bool counters_enabled
;
4567 bool resource_valid
;
4571 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4572 if (err
!= MNL_CB_OK
)
4575 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4576 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4577 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4578 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4579 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4583 table
= dpipe_table_alloc();
4587 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4588 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4589 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4591 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4593 if (resource_valid
) {
4594 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4595 table
->resource_valid
= true;
4598 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4599 if (!ctx
->print_tables
)
4602 pr_out_str(ctx
->dl
, "name", table
->name
);
4603 pr_out_uint(ctx
->dl
, "size", size
);
4604 pr_out_str(ctx
->dl
, "counters_enabled",
4605 counters_enabled
? "true" : "false");
4607 if (resource_valid
) {
4608 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4609 resource_path_print(ctx
->dl
, ctx
->resources
,
4610 table
->resource_id
);
4611 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4614 pr_out_array_start(ctx
->dl
, "match");
4615 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4616 goto err_matches_show
;
4617 pr_out_array_end(ctx
->dl
);
4619 pr_out_array_start(ctx
->dl
, "action");
4620 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4621 goto err_actions_show
;
4622 pr_out_array_end(ctx
->dl
);
4628 pr_out_array_end(ctx
->dl
);
4632 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4634 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4635 struct nlattr
*nla_table
;
4637 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4638 if (ctx
->print_tables
)
4639 pr_out_handle_start_arr(ctx
->dl
, tb
);
4640 if (dpipe_table_show(ctx
, nla_table
))
4641 goto err_table_show
;
4642 if (ctx
->print_tables
)
4643 pr_out_handle_end(ctx
->dl
);
4648 if (ctx
->print_tables
)
4649 pr_out_handle_end(ctx
->dl
);
4653 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4655 struct dpipe_ctx
*ctx
= data
;
4656 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4657 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4659 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4660 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4661 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4662 return MNL_CB_ERROR
;
4664 if (dpipe_tables_show(ctx
, tb
))
4665 return MNL_CB_ERROR
;
4669 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4671 static int cmd_dpipe_table_show(struct dl
*dl
)
4673 struct nlmsghdr
*nlh
;
4674 struct dpipe_ctx dpipe_ctx
= {};
4675 struct resource_ctx resource_ctx
= {};
4676 uint16_t flags
= NLM_F_REQUEST
;
4679 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4683 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4685 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4689 dpipe_ctx
.print_tables
= true;
4691 dl_opts_put(nlh
, dl
);
4692 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4695 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4696 goto err_headers_get
;
4699 err
= resource_ctx_init(&resource_ctx
, dl
);
4701 goto err_resource_ctx_init
;
4703 resource_ctx
.print_resources
= false;
4704 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4705 dl_opts_put(nlh
, dl
);
4706 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4709 dpipe_ctx
.resources
= resource_ctx
.resources
;
4711 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4712 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4713 dl_opts_put(nlh
, dl
);
4715 pr_out_section_start(dl
, "table");
4716 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4717 pr_out_section_end(dl
);
4719 resource_ctx_fini(&resource_ctx
);
4720 dpipe_ctx_fini(&dpipe_ctx
);
4723 err_resource_ctx_init
:
4725 dpipe_ctx_fini(&dpipe_ctx
);
4729 static int cmd_dpipe_table_set(struct dl
*dl
)
4731 struct nlmsghdr
*nlh
;
4734 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4735 NLM_F_REQUEST
| NLM_F_ACK
);
4737 err
= dl_argv_parse_put(nlh
, dl
,
4738 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4739 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4743 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4746 enum dpipe_value_type
{
4747 DPIPE_VALUE_TYPE_VALUE
,
4748 DPIPE_VALUE_TYPE_MASK
,
4752 dpipe_value_type_e2s(enum dpipe_value_type type
)
4755 case DPIPE_VALUE_TYPE_VALUE
:
4757 case DPIPE_VALUE_TYPE_MASK
:
4758 return "value_mask";
4764 struct dpipe_field_printer
{
4765 unsigned int field_id
;
4766 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4769 struct dpipe_header_printer
{
4770 struct dpipe_field_printer
*printers
;
4771 unsigned int printers_count
;
4772 unsigned int header_id
;
4775 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4776 enum dpipe_value_type type
,
4779 struct in_addr ip_addr
;
4781 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4782 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4786 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4787 enum dpipe_value_type type
,
4790 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4791 ether_ntoa((struct ether_addr
*)value
));
4794 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4795 enum dpipe_value_type type
,
4798 char str
[INET6_ADDRSTRLEN
];
4800 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4801 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4804 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4806 .printer
= dpipe_field_printer_ipv4_addr
,
4807 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4811 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4812 .printers
= dpipe_field_printers_ipv4
,
4813 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4814 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4817 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4819 .printer
= dpipe_field_printer_ethernet_addr
,
4820 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4824 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4825 .printers
= dpipe_field_printers_ethernet
,
4826 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4827 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4830 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4832 .printer
= dpipe_field_printer_ipv6_addr
,
4833 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4837 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4838 .printers
= dpipe_field_printers_ipv6
,
4839 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4840 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4843 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4844 &dpipe_header_printer_ipv4
,
4845 &dpipe_header_printer_ethernet
,
4846 &dpipe_header_printer_ipv6
,
4849 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4850 struct dpipe_op_info
*info
,
4851 enum dpipe_value_type type
,
4854 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4855 struct dpipe_header_printer
*header_printer
;
4856 struct dpipe_field_printer
*field_printer
;
4857 unsigned int field_printers_count
;
4861 for (i
= 0; i
< header_printers_count
; i
++) {
4862 header_printer
= dpipe_header_printers
[i
];
4863 if (header_printer
->header_id
!= info
->header_id
)
4865 field_printers_count
= header_printer
->printers_count
;
4866 for (j
= 0; j
< field_printers_count
; j
++) {
4867 field_printer
= &header_printer
->printers
[j
];
4868 if (field_printer
->field_id
!= info
->field_id
)
4870 field_printer
->printer(ctx
, type
, value
);
4878 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4880 unsigned int value_len
,
4881 struct dpipe_op_info
*info
,
4882 enum dpipe_value_type type
)
4884 if (info
->header_global
&&
4885 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4888 if (value_len
== sizeof(uint32_t)) {
4889 uint32_t *value_32
= value
;
4891 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4895 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4896 struct nlattr
**nla_match_value
,
4897 struct dpipe_op_info
*info
)
4899 void *value
, *value_mask
;
4900 uint32_t value_mapping
;
4904 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4905 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4907 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4908 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4911 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4912 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4916 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4917 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4918 DPIPE_VALUE_TYPE_MASK
);
4921 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4924 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4927 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4928 struct dpipe_match match
;
4931 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4932 if (err
!= MNL_CB_OK
)
4935 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4936 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4940 pr_out_entry_start(ctx
->dl
);
4941 if (dpipe_match_parse(&match
,
4942 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4943 goto err_match_parse
;
4944 pr_out_dpipe_match(&match
, ctx
);
4945 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4946 pr_out_entry_end(ctx
->dl
);
4951 pr_out_entry_end(ctx
->dl
);
4955 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4958 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4959 struct dpipe_action action
;
4962 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4963 if (err
!= MNL_CB_OK
)
4966 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4967 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4971 pr_out_entry_start(ctx
->dl
);
4972 if (dpipe_action_parse(&action
,
4973 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4974 goto err_action_parse
;
4975 pr_out_dpipe_action(&action
, ctx
);
4976 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4977 pr_out_entry_end(ctx
->dl
);
4982 pr_out_entry_end(ctx
->dl
);
4987 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4988 struct nlattr
*nla_action_values
)
4990 struct nlattr
*nla_action_value
;
4992 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4993 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5000 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5001 struct nlattr
*nla_match_values
)
5003 struct nlattr
*nla_match_value
;
5005 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5006 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5012 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5014 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5015 uint32_t entry_index
;
5019 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5020 if (err
!= MNL_CB_OK
)
5023 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5024 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5025 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5029 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5030 pr_out_uint(ctx
->dl
, "index", entry_index
);
5032 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5033 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5034 pr_out_uint(ctx
->dl
, "counter", counter
);
5037 pr_out_array_start(ctx
->dl
, "match_value");
5038 if (dpipe_tables_match_values_show(ctx
,
5039 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5040 goto err_match_values_show
;
5041 pr_out_array_end(ctx
->dl
);
5043 pr_out_array_start(ctx
->dl
, "action_value");
5044 if (dpipe_tables_action_values_show(ctx
,
5045 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5046 goto err_action_values_show
;
5047 pr_out_array_end(ctx
->dl
);
5050 err_action_values_show
:
5051 err_match_values_show
:
5052 pr_out_array_end(ctx
->dl
);
5056 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5058 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5059 struct nlattr
*nla_entry
;
5061 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5062 pr_out_handle_start_arr(ctx
->dl
, tb
);
5063 if (dpipe_entry_show(ctx
, nla_entry
))
5064 goto err_entry_show
;
5065 pr_out_handle_end(ctx
->dl
);
5070 pr_out_handle_end(ctx
->dl
);
5074 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5076 struct dpipe_ctx
*ctx
= data
;
5077 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5078 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5080 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5081 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5082 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5083 return MNL_CB_ERROR
;
5085 if (dpipe_table_entries_show(ctx
, tb
))
5086 return MNL_CB_ERROR
;
5090 static int cmd_dpipe_table_dump(struct dl
*dl
)
5092 struct nlmsghdr
*nlh
;
5093 struct dpipe_ctx ctx
= {};
5094 uint16_t flags
= NLM_F_REQUEST
;
5097 err
= dpipe_ctx_init(&ctx
, dl
);
5101 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5105 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5106 dl_opts_put(nlh
, dl
);
5107 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5109 pr_err("error get headers %s\n", strerror(ctx
.err
));
5113 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5114 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5115 dl_opts_put(nlh
, dl
);
5117 pr_out_section_start(dl
, "table_entry");
5118 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5119 pr_out_section_end(dl
);
5121 dpipe_ctx_fini(&ctx
);
5125 static void cmd_dpipe_table_help(void)
5127 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5128 "where OBJECT-LIST := { show | set | dump }\n");
5131 static int cmd_dpipe_table(struct dl
*dl
)
5133 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5134 cmd_dpipe_table_help();
5136 } else if (dl_argv_match(dl
, "show")) {
5138 return cmd_dpipe_table_show(dl
);
5139 } else if (dl_argv_match(dl
, "set")) {
5141 return cmd_dpipe_table_set(dl
);
5142 } else if (dl_argv_match(dl
, "dump")) {
5144 return cmd_dpipe_table_dump(dl
);
5146 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5150 static void cmd_dpipe_help(void)
5152 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5153 "where OBJECT-LIST := { header | table }\n");
5156 static int cmd_dpipe(struct dl
*dl
)
5158 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5161 } else if (dl_argv_match(dl
, "header")) {
5163 return cmd_dpipe_header(dl
);
5164 } else if (dl_argv_match(dl
, "table")) {
5166 return cmd_dpipe_table(dl
);
5168 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5173 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5174 struct nlattr
**nla_resource
)
5176 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5177 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5178 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5179 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5180 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5181 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5182 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5186 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5187 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5188 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5189 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5190 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5191 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5192 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5194 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5195 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5197 resource
->size_new
= resource
->size
;
5199 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5200 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5201 resource
->occ_valid
= true;
5204 if (resource
->size_new
!= resource
->size
)
5205 ctx
->pending_change
= true;
5211 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5212 struct resource
*parent_resource
, struct nlattr
*nl
)
5214 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5215 struct nlattr
*nla_child_resource
;
5216 struct nlattr
*nla_resources
;
5226 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5227 if (err
!= MNL_CB_OK
)
5230 err
= resource_parse(ctx
, resource
, nla_resource
);
5234 resource
->parent
= parent_resource
;
5235 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5238 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5239 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5241 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5242 struct resource
*child_resource
;
5243 struct list_head
*list
;
5245 child_resource
= resource_alloc();
5246 if (!child_resource
)
5250 list
= &ctx
->resources
->resource_list
;
5252 list
= &resource
->resource_list
;
5254 list_add_tail(&child_resource
->list
, list
);
5255 err
= resource_get(ctx
, child_resource
, resource
,
5256 nla_child_resource
);
5264 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5267 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5268 default: return "<unknown unit>";
5272 static void resource_show(struct resource
*resource
,
5273 struct resource_ctx
*ctx
)
5275 struct resource
*child_resource
;
5276 struct dpipe_table
*table
;
5277 struct dl
*dl
= ctx
->dl
;
5280 pr_out_str(dl
, "name", resource
->name
);
5282 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5283 pr_out_u64(dl
, "size", resource
->size
);
5284 if (resource
->size
!= resource
->size_new
)
5285 pr_out_u64(dl
, "size_new", resource
->size_new
);
5286 if (resource
->occ_valid
)
5287 pr_out_uint(dl
, "occ", resource
->size_occ
);
5288 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5290 if (resource
->size_min
!= resource
->size_max
) {
5291 pr_out_uint(dl
, "size_min", resource
->size_min
);
5292 pr_out_u64(dl
, "size_max", resource
->size_max
);
5293 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5296 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5297 if (table
->resource_id
== resource
->id
&&
5298 table
->resource_valid
)
5302 pr_out_array_start(dl
, "dpipe_tables");
5304 pr_out_str(dl
, "dpipe_tables", "none");
5306 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5307 if (table
->resource_id
!= resource
->id
||
5308 !table
->resource_valid
)
5310 pr_out_entry_start(dl
);
5311 pr_out_str(dl
, "table_name", table
->name
);
5312 pr_out_entry_end(dl
);
5315 pr_out_array_end(dl
);
5317 if (list_empty(&resource
->resource_list
))
5320 if (ctx
->pending_change
)
5321 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5323 pr_out_array_start(dl
, "resources");
5324 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5325 pr_out_entry_start(dl
);
5326 resource_show(child_resource
, ctx
);
5327 pr_out_entry_end(dl
);
5329 pr_out_array_end(dl
);
5333 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5335 struct resources
*resources
= ctx
->resources
;
5336 struct resource
*resource
;
5338 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5339 pr_out_handle_start_arr(ctx
->dl
, tb
);
5340 resource_show(resource
, ctx
);
5341 pr_out_handle_end(ctx
->dl
);
5345 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5347 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5350 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5352 struct resource_ctx
*ctx
= data
;
5353 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5354 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5357 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5358 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5359 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5360 return MNL_CB_ERROR
;
5362 err
= resources_get(ctx
, tb
);
5365 return MNL_CB_ERROR
;
5368 if (ctx
->print_resources
)
5369 resources_show(ctx
, tb
);
5374 static int cmd_resource_show(struct dl
*dl
)
5376 struct nlmsghdr
*nlh
;
5377 struct dpipe_ctx dpipe_ctx
= {};
5378 struct resource_ctx resource_ctx
= {};
5381 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5385 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5387 dl_opts_put(nlh
, dl
);
5389 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5393 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5396 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5400 err
= resource_ctx_init(&resource_ctx
, dl
);
5404 resource_ctx
.print_resources
= true;
5405 resource_ctx
.tables
= dpipe_ctx
.tables
;
5406 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5407 NLM_F_REQUEST
| NLM_F_ACK
);
5408 dl_opts_put(nlh
, dl
);
5409 pr_out_section_start(dl
, "resources");
5410 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5412 pr_out_section_end(dl
);
5413 resource_ctx_fini(&resource_ctx
);
5415 dpipe_ctx_fini(&dpipe_ctx
);
5419 static void cmd_resource_help(void)
5421 pr_err("Usage: devlink resource show DEV\n"
5422 " devlink resource set DEV path PATH size SIZE\n");
5425 static struct resource
*
5426 resource_find_by_name(struct list_head
*list
, char *name
)
5428 struct resource
*resource
;
5430 list_for_each_entry(resource
, list
, list
) {
5431 if (!strcmp(resource
->name
, name
))
5438 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5439 uint32_t *p_resource_id
, bool *p_resource_valid
)
5441 struct resource
*resource
;
5442 uint32_t resource_id
= 0;
5443 char *resource_path_dup
;
5444 struct list_head
*list
;
5445 const char del
[] = "/";
5446 char *resource_name
;
5448 resource_path_dup
= strdup(resource_path
);
5449 list
= &ctx
->resources
->resource_list
;
5450 resource_name
= strtok(resource_path_dup
, del
);
5451 while (resource_name
!= NULL
) {
5452 resource
= resource_find_by_name(list
, resource_name
);
5454 goto err_resource_lookup
;
5456 list
= &resource
->resource_list
;
5457 resource_name
= strtok(NULL
, del
);
5458 resource_id
= resource
->id
;
5460 free(resource_path_dup
);
5461 *p_resource_valid
= true;
5462 *p_resource_id
= resource_id
;
5465 err_resource_lookup
:
5466 free(resource_path_dup
);
5470 static int cmd_resource_set(struct dl
*dl
)
5472 struct nlmsghdr
*nlh
;
5473 struct resource_ctx ctx
= {};
5476 err
= resource_ctx_init(&ctx
, dl
);
5480 ctx
.print_resources
= false;
5481 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5482 DL_OPT_RESOURCE_SIZE
, 0);
5486 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5488 dl_opts_put(nlh
, dl
);
5489 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5491 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5495 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5496 &dl
->opts
.resource_id
,
5497 &dl
->opts
.resource_id_valid
);
5499 pr_err("error parsing resource path %s\n", strerror(-err
));
5503 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5504 NLM_F_REQUEST
| NLM_F_ACK
);
5506 dl_opts_put(nlh
, dl
);
5507 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5509 resource_ctx_fini(&ctx
);
5513 static int cmd_resource(struct dl
*dl
)
5515 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5516 cmd_resource_help();
5518 } else if (dl_argv_match(dl
, "show")) {
5520 return cmd_resource_show(dl
);
5521 } else if (dl_argv_match(dl
, "set")) {
5523 return cmd_resource_set(dl
);
5525 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5529 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5531 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5532 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5533 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5536 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5537 if (dl
->json_output
) {
5538 jsonw_name(dl
->jw
, buf
);
5539 jsonw_start_object(dl
->jw
);
5545 static void pr_out_region_handle_end(struct dl
*dl
)
5547 if (dl
->json_output
)
5548 jsonw_end_object(dl
->jw
);
5553 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5555 if (dl
->json_output
) {
5556 jsonw_name(dl
->jw
, "snapshot");
5557 jsonw_start_array(dl
->jw
);
5559 if (g_indent_newline
)
5560 pr_out("snapshot %s", array
? "[" : "");
5562 pr_out(" snapshot %s", array
? "[" : "");
5566 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5568 if (dl
->json_output
)
5569 jsonw_end_array(dl
->jw
);
5574 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5576 uint32_t snapshot_id
;
5578 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5581 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5583 if (dl
->json_output
)
5584 jsonw_uint(dl
->jw
, snapshot_id
);
5586 pr_out("%s%u", index
? " " : "", snapshot_id
);
5589 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5591 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5592 struct nlattr
*nla_sanpshot
;
5595 pr_out_region_snapshots_start(dl
, true);
5596 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5597 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5598 if (err
!= MNL_CB_OK
)
5600 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5602 pr_out_region_snapshots_end(dl
, true);
5605 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5607 pr_out_region_snapshots_start(dl
, false);
5608 pr_out_region_snapshots_id(dl
, tb
, 0);
5609 pr_out_region_snapshots_end(dl
, false);
5612 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5614 pr_out_region_handle_start(dl
, tb
);
5616 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5617 pr_out_u64(dl
, "size",
5618 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5620 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5621 pr_out_snapshots(dl
, tb
);
5623 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5624 pr_out_snapshot(dl
, tb
);
5626 pr_out_region_handle_end(dl
);
5629 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5631 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5632 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5633 struct dl
*dl
= data
;
5635 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5636 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5637 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5638 return MNL_CB_ERROR
;
5640 pr_out_region(dl
, tb
);
5645 static int cmd_region_show(struct dl
*dl
)
5647 struct nlmsghdr
*nlh
;
5648 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5651 if (dl_argc(dl
) == 0)
5652 flags
|= NLM_F_DUMP
;
5654 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5656 if (dl_argc(dl
) > 0) {
5657 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5662 pr_out_section_start(dl
, "regions");
5663 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5664 pr_out_section_end(dl
);
5668 static int cmd_region_snapshot_del(struct dl
*dl
)
5670 struct nlmsghdr
*nlh
;
5673 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5674 NLM_F_REQUEST
| NLM_F_ACK
);
5676 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5677 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5681 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5684 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5686 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5687 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5688 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5689 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5690 struct dl
*dl
= data
;
5693 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5694 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5695 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5696 return MNL_CB_ERROR
;
5698 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5699 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5700 if (err
!= MNL_CB_OK
)
5701 return MNL_CB_ERROR
;
5703 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5704 if (!nla_chunk_data
)
5707 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5708 if (!nla_chunk_addr
)
5711 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5712 mnl_attr_get_payload_len(nla_chunk_data
),
5713 mnl_attr_get_u64(nla_chunk_addr
));
5718 static int cmd_region_dump(struct dl
*dl
)
5720 struct nlmsghdr
*nlh
;
5723 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5724 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5726 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5727 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5731 pr_out_section_start(dl
, "dump");
5732 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5733 pr_out_section_end(dl
);
5734 if (!dl
->json_output
)
5739 static int cmd_region_read(struct dl
*dl
)
5741 struct nlmsghdr
*nlh
;
5744 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5745 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5747 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5748 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5749 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5753 pr_out_section_start(dl
, "read");
5754 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5755 pr_out_section_end(dl
);
5756 if (!dl
->json_output
)
5761 static void cmd_region_help(void)
5763 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5764 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5765 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5766 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5769 static int cmd_region(struct dl
*dl
)
5771 if (dl_no_arg(dl
)) {
5772 return cmd_region_show(dl
);
5773 } else if (dl_argv_match(dl
, "help")) {
5776 } else if (dl_argv_match(dl
, "show")) {
5778 return cmd_region_show(dl
);
5779 } else if (dl_argv_match(dl
, "del")) {
5781 return cmd_region_snapshot_del(dl
);
5782 } else if (dl_argv_match(dl
, "dump")) {
5784 return cmd_region_dump(dl
);
5785 } else if (dl_argv_match(dl
, "read")) {
5787 return cmd_region_read(dl
);
5789 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5793 static void help(void)
5795 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5796 " devlink [ -f[orce] ] -b[atch] filename\n"
5797 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5798 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5801 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5806 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5809 } else if (dl_argv_match(dl
, "dev")) {
5812 } else if (dl_argv_match(dl
, "port")) {
5814 return cmd_port(dl
);
5815 } else if (dl_argv_match(dl
, "sb")) {
5818 } else if (dl_argv_match(dl
, "monitor")) {
5821 } else if (dl_argv_match(dl
, "dpipe")) {
5823 return cmd_dpipe(dl
);
5824 } else if (dl_argv_match(dl
, "resource")) {
5826 return cmd_resource(dl
);
5827 } else if (dl_argv_match(dl
, "region")) {
5829 return cmd_region(dl
);
5831 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5835 static int dl_init(struct dl
*dl
)
5839 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5841 pr_err("Failed to connect to devlink Netlink\n");
5845 err
= ifname_map_init(dl
);
5847 pr_err("Failed to create index map\n");
5848 goto err_ifname_map_create
;
5850 if (dl
->json_output
) {
5851 dl
->jw
= jsonw_new(stdout
);
5853 pr_err("Failed to create JSON writer\n");
5856 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5861 ifname_map_fini(dl
);
5862 err_ifname_map_create
:
5863 mnlg_socket_close(dl
->nlg
);
5867 static void dl_fini(struct dl
*dl
)
5869 if (dl
->json_output
)
5870 jsonw_destroy(&dl
->jw
);
5871 ifname_map_fini(dl
);
5872 mnlg_socket_close(dl
->nlg
);
5875 static struct dl
*dl_alloc(void)
5879 dl
= calloc(1, sizeof(*dl
));
5885 static void dl_free(struct dl
*dl
)
5890 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5894 int ret
= EXIT_SUCCESS
;
5896 if (name
&& strcmp(name
, "-") != 0) {
5897 if (freopen(name
, "r", stdin
) == NULL
) {
5899 "Cannot open file \"%s\" for reading: %s\n",
5900 name
, strerror(errno
));
5901 return EXIT_FAILURE
;
5906 while (getcmdline(&line
, &len
, stdin
) != -1) {
5910 largc
= makeargs(line
, largv
, 100);
5912 continue; /* blank line */
5914 if (dl_cmd(dl
, largc
, largv
)) {
5915 fprintf(stderr
, "Command failed %s:%d\n",
5929 int main(int argc
, char **argv
)
5931 static const struct option long_options
[] = {
5932 { "Version", no_argument
, NULL
, 'V' },
5933 { "force", no_argument
, NULL
, 'f' },
5934 { "batch", required_argument
, NULL
, 'b' },
5935 { "no-nice-names", no_argument
, NULL
, 'n' },
5936 { "json", no_argument
, NULL
, 'j' },
5937 { "pretty", no_argument
, NULL
, 'p' },
5938 { "verbose", no_argument
, NULL
, 'v' },
5939 { NULL
, 0, NULL
, 0 }
5941 const char *batch_file
= NULL
;
5950 pr_err("Failed to allocate memory for devlink\n");
5951 return EXIT_FAILURE
;
5954 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5955 long_options
, NULL
)) >= 0) {
5959 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5966 batch_file
= optarg
;
5969 dl
->no_nice_names
= true;
5972 dl
->json_output
= true;
5975 dl
->pretty_output
= true;
5981 pr_err("Unknown option.\n");
5998 err
= dl_batch(dl
, batch_file
, force
);
6000 err
= dl_cmd(dl
, argc
, argv
);