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