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 static void pr_out_param_value(struct dl
*dl
, int nla_type
, struct nlattr
*nl
)
1925 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
1926 struct nlattr
*val_attr
;
1929 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
1930 if (err
!= MNL_CB_OK
)
1933 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
1934 (nla_type
!= MNL_TYPE_FLAG
&&
1935 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
1938 pr_out_str(dl
, "cmode",
1939 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
1940 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
1944 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
1947 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
1950 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
1952 case MNL_TYPE_STRING
:
1953 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
1956 pr_out_bool(dl
, "value", val_attr
? true : false);
1961 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
1963 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
1964 struct nlattr
*param_value_attr
;
1968 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
1969 if (err
!= MNL_CB_OK
)
1971 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
1972 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
1973 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
1977 pr_out_handle_start_arr(dl
, tb
);
1979 __pr_out_handle_start(dl
, tb
, true, false);
1981 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
1983 pr_out_str(dl
, "name",
1984 mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]));
1986 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
1987 pr_out_str(dl
, "type", "driver-specific");
1989 pr_out_str(dl
, "type", "generic");
1991 pr_out_array_start(dl
, "values");
1992 mnl_attr_for_each_nested(param_value_attr
,
1993 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
1994 pr_out_entry_start(dl
);
1995 pr_out_param_value(dl
, nla_type
, param_value_attr
);
1996 pr_out_entry_end(dl
);
1998 pr_out_array_end(dl
);
1999 pr_out_handle_end(dl
);
2002 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2004 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2005 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2006 struct dl
*dl
= data
;
2008 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2009 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2010 !tb
[DEVLINK_ATTR_PARAM
])
2011 return MNL_CB_ERROR
;
2012 pr_out_param(dl
, tb
, true);
2028 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2030 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2031 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2032 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2033 struct nlattr
*param_value_attr
;
2034 enum devlink_param_cmode cmode
;
2035 struct param_ctx
*ctx
= data
;
2036 struct dl
*dl
= ctx
->dl
;
2040 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2041 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2042 !tb
[DEVLINK_ATTR_PARAM
])
2043 return MNL_CB_ERROR
;
2045 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2046 if (err
!= MNL_CB_OK
)
2047 return MNL_CB_ERROR
;
2049 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2050 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2051 return MNL_CB_ERROR
;
2053 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2054 mnl_attr_for_each_nested(param_value_attr
,
2055 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2056 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2057 struct nlattr
*val_attr
;
2059 err
= mnl_attr_parse_nested(param_value_attr
,
2060 attr_cb
, nla_value
);
2061 if (err
!= MNL_CB_OK
)
2062 return MNL_CB_ERROR
;
2064 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2065 (nla_type
!= MNL_TYPE_FLAG
&&
2066 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2067 return MNL_CB_ERROR
;
2069 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2070 if (cmode
== dl
->opts
.cmode
) {
2071 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2074 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2077 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2080 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2082 case MNL_TYPE_STRING
:
2083 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2086 ctx
->value
.vbool
= val_attr
? true : false;
2092 ctx
->nla_type
= nla_type
;
2096 static int cmd_dev_param_set(struct dl
*dl
)
2098 struct param_ctx ctx
= {};
2099 struct nlmsghdr
*nlh
;
2106 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2108 DL_OPT_PARAM_VALUE
|
2109 DL_OPT_PARAM_CMODE
, 0);
2113 /* Get value type */
2114 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2115 NLM_F_REQUEST
| NLM_F_ACK
);
2116 dl_opts_put(nlh
, dl
);
2119 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2123 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2124 NLM_F_REQUEST
| NLM_F_ACK
);
2125 dl_opts_put(nlh
, dl
);
2127 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2128 switch (ctx
.nla_type
) {
2130 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2132 goto err_param_value_parse
;
2133 if (val_u8
== ctx
.value
.vu8
)
2135 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2138 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2140 goto err_param_value_parse
;
2141 if (val_u16
== ctx
.value
.vu16
)
2143 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2146 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2148 goto err_param_value_parse
;
2149 if (val_u32
== ctx
.value
.vu32
)
2151 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2154 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2156 goto err_param_value_parse
;
2157 if (val_bool
== ctx
.value
.vbool
)
2160 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2163 case MNL_TYPE_STRING
:
2164 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2165 dl
->opts
.param_value
);
2166 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2170 printf("Value type not supported\n");
2173 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2175 err_param_value_parse
:
2176 pr_err("Value \"%s\" is not a number or not within range\n",
2177 dl
->opts
.param_value
);
2181 static int cmd_dev_param_show(struct dl
*dl
)
2183 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2184 struct nlmsghdr
*nlh
;
2187 if (dl_argc(dl
) == 0)
2188 flags
|= NLM_F_DUMP
;
2190 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2192 if (dl_argc(dl
) > 0) {
2193 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2194 DL_OPT_PARAM_NAME
, 0);
2199 pr_out_section_start(dl
, "param");
2200 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2201 pr_out_section_end(dl
);
2205 static int cmd_dev_param(struct dl
*dl
)
2207 if (dl_argv_match(dl
, "help")) {
2210 } else if (dl_argv_match(dl
, "show") ||
2211 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2213 return cmd_dev_param_show(dl
);
2214 } else if (dl_argv_match(dl
, "set")) {
2216 return cmd_dev_param_set(dl
);
2218 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2221 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2223 struct dl
*dl
= data
;
2224 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2225 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2227 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2228 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2229 return MNL_CB_ERROR
;
2234 static int cmd_dev_show(struct dl
*dl
)
2236 struct nlmsghdr
*nlh
;
2237 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2240 if (dl_argc(dl
) == 0)
2241 flags
|= NLM_F_DUMP
;
2243 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2245 if (dl_argc(dl
) > 0) {
2246 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2251 pr_out_section_start(dl
, "dev");
2252 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2253 pr_out_section_end(dl
);
2257 static void cmd_dev_reload_help(void)
2259 pr_err("Usage: devlink dev reload [ DEV ]\n");
2262 static int cmd_dev_reload(struct dl
*dl
)
2264 struct nlmsghdr
*nlh
;
2267 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2268 cmd_dev_reload_help();
2272 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2273 NLM_F_REQUEST
| NLM_F_ACK
);
2275 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2279 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2282 static int cmd_dev(struct dl
*dl
)
2284 if (dl_argv_match(dl
, "help")) {
2287 } else if (dl_argv_match(dl
, "show") ||
2288 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2290 return cmd_dev_show(dl
);
2291 } else if (dl_argv_match(dl
, "eswitch")) {
2293 return cmd_dev_eswitch(dl
);
2294 } else if (dl_argv_match(dl
, "reload")) {
2296 return cmd_dev_reload(dl
);
2297 } else if (dl_argv_match(dl
, "param")) {
2299 return cmd_dev_param(dl
);
2301 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2305 static void cmd_port_help(void)
2307 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2308 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2309 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2310 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2313 static const char *port_type_name(uint32_t type
)
2316 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2317 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2318 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2319 case DEVLINK_PORT_TYPE_IB
: return "ib";
2320 default: return "<unknown type>";
2324 static const char *port_flavour_name(uint16_t flavour
)
2327 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2329 case DEVLINK_PORT_FLAVOUR_CPU
:
2331 case DEVLINK_PORT_FLAVOUR_DSA
:
2334 return "<unknown flavour>";
2338 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2340 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2341 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2343 pr_out_port_handle_start(dl
, tb
, false);
2345 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2347 pr_out_str(dl
, "type", port_type_name(port_type
));
2349 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2351 if (port_type
!= des_port_type
)
2352 pr_out_str(dl
, "des_type",
2353 port_type_name(des_port_type
));
2356 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2357 pr_out_str(dl
, "netdev",
2358 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2359 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2360 pr_out_str(dl
, "ibdev",
2361 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2362 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2363 uint16_t port_flavour
=
2364 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2366 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2368 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2369 pr_out_uint(dl
, "split_group",
2370 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2371 pr_out_port_handle_end(dl
);
2374 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2376 struct dl
*dl
= data
;
2377 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2378 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2380 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2381 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2382 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2383 return MNL_CB_ERROR
;
2384 pr_out_port(dl
, tb
);
2388 static int cmd_port_show(struct dl
*dl
)
2390 struct nlmsghdr
*nlh
;
2391 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2394 if (dl_argc(dl
) == 0)
2395 flags
|= NLM_F_DUMP
;
2397 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2399 if (dl_argc(dl
) > 0) {
2400 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2405 pr_out_section_start(dl
, "port");
2406 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2407 pr_out_section_end(dl
);
2411 static int cmd_port_set(struct dl
*dl
)
2413 struct nlmsghdr
*nlh
;
2416 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2417 NLM_F_REQUEST
| NLM_F_ACK
);
2419 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2423 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2426 static int cmd_port_split(struct dl
*dl
)
2428 struct nlmsghdr
*nlh
;
2431 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2432 NLM_F_REQUEST
| NLM_F_ACK
);
2434 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2438 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2441 static int cmd_port_unsplit(struct dl
*dl
)
2443 struct nlmsghdr
*nlh
;
2446 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2447 NLM_F_REQUEST
| NLM_F_ACK
);
2449 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2453 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2456 static int cmd_port(struct dl
*dl
)
2458 if (dl_argv_match(dl
, "help")) {
2461 } else if (dl_argv_match(dl
, "show") ||
2462 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2464 return cmd_port_show(dl
);
2465 } else if (dl_argv_match(dl
, "set")) {
2467 return cmd_port_set(dl
);
2468 } else if (dl_argv_match(dl
, "split")) {
2470 return cmd_port_split(dl
);
2471 } else if (dl_argv_match(dl
, "unsplit")) {
2473 return cmd_port_unsplit(dl
);
2475 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2479 static void cmd_sb_help(void)
2481 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2482 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2483 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2484 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2485 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2486 pr_err(" pool POOL_INDEX ]\n");
2487 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2488 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2489 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2490 pr_err(" type { ingress | egress } ]\n");
2491 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2492 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2493 pr_err(" th THRESHOLD\n");
2494 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2495 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2496 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2499 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2501 pr_out_handle_start_arr(dl
, tb
);
2502 pr_out_uint(dl
, "sb",
2503 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2504 pr_out_uint(dl
, "size",
2505 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2506 pr_out_uint(dl
, "ing_pools",
2507 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2508 pr_out_uint(dl
, "eg_pools",
2509 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2510 pr_out_uint(dl
, "ing_tcs",
2511 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2512 pr_out_uint(dl
, "eg_tcs",
2513 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2514 pr_out_handle_end(dl
);
2517 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2519 struct dl
*dl
= data
;
2520 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2521 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2523 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2524 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2525 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2526 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2527 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2528 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2529 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2530 return MNL_CB_ERROR
;
2535 static int cmd_sb_show(struct dl
*dl
)
2537 struct nlmsghdr
*nlh
;
2538 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2541 if (dl_argc(dl
) == 0)
2542 flags
|= NLM_F_DUMP
;
2544 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
2546 if (dl_argc(dl
) > 0) {
2547 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2552 pr_out_section_start(dl
, "sb");
2553 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
2554 pr_out_section_end(dl
);
2558 static const char *pool_type_name(uint8_t type
)
2561 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
2562 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
2563 default: return "<unknown type>";
2567 static const char *threshold_type_name(uint8_t type
)
2570 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
2571 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
2572 default: return "<unknown type>";
2576 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
2578 pr_out_handle_start_arr(dl
, tb
);
2579 pr_out_uint(dl
, "sb",
2580 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2581 pr_out_uint(dl
, "pool",
2582 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2583 pr_out_str(dl
, "type",
2584 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2585 pr_out_uint(dl
, "size",
2586 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
2587 pr_out_str(dl
, "thtype",
2588 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
2589 pr_out_handle_end(dl
);
2592 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2594 struct dl
*dl
= data
;
2595 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2596 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2598 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2599 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2600 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2601 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
2602 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
2603 return MNL_CB_ERROR
;
2604 pr_out_sb_pool(dl
, tb
);
2608 static int cmd_sb_pool_show(struct dl
*dl
)
2610 struct nlmsghdr
*nlh
;
2611 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2614 if (dl_argc(dl
) == 0)
2615 flags
|= NLM_F_DUMP
;
2617 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
2619 if (dl_argc(dl
) > 0) {
2620 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
2626 pr_out_section_start(dl
, "pool");
2627 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2628 pr_out_section_end(dl
);
2632 static int cmd_sb_pool_set(struct dl
*dl
)
2634 struct nlmsghdr
*nlh
;
2637 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
2638 NLM_F_REQUEST
| NLM_F_ACK
);
2640 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
2641 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
2645 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2648 static int cmd_sb_pool(struct dl
*dl
)
2650 if (dl_argv_match(dl
, "help")) {
2653 } else if (dl_argv_match(dl
, "show") ||
2654 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2656 return cmd_sb_pool_show(dl
);
2657 } else if (dl_argv_match(dl
, "set")) {
2659 return cmd_sb_pool_set(dl
);
2661 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2665 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
2667 pr_out_port_handle_start_arr(dl
, tb
, true);
2668 pr_out_uint(dl
, "sb",
2669 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2670 pr_out_uint(dl
, "pool",
2671 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2672 pr_out_uint(dl
, "threshold",
2673 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2674 pr_out_port_handle_end(dl
);
2677 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2679 struct dl
*dl
= data
;
2680 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2681 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2683 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2684 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2685 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2686 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2687 return MNL_CB_ERROR
;
2688 pr_out_sb_port_pool(dl
, tb
);
2692 static int cmd_sb_port_pool_show(struct dl
*dl
)
2694 struct nlmsghdr
*nlh
;
2695 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2698 if (dl_argc(dl
) == 0)
2699 flags
|= NLM_F_DUMP
;
2701 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2703 if (dl_argc(dl
) > 0) {
2704 err
= dl_argv_parse_put(nlh
, dl
,
2705 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
2711 pr_out_section_start(dl
, "port_pool");
2712 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
2713 pr_out_section_end(dl
);
2717 static int cmd_sb_port_pool_set(struct dl
*dl
)
2719 struct nlmsghdr
*nlh
;
2722 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2723 NLM_F_REQUEST
| NLM_F_ACK
);
2725 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2726 DL_OPT_SB_TH
, DL_OPT_SB
);
2730 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2733 static int cmd_sb_port_pool(struct dl
*dl
)
2735 if (dl_argv_match(dl
, "help")) {
2738 } else if (dl_argv_match(dl
, "show") ||
2739 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2741 return cmd_sb_port_pool_show(dl
);
2742 } else if (dl_argv_match(dl
, "set")) {
2744 return cmd_sb_port_pool_set(dl
);
2746 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2750 static int cmd_sb_port(struct dl
*dl
)
2752 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2755 } else if (dl_argv_match(dl
, "pool")) {
2757 return cmd_sb_port_pool(dl
);
2759 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2763 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2765 pr_out_port_handle_start_arr(dl
, tb
, true);
2766 pr_out_uint(dl
, "sb",
2767 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2768 pr_out_uint(dl
, "tc",
2769 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2770 pr_out_str(dl
, "type",
2771 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2772 pr_out_uint(dl
, "pool",
2773 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2774 pr_out_uint(dl
, "threshold",
2775 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2776 pr_out_port_handle_end(dl
);
2779 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2781 struct dl
*dl
= data
;
2782 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2783 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2785 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2786 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2787 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2788 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2789 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2790 return MNL_CB_ERROR
;
2791 pr_out_sb_tc_bind(dl
, tb
);
2795 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2797 struct nlmsghdr
*nlh
;
2798 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2801 if (dl_argc(dl
) == 0)
2802 flags
|= NLM_F_DUMP
;
2804 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2806 if (dl_argc(dl
) > 0) {
2807 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2808 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2813 pr_out_section_start(dl
, "tc_bind");
2814 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2815 pr_out_section_end(dl
);
2819 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2821 struct nlmsghdr
*nlh
;
2824 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2825 NLM_F_REQUEST
| NLM_F_ACK
);
2827 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2828 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2833 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2836 static int cmd_sb_tc_bind(struct dl
*dl
)
2838 if (dl_argv_match(dl
, "help")) {
2841 } else if (dl_argv_match(dl
, "show") ||
2842 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2844 return cmd_sb_tc_bind_show(dl
);
2845 } else if (dl_argv_match(dl
, "set")) {
2847 return cmd_sb_tc_bind_set(dl
);
2849 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2853 static int cmd_sb_tc(struct dl
*dl
)
2855 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2858 } else if (dl_argv_match(dl
, "bind")) {
2860 return cmd_sb_tc_bind(dl
);
2862 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2867 struct list_head list
;
2871 uint32_t bound_pool_index
;
2875 struct list_head list
;
2878 uint32_t port_index
;
2880 struct list_head pool_list
;
2881 struct list_head ing_tc_list
;
2882 struct list_head eg_tc_list
;
2888 struct list_head port_list
;
2891 static struct occ_item
*occ_item_alloc(void)
2893 return calloc(1, sizeof(struct occ_item
));
2896 static void occ_item_free(struct occ_item
*occ_item
)
2901 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
2903 struct occ_port
*occ_port
;
2905 occ_port
= calloc(1, sizeof(*occ_port
));
2908 occ_port
->port_index
= port_index
;
2909 INIT_LIST_HEAD(&occ_port
->pool_list
);
2910 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
2911 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2915 static void occ_port_free(struct occ_port
*occ_port
)
2917 struct occ_item
*occ_item
, *tmp
;
2919 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2920 occ_item_free(occ_item
);
2921 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2922 occ_item_free(occ_item
);
2923 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2924 occ_item_free(occ_item
);
2927 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2929 struct occ_show
*occ_show
;
2931 occ_show
= calloc(1, sizeof(*occ_show
));
2935 INIT_LIST_HEAD(&occ_show
->port_list
);
2939 static void occ_show_free(struct occ_show
*occ_show
)
2941 struct occ_port
*occ_port
, *tmp
;
2943 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2944 occ_port_free(occ_port
);
2947 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2950 struct occ_port
*occ_port
;
2951 uint32_t port_index
;
2953 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2955 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2956 if (occ_port
->port_index
== port_index
)
2959 occ_port
= occ_port_alloc(port_index
);
2962 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2966 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2969 struct occ_item
*occ_item
;
2972 pr_out_sp(7, " %s:", label
);
2973 list_for_each_entry(occ_item
, list
, list
) {
2974 if ((i
- 1) % 4 == 0 && i
!= 1)
2977 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2978 occ_item
->bound_pool_index
);
2980 pr_out_sp(7, "%2u:", occ_item
->index
);
2981 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2985 if ((i
- 1) % 4 != 0)
2989 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2990 struct list_head
*list
,
2993 struct occ_item
*occ_item
;
2996 jsonw_name(dl
->jw
, label
);
2997 jsonw_start_object(dl
->jw
);
2998 list_for_each_entry(occ_item
, list
, list
) {
2999 sprintf(buf
, "%u", occ_item
->index
);
3000 jsonw_name(dl
->jw
, buf
);
3001 jsonw_start_object(dl
->jw
);
3003 jsonw_uint_field(dl
->jw
, "bound_pool",
3004 occ_item
->bound_pool_index
);
3005 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3006 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3007 jsonw_end_object(dl
->jw
);
3009 jsonw_end_object(dl
->jw
);
3012 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3014 if (dl
->json_output
) {
3015 pr_out_json_occ_show_item_list(dl
, "pool",
3016 &occ_port
->pool_list
, false);
3017 pr_out_json_occ_show_item_list(dl
, "itc",
3018 &occ_port
->ing_tc_list
, true);
3019 pr_out_json_occ_show_item_list(dl
, "etc",
3020 &occ_port
->eg_tc_list
, true);
3023 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3024 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3025 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3029 static void pr_out_occ_show(struct occ_show
*occ_show
)
3031 struct dl
*dl
= occ_show
->dl
;
3032 struct dl_opts
*opts
= &dl
->opts
;
3033 struct occ_port
*occ_port
;
3035 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3036 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3037 occ_port
->port_index
, true, false);
3038 pr_out_occ_show_port(dl
, occ_port
);
3039 pr_out_port_handle_end(dl
);
3043 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3046 struct occ_port
*occ_port
;
3047 struct occ_item
*occ_item
;
3049 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3052 occ_port
= occ_port_get(occ_show
, tb
);
3054 occ_show
->err
= -ENOMEM
;
3058 occ_item
= occ_item_alloc();
3060 occ_show
->err
= -ENOMEM
;
3063 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3064 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3065 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3066 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3069 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3071 struct occ_show
*occ_show
= data
;
3072 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3073 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3075 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3076 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3077 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3078 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3079 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3080 return MNL_CB_ERROR
;
3081 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3085 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3088 struct occ_port
*occ_port
;
3089 struct occ_item
*occ_item
;
3092 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3095 occ_port
= occ_port_get(occ_show
, tb
);
3097 occ_show
->err
= -ENOMEM
;
3101 occ_item
= occ_item_alloc();
3103 occ_show
->err
= -ENOMEM
;
3106 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3107 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3108 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3109 occ_item
->bound_pool_index
=
3110 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3111 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3112 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3113 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3114 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3115 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3117 occ_item_free(occ_item
);
3120 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3122 struct occ_show
*occ_show
= data
;
3123 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3124 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3126 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3127 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3128 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3129 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3130 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3131 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3132 return MNL_CB_ERROR
;
3133 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3137 static int cmd_sb_occ_show(struct dl
*dl
)
3139 struct nlmsghdr
*nlh
;
3140 struct occ_show
*occ_show
;
3141 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3144 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3148 occ_show
= occ_show_alloc(dl
);
3152 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3154 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3155 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3159 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3161 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3162 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3166 pr_out_section_start(dl
, "occupancy");
3167 pr_out_occ_show(occ_show
);
3168 pr_out_section_end(dl
);
3171 occ_show_free(occ_show
);
3175 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3177 struct nlmsghdr
*nlh
;
3180 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3181 NLM_F_REQUEST
| NLM_F_ACK
);
3183 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3187 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3190 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3192 struct nlmsghdr
*nlh
;
3195 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3196 NLM_F_REQUEST
| NLM_F_ACK
);
3198 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3202 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3205 static int cmd_sb_occ(struct dl
*dl
)
3207 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3210 } else if (dl_argv_match(dl
, "show") ||
3211 dl_argv_match(dl
, "list")) {
3213 return cmd_sb_occ_show(dl
);
3214 } else if (dl_argv_match(dl
, "snapshot")) {
3216 return cmd_sb_occ_snapshot(dl
);
3217 } else if (dl_argv_match(dl
, "clearmax")) {
3219 return cmd_sb_occ_clearmax(dl
);
3221 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3225 static int cmd_sb(struct dl
*dl
)
3227 if (dl_argv_match(dl
, "help")) {
3230 } else if (dl_argv_match(dl
, "show") ||
3231 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3233 return cmd_sb_show(dl
);
3234 } else if (dl_argv_match(dl
, "pool")) {
3236 return cmd_sb_pool(dl
);
3237 } else if (dl_argv_match(dl
, "port")) {
3239 return cmd_sb_port(dl
);
3240 } else if (dl_argv_match(dl
, "tc")) {
3242 return cmd_sb_tc(dl
);
3243 } else if (dl_argv_match(dl
, "occupancy")) {
3245 return cmd_sb_occ(dl
);
3247 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3251 static const char *cmd_name(uint8_t cmd
)
3254 case DEVLINK_CMD_UNSPEC
: return "unspec";
3255 case DEVLINK_CMD_GET
: return "get";
3256 case DEVLINK_CMD_SET
: return "set";
3257 case DEVLINK_CMD_NEW
: return "new";
3258 case DEVLINK_CMD_DEL
: return "del";
3259 case DEVLINK_CMD_PORT_GET
: return "get";
3260 case DEVLINK_CMD_PORT_SET
: return "set";
3261 case DEVLINK_CMD_PORT_NEW
: return "new";
3262 case DEVLINK_CMD_PORT_DEL
: return "del";
3263 case DEVLINK_CMD_PARAM_GET
: return "get";
3264 case DEVLINK_CMD_PARAM_SET
: return "set";
3265 case DEVLINK_CMD_PARAM_NEW
: return "new";
3266 case DEVLINK_CMD_PARAM_DEL
: return "del";
3267 case DEVLINK_CMD_REGION_GET
: return "get";
3268 case DEVLINK_CMD_REGION_SET
: return "set";
3269 case DEVLINK_CMD_REGION_NEW
: return "new";
3270 case DEVLINK_CMD_REGION_DEL
: return "del";
3271 default: return "<unknown cmd>";
3275 static const char *cmd_obj(uint8_t cmd
)
3278 case DEVLINK_CMD_UNSPEC
: return "unspec";
3279 case DEVLINK_CMD_GET
:
3280 case DEVLINK_CMD_SET
:
3281 case DEVLINK_CMD_NEW
:
3282 case DEVLINK_CMD_DEL
:
3284 case DEVLINK_CMD_PORT_GET
:
3285 case DEVLINK_CMD_PORT_SET
:
3286 case DEVLINK_CMD_PORT_NEW
:
3287 case DEVLINK_CMD_PORT_DEL
:
3289 case DEVLINK_CMD_PARAM_GET
:
3290 case DEVLINK_CMD_PARAM_SET
:
3291 case DEVLINK_CMD_PARAM_NEW
:
3292 case DEVLINK_CMD_PARAM_DEL
:
3294 case DEVLINK_CMD_REGION_GET
:
3295 case DEVLINK_CMD_REGION_SET
:
3296 case DEVLINK_CMD_REGION_NEW
:
3297 case DEVLINK_CMD_REGION_DEL
:
3299 default: return "<unknown obj>";
3303 static void pr_out_mon_header(uint8_t cmd
)
3305 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3308 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3310 const char *obj
= cmd_obj(cmd
);
3311 unsigned int index
= 0;
3312 const char *cur_obj
;
3316 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3317 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3323 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3325 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3327 struct dl
*dl
= data
;
3328 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3329 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3330 uint8_t cmd
= genl
->cmd
;
3332 if (!cmd_filter_check(dl
, cmd
))
3336 case DEVLINK_CMD_GET
: /* fall through */
3337 case DEVLINK_CMD_SET
: /* fall through */
3338 case DEVLINK_CMD_NEW
: /* fall through */
3339 case DEVLINK_CMD_DEL
:
3340 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3341 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3342 return MNL_CB_ERROR
;
3343 pr_out_mon_header(genl
->cmd
);
3346 case DEVLINK_CMD_PORT_GET
: /* fall through */
3347 case DEVLINK_CMD_PORT_SET
: /* fall through */
3348 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3349 case DEVLINK_CMD_PORT_DEL
:
3350 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3351 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3352 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3353 return MNL_CB_ERROR
;
3354 pr_out_mon_header(genl
->cmd
);
3355 pr_out_port(dl
, tb
);
3357 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3358 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3359 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3360 case DEVLINK_CMD_PARAM_DEL
:
3361 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3362 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3363 !tb
[DEVLINK_ATTR_PARAM
])
3364 return MNL_CB_ERROR
;
3365 pr_out_mon_header(genl
->cmd
);
3366 pr_out_param(dl
, tb
, false);
3368 case DEVLINK_CMD_REGION_GET
: /* fall through */
3369 case DEVLINK_CMD_REGION_SET
: /* fall through */
3370 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3371 case DEVLINK_CMD_REGION_DEL
:
3372 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3373 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3374 !tb
[DEVLINK_ATTR_REGION_NAME
])
3375 return MNL_CB_ERROR
;
3376 pr_out_mon_header(genl
->cmd
);
3377 pr_out_region(dl
, tb
);
3383 static int cmd_mon_show(struct dl
*dl
)
3386 unsigned int index
= 0;
3387 const char *cur_obj
;
3389 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3390 if (strcmp(cur_obj
, "all") != 0 &&
3391 strcmp(cur_obj
, "dev") != 0 &&
3392 strcmp(cur_obj
, "port") != 0) {
3393 pr_err("Unknown object \"%s\"\n", cur_obj
);
3397 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3400 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3406 static void cmd_mon_help(void)
3408 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3409 "where OBJECT-LIST := { dev | port }\n");
3412 static int cmd_mon(struct dl
*dl
)
3414 if (dl_argv_match(dl
, "help")) {
3417 } else if (dl_no_arg(dl
)) {
3419 return cmd_mon_show(dl
);
3421 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3425 struct dpipe_field
{
3428 unsigned int bitwidth
;
3429 enum devlink_dpipe_field_mapping_type mapping_type
;
3432 struct dpipe_header
{
3433 struct list_head list
;
3436 struct dpipe_field
*fields
;
3437 unsigned int fields_count
;
3440 struct dpipe_table
{
3441 struct list_head list
;
3443 unsigned int resource_id
;
3444 bool resource_valid
;
3447 struct dpipe_tables
{
3448 struct list_head table_list
;
3458 enum devlink_resource_unit unit
;
3463 struct list_head list
;
3464 struct list_head resource_list
;
3465 struct resource
*parent
;
3469 struct list_head resource_list
;
3472 struct resource_ctx
{
3475 struct resources
*resources
;
3476 struct dpipe_tables
*tables
;
3477 bool print_resources
;
3478 bool pending_change
;
3481 static struct resource
*resource_alloc(void)
3483 struct resource
*resource
;
3485 resource
= calloc(1, sizeof(struct resource
));
3488 INIT_LIST_HEAD(&resource
->resource_list
);
3492 static void resource_free(struct resource
*resource
)
3494 struct resource
*child_resource
, *tmp
;
3496 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3498 free(child_resource
->name
);
3499 resource_free(child_resource
);
3504 static struct resources
*resources_alloc(void)
3506 struct resources
*resources
;
3508 resources
= calloc(1, sizeof(struct resources
));
3511 INIT_LIST_HEAD(&resources
->resource_list
);
3515 static void resources_free(struct resources
*resources
)
3517 struct resource
*resource
, *tmp
;
3519 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3520 resource_free(resource
);
3523 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3525 ctx
->resources
= resources_alloc();
3526 if (!ctx
->resources
)
3532 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3534 resources_free(ctx
->resources
);
3540 struct list_head global_headers
;
3541 struct list_head local_headers
;
3542 struct dpipe_tables
*tables
;
3543 struct resources
*resources
;
3548 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
3550 struct dpipe_header
*header
;
3552 header
= calloc(1, sizeof(struct dpipe_header
));
3555 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
3556 if (!header
->fields
)
3557 goto err_fields_alloc
;
3558 header
->fields_count
= fields_count
;
3566 static void dpipe_header_free(struct dpipe_header
*header
)
3568 free(header
->fields
);
3572 static void dpipe_header_clear(struct dpipe_header
*header
)
3574 struct dpipe_field
*field
;
3577 for (i
= 0; i
< header
->fields_count
; i
++) {
3578 field
= &header
->fields
[i
];
3584 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
3585 struct dpipe_header
*header
, bool global
)
3588 list_add(&header
->list
, &ctx
->global_headers
);
3590 list_add(&header
->list
, &ctx
->local_headers
);
3593 static void dpipe_header_del(struct dpipe_header
*header
)
3595 list_del(&header
->list
);
3598 static struct dpipe_table
*dpipe_table_alloc(void)
3600 return calloc(1, sizeof(struct dpipe_table
));
3603 static void dpipe_table_free(struct dpipe_table
*table
)
3608 static struct dpipe_tables
*dpipe_tables_alloc(void)
3610 struct dpipe_tables
*tables
;
3612 tables
= calloc(1, sizeof(struct dpipe_tables
));
3615 INIT_LIST_HEAD(&tables
->table_list
);
3619 static void dpipe_tables_free(struct dpipe_tables
*tables
)
3621 struct dpipe_table
*table
, *tmp
;
3623 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
3624 dpipe_table_free(table
);
3628 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
3630 ctx
->tables
= dpipe_tables_alloc();
3635 INIT_LIST_HEAD(&ctx
->global_headers
);
3636 INIT_LIST_HEAD(&ctx
->local_headers
);
3640 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
3642 struct dpipe_header
*header
, *tmp
;
3644 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
3646 dpipe_header_del(header
);
3647 dpipe_header_clear(header
);
3648 dpipe_header_free(header
);
3650 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
3652 dpipe_header_del(header
);
3653 dpipe_header_clear(header
);
3654 dpipe_header_free(header
);
3656 dpipe_tables_free(ctx
->tables
);
3659 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
3660 uint32_t header_id
, bool global
)
3662 struct list_head
*header_list
;
3663 struct dpipe_header
*header
;
3666 header_list
= &ctx
->global_headers
;
3668 header_list
= &ctx
->local_headers
;
3669 list_for_each_entry(header
, header_list
, list
) {
3670 if (header
->id
!= header_id
)
3672 return header
->name
;
3677 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
3679 uint32_t field_id
, bool global
)
3681 struct list_head
*header_list
;
3682 struct dpipe_header
*header
;
3685 header_list
= &ctx
->global_headers
;
3687 header_list
= &ctx
->local_headers
;
3688 list_for_each_entry(header
, header_list
, list
) {
3689 if (header
->id
!= header_id
)
3691 return header
->fields
[field_id
].name
;
3697 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
3699 switch (mapping_type
) {
3700 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
3702 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
3710 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
3711 uint32_t field_id
, bool global
)
3713 enum devlink_dpipe_field_mapping_type mapping_type
;
3714 struct list_head
*header_list
;
3715 struct dpipe_header
*header
;
3718 header_list
= &ctx
->global_headers
;
3720 header_list
= &ctx
->local_headers
;
3721 list_for_each_entry(header
, header_list
, list
) {
3722 if (header
->id
!= header_id
)
3724 mapping_type
= header
->fields
[field_id
].mapping_type
;
3725 return dpipe_field_mapping_e2s(mapping_type
);
3730 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
3731 struct dpipe_field
*fields
,
3732 unsigned int field_count
)
3734 struct dpipe_field
*field
;
3737 for (i
= 0; i
< field_count
; i
++) {
3739 pr_out_entry_start(ctx
->dl
);
3740 pr_out_str(ctx
->dl
, "name", field
->name
);
3741 if (ctx
->dl
->verbose
)
3742 pr_out_uint(ctx
->dl
, "id", field
->id
);
3743 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
3744 if (field
->mapping_type
)
3745 pr_out_str(ctx
->dl
, "mapping_type",
3746 dpipe_field_mapping_e2s(field
->mapping_type
));
3747 pr_out_entry_end(ctx
->dl
);
3752 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
3753 struct dpipe_header
*header
, bool global
)
3755 pr_out_handle_start_arr(ctx
->dl
, tb
);
3756 pr_out_str(ctx
->dl
, "name", header
->name
);
3757 if (ctx
->dl
->verbose
) {
3758 pr_out_uint(ctx
->dl
, "id", header
->id
);
3759 pr_out_str(ctx
->dl
, "global",
3760 global
? "true" : "false");
3762 pr_out_array_start(ctx
->dl
, "field");
3763 pr_out_dpipe_fields(ctx
, header
->fields
,
3764 header
->fields_count
);
3765 pr_out_array_end(ctx
->dl
);
3766 pr_out_handle_end(ctx
->dl
);
3769 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
3772 struct dpipe_header
*header
;
3774 list_for_each_entry(header
, &ctx
->local_headers
, list
)
3775 pr_out_dpipe_header(ctx
, tb
, header
, false);
3777 list_for_each_entry(header
, &ctx
->global_headers
, list
)
3778 pr_out_dpipe_header(ctx
, tb
, header
, true);
3781 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
3783 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
3787 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
3788 if (err
!= MNL_CB_OK
)
3790 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
3791 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
3792 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
3793 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
3796 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
3797 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3798 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
3799 field
->name
= strdup(name
);
3802 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
3806 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
3807 struct dpipe_field
*fields
)
3809 struct nlattr
*nla_field
;
3813 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
3814 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
3822 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
3824 struct nlattr
*nla_field
;
3825 unsigned int count
= 0;
3827 mnl_attr_for_each_nested(nla_field
, nla_fields
)
3832 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3834 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
3835 struct dpipe_header
*header
;
3836 unsigned int fields_count
;
3837 const char *header_name
;
3841 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
3842 if (err
!= MNL_CB_OK
)
3845 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
3846 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3847 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
3850 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
3851 header
= dpipe_header_alloc(fields_count
);
3855 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
3856 header
->name
= strdup(header_name
);
3857 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3858 header
->fields_count
= fields_count
;
3859 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3861 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
3865 dpipe_header_add(ctx
, header
, global
);
3869 dpipe_header_free(header
);
3873 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3875 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
3876 struct nlattr
*nla_header
;
3879 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
3880 err
= dpipe_header_get(ctx
, nla_header
);
3887 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
3889 struct dpipe_ctx
*ctx
= data
;
3890 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3891 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3894 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3895 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3896 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
3897 return MNL_CB_ERROR
;
3898 err
= dpipe_headers_get(ctx
, tb
);
3901 return MNL_CB_ERROR
;
3904 if (ctx
->print_headers
)
3905 pr_out_dpipe_headers(ctx
, tb
);
3909 static int cmd_dpipe_headers_show(struct dl
*dl
)
3911 struct nlmsghdr
*nlh
;
3912 struct dpipe_ctx ctx
= {};
3913 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3916 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3918 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3922 err
= dpipe_ctx_init(&ctx
, dl
);
3926 ctx
.print_headers
= true;
3928 pr_out_section_start(dl
, "header");
3929 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
3931 pr_err("error get headers %s\n", strerror(ctx
.err
));
3932 pr_out_section_end(dl
);
3934 dpipe_ctx_fini(&ctx
);
3938 static void cmd_dpipe_header_help(void)
3940 pr_err("Usage: devlink dpipe headers show DEV\n");
3943 static int cmd_dpipe_header(struct dl
*dl
)
3945 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3946 cmd_dpipe_header_help();
3948 } else if (dl_argv_match(dl
, "show")) {
3950 return cmd_dpipe_headers_show(dl
);
3952 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3957 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
3959 switch (action_type
) {
3960 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
3961 return "field_modify";
3967 struct dpipe_op_info
{
3973 struct dpipe_action
{
3974 struct dpipe_op_info info
;
3978 static void pr_out_dpipe_action(struct dpipe_action
*action
,
3979 struct dpipe_ctx
*ctx
)
3981 struct dpipe_op_info
*op_info
= &action
->info
;
3982 const char *mapping
;
3984 pr_out_str(ctx
->dl
, "type",
3985 dpipe_action_type_e2s(action
->type
));
3986 pr_out_str(ctx
->dl
, "header",
3987 dpipe_header_id2s(ctx
, op_info
->header_id
,
3988 op_info
->header_global
));
3989 pr_out_str(ctx
->dl
, "field",
3990 dpipe_field_id2s(ctx
, op_info
->header_id
,
3992 op_info
->header_global
));
3993 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3995 op_info
->header_global
);
3997 pr_out_str(ctx
->dl
, "mapping", mapping
);
4000 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4002 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4005 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4006 if (err
!= MNL_CB_OK
)
4009 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4010 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4011 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4012 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4016 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4017 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4018 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4019 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4024 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4025 struct nlattr
*nla_actions
)
4027 struct nlattr
*nla_action
;
4028 struct dpipe_action action
;
4030 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4031 pr_out_entry_start(ctx
->dl
);
4032 if (dpipe_action_parse(&action
, nla_action
))
4033 goto err_action_parse
;
4034 pr_out_dpipe_action(&action
, ctx
);
4035 pr_out_entry_end(ctx
->dl
);
4040 pr_out_entry_end(ctx
->dl
);
4045 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4047 switch (match_type
) {
4048 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4049 return "field_exact";
4055 struct dpipe_match
{
4056 struct dpipe_op_info info
;
4060 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4061 struct dpipe_ctx
*ctx
)
4063 struct dpipe_op_info
*op_info
= &match
->info
;
4064 const char *mapping
;
4066 pr_out_str(ctx
->dl
, "type",
4067 dpipe_match_type_e2s(match
->type
));
4068 pr_out_str(ctx
->dl
, "header",
4069 dpipe_header_id2s(ctx
, op_info
->header_id
,
4070 op_info
->header_global
));
4071 pr_out_str(ctx
->dl
, "field",
4072 dpipe_field_id2s(ctx
, op_info
->header_id
,
4074 op_info
->header_global
));
4075 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4077 op_info
->header_global
);
4079 pr_out_str(ctx
->dl
, "mapping", mapping
);
4082 static int dpipe_match_parse(struct dpipe_match
*match
,
4086 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4089 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4090 if (err
!= MNL_CB_OK
)
4093 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4094 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4095 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4096 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4100 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4101 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4102 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4103 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4108 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4109 struct nlattr
*nla_matches
)
4111 struct nlattr
*nla_match
;
4112 struct dpipe_match match
;
4114 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4115 pr_out_entry_start(ctx
->dl
);
4116 if (dpipe_match_parse(&match
, nla_match
))
4117 goto err_match_parse
;
4118 pr_out_dpipe_match(&match
, ctx
);
4119 pr_out_entry_end(ctx
->dl
);
4124 pr_out_entry_end(ctx
->dl
);
4128 static struct resource
*
4129 resource_find(struct resources
*resources
, struct resource
*resource
,
4130 uint64_t resource_id
)
4132 struct list_head
*list_head
;
4135 list_head
= &resources
->resource_list
;
4137 list_head
= &resource
->resource_list
;
4139 list_for_each_entry(resource
, list_head
, list
) {
4140 struct resource
*child_resource
;
4142 if (resource
->id
== resource_id
)
4145 child_resource
= resource_find(resources
, resource
,
4148 return child_resource
;
4154 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4155 uint64_t resource_id
)
4157 struct resource
*resource
, *parent_resource
;
4158 const char del
[] = "/";
4162 resource
= resource_find(resources
, NULL
, resource_id
);
4166 for (parent_resource
= resource
; parent_resource
;
4167 parent_resource
= parent_resource
->parent
)
4168 path_len
+= strlen(parent_resource
->name
) + 1;
4171 path
= calloc(1, path_len
);
4175 path
+= path_len
- 1;
4176 for (parent_resource
= resource
; parent_resource
;
4177 parent_resource
= parent_resource
->parent
) {
4178 path
-= strlen(parent_resource
->name
);
4179 memcpy(path
, parent_resource
->name
,
4180 strlen(parent_resource
->name
));
4181 path
-= strlen(del
);
4182 memcpy(path
, del
, strlen(del
));
4184 pr_out_str(dl
, "resource_path", path
);
4188 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4190 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4191 struct dpipe_table
*table
;
4192 uint32_t resource_units
;
4193 bool counters_enabled
;
4194 bool resource_valid
;
4198 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4199 if (err
!= MNL_CB_OK
)
4202 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4203 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4204 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4205 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4206 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4210 table
= dpipe_table_alloc();
4214 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4215 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4216 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4218 resource_valid
= !!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
];
4219 if (resource_valid
) {
4220 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4221 table
->resource_valid
= true;
4224 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4225 if (!ctx
->print_tables
)
4228 pr_out_str(ctx
->dl
, "name", table
->name
);
4229 pr_out_uint(ctx
->dl
, "size", size
);
4230 pr_out_str(ctx
->dl
, "counters_enabled",
4231 counters_enabled
? "true" : "false");
4233 if (resource_valid
) {
4234 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4235 resource_path_print(ctx
->dl
, ctx
->resources
,
4236 table
->resource_id
);
4237 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4240 pr_out_array_start(ctx
->dl
, "match");
4241 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4242 goto err_matches_show
;
4243 pr_out_array_end(ctx
->dl
);
4245 pr_out_array_start(ctx
->dl
, "action");
4246 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4247 goto err_actions_show
;
4248 pr_out_array_end(ctx
->dl
);
4254 pr_out_array_end(ctx
->dl
);
4258 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4260 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4261 struct nlattr
*nla_table
;
4263 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4264 if (ctx
->print_tables
)
4265 pr_out_handle_start_arr(ctx
->dl
, tb
);
4266 if (dpipe_table_show(ctx
, nla_table
))
4267 goto err_table_show
;
4268 if (ctx
->print_tables
)
4269 pr_out_handle_end(ctx
->dl
);
4274 if (ctx
->print_tables
)
4275 pr_out_handle_end(ctx
->dl
);
4279 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4281 struct dpipe_ctx
*ctx
= data
;
4282 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4283 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4285 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4286 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4287 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4288 return MNL_CB_ERROR
;
4290 if (dpipe_tables_show(ctx
, tb
))
4291 return MNL_CB_ERROR
;
4295 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4297 static int cmd_dpipe_table_show(struct dl
*dl
)
4299 struct nlmsghdr
*nlh
;
4300 struct dpipe_ctx dpipe_ctx
= {};
4301 struct resource_ctx resource_ctx
= {};
4302 uint16_t flags
= NLM_F_REQUEST
;
4305 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4309 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4311 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4315 dpipe_ctx
.print_tables
= true;
4317 dl_opts_put(nlh
, dl
);
4318 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4321 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4322 goto err_headers_get
;
4325 err
= resource_ctx_init(&resource_ctx
, dl
);
4327 goto err_resource_ctx_init
;
4329 resource_ctx
.print_resources
= false;
4330 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4331 dl_opts_put(nlh
, dl
);
4332 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4335 pr_err("error get resources %s\n", strerror(resource_ctx
.err
));
4336 goto err_resource_dump
;
4339 dpipe_ctx
.resources
= resource_ctx
.resources
;
4340 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4341 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4342 dl_opts_put(nlh
, dl
);
4344 pr_out_section_start(dl
, "table");
4345 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4346 pr_out_section_end(dl
);
4348 resource_ctx_fini(&resource_ctx
);
4349 dpipe_ctx_fini(&dpipe_ctx
);
4353 resource_ctx_fini(&resource_ctx
);
4354 err_resource_ctx_init
:
4356 dpipe_ctx_fini(&dpipe_ctx
);
4360 static int cmd_dpipe_table_set(struct dl
*dl
)
4362 struct nlmsghdr
*nlh
;
4365 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4366 NLM_F_REQUEST
| NLM_F_ACK
);
4368 err
= dl_argv_parse_put(nlh
, dl
,
4369 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4370 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4374 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4377 enum dpipe_value_type
{
4378 DPIPE_VALUE_TYPE_VALUE
,
4379 DPIPE_VALUE_TYPE_MASK
,
4383 dpipe_value_type_e2s(enum dpipe_value_type type
)
4386 case DPIPE_VALUE_TYPE_VALUE
:
4388 case DPIPE_VALUE_TYPE_MASK
:
4389 return "value_mask";
4395 struct dpipe_field_printer
{
4396 unsigned int field_id
;
4397 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4400 struct dpipe_header_printer
{
4401 struct dpipe_field_printer
*printers
;
4402 unsigned int printers_count
;
4403 unsigned int header_id
;
4406 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4407 enum dpipe_value_type type
,
4410 struct in_addr ip_addr
;
4412 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4413 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4417 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4418 enum dpipe_value_type type
,
4421 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4422 ether_ntoa((struct ether_addr
*)value
));
4425 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4426 enum dpipe_value_type type
,
4429 char str
[INET6_ADDRSTRLEN
];
4431 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4432 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4435 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4437 .printer
= dpipe_field_printer_ipv4_addr
,
4438 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4442 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4443 .printers
= dpipe_field_printers_ipv4
,
4444 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4445 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4448 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4450 .printer
= dpipe_field_printer_ethernet_addr
,
4451 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4455 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4456 .printers
= dpipe_field_printers_ethernet
,
4457 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4458 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4461 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4463 .printer
= dpipe_field_printer_ipv6_addr
,
4464 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4468 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4469 .printers
= dpipe_field_printers_ipv6
,
4470 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4471 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4474 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4475 &dpipe_header_printer_ipv4
,
4476 &dpipe_header_printer_ethernet
,
4477 &dpipe_header_printer_ipv6
,
4480 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4481 struct dpipe_op_info
*info
,
4482 enum dpipe_value_type type
,
4485 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4486 struct dpipe_header_printer
*header_printer
;
4487 struct dpipe_field_printer
*field_printer
;
4488 unsigned int field_printers_count
;
4492 for (i
= 0; i
< header_printers_count
; i
++) {
4493 header_printer
= dpipe_header_printers
[i
];
4494 if (header_printer
->header_id
!= info
->header_id
)
4496 field_printers_count
= header_printer
->printers_count
;
4497 for (j
= 0; j
< field_printers_count
; j
++) {
4498 field_printer
= &header_printer
->printers
[j
];
4499 if (field_printer
->field_id
!= info
->field_id
)
4501 field_printer
->printer(ctx
, type
, value
);
4509 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4511 unsigned int value_len
,
4512 struct dpipe_op_info
*info
,
4513 enum dpipe_value_type type
)
4515 if (info
->header_global
&&
4516 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4519 if (value_len
== sizeof(uint32_t)) {
4520 uint32_t *value_32
= value
;
4522 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4526 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4527 struct nlattr
**nla_match_value
,
4528 struct dpipe_op_info
*info
)
4530 void *value
, *value_mask
;
4531 uint32_t value_mapping
;
4535 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4536 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4538 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4539 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4542 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
4543 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
4547 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4548 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
4549 DPIPE_VALUE_TYPE_MASK
);
4552 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
4555 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
4558 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4559 struct dpipe_match match
;
4562 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
4563 if (err
!= MNL_CB_OK
)
4566 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
4567 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4571 pr_out_entry_start(ctx
->dl
);
4572 if (dpipe_match_parse(&match
,
4573 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
4574 goto err_match_parse
;
4575 pr_out_dpipe_match(&match
, ctx
);
4576 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
4577 pr_out_entry_end(ctx
->dl
);
4582 pr_out_entry_end(ctx
->dl
);
4586 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
4589 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
4590 struct dpipe_action action
;
4593 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
4594 if (err
!= MNL_CB_OK
)
4597 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
4598 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
4602 pr_out_entry_start(ctx
->dl
);
4603 if (dpipe_action_parse(&action
,
4604 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
4605 goto err_action_parse
;
4606 pr_out_dpipe_action(&action
, ctx
);
4607 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
4608 pr_out_entry_end(ctx
->dl
);
4613 pr_out_entry_end(ctx
->dl
);
4618 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
4619 struct nlattr
*nla_action_values
)
4621 struct nlattr
*nla_action_value
;
4623 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
4624 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
4631 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
4632 struct nlattr
*nla_match_values
)
4634 struct nlattr
*nla_match_value
;
4636 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
4637 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
4643 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4645 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
4646 uint32_t entry_index
;
4650 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
4651 if (err
!= MNL_CB_OK
)
4654 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
4655 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
4656 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
4660 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
4661 pr_out_uint(ctx
->dl
, "index", entry_index
);
4663 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
4664 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
4665 pr_out_uint(ctx
->dl
, "counter", counter
);
4668 pr_out_array_start(ctx
->dl
, "match_value");
4669 if (dpipe_tables_match_values_show(ctx
,
4670 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
4671 goto err_match_values_show
;
4672 pr_out_array_end(ctx
->dl
);
4674 pr_out_array_start(ctx
->dl
, "action_value");
4675 if (dpipe_tables_action_values_show(ctx
,
4676 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
4677 goto err_action_values_show
;
4678 pr_out_array_end(ctx
->dl
);
4681 err_action_values_show
:
4682 err_match_values_show
:
4683 pr_out_array_end(ctx
->dl
);
4687 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4689 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
4690 struct nlattr
*nla_entry
;
4692 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
4693 pr_out_handle_start_arr(ctx
->dl
, tb
);
4694 if (dpipe_entry_show(ctx
, nla_entry
))
4695 goto err_entry_show
;
4696 pr_out_handle_end(ctx
->dl
);
4701 pr_out_handle_end(ctx
->dl
);
4705 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
4707 struct dpipe_ctx
*ctx
= data
;
4708 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4709 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4711 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4712 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4713 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
4714 return MNL_CB_ERROR
;
4716 if (dpipe_table_entries_show(ctx
, tb
))
4717 return MNL_CB_ERROR
;
4721 static int cmd_dpipe_table_dump(struct dl
*dl
)
4723 struct nlmsghdr
*nlh
;
4724 struct dpipe_ctx ctx
= {};
4725 uint16_t flags
= NLM_F_REQUEST
;
4728 err
= dpipe_ctx_init(&ctx
, dl
);
4732 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
4736 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4737 dl_opts_put(nlh
, dl
);
4738 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4740 pr_err("error get headers %s\n", strerror(ctx
.err
));
4744 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4745 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
4746 dl_opts_put(nlh
, dl
);
4748 pr_out_section_start(dl
, "table_entry");
4749 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
4750 pr_out_section_end(dl
);
4752 dpipe_ctx_fini(&ctx
);
4756 static void cmd_dpipe_table_help(void)
4758 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
4759 "where OBJECT-LIST := { show | set | dump }\n");
4762 static int cmd_dpipe_table(struct dl
*dl
)
4764 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4765 cmd_dpipe_table_help();
4767 } else if (dl_argv_match(dl
, "show")) {
4769 return cmd_dpipe_table_show(dl
);
4770 } else if (dl_argv_match(dl
, "set")) {
4772 return cmd_dpipe_table_set(dl
);
4773 } else if (dl_argv_match(dl
, "dump")) {
4775 return cmd_dpipe_table_dump(dl
);
4777 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4781 static void cmd_dpipe_help(void)
4783 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
4784 "where OBJECT-LIST := { header | table }\n");
4787 static int cmd_dpipe(struct dl
*dl
)
4789 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4792 } else if (dl_argv_match(dl
, "header")) {
4794 return cmd_dpipe_header(dl
);
4795 } else if (dl_argv_match(dl
, "table")) {
4797 return cmd_dpipe_table(dl
);
4799 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4804 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
4805 struct nlattr
**nla_resource
)
4807 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
4808 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
4809 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
4810 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
4811 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
4812 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
4813 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
4817 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
4818 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
4819 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
4820 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
4821 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
4822 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
4823 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
4825 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
4826 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
4828 resource
->size_new
= resource
->size
;
4830 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
4831 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
4832 resource
->occ_valid
= true;
4835 if (resource
->size_new
!= resource
->size
)
4836 ctx
->pending_change
= true;
4842 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
4843 struct resource
*parent_resource
, struct nlattr
*nl
)
4845 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
4846 struct nlattr
*nla_child_resource
;
4847 struct nlattr
*nla_resources
;
4857 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
4858 if (err
!= MNL_CB_OK
)
4861 err
= resource_parse(ctx
, resource
, nla_resource
);
4865 resource
->parent
= parent_resource
;
4866 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
4869 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
4870 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
4872 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
4873 struct resource
*child_resource
;
4874 struct list_head
*list
;
4876 child_resource
= resource_alloc();
4877 if (!child_resource
)
4881 list
= &ctx
->resources
->resource_list
;
4883 list
= &resource
->resource_list
;
4885 list_add_tail(&child_resource
->list
, list
);
4886 err
= resource_get(ctx
, child_resource
, resource
,
4887 nla_child_resource
);
4895 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
4898 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
4899 default: return "<unknown unit>";
4903 static void resource_show(struct resource
*resource
,
4904 struct resource_ctx
*ctx
)
4906 struct resource
*child_resource
;
4907 struct dpipe_table
*table
;
4908 struct dl
*dl
= ctx
->dl
;
4911 pr_out_str(dl
, "name", resource
->name
);
4913 resource_path_print(dl
, ctx
->resources
, resource
->id
);
4914 pr_out_u64(dl
, "size", resource
->size
);
4915 if (resource
->size
!= resource
->size_new
)
4916 pr_out_u64(dl
, "size_new", resource
->size_new
);
4917 if (resource
->occ_valid
)
4918 pr_out_uint(dl
, "occ", resource
->size_occ
);
4919 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
4921 if (resource
->size_min
!= resource
->size_max
) {
4922 pr_out_uint(dl
, "size_min", resource
->size_min
);
4923 pr_out_u64(dl
, "size_max", resource
->size_max
);
4924 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
4927 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
4928 if (table
->resource_id
== resource
->id
&&
4929 table
->resource_valid
)
4933 pr_out_array_start(dl
, "dpipe_tables");
4935 pr_out_str(dl
, "dpipe_tables", "none");
4937 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
4938 if (table
->resource_id
!= resource
->id
||
4939 !table
->resource_valid
)
4941 pr_out_entry_start(dl
);
4942 pr_out_str(dl
, "table_name", table
->name
);
4943 pr_out_entry_end(dl
);
4946 pr_out_array_end(dl
);
4948 if (list_empty(&resource
->resource_list
))
4951 if (ctx
->pending_change
)
4952 pr_out_str(dl
, "size_valid", resource
->size_valid
?
4954 pr_out_array_start(dl
, "resources");
4955 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
4956 pr_out_entry_start(dl
);
4957 resource_show(child_resource
, ctx
);
4958 pr_out_entry_end(dl
);
4960 pr_out_array_end(dl
);
4964 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
4966 struct resources
*resources
= ctx
->resources
;
4967 struct resource
*resource
;
4969 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
4970 pr_out_handle_start_arr(ctx
->dl
, tb
);
4971 resource_show(resource
, ctx
);
4972 pr_out_handle_end(ctx
->dl
);
4976 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
4978 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
4981 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
4983 struct resource_ctx
*ctx
= data
;
4984 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4985 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4988 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4989 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4990 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
4991 return MNL_CB_ERROR
;
4993 err
= resources_get(ctx
, tb
);
4996 return MNL_CB_ERROR
;
4999 if (ctx
->print_resources
)
5000 resources_show(ctx
, tb
);
5005 static int cmd_resource_show(struct dl
*dl
)
5007 struct nlmsghdr
*nlh
;
5008 struct dpipe_ctx dpipe_ctx
= {};
5009 struct resource_ctx resource_ctx
= {};
5012 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5016 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5018 dl_opts_put(nlh
, dl
);
5020 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5024 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5027 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5031 err
= resource_ctx_init(&resource_ctx
, dl
);
5035 resource_ctx
.print_resources
= true;
5036 resource_ctx
.tables
= dpipe_ctx
.tables
;
5037 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5038 NLM_F_REQUEST
| NLM_F_ACK
);
5039 dl_opts_put(nlh
, dl
);
5040 pr_out_section_start(dl
, "resources");
5041 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5043 pr_out_section_end(dl
);
5044 resource_ctx_fini(&resource_ctx
);
5046 dpipe_ctx_fini(&dpipe_ctx
);
5050 static void cmd_resource_help(void)
5052 pr_err("Usage: devlink resource show DEV\n"
5053 " devlink resource set DEV path PATH size SIZE\n");
5056 static struct resource
*
5057 resource_find_by_name(struct list_head
*list
, char *name
)
5059 struct resource
*resource
;
5061 list_for_each_entry(resource
, list
, list
) {
5062 if (!strcmp(resource
->name
, name
))
5069 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5070 uint32_t *p_resource_id
, bool *p_resource_valid
)
5072 struct resource
*resource
;
5073 uint32_t resource_id
= 0;
5074 char *resource_path_dup
;
5075 struct list_head
*list
;
5076 const char del
[] = "/";
5077 char *resource_name
;
5079 resource_path_dup
= strdup(resource_path
);
5080 list
= &ctx
->resources
->resource_list
;
5081 resource_name
= strtok(resource_path_dup
, del
);
5082 while (resource_name
!= NULL
) {
5083 resource
= resource_find_by_name(list
, resource_name
);
5085 goto err_resource_lookup
;
5087 list
= &resource
->resource_list
;
5088 resource_name
= strtok(NULL
, del
);
5089 resource_id
= resource
->id
;
5091 free(resource_path_dup
);
5092 *p_resource_valid
= true;
5093 *p_resource_id
= resource_id
;
5096 err_resource_lookup
:
5097 free(resource_path_dup
);
5101 static int cmd_resource_set(struct dl
*dl
)
5103 struct nlmsghdr
*nlh
;
5104 struct resource_ctx ctx
= {};
5107 err
= resource_ctx_init(&ctx
, dl
);
5111 ctx
.print_resources
= false;
5112 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5113 DL_OPT_RESOURCE_SIZE
, 0);
5117 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5119 dl_opts_put(nlh
, dl
);
5120 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5122 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5126 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5127 &dl
->opts
.resource_id
,
5128 &dl
->opts
.resource_id_valid
);
5130 pr_err("error parsing resource path %s\n", strerror(-err
));
5134 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5135 NLM_F_REQUEST
| NLM_F_ACK
);
5137 dl_opts_put(nlh
, dl
);
5138 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5140 resource_ctx_fini(&ctx
);
5144 static int cmd_resource(struct dl
*dl
)
5146 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5147 cmd_resource_help();
5149 } else if (dl_argv_match(dl
, "show")) {
5151 return cmd_resource_show(dl
);
5152 } else if (dl_argv_match(dl
, "set")) {
5154 return cmd_resource_set(dl
);
5156 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5160 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5162 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5163 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5164 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5167 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5168 if (dl
->json_output
) {
5169 jsonw_name(dl
->jw
, buf
);
5170 jsonw_start_object(dl
->jw
);
5176 static void pr_out_region_handle_end(struct dl
*dl
)
5178 if (dl
->json_output
)
5179 jsonw_end_object(dl
->jw
);
5184 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5186 if (dl
->json_output
) {
5187 jsonw_name(dl
->jw
, "snapshot");
5188 jsonw_start_array(dl
->jw
);
5190 if (g_indent_newline
)
5191 pr_out("snapshot %s", array
? "[" : "");
5193 pr_out(" snapshot %s", array
? "[" : "");
5197 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5199 if (dl
->json_output
)
5200 jsonw_end_array(dl
->jw
);
5205 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5207 uint32_t snapshot_id
;
5209 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5212 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5214 if (dl
->json_output
)
5215 jsonw_uint(dl
->jw
, snapshot_id
);
5217 pr_out("%s%u", index
? " " : "", snapshot_id
);
5220 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5222 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5223 struct nlattr
*nla_sanpshot
;
5226 pr_out_region_snapshots_start(dl
, true);
5227 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5228 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5229 if (err
!= MNL_CB_OK
)
5231 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5233 pr_out_region_snapshots_end(dl
, true);
5236 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5238 pr_out_region_snapshots_start(dl
, false);
5239 pr_out_region_snapshots_id(dl
, tb
, 0);
5240 pr_out_region_snapshots_end(dl
, false);
5243 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5245 pr_out_region_handle_start(dl
, tb
);
5247 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5248 pr_out_u64(dl
, "size",
5249 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5251 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5252 pr_out_snapshots(dl
, tb
);
5254 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5255 pr_out_snapshot(dl
, tb
);
5257 pr_out_region_handle_end(dl
);
5260 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5262 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5263 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5264 struct dl
*dl
= data
;
5266 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5267 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5268 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5269 return MNL_CB_ERROR
;
5271 pr_out_region(dl
, tb
);
5276 static int cmd_region_show(struct dl
*dl
)
5278 struct nlmsghdr
*nlh
;
5279 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5282 if (dl_argc(dl
) == 0)
5283 flags
|= NLM_F_DUMP
;
5285 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5287 if (dl_argc(dl
) > 0) {
5288 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5293 pr_out_section_start(dl
, "regions");
5294 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5295 pr_out_section_end(dl
);
5299 static int cmd_region_snapshot_del(struct dl
*dl
)
5301 struct nlmsghdr
*nlh
;
5304 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5305 NLM_F_REQUEST
| NLM_F_ACK
);
5307 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5308 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5312 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5315 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5317 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5318 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5319 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5320 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5321 struct dl
*dl
= data
;
5324 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5325 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5326 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5327 return MNL_CB_ERROR
;
5329 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5330 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5331 if (err
!= MNL_CB_OK
)
5332 return MNL_CB_ERROR
;
5334 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5335 if (!nla_chunk_data
)
5338 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5339 if (!nla_chunk_addr
)
5342 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5343 mnl_attr_get_payload_len(nla_chunk_data
),
5344 mnl_attr_get_u64(nla_chunk_addr
));
5349 static int cmd_region_dump(struct dl
*dl
)
5351 struct nlmsghdr
*nlh
;
5354 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5355 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5357 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5358 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5362 pr_out_section_start(dl
, "dump");
5363 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5364 pr_out_section_end(dl
);
5365 if (!dl
->json_output
)
5370 static int cmd_region_read(struct dl
*dl
)
5372 struct nlmsghdr
*nlh
;
5375 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5376 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5378 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5379 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5380 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5384 pr_out_section_start(dl
, "read");
5385 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5386 pr_out_section_end(dl
);
5387 if (!dl
->json_output
)
5392 static void cmd_region_help(void)
5394 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5395 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5396 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5397 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5400 static int cmd_region(struct dl
*dl
)
5402 if (dl_no_arg(dl
)) {
5403 return cmd_region_show(dl
);
5404 } else if (dl_argv_match(dl
, "help")) {
5407 } else if (dl_argv_match(dl
, "show")) {
5409 return cmd_region_show(dl
);
5410 } else if (dl_argv_match(dl
, "del")) {
5412 return cmd_region_snapshot_del(dl
);
5413 } else if (dl_argv_match(dl
, "dump")) {
5415 return cmd_region_dump(dl
);
5416 } else if (dl_argv_match(dl
, "read")) {
5418 return cmd_region_read(dl
);
5420 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5424 static void help(void)
5426 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
5427 " devlink [ -f[orce] ] -b[atch] filename\n"
5428 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n"
5429 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
5432 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
5437 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5440 } else if (dl_argv_match(dl
, "dev")) {
5443 } else if (dl_argv_match(dl
, "port")) {
5445 return cmd_port(dl
);
5446 } else if (dl_argv_match(dl
, "sb")) {
5449 } else if (dl_argv_match(dl
, "monitor")) {
5452 } else if (dl_argv_match(dl
, "dpipe")) {
5454 return cmd_dpipe(dl
);
5455 } else if (dl_argv_match(dl
, "resource")) {
5457 return cmd_resource(dl
);
5458 } else if (dl_argv_match(dl
, "region")) {
5460 return cmd_region(dl
);
5462 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
5466 static int dl_init(struct dl
*dl
)
5470 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
5472 pr_err("Failed to connect to devlink Netlink\n");
5476 err
= ifname_map_init(dl
);
5478 pr_err("Failed to create index map\n");
5479 goto err_ifname_map_create
;
5481 if (dl
->json_output
) {
5482 dl
->jw
= jsonw_new(stdout
);
5484 pr_err("Failed to create JSON writer\n");
5487 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
5492 ifname_map_fini(dl
);
5493 err_ifname_map_create
:
5494 mnlg_socket_close(dl
->nlg
);
5498 static void dl_fini(struct dl
*dl
)
5500 if (dl
->json_output
)
5501 jsonw_destroy(&dl
->jw
);
5502 ifname_map_fini(dl
);
5503 mnlg_socket_close(dl
->nlg
);
5506 static struct dl
*dl_alloc(void)
5510 dl
= calloc(1, sizeof(*dl
));
5516 static void dl_free(struct dl
*dl
)
5521 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
5525 int ret
= EXIT_SUCCESS
;
5527 if (name
&& strcmp(name
, "-") != 0) {
5528 if (freopen(name
, "r", stdin
) == NULL
) {
5530 "Cannot open file \"%s\" for reading: %s\n",
5531 name
, strerror(errno
));
5532 return EXIT_FAILURE
;
5537 while (getcmdline(&line
, &len
, stdin
) != -1) {
5541 largc
= makeargs(line
, largv
, 100);
5543 continue; /* blank line */
5545 if (dl_cmd(dl
, largc
, largv
)) {
5546 fprintf(stderr
, "Command failed %s:%d\n",
5560 int main(int argc
, char **argv
)
5562 static const struct option long_options
[] = {
5563 { "Version", no_argument
, NULL
, 'V' },
5564 { "force", no_argument
, NULL
, 'f' },
5565 { "batch", required_argument
, NULL
, 'b' },
5566 { "no-nice-names", no_argument
, NULL
, 'n' },
5567 { "json", no_argument
, NULL
, 'j' },
5568 { "pretty", no_argument
, NULL
, 'p' },
5569 { "verbose", no_argument
, NULL
, 'v' },
5570 { NULL
, 0, NULL
, 0 }
5572 const char *batch_file
= NULL
;
5581 pr_err("Failed to allocate memory for devlink\n");
5582 return EXIT_FAILURE
;
5585 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
5586 long_options
, NULL
)) >= 0) {
5590 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
5597 batch_file
= optarg
;
5600 dl
->no_nice_names
= true;
5603 dl
->json_output
= true;
5606 dl
->pretty_output
= true;
5612 pr_err("Unknown option.\n");
5629 err
= dl_batch(dl
, batch_file
, force
);
5631 err
= dl_cmd(dl
, argc
, argv
);