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