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