]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res.c
rdma: Simplify code to reuse existing functions
[mirror_iproute2.git] / rdma / res.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * res.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_help(struct rd *rd)
11 {
12 pr_out("Usage: %s resource\n", rd->filename);
13 pr_out(" resource show [DEV]\n");
14 pr_out(" resource show [qp|cm_id|pd|mr|cq]\n");
15 pr_out(" resource show qp link [DEV/PORT]\n");
16 pr_out(" resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
17 pr_out(" resource show cm_id link [DEV/PORT]\n");
18 pr_out(" resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
19 pr_out(" resource show cq link [DEV/PORT]\n");
20 pr_out(" resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
21 pr_out(" resource show pd dev [DEV]\n");
22 pr_out(" resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
23 pr_out(" resource show mr dev [DEV]\n");
24 pr_out(" resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
25 return 0;
26 }
27
28 static int res_print_summary(struct rd *rd, struct nlattr **tb)
29 {
30 struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
31 struct nlattr *nla_entry;
32 const char *name;
33 uint64_t curr;
34 int err;
35
36 mnl_attr_for_each_nested(nla_entry, nla_table) {
37 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
38
39 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
40 if (err != MNL_CB_OK)
41 return -EINVAL;
42
43 if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
44 !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
45 return -EINVAL;
46 }
47
48 name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
49 curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
50 res_print_uint(rd, name, curr);
51 }
52 return 0;
53 }
54
55 static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
56 {
57 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
58 struct rd *rd = data;
59 const char *name;
60 uint32_t idx;
61
62 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
63 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
64 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
65 !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
66 return MNL_CB_ERROR;
67
68 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
69 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
70 if (rd->json_output) {
71 jsonw_uint_field(rd->jw, "ifindex", idx);
72 jsonw_string_field(rd->jw, "ifname", name);
73 } else {
74 pr_out("%u: %s: ", idx, name);
75 }
76
77 res_print_summary(rd, tb);
78
79 if (!rd->json_output)
80 pr_out("\n");
81 return MNL_CB_OK;
82 }
83
84 int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
85 {
86 uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
87 uint32_t seq;
88 int ret;
89
90 if (command != RDMA_NLDEV_CMD_RES_GET)
91 flags |= NLM_F_DUMP;
92
93 rd_prepare_msg(rd, command, &seq, flags);
94 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
95 if (rd->port_idx)
96 mnl_attr_put_u32(rd->nlh,
97 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
98
99 ret = rd_send_msg(rd);
100 if (ret)
101 return ret;
102
103 if (rd->json_output)
104 jsonw_start_object(rd->jw);
105 ret = rd_recv_msg(rd, callback, rd, seq);
106 if (rd->json_output)
107 jsonw_end_object(rd->jw);
108 return ret;
109 }
110
111 const char *qp_types_to_str(uint8_t idx)
112 {
113 static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
114 "UC", "UD", "RAW_IPV6",
115 "RAW_ETHERTYPE",
116 "UNKNOWN", "RAW_PACKET",
117 "XRC_INI", "XRC_TGT" };
118
119 if (idx < ARRAY_SIZE(qp_types_str))
120 return qp_types_str[idx];
121 return "UNKNOWN";
122 }
123
124 void print_comm(struct rd *rd, const char *str, struct nlattr **nla_line)
125 {
126 char tmp[18];
127
128 if (rd->json_output) {
129 /* Don't beatify output in JSON format */
130 jsonw_string_field(rd->jw, "comm", str);
131 return;
132 }
133
134 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
135 snprintf(tmp, sizeof(tmp), "%s", str);
136 else
137 snprintf(tmp, sizeof(tmp), "[%s]", str);
138
139 pr_out("comm %s ", tmp);
140 }
141
142 void print_dev(struct rd *rd, uint32_t idx, const char *name)
143 {
144 if (rd->json_output) {
145 jsonw_uint_field(rd->jw, "ifindex", idx);
146 jsonw_string_field(rd->jw, "ifname", name);
147 } else {
148 pr_out("dev %s ", name);
149 }
150 }
151
152 void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port,
153 struct nlattr **nla_line)
154 {
155 if (rd->json_output) {
156 jsonw_uint_field(rd->jw, "ifindex", idx);
157
158 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
159 jsonw_uint_field(rd->jw, "port", port);
160
161 jsonw_string_field(rd->jw, "ifname", name);
162 } else {
163 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
164 pr_out("link %s/%u ", name, port);
165 else
166 pr_out("link %s/- ", name);
167 }
168 }
169
170 char *get_task_name(uint32_t pid)
171 {
172 char *comm;
173 FILE *f;
174
175 if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
176 return NULL;
177
178 f = fopen(comm, "r");
179 free(comm);
180 if (!f)
181 return NULL;
182
183 if (fscanf(f, "%ms\n", &comm) != 1)
184 comm = NULL;
185
186 fclose(f);
187
188 return comm;
189 }
190
191 void print_key(struct rd *rd, const char *name, uint64_t val)
192 {
193 if (rd->json_output)
194 jsonw_xint_field(rd->jw, name, val);
195 else
196 pr_out("%s 0x%" PRIx64 " ", name, val);
197 }
198
199 void res_print_uint(struct rd *rd, const char *name, uint64_t val)
200 {
201 if (rd->json_output)
202 jsonw_u64_field(rd->jw, name, val);
203 else
204 pr_out("%s %" PRIu64 " ", name, val);
205 }
206
207 RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
208
209 static int res_show(struct rd *rd)
210 {
211 const struct rd_cmd cmds[] = {
212 { NULL, res_no_args },
213 { "qp", res_qp },
214 { "cm_id", res_cm_id },
215 { "cq", res_cq },
216 { "mr", res_mr },
217 { "pd", res_pd },
218 { 0 }
219 };
220
221 /*
222 * Special case to support "rdma res show DEV_NAME"
223 */
224 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
225 return rd_exec_dev(rd, _res_no_args);
226
227 return rd_exec_cmd(rd, cmds, "parameter");
228 }
229
230 int cmd_res(struct rd *rd)
231 {
232 const struct rd_cmd cmds[] = {
233 { NULL, res_show },
234 { "show", res_show },
235 { "list", res_show },
236 { "help", res_help },
237 { 0 }
238 };
239
240 return rd_exec_cmd(rd, cmds, "resource command");
241 }