2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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>
40 #include "core_priv.h"
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,
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,
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 },
112 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
113 enum rdma_nldev_print_type print_type)
115 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
117 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
118 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
124 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
125 enum rdma_nldev_print_type print_type,
128 if (put_driver_name_print_type(msg, name, print_type))
130 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
136 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
137 enum rdma_nldev_print_type print_type,
140 if (put_driver_name_print_type(msg, name, print_type))
142 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
143 RDMA_NLDEV_ATTR_PAD))
149 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
151 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
154 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
156 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
159 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
162 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
164 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
166 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
169 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
171 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
173 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
176 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
178 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
180 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
182 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
188 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
190 char fw[IB_FW_VERSION_NAME_MAX];
192 if (fill_nldev_handle(msg, device))
195 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
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))
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))
209 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
210 be64_to_cpu(device->node_guid),
211 RDMA_NLDEV_ATTR_PAD))
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))
217 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
222 static int fill_port_info(struct sk_buff *msg,
223 struct ib_device *device, u32 port,
224 const struct net *net)
226 struct net_device *netdev = NULL;
227 struct ib_port_attr attr;
230 if (fill_nldev_handle(msg, device))
233 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
236 ret = ib_query_port(device, port, &attr);
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))
246 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
247 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
249 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
251 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
253 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
256 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
258 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
261 if (device->get_netdev)
262 netdev = device->get_netdev(device, port);
264 if (netdev && net_eq(dev_net(netdev), net)) {
265 ret = nla_put_u32(msg,
266 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
269 ret = nla_put_string(msg,
270 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
279 static int fill_res_info_entry(struct sk_buff *msg,
280 const char *name, u64 curr)
282 struct nlattr *entry_attr;
284 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
288 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
290 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
291 RDMA_NLDEV_ATTR_PAD))
294 nla_nest_end(msg, entry_attr);
298 nla_nest_cancel(msg, entry_attr);
302 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
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",
312 struct rdma_restrack_root *res = &device->res;
313 struct nlattr *table_attr;
316 if (fill_nldev_handle(msg, device))
319 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
323 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
326 curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
327 ret = fill_res_info_entry(msg, names[i], curr);
332 nla_nest_end(msg, table_attr);
336 nla_nest_cancel(msg, table_attr);
340 static int fill_res_name_pid(struct sk_buff *msg,
341 struct rdma_restrack_entry *res)
344 * For user resources, user is should read /proc/PID/comm to get the
345 * name of the task file.
347 if (rdma_is_kernel_res(res)) {
348 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
352 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
353 task_pid_vnr(res->task)))
359 static int fill_res_qp_entry(struct sk_buff *msg, struct netlink_callback *cb,
360 struct rdma_restrack_entry *res, uint32_t port)
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;
369 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
373 if (port && port != qp_attr.port_num)
376 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
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))
385 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
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))
391 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
396 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
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))
405 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
407 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
410 if (fill_res_name_pid(msg, res))
413 if (resroot->fill_res_entry(msg, res))
416 nla_nest_end(msg, entry_attr);
420 nla_nest_cancel(msg, entry_attr);
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)
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;
435 if (port && port != cm_id->port_num)
438 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
442 if (cm_id->port_num &&
443 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
446 if (id_priv->qp_num) {
447 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
449 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
453 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
456 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
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))
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))
470 if (fill_res_name_pid(msg, res))
473 if (resroot->fill_res_entry(msg, res))
476 nla_nest_end(msg, entry_attr);
480 nla_nest_cancel(msg, entry_attr);
485 static int fill_res_cq_entry(struct sk_buff *msg, struct netlink_callback *cb,
486 struct rdma_restrack_entry *res, uint32_t port)
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;
492 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CQ_ENTRY);
496 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
498 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
499 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
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))
507 if (fill_res_name_pid(msg, res))
510 if (resroot->fill_res_entry(msg, res))
513 nla_nest_end(msg, entry_attr);
517 nla_nest_cancel(msg, entry_attr);
522 static int fill_res_mr_entry(struct sk_buff *msg, struct netlink_callback *cb,
523 struct rdma_restrack_entry *res, uint32_t port)
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;
529 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_MR_ENTRY);
533 if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
534 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
536 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
540 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
541 RDMA_NLDEV_ATTR_PAD))
544 if (fill_res_name_pid(msg, res))
547 if (resroot->fill_res_entry(msg, res))
550 nla_nest_end(msg, entry_attr);
554 nla_nest_cancel(msg, entry_attr);
559 static int fill_res_pd_entry(struct sk_buff *msg, struct netlink_callback *cb,
560 struct rdma_restrack_entry *res, uint32_t port)
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;
566 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_PD_ENTRY);
570 if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
571 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
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))
579 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
580 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
583 if (fill_res_name_pid(msg, res))
586 if (resroot->fill_res_entry(msg, res))
589 nla_nest_end(msg, entry_attr);
593 nla_nest_cancel(msg, entry_attr);
598 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
599 struct netlink_ext_ack *extack)
601 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
602 struct ib_device *device;
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])
612 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
614 device = ib_device_get_by_index(index);
618 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
624 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
625 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
628 err = fill_dev_info(msg, device);
634 put_device(&device->dev);
635 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
640 put_device(&device->dev);
644 static int _nldev_get_dumpit(struct ib_device *device,
646 struct netlink_callback *cb,
649 int start = cb->args[0];
650 struct nlmsghdr *nlh;
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),
659 if (fill_dev_info(skb, device)) {
660 nlmsg_cancel(skb, nlh);
668 out: cb->args[0] = idx;
672 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
675 * There is no need to take lock, because
676 * we are relying on ib_core's lists_rwsem
678 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
681 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
682 struct netlink_ext_ack *extack)
684 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
685 struct ib_device *device;
691 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
692 nldev_policy, extack);
694 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
695 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
698 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
699 device = ib_device_get_by_index(index);
703 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
704 if (!rdma_is_port_valid(device, port)) {
709 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
715 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
716 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
719 err = fill_port_info(msg, device, port, sock_net(skb->sk));
724 put_device(&device->dev);
726 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
731 put_device(&device->dev);
735 static int nldev_port_get_dumpit(struct sk_buff *skb,
736 struct netlink_callback *cb)
738 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
739 struct ib_device *device;
740 int start = cb->args[0];
741 struct nlmsghdr *nlh;
747 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
749 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
752 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
753 device = ib_device_get_by_index(ifindex);
757 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
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
764 * Usually, the user doesn't fill this field and it causes
765 * to return everything.
773 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
775 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
776 RDMA_NLDEV_CMD_PORT_GET),
779 if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
780 nlmsg_cancel(skb, nlh);
788 put_device(&device->dev);
793 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
794 struct netlink_ext_ack *extack)
796 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
797 struct ib_device *device;
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])
807 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
808 device = ib_device_get_by_index(index);
812 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
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),
822 ret = fill_res_info(msg, device);
827 put_device(&device->dev);
828 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
833 put_device(&device->dev);
837 static int _nldev_res_get_dumpit(struct ib_device *device,
839 struct netlink_callback *cb,
842 int start = cb->args[0];
843 struct nlmsghdr *nlh;
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),
852 if (fill_res_info(skb, device)) {
853 nlmsg_cancel(skb, nlh);
866 static int nldev_res_get_dumpit(struct sk_buff *skb,
867 struct netlink_callback *cb)
869 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
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;
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,
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,
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,
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,
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,
907 static int res_get_common_dumpit(struct sk_buff *skb,
908 struct netlink_callback *cb,
909 enum rdma_restrack_type res_type)
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;
922 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
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.
930 * But it is not needed for now.
932 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
935 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
936 device = ib_device_get_by_index(index);
941 * If no PORT_INDEX is supplied, we will return all QPs from that device
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)) {
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),
955 if (fill_nldev_handle(skb, device)) {
960 table_attr = nla_nest_start(skb, fe->nldev_attr);
966 down_read(&device->res.rwsem);
967 hash_for_each_possible(device->res.hash, res, node, res_type) {
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)))
976 * 1. Kern resources should be visible in init
978 * 2. Present only resources visible in the current
983 if (!rdma_restrack_get(res))
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.
993 up_read(&device->res.rwsem);
994 ret = fe->fill_res_func(skb, cb, res, port);
995 down_read(&device->res.rwsem);
997 * Return resource back, but it won't be released till
998 * the &device->res.rwsem will be released for write.
1000 rdma_restrack_put(res);
1002 if (ret == -EMSGSIZE)
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.
1013 up_read(&device->res.rwsem);
1015 nla_nest_end(skb, table_attr);
1016 nlmsg_end(skb, nlh);
1020 * No more entries to fill, cancel the message and
1021 * return 0 to mark end of dumpit.
1026 put_device(&device->dev);
1030 nla_nest_cancel(skb, table_attr);
1031 up_read(&device->res.rwsem);
1034 nlmsg_cancel(skb, nlh);
1037 put_device(&device->dev);
1041 static int nldev_res_get_qp_dumpit(struct sk_buff *skb,
1042 struct netlink_callback *cb)
1044 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_QP);
1047 static int nldev_res_get_cm_id_dumpit(struct sk_buff *skb,
1048 struct netlink_callback *cb)
1050 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CM_ID);
1053 static int nldev_res_get_cq_dumpit(struct sk_buff *skb,
1054 struct netlink_callback *cb)
1056 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CQ);
1059 static int nldev_res_get_mr_dumpit(struct sk_buff *skb,
1060 struct netlink_callback *cb)
1062 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR);
1065 static int nldev_res_get_pd_dumpit(struct sk_buff *skb,
1066 struct netlink_callback *cb)
1068 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_PD);
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,
1076 [RDMA_NLDEV_CMD_PORT_GET] = {
1077 .doit = nldev_port_get_doit,
1078 .dump = nldev_port_get_dumpit,
1080 [RDMA_NLDEV_CMD_RES_GET] = {
1081 .doit = nldev_res_get_doit,
1082 .dump = nldev_res_get_dumpit,
1084 [RDMA_NLDEV_CMD_RES_QP_GET] = {
1085 .dump = nldev_res_get_qp_dumpit,
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
1097 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
1098 .dump = nldev_res_get_cm_id_dumpit,
1100 [RDMA_NLDEV_CMD_RES_CQ_GET] = {
1101 .dump = nldev_res_get_cq_dumpit,
1103 [RDMA_NLDEV_CMD_RES_MR_GET] = {
1104 .dump = nldev_res_get_mr_dumpit,
1106 [RDMA_NLDEV_CMD_RES_PD_GET] = {
1107 .dump = nldev_res_get_pd_dumpit,
1111 void __init nldev_init(void)
1113 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
1116 void __exit nldev_exit(void)
1118 rdma_nl_unregister(RDMA_NL_NLDEV);
1121 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);