]> git.ipfire.org Git - people/arne_f/kernel.git/blob - net/smc/smc_pnet.c
Merge remote-tracking branches 'asoc/fix/topology', 'asoc/fix/adau17x1', 'asoc/fix...
[people/arne_f/kernel.git] / net / smc / smc_pnet.c
1 /*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * Generic netlink support functions to configure an SMC-R PNET table
5 *
6 * Copyright IBM Corp. 2016
7 *
8 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com>
9 */
10
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/ctype.h>
14 #include <net/netlink.h>
15 #include <net/genetlink.h>
16
17 #include <uapi/linux/if.h>
18 #include <uapi/linux/smc.h>
19
20 #include <rdma/ib_verbs.h>
21
22 #include "smc_pnet.h"
23 #include "smc_ib.h"
24
25 #define SMC_MAX_PNET_ID_LEN 16 /* Max. length of PNET id */
26
27 static struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
28 [SMC_PNETID_NAME] = {
29 .type = NLA_NUL_STRING,
30 .len = SMC_MAX_PNET_ID_LEN - 1
31 },
32 [SMC_PNETID_ETHNAME] = {
33 .type = NLA_NUL_STRING,
34 .len = IFNAMSIZ - 1
35 },
36 [SMC_PNETID_IBNAME] = {
37 .type = NLA_NUL_STRING,
38 .len = IB_DEVICE_NAME_MAX - 1
39 },
40 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
41 };
42
43 static struct genl_family smc_pnet_nl_family;
44
45 /**
46 * struct smc_pnettable - SMC PNET table anchor
47 * @lock: Lock for list action
48 * @pnetlist: List of PNETIDs
49 */
50 static struct smc_pnettable {
51 rwlock_t lock;
52 struct list_head pnetlist;
53 } smc_pnettable = {
54 .pnetlist = LIST_HEAD_INIT(smc_pnettable.pnetlist),
55 .lock = __RW_LOCK_UNLOCKED(smc_pnettable.lock)
56 };
57
58 /**
59 * struct smc_pnetentry - pnet identifier name entry
60 * @list: List node.
61 * @pnet_name: Pnet identifier name
62 * @ndev: pointer to network device.
63 * @smcibdev: Pointer to IB device.
64 */
65 struct smc_pnetentry {
66 struct list_head list;
67 char pnet_name[SMC_MAX_PNET_ID_LEN + 1];
68 struct net_device *ndev;
69 struct smc_ib_device *smcibdev;
70 u8 ib_port;
71 };
72
73 /* Check if two RDMA device entries are identical. Use device name and port
74 * number for comparison.
75 */
76 static bool smc_pnet_same_ibname(struct smc_pnetentry *pnetelem, char *ibname,
77 u8 ibport)
78 {
79 return pnetelem->ib_port == ibport &&
80 !strncmp(pnetelem->smcibdev->ibdev->name, ibname,
81 sizeof(pnetelem->smcibdev->ibdev->name));
82 }
83
84 /* Find a pnetid in the pnet table.
85 */
86 static struct smc_pnetentry *smc_pnet_find_pnetid(char *pnet_name)
87 {
88 struct smc_pnetentry *pnetelem, *found_pnetelem = NULL;
89
90 read_lock(&smc_pnettable.lock);
91 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
92 if (!strncmp(pnetelem->pnet_name, pnet_name,
93 sizeof(pnetelem->pnet_name))) {
94 found_pnetelem = pnetelem;
95 break;
96 }
97 }
98 read_unlock(&smc_pnettable.lock);
99 return found_pnetelem;
100 }
101
102 /* Remove a pnetid from the pnet table.
103 */
104 static int smc_pnet_remove_by_pnetid(char *pnet_name)
105 {
106 struct smc_pnetentry *pnetelem, *tmp_pe;
107 int rc = -ENOENT;
108
109 write_lock(&smc_pnettable.lock);
110 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
111 list) {
112 if (!strncmp(pnetelem->pnet_name, pnet_name,
113 sizeof(pnetelem->pnet_name))) {
114 list_del(&pnetelem->list);
115 dev_put(pnetelem->ndev);
116 kfree(pnetelem);
117 rc = 0;
118 break;
119 }
120 }
121 write_unlock(&smc_pnettable.lock);
122 return rc;
123 }
124
125 /* Remove a pnet entry mentioning a given network device from the pnet table.
126 */
127 static int smc_pnet_remove_by_ndev(struct net_device *ndev)
128 {
129 struct smc_pnetentry *pnetelem, *tmp_pe;
130 int rc = -ENOENT;
131
132 write_lock(&smc_pnettable.lock);
133 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
134 list) {
135 if (pnetelem->ndev == ndev) {
136 list_del(&pnetelem->list);
137 dev_put(pnetelem->ndev);
138 kfree(pnetelem);
139 rc = 0;
140 break;
141 }
142 }
143 write_unlock(&smc_pnettable.lock);
144 return rc;
145 }
146
147 /* Remove a pnet entry mentioning a given ib device from the pnet table.
148 */
149 int smc_pnet_remove_by_ibdev(struct smc_ib_device *ibdev)
150 {
151 struct smc_pnetentry *pnetelem, *tmp_pe;
152 int rc = -ENOENT;
153
154 write_lock(&smc_pnettable.lock);
155 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
156 list) {
157 if (pnetelem->smcibdev == ibdev) {
158 list_del(&pnetelem->list);
159 dev_put(pnetelem->ndev);
160 kfree(pnetelem);
161 rc = 0;
162 break;
163 }
164 }
165 write_unlock(&smc_pnettable.lock);
166 return rc;
167 }
168
169 /* Append a pnetid to the end of the pnet table if not already on this list.
170 */
171 static int smc_pnet_enter(struct smc_pnetentry *new_pnetelem)
172 {
173 struct smc_pnetentry *pnetelem;
174 int rc = -EEXIST;
175
176 write_lock(&smc_pnettable.lock);
177 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
178 if (!strncmp(pnetelem->pnet_name, new_pnetelem->pnet_name,
179 sizeof(new_pnetelem->pnet_name)) ||
180 !strncmp(pnetelem->ndev->name, new_pnetelem->ndev->name,
181 sizeof(new_pnetelem->ndev->name)) ||
182 smc_pnet_same_ibname(pnetelem,
183 new_pnetelem->smcibdev->ibdev->name,
184 new_pnetelem->ib_port)) {
185 dev_put(pnetelem->ndev);
186 goto found;
187 }
188 }
189 list_add_tail(&new_pnetelem->list, &smc_pnettable.pnetlist);
190 rc = 0;
191 found:
192 write_unlock(&smc_pnettable.lock);
193 return rc;
194 }
195
196 /* The limit for pnetid is 16 characters.
197 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
198 * Lower case letters are converted to upper case.
199 * Interior blanks should not be used.
200 */
201 static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
202 {
203 char *bf = skip_spaces(pnet_name);
204 size_t len = strlen(bf);
205 char *end = bf + len;
206
207 if (!len)
208 return false;
209 while (--end >= bf && isspace(*end))
210 ;
211 if (end - bf >= SMC_MAX_PNET_ID_LEN)
212 return false;
213 while (bf <= end) {
214 if (!isalnum(*bf))
215 return false;
216 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
217 bf++;
218 }
219 *pnetid = '\0';
220 return true;
221 }
222
223 /* Find an infiniband device by a given name. The device might not exist. */
224 static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
225 {
226 struct smc_ib_device *ibdev;
227
228 spin_lock(&smc_ib_devices.lock);
229 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
230 if (!strncmp(ibdev->ibdev->name, ib_name,
231 sizeof(ibdev->ibdev->name))) {
232 goto out;
233 }
234 }
235 ibdev = NULL;
236 out:
237 spin_unlock(&smc_ib_devices.lock);
238 return ibdev;
239 }
240
241 /* Parse the supplied netlink attributes and fill a pnetentry structure.
242 * For ethernet and infiniband device names verify that the devices exist.
243 */
244 static int smc_pnet_fill_entry(struct net *net, struct smc_pnetentry *pnetelem,
245 struct nlattr *tb[])
246 {
247 char *string, *ibname = NULL;
248 int rc = 0;
249
250 memset(pnetelem, 0, sizeof(*pnetelem));
251 INIT_LIST_HEAD(&pnetelem->list);
252 if (tb[SMC_PNETID_NAME]) {
253 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
254 if (!smc_pnetid_valid(string, pnetelem->pnet_name)) {
255 rc = -EINVAL;
256 goto error;
257 }
258 }
259 if (tb[SMC_PNETID_ETHNAME]) {
260 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
261 pnetelem->ndev = dev_get_by_name(net, string);
262 if (!pnetelem->ndev)
263 return -ENOENT;
264 }
265 if (tb[SMC_PNETID_IBNAME]) {
266 ibname = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
267 ibname = strim(ibname);
268 pnetelem->smcibdev = smc_pnet_find_ib(ibname);
269 if (!pnetelem->smcibdev) {
270 rc = -ENOENT;
271 goto error;
272 }
273 }
274 if (tb[SMC_PNETID_IBPORT]) {
275 pnetelem->ib_port = nla_get_u8(tb[SMC_PNETID_IBPORT]);
276 if (pnetelem->ib_port > SMC_MAX_PORTS) {
277 rc = -EINVAL;
278 goto error;
279 }
280 }
281 return 0;
282
283 error:
284 if (pnetelem->ndev)
285 dev_put(pnetelem->ndev);
286 return rc;
287 }
288
289 /* Convert an smc_pnetentry to a netlink attribute sequence */
290 static int smc_pnet_set_nla(struct sk_buff *msg, struct smc_pnetentry *pnetelem)
291 {
292 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name) ||
293 nla_put_string(msg, SMC_PNETID_ETHNAME, pnetelem->ndev->name) ||
294 nla_put_string(msg, SMC_PNETID_IBNAME,
295 pnetelem->smcibdev->ibdev->name) ||
296 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
297 return -1;
298 return 0;
299 }
300
301 /* Retrieve one PNETID entry */
302 static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
303 {
304 struct smc_pnetentry *pnetelem;
305 struct sk_buff *msg;
306 void *hdr;
307 int rc;
308
309 pnetelem = smc_pnet_find_pnetid(
310 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
311 if (!pnetelem)
312 return -ENOENT;
313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
314 if (!msg)
315 return -ENOMEM;
316
317 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
318 &smc_pnet_nl_family, 0, SMC_PNETID_GET);
319 if (!hdr) {
320 rc = -EMSGSIZE;
321 goto err_out;
322 }
323
324 if (smc_pnet_set_nla(msg, pnetelem)) {
325 rc = -ENOBUFS;
326 goto err_out;
327 }
328
329 genlmsg_end(msg, hdr);
330 return genlmsg_reply(msg, info);
331
332 err_out:
333 nlmsg_free(msg);
334 return rc;
335 }
336
337 static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
338 {
339 struct net *net = genl_info_net(info);
340 struct smc_pnetentry *pnetelem;
341 int rc;
342
343 pnetelem = kzalloc(sizeof(*pnetelem), GFP_KERNEL);
344 if (!pnetelem)
345 return -ENOMEM;
346 rc = smc_pnet_fill_entry(net, pnetelem, info->attrs);
347 if (!rc)
348 rc = smc_pnet_enter(pnetelem);
349 if (rc) {
350 kfree(pnetelem);
351 return rc;
352 }
353 rc = smc_ib_remember_port_attr(pnetelem->smcibdev, pnetelem->ib_port);
354 if (rc)
355 smc_pnet_remove_by_pnetid(pnetelem->pnet_name);
356 return rc;
357 }
358
359 static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
360 {
361 return smc_pnet_remove_by_pnetid(
362 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
363 }
364
365 static int smc_pnet_dump_start(struct netlink_callback *cb)
366 {
367 cb->args[0] = 0;
368 return 0;
369 }
370
371 static int smc_pnet_dumpinfo(struct sk_buff *skb,
372 u32 portid, u32 seq, u32 flags,
373 struct smc_pnetentry *pnetelem)
374 {
375 void *hdr;
376
377 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
378 flags, SMC_PNETID_GET);
379 if (!hdr)
380 return -ENOMEM;
381 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
382 genlmsg_cancel(skb, hdr);
383 return -EMSGSIZE;
384 }
385 genlmsg_end(skb, hdr);
386 return 0;
387 }
388
389 static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
390 {
391 struct smc_pnetentry *pnetelem;
392 int idx = 0;
393
394 read_lock(&smc_pnettable.lock);
395 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
396 if (idx++ < cb->args[0])
397 continue;
398 if (smc_pnet_dumpinfo(skb, NETLINK_CB(cb->skb).portid,
399 cb->nlh->nlmsg_seq, NLM_F_MULTI,
400 pnetelem)) {
401 --idx;
402 break;
403 }
404 }
405 cb->args[0] = idx;
406 read_unlock(&smc_pnettable.lock);
407 return skb->len;
408 }
409
410 /* Remove and delete all pnetids from pnet table.
411 */
412 static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
413 {
414 struct smc_pnetentry *pnetelem, *tmp_pe;
415
416 write_lock(&smc_pnettable.lock);
417 list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
418 list) {
419 list_del(&pnetelem->list);
420 dev_put(pnetelem->ndev);
421 kfree(pnetelem);
422 }
423 write_unlock(&smc_pnettable.lock);
424 return 0;
425 }
426
427 /* SMC_PNETID generic netlink operation definition */
428 static const struct genl_ops smc_pnet_ops[] = {
429 {
430 .cmd = SMC_PNETID_GET,
431 .flags = GENL_ADMIN_PERM,
432 .policy = smc_pnet_policy,
433 .doit = smc_pnet_get,
434 .dumpit = smc_pnet_dump,
435 .start = smc_pnet_dump_start
436 },
437 {
438 .cmd = SMC_PNETID_ADD,
439 .flags = GENL_ADMIN_PERM,
440 .policy = smc_pnet_policy,
441 .doit = smc_pnet_add
442 },
443 {
444 .cmd = SMC_PNETID_DEL,
445 .flags = GENL_ADMIN_PERM,
446 .policy = smc_pnet_policy,
447 .doit = smc_pnet_del
448 },
449 {
450 .cmd = SMC_PNETID_FLUSH,
451 .flags = GENL_ADMIN_PERM,
452 .policy = smc_pnet_policy,
453 .doit = smc_pnet_flush
454 }
455 };
456
457 /* SMC_PNETID family definition */
458 static struct genl_family smc_pnet_nl_family = {
459 .hdrsize = 0,
460 .name = SMCR_GENL_FAMILY_NAME,
461 .version = SMCR_GENL_FAMILY_VERSION,
462 .maxattr = SMC_PNETID_MAX,
463 .netnsok = true,
464 .module = THIS_MODULE,
465 .ops = smc_pnet_ops,
466 .n_ops = ARRAY_SIZE(smc_pnet_ops)
467 };
468
469 static int smc_pnet_netdev_event(struct notifier_block *this,
470 unsigned long event, void *ptr)
471 {
472 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
473
474 switch (event) {
475 case NETDEV_REBOOT:
476 case NETDEV_UNREGISTER:
477 smc_pnet_remove_by_ndev(event_dev);
478 default:
479 break;
480 }
481 return NOTIFY_DONE;
482 }
483
484 static struct notifier_block smc_netdev_notifier = {
485 .notifier_call = smc_pnet_netdev_event
486 };
487
488 int __init smc_pnet_init(void)
489 {
490 int rc;
491
492 rc = genl_register_family(&smc_pnet_nl_family);
493 if (rc)
494 return rc;
495 rc = register_netdevice_notifier(&smc_netdev_notifier);
496 if (rc)
497 genl_unregister_family(&smc_pnet_nl_family);
498 return rc;
499 }
500
501 void smc_pnet_exit(void)
502 {
503 smc_pnet_flush(NULL, NULL);
504 unregister_netdevice_notifier(&smc_netdev_notifier);
505 genl_unregister_family(&smc_pnet_nl_family);
506 }
507
508 /* PNET table analysis for a given sock:
509 * determine ib_device and port belonging to used internal TCP socket
510 * ethernet interface.
511 */
512 void smc_pnet_find_roce_resource(struct sock *sk,
513 struct smc_ib_device **smcibdev, u8 *ibport)
514 {
515 struct dst_entry *dst = sk_dst_get(sk);
516 struct smc_pnetentry *pnetelem;
517
518 *smcibdev = NULL;
519 *ibport = 0;
520
521 if (!dst)
522 return;
523 if (!dst->dev)
524 goto out_rel;
525 read_lock(&smc_pnettable.lock);
526 list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
527 if (dst->dev == pnetelem->ndev) {
528 if (smc_ib_port_active(pnetelem->smcibdev,
529 pnetelem->ib_port)) {
530 *smcibdev = pnetelem->smcibdev;
531 *ibport = pnetelem->ib_port;
532 }
533 break;
534 }
535 }
536 read_unlock(&smc_pnettable.lock);
537 out_rel:
538 dst_release(dst);
539 }