]>
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, |
74bd75c2 LR |
452 | }; |
453 | ||
6ae54b13 | 454 | int rd_attr_check(const struct nlattr *attr, int *typep) |
33115275 SW |
455 | { |
456 | int type; | |
457 | ||
458 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0) | |
459 | return MNL_CB_ERROR; | |
460 | ||
461 | type = mnl_attr_get_type(attr); | |
462 | ||
463 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
464 | return MNL_CB_ERROR; | |
465 | ||
466 | *typep = nldev_policy[type]; | |
467 | return MNL_CB_OK; | |
468 | } | |
469 | ||
74bd75c2 LR |
470 | int rd_attr_cb(const struct nlattr *attr, void *data) |
471 | { | |
472 | const struct nlattr **tb = data; | |
473 | int type; | |
474 | ||
f0cabaca LR |
475 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX - 1) < 0) |
476 | /* We received unknown attribute */ | |
fda0a61d | 477 | return MNL_CB_OK; |
74bd75c2 LR |
478 | |
479 | type = mnl_attr_get_type(attr); | |
480 | ||
481 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
482 | return MNL_CB_ERROR; | |
483 | ||
484 | tb[type] = attr; | |
485 | return MNL_CB_OK; | |
486 | } | |
487 | ||
488 | int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data) | |
489 | { | |
490 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
491 | struct dev_map *dev_map; | |
492 | struct rd *rd = data; | |
493 | const char *dev_name; | |
494 | ||
495 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
496 | if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) | |
497 | return MNL_CB_ERROR; | |
498 | if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { | |
499 | pr_err("This tool doesn't support switches yet\n"); | |
500 | return MNL_CB_ERROR; | |
501 | } | |
502 | ||
503 | dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
504 | ||
505 | dev_map = dev_map_alloc(dev_name); | |
506 | if (!dev_map) | |
507 | /* The main function will cleanup the allocations */ | |
508 | return MNL_CB_ERROR; | |
509 | list_add_tail(&dev_map->list, &rd->dev_map_list); | |
510 | ||
511 | dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
512 | dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
513 | return MNL_CB_OK; | |
514 | } | |
515 | ||
5fc17280 | 516 | void rd_free(struct rd *rd) |
74bd75c2 LR |
517 | { |
518 | if (!rd) | |
519 | return; | |
5fc17280 | 520 | free(rd->buff); |
74bd75c2 | 521 | dev_map_cleanup(rd); |
1174be72 | 522 | filters_cleanup(rd); |
74bd75c2 LR |
523 | } |
524 | ||
6e0de6e8 LR |
525 | int rd_set_arg_to_devname(struct rd *rd) |
526 | { | |
527 | int ret = 0; | |
528 | ||
529 | while (!rd_no_arg(rd)) { | |
530 | if (rd_argv_match(rd, "dev") || rd_argv_match(rd, "link")) { | |
531 | rd_arg_inc(rd); | |
532 | if (rd_no_arg(rd)) { | |
533 | pr_err("No device name was supplied\n"); | |
534 | ret = -EINVAL; | |
535 | } | |
536 | goto out; | |
537 | } | |
538 | rd_arg_inc(rd); | |
539 | } | |
540 | out: | |
541 | return ret; | |
542 | } | |
543 | ||
6416d1a0 | 544 | int rd_exec_link(struct rd *rd, int (*cb)(struct rd *rd), bool strict_port) |
874c734c LR |
545 | { |
546 | struct dev_map *dev_map; | |
547 | uint32_t port; | |
548 | int ret = 0; | |
549 | ||
550 | if (rd->json_output) | |
551 | jsonw_start_array(rd->jw); | |
552 | if (rd_no_arg(rd)) { | |
553 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
554 | rd->dev_idx = dev_map->idx; | |
6416d1a0 LR |
555 | port = (strict_port) ? 1 : 0; |
556 | for (; port < dev_map->num_ports + 1; port++) { | |
874c734c LR |
557 | rd->port_idx = port; |
558 | ret = cb(rd); | |
559 | if (ret) | |
560 | goto out; | |
561 | } | |
562 | } | |
563 | ||
564 | } else { | |
6416d1a0 LR |
565 | bool is_dump_all; |
566 | ||
874c734c | 567 | dev_map = dev_map_lookup(rd, true); |
6416d1a0 LR |
568 | ret = get_port_from_argv(rd, &port, &is_dump_all, strict_port); |
569 | if (!dev_map || port > dev_map->num_ports || (!port && ret)) { | |
874c734c LR |
570 | pr_err("Wrong device name\n"); |
571 | ret = -ENOENT; | |
572 | goto out; | |
573 | } | |
574 | rd_arg_inc(rd); | |
575 | rd->dev_idx = dev_map->idx; | |
6416d1a0 | 576 | rd->port_idx = port; |
874c734c LR |
577 | for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) { |
578 | ret = cb(rd); | |
579 | if (ret) | |
580 | goto out; | |
6416d1a0 | 581 | if (!is_dump_all) |
874c734c LR |
582 | /* |
583 | * We got request to show link for devname | |
584 | * with port index. | |
585 | */ | |
586 | break; | |
587 | } | |
588 | } | |
589 | ||
590 | out: | |
591 | if (rd->json_output) | |
592 | jsonw_end_array(rd->jw); | |
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 | ||
601 | if (rd->json_output) | |
602 | jsonw_start_array(rd->jw); | |
603 | if (rd_no_arg(rd)) { | |
604 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
605 | rd->dev_idx = dev_map->idx; | |
606 | ret = cb(rd); | |
607 | if (ret) | |
608 | goto out; | |
609 | } | |
610 | } else { | |
611 | dev_map = dev_map_lookup(rd, false); | |
612 | if (!dev_map) { | |
b5564441 | 613 | pr_err("Wrong device name - %s\n", rd_argv(rd)); |
8b92a2c9 LR |
614 | ret = -ENOENT; |
615 | goto out; | |
616 | } | |
617 | rd_arg_inc(rd); | |
618 | rd->dev_idx = dev_map->idx; | |
619 | ret = cb(rd); | |
620 | } | |
621 | out: | |
622 | if (rd->json_output) | |
623 | jsonw_end_array(rd->jw); | |
624 | return ret; | |
625 | } | |
626 | ||
a14ceed3 LR |
627 | int rd_exec_require_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
628 | { | |
629 | if (rd_no_arg(rd)) { | |
630 | pr_err("Please provide device name.\n"); | |
631 | return -EINVAL; | |
632 | } | |
633 | ||
634 | return rd_exec_dev(rd, cb); | |
635 | } | |
636 | ||
74bd75c2 LR |
637 | int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str) |
638 | { | |
639 | const struct rd_cmd *c; | |
640 | ||
641 | /* First argument in objs table is default variant */ | |
642 | if (rd_no_arg(rd)) | |
643 | return cmds->func(rd); | |
644 | ||
645 | for (c = cmds + 1; c->cmd; ++c) { | |
646 | if (rd_argv_match(rd, c->cmd)) { | |
647 | /* Move to next argument */ | |
648 | rd_arg_inc(rd); | |
649 | return c->func(rd); | |
650 | } | |
651 | } | |
652 | ||
653 | pr_err("Unknown %s '%s'.\n", str, rd_argv(rd)); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags) | |
658 | { | |
659 | *seq = time(NULL); | |
660 | ||
661 | rd->nlh = mnl_nlmsg_put_header(rd->buff); | |
662 | rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd); | |
663 | rd->nlh->nlmsg_seq = *seq; | |
664 | rd->nlh->nlmsg_flags = flags; | |
665 | } | |
666 | ||
667 | int rd_send_msg(struct rd *rd) | |
668 | { | |
669 | int ret; | |
670 | ||
671 | rd->nl = mnl_socket_open(NETLINK_RDMA); | |
672 | if (!rd->nl) { | |
673 | pr_err("Failed to open NETLINK_RDMA socket\n"); | |
674 | return -ENODEV; | |
675 | } | |
676 | ||
677 | ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID); | |
678 | if (ret < 0) { | |
679 | pr_err("Failed to bind socket with err %d\n", ret); | |
680 | goto err; | |
681 | } | |
682 | ||
683 | ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len); | |
684 | if (ret < 0) { | |
685 | pr_err("Failed to send to socket with err %d\n", ret); | |
686 | goto err; | |
687 | } | |
688 | return 0; | |
689 | ||
690 | err: | |
691 | mnl_socket_close(rd->nl); | |
692 | return ret; | |
693 | } | |
694 | ||
695 | int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq) | |
696 | { | |
697 | int ret; | |
698 | unsigned int portid; | |
699 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
700 | ||
701 | portid = mnl_socket_get_portid(rd->nl); | |
702 | do { | |
703 | ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf)); | |
704 | if (ret <= 0) | |
705 | break; | |
706 | ||
707 | ret = mnl_cb_run(buf, ret, seq, portid, callback, data); | |
708 | } while (ret > 0); | |
709 | ||
8f5cfd23 SW |
710 | if (ret < 0 && !rd->suppress_errors) |
711 | perror("error"); | |
712 | ||
74bd75c2 LR |
713 | mnl_socket_close(rd->nl); |
714 | return ret; | |
715 | } | |
40df8263 | 716 | |
8f5cfd23 SW |
717 | static int null_cb(const struct nlmsghdr *nlh, void *data) |
718 | { | |
719 | return MNL_CB_OK; | |
720 | } | |
721 | ||
722 | int rd_sendrecv_msg(struct rd *rd, unsigned int seq) | |
723 | { | |
724 | int ret; | |
725 | ||
726 | ret = rd_send_msg(rd); | |
727 | if (!ret) | |
728 | ret = rd_recv_msg(rd, null_cb, rd, seq); | |
729 | return ret; | |
730 | } | |
731 | ||
0fc8c30b | 732 | static struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name) |
40df8263 LR |
733 | { |
734 | struct dev_map *dev_map; | |
735 | ||
736 | list_for_each_entry(dev_map, &rd->dev_map_list, list) | |
737 | if (strcmp(dev_name, dev_map->dev_name) == 0) | |
738 | return dev_map; | |
739 | ||
740 | return NULL; | |
741 | } | |
742 | ||
743 | struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index) | |
744 | { | |
745 | struct dev_map *dev_map; | |
746 | char *dev_name; | |
747 | char *slash; | |
748 | ||
99da9032 LR |
749 | if (rd_no_arg(rd)) |
750 | return NULL; | |
751 | ||
40df8263 LR |
752 | dev_name = strdup(rd_argv(rd)); |
753 | if (allow_port_index) { | |
754 | slash = strrchr(dev_name, '/'); | |
755 | if (slash) | |
756 | *slash = '\0'; | |
757 | } | |
758 | ||
759 | dev_map = _dev_map_lookup(rd, dev_name); | |
760 | free(dev_name); | |
761 | return dev_map; | |
762 | } | |
33115275 SW |
763 | |
764 | #define nla_type(attr) ((attr)->nla_type & NLA_TYPE_MASK) | |
765 | ||
766 | void newline(struct rd *rd) | |
767 | { | |
768 | if (rd->json_output) | |
769 | jsonw_end_array(rd->jw); | |
770 | else | |
771 | pr_out("\n"); | |
772 | } | |
773 | ||
6ae54b13 | 774 | void newline_indent(struct rd *rd) |
33115275 SW |
775 | { |
776 | newline(rd); | |
777 | if (!rd->json_output) | |
778 | pr_out(" "); | |
779 | } | |
780 | ||
781 | static int print_driver_string(struct rd *rd, const char *key_str, | |
782 | const char *val_str) | |
783 | { | |
784 | if (rd->json_output) { | |
785 | jsonw_string_field(rd->jw, key_str, val_str); | |
786 | return 0; | |
787 | } else { | |
788 | return pr_out("%s %s ", key_str, val_str); | |
789 | } | |
790 | } | |
791 | ||
792 | static int print_driver_s32(struct rd *rd, const char *key_str, int32_t val, | |
793 | enum rdma_nldev_print_type print_type) | |
794 | { | |
795 | if (rd->json_output) { | |
796 | jsonw_int_field(rd->jw, key_str, val); | |
797 | return 0; | |
798 | } | |
799 | switch (print_type) { | |
800 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
801 | return pr_out("%s %d ", key_str, val); | |
802 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
803 | return pr_out("%s 0x%x ", key_str, val); | |
804 | default: | |
805 | return -EINVAL; | |
806 | } | |
807 | } | |
808 | ||
809 | static int print_driver_u32(struct rd *rd, const char *key_str, uint32_t val, | |
810 | enum rdma_nldev_print_type print_type) | |
811 | { | |
812 | if (rd->json_output) { | |
813 | jsonw_int_field(rd->jw, key_str, val); | |
814 | return 0; | |
815 | } | |
816 | switch (print_type) { | |
817 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
818 | return pr_out("%s %u ", key_str, val); | |
819 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
820 | return pr_out("%s 0x%x ", key_str, val); | |
821 | default: | |
822 | return -EINVAL; | |
823 | } | |
824 | } | |
825 | ||
826 | static int print_driver_s64(struct rd *rd, const char *key_str, int64_t val, | |
827 | enum rdma_nldev_print_type print_type) | |
828 | { | |
829 | if (rd->json_output) { | |
830 | jsonw_int_field(rd->jw, key_str, val); | |
831 | return 0; | |
832 | } | |
833 | switch (print_type) { | |
834 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
835 | return pr_out("%s %" PRId64 " ", key_str, val); | |
836 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
837 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
838 | default: | |
839 | return -EINVAL; | |
840 | } | |
841 | } | |
842 | ||
843 | static int print_driver_u64(struct rd *rd, const char *key_str, uint64_t val, | |
844 | enum rdma_nldev_print_type print_type) | |
845 | { | |
846 | if (rd->json_output) { | |
847 | jsonw_int_field(rd->jw, key_str, val); | |
848 | return 0; | |
849 | } | |
850 | switch (print_type) { | |
851 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
852 | return pr_out("%s %" PRIu64 " ", key_str, val); | |
853 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
854 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
855 | default: | |
856 | return -EINVAL; | |
857 | } | |
858 | } | |
859 | ||
860 | static int print_driver_entry(struct rd *rd, struct nlattr *key_attr, | |
861 | struct nlattr *val_attr, | |
862 | enum rdma_nldev_print_type print_type) | |
863 | { | |
33115275 | 864 | int attr_type = nla_type(val_attr); |
359f69f7 LR |
865 | int ret = -EINVAL; |
866 | char *key_str; | |
867 | ||
868 | if (asprintf(&key_str, "drv_%s", mnl_attr_get_str(key_attr)) == -1) | |
869 | return -ENOMEM; | |
33115275 SW |
870 | |
871 | switch (attr_type) { | |
872 | case RDMA_NLDEV_ATTR_DRIVER_STRING: | |
359f69f7 LR |
873 | ret = print_driver_string(rd, key_str, |
874 | mnl_attr_get_str(val_attr)); | |
875 | break; | |
33115275 | 876 | case RDMA_NLDEV_ATTR_DRIVER_S32: |
359f69f7 LR |
877 | ret = print_driver_s32(rd, key_str, mnl_attr_get_u32(val_attr), |
878 | print_type); | |
879 | break; | |
33115275 | 880 | case RDMA_NLDEV_ATTR_DRIVER_U32: |
359f69f7 LR |
881 | ret = print_driver_u32(rd, key_str, mnl_attr_get_u32(val_attr), |
882 | print_type); | |
883 | break; | |
33115275 | 884 | case RDMA_NLDEV_ATTR_DRIVER_S64: |
359f69f7 LR |
885 | ret = print_driver_s64(rd, key_str, mnl_attr_get_u64(val_attr), |
886 | print_type); | |
887 | break; | |
33115275 | 888 | case RDMA_NLDEV_ATTR_DRIVER_U64: |
359f69f7 LR |
889 | ret = print_driver_u64(rd, key_str, mnl_attr_get_u64(val_attr), |
890 | print_type); | |
891 | break; | |
33115275 | 892 | } |
359f69f7 LR |
893 | free(key_str); |
894 | return ret; | |
33115275 SW |
895 | } |
896 | ||
897 | void print_driver_table(struct rd *rd, struct nlattr *tb) | |
898 | { | |
899 | int print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
900 | struct nlattr *tb_entry, *key = NULL, *val; | |
901 | int type, cc = 0; | |
902 | int ret; | |
903 | ||
904 | if (!rd->show_driver_details || !tb) | |
905 | return; | |
906 | ||
907 | if (rd->pretty_output) | |
908 | newline_indent(rd); | |
909 | ||
910 | /* | |
911 | * Driver attrs are tuples of {key, [print-type], value}. | |
912 | * The key must be a string. If print-type is present, it | |
913 | * defines an alternate printf format type vs the native format | |
914 | * for the attribute. And the value can be any available | |
915 | * driver type. | |
916 | */ | |
917 | mnl_attr_for_each_nested(tb_entry, tb) { | |
918 | ||
919 | if (cc > MAX_LINE_LENGTH) { | |
920 | if (rd->pretty_output) | |
921 | newline_indent(rd); | |
922 | cc = 0; | |
923 | } | |
924 | if (rd_attr_check(tb_entry, &type) != MNL_CB_OK) | |
925 | return; | |
926 | if (!key) { | |
927 | if (type != MNL_TYPE_NUL_STRING) | |
928 | return; | |
929 | key = tb_entry; | |
930 | } else if (type == MNL_TYPE_U8) { | |
931 | print_type = mnl_attr_get_u8(tb_entry); | |
932 | } else { | |
933 | val = tb_entry; | |
934 | ret = print_driver_entry(rd, key, val, print_type); | |
935 | if (ret < 0) | |
936 | return; | |
937 | cc += ret; | |
938 | print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
939 | key = NULL; | |
940 | } | |
941 | } | |
942 | return; | |
943 | } |