nldev.c 61.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

33
#include <linux/module.h>
34 35
#include <linux/pid.h>
#include <linux/pid_namespace.h>
36
#include <linux/mutex.h>
37
#include <net/netlink.h>
38
#include <rdma/rdma_cm.h>
39 40 41
#include <rdma/rdma_netlink.h>

#include "core_priv.h"
42
#include "cma_priv.h"
43
#include "restrack.h"
44
#include "uverbs.h"
45

46 47 48
typedef int (*res_fill_func_t)(struct sk_buff*, bool,
			       struct rdma_restrack_entry*, uint32_t);

D
Doug Ledford 已提交
49 50 51
/*
 * Sort array elements by the netlink attribute name
 */
52
static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
D
Doug Ledford 已提交
53 54 55
	[RDMA_NLDEV_ATTR_CHARDEV]		= { .type = NLA_U64 },
	[RDMA_NLDEV_ATTR_CHARDEV_ABI]		= { .type = NLA_U64 },
	[RDMA_NLDEV_ATTR_CHARDEV_NAME]		= { .type = NLA_NUL_STRING,
56
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
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 },
D
Doug Ledford 已提交
60 61
	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
62
					.len = IB_DEVICE_NAME_MAX },
D
Doug Ledford 已提交
63 64
	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
65
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
66 67 68 69
	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
70
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
71 72 73 74 75
	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
	[RDMA_NLDEV_ATTR_FW_VERSION]		= { .type = NLA_NUL_STRING,
76
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
77 78
	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
79
					.len = IFNAMSIZ },
D
Doug Ledford 已提交
80 81 82 83 84 85 86 87
	[RDMA_NLDEV_ATTR_LMC]			= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
					.len = IFNAMSIZ },
	[RDMA_NLDEV_ATTR_NODE_GUID]		= { .type = NLA_U64 },
	[RDMA_NLDEV_ATTR_PORT_INDEX]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]	= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_PORT_STATE]		= { .type = NLA_U8 },
88
	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
D
Doug Ledford 已提交
89
	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
90
	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
91 92
	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
D
Doug Ledford 已提交
93 94
	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
95
	[RDMA_NLDEV_ATTR_RES_CTX]		= { .type = NLA_NESTED },
D
Doug Ledford 已提交
96
	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
97
	[RDMA_NLDEV_ATTR_RES_CTX_ENTRY]		= { .type = NLA_NESTED },
D
Doug Ledford 已提交
98 99
	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
			.len = sizeof(struct __kernel_sockaddr_storage) },
100
	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
D
Doug Ledford 已提交
101
	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
102
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
103 104 105 106
	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
107
	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
D
Doug Ledford 已提交
108 109 110
	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
	[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 },
D
Doug Ledford 已提交
113 114 115 116 117 118
	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
119
	[RDMA_NLDEV_ATTR_RES_RAW]		= { .type = NLA_BINARY },
D
Doug Ledford 已提交
120 121 122 123 124 125 126 127 128 129 130
	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
			.len = sizeof(struct __kernel_sockaddr_storage) },
	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
131
					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
D
Doug Ledford 已提交
132 133 134
	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
135 136 137
	[RDMA_NLDEV_ATTR_RES_SRQ]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_RES_SRQN]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_RES_SRQ_ENTRY]		= { .type = NLA_NESTED },
138 139
	[RDMA_NLDEV_ATTR_MIN_RANGE]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_MAX_RANGE]		= { .type = NLA_U32 },
D
Doug Ledford 已提交
140 141
	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
142 143 144
	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
145 146 147 148 149 150 151
	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
D
Doug Ledford 已提交
152
	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
153
	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
D
Doug Ledford 已提交
154 155
	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
156 157
};

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
				      enum rdma_nldev_print_type print_type)
{
	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
		return -EMSGSIZE;
	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
		return -EMSGSIZE;

	return 0;
}

static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
				   enum rdma_nldev_print_type print_type,
				   u32 value)
{
	if (put_driver_name_print_type(msg, name, print_type))
		return -EMSGSIZE;
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
		return -EMSGSIZE;

	return 0;
}

static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
				   enum rdma_nldev_print_type print_type,
				   u64 value)
{
	if (put_driver_name_print_type(msg, name, print_type))
		return -EMSGSIZE;
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
			      RDMA_NLDEV_ATTR_PAD))
		return -EMSGSIZE;

	return 0;
}

E
Erez Alfasi 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207
int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
			      const char *str)
{
	if (put_driver_name_print_type(msg, name,
				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
		return -EMSGSIZE;
	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
		return -EMSGSIZE;

	return 0;
}
EXPORT_SYMBOL(rdma_nl_put_driver_string);

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
{
	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
				       value);
}
EXPORT_SYMBOL(rdma_nl_put_driver_u32);

int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
			       u32 value)
{
	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
				       value);
}
EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);

int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
{
	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
				       value);
}
EXPORT_SYMBOL(rdma_nl_put_driver_u64);

int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
{
	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
				       value);
}
EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);

237
static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
238 239 240
{
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
		return -EMSGSIZE;
241 242
	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
			   dev_name(&device->dev)))
243
		return -EMSGSIZE;
244 245 246 247 248 249 250

	return 0;
}

static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
{
	char fw[IB_FW_VERSION_NAME_MAX];
251
	int ret = 0;
252
	u32 port;
253 254 255 256

	if (fill_nldev_handle(msg, device))
		return -EMSGSIZE;

257 258
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
		return -EMSGSIZE;
259 260 261

	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
262 263
			      device->attrs.device_cap_flags,
			      RDMA_NLDEV_ATTR_PAD))
264 265
		return -EMSGSIZE;

266
	ib_get_device_fw_str(device, fw);
267
	/* Device without FW has strlen(fw) = 0 */
268 269 270
	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
		return -EMSGSIZE;

271
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
272 273
			      be64_to_cpu(device->node_guid),
			      RDMA_NLDEV_ATTR_PAD))
274 275
		return -EMSGSIZE;
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
276 277
			      be64_to_cpu(device->attrs.sys_image_guid),
			      RDMA_NLDEV_ATTR_PAD))
278
		return -EMSGSIZE;
L
Leon Romanovsky 已提交
279 280
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
		return -EMSGSIZE;
281 282
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
		return -EMSGSIZE;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

	/*
	 * Link type is determined on first port and mlx4 device
	 * which can potentially have two different link type for the same
	 * IB device is considered as better to be avoided in the future,
	 */
	port = rdma_start_port(device);
	if (rdma_cap_opa_mad(device, port))
		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
	else if (rdma_protocol_ib(device, port))
		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
	else if (rdma_protocol_iwarp(device, port))
		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
	else if (rdma_protocol_roce(device, port))
		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
	else if (rdma_protocol_usnic(device, port))
		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
				     "usnic");
	return ret;
302 303
}

