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