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"
43 static int g_new_line_count
;
45 #define pr_err(args...) fprintf(stderr, ##args)
46 #define pr_out(args...) \
48 if (g_indent_newline) { \
49 fprintf(stdout, "%s", g_indent_str); \
50 g_indent_newline = false; \
52 fprintf(stdout, ##args); \
53 g_new_line_count = 0; \
56 #define pr_out_sp(num, args...) \
58 int ret = fprintf(stdout, ##args); \
60 fprintf(stdout, "%*s", num - ret, ""); \
61 g_new_line_count = 0; \
64 static int g_indent_level
;
65 static bool g_indent_newline
;
66 #define INDENT_STR_STEP 2
67 #define INDENT_STR_MAXLEN 32
68 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
70 static void __pr_out_indent_inc(void)
72 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
74 g_indent_level
+= INDENT_STR_STEP
;
75 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
76 g_indent_str
[g_indent_level
] = '\0';
79 static void __pr_out_indent_dec(void)
81 if (g_indent_level
- INDENT_STR_STEP
< 0)
83 g_indent_level
-= INDENT_STR_STEP
;
84 g_indent_str
[g_indent_level
] = '\0';
87 static void __pr_out_newline(void)
89 if (g_new_line_count
< 1) {
91 g_indent_newline
= true;
96 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
97 mnl_cb_t data_cb
, void *data
)
101 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
103 pr_err("devlink answers: %s\n", strerror(errno
));
109 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
110 const struct nlmsghdr
*nlh
,
111 mnl_cb_t data_cb
, void *data
)
115 err
= mnlg_socket_send(nlg
, nlh
);
117 pr_err("Failed to call mnlg_socket_send\n");
120 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
123 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
124 const char *group_name
)
128 err
= mnlg_socket_group_add(nlg
, group_name
);
130 pr_err("Failed to call mnlg_socket_group_add\n");
137 struct list_head list
;
144 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
145 const char *dev_name
,
149 struct ifname_map
*ifname_map
;
151 ifname_map
= calloc(1, sizeof(*ifname_map
));
154 ifname_map
->bus_name
= strdup(bus_name
);
155 ifname_map
->dev_name
= strdup(dev_name
);
156 ifname_map
->port_index
= port_index
;
157 ifname_map
->ifname
= strdup(ifname
);
158 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
159 !ifname_map
->ifname
) {
160 free(ifname_map
->ifname
);
161 free(ifname_map
->dev_name
);
162 free(ifname_map
->bus_name
);
169 static void ifname_map_free(struct ifname_map
*ifname_map
)
171 free(ifname_map
->ifname
);
172 free(ifname_map
->dev_name
);
173 free(ifname_map
->bus_name
);
177 #define DL_OPT_HANDLE BIT(0)
178 #define DL_OPT_HANDLEP BIT(1)
179 #define DL_OPT_PORT_TYPE BIT(2)
180 #define DL_OPT_PORT_COUNT BIT(3)
181 #define DL_OPT_SB BIT(4)
182 #define DL_OPT_SB_POOL BIT(5)
183 #define DL_OPT_SB_SIZE BIT(6)
184 #define DL_OPT_SB_TYPE BIT(7)
185 #define DL_OPT_SB_THTYPE BIT(8)
186 #define DL_OPT_SB_TH BIT(9)
187 #define DL_OPT_SB_TC BIT(10)
188 #define DL_OPT_ESWITCH_MODE BIT(11)
189 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
190 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
191 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
192 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
193 #define DL_OPT_RESOURCE_PATH BIT(16)
194 #define DL_OPT_RESOURCE_SIZE BIT(17)
195 #define DL_OPT_PARAM_NAME BIT(18)
196 #define DL_OPT_PARAM_VALUE BIT(19)
197 #define DL_OPT_PARAM_CMODE BIT(20)
198 #define DL_OPT_HANDLE_REGION BIT(21)
199 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
200 #define DL_OPT_REGION_ADDRESS BIT(23)
201 #define DL_OPT_REGION_LENGTH BIT(24)
202 #define DL_OPT_FLASH_FILE_NAME BIT(25)
203 #define DL_OPT_FLASH_COMPONENT BIT(26)
206 uint32_t present
; /* flags of present items */
210 enum devlink_port_type port_type
;
213 uint16_t sb_pool_index
;
214 uint32_t sb_pool_size
;
215 enum devlink_sb_pool_type sb_pool_type
;
216 enum devlink_sb_threshold_type sb_pool_thtype
;
217 uint32_t sb_threshold
;
218 uint16_t sb_tc_index
;
219 enum devlink_eswitch_mode eswitch_mode
;
220 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
221 const char *dpipe_table_name
;
222 bool dpipe_counters_enable
;
223 bool eswitch_encap_mode
;
224 const char *resource_path
;
225 uint32_t resource_size
;
226 uint32_t resource_id
;
227 bool resource_id_valid
;
228 const char *param_name
;
229 const char *param_value
;
230 enum devlink_param_cmode cmode
;
232 uint32_t region_snapshot_id
;
233 uint64_t region_address
;
234 uint64_t region_length
;
235 const char *flash_file_name
;
236 const char *flash_component
;
240 struct mnlg_socket
*nlg
;
241 struct list_head ifname_map_list
;
258 static int dl_argc(struct dl
*dl
)
263 static char *dl_argv(struct dl
*dl
)
265 if (dl_argc(dl
) == 0)
270 static void dl_arg_inc(struct dl
*dl
)
272 if (dl_argc(dl
) == 0)
278 static char *dl_argv_next(struct dl
*dl
)
282 if (dl_argc(dl
) == 0)
290 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
292 if (index
>= dl_argc(dl
))
294 return dl
->argv
[index
];
297 static int strcmpx(const char *str1
, const char *str2
)
299 if (strlen(str1
) > strlen(str2
))
301 return strncmp(str1
, str2
, strlen(str1
));
304 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
306 if (dl_argc(dl
) == 0)
308 return strcmpx(dl_argv(dl
), pattern
) == 0;
311 static bool dl_no_arg(struct dl
*dl
)
313 return dl_argc(dl
) == 0;
316 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
317 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
318 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
319 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
320 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
321 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
322 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
323 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
324 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
325 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
326 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
327 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
328 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
329 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
330 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
331 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
332 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
333 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
334 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
335 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
336 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
337 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
338 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
339 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
340 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
341 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
342 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
343 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
344 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
345 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
346 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
347 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
348 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
349 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
350 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
351 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
352 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
353 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
354 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
355 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
356 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
357 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
358 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
359 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
360 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
361 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
362 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
363 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
364 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
365 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
366 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
367 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
368 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
369 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
370 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
371 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
372 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
373 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
374 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
375 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
376 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
377 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
378 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
379 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
380 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
381 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
382 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
383 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
384 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
385 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
386 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
387 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
388 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
389 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
390 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
391 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
392 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
393 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
394 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
395 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
396 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
399 static int attr_cb(const struct nlattr
*attr
, void *data
)
401 const struct nlattr
**tb
= data
;
404 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
407 type
= mnl_attr_get_type(attr
);
408 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
415 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
417 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
418 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
419 struct dl
*dl
= data
;
420 struct ifname_map
*ifname_map
;
421 const char *bus_name
;
422 const char *dev_name
;
423 uint32_t port_ifindex
;
424 const char *port_ifname
;
426 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
427 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
428 !tb
[DEVLINK_ATTR_PORT_INDEX
])
431 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
434 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
435 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
436 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
437 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
438 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
439 port_ifindex
, port_ifname
);
442 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
447 static void ifname_map_fini(struct dl
*dl
)
449 struct ifname_map
*ifname_map
, *tmp
;
451 list_for_each_entry_safe(ifname_map
, tmp
,
452 &dl
->ifname_map_list
, list
) {
453 list_del(&ifname_map
->list
);
454 ifname_map_free(ifname_map
);
458 static int ifname_map_init(struct dl
*dl
)
460 struct nlmsghdr
*nlh
;
463 INIT_LIST_HEAD(&dl
->ifname_map_list
);
465 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
466 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
468 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
476 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
477 char **p_bus_name
, char **p_dev_name
,
478 uint32_t *p_port_index
)
480 struct ifname_map
*ifname_map
;
482 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
483 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
484 *p_bus_name
= ifname_map
->bus_name
;
485 *p_dev_name
= ifname_map
->dev_name
;
486 *p_port_index
= ifname_map
->port_index
;
493 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
494 const char *dev_name
, uint32_t port_index
,
497 struct ifname_map
*ifname_map
;
499 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
500 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
501 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
502 port_index
== ifname_map
->port_index
) {
503 *p_ifname
= ifname_map
->ifname
;
510 static unsigned int strslashcount(char *str
)
512 unsigned int count
= 0;
515 while ((pos
= strchr(pos
, '/'))) {
522 static int strslashrsplit(char *str
, char **before
, char **after
)
526 slash
= strrchr(str
, '/');
535 static int strtouint64_t(const char *str
, uint64_t *p_val
)
538 unsigned long long int val
;
540 val
= strtoull(str
, &endptr
, 10);
541 if (endptr
== str
|| *endptr
!= '\0')
549 static int strtouint32_t(const char *str
, uint32_t *p_val
)
552 unsigned long int val
;
554 val
= strtoul(str
, &endptr
, 10);
555 if (endptr
== str
|| *endptr
!= '\0')
563 static int strtouint16_t(const char *str
, uint16_t *p_val
)
566 unsigned long int val
;
568 val
= strtoul(str
, &endptr
, 10);
569 if (endptr
== str
|| *endptr
!= '\0')
577 static int strtouint8_t(const char *str
, uint8_t *p_val
)
580 unsigned long int val
;
582 val
= strtoul(str
, &endptr
, 10);
583 if (endptr
== str
|| *endptr
!= '\0')
591 static int strtobool(const char *str
, bool *p_val
)
595 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
597 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
605 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
607 strslashrsplit(str
, p_bus_name
, p_dev_name
);
611 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
613 char *str
= dl_argv_next(dl
);
616 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
619 if (strslashcount(str
) != 1) {
620 pr_err("Wrong devlink identification string format.\n");
621 pr_err("Expected \"bus_name/dev_name\".\n");
624 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
627 static int __dl_argv_handle_port(char *str
,
628 char **p_bus_name
, char **p_dev_name
,
629 uint32_t *p_port_index
)
635 err
= strslashrsplit(str
, &handlestr
, &portstr
);
637 pr_err("Port identification \"%s\" is invalid\n", str
);
640 err
= strtouint32_t(portstr
, p_port_index
);
642 pr_err("Port index \"%s\" is not a number or not within range\n",
646 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
648 pr_err("Port identification \"%s\" is invalid\n", str
);
654 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
655 char **p_bus_name
, char **p_dev_name
,
656 uint32_t *p_port_index
)
660 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
663 pr_err("Netdevice \"%s\" not found\n", str
);
669 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
670 char **p_dev_name
, uint32_t *p_port_index
)
672 char *str
= dl_argv_next(dl
);
673 unsigned int slash_count
;
676 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
679 slash_count
= strslashcount(str
);
680 switch (slash_count
) {
682 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
683 p_dev_name
, p_port_index
);
685 return __dl_argv_handle_port(str
, p_bus_name
,
686 p_dev_name
, p_port_index
);
688 pr_err("Wrong port identification string format.\n");
689 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
694 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
695 char **p_dev_name
, uint32_t *p_port_index
,
696 uint32_t *p_handle_bit
)
698 char *str
= dl_argv_next(dl
);
699 unsigned int slash_count
;
703 pr_err("One of following identifications expected:\n"
704 "Devlink identification (\"bus_name/dev_name\")\n"
705 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
708 slash_count
= strslashcount(str
);
709 if (slash_count
== 1) {
710 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
713 *p_handle_bit
= DL_OPT_HANDLE
;
714 } else if (slash_count
== 2) {
715 err
= __dl_argv_handle_port(str
, p_bus_name
,
716 p_dev_name
, p_port_index
);
719 *p_handle_bit
= DL_OPT_HANDLEP
;
720 } else if (slash_count
== 0) {
721 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
722 p_dev_name
, p_port_index
);
725 *p_handle_bit
= DL_OPT_HANDLEP
;
727 pr_err("Wrong port identification string format.\n");
728 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
734 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
735 char **p_dev_name
, char **p_region
)
740 err
= strslashrsplit(str
, &handlestr
, p_region
);
742 pr_err("Region identification \"%s\" is invalid\n", str
);
745 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
747 pr_err("Region identification \"%s\" is invalid\n", str
);
753 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
754 char **p_dev_name
, char **p_region
)
756 char *str
= dl_argv_next(dl
);
757 unsigned int slash_count
;
760 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
764 slash_count
= strslashcount(str
);
765 if (slash_count
!= 2) {
766 pr_err("Wrong region identification string format.\n");
767 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
771 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
774 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
776 char *str
= dl_argv_next(dl
);
780 pr_err("Unsigned number argument expected\n");
784 err
= strtouint64_t(str
, p_val
);
786 pr_err("\"%s\" is not a number or not within range\n", str
);
792 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
794 char *str
= dl_argv_next(dl
);
798 pr_err("Unsigned number argument expected\n");
802 err
= strtouint32_t(str
, p_val
);
804 pr_err("\"%s\" is not a number or not within range\n", str
);
810 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
812 char *str
= dl_argv_next(dl
);
816 pr_err("Unsigned number argument expected\n");
820 err
= strtouint16_t(str
, p_val
);
822 pr_err("\"%s\" is not a number or not within range\n", str
);
828 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
830 const char *str
= dl_argv_next(dl
);
833 pr_err("String parameter expected\n");
840 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
842 if (strcmp(typestr
, "auto") == 0) {
843 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
844 } else if (strcmp(typestr
, "eth") == 0) {
845 *p_type
= DEVLINK_PORT_TYPE_ETH
;
846 } else if (strcmp(typestr
, "ib") == 0) {
847 *p_type
= DEVLINK_PORT_TYPE_IB
;
849 pr_err("Unknown port type \"%s\"\n", typestr
);
855 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
857 if (strcmp(typestr
, "ingress") == 0) {
858 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
859 } else if (strcmp(typestr
, "egress") == 0) {
860 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
862 pr_err("Unknown pool type \"%s\"\n", typestr
);
868 static int threshold_type_get(const char *typestr
,
869 enum devlink_sb_threshold_type
*p_type
)
871 if (strcmp(typestr
, "static") == 0) {
872 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
873 } else if (strcmp(typestr
, "dynamic") == 0) {
874 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
876 pr_err("Unknown threshold type \"%s\"\n", typestr
);
882 static int eswitch_mode_get(const char *typestr
,
883 enum devlink_eswitch_mode
*p_mode
)
885 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
886 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
887 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
888 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
890 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
896 static int eswitch_inline_mode_get(const char *typestr
,
897 enum devlink_eswitch_inline_mode
*p_mode
)
899 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
900 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
901 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
902 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
903 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
904 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
905 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
906 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
908 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
914 static int dpipe_counters_enable_get(const char *typestr
,
915 bool *counters_enable
)
917 if (strcmp(typestr
, "enable") == 0) {
918 *counters_enable
= 1;
919 } else if (strcmp(typestr
, "disable") == 0) {
920 *counters_enable
= 0;
922 pr_err("Unknown counter_state \"%s\"\n", typestr
);
928 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
930 if (strcmp(typestr
, "enable") == 0) {
932 } else if (strcmp(typestr
, "disable") == 0) {
935 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
941 static int param_cmode_get(const char *cmodestr
,
942 enum devlink_param_cmode
*cmode
)
944 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
945 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
946 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
947 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
948 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
949 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
951 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
957 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
960 struct dl_opts
*opts
= &dl
->opts
;
961 uint32_t o_all
= o_required
| o_optional
;
962 uint32_t o_found
= 0;
965 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
968 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
969 &opts
->port_index
, &handle_bit
);
972 o_found
|= handle_bit
;
973 } else if (o_required
& DL_OPT_HANDLE
) {
974 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
977 o_found
|= DL_OPT_HANDLE
;
978 } else if (o_required
& DL_OPT_HANDLEP
) {
979 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
983 o_found
|= DL_OPT_HANDLEP
;
984 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
985 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
990 o_found
|= DL_OPT_HANDLE_REGION
;
993 while (dl_argc(dl
)) {
994 if (dl_argv_match(dl
, "type") &&
995 (o_all
& DL_OPT_PORT_TYPE
)) {
999 err
= dl_argv_str(dl
, &typestr
);
1002 err
= port_type_get(typestr
, &opts
->port_type
);
1005 o_found
|= DL_OPT_PORT_TYPE
;
1006 } else if (dl_argv_match(dl
, "count") &&
1007 (o_all
& DL_OPT_PORT_COUNT
)) {
1009 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1012 o_found
|= DL_OPT_PORT_COUNT
;
1013 } else if (dl_argv_match(dl
, "sb") &&
1014 (o_all
& DL_OPT_SB
)) {
1016 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1019 o_found
|= DL_OPT_SB
;
1020 } else if (dl_argv_match(dl
, "pool") &&
1021 (o_all
& DL_OPT_SB_POOL
)) {
1023 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1026 o_found
|= DL_OPT_SB_POOL
;
1027 } else if (dl_argv_match(dl
, "size") &&
1028 (o_all
& DL_OPT_SB_SIZE
)) {
1030 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1033 o_found
|= DL_OPT_SB_SIZE
;
1034 } else if (dl_argv_match(dl
, "type") &&
1035 (o_all
& DL_OPT_SB_TYPE
)) {
1036 const char *typestr
;
1039 err
= dl_argv_str(dl
, &typestr
);
1042 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1045 o_found
|= DL_OPT_SB_TYPE
;
1046 } else if (dl_argv_match(dl
, "thtype") &&
1047 (o_all
& DL_OPT_SB_THTYPE
)) {
1048 const char *typestr
;
1051 err
= dl_argv_str(dl
, &typestr
);
1054 err
= threshold_type_get(typestr
,
1055 &opts
->sb_pool_thtype
);
1058 o_found
|= DL_OPT_SB_THTYPE
;
1059 } else if (dl_argv_match(dl
, "th") &&
1060 (o_all
& DL_OPT_SB_TH
)) {
1062 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1065 o_found
|= DL_OPT_SB_TH
;
1066 } else if (dl_argv_match(dl
, "tc") &&
1067 (o_all
& DL_OPT_SB_TC
)) {
1069 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1072 o_found
|= DL_OPT_SB_TC
;
1073 } else if (dl_argv_match(dl
, "mode") &&
1074 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1075 const char *typestr
;
1078 err
= dl_argv_str(dl
, &typestr
);
1081 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1084 o_found
|= DL_OPT_ESWITCH_MODE
;
1085 } else if (dl_argv_match(dl
, "inline-mode") &&
1086 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1087 const char *typestr
;
1090 err
= dl_argv_str(dl
, &typestr
);
1093 err
= eswitch_inline_mode_get(
1094 typestr
, &opts
->eswitch_inline_mode
);
1097 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1098 } else if (dl_argv_match(dl
, "name") &&
1099 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1101 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1104 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1105 } else if (dl_argv_match(dl
, "counters") &&
1106 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1107 const char *typestr
;
1110 err
= dl_argv_str(dl
, &typestr
);
1113 err
= dpipe_counters_enable_get(typestr
,
1114 &opts
->dpipe_counters_enable
);
1117 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1118 } else if (dl_argv_match(dl
, "encap") &&
1119 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1120 const char *typestr
;
1123 err
= dl_argv_str(dl
, &typestr
);
1126 err
= eswitch_encap_mode_get(typestr
,
1127 &opts
->eswitch_encap_mode
);
1130 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1131 } else if (dl_argv_match(dl
, "path") &&
1132 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1134 err
= dl_argv_str(dl
, &opts
->resource_path
);
1137 o_found
|= DL_OPT_RESOURCE_PATH
;
1138 } else if (dl_argv_match(dl
, "size") &&
1139 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1141 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1144 o_found
|= DL_OPT_RESOURCE_SIZE
;
1145 } else if (dl_argv_match(dl
, "name") &&
1146 (o_all
& DL_OPT_PARAM_NAME
)) {
1148 err
= dl_argv_str(dl
, &opts
->param_name
);
1151 o_found
|= DL_OPT_PARAM_NAME
;
1152 } else if (dl_argv_match(dl
, "value") &&
1153 (o_all
& DL_OPT_PARAM_VALUE
)) {
1155 err
= dl_argv_str(dl
, &opts
->param_value
);
1158 o_found
|= DL_OPT_PARAM_VALUE
;
1159 } else if (dl_argv_match(dl
, "cmode") &&
1160 (o_all
& DL_OPT_PARAM_CMODE
)) {
1161 const char *cmodestr
;
1164 err
= dl_argv_str(dl
, &cmodestr
);
1167 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1170 o_found
|= DL_OPT_PARAM_CMODE
;
1171 } else if (dl_argv_match(dl
, "snapshot") &&
1172 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1174 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1177 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1178 } else if (dl_argv_match(dl
, "address") &&
1179 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1181 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1184 o_found
|= DL_OPT_REGION_ADDRESS
;
1185 } else if (dl_argv_match(dl
, "length") &&
1186 (o_all
& DL_OPT_REGION_LENGTH
)) {
1188 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1191 o_found
|= DL_OPT_REGION_LENGTH
;
1192 } else if (dl_argv_match(dl
, "file") &&
1193 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1195 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1198 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1199 } else if (dl_argv_match(dl
, "component") &&
1200 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1202 err
= dl_argv_str(dl
, &opts
->flash_component
);
1205 o_found
|= DL_OPT_FLASH_COMPONENT
;
1207 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1212 opts
->present
= o_found
;
1214 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1216 opts
->present
|= DL_OPT_SB
;
1219 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
1220 pr_err("Port type option expected.\n");
1224 if ((o_required
& DL_OPT_PORT_COUNT
) &&
1225 !(o_found
& DL_OPT_PORT_COUNT
)) {
1226 pr_err("Port split count option expected.\n");
1230 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
1231 pr_err("Pool index option expected.\n");
1235 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
1236 pr_err("Pool size option expected.\n");
1240 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
1241 pr_err("Pool type option expected.\n");
1245 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
1246 pr_err("Pool threshold type option expected.\n");
1250 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
1251 pr_err("Threshold option expected.\n");
1255 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
1256 pr_err("TC index option expected.\n");
1260 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1261 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1262 pr_err("E-Switch mode option expected.\n");
1266 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1267 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1268 pr_err("E-Switch inline-mode option expected.\n");
1272 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1273 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1274 pr_err("Dpipe table name expected\n");
1278 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1279 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1280 pr_err("Dpipe table counter state expected\n");
1284 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1285 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1286 pr_err("E-Switch encapsulation option expected.\n");
1290 if ((o_required
& DL_OPT_PARAM_NAME
) &&
1291 !(o_found
& DL_OPT_PARAM_NAME
)) {
1292 pr_err("Parameter name expected.\n");
1296 if ((o_required
& DL_OPT_PARAM_VALUE
) &&
1297 !(o_found
& DL_OPT_PARAM_VALUE
)) {
1298 pr_err("Value to set expected.\n");
1302 if ((o_required
& DL_OPT_PARAM_CMODE
) &&
1303 !(o_found
& DL_OPT_PARAM_CMODE
)) {
1304 pr_err("Configuration mode expected.\n");
1308 if ((o_required
& DL_OPT_REGION_SNAPSHOT_ID
) &&
1309 !(o_found
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1310 pr_err("Region snapshot id expected.\n");
1314 if ((o_required
& DL_OPT_REGION_ADDRESS
) &&
1315 !(o_found
& DL_OPT_REGION_ADDRESS
)) {
1316 pr_err("Region address value expected.\n");
1320 if ((o_required
& DL_OPT_REGION_LENGTH
) &&
1321 !(o_found
& DL_OPT_REGION_LENGTH
)) {
1322 pr_err("Region length value expected.\n");
1329 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1331 struct dl_opts
*opts
= &dl
->opts
;
1333 if (opts
->present
& DL_OPT_HANDLE
) {
1334 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1335 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1336 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1337 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1338 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1339 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1341 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1342 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1343 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1344 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1347 if (opts
->present
& DL_OPT_PORT_TYPE
)
1348 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1350 if (opts
->present
& DL_OPT_PORT_COUNT
)
1351 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1353 if (opts
->present
& DL_OPT_SB
)
1354 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1356 if (opts
->present
& DL_OPT_SB_POOL
)
1357 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1358 opts
->sb_pool_index
);
1359 if (opts
->present
& DL_OPT_SB_SIZE
)
1360 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1361 opts
->sb_pool_size
);
1362 if (opts
->present
& DL_OPT_SB_TYPE
)
1363 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1364 opts
->sb_pool_type
);
1365 if (opts
->present
& DL_OPT_SB_THTYPE
)
1366 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1367 opts
->sb_pool_thtype
);
1368 if (opts
->present
& DL_OPT_SB_TH
)
1369 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1370 opts
->sb_threshold
);
1371 if (opts
->present
& DL_OPT_SB_TC
)
1372 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1374 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1375 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1376 opts
->eswitch_mode
);
1377 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1378 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1379 opts
->eswitch_inline_mode
);
1380 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1381 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1382 opts
->dpipe_table_name
);
1383 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1384 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1385 opts
->dpipe_counters_enable
);
1386 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1387 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1388 opts
->eswitch_encap_mode
);
1389 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1390 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1392 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1393 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1394 opts
->resource_size
);
1395 if (opts
->present
& DL_OPT_PARAM_NAME
)
1396 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1398 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1399 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1401 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1402 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1403 opts
->region_snapshot_id
);
1404 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1405 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1406 opts
->region_address
);
1407 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1408 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1409 opts
->region_length
);
1410 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1411 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1412 opts
->flash_file_name
);
1413 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1414 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1415 opts
->flash_component
);
1418 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1419 uint32_t o_required
, uint32_t o_optional
)
1423 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1426 dl_opts_put(nlh
, dl
);
1430 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1432 struct dl_opts
*opts
= &dl
->opts
;
1433 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1434 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1435 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1436 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1438 if (opts
->present
& DL_OPT_HANDLE
&&
1439 attr_bus_name
&& attr_dev_name
) {
1440 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1441 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1443 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1444 strcmp(dev_name
, opts
->dev_name
) != 0)
1447 if (opts
->present
& DL_OPT_HANDLEP
&&
1448 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1449 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1450 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1451 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1453 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1454 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1455 port_index
!= opts
->port_index
)
1458 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1459 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1461 if (sb_index
!= opts
->sb_index
)
1467 static void cmd_dev_help(void)
1469 pr_err("Usage: devlink dev show [ DEV ]\n");
1470 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1471 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1472 pr_err(" [ encap { disable | enable } ]\n");
1473 pr_err(" devlink dev eswitch show DEV\n");
1474 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1475 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1476 pr_err(" devlink dev reload DEV\n");
1477 pr_err(" devlink dev info [ DEV ]\n");
1478 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1481 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1482 const char *dev_name
)
1484 if (!dl
->arr_last
.present
)
1486 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1487 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1490 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1491 const char *dev_name
)
1493 dl
->arr_last
.present
= true;
1494 free(dl
->arr_last
.dev_name
);
1495 free(dl
->arr_last
.bus_name
);
1496 dl
->arr_last
.bus_name
= strdup(bus_name
);
1497 dl
->arr_last
.dev_name
= strdup(dev_name
);
1500 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1501 const char *dev_name
)
1503 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1506 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1507 const char *dev_name
)
1509 return dl
->arr_last
.present
&&
1510 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1513 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1514 bool content
, bool array
)
1516 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1517 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1520 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1522 if (dl
->json_output
) {
1524 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1525 jsonw_end_array(dl
->jw
);
1526 if (should_arr_last_handle_start(dl
, bus_name
,
1528 jsonw_name(dl
->jw
, buf
);
1529 jsonw_start_array(dl
->jw
);
1530 jsonw_start_object(dl
->jw
);
1531 arr_last_handle_set(dl
, bus_name
, dev_name
);
1533 jsonw_start_object(dl
->jw
);
1536 jsonw_name(dl
->jw
, buf
);
1537 jsonw_start_object(dl
->jw
);
1541 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1542 __pr_out_indent_dec();
1543 if (should_arr_last_handle_start(dl
, bus_name
,
1545 pr_out("%s%s", buf
, content
? ":" : "");
1547 __pr_out_indent_inc();
1548 arr_last_handle_set(dl
, bus_name
, dev_name
);
1551 pr_out("%s%s", buf
, content
? ":" : "");
1556 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1558 __pr_out_handle_start(dl
, tb
, true, true);
1561 static void pr_out_handle_end(struct dl
*dl
)
1563 if (dl
->json_output
)
1564 jsonw_end_object(dl
->jw
);
1569 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1571 __pr_out_handle_start(dl
, tb
, false, false);
1572 pr_out_handle_end(dl
);
1575 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1576 const char *dev_name
, uint32_t port_index
)
1578 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1579 dl
->arr_last
.port_index
== port_index
;
1582 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1583 const char *dev_name
, uint32_t port_index
)
1585 arr_last_handle_set(dl
, bus_name
, dev_name
);
1586 dl
->arr_last
.port_index
= port_index
;
1589 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1590 const char *bus_name
,
1591 const char *dev_name
,
1592 uint32_t port_index
)
1594 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1597 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1598 const char *bus_name
,
1599 const char *dev_name
,
1600 uint32_t port_index
)
1602 return dl
->arr_last
.present
&&
1603 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1606 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1607 const char *dev_name
,
1608 uint32_t port_index
, bool try_nice
,
1611 static char buf
[32];
1612 char *ifname
= NULL
;
1614 if (dl
->no_nice_names
|| !try_nice
||
1615 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1616 port_index
, &ifname
) != 0)
1617 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1619 sprintf(buf
, "%s", ifname
);
1621 if (dl
->json_output
) {
1623 if (should_arr_last_port_handle_end(dl
, bus_name
,
1626 jsonw_end_array(dl
->jw
);
1627 if (should_arr_last_port_handle_start(dl
, bus_name
,
1630 jsonw_name(dl
->jw
, buf
);
1631 jsonw_start_array(dl
->jw
);
1632 jsonw_start_object(dl
->jw
);
1633 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1636 jsonw_start_object(dl
->jw
);
1639 jsonw_name(dl
->jw
, buf
);
1640 jsonw_start_object(dl
->jw
);
1647 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1649 const char *bus_name
;
1650 const char *dev_name
;
1651 uint32_t port_index
;
1653 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1654 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1655 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1656 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1659 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1661 const char *bus_name
;
1662 const char *dev_name
;
1663 uint32_t port_index
;
1665 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1666 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1667 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1668 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1671 static void pr_out_port_handle_end(struct dl
*dl
)
1673 if (dl
->json_output
)
1674 jsonw_end_object(dl
->jw
);
1680 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1682 if (dl
->json_output
) {
1683 jsonw_string_field(dl
->jw
, name
, val
);
1685 if (g_indent_newline
)
1686 pr_out("%s %s", name
, val
);
1688 pr_out(" %s %s", name
, val
);
1692 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1695 pr_out_str(dl
, name
, "true");
1697 pr_out_str(dl
, name
, "false");
1700 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1702 if (dl
->json_output
) {
1703 jsonw_uint_field(dl
->jw
, name
, val
);
1705 if (g_indent_newline
)
1706 pr_out("%s %u", name
, val
);
1708 pr_out(" %s %u", name
, val
);
1712 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1714 if (val
== (uint64_t) -1)
1715 return pr_out_str(dl
, name
, "unlimited");
1717 return pr_out_uint(dl
, name
, val
);
1720 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1722 if (dl
->json_output
) {
1723 jsonw_name(dl
->jw
, "address");
1724 jsonw_uint(dl
->jw
, addr
);
1725 jsonw_name(dl
->jw
, "data");
1726 jsonw_start_array(dl
->jw
);
1730 static void pr_out_region_chunk_end(struct dl
*dl
)
1732 if (dl
->json_output
)
1733 jsonw_end_array(dl
->jw
);
1736 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1739 static uint64_t align_val
;
1742 pr_out_region_chunk_start(dl
, addr
);
1744 if (!dl
->json_output
)
1745 if (!(align_val
% 16))
1746 pr_out("%s%016"PRIx64
" ",
1747 align_val
? "\n" : "",
1752 if (dl
->json_output
)
1753 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1755 pr_out("%02x ", data
[i
]);
1760 pr_out_region_chunk_end(dl
);
1763 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1765 pr_out_handle(dl
, tb
);
1768 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1770 if (dl
->json_output
) {
1771 jsonw_start_object(dl
->jw
);
1772 jsonw_name(dl
->jw
, name
);
1773 jsonw_start_object(dl
->jw
);
1777 static void pr_out_section_end(struct dl
*dl
)
1779 if (dl
->json_output
) {
1780 if (dl
->arr_last
.present
)
1781 jsonw_end_array(dl
->jw
);
1782 jsonw_end_object(dl
->jw
);
1783 jsonw_end_object(dl
->jw
);
1787 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1789 if (dl
->json_output
) {
1790 jsonw_name(dl
->jw
, name
);
1791 jsonw_start_array(dl
->jw
);
1793 __pr_out_indent_inc();
1795 pr_out("%s:", name
);
1796 __pr_out_indent_inc();
1801 static void pr_out_array_end(struct dl
*dl
)
1803 if (dl
->json_output
) {
1804 jsonw_end_array(dl
->jw
);
1806 __pr_out_indent_dec();
1807 __pr_out_indent_dec();
1811 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1813 if (dl
->json_output
) {
1814 jsonw_name(dl
->jw
, name
);
1815 jsonw_start_object(dl
->jw
);
1817 __pr_out_indent_inc();
1819 pr_out("%s:", name
);
1820 __pr_out_indent_inc();
1825 static void pr_out_object_end(struct dl
*dl
)
1827 if (dl
->json_output
) {
1828 jsonw_end_object(dl
->jw
);
1830 __pr_out_indent_dec();
1831 __pr_out_indent_dec();
1835 static void pr_out_entry_start(struct dl
*dl
)
1837 if (dl
->json_output
)
1838 jsonw_start_object(dl
->jw
);
1841 static void pr_out_entry_end(struct dl
*dl
)
1843 if (dl
->json_output
)
1844 jsonw_end_object(dl
->jw
);
1849 static const char *param_cmode_name(uint8_t cmode
)
1852 case DEVLINK_PARAM_CMODE_RUNTIME
:
1853 return PARAM_CMODE_RUNTIME_STR
;
1854 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1855 return PARAM_CMODE_DRIVERINIT_STR
;
1856 case DEVLINK_PARAM_CMODE_PERMANENT
:
1857 return PARAM_CMODE_PERMANENT_STR
;
1858 default: return "<unknown type>";
1862 static const char *eswitch_mode_name(uint32_t mode
)
1865 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1866 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1867 default: return "<unknown mode>";
1871 static const char *eswitch_inline_mode_name(uint32_t mode
)
1874 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1875 return ESWITCH_INLINE_MODE_NONE
;
1876 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1877 return ESWITCH_INLINE_MODE_LINK
;
1878 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1879 return ESWITCH_INLINE_MODE_NETWORK
;
1880 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1881 return ESWITCH_INLINE_MODE_TRANSPORT
;
1883 return "<unknown mode>";
1887 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1889 __pr_out_handle_start(dl
, tb
, true, false);
1891 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1892 pr_out_str(dl
, "mode",
1893 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1895 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1896 pr_out_str(dl
, "inline-mode",
1897 eswitch_inline_mode_name(mnl_attr_get_u8(
1898 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1900 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1901 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1903 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1906 pr_out_handle_end(dl
);
1909 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1911 struct dl
*dl
= data
;
1912 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1913 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1915 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1916 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1917 return MNL_CB_ERROR
;
1918 pr_out_eswitch(dl
, tb
);
1922 static int cmd_dev_eswitch_show(struct dl
*dl
)
1924 struct nlmsghdr
*nlh
;
1927 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1928 NLM_F_REQUEST
| NLM_F_ACK
);
1930 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1934 pr_out_section_start(dl
, "dev");
1935 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1936 pr_out_section_end(dl
);
1940 static int cmd_dev_eswitch_set(struct dl
*dl
)
1942 struct nlmsghdr
*nlh
;
1945 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1946 NLM_F_REQUEST
| NLM_F_ACK
);
1948 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1949 DL_OPT_ESWITCH_MODE
|
1950 DL_OPT_ESWITCH_INLINE_MODE
|
1951 DL_OPT_ESWITCH_ENCAP_MODE
);
1956 if (dl
->opts
.present
== 1) {
1957 pr_err("Need to set at least one option\n");
1961 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1964 static int cmd_dev_eswitch(struct dl
*dl
)
1966 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1969 } else if (dl_argv_match(dl
, "set")) {
1971 return cmd_dev_eswitch_set(dl
);
1972 } else if (dl_argv_match(dl
, "show")) {
1974 return cmd_dev_eswitch_show(dl
);
1976 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1980 struct param_val_conv
{
1986 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
1987 uint32_t len
, const char *name
)
1991 for (i
= 0; i
< len
; i
++)
1992 if (!strcmp(param_val_conv
[i
].name
, name
))
1999 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2000 uint32_t len
, const char *name
, const char *vstr
,
2005 for (i
= 0; i
< len
; i
++)
2006 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2007 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2008 *vuint
= param_val_conv
[i
].vuint
;
2016 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2017 uint32_t len
, const char *name
, uint32_t vuint
,
2022 for (i
= 0; i
< len
; i
++)
2023 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2024 param_val_conv
[i
].vuint
== vuint
) {
2025 *vstr
= param_val_conv
[i
].vstr
;
2032 static const struct param_val_conv param_val_conv
[] = {
2034 .name
= "fw_load_policy",
2036 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2039 .name
= "fw_load_policy",
2041 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2045 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2047 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2048 int nla_type
, struct nlattr
*nl
)
2050 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2051 struct nlattr
*val_attr
;
2056 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2057 if (err
!= MNL_CB_OK
)
2060 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2061 (nla_type
!= MNL_TYPE_FLAG
&&
2062 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2065 pr_out_str(dl
, "cmode",
2066 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2067 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2069 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2075 err
= param_val_conv_str_get(param_val_conv
,
2078 mnl_attr_get_u8(val_attr
),
2082 pr_out_str(dl
, "value", vstr
);
2084 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2089 err
= param_val_conv_str_get(param_val_conv
,
2092 mnl_attr_get_u16(val_attr
),
2096 pr_out_str(dl
, "value", vstr
);
2098 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2103 err
= param_val_conv_str_get(param_val_conv
,
2106 mnl_attr_get_u32(val_attr
),
2110 pr_out_str(dl
, "value", vstr
);
2112 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2115 case MNL_TYPE_STRING
:
2116 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2119 pr_out_bool(dl
, "value", val_attr
? true : false);
2124 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2126 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2127 struct nlattr
*param_value_attr
;
2128 const char *nla_name
;
2132 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2133 if (err
!= MNL_CB_OK
)
2135 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2136 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2137 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2141 pr_out_handle_start_arr(dl
, tb
);
2143 __pr_out_handle_start(dl
, tb
, true, false);
2145 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2147 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2148 pr_out_str(dl
, "name", nla_name
);
2150 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2151 pr_out_str(dl
, "type", "driver-specific");
2153 pr_out_str(dl
, "type", "generic");
2155 pr_out_array_start(dl
, "values");
2156 mnl_attr_for_each_nested(param_value_attr
,
2157 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2158 pr_out_entry_start(dl
);
2159 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2160 pr_out_entry_end(dl
);
2162 pr_out_array_end(dl
);
2163 pr_out_handle_end(dl
);
2166 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2168 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2169 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2170 struct dl
*dl
= data
;
2172 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2173 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2174 !tb
[DEVLINK_ATTR_PARAM
])
2175 return MNL_CB_ERROR
;
2176 pr_out_param(dl
, tb
, true);
2192 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2194 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2195 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2196 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2197 struct nlattr
*param_value_attr
;
2198 enum devlink_param_cmode cmode
;
2199 struct param_ctx
*ctx
= data
;
2200 struct dl
*dl
= ctx
->dl
;
2204 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2205 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2206 !tb
[DEVLINK_ATTR_PARAM
])
2207 return MNL_CB_ERROR
;
2209 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2210 if (err
!= MNL_CB_OK
)
2211 return MNL_CB_ERROR
;
2213 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2214 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2215 return MNL_CB_ERROR
;
2217 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2218 mnl_attr_for_each_nested(param_value_attr
,
2219 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2220 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2221 struct nlattr
*val_attr
;
2223 err
= mnl_attr_parse_nested(param_value_attr
,
2224 attr_cb
, nla_value
);
2225 if (err
!= MNL_CB_OK
)
2226 return MNL_CB_ERROR
;
2228 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2229 (nla_type
!= MNL_TYPE_FLAG
&&
2230 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2231 return MNL_CB_ERROR
;
2233 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2234 if (cmode
== dl
->opts
.cmode
) {
2235 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2238 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2241 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2244 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2246 case MNL_TYPE_STRING
:
2247 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2250 ctx
->value
.vbool
= val_attr
? true : false;
2256 ctx
->nla_type
= nla_type
;
2260 static int cmd_dev_param_set(struct dl
*dl
)
2262 struct param_ctx ctx
= {};
2263 struct nlmsghdr
*nlh
;
2271 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2273 DL_OPT_PARAM_VALUE
|
2274 DL_OPT_PARAM_CMODE
, 0);
2278 /* Get value type */
2279 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2280 NLM_F_REQUEST
| NLM_F_ACK
);
2281 dl_opts_put(nlh
, dl
);
2284 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2288 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2289 NLM_F_REQUEST
| NLM_F_ACK
);
2290 dl_opts_put(nlh
, dl
);
2292 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2293 dl
->opts
.param_name
);
2295 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2296 switch (ctx
.nla_type
) {
2299 err
= param_val_conv_uint_get(param_val_conv
,
2301 dl
->opts
.param_name
,
2302 dl
->opts
.param_value
,
2306 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2309 goto err_param_value_parse
;
2310 if (val_u8
== ctx
.value
.vu8
)
2312 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2316 err
= param_val_conv_uint_get(param_val_conv
,
2318 dl
->opts
.param_name
,
2319 dl
->opts
.param_value
,
2323 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2326 goto err_param_value_parse
;
2327 if (val_u16
== ctx
.value
.vu16
)
2329 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2333 err
= param_val_conv_uint_get(param_val_conv
,
2335 dl
->opts
.param_name
,
2336 dl
->opts
.param_value
,
2339 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2341 goto err_param_value_parse
;
2342 if (val_u32
== ctx
.value
.vu32
)
2344 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2347 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2349 goto err_param_value_parse
;
2350 if (val_bool
== ctx
.value
.vbool
)
2353 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2356 case MNL_TYPE_STRING
:
2357 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2358 dl
->opts
.param_value
);
2359 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2363 printf("Value type not supported\n");
2366 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2368 err_param_value_parse
:
2369 pr_err("Value \"%s\" is not a number or not within range\n",
2370 dl
->opts
.param_value
);
2374 static int cmd_dev_param_show(struct dl
*dl
)
2376 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2377 struct nlmsghdr
*nlh
;
2380 if (dl_argc(dl
) == 0)
2381 flags
|= NLM_F_DUMP
;
2383 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2385 if (dl_argc(dl
) > 0) {
2386 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2387 DL_OPT_PARAM_NAME
, 0);
2392 pr_out_section_start(dl
, "param");
2393 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2394 pr_out_section_end(dl
);
2398 static int cmd_dev_param(struct dl
*dl
)
2400 if (dl_argv_match(dl
, "help")) {
2403 } else if (dl_argv_match(dl
, "show") ||
2404 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2406 return cmd_dev_param_show(dl
);
2407 } else if (dl_argv_match(dl
, "set")) {
2409 return cmd_dev_param_set(dl
);
2411 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2414 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2416 struct dl
*dl
= data
;
2417 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2418 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2420 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2421 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2422 return MNL_CB_ERROR
;
2427 static int cmd_dev_show(struct dl
*dl
)
2429 struct nlmsghdr
*nlh
;
2430 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2433 if (dl_argc(dl
) == 0)
2434 flags
|= NLM_F_DUMP
;
2436 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2438 if (dl_argc(dl
) > 0) {
2439 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2444 pr_out_section_start(dl
, "dev");
2445 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2446 pr_out_section_end(dl
);
2450 static void cmd_dev_reload_help(void)
2452 pr_err("Usage: devlink dev reload [ DEV ]\n");
2455 static int cmd_dev_reload(struct dl
*dl
)
2457 struct nlmsghdr
*nlh
;
2460 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2461 cmd_dev_reload_help();
2465 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2466 NLM_F_REQUEST
| NLM_F_ACK
);
2468 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2472 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2475 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2476 const char *name
, int type
)
2478 struct nlattr
*version
;
2480 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2481 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2482 const char *ver_value
;
2483 const char *ver_name
;
2486 if (mnl_attr_get_type(version
) != type
)
2489 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2490 if (err
!= MNL_CB_OK
)
2493 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2494 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2498 pr_out_object_start(dl
, name
);
2502 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2503 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2505 pr_out_str(dl
, ver_name
, ver_value
);
2506 if (!dl
->json_output
)
2511 pr_out_object_end(dl
);
2514 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2515 struct nlattr
**tb
, bool has_versions
)
2517 __pr_out_handle_start(dl
, tb
, true, false);
2519 __pr_out_indent_inc();
2520 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2521 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2523 if (!dl
->json_output
)
2525 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2528 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2529 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2531 if (!dl
->json_output
)
2533 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2535 __pr_out_indent_dec();
2538 pr_out_object_start(dl
, "versions");
2540 pr_out_versions_single(dl
, nlh
, "fixed",
2541 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2542 pr_out_versions_single(dl
, nlh
, "running",
2543 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2544 pr_out_versions_single(dl
, nlh
, "stored",
2545 DEVLINK_ATTR_INFO_VERSION_STORED
);
2547 pr_out_object_end(dl
);
2550 pr_out_handle_end(dl
);
2553 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2555 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2556 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2557 bool has_versions
, has_info
;
2558 struct dl
*dl
= data
;
2560 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2562 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2563 return MNL_CB_ERROR
;
2565 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2566 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2567 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2568 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2569 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2573 pr_out_info(dl
, nlh
, tb
, has_versions
);
2578 static void cmd_dev_info_help(void)
2580 pr_err("Usage: devlink dev info [ DEV ]\n");
2583 static int cmd_dev_info(struct dl
*dl
)
2585 struct nlmsghdr
*nlh
;
2586 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2589 if (dl_argv_match(dl
, "help")) {
2590 cmd_dev_info_help();
2594 if (dl_argc(dl
) == 0)
2595 flags
|= NLM_F_DUMP
;
2597 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2599 if (dl_argc(dl
) > 0) {
2600 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2605 pr_out_section_start(dl
, "info");
2606 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2607 pr_out_section_end(dl
);
2611 static void cmd_dev_flash_help(void)
2613 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2616 static int cmd_dev_flash(struct dl
*dl
)
2618 struct nlmsghdr
*nlh
;
2621 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2622 cmd_dev_flash_help();
2626 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2627 NLM_F_REQUEST
| NLM_F_ACK
);
2629 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2630 DL_OPT_FLASH_COMPONENT
);
2634 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2637 static int cmd_dev(struct dl
*dl
)
2639 if (dl_argv_match(dl
, "help")) {
2642 } else if (dl_argv_match(dl
, "show") ||
2643 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2645 return cmd_dev_show(dl
);
2646 } else if (dl_argv_match(dl
, "eswitch")) {
2648 return cmd_dev_eswitch(dl
);
2649 } else if (dl_argv_match(dl
, "reload")) {
2651 return cmd_dev_reload(dl
);
2652 } else if (dl_argv_match(dl
, "param")) {
2654 return cmd_dev_param(dl
);
2655 } else if (dl_argv_match(dl
, "info")) {
2657 return cmd_dev_info(dl
);
2658 } else if (dl_argv_match(dl
, "flash")) {
2660 return cmd_dev_flash(dl
);
2662 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2666 static void cmd_port_help(void)
2668 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2669 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2670 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2671 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2674 static const char *port_type_name(uint32_t type
)
2677 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2678 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2679 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2680 case DEVLINK_PORT_TYPE_IB
: return "ib";
2681 default: return "<unknown type>";
2685 static const char *port_flavour_name(uint16_t flavour
)
2688 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2690 case DEVLINK_PORT_FLAVOUR_CPU
:
2692 case DEVLINK_PORT_FLAVOUR_DSA
:
2695 return "<unknown flavour>";
2699 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2701 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2702 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2704 pr_out_port_handle_start(dl
, tb
, false);
2706 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2708 pr_out_str(dl
, "type", port_type_name(port_type
));
2710 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2712 if (port_type
!= des_port_type
)
2713 pr_out_str(dl
, "des_type",
2714 port_type_name(des_port_type
));
2717 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2718 pr_out_str(dl
, "netdev",
2719 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2720 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2721 pr_out_str(dl
, "ibdev",
2722 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2723 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2724 uint16_t port_flavour
=
2725 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2727 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2729 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2730 pr_out_uint(dl
, "split_group",
2731 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2732 pr_out_port_handle_end(dl
);
2735 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2737 struct dl
*dl
= data
;
2738 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2739 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2741 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2742 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2743 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2744 return MNL_CB_ERROR
;
2745 pr_out_port(dl
, tb
);
2749 static int cmd_port_show(struct dl
*dl
)
2751 struct nlmsghdr
*nlh
;
2752 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2755 if (dl_argc(dl
) == 0)
2756 flags
|= NLM_F_DUMP
;
2758 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2760 if (dl_argc(dl
) > 0) {
2761 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2766 pr_out_section_start(dl
, "port");
2767 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2768 pr_out_section_end(dl
);
2772 static int cmd_port_set(struct dl
*dl
)
2774 struct nlmsghdr
*nlh
;
2777 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2778 NLM_F_REQUEST
| NLM_F_ACK
);
2780 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2784 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2787 static int cmd_port_split(struct dl
*dl
)
2789 struct nlmsghdr
*nlh
;
2792 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2793 NLM_F_REQUEST
| NLM_F_ACK
);
2795 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2799 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2802 static int cmd_port_unsplit(struct dl
*dl
)
2804 struct nlmsghdr
*nlh
;
2807 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2808 NLM_F_REQUEST
| NLM_F_ACK
);
2810 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2814 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2817 static int cmd_port(struct dl
*dl
)
2819 if (dl_argv_match(dl
, "help")) {
2822 } else if (dl_argv_match(dl
, "show") ||
2823 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2825 return cmd_port_show(dl
);
2826 } else if (dl_argv_match(dl
, "set")) {
2828 return cmd_port_set(dl
);
2829 } else if (dl_argv_match(dl
, "split")) {
2831 return cmd_port_split(dl
);
2832 } else if (dl_argv_match(dl
, "unsplit")) {
2834 return cmd_port_unsplit(dl
);
2836 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2840 static void cmd_sb_help(void)
2842 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2843 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2844 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2845 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2846 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2847 pr_err(" pool POOL_INDEX ]\n");
2848 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2849 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2850 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2851 pr_err(" type { ingress | egress } ]\n");
2852 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2853 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2854 pr_err(" th THRESHOLD\n");
2855 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2856 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2857 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2860 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2862 pr_out_handle_start_arr(dl
, tb
);
2863 pr_out_uint(dl
, "sb",
2864 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2865 pr_out_uint(dl
, "size",
2866 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2867 pr_out_uint(dl
, "ing_pools",
2868 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2869 pr_out_uint(dl
, "eg_pools",
2870 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2871 pr_out_uint(dl
, "ing_tcs",
2872 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2873 pr_out_uint(dl
, "eg_tcs",
2874 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2875 pr_out_handle_end(dl
);
2878 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2880 struct dl
*dl
= data
;
2881 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2882 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2884 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2885 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2886 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2887 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2888 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2889 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2890 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2891 return MNL_CB_ERROR
;
2896 static int cmd_sb_show(struct dl
*dl
)
2898 struct nlmsghdr
*nlh
;
2899 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2902 if (dl_argc(dl
) == 0)
2903 flags
|= NLM_F_DUMP
;
2905 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2907 if (dl_argc(dl
) > 0) {
2908 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2913 pr_out_section_start(dl
, "sb");
2914 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2915 pr_out_section_end(dl
);
2919 static const char *pool_type_name(uint8_t type
)
2922 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2923 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2924 default: return "<unknown type>";
2928 static const char *threshold_type_name(uint8_t type
)
2931 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2932 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2933 default: return "<unknown type>";
2937 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2939 pr_out_handle_start_arr(dl
, tb
);
2940 pr_out_uint(dl
, "sb",
2941 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2942 pr_out_uint(dl
, "pool",
2943 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2944 pr_out_str(dl
, "type",
2945 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2946 pr_out_uint(dl
, "size",
2947 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2948 pr_out_str(dl
, "thtype",
2949 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2950 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
2951 pr_out_uint(dl
, "cell_size",
2952 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
2953 pr_out_handle_end(dl
);
2956 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2958 struct dl
*dl
= data
;
2959 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2960 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2962 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2963 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2964 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2965 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2966 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2967 return MNL_CB_ERROR
;
2968 pr_out_sb_pool(dl
, tb
);
2972 static int cmd_sb_pool_show(struct dl
*dl
)
2974 struct nlmsghdr
*nlh
;
2975 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2978 if (dl_argc(dl
) == 0)
2979 flags
|= NLM_F_DUMP
;
2981 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2983 if (dl_argc(dl
) > 0) {
2984 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2990 pr_out_section_start(dl
, "pool");
2991 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2992 pr_out_section_end(dl
);
2996 static int cmd_sb_pool_set(struct dl
*dl
)
2998 struct nlmsghdr
*nlh
;
3001 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3002 NLM_F_REQUEST
| NLM_F_ACK
);
3004 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3005 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3009 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3012 static int cmd_sb_pool(struct dl
*dl
)
3014 if (dl_argv_match(dl
, "help")) {
3017 } else if (dl_argv_match(dl
, "show") ||
3018 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3020 return cmd_sb_pool_show(dl
);
3021 } else if (dl_argv_match(dl
, "set")) {
3023 return cmd_sb_pool_set(dl
);
3025 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3029 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3031 pr_out_port_handle_start_arr(dl
, tb
, true);
3032 pr_out_uint(dl
, "sb",
3033 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3034 pr_out_uint(dl
, "pool",
3035 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3036 pr_out_uint(dl
, "threshold",
3037 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3038 pr_out_port_handle_end(dl
);
3041 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3043 struct dl
*dl
= data
;
3044 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3045 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3047 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3048 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3049 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3050 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3051 return MNL_CB_ERROR
;
3052 pr_out_sb_port_pool(dl
, tb
);
3056 static int cmd_sb_port_pool_show(struct dl
*dl
)
3058 struct nlmsghdr
*nlh
;
3059 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3062 if (dl_argc(dl
) == 0)
3063 flags
|= NLM_F_DUMP
;
3065 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3067 if (dl_argc(dl
) > 0) {
3068 err
= dl_argv_parse_put(nlh
, dl
,
3069 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3075 pr_out_section_start(dl
, "port_pool");
3076 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3077 pr_out_section_end(dl
);
3081 static int cmd_sb_port_pool_set(struct dl
*dl
)
3083 struct nlmsghdr
*nlh
;
3086 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3087 NLM_F_REQUEST
| NLM_F_ACK
);
3089 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3090 DL_OPT_SB_TH
, DL_OPT_SB
);
3094 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3097 static int cmd_sb_port_pool(struct dl
*dl
)
3099 if (dl_argv_match(dl
, "help")) {
3102 } else if (dl_argv_match(dl
, "show") ||
3103 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3105 return cmd_sb_port_pool_show(dl
);
3106 } else if (dl_argv_match(dl
, "set")) {
3108 return cmd_sb_port_pool_set(dl
);
3110 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3114 static int cmd_sb_port(struct dl
*dl
)
3116 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3119 } else if (dl_argv_match(dl
, "pool")) {
3121 return cmd_sb_port_pool(dl
);
3123 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3127 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3129 pr_out_port_handle_start_arr(dl
, tb
, true);
3130 pr_out_uint(dl
, "sb",
3131 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3132 pr_out_uint(dl
, "tc",
3133 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3134 pr_out_str(dl
, "type",
3135 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3136 pr_out_uint(dl
, "pool",
3137 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3138 pr_out_uint(dl
, "threshold",
3139 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3140 pr_out_port_handle_end(dl
);
3143 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3145 struct dl
*dl
= data
;
3146 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3147 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3149 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3150 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3151 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3152 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3153 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3154 return MNL_CB_ERROR
;
3155 pr_out_sb_tc_bind(dl
, tb
);
3159 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3161 struct nlmsghdr
*nlh
;
3162 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3165 if (dl_argc(dl
) == 0)
3166 flags
|= NLM_F_DUMP
;
3168 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3170 if (dl_argc(dl
) > 0) {
3171 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3172 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3177 pr_out_section_start(dl
, "tc_bind");
3178 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3179 pr_out_section_end(dl
);
3183 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3185 struct nlmsghdr
*nlh
;
3188 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3189 NLM_F_REQUEST
| NLM_F_ACK
);
3191 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3192 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3197 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3200 static int cmd_sb_tc_bind(struct dl
*dl
)
3202 if (dl_argv_match(dl
, "help")) {
3205 } else if (dl_argv_match(dl
, "show") ||
3206 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3208 return cmd_sb_tc_bind_show(dl
);
3209 } else if (dl_argv_match(dl
, "set")) {
3211 return cmd_sb_tc_bind_set(dl
);
3213 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3217 static int cmd_sb_tc(struct dl
*dl
)
3219 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3222 } else if (dl_argv_match(dl
, "bind")) {
3224 return cmd_sb_tc_bind(dl
);
3226 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3231 struct list_head list
;
3235 uint32_t bound_pool_index
;
3239 struct list_head list
;
3242 uint32_t port_index
;
3244 struct list_head pool_list
;
3245 struct list_head ing_tc_list
;
3246 struct list_head eg_tc_list
;
3252 struct list_head port_list
;
3255 static struct occ_item
*occ_item_alloc(void)
3257 return calloc(1, sizeof(struct occ_item
));
3260 static void occ_item_free(struct occ_item
*occ_item
)
3265 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3267 struct occ_port
*occ_port
;
3269 occ_port
= calloc(1, sizeof(*occ_port
));
3272 occ_port
->port_index
= port_index
;
3273 INIT_LIST_HEAD(&occ_port
->pool_list
);
3274 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3275 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3279 static void occ_port_free(struct occ_port
*occ_port
)
3281 struct occ_item
*occ_item
, *tmp
;
3283 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3284 occ_item_free(occ_item
);
3285 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3286 occ_item_free(occ_item
);
3287 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3288 occ_item_free(occ_item
);
3291 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3293 struct occ_show
*occ_show
;
3295 occ_show
= calloc(1, sizeof(*occ_show
));
3299 INIT_LIST_HEAD(&occ_show
->port_list
);
3303 static void occ_show_free(struct occ_show
*occ_show
)
3305 struct occ_port
*occ_port
, *tmp
;
3307 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3308 occ_port_free(occ_port
);
3311 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3314 struct occ_port
*occ_port
;
3315 uint32_t port_index
;
3317 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3319 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3320 if (occ_port
->port_index
== port_index
)
3323 occ_port
= occ_port_alloc(port_index
);
3326 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3330 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3333 struct occ_item
*occ_item
;
3336 pr_out_sp(7, " %s:", label
);
3337 list_for_each_entry(occ_item
, list
, list
) {
3338 if ((i
- 1) % 4 == 0 && i
!= 1)
3341 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3342 occ_item
->bound_pool_index
);
3344 pr_out_sp(7, "%2u:", occ_item
->index
);
3345 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
3349 if ((i
- 1) % 4 != 0)
3353 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3354 struct list_head
*list
,
3357 struct occ_item
*occ_item
;
3360 jsonw_name(dl
->jw
, label
);
3361 jsonw_start_object(dl
->jw
);
3362 list_for_each_entry(occ_item
, list
, list
) {
3363 sprintf(buf
, "%u", occ_item
->index
);
3364 jsonw_name(dl
->jw
, buf
);
3365 jsonw_start_object(dl
->jw
);
3367 jsonw_uint_field(dl
->jw
, "bound_pool",
3368 occ_item
->bound_pool_index
);
3369 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3370 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3371 jsonw_end_object(dl
->jw
);
3373 jsonw_end_object(dl
->jw
);
3376 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3378 if (dl
->json_output
) {
3379 pr_out_json_occ_show_item_list(dl
, "pool",
3380 &occ_port
->pool_list
, false);
3381 pr_out_json_occ_show_item_list(dl
, "itc",
3382 &occ_port
->ing_tc_list
, true);
3383 pr_out_json_occ_show_item_list(dl
, "etc",
3384 &occ_port
->eg_tc_list
, true);
3387 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3388 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3389 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3393 static void pr_out_occ_show(struct occ_show
*occ_show
)
3395 struct dl
*dl
= occ_show
->dl
;
3396 struct dl_opts
*opts
= &dl
->opts
;
3397 struct occ_port
*occ_port
;
3399 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3400 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3401 occ_port
->port_index
, true, false);
3402 pr_out_occ_show_port(dl
, occ_port
);
3403 pr_out_port_handle_end(dl
);
3407 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3410 struct occ_port
*occ_port
;
3411 struct occ_item
*occ_item
;
3413 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3416 occ_port
= occ_port_get(occ_show
, tb
);
3418 occ_show
->err
= -ENOMEM
;
3422 occ_item
= occ_item_alloc();
3424 occ_show
->err
= -ENOMEM
;
3427 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3428 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3429 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3430 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3433 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3435 struct occ_show
*occ_show
= data
;
3436 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3437 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3439 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3440 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3441 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3442 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3443 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3444 return MNL_CB_ERROR
;
3445 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3449 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3452 struct occ_port
*occ_port
;
3453 struct occ_item
*occ_item
;
3456 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3459 occ_port
= occ_port_get(occ_show
, tb
);
3461 occ_show
->err
= -ENOMEM
;
3465 occ_item
= occ_item_alloc();
3467 occ_show
->err
= -ENOMEM
;
3470 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3471 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3472 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3473 occ_item
->bound_pool_index
=
3474 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3475 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3476 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3477 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3478 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3479 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3481 occ_item_free(occ_item
);
3484 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3486 struct occ_show
*occ_show
= data
;
3487 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3488 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3490 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3491 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3492 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3493 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3494 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3495 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3496 return MNL_CB_ERROR
;
3497 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3501 static int cmd_sb_occ_show(struct dl
*dl
)
3503 struct nlmsghdr
*nlh
;
3504 struct occ_show
*occ_show
;
3505 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3508 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3512 occ_show
= occ_show_alloc(dl
);
3516 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3518 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3519 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3523 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3525 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3526 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3530 pr_out_section_start(dl
, "occupancy");
3531 pr_out_occ_show(occ_show
);
3532 pr_out_section_end(dl
);
3535 occ_show_free(occ_show
);
3539 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3541 struct nlmsghdr
*nlh
;
3544 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3545 NLM_F_REQUEST
| NLM_F_ACK
);
3547 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3551 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3554 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3556 struct nlmsghdr
*nlh
;
3559 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3560 NLM_F_REQUEST
| NLM_F_ACK
);
3562 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3566 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3569 static int cmd_sb_occ(struct dl
*dl
)
3571 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3574 } else if (dl_argv_match(dl
, "show") ||
3575 dl_argv_match(dl
, "list")) {
3577 return cmd_sb_occ_show(dl
);
3578 } else if (dl_argv_match(dl
, "snapshot")) {
3580 return cmd_sb_occ_snapshot(dl
);
3581 } else if (dl_argv_match(dl
, "clearmax")) {
3583 return cmd_sb_occ_clearmax(dl
);
3585 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3589 static int cmd_sb(struct dl
*dl
)
3591 if (dl_argv_match(dl
, "help")) {
3594 } else if (dl_argv_match(dl
, "show") ||
3595 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3597 return cmd_sb_show(dl
);
3598 } else if (dl_argv_match(dl
, "pool")) {
3600 return cmd_sb_pool(dl
);
3601 } else if (dl_argv_match(dl
, "port")) {
3603 return cmd_sb_port(dl
);
3604 } else if (dl_argv_match(dl
, "tc")) {
3606 return cmd_sb_tc(dl
);
3607 } else if (dl_argv_match(dl
, "occupancy")) {
3609 return cmd_sb_occ(dl
);
3611 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3615 static const char *cmd_name(uint8_t cmd
)
3618 case DEVLINK_CMD_UNSPEC
: return "unspec";
3619 case DEVLINK_CMD_GET
: return "get";
3620 case DEVLINK_CMD_SET
: return "set";
3621 case DEVLINK_CMD_NEW
: return "new";
3622 case DEVLINK_CMD_DEL
: return "del";
3623 case DEVLINK_CMD_PORT_GET
: return "get";
3624 case DEVLINK_CMD_PORT_SET
: return "set";
3625 case DEVLINK_CMD_PORT_NEW
: return "new";
3626 case DEVLINK_CMD_PORT_DEL
: return "del";
3627 case DEVLINK_CMD_PARAM_GET
: return "get";
3628 case DEVLINK_CMD_PARAM_SET
: return "set";
3629 case DEVLINK_CMD_PARAM_NEW
: return "new";
3630 case DEVLINK_CMD_PARAM_DEL
: return "del";
3631 case DEVLINK_CMD_REGION_GET
: return "get";
3632 case DEVLINK_CMD_REGION_SET
: return "set";
3633 case DEVLINK_CMD_REGION_NEW
: return "new";
3634 case DEVLINK_CMD_REGION_DEL
: return "del";
3635 default: return "<unknown cmd>";
3639 static const char *cmd_obj(uint8_t cmd
)
3642 case DEVLINK_CMD_UNSPEC
: return "unspec";
3643 case DEVLINK_CMD_GET
:
3644 case DEVLINK_CMD_SET
:
3645 case DEVLINK_CMD_NEW
:
3646 case DEVLINK_CMD_DEL
:
3648 case DEVLINK_CMD_PORT_GET
:
3649 case DEVLINK_CMD_PORT_SET
:
3650 case DEVLINK_CMD_PORT_NEW
:
3651 case DEVLINK_CMD_PORT_DEL
:
3653 case DEVLINK_CMD_PARAM_GET
:
3654 case DEVLINK_CMD_PARAM_SET
:
3655 case DEVLINK_CMD_PARAM_NEW
:
3656 case DEVLINK_CMD_PARAM_DEL
:
3658 case DEVLINK_CMD_REGION_GET
:
3659 case DEVLINK_CMD_REGION_SET
:
3660 case DEVLINK_CMD_REGION_NEW
:
3661 case DEVLINK_CMD_REGION_DEL
:
3663 default: return "<unknown obj>";
3667 static void pr_out_mon_header(uint8_t cmd
)
3669 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3672 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3674 const char *obj
= cmd_obj(cmd
);
3675 unsigned int index
= 0;
3676 const char *cur_obj
;
3680 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3681 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3687 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3689 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3691 struct dl
*dl
= data
;
3692 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3693 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3694 uint8_t cmd
= genl
->cmd
;
3696 if (!cmd_filter_check(dl
, cmd
))
3700 case DEVLINK_CMD_GET
: /* fall through */
3701 case DEVLINK_CMD_SET
: /* fall through */
3702 case DEVLINK_CMD_NEW
: /* fall through */
3703 case DEVLINK_CMD_DEL
:
3704 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3705 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3706 return MNL_CB_ERROR
;
3707 pr_out_mon_header(genl
->cmd
);
3710 case DEVLINK_CMD_PORT_GET
: /* fall through */
3711 case DEVLINK_CMD_PORT_SET
: /* fall through */
3712 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3713 case DEVLINK_CMD_PORT_DEL
:
3714 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3715 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3716 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3717 return MNL_CB_ERROR
;
3718 pr_out_mon_header(genl
->cmd
);
3719 pr_out_port(dl
, tb
);
3721 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3722 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3723 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3724 case DEVLINK_CMD_PARAM_DEL
:
3725 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3726 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3727 !tb
[DEVLINK_ATTR_PARAM
])
3728 return MNL_CB_ERROR
;
3729 pr_out_mon_header(genl
->cmd
);
3730 pr_out_param(dl
, tb
, false);
3732 case DEVLINK_CMD_REGION_GET
: /* fall through */
3733 case DEVLINK_CMD_REGION_SET
: /* fall through */
3734 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3735 case DEVLINK_CMD_REGION_DEL
:
3736 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3737 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3738 !tb
[DEVLINK_ATTR_REGION_NAME
])
3739 return MNL_CB_ERROR
;
3740 pr_out_mon_header(genl
->cmd
);
3741 pr_out_region(dl
, tb
);
3747 static int cmd_mon_show(struct dl
*dl
)
3750 unsigned int index
= 0;
3751 const char *cur_obj
;
3753 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3754 if (strcmp(cur_obj
, "all") != 0 &&
3755 strcmp(cur_obj
, "dev") != 0 &&
3756 strcmp(cur_obj
, "port") != 0) {
3757 pr_err("Unknown object \"%s\"\n", cur_obj
);
3761 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3764 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3770 static void cmd_mon_help(void)
3772 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3773 "where OBJECT-LIST := { dev | port }\n");
3776 static int cmd_mon(struct dl
*dl
)
3778 if (dl_argv_match(dl
, "help")) {
3781 } else if (dl_no_arg(dl
)) {
3783 return cmd_mon_show(dl
);
3785 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3789 struct dpipe_field
{
3792 unsigned int bitwidth
;
3793 enum devlink_dpipe_field_mapping_type mapping_type
;
3796 struct dpipe_header
{
3797 struct list_head list
;
3800 struct dpipe_field
*fields
;
3801 unsigned int fields_count
;
3804 struct dpipe_table
{
3805 struct list_head list
;
3807 unsigned int resource_id
;
3808 bool resource_valid
;
3811 struct dpipe_tables
{
3812 struct list_head table_list
;
3822 enum devlink_resource_unit unit
;
3827 struct list_head list
;
3828 struct list_head resource_list
;
3829 struct resource
*parent
;
3833 struct list_head resource_list
;
3836 struct resource_ctx
{
3839 struct resources
*resources
;
3840 struct dpipe_tables
*tables
;
3841 bool print_resources
;
3842 bool pending_change
;
3845 static struct resource
*resource_alloc(void)
3847 struct resource
*resource
;
3849 resource
= calloc(1, sizeof(struct resource
));
3852 INIT_LIST_HEAD(&resource
->resource_list
);
3856 static void resource_free(struct resource
*resource
)
3858 struct resource
*child_resource
, *tmp
;
3860 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3862 free(child_resource
->name
);
3863 resource_free(child_resource
);
3868 static struct resources
*resources_alloc(void)
3870 struct resources
*resources
;
3872 resources
= calloc(1, sizeof(struct resources
));
3875 INIT_LIST_HEAD(&resources
->resource_list
);
3879 static void resources_free(struct resources
*resources
)
3881 struct resource
*resource
, *tmp
;
3883 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3884 resource_free(resource
);
3887 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3889 ctx
->resources
= resources_alloc();
3890 if (!ctx
->resources
)
3896 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3898 resources_free(ctx
->resources
);
3904 struct list_head global_headers
;
3905 struct list_head local_headers
;
3906 struct dpipe_tables
*tables
;
3907 struct resources
*resources
;
3912 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3914 struct dpipe_header
*header
;
3916 header
= calloc(1, sizeof(struct dpipe_header
));
3919 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3920 if (!header
->fields
)
3921 goto err_fields_alloc
;
3922 header
->fields_count
= fields_count
;
3930 static void dpipe_header_free(struct dpipe_header
*header
)
3932 free(header
->fields
);
3936 static void dpipe_header_clear(struct dpipe_header
*header
)
3938 struct dpipe_field
*field
;
3941 for (i
= 0; i
< header
->fields_count
; i
++) {
3942 field
= &header
->fields
[i
];
3948 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3949 struct dpipe_header
*header
, bool global
)
3952 list_add(&header
->list
, &ctx
->global_headers
);
3954 list_add(&header
->list
, &ctx
->local_headers
);
3957 static void dpipe_header_del(struct dpipe_header
*header
)
3959 list_del(&header
->list
);
3962 static struct dpipe_table
*dpipe_table_alloc(void)
3964 return calloc(1, sizeof(struct dpipe_table
));
3967 static void dpipe_table_free(struct dpipe_table
*table
)
3972 static struct dpipe_tables
*dpipe_tables_alloc(void)
3974 struct dpipe_tables
*tables
;
3976 tables
= calloc(1, sizeof(struct dpipe_tables
));
3979 INIT_LIST_HEAD(&tables
->table_list
);
3983 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3985 struct dpipe_table
*table
, *tmp
;
3987 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3988 dpipe_table_free(table
);
3992 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
3994 ctx
->tables
= dpipe_tables_alloc();
3999 INIT_LIST_HEAD(&ctx
->global_headers
);
4000 INIT_LIST_HEAD(&ctx
->local_headers
);
4004 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4006 struct dpipe_header
*header
, *tmp
;
4008 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4010 dpipe_header_del(header
);
4011 dpipe_header_clear(header
);
4012 dpipe_header_free(header
);
4014 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4016 dpipe_header_del(header
);
4017 dpipe_header_clear(header
);
4018 dpipe_header_free(header
);
4020 dpipe_tables_free(ctx
->tables
);
4023 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4024 uint32_t header_id
, bool global
)
4026 struct list_head
*header_list
;
4027 struct dpipe_header
*header
;
4030 header_list
= &ctx
->global_headers
;
4032 header_list
= &ctx
->local_headers
;
4033 list_for_each_entry(header
, header_list
, list
) {
4034 if (header
->id
!= header_id
)
4036 return header
->name
;
4041 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4043 uint32_t field_id
, bool global
)
4045 struct list_head
*header_list
;
4046 struct dpipe_header
*header
;
4049 header_list
= &ctx
->global_headers
;
4051 header_list
= &ctx
->local_headers
;
4052 list_for_each_entry(header
, header_list
, list
) {
4053 if (header
->id
!= header_id
)
4055 return header
->fields
[field_id
].name
;
4061 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4063 switch (mapping_type
) {
4064 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4066 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4074 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4075 uint32_t field_id
, bool global
)
4077 enum devlink_dpipe_field_mapping_type mapping_type
;
4078 struct list_head
*header_list
;
4079 struct dpipe_header
*header
;
4082 header_list
= &ctx
->global_headers
;
4084 header_list
= &ctx
->local_headers
;
4085 list_for_each_entry(header
, header_list
, list
) {
4086 if (header
->id
!= header_id
)
4088 mapping_type
= header
->fields
[field_id
].mapping_type
;
4089 return dpipe_field_mapping_e2s(mapping_type
);
4094 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4095 struct dpipe_field
*fields
,
4096 unsigned int field_count
)
4098 struct dpipe_field
*field
;
4101 for (i
= 0; i
< field_count
; i
++) {
4103 pr_out_entry_start(ctx
->dl
);
4104 pr_out_str(ctx
->dl
, "name", field
->name
);
4105 if (ctx
->dl
->verbose
)
4106 pr_out_uint(ctx
->dl
, "id", field
->id
);
4107 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4108 if (field
->mapping_type
)
4109 pr_out_str(ctx
->dl
, "mapping_type",
4110 dpipe_field_mapping_e2s(field
->mapping_type
));
4111 pr_out_entry_end(ctx
->dl
);
4116 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4117 struct dpipe_header
*header
, bool global
)
4119 pr_out_handle_start_arr(ctx
->dl
, tb
);
4120 pr_out_str(ctx
->dl
, "name", header
->name
);
4121 if (ctx
->dl
->verbose
) {
4122 pr_out_uint(ctx
->dl
, "id", header
->id
);
4123 pr_out_str(ctx
->dl
, "global",
4124 global
? "true" : "false");
4126 pr_out_array_start(ctx
->dl
, "field");
4127 pr_out_dpipe_fields(ctx
, header
->fields
,
4128 header
->fields_count
);
4129 pr_out_array_end(ctx
->dl
);
4130 pr_out_handle_end(ctx
->dl
);
4133 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4136 struct dpipe_header
*header
;
4138 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4139 pr_out_dpipe_header(ctx
, tb
, header
, false);
4141 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4142 pr_out_dpipe_header(ctx
, tb
, header
, true);
4145 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4147 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4151 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4152 if (err
!= MNL_CB_OK
)
4154 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4155 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4156 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4157 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4160 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4161 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4162 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4163 field
->name
= strdup(name
);
4166 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4170 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4171 struct dpipe_field
*fields
)
4173 struct nlattr
*nla_field
;
4177 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4178 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4186 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4188 struct nlattr
*nla_field
;
4189 unsigned int count
= 0;
4191 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4196 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4198 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4199 struct dpipe_header
*header
;
4200 unsigned int fields_count
;
4201 const char *header_name
;
4205 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4206 if (err
!= MNL_CB_OK
)
4209 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4210 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4211 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4214 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4215 header
= dpipe_header_alloc(fields_count
);
4219 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4220 header
->name
= strdup(header_name
);
4221 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4222 header
->fields_count
= fields_count
;
4223 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4225 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4229 dpipe_header_add(ctx
, header
, global
);
4233 dpipe_header_free(header
);
4237 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4239 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4240 struct nlattr
*nla_header
;
4243 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4244 err
= dpipe_header_get(ctx
, nla_header
);
4251 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4253 struct dpipe_ctx
*ctx
= data
;
4254 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4255 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4258 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4259 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4260 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4261 return MNL_CB_ERROR
;
4262 err
= dpipe_headers_get(ctx
, tb
);
4265 return MNL_CB_ERROR
;
4268 if (ctx
->print_headers
)
4269 pr_out_dpipe_headers(ctx
, tb
);
4273 static int cmd_dpipe_headers_show(struct dl
*dl
)
4275 struct nlmsghdr
*nlh
;
4276 struct dpipe_ctx ctx
= {};
4277 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4280 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4282 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4286 err
= dpipe_ctx_init(&ctx
, dl
);
4290 ctx
.print_headers
= true;
4292 pr_out_section_start(dl
, "header");
4293 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4295 pr_err("error get headers %s\n", strerror(ctx
.err
));
4296 pr_out_section_end(dl
);
4298 dpipe_ctx_fini(&ctx
);
4302 static void cmd_dpipe_header_help(void)
4304 pr_err("Usage: devlink dpipe headers show DEV\n");
4307 static int cmd_dpipe_header(struct dl
*dl
)
4309 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4310 cmd_dpipe_header_help();
4312 } else if (dl_argv_match(dl
, "show")) {
4314 return cmd_dpipe_headers_show(dl
);
4316 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4321 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4323 switch (action_type
) {
4324 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4325 return "field_modify";
4331 struct dpipe_op_info
{
4337 struct dpipe_action
{
4338 struct dpipe_op_info info
;
4342 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4343 struct dpipe_ctx
*ctx
)
4345 struct dpipe_op_info
*op_info
= &action
->info
;
4346 const char *mapping
;
4348 pr_out_str(ctx
->dl
, "type",
4349 dpipe_action_type_e2s(action
->type
));
4350 pr_out_str(ctx
->dl
, "header",
4351 dpipe_header_id2s(ctx
, op_info
->header_id
,
4352 op_info
->header_global
));
4353 pr_out_str(ctx
->dl
, "field",
4354 dpipe_field_id2s(ctx
, op_info
->header_id
,
4356 op_info
->header_global
));
4357 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4359 op_info
->header_global
);
4361 pr_out_str(ctx
->dl
, "mapping", mapping
);
4364 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4366 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4369 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4370 if (err
!= MNL_CB_OK
)
4373 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4374 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4375 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4376 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4380 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4381 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4382 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4383 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4388 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4389 struct nlattr
*nla_actions
)
4391 struct nlattr
*nla_action
;
4392 struct dpipe_action action
;
4394 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4395 pr_out_entry_start(ctx
->dl
);
4396 if (dpipe_action_parse(&action
, nla_action
))
4397 goto err_action_parse
;
4398 pr_out_dpipe_action(&action
, ctx
);
4399 pr_out_entry_end(ctx
->dl
);
4404 pr_out_entry_end(ctx
->dl
);
4409 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4411 switch (match_type
) {
4412 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4413 return "field_exact";
4419 struct dpipe_match
{
4420 struct dpipe_op_info info
;
4424 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4425 struct dpipe_ctx
*ctx
)
4427 struct dpipe_op_info
*op_info
= &match
->info
;
4428 const char *mapping
;
4430 pr_out_str(ctx
->dl
, "type",
4431 dpipe_match_type_e2s(match
->type
));
4432 pr_out_str(ctx
->dl
, "header",
4433 dpipe_header_id2s(ctx
, op_info
->header_id
,
4434 op_info
->header_global
));
4435 pr_out_str(ctx
->dl
, "field",
4436 dpipe_field_id2s(ctx
, op_info
->header_id
,
4438 op_info
->header_global
));
4439 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4441 op_info
->header_global
);
4443 pr_out_str(ctx
->dl
, "mapping", mapping
);
4446 static int dpipe_match_parse(struct dpipe_match
*match
,
4450 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4453 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4454 if (err
!= MNL_CB_OK
)
4457 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4458 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4459 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4460 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4464 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4465 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4466 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4467 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4472 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4473 struct nlattr
*nla_matches
)
4475 struct nlattr
*nla_match
;
4476 struct dpipe_match match
;
4478 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4479 pr_out_entry_start(ctx
->dl
);
4480 if (dpipe_match_parse(&match
, nla_match
))
4481 goto err_match_parse
;
4482 pr_out_dpipe_match(&match
, ctx
);
4483 pr_out_entry_end(ctx
->dl
);
4488 pr_out_entry_end(ctx
->dl
);
4492 static struct resource
*
4493 resource_find(struct resources
*resources
, struct resource
*resource
,
4494 uint64_t resource_id
)
4496 struct list_head
*list_head
;
4499 list_head
= &resources
->resource_list
;
4501 list_head
= &resource
->resource_list
;
4503 list_for_each_entry(resource
, list_head
, list
) {
4504 struct resource
*child_resource
;
4506 if (resource
->id
== resource_id
)
4509 child_resource
= resource_find(resources
, resource
,
4512 return child_resource
;
4518 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4519 uint64_t resource_id
)
4521 struct resource
*resource
, *parent_resource
;
4522 const char del
[] = "/";
4526 resource
= resource_find(resources
, NULL
, resource_id
);
4530 for (parent_resource
= resource
; parent_resource
;
4531 parent_resource
= parent_resource
->parent
)
4532 path_len
+= strlen(parent_resource
->name
) + 1;
4535 path
= calloc(1, path_len
);
4539 path
+= path_len
- 1;
4540 for (parent_resource
= resource
; parent_resource
;
4541 parent_resource
= parent_resource
->parent
) {
4542 path
-= strlen(parent_resource
->name
);
4543 memcpy(path
, parent_resource
->name
,
4544 strlen(parent_resource
->name
));
4545 path
-= strlen(del
);
4546 memcpy(path
, del
, strlen(del
));
4548 pr_out_str(dl
, "resource_path", path
);
4552 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4554 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4555 struct dpipe_table
*table
;
4556 uint32_t resource_units
;
4557 bool counters_enabled
;
4558 bool resource_valid
;
4562 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4563 if (err
!= MNL_CB_OK
)
4566 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4567 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4568 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4569 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4570 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4574 table
= dpipe_table_alloc();
4578 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4579 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4580 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4582 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4584 if (resource_valid
) {
4585 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4586 table
->resource_valid
= true;
4589 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4590 if (!ctx
->print_tables
)
4593 pr_out_str(ctx
->dl
, "name", table
->name
);
4594 pr_out_uint(ctx
->dl
, "size", size
);
4595 pr_out_str(ctx
->dl
, "counters_enabled",
4596 counters_enabled
? "true" : "false");
4598 if (resource_valid
) {
4599 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4600 resource_path_print(ctx
->dl
, ctx
->resources
,
4601 table
->resource_id
);
4602 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4605 pr_out_array_start(ctx
->dl
, "match");
4606 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4607 goto err_matches_show
;
4608 pr_out_array_end(ctx
->dl
);
4610 pr_out_array_start(ctx
->dl
, "action");
4611 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4612 goto err_actions_show
;
4613 pr_out_array_end(ctx
->dl
);
4619 pr_out_array_end(ctx
->dl
);
4623 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4625 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4626 struct nlattr
*nla_table
;
4628 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4629 if (ctx
->print_tables
)
4630 pr_out_handle_start_arr(ctx
->dl
, tb
);
4631 if (dpipe_table_show(ctx
, nla_table
))
4632 goto err_table_show
;
4633 if (ctx
->print_tables
)
4634 pr_out_handle_end(ctx
->dl
);
4639 if (ctx
->print_tables
)
4640 pr_out_handle_end(ctx
->dl
);
4644 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4646 struct dpipe_ctx
*ctx
= data
;
4647 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4648 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4650 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4651 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4652 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4653 return MNL_CB_ERROR
;
4655 if (dpipe_tables_show(ctx
, tb
))
4656 return MNL_CB_ERROR
;
4660 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4662 static int cmd_dpipe_table_show(struct dl
*dl
)
4664 struct nlmsghdr
*nlh
;
4665 struct dpipe_ctx dpipe_ctx
= {};
4666 struct resource_ctx resource_ctx
= {};
4667 uint16_t flags
= NLM_F_REQUEST
;
4670 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4674 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4676 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4680 dpipe_ctx
.print_tables
= true;
4682 dl_opts_put(nlh
, dl
);
4683 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4686 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4687 goto err_headers_get
;
4690 err
= resource_ctx_init(&resource_ctx
, dl
);
4692 goto err_resource_ctx_init
;
4694 resource_ctx
.print_resources
= false;
4695 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4696 dl_opts_put(nlh
, dl
);
4697 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4700 dpipe_ctx
.resources
= resource_ctx
.resources
;
4702 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4703 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4704 dl_opts_put(nlh
, dl
);
4706 pr_out_section_start(dl
, "table");
4707 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4708 pr_out_section_end(dl
);
4710 resource_ctx_fini(&resource_ctx
);
4711 dpipe_ctx_fini(&dpipe_ctx
);
4714 err_resource_ctx_init
:
4716 dpipe_ctx_fini(&dpipe_ctx
);
4720 static int cmd_dpipe_table_set(struct dl
*dl
)
4722 struct nlmsghdr
*nlh
;
4725 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4726 NLM_F_REQUEST
| NLM_F_ACK
);
4728 err
= dl_argv_parse_put(nlh
, dl
,
4729 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4730 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4734 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4737 enum dpipe_value_type
{
4738 DPIPE_VALUE_TYPE_VALUE
,
4739 DPIPE_VALUE_TYPE_MASK
,
4743 dpipe_value_type_e2s(enum dpipe_value_type type
)
4746 case DPIPE_VALUE_TYPE_VALUE
:
4748 case DPIPE_VALUE_TYPE_MASK
:
4749 return "value_mask";
4755 struct dpipe_field_printer
{
4756 unsigned int field_id
;
4757 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4760 struct dpipe_header_printer
{
4761 struct dpipe_field_printer
*printers
;
4762 unsigned int printers_count
;
4763 unsigned int header_id
;
4766 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4767 enum dpipe_value_type type
,
4770 struct in_addr ip_addr
;
4772 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4773 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4777 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4778 enum dpipe_value_type type
,
4781 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4782 ether_ntoa((struct ether_addr
*)value
));
4785 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4786 enum dpipe_value_type type
,
4789 char str
[INET6_ADDRSTRLEN
];
4791 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4792 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4795 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4797 .printer
= dpipe_field_printer_ipv4_addr
,
4798 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4802 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4803 .printers
= dpipe_field_printers_ipv4
,
4804 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4805 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4808 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4810 .printer
= dpipe_field_printer_ethernet_addr
,
4811 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4815 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4816 .printers
= dpipe_field_printers_ethernet
,
4817 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4818 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4821 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4823 .printer
= dpipe_field_printer_ipv6_addr
,
4824 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4828 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4829 .printers
= dpipe_field_printers_ipv6
,
4830 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4831 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4834 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4835 &dpipe_header_printer_ipv4
,
4836 &dpipe_header_printer_ethernet
,
4837 &dpipe_header_printer_ipv6
,
4840 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4841 struct dpipe_op_info
*info
,
4842 enum dpipe_value_type type
,
4845 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4846 struct dpipe_header_printer
*header_printer
;
4847 struct dpipe_field_printer
*field_printer
;
4848 unsigned int field_printers_count
;
4852 for (i
= 0; i
< header_printers_count
; i
++) {
4853 header_printer
= dpipe_header_printers
[i
];
4854 if (header_printer
->header_id
!= info
->header_id
)
4856 field_printers_count
= header_printer
->printers_count
;
4857 for (j
= 0; j
< field_printers_count
; j
++) {
4858 field_printer
= &header_printer
->printers
[j
];
4859 if (field_printer
->field_id
!= info
->field_id
)
4861 field_printer
->printer(ctx
, type
, value
);
4869 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4871 unsigned int value_len
,
4872 struct dpipe_op_info
*info
,
4873 enum dpipe_value_type type
)
4875 if (info
->header_global
&&
4876 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4879 if (value_len
== sizeof(uint32_t)) {
4880 uint32_t *value_32
= value
;
4882 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4886 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4887 struct nlattr
**nla_match_value
,
4888 struct dpipe_op_info
*info
)
4890 void *value
, *value_mask
;
4891 uint32_t value_mapping
;
4895 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4896 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4898 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4899 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4902 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4903 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4907 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4908 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4909 DPIPE_VALUE_TYPE_MASK
);
4912 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4915 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4918 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4919 struct dpipe_match match
;
4922 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4923 if (err
!= MNL_CB_OK
)
4926 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4927 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4931 pr_out_entry_start(ctx
->dl
);
4932 if (dpipe_match_parse(&match
,
4933 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4934 goto err_match_parse
;
4935 pr_out_dpipe_match(&match
, ctx
);
4936 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4937 pr_out_entry_end(ctx
->dl
);
4942 pr_out_entry_end(ctx
->dl
);
4946 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4949 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4950 struct dpipe_action action
;
4953 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4954 if (err
!= MNL_CB_OK
)
4957 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4958 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4962 pr_out_entry_start(ctx
->dl
);
4963 if (dpipe_action_parse(&action
,
4964 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4965 goto err_action_parse
;
4966 pr_out_dpipe_action(&action
, ctx
);
4967 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4968 pr_out_entry_end(ctx
->dl
);
4973 pr_out_entry_end(ctx
->dl
);
4978 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4979 struct nlattr
*nla_action_values
)
4981 struct nlattr
*nla_action_value
;
4983 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4984 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
4991 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
4992 struct nlattr
*nla_match_values
)
4994 struct nlattr
*nla_match_value
;
4996 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
4997 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5003 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5005 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5006 uint32_t entry_index
;
5010 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5011 if (err
!= MNL_CB_OK
)
5014 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5015 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5016 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5020 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5021 pr_out_uint(ctx
->dl
, "index", entry_index
);
5023 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5024 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5025 pr_out_uint(ctx
->dl
, "counter", counter
);
5028 pr_out_array_start(ctx
->dl
, "match_value");
5029 if (dpipe_tables_match_values_show(ctx
,
5030 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5031 goto err_match_values_show
;
5032 pr_out_array_end(ctx
->dl
);
5034 pr_out_array_start(ctx
->dl
, "action_value");
5035 if (dpipe_tables_action_values_show(ctx
,
5036 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5037 goto err_action_values_show
;
5038 pr_out_array_end(ctx
->dl
);
5041 err_action_values_show
:
5042 err_match_values_show
:
5043 pr_out_array_end(ctx
->dl
);
5047 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5049 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5050 struct nlattr
*nla_entry
;
5052 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5053 pr_out_handle_start_arr(ctx
->dl
, tb
);
5054 if (dpipe_entry_show(ctx
, nla_entry
))
5055 goto err_entry_show
;
5056 pr_out_handle_end(ctx
->dl
);
5061 pr_out_handle_end(ctx
->dl
);
5065 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5067 struct dpipe_ctx
*ctx
= data
;
5068 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5069 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5071 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5072 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5073 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5074 return MNL_CB_ERROR
;
5076 if (dpipe_table_entries_show(ctx
, tb
))
5077 return MNL_CB_ERROR
;
5081 static int cmd_dpipe_table_dump(struct dl
*dl
)
5083 struct nlmsghdr
*nlh
;
5084 struct dpipe_ctx ctx
= {};
5085 uint16_t flags
= NLM_F_REQUEST
;
5088 err
= dpipe_ctx_init(&ctx
, dl
);
5092 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5096 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5097 dl_opts_put(nlh
, dl
);
5098 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5100 pr_err("error get headers %s\n", strerror(ctx
.err
));
5104 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5105 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5106 dl_opts_put(nlh
, dl
);
5108 pr_out_section_start(dl
, "table_entry");
5109 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5110 pr_out_section_end(dl
);
5112 dpipe_ctx_fini(&ctx
);
5116 static void cmd_dpipe_table_help(void)
5118 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5119 "where OBJECT-LIST := { show | set | dump }\n");
5122 static int cmd_dpipe_table(struct dl
*dl
)
5124 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5125 cmd_dpipe_table_help();
5127 } else if (dl_argv_match(dl
, "show")) {
5129 return cmd_dpipe_table_show(dl
);
5130 } else if (dl_argv_match(dl
, "set")) {
5132 return cmd_dpipe_table_set(dl
);
5133 } else if (dl_argv_match(dl
, "dump")) {
5135 return cmd_dpipe_table_dump(dl
);
5137 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5141 static void cmd_dpipe_help(void)
5143 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5144 "where OBJECT-LIST := { header | table }\n");
5147 static int cmd_dpipe(struct dl
*dl
)
5149 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5152 } else if (dl_argv_match(dl
, "header")) {
5154 return cmd_dpipe_header(dl
);
5155 } else if (dl_argv_match(dl
, "table")) {
5157 return cmd_dpipe_table(dl
);
5159 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5164 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5165 struct nlattr
**nla_resource
)
5167 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5168 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5169 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5170 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5171 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5172 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5173 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5177 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5178 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5179 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5180 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5181 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5182 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5183 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5185 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5186 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5188 resource
->size_new
= resource
->size
;
5190 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5191 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5192 resource
->occ_valid
= true;
5195 if (resource
->size_new
!= resource
->size
)
5196 ctx
->pending_change
= true;
5202 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5203 struct resource
*parent_resource
, struct nlattr
*nl
)
5205 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5206 struct nlattr
*nla_child_resource
;
5207 struct nlattr
*nla_resources
;
5217 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5218 if (err
!= MNL_CB_OK
)
5221 err
= resource_parse(ctx
, resource
, nla_resource
);
5225 resource
->parent
= parent_resource
;
5226 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5229 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5230 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5232 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5233 struct resource
*child_resource
;
5234 struct list_head
*list
;
5236 child_resource
= resource_alloc();
5237 if (!child_resource
)
5241 list
= &ctx
->resources
->resource_list
;
5243 list
= &resource
->resource_list
;
5245 list_add_tail(&child_resource
->list
, list
);
5246 err
= resource_get(ctx
, child_resource
, resource
,
5247 nla_child_resource
);
5255 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5258 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5259 default: return "<unknown unit>";
5263 static void resource_show(struct resource
*resource
,
5264 struct resource_ctx
*ctx
)
5266 struct resource
*child_resource
;
5267 struct dpipe_table
*table
;
5268 struct dl
*dl
= ctx
->dl
;
5271 pr_out_str(dl
, "name", resource
->name
);
5273 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5274 pr_out_u64(dl
, "size", resource
->size
);
5275 if (resource
->size
!= resource
->size_new
)
5276 pr_out_u64(dl
, "size_new", resource
->size_new
);
5277 if (resource
->occ_valid
)
5278 pr_out_uint(dl
, "occ", resource
->size_occ
);
5279 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5281 if (resource
->size_min
!= resource
->size_max
) {
5282 pr_out_uint(dl
, "size_min", resource
->size_min
);
5283 pr_out_u64(dl
, "size_max", resource
->size_max
);
5284 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5287 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5288 if (table
->resource_id
== resource
->id
&&
5289 table
->resource_valid
)
5293 pr_out_array_start(dl
, "dpipe_tables");
5295 pr_out_str(dl
, "dpipe_tables", "none");
5297 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5298 if (table
->resource_id
!= resource
->id
||
5299 !table
->resource_valid
)
5301 pr_out_entry_start(dl
);
5302 pr_out_str(dl
, "table_name", table
->name
);
5303 pr_out_entry_end(dl
);
5306 pr_out_array_end(dl
);
5308 if (list_empty(&resource
->resource_list
))
5311 if (ctx
->pending_change
)
5312 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5314 pr_out_array_start(dl
, "resources");
5315 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5316 pr_out_entry_start(dl
);
5317 resource_show(child_resource
, ctx
);
5318 pr_out_entry_end(dl
);
5320 pr_out_array_end(dl
);
5324 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5326 struct resources
*resources
= ctx
->resources
;
5327 struct resource
*resource
;
5329 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5330 pr_out_handle_start_arr(ctx
->dl
, tb
);
5331 resource_show(resource
, ctx
);
5332 pr_out_handle_end(ctx
->dl
);
5336 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5338 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5341 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5343 struct resource_ctx
*ctx
= data
;
5344 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5345 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5348 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5349 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5350 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5351 return MNL_CB_ERROR
;
5353 err
= resources_get(ctx
, tb
);
5356 return MNL_CB_ERROR
;
5359 if (ctx
->print_resources
)
5360 resources_show(ctx
, tb
);
5365 static int cmd_resource_show(struct dl
*dl
)
5367 struct nlmsghdr
*nlh
;
5368 struct dpipe_ctx dpipe_ctx
= {};
5369 struct resource_ctx resource_ctx
= {};
5372 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5376 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5378 dl_opts_put(nlh
, dl
);
5380 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5384 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5387 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5391 err
= resource_ctx_init(&resource_ctx
, dl
);
5395 resource_ctx
.print_resources
= true;
5396 resource_ctx
.tables
= dpipe_ctx
.tables
;
5397 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5398 NLM_F_REQUEST
| NLM_F_ACK
);
5399 dl_opts_put(nlh
, dl
);
5400 pr_out_section_start(dl
, "resources");
5401 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5403 pr_out_section_end(dl
);
5404 resource_ctx_fini(&resource_ctx
);
5406 dpipe_ctx_fini(&dpipe_ctx
);
5410 static void cmd_resource_help(void)
5412 pr_err("Usage: devlink resource show DEV\n"
5413 " devlink resource set DEV path PATH size SIZE\n");
5416 static struct resource
*
5417 resource_find_by_name(struct list_head
*list
, char *name
)
5419 struct resource
*resource
;
5421 list_for_each_entry(resource
, list
, list
) {
5422 if (!strcmp(resource
->name
, name
))
5429 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5430 uint32_t *p_resource_id
, bool *p_resource_valid
)
5432 struct resource
*resource
;
5433 uint32_t resource_id
= 0;
5434 char *resource_path_dup
;
5435 struct list_head
*list
;
5436 const char del
[] = "/";
5437 char *resource_name
;
5439 resource_path_dup
= strdup(resource_path
);
5440 list
= &ctx
->resources
->resource_list
;
5441 resource_name
= strtok(resource_path_dup
, del
);
5442 while (resource_name
!= NULL
) {
5443 resource
= resource_find_by_name(list
, resource_name
);
5445 goto err_resource_lookup
;
5447 list
= &resource
->resource_list
;
5448 resource_name
= strtok(NULL
, del
);
5449 resource_id
= resource
->id
;
5451 free(resource_path_dup
);
5452 *p_resource_valid
= true;
5453 *p_resource_id
= resource_id
;
5456 err_resource_lookup
:
5457 free(resource_path_dup
);
5461 static int cmd_resource_set(struct dl
*dl
)
5463 struct nlmsghdr
*nlh
;
5464 struct resource_ctx ctx
= {};
5467 err
= resource_ctx_init(&ctx
, dl
);
5471 ctx
.print_resources
= false;
5472 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5473 DL_OPT_RESOURCE_SIZE
, 0);
5477 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5479 dl_opts_put(nlh
, dl
);
5480 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5482 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5486 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5487 &dl
->opts
.resource_id
,
5488 &dl
->opts
.resource_id_valid
);
5490 pr_err("error parsing resource path %s\n", strerror(-err
));
5494 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5495 NLM_F_REQUEST
| NLM_F_ACK
);
5497 dl_opts_put(nlh
, dl
);
5498 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5500 resource_ctx_fini(&ctx
);
5504 static int cmd_resource(struct dl
*dl
)
5506 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5507 cmd_resource_help();
5509 } else if (dl_argv_match(dl
, "show")) {
5511 return cmd_resource_show(dl
);
5512 } else if (dl_argv_match(dl
, "set")) {
5514 return cmd_resource_set(dl
);
5516 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5520 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5522 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5523 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5524 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5527 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5528 if (dl
->json_output
) {
5529 jsonw_name(dl
->jw
, buf
);
5530 jsonw_start_object(dl
->jw
);
5536 static void pr_out_region_handle_end(struct dl
*dl
)
5538 if (dl
->json_output
)
5539 jsonw_end_object(dl
->jw
);
5544 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5546 if (dl
->json_output
) {
5547 jsonw_name(dl
->jw
, "snapshot");
5548 jsonw_start_array(dl
->jw
);
5550 if (g_indent_newline
)
5551 pr_out("snapshot %s", array
? "[" : "");
5553 pr_out(" snapshot %s", array
? "[" : "");
5557 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5559 if (dl
->json_output
)
5560 jsonw_end_array(dl
->jw
);
5565 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5567 uint32_t snapshot_id
;
5569 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5572 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5574 if (dl
->json_output
)
5575 jsonw_uint(dl
->jw
, snapshot_id
);
5577 pr_out("%s%u", index
? " " : "", snapshot_id
);
5580 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5582 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5583 struct nlattr
*nla_sanpshot
;
5586 pr_out_region_snapshots_start(dl
, true);
5587 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5588 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5589 if (err
!= MNL_CB_OK
)
5591 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5593 pr_out_region_snapshots_end(dl
, true);
5596 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5598 pr_out_region_snapshots_start(dl
, false);
5599 pr_out_region_snapshots_id(dl
, tb
, 0);
5600 pr_out_region_snapshots_end(dl
, false);
5603 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5605 pr_out_region_handle_start(dl
, tb
);
5607 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5608 pr_out_u64(dl
, "size",
5609 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5611 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5612 pr_out_snapshots(dl
, tb
);
5614 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5615 pr_out_snapshot(dl
, tb
);
5617 pr_out_region_handle_end(dl
);
5620 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5622 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5623 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5624 struct dl
*dl
= data
;
5626 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5627 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5628 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5629 return MNL_CB_ERROR
;
5631 pr_out_region(dl
, tb
);
5636 static int cmd_region_show(struct dl
*dl
)
5638 struct nlmsghdr
*nlh
;
5639 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5642 if (dl_argc(dl
) == 0)
5643 flags
|= NLM_F_DUMP
;
5645 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5647 if (dl_argc(dl
) > 0) {
5648 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5653 pr_out_section_start(dl
, "regions");
5654 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5655 pr_out_section_end(dl
);
5659 static int cmd_region_snapshot_del(struct dl
*dl
)
5661 struct nlmsghdr
*nlh
;
5664 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5665 NLM_F_REQUEST
| NLM_F_ACK
);
5667 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5668 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5672 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5675 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5677 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5678 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5679 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5680 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5681 struct dl
*dl
= data
;
5684 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5685 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5686 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5687 return MNL_CB_ERROR
;
5689 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5690 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5691 if (err
!= MNL_CB_OK
)
5692 return MNL_CB_ERROR
;
5694 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5695 if (!nla_chunk_data
)
5698 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5699 if (!nla_chunk_addr
)
5702 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5703 mnl_attr_get_payload_len(nla_chunk_data
),
5704 mnl_attr_get_u64(nla_chunk_addr
));
5709 static int cmd_region_dump(struct dl
*dl
)
5711 struct nlmsghdr
*nlh
;
5714 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5715 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5717 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5718 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5722 pr_out_section_start(dl
, "dump");
5723 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5724 pr_out_section_end(dl
);
5725 if (!dl
->json_output
)
5730 static int cmd_region_read(struct dl
*dl
)
5732 struct nlmsghdr
*nlh
;
5735 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5736 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5738 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5739 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5740 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5744 pr_out_section_start(dl
, "read");
5745 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5746 pr_out_section_end(dl
);
5747 if (!dl
->json_output
)
5752 static void cmd_region_help(void)
5754 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5755 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5756 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5757 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5760 static int cmd_region(struct dl
*dl
)
5762 if (dl_no_arg(dl
)) {
5763 return cmd_region_show(dl
);
5764 } else if (dl_argv_match(dl
, "help")) {
5767 } else if (dl_argv_match(dl
, "show")) {
5769 return cmd_region_show(dl
);
5770 } else if (dl_argv_match(dl
, "del")) {
5772 return cmd_region_snapshot_del(dl
);
5773 } else if (dl_argv_match(dl
, "dump")) {
5775 return cmd_region_dump(dl
);
5776 } else if (dl_argv_match(dl
, "read")) {
5778 return cmd_region_read(dl
);
5780 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5784 static void help(void)
5786 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5787 " devlink [ -f[orce] ] -b[atch] filename\n"
5788 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5789 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5792 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5797 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5800 } else if (dl_argv_match(dl
, "dev")) {
5803 } else if (dl_argv_match(dl
, "port")) {
5805 return cmd_port(dl
);
5806 } else if (dl_argv_match(dl
, "sb")) {
5809 } else if (dl_argv_match(dl
, "monitor")) {
5812 } else if (dl_argv_match(dl
, "dpipe")) {
5814 return cmd_dpipe(dl
);
5815 } else if (dl_argv_match(dl
, "resource")) {
5817 return cmd_resource(dl
);
5818 } else if (dl_argv_match(dl
, "region")) {
5820 return cmd_region(dl
);
5822 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5826 static int dl_init(struct dl
*dl
)
5830 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5832 pr_err("Failed to connect to devlink Netlink\n");
5836 err
= ifname_map_init(dl
);
5838 pr_err("Failed to create index map\n");
5839 goto err_ifname_map_create
;
5841 if (dl
->json_output
) {
5842 dl
->jw
= jsonw_new(stdout
);
5844 pr_err("Failed to create JSON writer\n");
5847 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5852 ifname_map_fini(dl
);
5853 err_ifname_map_create
:
5854 mnlg_socket_close(dl
->nlg
);
5858 static void dl_fini(struct dl
*dl
)
5860 if (dl
->json_output
)
5861 jsonw_destroy(&dl
->jw
);
5862 ifname_map_fini(dl
);
5863 mnlg_socket_close(dl
->nlg
);
5866 static struct dl
*dl_alloc(void)
5870 dl
= calloc(1, sizeof(*dl
));
5876 static void dl_free(struct dl
*dl
)
5881 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5885 int ret
= EXIT_SUCCESS
;
5887 if (name
&& strcmp(name
, "-") != 0) {
5888 if (freopen(name
, "r", stdin
) == NULL
) {
5890 "Cannot open file \"%s\" for reading: %s\n",
5891 name
, strerror(errno
));
5892 return EXIT_FAILURE
;
5897 while (getcmdline(&line
, &len
, stdin
) != -1) {
5901 largc
= makeargs(line
, largv
, 100);
5903 continue; /* blank line */
5905 if (dl_cmd(dl
, largc
, largv
)) {
5906 fprintf(stderr
, "Command failed %s:%d\n",
5920 int main(int argc
, char **argv
)
5922 static const struct option long_options
[] = {
5923 { "Version", no_argument
, NULL
, 'V' },
5924 { "force", no_argument
, NULL
, 'f' },
5925 { "batch", required_argument
, NULL
, 'b' },
5926 { "no-nice-names", no_argument
, NULL
, 'n' },
5927 { "json", no_argument
, NULL
, 'j' },
5928 { "pretty", no_argument
, NULL
, 'p' },
5929 { "verbose", no_argument
, NULL
, 'v' },
5930 { NULL
, 0, NULL
, 0 }
5932 const char *batch_file
= NULL
;
5941 pr_err("Failed to allocate memory for devlink\n");
5942 return EXIT_FAILURE
;
5945 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5946 long_options
, NULL
)) >= 0) {
5950 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5957 batch_file
= optarg
;
5960 dl
->no_nice_names
= true;
5963 dl
->json_output
= true;
5966 dl
->pretty_output
= true;
5972 pr_err("Unknown option.\n");
5989 err
= dl_batch(dl
, batch_file
, force
);
5991 err
= dl_cmd(dl
, argc
, argv
);