1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Shared Memory Communications over RDMA (SMC-R) and RoCE 4 * 5 * Generic netlink support functions to configure an SMC-R PNET table 6 * 7 * Copyright IBM Corp. 2016 8 * 9 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/list.h> 14 #include <linux/ctype.h> 15 #include <linux/mutex.h> 16 #include <net/netlink.h> 17 #include <net/genetlink.h> 18 19 #include <uapi/linux/if.h> 20 #include <uapi/linux/smc.h> 21 22 #include <rdma/ib_verbs.h> 23 24 #include <net/netns/generic.h> 25 #include "smc_netns.h" 26 27 #include "smc_pnet.h" 28 #include "smc_ib.h" 29 #include "smc_ism.h" 30 #include "smc_core.h" 31 32 static struct net_device *__pnet_find_base_ndev(struct net_device *ndev); 33 static struct net_device *pnet_find_base_ndev(struct net_device *ndev, 34 netdevice_tracker *tracker); 35 36 static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = { 37 [SMC_PNETID_NAME] = { 38 .type = NLA_NUL_STRING, 39 .len = SMC_MAX_PNETID_LEN 40 }, 41 [SMC_PNETID_ETHNAME] = { 42 .type = NLA_NUL_STRING, 43 .len = IFNAMSIZ - 1 44 }, 45 [SMC_PNETID_IBNAME] = { 46 .type = NLA_NUL_STRING, 47 .len = IB_DEVICE_NAME_MAX - 1 48 }, 49 [SMC_PNETID_IBPORT] = { .type = NLA_U8 } 50 }; 51 52 static struct genl_family smc_pnet_nl_family; 53 54 enum smc_pnet_nametype { 55 SMC_PNET_ETH = 1, 56 SMC_PNET_IB = 2, 57 }; 58 59 /* pnet entry stored in pnet table */ 60 struct smc_pnetentry { 61 struct list_head list; 62 char pnet_name[SMC_MAX_PNETID_LEN + 1]; 63 enum smc_pnet_nametype type; 64 union { 65 struct { 66 char eth_name[IFNAMSIZ + 1]; 67 struct net_device *ndev; 68 netdevice_tracker dev_tracker; 69 }; 70 struct { 71 char ib_name[IB_DEVICE_NAME_MAX + 1]; 72 u8 ib_port; 73 }; 74 }; 75 }; 76 77 /* Check if the pnetid is set */ 78 bool smc_pnet_is_pnetid_set(u8 *pnetid) 79 { 80 if (pnetid[0] == 0 || pnetid[0] == _S) 81 return false; 82 return true; 83 } 84 85 /* Check if two given pnetids match */ 86 static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2) 87 { 88 int i; 89 90 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) { 91 if ((pnetid1[i] == 0 || pnetid1[i] == _S) && 92 (pnetid2[i] == 0 || pnetid2[i] == _S)) 93 break; 94 if (pnetid1[i] != pnetid2[i]) 95 return false; 96 } 97 return true; 98 } 99 100 /* Remove a pnetid from the pnet table. 101 */ 102 static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name) 103 { 104 struct smc_pnetentry *pnetelem, *tmp_pe; 105 struct smc_pnettable *pnettable; 106 struct smc_ib_device *ibdev; 107 struct smcd_dev *smcd; 108 struct smc_net *sn; 109 int rc = -ENOENT; 110 int ibport; 111 112 /* get pnettable for namespace */ 113 sn = net_generic(net, smc_net_id); 114 pnettable = &sn->pnettable; 115 116 /* remove table entry */ 117 mutex_lock(&pnettable->lock); 118 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, 119 list) { 120 if (!pnet_name || 121 smc_pnet_match(pnetelem->pnet_name, pnet_name)) { 122 list_del(&pnetelem->list); 123 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) { 124 netdev_put(pnetelem->ndev, 125 &pnetelem->dev_tracker); 126 pr_warn_ratelimited("smc: net device %s " 127 "erased user defined " 128 "pnetid %.16s\n", 129 pnetelem->eth_name, 130 pnetelem->pnet_name); 131 } 132 kfree(pnetelem); 133 rc = 0; 134 } 135 } 136 mutex_unlock(&pnettable->lock); 137 138 /* if this is not the initial namespace, stop here */ 139 if (net != &init_net) 140 return rc; 141 142 /* remove ib devices */ 143 mutex_lock(&smc_ib_devices.mutex); 144 list_for_each_entry(ibdev, &smc_ib_devices.list, list) { 145 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) { 146 if (ibdev->pnetid_by_user[ibport] && 147 (!pnet_name || 148 smc_pnet_match(pnet_name, 149 ibdev->pnetid[ibport]))) { 150 pr_warn_ratelimited("smc: ib device %s ibport " 151 "%d erased user defined " 152 "pnetid %.16s\n", 153 ibdev->ibdev->name, 154 ibport + 1, 155 ibdev->pnetid[ibport]); 156 memset(ibdev->pnetid[ibport], 0, 157 SMC_MAX_PNETID_LEN); 158 ibdev->pnetid_by_user[ibport] = false; 159 rc = 0; 160 } 161 } 162 } 163 mutex_unlock(&smc_ib_devices.mutex); 164 /* remove smcd devices */ 165 mutex_lock(&smcd_dev_list.mutex); 166 list_for_each_entry(smcd, &smcd_dev_list.list, list) { 167 if (smcd->pnetid_by_user && 168 (!pnet_name || 169 smc_pnet_match(pnet_name, smcd->pnetid))) { 170 pr_warn_ratelimited("smc: smcd device %s " 171 "erased user defined pnetid " 172 "%.16s\n", 173 dev_name(&smcd->dibs->dev), 174 smcd->pnetid); 175 memset(smcd->pnetid, 0, SMC_MAX_PNETID_LEN); 176 smcd->pnetid_by_user = false; 177 rc = 0; 178 } 179 } 180 mutex_unlock(&smcd_dev_list.mutex); 181 return rc; 182 } 183 184 /* Add the reference to a given network device to the pnet table. 185 */ 186 static int smc_pnet_add_by_ndev(struct net_device *ndev) 187 { 188 struct smc_pnetentry *pnetelem, *tmp_pe; 189 struct smc_pnettable *pnettable; 190 struct net *net = dev_net(ndev); 191 struct smc_net *sn; 192 int rc = -ENOENT; 193 194 /* get pnettable for namespace */ 195 sn = net_generic(net, smc_net_id); 196 pnettable = &sn->pnettable; 197 198 mutex_lock(&pnettable->lock); 199 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) { 200 if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev && 201 !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) { 202 netdev_hold(ndev, &pnetelem->dev_tracker, GFP_ATOMIC); 203 pnetelem->ndev = ndev; 204 rc = 0; 205 pr_warn_ratelimited("smc: adding net device %s with " 206 "user defined pnetid %.16s\n", 207 pnetelem->eth_name, 208 pnetelem->pnet_name); 209 break; 210 } 211 } 212 mutex_unlock(&pnettable->lock); 213 return rc; 214 } 215 216 /* Remove the reference to a given network device from the pnet table. 217 */ 218 static int smc_pnet_remove_by_ndev(struct net_device *ndev) 219 { 220 struct smc_pnetentry *pnetelem, *tmp_pe; 221 struct smc_pnettable *pnettable; 222 struct net *net = dev_net(ndev); 223 struct smc_net *sn; 224 int rc = -ENOENT; 225 226 /* get pnettable for namespace */ 227 sn = net_generic(net, smc_net_id); 228 pnettable = &sn->pnettable; 229 230 mutex_lock(&pnettable->lock); 231 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) { 232 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) { 233 netdev_put(pnetelem->ndev, &pnetelem->dev_tracker); 234 pnetelem->ndev = NULL; 235 rc = 0; 236 pr_warn_ratelimited("smc: removing net device %s with " 237 "user defined pnetid %.16s\n", 238 pnetelem->eth_name, 239 pnetelem->pnet_name); 240 break; 241 } 242 } 243 mutex_unlock(&pnettable->lock); 244 return rc; 245 } 246 247 /* Apply pnetid to ib device when no pnetid is set. 248 */ 249 static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port, 250 char *pnet_name) 251 { 252 bool applied = false; 253 254 mutex_lock(&smc_ib_devices.mutex); 255 if (!smc_pnet_is_pnetid_set(ib_dev->pnetid[ib_port - 1])) { 256 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name, 257 SMC_MAX_PNETID_LEN); 258 ib_dev->pnetid_by_user[ib_port - 1] = true; 259 applied = true; 260 } 261 mutex_unlock(&smc_ib_devices.mutex); 262 return applied; 263 } 264 265 /* Apply pnetid to smcd device when no pnetid is set. 266 */ 267 static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name) 268 { 269 bool applied = false; 270 271 mutex_lock(&smcd_dev_list.mutex); 272 if (!smc_pnet_is_pnetid_set(smcd_dev->pnetid)) { 273 memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN); 274 smcd_dev->pnetid_by_user = true; 275 applied = true; 276 } 277 mutex_unlock(&smcd_dev_list.mutex); 278 return applied; 279 } 280 281 /* The limit for pnetid is 16 characters. 282 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9. 283 * Lower case letters are converted to upper case. 284 * Interior blanks should not be used. 285 */ 286 static bool smc_pnetid_valid(const char *pnet_name, char *pnetid) 287 { 288 char *bf = skip_spaces(pnet_name); 289 size_t len = strlen(bf); 290 char *end = bf + len; 291 292 if (!len) 293 return false; 294 while (--end >= bf && isspace(*end)) 295 ; 296 if (end - bf >= SMC_MAX_PNETID_LEN) 297 return false; 298 while (bf <= end) { 299 if (!isalnum(*bf)) 300 return false; 301 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf; 302 bf++; 303 } 304 *pnetid = '\0'; 305 return true; 306 } 307 308 /* 309 * Find an infiniband device by a given name, restricted to the devices 310 * accessible from @net. The device might not exist. 311 */ 312 static struct smc_ib_device *smc_pnet_find_ib(struct net *net, char *ib_name) 313 { 314 struct smc_ib_device *ibdev; 315 316 mutex_lock(&smc_ib_devices.mutex); 317 list_for_each_entry(ibdev, &smc_ib_devices.list, list) { 318 if (!rdma_dev_access_netns(ibdev->ibdev, net)) 319 continue; 320 if (!strncmp(ibdev->ibdev->name, ib_name, 321 sizeof(ibdev->ibdev->name)) || 322 (ibdev->ibdev->dev.parent && 323 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name, 324 IB_DEVICE_NAME_MAX - 1))) { 325 goto out; 326 } 327 } 328 ibdev = NULL; 329 out: 330 mutex_unlock(&smc_ib_devices.mutex); 331 return ibdev; 332 } 333 334 /* Find an smcd device by a given name. The device might not exist. */ 335 static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name) 336 { 337 struct smcd_dev *smcd_dev; 338 339 mutex_lock(&smcd_dev_list.mutex); 340 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { 341 if (!strncmp(dev_name(&smcd_dev->dibs->dev), smcd_name, 342 IB_DEVICE_NAME_MAX - 1) || 343 (smcd_dev->dibs->dev.parent && 344 !strncmp(dev_name(smcd_dev->dibs->dev.parent), smcd_name, 345 IB_DEVICE_NAME_MAX - 1))) 346 goto out; 347 } 348 smcd_dev = NULL; 349 out: 350 mutex_unlock(&smcd_dev_list.mutex); 351 return smcd_dev; 352 } 353 354 static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net, 355 char *eth_name, char *pnet_name) 356 { 357 struct smc_pnetentry *tmp_pe, *new_pe; 358 struct net_device *ndev, *base_ndev; 359 u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; 360 netdevice_tracker base_tracker; 361 bool new_netdev; 362 int rc; 363 364 /* check if (base) netdev already has a pnetid. If there is one, we do 365 * not want to add a pnet table entry 366 */ 367 rc = -EEXIST; 368 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */ 369 if (ndev) { 370 base_ndev = pnet_find_base_ndev(ndev, &base_tracker); 371 if (!smc_pnetid_by_dev_port(base_ndev->dev.parent, 372 base_ndev->dev_port, ndev_pnetid)) { 373 netdev_put(base_ndev, &base_tracker); 374 goto out_put; 375 } 376 netdev_put(base_ndev, &base_tracker); 377 } 378 379 /* add a new netdev entry to the pnet table if there isn't one */ 380 rc = -ENOMEM; 381 new_pe = kzalloc_obj(*new_pe); 382 if (!new_pe) 383 goto out_put; 384 new_pe->type = SMC_PNET_ETH; 385 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN); 386 strscpy(new_pe->eth_name, eth_name); 387 rc = -EEXIST; 388 new_netdev = true; 389 mutex_lock(&pnettable->lock); 390 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { 391 if (tmp_pe->type == SMC_PNET_ETH && 392 !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) { 393 new_netdev = false; 394 break; 395 } 396 } 397 if (new_netdev) { 398 if (ndev) { 399 new_pe->ndev = ndev; 400 netdev_tracker_alloc(ndev, &new_pe->dev_tracker, 401 GFP_ATOMIC); 402 } 403 list_add_tail(&new_pe->list, &pnettable->pnetlist); 404 mutex_unlock(&pnettable->lock); 405 } else { 406 mutex_unlock(&pnettable->lock); 407 kfree(new_pe); 408 goto out_put; 409 } 410 if (ndev) 411 pr_warn_ratelimited("smc: net device %s " 412 "applied user defined pnetid %.16s\n", 413 new_pe->eth_name, new_pe->pnet_name); 414 return 0; 415 416 out_put: 417 dev_put(ndev); 418 return rc; 419 } 420 421 static int smc_pnet_add_ib(struct smc_pnettable *pnettable, struct net *net, 422 char *ib_name, u8 ib_port, char *pnet_name) 423 { 424 struct smc_pnetentry *tmp_pe, *new_pe; 425 struct smc_ib_device *ib_dev; 426 bool smcddev_applied = true; 427 bool ibdev_applied = true; 428 struct smcd_dev *smcd; 429 bool new_ibdev; 430 431 /* try to apply the pnetid to active devices */ 432 ib_dev = smc_pnet_find_ib(net, ib_name); 433 if (ib_dev) { 434 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name); 435 if (ibdev_applied) 436 pr_warn_ratelimited("smc: ib device %s ibport %d " 437 "applied user defined pnetid " 438 "%.16s\n", ib_dev->ibdev->name, 439 ib_port, 440 ib_dev->pnetid[ib_port - 1]); 441 } 442 smcd = smc_pnet_find_smcd(ib_name); 443 if (smcd) { 444 smcddev_applied = smc_pnet_apply_smcd(smcd, pnet_name); 445 if (smcddev_applied) { 446 pr_warn_ratelimited("smc: smcd device %s applied user defined pnetid %.16s\n", 447 dev_name(&smcd->dibs->dev), 448 smcd->pnetid); 449 } 450 } 451 /* Apply fails when a device has a hardware-defined pnetid set, do not 452 * add a pnet table entry in that case. 453 */ 454 if (!ibdev_applied || !smcddev_applied) 455 return -EEXIST; 456 457 /* add a new ib entry to the pnet table if there isn't one */ 458 new_pe = kzalloc_obj(*new_pe); 459 if (!new_pe) 460 return -ENOMEM; 461 new_pe->type = SMC_PNET_IB; 462 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN); 463 strscpy(new_pe->ib_name, ib_name); 464 new_pe->ib_port = ib_port; 465 466 new_ibdev = true; 467 mutex_lock(&pnettable->lock); 468 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { 469 if (tmp_pe->type == SMC_PNET_IB && 470 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) { 471 new_ibdev = false; 472 break; 473 } 474 } 475 if (new_ibdev) { 476 list_add_tail(&new_pe->list, &pnettable->pnetlist); 477 mutex_unlock(&pnettable->lock); 478 } else { 479 mutex_unlock(&pnettable->lock); 480 kfree(new_pe); 481 } 482 return (new_ibdev) ? 0 : -EEXIST; 483 } 484 485 /* Append a pnetid to the end of the pnet table if not already on this list. 486 */ 487 static int smc_pnet_enter(struct net *net, struct nlattr *tb[]) 488 { 489 char pnet_name[SMC_MAX_PNETID_LEN + 1]; 490 struct smc_pnettable *pnettable; 491 bool new_netdev = false; 492 bool new_ibdev = false; 493 struct smc_net *sn; 494 u8 ibport = 1; 495 char *string; 496 int rc; 497 498 /* get pnettable for namespace */ 499 sn = net_generic(net, smc_net_id); 500 pnettable = &sn->pnettable; 501 502 rc = -EINVAL; 503 if (!tb[SMC_PNETID_NAME]) 504 goto error; 505 string = (char *)nla_data(tb[SMC_PNETID_NAME]); 506 if (!smc_pnetid_valid(string, pnet_name)) 507 goto error; 508 509 if (tb[SMC_PNETID_ETHNAME]) { 510 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]); 511 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name); 512 if (!rc) 513 new_netdev = true; 514 else if (rc != -EEXIST) 515 goto error; 516 } 517 518 /* if this is not the initial namespace, stop here */ 519 if (net != &init_net) 520 return new_netdev ? 0 : -EEXIST; 521 522 rc = -EINVAL; 523 if (tb[SMC_PNETID_IBNAME]) { 524 string = (char *)nla_data(tb[SMC_PNETID_IBNAME]); 525 string = strim(string); 526 if (tb[SMC_PNETID_IBPORT]) { 527 ibport = nla_get_u8(tb[SMC_PNETID_IBPORT]); 528 if (ibport < 1 || ibport > SMC_MAX_PORTS) 529 goto error; 530 } 531 rc = smc_pnet_add_ib(pnettable, net, string, ibport, pnet_name); 532 if (!rc) 533 new_ibdev = true; 534 else if (rc != -EEXIST) 535 goto error; 536 } 537 return (new_netdev || new_ibdev) ? 0 : -EEXIST; 538 539 error: 540 return rc; 541 } 542 543 /* Convert an smc_pnetentry to a netlink attribute sequence */ 544 static int smc_pnet_set_nla(struct sk_buff *msg, 545 struct smc_pnetentry *pnetelem) 546 { 547 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name)) 548 return -1; 549 if (pnetelem->type == SMC_PNET_ETH) { 550 if (nla_put_string(msg, SMC_PNETID_ETHNAME, 551 pnetelem->eth_name)) 552 return -1; 553 } else { 554 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a")) 555 return -1; 556 } 557 if (pnetelem->type == SMC_PNET_IB) { 558 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) || 559 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port)) 560 return -1; 561 } else { 562 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") || 563 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff)) 564 return -1; 565 } 566 567 return 0; 568 } 569 570 static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info) 571 { 572 struct net *net = genl_info_net(info); 573 574 return smc_pnet_enter(net, info->attrs); 575 } 576 577 static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info) 578 { 579 struct net *net = genl_info_net(info); 580 581 if (!info->attrs[SMC_PNETID_NAME]) 582 return -EINVAL; 583 return smc_pnet_remove_by_pnetid(net, 584 (char *)nla_data(info->attrs[SMC_PNETID_NAME])); 585 } 586 587 static int smc_pnet_dump_start(struct netlink_callback *cb) 588 { 589 cb->args[0] = 0; 590 return 0; 591 } 592 593 static int smc_pnet_dumpinfo(struct sk_buff *skb, 594 u32 portid, u32 seq, u32 flags, 595 struct smc_pnetentry *pnetelem) 596 { 597 void *hdr; 598 599 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family, 600 flags, SMC_PNETID_GET); 601 if (!hdr) 602 return -ENOMEM; 603 if (smc_pnet_set_nla(skb, pnetelem) < 0) { 604 genlmsg_cancel(skb, hdr); 605 return -EMSGSIZE; 606 } 607 genlmsg_end(skb, hdr); 608 return 0; 609 } 610 611 static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid, 612 u32 seq, u8 *pnetid, int start_idx) 613 { 614 struct smc_pnettable *pnettable; 615 struct smc_pnetentry *pnetelem; 616 struct smc_net *sn; 617 int idx = 0; 618 619 /* get pnettable for namespace */ 620 sn = net_generic(net, smc_net_id); 621 pnettable = &sn->pnettable; 622 623 /* dump pnettable entries */ 624 mutex_lock(&pnettable->lock); 625 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) { 626 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid)) 627 continue; 628 if (idx++ < start_idx) 629 continue; 630 /* if this is not the initial namespace, dump only netdev */ 631 if (net != &init_net && pnetelem->type != SMC_PNET_ETH) 632 continue; 633 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI, 634 pnetelem)) { 635 --idx; 636 break; 637 } 638 } 639 mutex_unlock(&pnettable->lock); 640 return idx; 641 } 642 643 static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb) 644 { 645 struct net *net = sock_net(skb->sk); 646 int idx; 647 648 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid, 649 cb->nlh->nlmsg_seq, NULL, cb->args[0]); 650 651 cb->args[0] = idx; 652 return skb->len; 653 } 654 655 /* Retrieve one PNETID entry */ 656 static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info) 657 { 658 struct net *net = genl_info_net(info); 659 struct sk_buff *msg; 660 void *hdr; 661 662 if (!info->attrs[SMC_PNETID_NAME]) 663 return -EINVAL; 664 665 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 666 if (!msg) 667 return -ENOMEM; 668 669 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq, 670 nla_data(info->attrs[SMC_PNETID_NAME]), 0); 671 672 /* finish multi part message and send it */ 673 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0, 674 NLM_F_MULTI); 675 if (!hdr) { 676 nlmsg_free(msg); 677 return -EMSGSIZE; 678 } 679 return genlmsg_reply(msg, info); 680 } 681 682 /* Remove and delete all pnetids from pnet table. 683 */ 684 static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info) 685 { 686 struct net *net = genl_info_net(info); 687 688 smc_pnet_remove_by_pnetid(net, NULL); 689 return 0; 690 } 691 692 /* SMC_PNETID generic netlink operation definition */ 693 static const struct genl_ops smc_pnet_ops[] = { 694 { 695 .cmd = SMC_PNETID_GET, 696 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 697 /* can be retrieved by unprivileged users */ 698 .doit = smc_pnet_get, 699 .dumpit = smc_pnet_dump, 700 .start = smc_pnet_dump_start 701 }, 702 { 703 .cmd = SMC_PNETID_ADD, 704 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 705 .flags = GENL_ADMIN_PERM, 706 .doit = smc_pnet_add 707 }, 708 { 709 .cmd = SMC_PNETID_DEL, 710 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 711 .flags = GENL_ADMIN_PERM, 712 .doit = smc_pnet_del 713 }, 714 { 715 .cmd = SMC_PNETID_FLUSH, 716 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 717 .flags = GENL_ADMIN_PERM, 718 .doit = smc_pnet_flush 719 } 720 }; 721 722 /* SMC_PNETID family definition */ 723 static struct genl_family smc_pnet_nl_family __ro_after_init = { 724 .hdrsize = 0, 725 .name = SMCR_GENL_FAMILY_NAME, 726 .version = SMCR_GENL_FAMILY_VERSION, 727 .maxattr = SMC_PNETID_MAX, 728 .policy = smc_pnet_policy, 729 .netnsok = true, 730 .module = THIS_MODULE, 731 .ops = smc_pnet_ops, 732 .n_ops = ARRAY_SIZE(smc_pnet_ops), 733 .resv_start_op = SMC_PNETID_FLUSH + 1, 734 }; 735 736 bool smc_pnet_is_ndev_pnetid(struct net *net, u8 *pnetid) 737 { 738 struct smc_net *sn = net_generic(net, smc_net_id); 739 struct smc_pnetids_ndev_entry *pe; 740 bool rc = false; 741 742 read_lock(&sn->pnetids_ndev.lock); 743 list_for_each_entry(pe, &sn->pnetids_ndev.list, list) { 744 if (smc_pnet_match(pnetid, pe->pnetid)) { 745 rc = true; 746 goto unlock; 747 } 748 } 749 750 unlock: 751 read_unlock(&sn->pnetids_ndev.lock); 752 return rc; 753 } 754 755 static int smc_pnet_add_pnetid(struct net *net, u8 *pnetid) 756 { 757 struct smc_net *sn = net_generic(net, smc_net_id); 758 struct smc_pnetids_ndev_entry *pe, *pi; 759 760 pe = kzalloc_obj(*pe); 761 if (!pe) 762 return -ENOMEM; 763 764 write_lock(&sn->pnetids_ndev.lock); 765 list_for_each_entry(pi, &sn->pnetids_ndev.list, list) { 766 if (smc_pnet_match(pnetid, pi->pnetid)) { 767 refcount_inc(&pi->refcnt); 768 kfree(pe); 769 goto unlock; 770 } 771 } 772 refcount_set(&pe->refcnt, 1); 773 memcpy(pe->pnetid, pnetid, SMC_MAX_PNETID_LEN); 774 list_add_tail(&pe->list, &sn->pnetids_ndev.list); 775 776 unlock: 777 write_unlock(&sn->pnetids_ndev.lock); 778 return 0; 779 } 780 781 static void smc_pnet_remove_pnetid(struct net *net, u8 *pnetid) 782 { 783 struct smc_net *sn = net_generic(net, smc_net_id); 784 struct smc_pnetids_ndev_entry *pe, *pe2; 785 786 write_lock(&sn->pnetids_ndev.lock); 787 list_for_each_entry_safe(pe, pe2, &sn->pnetids_ndev.list, list) { 788 if (smc_pnet_match(pnetid, pe->pnetid)) { 789 if (refcount_dec_and_test(&pe->refcnt)) { 790 list_del(&pe->list); 791 kfree(pe); 792 } 793 break; 794 } 795 } 796 write_unlock(&sn->pnetids_ndev.lock); 797 } 798 799 static void smc_pnet_add_base_pnetid(struct net *net, struct net_device *dev, 800 u8 *ndev_pnetid) 801 { 802 struct net_device *base_dev; 803 804 base_dev = __pnet_find_base_ndev(dev); 805 if (base_dev->flags & IFF_UP && 806 !smc_pnetid_by_dev_port(base_dev->dev.parent, base_dev->dev_port, 807 ndev_pnetid)) { 808 /* add to PNETIDs list */ 809 smc_pnet_add_pnetid(net, ndev_pnetid); 810 } 811 } 812 813 /* create initial list of netdevice pnetids */ 814 static void smc_pnet_create_pnetids_list(struct net *net) 815 { 816 u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; 817 struct net_device *dev; 818 819 /* Newly created netns do not have devices. 820 * Do not even acquire rtnl. 821 */ 822 if (list_empty(&net->dev_base_head)) 823 return; 824 825 /* Note: This might not be needed, because smc_pnet_netdev_event() 826 * is also calling smc_pnet_add_base_pnetid() when handling 827 * NETDEV_UP event. 828 */ 829 rtnl_lock(); 830 for_each_netdev(net, dev) 831 smc_pnet_add_base_pnetid(net, dev, ndev_pnetid); 832 rtnl_unlock(); 833 } 834 835 /* clean up list of netdevice pnetids */ 836 static void smc_pnet_destroy_pnetids_list(struct net *net) 837 { 838 struct smc_net *sn = net_generic(net, smc_net_id); 839 struct smc_pnetids_ndev_entry *pe, *temp_pe; 840 841 write_lock(&sn->pnetids_ndev.lock); 842 list_for_each_entry_safe(pe, temp_pe, &sn->pnetids_ndev.list, list) { 843 list_del(&pe->list); 844 kfree(pe); 845 } 846 write_unlock(&sn->pnetids_ndev.lock); 847 } 848 849 static int smc_pnet_netdev_event(struct notifier_block *this, 850 unsigned long event, void *ptr) 851 { 852 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 853 struct net *net = dev_net(event_dev); 854 u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; 855 856 switch (event) { 857 case NETDEV_REBOOT: 858 case NETDEV_UNREGISTER: 859 smc_pnet_remove_by_ndev(event_dev); 860 smc_ib_ndev_change(event_dev, event); 861 return NOTIFY_OK; 862 case NETDEV_REGISTER: 863 smc_pnet_add_by_ndev(event_dev); 864 smc_ib_ndev_change(event_dev, event); 865 return NOTIFY_OK; 866 case NETDEV_UP: 867 smc_pnet_add_base_pnetid(net, event_dev, ndev_pnetid); 868 return NOTIFY_OK; 869 case NETDEV_DOWN: 870 event_dev = __pnet_find_base_ndev(event_dev); 871 if (!smc_pnetid_by_dev_port(event_dev->dev.parent, 872 event_dev->dev_port, ndev_pnetid)) { 873 /* remove from PNETIDs list */ 874 smc_pnet_remove_pnetid(net, ndev_pnetid); 875 } 876 return NOTIFY_OK; 877 default: 878 return NOTIFY_DONE; 879 } 880 } 881 882 static struct notifier_block smc_netdev_notifier = { 883 .notifier_call = smc_pnet_netdev_event 884 }; 885 886 /* init network namespace */ 887 int smc_pnet_net_init(struct net *net) 888 { 889 struct smc_net *sn = net_generic(net, smc_net_id); 890 struct smc_pnettable *pnettable = &sn->pnettable; 891 struct smc_pnetids_ndev *pnetids_ndev = &sn->pnetids_ndev; 892 893 INIT_LIST_HEAD(&pnettable->pnetlist); 894 mutex_init(&pnettable->lock); 895 INIT_LIST_HEAD(&pnetids_ndev->list); 896 rwlock_init(&pnetids_ndev->lock); 897 898 smc_pnet_create_pnetids_list(net); 899 900 return 0; 901 } 902 903 int __init smc_pnet_init(void) 904 { 905 int rc; 906 907 rc = genl_register_family(&smc_pnet_nl_family); 908 if (rc) 909 return rc; 910 rc = register_netdevice_notifier(&smc_netdev_notifier); 911 if (rc) 912 genl_unregister_family(&smc_pnet_nl_family); 913 914 return rc; 915 } 916 917 /* exit network namespace */ 918 void smc_pnet_net_exit(struct net *net) 919 { 920 /* flush pnet table */ 921 smc_pnet_remove_by_pnetid(net, NULL); 922 smc_pnet_destroy_pnetids_list(net); 923 } 924 925 void smc_pnet_exit(void) 926 { 927 unregister_netdevice_notifier(&smc_netdev_notifier); 928 genl_unregister_family(&smc_pnet_nl_family); 929 } 930 931 static struct net_device *__pnet_find_base_ndev(struct net_device *ndev) 932 { 933 int i, nest_lvl; 934 935 ASSERT_RTNL(); 936 nest_lvl = ndev->lower_level; 937 for (i = 0; i < nest_lvl; i++) { 938 struct list_head *lower = &ndev->adj_list.lower; 939 940 if (list_empty(lower)) 941 break; 942 lower = lower->next; 943 ndev = netdev_lower_get_next(ndev, &lower); 944 } 945 return ndev; 946 } 947 948 /* Determine one base device for stacked net devices. 949 * If the lower device level contains more than one devices 950 * (for instance with bonding slaves), just the first device 951 * is used to reach a base device. 952 */ 953 static struct net_device *pnet_find_base_ndev(struct net_device *ndev, 954 netdevice_tracker *tracker) 955 { 956 rtnl_lock(); 957 ndev = __pnet_find_base_ndev(ndev); 958 /* keep ndev alive after dropping RTNL, callers must netdev_put() */ 959 netdev_hold(ndev, tracker, GFP_KERNEL); 960 rtnl_unlock(); 961 return ndev; 962 } 963 964 static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev, 965 u8 *pnetid) 966 { 967 struct smc_pnettable *pnettable; 968 struct net *net = dev_net(ndev); 969 struct smc_pnetentry *pnetelem; 970 struct smc_net *sn; 971 int rc = -ENOENT; 972 973 /* get pnettable for namespace */ 974 sn = net_generic(net, smc_net_id); 975 pnettable = &sn->pnettable; 976 977 mutex_lock(&pnettable->lock); 978 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) { 979 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) { 980 /* get pnetid of netdev device */ 981 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN); 982 rc = 0; 983 break; 984 } 985 } 986 mutex_unlock(&pnettable->lock); 987 return rc; 988 } 989 990 static int smc_pnet_determine_gid(struct smc_ib_device *ibdev, int i, 991 struct smc_init_info *ini) 992 { 993 if (!ini->check_smcrv2 && 994 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->ib_gid, NULL, 995 NULL)) { 996 ini->ib_dev = ibdev; 997 ini->ib_port = i; 998 return 0; 999 } 1000 if (ini->check_smcrv2 && 1001 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->smcrv2.ib_gid_v2, 1002 NULL, &ini->smcrv2)) { 1003 ini->smcrv2.ib_dev_v2 = ibdev; 1004 ini->smcrv2.ib_port_v2 = i; 1005 return 0; 1006 } 1007 return -ENODEV; 1008 } 1009 1010 /* find a roce device for the given pnetid */ 1011 static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id, 1012 struct smc_init_info *ini, 1013 struct smc_ib_device *known_dev, 1014 struct net *net) 1015 { 1016 struct smc_ib_device *ibdev; 1017 int i; 1018 1019 mutex_lock(&smc_ib_devices.mutex); 1020 list_for_each_entry(ibdev, &smc_ib_devices.list, list) { 1021 if (ibdev == known_dev || 1022 !rdma_dev_access_netns(ibdev->ibdev, net)) 1023 continue; 1024 for (i = 1; i <= SMC_MAX_PORTS; i++) { 1025 if (!rdma_is_port_valid(ibdev->ibdev, i)) 1026 continue; 1027 if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) && 1028 smc_ib_port_active(ibdev, i) && 1029 !test_bit(i - 1, ibdev->ports_going_away)) { 1030 if (!smc_pnet_determine_gid(ibdev, i, ini)) 1031 goto out; 1032 } 1033 } 1034 } 1035 out: 1036 mutex_unlock(&smc_ib_devices.mutex); 1037 } 1038 1039 /* find alternate roce device with same pnet_id, vlan_id and net namespace */ 1040 void smc_pnet_find_alt_roce(struct smc_link_group *lgr, 1041 struct smc_init_info *ini, 1042 struct smc_ib_device *known_dev) 1043 { 1044 struct net *net = lgr->net; 1045 1046 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev, net); 1047 } 1048 1049 /* if handshake network device belongs to a roce device, return its 1050 * IB device and port 1051 */ 1052 static void smc_pnet_find_rdma_dev(struct net_device *netdev, 1053 struct smc_init_info *ini) 1054 { 1055 struct net *net = dev_net(netdev); 1056 struct smc_ib_device *ibdev; 1057 1058 mutex_lock(&smc_ib_devices.mutex); 1059 list_for_each_entry(ibdev, &smc_ib_devices.list, list) { 1060 struct net_device *ndev; 1061 int i; 1062 1063 /* check rdma net namespace */ 1064 if (!rdma_dev_access_netns(ibdev->ibdev, net)) 1065 continue; 1066 1067 for (i = 1; i <= SMC_MAX_PORTS; i++) { 1068 if (!rdma_is_port_valid(ibdev->ibdev, i)) 1069 continue; 1070 ndev = ib_device_get_netdev(ibdev->ibdev, i); 1071 if (!ndev) 1072 continue; 1073 dev_put(ndev); 1074 if (netdev == ndev && 1075 smc_ib_port_active(ibdev, i) && 1076 !test_bit(i - 1, ibdev->ports_going_away)) { 1077 if (!smc_pnet_determine_gid(ibdev, i, ini)) 1078 break; 1079 } 1080 } 1081 } 1082 mutex_unlock(&smc_ib_devices.mutex); 1083 } 1084 1085 /* Determine the corresponding IB device port based on the hardware PNETID. 1086 * Searching stops at the first matching active IB device port with vlan_id 1087 * configured. 1088 * If nothing found, check pnetid table. 1089 * If nothing found, try to use handshake device 1090 */ 1091 static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev, 1092 struct smc_init_info *ini) 1093 { 1094 u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; 1095 struct net_device *base_ndev; 1096 netdevice_tracker base_tracker; 1097 struct net *net; 1098 1099 base_ndev = pnet_find_base_ndev(ndev, &base_tracker); 1100 net = dev_net(ndev); 1101 if (smc_pnetid_by_dev_port(base_ndev->dev.parent, base_ndev->dev_port, 1102 ndev_pnetid) && 1103 smc_pnet_find_ndev_pnetid_by_table(base_ndev, ndev_pnetid) && 1104 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) { 1105 smc_pnet_find_rdma_dev(base_ndev, ini); 1106 netdev_put(base_ndev, &base_tracker); 1107 return; /* pnetid could not be determined */ 1108 } 1109 netdev_put(base_ndev, &base_tracker); 1110 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL, net); 1111 } 1112 1113 static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev, 1114 struct smc_init_info *ini) 1115 { 1116 u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; 1117 netdevice_tracker base_tracker; 1118 struct smcd_dev *ismdev; 1119 1120 ndev = pnet_find_base_ndev(ndev, &base_tracker); 1121 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, 1122 ndev_pnetid) && 1123 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) { 1124 netdev_put(ndev, &base_tracker); 1125 return; /* pnetid could not be determined */ 1126 } 1127 1128 mutex_lock(&smcd_dev_list.mutex); 1129 list_for_each_entry(ismdev, &smcd_dev_list.list, list) { 1130 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) && 1131 !ismdev->going_away && 1132 (!ini->ism_peer_gid[0].gid || 1133 !smc_ism_cantalk(&ini->ism_peer_gid[0], ini->vlan_id, 1134 ismdev))) { 1135 ini->ism_dev[0] = ismdev; 1136 break; 1137 } 1138 } 1139 mutex_unlock(&smcd_dev_list.mutex); 1140 netdev_put(ndev, &base_tracker); 1141 } 1142 1143 /* PNET table analysis for a given sock: 1144 * determine ib_device and port belonging to used internal TCP socket 1145 * ethernet interface. 1146 */ 1147 void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini) 1148 { 1149 struct net_device *dev; 1150 struct dst_entry *dst; 1151 1152 rcu_read_lock(); 1153 dst = __sk_dst_get(sk); 1154 dev = dst ? dst_dev_rcu(dst) : NULL; 1155 dev_hold(dev); 1156 rcu_read_unlock(); 1157 1158 if (dev) { 1159 smc_pnet_find_roce_by_pnetid(dev, ini); 1160 dev_put(dev); 1161 } 1162 } 1163 1164 void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini) 1165 { 1166 struct net_device *dev; 1167 struct dst_entry *dst; 1168 1169 ini->ism_dev[0] = NULL; 1170 1171 rcu_read_lock(); 1172 dst = __sk_dst_get(sk); 1173 dev = dst ? dst_dev_rcu(dst) : NULL; 1174 dev_hold(dev); 1175 rcu_read_unlock(); 1176 1177 if (dev) { 1178 smc_pnet_find_ism_by_pnetid(dev, ini); 1179 dev_put(dev); 1180 } 1181 } 1182 1183 /* Lookup and apply a pnet table entry to the given ib device. 1184 */ 1185 int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port) 1186 { 1187 char *ib_name = smcibdev->ibdev->name; 1188 struct smc_pnettable *pnettable; 1189 struct smc_pnetentry *tmp_pe; 1190 struct smc_net *sn; 1191 int rc = -ENOENT; 1192 1193 if (!rdma_dev_access_netns(smcibdev->ibdev, &init_net)) 1194 return -ENOENT; 1195 1196 /* get pnettable for init namespace */ 1197 sn = net_generic(&init_net, smc_net_id); 1198 pnettable = &sn->pnettable; 1199 1200 mutex_lock(&pnettable->lock); 1201 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { 1202 if (tmp_pe->type == SMC_PNET_IB && 1203 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) && 1204 tmp_pe->ib_port == ib_port) { 1205 smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name); 1206 rc = 0; 1207 break; 1208 } 1209 } 1210 mutex_unlock(&pnettable->lock); 1211 1212 return rc; 1213 } 1214 1215 /* Lookup and apply a pnet table entry to the given smcd device. 1216 */ 1217 int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev) 1218 { 1219 struct smc_pnettable *pnettable; 1220 struct smc_pnetentry *tmp_pe; 1221 struct smc_net *sn; 1222 int rc = -ENOENT; 1223 1224 /* get pnettable for init namespace */ 1225 sn = net_generic(&init_net, smc_net_id); 1226 pnettable = &sn->pnettable; 1227 1228 mutex_lock(&pnettable->lock); 1229 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { 1230 if (tmp_pe->type == SMC_PNET_IB && 1231 (!strncmp(tmp_pe->ib_name, 1232 dev_name(&smcddev->dibs->dev), 1233 sizeof(tmp_pe->ib_name)) || 1234 (smcddev->dibs->dev.parent && 1235 !strncmp(tmp_pe->ib_name, 1236 dev_name(smcddev->dibs->dev.parent), 1237 sizeof(tmp_pe->ib_name))))) { 1238 smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name); 1239 rc = 0; 1240 break; 1241 } 1242 } 1243 mutex_unlock(&pnettable->lock); 1244 1245 return rc; 1246 } 1247