304
static int fill_port_info(struct sk_buff *msg,
305 306
			  struct ib_device *device, u32 port,
			  const struct net *net)
307
{
308
	struct net_device *netdev = NULL;
309 310
	struct ib_port_attr attr;
	int ret;
311
	u64 cap_flags = 0;
312

313
	if (fill_nldev_handle(msg, device))
314
		return -EMSGSIZE;
315

316 317
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
		return -EMSGSIZE;
318 319 320 321 322

	ret = ib_query_port(device, port, &attr);
	if (ret)
		return ret;

323
	if (rdma_protocol_ib(device, port)) {
324 325 326 327
		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
				sizeof(attr.port_cap_flags2)) > sizeof(u64));
		cap_flags = attr.port_cap_flags |
			((u64)attr.port_cap_flags2 << 32);
328
		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
329
				      cap_flags, RDMA_NLDEV_ATTR_PAD))
330 331 332 333
			return -EMSGSIZE;
		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
			return -EMSGSIZE;
334 335 336 337
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
			return -EMSGSIZE;
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
			return -EMSGSIZE;
338 339
		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
			return -EMSGSIZE;
340
	}
341 342 343 344
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
		return -EMSGSIZE;
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
		return -EMSGSIZE;
345

346
	netdev = ib_device_get_netdev(device, port);
347 348 349 350 351 352 353 354 355 356 357 358 359
	if (netdev && net_eq(dev_net(netdev), net)) {
		ret = nla_put_u32(msg,
				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
		if (ret)
			goto out;
		ret = nla_put_string(msg,
				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
	}

out:
	if (netdev)
		dev_put(netdev);
	return ret;
360 361
}

362 363 364 365 366
static int fill_res_info_entry(struct sk_buff *msg,
			       const char *name, u64 curr)
{
	struct nlattr *entry_attr;

367 368
	entry_attr = nla_nest_start_noflag(msg,
					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
369 370 371 372 373
	if (!entry_attr)
		return -EMSGSIZE;

	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
		goto err;
374 375
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
			      RDMA_NLDEV_ATTR_PAD))
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		goto err;

	nla_nest_end(msg, entry_attr);
	return 0;

err:
	nla_nest_cancel(msg, entry_attr);
	return -EMSGSIZE;
}

static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
{
	static const char * const names[RDMA_RESTRACK_MAX] = {
		[RDMA_RESTRACK_PD] = "pd",
		[RDMA_RESTRACK_CQ] = "cq",
		[RDMA_RESTRACK_QP] = "qp",
392
		[RDMA_RESTRACK_CM_ID] = "cm_id",
393
		[RDMA_RESTRACK_MR] = "mr",
394
		[RDMA_RESTRACK_CTX] = "ctx",
395
		[RDMA_RESTRACK_SRQ] = "srq",
396 397 398 399 400 401 402 403
	};

	struct nlattr *table_attr;
	int ret, i, curr;

	if (fill_nldev_handle(msg, device))
		return -EMSGSIZE;

404
	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
405 406 407 408 409 410
	if (!table_attr)
		return -EMSGSIZE;

	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
		if (!names[i])
			continue;
411
		curr = rdma_restrack_count(device, i);
412 413 414 415 416 417 418 419 420 421 422 423 424
		ret = fill_res_info_entry(msg, names[i], curr);
		if (ret)
			goto err;
	}

	nla_nest_end(msg, table_attr);
	return 0;

err:
	nla_nest_cancel(msg, table_attr);
	return ret;
}

425 426 427
static int fill_res_name_pid(struct sk_buff *msg,
			     struct rdma_restrack_entry *res)
{
428 429
	int err = 0;

430 431 432 433 434
	/*
	 * For user resources, user is should read /proc/PID/comm to get the
	 * name of the task file.
	 */
	if (rdma_is_kernel_res(res)) {
435 436
		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
				     res->kern_name);
437
	} else {
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		pid_t pid;

		pid = task_pid_vnr(res->task);
		/*
		 * Task is dead and in zombie state.
		 * There is no need to print PID anymore.
		 */
		if (pid)
			/*
			 * This part is racy, task can be killed and PID will
			 * be zero right here but it is ok, next query won't
			 * return PID. We don't promise real-time reflection
			 * of SW objects.
			 */
			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
453
	}
454 455

	return err ? -EMSGSIZE : 0;
456 457
}

458 459 460 461
static int fill_res_qp_entry_query(struct sk_buff *msg,
				   struct rdma_restrack_entry *res,
				   struct ib_device *dev,
				   struct ib_qp *qp)
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
{
	struct ib_qp_init_attr qp_init_attr;
	struct ib_qp_attr qp_attr;
	int ret;

	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
	if (ret)
		return ret;

	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
				qp_attr.dest_qp_num))
			goto err;
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
				qp_attr.rq_psn))
			goto err;
	}

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
		goto err;

	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
			       qp_attr.path_mig_state))
			goto err;
	}
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
		goto err;
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
		goto err;

494 495
	if (dev->ops.fill_res_qp_entry)
		return dev->ops.fill_res_qp_entry(msg, qp);
496 497
	return 0;

498
err:	return -EMSGSIZE;
499 500
}

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
			     struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_qp *qp = container_of(res, struct ib_qp, res);
	struct ib_device *dev = qp->device;
	int ret;

	if (port && port != qp->port)
		return -EAGAIN;

	/* In create_qp() port is not set yet */
	if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
		return -EINVAL;

	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
	if (ret)
		return -EMSGSIZE;

	if (!rdma_is_kernel_res(res) &&
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
		return -EMSGSIZE;

	ret = fill_res_name_pid(msg, res);
	if (ret)
		return -EMSGSIZE;

	return fill_res_qp_entry_query(msg, res, dev, qp);
}

static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
				 struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_qp *qp = container_of(res, struct ib_qp, res);
	struct ib_device *dev = qp->device;

	if (port && port != qp->port)
		return -EAGAIN;
	if (!dev->ops.fill_res_qp_entry_raw)
		return -EINVAL;
	return dev->ops.fill_res_qp_entry_raw(msg, qp);
}

543
static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
544 545 546 547
				struct rdma_restrack_entry *res, uint32_t port)
{
	struct rdma_id_private *id_priv =
				container_of(res, struct rdma_id_private, res);
548
	struct ib_device *dev = id_priv->id.device;
549 550 551 552 553 554 555 556 557 558 559
	struct rdma_cm_id *cm_id = &id_priv->id;

	if (port && port != cm_id->port_num)
		return 0;

	if (cm_id->port_num &&
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
		goto err;

	if (id_priv->qp_num) {
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
560
			goto err;
561
		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
562 563 564
			goto err;
	}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
		goto err;

	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
		goto err;

	if (cm_id->route.addr.src_addr.ss_family &&
	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
		    sizeof(cm_id->route.addr.src_addr),
		    &cm_id->route.addr.src_addr))
		goto err;
	if (cm_id->route.addr.dst_addr.ss_family &&
	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
		    sizeof(cm_id->route.addr.dst_addr),
		    &cm_id->route.addr.dst_addr))
		goto err;

