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"
47 * This determines whether a non-privileged user is allowed to specify a
48 * controlled QKEY or not, when true non-privileged user is allowed to specify
51 static bool privileged_qkey;
53 typedef int (*res_fill_func_t)(struct sk_buff*, bool,
54 struct rdma_restrack_entry*, uint32_t);
57 * Sort array elements by the netlink attribute name
59 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
60 [RDMA_NLDEV_ATTR_CHARDEV] = { .type = NLA_U64 },
61 [RDMA_NLDEV_ATTR_CHARDEV_ABI] = { .type = NLA_U64 },
62 [RDMA_NLDEV_ATTR_CHARDEV_NAME] = { .type = NLA_NUL_STRING,
63 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
64 [RDMA_NLDEV_ATTR_CHARDEV_TYPE] = { .type = NLA_NUL_STRING,
65 .len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
66 [RDMA_NLDEV_ATTR_DEV_DIM] = { .type = NLA_U8 },
67 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
68 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
69 .len = IB_DEVICE_NAME_MAX },
70 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
71 [RDMA_NLDEV_ATTR_DEV_PROTOCOL] = { .type = NLA_NUL_STRING,
72 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
73 [RDMA_NLDEV_ATTR_DRIVER] = { .type = NLA_NESTED },
74 [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = { .type = NLA_NESTED },
75 [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = { .type = NLA_U8 },
76 [RDMA_NLDEV_ATTR_DRIVER_STRING] = { .type = NLA_NUL_STRING,
77 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
78 [RDMA_NLDEV_ATTR_DRIVER_S32] = { .type = NLA_S32 },
79 [RDMA_NLDEV_ATTR_DRIVER_S64] = { .type = NLA_S64 },
80 [RDMA_NLDEV_ATTR_DRIVER_U32] = { .type = NLA_U32 },
81 [RDMA_NLDEV_ATTR_DRIVER_U64] = { .type = NLA_U64 },
82 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING,
83 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
84 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 },
85 [RDMA_NLDEV_ATTR_LINK_TYPE] = { .type = NLA_NUL_STRING,
87 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 },
88 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 },
89 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING,
91 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 },
92 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
93 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
94 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 },
95 [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED },
96 [RDMA_NLDEV_ATTR_RES_CM_IDN] = { .type = NLA_U32 },
97 [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED },
98 [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED },
99 [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 },
100 [RDMA_NLDEV_ATTR_RES_CQN] = { .type = NLA_U32 },
101 [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED },
102 [RDMA_NLDEV_ATTR_RES_CTX] = { .type = NLA_NESTED },
103 [RDMA_NLDEV_ATTR_RES_CTXN] = { .type = NLA_U32 },
104 [RDMA_NLDEV_ATTR_RES_CTX_ENTRY] = { .type = NLA_NESTED },
105 [RDMA_NLDEV_ATTR_RES_DST_ADDR] = {
106 .len = sizeof(struct __kernel_sockaddr_storage) },
107 [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 },
108 [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING,
109 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
110 [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 },
111 [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 },
112 [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 },
113 [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED },
114 [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 },
115 [RDMA_NLDEV_ATTR_RES_MRN] = { .type = NLA_U32 },
116 [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED },
117 [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
118 [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED },
119 [RDMA_NLDEV_ATTR_RES_PDN] = { .type = NLA_U32 },
120 [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED },
121 [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 },
122 [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 },
123 [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 },
124 [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED },
125 [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED },
126 [RDMA_NLDEV_ATTR_RES_RAW] = { .type = NLA_BINARY },
127 [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 },
128 [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 },
129 [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 },
130 [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 },
131 [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = {
132 .len = sizeof(struct __kernel_sockaddr_storage) },
133 [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 },
134 [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED },
135 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED },
136 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
137 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
138 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
139 [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 },
140 [RDMA_NLDEV_ATTR_RES_SUBTYPE] = { .type = NLA_NUL_STRING,
141 .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
142 [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
143 [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 },
144 [RDMA_NLDEV_ATTR_RES_SRQ] = { .type = NLA_NESTED },
145 [RDMA_NLDEV_ATTR_RES_SRQN] = { .type = NLA_U32 },
146 [RDMA_NLDEV_ATTR_RES_SRQ_ENTRY] = { .type = NLA_NESTED },
147 [RDMA_NLDEV_ATTR_MIN_RANGE] = { .type = NLA_U32 },
148 [RDMA_NLDEV_ATTR_MAX_RANGE] = { .type = NLA_U32 },
149 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 },
150 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
151 [RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK] = { .type = NLA_U32 },
152 [RDMA_NLDEV_ATTR_STAT_MODE] = { .type = NLA_U32 },
153 [RDMA_NLDEV_ATTR_STAT_RES] = { .type = NLA_U32 },
154 [RDMA_NLDEV_ATTR_STAT_COUNTER] = { .type = NLA_NESTED },
155 [RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY] = { .type = NLA_NESTED },
156 [RDMA_NLDEV_ATTR_STAT_COUNTER_ID] = { .type = NLA_U32 },
157 [RDMA_NLDEV_ATTR_STAT_HWCOUNTERS] = { .type = NLA_NESTED },
158 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY] = { .type = NLA_NESTED },
159 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
160 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
161 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
162 [RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID] = { .type = NLA_U32 },
163 [RDMA_NLDEV_NET_NS_FD] = { .type = NLA_U32 },
164 [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = { .type = NLA_U8 },
165 [RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK] = { .type = NLA_U8 },
166 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX] = { .type = NLA_U32 },
167 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 },
168 [RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE] = { .type = NLA_U8 },
169 [RDMA_NLDEV_ATTR_DRIVER_DETAILS] = { .type = NLA_U8 },
170 [RDMA_NLDEV_ATTR_DEV_TYPE] = { .type = NLA_U8 },
171 [RDMA_NLDEV_ATTR_PARENT_NAME] = { .type = NLA_NUL_STRING },
172 [RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE] = { .type = NLA_U8 },
175 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
176 enum rdma_nldev_print_type print_type)
178 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
180 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
181 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
187 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
188 enum rdma_nldev_print_type print_type,
191 if (put_driver_name_print_type(msg, name, print_type))
193 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
199 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
200 enum rdma_nldev_print_type print_type,
203 if (put_driver_name_print_type(msg, name, print_type))
205 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
206 RDMA_NLDEV_ATTR_PAD))
212 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
215 if (put_driver_name_print_type(msg, name,
216 RDMA_NLDEV_PRINT_TYPE_UNSPEC))
218 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
223 EXPORT_SYMBOL(rdma_nl_put_driver_string);
225 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
227 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
230 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
232 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
235 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
238 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
240 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
242 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
245 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
247 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
249 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
252 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
254 bool rdma_nl_get_privileged_qkey(void)
256 return privileged_qkey || capable(CAP_NET_RAW);
258 EXPORT_SYMBOL(rdma_nl_get_privileged_qkey);
260 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
262 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
264 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
265 dev_name(&device->dev)))
271 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
273 char fw[IB_FW_VERSION_NAME_MAX];
277 if (fill_nldev_handle(msg, device))
280 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
283 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
284 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
285 device->attrs.device_cap_flags,
286 RDMA_NLDEV_ATTR_PAD))
289 ib_get_device_fw_str(device, fw);
290 /* Device without FW has strlen(fw) = 0 */
291 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
294 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
295 be64_to_cpu(device->node_guid),
296 RDMA_NLDEV_ATTR_PAD))
298 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
299 be64_to_cpu(device->attrs.sys_image_guid),
300 RDMA_NLDEV_ATTR_PAD))
302 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
304 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
308 nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_TYPE, device->type))
311 if (device->parent &&
312 nla_put_string(msg, RDMA_NLDEV_ATTR_PARENT_NAME,
313 dev_name(&device->parent->dev)))
316 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE,
317 device->name_assign_type))
321 * Link type is determined on first port and mlx4 device
322 * which can potentially have two different link type for the same
323 * IB device is considered as better to be avoided in the future,
325 port = rdma_start_port(device);
326 if (rdma_cap_opa_mad(device, port))
327 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
328 else if (rdma_protocol_ib(device, port))
329 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
330 else if (rdma_protocol_iwarp(device, port))
331 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
332 else if (rdma_protocol_roce(device, port))
333 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
334 else if (rdma_protocol_usnic(device, port))
335 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
340 static int fill_port_info(struct sk_buff *msg,
341 struct ib_device *device, u32 port,
342 const struct net *net)
344 struct net_device *netdev = NULL;
345 struct ib_port_attr attr;
349 if (fill_nldev_handle(msg, device))
352 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
355 ret = ib_query_port(device, port, &attr);
359 if (rdma_protocol_ib(device, port)) {
360 BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
361 sizeof(attr.port_cap_flags2)) > sizeof(u64));
362 cap_flags = attr.port_cap_flags |
363 ((u64)attr.port_cap_flags2 << 32);
364 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
365 cap_flags, RDMA_NLDEV_ATTR_PAD))
367 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
368 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
370 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
372 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
374 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
377 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
379 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
382 netdev = ib_device_get_netdev(device, port);
383 if (netdev && net_eq(dev_net(netdev), net)) {
384 ret = nla_put_u32(msg,
385 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
388 ret = nla_put_string(msg,
389 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
397 static int fill_res_info_entry(struct sk_buff *msg,
398 const char *name, u64 curr)
400 struct nlattr *entry_attr;
402 entry_attr = nla_nest_start_noflag(msg,
403 RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
407 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
409 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
410 RDMA_NLDEV_ATTR_PAD))
413 nla_nest_end(msg, entry_attr);
417 nla_nest_cancel(msg, entry_attr);
421 static int fill_res_info(struct sk_buff *msg, struct ib_device *device,
424 static const char * const names[RDMA_RESTRACK_MAX] = {
425 [RDMA_RESTRACK_PD] = "pd",
426 [RDMA_RESTRACK_CQ] = "cq",
427 [RDMA_RESTRACK_QP] = "qp",
428 [RDMA_RESTRACK_CM_ID] = "cm_id",
429 [RDMA_RESTRACK_MR] = "mr",
430 [RDMA_RESTRACK_CTX] = "ctx",
431 [RDMA_RESTRACK_SRQ] = "srq",
434 struct nlattr *table_attr;
437 if (fill_nldev_handle(msg, device))
440 table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
444 for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
447 curr = rdma_restrack_count(device, i, show_details);
448 ret = fill_res_info_entry(msg, names[i], curr);
453 nla_nest_end(msg, table_attr);
457 nla_nest_cancel(msg, table_attr);
461 static int fill_res_name_pid(struct sk_buff *msg,
462 struct rdma_restrack_entry *res)
467 * For user resources, user is should read /proc/PID/comm to get the
468 * name of the task file.
470 if (rdma_is_kernel_res(res)) {
471 err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
476 pid = task_pid_vnr(res->task);
478 * Task is dead and in zombie state.
479 * There is no need to print PID anymore.
483 * This part is racy, task can be killed and PID will
484 * be zero right here but it is ok, next query won't
485 * return PID. We don't promise real-time reflection
488 err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
491 return err ? -EMSGSIZE : 0;
494 static int fill_res_qp_entry_query(struct sk_buff *msg,
495 struct rdma_restrack_entry *res,
496 struct ib_device *dev,
499 struct ib_qp_init_attr qp_init_attr;
500 struct ib_qp_attr qp_attr;
503 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
507 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
508 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
509 qp_attr.dest_qp_num))
511 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
516 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
519 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
520 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
521 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
522 qp_attr.path_mig_state))
525 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
527 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
530 if (dev->ops.fill_res_qp_entry)
531 return dev->ops.fill_res_qp_entry(msg, qp);
534 err: return -EMSGSIZE;
537 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
538 struct rdma_restrack_entry *res, uint32_t port)
540 struct ib_qp *qp = container_of(res, struct ib_qp, res);
541 struct ib_device *dev = qp->device;
544 if (port && port != qp->port)
547 /* In create_qp() port is not set yet */
548 if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
551 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
555 if (!rdma_is_kernel_res(res) &&
556 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
559 ret = fill_res_name_pid(msg, res);
563 return fill_res_qp_entry_query(msg, res, dev, qp);
566 static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
567 struct rdma_restrack_entry *res, uint32_t port)
569 struct ib_qp *qp = container_of(res, struct ib_qp, res);
570 struct ib_device *dev = qp->device;
572 if (port && port != qp->port)
574 if (!dev->ops.fill_res_qp_entry_raw)
576 return dev->ops.fill_res_qp_entry_raw(msg, qp);
579 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
580 struct rdma_restrack_entry *res, uint32_t port)
582 struct rdma_id_private *id_priv =
583 container_of(res, struct rdma_id_private, res);
584 struct ib_device *dev = id_priv->id.device;
585 struct rdma_cm_id *cm_id = &id_priv->id;
587 if (port && port != cm_id->port_num)
590 if (cm_id->port_num &&
591 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
594 if (id_priv->qp_num) {
595 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
597 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
601 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
604 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
607 if (cm_id->route.addr.src_addr.ss_family &&
608 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
609 sizeof(cm_id->route.addr.src_addr),
610 &cm_id->route.addr.src_addr))
612 if (cm_id->route.addr.dst_addr.ss_family &&
613 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
614 sizeof(cm_id->route.addr.dst_addr),
615 &cm_id->route.addr.dst_addr))
618 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
621 if (fill_res_name_pid(msg, res))
624 if (dev->ops.fill_res_cm_id_entry)
625 return dev->ops.fill_res_cm_id_entry(msg, cm_id);
628 err: return -EMSGSIZE;
631 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
632 struct rdma_restrack_entry *res, uint32_t port)
634 struct ib_cq *cq = container_of(res, struct ib_cq, res);
635 struct ib_device *dev = cq->device;
637 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
639 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
640 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
643 /* Poll context is only valid for kernel CQs */
644 if (rdma_is_kernel_res(res) &&
645 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
648 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
651 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
653 if (!rdma_is_kernel_res(res) &&
654 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
655 cq->uobject->uevent.uobject.context->res.id))
658 if (fill_res_name_pid(msg, res))
661 return (dev->ops.fill_res_cq_entry) ?
662 dev->ops.fill_res_cq_entry(msg, cq) : 0;
665 static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
666 struct rdma_restrack_entry *res, uint32_t port)
668 struct ib_cq *cq = container_of(res, struct ib_cq, res);
669 struct ib_device *dev = cq->device;
671 if (!dev->ops.fill_res_cq_entry_raw)
673 return dev->ops.fill_res_cq_entry_raw(msg, cq);
676 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
677 struct rdma_restrack_entry *res, uint32_t port)
679 struct ib_mr *mr = container_of(res, struct ib_mr, res);
680 struct ib_device *dev = mr->pd->device;
682 if (has_cap_net_admin) {
683 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
685 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
689 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
690 RDMA_NLDEV_ATTR_PAD))
693 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
696 if (!rdma_is_kernel_res(res) &&
697 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
700 if (fill_res_name_pid(msg, res))
703 return (dev->ops.fill_res_mr_entry) ?
704 dev->ops.fill_res_mr_entry(msg, mr) :
708 static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
709 struct rdma_restrack_entry *res, uint32_t port)
711 struct ib_mr *mr = container_of(res, struct ib_mr, res);
712 struct ib_device *dev = mr->pd->device;
714 if (!dev->ops.fill_res_mr_entry_raw)
716 return dev->ops.fill_res_mr_entry_raw(msg, mr);
719 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
720 struct rdma_restrack_entry *res, uint32_t port)
722 struct ib_pd *pd = container_of(res, struct ib_pd, res);
724 if (has_cap_net_admin) {
725 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
728 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
729 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
730 pd->unsafe_global_rkey))
733 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
734 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
737 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
740 if (!rdma_is_kernel_res(res) &&
741 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
742 pd->uobject->context->res.id))
745 return fill_res_name_pid(msg, res);
747 err: return -EMSGSIZE;
750 static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
751 struct rdma_restrack_entry *res, uint32_t port)
753 struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
755 if (rdma_is_kernel_res(res))
758 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
761 return fill_res_name_pid(msg, res);
764 static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
767 struct nlattr *entry_attr;
772 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
776 if (min_range == max_range) {
777 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
780 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
782 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
785 nla_nest_end(msg, entry_attr);
789 nla_nest_cancel(msg, entry_attr);
793 static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
795 uint32_t min_range = 0, prev = 0;
796 struct rdma_restrack_entry *res;
797 struct rdma_restrack_root *rt;
798 struct nlattr *table_attr;
799 struct ib_qp *qp = NULL;
800 unsigned long id = 0;
802 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
806 rt = &srq->device->res[RDMA_RESTRACK_QP];
808 xa_for_each(&rt->xa, id, res) {
809 if (!rdma_restrack_get(res))
812 qp = container_of(res, struct ib_qp, res);
813 if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
814 rdma_restrack_put(res);
818 if (qp->qp_num < prev)
819 /* qp_num should be ascending */
822 if (min_range == 0) {
823 min_range = qp->qp_num;
824 } else if (qp->qp_num > (prev + 1)) {
825 if (fill_res_range_qp_entry(msg, min_range, prev))
828 min_range = qp->qp_num;
831 rdma_restrack_put(res);
836 if (fill_res_range_qp_entry(msg, min_range, prev))
839 nla_nest_end(msg, table_attr);
843 rdma_restrack_put(res);
846 nla_nest_cancel(msg, table_attr);
850 static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
851 struct rdma_restrack_entry *res, uint32_t port)
853 struct ib_srq *srq = container_of(res, struct ib_srq, res);
854 struct ib_device *dev = srq->device;
856 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
859 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
862 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
865 if (ib_srq_has_cq(srq->srq_type)) {
866 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
867 srq->ext.cq->res.id))
871 if (fill_res_srq_qps(msg, srq))
874 if (fill_res_name_pid(msg, res))
877 if (dev->ops.fill_res_srq_entry)
878 return dev->ops.fill_res_srq_entry(msg, srq);
886 static int fill_res_srq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
887 struct rdma_restrack_entry *res, uint32_t port)
889 struct ib_srq *srq = container_of(res, struct ib_srq, res);
890 struct ib_device *dev = srq->device;
892 if (!dev->ops.fill_res_srq_entry_raw)
894 return dev->ops.fill_res_srq_entry_raw(msg, srq);
897 static int fill_stat_counter_mode(struct sk_buff *msg,
898 struct rdma_counter *counter)
900 struct rdma_counter_mode *m = &counter->mode;
902 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
905 if (m->mode == RDMA_COUNTER_MODE_AUTO) {
906 if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
907 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
910 if ((m->mask & RDMA_COUNTER_MASK_PID) &&
911 fill_res_name_pid(msg, &counter->res))
918 static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
920 struct nlattr *entry_attr;
922 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
926 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
929 nla_nest_end(msg, entry_attr);
933 nla_nest_cancel(msg, entry_attr);
937 static int fill_stat_counter_qps(struct sk_buff *msg,
938 struct rdma_counter *counter)
940 struct rdma_restrack_entry *res;
941 struct rdma_restrack_root *rt;
942 struct nlattr *table_attr;
943 struct ib_qp *qp = NULL;
944 unsigned long id = 0;
947 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
951 rt = &counter->device->res[RDMA_RESTRACK_QP];
953 xa_for_each(&rt->xa, id, res) {
954 qp = container_of(res, struct ib_qp, res);
955 if (!qp->counter || (qp->counter->id != counter->id))
958 ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
964 nla_nest_end(msg, table_attr);
969 nla_nest_cancel(msg, table_attr);
973 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
976 struct nlattr *entry_attr;
978 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
982 if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
985 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
986 value, RDMA_NLDEV_ATTR_PAD))
989 nla_nest_end(msg, entry_attr);
993 nla_nest_cancel(msg, entry_attr);
996 EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
998 static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
999 struct rdma_restrack_entry *res, uint32_t port)
1001 struct ib_mr *mr = container_of(res, struct ib_mr, res);
1002 struct ib_device *dev = mr->pd->device;
1004 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
1007 if (dev->ops.fill_stat_mr_entry)
1008 return dev->ops.fill_stat_mr_entry(msg, mr);
1015 static int fill_stat_counter_hwcounters(struct sk_buff *msg,
1016 struct rdma_counter *counter)
1018 struct rdma_hw_stats *st = counter->stats;
1019 struct nlattr *table_attr;
1022 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
1026 mutex_lock(&st->lock);
1027 for (i = 0; i < st->num_counters; i++) {
1028 if (test_bit(i, st->is_disabled))
1030 if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name,
1034 mutex_unlock(&st->lock);
1036 nla_nest_end(msg, table_attr);
1040 mutex_unlock(&st->lock);
1041 nla_nest_cancel(msg, table_attr);
1045 static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
1046 struct rdma_restrack_entry *res,
1049 struct rdma_counter *counter =
1050 container_of(res, struct rdma_counter, res);
1052 if (port && port != counter->port)
1055 /* Dump it even query failed */
1056 rdma_counter_query_stats(counter);
1058 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
1059 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
1060 fill_stat_counter_mode(msg, counter) ||
1061 fill_stat_counter_qps(msg, counter) ||
1062 fill_stat_counter_hwcounters(msg, counter))
1068 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1069 struct netlink_ext_ack *extack)
1071 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1072 struct ib_device *device;
1073 struct sk_buff *msg;
1077 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1078 nldev_policy, extack);
1079 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1082 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1084 device = ib_device_get_by_index(sock_net(skb->sk), index);
1088 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1094 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1095 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1102 err = fill_dev_info(msg, device);
1106 nlmsg_end(msg, nlh);
1108 ib_device_put(device);
1109 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1114 ib_device_put(device);
1118 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1119 struct netlink_ext_ack *extack)
1121 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1122 struct ib_device *device;
1126 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1127 nldev_policy, extack);
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);
1136 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1137 char name[IB_DEVICE_NAME_MAX] = {};
1139 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1140 IB_DEVICE_NAME_MAX);
1141 if (strlen(name) == 0) {
1145 err = ib_device_rename(device, name);
1149 if (tb[RDMA_NLDEV_NET_NS_FD]) {
1152 ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1153 err = ib_device_set_netns_put(skb, device, ns_fd);
1157 if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1160 use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1161 err = ib_device_set_dim(device, use_dim);
1166 ib_device_put(device);
1171 static int _nldev_get_dumpit(struct ib_device *device,
1172 struct sk_buff *skb,
1173 struct netlink_callback *cb,
1176 int start = cb->args[0];
1177 struct nlmsghdr *nlh;
1182 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1183 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1186 if (!nlh || fill_dev_info(skb, device)) {
1187 nlmsg_cancel(skb, nlh);
1191 nlmsg_end(skb, nlh);
1195 out: cb->args[0] = idx;
1199 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1202 * There is no need to take lock, because
1203 * we are relying on ib_core's locking.
1205 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1208 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1209 struct netlink_ext_ack *extack)
1211 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1212 struct ib_device *device;
1213 struct sk_buff *msg;
1218 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1219 nldev_policy, extack);
1221 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1222 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1225 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1226 device = ib_device_get_by_index(sock_net(skb->sk), index);
1230 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1231 if (!rdma_is_port_valid(device, port)) {
1236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1242 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1243 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1250 err = fill_port_info(msg, device, port, sock_net(skb->sk));
1254 nlmsg_end(msg, nlh);
1255 ib_device_put(device);
1257 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1262 ib_device_put(device);
1266 static int nldev_port_get_dumpit(struct sk_buff *skb,
1267 struct netlink_callback *cb)
1269 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1270 struct ib_device *device;
1271 int start = cb->args[0];
1272 struct nlmsghdr *nlh;
1278 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1279 nldev_policy, NULL);
1280 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1283 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1284 device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1288 rdma_for_each_port (device, p) {
1290 * The dumpit function returns all information from specific
1291 * index. This specific index is taken from the netlink
1292 * messages request sent by user and it is available
1295 * Usually, the user doesn't fill this field and it causes
1296 * to return everything.
1304 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1306 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1307 RDMA_NLDEV_CMD_PORT_GET),
1310 if (!nlh || fill_port_info(skb, device, p, sock_net(skb->sk))) {
1311 nlmsg_cancel(skb, nlh);
1315 nlmsg_end(skb, nlh);
1319 ib_device_put(device);
1324 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1325 struct netlink_ext_ack *extack)
1327 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1328 bool show_details = false;
1329 struct ib_device *device;
1330 struct sk_buff *msg;
1334 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1335 nldev_policy, extack);
1336 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1339 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1340 device = ib_device_get_by_index(sock_net(skb->sk), index);
1344 if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS])
1345 show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]);
1347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1353 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1354 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1361 ret = fill_res_info(msg, device, show_details);
1365 nlmsg_end(msg, nlh);
1366 ib_device_put(device);
1367 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1372 ib_device_put(device);
1376 static int _nldev_res_get_dumpit(struct ib_device *device,
1377 struct sk_buff *skb,
1378 struct netlink_callback *cb,
1381 int start = cb->args[0];
1382 struct nlmsghdr *nlh;
1387 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1388 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1391 if (!nlh || fill_res_info(skb, device, false)) {
1392 nlmsg_cancel(skb, nlh);
1395 nlmsg_end(skb, nlh);
1404 static int nldev_res_get_dumpit(struct sk_buff *skb,
1405 struct netlink_callback *cb)
1407 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1410 struct nldev_fill_res_entry {
1411 enum rdma_nldev_attr nldev_attr;
1417 enum nldev_res_flags {
1418 NLDEV_PER_DEV = 1 << 0,
1421 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1422 [RDMA_RESTRACK_QP] = {
1423 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1424 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1425 .id = RDMA_NLDEV_ATTR_RES_LQPN,
1427 [RDMA_RESTRACK_CM_ID] = {
1428 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1429 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1430 .id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1432 [RDMA_RESTRACK_CQ] = {
1433 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1434 .flags = NLDEV_PER_DEV,
1435 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1436 .id = RDMA_NLDEV_ATTR_RES_CQN,
1438 [RDMA_RESTRACK_MR] = {
1439 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1440 .flags = NLDEV_PER_DEV,
1441 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1442 .id = RDMA_NLDEV_ATTR_RES_MRN,
1444 [RDMA_RESTRACK_PD] = {
1445 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1446 .flags = NLDEV_PER_DEV,
1447 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1448 .id = RDMA_NLDEV_ATTR_RES_PDN,
1450 [RDMA_RESTRACK_COUNTER] = {
1451 .nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1452 .entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1453 .id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1455 [RDMA_RESTRACK_CTX] = {
1456 .nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1457 .flags = NLDEV_PER_DEV,
1458 .entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1459 .id = RDMA_NLDEV_ATTR_RES_CTXN,
1461 [RDMA_RESTRACK_SRQ] = {
1462 .nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1463 .flags = NLDEV_PER_DEV,
1464 .entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1465 .id = RDMA_NLDEV_ATTR_RES_SRQN,
1470 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1471 struct netlink_ext_ack *extack,
1472 enum rdma_restrack_type res_type,
1473 res_fill_func_t fill_func)
1475 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1476 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1477 struct rdma_restrack_entry *res;
1478 struct ib_device *device;
1479 u32 index, id, port = 0;
1480 bool has_cap_net_admin;
1481 struct sk_buff *msg;
1484 ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1485 nldev_policy, extack);
1486 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1489 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1490 device = ib_device_get_by_index(sock_net(skb->sk), index);
1494 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1495 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1496 if (!rdma_is_port_valid(device, port)) {
1502 if ((port && fe->flags & NLDEV_PER_DEV) ||
1503 (!port && ~fe->flags & NLDEV_PER_DEV)) {
1508 id = nla_get_u32(tb[fe->id]);
1509 res = rdma_restrack_get_byid(device, res_type, id);
1515 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1521 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1522 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1523 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1526 if (!nlh || fill_nldev_handle(msg, device)) {
1531 has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1533 ret = fill_func(msg, has_cap_net_admin, res, port);
1537 rdma_restrack_put(res);
1538 nlmsg_end(msg, nlh);
1539 ib_device_put(device);
1540 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1545 rdma_restrack_put(res);
1547 ib_device_put(device);
1551 static int res_get_common_dumpit(struct sk_buff *skb,
1552 struct netlink_callback *cb,
1553 enum rdma_restrack_type res_type,
1554 res_fill_func_t fill_func)
1556 const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1557 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1558 struct rdma_restrack_entry *res;
1559 struct rdma_restrack_root *rt;
1560 int err, ret = 0, idx = 0;
1561 bool show_details = false;
1562 struct nlattr *table_attr;
1563 struct nlattr *entry_attr;
1564 struct ib_device *device;
1565 int start = cb->args[0];
1566 bool has_cap_net_admin;
1567 struct nlmsghdr *nlh;
1569 u32 index, port = 0;
1570 bool filled = false;
1572 err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1573 nldev_policy, NULL);
1575 * Right now, we are expecting the device index to get res information,
1576 * but it is possible to extend this code to return all devices in
1577 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1578 * if it doesn't exist, we will iterate over all devices.
1580 * But it is not needed for now.
1582 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1585 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1586 device = ib_device_get_by_index(sock_net(skb->sk), index);
1590 if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS])
1591 show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]);
1594 * If no PORT_INDEX is supplied, we will return all QPs from that device
1596 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1597 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1598 if (!rdma_is_port_valid(device, port)) {
1604 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1605 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1606 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1609 if (!nlh || fill_nldev_handle(skb, device)) {
1614 table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1620 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1622 rt = &device->res[res_type];
1625 * FIXME: if the skip ahead is something common this loop should
1626 * use xas_for_each & xas_pause to optimize, we can have a lot of
1629 xa_for_each(&rt->xa, id, res) {
1630 if (xa_get_mark(&rt->xa, res->id, RESTRACK_DD) && !show_details)
1633 if (idx < start || !rdma_restrack_get(res))
1640 entry_attr = nla_nest_start_noflag(skb, fe->entry);
1643 rdma_restrack_put(res);
1647 ret = fill_func(skb, has_cap_net_admin, res, port);
1649 rdma_restrack_put(res);
1652 nla_nest_cancel(skb, entry_attr);
1653 if (ret == -EMSGSIZE)
1659 nla_nest_end(skb, entry_attr);
1660 again: xa_lock(&rt->xa);
1666 nla_nest_end(skb, table_attr);
1667 nlmsg_end(skb, nlh);
1671 * No more entries to fill, cancel the message and
1672 * return 0 to mark end of dumpit.
1677 ib_device_put(device);
1681 nla_nest_cancel(skb, table_attr);
1684 nlmsg_cancel(skb, nlh);
1687 ib_device_put(device);
1691 #define RES_GET_FUNCS(name, type) \
1692 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \
1693 struct netlink_callback *cb) \
1695 return res_get_common_dumpit(skb, cb, type, \
1696 fill_res_##name##_entry); \
1698 static int nldev_res_get_##name##_doit(struct sk_buff *skb, \
1699 struct nlmsghdr *nlh, \
1700 struct netlink_ext_ack *extack) \
1702 return res_get_common_doit(skb, nlh, extack, type, \
1703 fill_res_##name##_entry); \
1706 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1707 RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1708 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1709 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1710 RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1711 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1712 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1713 RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1714 RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1715 RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1716 RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1717 RES_GET_FUNCS(srq_raw, RDMA_RESTRACK_SRQ);
1719 static LIST_HEAD(link_ops);
1720 static DECLARE_RWSEM(link_ops_rwsem);
1722 static const struct rdma_link_ops *link_ops_get(const char *type)
1724 const struct rdma_link_ops *ops;
1726 list_for_each_entry(ops, &link_ops, list) {
1727 if (!strcmp(ops->type, type))
1735 void rdma_link_register(struct rdma_link_ops *ops)
1737 down_write(&link_ops_rwsem);
1738 if (WARN_ON_ONCE(link_ops_get(ops->type)))
1740 list_add(&ops->list, &link_ops);
1742 up_write(&link_ops_rwsem);
1744 EXPORT_SYMBOL(rdma_link_register);
1746 void rdma_link_unregister(struct rdma_link_ops *ops)
1748 down_write(&link_ops_rwsem);
1749 list_del(&ops->list);
1750 up_write(&link_ops_rwsem);
1752 EXPORT_SYMBOL(rdma_link_unregister);
1754 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1755 struct netlink_ext_ack *extack)
1757 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1758 char ibdev_name[IB_DEVICE_NAME_MAX];
1759 const struct rdma_link_ops *ops;
1760 char ndev_name[IFNAMSIZ];
1761 struct net_device *ndev;
1762 char type[IFNAMSIZ];
1765 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1766 nldev_policy, extack);
1767 if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1768 !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1771 nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1772 sizeof(ibdev_name));
1773 if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1776 nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1777 nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1780 ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1784 down_read(&link_ops_rwsem);
1785 ops = link_ops_get(type);
1786 #ifdef CONFIG_MODULES
1788 up_read(&link_ops_rwsem);
1789 request_module("rdma-link-%s", type);
1790 down_read(&link_ops_rwsem);
1791 ops = link_ops_get(type);
1794 err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1795 up_read(&link_ops_rwsem);
1801 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1802 struct netlink_ext_ack *extack)
1804 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1805 struct ib_device *device;
1809 err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1810 nldev_policy, extack);
1811 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1814 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1815 device = ib_device_get_by_index(sock_net(skb->sk), index);
1819 if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) {
1820 ib_device_put(device);
1824 ib_unregister_device_and_put(device);
1828 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1829 struct netlink_ext_ack *extack)
1831 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1832 char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1833 struct ib_client_nl_info data = {};
1834 struct ib_device *ibdev = NULL;
1835 struct sk_buff *msg;
1839 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1841 if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1844 nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1845 sizeof(client_name));
1847 if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1848 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1849 ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1853 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1854 data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1855 if (!rdma_is_port_valid(ibdev, data.port)) {
1862 } else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1871 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1872 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1873 RDMA_NLDEV_CMD_GET_CHARDEV),
1881 err = ib_get_client_nl_info(ibdev, client_name, &data);
1885 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1886 huge_encode_dev(data.cdev->devt),
1887 RDMA_NLDEV_ATTR_PAD);
1890 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1891 RDMA_NLDEV_ATTR_PAD);
1894 if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1895 dev_name(data.cdev))) {
1900 nlmsg_end(msg, nlh);
1901 put_device(data.cdev);
1903 ib_device_put(ibdev);
1904 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1907 put_device(data.cdev);
1912 ib_device_put(ibdev);
1916 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1917 struct netlink_ext_ack *extack)
1919 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1920 struct sk_buff *msg;
1923 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1924 nldev_policy, extack);
1928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1932 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1933 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1934 RDMA_NLDEV_CMD_SYS_GET),
1941 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1942 (u8)ib_devices_shared_netns);
1948 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE,
1949 (u8)privileged_qkey);
1955 * Copy-on-fork is supported.
1957 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
1958 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
1959 * for more details. Don't backport this without them.
1961 * Return value ignored on purpose, assume copy-on-fork is not
1962 * supported in case of failure.
1964 nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
1966 nlmsg_end(msg, nlh);
1967 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1970 static int nldev_set_sys_set_netns_doit(struct nlattr *tb[])
1975 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1976 /* Only 0 and 1 are supported */
1980 err = rdma_compatdev_set(enable);
1984 static int nldev_set_sys_set_pqkey_doit(struct nlattr *tb[])
1988 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE]);
1989 /* Only 0 and 1 are supported */
1993 privileged_qkey = enable;
1997 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1998 struct netlink_ext_ack *extack)
2000 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2003 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2004 nldev_policy, extack);
2008 if (tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
2009 return nldev_set_sys_set_netns_doit(tb);
2011 if (tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE])
2012 return nldev_set_sys_set_pqkey_doit(tb);
2018 static int nldev_stat_set_mode_doit(struct sk_buff *msg,
2019 struct netlink_ext_ack *extack,
2020 struct nlattr *tb[],
2021 struct ib_device *device, u32 port)
2023 u32 mode, mask = 0, qpn, cntn = 0;
2026 /* Currently only counter for QP is supported */
2027 if (!tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2028 nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2031 mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
2032 if (mode == RDMA_COUNTER_MODE_AUTO) {
2033 if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
2035 tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
2036 return rdma_counter_set_auto_mode(device, port, mask, extack);
2039 if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
2042 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2043 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
2044 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2045 ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
2049 ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn);
2054 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2055 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2063 rdma_counter_unbind_qpn(device, port, qpn, cntn);
2067 static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
2068 struct ib_device *device,
2071 struct rdma_hw_stats *stats;
2072 struct nlattr *entry_attr;
2073 unsigned long *target;
2074 int rem, i, ret = 0;
2077 stats = ib_get_hw_stats_port(device, port);
2081 target = kcalloc(BITS_TO_LONGS(stats->num_counters),
2082 sizeof(*stats->is_disabled), GFP_KERNEL);
2086 nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS],
2088 index = nla_get_u32(entry_attr);
2089 if ((index >= stats->num_counters) ||
2090 !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) {
2095 set_bit(index, target);
2098 for (i = 0; i < stats->num_counters; i++) {
2099 if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL))
2102 ret = rdma_counter_modify(device, port, i, test_bit(i, target));
2112 static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2113 struct netlink_ext_ack *extack)
2115 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2116 struct ib_device *device;
2117 struct sk_buff *msg;
2121 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2123 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2124 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2127 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2128 device = ib_device_get_by_index(sock_net(skb->sk), index);
2132 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2133 if (!rdma_is_port_valid(device, port)) {
2135 goto err_put_device;
2138 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] &&
2139 !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2141 goto err_put_device;
2144 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2147 goto err_put_device;
2149 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2150 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2151 RDMA_NLDEV_CMD_STAT_SET),
2153 if (!nlh || fill_nldev_handle(msg, device) ||
2154 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2159 if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) {
2160 ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port);
2165 if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2166 ret = nldev_stat_set_counter_dynamic_doit(tb, device, port);
2171 nlmsg_end(msg, nlh);
2172 ib_device_put(device);
2173 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2178 ib_device_put(device);
2182 static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2183 struct netlink_ext_ack *extack)
2185 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2186 struct ib_device *device;
2187 struct sk_buff *msg;
2188 u32 index, port, qpn, cntn;
2191 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2192 nldev_policy, extack);
2193 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2194 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
2195 !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
2196 !tb[RDMA_NLDEV_ATTR_RES_LQPN])
2199 if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2202 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2203 device = ib_device_get_by_index(sock_net(skb->sk), index);
2207 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2208 if (!rdma_is_port_valid(device, port)) {
2213 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2218 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2219 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2220 RDMA_NLDEV_CMD_STAT_SET),
2227 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2228 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2229 if (fill_nldev_handle(msg, device) ||
2230 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2231 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2232 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2237 ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2241 nlmsg_end(msg, nlh);
2242 ib_device_put(device);
2243 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2248 ib_device_put(device);
2252 static int stat_get_doit_default_counter(struct sk_buff *skb,
2253 struct nlmsghdr *nlh,
2254 struct netlink_ext_ack *extack,
2255 struct nlattr *tb[])
2257 struct rdma_hw_stats *stats;
2258 struct nlattr *table_attr;
2259 struct ib_device *device;
2260 int ret, num_cnts, i;
2261 struct sk_buff *msg;
2265 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2268 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2269 device = ib_device_get_by_index(sock_net(skb->sk), index);
2273 if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2278 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2279 stats = ib_get_hw_stats_port(device, port);
2285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2291 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2292 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2293 RDMA_NLDEV_CMD_STAT_GET),
2296 if (!nlh || fill_nldev_handle(msg, device) ||
2297 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2302 mutex_lock(&stats->lock);
2304 num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2310 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2315 for (i = 0; i < num_cnts; i++) {
2316 if (test_bit(i, stats->is_disabled))
2319 v = stats->value[i] +
2320 rdma_counter_get_hwstat_value(device, port, i);
2321 if (rdma_nl_stat_hwcounter_entry(msg,
2322 stats->descs[i].name, v)) {
2327 nla_nest_end(msg, table_attr);
2329 mutex_unlock(&stats->lock);
2330 nlmsg_end(msg, nlh);
2331 ib_device_put(device);
2332 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2335 nla_nest_cancel(msg, table_attr);
2337 mutex_unlock(&stats->lock);
2341 ib_device_put(device);
2345 static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2346 struct netlink_ext_ack *extack, struct nlattr *tb[])
2349 static enum rdma_nl_counter_mode mode;
2350 static enum rdma_nl_counter_mask mask;
2351 struct ib_device *device;
2352 struct sk_buff *msg;
2356 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2357 return nldev_res_get_counter_doit(skb, nlh, extack);
2359 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2360 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2363 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2364 device = ib_device_get_by_index(sock_net(skb->sk), index);
2368 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2369 if (!rdma_is_port_valid(device, port)) {
2374 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2380 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2381 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2382 RDMA_NLDEV_CMD_STAT_GET),
2389 ret = rdma_counter_get_mode(device, port, &mode, &mask);
2393 if (fill_nldev_handle(msg, device) ||
2394 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2395 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2400 if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2401 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2406 nlmsg_end(msg, nlh);
2407 ib_device_put(device);
2408 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2413 ib_device_put(device);
2417 static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2418 struct netlink_ext_ack *extack)
2420 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2423 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2424 nldev_policy, extack);
2428 if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2429 return stat_get_doit_default_counter(skb, nlh, extack, tb);
2431 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2432 case RDMA_NLDEV_ATTR_RES_QP:
2433 ret = stat_get_doit_qp(skb, nlh, extack, tb);
2435 case RDMA_NLDEV_ATTR_RES_MR:
2436 ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2437 fill_stat_mr_entry);
2447 static int nldev_stat_get_dumpit(struct sk_buff *skb,
2448 struct netlink_callback *cb)
2450 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2453 ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2454 nldev_policy, NULL);
2455 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2458 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2459 case RDMA_NLDEV_ATTR_RES_QP:
2460 ret = nldev_res_get_counter_dumpit(skb, cb);
2462 case RDMA_NLDEV_ATTR_RES_MR:
2463 ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2464 fill_stat_mr_entry);
2474 static int nldev_stat_get_counter_status_doit(struct sk_buff *skb,
2475 struct nlmsghdr *nlh,
2476 struct netlink_ext_ack *extack)
2478 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry;
2479 struct rdma_hw_stats *stats;
2480 struct ib_device *device;
2481 struct sk_buff *msg;
2485 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2486 nldev_policy, extack);
2487 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2488 !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2491 devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2492 device = ib_device_get_by_index(sock_net(skb->sk), devid);
2496 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2497 if (!rdma_is_port_valid(device, port)) {
2502 stats = ib_get_hw_stats_port(device, port);
2508 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2515 msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2516 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS),
2520 if (!nlh || fill_nldev_handle(msg, device) ||
2521 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
2524 table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2528 mutex_lock(&stats->lock);
2529 for (i = 0; i < stats->num_counters; i++) {
2530 entry = nla_nest_start(msg,
2531 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
2535 if (nla_put_string(msg,
2536 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
2537 stats->descs[i].name) ||
2538 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i))
2541 if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) &&
2542 (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC,
2543 !test_bit(i, stats->is_disabled))))
2546 nla_nest_end(msg, entry);
2548 mutex_unlock(&stats->lock);
2550 nla_nest_end(msg, table);
2551 nlmsg_end(msg, nlh);
2552 ib_device_put(device);
2553 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2556 nla_nest_cancel(msg, entry);
2558 mutex_unlock(&stats->lock);
2559 nla_nest_cancel(msg, table);
2563 ib_device_put(device);
2567 static int nldev_newdev(struct sk_buff *skb, struct nlmsghdr *nlh,
2568 struct netlink_ext_ack *extack)
2570 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2571 enum rdma_nl_dev_type type;
2572 struct ib_device *parent;
2573 char name[IFNAMSIZ] = {};
2577 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2578 nldev_policy, extack);
2579 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2580 !tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_TYPE])
2583 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], sizeof(name));
2584 type = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_TYPE]);
2585 parentid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2586 parent = ib_device_get_by_index(sock_net(skb->sk), parentid);
2590 ret = ib_add_sub_device(parent, type, name);
2591 ib_device_put(parent);
2596 static int nldev_deldev(struct sk_buff *skb, struct nlmsghdr *nlh,
2597 struct netlink_ext_ack *extack)
2599 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2600 struct ib_device *device;
2604 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2605 nldev_policy, extack);
2606 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
2609 devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2610 device = ib_device_get_by_index(sock_net(skb->sk), devid);
2614 return ib_del_sub_device_and_put(device);
2617 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2618 [RDMA_NLDEV_CMD_GET] = {
2619 .doit = nldev_get_doit,
2620 .dump = nldev_get_dumpit,
2622 [RDMA_NLDEV_CMD_GET_CHARDEV] = {
2623 .doit = nldev_get_chardev,
2625 [RDMA_NLDEV_CMD_SET] = {
2626 .doit = nldev_set_doit,
2627 .flags = RDMA_NL_ADMIN_PERM,
2629 [RDMA_NLDEV_CMD_NEWLINK] = {
2630 .doit = nldev_newlink,
2631 .flags = RDMA_NL_ADMIN_PERM,
2633 [RDMA_NLDEV_CMD_DELLINK] = {
2634 .doit = nldev_dellink,
2635 .flags = RDMA_NL_ADMIN_PERM,
2637 [RDMA_NLDEV_CMD_PORT_GET] = {
2638 .doit = nldev_port_get_doit,
2639 .dump = nldev_port_get_dumpit,
2641 [RDMA_NLDEV_CMD_RES_GET] = {
2642 .doit = nldev_res_get_doit,
2643 .dump = nldev_res_get_dumpit,
2645 [RDMA_NLDEV_CMD_RES_QP_GET] = {
2646 .doit = nldev_res_get_qp_doit,
2647 .dump = nldev_res_get_qp_dumpit,
2649 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2650 .doit = nldev_res_get_cm_id_doit,
2651 .dump = nldev_res_get_cm_id_dumpit,
2653 [RDMA_NLDEV_CMD_RES_CQ_GET] = {
2654 .doit = nldev_res_get_cq_doit,
2655 .dump = nldev_res_get_cq_dumpit,
2657 [RDMA_NLDEV_CMD_RES_MR_GET] = {
2658 .doit = nldev_res_get_mr_doit,
2659 .dump = nldev_res_get_mr_dumpit,
2661 [RDMA_NLDEV_CMD_RES_PD_GET] = {
2662 .doit = nldev_res_get_pd_doit,
2663 .dump = nldev_res_get_pd_dumpit,
2665 [RDMA_NLDEV_CMD_RES_CTX_GET] = {
2666 .doit = nldev_res_get_ctx_doit,
2667 .dump = nldev_res_get_ctx_dumpit,
2669 [RDMA_NLDEV_CMD_RES_SRQ_GET] = {
2670 .doit = nldev_res_get_srq_doit,
2671 .dump = nldev_res_get_srq_dumpit,
2673 [RDMA_NLDEV_CMD_SYS_GET] = {
2674 .doit = nldev_sys_get_doit,
2676 [RDMA_NLDEV_CMD_SYS_SET] = {
2677 .doit = nldev_set_sys_set_doit,
2678 .flags = RDMA_NL_ADMIN_PERM,
2680 [RDMA_NLDEV_CMD_STAT_SET] = {
2681 .doit = nldev_stat_set_doit,
2682 .flags = RDMA_NL_ADMIN_PERM,
2684 [RDMA_NLDEV_CMD_STAT_GET] = {
2685 .doit = nldev_stat_get_doit,
2686 .dump = nldev_stat_get_dumpit,
2688 [RDMA_NLDEV_CMD_STAT_DEL] = {
2689 .doit = nldev_stat_del_doit,
2690 .flags = RDMA_NL_ADMIN_PERM,
2692 [RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
2693 .doit = nldev_res_get_qp_raw_doit,
2694 .dump = nldev_res_get_qp_raw_dumpit,
2695 .flags = RDMA_NL_ADMIN_PERM,
2697 [RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
2698 .doit = nldev_res_get_cq_raw_doit,
2699 .dump = nldev_res_get_cq_raw_dumpit,
2700 .flags = RDMA_NL_ADMIN_PERM,
2702 [RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
2703 .doit = nldev_res_get_mr_raw_doit,
2704 .dump = nldev_res_get_mr_raw_dumpit,
2705 .flags = RDMA_NL_ADMIN_PERM,
2707 [RDMA_NLDEV_CMD_RES_SRQ_GET_RAW] = {
2708 .doit = nldev_res_get_srq_raw_doit,
2709 .dump = nldev_res_get_srq_raw_dumpit,
2710 .flags = RDMA_NL_ADMIN_PERM,
2712 [RDMA_NLDEV_CMD_STAT_GET_STATUS] = {
2713 .doit = nldev_stat_get_counter_status_doit,
2715 [RDMA_NLDEV_CMD_NEWDEV] = {
2716 .doit = nldev_newdev,
2717 .flags = RDMA_NL_ADMIN_PERM,
2719 [RDMA_NLDEV_CMD_DELDEV] = {
2720 .doit = nldev_deldev,
2721 .flags = RDMA_NL_ADMIN_PERM,
2725 void __init nldev_init(void)
2727 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2730 void nldev_exit(void)
2732 rdma_nl_unregister(RDMA_NL_NLDEV);
2735 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);