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 <linux/mutex.h>
37 #include <net/netlink.h>
38 #include <rdma/rdma_cm.h>
39 #include <rdma/rdma_netlink.h>
41 #include "core_priv.h"
46 * Sort array elements by the netlink attribute name
48 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
49 [RDMA_NLDEV_ATTR_CHARDEV] = { .type = NLA_U64 },
50 [RDMA_NLDEV_ATTR_CHARDEV_ABI] = { .type = NLA_U64 },
51 [RDMA_NLDEV_ATTR_CHARDEV_NAME] = { .type = NLA_NUL_STRING,
52 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
53 [RDMA_NLDEV_ATTR_CHARDEV_TYPE] = { .type = NLA_NUL_STRING,
54 .len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
55 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
56 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
57 .len = IB_DEVICE_NAME_MAX },
58 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
59 [RDMA_NLDEV_ATTR_DEV_PROTOCOL] = { .type = NLA_NUL_STRING,
60 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
61 [RDMA_NLDEV_ATTR_DRIVER] = { .type = NLA_NESTED },
62 [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = { .type = NLA_NESTED },
63 [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = { .type = NLA_U8 },
64 [RDMA_NLDEV_ATTR_DRIVER_STRING] = { .type = NLA_NUL_STRING,
65 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
66 [RDMA_NLDEV_ATTR_DRIVER_S32] = { .type = NLA_S32 },
67 [RDMA_NLDEV_ATTR_DRIVER_S64] = { .type = NLA_S64 },
68 [RDMA_NLDEV_ATTR_DRIVER_U32] = { .type = NLA_U32 },
69 [RDMA_NLDEV_ATTR_DRIVER_U64] = { .type = NLA_U64 },
70 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING,
71 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
72 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 },
73 [RDMA_NLDEV_ATTR_LINK_TYPE] = { .type = NLA_NUL_STRING,
75 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 },
76 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 },
77 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING,
79 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 },
80 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
81 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
82 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 },
83 [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED },
84 [RDMA_NLDEV_ATTR_RES_CM_IDN] = { .type = NLA_U32 },
85 [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED },
86 [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED },
87 [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 },
88 [RDMA_NLDEV_ATTR_RES_CQN] = { .type = NLA_U32 },
89 [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED },
90 [RDMA_NLDEV_ATTR_RES_CTXN] = { .type = NLA_U32 },
91 [RDMA_NLDEV_ATTR_RES_DST_ADDR] = {
92 .len = sizeof(struct __kernel_sockaddr_storage) },
93 [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 },
94 [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING,
95 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
96 [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 },
97 [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 },
98 [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 },
99 [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED },
100 [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 },
101 [RDMA_NLDEV_ATTR_RES_MRN] = { .type = NLA_U32 },
102 [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED },
103 [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
104 [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED },
105 [RDMA_NLDEV_ATTR_RES_PDN] = { .type = NLA_U32 },
106 [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED },
107 [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 },
108 [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 },
109 [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 },
110 [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED },
111 [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED },
112 [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 },
113 [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 },
114 [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 },
115 [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 },
116 [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = {
117 .len = sizeof(struct __kernel_sockaddr_storage) },
118 [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 },
119 [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED },
120 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED },
121 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
122 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
123 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
124 [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 },
125 [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
126 [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 },
127 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 },
128 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
129 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
130 [RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID] = { .type = NLA_U32 },
131 [RDMA_NLDEV_NET_NS_FD] = { .type = NLA_U32 },
132 [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = { .type = NLA_U8 },
135 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
136 enum rdma_nldev_print_type print_type)
138 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
140 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
141 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
147 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
148 enum rdma_nldev_print_type print_type,
151 if (put_driver_name_print_type(msg, name, print_type))
153 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
159 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
160 enum rdma_nldev_print_type print_type,
163 if (put_driver_name_print_type(msg, name, print_type))
165 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
166 RDMA_NLDEV_ATTR_PAD))
172 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
174 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
177 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
179 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
182 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
185 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
187 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
189 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
192 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
194 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
196 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
199 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
201 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
203 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
205 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
206 dev_name(&device->dev)))
212 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
214 char fw[IB_FW_VERSION_NAME_MAX];
218 if (fill_nldev_handle(msg, device))
221 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
224 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
225 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
226 device->attrs.device_cap_flags,
227 RDMA_NLDEV_ATTR_PAD))
230 ib_get_device_fw_str(device, fw);
231 /* Device without FW has strlen(fw) = 0 */
232 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
235 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
236 be64_to_cpu(device->node_guid),
237 RDMA_NLDEV_ATTR_PAD))
239 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
240 be64_to_cpu(device->attrs.sys_image_guid),
241 RDMA_NLDEV_ATTR_PAD))
243 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
247 * Link type is determined on first port and mlx4 device
248 * which can potentially have two different link type for the same
249 * IB device is considered as better to be avoided in the future,
251 port = rdma_start_port(device);
252 if (rdma_cap_opa_mad(device, port))
253 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
254 else if (rdma_protocol_ib(device, port))
255 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
256 else if (rdma_protocol_iwarp(device, port))
257 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
258 else if (rdma_protocol_roce(device, port))
259 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
260 else if (rdma_protocol_usnic(device, port))
261 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
266 static int fill_port_info(struct sk_buff *msg,
267 struct ib_device *device, u32 port,
268 const struct net *net)
270 struct net_device *netdev = NULL;
271 struct ib_port_attr attr;
275 if (fill_nldev_handle(msg, device))
278 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
281 ret = ib_query_port(device, port, &attr);
285 if (rdma_protocol_ib(device, port)) {
286 BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
287 sizeof(attr.port_cap_flags2)) > sizeof(u64));
288 cap_flags = attr.port_cap_flags |
289 ((u64)attr.port_cap_flags2 << 32);
290 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
291 cap_flags, RDMA_NLDEV_ATTR_PAD))
293 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
294 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
296 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
298 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
300 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
303 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
305 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
308 netdev = ib_device_get_netdev(device, port);
309 if (netdev && net_eq(dev_net(netdev), net)) {
310 ret = nla_put_u32(msg,
311 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
314 ret = nla_put_string(msg,
315 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
324 static int fill_res_info_entry(struct sk_buff *msg,
325 const char *name, u64 curr)
327 struct nlattr *entry_attr;
329 entry_attr = nla_nest_start_noflag(msg,
330 RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
334 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
336 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
337 RDMA_NLDEV_ATTR_PAD))
340 nla_nest_end(msg, entry_attr);
344 nla_nest_cancel(msg, entry_attr);
348 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
350 static const char * const names[RDMA_RESTRACK_MAX] = {
351 [RDMA_RESTRACK_PD] = "pd",
352 [RDMA_RESTRACK_CQ] = "cq",
353 [RDMA_RESTRACK_QP] = "qp",
354 [RDMA_RESTRACK_CM_ID] = "cm_id",
355 [RDMA_RESTRACK_MR] = "mr",
356 [RDMA_RESTRACK_CTX] = "ctx",
359 struct nlattr *table_attr;
362 if (fill_nldev_handle(msg, device))
365 table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
369 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
372 curr = rdma_restrack_count(device, i,
373 task_active_pid_ns(current));
374 ret = fill_res_info_entry(msg, names[i], curr);
379 nla_nest_end(msg, table_attr);
383 nla_nest_cancel(msg, table_attr);
387 static int fill_res_name_pid(struct sk_buff *msg,
388 struct rdma_restrack_entry *res)
391 * For user resources, user is should read /proc/PID/comm to get the
392 * name of the task file.
394 if (rdma_is_kernel_res(res)) {
395 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
399 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
400 task_pid_vnr(res->task)))
406 static bool fill_res_entry(struct ib_device *dev, struct sk_buff *msg,
407 struct rdma_restrack_entry *res)
409 if (!dev->ops.fill_res_entry)
411 return dev->ops.fill_res_entry(msg, res);
414 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
415 struct rdma_restrack_entry *res, uint32_t port)
417 struct ib_qp *qp = container_of(res, struct ib_qp, res);
418 struct ib_device *dev = qp->device;
419 struct ib_qp_init_attr qp_init_attr;
420 struct ib_qp_attr qp_attr;
423 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
427 if (port && port != qp_attr.port_num)
430 /* In create_qp() port is not set yet */
431 if (qp_attr.port_num &&
432 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
435 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
437 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
438 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
439 qp_attr.dest_qp_num))
441 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
446 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
449 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
450 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
451 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
452 qp_attr.path_mig_state))
455 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
457 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
460 if (!rdma_is_kernel_res(res) &&
461 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
464 if (fill_res_name_pid(msg, res))
467 if (fill_res_entry(dev, msg, res))
472 err: return -EMSGSIZE;
475 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
476 struct rdma_restrack_entry *res, uint32_t port)
478 struct rdma_id_private *id_priv =
479 container_of(res, struct rdma_id_private, res);
480 struct ib_device *dev = id_priv->id.device;
481 struct rdma_cm_id *cm_id = &id_priv->id;
483 if (port && port != cm_id->port_num)
486 if (cm_id->port_num &&
487 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
490 if (id_priv->qp_num) {
491 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
493 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
497 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
500 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
503 if (cm_id->route.addr.src_addr.ss_family &&
504 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
505 sizeof(cm_id->route.addr.src_addr),
506 &cm_id->route.addr.src_addr))
508 if (cm_id->route.addr.dst_addr.ss_family &&
509 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
510 sizeof(cm_id->route.addr.dst_addr),
511 &cm_id->route.addr.dst_addr))
514 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
517 if (fill_res_name_pid(msg, res))
520 if (fill_res_entry(dev, msg, res))
525 err: return -EMSGSIZE;
528 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
529 struct rdma_restrack_entry *res, uint32_t port)
531 struct ib_cq *cq = container_of(res, struct ib_cq, res);
532 struct ib_device *dev = cq->device;
534 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
536 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
537 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
540 /* Poll context is only valid for kernel CQs */
541 if (rdma_is_kernel_res(res) &&
542 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
545 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
547 if (!rdma_is_kernel_res(res) &&
548 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
549 cq->uobject->context->res.id))
552 if (fill_res_name_pid(msg, res))
555 if (fill_res_entry(dev, msg, res))
560 err: return -EMSGSIZE;
563 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
564 struct rdma_restrack_entry *res, uint32_t port)
566 struct ib_mr *mr = container_of(res, struct ib_mr, res);
567 struct ib_device *dev = mr->pd->device;
569 if (has_cap_net_admin) {
570 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
572 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
576 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
577 RDMA_NLDEV_ATTR_PAD))
580 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
583 if (!rdma_is_kernel_res(res) &&
584 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
587 if (fill_res_name_pid(msg, res))
590 if (fill_res_entry(dev, msg, res))
595 err: return -EMSGSIZE;
598 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
599 struct rdma_restrack_entry *res, uint32_t port)
601 struct ib_pd *pd = container_of(res, struct ib_pd, res);
602 struct ib_device *dev = pd->device;
604 if (has_cap_net_admin) {
605 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
608 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
609 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
610 pd->unsafe_global_rkey))
613 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
614 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
617 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
620 if (!rdma_is_kernel_res(res) &&
621 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
622 pd->uobject->context->res.id))
625 if (fill_res_name_pid(msg, res))
628 if (fill_res_entry(dev, msg, res))
633 err: return -EMSGSIZE;
636 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
637 struct netlink_ext_ack *extack)
639 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
640 struct ib_device *device;
645 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
646 nldev_policy, extack);
647 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
650 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
652 device = ib_device_get_by_index(sock_net(skb->sk), index);
656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
662 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
663 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
666 err = fill_dev_info(msg, device);
672 ib_device_put(device);
673 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
678 ib_device_put(device);
682 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
683 struct netlink_ext_ack *extack)
685 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
686 struct ib_device *device;
690 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
691 nldev_policy, extack);
692 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
695 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
696 device = ib_device_get_by_index(sock_net(skb->sk), index);
700 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
701 char name[IB_DEVICE_NAME_MAX] = {};
703 nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
705 err = ib_device_rename(device, name);
709 if (tb[RDMA_NLDEV_NET_NS_FD]) {
712 ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
713 err = ib_device_set_netns_put(skb, device, ns_fd);
718 ib_device_put(device);
723 static int _nldev_get_dumpit(struct ib_device *device,
725 struct netlink_callback *cb,
728 int start = cb->args[0];
729 struct nlmsghdr *nlh;
734 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
735 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
738 if (fill_dev_info(skb, device)) {
739 nlmsg_cancel(skb, nlh);
747 out: cb->args[0] = idx;
751 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
754 * There is no need to take lock, because
755 * we are relying on ib_core's locking.
757 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
760 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
761 struct netlink_ext_ack *extack)
763 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
764 struct ib_device *device;
770 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
771 nldev_policy, extack);
773 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
774 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
777 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
778 device = ib_device_get_by_index(sock_net(skb->sk), index);
782 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
783 if (!rdma_is_port_valid(device, port)) {
788 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
794 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
795 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
798 err = fill_port_info(msg, device, port, sock_net(skb->sk));
803 ib_device_put(device);
805 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
810 ib_device_put(device);
814 static int nldev_port_get_dumpit(struct sk_buff *skb,
815 struct netlink_callback *cb)
817 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
818 struct ib_device *device;
819 int start = cb->args[0];
820 struct nlmsghdr *nlh;
826 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
828 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
831 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
832 device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
836 rdma_for_each_port (device, p) {
838 * The dumpit function returns all information from specific
839 * index. This specific index is taken from the netlink
840 * messages request sent by user and it is available
843 * Usually, the user doesn't fill this field and it causes
844 * to return everything.
852 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
854 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
855 RDMA_NLDEV_CMD_PORT_GET),
858 if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
859 nlmsg_cancel(skb, nlh);
867 ib_device_put(device);
872 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
873 struct netlink_ext_ack *extack)
875 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
876 struct ib_device *device;
881 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
882 nldev_policy, extack);
883 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
886 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
887 device = ib_device_get_by_index(sock_net(skb->sk), index);
891 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
897 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
898 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
901 ret = fill_res_info(msg, device);
906 ib_device_put(device);
907 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
912 ib_device_put(device);
916 static int _nldev_res_get_dumpit(struct ib_device *device,
918 struct netlink_callback *cb,
921 int start = cb->args[0];
922 struct nlmsghdr *nlh;
927 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
928 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
931 if (fill_res_info(skb, device)) {
932 nlmsg_cancel(skb, nlh);
944 static int nldev_res_get_dumpit(struct sk_buff *skb,
945 struct netlink_callback *cb)
947 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
950 struct nldev_fill_res_entry {
951 int (*fill_res_func)(struct sk_buff *msg, bool has_cap_net_admin,
952 struct rdma_restrack_entry *res, u32 port);
953 enum rdma_nldev_attr nldev_attr;
954 enum rdma_nldev_command nldev_cmd;
960 enum nldev_res_flags {
961 NLDEV_PER_DEV = 1 << 0,
964 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
965 [RDMA_RESTRACK_QP] = {
966 .fill_res_func = fill_res_qp_entry,
967 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
968 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
969 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
970 .id = RDMA_NLDEV_ATTR_RES_LQPN,
972 [RDMA_RESTRACK_CM_ID] = {
973 .fill_res_func = fill_res_cm_id_entry,
974 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
975 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
976 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
977 .id = RDMA_NLDEV_ATTR_RES_CM_IDN,
979 [RDMA_RESTRACK_CQ] = {
980 .fill_res_func = fill_res_cq_entry,
981 .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
982 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
983 .flags = NLDEV_PER_DEV,
984 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
985 .id = RDMA_NLDEV_ATTR_RES_CQN,
987 [RDMA_RESTRACK_MR] = {
988 .fill_res_func = fill_res_mr_entry,
989 .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
990 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
991 .flags = NLDEV_PER_DEV,
992 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
993 .id = RDMA_NLDEV_ATTR_RES_MRN,
995 [RDMA_RESTRACK_PD] = {
996 .fill_res_func = fill_res_pd_entry,
997 .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
998 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
999 .flags = NLDEV_PER_DEV,
1000 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1001 .id = RDMA_NLDEV_ATTR_RES_PDN,
1005 static bool is_visible_in_pid_ns(struct rdma_restrack_entry *res)
1008 * 1. Kern resources should be visible in init name space only
1009 * 2. Present only resources visible in the current namespace
1011 if (rdma_is_kernel_res(res))
1012 return task_active_pid_ns(current) == &init_pid_ns;
1013 return task_active_pid_ns(current) == task_active_pid_ns(res->task);
1016 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1017 struct netlink_ext_ack *extack,
1018 enum rdma_restrack_type res_type)
1020 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1021 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1022 struct rdma_restrack_entry *res;
1023 struct ib_device *device;
1024 u32 index, id, port = 0;
1025 bool has_cap_net_admin;
1026 struct sk_buff *msg;
1029 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1030 nldev_policy, extack);
1031 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1034 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1035 device = ib_device_get_by_index(sock_net(skb->sk), index);
1039 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1040 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1041 if (!rdma_is_port_valid(device, port)) {
1047 if ((port && fe->flags & NLDEV_PER_DEV) ||
1048 (!port && ~fe->flags & NLDEV_PER_DEV)) {
1053 id = nla_get_u32(tb[fe->id]);
1054 res = rdma_restrack_get_byid(device, res_type, id);
1060 if (!is_visible_in_pid_ns(res)) {
1065 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1071 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1072 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1075 if (fill_nldev_handle(msg, device)) {
1080 has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1081 ret = fe->fill_res_func(msg, has_cap_net_admin, res, port);
1082 rdma_restrack_put(res);
1086 nlmsg_end(msg, nlh);
1087 ib_device_put(device);
1088 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1093 rdma_restrack_put(res);
1095 ib_device_put(device);
1099 static int res_get_common_dumpit(struct sk_buff *skb,
1100 struct netlink_callback *cb,
1101 enum rdma_restrack_type res_type)
1103 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1104 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1105 struct rdma_restrack_entry *res;
1106 struct rdma_restrack_root *rt;
1107 int err, ret = 0, idx = 0;
1108 struct nlattr *table_attr;
1109 struct nlattr *entry_attr;
1110 struct ib_device *device;
1111 int start = cb->args[0];
1112 bool has_cap_net_admin;
1113 struct nlmsghdr *nlh;
1115 u32 index, port = 0;
1116 bool filled = false;
1118 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1119 nldev_policy, NULL);
1121 * Right now, we are expecting the device index to get res information,
1122 * but it is possible to extend this code to return all devices in
1123 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1124 * if it doesn't exist, we will iterate over all devices.
1126 * But it is not needed for now.
1128 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1131 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1132 device = ib_device_get_by_index(sock_net(skb->sk), index);
1137 * If no PORT_INDEX is supplied, we will return all QPs from that device
1139 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1140 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1141 if (!rdma_is_port_valid(device, port)) {
1147 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1148 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1151 if (fill_nldev_handle(skb, device)) {
1156 table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1162 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1164 rt = &device->res[res_type];
1167 * FIXME: if the skip ahead is something common this loop should
1168 * use xas_for_each & xas_pause to optimize, we can have a lot of
1171 xa_for_each(&rt->xa, id, res) {
1172 if (!is_visible_in_pid_ns(res))
1175 if (idx < start || !rdma_restrack_get(res))
1182 entry_attr = nla_nest_start_noflag(skb, fe->entry);
1185 rdma_restrack_put(res);
1189 ret = fe->fill_res_func(skb, has_cap_net_admin, res, port);
1190 rdma_restrack_put(res);
1193 nla_nest_cancel(skb, entry_attr);
1194 if (ret == -EMSGSIZE)
1200 nla_nest_end(skb, entry_attr);
1201 again: xa_lock(&rt->xa);
1207 nla_nest_end(skb, table_attr);
1208 nlmsg_end(skb, nlh);
1212 * No more entries to fill, cancel the message and
1213 * return 0 to mark end of dumpit.
1218 ib_device_put(device);
1222 nla_nest_cancel(skb, table_attr);
1225 nlmsg_cancel(skb, nlh);
1228 ib_device_put(device);
1232 #define RES_GET_FUNCS(name, type) \
1233 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \
1234 struct netlink_callback *cb) \
1236 return res_get_common_dumpit(skb, cb, type); \
1238 static int nldev_res_get_##name##_doit(struct sk_buff *skb, \
1239 struct nlmsghdr *nlh, \
1240 struct netlink_ext_ack *extack) \
1242 return res_get_common_doit(skb, nlh, extack, type); \
1245 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1246 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1247 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1248 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1249 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1251 static LIST_HEAD(link_ops);
1252 static DECLARE_RWSEM(link_ops_rwsem);
1254 static const struct rdma_link_ops *link_ops_get(const char *type)
1256 const struct rdma_link_ops *ops;
1258 list_for_each_entry(ops, &link_ops, list) {
1259 if (!strcmp(ops->type, type))
1267 void rdma_link_register(struct rdma_link_ops *ops)
1269 down_write(&link_ops_rwsem);
1270 if (WARN_ON_ONCE(link_ops_get(ops->type)))
1272 list_add(&ops->list, &link_ops);
1274 up_write(&link_ops_rwsem);
1276 EXPORT_SYMBOL(rdma_link_register);
1278 void rdma_link_unregister(struct rdma_link_ops *ops)
1280 down_write(&link_ops_rwsem);
1281 list_del(&ops->list);
1282 up_write(&link_ops_rwsem);
1284 EXPORT_SYMBOL(rdma_link_unregister);
1286 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1287 struct netlink_ext_ack *extack)
1289 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1290 char ibdev_name[IB_DEVICE_NAME_MAX];
1291 const struct rdma_link_ops *ops;
1292 char ndev_name[IFNAMSIZ];
1293 struct net_device *ndev;
1294 char type[IFNAMSIZ];
1297 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1298 nldev_policy, extack);
1299 if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1300 !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1303 nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1304 sizeof(ibdev_name));
1305 if (strchr(ibdev_name, '%'))
1308 nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1309 nla_strlcpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1312 ndev = dev_get_by_name(&init_net, ndev_name);
1316 down_read(&link_ops_rwsem);
1317 ops = link_ops_get(type);
1318 #ifdef CONFIG_MODULES
1320 up_read(&link_ops_rwsem);
1321 request_module("rdma-link-%s", type);
1322 down_read(&link_ops_rwsem);
1323 ops = link_ops_get(type);
1326 err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1327 up_read(&link_ops_rwsem);
1333 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1334 struct netlink_ext_ack *extack)
1336 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1337 struct ib_device *device;
1341 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1342 nldev_policy, extack);
1343 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1346 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1347 device = ib_device_get_by_index(sock_net(skb->sk), index);
1351 if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) {
1352 ib_device_put(device);
1356 ib_unregister_device_and_put(device);
1360 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1361 struct netlink_ext_ack *extack)
1363 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1364 char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1365 struct ib_client_nl_info data = {};
1366 struct ib_device *ibdev = NULL;
1367 struct sk_buff *msg;
1371 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1373 if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1376 nla_strlcpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1377 sizeof(client_name));
1379 if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1380 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1381 ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1385 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1386 data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1387 if (!rdma_is_port_valid(ibdev, data.port)) {
1394 } else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1398 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1403 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1404 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1405 RDMA_NLDEV_CMD_GET_CHARDEV),
1409 err = ib_get_client_nl_info(ibdev, client_name, &data);
1413 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1414 huge_encode_dev(data.cdev->devt),
1415 RDMA_NLDEV_ATTR_PAD);
1418 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1419 RDMA_NLDEV_ATTR_PAD);
1422 if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1423 dev_name(data.cdev))) {
1428 nlmsg_end(msg, nlh);
1429 put_device(data.cdev);
1431 ib_device_put(ibdev);
1432 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1435 put_device(data.cdev);
1440 ib_device_put(ibdev);
1444 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1445 struct netlink_ext_ack *extack)
1447 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1448 struct sk_buff *msg;
1451 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1452 nldev_policy, extack);
1456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1460 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1461 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1462 RDMA_NLDEV_CMD_SYS_GET),
1465 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1466 (u8)ib_devices_shared_netns);
1471 nlmsg_end(msg, nlh);
1472 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1475 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1476 struct netlink_ext_ack *extack)
1478 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1482 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1483 nldev_policy, extack);
1484 if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
1487 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1488 /* Only 0 and 1 are supported */
1492 err = rdma_compatdev_set(enable);
1496 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
1497 [RDMA_NLDEV_CMD_GET] = {
1498 .doit = nldev_get_doit,
1499 .dump = nldev_get_dumpit,
1501 [RDMA_NLDEV_CMD_GET_CHARDEV] = {
1502 .doit = nldev_get_chardev,
1504 [RDMA_NLDEV_CMD_SET] = {
1505 .doit = nldev_set_doit,
1506 .flags = RDMA_NL_ADMIN_PERM,
1508 [RDMA_NLDEV_CMD_NEWLINK] = {
1509 .doit = nldev_newlink,
1510 .flags = RDMA_NL_ADMIN_PERM,
1512 [RDMA_NLDEV_CMD_DELLINK] = {
1513 .doit = nldev_dellink,
1514 .flags = RDMA_NL_ADMIN_PERM,
1516 [RDMA_NLDEV_CMD_PORT_GET] = {
1517 .doit = nldev_port_get_doit,
1518 .dump = nldev_port_get_dumpit,
1520 [RDMA_NLDEV_CMD_RES_GET] = {
1521 .doit = nldev_res_get_doit,
1522 .dump = nldev_res_get_dumpit,
1524 [RDMA_NLDEV_CMD_RES_QP_GET] = {
1525 .doit = nldev_res_get_qp_doit,
1526 .dump = nldev_res_get_qp_dumpit,
1528 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
1529 .doit = nldev_res_get_cm_id_doit,
1530 .dump = nldev_res_get_cm_id_dumpit,
1532 [RDMA_NLDEV_CMD_RES_CQ_GET] = {
1533 .doit = nldev_res_get_cq_doit,
1534 .dump = nldev_res_get_cq_dumpit,
1536 [RDMA_NLDEV_CMD_RES_MR_GET] = {
1537 .doit = nldev_res_get_mr_doit,
1538 .dump = nldev_res_get_mr_dumpit,
1540 [RDMA_NLDEV_CMD_RES_PD_GET] = {
1541 .doit = nldev_res_get_pd_doit,
1542 .dump = nldev_res_get_pd_dumpit,
1544 [RDMA_NLDEV_CMD_SYS_GET] = {
1545 .doit = nldev_sys_get_doit,
1547 [RDMA_NLDEV_CMD_SYS_SET] = {
1548 .doit = nldev_set_sys_set_doit,
1549 .flags = RDMA_NL_ADMIN_PERM,
1553 void __init nldev_init(void)
1555 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
1558 void __exit nldev_exit(void)
1560 rdma_nl_unregister(RDMA_NL_NLDEV);
1563 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);