582 583 584
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
		goto err;

585 586 587
	if (fill_res_name_pid(msg, res))
		goto err;

588 589
	if (dev->ops.fill_res_cm_id_entry)
		return dev->ops.fill_res_cm_id_entry(msg, cm_id);
590 591
	return 0;

592
err: return -EMSGSIZE;
593 594
}

595
static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
596 597 598
			     struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_cq *cq = container_of(res, struct ib_cq, res);
599
	struct ib_device *dev = cq->device;
600 601

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
602
		return -EMSGSIZE;
603
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
604
			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
605
		return -EMSGSIZE;
606 607 608 609

	/* Poll context is only valid for kernel CQs */
	if (rdma_is_kernel_res(res) &&
	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
610
		return -EMSGSIZE;
611

612
	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
613
		return -EMSGSIZE;
614

615
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
616
		return -EMSGSIZE;
617 618
	if (!rdma_is_kernel_res(res) &&
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
619
			cq->uobject->uevent.uobject.context->res.id))
620
		return -EMSGSIZE;
621

622
	if (fill_res_name_pid(msg, res))
623
		return -EMSGSIZE;
624

625 626 627
	return (dev->ops.fill_res_cq_entry) ?
		dev->ops.fill_res_cq_entry(msg, cq) : 0;
}
628

629 630 631 632 633 634 635 636 637
static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
				 struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_cq *cq = container_of(res, struct ib_cq, res);
	struct ib_device *dev = cq->device;

	if (!dev->ops.fill_res_cq_entry_raw)
		return -EINVAL;
	return dev->ops.fill_res_cq_entry_raw(msg, cq);
638 639
}

640
static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
641 642 643
			     struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_mr *mr = container_of(res, struct ib_mr, res);
644
	struct ib_device *dev = mr->pd->device;
645

646
	if (has_cap_net_admin) {
647
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
648
			return -EMSGSIZE;
649
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
650
			return -EMSGSIZE;
651 652
	}

653 654
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
			      RDMA_NLDEV_ATTR_PAD))
655
		return -EMSGSIZE;
656

657
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
658
		return -EMSGSIZE;
659

660 661
	if (!rdma_is_kernel_res(res) &&
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
662
		return -EMSGSIZE;
663

664
	if (fill_res_name_pid(msg, res))
665
		return -EMSGSIZE;
666

667 668 669 670
	return (dev->ops.fill_res_mr_entry) ?
		       dev->ops.fill_res_mr_entry(msg, mr) :
		       0;
}
671

672 673 674 675 676 677 678 679 680
static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
				 struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_mr *mr = container_of(res, struct ib_mr, res);
	struct ib_device *dev = mr->pd->device;

	if (!dev->ops.fill_res_mr_entry_raw)
		return -EINVAL;
	return dev->ops.fill_res_mr_entry_raw(msg, mr);
681 682
}

683
static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
684 685 686 687
			     struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_pd *pd = container_of(res, struct ib_pd, res);

688
	if (has_cap_net_admin) {
689 690 691 692 693 694 695 696 697
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
				pd->local_dma_lkey))
			goto err;
		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
				pd->unsafe_global_rkey))
			goto err;
	}
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
698
			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
699 700
		goto err;

701 702 703
	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
		goto err;

704 705 706 707 708
	if (!rdma_is_kernel_res(res) &&
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
			pd->uobject->context->res.id))
		goto err;

709
	return fill_res_name_pid(msg, res);
710

711
err:	return -EMSGSIZE;
712 713
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727
static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
			      struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);

	if (rdma_is_kernel_res(res))
		return 0;

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
		return -EMSGSIZE;

	return fill_res_name_pid(msg, res);
}

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
				   uint32_t max_range)
{
	struct nlattr *entry_attr;

	if (!min_range)
		return 0;

	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
	if (!entry_attr)
		return -EMSGSIZE;

	if (min_range == max_range) {
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
			goto err;
	} else {
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
			goto err;
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
			goto err;
	}
	nla_nest_end(msg, entry_attr);
	return 0;

err:
	nla_nest_cancel(msg, entry_attr);
	return -EMSGSIZE;
}

static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
{
	uint32_t min_range = 0, prev = 0;
	struct rdma_restrack_entry *res;
	struct rdma_restrack_root *rt;
	struct nlattr *table_attr;
	struct ib_qp *qp = NULL;
	unsigned long id = 0;

	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
	if (!table_attr)
		return -EMSGSIZE;

	rt = &srq->device->res[RDMA_RESTRACK_QP];
	xa_lock(&rt->xa);
	xa_for_each(&rt->xa, id, res) {
		if (!rdma_restrack_get(res))
			continue;

		qp = container_of(res, struct ib_qp, res);
		if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
			rdma_restrack_put(res);
			continue;
		}

		if (qp->qp_num < prev)
			/* qp_num should be ascending */
			goto err_loop;

		if (min_range == 0) {
			min_range = qp->qp_num;
		} else if (qp->qp_num > (prev + 1)) {
			if (fill_res_range_qp_entry(msg, min_range, prev))
				goto err_loop;

			min_range = qp->qp_num;
		}
		prev = qp->qp_num;
		rdma_restrack_put(res);
	}

	xa_unlock(&rt->xa);

	if (fill_res_range_qp_entry(msg, min_range, prev))
		goto err;

	nla_nest_end(msg, table_attr);
	return 0;

err_loop:
	rdma_restrack_put(res);
	xa_unlock(&rt->xa);
err:
	nla_nest_cancel(msg, table_attr);
	return -EMSGSIZE;
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
			      struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_srq *srq = container_of(res, struct ib_srq, res);

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
		goto err;

	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
		goto err;

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
		goto err;

	if (ib_srq_has_cq(srq->srq_type)) {
		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
				srq->ext.cq->res.id))
			goto err;
	}

834 835 836
	if (fill_res_srq_qps(msg, srq))
		goto err;

837 838 839 840 841 842
	return fill_res_name_pid(msg, res);

err:
	return -EMSGSIZE;
}

843 844 845 846 847 848 849 850
static int fill_stat_counter_mode(struct sk_buff *msg,
				  struct rdma_counter *counter)
{
	struct rdma_counter_mode *m = &counter->mode;

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
		return -EMSGSIZE;

851
	if (m->mode == RDMA_COUNTER_MODE_AUTO) {
852 853 854 855
		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
			return -EMSGSIZE;

856 857 858 859 860
		if ((m->mask & RDMA_COUNTER_MASK_PID) &&
		    fill_res_name_pid(msg, &counter->res))
			return -EMSGSIZE;
	}

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	return 0;
}

