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) },
85 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
87 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
89 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
95 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
97 char fw[IB_FW_VERSION_NAME_MAX];
99 if (fill_nldev_handle(msg, device))
102 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
105 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
106 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
107 device->attrs.device_cap_flags, 0))
110 ib_get_device_fw_str(device, fw);
111 /* Device without FW has strlen(fw) */
112 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
115 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
116 be64_to_cpu(device->node_guid), 0))
118 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
119 be64_to_cpu(device->attrs.sys_image_guid), 0))
121 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
126 static int fill_port_info(struct sk_buff *msg,
127 struct ib_device *device, u32 port)
129 struct ib_port_attr attr;
132 if (fill_nldev_handle(msg, device))
135 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
138 ret = ib_query_port(device, port, &attr);
142 BUILD_BUG_ON(sizeof(attr.port_cap_flags) > sizeof(u64));
143 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
144 (u64)attr.port_cap_flags, 0))
146 if (rdma_protocol_ib(device, port) &&
147 nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
148 attr.subnet_prefix, 0))
150 if (rdma_protocol_ib(device, port)) {
151 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
153 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
155 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
158 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
160 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
165 static int fill_res_info_entry(struct sk_buff *msg,
166 const char *name, u64 curr)
168 struct nlattr *entry_attr;
170 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
174 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
176 if (nla_put_u64_64bit(msg,
177 RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr, 0))
180 nla_nest_end(msg, entry_attr);
184 nla_nest_cancel(msg, entry_attr);
188 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
190 static const char * const names[RDMA_RESTRACK_MAX] = {
191 [RDMA_RESTRACK_PD] = "pd",
192 [RDMA_RESTRACK_CQ] = "cq",
193 [RDMA_RESTRACK_QP] = "qp",
194 [RDMA_RESTRACK_CM_ID] = "cm_id",
197 struct rdma_restrack_root *res = &device->res;
198 struct nlattr *table_attr;
201 if (fill_nldev_handle(msg, device))
204 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
208 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
211 curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
212 ret = fill_res_info_entry(msg, names[i], curr);
217 nla_nest_end(msg, table_attr);
221 nla_nest_cancel(msg, table_attr);
225 static int fill_res_name_pid(struct sk_buff *msg,
226 struct rdma_restrack_entry *res)
229 * For user resources, user is should read /proc/PID/comm to get the
230 * name of the task file.
232 if (rdma_is_kernel_res(res)) {
233 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
237 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
238 task_pid_vnr(res->task)))
244 static int fill_res_qp_entry(struct sk_buff *msg, struct netlink_callback *cb,
245 struct rdma_restrack_entry *res, uint32_t port)
247 struct ib_qp *qp = container_of(res, struct ib_qp, res);
248 struct ib_qp_init_attr qp_init_attr;
249 struct nlattr *entry_attr;
250 struct ib_qp_attr qp_attr;
253 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
257 if (port && port != qp_attr.port_num)
260 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
264 /* In create_qp() port is not set yet */
265 if (qp_attr.port_num &&
266 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
269 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
271 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
272 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
273 qp_attr.dest_qp_num))
275 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
280 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
283 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
284 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
285 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
286 qp_attr.path_mig_state))
289 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
291 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
294 if (fill_res_name_pid(msg, res))
297 nla_nest_end(msg, entry_attr);
301 nla_nest_cancel(msg, entry_attr);
306 static int fill_res_cm_id_entry(struct sk_buff *msg,
307 struct netlink_callback *cb,
308 struct rdma_restrack_entry *res, uint32_t port)
310 struct rdma_id_private *id_priv =
311 container_of(res, struct rdma_id_private, res);
312 struct rdma_cm_id *cm_id = &id_priv->id;
313 struct nlattr *entry_attr;
315 if (port && port != cm_id->port_num)
318 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
322 if (cm_id->port_num &&
323 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
326 if (id_priv->qp_num) {
327 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
329 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
333 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
336 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
339 if (cm_id->route.addr.src_addr.ss_family &&
340 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
341 sizeof(cm_id->route.addr.src_addr),
342 &cm_id->route.addr.src_addr))
344 if (cm_id->route.addr.dst_addr.ss_family &&
345 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
346 sizeof(cm_id->route.addr.dst_addr),
347 &cm_id->route.addr.dst_addr))
350 if (fill_res_name_pid(msg, res))
353 nla_nest_end(msg, entry_attr);
357 nla_nest_cancel(msg, entry_attr);
362 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
363 struct netlink_ext_ack *extack)
365 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
366 struct ib_device *device;
371 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
372 nldev_policy, extack);
373 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
376 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
378 device = ib_device_get_by_index(index);
382 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
388 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
389 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
392 err = fill_dev_info(msg, device);
398 put_device(&device->dev);
399 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
404 put_device(&device->dev);
408 static int _nldev_get_dumpit(struct ib_device *device,
410 struct netlink_callback *cb,
413 int start = cb->args[0];
414 struct nlmsghdr *nlh;
419 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
420 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
423 if (fill_dev_info(skb, device)) {
424 nlmsg_cancel(skb, nlh);
432 out: cb->args[0] = idx;
436 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
439 * There is no need to take lock, because
440 * we are relying on ib_core's lists_rwsem
442 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
445 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
446 struct netlink_ext_ack *extack)
448 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
449 struct ib_device *device;
455 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
456 nldev_policy, extack);
458 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
459 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
462 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
463 device = ib_device_get_by_index(index);
467 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
468 if (!rdma_is_port_valid(device, port)) {
473 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
479 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
480 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
483 err = fill_port_info(msg, device, port);
488 put_device(&device->dev);
490 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
495 put_device(&device->dev);
499 static int nldev_port_get_dumpit(struct sk_buff *skb,
500 struct netlink_callback *cb)
502 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
503 struct ib_device *device;
504 int start = cb->args[0];
505 struct nlmsghdr *nlh;
511 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
513 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
516 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
517 device = ib_device_get_by_index(ifindex);
521 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
523 * The dumpit function returns all information from specific
524 * index. This specific index is taken from the netlink
525 * messages request sent by user and it is available
528 * Usually, the user doesn't fill this field and it causes
529 * to return everything.
537 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
539 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
540 RDMA_NLDEV_CMD_PORT_GET),
543 if (fill_port_info(skb, device, p)) {
544 nlmsg_cancel(skb, nlh);
552 put_device(&device->dev);
557 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
558 struct netlink_ext_ack *extack)
560 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
561 struct ib_device *device;
566 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
567 nldev_policy, extack);
568 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
571 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
572 device = ib_device_get_by_index(index);
576 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
582 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
583 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
586 ret = fill_res_info(msg, device);
591 put_device(&device->dev);
592 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
597 put_device(&device->dev);
601 static int _nldev_res_get_dumpit(struct ib_device *device,
603 struct netlink_callback *cb,
606 int start = cb->args[0];
607 struct nlmsghdr *nlh;
612 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
613 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
616 if (fill_res_info(skb, device)) {
617 nlmsg_cancel(skb, nlh);
630 static int nldev_res_get_dumpit(struct sk_buff *skb,
631 struct netlink_callback *cb)
633 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
636 struct nldev_fill_res_entry {
637 int (*fill_res_func)(struct sk_buff *msg, struct netlink_callback *cb,
638 struct rdma_restrack_entry *res, u32 port);
639 enum rdma_nldev_attr nldev_attr;
640 enum rdma_nldev_command nldev_cmd;
643 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
644 [RDMA_RESTRACK_QP] = {
645 .fill_res_func = fill_res_qp_entry,
646 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
647 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
649 [RDMA_RESTRACK_CM_ID] = {
650 .fill_res_func = fill_res_cm_id_entry,
651 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
652 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
656 static int res_get_common_dumpit(struct sk_buff *skb,
657 struct netlink_callback *cb,
658 enum rdma_restrack_type res_type)
660 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
661 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
662 struct rdma_restrack_entry *res;
663 int err, ret = 0, idx = 0;
664 struct nlattr *table_attr;
665 struct ib_device *device;
666 int start = cb->args[0];
667 struct nlmsghdr *nlh;
671 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
674 * Right now, we are expecting the device index to get res information,
675 * but it is possible to extend this code to return all devices in
676 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
677 * if it doesn't exist, we will iterate over all devices.
679 * But it is not needed for now.
681 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
684 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
685 device = ib_device_get_by_index(index);
690 * If no PORT_INDEX is supplied, we will return all QPs from that device
692 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
693 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
694 if (!rdma_is_port_valid(device, port)) {
700 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
701 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
704 if (fill_nldev_handle(skb, device)) {
709 table_attr = nla_nest_start(skb, fe->nldev_attr);
715 down_read(&device->res.rwsem);
716 hash_for_each_possible(device->res.hash, res, node, res_type) {
720 if ((rdma_is_kernel_res(res) &&
721 task_active_pid_ns(current) != &init_pid_ns) ||
722 (!rdma_is_kernel_res(res) && task_active_pid_ns(current) !=
723 task_active_pid_ns(res->task)))
725 * 1. Kern resources should be visible in init
727 * 2. Present only resources visible in the current
732 if (!rdma_restrack_get(res))
734 * Resource is under release now, but we are not
735 * relesing lock now, so it will be released in
736 * our next pass, once we will get ->next pointer.
742 up_read(&device->res.rwsem);
743 ret = fe->fill_res_func(skb, cb, res, port);
744 down_read(&device->res.rwsem);
746 * Return resource back, but it won't be released till
747 * the &device->res.rwsem will be released for write.
749 rdma_restrack_put(res);
751 if (ret == -EMSGSIZE)
753 * There is a chance to optimize here.
754 * It can be done by using list_prepare_entry
755 * and list_for_each_entry_continue afterwards.
762 up_read(&device->res.rwsem);
764 nla_nest_end(skb, table_attr);
769 * No more entries to fill, cancel the message and
770 * return 0 to mark end of dumpit.
775 put_device(&device->dev);
779 nla_nest_cancel(skb, table_attr);
780 up_read(&device->res.rwsem);
783 nlmsg_cancel(skb, nlh);
786 put_device(&device->dev);
790 static int nldev_res_get_qp_dumpit(struct sk_buff *skb,
791 struct netlink_callback *cb)
793 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_QP);
796 static int nldev_res_get_cm_id_dumpit(struct sk_buff *skb,
797 struct netlink_callback *cb)
799 return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CM_ID);
802 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
803 [RDMA_NLDEV_CMD_GET] = {
804 .doit = nldev_get_doit,
805 .dump = nldev_get_dumpit,
807 [RDMA_NLDEV_CMD_PORT_GET] = {
808 .doit = nldev_port_get_doit,
809 .dump = nldev_port_get_dumpit,
811 [RDMA_NLDEV_CMD_RES_GET] = {
812 .doit = nldev_res_get_doit,
813 .dump = nldev_res_get_dumpit,
815 [RDMA_NLDEV_CMD_RES_QP_GET] = {
816 .dump = nldev_res_get_qp_dumpit,
818 * .doit is not implemented yet for two reasons:
819 * 1. It is not needed yet.
820 * 2. There is a need to provide identifier, while it is easy
821 * for the QPs (device index + port index + LQPN), it is not
822 * the case for the rest of resources (PD and CQ). Because it
823 * is better to provide similar interface for all resources,
824 * let's wait till we will have other resources implemented
828 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
829 .dump = nldev_res_get_cm_id_dumpit,
833 void __init nldev_init(void)
835 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
838 void __exit nldev_exit(void)
840 rdma_nl_unregister(RDMA_NL_NLDEV);
843 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);