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 typedef int (*res_fill_func_t)(struct sk_buff*, bool,
47 struct rdma_restrack_entry*, uint32_t);
50 * Sort array elements by the netlink attribute name
52 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
53 [RDMA_NLDEV_ATTR_CHARDEV] = { .type = NLA_U64 },
54 [RDMA_NLDEV_ATTR_CHARDEV_ABI] = { .type = NLA_U64 },
55 [RDMA_NLDEV_ATTR_CHARDEV_NAME] = { .type = NLA_NUL_STRING,
56 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
57 [RDMA_NLDEV_ATTR_CHARDEV_TYPE] = { .type = NLA_NUL_STRING,
58 .len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
59 [RDMA_NLDEV_ATTR_DEV_DIM] = { .type = NLA_U8 },
60 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
61 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
62 .len = IB_DEVICE_NAME_MAX },
63 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
64 [RDMA_NLDEV_ATTR_DEV_PROTOCOL] = { .type = NLA_NUL_STRING,
65 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
66 [RDMA_NLDEV_ATTR_DRIVER] = { .type = NLA_NESTED },
67 [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = { .type = NLA_NESTED },
68 [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = { .type = NLA_U8 },
69 [RDMA_NLDEV_ATTR_DRIVER_STRING] = { .type = NLA_NUL_STRING,
70 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
71 [RDMA_NLDEV_ATTR_DRIVER_S32] = { .type = NLA_S32 },
72 [RDMA_NLDEV_ATTR_DRIVER_S64] = { .type = NLA_S64 },
73 [RDMA_NLDEV_ATTR_DRIVER_U32] = { .type = NLA_U32 },
74 [RDMA_NLDEV_ATTR_DRIVER_U64] = { .type = NLA_U64 },
75 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING,
76 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
77 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 },
78 [RDMA_NLDEV_ATTR_LINK_TYPE] = { .type = NLA_NUL_STRING,
80 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 },
81 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 },
82 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING,
84 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 },
85 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
86 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
87 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 },
88 [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED },
89 [RDMA_NLDEV_ATTR_RES_CM_IDN] = { .type = NLA_U32 },
90 [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED },
91 [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED },
92 [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 },
93 [RDMA_NLDEV_ATTR_RES_CQN] = { .type = NLA_U32 },
94 [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED },
95 [RDMA_NLDEV_ATTR_RES_CTX] = { .type = NLA_NESTED },
96 [RDMA_NLDEV_ATTR_RES_CTXN] = { .type = NLA_U32 },
97 [RDMA_NLDEV_ATTR_RES_CTX_ENTRY] = { .type = NLA_NESTED },
98 [RDMA_NLDEV_ATTR_RES_DST_ADDR] = {
99 .len = sizeof(struct __kernel_sockaddr_storage) },
100 [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 },
101 [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING,
102 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
103 [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 },
104 [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 },
105 [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 },
106 [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED },
107 [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 },
108 [RDMA_NLDEV_ATTR_RES_MRN] = { .type = NLA_U32 },
109 [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED },
110 [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
111 [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED },
112 [RDMA_NLDEV_ATTR_RES_PDN] = { .type = NLA_U32 },
113 [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED },
114 [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 },
115 [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 },
116 [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 },
117 [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED },
118 [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED },
119 [RDMA_NLDEV_ATTR_RES_RAW] = { .type = NLA_BINARY },
120 [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 },
121 [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 },
122 [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 },
123 [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 },
124 [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = {
125 .len = sizeof(struct __kernel_sockaddr_storage) },
126 [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 },
127 [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED },
128 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED },
129 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
130 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
131 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
132 [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 },
133 [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
134 [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 },
135 [RDMA_NLDEV_ATTR_RES_SRQ] = { .type = NLA_NESTED },
136 [RDMA_NLDEV_ATTR_RES_SRQN] = { .type = NLA_U32 },
137 [RDMA_NLDEV_ATTR_RES_SRQ_ENTRY] = { .type = NLA_NESTED },
138 [RDMA_NLDEV_ATTR_MIN_RANGE] = { .type = NLA_U32 },
139 [RDMA_NLDEV_ATTR_MAX_RANGE] = { .type = NLA_U32 },
140 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 },
141 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
142 [RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK] = { .type = NLA_U32 },
143 [RDMA_NLDEV_ATTR_STAT_MODE] = { .type = NLA_U32 },
144 [RDMA_NLDEV_ATTR_STAT_RES] = { .type = NLA_U32 },
145 [RDMA_NLDEV_ATTR_STAT_COUNTER] = { .type = NLA_NESTED },
146 [RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY] = { .type = NLA_NESTED },
147 [RDMA_NLDEV_ATTR_STAT_COUNTER_ID] = { .type = NLA_U32 },
148 [RDMA_NLDEV_ATTR_STAT_HWCOUNTERS] = { .type = NLA_NESTED },
149 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY] = { .type = NLA_NESTED },
150 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
151 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
152 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
153 [RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID] = { .type = NLA_U32 },
154 [RDMA_NLDEV_NET_NS_FD] = { .type = NLA_U32 },
155 [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = { .type = NLA_U8 },
156 [RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK] = { .type = NLA_U8 },
157 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX] = { .type = NLA_U32 },
158 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 },
161 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
162 enum rdma_nldev_print_type print_type)
164 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
166 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
167 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
173 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
174 enum rdma_nldev_print_type print_type,
177 if (put_driver_name_print_type(msg, name, print_type))
179 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
185 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
186 enum rdma_nldev_print_type print_type,
189 if (put_driver_name_print_type(msg, name, print_type))
191 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
192 RDMA_NLDEV_ATTR_PAD))
198 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
201 if (put_driver_name_print_type(msg, name,
202 RDMA_NLDEV_PRINT_TYPE_UNSPEC))
204 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
209 EXPORT_SYMBOL(rdma_nl_put_driver_string);
211 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
213 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
216 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
218 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
221 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
224 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
226 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
228 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
231 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
233 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
235 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
238 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
240 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
242 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
244 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
245 dev_name(&device->dev)))
251 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
253 char fw[IB_FW_VERSION_NAME_MAX];
257 if (fill_nldev_handle(msg, device))
260 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
263 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
264 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
265 device->attrs.device_cap_flags,
266 RDMA_NLDEV_ATTR_PAD))
269 ib_get_device_fw_str(device, fw);
270 /* Device without FW has strlen(fw) = 0 */
271 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
274 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
275 be64_to_cpu(device->node_guid),
276 RDMA_NLDEV_ATTR_PAD))
278 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
279 be64_to_cpu(device->attrs.sys_image_guid),
280 RDMA_NLDEV_ATTR_PAD))
282 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
284 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
288 * Link type is determined on first port and mlx4 device
289 * which can potentially have two different link type for the same
290 * IB device is considered as better to be avoided in the future,
292 port = rdma_start_port(device);
293 if (rdma_cap_opa_mad(device, port))
294 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
295 else if (rdma_protocol_ib(device, port))
296 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
297 else if (rdma_protocol_iwarp(device, port))
298 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
299 else if (rdma_protocol_roce(device, port))
300 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
301 else if (rdma_protocol_usnic(device, port))
302 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
307 static int fill_port_info(struct sk_buff *msg,
308 struct ib_device *device, u32 port,
309 const struct net *net)
311 struct net_device *netdev = NULL;
312 struct ib_port_attr attr;
316 if (fill_nldev_handle(msg, device))
319 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
322 ret = ib_query_port(device, port, &attr);
326 if (rdma_protocol_ib(device, port)) {
327 BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
328 sizeof(attr.port_cap_flags2)) > sizeof(u64));
329 cap_flags = attr.port_cap_flags |
330 ((u64)attr.port_cap_flags2 << 32);
331 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
332 cap_flags, RDMA_NLDEV_ATTR_PAD))
334 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
335 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
337 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
339 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
341 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
344 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
346 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
349 netdev = ib_device_get_netdev(device, port);
350 if (netdev && net_eq(dev_net(netdev), net)) {
351 ret = nla_put_u32(msg,
352 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
355 ret = nla_put_string(msg,
356 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
365 static int fill_res_info_entry(struct sk_buff *msg,
366 const char *name, u64 curr)
368 struct nlattr *entry_attr;
370 entry_attr = nla_nest_start_noflag(msg,
371 RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
375 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
377 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
378 RDMA_NLDEV_ATTR_PAD))
381 nla_nest_end(msg, entry_attr);
385 nla_nest_cancel(msg, entry_attr);
389 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
391 static const char * const names[RDMA_RESTRACK_MAX] = {
392 [RDMA_RESTRACK_PD] = "pd",
393 [RDMA_RESTRACK_CQ] = "cq",
394 [RDMA_RESTRACK_QP] = "qp",
395 [RDMA_RESTRACK_CM_ID] = "cm_id",
396 [RDMA_RESTRACK_MR] = "mr",
397 [RDMA_RESTRACK_CTX] = "ctx",
398 [RDMA_RESTRACK_SRQ] = "srq",
401 struct nlattr *table_attr;
404 if (fill_nldev_handle(msg, device))
407 table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
411 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
414 curr = rdma_restrack_count(device, i);
415 ret = fill_res_info_entry(msg, names[i], curr);
420 nla_nest_end(msg, table_attr);
424 nla_nest_cancel(msg, table_attr);
428 static int fill_res_name_pid(struct sk_buff *msg,
429 struct rdma_restrack_entry *res)
434 * For user resources, user is should read /proc/PID/comm to get the
435 * name of the task file.
437 if (rdma_is_kernel_res(res)) {
438 err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
443 pid = task_pid_vnr(res->task);
445 * Task is dead and in zombie state.
446 * There is no need to print PID anymore.
450 * This part is racy, task can be killed and PID will
451 * be zero right here but it is ok, next query won't
452 * return PID. We don't promise real-time reflection
455 err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
458 return err ? -EMSGSIZE : 0;
461 static int fill_res_qp_entry_query(struct sk_buff *msg,
462 struct rdma_restrack_entry *res,
463 struct ib_device *dev,
466 struct ib_qp_init_attr qp_init_attr;
467 struct ib_qp_attr qp_attr;
470 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
474 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
475 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
476 qp_attr.dest_qp_num))
478 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
483 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
486 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
487 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
488 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
489 qp_attr.path_mig_state))
492 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
494 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
497 if (dev->ops.fill_res_qp_entry)
498 return dev->ops.fill_res_qp_entry(msg, qp);
501 err: return -EMSGSIZE;
504 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
505 struct rdma_restrack_entry *res, uint32_t port)
507 struct ib_qp *qp = container_of(res, struct ib_qp, res);
508 struct ib_device *dev = qp->device;
511 if (port && port != qp->port)
514 /* In create_qp() port is not set yet */
515 if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
518 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
522 if (!rdma_is_kernel_res(res) &&
523 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
526 ret = fill_res_name_pid(msg, res);
530 return fill_res_qp_entry_query(msg, res, dev, qp);
533 static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
534 struct rdma_restrack_entry *res, uint32_t port)
536 struct ib_qp *qp = container_of(res, struct ib_qp, res);
537 struct ib_device *dev = qp->device;
539 if (port && port != qp->port)
541 if (!dev->ops.fill_res_qp_entry_raw)
543 return dev->ops.fill_res_qp_entry_raw(msg, qp);
546 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
547 struct rdma_restrack_entry *res, uint32_t port)
549 struct rdma_id_private *id_priv =
550 container_of(res, struct rdma_id_private, res);
551 struct ib_device *dev = id_priv->id.device;
552 struct rdma_cm_id *cm_id = &id_priv->id;
554 if (port && port != cm_id->port_num)
557 if (cm_id->port_num &&
558 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
561 if (id_priv->qp_num) {
562 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
564 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
568 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
571 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
574 if (cm_id->route.addr.src_addr.ss_family &&
575 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
576 sizeof(cm_id->route.addr.src_addr),
577 &cm_id->route.addr.src_addr))
579 if (cm_id->route.addr.dst_addr.ss_family &&
580 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
581 sizeof(cm_id->route.addr.dst_addr),
582 &cm_id->route.addr.dst_addr))
585 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
588 if (fill_res_name_pid(msg, res))
591 if (dev->ops.fill_res_cm_id_entry)
592 return dev->ops.fill_res_cm_id_entry(msg, cm_id);
595 err: return -EMSGSIZE;
598 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
599 struct rdma_restrack_entry *res, uint32_t port)
601 struct ib_cq *cq = container_of(res, struct ib_cq, res);
602 struct ib_device *dev = cq->device;
604 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
606 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
607 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
610 /* Poll context is only valid for kernel CQs */
611 if (rdma_is_kernel_res(res) &&
612 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
615 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
618 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
620 if (!rdma_is_kernel_res(res) &&
621 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
622 cq->uobject->uevent.uobject.context->res.id))
625 if (fill_res_name_pid(msg, res))
628 return (dev->ops.fill_res_cq_entry) ?
629 dev->ops.fill_res_cq_entry(msg, cq) : 0;
632 static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
633 struct rdma_restrack_entry *res, uint32_t port)
635 struct ib_cq *cq = container_of(res, struct ib_cq, res);
636 struct ib_device *dev = cq->device;
638 if (!dev->ops.fill_res_cq_entry_raw)
640 return dev->ops.fill_res_cq_entry_raw(msg, cq);
643 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
644 struct rdma_restrack_entry *res, uint32_t port)
646 struct ib_mr *mr = container_of(res, struct ib_mr, res);
647 struct ib_device *dev = mr->pd->device;
649 if (has_cap_net_admin) {
650 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
652 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
656 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
657 RDMA_NLDEV_ATTR_PAD))
660 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
663 if (!rdma_is_kernel_res(res) &&
664 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
667 if (fill_res_name_pid(msg, res))
670 return (dev->ops.fill_res_mr_entry) ?
671 dev->ops.fill_res_mr_entry(msg, mr) :
675 static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
676 struct rdma_restrack_entry *res, uint32_t port)
678 struct ib_mr *mr = container_of(res, struct ib_mr, res);
679 struct ib_device *dev = mr->pd->device;
681 if (!dev->ops.fill_res_mr_entry_raw)
683 return dev->ops.fill_res_mr_entry_raw(msg, mr);
686 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
687 struct rdma_restrack_entry *res, uint32_t port)
689 struct ib_pd *pd = container_of(res, struct ib_pd, res);
691 if (has_cap_net_admin) {
692 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
695 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
696 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
697 pd->unsafe_global_rkey))
700 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
701 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
704 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
707 if (!rdma_is_kernel_res(res) &&
708 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
709 pd->uobject->context->res.id))
712 return fill_res_name_pid(msg, res);
714 err: return -EMSGSIZE;
717 static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
718 struct rdma_restrack_entry *res, uint32_t port)
720 struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
722 if (rdma_is_kernel_res(res))
725 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
728 return fill_res_name_pid(msg, res);
731 static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
734 struct nlattr *entry_attr;
739 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
743 if (min_range == max_range) {
744 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
747 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
749 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
752 nla_nest_end(msg, entry_attr);
756 nla_nest_cancel(msg, entry_attr);
760 static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
762 uint32_t min_range = 0, prev = 0;
763 struct rdma_restrack_entry *res;
764 struct rdma_restrack_root *rt;
765 struct nlattr *table_attr;
766 struct ib_qp *qp = NULL;
767 unsigned long id = 0;
769 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
773 rt = &srq->device->res[RDMA_RESTRACK_QP];
775 xa_for_each(&rt->xa, id, res) {
776 if (!rdma_restrack_get(res))
779 qp = container_of(res, struct ib_qp, res);
780 if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
781 rdma_restrack_put(res);
785 if (qp->qp_num < prev)
786 /* qp_num should be ascending */
789 if (min_range == 0) {
790 min_range = qp->qp_num;
791 } else if (qp->qp_num > (prev + 1)) {
792 if (fill_res_range_qp_entry(msg, min_range, prev))
795 min_range = qp->qp_num;
798 rdma_restrack_put(res);
803 if (fill_res_range_qp_entry(msg, min_range, prev))
806 nla_nest_end(msg, table_attr);
810 rdma_restrack_put(res);
813 nla_nest_cancel(msg, table_attr);
817 static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
818 struct rdma_restrack_entry *res, uint32_t port)
820 struct ib_srq *srq = container_of(res, struct ib_srq, res);
822 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
825 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
828 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
831 if (ib_srq_has_cq(srq->srq_type)) {
832 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
833 srq->ext.cq->res.id))
837 if (fill_res_srq_qps(msg, srq))
840 return fill_res_name_pid(msg, res);
846 static int fill_stat_counter_mode(struct sk_buff *msg,
847 struct rdma_counter *counter)
849 struct rdma_counter_mode *m = &counter->mode;
851 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
854 if (m->mode == RDMA_COUNTER_MODE_AUTO) {
855 if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
856 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
859 if ((m->mask & RDMA_COUNTER_MASK_PID) &&
860 fill_res_name_pid(msg, &counter->res))
867 static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
869 struct nlattr *entry_attr;
871 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
875 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
878 nla_nest_end(msg, entry_attr);
882 nla_nest_cancel(msg, entry_attr);
886 static int fill_stat_counter_qps(struct sk_buff *msg,
887 struct rdma_counter *counter)
889 struct rdma_restrack_entry *res;
890 struct rdma_restrack_root *rt;
891 struct nlattr *table_attr;
892 struct ib_qp *qp = NULL;
893 unsigned long id = 0;
896 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
900 rt = &counter->device->res[RDMA_RESTRACK_QP];
902 xa_for_each(&rt->xa, id, res) {
903 qp = container_of(res, struct ib_qp, res);
904 if (!qp->counter || (qp->counter->id != counter->id))
907 ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
913 nla_nest_end(msg, table_attr);
918 nla_nest_cancel(msg, table_attr);
922 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
925 struct nlattr *entry_attr;
927 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
931 if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
934 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
935 value, RDMA_NLDEV_ATTR_PAD))
938 nla_nest_end(msg, entry_attr);
942 nla_nest_cancel(msg, entry_attr);
945 EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
947 static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
948 struct rdma_restrack_entry *res, uint32_t port)
950 struct ib_mr *mr = container_of(res, struct ib_mr, res);
951 struct ib_device *dev = mr->pd->device;
953 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
956 if (dev->ops.fill_stat_mr_entry)
957 return dev->ops.fill_stat_mr_entry(msg, mr);
964 static int fill_stat_counter_hwcounters(struct sk_buff *msg,
965 struct rdma_counter *counter)
967 struct rdma_hw_stats *st = counter->stats;
968 struct nlattr *table_attr;
971 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
975 mutex_lock(&st->lock);
976 for (i = 0; i < st->num_counters; i++) {
977 if (test_bit(i, st->is_disabled))
979 if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name,
983 mutex_unlock(&st->lock);
985 nla_nest_end(msg, table_attr);
989 mutex_unlock(&st->lock);
990 nla_nest_cancel(msg, table_attr);
994 static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
995 struct rdma_restrack_entry *res,
998 struct rdma_counter *counter =
999 container_of(res, struct rdma_counter, res);
1001 if (port && port != counter->port)
1004 /* Dump it even query failed */
1005 rdma_counter_query_stats(counter);
1007 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
1008 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
1009 fill_stat_counter_mode(msg, counter) ||
1010 fill_stat_counter_qps(msg, counter) ||
1011 fill_stat_counter_hwcounters(msg, counter))
1017 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1018 struct netlink_ext_ack *extack)
1020 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1021 struct ib_device *device;
1022 struct sk_buff *msg;
1026 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1027 nldev_policy, extack);
1028 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1031 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1033 device = ib_device_get_by_index(sock_net(skb->sk), index);
1037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1043 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1044 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1051 err = fill_dev_info(msg, device);
1055 nlmsg_end(msg, nlh);
1057 ib_device_put(device);
1058 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1063 ib_device_put(device);
1067 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1068 struct netlink_ext_ack *extack)
1070 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1071 struct ib_device *device;
1075 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1076 nldev_policy, extack);
1077 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1080 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1081 device = ib_device_get_by_index(sock_net(skb->sk), index);
1085 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1086 char name[IB_DEVICE_NAME_MAX] = {};
1088 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1089 IB_DEVICE_NAME_MAX);
1090 if (strlen(name) == 0) {
1094 err = ib_device_rename(device, name);
1098 if (tb[RDMA_NLDEV_NET_NS_FD]) {
1101 ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1102 err = ib_device_set_netns_put(skb, device, ns_fd);
1106 if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1109 use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1110 err = ib_device_set_dim(device, use_dim);
1115 ib_device_put(device);
1120 static int _nldev_get_dumpit(struct ib_device *device,
1121 struct sk_buff *skb,
1122 struct netlink_callback *cb,
1125 int start = cb->args[0];
1126 struct nlmsghdr *nlh;
1131 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1132 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1135 if (!nlh || fill_dev_info(skb, device)) {
1136 nlmsg_cancel(skb, nlh);
1140 nlmsg_end(skb, nlh);
1144 out: cb->args[0] = idx;
1148 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1151 * There is no need to take lock, because
1152 * we are relying on ib_core's locking.
1154 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1157 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1158 struct netlink_ext_ack *extack)
1160 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1161 struct ib_device *device;
1162 struct sk_buff *msg;
1167 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1168 nldev_policy, extack);
1170 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1171 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1174 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1175 device = ib_device_get_by_index(sock_net(skb->sk), index);
1179 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1180 if (!rdma_is_port_valid(device, port)) {
1185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1191 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1192 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1199 err = fill_port_info(msg, device, port, sock_net(skb->sk));
1203 nlmsg_end(msg, nlh);
1204 ib_device_put(device);
1206 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1211 ib_device_put(device);
1215 static int nldev_port_get_dumpit(struct sk_buff *skb,
1216 struct netlink_callback *cb)
1218 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1219 struct ib_device *device;
1220 int start = cb->args[0];
1221 struct nlmsghdr *nlh;
1227 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1228 nldev_policy, NULL);
1229 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1232 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1233 device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1237 rdma_for_each_port (device, p) {
1239 * The dumpit function returns all information from specific
1240 * index. This specific index is taken from the netlink
1241 * messages request sent by user and it is available
1244 * Usually, the user doesn't fill this field and it causes
1245 * to return everything.
1253 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1255 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1256 RDMA_NLDEV_CMD_PORT_GET),
1259 if (!nlh || fill_port_info(skb, device, p, sock_net(skb->sk))) {
1260 nlmsg_cancel(skb, nlh);
1264 nlmsg_end(skb, nlh);
1268 ib_device_put(device);
1273 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1274 struct netlink_ext_ack *extack)
1276 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1277 struct ib_device *device;
1278 struct sk_buff *msg;
1282 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1283 nldev_policy, extack);
1284 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1287 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1288 device = ib_device_get_by_index(sock_net(skb->sk), index);
1292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1298 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1299 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1306 ret = fill_res_info(msg, device);
1310 nlmsg_end(msg, nlh);
1311 ib_device_put(device);
1312 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1317 ib_device_put(device);
1321 static int _nldev_res_get_dumpit(struct ib_device *device,
1322 struct sk_buff *skb,
1323 struct netlink_callback *cb,
1326 int start = cb->args[0];
1327 struct nlmsghdr *nlh;
1332 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1333 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1336 if (!nlh || fill_res_info(skb, device)) {
1337 nlmsg_cancel(skb, nlh);
1340 nlmsg_end(skb, nlh);
1349 static int nldev_res_get_dumpit(struct sk_buff *skb,
1350 struct netlink_callback *cb)
1352 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1355 struct nldev_fill_res_entry {
1356 enum rdma_nldev_attr nldev_attr;
1362 enum nldev_res_flags {
1363 NLDEV_PER_DEV = 1 << 0,
1366 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1367 [RDMA_RESTRACK_QP] = {
1368 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1369 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1370 .id = RDMA_NLDEV_ATTR_RES_LQPN,
1372 [RDMA_RESTRACK_CM_ID] = {
1373 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1374 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1375 .id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1377 [RDMA_RESTRACK_CQ] = {
1378 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1379 .flags = NLDEV_PER_DEV,
1380 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1381 .id = RDMA_NLDEV_ATTR_RES_CQN,
1383 [RDMA_RESTRACK_MR] = {
1384 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1385 .flags = NLDEV_PER_DEV,
1386 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1387 .id = RDMA_NLDEV_ATTR_RES_MRN,
1389 [RDMA_RESTRACK_PD] = {
1390 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1391 .flags = NLDEV_PER_DEV,
1392 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1393 .id = RDMA_NLDEV_ATTR_RES_PDN,
1395 [RDMA_RESTRACK_COUNTER] = {
1396 .nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1397 .entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1398 .id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1400 [RDMA_RESTRACK_CTX] = {
1401 .nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1402 .flags = NLDEV_PER_DEV,
1403 .entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1404 .id = RDMA_NLDEV_ATTR_RES_CTXN,
1406 [RDMA_RESTRACK_SRQ] = {
1407 .nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1408 .flags = NLDEV_PER_DEV,
1409 .entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1410 .id = RDMA_NLDEV_ATTR_RES_SRQN,
1415 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1416 struct netlink_ext_ack *extack,
1417 enum rdma_restrack_type res_type,
1418 res_fill_func_t fill_func)
1420 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1421 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1422 struct rdma_restrack_entry *res;
1423 struct ib_device *device;
1424 u32 index, id, port = 0;
1425 bool has_cap_net_admin;
1426 struct sk_buff *msg;
1429 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1430 nldev_policy, extack);
1431 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1434 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1435 device = ib_device_get_by_index(sock_net(skb->sk), index);
1439 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1440 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1441 if (!rdma_is_port_valid(device, port)) {
1447 if ((port && fe->flags & NLDEV_PER_DEV) ||
1448 (!port && ~fe->flags & NLDEV_PER_DEV)) {
1453 id = nla_get_u32(tb[fe->id]);
1454 res = rdma_restrack_get_byid(device, res_type, id);
1460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1466 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1467 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1468 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1471 if (!nlh || fill_nldev_handle(msg, device)) {
1476 has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1478 ret = fill_func(msg, has_cap_net_admin, res, port);
1482 rdma_restrack_put(res);
1483 nlmsg_end(msg, nlh);
1484 ib_device_put(device);
1485 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1490 rdma_restrack_put(res);
1492 ib_device_put(device);
1496 static int res_get_common_dumpit(struct sk_buff *skb,
1497 struct netlink_callback *cb,
1498 enum rdma_restrack_type res_type,
1499 res_fill_func_t fill_func)
1501 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1502 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1503 struct rdma_restrack_entry *res;
1504 struct rdma_restrack_root *rt;
1505 int err, ret = 0, idx = 0;
1506 struct nlattr *table_attr;
1507 struct nlattr *entry_attr;
1508 struct ib_device *device;
1509 int start = cb->args[0];
1510 bool has_cap_net_admin;
1511 struct nlmsghdr *nlh;
1513 u32 index, port = 0;
1514 bool filled = false;
1516 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1517 nldev_policy, NULL);
1519 * Right now, we are expecting the device index to get res information,
1520 * but it is possible to extend this code to return all devices in
1521 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1522 * if it doesn't exist, we will iterate over all devices.
1524 * But it is not needed for now.
1526 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1529 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1530 device = ib_device_get_by_index(sock_net(skb->sk), index);
1535 * If no PORT_INDEX is supplied, we will return all QPs from that device
1537 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1538 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1539 if (!rdma_is_port_valid(device, port)) {
1545 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1546 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1547 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1550 if (!nlh || fill_nldev_handle(skb, device)) {
1555 table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1561 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1563 rt = &device->res[res_type];
1566 * FIXME: if the skip ahead is something common this loop should
1567 * use xas_for_each & xas_pause to optimize, we can have a lot of
1570 xa_for_each(&rt->xa, id, res) {
1571 if (idx < start || !rdma_restrack_get(res))
1578 entry_attr = nla_nest_start_noflag(skb, fe->entry);
1581 rdma_restrack_put(res);
1585 ret = fill_func(skb, has_cap_net_admin, res, port);
1587 rdma_restrack_put(res);
1590 nla_nest_cancel(skb, entry_attr);
1591 if (ret == -EMSGSIZE)
1597 nla_nest_end(skb, entry_attr);
1598 again: xa_lock(&rt->xa);
1604 nla_nest_end(skb, table_attr);
1605 nlmsg_end(skb, nlh);
1609 * No more entries to fill, cancel the message and
1610 * return 0 to mark end of dumpit.
1615 ib_device_put(device);
1619 nla_nest_cancel(skb, table_attr);
1622 nlmsg_cancel(skb, nlh);
1625 ib_device_put(device);
1629 #define RES_GET_FUNCS(name, type) \
1630 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \
1631 struct netlink_callback *cb) \
1633 return res_get_common_dumpit(skb, cb, type, \
1634 fill_res_##name##_entry); \
1636 static int nldev_res_get_##name##_doit(struct sk_buff *skb, \
1637 struct nlmsghdr *nlh, \
1638 struct netlink_ext_ack *extack) \
1640 return res_get_common_doit(skb, nlh, extack, type, \
1641 fill_res_##name##_entry); \
1644 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1645 RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1646 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1647 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1648 RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1649 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1650 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1651 RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1652 RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1653 RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1654 RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1656 static LIST_HEAD(link_ops);
1657 static DECLARE_RWSEM(link_ops_rwsem);
1659 static const struct rdma_link_ops *link_ops_get(const char *type)
1661 const struct rdma_link_ops *ops;
1663 list_for_each_entry(ops, &link_ops, list) {
1664 if (!strcmp(ops->type, type))
1672 void rdma_link_register(struct rdma_link_ops *ops)
1674 down_write(&link_ops_rwsem);
1675 if (WARN_ON_ONCE(link_ops_get(ops->type)))
1677 list_add(&ops->list, &link_ops);
1679 up_write(&link_ops_rwsem);
1681 EXPORT_SYMBOL(rdma_link_register);
1683 void rdma_link_unregister(struct rdma_link_ops *ops)
1685 down_write(&link_ops_rwsem);
1686 list_del(&ops->list);
1687 up_write(&link_ops_rwsem);
1689 EXPORT_SYMBOL(rdma_link_unregister);
1691 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1692 struct netlink_ext_ack *extack)
1694 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1695 char ibdev_name[IB_DEVICE_NAME_MAX];
1696 const struct rdma_link_ops *ops;
1697 char ndev_name[IFNAMSIZ];
1698 struct net_device *ndev;
1699 char type[IFNAMSIZ];
1702 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1703 nldev_policy, extack);
1704 if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1705 !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1708 nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1709 sizeof(ibdev_name));
1710 if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1713 nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1714 nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1717 ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1721 down_read(&link_ops_rwsem);
1722 ops = link_ops_get(type);
1723 #ifdef CONFIG_MODULES
1725 up_read(&link_ops_rwsem);
1726 request_module("rdma-link-%s", type);
1727 down_read(&link_ops_rwsem);
1728 ops = link_ops_get(type);
1731 err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1732 up_read(&link_ops_rwsem);
1738 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1739 struct netlink_ext_ack *extack)
1741 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1742 struct ib_device *device;
1746 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1747 nldev_policy, extack);
1748 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1751 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1752 device = ib_device_get_by_index(sock_net(skb->sk), index);
1756 if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) {
1757 ib_device_put(device);
1761 ib_unregister_device_and_put(device);
1765 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1766 struct netlink_ext_ack *extack)
1768 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1769 char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1770 struct ib_client_nl_info data = {};
1771 struct ib_device *ibdev = NULL;
1772 struct sk_buff *msg;
1776 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1778 if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1781 nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1782 sizeof(client_name));
1784 if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1785 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1786 ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1790 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1791 data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1792 if (!rdma_is_port_valid(ibdev, data.port)) {
1799 } else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1803 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1808 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1809 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1810 RDMA_NLDEV_CMD_GET_CHARDEV),
1818 err = ib_get_client_nl_info(ibdev, client_name, &data);
1822 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1823 huge_encode_dev(data.cdev->devt),
1824 RDMA_NLDEV_ATTR_PAD);
1827 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1828 RDMA_NLDEV_ATTR_PAD);
1831 if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1832 dev_name(data.cdev))) {
1837 nlmsg_end(msg, nlh);
1838 put_device(data.cdev);
1840 ib_device_put(ibdev);
1841 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1844 put_device(data.cdev);
1849 ib_device_put(ibdev);
1853 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1854 struct netlink_ext_ack *extack)
1856 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1857 struct sk_buff *msg;
1860 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1861 nldev_policy, extack);
1865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1869 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1870 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1871 RDMA_NLDEV_CMD_SYS_GET),
1878 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1879 (u8)ib_devices_shared_netns);
1886 * Copy-on-fork is supported.
1888 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
1889 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
1890 * for more details. Don't backport this without them.
1892 * Return value ignored on purpose, assume copy-on-fork is not
1893 * supported in case of failure.
1895 nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
1897 nlmsg_end(msg, nlh);
1898 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1901 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1902 struct netlink_ext_ack *extack)
1904 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1908 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1909 nldev_policy, extack);
1910 if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
1913 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1914 /* Only 0 and 1 are supported */
1918 err = rdma_compatdev_set(enable);
1922 static int nldev_stat_set_mode_doit(struct sk_buff *msg,
1923 struct netlink_ext_ack *extack,
1924 struct nlattr *tb[],
1925 struct ib_device *device, u32 port)
1927 u32 mode, mask = 0, qpn, cntn = 0;
1930 /* Currently only counter for QP is supported */
1931 if (!tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1932 nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1935 mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
1936 if (mode == RDMA_COUNTER_MODE_AUTO) {
1937 if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
1939 tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
1940 return rdma_counter_set_auto_mode(device, port, mask, extack);
1943 if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
1946 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
1947 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
1948 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
1949 ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
1953 ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn);
1958 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
1959 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
1967 rdma_counter_unbind_qpn(device, port, qpn, cntn);
1971 static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
1972 struct ib_device *device,
1975 struct rdma_hw_stats *stats;
1976 struct nlattr *entry_attr;
1977 unsigned long *target;
1978 int rem, i, ret = 0;
1981 stats = ib_get_hw_stats_port(device, port);
1985 target = kcalloc(BITS_TO_LONGS(stats->num_counters),
1986 sizeof(*stats->is_disabled), GFP_KERNEL);
1990 nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS],
1992 index = nla_get_u32(entry_attr);
1993 if ((index >= stats->num_counters) ||
1994 !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) {
1999 set_bit(index, target);
2002 for (i = 0; i < stats->num_counters; i++) {
2003 if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL))
2006 ret = rdma_counter_modify(device, port, i, test_bit(i, target));
2016 static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2017 struct netlink_ext_ack *extack)
2019 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2020 struct ib_device *device;
2021 struct sk_buff *msg;
2025 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2027 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2028 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2031 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2032 device = ib_device_get_by_index(sock_net(skb->sk), index);
2036 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2037 if (!rdma_is_port_valid(device, port)) {
2039 goto err_put_device;
2042 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] &&
2043 !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2045 goto err_put_device;
2048 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2051 goto err_put_device;
2053 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2054 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2055 RDMA_NLDEV_CMD_STAT_SET),
2057 if (!nlh || fill_nldev_handle(msg, device) ||
2058 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2063 if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) {
2064 ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port);
2069 if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2070 ret = nldev_stat_set_counter_dynamic_doit(tb, device, port);
2075 nlmsg_end(msg, nlh);
2076 ib_device_put(device);
2077 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2082 ib_device_put(device);
2086 static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2087 struct netlink_ext_ack *extack)
2089 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2090 struct ib_device *device;
2091 struct sk_buff *msg;
2092 u32 index, port, qpn, cntn;
2095 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2096 nldev_policy, extack);
2097 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2098 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
2099 !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
2100 !tb[RDMA_NLDEV_ATTR_RES_LQPN])
2103 if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2106 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2107 device = ib_device_get_by_index(sock_net(skb->sk), index);
2111 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2112 if (!rdma_is_port_valid(device, port)) {
2117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2122 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2123 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2124 RDMA_NLDEV_CMD_STAT_SET),
2131 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2132 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2133 if (fill_nldev_handle(msg, device) ||
2134 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2135 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2136 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2141 ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2145 nlmsg_end(msg, nlh);
2146 ib_device_put(device);
2147 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2152 ib_device_put(device);
2156 static int stat_get_doit_default_counter(struct sk_buff *skb,
2157 struct nlmsghdr *nlh,
2158 struct netlink_ext_ack *extack,
2159 struct nlattr *tb[])
2161 struct rdma_hw_stats *stats;
2162 struct nlattr *table_attr;
2163 struct ib_device *device;
2164 int ret, num_cnts, i;
2165 struct sk_buff *msg;
2169 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2172 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2173 device = ib_device_get_by_index(sock_net(skb->sk), index);
2177 if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2182 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2183 stats = ib_get_hw_stats_port(device, port);
2189 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2195 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2196 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2197 RDMA_NLDEV_CMD_STAT_GET),
2200 if (!nlh || fill_nldev_handle(msg, device) ||
2201 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2206 mutex_lock(&stats->lock);
2208 num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2214 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2219 for (i = 0; i < num_cnts; i++) {
2220 if (test_bit(i, stats->is_disabled))
2223 v = stats->value[i] +
2224 rdma_counter_get_hwstat_value(device, port, i);
2225 if (rdma_nl_stat_hwcounter_entry(msg,
2226 stats->descs[i].name, v)) {
2231 nla_nest_end(msg, table_attr);
2233 mutex_unlock(&stats->lock);
2234 nlmsg_end(msg, nlh);
2235 ib_device_put(device);
2236 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2239 nla_nest_cancel(msg, table_attr);
2241 mutex_unlock(&stats->lock);
2245 ib_device_put(device);
2249 static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2250 struct netlink_ext_ack *extack, struct nlattr *tb[])
2253 static enum rdma_nl_counter_mode mode;
2254 static enum rdma_nl_counter_mask mask;
2255 struct ib_device *device;
2256 struct sk_buff *msg;
2260 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2261 return nldev_res_get_counter_doit(skb, nlh, extack);
2263 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2264 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2267 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2268 device = ib_device_get_by_index(sock_net(skb->sk), index);
2272 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2273 if (!rdma_is_port_valid(device, port)) {
2278 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2284 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2285 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2286 RDMA_NLDEV_CMD_STAT_GET),
2293 ret = rdma_counter_get_mode(device, port, &mode, &mask);
2297 if (fill_nldev_handle(msg, device) ||
2298 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2299 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2304 if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2305 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2310 nlmsg_end(msg, nlh);
2311 ib_device_put(device);
2312 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2317 ib_device_put(device);
2321 static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2322 struct netlink_ext_ack *extack)
2324 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2327 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2328 nldev_policy, extack);
2332 if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2333 return stat_get_doit_default_counter(skb, nlh, extack, tb);
2335 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2336 case RDMA_NLDEV_ATTR_RES_QP:
2337 ret = stat_get_doit_qp(skb, nlh, extack, tb);
2339 case RDMA_NLDEV_ATTR_RES_MR:
2340 ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2341 fill_stat_mr_entry);
2351 static int nldev_stat_get_dumpit(struct sk_buff *skb,
2352 struct netlink_callback *cb)
2354 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2357 ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2358 nldev_policy, NULL);
2359 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2362 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2363 case RDMA_NLDEV_ATTR_RES_QP:
2364 ret = nldev_res_get_counter_dumpit(skb, cb);
2366 case RDMA_NLDEV_ATTR_RES_MR:
2367 ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2368 fill_stat_mr_entry);
2378 static int nldev_stat_get_counter_status_doit(struct sk_buff *skb,
2379 struct nlmsghdr *nlh,
2380 struct netlink_ext_ack *extack)
2382 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry;
2383 struct rdma_hw_stats *stats;
2384 struct ib_device *device;
2385 struct sk_buff *msg;
2389 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2390 nldev_policy, extack);
2391 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2392 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2395 devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2396 device = ib_device_get_by_index(sock_net(skb->sk), devid);
2400 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2401 if (!rdma_is_port_valid(device, port)) {
2406 stats = ib_get_hw_stats_port(device, port);
2412 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2419 msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2420 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS),
2424 if (!nlh || fill_nldev_handle(msg, device) ||
2425 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
2428 table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2432 mutex_lock(&stats->lock);
2433 for (i = 0; i < stats->num_counters; i++) {
2434 entry = nla_nest_start(msg,
2435 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
2439 if (nla_put_string(msg,
2440 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
2441 stats->descs[i].name) ||
2442 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i))
2445 if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) &&
2446 (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC,
2447 !test_bit(i, stats->is_disabled))))
2450 nla_nest_end(msg, entry);
2452 mutex_unlock(&stats->lock);
2454 nla_nest_end(msg, table);
2455 nlmsg_end(msg, nlh);
2456 ib_device_put(device);
2457 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2460 nla_nest_cancel(msg, entry);
2462 mutex_unlock(&stats->lock);
2463 nla_nest_cancel(msg, table);
2467 ib_device_put(device);
2471 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2472 [RDMA_NLDEV_CMD_GET] = {
2473 .doit = nldev_get_doit,
2474 .dump = nldev_get_dumpit,
2476 [RDMA_NLDEV_CMD_GET_CHARDEV] = {
2477 .doit = nldev_get_chardev,
2479 [RDMA_NLDEV_CMD_SET] = {
2480 .doit = nldev_set_doit,
2481 .flags = RDMA_NL_ADMIN_PERM,
2483 [RDMA_NLDEV_CMD_NEWLINK] = {
2484 .doit = nldev_newlink,
2485 .flags = RDMA_NL_ADMIN_PERM,
2487 [RDMA_NLDEV_CMD_DELLINK] = {
2488 .doit = nldev_dellink,
2489 .flags = RDMA_NL_ADMIN_PERM,
2491 [RDMA_NLDEV_CMD_PORT_GET] = {
2492 .doit = nldev_port_get_doit,
2493 .dump = nldev_port_get_dumpit,
2495 [RDMA_NLDEV_CMD_RES_GET] = {
2496 .doit = nldev_res_get_doit,
2497 .dump = nldev_res_get_dumpit,
2499 [RDMA_NLDEV_CMD_RES_QP_GET] = {
2500 .doit = nldev_res_get_qp_doit,
2501 .dump = nldev_res_get_qp_dumpit,
2503 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2504 .doit = nldev_res_get_cm_id_doit,
2505 .dump = nldev_res_get_cm_id_dumpit,
2507 [RDMA_NLDEV_CMD_RES_CQ_GET] = {
2508 .doit = nldev_res_get_cq_doit,
2509 .dump = nldev_res_get_cq_dumpit,
2511 [RDMA_NLDEV_CMD_RES_MR_GET] = {
2512 .doit = nldev_res_get_mr_doit,
2513 .dump = nldev_res_get_mr_dumpit,
2515 [RDMA_NLDEV_CMD_RES_PD_GET] = {
2516 .doit = nldev_res_get_pd_doit,
2517 .dump = nldev_res_get_pd_dumpit,
2519 [RDMA_NLDEV_CMD_RES_CTX_GET] = {
2520 .doit = nldev_res_get_ctx_doit,
2521 .dump = nldev_res_get_ctx_dumpit,
2523 [RDMA_NLDEV_CMD_RES_SRQ_GET] = {
2524 .doit = nldev_res_get_srq_doit,
2525 .dump = nldev_res_get_srq_dumpit,
2527 [RDMA_NLDEV_CMD_SYS_GET] = {
2528 .doit = nldev_sys_get_doit,
2530 [RDMA_NLDEV_CMD_SYS_SET] = {
2531 .doit = nldev_set_sys_set_doit,
2532 .flags = RDMA_NL_ADMIN_PERM,
2534 [RDMA_NLDEV_CMD_STAT_SET] = {
2535 .doit = nldev_stat_set_doit,
2536 .flags = RDMA_NL_ADMIN_PERM,
2538 [RDMA_NLDEV_CMD_STAT_GET] = {
2539 .doit = nldev_stat_get_doit,
2540 .dump = nldev_stat_get_dumpit,
2542 [RDMA_NLDEV_CMD_STAT_DEL] = {
2543 .doit = nldev_stat_del_doit,
2544 .flags = RDMA_NL_ADMIN_PERM,
2546 [RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
2547 .doit = nldev_res_get_qp_raw_doit,
2548 .dump = nldev_res_get_qp_raw_dumpit,
2549 .flags = RDMA_NL_ADMIN_PERM,
2551 [RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
2552 .doit = nldev_res_get_cq_raw_doit,
2553 .dump = nldev_res_get_cq_raw_dumpit,
2554 .flags = RDMA_NL_ADMIN_PERM,
2556 [RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
2557 .doit = nldev_res_get_mr_raw_doit,
2558 .dump = nldev_res_get_mr_raw_dumpit,
2559 .flags = RDMA_NL_ADMIN_PERM,
2561 [RDMA_NLDEV_CMD_STAT_GET_STATUS] = {
2562 .doit = nldev_stat_get_counter_status_doit,
2566 void __init nldev_init(void)
2568 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2571 void nldev_exit(void)
2573 rdma_nl_unregister(RDMA_NL_NLDEV);
2576 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);