]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-mr.c
d42e8d812d93b6a7dae7eb38868a88cafe1a225a
[mirror_iproute2.git] / rdma / res-mr.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * res-mr.c RDMA tool
4 * Authors: Leon Romanovsky <leonro@mellanox.com>
5 */
6
7 #include "res.h"
8 #include <inttypes.h>
9
10 static int res_mr_line(struct rd *rd, const char *name, int idx,
11 struct nlattr **nla_line)
12 {
13 uint32_t rkey = 0, lkey = 0;
14 uint64_t iova = 0, mrlen;
15 char *comm = NULL;
16 uint32_t pdn = 0;
17 uint32_t mrn = 0;
18 uint32_t pid = 0;
19
20 if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN] ||
21 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
22 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
23 return MNL_CB_ERROR;
24 }
25
26 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
27 rkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
28 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
29 lkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
30 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
31 iova = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
32
33 mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
34 if (rd_check_is_filtered(rd, "mrlen", mrlen))
35 goto out;
36
37 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
38 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
39 comm = get_task_name(pid);
40 }
41
42 if (rd_check_is_filtered(rd, "pid", pid))
43 goto out;
44
45 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
46 mrn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
47 if (rd_check_is_filtered(rd, "mrn", mrn))
48 goto out;
49
50 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
51 pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
52 if (rd_check_is_filtered(rd, "pdn", pdn))
53 goto out;
54
55 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
56 /* discard const from mnl_attr_get_str */
57 comm = (char *)mnl_attr_get_str(
58 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
59
60 if (rd->json_output)
61 jsonw_start_array(rd->jw);
62
63 print_dev(rd, idx, name);
64 res_print_uint(rd, "mrn", mrn, nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
65 print_key(rd, "rkey", rkey, nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
66 print_key(rd, "lkey", lkey, nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
67 print_key(rd, "iova", iova, nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
68 res_print_uint(rd, "mrlen", mrlen, nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
69 res_print_uint(rd, "pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
70 res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
71 print_comm(rd, comm, nla_line);
72
73 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
74 newline(rd);
75
76 out:
77 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
78 free(comm);
79 return MNL_CB_OK;
80 }
81
82 int res_mr_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
83 {
84 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
85 struct rd *rd = data;
86 const char *name;
87 uint32_t idx;
88
89 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
90 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
91 return MNL_CB_ERROR;
92
93 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
94 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
95
96 return res_mr_line(rd, name, idx, tb);
97 }
98
99 int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data)
100 {
101 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
102 struct nlattr *nla_table, *nla_entry;
103 struct rd *rd = data;
104 int ret = MNL_CB_OK;
105 const char *name;
106 uint32_t idx;
107
108 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
109 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
110 !tb[RDMA_NLDEV_ATTR_RES_MR])
111 return MNL_CB_ERROR;
112
113 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
114 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
115 nla_table = tb[RDMA_NLDEV_ATTR_RES_MR];
116
117 mnl_attr_for_each_nested(nla_entry, nla_table) {
118 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
119
120 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
121 if (ret != MNL_CB_OK)
122 break;
123
124 ret = res_mr_line(rd, name, idx, nla_line);
125 if (ret != MNL_CB_OK)
126 break;
127 }
128 return ret;
129 }