]> git.proxmox.com Git - mirror_iproute2.git/blame - rdma/res-mr.c
rdma: Move MR code to be suitable for per-line parsing
[mirror_iproute2.git] / rdma / res-mr.c
CommitLineData
42ed283e
LR
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
46695227
LR
10static int res_mr_line(struct rd *rd, const char *name, int idx,
11 struct nlattr *nla_entry)
12{
13 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
14 uint32_t rkey = 0, lkey = 0;
15 uint64_t iova = 0, mrlen;
16 char *comm = NULL;
17 uint32_t pdn = 0;
18 uint32_t mrn = 0;
19 uint32_t pid = 0;
20 int err;
21
22 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
23 if (err != MNL_CB_OK)
24 return MNL_CB_ERROR;
25
26 if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN] ||
27 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
28 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
29 return MNL_CB_ERROR;
30 }
31
32 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
33 rkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
34 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
35 lkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
36 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
37 iova = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
38
39 mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
40 if (rd_check_is_filtered(rd, "mrlen", mrlen))
41 goto out;
42
43 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
44 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
45 comm = get_task_name(pid);
46 }
47
48 if (rd_check_is_filtered(rd, "pid", pid))
49 goto out;
50
51 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
52 mrn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
53 if (rd_check_is_filtered(rd, "mrn", mrn))
54 goto out;
55
56 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
57 pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
58 if (rd_check_is_filtered(rd, "pdn", pdn))
59 goto out;
60
61 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
62 /* discard const from mnl_attr_get_str */
63 comm = (char *)mnl_attr_get_str(
64 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
65
66 if (rd->json_output)
67 jsonw_start_array(rd->jw);
68
69 print_dev(rd, idx, name);
70 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
71 print_key(rd, "rkey", rkey);
72 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
73 print_key(rd, "lkey", lkey);
74 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
75 print_key(rd, "iova", iova);
76 res_print_uint(rd, "mrlen", mrlen);
77 res_print_uint(rd, "pid", pid);
78 print_comm(rd, comm, nla_line);
79
80 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
81 res_print_uint(rd, "mrn", mrn);
82
83 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
84 res_print_uint(rd, "pdn", pdn);
85
86 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
87 newline(rd);
88
89out:
90 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
91 free(comm);
92 return MNL_CB_OK;
93}
42ed283e
LR
94int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data)
95{
96 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
97 struct nlattr *nla_table, *nla_entry;
98 struct rd *rd = data;
46695227 99 int ret = MNL_CB_OK;
42ed283e
LR
100 const char *name;
101 uint32_t idx;
102
103 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
104 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
105 !tb[RDMA_NLDEV_ATTR_RES_MR])
106 return MNL_CB_ERROR;
107
108 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
109 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
110 nla_table = tb[RDMA_NLDEV_ATTR_RES_MR];
111
112 mnl_attr_for_each_nested(nla_entry, nla_table) {
46695227
LR
113 ret = res_mr_line(rd, name, idx, nla_entry);
114
115 if (ret != MNL_CB_OK)
116 break;
42ed283e 117 }
46695227 118 return ret;
42ed283e 119}