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 41 #include "core_priv.h" 42 #include "cma_priv.h" 43 #include "restrack.h" 44 45 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = { 46 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 }, 47 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, 48 .len = IB_DEVICE_NAME_MAX - 1}, 49 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 }, 50 [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING, 51 .len = IB_FW_VERSION_NAME_MAX - 1}, 52 [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 }, 53 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 }, 54 [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 }, 55 [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 }, 56 [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 }, 57 [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 }, 58 [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 }, 59 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 }, 60 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 }, 61 [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED }, 62 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED }, 63 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = { .type = NLA_NUL_STRING, 64 .len = 16 }, 65 [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 }, 66 [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED }, 67 [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED }, 68 [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 }, 69 [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 }, 70 [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 }, 71 [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 }, 72 [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 }, 73 [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 }, 74 [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 }, 75 [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 }, 76 [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING, 77 .len = TASK_COMM_LEN }, 78 [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED }, 79 [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED }, 80 [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 }, 81 [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = { 82 .len = sizeof(struct __kernel_sockaddr_storage) }, 83 [RDMA_NLDEV_ATTR_RES_DST_ADDR] = { 84 .len = sizeof(struct __kernel_sockaddr_storage) }, 85 [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED }, 86 [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED }, 87 [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 }, 88 [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 }, 89 [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 }, 90 [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED }, 91 [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED }, 92 [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 }, 93 [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 }, 94 [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 }, 95 [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 }, 96 [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED }, 97 [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED }, 98 [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 }, 99 [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY] = { .type = NLA_U32 }, 100 [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 }, 101 [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING, 102 .len = IFNAMSIZ }, 103 [RDMA_NLDEV_ATTR_DRIVER] = { .type = NLA_NESTED }, 104 [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = { .type = NLA_NESTED }, 105 [RDMA_NLDEV_ATTR_DRIVER_STRING] = { .type = NLA_NUL_STRING, 106 .len = RDMA_NLDEV_ATTR_ENTRY_STRLEN }, 107 [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = { .type = NLA_U8 }, 108 [RDMA_NLDEV_ATTR_DRIVER_S32] = { .type = NLA_S32 }, 109 [RDMA_NLDEV_ATTR_DRIVER_U32] = { .type = NLA_U32 }, 110 [RDMA_NLDEV_ATTR_DRIVER_S64] = { .type = NLA_S64 }, 111 [RDMA_NLDEV_ATTR_DRIVER_U64] = { .type = NLA_U64 }, 112 [RDMA_NLDEV_ATTR_RES_PDN] = { .type = NLA_U32 }, 113 [RDMA_NLDEV_ATTR_RES_CQN] = { .type = NLA_U32 }, 114 [RDMA_NLDEV_ATTR_RES_MRN] = { .type = NLA_U32 }, 115 [RDMA_NLDEV_ATTR_RES_CM_IDN] = { .type = NLA_U32 }, 116 [RDMA_NLDEV_ATTR_RES_CTXN] = { .type = NLA_U32 }, 117 [RDMA_NLDEV_ATTR_LINK_TYPE] = { .type = NLA_NUL_STRING, 118 .len = RDMA_NLDEV_ATTR_ENTRY_STRLEN }, 119 }; 120 121 static int put_driver_name_print_type(struct sk_buff *msg, const char *name, 122 enum rdma_nldev_print_type print_type) 123 { 124 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name)) 125 return -EMSGSIZE; 126 if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC && 127 nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type)) 128 return -EMSGSIZE; 129 130 return 0; 131 } 132 133 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, 134 enum rdma_nldev_print_type print_type, 135 u32 value) 136 { 137 if (put_driver_name_print_type(msg, name, print_type)) 138 return -EMSGSIZE; 139 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value)) 140 return -EMSGSIZE; 141 142 return 0; 143 } 144 145 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, 146 enum rdma_nldev_print_type print_type, 147 u64 value) 148 { 149 if (put_driver_name_print_type(msg, name, print_type)) 150 return -EMSGSIZE; 151 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value, 152 RDMA_NLDEV_ATTR_PAD)) 153 return -EMSGSIZE; 154 155 return 0; 156 } 157 158 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value) 159 { 160 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC, 161 value); 162 } 163 EXPORT_SYMBOL(rdma_nl_put_driver_u32); 164 165 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, 166 u32 value) 167 { 168 return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX, 169 value); 170 } 171 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex); 172 173 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value) 174 { 175 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC, 176 value); 177 } 178 EXPORT_SYMBOL(rdma_nl_put_driver_u64); 179 180 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value) 181 { 182 return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX, 183 value); 184 } 185 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex); 186 187 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device) 188 { 189 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index)) 190 return -EMSGSIZE; 191 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, 192 dev_name(&device->dev))) 193 return -EMSGSIZE; 194 195 return 0; 196 } 197 198 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device) 199 { 200 char fw[IB_FW_VERSION_NAME_MAX]; 201 202 if (fill_nldev_handle(msg, device)) 203 return -EMSGSIZE; 204 205 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device))) 206 return -EMSGSIZE; 207 208 BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64)); 209 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS, 210 device->attrs.device_cap_flags, 211 RDMA_NLDEV_ATTR_PAD)) 212 return -EMSGSIZE; 213 214 ib_get_device_fw_str(device, fw); 215 /* Device without FW has strlen(fw) = 0 */ 216 if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw)) 217 return -EMSGSIZE; 218 219 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID, 220 be64_to_cpu(device->node_guid), 221 RDMA_NLDEV_ATTR_PAD)) 222 return -EMSGSIZE; 223 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID, 224 be64_to_cpu(device->attrs.sys_image_guid), 225 RDMA_NLDEV_ATTR_PAD)) 226 return -EMSGSIZE; 227 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type)) 228 return -EMSGSIZE; 229 return 0; 230 } 231 232 static int fill_port_info(struct sk_buff *msg, 233 struct ib_device *device, u32 port, 234 const struct net *net) 235 { 236 struct net_device *netdev = NULL; 237 struct ib_port_attr attr; 238 int ret; 239 u64 cap_flags = 0; 240 241 if (fill_nldev_handle(msg, device)) 242 return -EMSGSIZE; 243 244 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) 245 return -EMSGSIZE; 246 247 ret = ib_query_port(device, port, &attr); 248 if (ret) 249 return ret; 250 251 if (rdma_protocol_ib(device, port)) { 252 BUILD_BUG_ON((sizeof(attr.port_cap_flags) + 253 sizeof(attr.port_cap_flags2)) > sizeof(u64)); 254 cap_flags = attr.port_cap_flags | 255 ((u64)attr.port_cap_flags2 << 32); 256 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS, 257 cap_flags, RDMA_NLDEV_ATTR_PAD)) 258 return -EMSGSIZE; 259 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX, 260 attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD)) 261 return -EMSGSIZE; 262 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid)) 263 return -EMSGSIZE; 264 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid)) 265 return -EMSGSIZE; 266 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc)) 267 return -EMSGSIZE; 268 } 269 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state)) 270 return -EMSGSIZE; 271 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state)) 272 return -EMSGSIZE; 273 274 netdev = ib_device_get_netdev(device, port); 275 if (netdev && net_eq(dev_net(netdev), net)) { 276 ret = nla_put_u32(msg, 277 RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex); 278 if (ret) 279 goto out; 280 ret = nla_put_string(msg, 281 RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name); 282 } 283 284 out: 285 if (netdev) 286 dev_put(netdev); 287 return ret; 288 } 289 290 static int fill_res_info_entry(struct sk_buff *msg, 291 const char *name, u64 curr) 292 { 293 struct nlattr *entry_attr; 294 295 entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY); 296 if (!entry_attr) 297 return -EMSGSIZE; 298 299 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name)) 300 goto err; 301 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr, 302 RDMA_NLDEV_ATTR_PAD)) 303 goto err; 304 305 nla_nest_end(msg, entry_attr); 306 return 0; 307 308 err: 309 nla_nest_cancel(msg, entry_attr); 310 return -EMSGSIZE; 311 } 312 313 static int fill_res_info(struct sk_buff *msg, struct ib_device *device) 314 { 315 static const char * const names[RDMA_RESTRACK_MAX] = { 316 [RDMA_RESTRACK_PD] = "pd", 317 [RDMA_RESTRACK_CQ] = "cq", 318 [RDMA_RESTRACK_QP] = "qp", 319 [RDMA_RESTRACK_CM_ID] = "cm_id", 320 [RDMA_RESTRACK_MR] = "mr", 321 [RDMA_RESTRACK_CTX] = "ctx", 322 }; 323 324 struct nlattr *table_attr; 325 int ret, i, curr; 326 327 if (fill_nldev_handle(msg, device)) 328 return -EMSGSIZE; 329 330 table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY); 331 if (!table_attr) 332 return -EMSGSIZE; 333 334 for (i = 0; i < RDMA_RESTRACK_MAX; i++) { 335 if (!names[i]) 336 continue; 337 curr = rdma_restrack_count(device, i, 338 task_active_pid_ns(current)); 339 ret = fill_res_info_entry(msg, names[i], curr); 340 if (ret) 341 goto err; 342 } 343 344 nla_nest_end(msg, table_attr); 345 return 0; 346 347 err: 348 nla_nest_cancel(msg, table_attr); 349 return ret; 350 } 351 352 static int fill_res_name_pid(struct sk_buff *msg, 353 struct rdma_restrack_entry *res) 354 { 355 /* 356 * For user resources, user is should read /proc/PID/comm to get the 357 * name of the task file. 358 */ 359 if (rdma_is_kernel_res(res)) { 360 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME, 361 res->kern_name)) 362 return -EMSGSIZE; 363 } else { 364 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, 365 task_pid_vnr(res->task))) 366 return -EMSGSIZE; 367 } 368 return 0; 369 } 370 371 static bool fill_res_entry(struct ib_device *dev, struct sk_buff *msg, 372 struct rdma_restrack_entry *res) 373 { 374 if (!dev->ops.fill_res_entry) 375 return false; 376 return dev->ops.fill_res_entry(msg, res); 377 } 378 379 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin, 380 struct rdma_restrack_entry *res, uint32_t port) 381 { 382 struct ib_qp *qp = container_of(res, struct ib_qp, res); 383 struct ib_device *dev = qp->device; 384 struct ib_qp_init_attr qp_init_attr; 385 struct ib_qp_attr qp_attr; 386 int ret; 387 388 ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr); 389 if (ret) 390 return ret; 391 392 if (port && port != qp_attr.port_num) 393 return -EAGAIN; 394 395 /* In create_qp() port is not set yet */ 396 if (qp_attr.port_num && 397 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num)) 398 goto err; 399 400 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num)) 401 goto err; 402 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) { 403 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN, 404 qp_attr.dest_qp_num)) 405 goto err; 406 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN, 407 qp_attr.rq_psn)) 408 goto err; 409 } 410 411 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn)) 412 goto err; 413 414 if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC || 415 qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) { 416 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE, 417 qp_attr.path_mig_state)) 418 goto err; 419 } 420 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type)) 421 goto err; 422 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state)) 423 goto err; 424 425 if (!rdma_is_kernel_res(res) && 426 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id)) 427 goto err; 428 429 if (fill_res_name_pid(msg, res)) 430 goto err; 431 432 if (fill_res_entry(dev, msg, res)) 433 goto err; 434 435 return 0; 436 437 err: return -EMSGSIZE; 438 } 439 440 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin, 441 struct rdma_restrack_entry *res, uint32_t port) 442 { 443 struct rdma_id_private *id_priv = 444 container_of(res, struct rdma_id_private, res); 445 struct ib_device *dev = id_priv->id.device; 446 struct rdma_cm_id *cm_id = &id_priv->id; 447 448 if (port && port != cm_id->port_num) 449 return 0; 450 451 if (cm_id->port_num && 452 nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num)) 453 goto err; 454 455 if (id_priv->qp_num) { 456 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num)) 457 goto err; 458 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type)) 459 goto err; 460 } 461 462 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps)) 463 goto err; 464 465 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state)) 466 goto err; 467 468 if (cm_id->route.addr.src_addr.ss_family && 469 nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR, 470 sizeof(cm_id->route.addr.src_addr), 471 &cm_id->route.addr.src_addr)) 472 goto err; 473 if (cm_id->route.addr.dst_addr.ss_family && 474 nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR, 475 sizeof(cm_id->route.addr.dst_addr), 476 &cm_id->route.addr.dst_addr)) 477 goto err; 478 479 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id)) 480 goto err; 481 482 if (fill_res_name_pid(msg, res)) 483 goto err; 484 485 if (fill_res_entry(dev, msg, res)) 486 goto err; 487 488 return 0; 489 490 err: return -EMSGSIZE; 491 } 492 493 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin, 494 struct rdma_restrack_entry *res, uint32_t port) 495 { 496 struct ib_cq *cq = container_of(res, struct ib_cq, res); 497 struct ib_device *dev = cq->device; 498 499 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe)) 500 goto err; 501 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT, 502 atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD)) 503 goto err; 504 505 /* Poll context is only valid for kernel CQs */ 506 if (rdma_is_kernel_res(res) && 507 nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx)) 508 goto err; 509 510 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id)) 511 goto err; 512 if (!rdma_is_kernel_res(res) && 513 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, 514 cq->uobject->context->res.id)) 515 goto err; 516 517 if (fill_res_name_pid(msg, res)) 518 goto err; 519 520 if (fill_res_entry(dev, msg, res)) 521 goto err; 522 523 return 0; 524 525 err: return -EMSGSIZE; 526 } 527 528 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin, 529 struct rdma_restrack_entry *res, uint32_t port) 530 { 531 struct ib_mr *mr = container_of(res, struct ib_mr, res); 532 struct ib_device *dev = mr->pd->device; 533 534 if (has_cap_net_admin) { 535 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey)) 536 goto err; 537 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey)) 538 goto err; 539 } 540 541 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length, 542 RDMA_NLDEV_ATTR_PAD)) 543 goto err; 544 545 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id)) 546 goto err; 547 548 if (!rdma_is_kernel_res(res) && 549 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id)) 550 goto err; 551 552 if (fill_res_name_pid(msg, res)) 553 goto err; 554 555 if (fill_res_entry(dev, msg, res)) 556 goto err; 557 558 return 0; 559 560 err: return -EMSGSIZE; 561 } 562 563 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin, 564 struct rdma_restrack_entry *res, uint32_t port) 565 { 566 struct ib_pd *pd = container_of(res, struct ib_pd, res); 567 struct ib_device *dev = pd->device; 568 569 if (has_cap_net_admin) { 570 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY, 571 pd->local_dma_lkey)) 572 goto err; 573 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) && 574 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY, 575 pd->unsafe_global_rkey)) 576 goto err; 577 } 578 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT, 579 atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD)) 580 goto err; 581 582 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id)) 583 goto err; 584 585 if (!rdma_is_kernel_res(res) && 586 nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, 587 pd->uobject->context->res.id)) 588 goto err; 589 590 if (fill_res_name_pid(msg, res)) 591 goto err; 592 593 if (fill_res_entry(dev, msg, res)) 594 goto err; 595 596 return 0; 597 598 err: return -EMSGSIZE; 599 } 600 601 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 602 struct netlink_ext_ack *extack) 603 { 604 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 605 struct ib_device *device; 606 struct sk_buff *msg; 607 u32 index; 608 int err; 609 610 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 611 nldev_policy, extack); 612 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 613 return -EINVAL; 614 615 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 616 617 device = ib_device_get_by_index(index); 618 if (!device) 619 return -EINVAL; 620 621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 622 if (!msg) { 623 err = -ENOMEM; 624 goto err; 625 } 626 627 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 628 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 629 0, 0); 630 631 err = fill_dev_info(msg, device); 632 if (err) 633 goto err_free; 634 635 nlmsg_end(msg, nlh); 636 637 ib_device_put(device); 638 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); 639 640 err_free: 641 nlmsg_free(msg); 642 err: 643 ib_device_put(device); 644 return err; 645 } 646 647 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 648 struct netlink_ext_ack *extack) 649 { 650 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 651 struct ib_device *device; 652 u32 index; 653 int err; 654 655 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy, 656 extack); 657 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 658 return -EINVAL; 659 660 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 661 device = ib_device_get_by_index(index); 662 if (!device) 663 return -EINVAL; 664 665 if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) { 666 char name[IB_DEVICE_NAME_MAX] = {}; 667 668 nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], 669 IB_DEVICE_NAME_MAX); 670 err = ib_device_rename(device, name); 671 } 672 673 ib_device_put(device); 674 return err; 675 } 676 677 static int _nldev_get_dumpit(struct ib_device *device, 678 struct sk_buff *skb, 679 struct netlink_callback *cb, 680 unsigned int idx) 681 { 682 int start = cb->args[0]; 683 struct nlmsghdr *nlh; 684 685 if (idx < start) 686 return 0; 687 688 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 689 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 690 0, NLM_F_MULTI); 691 692 if (fill_dev_info(skb, device)) { 693 nlmsg_cancel(skb, nlh); 694 goto out; 695 } 696 697 nlmsg_end(skb, nlh); 698 699 idx++; 700 701 out: cb->args[0] = idx; 702 return skb->len; 703 } 704 705 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 706 { 707 /* 708 * There is no need to take lock, because 709 * we are relying on ib_core's lists_rwsem 710 */ 711 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb); 712 } 713 714 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 715 struct netlink_ext_ack *extack) 716 { 717 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 718 struct ib_device *device; 719 struct sk_buff *msg; 720 u32 index; 721 u32 port; 722 int err; 723 724 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 725 nldev_policy, extack); 726 if (err || 727 !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || 728 !tb[RDMA_NLDEV_ATTR_PORT_INDEX]) 729 return -EINVAL; 730 731 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 732 device = ib_device_get_by_index(index); 733 if (!device) 734 return -EINVAL; 735 736 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 737 if (!rdma_is_port_valid(device, port)) { 738 err = -EINVAL; 739 goto err; 740 } 741 742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 743 if (!msg) { 744 err = -ENOMEM; 745 goto err; 746 } 747 748 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 749 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET), 750 0, 0); 751 752 err = fill_port_info(msg, device, port, sock_net(skb->sk)); 753 if (err) 754 goto err_free; 755 756 nlmsg_end(msg, nlh); 757 ib_device_put(device); 758 759 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); 760 761 err_free: 762 nlmsg_free(msg); 763 err: 764 ib_device_put(device); 765 return err; 766 } 767 768 static int nldev_port_get_dumpit(struct sk_buff *skb, 769 struct netlink_callback *cb) 770 { 771 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 772 struct ib_device *device; 773 int start = cb->args[0]; 774 struct nlmsghdr *nlh; 775 u32 idx = 0; 776 u32 ifindex; 777 int err; 778 unsigned int p; 779 780 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 781 nldev_policy, NULL); 782 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 783 return -EINVAL; 784 785 ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 786 device = ib_device_get_by_index(ifindex); 787 if (!device) 788 return -EINVAL; 789 790 rdma_for_each_port (device, p) { 791 /* 792 * The dumpit function returns all information from specific 793 * index. This specific index is taken from the netlink 794 * messages request sent by user and it is available 795 * in cb->args[0]. 796 * 797 * Usually, the user doesn't fill this field and it causes 798 * to return everything. 799 * 800 */ 801 if (idx < start) { 802 idx++; 803 continue; 804 } 805 806 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 807 cb->nlh->nlmsg_seq, 808 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, 809 RDMA_NLDEV_CMD_PORT_GET), 810 0, NLM_F_MULTI); 811 812 if (fill_port_info(skb, device, p, sock_net(skb->sk))) { 813 nlmsg_cancel(skb, nlh); 814 goto out; 815 } 816 idx++; 817 nlmsg_end(skb, nlh); 818 } 819 820 out: 821 ib_device_put(device); 822 cb->args[0] = idx; 823 return skb->len; 824 } 825 826 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 827 struct netlink_ext_ack *extack) 828 { 829 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 830 struct ib_device *device; 831 struct sk_buff *msg; 832 u32 index; 833 int ret; 834 835 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 836 nldev_policy, extack); 837 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 838 return -EINVAL; 839 840 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 841 device = ib_device_get_by_index(index); 842 if (!device) 843 return -EINVAL; 844 845 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 846 if (!msg) { 847 ret = -ENOMEM; 848 goto err; 849 } 850 851 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 852 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET), 853 0, 0); 854 855 ret = fill_res_info(msg, device); 856 if (ret) 857 goto err_free; 858 859 nlmsg_end(msg, nlh); 860 ib_device_put(device); 861 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); 862 863 err_free: 864 nlmsg_free(msg); 865 err: 866 ib_device_put(device); 867 return ret; 868 } 869 870 static int _nldev_res_get_dumpit(struct ib_device *device, 871 struct sk_buff *skb, 872 struct netlink_callback *cb, 873 unsigned int idx) 874 { 875 int start = cb->args[0]; 876 struct nlmsghdr *nlh; 877 878 if (idx < start) 879 return 0; 880 881 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 882 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET), 883 0, NLM_F_MULTI); 884 885 if (fill_res_info(skb, device)) { 886 nlmsg_cancel(skb, nlh); 887 goto out; 888 } 889 890 nlmsg_end(skb, nlh); 891 892 idx++; 893 894 out: 895 cb->args[0] = idx; 896 return skb->len; 897 } 898 899 static int nldev_res_get_dumpit(struct sk_buff *skb, 900 struct netlink_callback *cb) 901 { 902 return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb); 903 } 904 905 struct nldev_fill_res_entry { 906 int (*fill_res_func)(struct sk_buff *msg, bool has_cap_net_admin, 907 struct rdma_restrack_entry *res, u32 port); 908 enum rdma_nldev_attr nldev_attr; 909 enum rdma_nldev_command nldev_cmd; 910 u8 flags; 911 u32 entry; 912 u32 id; 913 }; 914 915 enum nldev_res_flags { 916 NLDEV_PER_DEV = 1 << 0, 917 }; 918 919 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = { 920 [RDMA_RESTRACK_QP] = { 921 .fill_res_func = fill_res_qp_entry, 922 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET, 923 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP, 924 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY, 925 .id = RDMA_NLDEV_ATTR_RES_LQPN, 926 }, 927 [RDMA_RESTRACK_CM_ID] = { 928 .fill_res_func = fill_res_cm_id_entry, 929 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET, 930 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID, 931 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY, 932 .id = RDMA_NLDEV_ATTR_RES_CM_IDN, 933 }, 934 [RDMA_RESTRACK_CQ] = { 935 .fill_res_func = fill_res_cq_entry, 936 .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET, 937 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ, 938 .flags = NLDEV_PER_DEV, 939 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY, 940 .id = RDMA_NLDEV_ATTR_RES_CQN, 941 }, 942 [RDMA_RESTRACK_MR] = { 943 .fill_res_func = fill_res_mr_entry, 944 .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET, 945 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR, 946 .flags = NLDEV_PER_DEV, 947 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY, 948 .id = RDMA_NLDEV_ATTR_RES_MRN, 949 }, 950 [RDMA_RESTRACK_PD] = { 951 .fill_res_func = fill_res_pd_entry, 952 .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET, 953 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD, 954 .flags = NLDEV_PER_DEV, 955 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY, 956 .id = RDMA_NLDEV_ATTR_RES_PDN, 957 }, 958 }; 959 960 static bool is_visible_in_pid_ns(struct rdma_restrack_entry *res) 961 { 962 /* 963 * 1. Kern resources should be visible in init name space only 964 * 2. Present only resources visible in the current namespace 965 */ 966 if (rdma_is_kernel_res(res)) 967 return task_active_pid_ns(current) == &init_pid_ns; 968 return task_active_pid_ns(current) == task_active_pid_ns(res->task); 969 } 970 971 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh, 972 struct netlink_ext_ack *extack, 973 enum rdma_restrack_type res_type) 974 { 975 const struct nldev_fill_res_entry *fe = &fill_entries[res_type]; 976 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 977 struct rdma_restrack_entry *res; 978 struct ib_device *device; 979 u32 index, id, port = 0; 980 bool has_cap_net_admin; 981 struct sk_buff *msg; 982 int ret; 983 984 ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 985 nldev_policy, extack); 986 if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id]) 987 return -EINVAL; 988 989 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 990 device = ib_device_get_by_index(index); 991 if (!device) 992 return -EINVAL; 993 994 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 995 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 996 if (!rdma_is_port_valid(device, port)) { 997 ret = -EINVAL; 998 goto err; 999 } 1000 } 1001 1002 if ((port && fe->flags & NLDEV_PER_DEV) || 1003 (!port && ~fe->flags & NLDEV_PER_DEV)) { 1004 ret = -EINVAL; 1005 goto err; 1006 } 1007 1008 id = nla_get_u32(tb[fe->id]); 1009 res = rdma_restrack_get_byid(device, res_type, id); 1010 if (IS_ERR(res)) { 1011 ret = PTR_ERR(res); 1012 goto err; 1013 } 1014 1015 if (!is_visible_in_pid_ns(res)) { 1016 ret = -ENOENT; 1017 goto err_get; 1018 } 1019 1020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1021 if (!msg) { 1022 ret = -ENOMEM; 1023 goto err; 1024 } 1025 1026 nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 1027 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd), 1028 0, 0); 1029 1030 if (fill_nldev_handle(msg, device)) { 1031 ret = -EMSGSIZE; 1032 goto err_free; 1033 } 1034 1035 has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN); 1036 ret = fe->fill_res_func(msg, has_cap_net_admin, res, port); 1037 rdma_restrack_put(res); 1038 if (ret) 1039 goto err_free; 1040 1041 nlmsg_end(msg, nlh); 1042 ib_device_put(device); 1043 return rdma_nl_unicast(msg, NETLINK_CB(skb).portid); 1044 1045 err_free: 1046 nlmsg_free(msg); 1047 err_get: 1048 rdma_restrack_put(res); 1049 err: 1050 ib_device_put(device); 1051 return ret; 1052 } 1053 1054 static int res_get_common_dumpit(struct sk_buff *skb, 1055 struct netlink_callback *cb, 1056 enum rdma_restrack_type res_type) 1057 { 1058 const struct nldev_fill_res_entry *fe = &fill_entries[res_type]; 1059 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1060 struct rdma_restrack_entry *res; 1061 struct rdma_restrack_root *rt; 1062 int err, ret = 0, idx = 0; 1063 struct nlattr *table_attr; 1064 struct nlattr *entry_attr; 1065 struct ib_device *device; 1066 int start = cb->args[0]; 1067 bool has_cap_net_admin; 1068 struct nlmsghdr *nlh; 1069 unsigned long id; 1070 u32 index, port = 0; 1071 bool filled = false; 1072 1073 err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1074 nldev_policy, NULL); 1075 /* 1076 * Right now, we are expecting the device index to get res information, 1077 * but it is possible to extend this code to return all devices in 1078 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX. 1079 * if it doesn't exist, we will iterate over all devices. 1080 * 1081 * But it is not needed for now. 1082 */ 1083 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1084 return -EINVAL; 1085 1086 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1087 device = ib_device_get_by_index(index); 1088 if (!device) 1089 return -EINVAL; 1090 1091 /* 1092 * If no PORT_INDEX is supplied, we will return all QPs from that device 1093 */ 1094 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { 1095 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); 1096 if (!rdma_is_port_valid(device, port)) { 1097 ret = -EINVAL; 1098 goto err_index; 1099 } 1100 } 1101 1102 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1103 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd), 1104 0, NLM_F_MULTI); 1105 1106 if (fill_nldev_handle(skb, device)) { 1107 ret = -EMSGSIZE; 1108 goto err; 1109 } 1110 1111 table_attr = nla_nest_start(skb, fe->nldev_attr); 1112 if (!table_attr) { 1113 ret = -EMSGSIZE; 1114 goto err; 1115 } 1116 1117 has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN); 1118 1119 rt = &device->res[res_type]; 1120 xa_lock(&rt->xa); 1121 /* 1122 * FIXME: if the skip ahead is something common this loop should 1123 * use xas_for_each & xas_pause to optimize, we can have a lot of 1124 * objects. 1125 */ 1126 xa_for_each(&rt->xa, id, res) { 1127 if (!is_visible_in_pid_ns(res)) 1128 continue; 1129 1130 if (idx < start || !rdma_restrack_get(res)) 1131 goto next; 1132 1133 xa_unlock(&rt->xa); 1134 1135 filled = true; 1136 1137 entry_attr = nla_nest_start(skb, fe->entry); 1138 if (!entry_attr) { 1139 ret = -EMSGSIZE; 1140 rdma_restrack_put(res); 1141 goto msg_full; 1142 } 1143 1144 ret = fe->fill_res_func(skb, has_cap_net_admin, res, port); 1145 rdma_restrack_put(res); 1146 1147 if (ret) { 1148 nla_nest_cancel(skb, entry_attr); 1149 if (ret == -EMSGSIZE) 1150 goto msg_full; 1151 if (ret == -EAGAIN) 1152 goto again; 1153 goto res_err; 1154 } 1155 nla_nest_end(skb, entry_attr); 1156 again: xa_lock(&rt->xa); 1157 next: idx++; 1158 } 1159 xa_unlock(&rt->xa); 1160 1161 msg_full: 1162 nla_nest_end(skb, table_attr); 1163 nlmsg_end(skb, nlh); 1164 cb->args[0] = idx; 1165 1166 /* 1167 * No more entries to fill, cancel the message and 1168 * return 0 to mark end of dumpit. 1169 */ 1170 if (!filled) 1171 goto err; 1172 1173 ib_device_put(device); 1174 return skb->len; 1175 1176 res_err: 1177 nla_nest_cancel(skb, table_attr); 1178 1179 err: 1180 nlmsg_cancel(skb, nlh); 1181 1182 err_index: 1183 ib_device_put(device); 1184 return ret; 1185 } 1186 1187 #define RES_GET_FUNCS(name, type) \ 1188 static int nldev_res_get_##name##_dumpit(struct sk_buff *skb, \ 1189 struct netlink_callback *cb) \ 1190 { \ 1191 return res_get_common_dumpit(skb, cb, type); \ 1192 } \ 1193 static int nldev_res_get_##name##_doit(struct sk_buff *skb, \ 1194 struct nlmsghdr *nlh, \ 1195 struct netlink_ext_ack *extack) \ 1196 { \ 1197 return res_get_common_doit(skb, nlh, extack, type); \ 1198 } 1199 1200 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP); 1201 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID); 1202 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ); 1203 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD); 1204 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR); 1205 1206 static LIST_HEAD(link_ops); 1207 static DECLARE_RWSEM(link_ops_rwsem); 1208 1209 static const struct rdma_link_ops *link_ops_get(const char *type) 1210 { 1211 const struct rdma_link_ops *ops; 1212 1213 list_for_each_entry(ops, &link_ops, list) { 1214 if (!strcmp(ops->type, type)) 1215 goto out; 1216 } 1217 ops = NULL; 1218 out: 1219 return ops; 1220 } 1221 1222 void rdma_link_register(struct rdma_link_ops *ops) 1223 { 1224 down_write(&link_ops_rwsem); 1225 if (WARN_ON_ONCE(link_ops_get(ops->type))) 1226 goto out; 1227 list_add(&ops->list, &link_ops); 1228 out: 1229 up_write(&link_ops_rwsem); 1230 } 1231 EXPORT_SYMBOL(rdma_link_register); 1232 1233 void rdma_link_unregister(struct rdma_link_ops *ops) 1234 { 1235 down_write(&link_ops_rwsem); 1236 list_del(&ops->list); 1237 up_write(&link_ops_rwsem); 1238 } 1239 EXPORT_SYMBOL(rdma_link_unregister); 1240 1241 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 1242 struct netlink_ext_ack *extack) 1243 { 1244 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1245 char ibdev_name[IB_DEVICE_NAME_MAX]; 1246 const struct rdma_link_ops *ops; 1247 char ndev_name[IFNAMSIZ]; 1248 struct net_device *ndev; 1249 char type[IFNAMSIZ]; 1250 int err; 1251 1252 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1253 nldev_policy, extack); 1254 if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || 1255 !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME]) 1256 return -EINVAL; 1257 1258 nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME], 1259 sizeof(ibdev_name)); 1260 if (strchr(ibdev_name, '%')) 1261 return -EINVAL; 1262 1263 nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type)); 1264 nla_strlcpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME], 1265 sizeof(ndev_name)); 1266 1267 ndev = dev_get_by_name(&init_net, ndev_name); 1268 if (!ndev) 1269 return -ENODEV; 1270 1271 down_read(&link_ops_rwsem); 1272 ops = link_ops_get(type); 1273 #ifdef CONFIG_MODULES 1274 if (!ops) { 1275 up_read(&link_ops_rwsem); 1276 request_module("rdma-link-%s", type); 1277 down_read(&link_ops_rwsem); 1278 ops = link_ops_get(type); 1279 } 1280 #endif 1281 err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL; 1282 up_read(&link_ops_rwsem); 1283 dev_put(ndev); 1284 1285 return err; 1286 } 1287 1288 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 1289 struct netlink_ext_ack *extack) 1290 { 1291 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX]; 1292 struct ib_device *device; 1293 u32 index; 1294 int err; 1295 1296 err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, 1297 nldev_policy, extack); 1298 if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) 1299 return -EINVAL; 1300 1301 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); 1302 device = ib_device_get_by_index(index); 1303 if (!device) 1304 return -EINVAL; 1305 1306 if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) { 1307 ib_device_put(device); 1308 return -EINVAL; 1309 } 1310 1311 ib_unregister_device_and_put(device); 1312 return 0; 1313 } 1314 1315 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = { 1316 [RDMA_NLDEV_CMD_GET] = { 1317 .doit = nldev_get_doit, 1318 .dump = nldev_get_dumpit, 1319 }, 1320 [RDMA_NLDEV_CMD_SET] = { 1321 .doit = nldev_set_doit, 1322 .flags = RDMA_NL_ADMIN_PERM, 1323 }, 1324 [RDMA_NLDEV_CMD_NEWLINK] = { 1325 .doit = nldev_newlink, 1326 .flags = RDMA_NL_ADMIN_PERM, 1327 }, 1328 [RDMA_NLDEV_CMD_DELLINK] = { 1329 .doit = nldev_dellink, 1330 .flags = RDMA_NL_ADMIN_PERM, 1331 }, 1332 [RDMA_NLDEV_CMD_PORT_GET] = { 1333 .doit = nldev_port_get_doit, 1334 .dump = nldev_port_get_dumpit, 1335 }, 1336 [RDMA_NLDEV_CMD_RES_GET] = { 1337 .doit = nldev_res_get_doit, 1338 .dump = nldev_res_get_dumpit, 1339 }, 1340 [RDMA_NLDEV_CMD_RES_QP_GET] = { 1341 .doit = nldev_res_get_qp_doit, 1342 .dump = nldev_res_get_qp_dumpit, 1343 }, 1344 [RDMA_NLDEV_CMD_RES_CM_ID_GET] = { 1345 .doit = nldev_res_get_cm_id_doit, 1346 .dump = nldev_res_get_cm_id_dumpit, 1347 }, 1348 [RDMA_NLDEV_CMD_RES_CQ_GET] = { 1349 .doit = nldev_res_get_cq_doit, 1350 .dump = nldev_res_get_cq_dumpit, 1351 }, 1352 [RDMA_NLDEV_CMD_RES_MR_GET] = { 1353 .doit = nldev_res_get_mr_doit, 1354 .dump = nldev_res_get_mr_dumpit, 1355 }, 1356 [RDMA_NLDEV_CMD_RES_PD_GET] = { 1357 .doit = nldev_res_get_pd_doit, 1358 .dump = nldev_res_get_pd_dumpit, 1359 }, 1360 }; 1361 1362 void __init nldev_init(void) 1363 { 1364 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table); 1365 } 1366 1367 void __exit nldev_exit(void) 1368 { 1369 rdma_nl_unregister(RDMA_NL_NLDEV); 1370 } 1371 1372 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5); 1373