]>
Commit | Line | Data |
---|---|---|
835d8321 | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
74bd75c2 LR |
2 | /* |
3 | * utils.c RDMA tool | |
74bd75c2 LR |
4 | * Authors: Leon Romanovsky <leonro@mellanox.com> |
5 | */ | |
6 | ||
7 | #include "rdma.h" | |
6416d1a0 | 8 | #include <ctype.h> |
33115275 | 9 | #include <inttypes.h> |
74bd75c2 | 10 | |
5f4892e2 | 11 | int rd_argc(struct rd *rd) |
74bd75c2 LR |
12 | { |
13 | return rd->argc; | |
14 | } | |
15 | ||
6ae54b13 | 16 | char *rd_argv(struct rd *rd) |
74bd75c2 LR |
17 | { |
18 | if (!rd_argc(rd)) | |
19 | return NULL; | |
20 | return *rd->argv; | |
21 | } | |
22 | ||
6ae54b13 | 23 | int strcmpx(const char *str1, const char *str2) |
74bd75c2 LR |
24 | { |
25 | if (strlen(str1) > strlen(str2)) | |
26 | return -1; | |
27 | return strncmp(str1, str2, strlen(str1)); | |
28 | } | |
29 | ||
30 | static bool rd_argv_match(struct rd *rd, const char *pattern) | |
31 | { | |
32 | if (!rd_argc(rd)) | |
33 | return false; | |
34 | return strcmpx(rd_argv(rd), pattern) == 0; | |
35 | } | |
36 | ||
6ae54b13 | 37 | void rd_arg_inc(struct rd *rd) |
74bd75c2 LR |
38 | { |
39 | if (!rd_argc(rd)) | |
40 | return; | |
41 | rd->argc--; | |
42 | rd->argv++; | |
43 | } | |
44 | ||
6ae54b13 | 45 | bool rd_no_arg(struct rd *rd) |
74bd75c2 LR |
46 | { |
47 | return rd_argc(rd) == 0; | |
48 | } | |
49 | ||
6416d1a0 LR |
50 | /* |
51 | * Possible input:output | |
52 | * dev/port | first port | is_dump_all | |
53 | * mlx5_1 | 0 | true | |
54 | * mlx5_1/ | 0 | true | |
55 | * mlx5_1/0 | 0 | false | |
56 | * mlx5_1/1 | 1 | false | |
57 | * mlx5_1/- | 0 | false | |
58 | * | |
cbe10b4e | 59 | * In strict port mode, a non-0 port must be provided |
6416d1a0 LR |
60 | */ |
61 | static int get_port_from_argv(struct rd *rd, uint32_t *port, | |
62 | bool *is_dump_all, bool strict_port) | |
40df8263 LR |
63 | { |
64 | char *slash; | |
65 | ||
6416d1a0 | 66 | *port = 0; |
cbe10b4e | 67 | *is_dump_all = strict_port ? false : true; |
6416d1a0 | 68 | |
40df8263 LR |
69 | slash = strchr(rd_argv(rd), '/'); |
70 | /* if no port found, return 0 */ | |
6416d1a0 LR |
71 | if (slash++) { |
72 | if (*slash == '-') { | |
73 | if (strict_port) | |
74 | return -EINVAL; | |
75 | *is_dump_all = false; | |
76 | return 0; | |
77 | } | |
78 | ||
79 | if (isdigit(*slash)) { | |
80 | *is_dump_all = false; | |
81 | *port = atoi(slash); | |
82 | } | |
83 | if (!*port && strlen(slash)) | |
84 | return -EINVAL; | |
85 | } | |
cbe10b4e MZ |
86 | if (strict_port && (*port == 0)) |
87 | return -EINVAL; | |
88 | ||
6416d1a0 | 89 | return 0; |
40df8263 LR |
90 | } |
91 | ||
74bd75c2 LR |
92 | static struct dev_map *dev_map_alloc(const char *dev_name) |
93 | { | |
94 | struct dev_map *dev_map; | |
95 | ||
96 | dev_map = calloc(1, sizeof(*dev_map)); | |
97 | if (!dev_map) | |
98 | return NULL; | |
99 | dev_map->dev_name = strdup(dev_name); | |
5f826553 LR |
100 | if (!dev_map->dev_name) { |
101 | free(dev_map); | |
102 | return NULL; | |
103 | } | |
74bd75c2 LR |
104 | |
105 | return dev_map; | |
106 | } | |
107 | ||
74bd75c2 LR |
108 | static void dev_map_cleanup(struct rd *rd) |
109 | { | |
110 | struct dev_map *dev_map, *tmp; | |
111 | ||
112 | list_for_each_entry_safe(dev_map, tmp, | |
113 | &rd->dev_map_list, list) { | |
114 | list_del(&dev_map->list); | |
b1e6bc43 LR |
115 | free(dev_map->dev_name); |
116 | free(dev_map); | |
74bd75c2 LR |
117 | } |
118 | } | |
119 | ||
1174be72 LR |
120 | static int add_filter(struct rd *rd, char *key, char *value, |
121 | const struct filters valid_filters[]) | |
122 | { | |
123 | char cset[] = "1234567890,-"; | |
124 | struct filter_entry *fe; | |
125 | bool key_found = false; | |
126 | int idx = 0; | |
5a823593 | 127 | char *endp; |
1174be72 LR |
128 | int ret; |
129 | ||
130 | fe = calloc(1, sizeof(*fe)); | |
131 | if (!fe) | |
132 | return -ENOMEM; | |
133 | ||
134 | while (idx < MAX_NUMBER_OF_FILTERS && valid_filters[idx].name) { | |
135 | if (!strcmpx(key, valid_filters[idx].name)) { | |
136 | key_found = true; | |
137 | break; | |
138 | } | |
139 | idx++; | |
140 | } | |
141 | if (!key_found) { | |
142 | pr_err("Unsupported filter option: %s\n", key); | |
143 | ret = -EINVAL; | |
144 | goto err; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Check the filter validity, not optimal, but works | |
149 | * | |
150 | * Actually, there are three types of filters | |
151 | * numeric - for example PID or QPN | |
152 | * string - for example states | |
153 | * link - user requested to filter on specific link | |
154 | * e.g. mlx5_1/1, mlx5_1/-, mlx5_1 ... | |
155 | */ | |
156 | if (valid_filters[idx].is_number && | |
157 | strspn(value, cset) != strlen(value)) { | |
158 | pr_err("%s filter accepts \"%s\" characters only\n", key, cset); | |
159 | ret = -EINVAL; | |
160 | goto err; | |
161 | } | |
162 | ||
163 | fe->key = strdup(key); | |
164 | fe->value = strdup(value); | |
165 | if (!fe->key || !fe->value) { | |
166 | ret = -ENOMEM; | |
167 | goto err_alloc; | |
168 | } | |
169 | ||
5a823593 LR |
170 | errno = 0; |
171 | strtol(fe->value, &endp, 10); | |
172 | if (valid_filters[idx].is_doit && !errno && *endp == '\0') | |
173 | fe->is_doit = true; | |
174 | ||
1174be72 LR |
175 | for (idx = 0; idx < strlen(fe->value); idx++) |
176 | fe->value[idx] = tolower(fe->value[idx]); | |
177 | ||
178 | list_add_tail(&fe->list, &rd->filter_list); | |
179 | return 0; | |
180 | ||
181 | err_alloc: | |
182 | free(fe->value); | |
183 | free(fe->key); | |
184 | err: | |
185 | free(fe); | |
186 | return ret; | |
187 | } | |
188 | ||
5a823593 LR |
189 | bool rd_doit_index(struct rd *rd, uint32_t *idx) |
190 | { | |
191 | struct filter_entry *fe; | |
192 | ||
193 | list_for_each_entry(fe, &rd->filter_list, list) { | |
194 | if (fe->is_doit) { | |
195 | *idx = atoi(fe->value); | |
196 | return true; | |
197 | } | |
198 | } | |
199 | ||
200 | return false; | |
201 | } | |
202 | ||
1174be72 LR |
203 | int rd_build_filter(struct rd *rd, const struct filters valid_filters[]) |
204 | { | |
205 | int ret = 0; | |
206 | int idx = 0; | |
207 | ||
208 | if (!valid_filters || !rd_argc(rd)) | |
209 | goto out; | |
210 | ||
211 | if (rd_argc(rd) == 1) { | |
212 | pr_err("No filter data was supplied to filter option %s\n", rd_argv(rd)); | |
213 | ret = -EINVAL; | |
214 | goto out; | |
215 | } | |
216 | ||
217 | if (rd_argc(rd) % 2) { | |
218 | pr_err("There is filter option without data\n"); | |
219 | ret = -EINVAL; | |
220 | goto out; | |
221 | } | |
222 | ||
223 | while (idx != rd_argc(rd)) { | |
224 | /* | |
225 | * We can do micro-optimization and skip "dev" | |
226 | * and "link" filters, but it is not worth of it. | |
227 | */ | |
228 | ret = add_filter(rd, *(rd->argv + idx), | |
229 | *(rd->argv + idx + 1), valid_filters); | |
230 | if (ret) | |
231 | goto out; | |
232 | idx += 2; | |
233 | } | |
234 | ||
235 | out: | |
236 | return ret; | |
237 | } | |
238 | ||
78728b7e | 239 | static bool rd_check_is_key_exist(struct rd *rd, const char *key) |
1174be72 LR |
240 | { |
241 | struct filter_entry *fe; | |
242 | ||
243 | list_for_each_entry(fe, &rd->filter_list, list) { | |
244 | if (!strcmpx(fe->key, key)) | |
245 | return true; | |
246 | } | |
247 | ||
248 | return false; | |
249 | } | |
250 | ||
251 | /* | |
252 | * Check if string entry is filtered: | |
253 | * * key doesn't exist -> user didn't request -> not filtered | |
254 | */ | |
78728b7e LR |
255 | static bool rd_check_is_string_filtered(struct rd *rd, const char *key, |
256 | const char *val) | |
1174be72 LR |
257 | { |
258 | bool key_is_filtered = false; | |
259 | struct filter_entry *fe; | |
260 | char *p = NULL; | |
261 | char *str; | |
262 | ||
263 | list_for_each_entry(fe, &rd->filter_list, list) { | |
264 | if (!strcmpx(fe->key, key)) { | |
265 | /* We found the key */ | |
266 | p = strdup(fe->value); | |
267 | key_is_filtered = true; | |
268 | if (!p) { | |
269 | /* | |
270 | * Something extremely wrong if we fail | |
271 | * to allocate small amount of bytes. | |
272 | */ | |
273 | pr_err("Found key, but failed to allocate memory to store value\n"); | |
274 | return key_is_filtered; | |
275 | } | |
276 | ||
277 | /* | |
278 | * Need to check if value in range | |
279 | * It can come in the following formats | |
280 | * and their permutations: | |
281 | * str | |
282 | * str1,str2 | |
283 | */ | |
284 | str = strtok(p, ","); | |
285 | while (str) { | |
286 | if (strlen(str) == strlen(val) && | |
287 | !strcasecmp(str, val)) { | |
288 | key_is_filtered = false; | |
289 | goto out; | |
290 | } | |
291 | str = strtok(NULL, ","); | |
292 | } | |
293 | goto out; | |
294 | } | |
295 | } | |
296 | ||
297 | out: | |
298 | free(p); | |
299 | return key_is_filtered; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Check if key is filtered: | |
304 | * key doesn't exist -> user didn't request -> not filtered | |
305 | */ | |
78728b7e | 306 | static bool rd_check_is_filtered(struct rd *rd, const char *key, uint32_t val) |
1174be72 LR |
307 | { |
308 | bool key_is_filtered = false; | |
309 | struct filter_entry *fe; | |
310 | ||
311 | list_for_each_entry(fe, &rd->filter_list, list) { | |
312 | uint32_t left_val = 0, fe_value = 0; | |
313 | bool range_check = false; | |
314 | char *p = fe->value; | |
315 | ||
316 | if (!strcmpx(fe->key, key)) { | |
317 | /* We found the key */ | |
318 | key_is_filtered = true; | |
319 | /* | |
320 | * Need to check if value in range | |
321 | * It can come in the following formats | |
322 | * (and their permutations): | |
323 | * numb | |
324 | * numb1,numb2 | |
325 | * ,numb1,numb2 | |
326 | * numb1-numb2 | |
327 | * numb1,numb2-numb3,numb4-numb5 | |
328 | */ | |
329 | while (*p) { | |
330 | if (isdigit(*p)) { | |
331 | fe_value = strtol(p, &p, 10); | |
332 | if (fe_value == val || | |
333 | (range_check && left_val < val && | |
334 | val < fe_value)) { | |
335 | key_is_filtered = false; | |
336 | goto out; | |
337 | } | |
338 | range_check = false; | |
339 | } else { | |
340 | if (*p == '-') { | |
341 | left_val = fe_value; | |
342 | range_check = true; | |
343 | } | |
344 | p++; | |
345 | } | |
346 | } | |
347 | goto out; | |
348 | } | |
349 | } | |
350 | ||
351 | out: | |
352 | return key_is_filtered; | |
353 | } | |
354 | ||
78728b7e LR |
355 | bool rd_is_filtered_attr(struct rd *rd, const char *key, uint32_t val, |
356 | struct nlattr *attr) | |
357 | { | |
358 | if (!attr) | |
359 | return rd_check_is_key_exist(rd, key); | |
360 | ||
361 | return rd_check_is_filtered(rd, key, val); | |
362 | } | |
363 | ||
364 | bool rd_is_string_filtered_attr(struct rd *rd, const char *key, const char *val, | |
365 | struct nlattr *attr) | |
366 | { | |
367 | if (!attr) | |
368 | rd_check_is_key_exist(rd, key); | |
369 | ||
370 | return rd_check_is_string_filtered(rd, key, val); | |
371 | } | |
372 | ||
1174be72 LR |
373 | static void filters_cleanup(struct rd *rd) |
374 | { | |
375 | struct filter_entry *fe, *tmp; | |
376 | ||
377 | list_for_each_entry_safe(fe, tmp, | |
378 | &rd->filter_list, list) { | |
379 | list_del(&fe->list); | |
380 | free(fe->key); | |
381 | free(fe->value); | |
382 | free(fe); | |
383 | } | |
384 | } | |
385 | ||
74bd75c2 | 386 | static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = { |
40df8263 | 387 | [RDMA_NLDEV_ATTR_DEV_INDEX] = MNL_TYPE_U32, |
74bd75c2 LR |
388 | [RDMA_NLDEV_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING, |
389 | [RDMA_NLDEV_ATTR_PORT_INDEX] = MNL_TYPE_U32, | |
40df8263 LR |
390 | [RDMA_NLDEV_ATTR_CAP_FLAGS] = MNL_TYPE_U64, |
391 | [RDMA_NLDEV_ATTR_FW_VERSION] = MNL_TYPE_NUL_STRING, | |
392 | [RDMA_NLDEV_ATTR_NODE_GUID] = MNL_TYPE_U64, | |
393 | [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = MNL_TYPE_U64, | |
da990ab4 LR |
394 | [RDMA_NLDEV_ATTR_LID] = MNL_TYPE_U32, |
395 | [RDMA_NLDEV_ATTR_SM_LID] = MNL_TYPE_U32, | |
396 | [RDMA_NLDEV_ATTR_LMC] = MNL_TYPE_U8, | |
397 | [RDMA_NLDEV_ATTR_PORT_STATE] = MNL_TYPE_U8, | |
398 | [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = MNL_TYPE_U8, | |
40df8263 | 399 | [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = MNL_TYPE_U8, |
923aa825 LR |
400 | [RDMA_NLDEV_ATTR_RES_SUMMARY] = MNL_TYPE_NESTED, |
401 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = MNL_TYPE_NESTED, | |
402 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = MNL_TYPE_NUL_STRING, | |
403 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = MNL_TYPE_U64, | |
8ecac46a LR |
404 | [RDMA_NLDEV_ATTR_RES_QP] = MNL_TYPE_NESTED, |
405 | [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = MNL_TYPE_NESTED, | |
406 | [RDMA_NLDEV_ATTR_RES_LQPN] = MNL_TYPE_U32, | |
407 | [RDMA_NLDEV_ATTR_RES_RQPN] = MNL_TYPE_U32, | |
408 | [RDMA_NLDEV_ATTR_RES_RQ_PSN] = MNL_TYPE_U32, | |
409 | [RDMA_NLDEV_ATTR_RES_SQ_PSN] = MNL_TYPE_U32, | |
410 | [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = MNL_TYPE_U8, | |
411 | [RDMA_NLDEV_ATTR_RES_TYPE] = MNL_TYPE_U8, | |
412 | [RDMA_NLDEV_ATTR_RES_STATE] = MNL_TYPE_U8, | |
413 | [RDMA_NLDEV_ATTR_RES_PID] = MNL_TYPE_U32, | |
414 | [RDMA_NLDEV_ATTR_RES_KERN_NAME] = MNL_TYPE_NUL_STRING, | |
9a362cc7 SW |
415 | [RDMA_NLDEV_ATTR_RES_CM_ID] = MNL_TYPE_NESTED, |
416 | [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = MNL_TYPE_NESTED, | |
417 | [RDMA_NLDEV_ATTR_RES_PS] = MNL_TYPE_U32, | |
418 | [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = MNL_TYPE_UNSPEC, | |
419 | [RDMA_NLDEV_ATTR_RES_DST_ADDR] = MNL_TYPE_UNSPEC, | |
b0b8e32c SW |
420 | [RDMA_NLDEV_ATTR_RES_CQ] = MNL_TYPE_NESTED, |
421 | [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = MNL_TYPE_NESTED, | |
422 | [RDMA_NLDEV_ATTR_RES_CQE] = MNL_TYPE_U32, | |
423 | [RDMA_NLDEV_ATTR_RES_USECNT] = MNL_TYPE_U64, | |
424 | [RDMA_NLDEV_ATTR_RES_POLL_CTX] = MNL_TYPE_U8, | |
8958a15c SW |
425 | [RDMA_NLDEV_ATTR_RES_MR] = MNL_TYPE_NESTED, |
426 | [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = MNL_TYPE_NESTED, | |
427 | [RDMA_NLDEV_ATTR_RES_RKEY] = MNL_TYPE_U32, | |
428 | [RDMA_NLDEV_ATTR_RES_LKEY] = MNL_TYPE_U32, | |
429 | [RDMA_NLDEV_ATTR_RES_IOVA] = MNL_TYPE_U64, | |
430 | [RDMA_NLDEV_ATTR_RES_MRLEN] = MNL_TYPE_U64, | |
15259427 LR |
431 | [RDMA_NLDEV_ATTR_NDEV_INDEX] = MNL_TYPE_U32, |
432 | [RDMA_NLDEV_ATTR_NDEV_NAME] = MNL_TYPE_NUL_STRING, | |
33115275 SW |
433 | [RDMA_NLDEV_ATTR_DRIVER] = MNL_TYPE_NESTED, |
434 | [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = MNL_TYPE_NESTED, | |
435 | [RDMA_NLDEV_ATTR_DRIVER_STRING] = MNL_TYPE_NUL_STRING, | |
436 | [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = MNL_TYPE_U8, | |
437 | [RDMA_NLDEV_ATTR_DRIVER_S32] = MNL_TYPE_U32, | |
438 | [RDMA_NLDEV_ATTR_DRIVER_U32] = MNL_TYPE_U32, | |
439 | [RDMA_NLDEV_ATTR_DRIVER_S64] = MNL_TYPE_U64, | |
440 | [RDMA_NLDEV_ATTR_DRIVER_U64] = MNL_TYPE_U64, | |
c4572a46 | 441 | [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = MNL_TYPE_U8, |
5937552b MZ |
442 | [RDMA_NLDEV_ATTR_STAT_COUNTER] = MNL_TYPE_NESTED, |
443 | [RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY] = MNL_TYPE_NESTED, | |
444 | [RDMA_NLDEV_ATTR_STAT_COUNTER_ID] = MNL_TYPE_U32, | |
445 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTERS] = MNL_TYPE_NESTED, | |
446 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY] = MNL_TYPE_NESTED, | |
447 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = MNL_TYPE_NUL_STRING, | |
448 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = MNL_TYPE_U64, | |
1b2ca7ad MZ |
449 | [RDMA_NLDEV_ATTR_STAT_MODE] = MNL_TYPE_U32, |
450 | [RDMA_NLDEV_ATTR_STAT_RES] = MNL_TYPE_U32, | |
887fc739 | 451 | [RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK] = MNL_TYPE_U32, |
8a56ef32 | 452 | [RDMA_NLDEV_ATTR_DEV_DIM] = MNL_TYPE_U8, |
7c01e0fc | 453 | [RDMA_NLDEV_ATTR_RES_RAW] = MNL_TYPE_BINARY, |
74bd75c2 LR |
454 | }; |
455 | ||
6ae54b13 | 456 | int rd_attr_check(const struct nlattr *attr, int *typep) |
33115275 SW |
457 | { |
458 | int type; | |
459 | ||
460 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0) | |
461 | return MNL_CB_ERROR; | |
462 | ||
463 | type = mnl_attr_get_type(attr); | |
464 | ||
465 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
466 | return MNL_CB_ERROR; | |
467 | ||
468 | *typep = nldev_policy[type]; | |
469 | return MNL_CB_OK; | |
470 | } | |
471 | ||
74bd75c2 LR |
472 | int rd_attr_cb(const struct nlattr *attr, void *data) |
473 | { | |
474 | const struct nlattr **tb = data; | |
475 | int type; | |
476 | ||
f0cabaca LR |
477 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX - 1) < 0) |
478 | /* We received unknown attribute */ | |
fda0a61d | 479 | return MNL_CB_OK; |
74bd75c2 LR |
480 | |
481 | type = mnl_attr_get_type(attr); | |
482 | ||
483 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
484 | return MNL_CB_ERROR; | |
485 | ||
486 | tb[type] = attr; | |
487 | return MNL_CB_OK; | |
488 | } | |
489 | ||
490 | int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data) | |
491 | { | |
492 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
493 | struct dev_map *dev_map; | |
494 | struct rd *rd = data; | |
495 | const char *dev_name; | |
496 | ||
497 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
498 | if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) | |
499 | return MNL_CB_ERROR; | |
500 | if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { | |
501 | pr_err("This tool doesn't support switches yet\n"); | |
502 | return MNL_CB_ERROR; | |
503 | } | |
504 | ||
505 | dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
506 | ||
507 | dev_map = dev_map_alloc(dev_name); | |
508 | if (!dev_map) | |
509 | /* The main function will cleanup the allocations */ | |
510 | return MNL_CB_ERROR; | |
511 | list_add_tail(&dev_map->list, &rd->dev_map_list); | |
512 | ||
513 | dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
514 | dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
515 | return MNL_CB_OK; | |
516 | } | |
517 | ||
5fc17280 | 518 | void rd_free(struct rd *rd) |
74bd75c2 LR |
519 | { |
520 | if (!rd) | |
521 | return; | |
5fc17280 | 522 | free(rd->buff); |
74bd75c2 | 523 | dev_map_cleanup(rd); |
1174be72 | 524 | filters_cleanup(rd); |
74bd75c2 LR |
525 | } |
526 | ||
6e0de6e8 LR |
527 | int rd_set_arg_to_devname(struct rd *rd) |
528 | { | |
529 | int ret = 0; | |
530 | ||
531 | while (!rd_no_arg(rd)) { | |
532 | if (rd_argv_match(rd, "dev") || rd_argv_match(rd, "link")) { | |
533 | rd_arg_inc(rd); | |
534 | if (rd_no_arg(rd)) { | |
535 | pr_err("No device name was supplied\n"); | |
536 | ret = -EINVAL; | |
537 | } | |
538 | goto out; | |
539 | } | |
540 | rd_arg_inc(rd); | |
541 | } | |
542 | out: | |
543 | return ret; | |
544 | } | |
545 | ||
6416d1a0 | 546 | int rd_exec_link(struct rd *rd, int (*cb)(struct rd *rd), bool strict_port) |
874c734c LR |
547 | { |
548 | struct dev_map *dev_map; | |
549 | uint32_t port; | |
550 | int ret = 0; | |
551 | ||
b0a688a5 | 552 | new_json_obj(rd->json_output); |
874c734c LR |
553 | if (rd_no_arg(rd)) { |
554 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
555 | rd->dev_idx = dev_map->idx; | |
6416d1a0 LR |
556 | port = (strict_port) ? 1 : 0; |
557 | for (; port < dev_map->num_ports + 1; port++) { | |
874c734c LR |
558 | rd->port_idx = port; |
559 | ret = cb(rd); | |
560 | if (ret) | |
561 | goto out; | |
562 | } | |
563 | } | |
564 | ||
565 | } else { | |
6416d1a0 LR |
566 | bool is_dump_all; |
567 | ||
874c734c | 568 | dev_map = dev_map_lookup(rd, true); |
6416d1a0 LR |
569 | ret = get_port_from_argv(rd, &port, &is_dump_all, strict_port); |
570 | if (!dev_map || port > dev_map->num_ports || (!port && ret)) { | |
874c734c LR |
571 | pr_err("Wrong device name\n"); |
572 | ret = -ENOENT; | |
573 | goto out; | |
574 | } | |
575 | rd_arg_inc(rd); | |
576 | rd->dev_idx = dev_map->idx; | |
6416d1a0 | 577 | rd->port_idx = port; |
874c734c LR |
578 | for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) { |
579 | ret = cb(rd); | |
580 | if (ret) | |
581 | goto out; | |
6416d1a0 | 582 | if (!is_dump_all) |
874c734c LR |
583 | /* |
584 | * We got request to show link for devname | |
585 | * with port index. | |
586 | */ | |
587 | break; | |
588 | } | |
589 | } | |
590 | ||
591 | out: | |
b0a688a5 | 592 | delete_json_obj(); |
874c734c LR |
593 | return ret; |
594 | } | |
595 | ||
8b92a2c9 LR |
596 | int rd_exec_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
597 | { | |
598 | struct dev_map *dev_map; | |
599 | int ret = 0; | |
600 | ||
b0a688a5 | 601 | new_json_obj(rd->json_output); |
8b92a2c9 LR |
602 | if (rd_no_arg(rd)) { |
603 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
604 | rd->dev_idx = dev_map->idx; | |
605 | ret = cb(rd); | |
606 | if (ret) | |
607 | goto out; | |
608 | } | |
609 | } else { | |
610 | dev_map = dev_map_lookup(rd, false); | |
611 | if (!dev_map) { | |
b5564441 | 612 | pr_err("Wrong device name - %s\n", rd_argv(rd)); |
8b92a2c9 LR |
613 | ret = -ENOENT; |
614 | goto out; | |
615 | } | |
616 | rd_arg_inc(rd); | |
617 | rd->dev_idx = dev_map->idx; | |
618 | ret = cb(rd); | |
619 | } | |
620 | out: | |
b0a688a5 | 621 | delete_json_obj(); |
8b92a2c9 LR |
622 | return ret; |
623 | } | |
624 | ||
a14ceed3 LR |
625 | int rd_exec_require_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
626 | { | |
627 | if (rd_no_arg(rd)) { | |
628 | pr_err("Please provide device name.\n"); | |
629 | return -EINVAL; | |
630 | } | |
631 | ||
632 | return rd_exec_dev(rd, cb); | |
633 | } | |
634 | ||
74bd75c2 LR |
635 | int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str) |
636 | { | |
637 | const struct rd_cmd *c; | |
638 | ||
639 | /* First argument in objs table is default variant */ | |
640 | if (rd_no_arg(rd)) | |
641 | return cmds->func(rd); | |
642 | ||
643 | for (c = cmds + 1; c->cmd; ++c) { | |
644 | if (rd_argv_match(rd, c->cmd)) { | |
645 | /* Move to next argument */ | |
646 | rd_arg_inc(rd); | |
647 | return c->func(rd); | |
648 | } | |
649 | } | |
650 | ||
651 | pr_err("Unknown %s '%s'.\n", str, rd_argv(rd)); | |
652 | return 0; | |
653 | } | |
654 | ||
655 | void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags) | |
656 | { | |
657 | *seq = time(NULL); | |
658 | ||
659 | rd->nlh = mnl_nlmsg_put_header(rd->buff); | |
660 | rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd); | |
661 | rd->nlh->nlmsg_seq = *seq; | |
662 | rd->nlh->nlmsg_flags = flags; | |
663 | } | |
664 | ||
665 | int rd_send_msg(struct rd *rd) | |
666 | { | |
667 | int ret; | |
668 | ||
669 | rd->nl = mnl_socket_open(NETLINK_RDMA); | |
670 | if (!rd->nl) { | |
671 | pr_err("Failed to open NETLINK_RDMA socket\n"); | |
672 | return -ENODEV; | |
673 | } | |
674 | ||
675 | ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID); | |
676 | if (ret < 0) { | |
677 | pr_err("Failed to bind socket with err %d\n", ret); | |
678 | goto err; | |
679 | } | |
680 | ||
681 | ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len); | |
682 | if (ret < 0) { | |
683 | pr_err("Failed to send to socket with err %d\n", ret); | |
684 | goto err; | |
685 | } | |
686 | return 0; | |
687 | ||
688 | err: | |
689 | mnl_socket_close(rd->nl); | |
690 | return ret; | |
691 | } | |
692 | ||
693 | int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq) | |
694 | { | |
695 | int ret; | |
696 | unsigned int portid; | |
697 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
698 | ||
699 | portid = mnl_socket_get_portid(rd->nl); | |
700 | do { | |
701 | ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf)); | |
702 | if (ret <= 0) | |
703 | break; | |
704 | ||
705 | ret = mnl_cb_run(buf, ret, seq, portid, callback, data); | |
706 | } while (ret > 0); | |
707 | ||
8f5cfd23 SW |
708 | if (ret < 0 && !rd->suppress_errors) |
709 | perror("error"); | |
710 | ||
74bd75c2 LR |
711 | mnl_socket_close(rd->nl); |
712 | return ret; | |
713 | } | |
40df8263 | 714 | |
8f5cfd23 SW |
715 | static int null_cb(const struct nlmsghdr *nlh, void *data) |
716 | { | |
717 | return MNL_CB_OK; | |
718 | } | |
719 | ||
720 | int rd_sendrecv_msg(struct rd *rd, unsigned int seq) | |
721 | { | |
722 | int ret; | |
723 | ||
724 | ret = rd_send_msg(rd); | |
725 | if (!ret) | |
726 | ret = rd_recv_msg(rd, null_cb, rd, seq); | |
727 | return ret; | |
728 | } | |
729 | ||
0fc8c30b | 730 | static struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name) |
40df8263 LR |
731 | { |
732 | struct dev_map *dev_map; | |
733 | ||
734 | list_for_each_entry(dev_map, &rd->dev_map_list, list) | |
735 | if (strcmp(dev_name, dev_map->dev_name) == 0) | |
736 | return dev_map; | |
737 | ||
738 | return NULL; | |
739 | } | |
740 | ||
741 | struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index) | |
742 | { | |
743 | struct dev_map *dev_map; | |
744 | char *dev_name; | |
745 | char *slash; | |
746 | ||
99da9032 LR |
747 | if (rd_no_arg(rd)) |
748 | return NULL; | |
749 | ||
40df8263 LR |
750 | dev_name = strdup(rd_argv(rd)); |
751 | if (allow_port_index) { | |
752 | slash = strrchr(dev_name, '/'); | |
753 | if (slash) | |
754 | *slash = '\0'; | |
755 | } | |
756 | ||
757 | dev_map = _dev_map_lookup(rd, dev_name); | |
758 | free(dev_name); | |
759 | return dev_map; | |
760 | } | |
33115275 SW |
761 | |
762 | #define nla_type(attr) ((attr)->nla_type & NLA_TYPE_MASK) | |
763 | ||
764 | void newline(struct rd *rd) | |
765 | { | |
b0a688a5 IK |
766 | close_json_object(); |
767 | print_color_string(PRINT_FP, COLOR_NONE, NULL, "\n", NULL); | |
33115275 SW |
768 | } |
769 | ||
6ae54b13 | 770 | void newline_indent(struct rd *rd) |
33115275 SW |
771 | { |
772 | newline(rd); | |
b0a688a5 | 773 | print_color_string(PRINT_FP, COLOR_NONE, NULL, " ", NULL); |
33115275 SW |
774 | } |
775 | ||
776 | static int print_driver_string(struct rd *rd, const char *key_str, | |
777 | const char *val_str) | |
778 | { | |
b0a688a5 IK |
779 | print_color_string(PRINT_ANY, COLOR_NONE, key_str, key_str, val_str); |
780 | print_color_string(PRINT_FP, COLOR_NONE, NULL, " %s ", val_str); | |
781 | return 0; | |
33115275 SW |
782 | } |
783 | ||
8a56ef32 YF |
784 | void print_on_off(struct rd *rd, const char *key_str, bool on) |
785 | { | |
786 | print_driver_string(rd, key_str, (on) ? "on":"off"); | |
787 | } | |
788 | ||
33115275 SW |
789 | static int print_driver_s32(struct rd *rd, const char *key_str, int32_t val, |
790 | enum rdma_nldev_print_type print_type) | |
791 | { | |
b0a688a5 IK |
792 | if (!rd->json_output) { |
793 | switch (print_type) { | |
794 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
795 | return pr_out("%s %d ", key_str, val); | |
796 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
797 | return pr_out("%s 0x%x ", key_str, val); | |
798 | default: | |
799 | return -EINVAL; | |
800 | } | |
33115275 | 801 | } |
b0a688a5 IK |
802 | print_color_int(PRINT_JSON, COLOR_NONE, key_str, NULL, val); |
803 | return 0; | |
33115275 SW |
804 | } |
805 | ||
806 | static int print_driver_u32(struct rd *rd, const char *key_str, uint32_t val, | |
807 | enum rdma_nldev_print_type print_type) | |
808 | { | |
b0a688a5 IK |
809 | if (!rd->json_output) { |
810 | switch (print_type) { | |
811 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
812 | return pr_out("%s %u ", key_str, val); | |
813 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
814 | return pr_out("%s 0x%x ", key_str, val); | |
815 | default: | |
816 | return -EINVAL; | |
817 | } | |
33115275 | 818 | } |
b0a688a5 IK |
819 | print_color_int(PRINT_JSON, COLOR_NONE, key_str, NULL, val); |
820 | return 0; | |
33115275 SW |
821 | } |
822 | ||
823 | static int print_driver_s64(struct rd *rd, const char *key_str, int64_t val, | |
824 | enum rdma_nldev_print_type print_type) | |
825 | { | |
b0a688a5 IK |
826 | if (!rd->json_output) { |
827 | switch (print_type) { | |
828 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
829 | return pr_out("%s %" PRId64 " ", key_str, val); | |
830 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
831 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
832 | default: | |
833 | return -EINVAL; | |
834 | } | |
33115275 | 835 | } |
b0a688a5 IK |
836 | print_color_int(PRINT_JSON, COLOR_NONE, key_str, NULL, val); |
837 | return 0; | |
33115275 SW |
838 | } |
839 | ||
840 | static int print_driver_u64(struct rd *rd, const char *key_str, uint64_t val, | |
841 | enum rdma_nldev_print_type print_type) | |
842 | { | |
b0a688a5 IK |
843 | if (!rd->json_output) { |
844 | switch (print_type) { | |
845 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
846 | return pr_out("%s %" PRIu64 " ", key_str, val); | |
847 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
848 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
849 | default: | |
850 | return -EINVAL; | |
851 | } | |
33115275 | 852 | } |
b0a688a5 IK |
853 | print_color_int(PRINT_JSON, COLOR_NONE, key_str, NULL, val); |
854 | return 0; | |
33115275 SW |
855 | } |
856 | ||
857 | static int print_driver_entry(struct rd *rd, struct nlattr *key_attr, | |
858 | struct nlattr *val_attr, | |
859 | enum rdma_nldev_print_type print_type) | |
860 | { | |
33115275 | 861 | int attr_type = nla_type(val_attr); |
359f69f7 LR |
862 | int ret = -EINVAL; |
863 | char *key_str; | |
864 | ||
865 | if (asprintf(&key_str, "drv_%s", mnl_attr_get_str(key_attr)) == -1) | |
866 | return -ENOMEM; | |
33115275 SW |
867 | |
868 | switch (attr_type) { | |
869 | case RDMA_NLDEV_ATTR_DRIVER_STRING: | |
359f69f7 LR |
870 | ret = print_driver_string(rd, key_str, |
871 | mnl_attr_get_str(val_attr)); | |
872 | break; | |
33115275 | 873 | case RDMA_NLDEV_ATTR_DRIVER_S32: |
359f69f7 LR |
874 | ret = print_driver_s32(rd, key_str, mnl_attr_get_u32(val_attr), |
875 | print_type); | |
876 | break; | |
33115275 | 877 | case RDMA_NLDEV_ATTR_DRIVER_U32: |
359f69f7 LR |
878 | ret = print_driver_u32(rd, key_str, mnl_attr_get_u32(val_attr), |
879 | print_type); | |
880 | break; | |
33115275 | 881 | case RDMA_NLDEV_ATTR_DRIVER_S64: |
359f69f7 LR |
882 | ret = print_driver_s64(rd, key_str, mnl_attr_get_u64(val_attr), |
883 | print_type); | |
884 | break; | |
33115275 | 885 | case RDMA_NLDEV_ATTR_DRIVER_U64: |
359f69f7 LR |
886 | ret = print_driver_u64(rd, key_str, mnl_attr_get_u64(val_attr), |
887 | print_type); | |
888 | break; | |
33115275 | 889 | } |
359f69f7 LR |
890 | free(key_str); |
891 | return ret; | |
33115275 SW |
892 | } |
893 | ||
7c01e0fc MG |
894 | void print_raw_data(struct rd *rd, struct nlattr **nla_line) |
895 | { | |
896 | uint8_t *data; | |
897 | uint32_t len; | |
898 | int i = 0; | |
899 | ||
900 | if (!rd->show_raw) | |
901 | return; | |
902 | ||
903 | len = mnl_attr_get_payload_len(nla_line[RDMA_NLDEV_ATTR_RES_RAW]); | |
904 | data = mnl_attr_get_payload(nla_line[RDMA_NLDEV_ATTR_RES_RAW]); | |
905 | open_json_array(PRINT_JSON, "data"); | |
906 | while (i < len) { | |
907 | print_color_uint(PRINT_ANY, COLOR_NONE, NULL, "%d", data[i]); | |
908 | i++; | |
909 | } | |
910 | close_json_array(PRINT_ANY, ">"); | |
911 | } | |
912 | ||
33115275 SW |
913 | void print_driver_table(struct rd *rd, struct nlattr *tb) |
914 | { | |
915 | int print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
916 | struct nlattr *tb_entry, *key = NULL, *val; | |
917 | int type, cc = 0; | |
918 | int ret; | |
919 | ||
920 | if (!rd->show_driver_details || !tb) | |
921 | return; | |
922 | ||
923 | if (rd->pretty_output) | |
924 | newline_indent(rd); | |
925 | ||
926 | /* | |
927 | * Driver attrs are tuples of {key, [print-type], value}. | |
928 | * The key must be a string. If print-type is present, it | |
929 | * defines an alternate printf format type vs the native format | |
930 | * for the attribute. And the value can be any available | |
931 | * driver type. | |
932 | */ | |
933 | mnl_attr_for_each_nested(tb_entry, tb) { | |
934 | ||
935 | if (cc > MAX_LINE_LENGTH) { | |
936 | if (rd->pretty_output) | |
937 | newline_indent(rd); | |
938 | cc = 0; | |
939 | } | |
940 | if (rd_attr_check(tb_entry, &type) != MNL_CB_OK) | |
941 | return; | |
942 | if (!key) { | |
943 | if (type != MNL_TYPE_NUL_STRING) | |
944 | return; | |
945 | key = tb_entry; | |
946 | } else if (type == MNL_TYPE_U8) { | |
947 | print_type = mnl_attr_get_u8(tb_entry); | |
948 | } else { | |
949 | val = tb_entry; | |
950 | ret = print_driver_entry(rd, key, val, print_type); | |
951 | if (ret < 0) | |
952 | return; | |
953 | cc += ret; | |
954 | print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
955 | key = NULL; | |
956 | } | |
957 | } | |
958 | return; | |
959 | } |