summaryrefslogtreecommitdiffstats
path: root/rdma/res.c
blob: 3e024134d4fe0800b7eb9fd4922368f57d6afffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/*
 * res.c	RDMA tool
 * Authors:     Leon Romanovsky <leonro@mellanox.com>
 */

#include "res.h"
#include <inttypes.h>

static int res_help(struct rd *rd)
{
	pr_out("Usage: %s resource\n", rd->filename);
	pr_out("          resource show [DEV]\n");
	pr_out("          resource show [qp|cm_id|pd|mr|cq|ctx|srq]\n");
	pr_out("          resource show qp link [DEV/PORT]\n");
	pr_out("          resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show cm_id link [DEV/PORT]\n");
	pr_out("          resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show cq link [DEV/PORT]\n");
	pr_out("          resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show pd dev [DEV]\n");
	pr_out("          resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show mr dev [DEV]\n");
	pr_out("          resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show ctx dev [DEV]\n");
	pr_out("          resource show ctx dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
	pr_out("          resource show srq dev [DEV]\n");
	pr_out("          resource show srq dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
	return 0;
}

static int res_print_summary(struct nlattr **tb)
{
	struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
	struct nlattr *nla_entry;
	const char *name;
	uint64_t curr;
	int err;

	mnl_attr_for_each_nested(nla_entry, nla_table) {
		struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};

		err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
		if (err != MNL_CB_OK)
			return -EINVAL;

		if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
		    !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
			return -EINVAL;
		}

		name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
		curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
		res_print_u64(name, curr, nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
	}
	return 0;
}

static int res_no_args_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
{
	return MNL_CB_OK;
}

static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
	const char *name;
	uint32_t idx;

	mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
	    !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
	    !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
		return MNL_CB_ERROR;

	idx =  mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
	open_json_object(NULL);
	print_uint(PRINT_ANY, "ifindex", "%u: ", idx);
	print_string(PRINT_ANY, "ifname", "%s: ", name);
	res_print_summary(tb);
	close_json_object();
	newline();

	return MNL_CB_OK;
}

int _res_send_idx_msg(struct rd *rd, uint32_t command, mnl_cb_t callback,
		      uint32_t idx, uint32_t id)
{
	uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
	uint32_t seq;
	int ret;

	rd_prepare_msg(rd, command, &seq, flags);
	mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
	if (rd->port_idx)
		mnl_attr_put_u32(rd->nlh,
				 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);

	mnl_attr_put_u32(rd->nlh, id, idx);

	if (command == RDMA_NLDEV_CMD_STAT_GET)
		mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES,
				 RDMA_NLDEV_ATTR_RES_MR);

	ret = rd_send_msg(rd);
	if (ret)
		return ret;
	ret = rd_recv_msg(rd, callback, rd, seq);
	return ret;
}

int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
{
	uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
	uint32_t seq;
	int ret;

	if (command != RDMA_NLDEV_CMD_RES_GET)
		flags |= NLM_F_DUMP;

	rd_prepare_msg(rd, command, &seq, flags);
	mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
	if (rd->port_idx)
		mnl_attr_put_u32(rd->nlh,
				 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);

	if (command == RDMA_NLDEV_CMD_STAT_GET)
		mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES,
				 RDMA_NLDEV_ATTR_RES_MR);

	ret = rd_send_msg(rd);
	if (ret)
		return ret;

	ret = rd_recv_msg(rd, callback, rd, seq);
	return ret;
}

const char *qp_types_to_str(uint8_t idx)
{
	static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
						     "UC", "UD", "RAW_IPV6",
						     "RAW_ETHERTYPE",
						     "UNKNOWN", "RAW_PACKET",
						     "XRC_INI", "XRC_TGT",
	};

	if (idx < ARRAY_SIZE(qp_types_str))
		return qp_types_str[idx];

	return (idx == 0xFF) ? "DRIVER" : "UNKNOWN";
}

void print_comm(const char *str, struct nlattr **nla_line)
{
	char tmp[18];

	if (!str)
		return;

	if (nla_line[RDMA_NLDEV_ATTR_RES_PID] || is_json_context())
		snprintf(tmp, sizeof(tmp), "%s", str);
	else
		snprintf(tmp, sizeof(tmp), "[%s]", str);
	print_string(PRINT_ANY, "comm", "comm %s ", tmp);
}

void print_dev(uint32_t idx, const char *name)
{
	print_int(PRINT_ANY, "ifindex", NULL, idx);
	print_string(PRINT_ANY, "ifname", "dev %s ", name);
}

void print_link(uint32_t idx, const char *name, uint32_t port,
		struct nlattr **nla_line)
{
	char tmp[64] = {};

	print_uint(PRINT_JSON, "ifindex", NULL, idx);
	print_string(PRINT_ANY, "ifname", NULL, name);
	if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) {
		print_uint(PRINT_ANY, "port", NULL, port);
		snprintf(tmp, sizeof(tmp), "%s/%d", name, port);
	} else {
		snprintf(tmp, sizeof(tmp), "%s/-", name);
	}

	print_string(PRINT_FP, NULL, "link %s ", tmp);
}

void print_qp_type(uint32_t val)
{
	print_string(PRINT_ANY, "qp-type", "qp-type %s ", qp_types_to_str(val));
}

void print_key(const char *name, uint64_t val, struct nlattr *nlattr)
{
	if (!nlattr)
		return;
	print_string(PRINT_FP, NULL, name, NULL);
	print_hex(PRINT_ANY, name, " 0x%" PRIx64 " ", val);
}

void res_print_u32(const char *name, uint32_t val, struct nlattr *nlattr)
{
	if (!nlattr)
		return;
	print_uint(PRINT_ANY, name, name, val);
	print_uint(PRINT_FP, NULL, " %" PRIu32 " ", val);
}

void res_print_u64(const char *name, uint64_t val, struct nlattr *nlattr)
{
	if (!nlattr)
		return;
	print_u64(PRINT_ANY, name, name, val);
	print_u64(PRINT_FP, NULL, " %" PRIu64 " ", val);
}

RES_FUNC(res_no_args,	RDMA_NLDEV_CMD_RES_GET,	NULL, true, 0);

static int res_show(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		res_no_args	},
		{ "qp",		res_qp		},
		{ "cm_id",	res_cm_id	},
		{ "cq",		res_cq		},
		{ "mr",		res_mr		},
		{ "pd",		res_pd		},
		{ "ctx",	res_ctx		},
		{ "srq",	res_srq		},
		{ 0 }
	};

	/*
	 * Special case to support "rdma res show DEV_NAME"
	 */
	if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
		return rd_exec_dev(rd, _res_no_args);

	return rd_exec_cmd(rd, cmds, "parameter");
}

int cmd_res(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		res_show },
		{ "show",	res_show },
		{ "list",	res_show },
		{ "help",	res_help },
		{ 0 }
	};

	return rd_exec_cmd(rd, cmds, "resource command");
}