]>
Commit | Line | Data |
---|---|---|
74bd75c2 LR |
1 | /* |
2 | * utils.c RDMA 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: Leon Romanovsky <leonro@mellanox.com> | |
10 | */ | |
11 | ||
12 | #include "rdma.h" | |
6416d1a0 | 13 | #include <ctype.h> |
74bd75c2 | 14 | |
5f4892e2 | 15 | int rd_argc(struct rd *rd) |
74bd75c2 LR |
16 | { |
17 | return rd->argc; | |
18 | } | |
19 | ||
40df8263 | 20 | char *rd_argv(struct rd *rd) |
74bd75c2 LR |
21 | { |
22 | if (!rd_argc(rd)) | |
23 | return NULL; | |
24 | return *rd->argv; | |
25 | } | |
26 | ||
684d8209 | 27 | int strcmpx(const char *str1, const char *str2) |
74bd75c2 LR |
28 | { |
29 | if (strlen(str1) > strlen(str2)) | |
30 | return -1; | |
31 | return strncmp(str1, str2, strlen(str1)); | |
32 | } | |
33 | ||
34 | static bool rd_argv_match(struct rd *rd, const char *pattern) | |
35 | { | |
36 | if (!rd_argc(rd)) | |
37 | return false; | |
38 | return strcmpx(rd_argv(rd), pattern) == 0; | |
39 | } | |
40 | ||
41 | void rd_arg_inc(struct rd *rd) | |
42 | { | |
43 | if (!rd_argc(rd)) | |
44 | return; | |
45 | rd->argc--; | |
46 | rd->argv++; | |
47 | } | |
48 | ||
49 | bool rd_no_arg(struct rd *rd) | |
50 | { | |
51 | return rd_argc(rd) == 0; | |
52 | } | |
53 | ||
6416d1a0 LR |
54 | /* |
55 | * Possible input:output | |
56 | * dev/port | first port | is_dump_all | |
57 | * mlx5_1 | 0 | true | |
58 | * mlx5_1/ | 0 | true | |
59 | * mlx5_1/0 | 0 | false | |
60 | * mlx5_1/1 | 1 | false | |
61 | * mlx5_1/- | 0 | false | |
62 | * | |
63 | * In strict mode, /- will return error. | |
64 | */ | |
65 | static int get_port_from_argv(struct rd *rd, uint32_t *port, | |
66 | bool *is_dump_all, bool strict_port) | |
40df8263 LR |
67 | { |
68 | char *slash; | |
69 | ||
6416d1a0 LR |
70 | *port = 0; |
71 | *is_dump_all = true; | |
72 | ||
40df8263 LR |
73 | slash = strchr(rd_argv(rd), '/'); |
74 | /* if no port found, return 0 */ | |
6416d1a0 LR |
75 | if (slash++) { |
76 | if (*slash == '-') { | |
77 | if (strict_port) | |
78 | return -EINVAL; | |
79 | *is_dump_all = false; | |
80 | return 0; | |
81 | } | |
82 | ||
83 | if (isdigit(*slash)) { | |
84 | *is_dump_all = false; | |
85 | *port = atoi(slash); | |
86 | } | |
87 | if (!*port && strlen(slash)) | |
88 | return -EINVAL; | |
89 | } | |
90 | return 0; | |
40df8263 LR |
91 | } |
92 | ||
74bd75c2 LR |
93 | static struct dev_map *dev_map_alloc(const char *dev_name) |
94 | { | |
95 | struct dev_map *dev_map; | |
96 | ||
97 | dev_map = calloc(1, sizeof(*dev_map)); | |
98 | if (!dev_map) | |
99 | return NULL; | |
100 | dev_map->dev_name = strdup(dev_name); | |
101 | ||
102 | return dev_map; | |
103 | } | |
104 | ||
74bd75c2 LR |
105 | static void dev_map_cleanup(struct rd *rd) |
106 | { | |
107 | struct dev_map *dev_map, *tmp; | |
108 | ||
109 | list_for_each_entry_safe(dev_map, tmp, | |
110 | &rd->dev_map_list, list) { | |
111 | list_del(&dev_map->list); | |
b1e6bc43 LR |
112 | free(dev_map->dev_name); |
113 | free(dev_map); | |
74bd75c2 LR |
114 | } |
115 | } | |
116 | ||
1174be72 LR |
117 | static int add_filter(struct rd *rd, char *key, char *value, |
118 | const struct filters valid_filters[]) | |
119 | { | |
120 | char cset[] = "1234567890,-"; | |
121 | struct filter_entry *fe; | |
122 | bool key_found = false; | |
123 | int idx = 0; | |
124 | int ret; | |
125 | ||
126 | fe = calloc(1, sizeof(*fe)); | |
127 | if (!fe) | |
128 | return -ENOMEM; | |
129 | ||
130 | while (idx < MAX_NUMBER_OF_FILTERS && valid_filters[idx].name) { | |
131 | if (!strcmpx(key, valid_filters[idx].name)) { | |
132 | key_found = true; | |
133 | break; | |
134 | } | |
135 | idx++; | |
136 | } | |
137 | if (!key_found) { | |
138 | pr_err("Unsupported filter option: %s\n", key); | |
139 | ret = -EINVAL; | |
140 | goto err; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Check the filter validity, not optimal, but works | |
145 | * | |
146 | * Actually, there are three types of filters | |
147 | * numeric - for example PID or QPN | |
148 | * string - for example states | |
149 | * link - user requested to filter on specific link | |
150 | * e.g. mlx5_1/1, mlx5_1/-, mlx5_1 ... | |
151 | */ | |
152 | if (valid_filters[idx].is_number && | |
153 | strspn(value, cset) != strlen(value)) { | |
154 | pr_err("%s filter accepts \"%s\" characters only\n", key, cset); | |
155 | ret = -EINVAL; | |
156 | goto err; | |
157 | } | |
158 | ||
159 | fe->key = strdup(key); | |
160 | fe->value = strdup(value); | |
161 | if (!fe->key || !fe->value) { | |
162 | ret = -ENOMEM; | |
163 | goto err_alloc; | |
164 | } | |
165 | ||
166 | for (idx = 0; idx < strlen(fe->value); idx++) | |
167 | fe->value[idx] = tolower(fe->value[idx]); | |
168 | ||
169 | list_add_tail(&fe->list, &rd->filter_list); | |
170 | return 0; | |
171 | ||
172 | err_alloc: | |
173 | free(fe->value); | |
174 | free(fe->key); | |
175 | err: | |
176 | free(fe); | |
177 | return ret; | |
178 | } | |
179 | ||
180 | int rd_build_filter(struct rd *rd, const struct filters valid_filters[]) | |
181 | { | |
182 | int ret = 0; | |
183 | int idx = 0; | |
184 | ||
185 | if (!valid_filters || !rd_argc(rd)) | |
186 | goto out; | |
187 | ||
188 | if (rd_argc(rd) == 1) { | |
189 | pr_err("No filter data was supplied to filter option %s\n", rd_argv(rd)); | |
190 | ret = -EINVAL; | |
191 | goto out; | |
192 | } | |
193 | ||
194 | if (rd_argc(rd) % 2) { | |
195 | pr_err("There is filter option without data\n"); | |
196 | ret = -EINVAL; | |
197 | goto out; | |
198 | } | |
199 | ||
200 | while (idx != rd_argc(rd)) { | |
201 | /* | |
202 | * We can do micro-optimization and skip "dev" | |
203 | * and "link" filters, but it is not worth of it. | |
204 | */ | |
205 | ret = add_filter(rd, *(rd->argv + idx), | |
206 | *(rd->argv + idx + 1), valid_filters); | |
207 | if (ret) | |
208 | goto out; | |
209 | idx += 2; | |
210 | } | |
211 | ||
212 | out: | |
213 | return ret; | |
214 | } | |
215 | ||
216 | bool rd_check_is_key_exist(struct rd *rd, const char *key) | |
217 | { | |
218 | struct filter_entry *fe; | |
219 | ||
220 | list_for_each_entry(fe, &rd->filter_list, list) { | |
221 | if (!strcmpx(fe->key, key)) | |
222 | return true; | |
223 | } | |
224 | ||
225 | return false; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Check if string entry is filtered: | |
230 | * * key doesn't exist -> user didn't request -> not filtered | |
231 | */ | |
232 | bool rd_check_is_string_filtered(struct rd *rd, | |
233 | const char *key, const char *val) | |
234 | { | |
235 | bool key_is_filtered = false; | |
236 | struct filter_entry *fe; | |
237 | char *p = NULL; | |
238 | char *str; | |
239 | ||
240 | list_for_each_entry(fe, &rd->filter_list, list) { | |
241 | if (!strcmpx(fe->key, key)) { | |
242 | /* We found the key */ | |
243 | p = strdup(fe->value); | |
244 | key_is_filtered = true; | |
245 | if (!p) { | |
246 | /* | |
247 | * Something extremely wrong if we fail | |
248 | * to allocate small amount of bytes. | |
249 | */ | |
250 | pr_err("Found key, but failed to allocate memory to store value\n"); | |
251 | return key_is_filtered; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Need to check if value in range | |
256 | * It can come in the following formats | |
257 | * and their permutations: | |
258 | * str | |
259 | * str1,str2 | |
260 | */ | |
261 | str = strtok(p, ","); | |
262 | while (str) { | |
263 | if (strlen(str) == strlen(val) && | |
264 | !strcasecmp(str, val)) { | |
265 | key_is_filtered = false; | |
266 | goto out; | |
267 | } | |
268 | str = strtok(NULL, ","); | |
269 | } | |
270 | goto out; | |
271 | } | |
272 | } | |
273 | ||
274 | out: | |
275 | free(p); | |
276 | return key_is_filtered; | |
277 | } | |
278 | ||
279 | /* | |
280 | * Check if key is filtered: | |
281 | * key doesn't exist -> user didn't request -> not filtered | |
282 | */ | |
283 | bool rd_check_is_filtered(struct rd *rd, const char *key, uint32_t val) | |
284 | { | |
285 | bool key_is_filtered = false; | |
286 | struct filter_entry *fe; | |
287 | ||
288 | list_for_each_entry(fe, &rd->filter_list, list) { | |
289 | uint32_t left_val = 0, fe_value = 0; | |
290 | bool range_check = false; | |
291 | char *p = fe->value; | |
292 | ||
293 | if (!strcmpx(fe->key, key)) { | |
294 | /* We found the key */ | |
295 | key_is_filtered = true; | |
296 | /* | |
297 | * Need to check if value in range | |
298 | * It can come in the following formats | |
299 | * (and their permutations): | |
300 | * numb | |
301 | * numb1,numb2 | |
302 | * ,numb1,numb2 | |
303 | * numb1-numb2 | |
304 | * numb1,numb2-numb3,numb4-numb5 | |
305 | */ | |
306 | while (*p) { | |
307 | if (isdigit(*p)) { | |
308 | fe_value = strtol(p, &p, 10); | |
309 | if (fe_value == val || | |
310 | (range_check && left_val < val && | |
311 | val < fe_value)) { | |
312 | key_is_filtered = false; | |
313 | goto out; | |
314 | } | |
315 | range_check = false; | |
316 | } else { | |
317 | if (*p == '-') { | |
318 | left_val = fe_value; | |
319 | range_check = true; | |
320 | } | |
321 | p++; | |
322 | } | |
323 | } | |
324 | goto out; | |
325 | } | |
326 | } | |
327 | ||
328 | out: | |
329 | return key_is_filtered; | |
330 | } | |
331 | ||
332 | static void filters_cleanup(struct rd *rd) | |
333 | { | |
334 | struct filter_entry *fe, *tmp; | |
335 | ||
336 | list_for_each_entry_safe(fe, tmp, | |
337 | &rd->filter_list, list) { | |
338 | list_del(&fe->list); | |
339 | free(fe->key); | |
340 | free(fe->value); | |
341 | free(fe); | |
342 | } | |
343 | } | |
344 | ||
74bd75c2 | 345 | static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = { |
40df8263 | 346 | [RDMA_NLDEV_ATTR_DEV_INDEX] = MNL_TYPE_U32, |
74bd75c2 LR |
347 | [RDMA_NLDEV_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING, |
348 | [RDMA_NLDEV_ATTR_PORT_INDEX] = MNL_TYPE_U32, | |
40df8263 LR |
349 | [RDMA_NLDEV_ATTR_CAP_FLAGS] = MNL_TYPE_U64, |
350 | [RDMA_NLDEV_ATTR_FW_VERSION] = MNL_TYPE_NUL_STRING, | |
351 | [RDMA_NLDEV_ATTR_NODE_GUID] = MNL_TYPE_U64, | |
352 | [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = MNL_TYPE_U64, | |
da990ab4 LR |
353 | [RDMA_NLDEV_ATTR_LID] = MNL_TYPE_U32, |
354 | [RDMA_NLDEV_ATTR_SM_LID] = MNL_TYPE_U32, | |
355 | [RDMA_NLDEV_ATTR_LMC] = MNL_TYPE_U8, | |
356 | [RDMA_NLDEV_ATTR_PORT_STATE] = MNL_TYPE_U8, | |
357 | [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = MNL_TYPE_U8, | |
40df8263 | 358 | [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = MNL_TYPE_U8, |
923aa825 LR |
359 | [RDMA_NLDEV_ATTR_RES_SUMMARY] = MNL_TYPE_NESTED, |
360 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = MNL_TYPE_NESTED, | |
361 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = MNL_TYPE_NUL_STRING, | |
362 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = MNL_TYPE_U64, | |
74bd75c2 LR |
363 | }; |
364 | ||
365 | int rd_attr_cb(const struct nlattr *attr, void *data) | |
366 | { | |
367 | const struct nlattr **tb = data; | |
368 | int type; | |
369 | ||
370 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0) | |
371 | return MNL_CB_ERROR; | |
372 | ||
373 | type = mnl_attr_get_type(attr); | |
374 | ||
375 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
376 | return MNL_CB_ERROR; | |
377 | ||
378 | tb[type] = attr; | |
379 | return MNL_CB_OK; | |
380 | } | |
381 | ||
382 | int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data) | |
383 | { | |
384 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
385 | struct dev_map *dev_map; | |
386 | struct rd *rd = data; | |
387 | const char *dev_name; | |
388 | ||
389 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
390 | if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) | |
391 | return MNL_CB_ERROR; | |
392 | if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { | |
393 | pr_err("This tool doesn't support switches yet\n"); | |
394 | return MNL_CB_ERROR; | |
395 | } | |
396 | ||
397 | dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
398 | ||
399 | dev_map = dev_map_alloc(dev_name); | |
400 | if (!dev_map) | |
401 | /* The main function will cleanup the allocations */ | |
402 | return MNL_CB_ERROR; | |
403 | list_add_tail(&dev_map->list, &rd->dev_map_list); | |
404 | ||
405 | dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
406 | dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
407 | return MNL_CB_OK; | |
408 | } | |
409 | ||
5fc17280 | 410 | void rd_free(struct rd *rd) |
74bd75c2 LR |
411 | { |
412 | if (!rd) | |
413 | return; | |
5fc17280 | 414 | free(rd->buff); |
74bd75c2 | 415 | dev_map_cleanup(rd); |
1174be72 | 416 | filters_cleanup(rd); |
74bd75c2 LR |
417 | } |
418 | ||
6e0de6e8 LR |
419 | int rd_set_arg_to_devname(struct rd *rd) |
420 | { | |
421 | int ret = 0; | |
422 | ||
423 | while (!rd_no_arg(rd)) { | |
424 | if (rd_argv_match(rd, "dev") || rd_argv_match(rd, "link")) { | |
425 | rd_arg_inc(rd); | |
426 | if (rd_no_arg(rd)) { | |
427 | pr_err("No device name was supplied\n"); | |
428 | ret = -EINVAL; | |
429 | } | |
430 | goto out; | |
431 | } | |
432 | rd_arg_inc(rd); | |
433 | } | |
434 | out: | |
435 | return ret; | |
436 | } | |
437 | ||
6416d1a0 | 438 | int rd_exec_link(struct rd *rd, int (*cb)(struct rd *rd), bool strict_port) |
874c734c LR |
439 | { |
440 | struct dev_map *dev_map; | |
441 | uint32_t port; | |
442 | int ret = 0; | |
443 | ||
444 | if (rd->json_output) | |
445 | jsonw_start_array(rd->jw); | |
446 | if (rd_no_arg(rd)) { | |
447 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
448 | rd->dev_idx = dev_map->idx; | |
6416d1a0 LR |
449 | port = (strict_port) ? 1 : 0; |
450 | for (; port < dev_map->num_ports + 1; port++) { | |
874c734c LR |
451 | rd->port_idx = port; |
452 | ret = cb(rd); | |
453 | if (ret) | |
454 | goto out; | |
455 | } | |
456 | } | |
457 | ||
458 | } else { | |
6416d1a0 LR |
459 | bool is_dump_all; |
460 | ||
874c734c | 461 | dev_map = dev_map_lookup(rd, true); |
6416d1a0 LR |
462 | ret = get_port_from_argv(rd, &port, &is_dump_all, strict_port); |
463 | if (!dev_map || port > dev_map->num_ports || (!port && ret)) { | |
874c734c LR |
464 | pr_err("Wrong device name\n"); |
465 | ret = -ENOENT; | |
466 | goto out; | |
467 | } | |
468 | rd_arg_inc(rd); | |
469 | rd->dev_idx = dev_map->idx; | |
6416d1a0 | 470 | rd->port_idx = port; |
874c734c LR |
471 | for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) { |
472 | ret = cb(rd); | |
473 | if (ret) | |
474 | goto out; | |
6416d1a0 | 475 | if (!is_dump_all) |
874c734c LR |
476 | /* |
477 | * We got request to show link for devname | |
478 | * with port index. | |
479 | */ | |
480 | break; | |
481 | } | |
482 | } | |
483 | ||
484 | out: | |
485 | if (rd->json_output) | |
486 | jsonw_end_array(rd->jw); | |
487 | return ret; | |
488 | } | |
489 | ||
8b92a2c9 LR |
490 | int rd_exec_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
491 | { | |
492 | struct dev_map *dev_map; | |
493 | int ret = 0; | |
494 | ||
495 | if (rd->json_output) | |
496 | jsonw_start_array(rd->jw); | |
497 | if (rd_no_arg(rd)) { | |
498 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
499 | rd->dev_idx = dev_map->idx; | |
500 | ret = cb(rd); | |
501 | if (ret) | |
502 | goto out; | |
503 | } | |
504 | } else { | |
505 | dev_map = dev_map_lookup(rd, false); | |
506 | if (!dev_map) { | |
b5564441 | 507 | pr_err("Wrong device name - %s\n", rd_argv(rd)); |
8b92a2c9 LR |
508 | ret = -ENOENT; |
509 | goto out; | |
510 | } | |
511 | rd_arg_inc(rd); | |
512 | rd->dev_idx = dev_map->idx; | |
513 | ret = cb(rd); | |
514 | } | |
515 | out: | |
516 | if (rd->json_output) | |
517 | jsonw_end_array(rd->jw); | |
518 | return ret; | |
519 | } | |
520 | ||
74bd75c2 LR |
521 | int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str) |
522 | { | |
523 | const struct rd_cmd *c; | |
524 | ||
525 | /* First argument in objs table is default variant */ | |
526 | if (rd_no_arg(rd)) | |
527 | return cmds->func(rd); | |
528 | ||
529 | for (c = cmds + 1; c->cmd; ++c) { | |
530 | if (rd_argv_match(rd, c->cmd)) { | |
531 | /* Move to next argument */ | |
532 | rd_arg_inc(rd); | |
533 | return c->func(rd); | |
534 | } | |
535 | } | |
536 | ||
537 | pr_err("Unknown %s '%s'.\n", str, rd_argv(rd)); | |
538 | return 0; | |
539 | } | |
540 | ||
541 | void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags) | |
542 | { | |
543 | *seq = time(NULL); | |
544 | ||
545 | rd->nlh = mnl_nlmsg_put_header(rd->buff); | |
546 | rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd); | |
547 | rd->nlh->nlmsg_seq = *seq; | |
548 | rd->nlh->nlmsg_flags = flags; | |
549 | } | |
550 | ||
551 | int rd_send_msg(struct rd *rd) | |
552 | { | |
553 | int ret; | |
554 | ||
555 | rd->nl = mnl_socket_open(NETLINK_RDMA); | |
556 | if (!rd->nl) { | |
557 | pr_err("Failed to open NETLINK_RDMA socket\n"); | |
558 | return -ENODEV; | |
559 | } | |
560 | ||
561 | ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID); | |
562 | if (ret < 0) { | |
563 | pr_err("Failed to bind socket with err %d\n", ret); | |
564 | goto err; | |
565 | } | |
566 | ||
567 | ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len); | |
568 | if (ret < 0) { | |
569 | pr_err("Failed to send to socket with err %d\n", ret); | |
570 | goto err; | |
571 | } | |
572 | return 0; | |
573 | ||
574 | err: | |
575 | mnl_socket_close(rd->nl); | |
576 | return ret; | |
577 | } | |
578 | ||
579 | int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq) | |
580 | { | |
581 | int ret; | |
582 | unsigned int portid; | |
583 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
584 | ||
585 | portid = mnl_socket_get_portid(rd->nl); | |
586 | do { | |
587 | ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf)); | |
588 | if (ret <= 0) | |
589 | break; | |
590 | ||
591 | ret = mnl_cb_run(buf, ret, seq, portid, callback, data); | |
592 | } while (ret > 0); | |
593 | ||
594 | mnl_socket_close(rd->nl); | |
595 | return ret; | |
596 | } | |
40df8263 | 597 | |
0fc8c30b | 598 | static struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name) |
40df8263 LR |
599 | { |
600 | struct dev_map *dev_map; | |
601 | ||
602 | list_for_each_entry(dev_map, &rd->dev_map_list, list) | |
603 | if (strcmp(dev_name, dev_map->dev_name) == 0) | |
604 | return dev_map; | |
605 | ||
606 | return NULL; | |
607 | } | |
608 | ||
609 | struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index) | |
610 | { | |
611 | struct dev_map *dev_map; | |
612 | char *dev_name; | |
613 | char *slash; | |
614 | ||
99da9032 LR |
615 | if (rd_no_arg(rd)) |
616 | return NULL; | |
617 | ||
40df8263 LR |
618 | dev_name = strdup(rd_argv(rd)); |
619 | if (allow_port_index) { | |
620 | slash = strrchr(dev_name, '/'); | |
621 | if (slash) | |
622 | *slash = '\0'; | |
623 | } | |
624 | ||
625 | dev_map = _dev_map_lookup(rd, dev_name); | |
626 | free(dev_name); | |
627 | return dev_map; | |
628 | } |