]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/infiniband/core/nldev.c
RDMA/nldev: Prepare CAP_NET_ADMIN checks for .doit callbacks
[mirror_ubuntu-jammy-kernel.git] / drivers / infiniband / core / nldev.c
CommitLineData
6c80b41a
LR
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
e3bf14bd 33#include <linux/module.h>
bf3c5a93
LR
34#include <linux/pid.h>
35#include <linux/pid_namespace.h>
b4c598a6 36#include <net/netlink.h>
00313983 37#include <rdma/rdma_cm.h>
6c80b41a
LR
38#include <rdma/rdma_netlink.h>
39
40#include "core_priv.h"
00313983 41#include "cma_priv.h"
6c80b41a 42
b4c598a6
LR
43static 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 },
8621a7e3
LR
48 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING,
49 .len = IB_FW_VERSION_NAME_MAX - 1},
1aaff896
LR
50 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 },
51 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
12026fbb 52 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
80a06dd3
LR
53 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 },
54 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 },
34840fea 55 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 },
5654e49d
LR
56 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 },
57 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
1bb77b8c 58 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
bf3c5a93
LR
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 },
b5fa635a
LR
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 },
00313983
SW
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) },
a34fc089
SW
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 },
fccec5b8
SW
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 },
29cf1351
SW
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 },
5b2cc79d
LR
98 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 },
99 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING,
100 .len = IFNAMSIZ },
da5c8507
SW
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 },
b4c598a6
LR
110};
111
73937e8a
SW
112static 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
124static 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
136static 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
149int 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}
154EXPORT_SYMBOL(rdma_nl_put_driver_u32);
155
156int 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}
162EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
163
164int 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}
169EXPORT_SYMBOL(rdma_nl_put_driver_u64);
170
171int 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}
176EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
177
c2409810 178static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
b4c598a6
LR
179{
180 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
181 return -EMSGSIZE;
896de009
JG
182 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
183 dev_name(&device->dev)))
b4c598a6 184 return -EMSGSIZE;
c2409810
LR
185
186 return 0;
187}
188
189static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
190{
191 char fw[IB_FW_VERSION_NAME_MAX];
192
193 if (fill_nldev_handle(msg, device))
194 return -EMSGSIZE;
195
b4c598a6
LR
196 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
197 return -EMSGSIZE;
ac505253
LR
198
199 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
200 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
25a0ad85
SW
201 device->attrs.device_cap_flags,
202 RDMA_NLDEV_ATTR_PAD))
ac505253
LR
203 return -EMSGSIZE;
204
8621a7e3 205 ib_get_device_fw_str(device, fw);
5b2cc79d 206 /* Device without FW has strlen(fw) = 0 */
8621a7e3
LR
207 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
208 return -EMSGSIZE;
209
1aaff896 210 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
25a0ad85
SW
211 be64_to_cpu(device->node_guid),
212 RDMA_NLDEV_ATTR_PAD))
1aaff896
LR
213 return -EMSGSIZE;
214 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
25a0ad85
SW
215 be64_to_cpu(device->attrs.sys_image_guid),
216 RDMA_NLDEV_ATTR_PAD))
1aaff896 217 return -EMSGSIZE;
1bb77b8c
LR
218 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
219 return -EMSGSIZE;
b4c598a6
LR
220 return 0;
221}
222
7d02f605 223static int fill_port_info(struct sk_buff *msg,
5b2cc79d
LR
224 struct ib_device *device, u32 port,
225 const struct net *net)
7d02f605 226{
5b2cc79d 227 struct net_device *netdev = NULL;
ac505253
LR
228 struct ib_port_attr attr;
229 int ret;
4fa2813d 230 u64 cap_flags = 0;
ac505253 231
c2409810 232 if (fill_nldev_handle(msg, device))
7d02f605 233 return -EMSGSIZE;
c2409810 234
7d02f605
LR
235 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
236 return -EMSGSIZE;
ac505253
LR
237
238 ret = ib_query_port(device, port, &attr);
239 if (ret)
240 return ret;
241
80a06dd3 242 if (rdma_protocol_ib(device, port)) {
4fa2813d
MG
243 BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
244 sizeof(attr.port_cap_flags2)) > sizeof(u64));
245 cap_flags = attr.port_cap_flags |
246 ((u64)attr.port_cap_flags2 << 32);
dd8028f1 247 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
4fa2813d 248 cap_flags, RDMA_NLDEV_ATTR_PAD))
dd8028f1
LR
249 return -EMSGSIZE;
250 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
251 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
252 return -EMSGSIZE;
80a06dd3
LR
253 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
254 return -EMSGSIZE;
255 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
256 return -EMSGSIZE;
34840fea
LR
257 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
258 return -EMSGSIZE;
80a06dd3 259 }
5654e49d
LR
260 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
261 return -EMSGSIZE;
262 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
263 return -EMSGSIZE;
5b2cc79d 264
3023a1e9
KH
265 if (device->ops.get_netdev)
266 netdev = device->ops.get_netdev(device, port);
5b2cc79d
LR
267
268 if (netdev && net_eq(dev_net(netdev), net)) {
269 ret = nla_put_u32(msg,
270 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
271 if (ret)
272 goto out;
273 ret = nla_put_string(msg,
274 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
275 }
276
277out:
278 if (netdev)
279 dev_put(netdev);
280 return ret;
7d02f605
LR
281}
282
bf3c5a93
LR
283static int fill_res_info_entry(struct sk_buff *msg,
284 const char *name, u64 curr)
285{
286 struct nlattr *entry_attr;
287
288 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
289 if (!entry_attr)
290 return -EMSGSIZE;
291
292 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
293 goto err;
25a0ad85
SW
294 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
295 RDMA_NLDEV_ATTR_PAD))
bf3c5a93
LR
296 goto err;
297
298 nla_nest_end(msg, entry_attr);
299 return 0;
300
301err:
302 nla_nest_cancel(msg, entry_attr);
303 return -EMSGSIZE;
304}
305
306static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
307{
308 static const char * const names[RDMA_RESTRACK_MAX] = {
309 [RDMA_RESTRACK_PD] = "pd",
310 [RDMA_RESTRACK_CQ] = "cq",
311 [RDMA_RESTRACK_QP] = "qp",
00313983 312 [RDMA_RESTRACK_CM_ID] = "cm_id",
fccec5b8 313 [RDMA_RESTRACK_MR] = "mr",
ffd321e4 314 [RDMA_RESTRACK_CTX] = "ctx",
bf3c5a93
LR
315 };
316
317 struct rdma_restrack_root *res = &device->res;
318 struct nlattr *table_attr;
319 int ret, i, curr;
320
321 if (fill_nldev_handle(msg, device))
322 return -EMSGSIZE;
323
324 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
325 if (!table_attr)
326 return -EMSGSIZE;
327
328 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
329 if (!names[i])
330 continue;
331 curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
332 ret = fill_res_info_entry(msg, names[i], curr);
333 if (ret)
334 goto err;
335 }
336
337 nla_nest_end(msg, table_attr);
338 return 0;
339
340err:
341 nla_nest_cancel(msg, table_attr);
342 return ret;
343}
344
00313983
SW
345static int fill_res_name_pid(struct sk_buff *msg,
346 struct rdma_restrack_entry *res)
347{
348 /*
349 * For user resources, user is should read /proc/PID/comm to get the
350 * name of the task file.
351 */
352 if (rdma_is_kernel_res(res)) {
353 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
354 res->kern_name))
355 return -EMSGSIZE;
356 } else {
357 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
358 task_pid_vnr(res->task)))
359 return -EMSGSIZE;
360 }
361 return 0;
362}
363
659067b0 364static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
d12ff624 365 struct rdma_restrack_entry *res, uint32_t port)
b5fa635a 366{
d12ff624 367 struct ib_qp *qp = container_of(res, struct ib_qp, res);
da5c8507 368 struct rdma_restrack_root *resroot = &qp->device->res;
b5fa635a
LR
369 struct ib_qp_init_attr qp_init_attr;
370 struct nlattr *entry_attr;
371 struct ib_qp_attr qp_attr;
372 int ret;
373
374 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
375 if (ret)
376 return ret;
377
378 if (port && port != qp_attr.port_num)
379 return 0;
380
381 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
382 if (!entry_attr)
383 goto out;
384
385 /* In create_qp() port is not set yet */
386 if (qp_attr.port_num &&
387 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
388 goto err;
389
390 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
391 goto err;
392 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
393 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
394 qp_attr.dest_qp_num))
395 goto err;
396 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
397 qp_attr.rq_psn))
398 goto err;
399 }
400
401 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
402 goto err;
403
404 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
405 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
406 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
407 qp_attr.path_mig_state))
408 goto err;
409 }
410 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
411 goto err;
412 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
413 goto err;
414
00313983
SW
415 if (fill_res_name_pid(msg, res))
416 goto err;
417
da5c8507
SW
418 if (resroot->fill_res_entry(msg, res))
419 goto err;
420
00313983
SW
421 nla_nest_end(msg, entry_attr);
422 return 0;
423
424err:
425 nla_nest_cancel(msg, entry_attr);
426out:
427 return -EMSGSIZE;
428}
429
659067b0 430static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
00313983
SW
431 struct rdma_restrack_entry *res, uint32_t port)
432{
433 struct rdma_id_private *id_priv =
434 container_of(res, struct rdma_id_private, res);
da5c8507 435 struct rdma_restrack_root *resroot = &id_priv->id.device->res;
00313983
SW
436 struct rdma_cm_id *cm_id = &id_priv->id;
437 struct nlattr *entry_attr;
438
439 if (port && port != cm_id->port_num)
440 return 0;
441
442 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
443 if (!entry_attr)
444 goto out;
445
446 if (cm_id->port_num &&
447 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
448 goto err;
449
450 if (id_priv->qp_num) {
451 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
b5fa635a 452 goto err;
00313983 453 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
b5fa635a
LR
454 goto err;
455 }
456
00313983
SW
457 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
458 goto err;
459
460 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
461 goto err;
462
463 if (cm_id->route.addr.src_addr.ss_family &&
464 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
465 sizeof(cm_id->route.addr.src_addr),
466 &cm_id->route.addr.src_addr))
467 goto err;
468 if (cm_id->route.addr.dst_addr.ss_family &&
469 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
470 sizeof(cm_id->route.addr.dst_addr),
471 &cm_id->route.addr.dst_addr))
472 goto err;
473
474 if (fill_res_name_pid(msg, res))
475 goto err;
476
da5c8507
SW
477 if (resroot->fill_res_entry(msg, res))
478 goto err;
479
b5fa635a
LR
480 nla_nest_end(msg, entry_attr);
481 return 0;
482
483err:
484 nla_nest_cancel(msg, entry_attr);
485out:
486 return -EMSGSIZE;
487}
488
659067b0 489static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
a34fc089
SW
490 struct rdma_restrack_entry *res, uint32_t port)
491{
492 struct ib_cq *cq = container_of(res, struct ib_cq, res);
da5c8507 493 struct rdma_restrack_root *resroot = &cq->device->res;
a34fc089
SW
494 struct nlattr *entry_attr;
495
496 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CQ_ENTRY);
497 if (!entry_attr)
498 goto out;
499
500 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
501 goto err;
502 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
25a0ad85 503 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
a34fc089
SW
504 goto err;
505
506 /* Poll context is only valid for kernel CQs */
507 if (rdma_is_kernel_res(res) &&
508 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
509 goto err;
510
511 if (fill_res_name_pid(msg, res))
512 goto err;
513
da5c8507
SW
514 if (resroot->fill_res_entry(msg, res))
515 goto err;
516
a34fc089
SW
517 nla_nest_end(msg, entry_attr);
518 return 0;
519
520err:
521 nla_nest_cancel(msg, entry_attr);
522out:
523 return -EMSGSIZE;
524}
525
659067b0 526static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
fccec5b8
SW
527 struct rdma_restrack_entry *res, uint32_t port)
528{
529 struct ib_mr *mr = container_of(res, struct ib_mr, res);
da5c8507 530 struct rdma_restrack_root *resroot = &mr->pd->device->res;
fccec5b8
SW
531 struct nlattr *entry_attr;
532
533 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_MR_ENTRY);
534 if (!entry_attr)
535 goto out;
536
659067b0 537 if (has_cap_net_admin) {
fccec5b8
SW
538 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
539 goto err;
540 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
541 goto err;
fccec5b8
SW
542 }
543
25a0ad85
SW
544 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
545 RDMA_NLDEV_ATTR_PAD))
fccec5b8
SW
546 goto err;
547
548 if (fill_res_name_pid(msg, res))
549 goto err;
550
da5c8507
SW
551 if (resroot->fill_res_entry(msg, res))
552 goto err;
553
fccec5b8
SW
554 nla_nest_end(msg, entry_attr);
555 return 0;
556
557err:
558 nla_nest_cancel(msg, entry_attr);
559out:
560 return -EMSGSIZE;
561}
562
659067b0 563static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
29cf1351
SW
564 struct rdma_restrack_entry *res, uint32_t port)
565{
566 struct ib_pd *pd = container_of(res, struct ib_pd, res);
da5c8507 567 struct rdma_restrack_root *resroot = &pd->device->res;
29cf1351
SW
568 struct nlattr *entry_attr;
569
570 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_PD_ENTRY);
571 if (!entry_attr)
572 goto out;
573
659067b0 574 if (has_cap_net_admin) {
29cf1351
SW
575 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
576 pd->local_dma_lkey))
577 goto err;
578 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
579 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
580 pd->unsafe_global_rkey))
581 goto err;
582 }
583 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
25a0ad85 584 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
29cf1351
SW
585 goto err;
586 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
587 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
588 pd->unsafe_global_rkey))
589 goto err;
590
591 if (fill_res_name_pid(msg, res))
592 goto err;
593
da5c8507
SW
594 if (resroot->fill_res_entry(msg, res))
595 goto err;
596
29cf1351
SW
597 nla_nest_end(msg, entry_attr);
598 return 0;
599
600err:
601 nla_nest_cancel(msg, entry_attr);
602out:
603 return -EMSGSIZE;
604}
605
e5c9469e
LR
606static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
607 struct netlink_ext_ack *extack)
608{
609 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
610 struct ib_device *device;
611 struct sk_buff *msg;
612 u32 index;
613 int err;
614
615 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
616 nldev_policy, extack);
617 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
618 return -EINVAL;
619
620 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
621
f8978bd9 622 device = ib_device_get_by_index(index);
e5c9469e
LR
623 if (!device)
624 return -EINVAL;
625
626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
f8978bd9
LR
627 if (!msg) {
628 err = -ENOMEM;
629 goto err;
630 }
e5c9469e
LR
631
632 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
633 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
634 0, 0);
635
636 err = fill_dev_info(msg, device);
f8978bd9
LR
637 if (err)
638 goto err_free;
e5c9469e
LR
639
640 nlmsg_end(msg, nlh);
641
01b67117 642 ib_device_put(device);
e5c9469e 643 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
f8978bd9
LR
644
645err_free:
646 nlmsg_free(msg);
647err:
01b67117 648 ib_device_put(device);
f8978bd9 649 return err;
e5c9469e
LR
650}
651
05d940d3
LR
652static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
653 struct netlink_ext_ack *extack)
654{
655 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
656 struct ib_device *device;
657 u32 index;
658 int err;
659
660 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
661 extack);
662 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
663 return -EINVAL;
664
665 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
666 device = ib_device_get_by_index(index);
667 if (!device)
668 return -EINVAL;
669
670 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
671 char name[IB_DEVICE_NAME_MAX] = {};
672
673 nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
674 IB_DEVICE_NAME_MAX);
675 err = ib_device_rename(device, name);
676 }
677
01b67117 678 ib_device_put(device);
05d940d3
LR
679 return err;
680}
681
b4c598a6
LR
682static int _nldev_get_dumpit(struct ib_device *device,
683 struct sk_buff *skb,
684 struct netlink_callback *cb,
685 unsigned int idx)
686{
687 int start = cb->args[0];
688 struct nlmsghdr *nlh;
689
690 if (idx < start)
691 return 0;
692
693 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
694 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
695 0, NLM_F_MULTI);
696
697 if (fill_dev_info(skb, device)) {
698 nlmsg_cancel(skb, nlh);
699 goto out;
700 }
701
702 nlmsg_end(skb, nlh);
703
704 idx++;
705
706out: cb->args[0] = idx;
707 return skb->len;
708}
709
710static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
711{
712 /*
713 * There is no need to take lock, because
714 * we are relying on ib_core's lists_rwsem
715 */
716 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
717}
718
c3f66f7b
LR
719static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
720 struct netlink_ext_ack *extack)
721{
722 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
723 struct ib_device *device;
724 struct sk_buff *msg;
725 u32 index;
726 u32 port;
727 int err;
728
729 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
730 nldev_policy, extack);
287683d0
LR
731 if (err ||
732 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
733 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
c3f66f7b
LR
734 return -EINVAL;
735
736 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
f8978bd9 737 device = ib_device_get_by_index(index);
c3f66f7b
LR
738 if (!device)
739 return -EINVAL;
740
741 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
f8978bd9
LR
742 if (!rdma_is_port_valid(device, port)) {
743 err = -EINVAL;
744 goto err;
745 }
c3f66f7b
LR
746
747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
f8978bd9
LR
748 if (!msg) {
749 err = -ENOMEM;
750 goto err;
751 }
c3f66f7b
LR
752
753 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
754 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
755 0, 0);
756
5b2cc79d 757 err = fill_port_info(msg, device, port, sock_net(skb->sk));
f8978bd9
LR
758 if (err)
759 goto err_free;
c3f66f7b
LR
760
761 nlmsg_end(msg, nlh);
01b67117 762 ib_device_put(device);
c3f66f7b
LR
763
764 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
f8978bd9
LR
765
766err_free:
767 nlmsg_free(msg);
768err:
01b67117 769 ib_device_put(device);
f8978bd9 770 return err;
c3f66f7b
LR
771}
772
7d02f605
LR
773static int nldev_port_get_dumpit(struct sk_buff *skb,
774 struct netlink_callback *cb)
775{
776 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
777 struct ib_device *device;
778 int start = cb->args[0];
779 struct nlmsghdr *nlh;
780 u32 idx = 0;
781 u32 ifindex;
782 int err;
783 u32 p;
784
785 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
786 nldev_policy, NULL);
787 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
788 return -EINVAL;
789
790 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
f8978bd9 791 device = ib_device_get_by_index(ifindex);
7d02f605
LR
792 if (!device)
793 return -EINVAL;
794
795 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
796 /*
797 * The dumpit function returns all information from specific
798 * index. This specific index is taken from the netlink
799 * messages request sent by user and it is available
800 * in cb->args[0].
801 *
802 * Usually, the user doesn't fill this field and it causes
803 * to return everything.
804 *
805 */
806 if (idx < start) {
807 idx++;
808 continue;
809 }
810
811 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
812 cb->nlh->nlmsg_seq,
813 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
814 RDMA_NLDEV_CMD_PORT_GET),
815 0, NLM_F_MULTI);
816
5b2cc79d 817 if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
7d02f605
LR
818 nlmsg_cancel(skb, nlh);
819 goto out;
820 }
821 idx++;
822 nlmsg_end(skb, nlh);
823 }
824
f8978bd9 825out:
01b67117 826 ib_device_put(device);
f8978bd9 827 cb->args[0] = idx;
7d02f605
LR
828 return skb->len;
829}
830
bf3c5a93
LR
831static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
832 struct netlink_ext_ack *extack)
833{
834 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
835 struct ib_device *device;
836 struct sk_buff *msg;
837 u32 index;
838 int ret;
839
840 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
841 nldev_policy, extack);
842 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
843 return -EINVAL;
844
845 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
846 device = ib_device_get_by_index(index);
847 if (!device)
848 return -EINVAL;
849
850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
f34727a1
DC
851 if (!msg) {
852 ret = -ENOMEM;
bf3c5a93 853 goto err;
f34727a1 854 }
bf3c5a93
LR
855
856 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
857 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
858 0, 0);
859
860 ret = fill_res_info(msg, device);
861 if (ret)
862 goto err_free;
863
864 nlmsg_end(msg, nlh);
01b67117 865 ib_device_put(device);
bf3c5a93
LR
866 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
867
868err_free:
869 nlmsg_free(msg);
870err:
01b67117 871 ib_device_put(device);
bf3c5a93
LR
872 return ret;
873}
874
875static int _nldev_res_get_dumpit(struct ib_device *device,
876 struct sk_buff *skb,
877 struct netlink_callback *cb,
878 unsigned int idx)
879{
880 int start = cb->args[0];
881 struct nlmsghdr *nlh;
882
883 if (idx < start)
884 return 0;
885
886 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
887 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
888 0, NLM_F_MULTI);
889
890 if (fill_res_info(skb, device)) {
891 nlmsg_cancel(skb, nlh);
892 goto out;
893 }
894
895 nlmsg_end(skb, nlh);
896
897 idx++;
898
899out:
900 cb->args[0] = idx;
901 return skb->len;
902}
903
904static int nldev_res_get_dumpit(struct sk_buff *skb,
905 struct netlink_callback *cb)
906{
907 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
908}
909
d12ff624 910struct nldev_fill_res_entry {
659067b0 911 int (*fill_res_func)(struct sk_buff *msg, bool has_cap_net_admin,
d12ff624
SW
912 struct rdma_restrack_entry *res, u32 port);
913 enum rdma_nldev_attr nldev_attr;
914 enum rdma_nldev_command nldev_cmd;
915};
916
917static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
918 [RDMA_RESTRACK_QP] = {
919 .fill_res_func = fill_res_qp_entry,
920 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
921 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
922 },
00313983
SW
923 [RDMA_RESTRACK_CM_ID] = {
924 .fill_res_func = fill_res_cm_id_entry,
925 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
926 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
927 },
a34fc089
SW
928 [RDMA_RESTRACK_CQ] = {
929 .fill_res_func = fill_res_cq_entry,
930 .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
931 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
932 },
fccec5b8
SW
933 [RDMA_RESTRACK_MR] = {
934 .fill_res_func = fill_res_mr_entry,
935 .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
936 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
937 },
29cf1351
SW
938 [RDMA_RESTRACK_PD] = {
939 .fill_res_func = fill_res_pd_entry,
940 .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
941 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
942 },
d12ff624
SW
943};
944
8be565e6
LR
945static bool is_visible_in_pid_ns(struct rdma_restrack_entry *res)
946{
947 /*
948 * 1. Kern resources should be visible in init name space only
949 * 2. Present only resources visible in the current namespace
950 */
951 if (rdma_is_kernel_res(res))
952 return task_active_pid_ns(current) == &init_pid_ns;
953 return task_active_pid_ns(current) == task_active_pid_ns(res->task);
954}
955
d12ff624
SW
956static int res_get_common_dumpit(struct sk_buff *skb,
957 struct netlink_callback *cb,
958 enum rdma_restrack_type res_type)
b5fa635a 959{
d12ff624 960 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
b5fa635a
LR
961 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
962 struct rdma_restrack_entry *res;
963 int err, ret = 0, idx = 0;
964 struct nlattr *table_attr;
965 struct ib_device *device;
966 int start = cb->args[0];
659067b0 967 bool has_cap_net_admin;
b5fa635a
LR
968 struct nlmsghdr *nlh;
969 u32 index, port = 0;
d12ff624 970 bool filled = false;
b5fa635a
LR
971
972 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
973 nldev_policy, NULL);
974 /*
d12ff624 975 * Right now, we are expecting the device index to get res information,
b5fa635a
LR
976 * but it is possible to extend this code to return all devices in
977 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
978 * if it doesn't exist, we will iterate over all devices.
979 *
980 * But it is not needed for now.
981 */
982 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
983 return -EINVAL;
984
985 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
986 device = ib_device_get_by_index(index);
987 if (!device)
988 return -EINVAL;
989
990 /*
991 * If no PORT_INDEX is supplied, we will return all QPs from that device
992 */
993 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
994 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
995 if (!rdma_is_port_valid(device, port)) {
996 ret = -EINVAL;
997 goto err_index;
998 }
999 }
1000
1001 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
d12ff624 1002 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
b5fa635a
LR
1003 0, NLM_F_MULTI);
1004
1005 if (fill_nldev_handle(skb, device)) {
1006 ret = -EMSGSIZE;
1007 goto err;
1008 }
1009
d12ff624 1010 table_attr = nla_nest_start(skb, fe->nldev_attr);
b5fa635a
LR
1011 if (!table_attr) {
1012 ret = -EMSGSIZE;
1013 goto err;
1014 }
1015
659067b0
LR
1016 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1017
b5fa635a 1018 down_read(&device->res.rwsem);
d12ff624 1019 hash_for_each_possible(device->res.hash, res, node, res_type) {
b5fa635a
LR
1020 if (idx < start)
1021 goto next;
1022
8be565e6 1023 if (!is_visible_in_pid_ns(res))
b5fa635a
LR
1024 goto next;
1025
1026 if (!rdma_restrack_get(res))
1027 /*
1028 * Resource is under release now, but we are not
1029 * relesing lock now, so it will be released in
1030 * our next pass, once we will get ->next pointer.
1031 */
1032 goto next;
1033
d12ff624 1034 filled = true;
b5fa635a
LR
1035
1036 up_read(&device->res.rwsem);
659067b0 1037 ret = fe->fill_res_func(skb, has_cap_net_admin, res, port);
b5fa635a
LR
1038 down_read(&device->res.rwsem);
1039 /*
1040 * Return resource back, but it won't be released till
1041 * the &device->res.rwsem will be released for write.
1042 */
1043 rdma_restrack_put(res);
1044
1045 if (ret == -EMSGSIZE)
1046 /*
1047 * There is a chance to optimize here.
1048 * It can be done by using list_prepare_entry
1049 * and list_for_each_entry_continue afterwards.
1050 */
1051 break;
1052 if (ret)
1053 goto res_err;
1054next: idx++;
1055 }
1056 up_read(&device->res.rwsem);
1057
1058 nla_nest_end(skb, table_attr);
1059 nlmsg_end(skb, nlh);
1060 cb->args[0] = idx;
1061
1062 /*
d12ff624 1063 * No more entries to fill, cancel the message and
b5fa635a
LR
1064 * return 0 to mark end of dumpit.
1065 */
d12ff624 1066 if (!filled)
b5fa635a
LR
1067 goto err;
1068
01b67117 1069 ib_device_put(device);
b5fa635a
LR
1070 return skb->len;
1071
1072res_err:
1073 nla_nest_cancel(skb, table_attr);
1074 up_read(&device->res.rwsem);
1075
1076err:
1077 nlmsg_cancel(skb, nlh);
1078
1079err_index:
01b67117 1080 ib_device_put(device);
b5fa635a
LR
1081 return ret;
1082}
1083
f732e713
LR
1084#define RES_GET_FUNCS(name, type) \
1085 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \
1086 struct netlink_callback *cb) \
1087 { \
1088 return res_get_common_dumpit(skb, cb, type); \
1089 }
fccec5b8 1090
f732e713
LR
1091RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1092RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1093RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1094RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1095RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
29cf1351 1096
d0e312fe 1097static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
b4c598a6 1098 [RDMA_NLDEV_CMD_GET] = {
e5c9469e 1099 .doit = nldev_get_doit,
b4c598a6
LR
1100 .dump = nldev_get_dumpit,
1101 },
05d940d3
LR
1102 [RDMA_NLDEV_CMD_SET] = {
1103 .doit = nldev_set_doit,
1104 .flags = RDMA_NL_ADMIN_PERM,
1105 },
7d02f605 1106 [RDMA_NLDEV_CMD_PORT_GET] = {
c3f66f7b 1107 .doit = nldev_port_get_doit,
7d02f605
LR
1108 .dump = nldev_port_get_dumpit,
1109 },
bf3c5a93
LR
1110 [RDMA_NLDEV_CMD_RES_GET] = {
1111 .doit = nldev_res_get_doit,
1112 .dump = nldev_res_get_dumpit,
1113 },
b5fa635a
LR
1114 [RDMA_NLDEV_CMD_RES_QP_GET] = {
1115 .dump = nldev_res_get_qp_dumpit,
1116 /*
1117 * .doit is not implemented yet for two reasons:
1118 * 1. It is not needed yet.
1119 * 2. There is a need to provide identifier, while it is easy
1120 * for the QPs (device index + port index + LQPN), it is not
1121 * the case for the rest of resources (PD and CQ). Because it
1122 * is better to provide similar interface for all resources,
1123 * let's wait till we will have other resources implemented
1124 * too.
1125 */
1126 },
00313983
SW
1127 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
1128 .dump = nldev_res_get_cm_id_dumpit,
1129 },
a34fc089
SW
1130 [RDMA_NLDEV_CMD_RES_CQ_GET] = {
1131 .dump = nldev_res_get_cq_dumpit,
1132 },
fccec5b8
SW
1133 [RDMA_NLDEV_CMD_RES_MR_GET] = {
1134 .dump = nldev_res_get_mr_dumpit,
1135 },
29cf1351
SW
1136 [RDMA_NLDEV_CMD_RES_PD_GET] = {
1137 .dump = nldev_res_get_pd_dumpit,
1138 },
b4c598a6
LR
1139};
1140
6c80b41a
LR
1141void __init nldev_init(void)
1142{
b4c598a6 1143 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
6c80b41a
LR
1144}
1145
1146void __exit nldev_exit(void)
1147{
1148 rdma_nl_unregister(RDMA_NL_NLDEV);
1149}
e3bf14bd
JG
1150
1151MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);