]>
Commit | Line | Data |
---|---|---|
a3c4b484 JP |
1 | /* |
2 | * devlink.c Devlink tool | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@mellanox.com> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <stdbool.h> | |
16 | #include <unistd.h> | |
17 | #include <getopt.h> | |
18 | #include <limits.h> | |
19 | #include <errno.h> | |
8b4fbf0b | 20 | #include <inttypes.h> |
a3c4b484 JP |
21 | #include <linux/genetlink.h> |
22 | #include <linux/devlink.h> | |
23 | #include <libmnl/libmnl.h> | |
b2947f8b | 24 | #include <netinet/ether.h> |
a3c4b484 JP |
25 | |
26 | #include "SNAPSHOT.h" | |
27 | #include "list.h" | |
28 | #include "mnlg.h" | |
e3d0f0c0 | 29 | #include "json_writer.h" |
afdc1194 | 30 | #include "utils.h" |
a3c4b484 | 31 | |
f57856fa OG |
32 | #define ESWITCH_MODE_LEGACY "legacy" |
33 | #define ESWITCH_MODE_SWITCHDEV "switchdev" | |
6566ca8c RD |
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" | |
f57856fa | 38 | |
13925ae9 MS |
39 | #define PARAM_CMODE_RUNTIME_STR "runtime" |
40 | #define PARAM_CMODE_DRIVERINIT_STR "driverinit" | |
41 | #define PARAM_CMODE_PERMANENT_STR "permanent" | |
42 | ||
844646a5 AS |
43 | static int g_new_line_count; |
44 | ||
a3c4b484 | 45 | #define pr_err(args...) fprintf(stderr, ##args) |
153c1a9b AS |
46 | #define pr_out(args...) \ |
47 | do { \ | |
48 | if (g_indent_newline) { \ | |
49 | fprintf(stdout, "%s", g_indent_str); \ | |
50 | g_indent_newline = false; \ | |
51 | } \ | |
52 | fprintf(stdout, ##args); \ | |
844646a5 | 53 | g_new_line_count = 0; \ |
153c1a9b AS |
54 | } while (0) |
55 | ||
25ec49be JP |
56 | #define pr_out_sp(num, args...) \ |
57 | do { \ | |
58 | int ret = fprintf(stdout, ##args); \ | |
59 | if (ret < num) \ | |
60 | fprintf(stdout, "%*s", num - ret, ""); \ | |
844646a5 | 61 | g_new_line_count = 0; \ |
25ec49be | 62 | } while (0) |
a3c4b484 | 63 | |
153c1a9b AS |
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] = ""; | |
69 | ||
70 | static void __pr_out_indent_inc(void) | |
71 | { | |
72 | if (g_indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN) | |
73 | return; | |
74 | g_indent_level += INDENT_STR_STEP; | |
75 | memset(g_indent_str, ' ', sizeof(g_indent_str)); | |
76 | g_indent_str[g_indent_level] = '\0'; | |
77 | } | |
78 | ||
79 | static void __pr_out_indent_dec(void) | |
80 | { | |
81 | if (g_indent_level - INDENT_STR_STEP < 0) | |
82 | return; | |
83 | g_indent_level -= INDENT_STR_STEP; | |
84 | g_indent_str[g_indent_level] = '\0'; | |
85 | } | |
86 | ||
87 | static void __pr_out_newline(void) | |
88 | { | |
844646a5 AS |
89 | if (g_new_line_count < 1) { |
90 | pr_out("\n"); | |
91 | g_indent_newline = true; | |
92 | } | |
93 | g_new_line_count++; | |
153c1a9b AS |
94 | } |
95 | ||
a3c4b484 JP |
96 | static int _mnlg_socket_recv_run(struct mnlg_socket *nlg, |
97 | mnl_cb_t data_cb, void *data) | |
98 | { | |
99 | int err; | |
100 | ||
101 | err = mnlg_socket_recv_run(nlg, data_cb, data); | |
102 | if (err < 0) { | |
103 | pr_err("devlink answers: %s\n", strerror(errno)); | |
104 | return -errno; | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int _mnlg_socket_sndrcv(struct mnlg_socket *nlg, | |
110 | const struct nlmsghdr *nlh, | |
111 | mnl_cb_t data_cb, void *data) | |
112 | { | |
113 | int err; | |
114 | ||
115 | err = mnlg_socket_send(nlg, nlh); | |
116 | if (err < 0) { | |
117 | pr_err("Failed to call mnlg_socket_send\n"); | |
118 | return -errno; | |
119 | } | |
120 | return _mnlg_socket_recv_run(nlg, data_cb, data); | |
121 | } | |
122 | ||
123 | static int _mnlg_socket_group_add(struct mnlg_socket *nlg, | |
124 | const char *group_name) | |
125 | { | |
126 | int err; | |
127 | ||
128 | err = mnlg_socket_group_add(nlg, group_name); | |
129 | if (err < 0) { | |
130 | pr_err("Failed to call mnlg_socket_group_add\n"); | |
131 | return -errno; | |
132 | } | |
133 | return 0; | |
134 | } | |
135 | ||
136 | struct ifname_map { | |
137 | struct list_head list; | |
138 | char *bus_name; | |
139 | char *dev_name; | |
140 | uint32_t port_index; | |
141 | char *ifname; | |
142 | }; | |
143 | ||
144 | static struct ifname_map *ifname_map_alloc(const char *bus_name, | |
145 | const char *dev_name, | |
146 | uint32_t port_index, | |
147 | const char *ifname) | |
148 | { | |
149 | struct ifname_map *ifname_map; | |
150 | ||
151 | ifname_map = calloc(1, sizeof(*ifname_map)); | |
152 | if (!ifname_map) | |
153 | return NULL; | |
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); | |
163 | free(ifname_map); | |
164 | return NULL; | |
165 | } | |
166 | return ifname_map; | |
167 | } | |
168 | ||
169 | static void ifname_map_free(struct ifname_map *ifname_map) | |
170 | { | |
171 | free(ifname_map->ifname); | |
172 | free(ifname_map->dev_name); | |
173 | free(ifname_map->bus_name); | |
174 | free(ifname_map); | |
175 | } | |
176 | ||
6563a6eb JP |
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) | |
e6d7367d JP |
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) | |
f57856fa | 188 | #define DL_OPT_ESWITCH_MODE BIT(11) |
6566ca8c | 189 | #define DL_OPT_ESWITCH_INLINE_MODE BIT(12) |
153c1a9b AS |
190 | #define DL_OPT_DPIPE_TABLE_NAME BIT(13) |
191 | #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14) | |
d315b706 | 192 | #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15) |
8cd64409 AS |
193 | #define DL_OPT_RESOURCE_PATH BIT(16) |
194 | #define DL_OPT_RESOURCE_SIZE BIT(17) | |
13925ae9 MS |
195 | #define DL_OPT_PARAM_NAME BIT(18) |
196 | #define DL_OPT_PARAM_VALUE BIT(19) | |
197 | #define DL_OPT_PARAM_CMODE BIT(20) | |
8b4fbf0b AV |
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) | |
6563a6eb JP |
202 | |
203 | struct dl_opts { | |
204 | uint32_t present; /* flags of present items */ | |
205 | char *bus_name; | |
206 | char *dev_name; | |
207 | uint32_t port_index; | |
208 | enum devlink_port_type port_type; | |
209 | uint32_t port_count; | |
e6d7367d JP |
210 | uint32_t sb_index; |
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; | |
f57856fa | 217 | enum devlink_eswitch_mode eswitch_mode; |
6566ca8c | 218 | enum devlink_eswitch_inline_mode eswitch_inline_mode; |
153c1a9b AS |
219 | const char *dpipe_table_name; |
220 | bool dpipe_counters_enable; | |
d315b706 | 221 | bool eswitch_encap_mode; |
8cd64409 AS |
222 | const char *resource_path; |
223 | uint32_t resource_size; | |
224 | uint32_t resource_id; | |
225 | bool resource_id_valid; | |
13925ae9 MS |
226 | const char *param_name; |
227 | const char *param_value; | |
228 | enum devlink_param_cmode cmode; | |
8b4fbf0b AV |
229 | char *region_name; |
230 | uint32_t region_snapshot_id; | |
231 | uint64_t region_address; | |
232 | uint64_t region_length; | |
6563a6eb JP |
233 | }; |
234 | ||
a3c4b484 JP |
235 | struct dl { |
236 | struct mnlg_socket *nlg; | |
237 | struct list_head ifname_map_list; | |
238 | int argc; | |
239 | char **argv; | |
43f35be4 | 240 | bool no_nice_names; |
6563a6eb | 241 | struct dl_opts opts; |
e3d0f0c0 JP |
242 | json_writer_t *jw; |
243 | bool json_output; | |
244 | bool pretty_output; | |
153c1a9b | 245 | bool verbose; |
e3d0f0c0 JP |
246 | struct { |
247 | bool present; | |
248 | char *bus_name; | |
249 | char *dev_name; | |
250 | uint32_t port_index; | |
251 | } arr_last; | |
a3c4b484 JP |
252 | }; |
253 | ||
254 | static int dl_argc(struct dl *dl) | |
255 | { | |
256 | return dl->argc; | |
257 | } | |
258 | ||
259 | static char *dl_argv(struct dl *dl) | |
260 | { | |
261 | if (dl_argc(dl) == 0) | |
262 | return NULL; | |
263 | return *dl->argv; | |
264 | } | |
265 | ||
266 | static void dl_arg_inc(struct dl *dl) | |
267 | { | |
268 | if (dl_argc(dl) == 0) | |
269 | return; | |
270 | dl->argc--; | |
271 | dl->argv++; | |
272 | } | |
273 | ||
274 | static char *dl_argv_next(struct dl *dl) | |
275 | { | |
276 | char *ret; | |
277 | ||
278 | if (dl_argc(dl) == 0) | |
279 | return NULL; | |
280 | ||
281 | ret = *dl->argv; | |
282 | dl_arg_inc(dl); | |
283 | return ret; | |
284 | } | |
285 | ||
286 | static char *dl_argv_index(struct dl *dl, unsigned int index) | |
287 | { | |
288 | if (index >= dl_argc(dl)) | |
289 | return NULL; | |
290 | return dl->argv[index]; | |
291 | } | |
292 | ||
293 | static int strcmpx(const char *str1, const char *str2) | |
294 | { | |
295 | if (strlen(str1) > strlen(str2)) | |
296 | return -1; | |
297 | return strncmp(str1, str2, strlen(str1)); | |
298 | } | |
299 | ||
300 | static bool dl_argv_match(struct dl *dl, const char *pattern) | |
301 | { | |
302 | if (dl_argc(dl) == 0) | |
303 | return false; | |
304 | return strcmpx(dl_argv(dl), pattern) == 0; | |
305 | } | |
306 | ||
307 | static bool dl_no_arg(struct dl *dl) | |
308 | { | |
309 | return dl_argc(dl) == 0; | |
310 | } | |
311 | ||
4f10cede AS |
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, | |
d315b706 | 337 | [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = MNL_TYPE_U8, |
153c1a9b AS |
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, | |
13925ae9 MS |
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, | |
8b4fbf0b AV |
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, | |
4f10cede AS |
386 | }; |
387 | ||
a3c4b484 JP |
388 | static int attr_cb(const struct nlattr *attr, void *data) |
389 | { | |
390 | const struct nlattr **tb = data; | |
391 | int type; | |
392 | ||
a3c4b484 | 393 | if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0) |
c619f2be | 394 | return MNL_CB_OK; |
a3c4b484 | 395 | |
4f10cede AS |
396 | type = mnl_attr_get_type(attr); |
397 | if (mnl_attr_validate(attr, devlink_policy[type]) < 0) | |
6566ca8c | 398 | return MNL_CB_ERROR; |
4f10cede | 399 | |
a3c4b484 JP |
400 | tb[type] = attr; |
401 | return MNL_CB_OK; | |
402 | } | |
403 | ||
404 | static int ifname_map_cb(const struct nlmsghdr *nlh, void *data) | |
405 | { | |
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; | |
414 | ||
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]) | |
418 | return MNL_CB_ERROR; | |
419 | ||
420 | if (!tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) | |
421 | return MNL_CB_OK; | |
422 | ||
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); | |
429 | if (!ifname_map) | |
430 | return MNL_CB_ERROR; | |
431 | list_add(&ifname_map->list, &dl->ifname_map_list); | |
432 | ||
433 | return MNL_CB_OK; | |
434 | } | |
435 | ||
436 | static void ifname_map_fini(struct dl *dl) | |
437 | { | |
438 | struct ifname_map *ifname_map, *tmp; | |
439 | ||
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); | |
444 | } | |
445 | } | |
446 | ||
447 | static int ifname_map_init(struct dl *dl) | |
448 | { | |
449 | struct nlmsghdr *nlh; | |
450 | int err; | |
451 | ||
452 | INIT_LIST_HEAD(&dl->ifname_map_list); | |
453 | ||
454 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, | |
455 | NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); | |
456 | ||
457 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, ifname_map_cb, dl); | |
458 | if (err) { | |
459 | ifname_map_fini(dl); | |
460 | return err; | |
461 | } | |
462 | return 0; | |
463 | } | |
464 | ||
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) | |
468 | { | |
469 | struct ifname_map *ifname_map; | |
470 | ||
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; | |
476 | return 0; | |
477 | } | |
478 | } | |
479 | return -ENOENT; | |
480 | } | |
481 | ||
e6d7367d JP |
482 | static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name, |
483 | const char *dev_name, uint32_t port_index, | |
484 | char **p_ifname) | |
485 | { | |
486 | struct ifname_map *ifname_map; | |
487 | ||
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; | |
493 | return 0; | |
494 | } | |
495 | } | |
496 | return -ENOENT; | |
497 | } | |
498 | ||
a3c4b484 JP |
499 | static unsigned int strslashcount(char *str) |
500 | { | |
501 | unsigned int count = 0; | |
502 | char *pos = str; | |
503 | ||
504 | while ((pos = strchr(pos, '/'))) { | |
505 | count++; | |
506 | pos++; | |
507 | } | |
508 | return count; | |
509 | } | |
510 | ||
511 | static int strslashrsplit(char *str, char **before, char **after) | |
512 | { | |
513 | char *slash; | |
514 | ||
515 | slash = strrchr(str, '/'); | |
516 | if (!slash) | |
517 | return -EINVAL; | |
518 | *slash = '\0'; | |
519 | *before = str; | |
520 | *after = slash + 1; | |
521 | return 0; | |
522 | } | |
523 | ||
8b4fbf0b AV |
524 | static int strtouint64_t(const char *str, uint64_t *p_val) |
525 | { | |
526 | char *endptr; | |
527 | unsigned long long int val; | |
528 | ||
529 | val = strtoull(str, &endptr, 10); | |
530 | if (endptr == str || *endptr != '\0') | |
531 | return -EINVAL; | |
532 | if (val > ULONG_MAX) | |
533 | return -ERANGE; | |
534 | *p_val = val; | |
535 | return 0; | |
536 | } | |
537 | ||
a3c4b484 JP |
538 | static int strtouint32_t(const char *str, uint32_t *p_val) |
539 | { | |
540 | char *endptr; | |
541 | unsigned long int val; | |
542 | ||
543 | val = strtoul(str, &endptr, 10); | |
544 | if (endptr == str || *endptr != '\0') | |
545 | return -EINVAL; | |
546 | if (val > UINT_MAX) | |
547 | return -ERANGE; | |
548 | *p_val = val; | |
549 | return 0; | |
550 | } | |
551 | ||
e6d7367d JP |
552 | static int strtouint16_t(const char *str, uint16_t *p_val) |
553 | { | |
554 | char *endptr; | |
555 | unsigned long int val; | |
556 | ||
557 | val = strtoul(str, &endptr, 10); | |
558 | if (endptr == str || *endptr != '\0') | |
559 | return -EINVAL; | |
560 | if (val > USHRT_MAX) | |
561 | return -ERANGE; | |
562 | *p_val = val; | |
563 | return 0; | |
564 | } | |
565 | ||
13925ae9 MS |
566 | static int strtouint8_t(const char *str, uint8_t *p_val) |
567 | { | |
568 | char *endptr; | |
569 | unsigned long int val; | |
570 | ||
571 | val = strtoul(str, &endptr, 10); | |
572 | if (endptr == str || *endptr != '\0') | |
573 | return -EINVAL; | |
574 | if (val > UCHAR_MAX) | |
575 | return -ERANGE; | |
576 | *p_val = val; | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static int strtobool(const char *str, bool *p_val) | |
581 | { | |
582 | bool val; | |
583 | ||
584 | if (!strcmp(str, "true") || !strcmp(str, "1")) | |
585 | val = true; | |
586 | else if (!strcmp(str, "false") || !strcmp(str, "0")) | |
587 | val = false; | |
588 | else | |
589 | return -EINVAL; | |
590 | *p_val = val; | |
591 | return 0; | |
592 | } | |
593 | ||
2f85a9c5 JP |
594 | static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name) |
595 | { | |
596 | strslashrsplit(str, p_bus_name, p_dev_name); | |
597 | return 0; | |
598 | } | |
599 | ||
6563a6eb | 600 | static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name) |
a3c4b484 JP |
601 | { |
602 | char *str = dl_argv_next(dl); | |
a3c4b484 JP |
603 | |
604 | if (!str) { | |
605 | pr_err("Devlink identification (\"bus_name/dev_name\") expected\n"); | |
606 | return -EINVAL; | |
607 | } | |
608 | if (strslashcount(str) != 1) { | |
609 | pr_err("Wrong devlink identification string format.\n"); | |
610 | pr_err("Expected \"bus_name/dev_name\".\n"); | |
611 | return -EINVAL; | |
612 | } | |
2f85a9c5 JP |
613 | return __dl_argv_handle(str, p_bus_name, p_dev_name); |
614 | } | |
a3c4b484 | 615 | |
2f85a9c5 JP |
616 | static int __dl_argv_handle_port(char *str, |
617 | char **p_bus_name, char **p_dev_name, | |
618 | uint32_t *p_port_index) | |
619 | { | |
6e33f7b0 PS |
620 | char *handlestr; |
621 | char *portstr; | |
2f85a9c5 JP |
622 | int err; |
623 | ||
6e33f7b0 PS |
624 | err = strslashrsplit(str, &handlestr, &portstr); |
625 | if (err) { | |
626 | pr_err("Port identification \"%s\" is invalid\n", str); | |
627 | return err; | |
628 | } | |
2f85a9c5 JP |
629 | err = strtouint32_t(portstr, p_port_index); |
630 | if (err) { | |
631 | pr_err("Port index \"%s\" is not a number or not within range\n", | |
632 | portstr); | |
633 | return err; | |
634 | } | |
6e33f7b0 PS |
635 | err = strslashrsplit(handlestr, p_bus_name, p_dev_name); |
636 | if (err) { | |
637 | pr_err("Port identification \"%s\" is invalid\n", str); | |
638 | return err; | |
639 | } | |
2f85a9c5 JP |
640 | return 0; |
641 | } | |
642 | ||
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) | |
646 | { | |
647 | int err; | |
648 | ||
649 | err = ifname_map_lookup(dl, str, p_bus_name, p_dev_name, | |
650 | p_port_index); | |
651 | if (err) { | |
652 | pr_err("Netdevice \"%s\" not found\n", str); | |
653 | return err; | |
654 | } | |
a3c4b484 JP |
655 | return 0; |
656 | } | |
657 | ||
6563a6eb JP |
658 | static int dl_argv_handle_port(struct dl *dl, char **p_bus_name, |
659 | char **p_dev_name, uint32_t *p_port_index) | |
a3c4b484 JP |
660 | { |
661 | char *str = dl_argv_next(dl); | |
662 | unsigned int slash_count; | |
a3c4b484 JP |
663 | |
664 | if (!str) { | |
665 | pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n"); | |
666 | return -EINVAL; | |
667 | } | |
668 | slash_count = strslashcount(str); | |
7a34b9d0 HL |
669 | switch (slash_count) { |
670 | case 0: | |
671 | return __dl_argv_handle_port_ifname(dl, str, p_bus_name, | |
672 | p_dev_name, p_port_index); | |
673 | case 2: | |
674 | return __dl_argv_handle_port(str, p_bus_name, | |
675 | p_dev_name, p_port_index); | |
676 | default: | |
a3c4b484 JP |
677 | pr_err("Wrong port identification string format.\n"); |
678 | pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n"); | |
679 | return -EINVAL; | |
680 | } | |
2f85a9c5 JP |
681 | } |
682 | ||
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) | |
686 | { | |
687 | char *str = dl_argv_next(dl); | |
688 | unsigned int slash_count; | |
689 | int err; | |
690 | ||
691 | if (!str) { | |
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"); | |
695 | return -EINVAL; | |
696 | } | |
697 | slash_count = strslashcount(str); | |
698 | if (slash_count == 1) { | |
699 | err = __dl_argv_handle(str, p_bus_name, p_dev_name); | |
700 | if (err) | |
a3c4b484 | 701 | return err; |
2f85a9c5 JP |
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); | |
706 | if (err) | |
707 | return err; | |
708 | *p_handle_bit = DL_OPT_HANDLEP; | |
a3c4b484 | 709 | } else if (slash_count == 0) { |
2f85a9c5 JP |
710 | err = __dl_argv_handle_port_ifname(dl, str, p_bus_name, |
711 | p_dev_name, p_port_index); | |
712 | if (err) | |
a3c4b484 | 713 | return err; |
2f85a9c5 JP |
714 | *p_handle_bit = DL_OPT_HANDLEP; |
715 | } else { | |
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"); | |
718 | return -EINVAL; | |
a3c4b484 | 719 | } |
a3c4b484 JP |
720 | return 0; |
721 | } | |
722 | ||
8b4fbf0b AV |
723 | static int __dl_argv_handle_region(char *str, char **p_bus_name, |
724 | char **p_dev_name, char **p_region) | |
725 | { | |
726 | char *handlestr; | |
727 | int err; | |
728 | ||
729 | err = strslashrsplit(str, &handlestr, p_region); | |
730 | if (err) { | |
731 | pr_err("Region identification \"%s\" is invalid\n", str); | |
732 | return err; | |
733 | } | |
734 | err = strslashrsplit(handlestr, p_bus_name, p_dev_name); | |
735 | if (err) { | |
736 | pr_err("Region identification \"%s\" is invalid\n", str); | |
737 | return err; | |
738 | } | |
739 | return 0; | |
740 | } | |
741 | ||
742 | static int dl_argv_handle_region(struct dl *dl, char **p_bus_name, | |
743 | char **p_dev_name, char **p_region) | |
744 | { | |
745 | char *str = dl_argv_next(dl); | |
746 | unsigned int slash_count; | |
747 | ||
748 | if (!str) { | |
749 | pr_err("Expected \"bus_name/dev_name/region\" identification.\n"); | |
750 | return -EINVAL; | |
751 | } | |
752 | ||
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"); | |
757 | return -EINVAL; | |
758 | } | |
759 | ||
760 | return __dl_argv_handle_region(str, p_bus_name, p_dev_name, p_region); | |
761 | } | |
762 | ||
763 | static int dl_argv_uint64_t(struct dl *dl, uint64_t *p_val) | |
764 | { | |
765 | char *str = dl_argv_next(dl); | |
766 | int err; | |
767 | ||
768 | if (!str) { | |
769 | pr_err("Unsigned number argument expected\n"); | |
770 | return -EINVAL; | |
771 | } | |
772 | ||
773 | err = strtouint64_t(str, p_val); | |
774 | if (err) { | |
775 | pr_err("\"%s\" is not a number or not within range\n", str); | |
776 | return err; | |
777 | } | |
778 | return 0; | |
779 | } | |
780 | ||
a3c4b484 JP |
781 | static int dl_argv_uint32_t(struct dl *dl, uint32_t *p_val) |
782 | { | |
783 | char *str = dl_argv_next(dl); | |
784 | int err; | |
785 | ||
786 | if (!str) { | |
787 | pr_err("Unsigned number argument expected\n"); | |
788 | return -EINVAL; | |
789 | } | |
790 | ||
791 | err = strtouint32_t(str, p_val); | |
792 | if (err) { | |
793 | pr_err("\"%s\" is not a number or not within range\n", str); | |
794 | return err; | |
795 | } | |
796 | return 0; | |
797 | } | |
798 | ||
e6d7367d JP |
799 | static int dl_argv_uint16_t(struct dl *dl, uint16_t *p_val) |
800 | { | |
801 | char *str = dl_argv_next(dl); | |
802 | int err; | |
803 | ||
804 | if (!str) { | |
805 | pr_err("Unsigned number argument expected\n"); | |
806 | return -EINVAL; | |
807 | } | |
808 | ||
809 | err = strtouint16_t(str, p_val); | |
810 | if (err) { | |
811 | pr_err("\"%s\" is not a number or not within range\n", str); | |
812 | return err; | |
813 | } | |
814 | return 0; | |
815 | } | |
816 | ||
a3c4b484 JP |
817 | static int dl_argv_str(struct dl *dl, const char **p_str) |
818 | { | |
819 | const char *str = dl_argv_next(dl); | |
820 | ||
821 | if (!str) { | |
822 | pr_err("String parameter expected\n"); | |
823 | return -EINVAL; | |
824 | } | |
825 | *p_str = str; | |
826 | return 0; | |
827 | } | |
828 | ||
829 | static int port_type_get(const char *typestr, enum devlink_port_type *p_type) | |
830 | { | |
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; | |
837 | } else { | |
838 | pr_err("Unknown port type \"%s\"\n", typestr); | |
839 | return -EINVAL; | |
840 | } | |
841 | return 0; | |
842 | } | |
843 | ||
e6d7367d JP |
844 | static int pool_type_get(const char *typestr, enum devlink_sb_pool_type *p_type) |
845 | { | |
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; | |
850 | } else { | |
851 | pr_err("Unknown pool type \"%s\"\n", typestr); | |
852 | return -EINVAL; | |
853 | } | |
854 | return 0; | |
855 | } | |
856 | ||
857 | static int threshold_type_get(const char *typestr, | |
858 | enum devlink_sb_threshold_type *p_type) | |
859 | { | |
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; | |
864 | } else { | |
865 | pr_err("Unknown threshold type \"%s\"\n", typestr); | |
866 | return -EINVAL; | |
867 | } | |
868 | return 0; | |
869 | } | |
870 | ||
7c55d770 SH |
871 | static int eswitch_mode_get(const char *typestr, |
872 | enum devlink_eswitch_mode *p_mode) | |
f57856fa OG |
873 | { |
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; | |
878 | } else { | |
879 | pr_err("Unknown eswitch mode \"%s\"\n", typestr); | |
880 | return -EINVAL; | |
881 | } | |
882 | return 0; | |
883 | } | |
884 | ||
6566ca8c RD |
885 | static int eswitch_inline_mode_get(const char *typestr, |
886 | enum devlink_eswitch_inline_mode *p_mode) | |
887 | { | |
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; | |
896 | } else { | |
897 | pr_err("Unknown eswitch inline mode \"%s\"\n", typestr); | |
898 | return -EINVAL; | |
899 | } | |
900 | return 0; | |
901 | } | |
902 | ||
153c1a9b AS |
903 | static int dpipe_counters_enable_get(const char *typestr, |
904 | bool *counters_enable) | |
905 | { | |
906 | if (strcmp(typestr, "enable") == 0) { | |
907 | *counters_enable = 1; | |
908 | } else if (strcmp(typestr, "disable") == 0) { | |
909 | *counters_enable = 0; | |
910 | } else { | |
911 | pr_err("Unknown counter_state \"%s\"\n", typestr); | |
912 | return -EINVAL; | |
913 | } | |
914 | return 0; | |
915 | } | |
916 | ||
d315b706 RD |
917 | static int eswitch_encap_mode_get(const char *typestr, bool *p_mode) |
918 | { | |
919 | if (strcmp(typestr, "enable") == 0) { | |
920 | *p_mode = true; | |
921 | } else if (strcmp(typestr, "disable") == 0) { | |
922 | *p_mode = false; | |
923 | } else { | |
924 | pr_err("Unknown eswitch encap mode \"%s\"\n", typestr); | |
925 | return -EINVAL; | |
926 | } | |
927 | return 0; | |
928 | } | |
929 | ||
13925ae9 MS |
930 | static int param_cmode_get(const char *cmodestr, |
931 | enum devlink_param_cmode *cmode) | |
932 | { | |
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; | |
939 | } else { | |
940 | pr_err("Unknown configuration mode \"%s\"\n", cmodestr); | |
941 | return -EINVAL; | |
942 | } | |
943 | return 0; | |
944 | } | |
945 | ||
6563a6eb JP |
946 | static int dl_argv_parse(struct dl *dl, uint32_t o_required, |
947 | uint32_t o_optional) | |
a3c4b484 | 948 | { |
6563a6eb | 949 | struct dl_opts *opts = &dl->opts; |
a3c4b484 JP |
950 | uint32_t o_all = o_required | o_optional; |
951 | uint32_t o_found = 0; | |
952 | int err; | |
953 | ||
2f85a9c5 | 954 | if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) { |
8579a398 | 955 | uint32_t handle_bit; |
2f85a9c5 JP |
956 | |
957 | err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name, | |
958 | &opts->port_index, &handle_bit); | |
959 | if (err) | |
960 | return err; | |
961 | o_found |= handle_bit; | |
962 | } else if (o_required & DL_OPT_HANDLE) { | |
6563a6eb | 963 | err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name); |
a3c4b484 JP |
964 | if (err) |
965 | return err; | |
6563a6eb | 966 | o_found |= DL_OPT_HANDLE; |
a3c4b484 | 967 | } else if (o_required & DL_OPT_HANDLEP) { |
6563a6eb JP |
968 | err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name, |
969 | &opts->port_index); | |
a3c4b484 JP |
970 | if (err) |
971 | return err; | |
6563a6eb | 972 | o_found |= DL_OPT_HANDLEP; |
8b4fbf0b AV |
973 | } else if (o_required & DL_OPT_HANDLE_REGION) { |
974 | err = dl_argv_handle_region(dl, &opts->bus_name, | |
975 | &opts->dev_name, | |
976 | &opts->region_name); | |
977 | if (err) | |
978 | return err; | |
979 | o_found |= DL_OPT_HANDLE_REGION; | |
a3c4b484 JP |
980 | } |
981 | ||
982 | while (dl_argc(dl)) { | |
983 | if (dl_argv_match(dl, "type") && | |
984 | (o_all & DL_OPT_PORT_TYPE)) { | |
a3c4b484 JP |
985 | const char *typestr; |
986 | ||
987 | dl_arg_inc(dl); | |
988 | err = dl_argv_str(dl, &typestr); | |
989 | if (err) | |
990 | return err; | |
6563a6eb | 991 | err = port_type_get(typestr, &opts->port_type); |
a3c4b484 JP |
992 | if (err) |
993 | return err; | |
a3c4b484 JP |
994 | o_found |= DL_OPT_PORT_TYPE; |
995 | } else if (dl_argv_match(dl, "count") && | |
996 | (o_all & DL_OPT_PORT_COUNT)) { | |
a3c4b484 | 997 | dl_arg_inc(dl); |
6563a6eb | 998 | err = dl_argv_uint32_t(dl, &opts->port_count); |
a3c4b484 JP |
999 | if (err) |
1000 | return err; | |
a3c4b484 | 1001 | o_found |= DL_OPT_PORT_COUNT; |
e6d7367d JP |
1002 | } else if (dl_argv_match(dl, "sb") && |
1003 | (o_all & DL_OPT_SB)) { | |
1004 | dl_arg_inc(dl); | |
1005 | err = dl_argv_uint32_t(dl, &opts->sb_index); | |
1006 | if (err) | |
1007 | return err; | |
1008 | o_found |= DL_OPT_SB; | |
1009 | } else if (dl_argv_match(dl, "pool") && | |
1010 | (o_all & DL_OPT_SB_POOL)) { | |
1011 | dl_arg_inc(dl); | |
1012 | err = dl_argv_uint16_t(dl, &opts->sb_pool_index); | |
1013 | if (err) | |
1014 | return err; | |
1015 | o_found |= DL_OPT_SB_POOL; | |
1016 | } else if (dl_argv_match(dl, "size") && | |
1017 | (o_all & DL_OPT_SB_SIZE)) { | |
1018 | dl_arg_inc(dl); | |
1019 | err = dl_argv_uint32_t(dl, &opts->sb_pool_size); | |
1020 | if (err) | |
1021 | return err; | |
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; | |
1026 | ||
1027 | dl_arg_inc(dl); | |
1028 | err = dl_argv_str(dl, &typestr); | |
1029 | if (err) | |
1030 | return err; | |
1031 | err = pool_type_get(typestr, &opts->sb_pool_type); | |
1032 | if (err) | |
1033 | return err; | |
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; | |
1038 | ||
1039 | dl_arg_inc(dl); | |
1040 | err = dl_argv_str(dl, &typestr); | |
1041 | if (err) | |
1042 | return err; | |
1043 | err = threshold_type_get(typestr, | |
1044 | &opts->sb_pool_thtype); | |
1045 | if (err) | |
1046 | return err; | |
1047 | o_found |= DL_OPT_SB_THTYPE; | |
1048 | } else if (dl_argv_match(dl, "th") && | |
1049 | (o_all & DL_OPT_SB_TH)) { | |
1050 | dl_arg_inc(dl); | |
1051 | err = dl_argv_uint32_t(dl, &opts->sb_threshold); | |
1052 | if (err) | |
1053 | return err; | |
1054 | o_found |= DL_OPT_SB_TH; | |
1055 | } else if (dl_argv_match(dl, "tc") && | |
1056 | (o_all & DL_OPT_SB_TC)) { | |
1057 | dl_arg_inc(dl); | |
1058 | err = dl_argv_uint16_t(dl, &opts->sb_tc_index); | |
1059 | if (err) | |
1060 | return err; | |
1061 | o_found |= DL_OPT_SB_TC; | |
f57856fa OG |
1062 | } else if (dl_argv_match(dl, "mode") && |
1063 | (o_all & DL_OPT_ESWITCH_MODE)) { | |
1064 | const char *typestr; | |
7c55d770 | 1065 | |
f57856fa OG |
1066 | dl_arg_inc(dl); |
1067 | err = dl_argv_str(dl, &typestr); | |
1068 | if (err) | |
1069 | return err; | |
1070 | err = eswitch_mode_get(typestr, &opts->eswitch_mode); | |
1071 | if (err) | |
1072 | return err; | |
1073 | o_found |= DL_OPT_ESWITCH_MODE; | |
6566ca8c RD |
1074 | } else if (dl_argv_match(dl, "inline-mode") && |
1075 | (o_all & DL_OPT_ESWITCH_INLINE_MODE)) { | |
1076 | const char *typestr; | |
1077 | ||
1078 | dl_arg_inc(dl); | |
1079 | err = dl_argv_str(dl, &typestr); | |
1080 | if (err) | |
1081 | return err; | |
1082 | err = eswitch_inline_mode_get( | |
1083 | typestr, &opts->eswitch_inline_mode); | |
1084 | if (err) | |
1085 | return err; | |
1086 | o_found |= DL_OPT_ESWITCH_INLINE_MODE; | |
153c1a9b AS |
1087 | } else if (dl_argv_match(dl, "name") && |
1088 | (o_all & DL_OPT_DPIPE_TABLE_NAME)) { | |
1089 | dl_arg_inc(dl); | |
1090 | err = dl_argv_str(dl, &opts->dpipe_table_name); | |
1091 | if (err) | |
1092 | return err; | |
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; | |
1097 | ||
1098 | dl_arg_inc(dl); | |
1099 | err = dl_argv_str(dl, &typestr); | |
1100 | if (err) | |
1101 | return err; | |
1102 | err = dpipe_counters_enable_get(typestr, | |
1103 | &opts->dpipe_counters_enable); | |
1104 | if (err) | |
1105 | return err; | |
1106 | o_found |= DL_OPT_DPIPE_TABLE_COUNTERS; | |
d315b706 RD |
1107 | } else if (dl_argv_match(dl, "encap") && |
1108 | (o_all & DL_OPT_ESWITCH_ENCAP_MODE)) { | |
1109 | const char *typestr; | |
153c1a9b | 1110 | |
d315b706 RD |
1111 | dl_arg_inc(dl); |
1112 | err = dl_argv_str(dl, &typestr); | |
1113 | if (err) | |
1114 | return err; | |
1115 | err = eswitch_encap_mode_get(typestr, | |
1116 | &opts->eswitch_encap_mode); | |
1117 | if (err) | |
1118 | return err; | |
1119 | o_found |= DL_OPT_ESWITCH_ENCAP_MODE; | |
8cd64409 AS |
1120 | } else if (dl_argv_match(dl, "path") && |
1121 | (o_all & DL_OPT_RESOURCE_PATH)) { | |
1122 | dl_arg_inc(dl); | |
1123 | err = dl_argv_str(dl, &opts->resource_path); | |
1124 | if (err) | |
1125 | return err; | |
1126 | o_found |= DL_OPT_RESOURCE_PATH; | |
1127 | } else if (dl_argv_match(dl, "size") && | |
1128 | (o_all & DL_OPT_RESOURCE_SIZE)) { | |
1129 | dl_arg_inc(dl); | |
1130 | err = dl_argv_uint32_t(dl, &opts->resource_size); | |
1131 | if (err) | |
1132 | return err; | |
1133 | o_found |= DL_OPT_RESOURCE_SIZE; | |
13925ae9 MS |
1134 | } else if (dl_argv_match(dl, "name") && |
1135 | (o_all & DL_OPT_PARAM_NAME)) { | |
1136 | dl_arg_inc(dl); | |
1137 | err = dl_argv_str(dl, &opts->param_name); | |
1138 | if (err) | |
1139 | return err; | |
1140 | o_found |= DL_OPT_PARAM_NAME; | |
1141 | } else if (dl_argv_match(dl, "value") && | |
1142 | (o_all & DL_OPT_PARAM_VALUE)) { | |
1143 | dl_arg_inc(dl); | |
1144 | err = dl_argv_str(dl, &opts->param_value); | |
1145 | if (err) | |
1146 | return err; | |
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; | |
1151 | ||
1152 | dl_arg_inc(dl); | |
1153 | err = dl_argv_str(dl, &cmodestr); | |
1154 | if (err) | |
1155 | return err; | |
1156 | err = param_cmode_get(cmodestr, &opts->cmode); | |
1157 | if (err) | |
1158 | return err; | |
1159 | o_found |= DL_OPT_PARAM_CMODE; | |
8b4fbf0b AV |
1160 | } else if (dl_argv_match(dl, "snapshot") && |
1161 | (o_all & DL_OPT_REGION_SNAPSHOT_ID)) { | |
1162 | dl_arg_inc(dl); | |
1163 | err = dl_argv_uint32_t(dl, &opts->region_snapshot_id); | |
1164 | if (err) | |
1165 | return err; | |
1166 | o_found |= DL_OPT_REGION_SNAPSHOT_ID; | |
1167 | } else if (dl_argv_match(dl, "address") && | |
1168 | (o_all & DL_OPT_REGION_ADDRESS)) { | |
1169 | dl_arg_inc(dl); | |
1170 | err = dl_argv_uint64_t(dl, &opts->region_address); | |
1171 | if (err) | |
1172 | return err; | |
1173 | o_found |= DL_OPT_REGION_ADDRESS; | |
1174 | } else if (dl_argv_match(dl, "length") && | |
1175 | (o_all & DL_OPT_REGION_LENGTH)) { | |
1176 | dl_arg_inc(dl); | |
1177 | err = dl_argv_uint64_t(dl, &opts->region_length); | |
1178 | if (err) | |
1179 | return err; | |
1180 | o_found |= DL_OPT_REGION_LENGTH; | |
a3c4b484 JP |
1181 | } else { |
1182 | pr_err("Unknown option \"%s\"\n", dl_argv(dl)); | |
1183 | return -EINVAL; | |
1184 | } | |
1185 | } | |
1186 | ||
6563a6eb JP |
1187 | opts->present = o_found; |
1188 | ||
e6d7367d JP |
1189 | if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) { |
1190 | opts->sb_index = 0; | |
1191 | opts->present |= DL_OPT_SB; | |
1192 | } | |
1193 | ||
a3c4b484 JP |
1194 | if ((o_required & DL_OPT_PORT_TYPE) && !(o_found & DL_OPT_PORT_TYPE)) { |
1195 | pr_err("Port type option expected.\n"); | |
1196 | return -EINVAL; | |
1197 | } | |
1198 | ||
1199 | if ((o_required & DL_OPT_PORT_COUNT) && | |
1200 | !(o_found & DL_OPT_PORT_COUNT)) { | |
1201 | pr_err("Port split count option expected.\n"); | |
1202 | return -EINVAL; | |
1203 | } | |
1204 | ||
e6d7367d JP |
1205 | if ((o_required & DL_OPT_SB_POOL) && !(o_found & DL_OPT_SB_POOL)) { |
1206 | pr_err("Pool index option expected.\n"); | |
1207 | return -EINVAL; | |
1208 | } | |
1209 | ||
1210 | if ((o_required & DL_OPT_SB_SIZE) && !(o_found & DL_OPT_SB_SIZE)) { | |
1211 | pr_err("Pool size option expected.\n"); | |
1212 | return -EINVAL; | |
1213 | } | |
1214 | ||
1215 | if ((o_required & DL_OPT_SB_TYPE) && !(o_found & DL_OPT_SB_TYPE)) { | |
1216 | pr_err("Pool type option expected.\n"); | |
1217 | return -EINVAL; | |
1218 | } | |
1219 | ||
1220 | if ((o_required & DL_OPT_SB_THTYPE) && !(o_found & DL_OPT_SB_THTYPE)) { | |
1221 | pr_err("Pool threshold type option expected.\n"); | |
1222 | return -EINVAL; | |
1223 | } | |
1224 | ||
1225 | if ((o_required & DL_OPT_SB_TH) && !(o_found & DL_OPT_SB_TH)) { | |
1226 | pr_err("Threshold option expected.\n"); | |
1227 | return -EINVAL; | |
1228 | } | |
1229 | ||
1230 | if ((o_required & DL_OPT_SB_TC) && !(o_found & DL_OPT_SB_TC)) { | |
1231 | pr_err("TC index option expected.\n"); | |
1232 | return -EINVAL; | |
1233 | } | |
f57856fa | 1234 | |
7c55d770 SH |
1235 | if ((o_required & DL_OPT_ESWITCH_MODE) && |
1236 | !(o_found & DL_OPT_ESWITCH_MODE)) { | |
f57856fa OG |
1237 | pr_err("E-Switch mode option expected.\n"); |
1238 | return -EINVAL; | |
1239 | } | |
1240 | ||
6566ca8c RD |
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"); | |
1244 | return -EINVAL; | |
1245 | } | |
1246 | ||
153c1a9b AS |
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"); | |
1250 | return -EINVAL; | |
1251 | } | |
1252 | ||
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"); | |
1256 | return -EINVAL; | |
1257 | } | |
d315b706 RD |
1258 | |
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"); | |
1262 | return -EINVAL; | |
1263 | } | |
1264 | ||
13925ae9 MS |
1265 | if ((o_required & DL_OPT_PARAM_NAME) && |
1266 | !(o_found & DL_OPT_PARAM_NAME)) { | |
1267 | pr_err("Parameter name expected.\n"); | |
1268 | return -EINVAL; | |
1269 | } | |
1270 | ||
1271 | if ((o_required & DL_OPT_PARAM_VALUE) && | |
1272 | !(o_found & DL_OPT_PARAM_VALUE)) { | |
1273 | pr_err("Value to set expected.\n"); | |
1274 | return -EINVAL; | |
1275 | } | |
1276 | ||
1277 | if ((o_required & DL_OPT_PARAM_CMODE) && | |
1278 | !(o_found & DL_OPT_PARAM_CMODE)) { | |
1279 | pr_err("Configuration mode expected.\n"); | |
1280 | return -EINVAL; | |
1281 | } | |
1282 | ||
8b4fbf0b AV |
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"); | |
1286 | return -EINVAL; | |
1287 | } | |
1288 | ||
1289 | if ((o_required & DL_OPT_REGION_ADDRESS) && | |
1290 | !(o_found & DL_OPT_REGION_ADDRESS)) { | |
1291 | pr_err("Region address value expected.\n"); | |
1292 | return -EINVAL; | |
1293 | } | |
1294 | ||
1295 | if ((o_required & DL_OPT_REGION_LENGTH) && | |
1296 | !(o_found & DL_OPT_REGION_LENGTH)) { | |
1297 | pr_err("Region length value expected.\n"); | |
1298 | return -EINVAL; | |
1299 | } | |
1300 | ||
a3c4b484 JP |
1301 | return 0; |
1302 | } | |
1303 | ||
6563a6eb JP |
1304 | static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl) |
1305 | { | |
1306 | struct dl_opts *opts = &dl->opts; | |
1307 | ||
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, | |
1315 | opts->port_index); | |
8b4fbf0b AV |
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, | |
1320 | opts->region_name); | |
6563a6eb JP |
1321 | } |
1322 | if (opts->present & DL_OPT_PORT_TYPE) | |
1323 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_TYPE, | |
1324 | opts->port_type); | |
1325 | if (opts->present & DL_OPT_PORT_COUNT) | |
1326 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_SPLIT_COUNT, | |
1327 | opts->port_count); | |
e6d7367d JP |
1328 | if (opts->present & DL_OPT_SB) |
1329 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_INDEX, | |
1330 | opts->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, | |
1348 | opts->sb_tc_index); | |
f57856fa OG |
1349 | if (opts->present & DL_OPT_ESWITCH_MODE) |
1350 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_ESWITCH_MODE, | |
1351 | opts->eswitch_mode); | |
6566ca8c RD |
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); | |
153c1a9b AS |
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); | |
d315b706 RD |
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); | |
8cd64409 AS |
1364 | if ((opts->present & DL_OPT_RESOURCE_PATH) && opts->resource_id_valid) |
1365 | mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_ID, | |
1366 | opts->resource_id); | |
1367 | if (opts->present & DL_OPT_RESOURCE_SIZE) | |
1368 | mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE, | |
1369 | opts->resource_size); | |
13925ae9 MS |
1370 | if (opts->present & DL_OPT_PARAM_NAME) |
1371 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_NAME, | |
1372 | opts->param_name); | |
1373 | if (opts->present & DL_OPT_PARAM_CMODE) | |
1374 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, | |
1375 | opts->cmode); | |
8b4fbf0b AV |
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); | |
6563a6eb JP |
1385 | } |
1386 | ||
1387 | static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl, | |
1388 | uint32_t o_required, uint32_t o_optional) | |
1389 | { | |
1390 | int err; | |
1391 | ||
1392 | err = dl_argv_parse(dl, o_required, o_optional); | |
1393 | if (err) | |
1394 | return err; | |
1395 | dl_opts_put(nlh, dl); | |
1396 | return 0; | |
1397 | } | |
1398 | ||
25ec49be JP |
1399 | static bool dl_dump_filter(struct dl *dl, struct nlattr **tb) |
1400 | { | |
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]; | |
1406 | ||
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); | |
1411 | ||
1412 | if (strcmp(bus_name, opts->bus_name) != 0 || | |
1413 | strcmp(dev_name, opts->dev_name) != 0) | |
1414 | return false; | |
1415 | } | |
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); | |
1421 | ||
1422 | if (strcmp(bus_name, opts->bus_name) != 0 || | |
1423 | strcmp(dev_name, opts->dev_name) != 0 || | |
1424 | port_index != opts->port_index) | |
1425 | return false; | |
1426 | } | |
1427 | if (opts->present & DL_OPT_SB && attr_sb_index) { | |
1428 | uint32_t sb_index = mnl_attr_get_u32(attr_sb_index); | |
1429 | ||
1430 | if (sb_index != opts->sb_index) | |
1431 | return false; | |
1432 | } | |
1433 | return true; | |
1434 | } | |
707a91c5 | 1435 | |
a3c4b484 JP |
1436 | static void cmd_dev_help(void) |
1437 | { | |
7a9466db | 1438 | pr_err("Usage: devlink dev show [ DEV ]\n"); |
a93b6bb3 | 1439 | pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n"); |
6566ca8c | 1440 | pr_err(" [ inline-mode { none | link | network | transport } ]\n"); |
d315b706 | 1441 | pr_err(" [ encap { disable | enable } ]\n"); |
a93b6bb3 | 1442 | pr_err(" devlink dev eswitch show DEV\n"); |
13925ae9 MS |
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"); | |
06dd94f9 | 1445 | pr_err(" devlink dev reload DEV\n"); |
a3c4b484 JP |
1446 | } |
1447 | ||
e3d0f0c0 JP |
1448 | static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name, |
1449 | const char *dev_name) | |
43f35be4 | 1450 | { |
e3d0f0c0 JP |
1451 | if (!dl->arr_last.present) |
1452 | return false; | |
1453 | return strcmp(dl->arr_last.bus_name, bus_name) == 0 && | |
1454 | strcmp(dl->arr_last.dev_name, dev_name) == 0; | |
43f35be4 JP |
1455 | } |
1456 | ||
e3d0f0c0 JP |
1457 | static void arr_last_handle_set(struct dl *dl, const char *bus_name, |
1458 | const char *dev_name) | |
a3c4b484 | 1459 | { |
e3d0f0c0 JP |
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); | |
a3c4b484 JP |
1465 | } |
1466 | ||
e3d0f0c0 JP |
1467 | static bool should_arr_last_handle_start(struct dl *dl, const char *bus_name, |
1468 | const char *dev_name) | |
43f35be4 | 1469 | { |
e3d0f0c0 | 1470 | return !cmp_arr_last_handle(dl, bus_name, dev_name); |
43f35be4 JP |
1471 | } |
1472 | ||
e3d0f0c0 JP |
1473 | static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name, |
1474 | const char *dev_name) | |
68cab0ba | 1475 | { |
e3d0f0c0 JP |
1476 | return dl->arr_last.present && |
1477 | !cmp_arr_last_handle(dl, bus_name, dev_name); | |
43f35be4 JP |
1478 | } |
1479 | ||
e3d0f0c0 JP |
1480 | static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb, |
1481 | bool content, bool array) | |
e6d7367d | 1482 | { |
e3d0f0c0 JP |
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]); | |
1485 | char buf[32]; | |
e6d7367d | 1486 | |
e3d0f0c0 | 1487 | sprintf(buf, "%s/%s", bus_name, dev_name); |
e6d7367d | 1488 | |
e3d0f0c0 JP |
1489 | if (dl->json_output) { |
1490 | if (array) { | |
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, | |
1494 | dev_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); | |
1499 | } else { | |
1500 | jsonw_start_object(dl->jw); | |
1501 | } | |
1502 | } else { | |
1503 | jsonw_name(dl->jw, buf); | |
1504 | jsonw_start_object(dl->jw); | |
1505 | } | |
1506 | } else { | |
153c1a9b AS |
1507 | if (array) { |
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, | |
1511 | dev_name)) { | |
1512 | pr_out("%s%s", buf, content ? ":" : ""); | |
1513 | __pr_out_newline(); | |
1514 | __pr_out_indent_inc(); | |
1515 | arr_last_handle_set(dl, bus_name, dev_name); | |
1516 | } | |
1517 | } else { | |
1518 | pr_out("%s%s", buf, content ? ":" : ""); | |
1519 | } | |
e3d0f0c0 JP |
1520 | } |
1521 | } | |
e6d7367d | 1522 | |
e3d0f0c0 JP |
1523 | static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb) |
1524 | { | |
1525 | __pr_out_handle_start(dl, tb, true, true); | |
e6d7367d JP |
1526 | } |
1527 | ||
e3d0f0c0 JP |
1528 | static void pr_out_handle_end(struct dl *dl) |
1529 | { | |
1530 | if (dl->json_output) | |
1531 | jsonw_end_object(dl->jw); | |
1532 | else | |
153c1a9b | 1533 | __pr_out_newline(); |
e3d0f0c0 JP |
1534 | } |
1535 | ||
1536 | static void pr_out_handle(struct dl *dl, struct nlattr **tb) | |
1537 | { | |
1538 | __pr_out_handle_start(dl, tb, false, false); | |
1539 | pr_out_handle_end(dl); | |
1540 | } | |
1541 | ||
1542 | static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name, | |
1543 | const char *dev_name, uint32_t port_index) | |
1544 | { | |
1545 | return cmp_arr_last_handle(dl, bus_name, dev_name) && | |
1546 | dl->arr_last.port_index == port_index; | |
1547 | } | |
1548 | ||
1549 | static void arr_last_port_handle_set(struct dl *dl, const char *bus_name, | |
1550 | const char *dev_name, uint32_t port_index) | |
1551 | { | |
1552 | arr_last_handle_set(dl, bus_name, dev_name); | |
1553 | dl->arr_last.port_index = port_index; | |
1554 | } | |
1555 | ||
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) | |
1560 | { | |
1561 | return !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index); | |
1562 | } | |
1563 | ||
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) | |
1568 | { | |
1569 | return dl->arr_last.present && | |
1570 | !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index); | |
1571 | } | |
1572 | ||
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, | |
1576 | bool array) | |
1577 | { | |
1578 | static char buf[32]; | |
1579 | char *ifname = NULL; | |
1580 | ||
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); | |
1585 | else | |
1586 | sprintf(buf, "%s", ifname); | |
1587 | ||
1588 | if (dl->json_output) { | |
1589 | if (array) { | |
1590 | if (should_arr_last_port_handle_end(dl, bus_name, | |
1591 | dev_name, | |
1592 | port_index)) | |
1593 | jsonw_end_array(dl->jw); | |
1594 | if (should_arr_last_port_handle_start(dl, bus_name, | |
1595 | dev_name, | |
1596 | port_index)) { | |
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, | |
1601 | port_index); | |
1602 | } else { | |
1603 | jsonw_start_object(dl->jw); | |
1604 | } | |
1605 | } else { | |
1606 | jsonw_name(dl->jw, buf); | |
1607 | jsonw_start_object(dl->jw); | |
1608 | } | |
1609 | } else { | |
1610 | pr_out("%s:", buf); | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | static void pr_out_port_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice) | |
e6d7367d JP |
1615 | { |
1616 | const char *bus_name; | |
1617 | const char *dev_name; | |
1618 | uint32_t port_index; | |
1619 | ||
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]); | |
e3d0f0c0 JP |
1623 | __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, false); |
1624 | } | |
1625 | ||
1626 | static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool try_nice) | |
1627 | { | |
1628 | const char *bus_name; | |
1629 | const char *dev_name; | |
1630 | uint32_t port_index; | |
e6d7367d | 1631 | |
e3d0f0c0 JP |
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); | |
e6d7367d JP |
1636 | } |
1637 | ||
e3d0f0c0 | 1638 | static void pr_out_port_handle_end(struct dl *dl) |
a3c4b484 | 1639 | { |
e3d0f0c0 JP |
1640 | if (dl->json_output) |
1641 | jsonw_end_object(dl->jw); | |
1642 | else | |
1643 | pr_out("\n"); | |
1644 | } | |
1645 | ||
1646 | ||
1647 | static void pr_out_str(struct dl *dl, const char *name, const char *val) | |
1648 | { | |
153c1a9b | 1649 | if (dl->json_output) { |
e3d0f0c0 | 1650 | jsonw_string_field(dl->jw, name, val); |
153c1a9b AS |
1651 | } else { |
1652 | if (g_indent_newline) | |
1653 | pr_out("%s %s", name, val); | |
1654 | else | |
1655 | pr_out(" %s %s", name, val); | |
1656 | } | |
e3d0f0c0 JP |
1657 | } |
1658 | ||
13925ae9 MS |
1659 | static void pr_out_bool(struct dl *dl, const char *name, bool val) |
1660 | { | |
1661 | if (val) | |
1662 | pr_out_str(dl, name, "true"); | |
1663 | else | |
1664 | pr_out_str(dl, name, "false"); | |
1665 | } | |
1666 | ||
e3d0f0c0 JP |
1667 | static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) |
1668 | { | |
153c1a9b | 1669 | if (dl->json_output) { |
e3d0f0c0 | 1670 | jsonw_uint_field(dl->jw, name, val); |
153c1a9b AS |
1671 | } else { |
1672 | if (g_indent_newline) | |
1673 | pr_out("%s %u", name, val); | |
1674 | else | |
1675 | pr_out(" %s %u", name, val); | |
1676 | } | |
e3d0f0c0 JP |
1677 | } |
1678 | ||
43eb8728 DA |
1679 | static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) |
1680 | { | |
1681 | if (val == (uint64_t) -1) | |
1682 | return pr_out_str(dl, name, "unlimited"); | |
1683 | ||
1684 | return pr_out_uint(dl, name, val); | |
1685 | } | |
1686 | ||
8b4fbf0b AV |
1687 | static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr) |
1688 | { | |
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); | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | static void pr_out_region_chunk_end(struct dl *dl) | |
1698 | { | |
1699 | if (dl->json_output) | |
1700 | jsonw_end_array(dl->jw); | |
1701 | } | |
1702 | ||
1703 | static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len, | |
1704 | uint64_t addr) | |
1705 | { | |
1706 | static uint64_t align_val; | |
1707 | uint32_t i = 0; | |
1708 | ||
1709 | pr_out_region_chunk_start(dl, addr); | |
1710 | while (i < len) { | |
1711 | if (!dl->json_output) | |
1712 | if (!(align_val % 16)) | |
1713 | pr_out("%s%016"PRIx64" ", | |
1714 | align_val ? "\n" : "", | |
1715 | addr); | |
1716 | ||
1717 | align_val++; | |
1718 | ||
1719 | if (dl->json_output) | |
1720 | jsonw_printf(dl->jw, "%d", data[i]); | |
1721 | else | |
1722 | pr_out("%02x ", data[i]); | |
1723 | ||
1724 | addr++; | |
1725 | i++; | |
1726 | } | |
1727 | pr_out_region_chunk_end(dl); | |
1728 | } | |
1729 | ||
e3d0f0c0 JP |
1730 | static void pr_out_dev(struct dl *dl, struct nlattr **tb) |
1731 | { | |
1732 | pr_out_handle(dl, tb); | |
1733 | } | |
1734 | ||
1735 | static void pr_out_section_start(struct dl *dl, const char *name) | |
1736 | { | |
1737 | if (dl->json_output) { | |
1738 | jsonw_start_object(dl->jw); | |
1739 | jsonw_name(dl->jw, name); | |
1740 | jsonw_start_object(dl->jw); | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | static void pr_out_section_end(struct dl *dl) | |
1745 | { | |
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); | |
1751 | } | |
a3c4b484 JP |
1752 | } |
1753 | ||
153c1a9b AS |
1754 | static void pr_out_array_start(struct dl *dl, const char *name) |
1755 | { | |
1756 | if (dl->json_output) { | |
1757 | jsonw_name(dl->jw, name); | |
1758 | jsonw_start_array(dl->jw); | |
1759 | } else { | |
844646a5 | 1760 | __pr_out_indent_inc(); |
153c1a9b | 1761 | __pr_out_newline(); |
844646a5 | 1762 | pr_out("%s:", name); |
153c1a9b | 1763 | __pr_out_indent_inc(); |
844646a5 | 1764 | __pr_out_newline(); |
153c1a9b AS |
1765 | } |
1766 | } | |
1767 | ||
1768 | static void pr_out_array_end(struct dl *dl) | |
1769 | { | |
844646a5 | 1770 | if (dl->json_output) { |
153c1a9b | 1771 | jsonw_end_array(dl->jw); |
844646a5 AS |
1772 | } else { |
1773 | __pr_out_indent_dec(); | |
153c1a9b | 1774 | __pr_out_indent_dec(); |
844646a5 | 1775 | } |
153c1a9b AS |
1776 | } |
1777 | ||
1778 | static void pr_out_entry_start(struct dl *dl) | |
1779 | { | |
1780 | if (dl->json_output) | |
1781 | jsonw_start_object(dl->jw); | |
1782 | } | |
1783 | ||
1784 | static void pr_out_entry_end(struct dl *dl) | |
1785 | { | |
1786 | if (dl->json_output) | |
1787 | jsonw_end_object(dl->jw); | |
1788 | else | |
1789 | __pr_out_newline(); | |
1790 | } | |
1791 | ||
13925ae9 MS |
1792 | static const char *param_cmode_name(uint8_t cmode) |
1793 | { | |
1794 | switch (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>"; | |
1802 | } | |
1803 | } | |
1804 | ||
f57856fa OG |
1805 | static const char *eswitch_mode_name(uint32_t mode) |
1806 | { | |
1807 | switch (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>"; | |
1811 | } | |
1812 | } | |
1813 | ||
6566ca8c RD |
1814 | static const char *eswitch_inline_mode_name(uint32_t mode) |
1815 | { | |
1816 | switch (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; | |
1825 | default: | |
1826 | return "<unknown mode>"; | |
1827 | } | |
1828 | } | |
1829 | ||
f57856fa OG |
1830 | static void pr_out_eswitch(struct dl *dl, struct nlattr **tb) |
1831 | { | |
1832 | __pr_out_handle_start(dl, tb, true, false); | |
1833 | ||
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]))); | |
6566ca8c RD |
1837 | |
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]))); | |
1842 | ||
d315b706 RD |
1843 | if (tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { |
1844 | bool encap_mode = !!mnl_attr_get_u8(tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); | |
1845 | ||
1846 | pr_out_str(dl, "encap", encap_mode ? "enable" : "disable"); | |
1847 | } | |
1848 | ||
f57856fa OG |
1849 | pr_out_handle_end(dl); |
1850 | } | |
1851 | ||
1852 | static int cmd_dev_eswitch_show_cb(const struct nlmsghdr *nlh, void *data) | |
1853 | { | |
1854 | struct dl *dl = data; | |
1855 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
1856 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1857 | ||
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); | |
1862 | return MNL_CB_OK; | |
1863 | } | |
1864 | ||
1865 | static int cmd_dev_eswitch_show(struct dl *dl) | |
1866 | { | |
1867 | struct nlmsghdr *nlh; | |
1868 | int err; | |
1869 | ||
cdd2f7cc | 1870 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_GET, |
f57856fa OG |
1871 | NLM_F_REQUEST | NLM_F_ACK); |
1872 | ||
1873 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
1874 | if (err) | |
1875 | return err; | |
1876 | ||
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); | |
1880 | return err; | |
1881 | } | |
1882 | ||
1883 | static int cmd_dev_eswitch_set(struct dl *dl) | |
1884 | { | |
1885 | struct nlmsghdr *nlh; | |
1886 | int err; | |
1887 | ||
cdd2f7cc | 1888 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_SET, |
f57856fa OG |
1889 | NLM_F_REQUEST | NLM_F_ACK); |
1890 | ||
6566ca8c RD |
1891 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, |
1892 | DL_OPT_ESWITCH_MODE | | |
d315b706 RD |
1893 | DL_OPT_ESWITCH_INLINE_MODE | |
1894 | DL_OPT_ESWITCH_ENCAP_MODE); | |
6566ca8c | 1895 | |
f57856fa OG |
1896 | if (err) |
1897 | return err; | |
1898 | ||
6566ca8c RD |
1899 | if (dl->opts.present == 1) { |
1900 | pr_err("Need to set at least one option\n"); | |
1901 | return -ENOENT; | |
1902 | } | |
1903 | ||
f57856fa OG |
1904 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); |
1905 | } | |
1906 | ||
1907 | static int cmd_dev_eswitch(struct dl *dl) | |
1908 | { | |
a93b6bb3 RD |
1909 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { |
1910 | cmd_dev_help(); | |
1911 | return 0; | |
1912 | } else if (dl_argv_match(dl, "set")) { | |
f57856fa OG |
1913 | dl_arg_inc(dl); |
1914 | return cmd_dev_eswitch_set(dl); | |
1915 | } else if (dl_argv_match(dl, "show")) { | |
1916 | dl_arg_inc(dl); | |
1917 | return cmd_dev_eswitch_show(dl); | |
1918 | } | |
1919 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1920 | return -ENOENT; | |
1921 | } | |
1922 | ||
2557dca2 ST |
1923 | struct param_val_conv { |
1924 | const char *name; | |
1925 | const char *vstr; | |
1926 | uint32_t vuint; | |
1927 | }; | |
1928 | ||
1929 | static bool param_val_conv_exists(const struct param_val_conv *param_val_conv, | |
1930 | uint32_t len, const char *name) | |
1931 | { | |
1932 | uint32_t i; | |
1933 | ||
1934 | for (i = 0; i < len; i++) | |
1935 | if (!strcmp(param_val_conv[i].name, name)) | |
1936 | return true; | |
1937 | ||
1938 | return false; | |
1939 | } | |
1940 | ||
1941 | static int | |
1942 | param_val_conv_uint_get(const struct param_val_conv *param_val_conv, | |
1943 | uint32_t len, const char *name, const char *vstr, | |
1944 | uint32_t *vuint) | |
1945 | { | |
1946 | uint32_t i; | |
1947 | ||
1948 | for (i = 0; i < len; i++) | |
1949 | if (!strcmp(param_val_conv[i].name, name) && | |
1950 | !strcmp(param_val_conv[i].vstr, vstr)) { | |
1951 | *vuint = param_val_conv[i].vuint; | |
1952 | return 0; | |
1953 | } | |
1954 | ||
1955 | return -ENOENT; | |
1956 | } | |
1957 | ||
1958 | static int | |
1959 | param_val_conv_str_get(const struct param_val_conv *param_val_conv, | |
1960 | uint32_t len, const char *name, uint32_t vuint, | |
1961 | const char **vstr) | |
1962 | { | |
1963 | uint32_t i; | |
1964 | ||
1965 | for (i = 0; i < len; i++) | |
1966 | if (!strcmp(param_val_conv[i].name, name) && | |
1967 | param_val_conv[i].vuint == vuint) { | |
1968 | *vstr = param_val_conv[i].vstr; | |
1969 | return 0; | |
1970 | } | |
1971 | ||
1972 | return -ENOENT; | |
1973 | } | |
1974 | ||
a463fd4f ST |
1975 | static const struct param_val_conv param_val_conv[] = { |
1976 | { | |
1977 | .name = "fw_load_policy", | |
1978 | .vstr = "driver", | |
1979 | .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER, | |
1980 | }, | |
1981 | { | |
1982 | .name = "fw_load_policy", | |
1983 | .vstr = "flash", | |
1984 | .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH, | |
1985 | }, | |
1986 | }; | |
2557dca2 ST |
1987 | |
1988 | #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv) | |
1989 | ||
1990 | static void pr_out_param_value(struct dl *dl, const char *nla_name, | |
1991 | int nla_type, struct nlattr *nl) | |
13925ae9 MS |
1992 | { |
1993 | struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {}; | |
1994 | struct nlattr *val_attr; | |
2557dca2 ST |
1995 | const char *vstr; |
1996 | bool conv_exists; | |
13925ae9 MS |
1997 | int err; |
1998 | ||
1999 | err = mnl_attr_parse_nested(nl, attr_cb, nla_value); | |
2000 | if (err != MNL_CB_OK) | |
2001 | return; | |
2002 | ||
2003 | if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] || | |
2004 | (nla_type != MNL_TYPE_FLAG && | |
2005 | !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA])) | |
2006 | return; | |
2007 | ||
2008 | pr_out_str(dl, "cmode", | |
2009 | param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]))); | |
2010 | val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]; | |
2011 | ||
2557dca2 ST |
2012 | conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN, |
2013 | nla_name); | |
2014 | ||
13925ae9 MS |
2015 | switch (nla_type) { |
2016 | case MNL_TYPE_U8: | |
2557dca2 ST |
2017 | if (conv_exists) { |
2018 | err = param_val_conv_str_get(param_val_conv, | |
2019 | PARAM_VAL_CONV_LEN, | |
2020 | nla_name, | |
2021 | mnl_attr_get_u8(val_attr), | |
2022 | &vstr); | |
2023 | if (err) | |
2024 | return; | |
2025 | pr_out_str(dl, "value", vstr); | |
2026 | } else { | |
2027 | pr_out_uint(dl, "value", mnl_attr_get_u8(val_attr)); | |
2028 | } | |
13925ae9 MS |
2029 | break; |
2030 | case MNL_TYPE_U16: | |
2557dca2 ST |
2031 | if (conv_exists) { |
2032 | err = param_val_conv_str_get(param_val_conv, | |
2033 | PARAM_VAL_CONV_LEN, | |
2034 | nla_name, | |
2035 | mnl_attr_get_u16(val_attr), | |
2036 | &vstr); | |
2037 | if (err) | |
2038 | return; | |
2039 | pr_out_str(dl, "value", vstr); | |
2040 | } else { | |
2041 | pr_out_uint(dl, "value", mnl_attr_get_u16(val_attr)); | |
2042 | } | |
13925ae9 MS |
2043 | break; |
2044 | case MNL_TYPE_U32: | |
2557dca2 ST |
2045 | if (conv_exists) { |
2046 | err = param_val_conv_str_get(param_val_conv, | |
2047 | PARAM_VAL_CONV_LEN, | |
2048 | nla_name, | |
2049 | mnl_attr_get_u32(val_attr), | |
2050 | &vstr); | |
2051 | if (err) | |
2052 | return; | |
2053 | pr_out_str(dl, "value", vstr); | |
2054 | } else { | |
2055 | pr_out_uint(dl, "value", mnl_attr_get_u32(val_attr)); | |
2056 | } | |
13925ae9 MS |
2057 | break; |
2058 | case MNL_TYPE_STRING: | |
2059 | pr_out_str(dl, "value", mnl_attr_get_str(val_attr)); | |
2060 | break; | |
2061 | case MNL_TYPE_FLAG: | |
2062 | pr_out_bool(dl, "value", val_attr ? true : false); | |
2063 | break; | |
2064 | } | |
2065 | } | |
2066 | ||
2067 | static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array) | |
2068 | { | |
2069 | struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {}; | |
2070 | struct nlattr *param_value_attr; | |
2557dca2 | 2071 | const char *nla_name; |
13925ae9 MS |
2072 | int nla_type; |
2073 | int err; | |
2074 | ||
2075 | err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param); | |
2076 | if (err != MNL_CB_OK) | |
2077 | return; | |
2078 | if (!nla_param[DEVLINK_ATTR_PARAM_NAME] || | |
2079 | !nla_param[DEVLINK_ATTR_PARAM_TYPE] || | |
2080 | !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) | |
2081 | return; | |
2082 | ||
2083 | if (array) | |
2084 | pr_out_handle_start_arr(dl, tb); | |
2085 | else | |
2086 | __pr_out_handle_start(dl, tb, true, false); | |
2087 | ||
2088 | nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]); | |
2089 | ||
2557dca2 ST |
2090 | nla_name = mnl_attr_get_str(nla_param[DEVLINK_ATTR_PARAM_NAME]); |
2091 | pr_out_str(dl, "name", nla_name); | |
13925ae9 MS |
2092 | |
2093 | if (!nla_param[DEVLINK_ATTR_PARAM_GENERIC]) | |
2094 | pr_out_str(dl, "type", "driver-specific"); | |
2095 | else | |
2096 | pr_out_str(dl, "type", "generic"); | |
2097 | ||
2098 | pr_out_array_start(dl, "values"); | |
2099 | mnl_attr_for_each_nested(param_value_attr, | |
2100 | nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) { | |
2101 | pr_out_entry_start(dl); | |
2557dca2 | 2102 | pr_out_param_value(dl, nla_name, nla_type, param_value_attr); |
13925ae9 MS |
2103 | pr_out_entry_end(dl); |
2104 | } | |
2105 | pr_out_array_end(dl); | |
2106 | pr_out_handle_end(dl); | |
2107 | } | |
2108 | ||
2109 | static int cmd_dev_param_show_cb(const struct nlmsghdr *nlh, void *data) | |
2110 | { | |
2111 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2112 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2113 | struct dl *dl = data; | |
2114 | ||
2115 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2116 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2117 | !tb[DEVLINK_ATTR_PARAM]) | |
2118 | return MNL_CB_ERROR; | |
2119 | pr_out_param(dl, tb, true); | |
2120 | return MNL_CB_OK; | |
2121 | } | |
2122 | ||
2123 | struct param_ctx { | |
2124 | struct dl *dl; | |
2125 | int nla_type; | |
2126 | union { | |
2127 | uint8_t vu8; | |
2128 | uint16_t vu16; | |
2129 | uint32_t vu32; | |
2130 | const char *vstr; | |
2131 | bool vbool; | |
2132 | } value; | |
2133 | }; | |
2134 | ||
2135 | static int cmd_dev_param_set_cb(const struct nlmsghdr *nlh, void *data) | |
2136 | { | |
2137 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2138 | struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {}; | |
2139 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2140 | struct nlattr *param_value_attr; | |
2141 | enum devlink_param_cmode cmode; | |
2142 | struct param_ctx *ctx = data; | |
2143 | struct dl *dl = ctx->dl; | |
2144 | int nla_type; | |
2145 | int err; | |
2146 | ||
2147 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2148 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2149 | !tb[DEVLINK_ATTR_PARAM]) | |
2150 | return MNL_CB_ERROR; | |
2151 | ||
2152 | err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param); | |
2153 | if (err != MNL_CB_OK) | |
2154 | return MNL_CB_ERROR; | |
2155 | ||
2156 | if (!nla_param[DEVLINK_ATTR_PARAM_TYPE] || | |
2157 | !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) | |
2158 | return MNL_CB_ERROR; | |
2159 | ||
2160 | nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]); | |
2161 | mnl_attr_for_each_nested(param_value_attr, | |
2162 | nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) { | |
2163 | struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {}; | |
2164 | struct nlattr *val_attr; | |
2165 | ||
2166 | err = mnl_attr_parse_nested(param_value_attr, | |
2167 | attr_cb, nla_value); | |
2168 | if (err != MNL_CB_OK) | |
2169 | return MNL_CB_ERROR; | |
2170 | ||
2171 | if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] || | |
2172 | (nla_type != MNL_TYPE_FLAG && | |
2173 | !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA])) | |
2174 | return MNL_CB_ERROR; | |
2175 | ||
2176 | cmode = mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]); | |
2177 | if (cmode == dl->opts.cmode) { | |
2178 | val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]; | |
2179 | switch (nla_type) { | |
2180 | case MNL_TYPE_U8: | |
2181 | ctx->value.vu8 = mnl_attr_get_u8(val_attr); | |
2182 | break; | |
2183 | case MNL_TYPE_U16: | |
2184 | ctx->value.vu16 = mnl_attr_get_u16(val_attr); | |
2185 | break; | |
2186 | case MNL_TYPE_U32: | |
2187 | ctx->value.vu32 = mnl_attr_get_u32(val_attr); | |
2188 | break; | |
2189 | case MNL_TYPE_STRING: | |
2190 | ctx->value.vstr = mnl_attr_get_str(val_attr); | |
2191 | break; | |
2192 | case MNL_TYPE_FLAG: | |
2193 | ctx->value.vbool = val_attr ? true : false; | |
2194 | break; | |
2195 | } | |
2196 | break; | |
2197 | } | |
2198 | } | |
2199 | ctx->nla_type = nla_type; | |
2200 | return MNL_CB_OK; | |
2201 | } | |
2202 | ||
2203 | static int cmd_dev_param_set(struct dl *dl) | |
2204 | { | |
2205 | struct param_ctx ctx = {}; | |
2206 | struct nlmsghdr *nlh; | |
2557dca2 | 2207 | bool conv_exists; |
13925ae9 MS |
2208 | uint32_t val_u32; |
2209 | uint16_t val_u16; | |
2210 | uint8_t val_u8; | |
2211 | bool val_bool; | |
2212 | int err; | |
2213 | ||
2214 | err = dl_argv_parse(dl, DL_OPT_HANDLE | | |
2215 | DL_OPT_PARAM_NAME | | |
2216 | DL_OPT_PARAM_VALUE | | |
2217 | DL_OPT_PARAM_CMODE, 0); | |
2218 | if (err) | |
2219 | return err; | |
2220 | ||
2221 | /* Get value type */ | |
2222 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET, | |
2223 | NLM_F_REQUEST | NLM_F_ACK); | |
2224 | dl_opts_put(nlh, dl); | |
2225 | ||
2226 | ctx.dl = dl; | |
2227 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_set_cb, &ctx); | |
2228 | if (err) | |
2229 | return err; | |
2230 | ||
2231 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_SET, | |
2232 | NLM_F_REQUEST | NLM_F_ACK); | |
2233 | dl_opts_put(nlh, dl); | |
2234 | ||
2557dca2 ST |
2235 | conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN, |
2236 | dl->opts.param_name); | |
2237 | ||
13925ae9 MS |
2238 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_TYPE, ctx.nla_type); |
2239 | switch (ctx.nla_type) { | |
2240 | case MNL_TYPE_U8: | |
2557dca2 ST |
2241 | if (conv_exists) { |
2242 | err = param_val_conv_uint_get(param_val_conv, | |
2243 | PARAM_VAL_CONV_LEN, | |
2244 | dl->opts.param_name, | |
2245 | dl->opts.param_value, | |
2246 | &val_u32); | |
2247 | val_u8 = val_u32; | |
2248 | } else { | |
2249 | err = strtouint8_t(dl->opts.param_value, &val_u8); | |
2250 | } | |
13925ae9 MS |
2251 | if (err) |
2252 | goto err_param_value_parse; | |
2253 | if (val_u8 == ctx.value.vu8) | |
2254 | return 0; | |
2255 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u8); | |
2256 | break; | |
2257 | case MNL_TYPE_U16: | |
2557dca2 ST |
2258 | if (conv_exists) { |
2259 | err = param_val_conv_uint_get(param_val_conv, | |
2260 | PARAM_VAL_CONV_LEN, | |
2261 | dl->opts.param_name, | |
2262 | dl->opts.param_value, | |
2263 | &val_u32); | |
2264 | val_u16 = val_u32; | |
2265 | } else { | |
2266 | err = strtouint16_t(dl->opts.param_value, &val_u16); | |
2267 | } | |
13925ae9 MS |
2268 | if (err) |
2269 | goto err_param_value_parse; | |
2270 | if (val_u16 == ctx.value.vu16) | |
2271 | return 0; | |
2272 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u16); | |
2273 | break; | |
2274 | case MNL_TYPE_U32: | |
2557dca2 ST |
2275 | if (conv_exists) |
2276 | err = param_val_conv_uint_get(param_val_conv, | |
2277 | PARAM_VAL_CONV_LEN, | |
2278 | dl->opts.param_name, | |
2279 | dl->opts.param_value, | |
2280 | &val_u32); | |
2281 | else | |
2282 | err = strtouint32_t(dl->opts.param_value, &val_u32); | |
13925ae9 MS |
2283 | if (err) |
2284 | goto err_param_value_parse; | |
2285 | if (val_u32 == ctx.value.vu32) | |
2286 | return 0; | |
2287 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32); | |
2288 | break; | |
2289 | case MNL_TYPE_FLAG: | |
2290 | err = strtobool(dl->opts.param_value, &val_bool); | |
2291 | if (err) | |
2292 | goto err_param_value_parse; | |
2293 | if (val_bool == ctx.value.vbool) | |
2294 | return 0; | |
2295 | if (val_bool) | |
2296 | mnl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, | |
2297 | 0, NULL); | |
2298 | break; | |
2299 | case MNL_TYPE_STRING: | |
2300 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, | |
2301 | dl->opts.param_value); | |
2302 | if (!strcmp(dl->opts.param_value, ctx.value.vstr)) | |
2303 | return 0; | |
2304 | break; | |
2305 | default: | |
2306 | printf("Value type not supported\n"); | |
2307 | return -ENOTSUP; | |
2308 | } | |
2309 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2310 | ||
2311 | err_param_value_parse: | |
2312 | pr_err("Value \"%s\" is not a number or not within range\n", | |
2313 | dl->opts.param_value); | |
2314 | return err; | |
2315 | } | |
2316 | ||
2317 | static int cmd_dev_param_show(struct dl *dl) | |
2318 | { | |
2319 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2320 | struct nlmsghdr *nlh; | |
2321 | int err; | |
2322 | ||
2323 | if (dl_argc(dl) == 0) | |
2324 | flags |= NLM_F_DUMP; | |
2325 | ||
2326 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET, flags); | |
2327 | ||
2328 | if (dl_argc(dl) > 0) { | |
2329 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | | |
2330 | DL_OPT_PARAM_NAME, 0); | |
2331 | if (err) | |
2332 | return err; | |
2333 | } | |
2334 | ||
2335 | pr_out_section_start(dl, "param"); | |
2336 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_show_cb, dl); | |
2337 | pr_out_section_end(dl); | |
2338 | return err; | |
2339 | } | |
2340 | ||
2341 | static int cmd_dev_param(struct dl *dl) | |
2342 | { | |
2343 | if (dl_argv_match(dl, "help")) { | |
2344 | cmd_dev_help(); | |
2345 | return 0; | |
2346 | } else if (dl_argv_match(dl, "show") || | |
2347 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2348 | dl_arg_inc(dl); | |
2349 | return cmd_dev_param_show(dl); | |
2350 | } else if (dl_argv_match(dl, "set")) { | |
2351 | dl_arg_inc(dl); | |
2352 | return cmd_dev_param_set(dl); | |
2353 | } | |
2354 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2355 | return -ENOENT; | |
2356 | } | |
a3c4b484 JP |
2357 | static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data) |
2358 | { | |
e3d0f0c0 | 2359 | struct dl *dl = data; |
a3c4b484 JP |
2360 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
2361 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2362 | ||
2363 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2364 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) | |
2365 | return MNL_CB_ERROR; | |
e3d0f0c0 | 2366 | pr_out_dev(dl, tb); |
a3c4b484 JP |
2367 | return MNL_CB_OK; |
2368 | } | |
2369 | ||
2370 | static int cmd_dev_show(struct dl *dl) | |
2371 | { | |
2372 | struct nlmsghdr *nlh; | |
2373 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2374 | int err; | |
2375 | ||
2376 | if (dl_argc(dl) == 0) | |
2377 | flags |= NLM_F_DUMP; | |
2378 | ||
2379 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_GET, flags); | |
2380 | ||
2381 | if (dl_argc(dl) > 0) { | |
2382 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
2383 | if (err) | |
2384 | return err; | |
2385 | } | |
2386 | ||
e3d0f0c0 JP |
2387 | pr_out_section_start(dl, "dev"); |
2388 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_show_cb, dl); | |
2389 | pr_out_section_end(dl); | |
2390 | return err; | |
a3c4b484 JP |
2391 | } |
2392 | ||
06dd94f9 AS |
2393 | static void cmd_dev_reload_help(void) |
2394 | { | |
2395 | pr_err("Usage: devlink dev reload [ DEV ]\n"); | |
2396 | } | |
2397 | ||
2398 | static int cmd_dev_reload(struct dl *dl) | |
2399 | { | |
2400 | struct nlmsghdr *nlh; | |
2401 | int err; | |
2402 | ||
2403 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
2404 | cmd_dev_reload_help(); | |
2405 | return 0; | |
2406 | } | |
2407 | ||
2408 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RELOAD, | |
2409 | NLM_F_REQUEST | NLM_F_ACK); | |
2410 | ||
2411 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
2412 | if (err) | |
2413 | return err; | |
2414 | ||
2415 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2416 | } | |
2417 | ||
a3c4b484 JP |
2418 | static int cmd_dev(struct dl *dl) |
2419 | { | |
2420 | if (dl_argv_match(dl, "help")) { | |
2421 | cmd_dev_help(); | |
2422 | return 0; | |
2423 | } else if (dl_argv_match(dl, "show") || | |
2424 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2425 | dl_arg_inc(dl); | |
2426 | return cmd_dev_show(dl); | |
f57856fa OG |
2427 | } else if (dl_argv_match(dl, "eswitch")) { |
2428 | dl_arg_inc(dl); | |
2429 | return cmd_dev_eswitch(dl); | |
06dd94f9 AS |
2430 | } else if (dl_argv_match(dl, "reload")) { |
2431 | dl_arg_inc(dl); | |
2432 | return cmd_dev_reload(dl); | |
13925ae9 MS |
2433 | } else if (dl_argv_match(dl, "param")) { |
2434 | dl_arg_inc(dl); | |
2435 | return cmd_dev_param(dl); | |
a3c4b484 JP |
2436 | } |
2437 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2438 | return -ENOENT; | |
2439 | } | |
2440 | ||
2441 | static void cmd_port_help(void) | |
2442 | { | |
7a9466db JP |
2443 | pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n"); |
2444 | pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n"); | |
2445 | pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n"); | |
2446 | pr_err(" devlink port unsplit DEV/PORT_INDEX\n"); | |
a3c4b484 JP |
2447 | } |
2448 | ||
2449 | static const char *port_type_name(uint32_t type) | |
2450 | { | |
2451 | switch (type) { | |
2452 | case DEVLINK_PORT_TYPE_NOTSET: return "notset"; | |
2453 | case DEVLINK_PORT_TYPE_AUTO: return "auto"; | |
2454 | case DEVLINK_PORT_TYPE_ETH: return "eth"; | |
2455 | case DEVLINK_PORT_TYPE_IB: return "ib"; | |
2456 | default: return "<unknown type>"; | |
2457 | } | |
2458 | } | |
2459 | ||
852ed605 JP |
2460 | static const char *port_flavour_name(uint16_t flavour) |
2461 | { | |
2462 | switch (flavour) { | |
2463 | case DEVLINK_PORT_FLAVOUR_PHYSICAL: | |
2464 | return "physical"; | |
2465 | case DEVLINK_PORT_FLAVOUR_CPU: | |
2466 | return "cpu"; | |
2467 | case DEVLINK_PORT_FLAVOUR_DSA: | |
2468 | return "dsa"; | |
2469 | default: | |
2470 | return "<unknown flavour>"; | |
2471 | } | |
2472 | } | |
2473 | ||
e3d0f0c0 | 2474 | static void pr_out_port(struct dl *dl, struct nlattr **tb) |
a3c4b484 JP |
2475 | { |
2476 | struct nlattr *pt_attr = tb[DEVLINK_ATTR_PORT_TYPE]; | |
2477 | struct nlattr *dpt_attr = tb[DEVLINK_ATTR_PORT_DESIRED_TYPE]; | |
2478 | ||
e3d0f0c0 | 2479 | pr_out_port_handle_start(dl, tb, false); |
a3c4b484 JP |
2480 | if (pt_attr) { |
2481 | uint16_t port_type = mnl_attr_get_u16(pt_attr); | |
2482 | ||
e3d0f0c0 | 2483 | pr_out_str(dl, "type", port_type_name(port_type)); |
a3c4b484 JP |
2484 | if (dpt_attr) { |
2485 | uint16_t des_port_type = mnl_attr_get_u16(dpt_attr); | |
2486 | ||
2487 | if (port_type != des_port_type) | |
e3d0f0c0 JP |
2488 | pr_out_str(dl, "des_type", |
2489 | port_type_name(des_port_type)); | |
a3c4b484 JP |
2490 | } |
2491 | } | |
2492 | if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) | |
e3d0f0c0 JP |
2493 | pr_out_str(dl, "netdev", |
2494 | mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME])); | |
a3c4b484 | 2495 | if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) |
e3d0f0c0 JP |
2496 | pr_out_str(dl, "ibdev", |
2497 | mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME])); | |
852ed605 JP |
2498 | if (tb[DEVLINK_ATTR_PORT_FLAVOUR]) { |
2499 | uint16_t port_flavour = | |
2500 | mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_FLAVOUR]); | |
2501 | ||
2502 | pr_out_str(dl, "flavour", port_flavour_name(port_flavour)); | |
2503 | } | |
a3c4b484 | 2504 | if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]) |
e3d0f0c0 JP |
2505 | pr_out_uint(dl, "split_group", |
2506 | mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])); | |
2507 | pr_out_port_handle_end(dl); | |
a3c4b484 JP |
2508 | } |
2509 | ||
2510 | static int cmd_port_show_cb(const struct nlmsghdr *nlh, void *data) | |
2511 | { | |
e3d0f0c0 | 2512 | struct dl *dl = data; |
a3c4b484 JP |
2513 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
2514 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2515 | ||
2516 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2517 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2518 | !tb[DEVLINK_ATTR_PORT_INDEX]) | |
2519 | return MNL_CB_ERROR; | |
e3d0f0c0 | 2520 | pr_out_port(dl, tb); |
a3c4b484 JP |
2521 | return MNL_CB_OK; |
2522 | } | |
2523 | ||
2524 | static int cmd_port_show(struct dl *dl) | |
2525 | { | |
2526 | struct nlmsghdr *nlh; | |
2527 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2528 | int err; | |
2529 | ||
2530 | if (dl_argc(dl) == 0) | |
2531 | flags |= NLM_F_DUMP; | |
2532 | ||
2533 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, flags); | |
2534 | ||
2535 | if (dl_argc(dl) > 0) { | |
2536 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0); | |
2537 | if (err) | |
2538 | return err; | |
2539 | } | |
2540 | ||
e3d0f0c0 JP |
2541 | pr_out_section_start(dl, "port"); |
2542 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_port_show_cb, dl); | |
2543 | pr_out_section_end(dl); | |
2544 | return err; | |
a3c4b484 JP |
2545 | } |
2546 | ||
2547 | static int cmd_port_set(struct dl *dl) | |
2548 | { | |
2549 | struct nlmsghdr *nlh; | |
2550 | int err; | |
2551 | ||
2552 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SET, | |
2553 | NLM_F_REQUEST | NLM_F_ACK); | |
2554 | ||
2555 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_TYPE, 0); | |
2556 | if (err) | |
2557 | return err; | |
2558 | ||
2559 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2560 | } | |
2561 | ||
2562 | static int cmd_port_split(struct dl *dl) | |
2563 | { | |
2564 | struct nlmsghdr *nlh; | |
2565 | int err; | |
2566 | ||
2567 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SPLIT, | |
2568 | NLM_F_REQUEST | NLM_F_ACK); | |
2569 | ||
2570 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_COUNT, 0); | |
2571 | if (err) | |
2572 | return err; | |
2573 | ||
2574 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2575 | } | |
2576 | ||
2577 | static int cmd_port_unsplit(struct dl *dl) | |
2578 | { | |
2579 | struct nlmsghdr *nlh; | |
2580 | int err; | |
2581 | ||
2582 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_UNSPLIT, | |
2583 | NLM_F_REQUEST | NLM_F_ACK); | |
2584 | ||
2585 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0); | |
2586 | if (err) | |
2587 | return err; | |
2588 | ||
2589 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2590 | } | |
2591 | ||
2592 | static int cmd_port(struct dl *dl) | |
2593 | { | |
2594 | if (dl_argv_match(dl, "help")) { | |
2595 | cmd_port_help(); | |
2596 | return 0; | |
2597 | } else if (dl_argv_match(dl, "show") || | |
2598 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2599 | dl_arg_inc(dl); | |
2600 | return cmd_port_show(dl); | |
2601 | } else if (dl_argv_match(dl, "set")) { | |
2602 | dl_arg_inc(dl); | |
2603 | return cmd_port_set(dl); | |
2604 | } else if (dl_argv_match(dl, "split")) { | |
2605 | dl_arg_inc(dl); | |
2606 | return cmd_port_split(dl); | |
2607 | } else if (dl_argv_match(dl, "unsplit")) { | |
2608 | dl_arg_inc(dl); | |
2609 | return cmd_port_unsplit(dl); | |
2610 | } | |
2611 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2612 | return -ENOENT; | |
2613 | } | |
2614 | ||
e6d7367d JP |
2615 | static void cmd_sb_help(void) |
2616 | { | |
7a9466db JP |
2617 | pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n"); |
2618 | pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n"); | |
2619 | pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n"); | |
2620 | pr_err(" size POOL_SIZE thtype { static | dynamic }\n"); | |
2621 | pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n"); | |
2622 | pr_err(" pool POOL_INDEX ]\n"); | |
2623 | pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n"); | |
2624 | pr_err(" pool POOL_INDEX th THRESHOLD\n"); | |
2625 | pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n"); | |
2626 | pr_err(" type { ingress | egress } ]\n"); | |
2627 | pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n"); | |
2628 | pr_err(" type { ingress | egress } pool POOL_INDEX\n"); | |
2629 | pr_err(" th THRESHOLD\n"); | |
2630 | pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n"); | |
2631 | pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n"); | |
2632 | pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n"); | |
e6d7367d JP |
2633 | } |
2634 | ||
e3d0f0c0 | 2635 | static void pr_out_sb(struct dl *dl, struct nlattr **tb) |
e6d7367d | 2636 | { |
e3d0f0c0 JP |
2637 | pr_out_handle_start_arr(dl, tb); |
2638 | pr_out_uint(dl, "sb", | |
2639 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
2640 | pr_out_uint(dl, "size", | |
2641 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE])); | |
2642 | pr_out_uint(dl, "ing_pools", | |
2643 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT])); | |
2644 | pr_out_uint(dl, "eg_pools", | |
2645 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT])); | |
2646 | pr_out_uint(dl, "ing_tcs", | |
2647 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT])); | |
2648 | pr_out_uint(dl, "eg_tcs", | |
2649 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])); | |
2650 | pr_out_handle_end(dl); | |
e6d7367d JP |
2651 | } |
2652 | ||
2653 | static int cmd_sb_show_cb(const struct nlmsghdr *nlh, void *data) | |
2654 | { | |
e3d0f0c0 | 2655 | struct dl *dl = data; |
e6d7367d JP |
2656 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
2657 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2658 | ||
2659 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2660 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2661 | !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_SIZE] || | |
2662 | !tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] || | |
2663 | !tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] || | |
2664 | !tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT] || | |
2665 | !tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT]) | |
2666 | return MNL_CB_ERROR; | |
e3d0f0c0 | 2667 | pr_out_sb(dl, tb); |
e6d7367d JP |
2668 | return MNL_CB_OK; |
2669 | } | |
2670 | ||
2671 | static int cmd_sb_show(struct dl *dl) | |
2672 | { | |
2673 | struct nlmsghdr *nlh; | |
2674 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2675 | int err; | |
2676 | ||
2677 | if (dl_argc(dl) == 0) | |
2678 | flags |= NLM_F_DUMP; | |
2679 | ||
2680 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_GET, flags); | |
2681 | ||
2682 | if (dl_argc(dl) > 0) { | |
2683 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
2684 | if (err) | |
2685 | return err; | |
2686 | } | |
2687 | ||
e3d0f0c0 JP |
2688 | pr_out_section_start(dl, "sb"); |
2689 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_show_cb, dl); | |
2690 | pr_out_section_end(dl); | |
2691 | return err; | |
e6d7367d JP |
2692 | } |
2693 | ||
2694 | static const char *pool_type_name(uint8_t type) | |
2695 | { | |
2696 | switch (type) { | |
2697 | case DEVLINK_SB_POOL_TYPE_INGRESS: return "ingress"; | |
2698 | case DEVLINK_SB_POOL_TYPE_EGRESS: return "egress"; | |
2699 | default: return "<unknown type>"; | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | static const char *threshold_type_name(uint8_t type) | |
2704 | { | |
2705 | switch (type) { | |
2706 | case DEVLINK_SB_THRESHOLD_TYPE_STATIC: return "static"; | |
2707 | case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC: return "dynamic"; | |
2708 | default: return "<unknown type>"; | |
2709 | } | |
2710 | } | |
2711 | ||
e3d0f0c0 | 2712 | static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb) |
e6d7367d | 2713 | { |
e3d0f0c0 JP |
2714 | pr_out_handle_start_arr(dl, tb); |
2715 | pr_out_uint(dl, "sb", | |
2716 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
2717 | pr_out_uint(dl, "pool", | |
2718 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
2719 | pr_out_str(dl, "type", | |
2720 | pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); | |
2721 | pr_out_uint(dl, "size", | |
2722 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE])); | |
2723 | pr_out_str(dl, "thtype", | |
2724 | threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]))); | |
2725 | pr_out_handle_end(dl); | |
e6d7367d JP |
2726 | } |
2727 | ||
2728 | static int cmd_sb_pool_show_cb(const struct nlmsghdr *nlh, void *data) | |
2729 | { | |
e3d0f0c0 | 2730 | struct dl *dl = data; |
e6d7367d JP |
2731 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
2732 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2733 | ||
2734 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2735 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2736 | !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
2737 | !tb[DEVLINK_ATTR_SB_POOL_TYPE] || !tb[DEVLINK_ATTR_SB_POOL_SIZE] || | |
2738 | !tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) | |
2739 | return MNL_CB_ERROR; | |
e3d0f0c0 | 2740 | pr_out_sb_pool(dl, tb); |
e6d7367d JP |
2741 | return MNL_CB_OK; |
2742 | } | |
2743 | ||
2744 | static int cmd_sb_pool_show(struct dl *dl) | |
2745 | { | |
2746 | struct nlmsghdr *nlh; | |
2747 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2748 | int err; | |
2749 | ||
2750 | if (dl_argc(dl) == 0) | |
2751 | flags |= NLM_F_DUMP; | |
2752 | ||
2753 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_GET, flags); | |
2754 | ||
2755 | if (dl_argc(dl) > 0) { | |
2756 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL, | |
2757 | DL_OPT_SB); | |
2758 | if (err) | |
2759 | return err; | |
2760 | } | |
2761 | ||
e3d0f0c0 JP |
2762 | pr_out_section_start(dl, "pool"); |
2763 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_pool_show_cb, dl); | |
2764 | pr_out_section_end(dl); | |
2765 | return err; | |
e6d7367d JP |
2766 | } |
2767 | ||
2768 | static int cmd_sb_pool_set(struct dl *dl) | |
2769 | { | |
2770 | struct nlmsghdr *nlh; | |
2771 | int err; | |
2772 | ||
2773 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_SET, | |
2774 | NLM_F_REQUEST | NLM_F_ACK); | |
2775 | ||
2776 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL | | |
2777 | DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, DL_OPT_SB); | |
2778 | if (err) | |
2779 | return err; | |
2780 | ||
2781 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2782 | } | |
2783 | ||
2784 | static int cmd_sb_pool(struct dl *dl) | |
2785 | { | |
2786 | if (dl_argv_match(dl, "help")) { | |
2787 | cmd_sb_help(); | |
2788 | return 0; | |
2789 | } else if (dl_argv_match(dl, "show") || | |
2790 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2791 | dl_arg_inc(dl); | |
2792 | return cmd_sb_pool_show(dl); | |
2793 | } else if (dl_argv_match(dl, "set")) { | |
2794 | dl_arg_inc(dl); | |
2795 | return cmd_sb_pool_set(dl); | |
2796 | } | |
2797 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2798 | return -ENOENT; | |
2799 | } | |
2800 | ||
2801 | static void pr_out_sb_port_pool(struct dl *dl, struct nlattr **tb) | |
2802 | { | |
e3d0f0c0 JP |
2803 | pr_out_port_handle_start_arr(dl, tb, true); |
2804 | pr_out_uint(dl, "sb", | |
2805 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
2806 | pr_out_uint(dl, "pool", | |
2807 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
2808 | pr_out_uint(dl, "threshold", | |
2809 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); | |
2810 | pr_out_port_handle_end(dl); | |
e6d7367d JP |
2811 | } |
2812 | ||
2813 | static int cmd_sb_port_pool_show_cb(const struct nlmsghdr *nlh, void *data) | |
2814 | { | |
2815 | struct dl *dl = data; | |
2816 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2817 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2818 | ||
2819 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2820 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2821 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
2822 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD]) | |
2823 | return MNL_CB_ERROR; | |
2824 | pr_out_sb_port_pool(dl, tb); | |
2825 | return MNL_CB_OK; | |
2826 | } | |
2827 | ||
2828 | static int cmd_sb_port_pool_show(struct dl *dl) | |
2829 | { | |
2830 | struct nlmsghdr *nlh; | |
2831 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2832 | int err; | |
2833 | ||
2834 | if (dl_argc(dl) == 0) | |
2835 | flags |= NLM_F_DUMP; | |
2836 | ||
2837 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags); | |
2838 | ||
2839 | if (dl_argc(dl) > 0) { | |
2840 | err = dl_argv_parse_put(nlh, dl, | |
2841 | DL_OPT_HANDLEP | DL_OPT_SB_POOL, | |
2842 | DL_OPT_SB); | |
2843 | if (err) | |
2844 | return err; | |
2845 | } | |
2846 | ||
e3d0f0c0 JP |
2847 | pr_out_section_start(dl, "port_pool"); |
2848 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_port_pool_show_cb, dl); | |
2849 | pr_out_section_end(dl); | |
2850 | return 0; | |
e6d7367d JP |
2851 | } |
2852 | ||
2853 | static int cmd_sb_port_pool_set(struct dl *dl) | |
2854 | { | |
2855 | struct nlmsghdr *nlh; | |
2856 | int err; | |
2857 | ||
2858 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_SET, | |
2859 | NLM_F_REQUEST | NLM_F_ACK); | |
2860 | ||
2861 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL | | |
2862 | DL_OPT_SB_TH, DL_OPT_SB); | |
2863 | if (err) | |
2864 | return err; | |
2865 | ||
2866 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2867 | } | |
2868 | ||
2869 | static int cmd_sb_port_pool(struct dl *dl) | |
2870 | { | |
2871 | if (dl_argv_match(dl, "help")) { | |
2872 | cmd_sb_help(); | |
2873 | return 0; | |
2874 | } else if (dl_argv_match(dl, "show") || | |
2875 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2876 | dl_arg_inc(dl); | |
2877 | return cmd_sb_port_pool_show(dl); | |
2878 | } else if (dl_argv_match(dl, "set")) { | |
2879 | dl_arg_inc(dl); | |
2880 | return cmd_sb_port_pool_set(dl); | |
2881 | } | |
2882 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2883 | return -ENOENT; | |
2884 | } | |
2885 | ||
2886 | static int cmd_sb_port(struct dl *dl) | |
2887 | { | |
2888 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
2889 | cmd_sb_help(); | |
2890 | return 0; | |
2891 | } else if (dl_argv_match(dl, "pool")) { | |
2892 | dl_arg_inc(dl); | |
2893 | return cmd_sb_port_pool(dl); | |
2894 | } | |
2895 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2896 | return -ENOENT; | |
2897 | } | |
2898 | ||
2899 | static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb) | |
2900 | { | |
e3d0f0c0 JP |
2901 | pr_out_port_handle_start_arr(dl, tb, true); |
2902 | pr_out_uint(dl, "sb", | |
2903 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
2904 | pr_out_uint(dl, "tc", | |
2905 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX])); | |
2906 | pr_out_str(dl, "type", | |
2907 | pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); | |
2908 | pr_out_uint(dl, "pool", | |
2909 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
2910 | pr_out_uint(dl, "threshold", | |
e6d7367d | 2911 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); |
e3d0f0c0 | 2912 | pr_out_port_handle_end(dl); |
e6d7367d JP |
2913 | } |
2914 | ||
2915 | static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr *nlh, void *data) | |
2916 | { | |
2917 | struct dl *dl = data; | |
2918 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2919 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2920 | ||
2921 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2922 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2923 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
2924 | !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] || | |
2925 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD]) | |
2926 | return MNL_CB_ERROR; | |
2927 | pr_out_sb_tc_bind(dl, tb); | |
2928 | return MNL_CB_OK; | |
2929 | } | |
2930 | ||
2931 | static int cmd_sb_tc_bind_show(struct dl *dl) | |
2932 | { | |
2933 | struct nlmsghdr *nlh; | |
2934 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
2935 | int err; | |
2936 | ||
2937 | if (dl_argc(dl) == 0) | |
2938 | flags |= NLM_F_DUMP; | |
2939 | ||
2940 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags); | |
2941 | ||
2942 | if (dl_argc(dl) > 0) { | |
2943 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | | |
2944 | DL_OPT_SB_TYPE, DL_OPT_SB); | |
2945 | if (err) | |
2946 | return err; | |
2947 | } | |
2948 | ||
e3d0f0c0 JP |
2949 | pr_out_section_start(dl, "tc_bind"); |
2950 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_tc_bind_show_cb, dl); | |
2951 | pr_out_section_end(dl); | |
2952 | return err; | |
e6d7367d JP |
2953 | } |
2954 | ||
2955 | static int cmd_sb_tc_bind_set(struct dl *dl) | |
2956 | { | |
2957 | struct nlmsghdr *nlh; | |
2958 | int err; | |
2959 | ||
2960 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_SET, | |
2961 | NLM_F_REQUEST | NLM_F_ACK); | |
2962 | ||
2963 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | | |
2964 | DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH, | |
2965 | DL_OPT_SB); | |
2966 | if (err) | |
2967 | return err; | |
2968 | ||
2969 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2970 | } | |
2971 | ||
2972 | static int cmd_sb_tc_bind(struct dl *dl) | |
2973 | { | |
2974 | if (dl_argv_match(dl, "help")) { | |
2975 | cmd_sb_help(); | |
2976 | return 0; | |
2977 | } else if (dl_argv_match(dl, "show") || | |
2978 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2979 | dl_arg_inc(dl); | |
2980 | return cmd_sb_tc_bind_show(dl); | |
2981 | } else if (dl_argv_match(dl, "set")) { | |
2982 | dl_arg_inc(dl); | |
2983 | return cmd_sb_tc_bind_set(dl); | |
2984 | } | |
2985 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2986 | return -ENOENT; | |
2987 | } | |
2988 | ||
2989 | static int cmd_sb_tc(struct dl *dl) | |
2990 | { | |
2991 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
2992 | cmd_sb_help(); | |
2993 | return 0; | |
2994 | } else if (dl_argv_match(dl, "bind")) { | |
2995 | dl_arg_inc(dl); | |
2996 | return cmd_sb_tc_bind(dl); | |
2997 | } | |
2998 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2999 | return -ENOENT; | |
3000 | } | |
3001 | ||
25ec49be JP |
3002 | struct occ_item { |
3003 | struct list_head list; | |
3004 | uint32_t index; | |
3005 | uint32_t cur; | |
3006 | uint32_t max; | |
3007 | uint32_t bound_pool_index; | |
3008 | }; | |
3009 | ||
3010 | struct occ_port { | |
3011 | struct list_head list; | |
3012 | char *bus_name; | |
3013 | char *dev_name; | |
3014 | uint32_t port_index; | |
3015 | uint32_t sb_index; | |
3016 | struct list_head pool_list; | |
3017 | struct list_head ing_tc_list; | |
3018 | struct list_head eg_tc_list; | |
3019 | }; | |
3020 | ||
3021 | struct occ_show { | |
3022 | struct dl *dl; | |
3023 | int err; | |
3024 | struct list_head port_list; | |
3025 | }; | |
3026 | ||
3027 | static struct occ_item *occ_item_alloc(void) | |
3028 | { | |
3029 | return calloc(1, sizeof(struct occ_item)); | |
3030 | } | |
3031 | ||
3032 | static void occ_item_free(struct occ_item *occ_item) | |
3033 | { | |
3034 | free(occ_item); | |
3035 | } | |
3036 | ||
3037 | static struct occ_port *occ_port_alloc(uint32_t port_index) | |
3038 | { | |
3039 | struct occ_port *occ_port; | |
3040 | ||
3041 | occ_port = calloc(1, sizeof(*occ_port)); | |
3042 | if (!occ_port) | |
3043 | return NULL; | |
3044 | occ_port->port_index = port_index; | |
3045 | INIT_LIST_HEAD(&occ_port->pool_list); | |
3046 | INIT_LIST_HEAD(&occ_port->ing_tc_list); | |
3047 | INIT_LIST_HEAD(&occ_port->eg_tc_list); | |
3048 | return occ_port; | |
3049 | } | |
3050 | ||
3051 | static void occ_port_free(struct occ_port *occ_port) | |
3052 | { | |
3053 | struct occ_item *occ_item, *tmp; | |
3054 | ||
3055 | list_for_each_entry_safe(occ_item, tmp, &occ_port->pool_list, list) | |
3056 | occ_item_free(occ_item); | |
3057 | list_for_each_entry_safe(occ_item, tmp, &occ_port->ing_tc_list, list) | |
3058 | occ_item_free(occ_item); | |
3059 | list_for_each_entry_safe(occ_item, tmp, &occ_port->eg_tc_list, list) | |
3060 | occ_item_free(occ_item); | |
3061 | } | |
3062 | ||
3063 | static struct occ_show *occ_show_alloc(struct dl *dl) | |
3064 | { | |
3065 | struct occ_show *occ_show; | |
3066 | ||
3067 | occ_show = calloc(1, sizeof(*occ_show)); | |
3068 | if (!occ_show) | |
3069 | return NULL; | |
3070 | occ_show->dl = dl; | |
3071 | INIT_LIST_HEAD(&occ_show->port_list); | |
3072 | return occ_show; | |
3073 | } | |
3074 | ||
3075 | static void occ_show_free(struct occ_show *occ_show) | |
3076 | { | |
3077 | struct occ_port *occ_port, *tmp; | |
3078 | ||
3079 | list_for_each_entry_safe(occ_port, tmp, &occ_show->port_list, list) | |
3080 | occ_port_free(occ_port); | |
3081 | } | |
3082 | ||
3083 | static struct occ_port *occ_port_get(struct occ_show *occ_show, | |
3084 | struct nlattr **tb) | |
3085 | { | |
3086 | struct occ_port *occ_port; | |
3087 | uint32_t port_index; | |
3088 | ||
3089 | port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]); | |
3090 | ||
3091 | list_for_each_entry_reverse(occ_port, &occ_show->port_list, list) { | |
3092 | if (occ_port->port_index == port_index) | |
3093 | return occ_port; | |
3094 | } | |
3095 | occ_port = occ_port_alloc(port_index); | |
3096 | if (!occ_port) | |
3097 | return NULL; | |
3098 | list_add_tail(&occ_port->list, &occ_show->port_list); | |
3099 | return occ_port; | |
3100 | } | |
3101 | ||
3102 | static void pr_out_occ_show_item_list(const char *label, struct list_head *list, | |
3103 | bool bound_pool) | |
3104 | { | |
3105 | struct occ_item *occ_item; | |
3106 | int i = 1; | |
3107 | ||
3108 | pr_out_sp(7, " %s:", label); | |
3109 | list_for_each_entry(occ_item, list, list) { | |
3110 | if ((i - 1) % 4 == 0 && i != 1) | |
3111 | pr_out_sp(7, " "); | |
3112 | if (bound_pool) | |
3113 | pr_out_sp(7, "%2u(%u):", occ_item->index, | |
3114 | occ_item->bound_pool_index); | |
3115 | else | |
3116 | pr_out_sp(7, "%2u:", occ_item->index); | |
3117 | pr_out_sp(15, "%7u/%u", occ_item->cur, occ_item->max); | |
3118 | if (i++ % 4 == 0) | |
3119 | pr_out("\n"); | |
3120 | } | |
3121 | if ((i - 1) % 4 != 0) | |
3122 | pr_out("\n"); | |
3123 | } | |
3124 | ||
e3d0f0c0 JP |
3125 | static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label, |
3126 | struct list_head *list, | |
3127 | bool bound_pool) | |
3128 | { | |
3129 | struct occ_item *occ_item; | |
3130 | char buf[32]; | |
3131 | ||
3132 | jsonw_name(dl->jw, label); | |
3133 | jsonw_start_object(dl->jw); | |
3134 | list_for_each_entry(occ_item, list, list) { | |
3135 | sprintf(buf, "%u", occ_item->index); | |
3136 | jsonw_name(dl->jw, buf); | |
3137 | jsonw_start_object(dl->jw); | |
3138 | if (bound_pool) | |
3139 | jsonw_uint_field(dl->jw, "bound_pool", | |
3140 | occ_item->bound_pool_index); | |
3141 | jsonw_uint_field(dl->jw, "current", occ_item->cur); | |
3142 | jsonw_uint_field(dl->jw, "max", occ_item->max); | |
3143 | jsonw_end_object(dl->jw); | |
3144 | } | |
3145 | jsonw_end_object(dl->jw); | |
3146 | } | |
3147 | ||
3148 | static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port) | |
25ec49be | 3149 | { |
e3d0f0c0 JP |
3150 | if (dl->json_output) { |
3151 | pr_out_json_occ_show_item_list(dl, "pool", | |
3152 | &occ_port->pool_list, false); | |
3153 | pr_out_json_occ_show_item_list(dl, "itc", | |
3154 | &occ_port->ing_tc_list, true); | |
3155 | pr_out_json_occ_show_item_list(dl, "etc", | |
3156 | &occ_port->eg_tc_list, true); | |
3157 | } else { | |
3158 | pr_out("\n"); | |
3159 | pr_out_occ_show_item_list("pool", &occ_port->pool_list, false); | |
3160 | pr_out_occ_show_item_list("itc", &occ_port->ing_tc_list, true); | |
3161 | pr_out_occ_show_item_list("etc", &occ_port->eg_tc_list, true); | |
3162 | } | |
25ec49be JP |
3163 | } |
3164 | ||
3165 | static void pr_out_occ_show(struct occ_show *occ_show) | |
3166 | { | |
3167 | struct dl *dl = occ_show->dl; | |
3168 | struct dl_opts *opts = &dl->opts; | |
3169 | struct occ_port *occ_port; | |
3170 | ||
3171 | list_for_each_entry(occ_port, &occ_show->port_list, list) { | |
e3d0f0c0 JP |
3172 | __pr_out_port_handle_start(dl, opts->bus_name, opts->dev_name, |
3173 | occ_port->port_index, true, false); | |
3174 | pr_out_occ_show_port(dl, occ_port); | |
3175 | pr_out_port_handle_end(dl); | |
25ec49be JP |
3176 | } |
3177 | } | |
3178 | ||
3179 | static void cmd_sb_occ_port_pool_process(struct occ_show *occ_show, | |
3180 | struct nlattr **tb) | |
3181 | { | |
3182 | struct occ_port *occ_port; | |
3183 | struct occ_item *occ_item; | |
3184 | ||
3185 | if (occ_show->err || !dl_dump_filter(occ_show->dl, tb)) | |
3186 | return; | |
3187 | ||
3188 | occ_port = occ_port_get(occ_show, tb); | |
3189 | if (!occ_port) { | |
3190 | occ_show->err = -ENOMEM; | |
3191 | return; | |
3192 | } | |
3193 | ||
3194 | occ_item = occ_item_alloc(); | |
3195 | if (!occ_item) { | |
3196 | occ_show->err = -ENOMEM; | |
3197 | return; | |
3198 | } | |
3199 | occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]); | |
3200 | occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]); | |
3201 | occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]); | |
3202 | list_add_tail(&occ_item->list, &occ_port->pool_list); | |
3203 | } | |
3204 | ||
3205 | static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr *nlh, void *data) | |
3206 | { | |
3207 | struct occ_show *occ_show = data; | |
3208 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
3209 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
3210 | ||
3211 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3212 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
3213 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
3214 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
3215 | !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX]) | |
3216 | return MNL_CB_ERROR; | |
3217 | cmd_sb_occ_port_pool_process(occ_show, tb); | |
3218 | return MNL_CB_OK; | |
3219 | } | |
3220 | ||
3221 | static void cmd_sb_occ_tc_pool_process(struct occ_show *occ_show, | |
3222 | struct nlattr **tb) | |
3223 | { | |
3224 | struct occ_port *occ_port; | |
3225 | struct occ_item *occ_item; | |
3226 | uint8_t pool_type; | |
3227 | ||
3228 | if (occ_show->err || !dl_dump_filter(occ_show->dl, tb)) | |
3229 | return; | |
3230 | ||
3231 | occ_port = occ_port_get(occ_show, tb); | |
3232 | if (!occ_port) { | |
3233 | occ_show->err = -ENOMEM; | |
3234 | return; | |
3235 | } | |
3236 | ||
3237 | occ_item = occ_item_alloc(); | |
3238 | if (!occ_item) { | |
3239 | occ_show->err = -ENOMEM; | |
3240 | return; | |
3241 | } | |
3242 | occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]); | |
3243 | occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]); | |
3244 | occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]); | |
3245 | occ_item->bound_pool_index = | |
3246 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]); | |
3247 | pool_type = mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]); | |
3248 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS) | |
3249 | list_add_tail(&occ_item->list, &occ_port->ing_tc_list); | |
3250 | else if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS) | |
3251 | list_add_tail(&occ_item->list, &occ_port->eg_tc_list); | |
3252 | else | |
3253 | occ_item_free(occ_item); | |
3254 | } | |
3255 | ||
3256 | static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr *nlh, void *data) | |
3257 | { | |
3258 | struct occ_show *occ_show = data; | |
3259 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
3260 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
3261 | ||
3262 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3263 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
3264 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
3265 | !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] || | |
3266 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
3267 | !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX]) | |
3268 | return MNL_CB_ERROR; | |
3269 | cmd_sb_occ_tc_pool_process(occ_show, tb); | |
3270 | return MNL_CB_OK; | |
3271 | } | |
3272 | ||
3273 | static int cmd_sb_occ_show(struct dl *dl) | |
3274 | { | |
3275 | struct nlmsghdr *nlh; | |
3276 | struct occ_show *occ_show; | |
3277 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP; | |
3278 | int err; | |
3279 | ||
3280 | err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB); | |
3281 | if (err) | |
3282 | return err; | |
3283 | ||
3284 | occ_show = occ_show_alloc(dl); | |
3285 | if (!occ_show) | |
3286 | return -ENOMEM; | |
3287 | ||
3288 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags); | |
3289 | ||
3290 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, | |
3291 | cmd_sb_occ_port_pool_process_cb, occ_show); | |
3292 | if (err) | |
3293 | goto out; | |
3294 | ||
3295 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags); | |
3296 | ||
3297 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, | |
3298 | cmd_sb_occ_tc_pool_process_cb, occ_show); | |
3299 | if (err) | |
3300 | goto out; | |
3301 | ||
e3d0f0c0 | 3302 | pr_out_section_start(dl, "occupancy"); |
25ec49be | 3303 | pr_out_occ_show(occ_show); |
e3d0f0c0 | 3304 | pr_out_section_end(dl); |
25ec49be JP |
3305 | |
3306 | out: | |
3307 | occ_show_free(occ_show); | |
3308 | return err; | |
3309 | } | |
3310 | ||
3311 | static int cmd_sb_occ_snapshot(struct dl *dl) | |
3312 | { | |
3313 | struct nlmsghdr *nlh; | |
3314 | int err; | |
3315 | ||
3316 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_SNAPSHOT, | |
3317 | NLM_F_REQUEST | NLM_F_ACK); | |
3318 | ||
3319 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
3320 | if (err) | |
3321 | return err; | |
3322 | ||
3323 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
3324 | } | |
3325 | ||
3326 | static int cmd_sb_occ_clearmax(struct dl *dl) | |
3327 | { | |
3328 | struct nlmsghdr *nlh; | |
3329 | int err; | |
3330 | ||
3331 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_MAX_CLEAR, | |
3332 | NLM_F_REQUEST | NLM_F_ACK); | |
3333 | ||
3334 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
3335 | if (err) | |
3336 | return err; | |
3337 | ||
3338 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
3339 | } | |
3340 | ||
3341 | static int cmd_sb_occ(struct dl *dl) | |
3342 | { | |
3343 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
3344 | cmd_sb_help(); | |
3345 | return 0; | |
3346 | } else if (dl_argv_match(dl, "show") || | |
3347 | dl_argv_match(dl, "list")) { | |
3348 | dl_arg_inc(dl); | |
3349 | return cmd_sb_occ_show(dl); | |
3350 | } else if (dl_argv_match(dl, "snapshot")) { | |
3351 | dl_arg_inc(dl); | |
3352 | return cmd_sb_occ_snapshot(dl); | |
3353 | } else if (dl_argv_match(dl, "clearmax")) { | |
3354 | dl_arg_inc(dl); | |
3355 | return cmd_sb_occ_clearmax(dl); | |
3356 | } | |
3357 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
3358 | return -ENOENT; | |
3359 | } | |
3360 | ||
e6d7367d JP |
3361 | static int cmd_sb(struct dl *dl) |
3362 | { | |
3363 | if (dl_argv_match(dl, "help")) { | |
3364 | cmd_sb_help(); | |
3365 | return 0; | |
3366 | } else if (dl_argv_match(dl, "show") || | |
3367 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
3368 | dl_arg_inc(dl); | |
3369 | return cmd_sb_show(dl); | |
3370 | } else if (dl_argv_match(dl, "pool")) { | |
3371 | dl_arg_inc(dl); | |
3372 | return cmd_sb_pool(dl); | |
3373 | } else if (dl_argv_match(dl, "port")) { | |
3374 | dl_arg_inc(dl); | |
3375 | return cmd_sb_port(dl); | |
3376 | } else if (dl_argv_match(dl, "tc")) { | |
3377 | dl_arg_inc(dl); | |
3378 | return cmd_sb_tc(dl); | |
25ec49be JP |
3379 | } else if (dl_argv_match(dl, "occupancy")) { |
3380 | dl_arg_inc(dl); | |
3381 | return cmd_sb_occ(dl); | |
e6d7367d JP |
3382 | } |
3383 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
3384 | return -ENOENT; | |
3385 | } | |
3386 | ||
a3c4b484 JP |
3387 | static const char *cmd_name(uint8_t cmd) |
3388 | { | |
3389 | switch (cmd) { | |
3390 | case DEVLINK_CMD_UNSPEC: return "unspec"; | |
3391 | case DEVLINK_CMD_GET: return "get"; | |
3392 | case DEVLINK_CMD_SET: return "set"; | |
3393 | case DEVLINK_CMD_NEW: return "new"; | |
3394 | case DEVLINK_CMD_DEL: return "del"; | |
3395 | case DEVLINK_CMD_PORT_GET: return "get"; | |
3396 | case DEVLINK_CMD_PORT_SET: return "set"; | |
da7a1aa7 | 3397 | case DEVLINK_CMD_PORT_NEW: return "new"; |
a3c4b484 | 3398 | case DEVLINK_CMD_PORT_DEL: return "del"; |
13925ae9 MS |
3399 | case DEVLINK_CMD_PARAM_GET: return "get"; |
3400 | case DEVLINK_CMD_PARAM_SET: return "set"; | |
3401 | case DEVLINK_CMD_PARAM_NEW: return "new"; | |
3402 | case DEVLINK_CMD_PARAM_DEL: return "del"; | |
8b4fbf0b AV |
3403 | case DEVLINK_CMD_REGION_GET: return "get"; |
3404 | case DEVLINK_CMD_REGION_SET: return "set"; | |
3405 | case DEVLINK_CMD_REGION_NEW: return "new"; | |
3406 | case DEVLINK_CMD_REGION_DEL: return "del"; | |
a3c4b484 JP |
3407 | default: return "<unknown cmd>"; |
3408 | } | |
3409 | } | |
3410 | ||
3411 | static const char *cmd_obj(uint8_t cmd) | |
3412 | { | |
3413 | switch (cmd) { | |
3414 | case DEVLINK_CMD_UNSPEC: return "unspec"; | |
3415 | case DEVLINK_CMD_GET: | |
3416 | case DEVLINK_CMD_SET: | |
3417 | case DEVLINK_CMD_NEW: | |
3418 | case DEVLINK_CMD_DEL: | |
3419 | return "dev"; | |
3420 | case DEVLINK_CMD_PORT_GET: | |
3421 | case DEVLINK_CMD_PORT_SET: | |
3422 | case DEVLINK_CMD_PORT_NEW: | |
3423 | case DEVLINK_CMD_PORT_DEL: | |
3424 | return "port"; | |
13925ae9 MS |
3425 | case DEVLINK_CMD_PARAM_GET: |
3426 | case DEVLINK_CMD_PARAM_SET: | |
3427 | case DEVLINK_CMD_PARAM_NEW: | |
3428 | case DEVLINK_CMD_PARAM_DEL: | |
3429 | return "param"; | |
8b4fbf0b AV |
3430 | case DEVLINK_CMD_REGION_GET: |
3431 | case DEVLINK_CMD_REGION_SET: | |
3432 | case DEVLINK_CMD_REGION_NEW: | |
3433 | case DEVLINK_CMD_REGION_DEL: | |
3434 | return "region"; | |
a3c4b484 JP |
3435 | default: return "<unknown obj>"; |
3436 | } | |
3437 | } | |
3438 | ||
3439 | static void pr_out_mon_header(uint8_t cmd) | |
3440 | { | |
3441 | pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd)); | |
3442 | } | |
3443 | ||
3444 | static bool cmd_filter_check(struct dl *dl, uint8_t cmd) | |
3445 | { | |
3446 | const char *obj = cmd_obj(cmd); | |
3447 | unsigned int index = 0; | |
3448 | const char *cur_obj; | |
3449 | ||
3450 | if (dl_no_arg(dl)) | |
3451 | return true; | |
3452 | while ((cur_obj = dl_argv_index(dl, index++))) { | |
3453 | if (strcmp(cur_obj, obj) == 0 || strcmp(cur_obj, "all") == 0) | |
3454 | return true; | |
3455 | } | |
3456 | return false; | |
3457 | } | |
3458 | ||
8b4fbf0b AV |
3459 | static void pr_out_region(struct dl *dl, struct nlattr **tb); |
3460 | ||
a3c4b484 JP |
3461 | static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data) |
3462 | { | |
3463 | struct dl *dl = data; | |
3464 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
3465 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
3466 | uint8_t cmd = genl->cmd; | |
3467 | ||
3468 | if (!cmd_filter_check(dl, cmd)) | |
3469 | return MNL_CB_OK; | |
3470 | ||
3471 | switch (cmd) { | |
3472 | case DEVLINK_CMD_GET: /* fall through */ | |
3473 | case DEVLINK_CMD_SET: /* fall through */ | |
3474 | case DEVLINK_CMD_NEW: /* fall through */ | |
3475 | case DEVLINK_CMD_DEL: | |
3476 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3477 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) | |
3478 | return MNL_CB_ERROR; | |
3479 | pr_out_mon_header(genl->cmd); | |
e3d0f0c0 | 3480 | pr_out_dev(dl, tb); |
a3c4b484 JP |
3481 | break; |
3482 | case DEVLINK_CMD_PORT_GET: /* fall through */ | |
3483 | case DEVLINK_CMD_PORT_SET: /* fall through */ | |
3484 | case DEVLINK_CMD_PORT_NEW: /* fall through */ | |
3485 | case DEVLINK_CMD_PORT_DEL: | |
3486 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3487 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
3488 | !tb[DEVLINK_ATTR_PORT_INDEX]) | |
3489 | return MNL_CB_ERROR; | |
3490 | pr_out_mon_header(genl->cmd); | |
e3d0f0c0 | 3491 | pr_out_port(dl, tb); |
a3c4b484 | 3492 | break; |
13925ae9 MS |
3493 | case DEVLINK_CMD_PARAM_GET: /* fall through */ |
3494 | case DEVLINK_CMD_PARAM_SET: /* fall through */ | |
3495 | case DEVLINK_CMD_PARAM_NEW: /* fall through */ | |
3496 | case DEVLINK_CMD_PARAM_DEL: | |
3497 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3498 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
3499 | !tb[DEVLINK_ATTR_PARAM]) | |
3500 | return MNL_CB_ERROR; | |
3501 | pr_out_mon_header(genl->cmd); | |
3502 | pr_out_param(dl, tb, false); | |
3503 | break; | |
8b4fbf0b AV |
3504 | case DEVLINK_CMD_REGION_GET: /* fall through */ |
3505 | case DEVLINK_CMD_REGION_SET: /* fall through */ | |
3506 | case DEVLINK_CMD_REGION_NEW: /* fall through */ | |
3507 | case DEVLINK_CMD_REGION_DEL: | |
3508 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
3509 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
3510 | !tb[DEVLINK_ATTR_REGION_NAME]) | |
3511 | return MNL_CB_ERROR; | |
3512 | pr_out_mon_header(genl->cmd); | |
3513 | pr_out_region(dl, tb); | |
3514 | break; | |
a3c4b484 JP |
3515 | } |
3516 | return MNL_CB_OK; | |
3517 | } | |
3518 | ||
3519 | static int cmd_mon_show(struct dl *dl) | |
3520 | { | |
3521 | int err; | |
3522 | unsigned int index = 0; | |
3523 | const char *cur_obj; | |
3524 | ||
3525 | while ((cur_obj = dl_argv_index(dl, index++))) { | |
3526 | if (strcmp(cur_obj, "all") != 0 && | |
3527 | strcmp(cur_obj, "dev") != 0 && | |
3528 | strcmp(cur_obj, "port") != 0) { | |
3529 | pr_err("Unknown object \"%s\"\n", cur_obj); | |
3530 | return -EINVAL; | |
3531 | } | |
3532 | } | |
3533 | err = _mnlg_socket_group_add(dl->nlg, DEVLINK_GENL_MCGRP_CONFIG_NAME); | |
3534 | if (err) | |
3535 | return err; | |
3536 | err = _mnlg_socket_recv_run(dl->nlg, cmd_mon_show_cb, dl); | |
3537 | if (err) | |
3538 | return err; | |
3539 | return 0; | |
3540 | } | |
3541 | ||
3542 | static void cmd_mon_help(void) | |
3543 | { | |
7a9466db | 3544 | pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n" |
a3c4b484 JP |
3545 | "where OBJECT-LIST := { dev | port }\n"); |
3546 | } | |
3547 | ||
3548 | static int cmd_mon(struct dl *dl) | |
3549 | { | |
3550 | if (dl_argv_match(dl, "help")) { | |
3551 | cmd_mon_help(); | |
3552 | return 0; | |
3553 | } else if (dl_no_arg(dl)) { | |
3554 | dl_arg_inc(dl); | |
3555 | return cmd_mon_show(dl); | |
3556 | } | |
3557 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
3558 | return -ENOENT; | |
3559 | } | |
3560 | ||
153c1a9b AS |
3561 | struct dpipe_field { |
3562 | char *name; | |
3563 | unsigned int id; | |
3564 | unsigned int bitwidth; | |
3565 | enum devlink_dpipe_field_mapping_type mapping_type; | |
3566 | }; | |
3567 | ||
3568 | struct dpipe_header { | |
3569 | struct list_head list; | |
3570 | char *name; | |
3571 | unsigned int id; | |
3572 | struct dpipe_field *fields; | |
3573 | unsigned int fields_count; | |
3574 | }; | |
3575 | ||
ead18027 AS |
3576 | struct dpipe_table { |
3577 | struct list_head list; | |
3578 | char *name; | |
3579 | unsigned int resource_id; | |
3580 | bool resource_valid; | |
3581 | }; | |
3582 | ||
3583 | struct dpipe_tables { | |
3584 | struct list_head table_list; | |
3585 | }; | |
3586 | ||
8cd64409 AS |
3587 | struct resource { |
3588 | char *name; | |
3589 | uint64_t size; | |
3590 | uint64_t size_new; | |
3591 | uint64_t size_min; | |
3592 | uint64_t size_max; | |
3593 | uint64_t size_gran; | |
3594 | enum devlink_resource_unit unit; | |
3595 | bool size_valid; | |
3596 | uint64_t size_occ; | |
3597 | bool occ_valid; | |
3598 | uint64_t id; | |
3599 | struct list_head list; | |
3600 | struct list_head resource_list; | |
3601 | struct resource *parent; | |
3602 | }; | |
3603 | ||
3604 | struct resources { | |
3605 | struct list_head resource_list; | |
3606 | }; | |
3607 | ||
3608 | struct resource_ctx { | |
3609 | struct dl *dl; | |
3610 | int err; | |
3611 | struct resources *resources; | |
ead18027 | 3612 | struct dpipe_tables *tables; |
8cd64409 AS |
3613 | bool print_resources; |
3614 | bool pending_change; | |
3615 | }; | |
3616 | ||
3617 | static struct resource *resource_alloc(void) | |
3618 | { | |
3619 | struct resource *resource; | |
3620 | ||
3621 | resource = calloc(1, sizeof(struct resource)); | |
3622 | if (!resource) | |
3623 | return NULL; | |
3624 | INIT_LIST_HEAD(&resource->resource_list); | |
3625 | return resource; | |
3626 | } | |
3627 | ||
3628 | static void resource_free(struct resource *resource) | |
3629 | { | |
3630 | struct resource *child_resource, *tmp; | |
3631 | ||
3632 | list_for_each_entry_safe(child_resource, tmp, &resource->resource_list, | |
3633 | list) { | |
3634 | free(child_resource->name); | |
3635 | resource_free(child_resource); | |
3636 | } | |
3637 | free(resource); | |
3638 | } | |
3639 | ||
3640 | static struct resources *resources_alloc(void) | |
3641 | { | |
3642 | struct resources *resources; | |
3643 | ||
3644 | resources = calloc(1, sizeof(struct resources)); | |
3645 | if (!resources) | |
3646 | return NULL; | |
3647 | INIT_LIST_HEAD(&resources->resource_list); | |
3648 | return resources; | |
3649 | } | |
3650 | ||
3651 | static void resources_free(struct resources *resources) | |
3652 | { | |
3653 | struct resource *resource, *tmp; | |
3654 | ||
3655 | list_for_each_entry_safe(resource, tmp, &resources->resource_list, list) | |
3656 | resource_free(resource); | |
3657 | } | |
3658 | ||
3659 | static int resource_ctx_init(struct resource_ctx *ctx, struct dl *dl) | |
3660 | { | |
3661 | ctx->resources = resources_alloc(); | |
3662 | if (!ctx->resources) | |
3663 | return -ENOMEM; | |
3664 | ctx->dl = dl; | |
3665 | return 0; | |
3666 | } | |
3667 | ||
3668 | static void resource_ctx_fini(struct resource_ctx *ctx) | |
3669 | { | |
3670 | resources_free(ctx->resources); | |
3671 | } | |
3672 | ||
153c1a9b AS |
3673 | struct dpipe_ctx { |
3674 | struct dl *dl; | |
3675 | int err; | |
3676 | struct list_head global_headers; | |
3677 | struct list_head local_headers; | |
ead18027 AS |
3678 | struct dpipe_tables *tables; |
3679 | struct resources *resources; | |
153c1a9b | 3680 | bool print_headers; |
ead18027 | 3681 | bool print_tables; |
153c1a9b AS |
3682 | }; |
3683 | ||
3684 | static struct dpipe_header *dpipe_header_alloc(unsigned int fields_count) | |
a3c4b484 | 3685 | { |
153c1a9b AS |
3686 | struct dpipe_header *header; |
3687 | ||
3688 | header = calloc(1, sizeof(struct dpipe_header)); | |
3689 | if (!header) | |
3690 | return NULL; | |
3691 | header->fields = calloc(fields_count, sizeof(struct dpipe_field)); | |
3692 | if (!header->fields) | |
3693 | goto err_fields_alloc; | |
3694 | header->fields_count = fields_count; | |
3695 | return header; | |
3696 | ||
3697 | err_fields_alloc: | |
3698 | free(header); | |
3699 | return NULL; | |
a3c4b484 JP |
3700 | } |
3701 | ||
153c1a9b | 3702 | static void dpipe_header_free(struct dpipe_header *header) |
a3c4b484 | 3703 | { |
153c1a9b AS |
3704 | free(header->fields); |
3705 | free(header); | |
3706 | } | |
3707 | ||
3708 | static void dpipe_header_clear(struct dpipe_header *header) | |
3709 | { | |
3710 | struct dpipe_field *field; | |
3711 | int i; | |
3712 | ||
3713 | for (i = 0; i < header->fields_count; i++) { | |
3714 | field = &header->fields[i]; | |
3715 | free(field->name); | |
a3c4b484 | 3716 | } |
153c1a9b | 3717 | free(header->name); |
a3c4b484 JP |
3718 | } |
3719 | ||
153c1a9b AS |
3720 | static void dpipe_header_add(struct dpipe_ctx *ctx, |
3721 | struct dpipe_header *header, bool global) | |
a3c4b484 | 3722 | { |
153c1a9b AS |
3723 | if (global) |
3724 | list_add(&header->list, &ctx->global_headers); | |
3725 | else | |
3726 | list_add(&header->list, &ctx->local_headers); | |
3727 | } | |
a3c4b484 | 3728 | |
153c1a9b AS |
3729 | static void dpipe_header_del(struct dpipe_header *header) |
3730 | { | |
3731 | list_del(&header->list); | |
3732 | } | |
a3c4b484 | 3733 | |
ead18027 AS |
3734 | static struct dpipe_table *dpipe_table_alloc(void) |
3735 | { | |
3736 | return calloc(1, sizeof(struct dpipe_table)); | |
3737 | } | |
3738 | ||
3739 | static void dpipe_table_free(struct dpipe_table *table) | |
3740 | { | |
3741 | free(table); | |
3742 | } | |
3743 | ||
3744 | static struct dpipe_tables *dpipe_tables_alloc(void) | |
3745 | { | |
3746 | struct dpipe_tables *tables; | |
3747 | ||
3748 | tables = calloc(1, sizeof(struct dpipe_tables)); | |
3749 | if (!tables) | |
3750 | return NULL; | |
3751 | INIT_LIST_HEAD(&tables->table_list); | |
3752 | return tables; | |
3753 | } | |
3754 | ||
3755 | static void dpipe_tables_free(struct dpipe_tables *tables) | |
3756 | { | |
3757 | struct dpipe_table *table, *tmp; | |
3758 | ||
3759 | list_for_each_entry_safe(table, tmp, &tables->table_list, list) | |
3760 | dpipe_table_free(table); | |
3761 | free(tables); | |
3762 | } | |
3763 | ||
06a2cda9 | 3764 | static int dpipe_ctx_init(struct dpipe_ctx *ctx, struct dl *dl) |
153c1a9b | 3765 | { |
ead18027 AS |
3766 | ctx->tables = dpipe_tables_alloc(); |
3767 | if (!ctx->tables) | |
3768 | return -ENOMEM; | |
3769 | ||
153c1a9b AS |
3770 | ctx->dl = dl; |
3771 | INIT_LIST_HEAD(&ctx->global_headers); | |
3772 | INIT_LIST_HEAD(&ctx->local_headers); | |
06a2cda9 | 3773 | return 0; |
153c1a9b AS |
3774 | } |
3775 | ||
06a2cda9 | 3776 | static void dpipe_ctx_fini(struct dpipe_ctx *ctx) |
153c1a9b AS |
3777 | { |
3778 | struct dpipe_header *header, *tmp; | |
3779 | ||
3780 | list_for_each_entry_safe(header, tmp, &ctx->global_headers, | |
3781 | list) { | |
3782 | dpipe_header_del(header); | |
3783 | dpipe_header_clear(header); | |
3784 | dpipe_header_free(header); | |
3785 | } | |
3786 | list_for_each_entry_safe(header, tmp, &ctx->local_headers, | |
3787 | list) { | |
3788 | dpipe_header_del(header); | |
3789 | dpipe_header_clear(header); | |
3790 | dpipe_header_free(header); | |
a3c4b484 | 3791 | } |
ead18027 | 3792 | dpipe_tables_free(ctx->tables); |
153c1a9b | 3793 | } |
a3c4b484 | 3794 | |
153c1a9b AS |
3795 | static const char *dpipe_header_id2s(struct dpipe_ctx *ctx, |
3796 | uint32_t header_id, bool global) | |
3797 | { | |
3798 | struct list_head *header_list; | |
3799 | struct dpipe_header *header; | |
3800 | ||
3801 | if (global) | |
3802 | header_list = &ctx->global_headers; | |
3803 | else | |
3804 | header_list = &ctx->local_headers; | |
3805 | list_for_each_entry(header, header_list, list) { | |
3806 | if (header->id != header_id) | |
3807 | continue; | |
3808 | return header->name; | |
a3c4b484 | 3809 | } |
153c1a9b AS |
3810 | return NULL; |
3811 | } | |
3812 | ||
3813 | static const char *dpipe_field_id2s(struct dpipe_ctx *ctx, | |
3814 | uint32_t header_id, | |
3815 | uint32_t field_id, bool global) | |
3816 | { | |
3817 | struct list_head *header_list; | |
3818 | struct dpipe_header *header; | |
3819 | ||
3820 | if (global) | |
3821 | header_list = &ctx->global_headers; | |
3822 | else | |
3823 | header_list = &ctx->local_headers; | |
3824 | list_for_each_entry(header, header_list, list) { | |
3825 | if (header->id != header_id) | |
3826 | continue; | |
3827 | return header->fields[field_id].name; | |
e3d0f0c0 | 3828 | } |
153c1a9b AS |
3829 | return NULL; |
3830 | } | |
a3c4b484 | 3831 | |
153c1a9b AS |
3832 | static const char * |
3833 | dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type) | |
3834 | { | |
3835 | switch (mapping_type) { | |
3836 | case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE: | |
3837 | return NULL; | |
3838 | case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX: | |
3839 | return "ifindex"; | |
3840 | default: | |
3841 | return "<unknown>"; | |
3842 | } | |
a3c4b484 JP |
3843 | } |
3844 | ||
153c1a9b AS |
3845 | static const char * |
3846 | dpipe_mapping_get(struct dpipe_ctx *ctx, uint32_t header_id, | |
3847 | uint32_t field_id, bool global) | |
a3c4b484 | 3848 | { |
153c1a9b AS |
3849 | enum devlink_dpipe_field_mapping_type mapping_type; |
3850 | struct list_head *header_list; | |
3851 | struct dpipe_header *header; | |
3852 | ||
3853 | if (global) | |
3854 | header_list = &ctx->global_headers; | |
3855 | else | |
3856 | header_list = &ctx->local_headers; | |
3857 | list_for_each_entry(header, header_list, list) { | |
3858 | if (header->id != header_id) | |
3859 | continue; | |
3860 | mapping_type = header->fields[field_id].mapping_type; | |
3861 | return dpipe_field_mapping_e2s(mapping_type); | |
3862 | } | |
3863 | return NULL; | |
a3c4b484 JP |
3864 | } |
3865 | ||
153c1a9b AS |
3866 | static void pr_out_dpipe_fields(struct dpipe_ctx *ctx, |
3867 | struct dpipe_field *fields, | |
3868 | unsigned int field_count) | |
a3c4b484 | 3869 | { |
153c1a9b AS |
3870 | struct dpipe_field *field; |
3871 | int i; | |
a3c4b484 | 3872 | |
153c1a9b AS |
3873 | for (i = 0; i < field_count; i++) { |
3874 | field = &fields[i]; | |
3875 | pr_out_entry_start(ctx->dl); | |
3876 | pr_out_str(ctx->dl, "name", field->name); | |
3877 | if (ctx->dl->verbose) | |
3878 | pr_out_uint(ctx->dl, "id", field->id); | |
3879 | pr_out_uint(ctx->dl, "bitwidth", field->bitwidth); | |
3880 | if (field->mapping_type) | |
3881 | pr_out_str(ctx->dl, "mapping_type", | |
3882 | dpipe_field_mapping_e2s(field->mapping_type)); | |
3883 | pr_out_entry_end(ctx->dl); | |
3884 | } | |
a3c4b484 JP |
3885 | } |
3886 | ||
153c1a9b AS |
3887 | static void |
3888 | pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb, | |
3889 | struct dpipe_header *header, bool global) | |
a3c4b484 | 3890 | { |
153c1a9b AS |
3891 | pr_out_handle_start_arr(ctx->dl, tb); |
3892 | pr_out_str(ctx->dl, "name", header->name); | |
3893 | if (ctx->dl->verbose) { | |
3894 | pr_out_uint(ctx->dl, "id", header->id); | |
3895 | pr_out_str(ctx->dl, "global", | |
3896 | global ? "true" : "false"); | |
3897 | } | |
3898 | pr_out_array_start(ctx->dl, "field"); | |
3899 | pr_out_dpipe_fields(ctx, header->fields, | |
3900 | header->fields_count); | |
3901 | pr_out_array_end(ctx->dl); | |
3902 | pr_out_handle_end(ctx->dl); | |
a3c4b484 JP |
3903 | } |
3904 | ||
153c1a9b AS |
3905 | static void pr_out_dpipe_headers(struct dpipe_ctx *ctx, |
3906 | struct nlattr **tb) | |
a3c4b484 | 3907 | { |
153c1a9b AS |
3908 | struct dpipe_header *header; |
3909 | ||
3910 | list_for_each_entry(header, &ctx->local_headers, list) | |
3911 | pr_out_dpipe_header(ctx, tb, header, false); | |
3912 | ||
3913 | list_for_each_entry(header, &ctx->global_headers, list) | |
3914 | pr_out_dpipe_header(ctx, tb, header, true); | |
3915 | } | |
3916 | ||
3917 | static int dpipe_header_field_get(struct nlattr *nl, struct dpipe_field *field) | |
3918 | { | |
3919 | struct nlattr *nla_field[DEVLINK_ATTR_MAX + 1] = {}; | |
3920 | const char *name; | |
a3c4b484 | 3921 | int err; |
a3c4b484 | 3922 | |
153c1a9b AS |
3923 | err = mnl_attr_parse_nested(nl, attr_cb, nla_field); |
3924 | if (err != MNL_CB_OK) | |
3925 | return -EINVAL; | |
3926 | if (!nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID] || | |
3927 | !nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME] || | |
3928 | !nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] || | |
3929 | !nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE]) | |
3930 | return -EINVAL; | |
3931 | ||
3932 | name = mnl_attr_get_str(nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME]); | |
3933 | field->id = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID]); | |
3934 | field->bitwidth = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH]); | |
3935 | field->name = strdup(name); | |
3936 | if (!field->name) | |
3937 | return -ENOMEM; | |
3938 | field->mapping_type = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE]); | |
3939 | return 0; | |
3940 | } | |
3941 | ||
3942 | static int dpipe_header_fields_get(struct nlattr *nla_fields, | |
3943 | struct dpipe_field *fields) | |
3944 | { | |
3945 | struct nlattr *nla_field; | |
3946 | int count = 0; | |
3947 | int err; | |
3948 | ||
3949 | mnl_attr_for_each_nested(nla_field, nla_fields) { | |
3950 | err = dpipe_header_field_get(nla_field, &fields[count]); | |
3951 | if (err) | |
3952 | return err; | |
3953 | count++; | |
43f35be4 | 3954 | } |
153c1a9b AS |
3955 | return 0; |
3956 | } | |
43f35be4 | 3957 | |
153c1a9b AS |
3958 | static unsigned int dpipe_header_field_count_get(struct nlattr *nla_fields) |
3959 | { | |
3960 | struct nlattr *nla_field; | |
3961 | unsigned int count = 0; | |
a3c4b484 | 3962 | |
153c1a9b AS |
3963 | mnl_attr_for_each_nested(nla_field, nla_fields) |
3964 | count++; | |
3965 | return count; | |
3966 | } | |
3967 | ||
3968 | static int dpipe_header_get(struct dpipe_ctx *ctx, struct nlattr *nl) | |
3969 | { | |
3970 | struct nlattr *nla_header[DEVLINK_ATTR_MAX + 1] = {}; | |
3971 | struct dpipe_header *header; | |
3972 | unsigned int fields_count; | |
3973 | const char *header_name; | |
3974 | bool global; | |
3975 | int err; | |
3976 | ||
3977 | err = mnl_attr_parse_nested(nl, attr_cb, nla_header); | |
3978 | if (err != MNL_CB_OK) | |
3979 | return -EINVAL; | |
3980 | ||
3981 | if (!nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME] || | |
3982 | !nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID] || | |
3983 | !nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS]) | |
3984 | return -EINVAL; | |
3985 | ||
3986 | fields_count = dpipe_header_field_count_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS]); | |
3987 | header = dpipe_header_alloc(fields_count); | |
3988 | if (!header) | |
3989 | return -ENOMEM; | |
3990 | ||
3991 | header_name = mnl_attr_get_str(nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME]); | |
3992 | header->name = strdup(header_name); | |
3993 | header->id = mnl_attr_get_u32(nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID]); | |
3994 | header->fields_count = fields_count; | |
3995 | global = !!mnl_attr_get_u8(nla_header[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]); | |
3996 | ||
3997 | err = dpipe_header_fields_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS], | |
3998 | header->fields); | |
3999 | if (err) | |
4000 | goto err_field_get; | |
4001 | dpipe_header_add(ctx, header, global); | |
4002 | return 0; | |
4003 | ||
4004 | err_field_get: | |
4005 | dpipe_header_free(header); | |
4006 | return err; | |
4007 | } | |
4008 | ||
4009 | static int dpipe_headers_get(struct dpipe_ctx *ctx, struct nlattr **tb) | |
4010 | { | |
4011 | struct nlattr *nla_headers = tb[DEVLINK_ATTR_DPIPE_HEADERS]; | |
4012 | struct nlattr *nla_header; | |
4013 | int err; | |
4014 | ||
4015 | mnl_attr_for_each_nested(nla_header, nla_headers) { | |
4016 | err = dpipe_header_get(ctx, nla_header); | |
4017 | if (err) | |
4018 | return err; | |
4019 | } | |
4020 | return 0; | |
4021 | } | |
4022 | ||
4023 | static int cmd_dpipe_header_cb(const struct nlmsghdr *nlh, void *data) | |
4024 | { | |
4025 | struct dpipe_ctx *ctx = data; | |
4026 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
4027 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
4028 | int err; | |
4029 | ||
4030 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
4031 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
4032 | !tb[DEVLINK_ATTR_DPIPE_HEADERS]) | |
4033 | return MNL_CB_ERROR; | |
4034 | err = dpipe_headers_get(ctx, tb); | |
4035 | if (err) { | |
4036 | ctx->err = err; | |
4037 | return MNL_CB_ERROR; | |
4038 | } | |
4039 | ||
4040 | if (ctx->print_headers) | |
4041 | pr_out_dpipe_headers(ctx, tb); | |
4042 | return MNL_CB_OK; | |
4043 | } | |
4044 | ||
4045 | static int cmd_dpipe_headers_show(struct dl *dl) | |
4046 | { | |
4047 | struct nlmsghdr *nlh; | |
06a2cda9 | 4048 | struct dpipe_ctx ctx = {}; |
153c1a9b AS |
4049 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; |
4050 | int err; | |
4051 | ||
4052 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags); | |
4053 | ||
4054 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
4055 | if (err) | |
4056 | return err; | |
4057 | ||
06a2cda9 AS |
4058 | err = dpipe_ctx_init(&ctx, dl); |
4059 | if (err) | |
4060 | return err; | |
153c1a9b | 4061 | |
06a2cda9 | 4062 | ctx.print_headers = true; |
153c1a9b AS |
4063 | |
4064 | pr_out_section_start(dl, "header"); | |
06a2cda9 | 4065 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx); |
153c1a9b | 4066 | if (err) |
06a2cda9 | 4067 | pr_err("error get headers %s\n", strerror(ctx.err)); |
153c1a9b AS |
4068 | pr_out_section_end(dl); |
4069 | ||
06a2cda9 | 4070 | dpipe_ctx_fini(&ctx); |
153c1a9b AS |
4071 | return err; |
4072 | } | |
4073 | ||
4074 | static void cmd_dpipe_header_help(void) | |
4075 | { | |
4076 | pr_err("Usage: devlink dpipe headers show DEV\n"); | |
4077 | } | |
4078 | ||
4079 | static int cmd_dpipe_header(struct dl *dl) | |
4080 | { | |
4081 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
4082 | cmd_dpipe_header_help(); | |
4083 | return 0; | |
4084 | } else if (dl_argv_match(dl, "show")) { | |
4085 | dl_arg_inc(dl); | |
4086 | return cmd_dpipe_headers_show(dl); | |
4087 | } | |
4088 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
4089 | return -ENOENT; | |
4090 | } | |
4091 | ||
4092 | static const char | |
4093 | *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type) | |
4094 | { | |
4095 | switch (action_type) { | |
4096 | case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY: | |
4097 | return "field_modify"; | |
4098 | default: | |
4099 | return "<unknown>"; | |
4100 | } | |
4101 | } | |
4102 | ||
92b2a5bb AS |
4103 | struct dpipe_op_info { |
4104 | uint32_t header_id; | |
4105 | uint32_t field_id; | |
4106 | bool header_global; | |
4107 | }; | |
4108 | ||
4109 | struct dpipe_action { | |
4110 | struct dpipe_op_info info; | |
4111 | uint32_t type; | |
4112 | }; | |
4113 | ||
4114 | static void pr_out_dpipe_action(struct dpipe_action *action, | |
4115 | struct dpipe_ctx *ctx) | |
153c1a9b | 4116 | { |
92b2a5bb | 4117 | struct dpipe_op_info *op_info = &action->info; |
153c1a9b AS |
4118 | const char *mapping; |
4119 | ||
92b2a5bb AS |
4120 | pr_out_str(ctx->dl, "type", |
4121 | dpipe_action_type_e2s(action->type)); | |
4122 | pr_out_str(ctx->dl, "header", | |
4123 | dpipe_header_id2s(ctx, op_info->header_id, | |
4124 | op_info->header_global)); | |
4125 | pr_out_str(ctx->dl, "field", | |
4126 | dpipe_field_id2s(ctx, op_info->header_id, | |
4127 | op_info->field_id, | |
4128 | op_info->header_global)); | |
4129 | mapping = dpipe_mapping_get(ctx, op_info->header_id, | |
4130 | op_info->field_id, | |
4131 | op_info->header_global); | |
153c1a9b AS |
4132 | if (mapping) |
4133 | pr_out_str(ctx->dl, "mapping", mapping); | |
4134 | } | |
4135 | ||
92b2a5bb | 4136 | static int dpipe_action_parse(struct dpipe_action *action, struct nlattr *nl) |
153c1a9b AS |
4137 | { |
4138 | struct nlattr *nla_action[DEVLINK_ATTR_MAX + 1] = {}; | |
153c1a9b AS |
4139 | int err; |
4140 | ||
4141 | err = mnl_attr_parse_nested(nl, attr_cb, nla_action); | |
4142 | if (err != MNL_CB_OK) | |
4143 | return -EINVAL; | |
4144 | ||
4145 | if (!nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE] || | |
4146 | !nla_action[DEVLINK_ATTR_DPIPE_HEADER_INDEX] || | |
4147 | !nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID] || | |
4148 | !nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]) { | |
4149 | return -EINVAL; | |
4150 | } | |
4151 | ||
92b2a5bb AS |
4152 | action->type = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE]); |
4153 | action->info.header_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID]); | |
4154 | action->info.field_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]); | |
4155 | action->info.header_global = !!mnl_attr_get_u8(nla_action[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]); | |
153c1a9b | 4156 | |
153c1a9b AS |
4157 | return 0; |
4158 | } | |
4159 | ||
4160 | static int dpipe_table_actions_show(struct dpipe_ctx *ctx, | |
4161 | struct nlattr *nla_actions) | |
4162 | { | |
4163 | struct nlattr *nla_action; | |
92b2a5bb | 4164 | struct dpipe_action action; |
153c1a9b AS |
4165 | |
4166 | mnl_attr_for_each_nested(nla_action, nla_actions) { | |
4167 | pr_out_entry_start(ctx->dl); | |
92b2a5bb AS |
4168 | if (dpipe_action_parse(&action, nla_action)) |
4169 | goto err_action_parse; | |
4170 | pr_out_dpipe_action(&action, ctx); | |
153c1a9b AS |
4171 | pr_out_entry_end(ctx->dl); |
4172 | } | |
4173 | return 0; | |
4174 | ||
92b2a5bb | 4175 | err_action_parse: |
153c1a9b AS |
4176 | pr_out_entry_end(ctx->dl); |
4177 | return -EINVAL; | |
4178 | } | |
4179 | ||
4180 | static const char * | |
4181 | dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type) | |
4182 | { | |
4183 | switch (match_type) { | |
4184 | case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT: | |
4185 | return "field_exact"; | |
4186 | default: | |
4187 | return "<unknown>"; | |
4188 | } | |
4189 | } | |
4190 | ||
92b2a5bb AS |
4191 | struct dpipe_match { |
4192 | struct dpipe_op_info info; | |
4193 | uint32_t type; | |
4194 | }; | |
4195 | ||
4196 | static void pr_out_dpipe_match(struct dpipe_match *match, | |
4197 | struct dpipe_ctx *ctx) | |
153c1a9b | 4198 | { |
92b2a5bb | 4199 | struct dpipe_op_info *op_info = &match->info; |
153c1a9b AS |
4200 | const char *mapping; |
4201 | ||
92b2a5bb AS |
4202 | pr_out_str(ctx->dl, "type", |
4203 | dpipe_match_type_e2s(match->type)); | |
4204 | pr_out_str(ctx->dl, "header", | |
4205 | dpipe_header_id2s(ctx, op_info->header_id, | |
4206 | op_info->header_global)); | |
4207 | pr_out_str(ctx->dl, "field", | |
4208 | dpipe_field_id2s(ctx, op_info->header_id, | |
4209 | op_info->field_id, | |
4210 | op_info->header_global)); | |
4211 | mapping = dpipe_mapping_get(ctx, op_info->header_id, | |
4212 | op_info->field_id, | |
4213 | op_info->header_global); | |
153c1a9b AS |
4214 | if (mapping) |
4215 | pr_out_str(ctx->dl, "mapping", mapping); | |
153c1a9b AS |
4216 | } |
4217 | ||
92b2a5bb AS |
4218 | static int dpipe_match_parse(struct dpipe_match *match, |
4219 | struct nlattr *nl) | |
4220 | ||
153c1a9b AS |
4221 | { |
4222 | struct nlattr *nla_match[DEVLINK_ATTR_MAX + 1] = {}; | |
153c1a9b AS |
4223 | int err; |
4224 | ||
4225 | err = mnl_attr_parse_nested(nl, attr_cb, nla_match); | |
4226 | if (err != MNL_CB_OK) | |
4227 | return -EINVAL; | |
4228 | ||
4229 | if (!nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE] || | |
4230 | !nla_match[DEVLINK_ATTR_DPIPE_HEADER_INDEX] || | |
4231 | !nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID] || | |
4232 | !nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]) { | |
4233 | return -EINVAL; | |
4234 | } | |
4235 | ||
92b2a5bb AS |
4236 | match->type = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE]); |
4237 | match->info.header_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID]); | |
4238 | match->info.field_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]); | |
4239 | match->info.header_global = !!mnl_attr_get_u8(nla_match[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]); | |
153c1a9b | 4240 | |
153c1a9b AS |
4241 | return 0; |
4242 | } | |
4243 | ||
4244 | static int dpipe_table_matches_show(struct dpipe_ctx *ctx, | |
4245 | struct nlattr *nla_matches) | |
4246 | { | |
4247 | struct nlattr *nla_match; | |
92b2a5bb | 4248 | struct dpipe_match match; |
153c1a9b AS |
4249 | |
4250 | mnl_attr_for_each_nested(nla_match, nla_matches) { | |
4251 | pr_out_entry_start(ctx->dl); | |
92b2a5bb AS |
4252 | if (dpipe_match_parse(&match, nla_match)) |
4253 | goto err_match_parse; | |
4254 | pr_out_dpipe_match(&match, ctx); | |
153c1a9b AS |
4255 | pr_out_entry_end(ctx->dl); |
4256 | } | |
4257 | return 0; | |
4258 | ||
92b2a5bb | 4259 | err_match_parse: |
153c1a9b AS |
4260 | pr_out_entry_end(ctx->dl); |
4261 | return -EINVAL; | |
4262 | } | |
4263 | ||
8cd64409 AS |
4264 | static struct resource * |
4265 | resource_find(struct resources *resources, struct resource *resource, | |
4266 | uint64_t resource_id) | |
4267 | { | |
4268 | struct list_head *list_head; | |
4269 | ||
4270 | if (!resource) | |
4271 | list_head = &resources->resource_list; | |
4272 | else | |
4273 | list_head = &resource->resource_list; | |
4274 | ||
4275 | list_for_each_entry(resource, list_head, list) { | |
4276 | struct resource *child_resource; | |
4277 | ||
4278 | if (resource->id == resource_id) | |
4279 | return resource; | |
4280 | ||
4281 | child_resource = resource_find(resources, resource, | |
4282 | resource_id); | |
4283 | if (child_resource) | |
4284 | return child_resource; | |
4285 | } | |
4286 | return NULL; | |
4287 | } | |
4288 | ||
4289 | static void | |
4290 | resource_path_print(struct dl *dl, struct resources *resources, | |
4291 | uint64_t resource_id) | |
4292 | { | |
4293 | struct resource *resource, *parent_resource; | |
4294 | const char del[] = "/"; | |
4295 | int path_len = 0; | |
4296 | char *path; | |
4297 | ||
4298 | resource = resource_find(resources, NULL, resource_id); | |
4299 | if (!resource) | |
4300 | return; | |
4301 | ||
4302 | for (parent_resource = resource; parent_resource; | |
4303 | parent_resource = parent_resource->parent) | |
4304 | path_len += strlen(parent_resource->name) + 1; | |
4305 | ||
4306 | path_len++; | |
4307 | path = calloc(1, path_len); | |
4308 | if (!path) | |
4309 | return; | |
4310 | ||
4311 | path += path_len - 1; | |
4312 | for (parent_resource = resource; parent_resource; | |
4313 | parent_resource = parent_resource->parent) { | |
4314 | path -= strlen(parent_resource->name); | |
4315 | memcpy(path, parent_resource->name, | |
4316 | strlen(parent_resource->name)); | |
4317 | path -= strlen(del); | |
4318 | memcpy(path, del, strlen(del)); | |
4319 | } | |
4320 | pr_out_str(dl, "resource_path", path); | |
4321 | free(path); | |
4322 | } | |
4323 | ||
153c1a9b AS |
4324 | static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl) |
4325 | { | |
4326 | struct nlattr *nla_table[DEVLINK_ATTR_MAX + 1] = {}; | |
ead18027 AS |
4327 | struct dpipe_table *table; |
4328 | uint32_t resource_units; | |
153c1a9b | 4329 | bool counters_enabled; |
ead18027 | 4330 | bool resource_valid; |
153c1a9b AS |
4331 | uint32_t size; |
4332 | int err; | |
4333 | ||
4334 | err = mnl_attr_parse_nested(nl, attr_cb, nla_table); | |
4335 | if (err != MNL_CB_OK) | |
4336 | return -EINVAL; | |
4337 | ||
4338 | if (!nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME] || | |
4339 | !nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE] || | |
4340 | !nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] || | |
4341 | !nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] || | |
4342 | !nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) { | |
4343 | return -EINVAL; | |
4344 | } | |
4345 | ||
ead18027 AS |
4346 | table = dpipe_table_alloc(); |
4347 | if (!table) | |
4348 | return -ENOMEM; | |
4349 | ||
4350 | table->name = strdup(mnl_attr_get_str(nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME])); | |
153c1a9b AS |
4351 | size = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE]); |
4352 | counters_enabled = !!mnl_attr_get_u8(nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]); | |
4353 | ||
ead18027 AS |
4354 | resource_valid = !!nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID]; |
4355 | if (resource_valid) { | |
4356 | table->resource_id = mnl_attr_get_u64(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID]); | |
4357 | table->resource_valid = true; | |
4358 | } | |
4359 | ||
4360 | list_add_tail(&table->list, &ctx->tables->table_list); | |
4361 | if (!ctx->print_tables) | |
4362 | return 0; | |
4363 | ||
4364 | pr_out_str(ctx->dl, "name", table->name); | |
153c1a9b AS |
4365 | pr_out_uint(ctx->dl, "size", size); |
4366 | pr_out_str(ctx->dl, "counters_enabled", | |
4367 | counters_enabled ? "true" : "false"); | |
4368 | ||
ead18027 AS |
4369 | if (resource_valid) { |
4370 | resource_units = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS]); | |
4371 | resource_path_print(ctx->dl, ctx->resources, | |
4372 | table->resource_id); | |
4373 | pr_out_uint(ctx->dl, "resource_units", resource_units); | |
4374 | } | |
4375 | ||
153c1a9b AS |
4376 | pr_out_array_start(ctx->dl, "match"); |
4377 | if (dpipe_table_matches_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES])) | |
4378 | goto err_matches_show; | |
4379 | pr_out_array_end(ctx->dl); | |
4380 | ||
4381 | pr_out_array_start(ctx->dl, "action"); | |
4382 | if (dpipe_table_actions_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS])) | |
4383 | goto err_actions_show; | |
4384 | pr_out_array_end(ctx->dl); | |
4385 | ||
4386 | return 0; | |
4387 | ||
4388 | err_actions_show: | |
4389 | err_matches_show: | |
4390 | pr_out_array_end(ctx->dl); | |
4391 | return -EINVAL; | |
4392 | } | |
4393 | ||
4394 | static int dpipe_tables_show(struct dpipe_ctx *ctx, struct nlattr **tb) | |
4395 | { | |
4396 | struct nlattr *nla_tables = tb[DEVLINK_ATTR_DPIPE_TABLES]; | |
4397 | struct nlattr *nla_table; | |
4398 | ||
4399 | mnl_attr_for_each_nested(nla_table, nla_tables) { | |
ead18027 AS |
4400 | if (ctx->print_tables) |
4401 | pr_out_handle_start_arr(ctx->dl, tb); | |
153c1a9b AS |
4402 | if (dpipe_table_show(ctx, nla_table)) |
4403 | goto err_table_show; | |
ead18027 AS |
4404 | if (ctx->print_tables) |
4405 | pr_out_handle_end(ctx->dl); | |
153c1a9b AS |
4406 | } |
4407 | return 0; | |
4408 | ||
4409 | err_table_show: | |
ead18027 AS |
4410 | if (ctx->print_tables) |
4411 | pr_out_handle_end(ctx->dl); | |
153c1a9b AS |
4412 | return -EINVAL; |
4413 | } | |
4414 | ||
4415 | static int cmd_dpipe_table_show_cb(const struct nlmsghdr *nlh, void *data) | |
4416 | { | |
4417 | struct dpipe_ctx *ctx = data; | |
4418 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
4419 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
4420 | ||
4421 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
4422 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
4423 | !tb[DEVLINK_ATTR_DPIPE_TABLES]) | |
4424 | return MNL_CB_ERROR; | |
4425 | ||
4426 | if (dpipe_tables_show(ctx, tb)) | |
4427 | return MNL_CB_ERROR; | |
4428 | return MNL_CB_OK; | |
4429 | } | |
4430 | ||
ead18027 AS |
4431 | static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data); |
4432 | ||
153c1a9b AS |
4433 | static int cmd_dpipe_table_show(struct dl *dl) |
4434 | { | |
4435 | struct nlmsghdr *nlh; | |
ead18027 AS |
4436 | struct dpipe_ctx dpipe_ctx = {}; |
4437 | struct resource_ctx resource_ctx = {}; | |
153c1a9b AS |
4438 | uint16_t flags = NLM_F_REQUEST; |
4439 | int err; | |
4440 | ||
ead18027 | 4441 | err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_DPIPE_TABLE_NAME); |
06a2cda9 AS |
4442 | if (err) |
4443 | return err; | |
153c1a9b | 4444 | |
ead18027 AS |
4445 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags); |
4446 | ||
4447 | err = dpipe_ctx_init(&dpipe_ctx, dl); | |
153c1a9b | 4448 | if (err) |
ead18027 AS |
4449 | return err; |
4450 | ||
4451 | dpipe_ctx.print_tables = true; | |
153c1a9b | 4452 | |
153c1a9b | 4453 | dl_opts_put(nlh, dl); |
ead18027 AS |
4454 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, |
4455 | &dpipe_ctx); | |
153c1a9b | 4456 | if (err) { |
ead18027 AS |
4457 | pr_err("error get headers %s\n", strerror(dpipe_ctx.err)); |
4458 | goto err_headers_get; | |
4459 | } | |
4460 | ||
4461 | err = resource_ctx_init(&resource_ctx, dl); | |
4462 | if (err) | |
4463 | goto err_resource_ctx_init; | |
4464 | ||
4465 | resource_ctx.print_resources = false; | |
4466 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP, flags); | |
4467 | dl_opts_put(nlh, dl); | |
4468 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, | |
4469 | &resource_ctx); | |
4470 | if (err) { | |
4471 | pr_err("error get resources %s\n", strerror(resource_ctx.err)); | |
4472 | goto err_resource_dump; | |
153c1a9b AS |
4473 | } |
4474 | ||
ead18027 | 4475 | dpipe_ctx.resources = resource_ctx.resources; |
153c1a9b AS |
4476 | flags = NLM_F_REQUEST | NLM_F_ACK; |
4477 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, flags); | |
4478 | dl_opts_put(nlh, dl); | |
4479 | ||
4480 | pr_out_section_start(dl, "table"); | |
ead18027 | 4481 | _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb, &dpipe_ctx); |
153c1a9b | 4482 | pr_out_section_end(dl); |
ead18027 AS |
4483 | |
4484 | resource_ctx_fini(&resource_ctx); | |
4485 | dpipe_ctx_fini(&dpipe_ctx); | |
4486 | return 0; | |
4487 | ||
4488 | err_resource_dump: | |
4489 | resource_ctx_fini(&resource_ctx); | |
4490 | err_resource_ctx_init: | |
4491 | err_headers_get: | |
4492 | dpipe_ctx_fini(&dpipe_ctx); | |
153c1a9b AS |
4493 | return err; |
4494 | } | |
4495 | ||
4496 | static int cmd_dpipe_table_set(struct dl *dl) | |
4497 | { | |
4498 | struct nlmsghdr *nlh; | |
4499 | int err; | |
4500 | ||
4501 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET, | |
4502 | NLM_F_REQUEST | NLM_F_ACK); | |
4503 | ||
4504 | err = dl_argv_parse_put(nlh, dl, | |
4505 | DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME | | |
4506 | DL_OPT_DPIPE_TABLE_COUNTERS, 0); | |
4507 | if (err) | |
4508 | return err; | |
4509 | ||
4510 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
4511 | } | |
4512 | ||
31639589 AS |
4513 | enum dpipe_value_type { |
4514 | DPIPE_VALUE_TYPE_VALUE, | |
4515 | DPIPE_VALUE_TYPE_MASK, | |
4516 | }; | |
4517 | ||
4518 | static const char * | |
4519 | dpipe_value_type_e2s(enum dpipe_value_type type) | |
4520 | { | |
4521 | switch (type) { | |
4522 | case DPIPE_VALUE_TYPE_VALUE: | |
4523 | return "value"; | |
4524 | case DPIPE_VALUE_TYPE_MASK: | |
4525 | return "value_mask"; | |
4526 | default: | |
4527 | return "<unknown>"; | |
4528 | } | |
4529 | } | |
4530 | ||
4531 | struct dpipe_field_printer { | |
4532 | unsigned int field_id; | |
4533 | void (*printer)(struct dpipe_ctx *, enum dpipe_value_type, void *); | |
4534 | }; | |
4535 | ||
4536 | struct dpipe_header_printer { | |
4537 | struct dpipe_field_printer *printers; | |
4538 | unsigned int printers_count; | |
4539 | unsigned int header_id; | |
4540 | }; | |
4541 | ||
b2947f8b AS |
4542 | static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx *ctx, |
4543 | enum dpipe_value_type type, | |
4544 | void *value) | |
4545 | { | |
4546 | struct in_addr ip_addr; | |
4547 | ||
4548 | ip_addr.s_addr = htonl(*(uint32_t *)value); | |
4549 | pr_out_str(ctx->dl, dpipe_value_type_e2s(type), inet_ntoa(ip_addr)); | |
4550 | } | |
4551 | ||
4552 | static void | |
4553 | dpipe_field_printer_ethernet_addr(struct dpipe_ctx *ctx, | |
4554 | enum dpipe_value_type type, | |
4555 | void *value) | |
4556 | { | |
4557 | pr_out_str(ctx->dl, dpipe_value_type_e2s(type), | |
4558 | ether_ntoa((struct ether_addr *)value)); | |
4559 | } | |
4560 | ||
4561 | static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx *ctx, | |
4562 | enum dpipe_value_type type, | |
4563 | void *value) | |
4564 | { | |
4565 | char str[INET6_ADDRSTRLEN]; | |
4566 | ||
4567 | inet_ntop(AF_INET6, value, str, INET6_ADDRSTRLEN); | |
4568 | pr_out_str(ctx->dl, dpipe_value_type_e2s(type), str); | |
4569 | } | |
4570 | ||
4571 | static struct dpipe_field_printer dpipe_field_printers_ipv4[] = { | |
4572 | { | |
4573 | .printer = dpipe_field_printer_ipv4_addr, | |
4574 | .field_id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP, | |
4575 | } | |
4576 | }; | |
4577 | ||
4578 | static struct dpipe_header_printer dpipe_header_printer_ipv4 = { | |
4579 | .printers = dpipe_field_printers_ipv4, | |
4580 | .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv4), | |
4581 | .header_id = DEVLINK_DPIPE_HEADER_IPV4, | |
4582 | }; | |
4583 | ||
4584 | static struct dpipe_field_printer dpipe_field_printers_ethernet[] = { | |
4585 | { | |
4586 | .printer = dpipe_field_printer_ethernet_addr, | |
4587 | .field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC, | |
4588 | }, | |
4589 | }; | |
4590 | ||
4591 | static struct dpipe_header_printer dpipe_header_printer_ethernet = { | |
4592 | .printers = dpipe_field_printers_ethernet, | |
4593 | .printers_count = ARRAY_SIZE(dpipe_field_printers_ethernet), | |
4594 | .header_id = DEVLINK_DPIPE_HEADER_ETHERNET, | |
4595 | }; | |
4596 | ||
4597 | static struct dpipe_field_printer dpipe_field_printers_ipv6[] = { | |
4598 | { | |
4599 | .printer = dpipe_field_printer_ipv6_addr, | |
4600 | .field_id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP, | |
4601 | } | |
4602 | }; | |
4603 | ||
4604 | static struct dpipe_header_printer dpipe_header_printer_ipv6 = { | |
4605 | .printers = dpipe_field_printers_ipv6, | |
4606 | .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv6), | |
4607 | .header_id = DEVLINK_DPIPE_HEADER_IPV6, | |
4608 | }; | |
4609 | ||
4610 | static struct dpipe_header_printer *dpipe_header_printers[] = { | |
4611 | &dpipe_header_printer_ipv4, | |
4612 | &dpipe_header_printer_ethernet, | |
4613 | &dpipe_header_printer_ipv6, | |
4614 | }; | |
31639589 AS |
4615 | |
4616 | static int dpipe_print_prot_header(struct dpipe_ctx *ctx, | |
4617 | struct dpipe_op_info *info, | |
4618 | enum dpipe_value_type type, | |
4619 | void *value) | |
153c1a9b | 4620 | { |
31639589 AS |
4621 | unsigned int header_printers_count = ARRAY_SIZE(dpipe_header_printers); |
4622 | struct dpipe_header_printer *header_printer; | |
4623 | struct dpipe_field_printer *field_printer; | |
4624 | unsigned int field_printers_count; | |
4625 | int j; | |
4626 | int i; | |
4627 | ||
4628 | for (i = 0; i < header_printers_count; i++) { | |
4629 | header_printer = dpipe_header_printers[i]; | |
4630 | if (header_printer->header_id != info->header_id) | |
4631 | continue; | |
4632 | field_printers_count = header_printer->printers_count; | |
4633 | for (j = 0; j < field_printers_count; j++) { | |
4634 | field_printer = &header_printer->printers[j]; | |
4635 | if (field_printer->field_id != info->field_id) | |
4636 | continue; | |
4637 | field_printer->printer(ctx, type, value); | |
4638 | return 0; | |
4639 | } | |
4640 | } | |
4641 | ||
4642 | return -EINVAL; | |
4643 | } | |
4644 | ||
4645 | static void __pr_out_entry_value(struct dpipe_ctx *ctx, | |
4646 | void *value, | |
4647 | unsigned int value_len, | |
4648 | struct dpipe_op_info *info, | |
4649 | enum dpipe_value_type type) | |
4650 | { | |
4651 | if (info->header_global && | |
4652 | !dpipe_print_prot_header(ctx, info, type, value)) | |
4653 | return; | |
4654 | ||
4655 | if (value_len == sizeof(uint32_t)) { | |
4656 | uint32_t *value_32 = value; | |
4657 | ||
4658 | pr_out_uint(ctx->dl, dpipe_value_type_e2s(type), *value_32); | |
4659 | } | |
4660 | } | |
4661 | ||
4662 | static void pr_out_dpipe_entry_value(struct dpipe_ctx *ctx, | |
4663 | struct nlattr **nla_match_value, | |
4664 | struct dpipe_op_info *info) | |
4665 | { | |
4666 | void *value, *value_mask; | |
4667 | uint32_t value_mapping; | |
153c1a9b AS |
4668 | uint16_t value_len; |
4669 | bool mask, mapping; | |
4670 | ||
4671 | mask = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MASK]; | |
4672 | mapping = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]; | |
4673 | ||
4674 | value_len = mnl_attr_get_payload_len(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]); | |
31639589 | 4675 | value = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]); |
153c1a9b | 4676 | |
31639589 AS |
4677 | if (mapping) { |
4678 | value_mapping = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]); | |
4679 | pr_out_uint(ctx->dl, "mapping_value", value_mapping); | |
4680 | } | |
153c1a9b | 4681 | |
31639589 AS |
4682 | if (mask) { |
4683 | value_mask = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]); | |
4684 | __pr_out_entry_value(ctx, value_mask, value_len, info, | |
4685 | DPIPE_VALUE_TYPE_MASK); | |
153c1a9b AS |
4686 | } |
4687 | ||
31639589 | 4688 | __pr_out_entry_value(ctx, value, value_len, info, DPIPE_VALUE_TYPE_VALUE); |
153c1a9b AS |
4689 | } |
4690 | ||
4691 | static int dpipe_entry_match_value_show(struct dpipe_ctx *ctx, | |
4692 | struct nlattr *nl) | |
4693 | { | |
4694 | struct nlattr *nla_match_value[DEVLINK_ATTR_MAX + 1] = {}; | |
92b2a5bb | 4695 | struct dpipe_match match; |
153c1a9b AS |
4696 | int err; |
4697 | ||
4698 | err = mnl_attr_parse_nested(nl, attr_cb, nla_match_value); | |
4699 | if (err != MNL_CB_OK) | |
4700 | return -EINVAL; | |
4701 | ||
4702 | if (!nla_match_value[DEVLINK_ATTR_DPIPE_MATCH] || | |
4703 | !nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]) { | |
4704 | return -EINVAL; | |
4705 | } | |
4706 | ||
4707 | pr_out_entry_start(ctx->dl); | |
92b2a5bb AS |
4708 | if (dpipe_match_parse(&match, |
4709 | nla_match_value[DEVLINK_ATTR_DPIPE_MATCH])) | |
4710 | goto err_match_parse; | |
4711 | pr_out_dpipe_match(&match, ctx); | |
31639589 | 4712 | pr_out_dpipe_entry_value(ctx, nla_match_value, &match.info); |
153c1a9b AS |
4713 | pr_out_entry_end(ctx->dl); |
4714 | ||
4715 | return 0; | |
4716 | ||
92b2a5bb | 4717 | err_match_parse: |
153c1a9b AS |
4718 | pr_out_entry_end(ctx->dl); |
4719 | return -EINVAL; | |
4720 | } | |
4721 | ||
4722 | static int dpipe_entry_action_value_show(struct dpipe_ctx *ctx, | |
4723 | struct nlattr *nl) | |
4724 | { | |
4725 | struct nlattr *nla_action_value[DEVLINK_ATTR_MAX + 1] = {}; | |
92b2a5bb | 4726 | struct dpipe_action action; |
153c1a9b AS |
4727 | int err; |
4728 | ||
4729 | err = mnl_attr_parse_nested(nl, attr_cb, nla_action_value); | |
4730 | if (err != MNL_CB_OK) | |
4731 | return -EINVAL; | |
4732 | ||
4733 | if (!nla_action_value[DEVLINK_ATTR_DPIPE_ACTION] || | |
4734 | !nla_action_value[DEVLINK_ATTR_DPIPE_VALUE]) { | |
4735 | return -EINVAL; | |
4736 | } | |
4737 | ||
4738 | pr_out_entry_start(ctx->dl); | |
92b2a5bb AS |
4739 | if (dpipe_action_parse(&action, |
4740 | nla_action_value[DEVLINK_ATTR_DPIPE_ACTION])) | |
4741 | goto err_action_parse; | |
4742 | pr_out_dpipe_action(&action, ctx); | |
31639589 | 4743 | pr_out_dpipe_entry_value(ctx, nla_action_value, &action.info); |
153c1a9b AS |
4744 | pr_out_entry_end(ctx->dl); |
4745 | ||
4746 | return 0; | |
4747 | ||
92b2a5bb | 4748 | err_action_parse: |
153c1a9b AS |
4749 | pr_out_entry_end(ctx->dl); |
4750 | return -EINVAL; | |
4751 | } | |
4752 | ||
4753 | static int | |
4754 | dpipe_tables_action_values_show(struct dpipe_ctx *ctx, | |
4755 | struct nlattr *nla_action_values) | |
4756 | { | |
4757 | struct nlattr *nla_action_value; | |
4758 | ||
4759 | mnl_attr_for_each_nested(nla_action_value, nla_action_values) { | |
4760 | if (dpipe_entry_action_value_show(ctx, nla_action_value)) | |
4761 | return -EINVAL; | |
4762 | } | |
4763 | return 0; | |
4764 | } | |
4765 | ||
4766 | static int | |
4767 | dpipe_tables_match_values_show(struct dpipe_ctx *ctx, | |
4768 | struct nlattr *nla_match_values) | |
4769 | { | |
4770 | struct nlattr *nla_match_value; | |
4771 | ||
4772 | mnl_attr_for_each_nested(nla_match_value, nla_match_values) { | |
4773 | if (dpipe_entry_match_value_show(ctx, nla_match_value)) | |
4774 | return -EINVAL; | |
4775 | } | |
4776 | return 0; | |
4777 | } | |
4778 | ||
4779 | static int dpipe_entry_show(struct dpipe_ctx *ctx, struct nlattr *nl) | |
4780 | { | |
4781 | struct nlattr *nla_entry[DEVLINK_ATTR_MAX + 1] = {}; | |
4782 | uint32_t entry_index; | |
4783 | uint64_t counter; | |
4784 | int err; | |
4785 | ||
4786 | err = mnl_attr_parse_nested(nl, attr_cb, nla_entry); | |
4787 | if (err != MNL_CB_OK) | |
4788 | return -EINVAL; | |
4789 | ||
4790 | if (!nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] || | |
4791 | !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] || | |
4792 | !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]) { | |
4793 | return -EINVAL; | |
4794 | } | |
4795 | ||
4796 | entry_index = mnl_attr_get_u32(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX]); | |
4797 | pr_out_uint(ctx->dl, "index", entry_index); | |
4798 | ||
4799 | if (nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]) { | |
4800 | counter = mnl_attr_get_u64(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]); | |
4801 | pr_out_uint(ctx->dl, "counter", counter); | |
4802 | } | |
4803 | ||
4804 | pr_out_array_start(ctx->dl, "match_value"); | |
4805 | if (dpipe_tables_match_values_show(ctx, | |
4806 | nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES])) | |
4807 | goto err_match_values_show; | |
4808 | pr_out_array_end(ctx->dl); | |
4809 | ||
4810 | pr_out_array_start(ctx->dl, "action_value"); | |
4811 | if (dpipe_tables_action_values_show(ctx, | |
4812 | nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES])) | |
4813 | goto err_action_values_show; | |
4814 | pr_out_array_end(ctx->dl); | |
4815 | return 0; | |
4816 | ||
4817 | err_action_values_show: | |
4818 | err_match_values_show: | |
4819 | pr_out_array_end(ctx->dl); | |
4820 | return -EINVAL; | |
4821 | } | |
4822 | ||
4823 | static int dpipe_table_entries_show(struct dpipe_ctx *ctx, struct nlattr **tb) | |
4824 | { | |
4825 | struct nlattr *nla_entries = tb[DEVLINK_ATTR_DPIPE_ENTRIES]; | |
4826 | struct nlattr *nla_entry; | |
4827 | ||
4828 | mnl_attr_for_each_nested(nla_entry, nla_entries) { | |
4829 | pr_out_handle_start_arr(ctx->dl, tb); | |
4830 | if (dpipe_entry_show(ctx, nla_entry)) | |
4831 | goto err_entry_show; | |
4832 | pr_out_handle_end(ctx->dl); | |
4833 | } | |
4834 | return 0; | |
4835 | ||
4836 | err_entry_show: | |
4837 | pr_out_handle_end(ctx->dl); | |
4838 | return -EINVAL; | |
4839 | } | |
4840 | ||
4841 | static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr *nlh, void *data) | |
4842 | { | |
4843 | struct dpipe_ctx *ctx = data; | |
4844 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
4845 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
4846 | ||
4847 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
4848 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
4849 | !tb[DEVLINK_ATTR_DPIPE_ENTRIES]) | |
4850 | return MNL_CB_ERROR; | |
4851 | ||
4852 | if (dpipe_table_entries_show(ctx, tb)) | |
4853 | return MNL_CB_ERROR; | |
4854 | return MNL_CB_OK; | |
4855 | } | |
4856 | ||
4857 | static int cmd_dpipe_table_dump(struct dl *dl) | |
4858 | { | |
4859 | struct nlmsghdr *nlh; | |
06a2cda9 | 4860 | struct dpipe_ctx ctx = {}; |
153c1a9b AS |
4861 | uint16_t flags = NLM_F_REQUEST; |
4862 | int err; | |
4863 | ||
06a2cda9 AS |
4864 | err = dpipe_ctx_init(&ctx, dl); |
4865 | if (err) | |
4866 | return err; | |
153c1a9b AS |
4867 | |
4868 | err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME, 0); | |
4869 | if (err) | |
4870 | goto out; | |
4871 | ||
4872 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags); | |
4873 | dl_opts_put(nlh, dl); | |
06a2cda9 | 4874 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx); |
153c1a9b | 4875 | if (err) { |
06a2cda9 | 4876 | pr_err("error get headers %s\n", strerror(ctx.err)); |
153c1a9b AS |
4877 | goto out; |
4878 | } | |
4879 | ||
4880 | flags = NLM_F_REQUEST | NLM_F_ACK; | |
4881 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_ENTRIES_GET, flags); | |
4882 | dl_opts_put(nlh, dl); | |
4883 | ||
4884 | pr_out_section_start(dl, "table_entry"); | |
06a2cda9 | 4885 | _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_entry_dump_cb, &ctx); |
153c1a9b AS |
4886 | pr_out_section_end(dl); |
4887 | out: | |
06a2cda9 | 4888 | dpipe_ctx_fini(&ctx); |
153c1a9b AS |
4889 | return err; |
4890 | } | |
4891 | ||
4892 | static void cmd_dpipe_table_help(void) | |
4893 | { | |
4894 | pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n" | |
4895 | "where OBJECT-LIST := { show | set | dump }\n"); | |
4896 | } | |
4897 | ||
4898 | static int cmd_dpipe_table(struct dl *dl) | |
4899 | { | |
4900 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
4901 | cmd_dpipe_table_help(); | |
4902 | return 0; | |
4903 | } else if (dl_argv_match(dl, "show")) { | |
4904 | dl_arg_inc(dl); | |
4905 | return cmd_dpipe_table_show(dl); | |
4906 | } else if (dl_argv_match(dl, "set")) { | |
4907 | dl_arg_inc(dl); | |
4908 | return cmd_dpipe_table_set(dl); | |
4909 | } else if (dl_argv_match(dl, "dump")) { | |
4910 | dl_arg_inc(dl); | |
4911 | return cmd_dpipe_table_dump(dl); | |
4912 | } | |
4913 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
4914 | return -ENOENT; | |
4915 | } | |
4916 | ||
4917 | static void cmd_dpipe_help(void) | |
4918 | { | |
4919 | pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n" | |
4920 | "where OBJECT-LIST := { header | table }\n"); | |
4921 | } | |
4922 | ||
4923 | static int cmd_dpipe(struct dl *dl) | |
4924 | { | |
4925 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
4926 | cmd_dpipe_help(); | |
4927 | return 0; | |
4928 | } else if (dl_argv_match(dl, "header")) { | |
4929 | dl_arg_inc(dl); | |
4930 | return cmd_dpipe_header(dl); | |
4931 | } else if (dl_argv_match(dl, "table")) { | |
4932 | dl_arg_inc(dl); | |
4933 | return cmd_dpipe_table(dl); | |
4934 | } | |
4935 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
4936 | return -ENOENT; | |
4937 | } | |
4938 | ||
8cd64409 AS |
4939 | static int |
4940 | resource_parse(struct resource_ctx *ctx, struct resource *resource, | |
4941 | struct nlattr **nla_resource) | |
4942 | { | |
4943 | if (!nla_resource[DEVLINK_ATTR_RESOURCE_NAME] || | |
4944 | !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE] || | |
4945 | !nla_resource[DEVLINK_ATTR_RESOURCE_ID] || | |
4946 | !nla_resource[DEVLINK_ATTR_RESOURCE_UNIT] || | |
4947 | !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN] || | |
4948 | !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX] || | |
4949 | !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]) { | |
4950 | return -EINVAL; | |
4951 | } | |
4952 | ||
4953 | resource->name = strdup(mnl_attr_get_str(nla_resource[DEVLINK_ATTR_RESOURCE_NAME])); | |
4954 | resource->size = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE]); | |
4955 | resource->id = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_ID]); | |
4956 | resource->unit = mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_UNIT]); | |
4957 | resource->size_min = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN]); | |
4958 | resource->size_max = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX]); | |
4959 | resource->size_gran = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]); | |
4960 | ||
4961 | if (nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW]) | |
4962 | resource->size_new = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW]); | |
4963 | else | |
4964 | resource->size_new = resource->size; | |
4965 | ||
4966 | if (nla_resource[DEVLINK_ATTR_RESOURCE_OCC]) { | |
4967 | resource->size_occ = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_OCC]); | |
4968 | resource->occ_valid = true; | |
4969 | } | |
4970 | ||
4971 | if (resource->size_new != resource->size) | |
4972 | ctx->pending_change = true; | |
4973 | ||
4974 | return 0; | |
4975 | } | |
4976 | ||
4977 | static int | |
4978 | resource_get(struct resource_ctx *ctx, struct resource *resource, | |
4979 | struct resource *parent_resource, struct nlattr *nl) | |
4980 | { | |
4981 | struct nlattr *nla_resource[DEVLINK_ATTR_MAX + 1] = {}; | |
4982 | struct nlattr *nla_child_resource; | |
4983 | struct nlattr *nla_resources; | |
4984 | bool top = false; | |
4985 | int err; | |
4986 | ||
4987 | if (!resource) { | |
4988 | nla_resources = nl; | |
4989 | top = true; | |
4990 | goto out; | |
4991 | } | |
4992 | ||
4993 | err = mnl_attr_parse_nested(nl, attr_cb, nla_resource); | |
4994 | if (err != MNL_CB_OK) | |
4995 | return -EINVAL; | |
4996 | ||
4997 | err = resource_parse(ctx, resource, nla_resource); | |
4998 | if (err) | |
4999 | return err; | |
5000 | ||
5001 | resource->parent = parent_resource; | |
5002 | if (!nla_resource[DEVLINK_ATTR_RESOURCE_LIST]) | |
5003 | return 0; | |
5004 | ||
5005 | resource->size_valid = !!mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_VALID]); | |
5006 | nla_resources = nla_resource[DEVLINK_ATTR_RESOURCE_LIST]; | |
5007 | out: | |
5008 | mnl_attr_for_each_nested(nla_child_resource, nla_resources) { | |
5009 | struct resource *child_resource; | |
5010 | struct list_head *list; | |
5011 | ||
5012 | child_resource = resource_alloc(); | |
5013 | if (!child_resource) | |
5014 | return -ENOMEM; | |
5015 | ||
5016 | if (top) | |
5017 | list = &ctx->resources->resource_list; | |
5018 | else | |
5019 | list = &resource->resource_list; | |
5020 | ||
5021 | list_add_tail(&child_resource->list, list); | |
5022 | err = resource_get(ctx, child_resource, resource, | |
5023 | nla_child_resource); | |
5024 | if (err) | |
5025 | return err; | |
5026 | } | |
5027 | ||
5028 | return 0; | |
5029 | } | |
5030 | ||
5031 | static const char *resource_unit_str_get(enum devlink_resource_unit unit) | |
5032 | { | |
5033 | switch (unit) { | |
5034 | case DEVLINK_RESOURCE_UNIT_ENTRY: return "entry"; | |
5035 | default: return "<unknown unit>"; | |
5036 | } | |
5037 | } | |
5038 | ||
5039 | static void resource_show(struct resource *resource, | |
5040 | struct resource_ctx *ctx) | |
5041 | { | |
5042 | struct resource *child_resource; | |
ead18027 | 5043 | struct dpipe_table *table; |
8cd64409 | 5044 | struct dl *dl = ctx->dl; |
ead18027 | 5045 | bool array = false; |
8cd64409 AS |
5046 | |
5047 | pr_out_str(dl, "name", resource->name); | |
5048 | if (dl->verbose) | |
5049 | resource_path_print(dl, ctx->resources, resource->id); | |
43eb8728 | 5050 | pr_out_u64(dl, "size", resource->size); |
8cd64409 | 5051 | if (resource->size != resource->size_new) |
43eb8728 | 5052 | pr_out_u64(dl, "size_new", resource->size_new); |
8cd64409 AS |
5053 | if (resource->occ_valid) |
5054 | pr_out_uint(dl, "occ", resource->size_occ); | |
5055 | pr_out_str(dl, "unit", resource_unit_str_get(resource->unit)); | |
5056 | ||
5057 | if (resource->size_min != resource->size_max) { | |
5058 | pr_out_uint(dl, "size_min", resource->size_min); | |
43eb8728 | 5059 | pr_out_u64(dl, "size_max", resource->size_max); |
8cd64409 AS |
5060 | pr_out_uint(dl, "size_gran", resource->size_gran); |
5061 | } | |
5062 | ||
ead18027 AS |
5063 | list_for_each_entry(table, &ctx->tables->table_list, list) |
5064 | if (table->resource_id == resource->id && | |
5065 | table->resource_valid) | |
5066 | array = true; | |
5067 | ||
5068 | if (array) | |
5069 | pr_out_array_start(dl, "dpipe_tables"); | |
5070 | else | |
5071 | pr_out_str(dl, "dpipe_tables", "none"); | |
5072 | ||
5073 | list_for_each_entry(table, &ctx->tables->table_list, list) { | |
5074 | if (table->resource_id != resource->id || | |
5075 | !table->resource_valid) | |
5076 | continue; | |
5077 | pr_out_entry_start(dl); | |
5078 | pr_out_str(dl, "table_name", table->name); | |
5079 | pr_out_entry_end(dl); | |
5080 | } | |
5081 | if (array) | |
5082 | pr_out_array_end(dl); | |
5083 | ||
8cd64409 AS |
5084 | if (list_empty(&resource->resource_list)) |
5085 | return; | |
5086 | ||
5087 | if (ctx->pending_change) | |
5088 | pr_out_str(dl, "size_valid", resource->size_valid ? | |
5089 | "true" : "false"); | |
5090 | pr_out_array_start(dl, "resources"); | |
5091 | list_for_each_entry(child_resource, &resource->resource_list, list) { | |
5092 | pr_out_entry_start(dl); | |
5093 | resource_show(child_resource, ctx); | |
5094 | pr_out_entry_end(dl); | |
5095 | } | |
5096 | pr_out_array_end(dl); | |
5097 | } | |
5098 | ||
5099 | static void | |
5100 | resources_show(struct resource_ctx *ctx, struct nlattr **tb) | |
5101 | { | |
5102 | struct resources *resources = ctx->resources; | |
5103 | struct resource *resource; | |
5104 | ||
5105 | list_for_each_entry(resource, &resources->resource_list, list) { | |
5106 | pr_out_handle_start_arr(ctx->dl, tb); | |
5107 | resource_show(resource, ctx); | |
5108 | pr_out_handle_end(ctx->dl); | |
5109 | } | |
5110 | } | |
5111 | ||
5112 | static int resources_get(struct resource_ctx *ctx, struct nlattr **tb) | |
5113 | { | |
5114 | return resource_get(ctx, NULL, NULL, tb[DEVLINK_ATTR_RESOURCE_LIST]); | |
5115 | } | |
5116 | ||
5117 | static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data) | |
5118 | { | |
5119 | struct resource_ctx *ctx = data; | |
5120 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
5121 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
5122 | int err; | |
5123 | ||
5124 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
5125 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
5126 | !tb[DEVLINK_ATTR_RESOURCE_LIST]) | |
5127 | return MNL_CB_ERROR; | |
5128 | ||
5129 | err = resources_get(ctx, tb); | |
5130 | if (err) { | |
5131 | ctx->err = err; | |
5132 | return MNL_CB_ERROR; | |
5133 | } | |
5134 | ||
5135 | if (ctx->print_resources) | |
5136 | resources_show(ctx, tb); | |
5137 | ||
5138 | return MNL_CB_OK; | |
5139 | } | |
5140 | ||
5141 | static int cmd_resource_show(struct dl *dl) | |
5142 | { | |
5143 | struct nlmsghdr *nlh; | |
ead18027 AS |
5144 | struct dpipe_ctx dpipe_ctx = {}; |
5145 | struct resource_ctx resource_ctx = {}; | |
8cd64409 AS |
5146 | int err; |
5147 | ||
ead18027 | 5148 | err = dl_argv_parse(dl, DL_OPT_HANDLE, 0); |
8cd64409 AS |
5149 | if (err) |
5150 | return err; | |
5151 | ||
ead18027 AS |
5152 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, |
5153 | NLM_F_REQUEST); | |
5154 | dl_opts_put(nlh, dl); | |
5155 | ||
5156 | err = dpipe_ctx_init(&dpipe_ctx, dl); | |
8cd64409 AS |
5157 | if (err) |
5158 | return err; | |
5159 | ||
ead18027 AS |
5160 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb, |
5161 | &dpipe_ctx); | |
5162 | if (err) { | |
5163 | pr_err("error get tables %s\n", strerror(dpipe_ctx.err)); | |
5164 | goto out; | |
5165 | } | |
5166 | ||
5167 | err = resource_ctx_init(&resource_ctx, dl); | |
5168 | if (err) | |
5169 | goto out; | |
5170 | ||
5171 | resource_ctx.print_resources = true; | |
5172 | resource_ctx.tables = dpipe_ctx.tables; | |
5173 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP, | |
5174 | NLM_F_REQUEST | NLM_F_ACK); | |
5175 | dl_opts_put(nlh, dl); | |
8cd64409 | 5176 | pr_out_section_start(dl, "resources"); |
ead18027 AS |
5177 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, |
5178 | &resource_ctx); | |
8cd64409 | 5179 | pr_out_section_end(dl); |
ead18027 AS |
5180 | resource_ctx_fini(&resource_ctx); |
5181 | out: | |
5182 | dpipe_ctx_fini(&dpipe_ctx); | |
8cd64409 AS |
5183 | return err; |
5184 | } | |
5185 | ||
5186 | static void cmd_resource_help(void) | |
5187 | { | |
5188 | pr_err("Usage: devlink resource show DEV\n" | |
5189 | " devlink resource set DEV path PATH size SIZE\n"); | |
5190 | } | |
5191 | ||
5192 | static struct resource * | |
5193 | resource_find_by_name(struct list_head *list, char *name) | |
5194 | { | |
5195 | struct resource *resource; | |
5196 | ||
5197 | list_for_each_entry(resource, list, list) { | |
5198 | if (!strcmp(resource->name, name)) | |
5199 | return resource; | |
5200 | } | |
5201 | return NULL; | |
5202 | } | |
5203 | ||
5204 | static int | |
5205 | resource_path_parse(struct resource_ctx *ctx, const char *resource_path, | |
5206 | uint32_t *p_resource_id, bool *p_resource_valid) | |
5207 | { | |
5208 | struct resource *resource; | |
5209 | uint32_t resource_id = 0; | |
5210 | char *resource_path_dup; | |
5211 | struct list_head *list; | |
5212 | const char del[] = "/"; | |
5213 | char *resource_name; | |
5214 | ||
5215 | resource_path_dup = strdup(resource_path); | |
5216 | list = &ctx->resources->resource_list; | |
5217 | resource_name = strtok(resource_path_dup, del); | |
5218 | while (resource_name != NULL) { | |
5219 | resource = resource_find_by_name(list, resource_name); | |
5220 | if (!resource) | |
5221 | goto err_resource_lookup; | |
5222 | ||
5223 | list = &resource->resource_list; | |
5224 | resource_name = strtok(NULL, del); | |
5225 | resource_id = resource->id; | |
5226 | } | |
5227 | free(resource_path_dup); | |
5228 | *p_resource_valid = true; | |
5229 | *p_resource_id = resource_id; | |
5230 | return 0; | |
5231 | ||
5232 | err_resource_lookup: | |
5233 | free(resource_path_dup); | |
5234 | return -EINVAL; | |
5235 | } | |
5236 | ||
5237 | static int cmd_resource_set(struct dl *dl) | |
5238 | { | |
5239 | struct nlmsghdr *nlh; | |
5240 | struct resource_ctx ctx = {}; | |
5241 | int err; | |
5242 | ||
5243 | err = resource_ctx_init(&ctx, dl); | |
5244 | if (err) | |
5245 | return err; | |
5246 | ||
5247 | ctx.print_resources = false; | |
5248 | err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_RESOURCE_PATH | | |
5249 | DL_OPT_RESOURCE_SIZE, 0); | |
5250 | if (err) | |
5251 | goto out; | |
5252 | ||
5253 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP, | |
5254 | NLM_F_REQUEST); | |
5255 | dl_opts_put(nlh, dl); | |
5256 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, &ctx); | |
5257 | if (err) { | |
5258 | pr_err("error getting resources %s\n", strerror(ctx.err)); | |
5259 | goto out; | |
5260 | } | |
5261 | ||
5262 | err = resource_path_parse(&ctx, dl->opts.resource_path, | |
5263 | &dl->opts.resource_id, | |
5264 | &dl->opts.resource_id_valid); | |
5265 | if (err) { | |
b1ffc1f4 | 5266 | pr_err("error parsing resource path %s\n", strerror(-err)); |
8cd64409 AS |
5267 | goto out; |
5268 | } | |
5269 | ||
5270 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_SET, | |
5271 | NLM_F_REQUEST | NLM_F_ACK); | |
5272 | ||
5273 | dl_opts_put(nlh, dl); | |
5274 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
5275 | out: | |
5276 | resource_ctx_fini(&ctx); | |
5277 | return err; | |
5278 | } | |
5279 | ||
5280 | static int cmd_resource(struct dl *dl) | |
5281 | { | |
5282 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
5283 | cmd_resource_help(); | |
5284 | return 0; | |
5285 | } else if (dl_argv_match(dl, "show")) { | |
5286 | dl_arg_inc(dl); | |
5287 | return cmd_resource_show(dl); | |
5288 | } else if (dl_argv_match(dl, "set")) { | |
5289 | dl_arg_inc(dl); | |
5290 | return cmd_resource_set(dl); | |
5291 | } | |
5292 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
5293 | return -ENOENT; | |
5294 | } | |
5295 | ||
8b4fbf0b AV |
5296 | static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb) |
5297 | { | |
5298 | const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); | |
5299 | const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); | |
5300 | const char *region_name = mnl_attr_get_str(tb[DEVLINK_ATTR_REGION_NAME]); | |
5301 | char buf[256]; | |
5302 | ||
5303 | sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name); | |
5304 | if (dl->json_output) { | |
5305 | jsonw_name(dl->jw, buf); | |
5306 | jsonw_start_object(dl->jw); | |
5307 | } else { | |
5308 | pr_out("%s:", buf); | |
5309 | } | |
5310 | } | |
5311 | ||
5312 | static void pr_out_region_handle_end(struct dl *dl) | |
5313 | { | |
5314 | if (dl->json_output) | |
5315 | jsonw_end_object(dl->jw); | |
5316 | else | |
5317 | pr_out("\n"); | |
5318 | } | |
5319 | ||
5320 | static void pr_out_region_snapshots_start(struct dl *dl, bool array) | |
5321 | { | |
5322 | if (dl->json_output) { | |
5323 | jsonw_name(dl->jw, "snapshot"); | |
5324 | jsonw_start_array(dl->jw); | |
5325 | } else { | |
5326 | if (g_indent_newline) | |
5327 | pr_out("snapshot %s", array ? "[" : ""); | |
5328 | else | |
5329 | pr_out(" snapshot %s", array ? "[" : ""); | |
5330 | } | |
5331 | } | |
5332 | ||
5333 | static void pr_out_region_snapshots_end(struct dl *dl, bool array) | |
5334 | { | |
5335 | if (dl->json_output) | |
5336 | jsonw_end_array(dl->jw); | |
5337 | else if (array) | |
5338 | pr_out("]"); | |
5339 | } | |
5340 | ||
5341 | static void pr_out_region_snapshots_id(struct dl *dl, struct nlattr **tb, int index) | |
5342 | { | |
5343 | uint32_t snapshot_id; | |
5344 | ||
5345 | if (!tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) | |
5346 | return; | |
5347 | ||
5348 | snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); | |
5349 | ||
5350 | if (dl->json_output) | |
5351 | jsonw_uint(dl->jw, snapshot_id); | |
5352 | else | |
5353 | pr_out("%s%u", index ? " " : "", snapshot_id); | |
5354 | } | |
5355 | ||
5356 | static void pr_out_snapshots(struct dl *dl, struct nlattr **tb) | |
5357 | { | |
5358 | struct nlattr *tb_snapshot[DEVLINK_ATTR_MAX + 1] = {}; | |
5359 | struct nlattr *nla_sanpshot; | |
5360 | int err, index = 0; | |
5361 | ||
5362 | pr_out_region_snapshots_start(dl, true); | |
5363 | mnl_attr_for_each_nested(nla_sanpshot, tb[DEVLINK_ATTR_REGION_SNAPSHOTS]) { | |
5364 | err = mnl_attr_parse_nested(nla_sanpshot, attr_cb, tb_snapshot); | |
5365 | if (err != MNL_CB_OK) | |
5366 | return; | |
5367 | pr_out_region_snapshots_id(dl, tb_snapshot, index++); | |
5368 | } | |
5369 | pr_out_region_snapshots_end(dl, true); | |
5370 | } | |
5371 | ||
5372 | static void pr_out_snapshot(struct dl *dl, struct nlattr **tb) | |
5373 | { | |
5374 | pr_out_region_snapshots_start(dl, false); | |
5375 | pr_out_region_snapshots_id(dl, tb, 0); | |
5376 | pr_out_region_snapshots_end(dl, false); | |
5377 | } | |
5378 | ||
5379 | static void pr_out_region(struct dl *dl, struct nlattr **tb) | |
5380 | { | |
5381 | pr_out_region_handle_start(dl, tb); | |
5382 | ||
5383 | if (tb[DEVLINK_ATTR_REGION_SIZE]) | |
5384 | pr_out_u64(dl, "size", | |
5385 | mnl_attr_get_u64(tb[DEVLINK_ATTR_REGION_SIZE])); | |
5386 | ||
5387 | if (tb[DEVLINK_ATTR_REGION_SNAPSHOTS]) | |
5388 | pr_out_snapshots(dl, tb); | |
5389 | ||
5390 | if (tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) | |
5391 | pr_out_snapshot(dl, tb); | |
5392 | ||
5393 | pr_out_region_handle_end(dl); | |
5394 | } | |
5395 | ||
5396 | static int cmd_region_show_cb(const struct nlmsghdr *nlh, void *data) | |
5397 | { | |
5398 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
5399 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
5400 | struct dl *dl = data; | |
5401 | ||
5402 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
5403 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
5404 | !tb[DEVLINK_ATTR_REGION_NAME] || !tb[DEVLINK_ATTR_REGION_SIZE]) | |
5405 | return MNL_CB_ERROR; | |
5406 | ||
5407 | pr_out_region(dl, tb); | |
5408 | ||
5409 | return MNL_CB_OK; | |
5410 | } | |
5411 | ||
5412 | static int cmd_region_show(struct dl *dl) | |
5413 | { | |
5414 | struct nlmsghdr *nlh; | |
5415 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
5416 | int err; | |
5417 | ||
5418 | if (dl_argc(dl) == 0) | |
5419 | flags |= NLM_F_DUMP; | |
5420 | ||
5421 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_GET, flags); | |
5422 | ||
5423 | if (dl_argc(dl) > 0) { | |
5424 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION, 0); | |
5425 | if (err) | |
5426 | return err; | |
5427 | } | |
5428 | ||
5429 | pr_out_section_start(dl, "regions"); | |
5430 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_show_cb, dl); | |
5431 | pr_out_section_end(dl); | |
5432 | return err; | |
5433 | } | |
5434 | ||
5435 | static int cmd_region_snapshot_del(struct dl *dl) | |
5436 | { | |
5437 | struct nlmsghdr *nlh; | |
5438 | int err; | |
5439 | ||
5440 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_DEL, | |
5441 | NLM_F_REQUEST | NLM_F_ACK); | |
5442 | ||
5443 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION | | |
5444 | DL_OPT_REGION_SNAPSHOT_ID, 0); | |
5445 | if (err) | |
5446 | return err; | |
5447 | ||
5448 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
5449 | } | |
5450 | ||
5451 | static int cmd_region_read_cb(const struct nlmsghdr *nlh, void *data) | |
5452 | { | |
5453 | struct nlattr *nla_entry, *nla_chunk_data, *nla_chunk_addr; | |
5454 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
5455 | struct nlattr *tb_field[DEVLINK_ATTR_MAX + 1] = {}; | |
5456 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
5457 | struct dl *dl = data; | |
5458 | int err; | |
5459 | ||
5460 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
5461 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
5462 | !tb[DEVLINK_ATTR_REGION_CHUNKS]) | |
5463 | return MNL_CB_ERROR; | |
5464 | ||
5465 | mnl_attr_for_each_nested(nla_entry, tb[DEVLINK_ATTR_REGION_CHUNKS]) { | |
5466 | err = mnl_attr_parse_nested(nla_entry, attr_cb, tb_field); | |
5467 | if (err != MNL_CB_OK) | |
5468 | return MNL_CB_ERROR; | |
5469 | ||
5470 | nla_chunk_data = tb_field[DEVLINK_ATTR_REGION_CHUNK_DATA]; | |
5471 | if (!nla_chunk_data) | |
5472 | continue; | |
5473 | ||
5474 | nla_chunk_addr = tb_field[DEVLINK_ATTR_REGION_CHUNK_ADDR]; | |
5475 | if (!nla_chunk_addr) | |
5476 | continue; | |
5477 | ||
5478 | pr_out_region_chunk(dl, mnl_attr_get_payload(nla_chunk_data), | |
5479 | mnl_attr_get_payload_len(nla_chunk_data), | |
5480 | mnl_attr_get_u64(nla_chunk_addr)); | |
5481 | } | |
5482 | return MNL_CB_OK; | |
5483 | } | |
5484 | ||
5485 | static int cmd_region_dump(struct dl *dl) | |
5486 | { | |
5487 | struct nlmsghdr *nlh; | |
5488 | int err; | |
5489 | ||
5490 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ, | |
5491 | NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); | |
5492 | ||
5493 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION | | |
5494 | DL_OPT_REGION_SNAPSHOT_ID, 0); | |
5495 | if (err) | |
5496 | return err; | |
5497 | ||
5498 | pr_out_section_start(dl, "dump"); | |
5499 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl); | |
5500 | pr_out_section_end(dl); | |
5501 | if (!dl->json_output) | |
5502 | pr_out("\n"); | |
5503 | return err; | |
5504 | } | |
5505 | ||
5506 | static int cmd_region_read(struct dl *dl) | |
5507 | { | |
5508 | struct nlmsghdr *nlh; | |
5509 | int err; | |
5510 | ||
5511 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ, | |
5512 | NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); | |
5513 | ||
5514 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION | | |
5515 | DL_OPT_REGION_ADDRESS | DL_OPT_REGION_LENGTH | | |
5516 | DL_OPT_REGION_SNAPSHOT_ID, 0); | |
5517 | if (err) | |
5518 | return err; | |
5519 | ||
5520 | pr_out_section_start(dl, "read"); | |
5521 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl); | |
5522 | pr_out_section_end(dl); | |
5523 | if (!dl->json_output) | |
5524 | pr_out("\n"); | |
5525 | return err; | |
5526 | } | |
5527 | ||
5528 | static void cmd_region_help(void) | |
5529 | { | |
5530 | pr_err("Usage: devlink region show [ DEV/REGION ]\n"); | |
5531 | pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n"); | |
5532 | pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n"); | |
5533 | pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n"); | |
5534 | } | |
5535 | ||
5536 | static int cmd_region(struct dl *dl) | |
5537 | { | |
5538 | if (dl_no_arg(dl)) { | |
5539 | return cmd_region_show(dl); | |
5540 | } else if (dl_argv_match(dl, "help")) { | |
5541 | cmd_region_help(); | |
5542 | return 0; | |
5543 | } else if (dl_argv_match(dl, "show")) { | |
5544 | dl_arg_inc(dl); | |
5545 | return cmd_region_show(dl); | |
5546 | } else if (dl_argv_match(dl, "del")) { | |
5547 | dl_arg_inc(dl); | |
5548 | return cmd_region_snapshot_del(dl); | |
5549 | } else if (dl_argv_match(dl, "dump")) { | |
5550 | dl_arg_inc(dl); | |
5551 | return cmd_region_dump(dl); | |
5552 | } else if (dl_argv_match(dl, "read")) { | |
5553 | dl_arg_inc(dl); | |
5554 | return cmd_region_read(dl); | |
5555 | } | |
5556 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
5557 | return -ENOENT; | |
5558 | } | |
5559 | ||
153c1a9b AS |
5560 | static void help(void) |
5561 | { | |
5562 | pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
3e897912 | 5563 | " devlink [ -f[orce] ] -b[atch] filename\n" |
8b4fbf0b | 5564 | "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region }\n" |
bb75b9bf | 5565 | " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n"); |
153c1a9b AS |
5566 | } |
5567 | ||
3e897912 | 5568 | static int dl_cmd(struct dl *dl, int argc, char **argv) |
153c1a9b | 5569 | { |
3e897912 IV |
5570 | dl->argc = argc; |
5571 | dl->argv = argv; | |
5572 | ||
153c1a9b AS |
5573 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { |
5574 | help(); | |
5575 | return 0; | |
5576 | } else if (dl_argv_match(dl, "dev")) { | |
5577 | dl_arg_inc(dl); | |
5578 | return cmd_dev(dl); | |
5579 | } else if (dl_argv_match(dl, "port")) { | |
5580 | dl_arg_inc(dl); | |
5581 | return cmd_port(dl); | |
5582 | } else if (dl_argv_match(dl, "sb")) { | |
5583 | dl_arg_inc(dl); | |
5584 | return cmd_sb(dl); | |
5585 | } else if (dl_argv_match(dl, "monitor")) { | |
5586 | dl_arg_inc(dl); | |
5587 | return cmd_mon(dl); | |
5588 | } else if (dl_argv_match(dl, "dpipe")) { | |
5589 | dl_arg_inc(dl); | |
5590 | return cmd_dpipe(dl); | |
8cd64409 AS |
5591 | } else if (dl_argv_match(dl, "resource")) { |
5592 | dl_arg_inc(dl); | |
5593 | return cmd_resource(dl); | |
8b4fbf0b AV |
5594 | } else if (dl_argv_match(dl, "region")) { |
5595 | dl_arg_inc(dl); | |
5596 | return cmd_region(dl); | |
153c1a9b AS |
5597 | } |
5598 | pr_err("Object \"%s\" not found\n", dl_argv(dl)); | |
5599 | return -ENOENT; | |
5600 | } | |
5601 | ||
3e897912 | 5602 | static int dl_init(struct dl *dl) |
153c1a9b AS |
5603 | { |
5604 | int err; | |
5605 | ||
153c1a9b AS |
5606 | dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION); |
5607 | if (!dl->nlg) { | |
5608 | pr_err("Failed to connect to devlink Netlink\n"); | |
5609 | return -errno; | |
5610 | } | |
5611 | ||
5612 | err = ifname_map_init(dl); | |
5613 | if (err) { | |
5614 | pr_err("Failed to create index map\n"); | |
5615 | goto err_ifname_map_create; | |
5616 | } | |
5617 | if (dl->json_output) { | |
5618 | dl->jw = jsonw_new(stdout); | |
5619 | if (!dl->jw) { | |
5620 | pr_err("Failed to create JSON writer\n"); | |
5621 | goto err_json_new; | |
5622 | } | |
5623 | jsonw_pretty(dl->jw, dl->pretty_output); | |
5624 | } | |
5625 | return 0; | |
5626 | ||
5627 | err_json_new: | |
5628 | ifname_map_fini(dl); | |
5629 | err_ifname_map_create: | |
5630 | mnlg_socket_close(dl->nlg); | |
5631 | return err; | |
5632 | } | |
5633 | ||
5634 | static void dl_fini(struct dl *dl) | |
5635 | { | |
5636 | if (dl->json_output) | |
5637 | jsonw_destroy(&dl->jw); | |
5638 | ifname_map_fini(dl); | |
5639 | mnlg_socket_close(dl->nlg); | |
5640 | } | |
5641 | ||
5642 | static struct dl *dl_alloc(void) | |
5643 | { | |
5644 | struct dl *dl; | |
5645 | ||
5646 | dl = calloc(1, sizeof(*dl)); | |
5647 | if (!dl) | |
5648 | return NULL; | |
5649 | return dl; | |
5650 | } | |
5651 | ||
5652 | static void dl_free(struct dl *dl) | |
5653 | { | |
5654 | free(dl); | |
5655 | } | |
5656 | ||
3e897912 IV |
5657 | static int dl_batch(struct dl *dl, const char *name, bool force) |
5658 | { | |
5659 | char *line = NULL; | |
5660 | size_t len = 0; | |
5661 | int ret = EXIT_SUCCESS; | |
5662 | ||
5663 | if (name && strcmp(name, "-") != 0) { | |
5664 | if (freopen(name, "r", stdin) == NULL) { | |
5665 | fprintf(stderr, | |
5666 | "Cannot open file \"%s\" for reading: %s\n", | |
5667 | name, strerror(errno)); | |
5668 | return EXIT_FAILURE; | |
5669 | } | |
5670 | } | |
5671 | ||
5672 | cmdlineno = 0; | |
5673 | while (getcmdline(&line, &len, stdin) != -1) { | |
5674 | char *largv[100]; | |
5675 | int largc; | |
5676 | ||
5677 | largc = makeargs(line, largv, 100); | |
5678 | if (!largc) | |
5679 | continue; /* blank line */ | |
5680 | ||
5681 | if (dl_cmd(dl, largc, largv)) { | |
5682 | fprintf(stderr, "Command failed %s:%d\n", | |
5683 | name, cmdlineno); | |
5684 | ret = EXIT_FAILURE; | |
5685 | if (!force) | |
5686 | break; | |
5687 | } | |
5688 | } | |
5689 | ||
5690 | if (line) | |
5691 | free(line); | |
5692 | ||
5693 | return ret; | |
5694 | } | |
5695 | ||
153c1a9b AS |
5696 | int main(int argc, char **argv) |
5697 | { | |
5698 | static const struct option long_options[] = { | |
5699 | { "Version", no_argument, NULL, 'V' }, | |
3e897912 IV |
5700 | { "force", no_argument, NULL, 'f' }, |
5701 | { "batch", required_argument, NULL, 'b' }, | |
153c1a9b AS |
5702 | { "no-nice-names", no_argument, NULL, 'n' }, |
5703 | { "json", no_argument, NULL, 'j' }, | |
5704 | { "pretty", no_argument, NULL, 'p' }, | |
5705 | { "verbose", no_argument, NULL, 'v' }, | |
5706 | { NULL, 0, NULL, 0 } | |
5707 | }; | |
3e897912 IV |
5708 | const char *batch_file = NULL; |
5709 | bool force = false; | |
153c1a9b AS |
5710 | struct dl *dl; |
5711 | int opt; | |
5712 | int err; | |
5713 | int ret; | |
5714 | ||
5715 | dl = dl_alloc(); | |
5716 | if (!dl) { | |
5717 | pr_err("Failed to allocate memory for devlink\n"); | |
5718 | return EXIT_FAILURE; | |
5719 | } | |
5720 | ||
3e897912 | 5721 | while ((opt = getopt_long(argc, argv, "Vfb:njpv", |
153c1a9b AS |
5722 | long_options, NULL)) >= 0) { |
5723 | ||
5724 | switch (opt) { | |
5725 | case 'V': | |
5726 | printf("devlink utility, iproute2-ss%s\n", SNAPSHOT); | |
5727 | ret = EXIT_SUCCESS; | |
5728 | goto dl_free; | |
3e897912 IV |
5729 | case 'f': |
5730 | force = true; | |
5731 | break; | |
5732 | case 'b': | |
5733 | batch_file = optarg; | |
5734 | break; | |
153c1a9b AS |
5735 | case 'n': |
5736 | dl->no_nice_names = true; | |
5737 | break; | |
5738 | case 'j': | |
5739 | dl->json_output = true; | |
5740 | break; | |
5741 | case 'p': | |
5742 | dl->pretty_output = true; | |
5743 | break; | |
5744 | case 'v': | |
5745 | dl->verbose = true; | |
e3d0f0c0 | 5746 | break; |
a3c4b484 JP |
5747 | default: |
5748 | pr_err("Unknown option.\n"); | |
5749 | help(); | |
b77c77d2 LR |
5750 | ret = EXIT_FAILURE; |
5751 | goto dl_free; | |
a3c4b484 JP |
5752 | } |
5753 | } | |
5754 | ||
5755 | argc -= optind; | |
5756 | argv += optind; | |
5757 | ||
3e897912 | 5758 | err = dl_init(dl); |
a3c4b484 JP |
5759 | if (err) { |
5760 | ret = EXIT_FAILURE; | |
5761 | goto dl_free; | |
5762 | } | |
5763 | ||
3e897912 IV |
5764 | if (batch_file) |
5765 | err = dl_batch(dl, batch_file, force); | |
5766 | else | |
5767 | err = dl_cmd(dl, argc, argv); | |
5768 | ||
a3c4b484 JP |
5769 | if (err) { |
5770 | ret = EXIT_FAILURE; | |
5771 | goto dl_fini; | |
5772 | } | |
5773 | ||
5774 | ret = EXIT_SUCCESS; | |
5775 | ||
5776 | dl_fini: | |
5777 | dl_fini(dl); | |
5778 | dl_free: | |
5779 | dl_free(dl); | |
5780 | ||
5781 | return ret; | |
5782 | } |