• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the names of the copyright holders nor the names of its
13  *    contributors may be used to endorse or promote products derived from
14  *    this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") version 2 as published by the Free
18  * Software Foundation.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <linux/module.h>
34 #include <linux/pid.h>
35 #include <linux/pid_namespace.h>
36 #include <net/netlink.h>
37 #include <rdma/rdma_cm.h>
38 #include <rdma/rdma_netlink.h>
39 
40 #include "core_priv.h"
41 #include "cma_priv.h"
42 
43 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
44 	[RDMA_NLDEV_ATTR_DEV_INDEX]     = { .type = NLA_U32 },
45 	[RDMA_NLDEV_ATTR_DEV_NAME]	= { .type = NLA_NUL_STRING,
46 					    .len = IB_DEVICE_NAME_MAX - 1},
47 	[RDMA_NLDEV_ATTR_PORT_INDEX]	= { .type = NLA_U32 },
48 	[RDMA_NLDEV_ATTR_FW_VERSION]	= { .type = NLA_NUL_STRING,
49 					    .len = IB_FW_VERSION_NAME_MAX - 1},
50 	[RDMA_NLDEV_ATTR_NODE_GUID]	= { .type = NLA_U64 },
51 	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
52 	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]	= { .type = NLA_U64 },
53 	[RDMA_NLDEV_ATTR_LID]		= { .type = NLA_U32 },
54 	[RDMA_NLDEV_ATTR_SM_LID]	= { .type = NLA_U32 },
55 	[RDMA_NLDEV_ATTR_LMC]		= { .type = NLA_U8 },
56 	[RDMA_NLDEV_ATTR_PORT_STATE]	= { .type = NLA_U8 },
57 	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
58 	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
59 	[RDMA_NLDEV_ATTR_RES_SUMMARY]	= { .type = NLA_NESTED },
60 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
61 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = { .type = NLA_NUL_STRING,
62 					     .len = 16 },
63 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 },
64 	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
65 	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
66 	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
67 	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
68 	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
69 	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
70 	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
71 	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
72 	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
73 	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
74 	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
75 						    .len = TASK_COMM_LEN },
76 	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
77 	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
78 	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
79 	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]	= {
80 			.len = sizeof(struct __kernel_sockaddr_storage) },
81 	[RDMA_NLDEV_ATTR_RES_DST_ADDR]	= {
82 			.len = sizeof(struct __kernel_sockaddr_storage) },
83 	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
84 	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
85 	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
86 	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
87 	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
88 	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
89 	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
90 	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
91 	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
92 	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
93 	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
94 	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
95 	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
96 	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
97 	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY] = { .type = NLA_U32 },
98 	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
99 	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
100 						    .len = IFNAMSIZ },
101 	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
102 	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
103 	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
104 				    .len = RDMA_NLDEV_ATTR_ENTRY_STRLEN },
105 	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
106 	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
107 	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
108 	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
109 	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
110 };
111 
put_driver_name_print_type(struct sk_buff * msg,const char * name,enum rdma_nldev_print_type print_type)112 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
113 				      enum rdma_nldev_print_type print_type)
114 {
115 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
116 		return -EMSGSIZE;
117 	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
118 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
119 		return -EMSGSIZE;
120 
121 	return 0;
122 }
123 
_rdma_nl_put_driver_u32(struct sk_buff * msg,const char * name,enum rdma_nldev_print_type print_type,u32 value)124 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
125 				   enum rdma_nldev_print_type print_type,
126 				   u32 value)
127 {
128 	if (put_driver_name_print_type(msg, name, print_type))
129 		return -EMSGSIZE;
130 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
131 		return -EMSGSIZE;
132 
133 	return 0;
134 }
135 
_rdma_nl_put_driver_u64(struct sk_buff * msg,const char * name,enum rdma_nldev_print_type print_type,u64 value)136 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
137 				   enum rdma_nldev_print_type print_type,
138 				   u64 value)
139 {
140 	if (put_driver_name_print_type(msg, name, print_type))
141 		return -EMSGSIZE;
142 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
143 			      RDMA_NLDEV_ATTR_PAD))
144 		return -EMSGSIZE;
145 
146 	return 0;
147 }
148 
rdma_nl_put_driver_u32(struct sk_buff * msg,const char * name,u32 value)149 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
150 {
151 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
152 				       value);
153 }
154 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
155 
rdma_nl_put_driver_u32_hex(struct sk_buff * msg,const char * name,u32 value)156 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
157 			       u32 value)
158 {
159 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
160 				       value);
161 }
162 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
163 
rdma_nl_put_driver_u64(struct sk_buff * msg,const char * name,u64 value)164 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
165 {
166 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
167 				       value);
168 }
169 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
170 
rdma_nl_put_driver_u64_hex(struct sk_buff * msg,const char * name,u64 value)171 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
172 {
173 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
174 				       value);
175 }
176 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
177 
fill_nldev_handle(struct sk_buff * msg,struct ib_device * device)178 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
179 {
180 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
181 		return -EMSGSIZE;
182 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
183 		return -EMSGSIZE;
184 
185 	return 0;
186 }
187 
fill_dev_info(struct sk_buff * msg,struct ib_device * device)188 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
189 {
190 	char fw[IB_FW_VERSION_NAME_MAX];
191 
192 	if (fill_nldev_handle(msg, device))
193 		return -EMSGSIZE;
194 
195 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
196 		return -EMSGSIZE;
197 
198 	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
199 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
200 			      device->attrs.device_cap_flags,
201 			      RDMA_NLDEV_ATTR_PAD))
202 		return -EMSGSIZE;
203 
204 	ib_get_device_fw_str(device, fw);
205 	/* Device without FW has strlen(fw) = 0 */
206 	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
207 		return -EMSGSIZE;
208 
209 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
210 			      be64_to_cpu(device->node_guid),
211 			      RDMA_NLDEV_ATTR_PAD))
212 		return -EMSGSIZE;
213 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
214 			      be64_to_cpu(device->attrs.sys_image_guid),
215 			      RDMA_NLDEV_ATTR_PAD))
216 		return -EMSGSIZE;
217 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
218 		return -EMSGSIZE;
219 	return 0;
220 }
221 
fill_port_info(struct sk_buff * msg,struct ib_device * device,u32 port,const struct net * net)222 static int fill_port_info(struct sk_buff *msg,
223 			  struct ib_device *device, u32 port,
224 			  const struct net *net)
225 {
226 	struct net_device *netdev = NULL;
227 	struct ib_port_attr attr;
228 	int ret;
229 
230 	if (fill_nldev_handle(msg, device))
231 		return -EMSGSIZE;
232 
233 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
234 		return -EMSGSIZE;
235 
236 	ret = ib_query_port(device, port, &attr);
237 	if (ret)
238 		return ret;
239 
240 	if (rdma_protocol_ib(device, port)) {
241 		BUILD_BUG_ON(sizeof(attr.port_cap_flags) > sizeof(u64));
242 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
243 				      (u64)attr.port_cap_flags,
244 				      RDMA_NLDEV_ATTR_PAD))
245 			return -EMSGSIZE;
246 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
247 				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
248 			return -EMSGSIZE;
249 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
250 			return -EMSGSIZE;
251 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
252 			return -EMSGSIZE;
253 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
254 			return -EMSGSIZE;
255 	}
256 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
257 		return -EMSGSIZE;
258 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
259 		return -EMSGSIZE;
260 
261 	if (device->get_netdev)
262 		netdev = device->get_netdev(device, port);
263 
264 	if (netdev && net_eq(dev_net(netdev), net)) {
265 		ret = nla_put_u32(msg,
266 				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
267 		if (ret)
268 			goto out;
269 		ret = nla_put_string(msg,
270 				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
271 	}
272 
273 out:
274 	if (netdev)
275 		dev_put(netdev);
276 	return ret;
277 }
278 
fill_res_info_entry(struct sk_buff * msg,const char * name,u64 curr)279 static int fill_res_info_entry(struct sk_buff *msg,
280 			       const char *name, u64 curr)
281 {
282 	struct nlattr *entry_attr;
283 
284 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
285 	if (!entry_attr)
286 		return -EMSGSIZE;
287 
288 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
289 		goto err;
290 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
291 			      RDMA_NLDEV_ATTR_PAD))
292 		goto err;
293 
294 	nla_nest_end(msg, entry_attr);
295 	return 0;
296 
297 err:
298 	nla_nest_cancel(msg, entry_attr);
299 	return -EMSGSIZE;
300 }
301 
fill_res_info(struct sk_buff * msg,struct ib_device * device)302 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
303 {
304 	static const char * const names[RDMA_RESTRACK_MAX] = {
305 		[RDMA_RESTRACK_PD] = "pd",
306 		[RDMA_RESTRACK_CQ] = "cq",
307 		[RDMA_RESTRACK_QP] = "qp",
308 		[RDMA_RESTRACK_CM_ID] = "cm_id",
309 		[RDMA_RESTRACK_MR] = "mr",
310 	};
311 
312 	struct rdma_restrack_root *res = &device->res;
313 	struct nlattr *table_attr;
314 	int ret, i, curr;
315 
316 	if (fill_nldev_handle(msg, device))
317 		return -EMSGSIZE;
318 
319 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
320 	if (!table_attr)
321 		return -EMSGSIZE;
322 
323 	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
324 		if (!names[i])
325 			continue;
326 		curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
327 		ret = fill_res_info_entry(msg, names[i], curr);
328 		if (ret)
329 			goto err;
330 	}
331 
332 	nla_nest_end(msg, table_attr);
333 	return 0;
334 
335 err:
336 	nla_nest_cancel(msg, table_attr);
337 	return ret;
338 }
339 
fill_res_name_pid(struct sk_buff * msg,struct rdma_restrack_entry * res)340 static int fill_res_name_pid(struct sk_buff *msg,
341 			     struct rdma_restrack_entry *res)
342 {
343 	/*
344 	 * For user resources, user is should read /proc/PID/comm to get the
345 	 * name of the task file.
346 	 */
347 	if (rdma_is_kernel_res(res)) {
348 		if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
349 		    res->kern_name))
350 			return -EMSGSIZE;
351 	} else {
352 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
353 		    task_pid_vnr(res->task)))
354 			return -EMSGSIZE;
355 	}
356 	return 0;
357 }
358 
fill_res_qp_entry(struct sk_buff * msg,struct netlink_callback * cb,struct rdma_restrack_entry * res,uint32_t port)359 static int fill_res_qp_entry(struct sk_buff *msg, struct netlink_callback *cb,
360 			     struct rdma_restrack_entry *res, uint32_t port)
361 {
362 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
363 	struct rdma_restrack_root *resroot = &qp->device->res;
364 	struct ib_qp_init_attr qp_init_attr;
365 	struct nlattr *entry_attr;
366 	struct ib_qp_attr qp_attr;
367 	int ret;
368 
369 	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
370 	if (ret)
371 		return ret;
372 
373 	if (port && port != qp_attr.port_num)
374 		return 0;
375 
376 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
377 	if (!entry_attr)
378 		goto out;
379 
380 	/* In create_qp() port is not set yet */
381 	if (qp_attr.port_num &&
382 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
383 		goto err;
384 
385 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
386 		goto err;
387 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
388 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
389 				qp_attr.dest_qp_num))
390 			goto err;
391 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
392 				qp_attr.rq_psn))
393 			goto err;
394 	}
395 
396 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
397 		goto err;
398 
399 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
400 	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
401 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
402 			       qp_attr.path_mig_state))
403 			goto err;
404 	}
405 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
406 		goto err;
407 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
408 		goto err;
409 
410 	if (fill_res_name_pid(msg, res))
411 		goto err;
412 
413 	if (resroot->fill_res_entry(msg, res))
414 		goto err;
415 
416 	nla_nest_end(msg, entry_attr);
417 	return 0;
418 
419 err:
420 	nla_nest_cancel(msg, entry_attr);
421 out:
422 	return -EMSGSIZE;
423 }
424 
fill_res_cm_id_entry(struct sk_buff * msg,struct netlink_callback * cb,struct rdma_restrack_entry * res,uint32_t port)425 static int fill_res_cm_id_entry(struct sk_buff *msg,
426 				struct netlink_callback *cb,
427 				struct rdma_restrack_entry *res, uint32_t port)
428 {
429 	struct rdma_id_private *id_priv =
430 				container_of(res, struct rdma_id_private, res);
431 	struct rdma_restrack_root *resroot = &id_priv->id.device->res;
432 	struct rdma_cm_id *cm_id = &id_priv->id;
433 	struct nlattr *entry_attr;
434 
435 	if (port && port != cm_id->port_num)
436 		return 0;
437 
438 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
439 	if (!entry_attr)
440 		goto out;
441 
442 	if (cm_id->port_num &&
443 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
444 		goto err;
445 
446 	if (id_priv->qp_num) {
447 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
448 			goto err;
449 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
450 			goto err;
451 	}
452 
453 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
454 		goto err;
455 
456 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
457 		goto err;
458 
459 	if (cm_id->route.addr.src_addr.ss_family &&
460 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
461 		    sizeof(cm_id->route.addr.src_addr),
462 		    &cm_id->route.addr.src_addr))
463 		goto err;
464 	if (cm_id->route.addr.dst_addr.ss_family &&
465 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
466 		    sizeof(cm_id->route.addr.dst_addr),
467 		    &cm_id->route.addr.dst_addr))
468 		goto err;
469 
470 	if (fill_res_name_pid(msg, res))
471 		goto err;
472 
473 	if (resroot->fill_res_entry(msg, res))
474 		goto err;
475 
476 	nla_nest_end(msg, entry_attr);
477 	return 0;
478 
479 err:
480 	nla_nest_cancel(msg, entry_attr);
481 out:
482 	return -EMSGSIZE;
483 }
484 
fill_res_cq_entry(struct sk_buff * msg,struct netlink_callback * cb,struct rdma_restrack_entry * res,uint32_t port)485 static int fill_res_cq_entry(struct sk_buff *msg, struct netlink_callback *cb,
486 			     struct rdma_restrack_entry *res, uint32_t port)
487 {
488 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
489 	struct rdma_restrack_root *resroot = &cq->device->res;
490 	struct nlattr *entry_attr;
491 
492 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CQ_ENTRY);
493 	if (!entry_attr)
494 		goto out;
495 
496 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
497 		goto err;
498 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
499 			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
500 		goto err;
501 
502 	/* Poll context is only valid for kernel CQs */
503 	if (rdma_is_kernel_res(res) &&
504 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
505 		goto err;
506 
507 	if (fill_res_name_pid(msg, res))
508 		goto err;
509 
510 	if (resroot->fill_res_entry(msg, res))
511 		goto err;
512 
513 	nla_nest_end(msg, entry_attr);
514 	return 0;
515 
516 err:
517 	nla_nest_cancel(msg, entry_attr);
518 out:
519 	return -EMSGSIZE;
520 }
521 
fill_res_mr_entry(struct sk_buff * msg,struct netlink_callback * cb,struct rdma_restrack_entry * res,uint32_t port)522 static int fill_res_mr_entry(struct sk_buff *msg, struct netlink_callback *cb,
523 			     struct rdma_restrack_entry *res, uint32_t port)
524 {
525 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
526 	struct rdma_restrack_root *resroot = &mr->pd->device->res;
527 	struct nlattr *entry_attr;
528 
529 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_MR_ENTRY);
530 	if (!entry_attr)
531 		goto out;
532 
533 	if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
534 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
535 			goto err;
536 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
537 			goto err;
538 	}
539 
540 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
541 			      RDMA_NLDEV_ATTR_PAD))
542 		goto err;
543 
544 	if (fill_res_name_pid(msg, res))
545 		goto err;
546 
547 	if (resroot->fill_res_entry(msg, res))
548 		goto err;
549 
550 	nla_nest_end(msg, entry_attr);
551 	return 0;
552 
553 err:
554 	nla_nest_cancel(msg, entry_attr);
555 out:
556 	return -EMSGSIZE;
557 }
558 
fill_res_pd_entry(struct sk_buff * msg,struct netlink_callback * cb,struct rdma_restrack_entry * res,uint32_t port)559 static int fill_res_pd_entry(struct sk_buff *msg, struct netlink_callback *cb,
560 			     struct rdma_restrack_entry *res, uint32_t port)
561 {
562 	struct ib_pd *pd = container_of(res, struct ib_pd, res);
563 	struct rdma_restrack_root *resroot = &pd->device->res;
564 	struct nlattr *entry_attr;
565 
566 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_PD_ENTRY);
567 	if (!entry_attr)
568 		goto out;
569 
570 	if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
571 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
572 				pd->local_dma_lkey))
573 			goto err;
574 		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
575 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
576 				pd->unsafe_global_rkey))
577 			goto err;
578 	}
579 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
580 			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
581 		goto err;
582 
583 	if (fill_res_name_pid(msg, res))
584 		goto err;
585 
586 	if (resroot->fill_res_entry(msg, res))
587 		goto err;
588 
589 	nla_nest_end(msg, entry_attr);
590 	return 0;
591 
592 err:
593 	nla_nest_cancel(msg, entry_attr);
594 out:
595 	return -EMSGSIZE;
596 }
597 
nldev_get_doit(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)598 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
599 			  struct netlink_ext_ack *extack)
600 {
601 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
602 	struct ib_device *device;
603 	struct sk_buff *msg;
604 	u32 index;
605 	int err;
606 
607 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
608 			  nldev_policy, extack);
609 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
610 		return -EINVAL;
611 
612 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
613 
614 	device = ib_device_get_by_index(index);
615 	if (!device)
616 		return -EINVAL;
617 
618 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
619 	if (!msg) {
620 		err = -ENOMEM;
621 		goto err;
622 	}
623 
624 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
625 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
626 			0, 0);
627 
628 	err = fill_dev_info(msg, device);
629 	if (err)
630 		goto err_free;
631 
632 	nlmsg_end(msg, nlh);
633 
634 	put_device(&device->dev);
635 	return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
636 
637 err_free:
638 	nlmsg_free(msg);
639 err:
640 	put_device(&device->dev);
641 	return err;
642 }
643 
_nldev_get_dumpit(struct ib_device * device,struct sk_buff * skb,struct netlink_callback * cb,unsigned int idx)644 static int _nldev_get_dumpit(struct ib_device *device,
645 			     struct sk_buff *skb,
646 			     struct netlink_callback *cb,
647 			     unsigned int idx)
648 {
649 	int start = cb->args[0];
650 	struct nlmsghdr *nlh;
651 
652 	if (idx < start)
653 		return 0;
654 
655 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
656 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
657 			0, NLM_F_MULTI);
658 
659 	if (fill_dev_info(skb, device)) {
660 		nlmsg_cancel(skb, nlh);
661 		goto out;
662 	}
663 
664 	nlmsg_end(skb, nlh);
665 
666 	idx++;
667 
668 out:	cb->args[0] = idx;
669 	return skb->len;
670 }
671 
nldev_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)672 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
673 {
674 	/*
675 	 * There is no need to take lock, because
676 	 * we are relying on ib_core's lists_rwsem
677 	 */
678 	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
679 }
680 
nldev_port_get_doit(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)681 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
682 			       struct netlink_ext_ack *extack)
683 {
684 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
685 	struct ib_device *device;
686 	struct sk_buff *msg;
687 	u32 index;
688 	u32 port;
689 	int err;
690 
691 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
692 			  nldev_policy, extack);
693 	if (err ||
694 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
695 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
696 		return -EINVAL;
697 
698 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
699 	device = ib_device_get_by_index(index);
700 	if (!device)
701 		return -EINVAL;
702 
703 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
704 	if (!rdma_is_port_valid(device, port)) {
705 		err = -EINVAL;
706 		goto err;
707 	}
708 
709 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
710 	if (!msg) {
711 		err = -ENOMEM;
712 		goto err;
713 	}
714 
715 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
716 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
717 			0, 0);
718 
719 	err = fill_port_info(msg, device, port, sock_net(skb->sk));
720 	if (err)
721 		goto err_free;
722 
723 	nlmsg_end(msg, nlh);
724 	put_device(&device->dev);
725 
726 	return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
727 
728 err_free:
729 	nlmsg_free(msg);
730 err:
731 	put_device(&device->dev);
732 	return err;
733 }
734 
nldev_port_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)735 static int nldev_port_get_dumpit(struct sk_buff *skb,
736 				 struct netlink_callback *cb)
737 {
738 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
739 	struct ib_device *device;
740 	int start = cb->args[0];
741 	struct nlmsghdr *nlh;
742 	u32 idx = 0;
743 	u32 ifindex;
744 	int err;
745 	u32 p;
746 
747 	err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
748 			  nldev_policy, NULL);
749 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
750 		return -EINVAL;
751 
752 	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
753 	device = ib_device_get_by_index(ifindex);
754 	if (!device)
755 		return -EINVAL;
756 
757 	for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
758 		/*
759 		 * The dumpit function returns all information from specific
760 		 * index. This specific index is taken from the netlink
761 		 * messages request sent by user and it is available
762 		 * in cb->args[0].
763 		 *
764 		 * Usually, the user doesn't fill this field and it causes
765 		 * to return everything.
766 		 *
767 		 */
768 		if (idx < start) {
769 			idx++;
770 			continue;
771 		}
772 
773 		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
774 				cb->nlh->nlmsg_seq,
775 				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
776 						 RDMA_NLDEV_CMD_PORT_GET),
777 				0, NLM_F_MULTI);
778 
779 		if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
780 			nlmsg_cancel(skb, nlh);
781 			goto out;
782 		}
783 		idx++;
784 		nlmsg_end(skb, nlh);
785 	}
786 
787 out:
788 	put_device(&device->dev);
789 	cb->args[0] = idx;
790 	return skb->len;
791 }
792 
nldev_res_get_doit(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)793 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
794 			      struct netlink_ext_ack *extack)
795 {
796 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
797 	struct ib_device *device;
798 	struct sk_buff *msg;
799 	u32 index;
800 	int ret;
801 
802 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
803 			  nldev_policy, extack);
804 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
805 		return -EINVAL;
806 
807 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
808 	device = ib_device_get_by_index(index);
809 	if (!device)
810 		return -EINVAL;
811 
812 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
813 	if (!msg) {
814 		ret = -ENOMEM;
815 		goto err;
816 	}
817 
818 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
819 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
820 			0, 0);
821 
822 	ret = fill_res_info(msg, device);
823 	if (ret)
824 		goto err_free;
825 
826 	nlmsg_end(msg, nlh);
827 	put_device(&device->dev);
828 	return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
829 
830 err_free:
831 	nlmsg_free(msg);
832 err:
833 	put_device(&device->dev);
834 	return ret;
835 }
836 
_nldev_res_get_dumpit(struct ib_device * device,struct sk_buff * skb,struct netlink_callback * cb,unsigned int idx)837 static int _nldev_res_get_dumpit(struct ib_device *device,
838 				 struct sk_buff *skb,
839 				 struct netlink_callback *cb,
840 				 unsigned int idx)
841 {
842 	int start = cb->args[0];
843 	struct nlmsghdr *nlh;
844 
845 	if (idx < start)
846 		return 0;
847 
848 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
849 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
850 			0, NLM_F_MULTI);
851 
852 	if (fill_res_info(skb, device)) {
853 		nlmsg_cancel(skb, nlh);
854 		goto out;
855 	}
856 
857 	nlmsg_end(skb, nlh);
858 
859 	idx++;
860 
861 out:
862 	cb->args[0] = idx;
863 	return skb->len;
864 }
865 
nldev_res_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)866 static int nldev_res_get_dumpit(struct sk_buff *skb,
867 				struct netlink_callback *cb)
868 {
869 	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
870 }
871 
872 struct nldev_fill_res_entry {
873 	int (*fill_res_func)(struct sk_buff *msg, struct netlink_callback *cb,
874 			     struct rdma_restrack_entry *res, u32 port);
875 	enum rdma_nldev_attr nldev_attr;
876 	enum rdma_nldev_command nldev_cmd;
877 };
878 
879 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
880 	[RDMA_RESTRACK_QP] = {
881 		.fill_res_func = fill_res_qp_entry,
882 		.nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
883 		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
884 	},
885 	[RDMA_RESTRACK_CM_ID] = {
886 		.fill_res_func = fill_res_cm_id_entry,
887 		.nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
888 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
889 	},
890 	[RDMA_RESTRACK_CQ] = {
891 		.fill_res_func = fill_res_cq_entry,
892 		.nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
893 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
894 	},
895 	[RDMA_RESTRACK_MR] = {
896 		.fill_res_func = fill_res_mr_entry,
897 		.nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
898 		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
899 	},
900 	[RDMA_RESTRACK_PD] = {
901 		.fill_res_func = fill_res_pd_entry,
902 		.nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
903 		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
904 	},
905 };
906 
res_get_common_dumpit(struct sk_buff * skb,struct netlink_callback * cb,enum rdma_restrack_type res_type)907 static int res_get_common_dumpit(struct sk_buff *skb,
908 				 struct netlink_callback *cb,
909 				 enum rdma_restrack_type res_type)
910 {
911 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
912 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
913 	struct rdma_restrack_entry *res;
914 	int err, ret = 0, idx = 0;
915 	struct nlattr *table_attr;
916 	struct ib_device *device;
917 	int start = cb->args[0];
918 	struct nlmsghdr *nlh;
919 	u32 index, port = 0;
920 	bool filled = false;
921 
922 	err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
923 			  nldev_policy, NULL);
924 	/*
925 	 * Right now, we are expecting the device index to get res information,
926 	 * but it is possible to extend this code to return all devices in
927 	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
928 	 * if it doesn't exist, we will iterate over all devices.
929 	 *
930 	 * But it is not needed for now.
931 	 */
932 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
933 		return -EINVAL;
934 
935 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
936 	device = ib_device_get_by_index(index);
937 	if (!device)
938 		return -EINVAL;
939 
940 	/*
941 	 * If no PORT_INDEX is supplied, we will return all QPs from that device
942 	 */
943 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
944 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
945 		if (!rdma_is_port_valid(device, port)) {
946 			ret = -EINVAL;
947 			goto err_index;
948 		}
949 	}
950 
951 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
952 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
953 			0, NLM_F_MULTI);
954 
955 	if (fill_nldev_handle(skb, device)) {
956 		ret = -EMSGSIZE;
957 		goto err;
958 	}
959 
960 	table_attr = nla_nest_start(skb, fe->nldev_attr);
961 	if (!table_attr) {
962 		ret = -EMSGSIZE;
963 		goto err;
964 	}
965 
966 	down_read(&device->res.rwsem);
967 	hash_for_each_possible(device->res.hash, res, node, res_type) {
968 		if (idx < start)
969 			goto next;
970 
971 		if ((rdma_is_kernel_res(res) &&
972 		     task_active_pid_ns(current) != &init_pid_ns) ||
973 		    (!rdma_is_kernel_res(res) && task_active_pid_ns(current) !=
974 		     task_active_pid_ns(res->task)))
975 			/*
976 			 * 1. Kern resources should be visible in init
977 			 *    namspace only
978 			 * 2. Present only resources visible in the current
979 			 *    namespace
980 			 */
981 			goto next;
982 
983 		if (!rdma_restrack_get(res))
984 			/*
985 			 * Resource is under release now, but we are not
986 			 * relesing lock now, so it will be released in
987 			 * our next pass, once we will get ->next pointer.
988 			 */
989 			goto next;
990 
991 		filled = true;
992 
993 		up_read(&device->res.rwsem);
994 		ret = fe->fill_res_func(skb, cb, res, port);
995 		down_read(&device->res.rwsem);
996 		/*
997 		 * Return resource back, but it won't be released till
998 		 * the &device->res.rwsem will be released for write.
999 		 */
1000 		rdma_restrack_put(res);
1001 
1002 		if (ret == -EMSGSIZE)
1003 			/*
1004 			 * There is a chance to optimize here.
1005 			 * It can be done by using list_prepare_entry
1006 			 * and list_for_each_entry_continue afterwards.
1007 			 */
1008 			break;
1009 		if (ret)
1010 			goto res_err;
1011 next:		idx++;
1012 	}
1013 	up_read(&device->res.rwsem);
1014 
1015 	nla_nest_end(skb, table_attr);
1016 	nlmsg_end(skb, nlh);
1017 	cb->args[0] = idx;
1018 
1019 	/*
1020 	 * No more entries to fill, cancel the message and
1021 	 * return 0 to mark end of dumpit.
1022 	 */
1023 	if (!filled)
1024 		goto err;
1025 
1026 	put_device(&device->dev);
1027 	return skb->len;
1028 
1029 res_err:
1030 	nla_nest_cancel(skb, table_attr);
1031 	up_read(&device->res.rwsem);
1032 
1033 err:
1034 	nlmsg_cancel(skb, nlh);
1035 
1036 err_index:
1037 	put_device(&device->dev);
1038 	return ret;
1039 }
1040 
nldev_res_get_qp_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1041 static int nldev_res_get_qp_dumpit(struct sk_buff *skb,
1042 				   struct netlink_callback *cb)
1043 {
1044 	return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_QP);
1045 }
1046 
nldev_res_get_cm_id_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1047 static int nldev_res_get_cm_id_dumpit(struct sk_buff *skb,
1048 				      struct netlink_callback *cb)
1049 {
1050 	return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CM_ID);
1051 }
1052 
nldev_res_get_cq_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1053 static int nldev_res_get_cq_dumpit(struct sk_buff *skb,
1054 				   struct netlink_callback *cb)
1055 {
1056 	return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CQ);
1057 }
1058 
nldev_res_get_mr_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1059 static int nldev_res_get_mr_dumpit(struct sk_buff *skb,
1060 				   struct netlink_callback *cb)
1061 {
1062 	return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR);
1063 }
1064 
nldev_res_get_pd_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1065 static int nldev_res_get_pd_dumpit(struct sk_buff *skb,
1066 				   struct netlink_callback *cb)
1067 {
1068 	return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_PD);
1069 }
1070 
1071 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
1072 	[RDMA_NLDEV_CMD_GET] = {
1073 		.doit = nldev_get_doit,
1074 		.dump = nldev_get_dumpit,
1075 	},
1076 	[RDMA_NLDEV_CMD_PORT_GET] = {
1077 		.doit = nldev_port_get_doit,
1078 		.dump = nldev_port_get_dumpit,
1079 	},
1080 	[RDMA_NLDEV_CMD_RES_GET] = {
1081 		.doit = nldev_res_get_doit,
1082 		.dump = nldev_res_get_dumpit,
1083 	},
1084 	[RDMA_NLDEV_CMD_RES_QP_GET] = {
1085 		.dump = nldev_res_get_qp_dumpit,
1086 		/*
1087 		 * .doit is not implemented yet for two reasons:
1088 		 * 1. It is not needed yet.
1089 		 * 2. There is a need to provide identifier, while it is easy
1090 		 * for the QPs (device index + port index + LQPN), it is not
1091 		 * the case for the rest of resources (PD and CQ). Because it
1092 		 * is better to provide similar interface for all resources,
1093 		 * let's wait till we will have other resources implemented
1094 		 * too.
1095 		 */
1096 	},
1097 	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
1098 		.dump = nldev_res_get_cm_id_dumpit,
1099 	},
1100 	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
1101 		.dump = nldev_res_get_cq_dumpit,
1102 	},
1103 	[RDMA_NLDEV_CMD_RES_MR_GET] = {
1104 		.dump = nldev_res_get_mr_dumpit,
1105 	},
1106 	[RDMA_NLDEV_CMD_RES_PD_GET] = {
1107 		.dump = nldev_res_get_pd_dumpit,
1108 	},
1109 };
1110 
nldev_init(void)1111 void __init nldev_init(void)
1112 {
1113 	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
1114 }
1115 
nldev_exit(void)1116 void __exit nldev_exit(void)
1117 {
1118 	rdma_nl_unregister(RDMA_NL_NLDEV);
1119 }
1120 
1121 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
1122