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 <net/netlink.h>
16 #include <net/genetlink.h>
18 #include <uapi/linux/if.h>
19 #include <uapi/linux/smc.h>
21 #include <rdma/ib_verbs.h>
27 #define SMC_ASCII_BLANK 32
29 static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
31 static struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
33 .type = NLA_NUL_STRING,
34 .len = SMC_MAX_PNETID_LEN
36 [SMC_PNETID_ETHNAME] = {
37 .type = NLA_NUL_STRING,
40 [SMC_PNETID_IBNAME] = {
41 .type = NLA_NUL_STRING,
42 .len = IB_DEVICE_NAME_MAX - 1
44 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
47 static struct genl_family smc_pnet_nl_family;
50 * struct smc_pnettable - SMC PNET table anchor
51 * @lock: Lock for list action
52 * @pnetlist: List of PNETIDs
54 static struct smc_pnettable {
56 struct list_head pnetlist;
58 .pnetlist = LIST_HEAD_INIT(smc_pnettable.pnetlist),
59 .lock = __RW_LOCK_UNLOCKED(smc_pnettable.lock)
63 * struct smc_user_pnetentry - pnet identifier name entry for/from user
65 * @pnet_name: Pnet identifier name
66 * @ndev: pointer to network device.
67 * @smcibdev: Pointer to IB device.
68 * @ib_port: Port of IB device.
70 struct smc_user_pnetentry {
71 struct list_head list;
72 char pnet_name[SMC_MAX_PNETID_LEN + 1];
73 struct net_device *ndev;
74 struct smc_ib_device *smcibdev;
78 /* pnet entry stored in pnet table */
79 struct smc_pnetentry {
80 struct list_head list;
81 char pnet_name[SMC_MAX_PNETID_LEN + 1];
82 struct net_device *ndev;
85 /* Check if two given pnetids match */
86 static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
90 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) {
91 if ((pnetid1[i] == 0 || pnetid1[i] == SMC_ASCII_BLANK) &&
92 (pnetid2[i] == 0 || pnetid2[i] == SMC_ASCII_BLANK))
94 if (pnetid1[i] != pnetid2[i])
100 /* Remove a pnetid from the pnet table.
102 static int smc_pnet_remove_by_pnetid(char *pnet_name)
104 struct smc_pnetentry *pnetelem, *tmp_pe;
105 struct smc_ib_device *ibdev;
109 /* remove netdevices */
110 write_lock(&smc_pnettable.lock);
111 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
114 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
115 list_del(&pnetelem->list);
116 dev_put(pnetelem->ndev);
121 write_unlock(&smc_pnettable.lock);
122 /* remove ib devices */
123 spin_lock(&smc_ib_devices.lock);
124 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
125 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
126 if (ibdev->pnetid_by_user[ibport] &&
128 smc_pnet_match(pnet_name,
129 ibdev->pnetid[ibport]))) {
130 memset(ibdev->pnetid[ibport], 0,
132 ibdev->pnetid_by_user[ibport] = false;
137 spin_unlock(&smc_ib_devices.lock);
141 /* Remove a pnet entry mentioning a given network device from the pnet table.
143 static int smc_pnet_remove_by_ndev(struct net_device *ndev)
145 struct smc_pnetentry *pnetelem, *tmp_pe;
148 write_lock(&smc_pnettable.lock);
149 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
151 if (pnetelem->ndev == ndev) {
152 list_del(&pnetelem->list);
153 dev_put(pnetelem->ndev);
159 write_unlock(&smc_pnettable.lock);
163 /* Append a pnetid to the end of the pnet table if not already on this list.
165 static int smc_pnet_enter(struct smc_user_pnetentry *new_pnetelem)
167 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
168 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
169 struct smc_pnetentry *tmp_pnetelem;
170 struct smc_pnetentry *pnetelem;
171 struct net_device *ndev;
172 bool new_netdev = true;
173 bool new_ibdev = false;
175 if (new_pnetelem->smcibdev) {
176 struct smc_ib_device *ib_dev = new_pnetelem->smcibdev;
177 int ib_port = new_pnetelem->ib_port;
179 spin_lock(&smc_ib_devices.lock);
180 if (smc_pnet_match(ib_dev->pnetid[ib_port - 1], pnet_null)) {
181 memcpy(ib_dev->pnetid[ib_port - 1],
182 new_pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
183 ib_dev->pnetid_by_user[ib_port - 1] = true;
186 spin_unlock(&smc_ib_devices.lock);
189 if (!new_pnetelem->ndev)
190 return new_ibdev ? 0 : -EEXIST;
192 /* check if (base) netdev already has a pnetid. If there is one, we do
193 * not want to add a pnet table entry
195 ndev = pnet_find_base_ndev(new_pnetelem->ndev);
196 if (!smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
198 return new_ibdev ? 0 : -EEXIST;
200 /* add a new netdev entry to the pnet table if there isn't one */
201 tmp_pnetelem = kzalloc(sizeof(*pnetelem), GFP_KERNEL);
204 memcpy(tmp_pnetelem->pnet_name, new_pnetelem->pnet_name,
206 tmp_pnetelem->ndev = new_pnetelem->ndev;
208 write_lock(&smc_pnettable.lock);
209 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
210 if (pnetelem->ndev == new_pnetelem->ndev)
214 dev_hold(tmp_pnetelem->ndev);
215 list_add_tail(&tmp_pnetelem->list, &smc_pnettable.pnetlist);
216 write_unlock(&smc_pnettable.lock);
218 write_unlock(&smc_pnettable.lock);
222 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
225 /* The limit for pnetid is 16 characters.
226 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
227 * Lower case letters are converted to upper case.
228 * Interior blanks should not be used.
230 static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
232 char *bf = skip_spaces(pnet_name);
233 size_t len = strlen(bf);
234 char *end = bf + len;
238 while (--end >= bf && isspace(*end))
240 if (end - bf >= SMC_MAX_PNETID_LEN)
245 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
252 /* Find an infiniband device by a given name. The device might not exist. */
253 static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
255 struct smc_ib_device *ibdev;
257 spin_lock(&smc_ib_devices.lock);
258 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
259 if (!strncmp(ibdev->ibdev->name, ib_name,
260 sizeof(ibdev->ibdev->name))) {
266 spin_unlock(&smc_ib_devices.lock);
270 /* Parse the supplied netlink attributes and fill a pnetentry structure.
271 * For ethernet and infiniband device names verify that the devices exist.
273 static int smc_pnet_fill_entry(struct net *net,
274 struct smc_user_pnetentry *pnetelem,
277 char *string, *ibname;
280 memset(pnetelem, 0, sizeof(*pnetelem));
281 INIT_LIST_HEAD(&pnetelem->list);
284 if (!tb[SMC_PNETID_NAME])
286 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
287 if (!smc_pnetid_valid(string, pnetelem->pnet_name))
291 if (tb[SMC_PNETID_ETHNAME]) {
292 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
293 pnetelem->ndev = dev_get_by_name(net, string);
299 if (tb[SMC_PNETID_IBNAME]) {
300 ibname = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
301 ibname = strim(ibname);
302 pnetelem->smcibdev = smc_pnet_find_ib(ibname);
303 if (!pnetelem->smcibdev)
305 if (pnetelem->smcibdev) {
306 if (!tb[SMC_PNETID_IBPORT])
308 pnetelem->ib_port = nla_get_u8(tb[SMC_PNETID_IBPORT]);
309 if (pnetelem->ib_port < 1 ||
310 pnetelem->ib_port > SMC_MAX_PORTS)
319 dev_put(pnetelem->ndev);
323 /* Convert an smc_pnetentry to a netlink attribute sequence */
324 static int smc_pnet_set_nla(struct sk_buff *msg,
325 struct smc_user_pnetentry *pnetelem)
327 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
329 if (pnetelem->ndev) {
330 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
331 pnetelem->ndev->name))
334 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
337 if (pnetelem->smcibdev) {
338 if (nla_put_string(msg, SMC_PNETID_IBNAME,
339 pnetelem->smcibdev->ibdev->name) ||
340 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
343 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
344 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
351 static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
353 struct net *net = genl_info_net(info);
354 struct smc_user_pnetentry pnetelem;
357 rc = smc_pnet_fill_entry(net, &pnetelem, info->attrs);
359 rc = smc_pnet_enter(&pnetelem);
361 dev_put(pnetelem.ndev);
365 static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
367 if (!info->attrs[SMC_PNETID_NAME])
369 return smc_pnet_remove_by_pnetid(
370 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
373 static int smc_pnet_dump_start(struct netlink_callback *cb)
379 static int smc_pnet_dumpinfo(struct sk_buff *skb,
380 u32 portid, u32 seq, u32 flags,
381 struct smc_user_pnetentry *pnetelem)
385 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
386 flags, SMC_PNETID_GET);
389 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
390 genlmsg_cancel(skb, hdr);
393 genlmsg_end(skb, hdr);
397 static int _smc_pnet_dump(struct sk_buff *skb, u32 portid, u32 seq, u8 *pnetid,
400 struct smc_user_pnetentry tmp_entry;
401 struct smc_pnetentry *pnetelem;
402 struct smc_ib_device *ibdev;
406 /* dump netdevices */
407 read_lock(&smc_pnettable.lock);
408 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
409 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
411 if (idx++ < start_idx)
413 memset(&tmp_entry, 0, sizeof(tmp_entry));
414 memcpy(&tmp_entry.pnet_name, pnetelem->pnet_name,
416 tmp_entry.ndev = pnetelem->ndev;
417 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
423 read_unlock(&smc_pnettable.lock);
425 /* dump ib devices */
426 spin_lock(&smc_ib_devices.lock);
427 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
428 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
429 if (ibdev->pnetid_by_user[ibport]) {
431 !smc_pnet_match(ibdev->pnetid[ibport],
434 if (idx++ < start_idx)
436 memset(&tmp_entry, 0, sizeof(tmp_entry));
437 memcpy(&tmp_entry.pnet_name,
438 ibdev->pnetid[ibport],
440 tmp_entry.smcibdev = ibdev;
441 tmp_entry.ib_port = ibport + 1;
442 if (smc_pnet_dumpinfo(skb, portid, seq,
451 spin_unlock(&smc_ib_devices.lock);
456 static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
460 idx = _smc_pnet_dump(skb, NETLINK_CB(cb->skb).portid,
461 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
467 /* Retrieve one PNETID entry */
468 static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
473 if (!info->attrs[SMC_PNETID_NAME])
476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
480 _smc_pnet_dump(msg, info->snd_portid, info->snd_seq,
481 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
483 /* finish multi part message and send it */
484 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
490 return genlmsg_reply(msg, info);
493 /* Remove and delete all pnetids from pnet table.
495 static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
497 return smc_pnet_remove_by_pnetid(NULL);
500 /* SMC_PNETID generic netlink operation definition */
501 static const struct genl_ops smc_pnet_ops[] = {
503 .cmd = SMC_PNETID_GET,
504 .flags = GENL_ADMIN_PERM,
505 .policy = smc_pnet_policy,
506 .doit = smc_pnet_get,
507 .dumpit = smc_pnet_dump,
508 .start = smc_pnet_dump_start
511 .cmd = SMC_PNETID_ADD,
512 .flags = GENL_ADMIN_PERM,
513 .policy = smc_pnet_policy,
517 .cmd = SMC_PNETID_DEL,
518 .flags = GENL_ADMIN_PERM,
519 .policy = smc_pnet_policy,
523 .cmd = SMC_PNETID_FLUSH,
524 .flags = GENL_ADMIN_PERM,
525 .policy = smc_pnet_policy,
526 .doit = smc_pnet_flush
530 /* SMC_PNETID family definition */
531 static struct genl_family smc_pnet_nl_family __ro_after_init = {
533 .name = SMCR_GENL_FAMILY_NAME,
534 .version = SMCR_GENL_FAMILY_VERSION,
535 .maxattr = SMC_PNETID_MAX,
537 .module = THIS_MODULE,
539 .n_ops = ARRAY_SIZE(smc_pnet_ops)
542 static int smc_pnet_netdev_event(struct notifier_block *this,
543 unsigned long event, void *ptr)
545 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
549 case NETDEV_UNREGISTER:
550 smc_pnet_remove_by_ndev(event_dev);
557 static struct notifier_block smc_netdev_notifier = {
558 .notifier_call = smc_pnet_netdev_event
561 int __init smc_pnet_init(void)
565 rc = genl_register_family(&smc_pnet_nl_family);
568 rc = register_netdevice_notifier(&smc_netdev_notifier);
570 genl_unregister_family(&smc_pnet_nl_family);
574 void smc_pnet_exit(void)
576 smc_pnet_flush(NULL, NULL);
577 unregister_netdevice_notifier(&smc_netdev_notifier);
578 genl_unregister_family(&smc_pnet_nl_family);
581 /* Determine one base device for stacked net devices.
582 * If the lower device level contains more than one devices
583 * (for instance with bonding slaves), just the first device
584 * is used to reach a base device.
586 static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
591 nest_lvl = dev_get_nest_level(ndev);
592 for (i = 0; i < nest_lvl; i++) {
593 struct list_head *lower = &ndev->adj_list.lower;
595 if (list_empty(lower))
598 ndev = netdev_lower_get_next(ndev, &lower);
604 static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *netdev,
607 struct smc_pnetentry *pnetelem;
610 read_lock(&smc_pnettable.lock);
611 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
612 if (netdev == pnetelem->ndev) {
613 /* get pnetid of netdev device */
614 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
619 read_unlock(&smc_pnettable.lock);
623 /* Determine the corresponding IB device port based on the hardware PNETID.
624 * Searching stops at the first matching active IB device port with vlan_id
627 static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
628 struct smc_ib_device **smcibdev,
629 u8 *ibport, unsigned short vlan_id,
632 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
633 struct smc_ib_device *ibdev;
636 ndev = pnet_find_base_ndev(ndev);
637 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
639 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
640 return; /* pnetid could not be determined */
642 spin_lock(&smc_ib_devices.lock);
643 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
644 for (i = 1; i <= SMC_MAX_PORTS; i++) {
645 if (!rdma_is_port_valid(ibdev->ibdev, i))
647 if (smc_pnet_match(ibdev->pnetid[i - 1], ndev_pnetid) &&
648 smc_ib_port_active(ibdev, i) &&
649 !smc_ib_determine_gid(ibdev, i, vlan_id, gid,
658 spin_unlock(&smc_ib_devices.lock);
661 static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
662 struct smcd_dev **smcismdev)
664 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
665 struct smcd_dev *ismdev;
667 ndev = pnet_find_base_ndev(ndev);
668 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
670 return; /* pnetid could not be determined */
672 spin_lock(&smcd_dev_list.lock);
673 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
674 if (!memcmp(ismdev->pnetid, ndev_pnetid, SMC_MAX_PNETID_LEN)) {
679 spin_unlock(&smcd_dev_list.lock);
682 /* PNET table analysis for a given sock:
683 * determine ib_device and port belonging to used internal TCP socket
684 * ethernet interface.
686 void smc_pnet_find_roce_resource(struct sock *sk,
687 struct smc_ib_device **smcibdev, u8 *ibport,
688 unsigned short vlan_id, u8 gid[])
690 struct dst_entry *dst = sk_dst_get(sk);
700 smc_pnet_find_roce_by_pnetid(dst->dev, smcibdev, ibport, vlan_id, gid);
708 void smc_pnet_find_ism_resource(struct sock *sk, struct smcd_dev **smcismdev)
710 struct dst_entry *dst = sk_dst_get(sk);
718 /* if possible, lookup via hardware-defined pnetid */
719 smc_pnet_find_ism_by_pnetid(dst->dev, smcismdev);