static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
{
	struct nlattr *entry_attr;

	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
	if (!entry_attr)
		return -EMSGSIZE;

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
		goto err;

	nla_nest_end(msg, entry_attr);
	return 0;

err:
	nla_nest_cancel(msg, entry_attr);
	return -EMSGSIZE;
}

static int fill_stat_counter_qps(struct sk_buff *msg,
				 struct rdma_counter *counter)
{
	struct rdma_restrack_entry *res;
	struct rdma_restrack_root *rt;
	struct nlattr *table_attr;
	struct ib_qp *qp = NULL;
	unsigned long id = 0;
	int ret = 0;

	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);

	rt = &counter->device->res[RDMA_RESTRACK_QP];
	xa_lock(&rt->xa);
	xa_for_each(&rt->xa, id, res) {
		qp = container_of(res, struct ib_qp, res);
		if (!qp->counter || (qp->counter->id != counter->id))
			continue;

		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
		if (ret)
			goto err;
	}

	xa_unlock(&rt->xa);
	nla_nest_end(msg, table_attr);
	return 0;

err:
	xa_unlock(&rt->xa);
	nla_nest_cancel(msg, table_attr);
	return ret;
}

E
Erez Alfasi 已提交
917 918
int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
				 u64 value)
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
	struct nlattr *entry_attr;

	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
	if (!entry_attr)
		return -EMSGSIZE;

	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
			   name))
		goto err;
	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
			      value, RDMA_NLDEV_ATTR_PAD))
		goto err;

	nla_nest_end(msg, entry_attr);
	return 0;

err:
	nla_nest_cancel(msg, entry_attr);
	return -EMSGSIZE;
}
E
Erez Alfasi 已提交
940 941 942 943 944 945 946 947 948 949 950
EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);

static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
			      struct rdma_restrack_entry *res, uint32_t port)
{
	struct ib_mr *mr = container_of(res, struct ib_mr, res);
	struct ib_device *dev = mr->pd->device;

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
		goto err;

951 952
	if (dev->ops.fill_stat_mr_entry)
		return dev->ops.fill_stat_mr_entry(msg, mr);
E
Erez Alfasi 已提交
953 954 955 956 957
	return 0;

err:
	return -EMSGSIZE;
}
958 959 960 961 962 963 964 965 966 967 968 969 970

static int fill_stat_counter_hwcounters(struct sk_buff *msg,
					struct rdma_counter *counter)
{
	struct rdma_hw_stats *st = counter->stats;
	struct nlattr *table_attr;
	int i;

	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
	if (!table_attr)
		return -EMSGSIZE;

	for (i = 0; i < st->num_counters; i++)
E
Erez Alfasi 已提交
971
		if (rdma_nl_stat_hwcounter_entry(msg, st->names[i], st->value[i]))
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
			goto err;

	nla_nest_end(msg, table_attr);
	return 0;

err:
	nla_nest_cancel(msg, table_attr);
	return -EMSGSIZE;
}

static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
				  struct rdma_restrack_entry *res,
				  uint32_t port)
{
	struct rdma_counter *counter =
		container_of(res, struct rdma_counter, res);

	if (port && port != counter->port)
990
		return -EAGAIN;
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

	/* Dump it even query failed */
	rdma_counter_query_stats(counter);

	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
	    fill_stat_counter_mode(msg, counter) ||
	    fill_stat_counter_qps(msg, counter) ||
	    fill_stat_counter_hwcounters(msg, counter))
		return -EMSGSIZE;

	return 0;
}

1005 1006 1007 1008 1009 1010 1011 1012 1013
static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	struct sk_buff *msg;
	u32 index;
	int err;

1014 1015
	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1016 1017 1018 1019 1020
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);

1021
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1022 1023 1024 1025
	if (!device)
		return -EINVAL;

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1026 1027 1028 1029
	if (!msg) {
		err = -ENOMEM;
		goto err;
	}
1030 1031 1032 1033 1034 1035

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
			0, 0);

	err = fill_dev_info(msg, device);
1036 1037
	if (err)
		goto err_free;
1038 1039 1040

	nlmsg_end(msg, nlh);

1041
	ib_device_put(device);
1042
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1043 1044 1045 1046

err_free:
	nlmsg_free(msg);
err:
1047
	ib_device_put(device);
1048
	return err;
1049 1050
}

1051 1052 1053 1054 1055 1056 1057 1058
static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	u32 index;
	int err;

1059 1060
	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1061 1062 1063 1064
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1065
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1066 1067 1068 1069 1070 1071
	if (!device)
		return -EINVAL;

	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
		char name[IB_DEVICE_NAME_MAX] = {};

1072
		nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1073
			    IB_DEVICE_NAME_MAX);
1074 1075 1076 1077
		if (strlen(name) == 0) {
			err = -EINVAL;
			goto done;
		}
1078
		err = ib_device_rename(device, name);
1079
		goto done;
1080 1081
	}

1082 1083 1084 1085 1086 1087 1088 1089
	if (tb[RDMA_NLDEV_NET_NS_FD]) {
		u32 ns_fd;

		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
		err = ib_device_set_netns_put(skb, device, ns_fd);
		goto put_done;
	}

1090 1091 1092 1093 1094 1095 1096 1097
	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
		u8 use_dim;

		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
		err = ib_device_set_dim(device,  use_dim);
		goto done;
	}

1098
done:
1099
	ib_device_put(device);
1100
put_done:
1101 1102 1103
	return err;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
static int _nldev_get_dumpit(struct ib_device *device,
			     struct sk_buff *skb,
			     struct netlink_callback *cb,
			     unsigned int idx)
{
	int start = cb->args[0];
	struct nlmsghdr *nlh;

	if (idx < start)
		return 0;

	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
			0, NLM_F_MULTI);

	if (fill_dev_info(skb, device)) {
		nlmsg_cancel(skb, nlh);
		goto out;
	}

	nlmsg_end(skb, nlh);

	idx++;

out:	cb->args[0] = idx;
	return skb->len;
}

static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
{
	/*
	 * There is no need to take lock, because
1136
	 * we are relying on ib_core's locking.
1137 1138 1139 1140
	 */
	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
}

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	struct sk_buff *msg;
	u32 index;
	u32 port;
	int err;

1151 1152
	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1153 1154 1155
	if (err ||
	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1156 1157 1158
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1159
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1160 1161 1162 1163
	if (!device)
		return -EINVAL;

	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1164 1165 1166 1167
	if (!rdma_is_port_valid(device, port)) {
		err = -EINVAL;
		goto err;
	}
1168 1169

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1170 1171 1172 1173
	if (!msg) {
		err = -ENOMEM;
		goto err;
	}
1174 1175 1176 1177 1178

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
			0, 0);

1179
	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1180 1181
	if (err)
		goto err_free;
1182 1183

	nlmsg_end(msg, nlh);
1184
	ib_device_put(device);
1185

