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