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