1186
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1187 1188 1189 1190

err_free:
	nlmsg_free(msg);
err:
1191
	ib_device_put(device);
1192
	return err;
1193 1194
}

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
static int nldev_port_get_dumpit(struct sk_buff *skb,
				 struct netlink_callback *cb)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	int start = cb->args[0];
	struct nlmsghdr *nlh;
	u32 idx = 0;
	u32 ifindex;
	int err;
1205
	unsigned int p;
1206

1207 1208
	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, NULL);
1209 1210 1211 1212
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1213
	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1214 1215 1216
	if (!device)
		return -EINVAL;

1217
	rdma_for_each_port (device, p) {
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
		/*
		 * The dumpit function returns all information from specific
		 * index. This specific index is taken from the netlink
		 * messages request sent by user and it is available
		 * in cb->args[0].
		 *
		 * Usually, the user doesn't fill this field and it causes
		 * to return everything.
		 *
		 */
		if (idx < start) {
			idx++;
			continue;
		}

		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
				cb->nlh->nlmsg_seq,
				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
						 RDMA_NLDEV_CMD_PORT_GET),
				0, NLM_F_MULTI);

1239
		if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
1240 1241 1242 1243 1244 1245 1246
			nlmsg_cancel(skb, nlh);
			goto out;
		}
		idx++;
		nlmsg_end(skb, nlh);
	}

1247
out:
1248
	ib_device_put(device);
1249
	cb->args[0] = idx;
1250 1251 1252
	return skb->len;
}

1253 1254 1255 1256 1257 1258 1259 1260 1261
static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			      struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	struct sk_buff *msg;
	u32 index;
	int ret;

1262 1263
	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1264 1265 1266 1267
	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1268
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1269 1270 1271 1272
	if (!device)
		return -EINVAL;

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1273 1274
	if (!msg) {
		ret = -ENOMEM;
1275
		goto err;
1276
	}
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
			0, 0);

	ret = fill_res_info(msg, device);
	if (ret)
		goto err_free;

	nlmsg_end(msg, nlh);
1287
	ib_device_put(device);
1288
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1289 1290 1291 1292

err_free:
	nlmsg_free(msg);
err:
1293
	ib_device_put(device);
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	return ret;
}

static int _nldev_res_get_dumpit(struct ib_device *device,
				 struct sk_buff *skb,
				 struct netlink_callback *cb,
				 unsigned int idx)
{
	int start = cb->args[0];
	struct nlmsghdr *nlh;

	if (idx < start)
		return 0;

	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
			0, NLM_F_MULTI);

	if (fill_res_info(skb, device)) {
		nlmsg_cancel(skb, nlh);
		goto out;
	}
	nlmsg_end(skb, nlh);

	idx++;

out:
	cb->args[0] = idx;
	return skb->len;
}

static int nldev_res_get_dumpit(struct sk_buff *skb,
				struct netlink_callback *cb)
{
	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
}

1331 1332
struct nldev_fill_res_entry {
	enum rdma_nldev_attr nldev_attr;
1333 1334 1335 1336 1337 1338 1339
	u8 flags;
	u32 entry;
	u32 id;
};

enum nldev_res_flags {
	NLDEV_PER_DEV = 1 << 0,
1340 1341 1342 1343 1344
};

static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
	[RDMA_RESTRACK_QP] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1345
		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1346
		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1347
	},
1348 1349
	[RDMA_RESTRACK_CM_ID] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1350
		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1351
		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1352
	},
1353 1354
	[RDMA_RESTRACK_CQ] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1355 1356
		.flags = NLDEV_PER_DEV,
		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1357
		.id = RDMA_NLDEV_ATTR_RES_CQN,
1358
	},
1359 1360
	[RDMA_RESTRACK_MR] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1361 1362
		.flags = NLDEV_PER_DEV,
		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1363
		.id = RDMA_NLDEV_ATTR_RES_MRN,
1364
	},
1365 1366
	[RDMA_RESTRACK_PD] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1367 1368
		.flags = NLDEV_PER_DEV,
		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1369
		.id = RDMA_NLDEV_ATTR_RES_PDN,
1370
	},
1371 1372 1373 1374 1375
	[RDMA_RESTRACK_COUNTER] = {
		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
	},
1376 1377 1378 1379 1380 1381
	[RDMA_RESTRACK_CTX] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
		.flags = NLDEV_PER_DEV,
		.entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
		.id = RDMA_NLDEV_ATTR_RES_CTXN,
	},
1382 1383 1384 1385 1386 1387 1388
	[RDMA_RESTRACK_SRQ] = {
		.nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
		.flags = NLDEV_PER_DEV,
		.entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
		.id = RDMA_NLDEV_ATTR_RES_SRQN,
	},

1389 1390
};

1391 1392
static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack,
1393 1394
			       enum rdma_restrack_type res_type,
			       res_fill_func_t fill_func)
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
{
	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct rdma_restrack_entry *res;
	struct ib_device *device;
	u32 index, id, port = 0;
	bool has_cap_net_admin;
	struct sk_buff *msg;
	int ret;

1405 1406
	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1407 1408 1409 1410
	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1411
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	if (!device)
		return -EINVAL;

	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
		if (!rdma_is_port_valid(device, port)) {
			ret = -EINVAL;
			goto err;
		}
	}

	if ((port && fe->flags & NLDEV_PER_DEV) ||
	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
		ret = -EINVAL;
		goto err;
	}

	id = nla_get_u32(tb[fe->id]);
	res = rdma_restrack_get_byid(device, res_type, id);
	if (IS_ERR(res)) {
		ret = PTR_ERR(res);
		goto err;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		ret = -ENOMEM;
1439
		goto err_get;
1440 1441 1442
	}

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1443 1444
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1445 1446 1447 1448 1449 1450 1451 1452
			0, 0);

	if (fill_nldev_handle(msg, device)) {
		ret = -EMSGSIZE;
		goto err_free;
	}

	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1453 1454

	ret = fill_func(msg, has_cap_net_admin, res, port);
1455 1456 1457
	if (ret)
		goto err_free;

1458
	rdma_restrack_put(res);
1459 1460
	nlmsg_end(msg, nlh);
	ib_device_put(device);
1461
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

err_free:
	nlmsg_free(msg);
err_get:
	rdma_restrack_put(res);
err:
	ib_device_put(device);
	return ret;
}

1472 1473
static int res_get_common_dumpit(struct sk_buff *skb,
				 struct netlink_callback *cb,
1474 1475
				 enum rdma_restrack_type res_type,
				 res_fill_func_t fill_func)
1476
{
1477
	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1478 1479
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct rdma_restrack_entry *res;
1480
	struct rdma_restrack_root *rt;
1481 1482
	int err, ret = 0, idx = 0;
	struct nlattr *table_attr;
1483
	struct nlattr *entry_attr;
1484 1485
	struct ib_device *device;
	int start = cb->args[0];
1486
	bool has_cap_net_admin;
1487
	struct nlmsghdr *nlh;
1488
	unsigned long id;
1489
	u32 index, port = 0;
1490
	bool filled = false;
1491

1492 1493
	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, NULL);
