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)
204 uint32_t present
; /* flags of present items */
208 enum devlink_port_type port_type
;
211 uint16_t sb_pool_index
;
212 uint32_t sb_pool_size
;
213 enum devlink_sb_pool_type sb_pool_type
;
214 enum devlink_sb_threshold_type sb_pool_thtype
;
215 uint32_t sb_threshold
;
216 uint16_t sb_tc_index
;
217 enum devlink_eswitch_mode eswitch_mode
;
218 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
219 const char *dpipe_table_name
;
220 bool dpipe_counters_enable
;
221 bool eswitch_encap_mode
;
222 const char *resource_path
;
223 uint32_t resource_size
;
224 uint32_t resource_id
;
225 bool resource_id_valid
;
226 const char *param_name
;
227 const char *param_value
;
228 enum devlink_param_cmode cmode
;
230 uint32_t region_snapshot_id
;
231 uint64_t region_address
;
232 uint64_t region_length
;
236 struct mnlg_socket
*nlg
;
237 struct list_head ifname_map_list
;
254 static int dl_argc(struct dl
*dl
)
259 static char *dl_argv(struct dl
*dl
)
261 if (dl_argc(dl
) == 0)
266 static void dl_arg_inc(struct dl
*dl
)
268 if (dl_argc(dl
) == 0)
274 static char *dl_argv_next(struct dl
*dl
)
278 if (dl_argc(dl
) == 0)
286 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
288 if (index
>= dl_argc(dl
))
290 return dl
->argv
[index
];
293 static int strcmpx(const char *str1
, const char *str2
)
295 if (strlen(str1
) > strlen(str2
))
297 return strncmp(str1
, str2
, strlen(str1
));
300 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
302 if (dl_argc(dl
) == 0)
304 return strcmpx(dl_argv(dl
), pattern
) == 0;
307 static bool dl_no_arg(struct dl
*dl
)
309 return dl_argc(dl
) == 0;
312 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
313 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
314 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
315 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
316 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
317 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
318 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
319 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
320 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
321 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
322 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
323 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
324 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
325 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
326 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
327 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
328 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
329 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
330 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
331 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
332 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
333 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
334 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
335 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
336 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
337 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
338 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
339 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
340 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
341 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
342 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
343 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
344 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
345 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
346 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
347 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
348 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
349 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
350 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
351 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
352 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
353 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
354 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
355 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
356 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
357 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
358 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
359 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
360 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
361 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
362 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
363 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
364 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
365 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
366 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
367 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
368 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
369 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
370 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
371 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
372 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
373 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
374 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
375 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
376 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
377 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
378 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
379 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
380 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
381 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
382 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
383 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
384 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
385 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
388 static int attr_cb(const struct nlattr
*attr
, void *data
)
390 const struct nlattr
**tb
= data
;
393 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
396 type
= mnl_attr_get_type(attr
);
397 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
404 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
406 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
407 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
408 struct dl
*dl
= data
;
409 struct ifname_map
*ifname_map
;
410 const char *bus_name
;
411 const char *dev_name
;
412 uint32_t port_ifindex
;
413 const char *port_ifname
;
415 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
416 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
417 !tb
[DEVLINK_ATTR_PORT_INDEX
])
420 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
423 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
424 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
425 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
426 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
427 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
428 port_ifindex
, port_ifname
);
431 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
436 static void ifname_map_fini(struct dl
*dl
)
438 struct ifname_map
*ifname_map
, *tmp
;
440 list_for_each_entry_safe(ifname_map
, tmp
,
441 &dl
->ifname_map_list
, list
) {
442 list_del(&ifname_map
->list
);
443 ifname_map_free(ifname_map
);
447 static int ifname_map_init(struct dl
*dl
)
449 struct nlmsghdr
*nlh
;
452 INIT_LIST_HEAD(&dl
->ifname_map_list
);
454 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
455 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
457 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
465 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
466 char **p_bus_name
, char **p_dev_name
,
467 uint32_t *p_port_index
)
469 struct ifname_map
*ifname_map
;
471 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
472 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
473 *p_bus_name
= ifname_map
->bus_name
;
474 *p_dev_name
= ifname_map
->dev_name
;
475 *p_port_index
= ifname_map
->port_index
;
482 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
483 const char *dev_name
, uint32_t port_index
,
486 struct ifname_map
*ifname_map
;
488 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
489 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
490 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
491 port_index
== ifname_map
->port_index
) {
492 *p_ifname
= ifname_map
->ifname
;
499 static unsigned int strslashcount(char *str
)
501 unsigned int count
= 0;
504 while ((pos
= strchr(pos
, '/'))) {
511 static int strslashrsplit(char *str
, char **before
, char **after
)
515 slash
= strrchr(str
, '/');
524 static int strtouint64_t(const char *str
, uint64_t *p_val
)
527 unsigned long long int val
;
529 val
= strtoull(str
, &endptr
, 10);
530 if (endptr
== str
|| *endptr
!= '\0')
538 static int strtouint32_t(const char *str
, uint32_t *p_val
)
541 unsigned long int val
;
543 val
= strtoul(str
, &endptr
, 10);
544 if (endptr
== str
|| *endptr
!= '\0')
552 static int strtouint16_t(const char *str
, uint16_t *p_val
)
555 unsigned long int val
;
557 val
= strtoul(str
, &endptr
, 10);
558 if (endptr
== str
|| *endptr
!= '\0')
566 static int strtouint8_t(const char *str
, uint8_t *p_val
)
569 unsigned long int val
;
571 val
= strtoul(str
, &endptr
, 10);
572 if (endptr
== str
|| *endptr
!= '\0')
580 static int strtobool(const char *str
, bool *p_val
)
584 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
586 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
594 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
596 strslashrsplit(str
, p_bus_name
, p_dev_name
);
600 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
602 char *str
= dl_argv_next(dl
);
605 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
608 if (strslashcount(str
) != 1) {
609 pr_err("Wrong devlink identification string format.\n");
610 pr_err("Expected \"bus_name/dev_name\".\n");
613 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
616 static int __dl_argv_handle_port(char *str
,
617 char **p_bus_name
, char **p_dev_name
,
618 uint32_t *p_port_index
)
624 err
= strslashrsplit(str
, &handlestr
, &portstr
);
626 pr_err("Port identification \"%s\" is invalid\n", str
);
629 err
= strtouint32_t(portstr
, p_port_index
);
631 pr_err("Port index \"%s\" is not a number or not within range\n",
635 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
637 pr_err("Port identification \"%s\" is invalid\n", str
);
643 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
644 char **p_bus_name
, char **p_dev_name
,
645 uint32_t *p_port_index
)
649 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
652 pr_err("Netdevice \"%s\" not found\n", str
);
658 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
659 char **p_dev_name
, uint32_t *p_port_index
)
661 char *str
= dl_argv_next(dl
);
662 unsigned int slash_count
;
665 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
668 slash_count
= strslashcount(str
);
669 switch (slash_count
) {
671 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
672 p_dev_name
, p_port_index
);
674 return __dl_argv_handle_port(str
, p_bus_name
,
675 p_dev_name
, p_port_index
);
677 pr_err("Wrong port identification string format.\n");
678 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
683 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
684 char **p_dev_name
, uint32_t *p_port_index
,
685 uint32_t *p_handle_bit
)
687 char *str
= dl_argv_next(dl
);
688 unsigned int slash_count
;
692 pr_err("One of following identifications expected:\n"
693 "Devlink identification (\"bus_name/dev_name\")\n"
694 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
697 slash_count
= strslashcount(str
);
698 if (slash_count
== 1) {
699 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
702 *p_handle_bit
= DL_OPT_HANDLE
;
703 } else if (slash_count
== 2) {
704 err
= __dl_argv_handle_port(str
, p_bus_name
,
705 p_dev_name
, p_port_index
);
708 *p_handle_bit
= DL_OPT_HANDLEP
;
709 } else if (slash_count
== 0) {
710 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
711 p_dev_name
, p_port_index
);
714 *p_handle_bit
= DL_OPT_HANDLEP
;
716 pr_err("Wrong port identification string format.\n");
717 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
723 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
724 char **p_dev_name
, char **p_region
)
729 err
= strslashrsplit(str
, &handlestr
, p_region
);
731 pr_err("Region identification \"%s\" is invalid\n", str
);
734 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
736 pr_err("Region identification \"%s\" is invalid\n", str
);
742 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
743 char **p_dev_name
, char **p_region
)
745 char *str
= dl_argv_next(dl
);
746 unsigned int slash_count
;
749 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
753 slash_count
= strslashcount(str
);
754 if (slash_count
!= 2) {
755 pr_err("Wrong region identification string format.\n");
756 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
760 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
763 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
765 char *str
= dl_argv_next(dl
);
769 pr_err("Unsigned number argument expected\n");
773 err
= strtouint64_t(str
, p_val
);
775 pr_err("\"%s\" is not a number or not within range\n", str
);
781 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
783 char *str
= dl_argv_next(dl
);
787 pr_err("Unsigned number argument expected\n");
791 err
= strtouint32_t(str
, p_val
);
793 pr_err("\"%s\" is not a number or not within range\n", str
);
799 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
801 char *str
= dl_argv_next(dl
);
805 pr_err("Unsigned number argument expected\n");
809 err
= strtouint16_t(str
, p_val
);
811 pr_err("\"%s\" is not a number or not within range\n", str
);
817 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
819 const char *str
= dl_argv_next(dl
);
822 pr_err("String parameter expected\n");
829 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
831 if (strcmp(typestr
, "auto") == 0) {
832 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
833 } else if (strcmp(typestr
, "eth") == 0) {
834 *p_type
= DEVLINK_PORT_TYPE_ETH
;
835 } else if (strcmp(typestr
, "ib") == 0) {
836 *p_type
= DEVLINK_PORT_TYPE_IB
;
838 pr_err("Unknown port type \"%s\"\n", typestr
);
844 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
846 if (strcmp(typestr
, "ingress") == 0) {
847 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
848 } else if (strcmp(typestr
, "egress") == 0) {
849 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
851 pr_err("Unknown pool type \"%s\"\n", typestr
);
857 static int threshold_type_get(const char *typestr
,
858 enum devlink_sb_threshold_type
*p_type
)
860 if (strcmp(typestr
, "static") == 0) {
861 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
862 } else if (strcmp(typestr
, "dynamic") == 0) {
863 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
865 pr_err("Unknown threshold type \"%s\"\n", typestr
);
871 static int eswitch_mode_get(const char *typestr
,
872 enum devlink_eswitch_mode
*p_mode
)
874 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
875 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
876 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
877 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
879 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
885 static int eswitch_inline_mode_get(const char *typestr
,
886 enum devlink_eswitch_inline_mode
*p_mode
)
888 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
889 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
890 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
891 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
892 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
893 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
894 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
895 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
897 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
903 static int dpipe_counters_enable_get(const char *typestr
,
904 bool *counters_enable
)
906 if (strcmp(typestr
, "enable") == 0) {
907 *counters_enable
= 1;
908 } else if (strcmp(typestr
, "disable") == 0) {
909 *counters_enable
= 0;
911 pr_err("Unknown counter_state \"%s\"\n", typestr
);
917 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
919 if (strcmp(typestr
, "enable") == 0) {
921 } else if (strcmp(typestr
, "disable") == 0) {
924 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
930 static int param_cmode_get(const char *cmodestr
,
931 enum devlink_param_cmode
*cmode
)
933 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
934 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
935 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
936 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
937 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
938 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
940 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
946 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
949 struct dl_opts
*opts
= &dl
->opts
;
950 uint32_t o_all
= o_required
| o_optional
;
951 uint32_t o_found
= 0;
954 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
957 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
958 &opts
->port_index
, &handle_bit
);
961 o_found
|= handle_bit
;
962 } else if (o_required
& DL_OPT_HANDLE
) {
963 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
966 o_found
|= DL_OPT_HANDLE
;
967 } else if (o_required
& DL_OPT_HANDLEP
) {
968 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
972 o_found
|= DL_OPT_HANDLEP
;
973 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
974 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
979 o_found
|= DL_OPT_HANDLE_REGION
;
982 while (dl_argc(dl
)) {
983 if (dl_argv_match(dl
, "type") &&
984 (o_all
& DL_OPT_PORT_TYPE
)) {
988 err
= dl_argv_str(dl
, &typestr
);
991 err
= port_type_get(typestr
, &opts
->port_type
);
994 o_found
|= DL_OPT_PORT_TYPE
;
995 } else if (dl_argv_match(dl
, "count") &&
996 (o_all
& DL_OPT_PORT_COUNT
)) {
998 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1001 o_found
|= DL_OPT_PORT_COUNT
;
1002 } else if (dl_argv_match(dl
, "sb") &&
1003 (o_all
& DL_OPT_SB
)) {
1005 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1008 o_found
|= DL_OPT_SB
;
1009 } else if (dl_argv_match(dl
, "pool") &&
1010 (o_all
& DL_OPT_SB_POOL
)) {
1012 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1015 o_found
|= DL_OPT_SB_POOL
;
1016 } else if (dl_argv_match(dl
, "size") &&
1017 (o_all
& DL_OPT_SB_SIZE
)) {
1019 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1022 o_found
|= DL_OPT_SB_SIZE
;
1023 } else if (dl_argv_match(dl
, "type") &&
1024 (o_all
& DL_OPT_SB_TYPE
)) {
1025 const char *typestr
;
1028 err
= dl_argv_str(dl
, &typestr
);
1031 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1034 o_found
|= DL_OPT_SB_TYPE
;
1035 } else if (dl_argv_match(dl
, "thtype") &&
1036 (o_all
& DL_OPT_SB_THTYPE
)) {
1037 const char *typestr
;
1040 err
= dl_argv_str(dl
, &typestr
);
1043 err
= threshold_type_get(typestr
,
1044 &opts
->sb_pool_thtype
);
1047 o_found
|= DL_OPT_SB_THTYPE
;
1048 } else if (dl_argv_match(dl
, "th") &&
1049 (o_all
& DL_OPT_SB_TH
)) {
1051 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1054 o_found
|= DL_OPT_SB_TH
;
1055 } else if (dl_argv_match(dl
, "tc") &&
1056 (o_all
& DL_OPT_SB_TC
)) {
1058 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1061 o_found
|= DL_OPT_SB_TC
;
1062 } else if (dl_argv_match(dl
, "mode") &&
1063 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1064 const char *typestr
;
1067 err
= dl_argv_str(dl
, &typestr
);
1070 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1073 o_found
|= DL_OPT_ESWITCH_MODE
;
1074 } else if (dl_argv_match(dl
, "inline-mode") &&
1075 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1076 const char *typestr
;
1079 err
= dl_argv_str(dl
, &typestr
);
1082 err
= eswitch_inline_mode_get(
1083 typestr
, &opts
->eswitch_inline_mode
);
1086 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1087 } else if (dl_argv_match(dl
, "name") &&
1088 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1090 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1093 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1094 } else if (dl_argv_match(dl
, "counters") &&
1095 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1096 const char *typestr
;
1099 err
= dl_argv_str(dl
, &typestr
);
1102 err
= dpipe_counters_enable_get(typestr
,
1103 &opts
->dpipe_counters_enable
);
1106 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1107 } else if (dl_argv_match(dl
, "encap") &&
1108 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1109 const char *typestr
;
1112 err
= dl_argv_str(dl
, &typestr
);
1115 err
= eswitch_encap_mode_get(typestr
,
1116 &opts
->eswitch_encap_mode
);
1119 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1120 } else if (dl_argv_match(dl
, "path") &&
1121 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1123 err
= dl_argv_str(dl
, &opts
->resource_path
);
1126 o_found
|= DL_OPT_RESOURCE_PATH
;
1127 } else if (dl_argv_match(dl
, "size") &&
1128 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1130 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1133 o_found
|= DL_OPT_RESOURCE_SIZE
;
1134 } else if (dl_argv_match(dl
, "name") &&
1135 (o_all
& DL_OPT_PARAM_NAME
)) {
1137 err
= dl_argv_str(dl
, &opts
->param_name
);
1140 o_found
|= DL_OPT_PARAM_NAME
;
1141 } else if (dl_argv_match(dl
, "value") &&
1142 (o_all
& DL_OPT_PARAM_VALUE
)) {
1144 err
= dl_argv_str(dl
, &opts
->param_value
);
1147 o_found
|= DL_OPT_PARAM_VALUE
;
1148 } else if (dl_argv_match(dl
, "cmode") &&
1149 (o_all
& DL_OPT_PARAM_CMODE
)) {
1150 const char *cmodestr
;
1153 err
= dl_argv_str(dl
, &cmodestr
);
1156 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1159 o_found
|= DL_OPT_PARAM_CMODE
;
1160 } else if (dl_argv_match(dl
, "snapshot") &&
1161 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1163 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1166 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1167 } else if (dl_argv_match(dl
, "address") &&
1168 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1170 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1173 o_found
|= DL_OPT_REGION_ADDRESS
;
1174 } else if (dl_argv_match(dl
, "length") &&
1175 (o_all
& DL_OPT_REGION_LENGTH
)) {
1177 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1180 o_found
|= DL_OPT_REGION_LENGTH
;
1182 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1187 opts
->present
= o_found
;
1189 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1191 opts
->present
|= DL_OPT_SB
;
1194 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
1195 pr_err("Port type option expected.\n");
1199 if ((o_required
& DL_OPT_PORT_COUNT
) &&
1200 !(o_found
& DL_OPT_PORT_COUNT
)) {
1201 pr_err("Port split count option expected.\n");
1205 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
1206 pr_err("Pool index option expected.\n");
1210 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
1211 pr_err("Pool size option expected.\n");
1215 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
1216 pr_err("Pool type option expected.\n");
1220 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
1221 pr_err("Pool threshold type option expected.\n");
1225 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
1226 pr_err("Threshold option expected.\n");
1230 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
1231 pr_err("TC index option expected.\n");
1235 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1236 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1237 pr_err("E-Switch mode option expected.\n");
1241 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1242 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1243 pr_err("E-Switch inline-mode option expected.\n");
1247 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1248 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1249 pr_err("Dpipe table name expected\n");
1253 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1254 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1255 pr_err("Dpipe table counter state expected\n");
1259 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1260 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1261 pr_err("E-Switch encapsulation option expected.\n");
1265 if ((o_required
& DL_OPT_PARAM_NAME
) &&
1266 !(o_found
& DL_OPT_PARAM_NAME
)) {
1267 pr_err("Parameter name expected.\n");
1271 if ((o_required
& DL_OPT_PARAM_VALUE
) &&
1272 !(o_found
& DL_OPT_PARAM_VALUE
)) {
1273 pr_err("Value to set expected.\n");
1277 if ((o_required
& DL_OPT_PARAM_CMODE
) &&
1278 !(o_found
& DL_OPT_PARAM_CMODE
)) {
1279 pr_err("Configuration mode expected.\n");
1283 if ((o_required
& DL_OPT_REGION_SNAPSHOT_ID
) &&
1284 !(o_found
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1285 pr_err("Region snapshot id expected.\n");
1289 if ((o_required
& DL_OPT_REGION_ADDRESS
) &&
1290 !(o_found
& DL_OPT_REGION_ADDRESS
)) {
1291 pr_err("Region address value expected.\n");
1295 if ((o_required
& DL_OPT_REGION_LENGTH
) &&
1296 !(o_found
& DL_OPT_REGION_LENGTH
)) {
1297 pr_err("Region length value expected.\n");
1304 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1306 struct dl_opts
*opts
= &dl
->opts
;
1308 if (opts
->present
& DL_OPT_HANDLE
) {
1309 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1310 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1311 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1312 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1313 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1314 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1316 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1317 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1318 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1319 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1322 if (opts
->present
& DL_OPT_PORT_TYPE
)
1323 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1325 if (opts
->present
& DL_OPT_PORT_COUNT
)
1326 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1328 if (opts
->present
& DL_OPT_SB
)
1329 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1331 if (opts
->present
& DL_OPT_SB_POOL
)
1332 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1333 opts
->sb_pool_index
);
1334 if (opts
->present
& DL_OPT_SB_SIZE
)
1335 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1336 opts
->sb_pool_size
);
1337 if (opts
->present
& DL_OPT_SB_TYPE
)
1338 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1339 opts
->sb_pool_type
);
1340 if (opts
->present
& DL_OPT_SB_THTYPE
)
1341 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1342 opts
->sb_pool_thtype
);
1343 if (opts
->present
& DL_OPT_SB_TH
)
1344 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1345 opts
->sb_threshold
);
1346 if (opts
->present
& DL_OPT_SB_TC
)
1347 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1349 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1350 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1351 opts
->eswitch_mode
);
1352 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1353 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1354 opts
->eswitch_inline_mode
);
1355 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1356 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1357 opts
->dpipe_table_name
);
1358 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1359 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1360 opts
->dpipe_counters_enable
);
1361 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1362 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1363 opts
->eswitch_encap_mode
);
1364 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1365 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1367 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1368 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1369 opts
->resource_size
);
1370 if (opts
->present
& DL_OPT_PARAM_NAME
)
1371 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1373 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1374 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1376 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1377 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1378 opts
->region_snapshot_id
);
1379 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1380 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1381 opts
->region_address
);
1382 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1383 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1384 opts
->region_length
);
1387 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1388 uint32_t o_required
, uint32_t o_optional
)
1392 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1395 dl_opts_put(nlh
, dl
);
1399 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1401 struct dl_opts
*opts
= &dl
->opts
;
1402 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1403 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1404 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1405 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1407 if (opts
->present
& DL_OPT_HANDLE
&&
1408 attr_bus_name
&& attr_dev_name
) {
1409 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1410 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1412 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1413 strcmp(dev_name
, opts
->dev_name
) != 0)
1416 if (opts
->present
& DL_OPT_HANDLEP
&&
1417 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1418 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1419 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1420 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1422 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1423 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1424 port_index
!= opts
->port_index
)
1427 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1428 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1430 if (sb_index
!= opts
->sb_index
)
1436 static void cmd_dev_help(void)
1438 pr_err("Usage: devlink dev show [ DEV ]\n");
1439 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1440 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1441 pr_err(" [ encap { disable | enable } ]\n");
1442 pr_err(" devlink dev eswitch show DEV\n");
1443 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1444 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1445 pr_err(" devlink dev reload DEV\n");
1448 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1449 const char *dev_name
)
1451 if (!dl
->arr_last
.present
)
1453 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1454 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1457 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1458 const char *dev_name
)
1460 dl
->arr_last
.present
= true;
1461 free(dl
->arr_last
.dev_name
);
1462 free(dl
->arr_last
.bus_name
);
1463 dl
->arr_last
.bus_name
= strdup(bus_name
);
1464 dl
->arr_last
.dev_name
= strdup(dev_name
);
1467 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1468 const char *dev_name
)
1470 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1473 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1474 const char *dev_name
)
1476 return dl
->arr_last
.present
&&
1477 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1480 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1481 bool content
, bool array
)
1483 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1484 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1487 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1489 if (dl
->json_output
) {
1491 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1492 jsonw_end_array(dl
->jw
);
1493 if (should_arr_last_handle_start(dl
, bus_name
,
1495 jsonw_name(dl
->jw
, buf
);
1496 jsonw_start_array(dl
->jw
);
1497 jsonw_start_object(dl
->jw
);
1498 arr_last_handle_set(dl
, bus_name
, dev_name
);
1500 jsonw_start_object(dl
->jw
);
1503 jsonw_name(dl
->jw
, buf
);
1504 jsonw_start_object(dl
->jw
);
1508 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1509 __pr_out_indent_dec();
1510 if (should_arr_last_handle_start(dl
, bus_name
,
1512 pr_out("%s%s", buf
, content
? ":" : "");
1514 __pr_out_indent_inc();
1515 arr_last_handle_set(dl
, bus_name
, dev_name
);
1518 pr_out("%s%s", buf
, content
? ":" : "");
1523 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1525 __pr_out_handle_start(dl
, tb
, true, true);
1528 static void pr_out_handle_end(struct dl
*dl
)
1530 if (dl
->json_output
)
1531 jsonw_end_object(dl
->jw
);
1536 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1538 __pr_out_handle_start(dl
, tb
, false, false);
1539 pr_out_handle_end(dl
);
1542 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1543 const char *dev_name
, uint32_t port_index
)
1545 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1546 dl
->arr_last
.port_index
== port_index
;
1549 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1550 const char *dev_name
, uint32_t port_index
)
1552 arr_last_handle_set(dl
, bus_name
, dev_name
);
1553 dl
->arr_last
.port_index
= port_index
;
1556 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1557 const char *bus_name
,
1558 const char *dev_name
,
1559 uint32_t port_index
)
1561 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1564 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1565 const char *bus_name
,
1566 const char *dev_name
,
1567 uint32_t port_index
)
1569 return dl
->arr_last
.present
&&
1570 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1573 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1574 const char *dev_name
,
1575 uint32_t port_index
, bool try_nice
,
1578 static char buf
[32];
1579 char *ifname
= NULL
;
1581 if (dl
->no_nice_names
|| !try_nice
||
1582 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1583 port_index
, &ifname
) != 0)
1584 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1586 sprintf(buf
, "%s", ifname
);
1588 if (dl
->json_output
) {
1590 if (should_arr_last_port_handle_end(dl
, bus_name
,
1593 jsonw_end_array(dl
->jw
);
1594 if (should_arr_last_port_handle_start(dl
, bus_name
,
1597 jsonw_name(dl
->jw
, buf
);
1598 jsonw_start_array(dl
->jw
);
1599 jsonw_start_object(dl
->jw
);
1600 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1603 jsonw_start_object(dl
->jw
);
1606 jsonw_name(dl
->jw
, buf
);
1607 jsonw_start_object(dl
->jw
);
1614 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1616 const char *bus_name
;
1617 const char *dev_name
;
1618 uint32_t port_index
;
1620 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1621 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1622 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1623 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1626 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1628 const char *bus_name
;
1629 const char *dev_name
;
1630 uint32_t port_index
;
1632 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1633 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1634 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1635 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1638 static void pr_out_port_handle_end(struct dl
*dl
)
1640 if (dl
->json_output
)
1641 jsonw_end_object(dl
->jw
);
1647 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1649 if (dl
->json_output
) {
1650 jsonw_string_field(dl
->jw
, name
, val
);
1652 if (g_indent_newline
)
1653 pr_out("%s %s", name
, val
);
1655 pr_out(" %s %s", name
, val
);
1659 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1662 pr_out_str(dl
, name
, "true");
1664 pr_out_str(dl
, name
, "false");
1667 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1669 if (dl
->json_output
) {
1670 jsonw_uint_field(dl
->jw
, name
, val
);
1672 if (g_indent_newline
)
1673 pr_out("%s %u", name
, val
);
1675 pr_out(" %s %u", name
, val
);
1679 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1681 if (val
== (uint64_t) -1)
1682 return pr_out_str(dl
, name
, "unlimited");
1684 return pr_out_uint(dl
, name
, val
);
1687 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1689 if (dl
->json_output
) {
1690 jsonw_name(dl
->jw
, "address");
1691 jsonw_uint(dl
->jw
, addr
);
1692 jsonw_name(dl
->jw
, "data");
1693 jsonw_start_array(dl
->jw
);
1697 static void pr_out_region_chunk_end(struct dl
*dl
)
1699 if (dl
->json_output
)
1700 jsonw_end_array(dl
->jw
);
1703 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1706 static uint64_t align_val
;
1709 pr_out_region_chunk_start(dl
, addr
);
1711 if (!dl
->json_output
)
1712 if (!(align_val
% 16))
1713 pr_out("%s%016"PRIx64
" ",
1714 align_val
? "\n" : "",
1719 if (dl
->json_output
)
1720 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1722 pr_out("%02x ", data
[i
]);
1727 pr_out_region_chunk_end(dl
);
1730 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1732 pr_out_handle(dl
, tb
);
1735 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1737 if (dl
->json_output
) {
1738 jsonw_start_object(dl
->jw
);
1739 jsonw_name(dl
->jw
, name
);
1740 jsonw_start_object(dl
->jw
);
1744 static void pr_out_section_end(struct dl
*dl
)
1746 if (dl
->json_output
) {
1747 if (dl
->arr_last
.present
)
1748 jsonw_end_array(dl
->jw
);
1749 jsonw_end_object(dl
->jw
);
1750 jsonw_end_object(dl
->jw
);
1754 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1756 if (dl
->json_output
) {
1757 jsonw_name(dl
->jw
, name
);
1758 jsonw_start_array(dl
->jw
);
1760 __pr_out_indent_inc();
1762 pr_out("%s:", name
);
1763 __pr_out_indent_inc();
1768 static void pr_out_array_end(struct dl
*dl
)
1770 if (dl
->json_output
) {
1771 jsonw_end_array(dl
->jw
);
1773 __pr_out_indent_dec();
1774 __pr_out_indent_dec();
1778 static void pr_out_entry_start(struct dl
*dl
)
1780 if (dl
->json_output
)
1781 jsonw_start_object(dl
->jw
);
1784 static void pr_out_entry_end(struct dl
*dl
)
1786 if (dl
->json_output
)
1787 jsonw_end_object(dl
->jw
);
1792 static const char *param_cmode_name(uint8_t cmode
)
1795 case DEVLINK_PARAM_CMODE_RUNTIME
:
1796 return PARAM_CMODE_RUNTIME_STR
;
1797 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1798 return PARAM_CMODE_DRIVERINIT_STR
;
1799 case DEVLINK_PARAM_CMODE_PERMANENT
:
1800 return PARAM_CMODE_PERMANENT_STR
;
1801 default: return "<unknown type>";
1805 static const char *eswitch_mode_name(uint32_t mode
)
1808 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1809 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1810 default: return "<unknown mode>";
1814 static const char *eswitch_inline_mode_name(uint32_t mode
)
1817 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1818 return ESWITCH_INLINE_MODE_NONE
;
1819 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1820 return ESWITCH_INLINE_MODE_LINK
;
1821 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1822 return ESWITCH_INLINE_MODE_NETWORK
;
1823 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1824 return ESWITCH_INLINE_MODE_TRANSPORT
;
1826 return "<unknown mode>";
1830 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1832 __pr_out_handle_start(dl
, tb
, true, false);
1834 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1835 pr_out_str(dl
, "mode",
1836 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1838 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1839 pr_out_str(dl
, "inline-mode",
1840 eswitch_inline_mode_name(mnl_attr_get_u8(
1841 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1843 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1844 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1846 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1849 pr_out_handle_end(dl
);
1852 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1854 struct dl
*dl
= data
;
1855 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1856 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1858 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1859 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1860 return MNL_CB_ERROR
;
1861 pr_out_eswitch(dl
, tb
);
1865 static int cmd_dev_eswitch_show(struct dl
*dl
)
1867 struct nlmsghdr
*nlh
;
1870 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1871 NLM_F_REQUEST
| NLM_F_ACK
);
1873 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1877 pr_out_section_start(dl
, "dev");
1878 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1879 pr_out_section_end(dl
);
1883 static int cmd_dev_eswitch_set(struct dl
*dl
)
1885 struct nlmsghdr
*nlh
;
1888 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1889 NLM_F_REQUEST
| NLM_F_ACK
);
1891 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1892 DL_OPT_ESWITCH_MODE
|
1893 DL_OPT_ESWITCH_INLINE_MODE
|
1894 DL_OPT_ESWITCH_ENCAP_MODE
);
1899 if (dl
->opts
.present
== 1) {
1900 pr_err("Need to set at least one option\n");
1904 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1907 static int cmd_dev_eswitch(struct dl
*dl
)
1909 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1912 } else if (dl_argv_match(dl
, "set")) {
1914 return cmd_dev_eswitch_set(dl
);
1915 } else if (dl_argv_match(dl
, "show")) {
1917 return cmd_dev_eswitch_show(dl
);
1919 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1923 struct param_val_conv
{
1929 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
1930 uint32_t len
, const char *name
)
1934 for (i
= 0; i
< len
; i
++)
1935 if (!strcmp(param_val_conv
[i
].name
, name
))
1942 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
1943 uint32_t len
, const char *name
, const char *vstr
,
1948 for (i
= 0; i
< len
; i
++)
1949 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1950 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
1951 *vuint
= param_val_conv
[i
].vuint
;
1959 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
1960 uint32_t len
, const char *name
, uint32_t vuint
,
1965 for (i
= 0; i
< len
; i
++)
1966 if (!strcmp(param_val_conv
[i
].name
, name
) &&
1967 param_val_conv
[i
].vuint
== vuint
) {
1968 *vstr
= param_val_conv
[i
].vstr
;
1975 static const struct param_val_conv param_val_conv
[] = {
1977 .name
= "fw_load_policy",
1979 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
1982 .name
= "fw_load_policy",
1984 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
1988 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
1990 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
1991 int nla_type
, struct nlattr
*nl
)
1993 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
1994 struct nlattr
*val_attr
;
1999 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2000 if (err
!= MNL_CB_OK
)
2003 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2004 (nla_type
!= MNL_TYPE_FLAG
&&
2005 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2008 pr_out_str(dl
, "cmode",
2009 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2010 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2012 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2018 err
= param_val_conv_str_get(param_val_conv
,
2021 mnl_attr_get_u8(val_attr
),
2025 pr_out_str(dl
, "value", vstr
);
2027 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2032 err
= param_val_conv_str_get(param_val_conv
,
2035 mnl_attr_get_u16(val_attr
),
2039 pr_out_str(dl
, "value", vstr
);
2041 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2046 err
= param_val_conv_str_get(param_val_conv
,
2049 mnl_attr_get_u32(val_attr
),
2053 pr_out_str(dl
, "value", vstr
);
2055 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2058 case MNL_TYPE_STRING
:
2059 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2062 pr_out_bool(dl
, "value", val_attr
? true : false);
2067 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2069 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2070 struct nlattr
*param_value_attr
;
2071 const char *nla_name
;
2075 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2076 if (err
!= MNL_CB_OK
)
2078 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2079 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2080 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2084 pr_out_handle_start_arr(dl
, tb
);
2086 __pr_out_handle_start(dl
, tb
, true, false);
2088 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2090 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2091 pr_out_str(dl
, "name", nla_name
);
2093 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2094 pr_out_str(dl
, "type", "driver-specific");
2096 pr_out_str(dl
, "type", "generic");
2098 pr_out_array_start(dl
, "values");
2099 mnl_attr_for_each_nested(param_value_attr
,
2100 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2101 pr_out_entry_start(dl
);
2102 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2103 pr_out_entry_end(dl
);
2105 pr_out_array_end(dl
);
2106 pr_out_handle_end(dl
);
2109 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2111 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2112 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2113 struct dl
*dl
= data
;
2115 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2116 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2117 !tb
[DEVLINK_ATTR_PARAM
])
2118 return MNL_CB_ERROR
;
2119 pr_out_param(dl
, tb
, true);
2135 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2137 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2138 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2139 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2140 struct nlattr
*param_value_attr
;
2141 enum devlink_param_cmode cmode
;
2142 struct param_ctx
*ctx
= data
;
2143 struct dl
*dl
= ctx
->dl
;
2147 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2148 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2149 !tb
[DEVLINK_ATTR_PARAM
])
2150 return MNL_CB_ERROR
;
2152 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2153 if (err
!= MNL_CB_OK
)
2154 return MNL_CB_ERROR
;
2156 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2157 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2158 return MNL_CB_ERROR
;
2160 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2161 mnl_attr_for_each_nested(param_value_attr
,
2162 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2163 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2164 struct nlattr
*val_attr
;
2166 err
= mnl_attr_parse_nested(param_value_attr
,
2167 attr_cb
, nla_value
);
2168 if (err
!= MNL_CB_OK
)
2169 return MNL_CB_ERROR
;
2171 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2172 (nla_type
!= MNL_TYPE_FLAG
&&
2173 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2174 return MNL_CB_ERROR
;
2176 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2177 if (cmode
== dl
->opts
.cmode
) {
2178 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2181 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2184 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2187 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2189 case MNL_TYPE_STRING
:
2190 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2193 ctx
->value
.vbool
= val_attr
? true : false;
2199 ctx
->nla_type
= nla_type
;
2203 static int cmd_dev_param_set(struct dl
*dl
)
2205 struct param_ctx ctx
= {};
2206 struct nlmsghdr
*nlh
;
2214 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2216 DL_OPT_PARAM_VALUE
|
2217 DL_OPT_PARAM_CMODE
, 0);
2221 /* Get value type */
2222 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2223 NLM_F_REQUEST
| NLM_F_ACK
);
2224 dl_opts_put(nlh
, dl
);
2227 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2231 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2232 NLM_F_REQUEST
| NLM_F_ACK
);
2233 dl_opts_put(nlh
, dl
);
2235 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2236 dl
->opts
.param_name
);
2238 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2239 switch (ctx
.nla_type
) {
2242 err
= param_val_conv_uint_get(param_val_conv
,
2244 dl
->opts
.param_name
,
2245 dl
->opts
.param_value
,
2249 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2252 goto err_param_value_parse
;
2253 if (val_u8
== ctx
.value
.vu8
)
2255 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2259 err
= param_val_conv_uint_get(param_val_conv
,
2261 dl
->opts
.param_name
,
2262 dl
->opts
.param_value
,
2266 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2269 goto err_param_value_parse
;
2270 if (val_u16
== ctx
.value
.vu16
)
2272 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2276 err
= param_val_conv_uint_get(param_val_conv
,
2278 dl
->opts
.param_name
,
2279 dl
->opts
.param_value
,
2282 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2284 goto err_param_value_parse
;
2285 if (val_u32
== ctx
.value
.vu32
)
2287 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2290 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2292 goto err_param_value_parse
;
2293 if (val_bool
== ctx
.value
.vbool
)
2296 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2299 case MNL_TYPE_STRING
:
2300 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2301 dl
->opts
.param_value
);
2302 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2306 printf("Value type not supported\n");
2309 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2311 err_param_value_parse
:
2312 pr_err("Value \"%s\" is not a number or not within range\n",
2313 dl
->opts
.param_value
);
2317 static int cmd_dev_param_show(struct dl
*dl
)
2319 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2320 struct nlmsghdr
*nlh
;
2323 if (dl_argc(dl
) == 0)
2324 flags
|= NLM_F_DUMP
;
2326 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2328 if (dl_argc(dl
) > 0) {
2329 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2330 DL_OPT_PARAM_NAME
, 0);
2335 pr_out_section_start(dl
, "param");
2336 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2337 pr_out_section_end(dl
);
2341 static int cmd_dev_param(struct dl
*dl
)
2343 if (dl_argv_match(dl
, "help")) {
2346 } else if (dl_argv_match(dl
, "show") ||
2347 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2349 return cmd_dev_param_show(dl
);
2350 } else if (dl_argv_match(dl
, "set")) {
2352 return cmd_dev_param_set(dl
);
2354 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2357 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2359 struct dl
*dl
= data
;
2360 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2361 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2363 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2364 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2365 return MNL_CB_ERROR
;
2370 static int cmd_dev_show(struct dl
*dl
)
2372 struct nlmsghdr
*nlh
;
2373 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2376 if (dl_argc(dl
) == 0)
2377 flags
|= NLM_F_DUMP
;
2379 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2381 if (dl_argc(dl
) > 0) {
2382 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2387 pr_out_section_start(dl
, "dev");
2388 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2389 pr_out_section_end(dl
);
2393 static void cmd_dev_reload_help(void)
2395 pr_err("Usage: devlink dev reload [ DEV ]\n");
2398 static int cmd_dev_reload(struct dl
*dl
)
2400 struct nlmsghdr
*nlh
;
2403 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2404 cmd_dev_reload_help();
2408 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2409 NLM_F_REQUEST
| NLM_F_ACK
);
2411 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2415 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2418 static int cmd_dev(struct dl
*dl
)
2420 if (dl_argv_match(dl
, "help")) {
2423 } else if (dl_argv_match(dl
, "show") ||
2424 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2426 return cmd_dev_show(dl
);
2427 } else if (dl_argv_match(dl
, "eswitch")) {
2429 return cmd_dev_eswitch(dl
);
2430 } else if (dl_argv_match(dl
, "reload")) {
2432 return cmd_dev_reload(dl
);
2433 } else if (dl_argv_match(dl
, "param")) {
2435 return cmd_dev_param(dl
);
2437 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2441 static void cmd_port_help(void)
2443 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2444 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2445 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2446 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2449 static const char *port_type_name(uint32_t type
)
2452 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2453 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2454 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2455 case DEVLINK_PORT_TYPE_IB
: return "ib";
2456 default: return "<unknown type>";
2460 static const char *port_flavour_name(uint16_t flavour
)
2463 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2465 case DEVLINK_PORT_FLAVOUR_CPU
:
2467 case DEVLINK_PORT_FLAVOUR_DSA
:
2470 return "<unknown flavour>";
2474 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2476 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2477 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2479 pr_out_port_handle_start(dl
, tb
, false);
2481 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2483 pr_out_str(dl
, "type", port_type_name(port_type
));
2485 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2487 if (port_type
!= des_port_type
)
2488 pr_out_str(dl
, "des_type",
2489 port_type_name(des_port_type
));
2492 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2493 pr_out_str(dl
, "netdev",
2494 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2495 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2496 pr_out_str(dl
, "ibdev",
2497 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2498 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2499 uint16_t port_flavour
=
2500 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2502 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2504 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2505 pr_out_uint(dl
, "split_group",
2506 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2507 pr_out_port_handle_end(dl
);
2510 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2512 struct dl
*dl
= data
;
2513 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2514 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2516 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2517 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2518 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2519 return MNL_CB_ERROR
;
2520 pr_out_port(dl
, tb
);
2524 static int cmd_port_show(struct dl
*dl
)
2526 struct nlmsghdr
*nlh
;
2527 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2530 if (dl_argc(dl
) == 0)
2531 flags
|= NLM_F_DUMP
;
2533 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2535 if (dl_argc(dl
) > 0) {
2536 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2541 pr_out_section_start(dl
, "port");
2542 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2543 pr_out_section_end(dl
);
2547 static int cmd_port_set(struct dl
*dl
)
2549 struct nlmsghdr
*nlh
;
2552 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2553 NLM_F_REQUEST
| NLM_F_ACK
);
2555 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2559 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2562 static int cmd_port_split(struct dl
*dl
)
2564 struct nlmsghdr
*nlh
;
2567 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2568 NLM_F_REQUEST
| NLM_F_ACK
);
2570 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2574 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2577 static int cmd_port_unsplit(struct dl
*dl
)
2579 struct nlmsghdr
*nlh
;
2582 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2583 NLM_F_REQUEST
| NLM_F_ACK
);
2585 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2589 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2592 static int cmd_port(struct dl
*dl
)
2594 if (dl_argv_match(dl
, "help")) {
2597 } else if (dl_argv_match(dl
, "show") ||
2598 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2600 return cmd_port_show(dl
);
2601 } else if (dl_argv_match(dl
, "set")) {
2603 return cmd_port_set(dl
);
2604 } else if (dl_argv_match(dl
, "split")) {
2606 return cmd_port_split(dl
);
2607 } else if (dl_argv_match(dl
, "unsplit")) {
2609 return cmd_port_unsplit(dl
);
2611 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2615 static void cmd_sb_help(void)
2617 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2618 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2619 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2620 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2621 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2622 pr_err(" pool POOL_INDEX ]\n");
2623 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2624 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2625 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2626 pr_err(" type { ingress | egress } ]\n");
2627 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2628 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2629 pr_err(" th THRESHOLD\n");
2630 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2631 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2632 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2635 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2637 pr_out_handle_start_arr(dl
, tb
);
2638 pr_out_uint(dl
, "sb",
2639 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2640 pr_out_uint(dl
, "size",
2641 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2642 pr_out_uint(dl
, "ing_pools",
2643 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2644 pr_out_uint(dl
, "eg_pools",
2645 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2646 pr_out_uint(dl
, "ing_tcs",
2647 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2648 pr_out_uint(dl
, "eg_tcs",
2649 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2650 pr_out_handle_end(dl
);
2653 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2655 struct dl
*dl
= data
;
2656 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2657 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2659 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2660 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2661 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2662 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2663 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2664 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2665 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2666 return MNL_CB_ERROR
;
2671 static int cmd_sb_show(struct dl
*dl
)
2673 struct nlmsghdr
*nlh
;
2674 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2677 if (dl_argc(dl
) == 0)
2678 flags
|= NLM_F_DUMP
;
2680 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2682 if (dl_argc(dl
) > 0) {
2683 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2688 pr_out_section_start(dl
, "sb");
2689 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2690 pr_out_section_end(dl
);
2694 static const char *pool_type_name(uint8_t type
)
2697 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2698 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2699 default: return "<unknown type>";
2703 static const char *threshold_type_name(uint8_t type
)
2706 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2707 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2708 default: return "<unknown type>";
2712 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2714 pr_out_handle_start_arr(dl
, tb
);
2715 pr_out_uint(dl
, "sb",
2716 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2717 pr_out_uint(dl
, "pool",
2718 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2719 pr_out_str(dl
, "type",
2720 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2721 pr_out_uint(dl
, "size",
2722 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2723 pr_out_str(dl
, "thtype",
2724 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2725 pr_out_handle_end(dl
);
2728 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2730 struct dl
*dl
= data
;
2731 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2732 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2734 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2735 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2736 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2737 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2738 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2739 return MNL_CB_ERROR
;
2740 pr_out_sb_pool(dl
, tb
);
2744 static int cmd_sb_pool_show(struct dl
*dl
)
2746 struct nlmsghdr
*nlh
;
2747 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2750 if (dl_argc(dl
) == 0)
2751 flags
|= NLM_F_DUMP
;
2753 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2755 if (dl_argc(dl
) > 0) {
2756 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2762 pr_out_section_start(dl
, "pool");
2763 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2764 pr_out_section_end(dl
);
2768 static int cmd_sb_pool_set(struct dl
*dl
)
2770 struct nlmsghdr
*nlh
;
2773 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
2774 NLM_F_REQUEST
| NLM_F_ACK
);
2776 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
2777 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
2781 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2784 static int cmd_sb_pool(struct dl
*dl
)
2786 if (dl_argv_match(dl
, "help")) {
2789 } else if (dl_argv_match(dl
, "show") ||
2790 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2792 return cmd_sb_pool_show(dl
);
2793 } else if (dl_argv_match(dl
, "set")) {
2795 return cmd_sb_pool_set(dl
);
2797 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2801 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
2803 pr_out_port_handle_start_arr(dl
, tb
, true);
2804 pr_out_uint(dl
, "sb",
2805 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2806 pr_out_uint(dl
, "pool",
2807 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2808 pr_out_uint(dl
, "threshold",
2809 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2810 pr_out_port_handle_end(dl
);
2813 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2815 struct dl
*dl
= data
;
2816 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2817 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2819 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2820 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2821 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2822 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2823 return MNL_CB_ERROR
;
2824 pr_out_sb_port_pool(dl
, tb
);
2828 static int cmd_sb_port_pool_show(struct dl
*dl
)
2830 struct nlmsghdr
*nlh
;
2831 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2834 if (dl_argc(dl
) == 0)
2835 flags
|= NLM_F_DUMP
;
2837 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2839 if (dl_argc(dl
) > 0) {
2840 err
= dl_argv_parse_put(nlh
, dl
,
2841 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
2847 pr_out_section_start(dl
, "port_pool");
2848 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
2849 pr_out_section_end(dl
);
2853 static int cmd_sb_port_pool_set(struct dl
*dl
)
2855 struct nlmsghdr
*nlh
;
2858 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2859 NLM_F_REQUEST
| NLM_F_ACK
);
2861 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2862 DL_OPT_SB_TH
, DL_OPT_SB
);
2866 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2869 static int cmd_sb_port_pool(struct dl
*dl
)
2871 if (dl_argv_match(dl
, "help")) {
2874 } else if (dl_argv_match(dl
, "show") ||
2875 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2877 return cmd_sb_port_pool_show(dl
);
2878 } else if (dl_argv_match(dl
, "set")) {
2880 return cmd_sb_port_pool_set(dl
);
2882 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2886 static int cmd_sb_port(struct dl
*dl
)
2888 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2891 } else if (dl_argv_match(dl
, "pool")) {
2893 return cmd_sb_port_pool(dl
);
2895 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2899 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2901 pr_out_port_handle_start_arr(dl
, tb
, true);
2902 pr_out_uint(dl
, "sb",
2903 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2904 pr_out_uint(dl
, "tc",
2905 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2906 pr_out_str(dl
, "type",
2907 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2908 pr_out_uint(dl
, "pool",
2909 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2910 pr_out_uint(dl
, "threshold",
2911 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2912 pr_out_port_handle_end(dl
);
2915 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2917 struct dl
*dl
= data
;
2918 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2919 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2921 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2922 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2923 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2924 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2925 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2926 return MNL_CB_ERROR
;
2927 pr_out_sb_tc_bind(dl
, tb
);
2931 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2933 struct nlmsghdr
*nlh
;
2934 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2937 if (dl_argc(dl
) == 0)
2938 flags
|= NLM_F_DUMP
;
2940 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2942 if (dl_argc(dl
) > 0) {
2943 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2944 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2949 pr_out_section_start(dl
, "tc_bind");
2950 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2951 pr_out_section_end(dl
);
2955 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2957 struct nlmsghdr
*nlh
;
2960 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2961 NLM_F_REQUEST
| NLM_F_ACK
);
2963 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2964 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2969 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2972 static int cmd_sb_tc_bind(struct dl
*dl
)
2974 if (dl_argv_match(dl
, "help")) {
2977 } else if (dl_argv_match(dl
, "show") ||
2978 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2980 return cmd_sb_tc_bind_show(dl
);
2981 } else if (dl_argv_match(dl
, "set")) {
2983 return cmd_sb_tc_bind_set(dl
);
2985 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2989 static int cmd_sb_tc(struct dl
*dl
)
2991 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2994 } else if (dl_argv_match(dl
, "bind")) {
2996 return cmd_sb_tc_bind(dl
);
2998 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3003 struct list_head list
;
3007 uint32_t bound_pool_index
;
3011 struct list_head list
;
3014 uint32_t port_index
;
3016 struct list_head pool_list
;
3017 struct list_head ing_tc_list
;
3018 struct list_head eg_tc_list
;
3024 struct list_head port_list
;
3027 static struct occ_item
*occ_item_alloc(void)
3029 return calloc(1, sizeof(struct occ_item
));
3032 static void occ_item_free(struct occ_item
*occ_item
)
3037 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3039 struct occ_port
*occ_port
;
3041 occ_port
= calloc(1, sizeof(*occ_port
));
3044 occ_port
->port_index
= port_index
;
3045 INIT_LIST_HEAD(&occ_port
->pool_list
);
3046 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3047 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3051 static void occ_port_free(struct occ_port
*occ_port
)
3053 struct occ_item
*occ_item
, *tmp
;
3055 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3056 occ_item_free(occ_item
);
3057 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3058 occ_item_free(occ_item
);
3059 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3060 occ_item_free(occ_item
);
3063 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3065 struct occ_show
*occ_show
;
3067 occ_show
= calloc(1, sizeof(*occ_show
));
3071 INIT_LIST_HEAD(&occ_show
->port_list
);
3075 static void occ_show_free(struct occ_show
*occ_show
)
3077 struct occ_port
*occ_port
, *tmp
;
3079 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3080 occ_port_free(occ_port
);
3083 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3086 struct occ_port
*occ_port
;
3087 uint32_t port_index
;
3089 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3091 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3092 if (occ_port
->port_index
== port_index
)
3095 occ_port
= occ_port_alloc(port_index
);
3098 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3102 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3105 struct occ_item
*occ_item
;
3108 pr_out_sp(7, " %s:", label
);
3109 list_for_each_entry(occ_item
, list
, list
) {
3110 if ((i
- 1) % 4 == 0 && i
!= 1)
3113 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3114 occ_item
->bound_pool_index
);
3116 pr_out_sp(7, "%2u:", occ_item
->index
);
3117 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
3121 if ((i
- 1) % 4 != 0)
3125 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3126 struct list_head
*list
,
3129 struct occ_item
*occ_item
;
3132 jsonw_name(dl
->jw
, label
);
3133 jsonw_start_object(dl
->jw
);
3134 list_for_each_entry(occ_item
, list
, list
) {
3135 sprintf(buf
, "%u", occ_item
->index
);
3136 jsonw_name(dl
->jw
, buf
);
3137 jsonw_start_object(dl
->jw
);
3139 jsonw_uint_field(dl
->jw
, "bound_pool",
3140 occ_item
->bound_pool_index
);
3141 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3142 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3143 jsonw_end_object(dl
->jw
);
3145 jsonw_end_object(dl
->jw
);
3148 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3150 if (dl
->json_output
) {
3151 pr_out_json_occ_show_item_list(dl
, "pool",
3152 &occ_port
->pool_list
, false);
3153 pr_out_json_occ_show_item_list(dl
, "itc",
3154 &occ_port
->ing_tc_list
, true);
3155 pr_out_json_occ_show_item_list(dl
, "etc",
3156 &occ_port
->eg_tc_list
, true);
3159 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3160 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3161 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3165 static void pr_out_occ_show(struct occ_show
*occ_show
)
3167 struct dl
*dl
= occ_show
->dl
;
3168 struct dl_opts
*opts
= &dl
->opts
;
3169 struct occ_port
*occ_port
;
3171 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3172 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3173 occ_port
->port_index
, true, false);
3174 pr_out_occ_show_port(dl
, occ_port
);
3175 pr_out_port_handle_end(dl
);
3179 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3182 struct occ_port
*occ_port
;
3183 struct occ_item
*occ_item
;
3185 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3188 occ_port
= occ_port_get(occ_show
, tb
);
3190 occ_show
->err
= -ENOMEM
;
3194 occ_item
= occ_item_alloc();
3196 occ_show
->err
= -ENOMEM
;
3199 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3200 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3201 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3202 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3205 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3207 struct occ_show
*occ_show
= data
;
3208 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3209 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3211 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3212 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3213 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3214 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3215 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3216 return MNL_CB_ERROR
;
3217 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3221 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3224 struct occ_port
*occ_port
;
3225 struct occ_item
*occ_item
;
3228 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3231 occ_port
= occ_port_get(occ_show
, tb
);
3233 occ_show
->err
= -ENOMEM
;
3237 occ_item
= occ_item_alloc();
3239 occ_show
->err
= -ENOMEM
;
3242 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3243 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3244 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3245 occ_item
->bound_pool_index
=
3246 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3247 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3248 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3249 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3250 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3251 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3253 occ_item_free(occ_item
);
3256 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3258 struct occ_show
*occ_show
= data
;
3259 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3260 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3262 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3263 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3264 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3265 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3266 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3267 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3268 return MNL_CB_ERROR
;
3269 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3273 static int cmd_sb_occ_show(struct dl
*dl
)
3275 struct nlmsghdr
*nlh
;
3276 struct occ_show
*occ_show
;
3277 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3280 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3284 occ_show
= occ_show_alloc(dl
);
3288 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3290 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3291 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3295 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3297 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3298 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3302 pr_out_section_start(dl
, "occupancy");
3303 pr_out_occ_show(occ_show
);
3304 pr_out_section_end(dl
);
3307 occ_show_free(occ_show
);
3311 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3313 struct nlmsghdr
*nlh
;
3316 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3317 NLM_F_REQUEST
| NLM_F_ACK
);
3319 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3323 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3326 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3328 struct nlmsghdr
*nlh
;
3331 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3332 NLM_F_REQUEST
| NLM_F_ACK
);
3334 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3338 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3341 static int cmd_sb_occ(struct dl
*dl
)
3343 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3346 } else if (dl_argv_match(dl
, "show") ||
3347 dl_argv_match(dl
, "list")) {
3349 return cmd_sb_occ_show(dl
);
3350 } else if (dl_argv_match(dl
, "snapshot")) {
3352 return cmd_sb_occ_snapshot(dl
);
3353 } else if (dl_argv_match(dl
, "clearmax")) {
3355 return cmd_sb_occ_clearmax(dl
);
3357 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3361 static int cmd_sb(struct dl
*dl
)
3363 if (dl_argv_match(dl
, "help")) {
3366 } else if (dl_argv_match(dl
, "show") ||
3367 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3369 return cmd_sb_show(dl
);
3370 } else if (dl_argv_match(dl
, "pool")) {
3372 return cmd_sb_pool(dl
);
3373 } else if (dl_argv_match(dl
, "port")) {
3375 return cmd_sb_port(dl
);
3376 } else if (dl_argv_match(dl
, "tc")) {
3378 return cmd_sb_tc(dl
);
3379 } else if (dl_argv_match(dl
, "occupancy")) {
3381 return cmd_sb_occ(dl
);
3383 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3387 static const char *cmd_name(uint8_t cmd
)
3390 case DEVLINK_CMD_UNSPEC
: return "unspec";
3391 case DEVLINK_CMD_GET
: return "get";
3392 case DEVLINK_CMD_SET
: return "set";
3393 case DEVLINK_CMD_NEW
: return "new";
3394 case DEVLINK_CMD_DEL
: return "del";
3395 case DEVLINK_CMD_PORT_GET
: return "get";
3396 case DEVLINK_CMD_PORT_SET
: return "set";
3397 case DEVLINK_CMD_PORT_NEW
: return "new";
3398 case DEVLINK_CMD_PORT_DEL
: return "del";
3399 case DEVLINK_CMD_PARAM_GET
: return "get";
3400 case DEVLINK_CMD_PARAM_SET
: return "set";
3401 case DEVLINK_CMD_PARAM_NEW
: return "new";
3402 case DEVLINK_CMD_PARAM_DEL
: return "del";
3403 case DEVLINK_CMD_REGION_GET
: return "get";
3404 case DEVLINK_CMD_REGION_SET
: return "set";
3405 case DEVLINK_CMD_REGION_NEW
: return "new";
3406 case DEVLINK_CMD_REGION_DEL
: return "del";
3407 default: return "<unknown cmd>";
3411 static const char *cmd_obj(uint8_t cmd
)
3414 case DEVLINK_CMD_UNSPEC
: return "unspec";
3415 case DEVLINK_CMD_GET
:
3416 case DEVLINK_CMD_SET
:
3417 case DEVLINK_CMD_NEW
:
3418 case DEVLINK_CMD_DEL
:
3420 case DEVLINK_CMD_PORT_GET
:
3421 case DEVLINK_CMD_PORT_SET
:
3422 case DEVLINK_CMD_PORT_NEW
:
3423 case DEVLINK_CMD_PORT_DEL
:
3425 case DEVLINK_CMD_PARAM_GET
:
3426 case DEVLINK_CMD_PARAM_SET
:
3427 case DEVLINK_CMD_PARAM_NEW
:
3428 case DEVLINK_CMD_PARAM_DEL
:
3430 case DEVLINK_CMD_REGION_GET
:
3431 case DEVLINK_CMD_REGION_SET
:
3432 case DEVLINK_CMD_REGION_NEW
:
3433 case DEVLINK_CMD_REGION_DEL
:
3435 default: return "<unknown obj>";
3439 static void pr_out_mon_header(uint8_t cmd
)
3441 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3444 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3446 const char *obj
= cmd_obj(cmd
);
3447 unsigned int index
= 0;
3448 const char *cur_obj
;
3452 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3453 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3459 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3461 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3463 struct dl
*dl
= data
;
3464 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3465 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3466 uint8_t cmd
= genl
->cmd
;
3468 if (!cmd_filter_check(dl
, cmd
))
3472 case DEVLINK_CMD_GET
: /* fall through */
3473 case DEVLINK_CMD_SET
: /* fall through */
3474 case DEVLINK_CMD_NEW
: /* fall through */
3475 case DEVLINK_CMD_DEL
:
3476 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3477 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3478 return MNL_CB_ERROR
;
3479 pr_out_mon_header(genl
->cmd
);
3482 case DEVLINK_CMD_PORT_GET
: /* fall through */
3483 case DEVLINK_CMD_PORT_SET
: /* fall through */
3484 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3485 case DEVLINK_CMD_PORT_DEL
:
3486 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3487 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3488 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3489 return MNL_CB_ERROR
;
3490 pr_out_mon_header(genl
->cmd
);
3491 pr_out_port(dl
, tb
);
3493 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3494 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3495 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3496 case DEVLINK_CMD_PARAM_DEL
:
3497 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3498 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3499 !tb
[DEVLINK_ATTR_PARAM
])
3500 return MNL_CB_ERROR
;
3501 pr_out_mon_header(genl
->cmd
);
3502 pr_out_param(dl
, tb
, false);
3504 case DEVLINK_CMD_REGION_GET
: /* fall through */
3505 case DEVLINK_CMD_REGION_SET
: /* fall through */
3506 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3507 case DEVLINK_CMD_REGION_DEL
:
3508 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3509 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3510 !tb
[DEVLINK_ATTR_REGION_NAME
])
3511 return MNL_CB_ERROR
;
3512 pr_out_mon_header(genl
->cmd
);
3513 pr_out_region(dl
, tb
);
3519 static int cmd_mon_show(struct dl
*dl
)
3522 unsigned int index
= 0;
3523 const char *cur_obj
;
3525 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3526 if (strcmp(cur_obj
, "all") != 0 &&
3527 strcmp(cur_obj
, "dev") != 0 &&
3528 strcmp(cur_obj
, "port") != 0) {
3529 pr_err("Unknown object \"%s\"\n", cur_obj
);
3533 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3536 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3542 static void cmd_mon_help(void)
3544 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3545 "where OBJECT-LIST := { dev | port }\n");
3548 static int cmd_mon(struct dl
*dl
)
3550 if (dl_argv_match(dl
, "help")) {
3553 } else if (dl_no_arg(dl
)) {
3555 return cmd_mon_show(dl
);
3557 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3561 struct dpipe_field
{
3564 unsigned int bitwidth
;
3565 enum devlink_dpipe_field_mapping_type mapping_type
;
3568 struct dpipe_header
{
3569 struct list_head list
;
3572 struct dpipe_field
*fields
;
3573 unsigned int fields_count
;
3576 struct dpipe_table
{
3577 struct list_head list
;
3579 unsigned int resource_id
;
3580 bool resource_valid
;
3583 struct dpipe_tables
{
3584 struct list_head table_list
;
3594 enum devlink_resource_unit unit
;
3599 struct list_head list
;
3600 struct list_head resource_list
;
3601 struct resource
*parent
;
3605 struct list_head resource_list
;
3608 struct resource_ctx
{
3611 struct resources
*resources
;
3612 struct dpipe_tables
*tables
;
3613 bool print_resources
;
3614 bool pending_change
;
3617 static struct resource
*resource_alloc(void)
3619 struct resource
*resource
;
3621 resource
= calloc(1, sizeof(struct resource
));
3624 INIT_LIST_HEAD(&resource
->resource_list
);
3628 static void resource_free(struct resource
*resource
)
3630 struct resource
*child_resource
, *tmp
;
3632 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3634 free(child_resource
->name
);
3635 resource_free(child_resource
);
3640 static struct resources
*resources_alloc(void)
3642 struct resources
*resources
;
3644 resources
= calloc(1, sizeof(struct resources
));
3647 INIT_LIST_HEAD(&resources
->resource_list
);
3651 static void resources_free(struct resources
*resources
)
3653 struct resource
*resource
, *tmp
;
3655 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3656 resource_free(resource
);
3659 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3661 ctx
->resources
= resources_alloc();
3662 if (!ctx
->resources
)
3668 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3670 resources_free(ctx
->resources
);
3676 struct list_head global_headers
;
3677 struct list_head local_headers
;
3678 struct dpipe_tables
*tables
;
3679 struct resources
*resources
;
3684 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3686 struct dpipe_header
*header
;
3688 header
= calloc(1, sizeof(struct dpipe_header
));
3691 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3692 if (!header
->fields
)
3693 goto err_fields_alloc
;
3694 header
->fields_count
= fields_count
;
3702 static void dpipe_header_free(struct dpipe_header
*header
)
3704 free(header
->fields
);
3708 static void dpipe_header_clear(struct dpipe_header
*header
)
3710 struct dpipe_field
*field
;
3713 for (i
= 0; i
< header
->fields_count
; i
++) {
3714 field
= &header
->fields
[i
];
3720 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3721 struct dpipe_header
*header
, bool global
)
3724 list_add(&header
->list
, &ctx
->global_headers
);
3726 list_add(&header
->list
, &ctx
->local_headers
);
3729 static void dpipe_header_del(struct dpipe_header
*header
)
3731 list_del(&header
->list
);
3734 static struct dpipe_table
*dpipe_table_alloc(void)
3736 return calloc(1, sizeof(struct dpipe_table
));
3739 static void dpipe_table_free(struct dpipe_table
*table
)
3744 static struct dpipe_tables
*dpipe_tables_alloc(void)
3746 struct dpipe_tables
*tables
;
3748 tables
= calloc(1, sizeof(struct dpipe_tables
));
3751 INIT_LIST_HEAD(&tables
->table_list
);
3755 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3757 struct dpipe_table
*table
, *tmp
;
3759 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3760 dpipe_table_free(table
);
3764 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
3766 ctx
->tables
= dpipe_tables_alloc();
3771 INIT_LIST_HEAD(&ctx
->global_headers
);
3772 INIT_LIST_HEAD(&ctx
->local_headers
);
3776 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
3778 struct dpipe_header
*header
, *tmp
;
3780 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
3782 dpipe_header_del(header
);
3783 dpipe_header_clear(header
);
3784 dpipe_header_free(header
);
3786 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
3788 dpipe_header_del(header
);
3789 dpipe_header_clear(header
);
3790 dpipe_header_free(header
);
3792 dpipe_tables_free(ctx
->tables
);
3795 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
3796 uint32_t header_id
, bool global
)
3798 struct list_head
*header_list
;
3799 struct dpipe_header
*header
;
3802 header_list
= &ctx
->global_headers
;
3804 header_list
= &ctx
->local_headers
;
3805 list_for_each_entry(header
, header_list
, list
) {
3806 if (header
->id
!= header_id
)
3808 return header
->name
;
3813 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
3815 uint32_t field_id
, bool global
)
3817 struct list_head
*header_list
;
3818 struct dpipe_header
*header
;
3821 header_list
= &ctx
->global_headers
;
3823 header_list
= &ctx
->local_headers
;
3824 list_for_each_entry(header
, header_list
, list
) {
3825 if (header
->id
!= header_id
)
3827 return header
->fields
[field_id
].name
;
3833 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
3835 switch (mapping_type
) {
3836 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
3838 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
3846 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
3847 uint32_t field_id
, bool global
)
3849 enum devlink_dpipe_field_mapping_type mapping_type
;
3850 struct list_head
*header_list
;
3851 struct dpipe_header
*header
;
3854 header_list
= &ctx
->global_headers
;
3856 header_list
= &ctx
->local_headers
;
3857 list_for_each_entry(header
, header_list
, list
) {
3858 if (header
->id
!= header_id
)
3860 mapping_type
= header
->fields
[field_id
].mapping_type
;
3861 return dpipe_field_mapping_e2s(mapping_type
);
3866 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
3867 struct dpipe_field
*fields
,
3868 unsigned int field_count
)
3870 struct dpipe_field
*field
;
3873 for (i
= 0; i
< field_count
; i
++) {
3875 pr_out_entry_start(ctx
->dl
);
3876 pr_out_str(ctx
->dl
, "name", field
->name
);
3877 if (ctx
->dl
->verbose
)
3878 pr_out_uint(ctx
->dl
, "id", field
->id
);
3879 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
3880 if (field
->mapping_type
)
3881 pr_out_str(ctx
->dl
, "mapping_type",
3882 dpipe_field_mapping_e2s(field
->mapping_type
));
3883 pr_out_entry_end(ctx
->dl
);
3888 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
3889 struct dpipe_header
*header
, bool global
)
3891 pr_out_handle_start_arr(ctx
->dl
, tb
);
3892 pr_out_str(ctx
->dl
, "name", header
->name
);
3893 if (ctx
->dl
->verbose
) {
3894 pr_out_uint(ctx
->dl
, "id", header
->id
);
3895 pr_out_str(ctx
->dl
, "global",
3896 global
? "true" : "false");
3898 pr_out_array_start(ctx
->dl
, "field");
3899 pr_out_dpipe_fields(ctx
, header
->fields
,
3900 header
->fields_count
);
3901 pr_out_array_end(ctx
->dl
);
3902 pr_out_handle_end(ctx
->dl
);
3905 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
3908 struct dpipe_header
*header
;
3910 list_for_each_entry(header
, &ctx
->local_headers
, list
)
3911 pr_out_dpipe_header(ctx
, tb
, header
, false);
3913 list_for_each_entry(header
, &ctx
->global_headers
, list
)
3914 pr_out_dpipe_header(ctx
, tb
, header
, true);
3917 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
3919 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
3923 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
3924 if (err
!= MNL_CB_OK
)
3926 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
3927 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
3928 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
3929 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
3932 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
3933 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3934 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
3935 field
->name
= strdup(name
);
3938 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
3942 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
3943 struct dpipe_field
*fields
)
3945 struct nlattr
*nla_field
;
3949 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
3950 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
3958 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
3960 struct nlattr
*nla_field
;
3961 unsigned int count
= 0;
3963 mnl_attr_for_each_nested(nla_field
, nla_fields
)
3968 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3970 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
3971 struct dpipe_header
*header
;
3972 unsigned int fields_count
;
3973 const char *header_name
;
3977 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
3978 if (err
!= MNL_CB_OK
)
3981 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
3982 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3983 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
3986 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
3987 header
= dpipe_header_alloc(fields_count
);
3991 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
3992 header
->name
= strdup(header_name
);
3993 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3994 header
->fields_count
= fields_count
;
3995 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3997 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4001 dpipe_header_add(ctx
, header
, global
);
4005 dpipe_header_free(header
);
4009 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4011 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4012 struct nlattr
*nla_header
;
4015 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4016 err
= dpipe_header_get(ctx
, nla_header
);
4023 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4025 struct dpipe_ctx
*ctx
= data
;
4026 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4027 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4030 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4031 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4032 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4033 return MNL_CB_ERROR
;
4034 err
= dpipe_headers_get(ctx
, tb
);
4037 return MNL_CB_ERROR
;
4040 if (ctx
->print_headers
)
4041 pr_out_dpipe_headers(ctx
, tb
);
4045 static int cmd_dpipe_headers_show(struct dl
*dl
)
4047 struct nlmsghdr
*nlh
;
4048 struct dpipe_ctx ctx
= {};
4049 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4052 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4054 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4058 err
= dpipe_ctx_init(&ctx
, dl
);
4062 ctx
.print_headers
= true;
4064 pr_out_section_start(dl
, "header");
4065 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4067 pr_err("error get headers %s\n", strerror(ctx
.err
));
4068 pr_out_section_end(dl
);
4070 dpipe_ctx_fini(&ctx
);
4074 static void cmd_dpipe_header_help(void)
4076 pr_err("Usage: devlink dpipe headers show DEV\n");
4079 static int cmd_dpipe_header(struct dl
*dl
)
4081 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4082 cmd_dpipe_header_help();
4084 } else if (dl_argv_match(dl
, "show")) {
4086 return cmd_dpipe_headers_show(dl
);
4088 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4093 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4095 switch (action_type
) {
4096 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4097 return "field_modify";
4103 struct dpipe_op_info
{
4109 struct dpipe_action
{
4110 struct dpipe_op_info info
;
4114 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4115 struct dpipe_ctx
*ctx
)
4117 struct dpipe_op_info
*op_info
= &action
->info
;
4118 const char *mapping
;
4120 pr_out_str(ctx
->dl
, "type",
4121 dpipe_action_type_e2s(action
->type
));
4122 pr_out_str(ctx
->dl
, "header",
4123 dpipe_header_id2s(ctx
, op_info
->header_id
,
4124 op_info
->header_global
));
4125 pr_out_str(ctx
->dl
, "field",
4126 dpipe_field_id2s(ctx
, op_info
->header_id
,
4128 op_info
->header_global
));
4129 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4131 op_info
->header_global
);
4133 pr_out_str(ctx
->dl
, "mapping", mapping
);
4136 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4138 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4141 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4142 if (err
!= MNL_CB_OK
)
4145 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4146 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4147 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4148 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4152 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4153 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4154 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4155 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4160 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4161 struct nlattr
*nla_actions
)
4163 struct nlattr
*nla_action
;
4164 struct dpipe_action action
;
4166 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4167 pr_out_entry_start(ctx
->dl
);
4168 if (dpipe_action_parse(&action
, nla_action
))
4169 goto err_action_parse
;
4170 pr_out_dpipe_action(&action
, ctx
);
4171 pr_out_entry_end(ctx
->dl
);
4176 pr_out_entry_end(ctx
->dl
);
4181 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4183 switch (match_type
) {
4184 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4185 return "field_exact";
4191 struct dpipe_match
{
4192 struct dpipe_op_info info
;
4196 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4197 struct dpipe_ctx
*ctx
)
4199 struct dpipe_op_info
*op_info
= &match
->info
;
4200 const char *mapping
;
4202 pr_out_str(ctx
->dl
, "type",
4203 dpipe_match_type_e2s(match
->type
));
4204 pr_out_str(ctx
->dl
, "header",
4205 dpipe_header_id2s(ctx
, op_info
->header_id
,
4206 op_info
->header_global
));
4207 pr_out_str(ctx
->dl
, "field",
4208 dpipe_field_id2s(ctx
, op_info
->header_id
,
4210 op_info
->header_global
));
4211 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4213 op_info
->header_global
);
4215 pr_out_str(ctx
->dl
, "mapping", mapping
);
4218 static int dpipe_match_parse(struct dpipe_match
*match
,
4222 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4225 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4226 if (err
!= MNL_CB_OK
)
4229 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4230 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4231 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4232 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4236 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4237 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4238 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4239 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4244 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4245 struct nlattr
*nla_matches
)
4247 struct nlattr
*nla_match
;
4248 struct dpipe_match match
;
4250 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4251 pr_out_entry_start(ctx
->dl
);
4252 if (dpipe_match_parse(&match
, nla_match
))
4253 goto err_match_parse
;
4254 pr_out_dpipe_match(&match
, ctx
);
4255 pr_out_entry_end(ctx
->dl
);
4260 pr_out_entry_end(ctx
->dl
);
4264 static struct resource
*
4265 resource_find(struct resources
*resources
, struct resource
*resource
,
4266 uint64_t resource_id
)
4268 struct list_head
*list_head
;
4271 list_head
= &resources
->resource_list
;
4273 list_head
= &resource
->resource_list
;
4275 list_for_each_entry(resource
, list_head
, list
) {
4276 struct resource
*child_resource
;
4278 if (resource
->id
== resource_id
)
4281 child_resource
= resource_find(resources
, resource
,
4284 return child_resource
;
4290 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4291 uint64_t resource_id
)
4293 struct resource
*resource
, *parent_resource
;
4294 const char del
[] = "/";
4298 resource
= resource_find(resources
, NULL
, resource_id
);
4302 for (parent_resource
= resource
; parent_resource
;
4303 parent_resource
= parent_resource
->parent
)
4304 path_len
+= strlen(parent_resource
->name
) + 1;
4307 path
= calloc(1, path_len
);
4311 path
+= path_len
- 1;
4312 for (parent_resource
= resource
; parent_resource
;
4313 parent_resource
= parent_resource
->parent
) {
4314 path
-= strlen(parent_resource
->name
);
4315 memcpy(path
, parent_resource
->name
,
4316 strlen(parent_resource
->name
));
4317 path
-= strlen(del
);
4318 memcpy(path
, del
, strlen(del
));
4320 pr_out_str(dl
, "resource_path", path
);
4324 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4326 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4327 struct dpipe_table
*table
;
4328 uint32_t resource_units
;
4329 bool counters_enabled
;
4330 bool resource_valid
;
4334 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4335 if (err
!= MNL_CB_OK
)
4338 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4339 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4340 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4341 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4342 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4346 table
= dpipe_table_alloc();
4350 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4351 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4352 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4354 resource_valid
= !!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
];
4355 if (resource_valid
) {
4356 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4357 table
->resource_valid
= true;
4360 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4361 if (!ctx
->print_tables
)
4364 pr_out_str(ctx
->dl
, "name", table
->name
);
4365 pr_out_uint(ctx
->dl
, "size", size
);
4366 pr_out_str(ctx
->dl
, "counters_enabled",
4367 counters_enabled
? "true" : "false");
4369 if (resource_valid
) {
4370 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4371 resource_path_print(ctx
->dl
, ctx
->resources
,
4372 table
->resource_id
);
4373 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4376 pr_out_array_start(ctx
->dl
, "match");
4377 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4378 goto err_matches_show
;
4379 pr_out_array_end(ctx
->dl
);
4381 pr_out_array_start(ctx
->dl
, "action");
4382 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4383 goto err_actions_show
;
4384 pr_out_array_end(ctx
->dl
);
4390 pr_out_array_end(ctx
->dl
);
4394 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4396 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4397 struct nlattr
*nla_table
;
4399 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4400 if (ctx
->print_tables
)
4401 pr_out_handle_start_arr(ctx
->dl
, tb
);
4402 if (dpipe_table_show(ctx
, nla_table
))
4403 goto err_table_show
;
4404 if (ctx
->print_tables
)
4405 pr_out_handle_end(ctx
->dl
);
4410 if (ctx
->print_tables
)
4411 pr_out_handle_end(ctx
->dl
);
4415 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4417 struct dpipe_ctx
*ctx
= data
;
4418 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4419 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4421 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4422 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4423 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4424 return MNL_CB_ERROR
;
4426 if (dpipe_tables_show(ctx
, tb
))
4427 return MNL_CB_ERROR
;
4431 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4433 static int cmd_dpipe_table_show(struct dl
*dl
)
4435 struct nlmsghdr
*nlh
;
4436 struct dpipe_ctx dpipe_ctx
= {};
4437 struct resource_ctx resource_ctx
= {};
4438 uint16_t flags
= NLM_F_REQUEST
;
4441 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4445 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4447 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4451 dpipe_ctx
.print_tables
= true;
4453 dl_opts_put(nlh
, dl
);
4454 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4457 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4458 goto err_headers_get
;
4461 err
= resource_ctx_init(&resource_ctx
, dl
);
4463 goto err_resource_ctx_init
;
4465 resource_ctx
.print_resources
= false;
4466 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4467 dl_opts_put(nlh
, dl
);
4468 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4471 pr_err("error get resources %s\n", strerror(resource_ctx
.err
));
4472 goto err_resource_dump
;
4475 dpipe_ctx
.resources
= resource_ctx
.resources
;
4476 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4477 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4478 dl_opts_put(nlh
, dl
);
4480 pr_out_section_start(dl
, "table");
4481 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4482 pr_out_section_end(dl
);
4484 resource_ctx_fini(&resource_ctx
);
4485 dpipe_ctx_fini(&dpipe_ctx
);
4489 resource_ctx_fini(&resource_ctx
);
4490 err_resource_ctx_init
:
4492 dpipe_ctx_fini(&dpipe_ctx
);
4496 static int cmd_dpipe_table_set(struct dl
*dl
)
4498 struct nlmsghdr
*nlh
;
4501 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4502 NLM_F_REQUEST
| NLM_F_ACK
);
4504 err
= dl_argv_parse_put(nlh
, dl
,
4505 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4506 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4510 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4513 enum dpipe_value_type
{
4514 DPIPE_VALUE_TYPE_VALUE
,
4515 DPIPE_VALUE_TYPE_MASK
,
4519 dpipe_value_type_e2s(enum dpipe_value_type type
)
4522 case DPIPE_VALUE_TYPE_VALUE
:
4524 case DPIPE_VALUE_TYPE_MASK
:
4525 return "value_mask";
4531 struct dpipe_field_printer
{
4532 unsigned int field_id
;
4533 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4536 struct dpipe_header_printer
{
4537 struct dpipe_field_printer
*printers
;
4538 unsigned int printers_count
;
4539 unsigned int header_id
;
4542 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4543 enum dpipe_value_type type
,
4546 struct in_addr ip_addr
;
4548 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4549 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4553 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4554 enum dpipe_value_type type
,
4557 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4558 ether_ntoa((struct ether_addr
*)value
));
4561 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4562 enum dpipe_value_type type
,
4565 char str
[INET6_ADDRSTRLEN
];
4567 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4568 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4571 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4573 .printer
= dpipe_field_printer_ipv4_addr
,
4574 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4578 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4579 .printers
= dpipe_field_printers_ipv4
,
4580 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4581 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4584 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4586 .printer
= dpipe_field_printer_ethernet_addr
,
4587 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4591 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4592 .printers
= dpipe_field_printers_ethernet
,
4593 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4594 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4597 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4599 .printer
= dpipe_field_printer_ipv6_addr
,
4600 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4604 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4605 .printers
= dpipe_field_printers_ipv6
,
4606 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4607 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4610 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4611 &dpipe_header_printer_ipv4
,
4612 &dpipe_header_printer_ethernet
,
4613 &dpipe_header_printer_ipv6
,
4616 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4617 struct dpipe_op_info
*info
,
4618 enum dpipe_value_type type
,
4621 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4622 struct dpipe_header_printer
*header_printer
;
4623 struct dpipe_field_printer
*field_printer
;
4624 unsigned int field_printers_count
;
4628 for (i
= 0; i
< header_printers_count
; i
++) {
4629 header_printer
= dpipe_header_printers
[i
];
4630 if (header_printer
->header_id
!= info
->header_id
)
4632 field_printers_count
= header_printer
->printers_count
;
4633 for (j
= 0; j
< field_printers_count
; j
++) {
4634 field_printer
= &header_printer
->printers
[j
];
4635 if (field_printer
->field_id
!= info
->field_id
)
4637 field_printer
->printer(ctx
, type
, value
);
4645 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4647 unsigned int value_len
,
4648 struct dpipe_op_info
*info
,
4649 enum dpipe_value_type type
)
4651 if (info
->header_global
&&
4652 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4655 if (value_len
== sizeof(uint32_t)) {
4656 uint32_t *value_32
= value
;
4658 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4662 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4663 struct nlattr
**nla_match_value
,
4664 struct dpipe_op_info
*info
)
4666 void *value
, *value_mask
;
4667 uint32_t value_mapping
;
4671 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4672 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4674 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4675 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4678 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4679 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4683 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4684 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4685 DPIPE_VALUE_TYPE_MASK
);
4688 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4691 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4694 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4695 struct dpipe_match match
;
4698 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4699 if (err
!= MNL_CB_OK
)
4702 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4703 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4707 pr_out_entry_start(ctx
->dl
);
4708 if (dpipe_match_parse(&match
,
4709 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4710 goto err_match_parse
;
4711 pr_out_dpipe_match(&match
, ctx
);
4712 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4713 pr_out_entry_end(ctx
->dl
);
4718 pr_out_entry_end(ctx
->dl
);
4722 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4725 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4726 struct dpipe_action action
;
4729 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4730 if (err
!= MNL_CB_OK
)
4733 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4734 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4738 pr_out_entry_start(ctx
->dl
);
4739 if (dpipe_action_parse(&action
,
4740 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4741 goto err_action_parse
;
4742 pr_out_dpipe_action(&action
, ctx
);
4743 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4744 pr_out_entry_end(ctx
->dl
);
4749 pr_out_entry_end(ctx
->dl
);
4754 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4755 struct nlattr
*nla_action_values
)
4757 struct nlattr
*nla_action_value
;
4759 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4760 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
4767 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
4768 struct nlattr
*nla_match_values
)
4770 struct nlattr
*nla_match_value
;
4772 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
4773 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
4779 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4781 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
4782 uint32_t entry_index
;
4786 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
4787 if (err
!= MNL_CB_OK
)
4790 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
4791 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
4792 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
4796 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
4797 pr_out_uint(ctx
->dl
, "index", entry_index
);
4799 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
4800 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
4801 pr_out_uint(ctx
->dl
, "counter", counter
);
4804 pr_out_array_start(ctx
->dl
, "match_value");
4805 if (dpipe_tables_match_values_show(ctx
,
4806 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
4807 goto err_match_values_show
;
4808 pr_out_array_end(ctx
->dl
);
4810 pr_out_array_start(ctx
->dl
, "action_value");
4811 if (dpipe_tables_action_values_show(ctx
,
4812 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
4813 goto err_action_values_show
;
4814 pr_out_array_end(ctx
->dl
);
4817 err_action_values_show
:
4818 err_match_values_show
:
4819 pr_out_array_end(ctx
->dl
);
4823 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4825 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
4826 struct nlattr
*nla_entry
;
4828 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
4829 pr_out_handle_start_arr(ctx
->dl
, tb
);
4830 if (dpipe_entry_show(ctx
, nla_entry
))
4831 goto err_entry_show
;
4832 pr_out_handle_end(ctx
->dl
);
4837 pr_out_handle_end(ctx
->dl
);
4841 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
4843 struct dpipe_ctx
*ctx
= data
;
4844 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4845 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4847 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4848 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4849 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
4850 return MNL_CB_ERROR
;
4852 if (dpipe_table_entries_show(ctx
, tb
))
4853 return MNL_CB_ERROR
;
4857 static int cmd_dpipe_table_dump(struct dl
*dl
)
4859 struct nlmsghdr
*nlh
;
4860 struct dpipe_ctx ctx
= {};
4861 uint16_t flags
= NLM_F_REQUEST
;
4864 err
= dpipe_ctx_init(&ctx
, dl
);
4868 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
4872 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4873 dl_opts_put(nlh
, dl
);
4874 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4876 pr_err("error get headers %s\n", strerror(ctx
.err
));
4880 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4881 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
4882 dl_opts_put(nlh
, dl
);
4884 pr_out_section_start(dl
, "table_entry");
4885 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
4886 pr_out_section_end(dl
);
4888 dpipe_ctx_fini(&ctx
);
4892 static void cmd_dpipe_table_help(void)
4894 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
4895 "where OBJECT-LIST := { show | set | dump }\n");
4898 static int cmd_dpipe_table(struct dl
*dl
)
4900 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4901 cmd_dpipe_table_help();
4903 } else if (dl_argv_match(dl
, "show")) {
4905 return cmd_dpipe_table_show(dl
);
4906 } else if (dl_argv_match(dl
, "set")) {
4908 return cmd_dpipe_table_set(dl
);
4909 } else if (dl_argv_match(dl
, "dump")) {
4911 return cmd_dpipe_table_dump(dl
);
4913 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4917 static void cmd_dpipe_help(void)
4919 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
4920 "where OBJECT-LIST := { header | table }\n");
4923 static int cmd_dpipe(struct dl
*dl
)
4925 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4928 } else if (dl_argv_match(dl
, "header")) {
4930 return cmd_dpipe_header(dl
);
4931 } else if (dl_argv_match(dl
, "table")) {
4933 return cmd_dpipe_table(dl
);
4935 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4940 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
4941 struct nlattr
**nla_resource
)
4943 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
4944 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
4945 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
4946 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
4947 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
4948 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
4949 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
4953 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
4954 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
4955 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
4956 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
4957 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
4958 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
4959 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
4961 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
4962 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
4964 resource
->size_new
= resource
->size
;
4966 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
4967 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
4968 resource
->occ_valid
= true;
4971 if (resource
->size_new
!= resource
->size
)
4972 ctx
->pending_change
= true;
4978 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
4979 struct resource
*parent_resource
, struct nlattr
*nl
)
4981 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
4982 struct nlattr
*nla_child_resource
;
4983 struct nlattr
*nla_resources
;
4993 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
4994 if (err
!= MNL_CB_OK
)
4997 err
= resource_parse(ctx
, resource
, nla_resource
);
5001 resource
->parent
= parent_resource
;
5002 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5005 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5006 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5008 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5009 struct resource
*child_resource
;
5010 struct list_head
*list
;
5012 child_resource
= resource_alloc();
5013 if (!child_resource
)
5017 list
= &ctx
->resources
->resource_list
;
5019 list
= &resource
->resource_list
;
5021 list_add_tail(&child_resource
->list
, list
);
5022 err
= resource_get(ctx
, child_resource
, resource
,
5023 nla_child_resource
);
5031 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5034 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5035 default: return "<unknown unit>";
5039 static void resource_show(struct resource
*resource
,
5040 struct resource_ctx
*ctx
)
5042 struct resource
*child_resource
;
5043 struct dpipe_table
*table
;
5044 struct dl
*dl
= ctx
->dl
;
5047 pr_out_str(dl
, "name", resource
->name
);
5049 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5050 pr_out_u64(dl
, "size", resource
->size
);
5051 if (resource
->size
!= resource
->size_new
)
5052 pr_out_u64(dl
, "size_new", resource
->size_new
);
5053 if (resource
->occ_valid
)
5054 pr_out_uint(dl
, "occ", resource
->size_occ
);
5055 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5057 if (resource
->size_min
!= resource
->size_max
) {
5058 pr_out_uint(dl
, "size_min", resource
->size_min
);
5059 pr_out_u64(dl
, "size_max", resource
->size_max
);
5060 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5063 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5064 if (table
->resource_id
== resource
->id
&&
5065 table
->resource_valid
)
5069 pr_out_array_start(dl
, "dpipe_tables");
5071 pr_out_str(dl
, "dpipe_tables", "none");
5073 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5074 if (table
->resource_id
!= resource
->id
||
5075 !table
->resource_valid
)
5077 pr_out_entry_start(dl
);
5078 pr_out_str(dl
, "table_name", table
->name
);
5079 pr_out_entry_end(dl
);
5082 pr_out_array_end(dl
);
5084 if (list_empty(&resource
->resource_list
))
5087 if (ctx
->pending_change
)
5088 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5090 pr_out_array_start(dl
, "resources");
5091 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5092 pr_out_entry_start(dl
);
5093 resource_show(child_resource
, ctx
);
5094 pr_out_entry_end(dl
);
5096 pr_out_array_end(dl
);
5100 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5102 struct resources
*resources
= ctx
->resources
;
5103 struct resource
*resource
;
5105 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5106 pr_out_handle_start_arr(ctx
->dl
, tb
);
5107 resource_show(resource
, ctx
);
5108 pr_out_handle_end(ctx
->dl
);
5112 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5114 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5117 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5119 struct resource_ctx
*ctx
= data
;
5120 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5121 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5124 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5125 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5126 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5127 return MNL_CB_ERROR
;
5129 err
= resources_get(ctx
, tb
);
5132 return MNL_CB_ERROR
;
5135 if (ctx
->print_resources
)
5136 resources_show(ctx
, tb
);
5141 static int cmd_resource_show(struct dl
*dl
)
5143 struct nlmsghdr
*nlh
;
5144 struct dpipe_ctx dpipe_ctx
= {};
5145 struct resource_ctx resource_ctx
= {};
5148 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5152 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5154 dl_opts_put(nlh
, dl
);
5156 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5160 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5163 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5167 err
= resource_ctx_init(&resource_ctx
, dl
);
5171 resource_ctx
.print_resources
= true;
5172 resource_ctx
.tables
= dpipe_ctx
.tables
;
5173 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5174 NLM_F_REQUEST
| NLM_F_ACK
);
5175 dl_opts_put(nlh
, dl
);
5176 pr_out_section_start(dl
, "resources");
5177 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5179 pr_out_section_end(dl
);
5180 resource_ctx_fini(&resource_ctx
);
5182 dpipe_ctx_fini(&dpipe_ctx
);
5186 static void cmd_resource_help(void)
5188 pr_err("Usage: devlink resource show DEV\n"
5189 " devlink resource set DEV path PATH size SIZE\n");
5192 static struct resource
*
5193 resource_find_by_name(struct list_head
*list
, char *name
)
5195 struct resource
*resource
;
5197 list_for_each_entry(resource
, list
, list
) {
5198 if (!strcmp(resource
->name
, name
))
5205 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5206 uint32_t *p_resource_id
, bool *p_resource_valid
)
5208 struct resource
*resource
;
5209 uint32_t resource_id
= 0;
5210 char *resource_path_dup
;
5211 struct list_head
*list
;
5212 const char del
[] = "/";
5213 char *resource_name
;
5215 resource_path_dup
= strdup(resource_path
);
5216 list
= &ctx
->resources
->resource_list
;
5217 resource_name
= strtok(resource_path_dup
, del
);
5218 while (resource_name
!= NULL
) {
5219 resource
= resource_find_by_name(list
, resource_name
);
5221 goto err_resource_lookup
;
5223 list
= &resource
->resource_list
;
5224 resource_name
= strtok(NULL
, del
);
5225 resource_id
= resource
->id
;
5227 free(resource_path_dup
);
5228 *p_resource_valid
= true;
5229 *p_resource_id
= resource_id
;
5232 err_resource_lookup
:
5233 free(resource_path_dup
);
5237 static int cmd_resource_set(struct dl
*dl
)
5239 struct nlmsghdr
*nlh
;
5240 struct resource_ctx ctx
= {};
5243 err
= resource_ctx_init(&ctx
, dl
);
5247 ctx
.print_resources
= false;
5248 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5249 DL_OPT_RESOURCE_SIZE
, 0);
5253 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5255 dl_opts_put(nlh
, dl
);
5256 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5258 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5262 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5263 &dl
->opts
.resource_id
,
5264 &dl
->opts
.resource_id_valid
);
5266 pr_err("error parsing resource path %s\n", strerror(-err
));
5270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5271 NLM_F_REQUEST
| NLM_F_ACK
);
5273 dl_opts_put(nlh
, dl
);
5274 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5276 resource_ctx_fini(&ctx
);
5280 static int cmd_resource(struct dl
*dl
)
5282 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5283 cmd_resource_help();
5285 } else if (dl_argv_match(dl
, "show")) {
5287 return cmd_resource_show(dl
);
5288 } else if (dl_argv_match(dl
, "set")) {
5290 return cmd_resource_set(dl
);
5292 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5296 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5298 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5299 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5300 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5303 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5304 if (dl
->json_output
) {
5305 jsonw_name(dl
->jw
, buf
);
5306 jsonw_start_object(dl
->jw
);
5312 static void pr_out_region_handle_end(struct dl
*dl
)
5314 if (dl
->json_output
)
5315 jsonw_end_object(dl
->jw
);
5320 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5322 if (dl
->json_output
) {
5323 jsonw_name(dl
->jw
, "snapshot");
5324 jsonw_start_array(dl
->jw
);
5326 if (g_indent_newline
)
5327 pr_out("snapshot %s", array
? "[" : "");
5329 pr_out(" snapshot %s", array
? "[" : "");
5333 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5335 if (dl
->json_output
)
5336 jsonw_end_array(dl
->jw
);
5341 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5343 uint32_t snapshot_id
;
5345 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5348 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5350 if (dl
->json_output
)
5351 jsonw_uint(dl
->jw
, snapshot_id
);
5353 pr_out("%s%u", index
? " " : "", snapshot_id
);
5356 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5358 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5359 struct nlattr
*nla_sanpshot
;
5362 pr_out_region_snapshots_start(dl
, true);
5363 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5364 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5365 if (err
!= MNL_CB_OK
)
5367 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5369 pr_out_region_snapshots_end(dl
, true);
5372 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5374 pr_out_region_snapshots_start(dl
, false);
5375 pr_out_region_snapshots_id(dl
, tb
, 0);
5376 pr_out_region_snapshots_end(dl
, false);
5379 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5381 pr_out_region_handle_start(dl
, tb
);
5383 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5384 pr_out_u64(dl
, "size",
5385 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5387 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5388 pr_out_snapshots(dl
, tb
);
5390 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5391 pr_out_snapshot(dl
, tb
);
5393 pr_out_region_handle_end(dl
);
5396 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5398 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5399 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5400 struct dl
*dl
= data
;
5402 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5403 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5404 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5405 return MNL_CB_ERROR
;
5407 pr_out_region(dl
, tb
);
5412 static int cmd_region_show(struct dl
*dl
)
5414 struct nlmsghdr
*nlh
;
5415 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5418 if (dl_argc(dl
) == 0)
5419 flags
|= NLM_F_DUMP
;
5421 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5423 if (dl_argc(dl
) > 0) {
5424 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5429 pr_out_section_start(dl
, "regions");
5430 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5431 pr_out_section_end(dl
);
5435 static int cmd_region_snapshot_del(struct dl
*dl
)
5437 struct nlmsghdr
*nlh
;
5440 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5441 NLM_F_REQUEST
| NLM_F_ACK
);
5443 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5444 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5448 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5451 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5453 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5454 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5455 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5456 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5457 struct dl
*dl
= data
;
5460 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5461 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5462 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5463 return MNL_CB_ERROR
;
5465 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5466 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5467 if (err
!= MNL_CB_OK
)
5468 return MNL_CB_ERROR
;
5470 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5471 if (!nla_chunk_data
)
5474 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5475 if (!nla_chunk_addr
)
5478 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5479 mnl_attr_get_payload_len(nla_chunk_data
),
5480 mnl_attr_get_u64(nla_chunk_addr
));
5485 static int cmd_region_dump(struct dl
*dl
)
5487 struct nlmsghdr
*nlh
;
5490 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5491 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5493 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5494 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5498 pr_out_section_start(dl
, "dump");
5499 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5500 pr_out_section_end(dl
);
5501 if (!dl
->json_output
)
5506 static int cmd_region_read(struct dl
*dl
)
5508 struct nlmsghdr
*nlh
;
5511 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5512 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5514 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5515 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5516 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5520 pr_out_section_start(dl
, "read");
5521 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5522 pr_out_section_end(dl
);
5523 if (!dl
->json_output
)
5528 static void cmd_region_help(void)
5530 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5531 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5532 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5533 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5536 static int cmd_region(struct dl
*dl
)
5538 if (dl_no_arg(dl
)) {
5539 return cmd_region_show(dl
);
5540 } else if (dl_argv_match(dl
, "help")) {
5543 } else if (dl_argv_match(dl
, "show")) {
5545 return cmd_region_show(dl
);
5546 } else if (dl_argv_match(dl
, "del")) {
5548 return cmd_region_snapshot_del(dl
);
5549 } else if (dl_argv_match(dl
, "dump")) {
5551 return cmd_region_dump(dl
);
5552 } else if (dl_argv_match(dl
, "read")) {
5554 return cmd_region_read(dl
);
5556 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5560 static void help(void)
5562 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5563 " devlink [ -f[orce] ] -b[atch] filename\n"
5564 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5565 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5568 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5573 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5576 } else if (dl_argv_match(dl
, "dev")) {
5579 } else if (dl_argv_match(dl
, "port")) {
5581 return cmd_port(dl
);
5582 } else if (dl_argv_match(dl
, "sb")) {
5585 } else if (dl_argv_match(dl
, "monitor")) {
5588 } else if (dl_argv_match(dl
, "dpipe")) {
5590 return cmd_dpipe(dl
);
5591 } else if (dl_argv_match(dl
, "resource")) {
5593 return cmd_resource(dl
);
5594 } else if (dl_argv_match(dl
, "region")) {
5596 return cmd_region(dl
);
5598 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5602 static int dl_init(struct dl
*dl
)
5606 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5608 pr_err("Failed to connect to devlink Netlink\n");
5612 err
= ifname_map_init(dl
);
5614 pr_err("Failed to create index map\n");
5615 goto err_ifname_map_create
;
5617 if (dl
->json_output
) {
5618 dl
->jw
= jsonw_new(stdout
);
5620 pr_err("Failed to create JSON writer\n");
5623 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5628 ifname_map_fini(dl
);
5629 err_ifname_map_create
:
5630 mnlg_socket_close(dl
->nlg
);
5634 static void dl_fini(struct dl
*dl
)
5636 if (dl
->json_output
)
5637 jsonw_destroy(&dl
->jw
);
5638 ifname_map_fini(dl
);
5639 mnlg_socket_close(dl
->nlg
);
5642 static struct dl
*dl_alloc(void)
5646 dl
= calloc(1, sizeof(*dl
));
5652 static void dl_free(struct dl
*dl
)
5657 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5661 int ret
= EXIT_SUCCESS
;
5663 if (name
&& strcmp(name
, "-") != 0) {
5664 if (freopen(name
, "r", stdin
) == NULL
) {
5666 "Cannot open file \"%s\" for reading: %s\n",
5667 name
, strerror(errno
));
5668 return EXIT_FAILURE
;
5673 while (getcmdline(&line
, &len
, stdin
) != -1) {
5677 largc
= makeargs(line
, largv
, 100);
5679 continue; /* blank line */
5681 if (dl_cmd(dl
, largc
, largv
)) {
5682 fprintf(stderr
, "Command failed %s:%d\n",
5696 int main(int argc
, char **argv
)
5698 static const struct option long_options
[] = {
5699 { "Version", no_argument
, NULL
, 'V' },
5700 { "force", no_argument
, NULL
, 'f' },
5701 { "batch", required_argument
, NULL
, 'b' },
5702 { "no-nice-names", no_argument
, NULL
, 'n' },
5703 { "json", no_argument
, NULL
, 'j' },
5704 { "pretty", no_argument
, NULL
, 'p' },
5705 { "verbose", no_argument
, NULL
, 'v' },
5706 { NULL
, 0, NULL
, 0 }
5708 const char *batch_file
= NULL
;
5717 pr_err("Failed to allocate memory for devlink\n");
5718 return EXIT_FAILURE
;
5721 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5722 long_options
, NULL
)) >= 0) {
5726 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5733 batch_file
= optarg
;
5736 dl
->no_nice_names
= true;
5739 dl
->json_output
= true;
5742 dl
->pretty_output
= true;
5748 pr_err("Unknown option.\n");
5765 err
= dl_batch(dl
, batch_file
, force
);
5767 err
= dl_cmd(dl
, argc
, argv
);