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