1494
	/*
1495
	 * Right now, we are expecting the device index to get res information,
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
	 * but it is possible to extend this code to return all devices in
	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
	 * if it doesn't exist, we will iterate over all devices.
	 *
	 * But it is not needed for now.
	 */
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1506
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	if (!device)
		return -EINVAL;

	/*
	 * If no PORT_INDEX is supplied, we will return all QPs from that device
	 */
	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
		if (!rdma_is_port_valid(device, port)) {
			ret = -EINVAL;
			goto err_index;
		}
	}

	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1522 1523
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1524 1525 1526 1527 1528 1529 1530
			0, NLM_F_MULTI);

	if (fill_nldev_handle(skb, device)) {
		ret = -EMSGSIZE;
		goto err;
	}

1531
	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1532 1533 1534 1535 1536
	if (!table_attr) {
		ret = -EMSGSIZE;
		goto err;
	}

1537 1538
	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);

1539 1540
	rt = &device->res[res_type];
	xa_lock(&rt->xa);
1541 1542 1543 1544 1545
	/*
	 * FIXME: if the skip ahead is something common this loop should
	 * use xas_for_each & xas_pause to optimize, we can have a lot of
	 * objects.
	 */
1546
	xa_for_each(&rt->xa, id, res) {
1547
		if (idx < start || !rdma_restrack_get(res))
1548 1549
			goto next;

1550 1551
		xa_unlock(&rt->xa);

1552
		filled = true;
1553

1554
		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1555 1556 1557
		if (!entry_attr) {
			ret = -EMSGSIZE;
			rdma_restrack_put(res);
1558
			goto msg_full;
1559 1560
		}

1561 1562
		ret = fill_func(skb, has_cap_net_admin, res, port);

1563 1564
		rdma_restrack_put(res);

1565
		if (ret) {
1566
			nla_nest_cancel(skb, entry_attr);
1567 1568 1569 1570
			if (ret == -EMSGSIZE)
				goto msg_full;
			if (ret == -EAGAIN)
				goto again;
1571
			goto res_err;
1572
		}
1573
		nla_nest_end(skb, entry_attr);
1574
again:		xa_lock(&rt->xa);
1575 1576
next:		idx++;
	}
1577
	xa_unlock(&rt->xa);
1578

1579
msg_full:
1580 1581 1582 1583 1584
	nla_nest_end(skb, table_attr);
	nlmsg_end(skb, nlh);
	cb->args[0] = idx;

	/*
1585
	 * No more entries to fill, cancel the message and
1586 1587
	 * return 0 to mark end of dumpit.
	 */
1588
	if (!filled)
1589 1590
		goto err;

1591
	ib_device_put(device);
1592 1593 1594 1595 1596 1597 1598 1599 1600
	return skb->len;

res_err:
	nla_nest_cancel(skb, table_attr);

err:
	nlmsg_cancel(skb, nlh);

err_index:
1601
	ib_device_put(device);
1602 1603 1604
	return ret;
}

1605 1606
#define RES_GET_FUNCS(name, type)                                              \
	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1607
						 struct netlink_callback *cb)  \
1608 1609 1610 1611 1612 1613
	{                                                                      \
		return res_get_common_dumpit(skb, cb, type,                    \
					     fill_res_##name##_entry);         \
	}                                                                      \
	static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
					       struct nlmsghdr *nlh,           \
1614
					       struct netlink_ext_ack *extack) \
1615 1616 1617
	{                                                                      \
		return res_get_common_doit(skb, nlh, extack, type,             \
					   fill_res_##name##_entry);           \
1618
	}
1619

1620
RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1621
RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1622 1623
RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1624
RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1625 1626
RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1627
RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1628
RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1629
RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1630
RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
static LIST_HEAD(link_ops);
static DECLARE_RWSEM(link_ops_rwsem);

static const struct rdma_link_ops *link_ops_get(const char *type)
{
	const struct rdma_link_ops *ops;

	list_for_each_entry(ops, &link_ops, list) {
		if (!strcmp(ops->type, type))
			goto out;
	}
	ops = NULL;
out:
	return ops;
}

void rdma_link_register(struct rdma_link_ops *ops)
{
	down_write(&link_ops_rwsem);
D
Dan Carpenter 已提交
1651
	if (WARN_ON_ONCE(link_ops_get(ops->type)))
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
		goto out;
	list_add(&ops->list, &link_ops);
out:
	up_write(&link_ops_rwsem);
}
EXPORT_SYMBOL(rdma_link_register);

void rdma_link_unregister(struct rdma_link_ops *ops)
{
	down_write(&link_ops_rwsem);
	list_del(&ops->list);
	up_write(&link_ops_rwsem);
}
EXPORT_SYMBOL(rdma_link_unregister);

static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	char ibdev_name[IB_DEVICE_NAME_MAX];
	const struct rdma_link_ops *ops;
	char ndev_name[IFNAMSIZ];
	struct net_device *ndev;
	char type[IFNAMSIZ];
	int err;

1678 1679
	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1680 1681 1682 1683
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
		return -EINVAL;

1684
	nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1685
		    sizeof(ibdev_name));
1686
	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1687 1688
		return -EINVAL;

1689 1690
	nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
	nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1691 1692
		    sizeof(ndev_name));

1693
	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	if (!ndev)
		return -ENODEV;

	down_read(&link_ops_rwsem);
	ops = link_ops_get(type);
#ifdef CONFIG_MODULES
	if (!ops) {
		up_read(&link_ops_rwsem);
		request_module("rdma-link-%s", type);
		down_read(&link_ops_rwsem);
		ops = link_ops_get(type);
	}
#endif
	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
	up_read(&link_ops_rwsem);
	dev_put(ndev);

	return err;
}

static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	u32 index;
	int err;

1722 1723
	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
				     nldev_policy, extack);
1724 1725 1726 1727
	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1728
	device = ib_device_get_by_index(sock_net(skb->sk), index);
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
	if (!device)
		return -EINVAL;

	if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) {
		ib_device_put(device);
		return -EINVAL;
	}

	ib_unregister_device_and_put(device);
	return 0;
}

1741 1742 1743 1744
static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1745
	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	struct ib_client_nl_info data = {};
	struct ib_device *ibdev = NULL;
	struct sk_buff *msg;
	u32 index;
	int err;

	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
			  extack);
	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
		return -EINVAL;

