1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Generic netlink support functions to configure an SMC-R PNET table
7 * Copyright IBM Corp. 2016
9 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com>
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>
19 #include <uapi/linux/if.h>
20 #include <uapi/linux/smc.h>
22 #include <rdma/ib_verbs.h>
24 #include <net/netns/generic.h>
25 #include "smc_netns.h"
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);
35 static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
37 .type = NLA_NUL_STRING,
38 .len = SMC_MAX_PNETID_LEN
40 [SMC_PNETID_ETHNAME] = {
41 .type = NLA_NUL_STRING,
44 [SMC_PNETID_IBNAME] = {
45 .type = NLA_NUL_STRING,
46 .len = IB_DEVICE_NAME_MAX - 1
48 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
51 static struct genl_family smc_pnet_nl_family;
53 enum smc_pnet_nametype {
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;
65 char eth_name[IFNAMSIZ + 1];
66 struct net_device *ndev;
67 netdevice_tracker dev_tracker;
70 char ib_name[IB_DEVICE_NAME_MAX + 1];
76 /* Check if the pnetid is set */
77 bool smc_pnet_is_pnetid_set(u8 *pnetid)
79 if (pnetid[0] == 0 || pnetid[0] == _S)
84 /* Check if two given pnetids match */
85 static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
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))
93 if (pnetid1[i] != pnetid2[i])
99 /* Remove a pnetid from the pnet table.
101 static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name)
103 struct smc_pnetentry *pnetelem, *tmp_pe;
104 struct smc_pnettable *pnettable;
105 struct smc_ib_device *ibdev;
106 struct smcd_dev *smcd_dev;
111 /* get pnettable for namespace */
112 sn = net_generic(net, smc_net_id);
113 pnettable = &sn->pnettable;
115 /* remove table entry */
116 mutex_lock(&pnettable->lock);
117 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist,
120 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
121 list_del(&pnetelem->list);
122 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) {
123 dev_put_track(pnetelem->ndev, &pnetelem->dev_tracker);
124 pr_warn_ratelimited("smc: net device %s "
125 "erased user defined "
128 pnetelem->pnet_name);
134 mutex_unlock(&pnettable->lock);
136 /* if this is not the initial namespace, stop here */
137 if (net != &init_net)
140 /* remove ib devices */
141 mutex_lock(&smc_ib_devices.mutex);
142 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
143 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
144 if (ibdev->pnetid_by_user[ibport] &&
146 smc_pnet_match(pnet_name,
147 ibdev->pnetid[ibport]))) {
148 pr_warn_ratelimited("smc: ib device %s ibport "
149 "%d erased user defined "
153 ibdev->pnetid[ibport]);
154 memset(ibdev->pnetid[ibport], 0,
156 ibdev->pnetid_by_user[ibport] = false;
161 mutex_unlock(&smc_ib_devices.mutex);
162 /* remove smcd devices */
163 mutex_lock(&smcd_dev_list.mutex);
164 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
165 if (smcd_dev->pnetid_by_user &&
167 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
168 pr_warn_ratelimited("smc: smcd device %s "
169 "erased user defined pnetid "
170 "%.16s\n", dev_name(&smcd_dev->dev),
172 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
173 smcd_dev->pnetid_by_user = false;
177 mutex_unlock(&smcd_dev_list.mutex);
181 /* Add the reference to a given network device to the pnet table.
183 static int smc_pnet_add_by_ndev(struct net_device *ndev)
185 struct smc_pnetentry *pnetelem, *tmp_pe;
186 struct smc_pnettable *pnettable;
187 struct net *net = dev_net(ndev);
191 /* get pnettable for namespace */
192 sn = net_generic(net, smc_net_id);
193 pnettable = &sn->pnettable;
195 mutex_lock(&pnettable->lock);
196 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
197 if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev &&
198 !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) {
199 dev_hold_track(ndev, &pnetelem->dev_tracker, GFP_ATOMIC);
200 pnetelem->ndev = ndev;
202 pr_warn_ratelimited("smc: adding net device %s with "
203 "user defined pnetid %.16s\n",
205 pnetelem->pnet_name);
209 mutex_unlock(&pnettable->lock);
213 /* Remove the reference to a given network device from the pnet table.
215 static int smc_pnet_remove_by_ndev(struct net_device *ndev)
217 struct smc_pnetentry *pnetelem, *tmp_pe;
218 struct smc_pnettable *pnettable;
219 struct net *net = dev_net(ndev);
223 /* get pnettable for namespace */
224 sn = net_generic(net, smc_net_id);
225 pnettable = &sn->pnettable;
227 mutex_lock(&pnettable->lock);
228 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
229 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) {
230 dev_put_track(pnetelem->ndev, &pnetelem->dev_tracker);
231 pnetelem->ndev = NULL;
233 pr_warn_ratelimited("smc: removing net device %s with "
234 "user defined pnetid %.16s\n",
236 pnetelem->pnet_name);
240 mutex_unlock(&pnettable->lock);
244 /* Apply pnetid to ib device when no pnetid is set.
246 static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port,
249 bool applied = false;
251 mutex_lock(&smc_ib_devices.mutex);
252 if (!smc_pnet_is_pnetid_set(ib_dev->pnetid[ib_port - 1])) {
253 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name,
255 ib_dev->pnetid_by_user[ib_port - 1] = true;
258 mutex_unlock(&smc_ib_devices.mutex);
262 /* Apply pnetid to smcd device when no pnetid is set.
264 static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name)
266 bool applied = false;
268 mutex_lock(&smcd_dev_list.mutex);
269 if (!smc_pnet_is_pnetid_set(smcd_dev->pnetid)) {
270 memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN);
271 smcd_dev->pnetid_by_user = true;
274 mutex_unlock(&smcd_dev_list.mutex);
278 /* The limit for pnetid is 16 characters.
279 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
280 * Lower case letters are converted to upper case.
281 * Interior blanks should not be used.
283 static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
285 char *bf = skip_spaces(pnet_name);
286 size_t len = strlen(bf);
287 char *end = bf + len;
291 while (--end >= bf && isspace(*end))
293 if (end - bf >= SMC_MAX_PNETID_LEN)
298 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
305 /* Find an infiniband device by a given name. The device might not exist. */
306 static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
308 struct smc_ib_device *ibdev;
310 mutex_lock(&smc_ib_devices.mutex);
311 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
312 if (!strncmp(ibdev->ibdev->name, ib_name,
313 sizeof(ibdev->ibdev->name)) ||
314 (ibdev->ibdev->dev.parent &&
315 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name,
316 IB_DEVICE_NAME_MAX - 1))) {
322 mutex_unlock(&smc_ib_devices.mutex);
326 /* Find an smcd device by a given name. The device might not exist. */
327 static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
329 struct smcd_dev *smcd_dev;
331 mutex_lock(&smcd_dev_list.mutex);
332 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
333 if (!strncmp(dev_name(&smcd_dev->dev), smcd_name,
334 IB_DEVICE_NAME_MAX - 1))
339 mutex_unlock(&smcd_dev_list.mutex);
343 static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
344 char *eth_name, char *pnet_name)
346 struct smc_pnetentry *tmp_pe, *new_pe;
347 struct net_device *ndev, *base_ndev;
348 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
352 /* check if (base) netdev already has a pnetid. If there is one, we do
353 * not want to add a pnet table entry
356 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */
358 base_ndev = pnet_find_base_ndev(ndev);
359 if (!smc_pnetid_by_dev_port(base_ndev->dev.parent,
360 base_ndev->dev_port, ndev_pnetid))
364 /* add a new netdev entry to the pnet table if there isn't one */
366 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
369 new_pe->type = SMC_PNET_ETH;
370 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
371 strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
374 mutex_lock(&pnettable->lock);
375 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
376 if (tmp_pe->type == SMC_PNET_ETH &&
377 !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) {
385 netdev_tracker_alloc(ndev, &new_pe->dev_tracker,
388 list_add_tail(&new_pe->list, &pnettable->pnetlist);
389 mutex_unlock(&pnettable->lock);
391 mutex_unlock(&pnettable->lock);
396 pr_warn_ratelimited("smc: net device %s "
397 "applied user defined pnetid %.16s\n",
398 new_pe->eth_name, new_pe->pnet_name);
406 static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,
407 u8 ib_port, char *pnet_name)
409 struct smc_pnetentry *tmp_pe, *new_pe;
410 struct smc_ib_device *ib_dev;
411 bool smcddev_applied = true;
412 bool ibdev_applied = true;
413 struct smcd_dev *smcd_dev;
416 /* try to apply the pnetid to active devices */
417 ib_dev = smc_pnet_find_ib(ib_name);
419 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name);
421 pr_warn_ratelimited("smc: ib device %s ibport %d "
422 "applied user defined pnetid "
423 "%.16s\n", ib_dev->ibdev->name,
425 ib_dev->pnetid[ib_port - 1]);
427 smcd_dev = smc_pnet_find_smcd(ib_name);
429 smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name);
431 pr_warn_ratelimited("smc: smcd device %s "
432 "applied user defined pnetid "
433 "%.16s\n", dev_name(&smcd_dev->dev),
436 /* Apply fails when a device has a hardware-defined pnetid set, do not
437 * add a pnet table entry in that case.
439 if (!ibdev_applied || !smcddev_applied)
442 /* add a new ib entry to the pnet table if there isn't one */
443 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
446 new_pe->type = SMC_PNET_IB;
447 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
448 strncpy(new_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX);
449 new_pe->ib_port = ib_port;
452 mutex_lock(&pnettable->lock);
453 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
454 if (tmp_pe->type == SMC_PNET_IB &&
455 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
461 list_add_tail(&new_pe->list, &pnettable->pnetlist);
462 mutex_unlock(&pnettable->lock);
464 mutex_unlock(&pnettable->lock);
467 return (new_ibdev) ? 0 : -EEXIST;
470 /* Append a pnetid to the end of the pnet table if not already on this list.
472 static int smc_pnet_enter(struct net *net, struct nlattr *tb[])
474 char pnet_name[SMC_MAX_PNETID_LEN + 1];
475 struct smc_pnettable *pnettable;
476 bool new_netdev = false;
477 bool new_ibdev = false;
483 /* get pnettable for namespace */
484 sn = net_generic(net, smc_net_id);
485 pnettable = &sn->pnettable;
488 if (!tb[SMC_PNETID_NAME])
490 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
491 if (!smc_pnetid_valid(string, pnet_name))
494 if (tb[SMC_PNETID_ETHNAME]) {
495 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
496 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name);
499 else if (rc != -EEXIST)
503 /* if this is not the initial namespace, stop here */
504 if (net != &init_net)
505 return new_netdev ? 0 : -EEXIST;
508 if (tb[SMC_PNETID_IBNAME]) {
509 string = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
510 string = strim(string);
511 if (tb[SMC_PNETID_IBPORT]) {
512 ibport = nla_get_u8(tb[SMC_PNETID_IBPORT]);
513 if (ibport < 1 || ibport > SMC_MAX_PORTS)
516 rc = smc_pnet_add_ib(pnettable, string, ibport, pnet_name);
519 else if (rc != -EEXIST)
522 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
528 /* Convert an smc_pnetentry to a netlink attribute sequence */
529 static int smc_pnet_set_nla(struct sk_buff *msg,
530 struct smc_pnetentry *pnetelem)
532 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
534 if (pnetelem->type == SMC_PNET_ETH) {
535 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
539 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
542 if (pnetelem->type == SMC_PNET_IB) {
543 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) ||
544 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
547 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
548 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
555 static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
557 struct net *net = genl_info_net(info);
559 return smc_pnet_enter(net, info->attrs);
562 static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
564 struct net *net = genl_info_net(info);
566 if (!info->attrs[SMC_PNETID_NAME])
568 return smc_pnet_remove_by_pnetid(net,
569 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
572 static int smc_pnet_dump_start(struct netlink_callback *cb)
578 static int smc_pnet_dumpinfo(struct sk_buff *skb,
579 u32 portid, u32 seq, u32 flags,
580 struct smc_pnetentry *pnetelem)
584 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
585 flags, SMC_PNETID_GET);
588 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
589 genlmsg_cancel(skb, hdr);
592 genlmsg_end(skb, hdr);
596 static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid,
597 u32 seq, u8 *pnetid, int start_idx)
599 struct smc_pnettable *pnettable;
600 struct smc_pnetentry *pnetelem;
604 /* get pnettable for namespace */
605 sn = net_generic(net, smc_net_id);
606 pnettable = &sn->pnettable;
608 /* dump pnettable entries */
609 mutex_lock(&pnettable->lock);
610 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
611 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
613 if (idx++ < start_idx)
615 /* if this is not the initial namespace, dump only netdev */
616 if (net != &init_net && pnetelem->type != SMC_PNET_ETH)
618 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
624 mutex_unlock(&pnettable->lock);
628 static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
630 struct net *net = sock_net(skb->sk);
633 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid,
634 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
640 /* Retrieve one PNETID entry */
641 static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
643 struct net *net = genl_info_net(info);
647 if (!info->attrs[SMC_PNETID_NAME])
650 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
654 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq,
655 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
657 /* finish multi part message and send it */
658 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
664 return genlmsg_reply(msg, info);
667 /* Remove and delete all pnetids from pnet table.
669 static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
671 struct net *net = genl_info_net(info);
673 smc_pnet_remove_by_pnetid(net, NULL);
677 /* SMC_PNETID generic netlink operation definition */
678 static const struct genl_ops smc_pnet_ops[] = {
680 .cmd = SMC_PNETID_GET,
681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
682 /* can be retrieved by unprivileged users */
683 .doit = smc_pnet_get,
684 .dumpit = smc_pnet_dump,
685 .start = smc_pnet_dump_start
688 .cmd = SMC_PNETID_ADD,
689 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
690 .flags = GENL_ADMIN_PERM,
694 .cmd = SMC_PNETID_DEL,
695 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
696 .flags = GENL_ADMIN_PERM,
700 .cmd = SMC_PNETID_FLUSH,
701 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
702 .flags = GENL_ADMIN_PERM,
703 .doit = smc_pnet_flush
707 /* SMC_PNETID family definition */
708 static struct genl_family smc_pnet_nl_family __ro_after_init = {
710 .name = SMCR_GENL_FAMILY_NAME,
711 .version = SMCR_GENL_FAMILY_VERSION,
712 .maxattr = SMC_PNETID_MAX,
713 .policy = smc_pnet_policy,
715 .module = THIS_MODULE,
717 .n_ops = ARRAY_SIZE(smc_pnet_ops)
720 bool smc_pnet_is_ndev_pnetid(struct net *net, u8 *pnetid)
722 struct smc_net *sn = net_generic(net, smc_net_id);
723 struct smc_pnetids_ndev_entry *pe;
726 read_lock(&sn->pnetids_ndev.lock);
727 list_for_each_entry(pe, &sn->pnetids_ndev.list, list) {
728 if (smc_pnet_match(pnetid, pe->pnetid)) {
735 read_unlock(&sn->pnetids_ndev.lock);
739 static int smc_pnet_add_pnetid(struct net *net, u8 *pnetid)
741 struct smc_net *sn = net_generic(net, smc_net_id);
742 struct smc_pnetids_ndev_entry *pe, *pi;
744 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
748 write_lock(&sn->pnetids_ndev.lock);
749 list_for_each_entry(pi, &sn->pnetids_ndev.list, list) {
750 if (smc_pnet_match(pnetid, pe->pnetid)) {
751 refcount_inc(&pi->refcnt);
756 refcount_set(&pe->refcnt, 1);
757 memcpy(pe->pnetid, pnetid, SMC_MAX_PNETID_LEN);
758 list_add_tail(&pe->list, &sn->pnetids_ndev.list);
761 write_unlock(&sn->pnetids_ndev.lock);
765 static void smc_pnet_remove_pnetid(struct net *net, u8 *pnetid)
767 struct smc_net *sn = net_generic(net, smc_net_id);
768 struct smc_pnetids_ndev_entry *pe, *pe2;
770 write_lock(&sn->pnetids_ndev.lock);
771 list_for_each_entry_safe(pe, pe2, &sn->pnetids_ndev.list, list) {
772 if (smc_pnet_match(pnetid, pe->pnetid)) {
773 if (refcount_dec_and_test(&pe->refcnt)) {
780 write_unlock(&sn->pnetids_ndev.lock);
783 static void smc_pnet_add_base_pnetid(struct net *net, struct net_device *dev,
786 struct net_device *base_dev;
788 base_dev = __pnet_find_base_ndev(dev);
789 if (base_dev->flags & IFF_UP &&
790 !smc_pnetid_by_dev_port(base_dev->dev.parent, base_dev->dev_port,
792 /* add to PNETIDs list */
793 smc_pnet_add_pnetid(net, ndev_pnetid);
797 /* create initial list of netdevice pnetids */
798 static void smc_pnet_create_pnetids_list(struct net *net)
800 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
801 struct net_device *dev;
804 for_each_netdev(net, dev)
805 smc_pnet_add_base_pnetid(net, dev, ndev_pnetid);
809 /* clean up list of netdevice pnetids */
810 static void smc_pnet_destroy_pnetids_list(struct net *net)
812 struct smc_net *sn = net_generic(net, smc_net_id);
813 struct smc_pnetids_ndev_entry *pe, *temp_pe;
815 write_lock(&sn->pnetids_ndev.lock);
816 list_for_each_entry_safe(pe, temp_pe, &sn->pnetids_ndev.list, list) {
820 write_unlock(&sn->pnetids_ndev.lock);
823 static int smc_pnet_netdev_event(struct notifier_block *this,
824 unsigned long event, void *ptr)
826 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
827 struct net *net = dev_net(event_dev);
828 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
832 case NETDEV_UNREGISTER:
833 smc_pnet_remove_by_ndev(event_dev);
834 smc_ib_ndev_change(event_dev, event);
836 case NETDEV_REGISTER:
837 smc_pnet_add_by_ndev(event_dev);
838 smc_ib_ndev_change(event_dev, event);
841 smc_pnet_add_base_pnetid(net, event_dev, ndev_pnetid);
844 event_dev = __pnet_find_base_ndev(event_dev);
845 if (!smc_pnetid_by_dev_port(event_dev->dev.parent,
846 event_dev->dev_port, ndev_pnetid)) {
847 /* remove from PNETIDs list */
848 smc_pnet_remove_pnetid(net, ndev_pnetid);
856 static struct notifier_block smc_netdev_notifier = {
857 .notifier_call = smc_pnet_netdev_event
860 /* init network namespace */
861 int smc_pnet_net_init(struct net *net)
863 struct smc_net *sn = net_generic(net, smc_net_id);
864 struct smc_pnettable *pnettable = &sn->pnettable;
865 struct smc_pnetids_ndev *pnetids_ndev = &sn->pnetids_ndev;
867 INIT_LIST_HEAD(&pnettable->pnetlist);
868 mutex_init(&pnettable->lock);
869 INIT_LIST_HEAD(&pnetids_ndev->list);
870 rwlock_init(&pnetids_ndev->lock);
872 smc_pnet_create_pnetids_list(net);
874 /* disable handshake limitation by default */
875 net->smc.limit_smc_hs = 0;
880 int __init smc_pnet_init(void)
884 rc = genl_register_family(&smc_pnet_nl_family);
887 rc = register_netdevice_notifier(&smc_netdev_notifier);
889 genl_unregister_family(&smc_pnet_nl_family);
894 /* exit network namespace */
895 void smc_pnet_net_exit(struct net *net)
897 /* flush pnet table */
898 smc_pnet_remove_by_pnetid(net, NULL);
899 smc_pnet_destroy_pnetids_list(net);
902 void smc_pnet_exit(void)
904 unregister_netdevice_notifier(&smc_netdev_notifier);
905 genl_unregister_family(&smc_pnet_nl_family);
908 static struct net_device *__pnet_find_base_ndev(struct net_device *ndev)
913 nest_lvl = ndev->lower_level;
914 for (i = 0; i < nest_lvl; i++) {
915 struct list_head *lower = &ndev->adj_list.lower;
917 if (list_empty(lower))
920 ndev = netdev_lower_get_next(ndev, &lower);
925 /* Determine one base device for stacked net devices.
926 * If the lower device level contains more than one devices
927 * (for instance with bonding slaves), just the first device
928 * is used to reach a base device.
930 static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
933 ndev = __pnet_find_base_ndev(ndev);
938 static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev,
941 struct smc_pnettable *pnettable;
942 struct net *net = dev_net(ndev);
943 struct smc_pnetentry *pnetelem;
947 /* get pnettable for namespace */
948 sn = net_generic(net, smc_net_id);
949 pnettable = &sn->pnettable;
951 mutex_lock(&pnettable->lock);
952 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
953 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) {
954 /* get pnetid of netdev device */
955 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
960 mutex_unlock(&pnettable->lock);
964 static int smc_pnet_determine_gid(struct smc_ib_device *ibdev, int i,
965 struct smc_init_info *ini)
967 if (!ini->check_smcrv2 &&
968 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->ib_gid, NULL,
974 if (ini->check_smcrv2 &&
975 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->smcrv2.ib_gid_v2,
976 NULL, &ini->smcrv2)) {
977 ini->smcrv2.ib_dev_v2 = ibdev;
978 ini->smcrv2.ib_port_v2 = i;
984 /* find a roce device for the given pnetid */
985 static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id,
986 struct smc_init_info *ini,
987 struct smc_ib_device *known_dev,
990 struct smc_ib_device *ibdev;
993 mutex_lock(&smc_ib_devices.mutex);
994 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
995 if (ibdev == known_dev ||
996 !rdma_dev_access_netns(ibdev->ibdev, net))
998 for (i = 1; i <= SMC_MAX_PORTS; i++) {
999 if (!rdma_is_port_valid(ibdev->ibdev, i))
1001 if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) &&
1002 smc_ib_port_active(ibdev, i) &&
1003 !test_bit(i - 1, ibdev->ports_going_away)) {
1004 if (!smc_pnet_determine_gid(ibdev, i, ini))
1010 mutex_unlock(&smc_ib_devices.mutex);
1013 /* find alternate roce device with same pnet_id, vlan_id and net namespace */
1014 void smc_pnet_find_alt_roce(struct smc_link_group *lgr,
1015 struct smc_init_info *ini,
1016 struct smc_ib_device *known_dev)
1018 struct net *net = lgr->net;
1020 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev, net);
1023 /* if handshake network device belongs to a roce device, return its
1024 * IB device and port
1026 static void smc_pnet_find_rdma_dev(struct net_device *netdev,
1027 struct smc_init_info *ini)
1029 struct net *net = dev_net(netdev);
1030 struct smc_ib_device *ibdev;
1032 mutex_lock(&smc_ib_devices.mutex);
1033 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
1034 struct net_device *ndev;
1037 /* check rdma net namespace */
1038 if (!rdma_dev_access_netns(ibdev->ibdev, net))
1041 for (i = 1; i <= SMC_MAX_PORTS; i++) {
1042 if (!rdma_is_port_valid(ibdev->ibdev, i))
1044 if (!ibdev->ibdev->ops.get_netdev)
1046 ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i);
1050 if (netdev == ndev &&
1051 smc_ib_port_active(ibdev, i) &&
1052 !test_bit(i - 1, ibdev->ports_going_away)) {
1053 if (!smc_pnet_determine_gid(ibdev, i, ini))
1058 mutex_unlock(&smc_ib_devices.mutex);
1061 /* Determine the corresponding IB device port based on the hardware PNETID.
1062 * Searching stops at the first matching active IB device port with vlan_id
1064 * If nothing found, check pnetid table.
1065 * If nothing found, try to use handshake device
1067 static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
1068 struct smc_init_info *ini)
1070 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
1073 ndev = pnet_find_base_ndev(ndev);
1074 net = dev_net(ndev);
1075 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
1077 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) {
1078 smc_pnet_find_rdma_dev(ndev, ini);
1079 return; /* pnetid could not be determined */
1081 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL, net);
1084 static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
1085 struct smc_init_info *ini)
1087 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
1088 struct smcd_dev *ismdev;
1090 ndev = pnet_find_base_ndev(ndev);
1091 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
1093 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
1094 return; /* pnetid could not be determined */
1096 mutex_lock(&smcd_dev_list.mutex);
1097 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
1098 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) &&
1099 !ismdev->going_away &&
1100 (!ini->ism_peer_gid[0] ||
1101 !smc_ism_cantalk(ini->ism_peer_gid[0], ini->vlan_id,
1103 ini->ism_dev[0] = ismdev;
1107 mutex_unlock(&smcd_dev_list.mutex);
1110 /* PNET table analysis for a given sock:
1111 * determine ib_device and port belonging to used internal TCP socket
1112 * ethernet interface.
1114 void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini)
1116 struct dst_entry *dst = sk_dst_get(sk);
1123 smc_pnet_find_roce_by_pnetid(dst->dev, ini);
1131 void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini)
1133 struct dst_entry *dst = sk_dst_get(sk);
1135 ini->ism_dev[0] = NULL;
1141 smc_pnet_find_ism_by_pnetid(dst->dev, ini);
1149 /* Lookup and apply a pnet table entry to the given ib device.
1151 int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)
1153 char *ib_name = smcibdev->ibdev->name;
1154 struct smc_pnettable *pnettable;
1155 struct smc_pnetentry *tmp_pe;
1159 /* get pnettable for init namespace */
1160 sn = net_generic(&init_net, smc_net_id);
1161 pnettable = &sn->pnettable;
1163 mutex_lock(&pnettable->lock);
1164 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1165 if (tmp_pe->type == SMC_PNET_IB &&
1166 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) &&
1167 tmp_pe->ib_port == ib_port) {
1168 smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name);
1173 mutex_unlock(&pnettable->lock);
1178 /* Lookup and apply a pnet table entry to the given smcd device.
1180 int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev)
1182 const char *ib_name = dev_name(&smcddev->dev);
1183 struct smc_pnettable *pnettable;
1184 struct smc_pnetentry *tmp_pe;
1188 /* get pnettable for init namespace */
1189 sn = net_generic(&init_net, smc_net_id);
1190 pnettable = &sn->pnettable;
1192 mutex_lock(&pnettable->lock);
1193 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1194 if (tmp_pe->type == SMC_PNET_IB &&
1195 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
1196 smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name);
1201 mutex_unlock(&pnettable->lock);