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