1757
	nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1758
		    sizeof(client_name));
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812

	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
		if (!ibdev)
			return -EINVAL;

		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
			if (!rdma_is_port_valid(ibdev, data.port)) {
				err = -EINVAL;
				goto out_put;
			}
		} else {
			data.port = -1;
		}
	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
		return -EINVAL;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		err = -ENOMEM;
		goto out_put;
	}
	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_GET_CHARDEV),
			0, 0);

	data.nl_msg = msg;
	err = ib_get_client_nl_info(ibdev, client_name, &data);
	if (err)
		goto out_nlmsg;

	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
				huge_encode_dev(data.cdev->devt),
				RDMA_NLDEV_ATTR_PAD);
	if (err)
		goto out_data;
	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
				RDMA_NLDEV_ATTR_PAD);
	if (err)
		goto out_data;
	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
			   dev_name(data.cdev))) {
		err = -EMSGSIZE;
		goto out_data;
	}

	nlmsg_end(msg, nlh);
	put_device(data.cdev);
	if (ibdev)
		ib_device_put(ibdev);
1813
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824

out_data:
	put_device(data.cdev);
out_nlmsg:
	nlmsg_free(msg);
out_put:
	if (ibdev)
		ib_device_put(ibdev);
	return err;
}

1825 1826
static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			      struct netlink_ext_ack *extack)
1827 1828
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1829
	struct sk_buff *msg;
1830 1831
	int err;

1832 1833
	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, extack);
1834 1835 1836
	if (err)
		return err;

1837 1838 1839 1840 1841
	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg)
		return -ENOMEM;

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1842 1843 1844 1845
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_SYS_GET),
			0, 0);

1846
	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1847 1848
			 (u8)ib_devices_shared_netns);
	if (err) {
1849
		nlmsg_free(msg);
1850 1851
		return err;
	}
1852
	nlmsg_end(msg, nlh);
1853
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1854 1855
}

1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
				  struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	u8 enable;
	int err;

	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, extack);
	if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
		return -EINVAL;

	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
	/* Only 0 and 1 are supported */
	if (enable > 1)
		return -EINVAL;

	err = rdma_compatdev_set(enable);
	return err;
}

1877 1878 1879
static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
{
1880
	u32 index, port, mode, mask = 0, qpn, cntn = 0;
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	struct sk_buff *msg;
	int ret;

	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, extack);
	/* Currently only counter for QP is supported */
	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX] || !tb[RDMA_NLDEV_ATTR_STAT_MODE])
		return -EINVAL;

	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	device = ib_device_get_by_index(sock_net(skb->sk), index);
	if (!device)
		return -EINVAL;

	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
	if (!rdma_is_port_valid(device, port)) {
		ret = -EINVAL;
		goto err;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		ret = -ENOMEM;
		goto err;
	}
	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_STAT_SET),
			0, 0);

	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
1919 1920 1921 1922
	if (mode == RDMA_COUNTER_MODE_AUTO) {
		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
			mask = nla_get_u32(
				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
1923
		ret = rdma_counter_set_auto_mode(device, port, mask, extack);
1924 1925 1926
		if (ret)
			goto err_msg;
	} else {
1927 1928
		if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
			goto err_msg;
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
		qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
		if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
			cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
			ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
		} else {
			ret = rdma_counter_bind_qpn_alloc(device, port,
							  qpn, &cntn);
		}
		if (ret)
			goto err_msg;

		if (fill_nldev_handle(msg, device) ||
		    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
			ret = -EMSGSIZE;
			goto err_fill;
		}
1947 1948
	}

1949 1950
	nlmsg_end(msg, nlh);
	ib_device_put(device);
1951
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969

err_fill:
	rdma_counter_unbind_qpn(device, port, qpn, cntn);
err_msg:
	nlmsg_free(msg);
err:
	ib_device_put(device);
	return ret;
}

static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	struct ib_device *device;
	struct sk_buff *msg;
	u32 index, port, qpn, cntn;
	int ret;
1970

1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, extack);
	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
		return -EINVAL;

	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	device = ib_device_get_by_index(sock_net(skb->sk), index);
	if (!device)
		return -EINVAL;

	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
	if (!rdma_is_port_valid(device, port)) {
		ret = -EINVAL;
		goto err;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		ret = -ENOMEM;
		goto err;
	}
	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_STAT_SET),
			0, 0);

	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
	if (fill_nldev_handle(msg, device) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2009
		ret = -EMSGSIZE;
2010
		goto err_fill;
2011 2012
	}

2013 2014 2015 2016
	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
	if (ret)
		goto err_fill;

2017 2018
	nlmsg_end(msg, nlh);
	ib_device_put(device);
2019
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2020

2021
err_fill:
2022 2023 2024 2025 2026 2027
	nlmsg_free(msg);
err:
	ib_device_put(device);
	return ret;
}

2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
static int stat_get_doit_default_counter(struct sk_buff *skb,
					 struct nlmsghdr *nlh,
					 struct netlink_ext_ack *extack,
					 struct nlattr *tb[])
{
	struct rdma_hw_stats *stats;
	struct nlattr *table_attr;
	struct ib_device *device;
	int ret, num_cnts, i;
	struct sk_buff *msg;
	u32 index, port;
	u64 v;

	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	device = ib_device_get_by_index(sock_net(skb->sk), index);
	if (!device)
		return -EINVAL;

	if (!device->ops.alloc_hw_stats || !device->ops.get_hw_stats) {
		ret = -EINVAL;
		goto err;
	}

	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
	if (!rdma_is_port_valid(device, port)) {
		ret = -EINVAL;
		goto err;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		ret = -ENOMEM;
		goto err;
	}

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_STAT_GET),
			0, 0);

	if (fill_nldev_handle(msg, device) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
		ret = -EMSGSIZE;
		goto err_msg;
	}

	stats = device->port_data ? device->port_data[port].hw_stats : NULL;
	if (stats == NULL) {
		ret = -EINVAL;
		goto err_msg;
	}
	mutex_lock(&stats->lock);

	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
	if (num_cnts < 0) {
		ret = -EINVAL;
		goto err_stats;
	}

	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
	if (!table_attr) {
		ret = -EMSGSIZE;
		goto err_stats;
	}
	for (i = 0; i < num_cnts; i++) {
		v = stats->value[i] +
			rdma_counter_get_hwstat_value(device, port, i);
E
Erez Alfasi 已提交
2098
		if (rdma_nl_stat_hwcounter_entry(msg, stats->names[i], v)) {
2099 2100 2101 2102 2103 2104 2105 2106 2107
			ret = -EMSGSIZE;
			goto err_table;
		}
	}
	nla_nest_end(msg, table_attr);

	mutex_unlock(&stats->lock);
	nlmsg_end(msg, nlh);
	ib_device_put(device);
2108
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120

err_table:
	nla_nest_cancel(msg, table_attr);
err_stats:
	mutex_unlock(&stats->lock);
err_msg:
	nlmsg_free(msg);
err:
	ib_device_put(device);
	return ret;
}

2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct netlink_ext_ack *extack, struct nlattr *tb[])

