]> git.proxmox.com Git - mirror_iproute2.git/blame - rdma/res-qp.c
rdma: Perform single .doit call to query specific objects
[mirror_iproute2.git] / rdma / res-qp.c
CommitLineData
687daf98
LR
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * res-qp.c RDMA tool
4 * Authors: Leon Romanovsky <leonro@mellanox.com>
5 */
6
7#include "res.h"
8#include <inttypes.h>
9
10static const char *path_mig_to_str(uint8_t idx)
11{
12 static const char *const path_mig_str[] = { "MIGRATED", "REARM",
13 "ARMED" };
14
15 if (idx < ARRAY_SIZE(path_mig_str))
16 return path_mig_str[idx];
17 return "UNKNOWN";
18}
19
20static const char *qp_states_to_str(uint8_t idx)
21{
22 static const char *const qp_states_str[] = { "RESET", "INIT", "RTR",
23 "RTS", "SQD", "SQE",
24 "ERR" };
25
26 if (idx < ARRAY_SIZE(qp_states_str))
27 return qp_states_str[idx];
28 return "UNKNOWN";
29}
30
31static void print_rqpn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
32{
33 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN])
34 return;
35
36 if (rd->json_output)
37 jsonw_uint_field(rd->jw, "rqpn", val);
38 else
39 pr_out("rqpn %u ", val);
40}
41
42static void print_type(struct rd *rd, uint32_t val)
43{
44 if (rd->json_output)
45 jsonw_string_field(rd->jw, "type", qp_types_to_str(val));
46 else
47 pr_out("type %s ", qp_types_to_str(val));
48}
49
50static void print_state(struct rd *rd, uint32_t val)
51{
52 if (rd->json_output)
53 jsonw_string_field(rd->jw, "state", qp_states_to_str(val));
54 else
55 pr_out("state %s ", qp_states_to_str(val));
56}
57
58static void print_rqpsn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
59{
60 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN])
61 return;
62
63 if (rd->json_output)
64 jsonw_uint_field(rd->jw, "rq-psn", val);
65 else
66 pr_out("rq-psn %u ", val);
67}
68
687daf98
LR
69static void print_pathmig(struct rd *rd, uint32_t val, struct nlattr **nla_line)
70{
71 if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])
72 return;
73
74 if (rd->json_output)
75 jsonw_string_field(rd->jw, "path-mig-state",
76 path_mig_to_str(val));
77 else
78 pr_out("path-mig-state %s ", path_mig_to_str(val));
79}
80
6da9d251 81static int res_qp_line(struct rd *rd, const char *name, int idx,
5a823593 82 struct nlattr **nla_line)
6da9d251 83{
6da9d251
LR
84 uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn;
85 uint8_t type, state, path_mig_state = 0;
86 uint32_t port = 0, pid = 0;
87 uint32_t pdn = 0;
88 char *comm = NULL;
6da9d251
LR
89
90 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] ||
91 !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] ||
92 !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] ||
93 !nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
94 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
95 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
96 return MNL_CB_ERROR;
97 }
98
99 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
100 port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
101
102 if (port != rd->port_idx)
103 goto out;
104
105 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
106 if (rd_check_is_filtered(rd, "lqpn", lqpn))
107 goto out;
108
109 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
110 pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
111 if (rd_check_is_filtered(rd, "pdn", pdn))
112 goto out;
113
114 if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) {
115 rqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQPN]);
116 if (rd_check_is_filtered(rd, "rqpn", rqpn))
117 goto out;
118 } else {
119 if (rd_check_is_key_exist(rd, "rqpn"))
120 goto out;
121 }
122
123 if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) {
124 rq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]);
125 if (rd_check_is_filtered(rd, "rq-psn", rq_psn))
126 goto out;
127 } else {
128 if (rd_check_is_key_exist(rd, "rq-psn"))
129 goto out;
130 }
131
132 sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
133 if (rd_check_is_filtered(rd, "sq-psn", sq_psn))
134 goto out;
135
136 if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) {
137 path_mig_state = mnl_attr_get_u8(
138 nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]);
139 if (rd_check_is_string_filtered(rd, "path-mig-state",
140 path_mig_to_str(path_mig_state)))
141 goto out;
142 } else {
143 if (rd_check_is_key_exist(rd, "path-mig-state"))
144 goto out;
145 }
146
147 type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
148 if (rd_check_is_string_filtered(rd, "type", qp_types_to_str(type)))
149 goto out;
150
151 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
152 if (rd_check_is_string_filtered(rd, "state", qp_states_to_str(state)))
153 goto out;
154
155 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
156 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
157 comm = get_task_name(pid);
158 }
159
160 if (rd_check_is_filtered(rd, "pid", pid))
161 goto out;
162
163 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
164 /* discard const from mnl_attr_get_str */
165 comm = (char *)mnl_attr_get_str(
166 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
167
168 if (rd->json_output)
169 jsonw_start_array(rd->jw);
170
171 print_link(rd, idx, name, port, nla_line);
172
127ff956 173 res_print_uint(rd, "lqpn", lqpn, nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
6da9d251
LR
174 print_rqpn(rd, rqpn, nla_line);
175
176 print_type(rd, type);
177 print_state(rd, state);
178
179 print_rqpsn(rd, rq_psn, nla_line);
127ff956
LR
180 res_print_uint(rd, "sq-psn", sq_psn,
181 nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
6da9d251
LR
182
183 print_pathmig(rd, path_mig_state, nla_line);
127ff956
LR
184 res_print_uint(rd, "pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
185 res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
6da9d251
LR
186 print_comm(rd, comm, nla_line);
187
188 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
189 newline(rd);
190out:
191 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
192 free(comm);
193 return MNL_CB_OK;
194}
195
5a823593
LR
196int res_qp_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
197{
198 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
199 struct rd *rd = data;
200 const char *name;
201 uint32_t idx;
202
203 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
204 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
205 return MNL_CB_ERROR;
206
207 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
208 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
209
210 return res_qp_line(rd, name, idx, tb);
211}
212
687daf98
LR
213int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data)
214{
215 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
216 struct nlattr *nla_table, *nla_entry;
217 struct rd *rd = data;
6da9d251 218 int ret = MNL_CB_OK;
687daf98
LR
219 const char *name;
220 uint32_t idx;
221
222 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
223 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
224 !tb[RDMA_NLDEV_ATTR_RES_QP])
225 return MNL_CB_ERROR;
226
227 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
228 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
229 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
230
231 mnl_attr_for_each_nested(nla_entry, nla_table) {
5a823593
LR
232 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
233
234 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
235 if (ret != MNL_CB_OK)
236 break;
6da9d251 237
5a823593 238 ret = res_qp_line(rd, name, idx, nla_line);
6da9d251
LR
239 if (ret != MNL_CB_OK)
240 break;
687daf98 241 }
6da9d251 242 return ret;
687daf98 243}