| 1 |
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 |
/* |
| 3 |
* res-ctx.c RDMA tool |
| 4 |
* Authors: Neta Ostrovsky <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "res.h" |
| 8 |
#include <inttypes.h> |
| 9 |
|
| 10 |
static int res_ctx_line(struct rd *rd, const char *name, int idx, |
| 11 |
struct nlattr **nla_line) |
| 12 |
{ |
| 13 |
char *comm = NULL; |
| 14 |
uint32_t ctxn = 0; |
| 15 |
uint32_t pid = 0; |
| 16 |
SPRINT_BUF(b); |
| 17 |
|
| 18 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) |
| 19 |
return MNL_CB_ERROR; |
| 20 |
|
| 21 |
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { |
| 22 |
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]); |
| 23 |
if (!get_task_name(pid, b, sizeof(b))) |
| 24 |
comm = b; |
| 25 |
} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) { |
| 26 |
/* discard const from mnl_attr_get_str */ |
| 27 |
comm = (char *)mnl_attr_get_str( |
| 28 |
nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); |
| 29 |
} |
| 30 |
|
| 31 |
if (rd_is_filtered_attr(rd, "pid", pid, |
| 32 |
nla_line[RDMA_NLDEV_ATTR_RES_PID])) |
| 33 |
goto out; |
| 34 |
|
| 35 |
if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) |
| 36 |
ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]); |
| 37 |
|
| 38 |
if (rd_is_filtered_attr(rd, "ctxn", ctxn, |
| 39 |
nla_line[RDMA_NLDEV_ATTR_RES_CTXN])) |
| 40 |
goto out; |
| 41 |
|
| 42 |
open_json_object(NULL); |
| 43 |
print_dev(idx, name); |
| 44 |
res_print_u32("ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]); |
| 45 |
res_print_u32("pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]); |
| 46 |
print_comm(comm, nla_line); |
| 47 |
|
| 48 |
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
| 49 |
close_json_object(); |
| 50 |
newline(); |
| 51 |
|
| 52 |
out: |
| 53 |
return MNL_CB_OK; |
| 54 |
} |
| 55 |
|
| 56 |
int res_ctx_idx_parse_cb(const struct nlmsghdr *nlh, void *data) |
| 57 |
{ |
| 58 |
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; |
| 59 |
struct rd *rd = data; |
| 60 |
const char *name; |
| 61 |
uint32_t idx; |
| 62 |
|
| 63 |
mnl_attr_parse(nlh, 0, rd_attr_cb, tb); |
| 64 |
if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME]) |
| 65 |
return MNL_CB_ERROR; |
| 66 |
|
| 67 |
name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); |
| 68 |
idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); |
| 69 |
|
| 70 |
return res_ctx_line(rd, name, idx, tb); |
| 71 |
} |
| 72 |
|
| 73 |
int res_ctx_parse_cb(const struct nlmsghdr *nlh, void *data) |
| 74 |
{ |
| 75 |
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; |
| 76 |
struct nlattr *nla_table, *nla_entry; |
| 77 |
struct rd *rd = data; |
| 78 |
int ret = MNL_CB_OK; |
| 79 |
const char *name; |
| 80 |
uint32_t idx; |
| 81 |
|
| 82 |
mnl_attr_parse(nlh, 0, rd_attr_cb, tb); |
| 83 |
if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || |
| 84 |
!tb[RDMA_NLDEV_ATTR_RES_CTX]) |
| 85 |
return MNL_CB_ERROR; |
| 86 |
|
| 87 |
name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); |
| 88 |
idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); |
| 89 |
nla_table = tb[RDMA_NLDEV_ATTR_RES_CTX]; |
| 90 |
|
| 91 |
mnl_attr_for_each_nested(nla_entry, nla_table) { |
| 92 |
struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; |
| 93 |
|
| 94 |
ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); |
| 95 |
if (ret != MNL_CB_OK) |
| 96 |
break; |
| 97 |
|
| 98 |
ret = res_ctx_line(rd, name, idx, nla_line); |
| 99 |
if (ret != MNL_CB_OK) |
| 100 |
break; |
| 101 |
} |
| 102 |
return ret; |
| 103 |
} |