]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-qp.c
85725fea4c81776a88d3c53d3e0b7e60b8a4d360
[mirror_iproute2.git] / rdma / res-qp.c
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
10 static 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
20 static 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
31 static 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
42 static 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
50 static 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
58 static 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
69 static void print_sqpsn(struct rd *rd, uint32_t val)
70 {
71 if (rd->json_output)
72 jsonw_uint_field(rd->jw, "sq-psn", val);
73 else
74 pr_out("sq-psn %u ", val);
75 }
76
77 static void print_pathmig(struct rd *rd, uint32_t val, struct nlattr **nla_line)
78 {
79 if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])
80 return;
81
82 if (rd->json_output)
83 jsonw_string_field(rd->jw, "path-mig-state",
84 path_mig_to_str(val));
85 else
86 pr_out("path-mig-state %s ", path_mig_to_str(val));
87 }
88
89 int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data)
90 {
91 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
92 struct nlattr *nla_table, *nla_entry;
93 struct rd *rd = data;
94 const char *name;
95 uint32_t idx;
96
97 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
98 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
99 !tb[RDMA_NLDEV_ATTR_RES_QP])
100 return MNL_CB_ERROR;
101
102 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
103 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
104 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
105
106 mnl_attr_for_each_nested(nla_entry, nla_table) {
107 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
108 uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn;
109 uint8_t type, state, path_mig_state = 0;
110 uint32_t port = 0, pid = 0;
111 uint32_t pdn = 0;
112 char *comm = NULL;
113 int err;
114
115 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
116 if (err != MNL_CB_OK)
117 return MNL_CB_ERROR;
118
119 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] ||
120 !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] ||
121 !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] ||
122 !nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
123 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
124 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
125 return MNL_CB_ERROR;
126 }
127
128 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
129 port = mnl_attr_get_u32(
130 nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
131
132 if (port != rd->port_idx)
133 continue;
134
135 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
136 if (rd_check_is_filtered(rd, "lqpn", lqpn))
137 continue;
138
139 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
140 pdn = mnl_attr_get_u32(
141 nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
142 if (rd_check_is_filtered(rd, "pdn", pdn))
143 continue;
144
145 if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) {
146 rqpn = mnl_attr_get_u32(
147 nla_line[RDMA_NLDEV_ATTR_RES_RQPN]);
148 if (rd_check_is_filtered(rd, "rqpn", rqpn))
149 continue;
150 } else {
151 if (rd_check_is_key_exist(rd, "rqpn"))
152 continue;
153 }
154
155 if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) {
156 rq_psn = mnl_attr_get_u32(
157 nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]);
158 if (rd_check_is_filtered(rd, "rq-psn", rq_psn))
159 continue;
160 } else {
161 if (rd_check_is_key_exist(rd, "rq-psn"))
162 continue;
163 }
164
165 sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
166 if (rd_check_is_filtered(rd, "sq-psn", sq_psn))
167 continue;
168
169 if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) {
170 path_mig_state = mnl_attr_get_u8(
171 nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]);
172 if (rd_check_is_string_filtered(
173 rd, "path-mig-state",
174 path_mig_to_str(path_mig_state)))
175 continue;
176 } else {
177 if (rd_check_is_key_exist(rd, "path-mig-state"))
178 continue;
179 }
180
181 type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
182 if (rd_check_is_string_filtered(rd, "type",
183 qp_types_to_str(type)))
184 continue;
185
186 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
187 if (rd_check_is_string_filtered(rd, "state",
188 qp_states_to_str(state)))
189 continue;
190
191 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
192 pid = mnl_attr_get_u32(
193 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
194 comm = get_task_name(pid);
195 }
196
197 if (rd_check_is_filtered(rd, "pid", pid)) {
198 free(comm);
199 continue;
200 }
201
202 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
203 /* discard const from mnl_attr_get_str */
204 comm = (char *)mnl_attr_get_str(
205 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
206
207 if (rd->json_output)
208 jsonw_start_array(rd->jw);
209
210 print_link(rd, idx, name, port, nla_line);
211
212 print_lqpn(rd, lqpn);
213 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
214 res_print_uint(rd, "pdn", pdn);
215 print_rqpn(rd, rqpn, nla_line);
216
217 print_type(rd, type);
218 print_state(rd, state);
219
220 print_rqpsn(rd, rq_psn, nla_line);
221 print_sqpsn(rd, sq_psn);
222
223 print_pathmig(rd, path_mig_state, nla_line);
224 print_pid(rd, pid);
225 print_comm(rd, comm, nla_line);
226
227 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
228 free(comm);
229
230 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
231 newline(rd);
232 }
233 return MNL_CB_OK;
234 }