{
	static enum rdma_nl_counter_mode mode;
	static enum rdma_nl_counter_mask mask;
	struct ib_device *device;
	struct sk_buff *msg;
	u32 index, port;
	int ret;

	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
		return nldev_res_get_counter_doit(skb, nlh, extack);

	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
		return -EINVAL;

	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	device = ib_device_get_by_index(sock_net(skb->sk), index);
	if (!device)
		return -EINVAL;

	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
	if (!rdma_is_port_valid(device, port)) {
		ret = -EINVAL;
		goto err;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg) {
		ret = -ENOMEM;
		goto err;
	}

	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
					 RDMA_NLDEV_CMD_STAT_GET),
			0, 0);

	ret = rdma_counter_get_mode(device, port, &mode, &mask);
	if (ret)
		goto err_msg;

	if (fill_nldev_handle(msg, device) ||
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2167 2168
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
		ret = -EMSGSIZE;
2169
		goto err_msg;
2170
	}
2171 2172

	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2173 2174
	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
		ret = -EMSGSIZE;
2175
		goto err_msg;
2176
	}
2177 2178 2179

	nlmsg_end(msg, nlh);
	ib_device_put(device);
2180
	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2181 2182 2183 2184 2185 2186 2187 2188

err_msg:
	nlmsg_free(msg);
err:
	ib_device_put(device);
	return ret;
}

2189 2190 2191 2192 2193 2194 2195 2196
static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	int ret;

	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, extack);
2197
	if (ret)
2198 2199
		return -EINVAL;

2200 2201 2202
	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
		return stat_get_doit_default_counter(skb, nlh, extack, tb);

2203 2204
	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
	case RDMA_NLDEV_ATTR_RES_QP:
2205
		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2206
		break;
E
Erez Alfasi 已提交
2207 2208 2209 2210
	case RDMA_NLDEV_ATTR_RES_MR:
		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
					  fill_stat_mr_entry);
		break;
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static int nldev_stat_get_dumpit(struct sk_buff *skb,
				 struct netlink_callback *cb)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
	int ret;

	ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
			  nldev_policy, NULL);
	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
		return -EINVAL;

	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
	case RDMA_NLDEV_ATTR_RES_QP:
		ret = nldev_res_get_counter_dumpit(skb, cb);
		break;
E
Erez Alfasi 已提交
2234 2235 2236 2237
	case RDMA_NLDEV_ATTR_RES_MR:
		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
					    fill_stat_mr_entry);
		break;
2238 2239 2240 2241 2242 2243 2244 2245
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

2246
static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2247
	[RDMA_NLDEV_CMD_GET] = {
2248
		.doit = nldev_get_doit,
2249 2250
		.dump = nldev_get_dumpit,
	},
2251 2252 2253
	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
		.doit = nldev_get_chardev,
	},
2254 2255 2256 2257
	[RDMA_NLDEV_CMD_SET] = {
		.doit = nldev_set_doit,
		.flags = RDMA_NL_ADMIN_PERM,
	},
2258 2259 2260 2261 2262 2263 2264 2265
	[RDMA_NLDEV_CMD_NEWLINK] = {
		.doit = nldev_newlink,
		.flags = RDMA_NL_ADMIN_PERM,
	},
	[RDMA_NLDEV_CMD_DELLINK] = {
		.doit = nldev_dellink,
		.flags = RDMA_NL_ADMIN_PERM,
	},
2266
	[RDMA_NLDEV_CMD_PORT_GET] = {
2267
		.doit = nldev_port_get_doit,
2268 2269
		.dump = nldev_port_get_dumpit,
	},
2270 2271 2272 2273
	[RDMA_NLDEV_CMD_RES_GET] = {
		.doit = nldev_res_get_doit,
		.dump = nldev_res_get_dumpit,
	},
2274
	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2275
		.doit = nldev_res_get_qp_doit,
2276 2277
		.dump = nldev_res_get_qp_dumpit,
	},
2278
	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2279
		.doit = nldev_res_get_cm_id_doit,
2280 2281
		.dump = nldev_res_get_cm_id_dumpit,
	},
2282
	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2283
		.doit = nldev_res_get_cq_doit,
2284 2285
		.dump = nldev_res_get_cq_dumpit,
	},
2286
	[RDMA_NLDEV_CMD_RES_MR_GET] = {
2287
		.doit = nldev_res_get_mr_doit,
2288 2289
		.dump = nldev_res_get_mr_dumpit,
	},
2290
	[RDMA_NLDEV_CMD_RES_PD_GET] = {
2291
		.doit = nldev_res_get_pd_doit,
2292 2293
		.dump = nldev_res_get_pd_dumpit,
	},
2294 2295 2296 2297
	[RDMA_NLDEV_CMD_RES_CTX_GET] = {
		.doit = nldev_res_get_ctx_doit,
		.dump = nldev_res_get_ctx_dumpit,
	},
2298 2299 2300 2301
	[RDMA_NLDEV_CMD_RES_SRQ_GET] = {
		.doit = nldev_res_get_srq_doit,
		.dump = nldev_res_get_srq_dumpit,
	},
2302
	[RDMA_NLDEV_CMD_SYS_GET] = {
2303
		.doit = nldev_sys_get_doit,
2304
	},
2305 2306
	[RDMA_NLDEV_CMD_SYS_SET] = {
		.doit = nldev_set_sys_set_doit,
2307 2308 2309
	},
	[RDMA_NLDEV_CMD_STAT_SET] = {
		.doit = nldev_stat_set_doit,
2310 2311
		.flags = RDMA_NL_ADMIN_PERM,
	},
2312 2313 2314 2315
	[RDMA_NLDEV_CMD_STAT_GET] = {
		.doit = nldev_stat_get_doit,
		.dump = nldev_stat_get_dumpit,
	},
2316 2317 2318 2319
	[RDMA_NLDEV_CMD_STAT_DEL] = {
		.doit = nldev_stat_del_doit,
		.flags = RDMA_NL_ADMIN_PERM,
	},
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
	[RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
		.doit = nldev_res_get_qp_raw_doit,
		.dump = nldev_res_get_qp_raw_dumpit,
		.flags = RDMA_NL_ADMIN_PERM,
	},
	[RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
		.doit = nldev_res_get_cq_raw_doit,
		.dump = nldev_res_get_cq_raw_dumpit,
		.flags = RDMA_NL_ADMIN_PERM,
	},
	[RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
		.doit = nldev_res_get_mr_raw_doit,
		.dump = nldev_res_get_mr_raw_dumpit,
		.flags = RDMA_NL_ADMIN_PERM,
	},
2335 2336
};

2337 2338
void __init nldev_init(void)
{
2339
	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2340 2341 2342 2343 2344 2345
}

void __exit nldev_exit(void)
{
	rdma_nl_unregister(RDMA_NL_NLDEV);
}
2346 2347

MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);