1 /* 2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 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. 15 * 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. 19 * 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. 31 */ 32 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> 40 #include <rdma/frmr_pools.h> 41 42 #include "core_priv.h" 43 #include "cma_priv.h" 44 #include "restrack.h" 45 #include "uverbs.h" 46 #include "frmr_pools.h" 47 48 /* 49 * This determines whether a non-privileged user is allowed to specify a 50 * controlled QKEY or not, when true non-privileged user is allowed to specify 51 * a controlled QKEY. 52 */ 53 static bool privileged_qkey; 54 static DEFINE_MUTEX(nldev_dellink_mutex); 55 56 typedef int (*res_fill_func_t)(struct sk_buff*, bool, 57 struct rdma_restrack_entry*, uint32_t); 58 59 /* 60 * Sort array elements by the netlink attribute name 61 */ 62 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = { 63 [RDMA_NLDEV_ATTR_CHARDEV] = { .type = NLA_U64 }, 64 [RDMA_NLDEV_ATTR_CHARDEV_ABI] = { .type = NLA_U64 }, 65 [RDMA_NLDEV_ATTR_CHARDEV_NAME] = { .type = NLA_NUL_STRING, 66 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 67 [RDMA_NLDEV_ATTR_CHARDEV_TYPE] = { .type = NLA_NUL_STRING, 68 .len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE }, 69 [RDMA_NLDEV_ATTR_DEV_DIM] = { .type = NLA_U8 }, 70 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 }, 71 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, 72 .len = IB_DEVICE_NAME_MAX }, 73 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 }, 74 [RDMA_NLDEV_ATTR_DEV_PROTOCOL] = { .type = NLA_NUL_STRING, 75 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 76 [RDMA_NLDEV_ATTR_DRIVER] = { .type = NLA_NESTED }, 77 [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = { .type = NLA_NESTED }, 78 [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = { .type = NLA_U8 }, 79 [RDMA_NLDEV_ATTR_DRIVER_STRING] = { .type = NLA_NUL_STRING, 80 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 81 [RDMA_NLDEV_ATTR_DRIVER_S32] = { .type = NLA_S32 }, 82 [RDMA_NLDEV_ATTR_DRIVER_S64] = { .type = NLA_S64 }, 83 [RDMA_NLDEV_ATTR_DRIVER_U32] = { .type = NLA_U32 }, 84 [RDMA_NLDEV_ATTR_DRIVER_U64] = { .type = NLA_U64 }, 85 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING, 86 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 87 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 }, 88 [RDMA_NLDEV_ATTR_LINK_TYPE] = { .type = NLA_NUL_STRING, 89 .len = IFNAMSIZ }, 90 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 }, 91 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 }, 92 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING, 93 .len = IFNAMSIZ }, 94 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 }, 95 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 }, 96 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 }, 97 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 }, 98 [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED }, 99 [RDMA_NLDEV_ATTR_RES_CM_IDN] = { .type = NLA_U32 }, 100 [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED }, 101 [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED }, 102 [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 }, 103 [RDMA_NLDEV_ATTR_RES_CQN] = { .type = NLA_U32 }, 104 [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED }, 105 [RDMA_NLDEV_ATTR_RES_CTX] = { .type = NLA_NESTED }, 106 [RDMA_NLDEV_ATTR_RES_CTXN] = { .type = NLA_U32 }, 107 [RDMA_NLDEV_ATTR_RES_CTX_ENTRY] = { .type = NLA_NESTED }, 108 [RDMA_NLDEV_ATTR_RES_DST_ADDR] = { 109 .len = sizeof(struct __kernel_sockaddr_storage) }, 110 [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 }, 111 [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING, 112 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 113 [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 }, 114 [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 }, 115 [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 }, 116 [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED }, 117 [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 }, 118 [RDMA_NLDEV_ATTR_RES_MRN] = { .type = NLA_U32 }, 119 [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED }, 120 [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 }, 121 [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED }, 122 [RDMA_NLDEV_ATTR_RES_PDN] = { .type = NLA_U32 }, 123 [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED }, 124 [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 }, 125 [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 }, 126 [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 }, 127 [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED }, 128 [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED }, 129 [RDMA_NLDEV_ATTR_RES_RAW] = { .type = NLA_BINARY }, 130 [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 }, 131 [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 }, 132 [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 }, 133 [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 }, 134 [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = { 135 .len = sizeof(struct __kernel_sockaddr_storage) }, 136 [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 }, 137 [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED }, 138 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED }, 139 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 }, 140 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING, 141 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 142 [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 }, 143 [RDMA_NLDEV_ATTR_RES_SUBTYPE] = { .type = NLA_NUL_STRING, 144 .len = RDMA_NLDEV_ATTR_EMPTY_STRING }, 145 [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 }, 146 [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 }, 147 [RDMA_NLDEV_ATTR_RES_SRQ] = { .type = NLA_NESTED }, 148 [RDMA_NLDEV_ATTR_RES_SRQN] = { .type = NLA_U32 }, 149 [RDMA_NLDEV_ATTR_RES_SRQ_ENTRY] = { .type = NLA_NESTED }, 150 [RDMA_NLDEV_ATTR_MIN_RANGE] = { .type = NLA_U32 }, 151 [RDMA_NLDEV_ATTR_MAX_RANGE] = { .type = NLA_U32 }, 152 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 }, 153 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 }, 154 [RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK] = { .type = NLA_U32 }, 155 [RDMA_NLDEV_ATTR_STAT_MODE] = { .type = NLA_U32 }, 156 [RDMA_NLDEV_ATTR_STAT_RES] = { .type = NLA_U32 }, 157 [RDMA_NLDEV_ATTR_STAT_COUNTER] = { .type = NLA_NESTED }, 158 [RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY] = { .type = NLA_NESTED }, 159 [RDMA_NLDEV_ATTR_STAT_COUNTER_ID] = { .type = NLA_U32 }, 160 [RDMA_NLDEV_ATTR_STAT_HWCOUNTERS] = { .type = NLA_NESTED }, 161 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY] = { .type = NLA_NESTED }, 162 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING }, 163 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 }, 164 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 }, 165 [RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID] = { .type = NLA_U32 }, 166 [RDMA_NLDEV_NET_NS_FD] = { .type = NLA_U32 }, 167 [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = { .type = NLA_U8 }, 168 [RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK] = { .type = NLA_U8 }, 169 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX] = { .type = NLA_U32 }, 170 [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 }, 171 [RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE] = { .type = NLA_U8 }, 172 [RDMA_NLDEV_ATTR_DRIVER_DETAILS] = { .type = NLA_U8 }, 173 [RDMA_NLDEV_ATTR_DEV_TYPE] = { .type = NLA_U8 }, 174 [RDMA_NLDEV_ATTR_PARENT_NAME] = { .type = NLA_NUL_STRING }, 175 [RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE] = { .type = NLA_U8 }, 176 [RDMA_NLDEV_ATTR_EVENT_TYPE] = { .type = NLA_U8 }, 177 [RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED] = { .type = NLA_U8 }, 178 [RDMA_NLDEV_ATTR_FRMR_POOLS] = { .type = NLA_NESTED }, 179 [RDMA_NLDEV_ATTR_FRMR_POOL_ENTRY] = { .type = NLA_NESTED }, 180 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY] = { .type = NLA_NESTED }, 181 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS] = { .type = NLA_U8 }, 182 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS] = { .type = NLA_U32 }, 183 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY] = { .type = NLA_U64 }, 184 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS] = { .type = NLA_U64 }, 185 [RDMA_NLDEV_ATTR_FRMR_POOL_QUEUE_HANDLES] = { .type = NLA_U32 }, 186 [RDMA_NLDEV_ATTR_FRMR_POOL_MAX_IN_USE] = { .type = NLA_U64 }, 187 [RDMA_NLDEV_ATTR_FRMR_POOL_IN_USE] = { .type = NLA_U64 }, 188 [RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD] = { .type = NLA_U32 }, 189 [RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES] = { .type = NLA_U32 }, 190 [RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY] = { .type = NLA_U64 }, 191 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX] = { .type = NLA_U64 }, 192 }; 193 194 static int put_driver_name_print_type(struct sk_buff *msg, const char *name, 195 enum rdma_nldev_print_type print_type) 196 { 197 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name)) 198 return -EMSGSIZE; 199 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC && 200 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type)) 201 return -EMSGSIZE; 202 203 return 0; 204 } 205 206 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, 207 enum rdma_nldev_print_type print_type, 208 u32 value) 209 { 210 if (put_driver_name_print_type(msg, name, print_type)) 211 return -EMSGSIZE; 212 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value)) 213 return -EMSGSIZE; 214 215 return 0; 216 } 217 218 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, 219 enum rdma_nldev_print_type print_type, 220 u64 value) 221 { 222 if (put_driver_name_print_type(msg, name, print_type)) 223 return -EMSGSIZE; 224 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value, 225 RDMA_NLDEV_ATTR_PAD)) 226 return -EMSGSIZE; 227 228 return 0; 229 } 230 231 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name, 232 const char *str) 233 { 234 if (put_driver_name_print_type(msg, name, 235 RDMA_NLDEV_PRINT_TYPE_UNSPEC)) 236 return -EMSGSIZE; 237 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str)) 238 return -EMSGSIZE; 239 240 return 0; 241 } 242 EXPORT_SYMBOL(rdma_nl_put_driver_string); 243 244 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value) 245 { 246 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC, 247 value); 248 } 249 EXPORT_SYMBOL(rdma_nl_put_driver_u32); 250 251 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, 252 u32 value) 253 { 254 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX, 255 value); 256 } 257 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex); 258 259 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value) 260 { 261 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC, 262 value); 263 } 264 EXPORT_SYMBOL(rdma_nl_put_driver_u64); 265 266 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value) 267 { 268 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX, 269 value); 270 } 271 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex); 272 273 bool rdma_nl_get_privileged_qkey(void) 274 { 275 return privileged_qkey; 276 } 277 EXPORT_SYMBOL(rdma_nl_get_privileged_qkey); 278 279 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device) 280 { 281 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index)) 282 return -EMSGSIZE; 283 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, 284 dev_name(&device->dev))) 285 return -EMSGSIZE; 286 287 return 0; 288 } 289 290 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device) 291 { 292 char fw[IB_FW_VERSION_NAME_MAX]; 293 int ret = 0; 294 u32 port; 295 296 if (fill_nldev_handle(msg, device)) 297 return -EMSGSIZE; 298 299 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device))) 300 return -EMSGSIZE; 301 302 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64)); 303 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS, 304 device->attrs.device_cap_flags, 305 RDMA_NLDEV_ATTR_PAD)) 306 return -EMSGSIZE; 307 308 ib_get_device_fw_str(device, fw); 309 /* Device without FW has strlen(fw) = 0 */ 310 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw)) 311 return -EMSGSIZE; 312 313 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID, 314 be64_to_cpu(device->node_guid), 315 RDMA_NLDEV_ATTR_PAD)) 316 return -EMSGSIZE; 317 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID, 318 be64_to_cpu(device->attrs.sys_image_guid), 319 RDMA_NLDEV_ATTR_PAD)) 320 return -EMSGSIZE; 321 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type)) 322 return -EMSGSIZE; 323 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim)) 324 return -EMSGSIZE; 325 326 if (device->type && 327 nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_TYPE, device->type)) 328 return -EMSGSIZE; 329 330 if (device->parent && 331 nla_put_string(msg, RDMA_NLDEV_ATTR_PARENT_NAME, 332 dev_name(&device->parent->dev))) 333 return -EMSGSIZE; 334 335 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE, 336 device->name_assign_type)) 337 return -EMSGSIZE; 338 339 /* 340 * Link type is determined on first port and mlx4 device 341 * which can potentially have two different link type for the same 342 * IB device is considered as better to be avoided in the future, 343 */ 344 port = rdma_start_port(device); 345 if (rdma_cap_opa_mad(device, port)) 346 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa"); 347 else if (rdma_protocol_ib(device, port)) 348 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib"); 349 else if (rdma_protocol_iwarp(device, port)) 350 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw"); 351 else if (rdma_protocol_roce(device, port)) 352 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce"); 353 else if (rdma_protocol_usnic(device, port)) 354 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, 355 "usnic"); 356 return ret; 357 } 358 359 static int fill_port_info(struct sk_buff *msg, 360 struct ib_device *device, u32 port, 361 const struct net *net) 362 { 363 struct net_device *netdev = NULL; 364 struct ib_port_attr attr; 365 int ret; 366 u64 cap_flags = 0; 367 368 if (fill_nldev_handle(msg, device)) 369 return -EMSGSIZE; 370 371 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) 372 return -EMSGSIZE; 373 374 ret = ib_query_port(device, port, &attr); 375 if (ret) 376 return ret; 377 378 if (rdma_protocol_ib(device, port)) { 379 BUILD_BUG_ON((sizeof(attr.port_cap_flags) + 380 sizeof(attr.port_cap_flags2)) > sizeof(u64)); 381 cap_flags = attr.port_cap_flags | 382 ((u64)attr.port_cap_flags2 << 32); 383 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS, 384 cap_flags, RDMA_NLDEV_ATTR_PAD)) 385 return -EMSGSIZE; 386 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX, 387 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD)) 388 return -EMSGSIZE; 389 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid)) 390 return -EMSGSIZE; 391 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid)) 392 return -EMSGSIZE; 393 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc)) 394 return -EMSGSIZE; 395 } 396 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state)) 397 return -EMSGSIZE; 398 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state)) 399 return -EMSGSIZE; 400 401 netdev = ib_device_get_netdev(device, port); 402 if (netdev && net_eq(dev_net(netdev), net)) { 403 ret = nla_put_u32(msg, 404 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex); 405 if (ret) 406 goto out; 407 ret = nla_put_string(msg, 408 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name); 409 } 410 411 out: 412 dev_put(netdev); 413 return ret; 414 } 415 416 static int fill_res_info_entry(struct sk_buff *msg, 417 const char *name, u64 curr, u64 max) 418 { 419 struct nlattr *entry_attr; 420 421 entry_attr = nla_nest_start_noflag(msg, 422 RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY); 423 if (!entry_attr) 424 return -EMSGSIZE; 425 426 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name)) 427 goto err; 428 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr, 429 RDMA_NLDEV_ATTR_PAD)) 430 goto err; 431 if (max && nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX, 432 max, RDMA_NLDEV_ATTR_PAD)) 433 goto err; 434 435 nla_nest_end(msg, entry_attr); 436 return 0; 437 438 err: 439 nla_nest_cancel(msg, entry_attr); 440 return -EMSGSIZE; 441 } 442 443 static int fill_res_info(struct sk_buff *msg, struct ib_device *device, 444 bool show_details) 445 { 446 static const char * const names[RDMA_RESTRACK_MAX] = { 447 [RDMA_RESTRACK_PD] = "pd", 448 [RDMA_RESTRACK_CQ] = "cq", 449 [RDMA_RESTRACK_QP] = "qp", 450 [RDMA_RESTRACK_CM_ID] = "cm_id", 451 [RDMA_RESTRACK_MR] = "mr", 452 [RDMA_RESTRACK_CTX] = "ctx", 453 [RDMA_RESTRACK_SRQ] = "srq", 454 [RDMA_RESTRACK_COMP_CNTR] = "comp_cntr", 455 }; 456 457 struct ib_comp_cntr_caps comp_cntr_caps = {}; 458 struct nlattr *table_attr; 459 u64 curr, max; 460 int ret, i; 461 462 if (fill_nldev_handle(msg, device)) 463 return -EMSGSIZE; 464 465 table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY); 466 if (!table_attr) 467 return -EMSGSIZE; 468 469 if (device->ops.query_comp_cntr_caps) 470 device->ops.query_comp_cntr_caps(device, &comp_cntr_caps, NULL); 471 472 for (i = 0; i < RDMA_RESTRACK_MAX; i++) { 473 if (!names[i]) 474 continue; 475 curr = rdma_restrack_count(device, i, show_details); 476 switch (i) { 477 case RDMA_RESTRACK_QP: 478 max = device->attrs.max_qp; 479 break; 480 case RDMA_RESTRACK_CQ: 481 max = device->attrs.max_cq; 482 break; 483 case RDMA_RESTRACK_MR: 484 max = device->attrs.max_mr; 485 break; 486 case RDMA_RESTRACK_PD: 487 max = device->attrs.max_pd; 488 break; 489 case RDMA_RESTRACK_SRQ: 490 max = device->attrs.max_srq; 491 break; 492 case RDMA_RESTRACK_COMP_CNTR: 493 max = comp_cntr_caps.max_counters; 494 break; 495 default: 496 max = 0; 497 } 498 ret = fill_res_info_entry(msg, names[i], curr, max); 499 if (ret) 500 goto err; 501 } 502 503 nla_nest_end(msg, table_attr); 504 return 0; 505 506 err: 507 nla_nest_cancel(msg, table_attr); 508 return ret; 509 } 510 511 static int fill_res_name_pid(struct sk_buff *msg, 512 struct rdma_restrack_entry *res) 513 { 514 int err = 0; 515 516 /* 517 * For user resources, user is should read /proc/PID/comm to get the 518 * name of the task file. 519 */ 520 if (rdma_is_kernel_res(res)) { 521 err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME, 522 res->kern_name); 523 } else { 524 pid_t pid; 525 526 pid = task_pid_vnr(res->task); 527 /* 528 * Task is dead and in zombie state. 529 * There is no need to print PID anymore. 530 */ 531 if (pid) 532 /* 533 * This part is racy, task can be killed and PID will 534 * be zero right here but it is ok, next query won't 535 * return PID. We don't promise real-time reflection 536 * of SW objects. 537 */ 538 err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid); 539 } 540 541 return err ? -EMSGSIZE : 0; 542 } 543 544 static int fill_res_qp_entry_query(struct sk_buff *msg, 545 struct rdma_restrack_entry *res, 546 struct ib_device *dev, 547 struct ib_qp *qp) 548 { 549 struct ib_qp_init_attr qp_init_attr; 550 struct ib_qp_attr qp_attr; 551 int ret; 552 553 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr); 554 if (ret) 555 return ret; 556 557 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) { 558 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN, 559 qp_attr.dest_qp_num)) 560 goto err; 561 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN, 562 qp_attr.rq_psn)) 563 goto err; 564 } 565 566 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn)) 567 goto err; 568 569 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC || 570 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) { 571 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE, 572 qp_attr.path_mig_state)) 573 goto err; 574 } 575 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type)) 576 goto err; 577 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state)) 578 goto err; 579 580 if (dev->ops.fill_res_qp_entry) 581 return dev->ops.fill_res_qp_entry(msg, qp); 582 return 0; 583 584 err: return -EMSGSIZE; 585 } 586 587 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin, 588 struct rdma_restrack_entry *res, uint32_t port) 589 { 590 struct ib_qp *qp = container_of(res, struct ib_qp, res); 591 struct ib_device *dev = qp->device; 592 int ret; 593 594 if (port && port != qp->port) 595 return -EAGAIN; 596 597 /* In create_qp() port is not set yet */ 598 if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port)) 599 return -EMSGSIZE; 600 601 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num); 602 if (ret) 603 return -EMSGSIZE; 604 605 if (!rdma_is_kernel_res(res) && 606 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id)) 607 return -EMSGSIZE; 608 609 ret = fill_res_name_pid(msg, res); 610 if (ret) 611 return -EMSGSIZE; 612 613 return fill_res_qp_entry_query(msg, res, dev, qp); 614 } 615 616 static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin, 617 struct rdma_restrack_entry *res, uint32_t port) 618 { 619 struct ib_qp *qp = container_of(res, struct ib_qp, res); 620 struct ib_device *dev = qp->device; 621 622 if (port && port != qp->port) 623 return -EAGAIN; 624 if (!dev->ops.fill_res_qp_entry_raw) 625 return -EINVAL; 626 return dev->ops.fill_res_qp_entry_raw(msg, qp); 627 } 628 629 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin, 630 struct rdma_restrack_entry *res, uint32_t port) 631 { 632 struct rdma_id_private *id_priv = 633 container_of(res, struct rdma_id_private, res); 634 struct ib_device *dev = id_priv->id.device; 635 struct rdma_cm_id *cm_id = &id_priv->id; 636 637 if (port && port != cm_id->port_num) 638 return -EAGAIN; 639 640 if (cm_id->port_num && 641 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num)) 642 goto err; 643 644 if (id_priv->qp_num) { 645 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num)) 646 goto err; 647 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type)) 648 goto err; 649 } 650 651 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps)) 652 goto err; 653 654 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state)) 655 goto err; 656 657 if (cm_id->route.addr.src_addr.ss_family && 658 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR, 659 sizeof(cm_id->route.addr.src_addr), 660 &cm_id->route.addr.src_addr)) 661 goto err; 662 if (cm_id->route.addr.dst_addr.ss_family && 663 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR, 664 sizeof(cm_id->route.addr.dst_addr), 665 &cm_id->route.addr.dst_addr)) 666 goto err; 667 668 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id)) 669 goto err; 670 671 if (fill_res_name_pid(msg, res)) 672 goto err; 673 674 if (dev->ops.fill_res_cm_id_entry) 675 return dev->ops.fill_res_cm_id_entry(msg, cm_id); 676 return 0; 677 678 err: return -EMSGSIZE; 679 } 680 681 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin, 682 struct rdma_restrack_entry *res, uint32_t port) 683 { 684 struct ib_cq *cq = container_of(res, struct ib_cq, res); 685 struct ib_device *dev = cq->device; 686 687 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe)) 688 return -EMSGSIZE; 689 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT, 690 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD)) 691 return -EMSGSIZE; 692 693 /* Poll context is only valid for kernel CQs */ 694 if (rdma_is_kernel_res(res) && 695 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx)) 696 return -EMSGSIZE; 697 698 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL))) 699 return -EMSGSIZE; 700 701 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id)) 702 return -EMSGSIZE; 703 if (!rdma_is_kernel_res(res) && 704 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, 705 cq->uobject->uevent.uobject.context->res.id)) 706 return -EMSGSIZE; 707 708 if (fill_res_name_pid(msg, res)) 709 return -EMSGSIZE; 710 711 return (dev->ops.fill_res_cq_entry) ? 712 dev->ops.fill_res_cq_entry(msg, cq) : 0; 713 } 714 715 static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin, 716 struct rdma_restrack_entry *res, uint32_t port) 717 { 718 struct ib_cq *cq = container_of(res, struct ib_cq, res); 719 struct ib_device *dev = cq->device; 720 721 if (!dev->ops.fill_res_cq_entry_raw) 722 return -EINVAL; 723 return dev->ops.fill_res_cq_entry_raw(msg, cq); 724 } 725 726 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin, 727 struct rdma_restrack_entry *res, uint32_t port) 728 { 729 struct ib_mr *mr = container_of(res, struct ib_mr, res); 730 struct ib_device *dev = mr->device; 731 732 if (has_cap_net_admin) { 733 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey)) 734 return -EMSGSIZE; 735 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey)) 736 return -EMSGSIZE; 737 } 738 739 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length, 740 RDMA_NLDEV_ATTR_PAD)) 741 return -EMSGSIZE; 742 743 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id)) 744 return -EMSGSIZE; 745 746 if (!rdma_is_kernel_res(res)) { 747 struct ib_pd *pd = READ_ONCE(mr->pd); 748 749 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, pd->res.id)) 750 return -EMSGSIZE; 751 } 752 753 if (fill_res_name_pid(msg, res)) 754 return -EMSGSIZE; 755 756 return (dev->ops.fill_res_mr_entry) ? 757 dev->ops.fill_res_mr_entry(msg, mr) : 758 0; 759 } 760 761 static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin, 762 struct rdma_restrack_entry *res, uint32_t port) 763 { 764 struct ib_mr *mr = container_of(res, struct ib_mr, res); 765 struct ib_device *dev = mr->device; 766 767 if (!dev->ops.fill_res_mr_entry_raw) 768 return -EINVAL; 769 return dev->ops.fill_res_mr_entry_raw(msg, mr); 770 } 771 772 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin, 773 struct rdma_restrack_entry *res, uint32_t port) 774 { 775 struct ib_pd *pd = container_of(res, struct ib_pd, res); 776 777 if (has_cap_net_admin) { 778 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY, 779 pd->local_dma_lkey)) 780 goto err; 781 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) && 782 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY, 783 pd->unsafe_global_rkey)) 784 goto err; 785 } 786 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT, 787 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD)) 788 goto err; 789 790 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id)) 791 goto err; 792 793 if (!rdma_is_kernel_res(res) && 794 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, 795 pd->uobject->context->res.id)) 796 goto err; 797 798 return fill_res_name_pid(msg, res); 799 800 err: return -EMSGSIZE; 801 } 802 803 static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin, 804 struct rdma_restrack_entry *res, uint32_t port) 805 { 806 struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res); 807 808 if (rdma_is_kernel_res(res)) 809 return 0; 810 811 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id)) 812 return -EMSGSIZE; 813 814 return fill_res_name_pid(msg, res); 815 } 816 817 static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range, 818 uint32_t max_range) 819 { 820 struct nlattr *entry_attr; 821 822 if (!min_range) 823 return 0; 824 825 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY); 826 if (!entry_attr) 827 return -EMSGSIZE; 828 829 if (min_range == max_range) { 830 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range)) 831 goto err; 832 } else { 833 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range)) 834 goto err; 835 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range)) 836 goto err; 837 } 838 nla_nest_end(msg, entry_attr); 839 return 0; 840 841 err: 842 nla_nest_cancel(msg, entry_attr); 843 return -EMSGSIZE; 844 } 845 846 static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq) 847 { 848 uint32_t min_range = 0, prev = 0; 849 struct rdma_restrack_entry *res; 850 struct rdma_restrack_root *rt; 851 struct nlattr *table_attr; 852 struct ib_qp *qp = NULL; 853 unsigned long id = 0; 854 855 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP); 856 if (!table_attr) 857 return -EMSGSIZE; 858 859 rt = &srq->device->res[RDMA_RESTRACK_QP]; 860 xa_lock(&rt->xa); 861 xa_for_each(&rt->xa, id, res) { 862 if (!rdma_restrack_get(res)) 863 continue; 864 865 qp = container_of(res, struct ib_qp, res); 866 if (!qp->srq || (qp->srq->res.id != srq->res.id)) { 867 rdma_restrack_put(res); 868 continue; 869 } 870 871 if (qp->qp_num < prev) 872 /* qp_num should be ascending */ 873 goto err_loop; 874 875 if (min_range == 0) { 876 min_range = qp->qp_num; 877 } else if (qp->qp_num > (prev + 1)) { 878 if (fill_res_range_qp_entry(msg, min_range, prev)) 879 goto err_loop; 880 881 min_range = qp->qp_num; 882 } 883 prev = qp->qp_num; 884 rdma_restrack_put(res); 885 } 886 887 xa_unlock(&rt->xa); 888 889 if (fill_res_range_qp_entry(msg, min_range, prev)) 890 goto err; 891 892 nla_nest_end(msg, table_attr); 893 return 0; 894 895 err_loop: 896 rdma_restrack_put(res); 897 xa_unlock(&rt->xa); 898 err: 899 nla_nest_cancel(msg, table_attr); 900 return -EMSGSIZE; 901 } 902 903 static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin, 904 struct rdma_restrack_entry *res, uint32_t port) 905 { 906 struct ib_srq *srq = container_of(res, struct ib_srq, res); 907 struct ib_device *dev = srq->device; 908 909 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id)) 910 goto err; 911 912 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type)) 913 goto err; 914 915 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id)) 916 goto err; 917 918 if (ib_srq_has_cq(srq->srq_type)) { 919 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, 920 srq->ext.cq->res.id)) 921 goto err; 922 } 923 924 if (fill_res_srq_qps(msg, srq)) 925 goto err; 926 927 if (fill_res_name_pid(msg, res)) 928 goto err; 929 930 if (dev->ops.fill_res_srq_entry) 931 return dev->ops.fill_res_srq_entry(msg, srq); 932 933 return 0; 934 935 err: 936 return -EMSGSIZE; 937 } 938 939 static int fill_res_srq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin, 940 struct rdma_restrack_entry *res, uint32_t port) 941 { 942 struct ib_srq *srq = container_of(res, struct ib_srq, res); 943 struct ib_device *dev = srq->device; 944 945 if (!dev->ops.fill_res_srq_entry_raw) 946 return -EINVAL; 947 return dev->ops.fill_res_srq_entry_raw(msg, srq); 948 } 949 950 static int fill_stat_counter_mode(struct sk_buff *msg, 951 struct rdma_counter *counter) 952 { 953 struct rdma_counter_mode *m = &counter->mode; 954 955 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode)) 956 return -EMSGSIZE; 957 958 if (m->mode == RDMA_COUNTER_MODE_AUTO) { 959 if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) && 960 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type)) 961 return -EMSGSIZE; 962 963 if ((m->mask & RDMA_COUNTER_MASK_PID) && 964 fill_res_name_pid(msg, &counter->res)) 965 return -EMSGSIZE; 966 } 967 968 return 0; 969 } 970 971 static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn) 972 { 973 struct nlattr *entry_attr; 974 975 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY); 976 if (!entry_attr) 977 return -EMSGSIZE; 978 979 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) 980 goto err; 981 982 nla_nest_end(msg, entry_attr); 983 return 0; 984 985 err: 986 nla_nest_cancel(msg, entry_attr); 987 return -EMSGSIZE; 988 } 989 990 static int fill_stat_counter_qps(struct sk_buff *msg, 991 struct rdma_counter *counter) 992 { 993 struct rdma_restrack_entry *res; 994 struct rdma_restrack_root *rt; 995 struct nlattr *table_attr; 996 struct ib_qp *qp = NULL; 997 unsigned long id = 0; 998 int ret = 0; 999 1000 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP); 1001 if (!table_attr) 1002 return -EMSGSIZE; 1003 1004 rt = &counter->device->res[RDMA_RESTRACK_QP]; 1005 xa_lock(&rt->xa); 1006 xa_for_each(&rt->xa, id, res) { 1007 qp = container_of(res, struct ib_qp, res); 1008 if (!qp->counter || (qp->counter->id != counter->id)) 1009 continue; 1010 1011 ret = fill_stat_counter_qp_entry(msg, qp->qp_num); 1012 if (ret) 1013 goto err; 1014 } 1015 1016 xa_unlock(&rt->xa); 1017 nla_nest_end(msg, table_attr); 1018 return 0; 1019 1020 err: 1021 xa_unlock(&rt->xa); 1022 nla_nest_cancel(msg, table_attr); 1023 return ret; 1024 } 1025 1026 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name, 1027 u64 value) 1028 { 1029 struct nlattr *entry_attr; 1030 1031 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY); 1032 if (!entry_attr) 1033 return -EMSGSIZE; 1034 1035 if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME, 1036 name)) 1037 goto err; 1038 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE, 1039 value, RDMA_NLDEV_ATTR_PAD)) 1040 goto err; 1041 1042 nla_nest_end(msg, entry_attr); 1043 return 0; 1044 1045 err: 1046 nla_nest_cancel(msg, entry_attr); 1047 return -EMSGSIZE; 1048 } 1049 EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry); 1050 1051 static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin, 1052 struct rdma_restrack_entry *res, uint32_t port) 1053 { 1054 struct ib_mr *mr = container_of(res, struct ib_mr, res); 1055 struct ib_device *dev = mr->device; 1056 1057 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id)) 1058 goto err; 1059 1060 if (dev->ops.fill_stat_mr_entry) 1061 return dev->ops.fill_stat_mr_entry(msg, mr); 1062 return 0; 1063 1064 err: 1065 return -EMSGSIZE; 1066 } 1067 1068 static int fill_stat_counter_hwcounters(struct sk_buff *msg, 1069 struct rdma_counter *counter) 1070 { 1071 struct rdma_hw_stats *st = counter->stats; 1072 struct nlattr *table_attr; 1073 int i; 1074 1075 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS); 1076 if (!table_attr) 1077 return -EMSGSIZE; 1078 1079 mutex_lock(&st->lock); 1080 for (i = 0; i < st->num_counters; i++) { 1081 if (test_bit(i, st->is_disabled)) 1082 continue; 1083 if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name, 1084 st->value[i])) 1085 goto err; 1086 } 1087 mutex_unlock(&st->lock); 1088 1089 nla_nest_end(msg, table_attr); 1090 return 0; 1091 1092 err: 1093 mutex_unlock(&st->lock); 1094 nla_nest_cancel(msg, table_attr); 1095 return -EMSGSIZE; 1096 } 1097 1098 static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin, 1099 struct rdma_restrack_entry *res, 1100 uint32_t port) 1101 { 1102 struct rdma_counter *counter = 1103 container_of(res, struct rdma_counter, res); 1104 1105 if (port && port != counter->port) 1106 return -EAGAIN; 1107 1108 /* Dump it even query failed */ 1109 rdma_counter_query_stats(counter); 1110 1111 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) || 1112 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) || 1113 fill_stat_counter_mode(msg, counter) || 1114 fill_stat_counter_qps(msg, counter) || 1115 fill_stat_counter_hwcounters(msg, counter)) 1116 return -EMSGSIZE; 1117 1118 return 0; 1119 } 1120 1121 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1122 struct netlink_ext_ack *extack) 1123 { 1124 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1125 struct ib_device *device; 1126 struct sk_buff *msg; 1127 u32 index; 1128 int err; 1129 1130 err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1131 nldev_policy, NL_VALIDATE_LIBERAL, extack); 1132 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1133 return -EINVAL; 1134 1135 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1136 1137 device = ib_device_get_by_index(sock_net(skb->sk), index); 1138 if (!device) 1139 return -EINVAL; 1140 1141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1142 if (!msg) { 1143 err = -ENOMEM; 1144 goto err; 1145 } 1146 1147 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1148 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 1149 0, 0); 1150 if (!nlh) { 1151 err = -EMSGSIZE; 1152 goto err_free; 1153 } 1154 1155 err = fill_dev_info(msg, device); 1156 if (err) 1157 goto err_free; 1158 1159 nlmsg_end(msg, nlh); 1160 1161 ib_device_put(device); 1162 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 1163 1164 err_free: 1165 nlmsg_free(msg); 1166 err: 1167 ib_device_put(device); 1168 return err; 1169 } 1170 1171 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1172 struct netlink_ext_ack *extack) 1173 { 1174 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1175 struct ib_device *device; 1176 u32 index; 1177 int err; 1178 1179 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1180 nldev_policy, extack); 1181 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1182 return -EINVAL; 1183 1184 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1185 device = ib_device_get_by_index(sock_net(skb->sk), index); 1186 if (!device) 1187 return -EINVAL; 1188 1189 if (tb[RDMA_NLDEV_NET_NS_FD]) { 1190 char name[IB_DEVICE_NAME_MAX] = {}; 1191 u32 ns_fd; 1192 1193 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) { 1194 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], 1195 IB_DEVICE_NAME_MAX); 1196 if (strlen(name) == 0) { 1197 err = -EINVAL; 1198 goto done; 1199 } 1200 } 1201 ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]); 1202 err = ib_device_set_netns_put(skb, device, ns_fd, 1203 name[0] ? name : NULL, extack); 1204 goto put_done; 1205 } 1206 1207 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) { 1208 char name[IB_DEVICE_NAME_MAX] = {}; 1209 1210 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], 1211 IB_DEVICE_NAME_MAX); 1212 if (strlen(name) == 0) { 1213 err = -EINVAL; 1214 goto done; 1215 } 1216 err = ib_device_rename(device, name); 1217 goto done; 1218 } 1219 1220 if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) { 1221 u8 use_dim; 1222 1223 use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]); 1224 err = ib_device_set_dim(device, use_dim); 1225 goto done; 1226 } 1227 1228 done: 1229 ib_device_put(device); 1230 put_done: 1231 return err; 1232 } 1233 1234 static int _nldev_get_dumpit(struct ib_device *device, 1235 struct sk_buff *skb, 1236 struct netlink_callback *cb, 1237 unsigned int idx) 1238 { 1239 int start = cb->args[0]; 1240 struct nlmsghdr *nlh; 1241 1242 if (idx < start) 1243 return 0; 1244 1245 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1246 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 1247 0, NLM_F_MULTI); 1248 1249 if (!nlh || fill_dev_info(skb, device)) { 1250 nlmsg_cancel(skb, nlh); 1251 goto out; 1252 } 1253 1254 nlmsg_end(skb, nlh); 1255 1256 idx++; 1257 1258 out: cb->args[0] = idx; 1259 return skb->len; 1260 } 1261 1262 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 1263 { 1264 /* 1265 * There is no need to take lock, because 1266 * we are relying on ib_core's locking. 1267 */ 1268 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb); 1269 } 1270 1271 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1272 struct netlink_ext_ack *extack) 1273 { 1274 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1275 struct ib_device *device; 1276 struct sk_buff *msg; 1277 u32 index; 1278 u32 port; 1279 int err; 1280 1281 err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1282 nldev_policy, NL_VALIDATE_LIBERAL, extack); 1283 if (err || 1284 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || 1285 !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 1286 return -EINVAL; 1287 1288 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1289 device = ib_device_get_by_index(sock_net(skb->sk), index); 1290 if (!device) 1291 return -EINVAL; 1292 1293 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 1294 if (!rdma_is_port_valid(device, port)) { 1295 err = -EINVAL; 1296 goto err; 1297 } 1298 1299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1300 if (!msg) { 1301 err = -ENOMEM; 1302 goto err; 1303 } 1304 1305 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1306 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 1307 0, 0); 1308 if (!nlh) { 1309 err = -EMSGSIZE; 1310 goto err_free; 1311 } 1312 1313 err = fill_port_info(msg, device, port, sock_net(skb->sk)); 1314 if (err) 1315 goto err_free; 1316 1317 nlmsg_end(msg, nlh); 1318 ib_device_put(device); 1319 1320 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 1321 1322 err_free: 1323 nlmsg_free(msg); 1324 err: 1325 ib_device_put(device); 1326 return err; 1327 } 1328 1329 static int nldev_port_get_dumpit(struct sk_buff *skb, 1330 struct netlink_callback *cb) 1331 { 1332 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1333 struct ib_device *device; 1334 int start = cb->args[0]; 1335 struct nlmsghdr *nlh; 1336 u32 idx = 0; 1337 u32 ifindex; 1338 int err; 1339 unsigned int p; 1340 1341 err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1342 nldev_policy, NL_VALIDATE_LIBERAL, NULL); 1343 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1344 return -EINVAL; 1345 1346 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1347 device = ib_device_get_by_index(sock_net(skb->sk), ifindex); 1348 if (!device) 1349 return -EINVAL; 1350 1351 rdma_for_each_port (device, p) { 1352 /* 1353 * The dumpit function returns all information from specific 1354 * index. This specific index is taken from the netlink 1355 * messages request sent by user and it is available 1356 * in cb->args[0]. 1357 * 1358 * Usually, the user doesn't fill this field and it causes 1359 * to return everything. 1360 * 1361 */ 1362 if (idx < start) { 1363 idx++; 1364 continue; 1365 } 1366 1367 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 1368 cb->nlh->nlmsg_seq, 1369 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 1370 RDMA_NLDEV_CMD_PORT_GET), 1371 0, NLM_F_MULTI); 1372 1373 if (!nlh || fill_port_info(skb, device, p, sock_net(skb->sk))) { 1374 nlmsg_cancel(skb, nlh); 1375 goto out; 1376 } 1377 idx++; 1378 nlmsg_end(skb, nlh); 1379 } 1380 1381 out: 1382 ib_device_put(device); 1383 cb->args[0] = idx; 1384 return skb->len; 1385 } 1386 1387 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1388 struct netlink_ext_ack *extack) 1389 { 1390 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1391 bool show_details = false; 1392 struct ib_device *device; 1393 struct sk_buff *msg; 1394 u32 index; 1395 int ret; 1396 1397 ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1398 nldev_policy, NL_VALIDATE_LIBERAL, extack); 1399 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1400 return -EINVAL; 1401 1402 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1403 device = ib_device_get_by_index(sock_net(skb->sk), index); 1404 if (!device) 1405 return -EINVAL; 1406 1407 if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]) 1408 show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]); 1409 1410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1411 if (!msg) { 1412 ret = -ENOMEM; 1413 goto err; 1414 } 1415 1416 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1417 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET), 1418 0, 0); 1419 if (!nlh) { 1420 ret = -EMSGSIZE; 1421 goto err_free; 1422 } 1423 1424 ret = fill_res_info(msg, device, show_details); 1425 if (ret) 1426 goto err_free; 1427 1428 nlmsg_end(msg, nlh); 1429 ib_device_put(device); 1430 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 1431 1432 err_free: 1433 nlmsg_free(msg); 1434 err: 1435 ib_device_put(device); 1436 return ret; 1437 } 1438 1439 static int _nldev_res_get_dumpit(struct ib_device *device, 1440 struct sk_buff *skb, 1441 struct netlink_callback *cb, 1442 unsigned int idx) 1443 { 1444 int start = cb->args[0]; 1445 struct nlmsghdr *nlh; 1446 1447 if (idx < start) 1448 return 0; 1449 1450 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1451 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET), 1452 0, NLM_F_MULTI); 1453 1454 if (!nlh || fill_res_info(skb, device, false)) { 1455 nlmsg_cancel(skb, nlh); 1456 goto out; 1457 } 1458 nlmsg_end(skb, nlh); 1459 1460 idx++; 1461 1462 out: 1463 cb->args[0] = idx; 1464 return skb->len; 1465 } 1466 1467 static int nldev_res_get_dumpit(struct sk_buff *skb, 1468 struct netlink_callback *cb) 1469 { 1470 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb); 1471 } 1472 1473 struct nldev_fill_res_entry { 1474 enum rdma_nldev_attr nldev_attr; 1475 u8 flags; 1476 u32 entry; 1477 u32 id; 1478 }; 1479 1480 enum nldev_res_flags { 1481 NLDEV_PER_DEV = 1 << 0, 1482 }; 1483 1484 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = { 1485 [RDMA_RESTRACK_QP] = { 1486 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP, 1487 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY, 1488 .id = RDMA_NLDEV_ATTR_RES_LQPN, 1489 }, 1490 [RDMA_RESTRACK_CM_ID] = { 1491 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID, 1492 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY, 1493 .id = RDMA_NLDEV_ATTR_RES_CM_IDN, 1494 }, 1495 [RDMA_RESTRACK_CQ] = { 1496 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ, 1497 .flags = NLDEV_PER_DEV, 1498 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY, 1499 .id = RDMA_NLDEV_ATTR_RES_CQN, 1500 }, 1501 [RDMA_RESTRACK_MR] = { 1502 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR, 1503 .flags = NLDEV_PER_DEV, 1504 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY, 1505 .id = RDMA_NLDEV_ATTR_RES_MRN, 1506 }, 1507 [RDMA_RESTRACK_PD] = { 1508 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD, 1509 .flags = NLDEV_PER_DEV, 1510 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY, 1511 .id = RDMA_NLDEV_ATTR_RES_PDN, 1512 }, 1513 [RDMA_RESTRACK_COUNTER] = { 1514 .nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER, 1515 .entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY, 1516 .id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID, 1517 }, 1518 [RDMA_RESTRACK_CTX] = { 1519 .nldev_attr = RDMA_NLDEV_ATTR_RES_CTX, 1520 .flags = NLDEV_PER_DEV, 1521 .entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY, 1522 .id = RDMA_NLDEV_ATTR_RES_CTXN, 1523 }, 1524 [RDMA_RESTRACK_SRQ] = { 1525 .nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ, 1526 .flags = NLDEV_PER_DEV, 1527 .entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY, 1528 .id = RDMA_NLDEV_ATTR_RES_SRQN, 1529 }, 1530 1531 }; 1532 1533 static noinline_for_stack int 1534 res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1535 struct netlink_ext_ack *extack, 1536 enum rdma_restrack_type res_type, 1537 res_fill_func_t fill_func) 1538 { 1539 const struct nldev_fill_res_entry *fe = &fill_entries[res_type]; 1540 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1541 struct rdma_restrack_entry *res; 1542 struct ib_device *device; 1543 u32 index, id, port = 0; 1544 bool has_cap_net_admin; 1545 struct sk_buff *msg; 1546 int ret; 1547 1548 ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1549 nldev_policy, NL_VALIDATE_LIBERAL, extack); 1550 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id]) 1551 return -EINVAL; 1552 1553 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1554 device = ib_device_get_by_index(sock_net(skb->sk), index); 1555 if (!device) 1556 return -EINVAL; 1557 1558 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 1559 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 1560 if (!rdma_is_port_valid(device, port)) { 1561 ret = -EINVAL; 1562 goto err; 1563 } 1564 } 1565 1566 if ((port && fe->flags & NLDEV_PER_DEV) || 1567 (!port && ~fe->flags & NLDEV_PER_DEV)) { 1568 ret = -EINVAL; 1569 goto err; 1570 } 1571 1572 id = nla_get_u32(tb[fe->id]); 1573 res = rdma_restrack_get_byid(device, res_type, id); 1574 if (IS_ERR(res)) { 1575 ret = PTR_ERR(res); 1576 goto err; 1577 } 1578 1579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1580 if (!msg) { 1581 ret = -ENOMEM; 1582 goto err_get; 1583 } 1584 1585 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1586 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 1587 RDMA_NL_GET_OP(nlh->nlmsg_type)), 1588 0, 0); 1589 1590 if (!nlh || fill_nldev_handle(msg, device)) { 1591 ret = -EMSGSIZE; 1592 goto err_free; 1593 } 1594 1595 has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN); 1596 1597 ret = fill_func(msg, has_cap_net_admin, res, port); 1598 if (ret) 1599 goto err_free; 1600 1601 rdma_restrack_put(res); 1602 nlmsg_end(msg, nlh); 1603 ib_device_put(device); 1604 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 1605 1606 err_free: 1607 nlmsg_free(msg); 1608 err_get: 1609 rdma_restrack_put(res); 1610 err: 1611 ib_device_put(device); 1612 return ret; 1613 } 1614 1615 static int res_get_common_dumpit(struct sk_buff *skb, 1616 struct netlink_callback *cb, 1617 enum rdma_restrack_type res_type, 1618 res_fill_func_t fill_func) 1619 { 1620 const struct nldev_fill_res_entry *fe = &fill_entries[res_type]; 1621 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1622 struct rdma_restrack_entry *res; 1623 struct rdma_restrack_root *rt; 1624 int err, ret = 0, idx = 0; 1625 bool show_details = false; 1626 struct nlattr *table_attr; 1627 struct nlattr *entry_attr; 1628 struct ib_device *device; 1629 int start = cb->args[0]; 1630 bool has_cap_net_admin; 1631 struct nlmsghdr *nlh; 1632 unsigned long id; 1633 u32 index, port = 0; 1634 bool filled = false; 1635 1636 err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1637 nldev_policy, NL_VALIDATE_LIBERAL, NULL); 1638 /* 1639 * Right now, we are expecting the device index to get res information, 1640 * but it is possible to extend this code to return all devices in 1641 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX. 1642 * if it doesn't exist, we will iterate over all devices. 1643 * 1644 * But it is not needed for now. 1645 */ 1646 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1647 return -EINVAL; 1648 1649 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1650 device = ib_device_get_by_index(sock_net(skb->sk), index); 1651 if (!device) 1652 return -EINVAL; 1653 1654 if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]) 1655 show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]); 1656 1657 /* 1658 * If no PORT_INDEX is supplied, we will return all QPs from that device 1659 */ 1660 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 1661 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 1662 if (!rdma_is_port_valid(device, port)) { 1663 ret = -EINVAL; 1664 goto err_index; 1665 } 1666 } 1667 1668 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1669 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 1670 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)), 1671 0, NLM_F_MULTI); 1672 1673 if (!nlh || fill_nldev_handle(skb, device)) { 1674 ret = -EMSGSIZE; 1675 goto err; 1676 } 1677 1678 table_attr = nla_nest_start_noflag(skb, fe->nldev_attr); 1679 if (!table_attr) { 1680 ret = -EMSGSIZE; 1681 goto err; 1682 } 1683 1684 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN); 1685 1686 rt = &device->res[res_type]; 1687 xa_lock(&rt->xa); 1688 /* 1689 * FIXME: if the skip ahead is something common this loop should 1690 * use xas_for_each & xas_pause to optimize, we can have a lot of 1691 * objects. 1692 */ 1693 xa_for_each(&rt->xa, id, res) { 1694 if (xa_get_mark(&rt->xa, res->id, RESTRACK_DD) && !show_details) 1695 goto next; 1696 1697 if (idx < start || !rdma_restrack_get(res)) 1698 goto next; 1699 1700 xa_unlock(&rt->xa); 1701 1702 filled = true; 1703 1704 entry_attr = nla_nest_start_noflag(skb, fe->entry); 1705 if (!entry_attr) { 1706 ret = -EMSGSIZE; 1707 rdma_restrack_put(res); 1708 goto msg_full; 1709 } 1710 1711 ret = fill_func(skb, has_cap_net_admin, res, port); 1712 1713 rdma_restrack_put(res); 1714 1715 if (ret) { 1716 nla_nest_cancel(skb, entry_attr); 1717 if (ret == -EMSGSIZE) 1718 goto msg_full; 1719 if (ret == -EAGAIN) 1720 goto again; 1721 goto res_err; 1722 } 1723 nla_nest_end(skb, entry_attr); 1724 again: xa_lock(&rt->xa); 1725 next: idx++; 1726 } 1727 xa_unlock(&rt->xa); 1728 1729 msg_full: 1730 nla_nest_end(skb, table_attr); 1731 nlmsg_end(skb, nlh); 1732 cb->args[0] = idx; 1733 1734 /* 1735 * No more entries to fill, cancel the message and 1736 * return 0 to mark end of dumpit. 1737 */ 1738 if (!filled) 1739 goto err; 1740 1741 ib_device_put(device); 1742 return skb->len; 1743 1744 res_err: 1745 nla_nest_cancel(skb, table_attr); 1746 1747 err: 1748 nlmsg_cancel(skb, nlh); 1749 1750 err_index: 1751 ib_device_put(device); 1752 return ret; 1753 } 1754 1755 #define RES_GET_FUNCS(name, type) \ 1756 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \ 1757 struct netlink_callback *cb) \ 1758 { \ 1759 return res_get_common_dumpit(skb, cb, type, \ 1760 fill_res_##name##_entry); \ 1761 } \ 1762 static int nldev_res_get_##name##_doit(struct sk_buff *skb, \ 1763 struct nlmsghdr *nlh, \ 1764 struct netlink_ext_ack *extack) \ 1765 { \ 1766 return res_get_common_doit(skb, nlh, extack, type, \ 1767 fill_res_##name##_entry); \ 1768 } 1769 1770 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP); 1771 RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP); 1772 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID); 1773 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ); 1774 RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ); 1775 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD); 1776 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR); 1777 RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR); 1778 RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER); 1779 RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX); 1780 RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ); 1781 RES_GET_FUNCS(srq_raw, RDMA_RESTRACK_SRQ); 1782 1783 static LIST_HEAD(link_ops); 1784 static DECLARE_RWSEM(link_ops_rwsem); 1785 1786 static const struct rdma_link_ops *link_ops_get(const char *type) 1787 { 1788 const struct rdma_link_ops *ops; 1789 1790 list_for_each_entry(ops, &link_ops, list) { 1791 if (!strcmp(ops->type, type)) 1792 goto out; 1793 } 1794 ops = NULL; 1795 out: 1796 return ops; 1797 } 1798 1799 void rdma_link_register(struct rdma_link_ops *ops) 1800 { 1801 down_write(&link_ops_rwsem); 1802 if (WARN_ON_ONCE(link_ops_get(ops->type))) 1803 goto out; 1804 list_add(&ops->list, &link_ops); 1805 out: 1806 up_write(&link_ops_rwsem); 1807 } 1808 EXPORT_SYMBOL(rdma_link_register); 1809 1810 void rdma_link_unregister(struct rdma_link_ops *ops) 1811 { 1812 down_write(&link_ops_rwsem); 1813 list_del(&ops->list); 1814 up_write(&link_ops_rwsem); 1815 } 1816 EXPORT_SYMBOL(rdma_link_unregister); 1817 1818 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 1819 struct netlink_ext_ack *extack) 1820 { 1821 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1822 char ibdev_name[IB_DEVICE_NAME_MAX]; 1823 const struct rdma_link_ops *ops; 1824 char ndev_name[IFNAMSIZ]; 1825 struct net_device *ndev; 1826 char type[IFNAMSIZ]; 1827 int err; 1828 1829 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1830 nldev_policy, extack); 1831 if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || 1832 !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME]) 1833 return -EINVAL; 1834 1835 nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME], 1836 sizeof(ibdev_name)); 1837 if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0) 1838 return -EINVAL; 1839 1840 nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type)); 1841 nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME], 1842 sizeof(ndev_name)); 1843 1844 ndev = dev_get_by_name(sock_net(skb->sk), ndev_name); 1845 if (!ndev) 1846 return -ENODEV; 1847 1848 down_read(&link_ops_rwsem); 1849 ops = link_ops_get(type); 1850 #ifdef CONFIG_MODULES 1851 if (!ops) { 1852 up_read(&link_ops_rwsem); 1853 request_module("rdma-link-%s", type); 1854 down_read(&link_ops_rwsem); 1855 ops = link_ops_get(type); 1856 } 1857 #endif 1858 err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL; 1859 up_read(&link_ops_rwsem); 1860 dev_put(ndev); 1861 1862 return err; 1863 } 1864 1865 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 1866 struct netlink_ext_ack *extack) 1867 { 1868 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1869 struct ib_device *device; 1870 u32 index; 1871 int err; 1872 1873 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1874 nldev_policy, extack); 1875 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1876 return -EINVAL; 1877 1878 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1879 device = ib_device_get_by_index(sock_net(skb->sk), index); 1880 if (!device) 1881 return -EINVAL; 1882 1883 if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) { 1884 ib_device_put(device); 1885 return -EINVAL; 1886 } 1887 1888 /* 1889 * This path is triggered by the 'rdma link delete' administrative command. 1890 * For Soft-RoCE (RXE), we ensure that transport sockets are closed here. 1891 * Note: iWARP driver does not implement .dellink, so this logic is 1892 * implicitly scoped to the driver supporting dynamic link deletion like RXE. 1893 */ 1894 if (device->link_ops && device->link_ops->dellink) { 1895 mutex_lock(&nldev_dellink_mutex); 1896 err = device->link_ops->dellink(device); 1897 mutex_unlock(&nldev_dellink_mutex); 1898 if (err) 1899 return err; 1900 } 1901 1902 ib_unregister_device_and_put(device); 1903 return 0; 1904 } 1905 1906 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh, 1907 struct netlink_ext_ack *extack) 1908 { 1909 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1910 char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE]; 1911 struct ib_client_nl_info data = {}; 1912 struct ib_device *ibdev = NULL; 1913 struct sk_buff *msg; 1914 u32 index; 1915 int err; 1916 1917 err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy, 1918 NL_VALIDATE_LIBERAL, extack); 1919 if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE]) 1920 return -EINVAL; 1921 1922 nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE], 1923 sizeof(client_name)); 1924 1925 if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) { 1926 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1927 ibdev = ib_device_get_by_index(sock_net(skb->sk), index); 1928 if (!ibdev) 1929 return -EINVAL; 1930 1931 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 1932 data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 1933 if (!rdma_is_port_valid(ibdev, data.port)) { 1934 err = -EINVAL; 1935 goto out_put; 1936 } 1937 } else { 1938 data.port = -1; 1939 } 1940 } else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 1941 return -EINVAL; 1942 } 1943 1944 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1945 if (!msg) { 1946 err = -ENOMEM; 1947 goto out_put; 1948 } 1949 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1950 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 1951 RDMA_NLDEV_CMD_GET_CHARDEV), 1952 0, 0); 1953 if (!nlh) { 1954 err = -EMSGSIZE; 1955 goto out_nlmsg; 1956 } 1957 1958 data.nl_msg = msg; 1959 err = ib_get_client_nl_info(ibdev, client_name, &data); 1960 if (err) 1961 goto out_nlmsg; 1962 1963 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV, 1964 huge_encode_dev(data.cdev->devt), 1965 RDMA_NLDEV_ATTR_PAD); 1966 if (err) 1967 goto out_data; 1968 err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi, 1969 RDMA_NLDEV_ATTR_PAD); 1970 if (err) 1971 goto out_data; 1972 if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME, 1973 dev_name(data.cdev))) { 1974 err = -EMSGSIZE; 1975 goto out_data; 1976 } 1977 1978 nlmsg_end(msg, nlh); 1979 put_device(data.cdev); 1980 if (ibdev) 1981 ib_device_put(ibdev); 1982 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 1983 1984 out_data: 1985 put_device(data.cdev); 1986 out_nlmsg: 1987 nlmsg_free(msg); 1988 out_put: 1989 if (ibdev) 1990 ib_device_put(ibdev); 1991 return err; 1992 } 1993 1994 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 1995 struct netlink_ext_ack *extack) 1996 { 1997 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1998 struct sk_buff *msg; 1999 int err; 2000 2001 err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2002 nldev_policy, NL_VALIDATE_LIBERAL, extack); 2003 if (err) 2004 return err; 2005 2006 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2007 if (!msg) 2008 return -ENOMEM; 2009 2010 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2011 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2012 RDMA_NLDEV_CMD_SYS_GET), 2013 0, 0); 2014 if (!nlh) { 2015 nlmsg_free(msg); 2016 return -EMSGSIZE; 2017 } 2018 2019 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE, 2020 (u8)ib_devices_shared_netns); 2021 if (err) { 2022 nlmsg_free(msg); 2023 return err; 2024 } 2025 2026 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE, 2027 (u8)privileged_qkey); 2028 if (err) { 2029 nlmsg_free(msg); 2030 return err; 2031 } 2032 2033 err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_MONITOR_MODE, 1); 2034 if (err) { 2035 nlmsg_free(msg); 2036 return err; 2037 } 2038 /* 2039 * Copy-on-fork is supported. 2040 * See commits: 2041 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes") 2042 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm") 2043 * for more details. Don't backport this without them. 2044 * 2045 * Return value ignored on purpose, assume copy-on-fork is not 2046 * supported in case of failure. 2047 */ 2048 nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1); 2049 2050 nlmsg_end(msg, nlh); 2051 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2052 } 2053 2054 static int nldev_set_sys_set_netns_doit(struct nlattr *tb[]) 2055 { 2056 u8 enable; 2057 int err; 2058 2059 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]); 2060 /* Only 0 and 1 are supported */ 2061 if (enable > 1) 2062 return -EINVAL; 2063 2064 err = rdma_compatdev_set(enable); 2065 return err; 2066 } 2067 2068 static int nldev_set_sys_set_pqkey_doit(struct nlattr *tb[]) 2069 { 2070 u8 enable; 2071 2072 enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE]); 2073 /* Only 0 and 1 are supported */ 2074 if (enable > 1) 2075 return -EINVAL; 2076 2077 privileged_qkey = enable; 2078 return 0; 2079 } 2080 2081 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 2082 struct netlink_ext_ack *extack) 2083 { 2084 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2085 int err; 2086 2087 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2088 nldev_policy, extack); 2089 if (err) 2090 return -EINVAL; 2091 2092 if (tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]) 2093 return nldev_set_sys_set_netns_doit(tb); 2094 2095 if (tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE]) 2096 return nldev_set_sys_set_pqkey_doit(tb); 2097 2098 return -EINVAL; 2099 } 2100 2101 2102 static int nldev_stat_set_mode_doit(struct sk_buff *msg, 2103 struct netlink_ext_ack *extack, 2104 struct nlattr *tb[], 2105 struct ib_device *device, u32 port) 2106 { 2107 u32 mode, mask = 0, qpn, cntn = 0; 2108 bool opcnt = false; 2109 int ret; 2110 2111 /* Currently only counter for QP is supported */ 2112 if (!tb[RDMA_NLDEV_ATTR_STAT_RES] || 2113 nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP) 2114 return -EINVAL; 2115 2116 if (tb[RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED]) 2117 opcnt = !!nla_get_u8( 2118 tb[RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED]); 2119 2120 mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]); 2121 if (mode == RDMA_COUNTER_MODE_AUTO) { 2122 if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]) 2123 mask = nla_get_u32( 2124 tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]); 2125 return rdma_counter_set_auto_mode(device, port, mask, opcnt, 2126 extack); 2127 } 2128 2129 if (!tb[RDMA_NLDEV_ATTR_RES_LQPN]) 2130 return -EINVAL; 2131 2132 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]); 2133 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) { 2134 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]); 2135 ret = rdma_counter_bind_qpn(device, port, qpn, cntn); 2136 if (ret) 2137 return ret; 2138 } else { 2139 ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn); 2140 if (ret) 2141 return ret; 2142 } 2143 2144 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) || 2145 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) { 2146 ret = -EMSGSIZE; 2147 goto err_fill; 2148 } 2149 2150 return 0; 2151 2152 err_fill: 2153 rdma_counter_unbind_qpn(device, port, qpn, cntn); 2154 return ret; 2155 } 2156 2157 static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[], 2158 struct ib_device *device, 2159 u32 port) 2160 { 2161 struct rdma_hw_stats *stats; 2162 struct nlattr *entry_attr; 2163 unsigned long *target; 2164 int rem, i, ret = 0; 2165 u32 index; 2166 2167 stats = ib_get_hw_stats_port(device, port); 2168 if (!stats) 2169 return -EINVAL; 2170 2171 target = kcalloc(BITS_TO_LONGS(stats->num_counters), 2172 sizeof(*stats->is_disabled), GFP_KERNEL); 2173 if (!target) 2174 return -ENOMEM; 2175 2176 nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS], 2177 rem) { 2178 if (nla_len(entry_attr) != sizeof(u32)) { 2179 ret = -EINVAL; 2180 goto out; 2181 } 2182 2183 index = nla_get_u32(entry_attr); 2184 if ((index >= stats->num_counters) || 2185 !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) { 2186 ret = -EINVAL; 2187 goto out; 2188 } 2189 2190 set_bit(index, target); 2191 } 2192 2193 for (i = 0; i < stats->num_counters; i++) { 2194 if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL)) 2195 continue; 2196 2197 ret = rdma_counter_modify(device, port, i, test_bit(i, target)); 2198 if (ret) 2199 goto out; 2200 } 2201 2202 out: 2203 kfree(target); 2204 return ret; 2205 } 2206 2207 static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 2208 struct netlink_ext_ack *extack) 2209 { 2210 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2211 struct ib_device *device; 2212 struct sk_buff *msg; 2213 u32 index, port; 2214 int ret; 2215 2216 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy, 2217 extack); 2218 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || 2219 !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 2220 return -EINVAL; 2221 2222 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2223 device = ib_device_get_by_index(sock_net(skb->sk), index); 2224 if (!device) 2225 return -EINVAL; 2226 2227 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 2228 if (!rdma_is_port_valid(device, port)) { 2229 ret = -EINVAL; 2230 goto err_put_device; 2231 } 2232 2233 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] && 2234 !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) { 2235 ret = -EINVAL; 2236 goto err_put_device; 2237 } 2238 2239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2240 if (!msg) { 2241 ret = -ENOMEM; 2242 goto err_put_device; 2243 } 2244 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2245 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2246 RDMA_NLDEV_CMD_STAT_SET), 2247 0, 0); 2248 if (!nlh || fill_nldev_handle(msg, device) || 2249 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) { 2250 ret = -EMSGSIZE; 2251 goto err_free_msg; 2252 } 2253 2254 if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) { 2255 ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port); 2256 if (ret) 2257 goto err_free_msg; 2258 } 2259 2260 if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) { 2261 ret = nldev_stat_set_counter_dynamic_doit(tb, device, port); 2262 if (ret) 2263 goto err_free_msg; 2264 } 2265 2266 nlmsg_end(msg, nlh); 2267 ib_device_put(device); 2268 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2269 2270 err_free_msg: 2271 nlmsg_free(msg); 2272 err_put_device: 2273 ib_device_put(device); 2274 return ret; 2275 } 2276 2277 static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 2278 struct netlink_ext_ack *extack) 2279 { 2280 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2281 struct ib_device *device; 2282 struct sk_buff *msg; 2283 u32 index, port, qpn, cntn; 2284 int ret; 2285 2286 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2287 nldev_policy, extack); 2288 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] || 2289 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] || 2290 !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] || 2291 !tb[RDMA_NLDEV_ATTR_RES_LQPN]) 2292 return -EINVAL; 2293 2294 if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP) 2295 return -EINVAL; 2296 2297 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2298 device = ib_device_get_by_index(sock_net(skb->sk), index); 2299 if (!device) 2300 return -EINVAL; 2301 2302 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 2303 if (!rdma_is_port_valid(device, port)) { 2304 ret = -EINVAL; 2305 goto err; 2306 } 2307 2308 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2309 if (!msg) { 2310 ret = -ENOMEM; 2311 goto err; 2312 } 2313 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2314 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2315 RDMA_NLDEV_CMD_STAT_SET), 2316 0, 0); 2317 if (!nlh) { 2318 ret = -EMSGSIZE; 2319 goto err_fill; 2320 } 2321 2322 cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]); 2323 qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]); 2324 if (fill_nldev_handle(msg, device) || 2325 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) || 2326 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) || 2327 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) { 2328 ret = -EMSGSIZE; 2329 goto err_fill; 2330 } 2331 2332 ret = rdma_counter_unbind_qpn(device, port, qpn, cntn); 2333 if (ret) 2334 goto err_fill; 2335 2336 nlmsg_end(msg, nlh); 2337 ib_device_put(device); 2338 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2339 2340 err_fill: 2341 nlmsg_free(msg); 2342 err: 2343 ib_device_put(device); 2344 return ret; 2345 } 2346 2347 static noinline_for_stack int 2348 stat_get_doit_default_counter(struct sk_buff *skb, struct nlmsghdr *nlh, 2349 struct netlink_ext_ack *extack, 2350 struct nlattr *tb[]) 2351 { 2352 struct rdma_hw_stats *stats; 2353 struct nlattr *table_attr; 2354 struct ib_device *device; 2355 int ret, num_cnts, i; 2356 struct sk_buff *msg; 2357 u32 index, port; 2358 u64 v; 2359 2360 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 2361 return -EINVAL; 2362 2363 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2364 device = ib_device_get_by_index(sock_net(skb->sk), index); 2365 if (!device) 2366 return -EINVAL; 2367 2368 if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) { 2369 ret = -EINVAL; 2370 goto err; 2371 } 2372 2373 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 2374 stats = ib_get_hw_stats_port(device, port); 2375 if (!stats) { 2376 ret = -EINVAL; 2377 goto err; 2378 } 2379 2380 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2381 if (!msg) { 2382 ret = -ENOMEM; 2383 goto err; 2384 } 2385 2386 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2387 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2388 RDMA_NLDEV_CMD_STAT_GET), 2389 0, 0); 2390 2391 if (!nlh || fill_nldev_handle(msg, device) || 2392 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) { 2393 ret = -EMSGSIZE; 2394 goto err_msg; 2395 } 2396 2397 mutex_lock(&stats->lock); 2398 2399 num_cnts = device->ops.get_hw_stats(device, stats, port, 0); 2400 if (num_cnts < 0) { 2401 ret = -EINVAL; 2402 goto err_stats; 2403 } 2404 2405 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS); 2406 if (!table_attr) { 2407 ret = -EMSGSIZE; 2408 goto err_stats; 2409 } 2410 for (i = 0; i < num_cnts; i++) { 2411 if (test_bit(i, stats->is_disabled)) 2412 continue; 2413 2414 v = stats->value[i] + 2415 rdma_counter_get_hwstat_value(device, port, i); 2416 if (rdma_nl_stat_hwcounter_entry(msg, 2417 stats->descs[i].name, v)) { 2418 ret = -EMSGSIZE; 2419 goto err_table; 2420 } 2421 } 2422 nla_nest_end(msg, table_attr); 2423 2424 mutex_unlock(&stats->lock); 2425 nlmsg_end(msg, nlh); 2426 ib_device_put(device); 2427 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2428 2429 err_table: 2430 nla_nest_cancel(msg, table_attr); 2431 err_stats: 2432 mutex_unlock(&stats->lock); 2433 err_msg: 2434 nlmsg_free(msg); 2435 err: 2436 ib_device_put(device); 2437 return ret; 2438 } 2439 2440 static noinline_for_stack int 2441 stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh, 2442 struct netlink_ext_ack *extack, struct nlattr *tb[]) 2443 2444 { 2445 static enum rdma_nl_counter_mode mode; 2446 static enum rdma_nl_counter_mask mask; 2447 struct ib_device *device; 2448 struct sk_buff *msg; 2449 u32 index, port; 2450 bool opcnt; 2451 int ret; 2452 2453 if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) 2454 return nldev_res_get_counter_doit(skb, nlh, extack); 2455 2456 if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] || 2457 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 2458 return -EINVAL; 2459 2460 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2461 device = ib_device_get_by_index(sock_net(skb->sk), index); 2462 if (!device) 2463 return -EINVAL; 2464 2465 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 2466 if (!rdma_is_port_valid(device, port)) { 2467 ret = -EINVAL; 2468 goto err; 2469 } 2470 2471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2472 if (!msg) { 2473 ret = -ENOMEM; 2474 goto err; 2475 } 2476 2477 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2478 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2479 RDMA_NLDEV_CMD_STAT_GET), 2480 0, 0); 2481 if (!nlh) { 2482 ret = -EMSGSIZE; 2483 goto err_msg; 2484 } 2485 2486 ret = rdma_counter_get_mode(device, port, &mode, &mask, &opcnt); 2487 if (ret) 2488 goto err_msg; 2489 2490 if (fill_nldev_handle(msg, device) || 2491 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) || 2492 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) { 2493 ret = -EMSGSIZE; 2494 goto err_msg; 2495 } 2496 2497 if ((mode == RDMA_COUNTER_MODE_AUTO) && 2498 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) { 2499 ret = -EMSGSIZE; 2500 goto err_msg; 2501 } 2502 2503 if ((mode == RDMA_COUNTER_MODE_AUTO) && 2504 nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED, opcnt)) { 2505 ret = -EMSGSIZE; 2506 goto err_msg; 2507 } 2508 2509 nlmsg_end(msg, nlh); 2510 ib_device_put(device); 2511 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2512 2513 err_msg: 2514 nlmsg_free(msg); 2515 err: 2516 ib_device_put(device); 2517 return ret; 2518 } 2519 2520 static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 2521 struct netlink_ext_ack *extack) 2522 { 2523 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2524 int ret; 2525 2526 ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2527 nldev_policy, NL_VALIDATE_LIBERAL, extack); 2528 if (ret) 2529 return -EINVAL; 2530 2531 if (!tb[RDMA_NLDEV_ATTR_STAT_RES]) 2532 return stat_get_doit_default_counter(skb, nlh, extack, tb); 2533 2534 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) { 2535 case RDMA_NLDEV_ATTR_RES_QP: 2536 ret = stat_get_doit_qp(skb, nlh, extack, tb); 2537 break; 2538 case RDMA_NLDEV_ATTR_RES_MR: 2539 ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR, 2540 fill_stat_mr_entry); 2541 break; 2542 default: 2543 ret = -EINVAL; 2544 break; 2545 } 2546 2547 return ret; 2548 } 2549 2550 static int nldev_stat_get_dumpit(struct sk_buff *skb, 2551 struct netlink_callback *cb) 2552 { 2553 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2554 int ret; 2555 2556 ret = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2557 nldev_policy, NL_VALIDATE_LIBERAL, NULL); 2558 if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES]) 2559 return -EINVAL; 2560 2561 switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) { 2562 case RDMA_NLDEV_ATTR_RES_QP: 2563 ret = nldev_res_get_counter_dumpit(skb, cb); 2564 break; 2565 case RDMA_NLDEV_ATTR_RES_MR: 2566 ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR, 2567 fill_stat_mr_entry); 2568 break; 2569 default: 2570 ret = -EINVAL; 2571 break; 2572 } 2573 2574 return ret; 2575 } 2576 2577 static int nldev_stat_get_counter_status_doit(struct sk_buff *skb, 2578 struct nlmsghdr *nlh, 2579 struct netlink_ext_ack *extack) 2580 { 2581 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry; 2582 struct rdma_hw_stats *stats; 2583 struct ib_device *device; 2584 struct sk_buff *msg; 2585 u32 devid, port; 2586 int ret, i; 2587 2588 ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2589 nldev_policy, NL_VALIDATE_LIBERAL, extack); 2590 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || 2591 !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 2592 return -EINVAL; 2593 2594 devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2595 device = ib_device_get_by_index(sock_net(skb->sk), devid); 2596 if (!device) 2597 return -EINVAL; 2598 2599 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 2600 if (!rdma_is_port_valid(device, port)) { 2601 ret = -EINVAL; 2602 goto err; 2603 } 2604 2605 stats = ib_get_hw_stats_port(device, port); 2606 if (!stats) { 2607 ret = -EINVAL; 2608 goto err; 2609 } 2610 2611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2612 if (!msg) { 2613 ret = -ENOMEM; 2614 goto err; 2615 } 2616 2617 nlh = nlmsg_put( 2618 msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 2619 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS), 2620 0, 0); 2621 2622 ret = -EMSGSIZE; 2623 if (!nlh || fill_nldev_handle(msg, device) || 2624 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) 2625 goto err_msg; 2626 2627 table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS); 2628 if (!table) 2629 goto err_msg; 2630 2631 mutex_lock(&stats->lock); 2632 for (i = 0; i < stats->num_counters; i++) { 2633 entry = nla_nest_start(msg, 2634 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY); 2635 if (!entry) 2636 goto err_msg_table; 2637 2638 if (nla_put_string(msg, 2639 RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME, 2640 stats->descs[i].name) || 2641 nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i)) 2642 goto err_msg_entry; 2643 2644 if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) && 2645 (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC, 2646 !test_bit(i, stats->is_disabled)))) 2647 goto err_msg_entry; 2648 2649 nla_nest_end(msg, entry); 2650 } 2651 mutex_unlock(&stats->lock); 2652 2653 nla_nest_end(msg, table); 2654 nlmsg_end(msg, nlh); 2655 ib_device_put(device); 2656 return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid); 2657 2658 err_msg_entry: 2659 nla_nest_cancel(msg, entry); 2660 err_msg_table: 2661 mutex_unlock(&stats->lock); 2662 nla_nest_cancel(msg, table); 2663 err_msg: 2664 nlmsg_free(msg); 2665 err: 2666 ib_device_put(device); 2667 return ret; 2668 } 2669 2670 static int nldev_newdev(struct sk_buff *skb, struct nlmsghdr *nlh, 2671 struct netlink_ext_ack *extack) 2672 { 2673 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2674 enum rdma_nl_dev_type type; 2675 struct ib_device *parent; 2676 char name[IFNAMSIZ] = {}; 2677 u32 parentid; 2678 int ret; 2679 2680 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2681 nldev_policy, extack); 2682 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || 2683 !tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_TYPE]) 2684 return -EINVAL; 2685 2686 nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], sizeof(name)); 2687 type = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_TYPE]); 2688 parentid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2689 parent = ib_device_get_by_index(sock_net(skb->sk), parentid); 2690 if (!parent) 2691 return -EINVAL; 2692 2693 ret = ib_add_sub_device(parent, type, name); 2694 ib_device_put(parent); 2695 2696 return ret; 2697 } 2698 2699 static int nldev_deldev(struct sk_buff *skb, struct nlmsghdr *nlh, 2700 struct netlink_ext_ack *extack) 2701 { 2702 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2703 struct ib_device *device; 2704 u32 devid; 2705 int ret; 2706 2707 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2708 nldev_policy, extack); 2709 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 2710 return -EINVAL; 2711 2712 devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 2713 device = ib_device_get_by_index(sock_net(skb->sk), devid); 2714 if (!device) 2715 return -EINVAL; 2716 2717 return ib_del_sub_device_and_put(device); 2718 } 2719 2720 static int fill_frmr_pool_key(struct sk_buff *msg, struct ib_frmr_key *key) 2721 { 2722 struct nlattr *key_attr; 2723 2724 key_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY); 2725 if (!key_attr) 2726 return -EMSGSIZE; 2727 2728 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS, key->ats)) 2729 goto err; 2730 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS, 2731 key->access_flags)) 2732 goto err; 2733 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY, 2734 key->vendor_key, RDMA_NLDEV_ATTR_PAD)) 2735 goto err; 2736 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS, 2737 key->num_dma_blocks, RDMA_NLDEV_ATTR_PAD)) 2738 goto err; 2739 2740 if (key->kernel_vendor_key && 2741 nla_put_u64_64bit(msg, 2742 RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY, 2743 key->kernel_vendor_key, RDMA_NLDEV_ATTR_PAD)) 2744 goto err; 2745 2746 nla_nest_end(msg, key_attr); 2747 return 0; 2748 2749 err: 2750 return -EMSGSIZE; 2751 } 2752 2753 static int fill_frmr_pool_entry(struct sk_buff *msg, struct ib_frmr_pool *pool) 2754 { 2755 if (fill_frmr_pool_key(msg, &pool->key)) 2756 return -EMSGSIZE; 2757 2758 spin_lock(&pool->lock); 2759 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_QUEUE_HANDLES, 2760 pool->queue.ci + pool->inactive_queue.ci)) 2761 goto err_unlock; 2762 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_MAX_IN_USE, 2763 pool->max_in_use, RDMA_NLDEV_ATTR_PAD)) 2764 goto err_unlock; 2765 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_IN_USE, 2766 pool->in_use, RDMA_NLDEV_ATTR_PAD)) 2767 goto err_unlock; 2768 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES, 2769 pool->pinned_handles)) 2770 goto err_unlock; 2771 spin_unlock(&pool->lock); 2772 2773 return 0; 2774 2775 err_unlock: 2776 spin_unlock(&pool->lock); 2777 return -EMSGSIZE; 2778 } 2779 2780 static int nldev_frmr_pools_parse_key(struct nlattr *tb[], 2781 struct ib_frmr_key *key, 2782 struct netlink_ext_ack *extack) 2783 { 2784 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS]) 2785 key->ats = nla_get_u8(tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS]); 2786 2787 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS]) 2788 key->access_flags = nla_get_u32( 2789 tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS]); 2790 2791 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY]) 2792 key->vendor_key = nla_get_u64( 2793 tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY]); 2794 2795 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS]) 2796 key->num_dma_blocks = nla_get_u64( 2797 tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS]); 2798 2799 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY]) 2800 return -EINVAL; 2801 2802 return 0; 2803 } 2804 2805 static int nldev_frmr_pools_set_pinned(struct ib_device *device, 2806 struct nlattr *tb[], 2807 struct netlink_ext_ack *extack) 2808 { 2809 struct nlattr *key_tb[RDMA_NLDEV_ATTR_MAX]; 2810 struct ib_frmr_key key = { 0 }; 2811 u32 pinned_handles = 0; 2812 int err = 0; 2813 2814 pinned_handles = 2815 nla_get_u32(tb[RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES]); 2816 2817 if (!tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY]) 2818 return -EINVAL; 2819 2820 err = nla_parse_nested(key_tb, RDMA_NLDEV_ATTR_MAX - 1, 2821 tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY], nldev_policy, 2822 extack); 2823 if (err) 2824 return err; 2825 2826 err = nldev_frmr_pools_parse_key(key_tb, &key, extack); 2827 if (err) 2828 return err; 2829 2830 err = ib_frmr_pools_set_pinned(device, &key, pinned_handles); 2831 2832 return err; 2833 } 2834 2835 static int nldev_frmr_pools_get_dumpit(struct sk_buff *skb, 2836 struct netlink_callback *cb) 2837 { 2838 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 2839 struct ib_frmr_pools *pools; 2840 int err, ret = 0, idx = 0; 2841 struct ib_frmr_pool *pool; 2842 struct nlattr *table_attr; 2843 struct nlattr *entry_attr; 2844 bool show_details = false; 2845 struct ib_device *device; 2846 int start = cb->args[0]; 2847 struct rb_node *node; 2848 struct nlmsghdr *nlh; 2849 bool filled = false; 2850 2851 err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 2852 nldev_policy, NL_VALIDATE_LIBERAL, NULL); 2853 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 2854 return -EINVAL; 2855 2856 device = ib_device_get_by_index( 2857 sock_net(skb->sk), nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX])); 2858 if (!device) 2859 return -EINVAL; 2860 2861 if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]) 2862 show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]); 2863 2864 pools = device->frmr_pools; 2865 if (!pools) { 2866 ib_device_put(device); 2867 return 0; 2868 } 2869 2870 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 2871 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 2872 RDMA_NLDEV_CMD_FRMR_POOLS_GET), 2873 0, NLM_F_MULTI); 2874 2875 if (!nlh || fill_nldev_handle(skb, device)) { 2876 ret = -EMSGSIZE; 2877 goto err; 2878 } 2879 2880 table_attr = nla_nest_start_noflag(skb, RDMA_NLDEV_ATTR_FRMR_POOLS); 2881 if (!table_attr) { 2882 ret = -EMSGSIZE; 2883 goto err; 2884 } 2885 2886 read_lock(&pools->rb_lock); 2887 for (node = rb_first(&pools->rb_root); node; node = rb_next(node)) { 2888 pool = rb_entry(node, struct ib_frmr_pool, node); 2889 if (pool->key.kernel_vendor_key && !show_details) 2890 continue; 2891 2892 if (idx < start) { 2893 idx++; 2894 continue; 2895 } 2896 2897 filled = true; 2898 2899 entry_attr = nla_nest_start_noflag( 2900 skb, RDMA_NLDEV_ATTR_FRMR_POOL_ENTRY); 2901 if (!entry_attr) { 2902 ret = -EMSGSIZE; 2903 goto end_msg; 2904 } 2905 2906 if (fill_frmr_pool_entry(skb, pool)) { 2907 nla_nest_cancel(skb, entry_attr); 2908 ret = -EMSGSIZE; 2909 goto end_msg; 2910 } 2911 2912 nla_nest_end(skb, entry_attr); 2913 idx++; 2914 } 2915 end_msg: 2916 read_unlock(&pools->rb_lock); 2917 2918 nla_nest_end(skb, table_attr); 2919 nlmsg_end(skb, nlh); 2920 cb->args[0] = idx; 2921 2922 /* 2923 * No more entries to fill, cancel the message and 2924 * return 0 to mark end of dumpit. 2925 */ 2926 if (!filled) 2927 goto err; 2928 2929 ib_device_put(device); 2930 return skb->len; 2931 2932 err: 2933 nlmsg_cancel(skb, nlh); 2934 ib_device_put(device); 2935 return ret; 2936 } 2937 2938 static int nldev_frmr_pools_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 2939 struct netlink_ext_ack *extack) 2940 { 2941 struct ib_device *device; 2942 struct nlattr **tb; 2943 u32 aging_period; 2944 int err; 2945 2946 tb = kzalloc_objs(*tb, RDMA_NLDEV_ATTR_MAX, GFP_KERNEL); 2947 if (!tb) 2948 return -ENOMEM; 2949 2950 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy, 2951 extack); 2952 if (err) 2953 goto free_tb; 2954 2955 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX]) { 2956 err = -EINVAL; 2957 goto free_tb; 2958 } 2959 2960 device = ib_device_get_by_index( 2961 sock_net(skb->sk), nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX])); 2962 if (!device) { 2963 err = -EINVAL; 2964 goto free_tb; 2965 } 2966 2967 if (tb[RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD]) { 2968 aging_period = nla_get_u32( 2969 tb[RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD]); 2970 err = ib_frmr_pools_set_aging_period(device, aging_period); 2971 goto done; 2972 } 2973 2974 if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES]) 2975 err = nldev_frmr_pools_set_pinned(device, tb, extack); 2976 2977 done: 2978 ib_device_put(device); 2979 free_tb: 2980 kfree(tb); 2981 return err; 2982 } 2983 2984 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = { 2985 [RDMA_NLDEV_CMD_GET] = { 2986 .doit = nldev_get_doit, 2987 .dump = nldev_get_dumpit, 2988 }, 2989 [RDMA_NLDEV_CMD_GET_CHARDEV] = { 2990 .doit = nldev_get_chardev, 2991 }, 2992 [RDMA_NLDEV_CMD_SET] = { 2993 .doit = nldev_set_doit, 2994 .flags = RDMA_NL_ADMIN_PERM, 2995 }, 2996 [RDMA_NLDEV_CMD_NEWLINK] = { 2997 .doit = nldev_newlink, 2998 .flags = RDMA_NL_ADMIN_PERM, 2999 }, 3000 [RDMA_NLDEV_CMD_DELLINK] = { 3001 .doit = nldev_dellink, 3002 .flags = RDMA_NL_ADMIN_PERM, 3003 }, 3004 [RDMA_NLDEV_CMD_PORT_GET] = { 3005 .doit = nldev_port_get_doit, 3006 .dump = nldev_port_get_dumpit, 3007 }, 3008 [RDMA_NLDEV_CMD_RES_GET] = { 3009 .doit = nldev_res_get_doit, 3010 .dump = nldev_res_get_dumpit, 3011 }, 3012 [RDMA_NLDEV_CMD_RES_QP_GET] = { 3013 .doit = nldev_res_get_qp_doit, 3014 .dump = nldev_res_get_qp_dumpit, 3015 }, 3016 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = { 3017 .doit = nldev_res_get_cm_id_doit, 3018 .dump = nldev_res_get_cm_id_dumpit, 3019 }, 3020 [RDMA_NLDEV_CMD_RES_CQ_GET] = { 3021 .doit = nldev_res_get_cq_doit, 3022 .dump = nldev_res_get_cq_dumpit, 3023 }, 3024 [RDMA_NLDEV_CMD_RES_MR_GET] = { 3025 .doit = nldev_res_get_mr_doit, 3026 .dump = nldev_res_get_mr_dumpit, 3027 }, 3028 [RDMA_NLDEV_CMD_RES_PD_GET] = { 3029 .doit = nldev_res_get_pd_doit, 3030 .dump = nldev_res_get_pd_dumpit, 3031 }, 3032 [RDMA_NLDEV_CMD_RES_CTX_GET] = { 3033 .doit = nldev_res_get_ctx_doit, 3034 .dump = nldev_res_get_ctx_dumpit, 3035 }, 3036 [RDMA_NLDEV_CMD_RES_SRQ_GET] = { 3037 .doit = nldev_res_get_srq_doit, 3038 .dump = nldev_res_get_srq_dumpit, 3039 }, 3040 [RDMA_NLDEV_CMD_SYS_GET] = { 3041 .doit = nldev_sys_get_doit, 3042 }, 3043 [RDMA_NLDEV_CMD_SYS_SET] = { 3044 .doit = nldev_set_sys_set_doit, 3045 .flags = RDMA_NL_ADMIN_PERM, 3046 }, 3047 [RDMA_NLDEV_CMD_STAT_SET] = { 3048 .doit = nldev_stat_set_doit, 3049 .flags = RDMA_NL_ADMIN_PERM, 3050 }, 3051 [RDMA_NLDEV_CMD_STAT_GET] = { 3052 .doit = nldev_stat_get_doit, 3053 .dump = nldev_stat_get_dumpit, 3054 }, 3055 [RDMA_NLDEV_CMD_STAT_DEL] = { 3056 .doit = nldev_stat_del_doit, 3057 .flags = RDMA_NL_ADMIN_PERM, 3058 }, 3059 [RDMA_NLDEV_CMD_RES_QP_GET_RAW] = { 3060 .doit = nldev_res_get_qp_raw_doit, 3061 .dump = nldev_res_get_qp_raw_dumpit, 3062 .flags = RDMA_NL_ADMIN_PERM, 3063 }, 3064 [RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = { 3065 .doit = nldev_res_get_cq_raw_doit, 3066 .dump = nldev_res_get_cq_raw_dumpit, 3067 .flags = RDMA_NL_ADMIN_PERM, 3068 }, 3069 [RDMA_NLDEV_CMD_RES_MR_GET_RAW] = { 3070 .doit = nldev_res_get_mr_raw_doit, 3071 .dump = nldev_res_get_mr_raw_dumpit, 3072 .flags = RDMA_NL_ADMIN_PERM, 3073 }, 3074 [RDMA_NLDEV_CMD_RES_SRQ_GET_RAW] = { 3075 .doit = nldev_res_get_srq_raw_doit, 3076 .dump = nldev_res_get_srq_raw_dumpit, 3077 .flags = RDMA_NL_ADMIN_PERM, 3078 }, 3079 [RDMA_NLDEV_CMD_STAT_GET_STATUS] = { 3080 .doit = nldev_stat_get_counter_status_doit, 3081 }, 3082 [RDMA_NLDEV_CMD_NEWDEV] = { 3083 .doit = nldev_newdev, 3084 .flags = RDMA_NL_ADMIN_PERM, 3085 }, 3086 [RDMA_NLDEV_CMD_DELDEV] = { 3087 .doit = nldev_deldev, 3088 .flags = RDMA_NL_ADMIN_PERM, 3089 }, 3090 [RDMA_NLDEV_CMD_FRMR_POOLS_GET] = { 3091 .dump = nldev_frmr_pools_get_dumpit, 3092 }, 3093 [RDMA_NLDEV_CMD_FRMR_POOLS_SET] = { 3094 .doit = nldev_frmr_pools_set_doit, 3095 .flags = RDMA_NL_ADMIN_PERM, 3096 }, 3097 }; 3098 3099 static int fill_mon_netdev_rename(struct sk_buff *msg, 3100 struct ib_device *device, u32 port, 3101 const struct net *net) 3102 { 3103 struct net_device *netdev = ib_device_get_netdev(device, port); 3104 int ret = 0; 3105 3106 if (!netdev || !net_eq(dev_net(netdev), net)) 3107 goto out; 3108 3109 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex); 3110 if (ret) 3111 goto out; 3112 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name); 3113 out: 3114 dev_put(netdev); 3115 return ret; 3116 } 3117 3118 static int fill_mon_netdev_association(struct sk_buff *msg, 3119 struct ib_device *device, u32 port, 3120 const struct net *net) 3121 { 3122 struct net_device *netdev = ib_device_get_netdev(device, port); 3123 int ret = 0; 3124 3125 if (netdev && !net_eq(dev_net(netdev), net)) 3126 goto out; 3127 3128 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index); 3129 if (ret) 3130 goto out; 3131 3132 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, 3133 dev_name(&device->dev)); 3134 if (ret) 3135 goto out; 3136 3137 ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port); 3138 if (ret) 3139 goto out; 3140 3141 if (netdev) { 3142 ret = nla_put_u32(msg, 3143 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex); 3144 if (ret) 3145 goto out; 3146 3147 ret = nla_put_string(msg, 3148 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name); 3149 } 3150 3151 out: 3152 dev_put(netdev); 3153 return ret; 3154 } 3155 3156 static void rdma_nl_notify_err_msg(struct ib_device *device, u32 port_num, 3157 enum rdma_nl_notify_event_type type) 3158 { 3159 struct net_device *netdev; 3160 3161 switch (type) { 3162 case RDMA_REGISTER_EVENT: 3163 dev_warn_ratelimited(&device->dev, 3164 "Failed to send RDMA monitor register device event\n"); 3165 break; 3166 case RDMA_UNREGISTER_EVENT: 3167 dev_warn_ratelimited(&device->dev, 3168 "Failed to send RDMA monitor unregister device event\n"); 3169 break; 3170 case RDMA_NETDEV_ATTACH_EVENT: 3171 netdev = ib_device_get_netdev(device, port_num); 3172 dev_warn_ratelimited(&device->dev, 3173 "Failed to send RDMA monitor netdev attach event: port %d netdev %d\n", 3174 port_num, netdev->ifindex); 3175 dev_put(netdev); 3176 break; 3177 case RDMA_NETDEV_DETACH_EVENT: 3178 dev_warn_ratelimited(&device->dev, 3179 "Failed to send RDMA monitor netdev detach event: port %d\n", 3180 port_num); 3181 break; 3182 case RDMA_RENAME_EVENT: 3183 dev_warn_ratelimited(&device->dev, 3184 "Failed to send RDMA monitor rename device event\n"); 3185 break; 3186 3187 case RDMA_NETDEV_RENAME_EVENT: 3188 netdev = ib_device_get_netdev(device, port_num); 3189 dev_warn_ratelimited(&device->dev, 3190 "Failed to send RDMA monitor netdev rename event: port %d netdev %d\n", 3191 port_num, netdev->ifindex); 3192 dev_put(netdev); 3193 break; 3194 default: 3195 break; 3196 } 3197 } 3198 3199 int rdma_nl_notify_event(struct ib_device *device, u32 port_num, 3200 enum rdma_nl_notify_event_type type) 3201 { 3202 struct sk_buff *skb; 3203 int ret = -EMSGSIZE; 3204 struct net *net; 3205 void *nlh; 3206 3207 net = read_pnet(&device->coredev.rdma_net); 3208 if (!net) 3209 return -EINVAL; 3210 3211 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 3212 if (!skb) 3213 return -ENOMEM; 3214 nlh = nlmsg_put(skb, 0, 0, 3215 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_MONITOR), 3216 0, 0); 3217 if (!nlh) 3218 goto err_free; 3219 3220 switch (type) { 3221 case RDMA_REGISTER_EVENT: 3222 case RDMA_UNREGISTER_EVENT: 3223 case RDMA_RENAME_EVENT: 3224 ret = fill_nldev_handle(skb, device); 3225 if (ret) 3226 goto err_free; 3227 break; 3228 case RDMA_NETDEV_ATTACH_EVENT: 3229 case RDMA_NETDEV_DETACH_EVENT: 3230 ret = fill_mon_netdev_association(skb, device, port_num, net); 3231 if (ret) 3232 goto err_free; 3233 break; 3234 case RDMA_NETDEV_RENAME_EVENT: 3235 ret = fill_mon_netdev_rename(skb, device, port_num, net); 3236 if (ret) 3237 goto err_free; 3238 break; 3239 default: 3240 break; 3241 } 3242 3243 ret = nla_put_u8(skb, RDMA_NLDEV_ATTR_EVENT_TYPE, type); 3244 if (ret) 3245 goto err_free; 3246 3247 nlmsg_end(skb, nlh); 3248 ret = rdma_nl_multicast(net, skb, RDMA_NL_GROUP_NOTIFY, GFP_KERNEL); 3249 if (ret && ret != -ESRCH) { 3250 skb = NULL; /* skb is freed in the netlink send-op handling */ 3251 goto err_free; 3252 } 3253 return 0; 3254 3255 err_free: 3256 rdma_nl_notify_err_msg(device, port_num, type); 3257 nlmsg_free(skb); 3258 return ret; 3259 } 3260 3261 void __init nldev_init(void) 3262 { 3263 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table); 3264 } 3265 3266 void nldev_exit(void) 3267 { 3268 rdma_nl_unregister(RDMA_NL_NLDEV); 3269 } 3270 3271 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5); 3272