Whamcloud - gitweb
LU-15117 ofd: no lock for dt_bufs_get() in read path
[fs/lustre-release.git] / lnet / lnet / config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/ctype.h>
35 #include <linux/inetdevice.h>
36 #include <linux/nsproxy.h>
37 #include <net/net_namespace.h>
38 #include <lnet/lib-lnet.h>
39 #include <net/addrconf.h>
40
41 /* tmp struct for parsing routes */
42 struct lnet_text_buf {
43         struct list_head        ltb_list;       /* stash on lists */
44         int                     ltb_size;       /* allocated size */
45         char                    ltb_text[0];    /* text buffer */
46 };
47
48 static int lnet_tbnob = 0;                      /* track text buf allocation */
49 #define LNET_MAX_TEXTBUF_NOB     (64<<10)       /* bound allocation */
50 #define LNET_SINGLE_TEXTBUF_NOB  (4<<10)
51
52 #define SPACESTR " \t\v\r\n"
53 #define DELIMITERS ":()[]"
54
55 #ifndef HAVE_STRSCPY
56 #define strscpy(s1, s2, sz)     strlcpy((s1), (s2), (sz))
57 #endif
58
59 static void
60 lnet_syntax(const char *name, const char *str, int offset, int width)
61 {
62         static char dots[LNET_SINGLE_TEXTBUF_NOB];
63         static char dashes[LNET_SINGLE_TEXTBUF_NOB];
64
65         memset(dots, '.', sizeof(dots));
66         dots[sizeof(dots)-1] = 0;
67         memset(dashes, '-', sizeof(dashes));
68         dashes[sizeof(dashes)-1] = 0;
69
70         LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
71         LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
72                            (int)strlen(name), dots, offset, dots,
73                             (width < 1) ? 0 : width - 1, dashes);
74 }
75
76 static int
77 lnet_issep (char c)
78 {
79         switch (c) {
80         case '\n':
81         case '\r':
82         case ';':
83                 return 1;
84         default:
85                 return 0;
86         }
87 }
88
89 bool
90 lnet_net_unique(__u32 net_id, struct list_head *netlist,
91                 struct lnet_net **net)
92 {
93         struct lnet_net  *net_l;
94
95         if (!netlist)
96                 return true;
97
98         list_for_each_entry(net_l, netlist, net_list) {
99                 if (net_l->net_id == net_id) {
100                         if (net != NULL)
101                                 *net = net_l;
102                         return false;
103                 }
104         }
105
106         return true;
107 }
108
109 /* check that the NI is unique within the list of NIs already added to
110  * a network */
111 bool
112 lnet_ni_unique_net(struct list_head *nilist, char *iface)
113 {
114         struct list_head *tmp;
115         struct lnet_ni *ni;
116
117         list_for_each(tmp, nilist) {
118                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
119
120                 if (ni->ni_interface != NULL &&
121                     strncmp(ni->ni_interface, iface, strlen(iface)) == 0)
122                         return false;
123         }
124
125         return true;
126 }
127 static bool
128 in_array(__u32 *array, __u32 size, __u32 value)
129 {
130         int i;
131
132         for (i = 0; i < size; i++) {
133                 if (array[i] == value)
134                         return false;
135         }
136
137         return true;
138 }
139
140 static int
141 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
142 {
143         __u32 *added_cpts = NULL;
144         int i, j = 0, rc = 0;
145
146         /*
147          * no need to go futher since a subset of the NIs already exist on
148          * all CPTs
149          */
150         if (net->net_ncpts == LNET_CPT_NUMBER)
151                 return 0;
152
153         if (cpts == NULL) {
154                 /* there is an NI which will exist on all CPTs */
155                 if (net->net_cpts != NULL)
156                         CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
157                 net->net_cpts = NULL;
158                 net->net_ncpts = LNET_CPT_NUMBER;
159                 return 0;
160         }
161
162         if (net->net_cpts == NULL) {
163                 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
164                 if (net->net_cpts == NULL)
165                         return -ENOMEM;
166                 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
167                 net->net_ncpts = ncpts;
168                 return 0;
169         }
170
171         CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
172         if (added_cpts == NULL)
173                 return -ENOMEM;
174
175         for (i = 0; i < ncpts; i++) {
176                 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
177                         added_cpts[j] = cpts[i];
178                         j++;
179                 }
180         }
181
182         /* append the new cpts if any to the list of cpts in the net */
183         if (j > 0) {
184                 __u32 *array = NULL, *loc;
185                 __u32 total_entries = j + net->net_ncpts;
186
187                 CFS_ALLOC_PTR_ARRAY(array, total_entries);
188                 if (array == NULL) {
189                         rc = -ENOMEM;
190                         goto failed;
191                 }
192
193                 memcpy(array, net->net_cpts,
194                        net->net_ncpts * sizeof(*net->net_cpts));
195                 loc = array + net->net_ncpts;
196                 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
197
198                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
199                 net->net_ncpts = total_entries;
200                 net->net_cpts = array;
201         }
202
203 failed:
204         CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
205
206         return rc;
207 }
208
209 static void
210 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
211 {
212         struct lnet_ni *ni;
213         int rc;
214
215         /*
216          * Operation Assumption:
217          *      This function is called after an NI has been removed from
218          *      its parent net.
219          *
220          * if we're removing an NI which exists on all CPTs then
221          * we have to check if any of the other NIs on this net also
222          * exists on all CPTs. If none, then we need to build our Net CPT
223          * list based on the remaining NIs.
224          *
225          * If the NI being removed exist on a subset of the CPTs then we
226          * alo rebuild the Net CPT list based on the remaining NIs, which
227          * should resutl in the expected Net CPT list.
228          */
229
230         /*
231          * sometimes this function can be called due to some failure
232          * creating an NI, before any of the cpts are allocated, so check
233          * for that case and don't do anything
234          */
235         if (ncpts == 0)
236                 return;
237
238         if (ncpts == LNET_CPT_NUMBER) {
239                 /*
240                  * first iteration through the NI list in the net to see
241                  * if any of the NIs exist on all the CPTs. If one is
242                  * found then our job is done.
243                  */
244                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
245                         if (ni->ni_ncpts == LNET_CPT_NUMBER)
246                                 return;
247                 }
248         }
249
250         /*
251          * Rebuild the Net CPT list again, thereby only including only the
252          * CPTs which the remaining NIs are associated with.
253          */
254         if (net->net_cpts != NULL) {
255                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
256                 net->net_cpts = NULL;
257         }
258
259         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
260                 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
261                                           net);
262                 if (rc != 0) {
263                         CERROR("Out of Memory\n");
264                         /*
265                          * do our best to keep on going. Delete
266                          * the net cpts and set it to NULL. This
267                          * way we can keep on going but less
268                          * efficiently, since memory accesses might be
269                          * accross CPT lines.
270                          */
271                         if (net->net_cpts != NULL) {
272                                 CFS_FREE_PTR_ARRAY(net->net_cpts,
273                                                    net->net_ncpts);
274                                 net->net_cpts = NULL;
275                                 net->net_ncpts = LNET_CPT_NUMBER;
276                         }
277                         return;
278                 }
279         }
280 }
281
282 void
283 lnet_ni_free(struct lnet_ni *ni)
284 {
285         lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
286
287         if (ni->ni_refs != NULL)
288                 cfs_percpt_free(ni->ni_refs);
289
290         if (ni->ni_tx_queues != NULL)
291                 cfs_percpt_free(ni->ni_tx_queues);
292
293         if (ni->ni_cpts != NULL)
294                 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
295
296         if (ni->ni_interface != NULL) {
297                 LIBCFS_FREE(ni->ni_interface,
298                             strlen(ni->ni_interface) + 1);
299         }
300
301         /* release reference to net namespace */
302         if (ni->ni_net_ns != NULL)
303                 put_net(ni->ni_net_ns);
304
305         LIBCFS_FREE(ni, sizeof(*ni));
306 }
307
308 void
309 lnet_net_free(struct lnet_net *net)
310 {
311         struct list_head *tmp, *tmp2;
312         struct lnet_ni *ni;
313
314         LASSERT(list_empty(&net->net_ni_zombie));
315
316         /*
317          * delete any nis that haven't been added yet. This could happen
318          * if there is a failure on net startup
319          */
320         list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
321                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
322                 list_del_init(&ni->ni_netlist);
323                 lnet_ni_free(ni);
324         }
325
326         /* delete any nis which have been started. */
327         list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
328                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
329                 list_del_init(&ni->ni_netlist);
330                 lnet_ni_free(ni);
331         }
332
333         if (net->net_cpts != NULL)
334                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
335
336         LIBCFS_FREE(net, sizeof(*net));
337 }
338
339 struct lnet_net *
340 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
341 {
342         struct lnet_net         *net;
343
344         if (!lnet_net_unique(net_id, net_list, &net)) {
345                 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
346                        libcfs_net2str(net->net_id));
347                 return net;
348         }
349
350         LIBCFS_ALLOC(net, sizeof(*net));
351         if (net == NULL) {
352                 CERROR("Out of memory creating network %s\n",
353                        libcfs_net2str(net_id));
354                 return NULL;
355         }
356
357         INIT_LIST_HEAD(&net->net_list);
358         INIT_LIST_HEAD(&net->net_ni_list);
359         INIT_LIST_HEAD(&net->net_ni_added);
360         INIT_LIST_HEAD(&net->net_ni_zombie);
361         INIT_LIST_HEAD(&net->net_rtr_pref_nids);
362         spin_lock_init(&net->net_lock);
363
364         net->net_id = net_id;
365         net->net_last_alive = ktime_get_seconds();
366
367         net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
368
369         /* initialize global paramters to undefiend */
370         net->net_tunables.lct_peer_timeout = -1;
371         net->net_tunables.lct_max_tx_credits = -1;
372         net->net_tunables.lct_peer_tx_credits = -1;
373         net->net_tunables.lct_peer_rtr_credits = -1;
374
375         if (net_list)
376                 list_add_tail(&net->net_list, net_list);
377
378         return net;
379 }
380
381 static int
382 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
383 {
384         size_t iface_len = strlen(iface) + 1;
385
386         if (ni == NULL)
387                 return -ENOMEM;
388
389         if (ni->ni_interface != NULL) {
390                 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
391                                    iface, ni->ni_interface,
392                                    libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
393                                    -EINVAL);
394                 return -EINVAL;
395         }
396
397         /* Allocate memory for the interface, so the code parsing input into
398          * tokens and adding interfaces can free the input safely.
399          * ni->ni_interface is freed in lnet_ni_free().
400          */
401         LIBCFS_ALLOC(ni->ni_interface, iface_len);
402
403         if (ni->ni_interface == NULL) {
404                 CERROR("%s: cannot allocate net interface name: rc = %d\n",
405                         iface, -ENOMEM);
406                 return -ENOMEM;
407         }
408
409         strscpy(ni->ni_interface, iface, iface_len);
410
411         return 0;
412 }
413
414 static struct lnet_ni *
415 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
416 {
417         struct lnet_tx_queue    *tq;
418         struct lnet_ni          *ni;
419         int                     i;
420
421         if (iface != NULL)
422                 /* make sure that this NI is unique in the net it's
423                  * being added to */
424                 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
425                         return NULL;
426
427         LIBCFS_ALLOC(ni, sizeof(*ni));
428         if (ni == NULL) {
429                 CERROR("Out of memory creating network interface %s%s\n",
430                        libcfs_net2str(net->net_id),
431                        (iface != NULL) ? iface : "");
432                 return NULL;
433         }
434
435         spin_lock_init(&ni->ni_lock);
436         INIT_LIST_HEAD(&ni->ni_netlist);
437         INIT_LIST_HEAD(&ni->ni_recovery);
438         LNetInvalidateMDHandle(&ni->ni_ping_mdh);
439         ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
440                                        sizeof(*ni->ni_refs[0]));
441         if (ni->ni_refs == NULL)
442                 goto failed;
443
444         ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
445                                             sizeof(*ni->ni_tx_queues[0]));
446         if (ni->ni_tx_queues == NULL)
447                 goto failed;
448
449         cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
450                 INIT_LIST_HEAD(&tq->tq_delayed);
451
452         ni->ni_net = net;
453         /* LND will fill in the address part of the NID */
454         ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
455         ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
456
457         /* Store net namespace in which current ni is being created */
458         if (current->nsproxy && current->nsproxy->net_ns)
459                 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
460         else
461                 ni->ni_net_ns = get_net(&init_net);
462
463         ni->ni_state = LNET_NI_STATE_INIT;
464         ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
465         list_add_tail(&ni->ni_netlist, &net->net_ni_added);
466
467         /*
468          * if an interface name is provided then make sure to add in that
469          * interface name in NI
470          */
471         if (iface)
472                 if (lnet_ni_add_interface(ni, iface) != 0)
473                         goto failed;
474
475         return ni;
476 failed:
477         lnet_ni_free(ni);
478         return NULL;
479 }
480
481 /* allocate and add to the provided network */
482 struct lnet_ni *
483 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
484 {
485         struct lnet_ni          *ni;
486         int                     rc;
487
488         ni = lnet_ni_alloc_common(net, iface);
489         if (!ni)
490                 return NULL;
491
492         if (!el) {
493                 ni->ni_cpts  = NULL;
494                 ni->ni_ncpts = LNET_CPT_NUMBER;
495         } else {
496                 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
497                 if (rc <= 0) {
498                         CERROR("Failed to set CPTs for NI %s(%s): %d\n",
499                                libcfs_net2str(net->net_id),
500                                (iface != NULL) ? iface : "", rc);
501                         goto failed;
502                 }
503
504                 LASSERT(rc <= LNET_CPT_NUMBER);
505                 if (rc == LNET_CPT_NUMBER) {
506                         CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
507                         ni->ni_cpts = NULL;
508                 }
509
510                 ni->ni_ncpts = rc;
511         }
512
513         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
514         if (rc != 0)
515                 goto failed;
516
517         return ni;
518 failed:
519         lnet_ni_free(ni);
520         return NULL;
521 }
522
523 struct lnet_ni *
524 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
525                           char *iface)
526 {
527         struct lnet_ni          *ni;
528         int                     rc;
529
530         ni = lnet_ni_alloc_common(net, iface);
531         if (!ni)
532                 return NULL;
533
534         if (ncpts == 0) {
535                 ni->ni_cpts  = NULL;
536                 ni->ni_ncpts = LNET_CPT_NUMBER;
537         } else {
538                 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
539
540                 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
541                 if (ni->ni_cpts == NULL)
542                         goto failed;
543                 memcpy(ni->ni_cpts, cpts, array_size);
544                 ni->ni_ncpts = ncpts;
545         }
546
547         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
548         if (rc != 0)
549                 goto failed;
550
551         return ni;
552 failed:
553         lnet_ni_free(ni);
554         return NULL;
555 }
556
557 /*
558  * Parse the networks string and create the matching set of NIs on the
559  * nilist.
560  */
561 int
562 lnet_parse_networks(struct list_head *netlist, const char *networks)
563 {
564         struct cfs_expr_list *net_el = NULL;
565         struct cfs_expr_list *ni_el = NULL;
566         int             tokensize;
567         char            *tokens;
568         char            *str;
569         struct lnet_net *net;
570         struct lnet_ni  *ni = NULL;
571         __u32           net_id;
572         int             nnets = 0;
573
574         if (networks == NULL) {
575                 CERROR("networks string is undefined\n");
576                 return -EINVAL;
577         }
578
579         if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
580                 /* _WAY_ conservative */
581                 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
582                                    "long\n");
583                 return -EINVAL;
584         }
585
586         tokensize = strlen(networks) + 1;
587
588         LIBCFS_ALLOC(tokens, tokensize);
589         if (tokens == NULL) {
590                 CERROR("Can't allocate net tokens\n");
591                 return -ENOMEM;
592         }
593
594         memcpy(tokens, networks, tokensize);
595         str = tokens;
596
597         /*
598          * Main parser loop.
599          *
600          * NB we don't check interface conflicts here; it's the LNDs
601          * responsibility (if it cares at all)
602          */
603         do {
604                 char *nistr;
605                 char *elstr;
606                 char *name;
607                 int rc;
608
609                 /*
610                  * Parse a network string into its components.
611                  *
612                  * <name>{"("...")"}{"["<el>"]"}
613                  */
614
615                 /* Network name (mandatory) */
616                 while (isspace(*str))
617                         *str++ = '\0';
618                 if (!*str)
619                         break;
620                 name = str;
621                 str += strcspn(str, SPACESTR ":()[],");
622                 while (isspace(*str))
623                         *str++ = '\0';
624
625                 /* Interface list (optional) */
626                 if (*str == '(') {
627                         *str++ = '\0';
628                         nistr = str;
629                         str += strcspn(str, ")");
630                         if (*str != ')') {
631                                 str = nistr;
632                                 goto failed_syntax;
633                         }
634                         do {
635                                 *str++ = '\0';
636                         } while (isspace(*str));
637                 } else {
638                         nistr = NULL;
639                 }
640
641                 /* CPT expression (optional) */
642                 if (*str == '[') {
643                         elstr = str;
644                         str += strcspn(str, "]");
645                         if (*str != ']') {
646                                 str = elstr;
647                                 goto failed_syntax;
648                         }
649                         rc = cfs_expr_list_parse(elstr, str - elstr + 1,
650                                                 0, LNET_CPT_NUMBER - 1,
651                                                 &net_el);
652                         if (rc != 0) {
653                                 str = elstr;
654                                 goto failed_syntax;
655                         }
656                         *elstr = '\0';
657                         do {
658                                 *str++ = '\0';
659                         } while (isspace(*str));
660                 }
661
662                 /* Bad delimiters */
663                 if (*str && (strchr(DELIMITERS, *str) != NULL))
664                         goto failed_syntax;
665
666                 /* go to the next net if it exits */
667                 str += strcspn(str, ",");
668                 if (*str == ',')
669                         *str++ = '\0';
670
671                 /*
672                  * At this point the name is properly terminated.
673                  */
674                 net_id = libcfs_str2net(name);
675                 if (net_id == LNET_NET_ANY) {
676                         LCONSOLE_ERROR_MSG(0x113,
677                                         "Unrecognised network type\n");
678                         str = name;
679                         goto failed_syntax;
680                 }
681
682                 if (LNET_NETTYP(net_id) == LOLND) {
683                         /* Loopback is implicit, and there can be only one. */
684                         if (net_el) {
685                                 cfs_expr_list_free(net_el);
686                                 net_el = NULL;
687                         }
688                         /* Should we error out instead? */
689                         continue;
690                 }
691
692                 /*
693                  * All network paramaters are now known.
694                  */
695                 nnets++;
696
697                 /* always allocate a net, since we will eventually add an
698                  * interface to it, or we will fail, in which case we'll
699                  * just delete it */
700                 net = lnet_net_alloc(net_id, netlist);
701                 if (IS_ERR_OR_NULL(net))
702                         goto failed;
703
704                 if (!nistr) {
705                         /*
706                          * No interface list was specified, allocate a
707                          * ni using the defaults.
708                          */
709                         ni = lnet_ni_alloc(net, net_el, NULL);
710                         if (IS_ERR_OR_NULL(ni))
711                                 goto failed;
712
713                         if (!nistr) {
714                                 if (net_el) {
715                                         cfs_expr_list_free(net_el);
716                                         net_el = NULL;
717                                 }
718                                 continue;
719                         }
720                 }
721
722                 do {
723                         elstr = NULL;
724
725                         /* Interface name (mandatory) */
726                         while (isspace(*nistr))
727                                 *nistr++ = '\0';
728                         name = nistr;
729                         nistr += strcspn(nistr, SPACESTR "[],");
730                         while (isspace(*nistr))
731                                 *nistr++ = '\0';
732
733                         /* CPT expression (optional) */
734                         if (*nistr == '[') {
735                                 elstr = nistr;
736                                 nistr += strcspn(nistr, "]");
737                                 if (*nistr != ']') {
738                                         str = elstr;
739                                         goto failed_syntax;
740                                 }
741                                 rc = cfs_expr_list_parse(elstr,
742                                                         nistr - elstr + 1,
743                                                         0, LNET_CPT_NUMBER - 1,
744                                                         &ni_el);
745                                 if (rc != 0) {
746                                         str = elstr;
747                                         goto failed_syntax;
748                                 }
749                                 *elstr = '\0';
750                                 do {
751                                         *nistr++ = '\0';
752                                 } while (isspace(*nistr));
753                         } else {
754                                 ni_el = net_el;
755                         }
756
757                         /*
758                          * End of single interface specificaton,
759                          * advance to the start of the next one, if
760                          * any.
761                          */
762                         if (*nistr == ',') {
763                                 do {
764                                         *nistr++ = '\0';
765                                 } while (isspace(*nistr));
766                                 if (!*nistr) {
767                                         str = nistr;
768                                         goto failed_syntax;
769                                 }
770                         } else if (*nistr) {
771                                 str = nistr;
772                                 goto failed_syntax;
773                         }
774
775                         /*
776                          * At this point the name is properly terminated.
777                          */
778                         if (!*name) {
779                                 str = name;
780                                 goto failed_syntax;
781                         }
782
783                         ni = lnet_ni_alloc(net, ni_el, name);
784                         if (IS_ERR_OR_NULL(ni))
785                                 goto failed;
786
787                         if (ni_el) {
788                                 if (ni_el != net_el) {
789                                         cfs_expr_list_free(ni_el);
790                                         ni_el = NULL;
791                                 }
792                         }
793                 } while (*nistr);
794
795                 if (net_el) {
796                         cfs_expr_list_free(net_el);
797                         net_el = NULL;
798                 }
799         } while (*str);
800
801         LIBCFS_FREE(tokens, tokensize);
802         return nnets;
803
804  failed_syntax:
805         lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
806  failed:
807         /* free the net list and all the nis on each net */
808         while ((net = list_first_entry_or_null(netlist,
809                                                struct lnet_net,
810                                                net_list)) != NULL) {
811                 list_del_init(&net->net_list);
812                 lnet_net_free(net);
813         }
814
815         if (ni_el && ni_el != net_el)
816                 cfs_expr_list_free(ni_el);
817         if (net_el)
818                 cfs_expr_list_free(net_el);
819
820         LIBCFS_FREE(tokens, tokensize);
821
822         return -EINVAL;
823 }
824
825 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
826 {
827         struct lnet_text_buf *ltb;
828         int nob;
829
830         /* NB allocate space for the terminating 0 */
831         nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
832         if (nob > LNET_SINGLE_TEXTBUF_NOB) {
833                 /* _way_ conservative for "route net gateway..." */
834                 CERROR("text buffer too big\n");
835                 return NULL;
836         }
837
838         if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
839                 CERROR("Too many text buffers\n");
840                 return NULL;
841         }
842
843         LIBCFS_ALLOC(ltb, nob);
844         if (ltb == NULL)
845                 return NULL;
846
847         ltb->ltb_size = nob;
848         ltb->ltb_text[0] = 0;
849         lnet_tbnob += nob;
850         return ltb;
851 }
852
853 static void
854 lnet_free_text_buf(struct lnet_text_buf *ltb)
855 {
856         lnet_tbnob -= ltb->ltb_size;
857         LIBCFS_FREE(ltb, ltb->ltb_size);
858 }
859
860 static void
861 lnet_free_text_bufs(struct list_head *tbs)
862 {
863         struct lnet_text_buf  *ltb;
864
865         while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
866                                                ltb_list)) != NULL) {
867                 list_del(&ltb->ltb_list);
868                 lnet_free_text_buf(ltb);
869         }
870 }
871
872 static int
873 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
874 {
875         LIST_HEAD(pending);
876         const char *sep;
877         int nob;
878         int i;
879         struct lnet_text_buf *ltb;
880
881         /* Split 'str' into separate commands */
882         for (;;) {
883                 /* skip leading whitespace */
884                 while (isspace(*str))
885                         str++;
886
887                 /* scan for separator or comment */
888                 for (sep = str; *sep != 0; sep++)
889                         if (lnet_issep(*sep) || *sep == '#')
890                                 break;
891
892                 nob = (int)(sep - str);
893                 if (nob > 0) {
894                         ltb = lnet_new_text_buf(nob);
895                         if (ltb == NULL) {
896                                 lnet_free_text_bufs(&pending);
897                                 return -ENOMEM;
898                         }
899
900                         for (i = 0; i < nob; i++)
901                                 if (isspace(str[i]))
902                                         ltb->ltb_text[i] = ' ';
903                                 else
904                                         ltb->ltb_text[i] = str[i];
905
906                         ltb->ltb_text[nob] = 0;
907
908                         list_add_tail(&ltb->ltb_list, &pending);
909                 }
910
911                 if (*sep == '#') {
912                         /* scan for separator */
913                         do {
914                                 sep++;
915                         } while (*sep != 0 && !lnet_issep(*sep));
916                 }
917
918                 if (*sep == 0)
919                         break;
920
921                 str = sep + 1;
922         }
923
924         list_splice(&pending, tbs->prev);
925         return 0;
926 }
927
928 static int
929 lnet_expand1tb(struct list_head *list,
930                char *str, char *sep1, char *sep2,
931                char *item, int itemlen)
932 {
933         int              len1 = (int)(sep1 - str);
934         int              len2 = strlen(sep2 + 1);
935         struct lnet_text_buf *ltb;
936
937         LASSERT (*sep1 == '[');
938         LASSERT (*sep2 == ']');
939
940         ltb = lnet_new_text_buf(len1 + itemlen + len2);
941         if (ltb == NULL)
942                 return -ENOMEM;
943
944         memcpy(ltb->ltb_text, str, len1);
945         memcpy(&ltb->ltb_text[len1], item, itemlen);
946         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
947         ltb->ltb_text[len1 + itemlen + len2] = 0;
948
949         list_add_tail(&ltb->ltb_list, list);
950         return 0;
951 }
952
953 static int
954 lnet_str2tbs_expand(struct list_head *tbs, char *str)
955 {
956         char              num[16];
957         LIST_HEAD(pending);
958         char             *sep;
959         char             *sep2;
960         char             *parsed;
961         char             *enditem;
962         int               lo;
963         int               hi;
964         int               stride;
965         int               i;
966         int               nob;
967         int               scanned;
968
969         sep = strchr(str, '[');
970         if (sep == NULL)                        /* nothing to expand */
971                 return 0;
972
973         sep2 = strchr(sep, ']');
974         if (sep2 == NULL)
975                 goto failed;
976
977         for (parsed = sep; parsed < sep2; parsed = enditem) {
978
979                 enditem = ++parsed;
980                 while (enditem < sep2 && *enditem != ',')
981                         enditem++;
982
983                 if (enditem == parsed)          /* no empty items */
984                         goto failed;
985
986                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
987
988                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
989
990                                 /* simple string enumeration */
991                                 if (lnet_expand1tb(&pending, str, sep, sep2,
992                                                    parsed, (int)(enditem - parsed)) != 0)
993                                         goto failed;
994
995                                 continue;
996                         }
997
998                         stride = 1;
999                 }
1000
1001                 /* range expansion */
1002
1003                 if (enditem != parsed + scanned) /* no trailing junk */
1004                         goto failed;
1005
1006                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1007                     (hi - lo) % stride != 0)
1008                         goto failed;
1009
1010                 for (i = lo; i <= hi; i += stride) {
1011
1012                         snprintf(num, sizeof(num), "%d", i);
1013                         nob = strlen(num);
1014                         if (nob + 1 == sizeof(num))
1015                                 goto failed;
1016
1017                         if (lnet_expand1tb(&pending, str, sep, sep2,
1018                                            num, nob) != 0)
1019                                 goto failed;
1020                 }
1021         }
1022
1023         list_splice(&pending, tbs->prev);
1024         return 1;
1025
1026  failed:
1027         lnet_free_text_bufs(&pending);
1028         return -EINVAL;
1029 }
1030
1031 static int
1032 lnet_parse_hops (char *str, unsigned int *hops)
1033 {
1034         int     len = strlen(str);
1035         int     nob = len;
1036
1037         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1038                 nob == len &&
1039                 *hops > 0 && *hops < 256);
1040 }
1041
1042 #define LNET_PRIORITY_SEPARATOR (':')
1043
1044 static int
1045 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1046 {
1047         int   nob;
1048         char *sep;
1049         int   len;
1050
1051         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1052         if (sep == NULL) {
1053                 *priority = 0;
1054                 return 0;
1055         }
1056         len = strlen(sep + 1);
1057
1058         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1059                 /* Update the caller's token pointer so it treats the found
1060                    priority as the token to report in the error message. */
1061                 *token += sep - str + 1;
1062                 return -EINVAL;
1063         }
1064
1065         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1066
1067         /*
1068          * Change priority separator to \0 to be able to parse NID
1069          */
1070         *sep = '\0';
1071         return 0;
1072 }
1073
1074 static int
1075 lnet_parse_route(char *str, int *im_a_router)
1076 {
1077         /* static scratch buffer OK (single threaded) */
1078         static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1079
1080         LIST_HEAD(nets);
1081         LIST_HEAD(gateways);
1082         struct list_head *tmp1;
1083         struct list_head *tmp2;
1084         __u32 net;
1085         struct lnet_nid nid;
1086         struct lnet_text_buf *ltb = NULL;
1087         struct lnet_text_buf *ltb1, *ltb2;
1088         int rc;
1089         char *sep;
1090         char *token = str;
1091         int ntokens = 0;
1092         int myrc = -1;
1093         __u32 hops;
1094         int got_hops = 0;
1095         unsigned int priority = 0;
1096
1097         /* save a copy of the string for error messages */
1098         strncpy(cmd, str, sizeof(cmd));
1099         cmd[sizeof(cmd) - 1] = '\0';
1100
1101         sep = str;
1102         for (;;) {
1103                 /* scan for token start */
1104                 while (isspace(*sep))
1105                         sep++;
1106                 if (*sep == 0) {
1107                         if (ntokens < (got_hops ? 3 : 2))
1108                                 goto token_error;
1109                         break;
1110                 }
1111
1112                 ntokens++;
1113                 token = sep++;
1114
1115                 /* scan for token end */
1116                 while (*sep != 0 && !isspace(*sep))
1117                         sep++;
1118                 if (*sep != 0)
1119                         *sep++ = 0;
1120
1121                 if (ntokens == 1) {
1122                         tmp2 = &nets;           /* expanding nets */
1123                 } else if (ntokens == 2 &&
1124                            lnet_parse_hops(token, &hops)) {
1125                         got_hops = 1;           /* got a hop count */
1126                         continue;
1127                 } else {
1128                         tmp2 = &gateways;       /* expanding gateways */
1129                 }
1130
1131                 ltb = lnet_new_text_buf(strlen(token));
1132                 if (ltb == NULL)
1133                         goto out;
1134
1135                 strcpy(ltb->ltb_text, token);
1136                 tmp1 = &ltb->ltb_list;
1137                 list_add_tail(tmp1, tmp2);
1138
1139                 while (tmp1 != tmp2) {
1140                         ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1141
1142                         rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1143                         if (rc < 0)
1144                                 goto token_error;
1145
1146                         tmp1 = tmp1->next;
1147
1148                         if (rc > 0) {           /* expanded! */
1149                                 list_del(&ltb->ltb_list);
1150                                 lnet_free_text_buf(ltb);
1151                                 continue;
1152                         }
1153
1154                         if (ntokens == 1) {
1155                                 net = libcfs_str2net(ltb->ltb_text);
1156                                 if (net == LNET_NET_ANY ||
1157                                     LNET_NETTYP(net) == LOLND)
1158                                         goto token_error;
1159                         } else {
1160                                 rc = lnet_parse_priority(ltb->ltb_text,
1161                                                          &priority, &token);
1162                                 if (rc < 0)
1163                                         goto token_error;
1164
1165                                 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1166                                     nid_is_lo0(&nid))
1167                                         goto token_error;
1168                         }
1169                 }
1170         }
1171
1172         /* if there are no hops set then we want to flag this value as
1173          * unset since hops is an optional parameter */
1174         if (!got_hops)
1175                 hops = LNET_UNDEFINED_HOPS;
1176
1177         LASSERT(!list_empty(&nets));
1178         LASSERT(!list_empty(&gateways));
1179
1180         list_for_each_entry(ltb1, &nets, ltb_list) {
1181                 net = libcfs_str2net(ltb1->ltb_text);
1182                 LASSERT(net != LNET_NET_ANY);
1183
1184                 list_for_each_entry(ltb2, &gateways, ltb_list) {
1185                         LASSERT(libcfs_strnid(&nid, ltb->ltb_text) == 0);
1186
1187                         if (lnet_islocalnid(&nid)) {
1188                                 *im_a_router = 1;
1189                                 continue;
1190                         }
1191
1192                         rc = lnet_add_route(net, hops, &nid, priority, 1);
1193                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1194                                 CERROR("Can't create route "
1195                                        "to %s via %s\n",
1196                                        libcfs_net2str(net),
1197                                        libcfs_nidstr(&nid));
1198                                 goto out;
1199                         }
1200                 }
1201         }
1202
1203         myrc = 0;
1204         goto out;
1205
1206 token_error:
1207         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1208 out:
1209         lnet_free_text_bufs(&nets);
1210         lnet_free_text_bufs(&gateways);
1211         return myrc;
1212 }
1213
1214 static int
1215 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1216 {
1217         struct lnet_text_buf *ltb;
1218
1219         while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1220                                                ltb_list)) != NULL) {
1221                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1222                         lnet_free_text_bufs(tbs);
1223                         return -EINVAL;
1224                 }
1225
1226                 list_del(&ltb->ltb_list);
1227                 lnet_free_text_buf(ltb);
1228         }
1229
1230         return 0;
1231 }
1232
1233 int
1234 lnet_parse_routes(const char *routes, int *im_a_router)
1235 {
1236         LIST_HEAD(tbs);
1237         int rc = 0;
1238
1239         *im_a_router = 0;
1240
1241         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1242                 CERROR("Error parsing routes\n");
1243                 rc = -EINVAL;
1244         } else {
1245                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1246         }
1247
1248         LASSERT (lnet_tbnob == 0);
1249         return rc;
1250 }
1251
1252 static int
1253 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1254 {
1255         LIST_HEAD(list);
1256         int             rc;
1257         int             i;
1258
1259         rc = cfs_ip_addr_parse(token, len, &list);
1260         if (rc != 0)
1261                 return rc;
1262
1263         for (rc = i = 0; !rc && i < nip; i++)
1264                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1265
1266         cfs_expr_list_free_list(&list);
1267
1268         return rc;
1269 }
1270
1271 static int
1272 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1273 {
1274         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1275
1276         int   matched = 0;
1277         int   ntokens = 0;
1278         int   len;
1279         char *net = NULL;
1280         char *sep;
1281         char *token;
1282         int   rc;
1283
1284         LASSERT(strlen(net_entry) < sizeof(tokens));
1285
1286         /* work on a copy of the string */
1287         strcpy(tokens, net_entry);
1288         sep = tokens;
1289         for (;;) {
1290                 /* scan for token start */
1291                 while (isspace(*sep))
1292                         sep++;
1293                 if (*sep == 0)
1294                         break;
1295
1296                 token = sep++;
1297
1298                 /* scan for token end */
1299                 while (*sep != 0 && !isspace(*sep))
1300                         sep++;
1301                 if (*sep != 0)
1302                         *sep++ = 0;
1303
1304                 if (ntokens++ == 0) {
1305                         net = token;
1306                         continue;
1307                 }
1308
1309                 len = strlen(token);
1310
1311                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1312                 if (rc < 0) {
1313                         lnet_syntax("ip2nets", net_entry,
1314                                     (int)(token - tokens), len);
1315                         return rc;
1316                 }
1317
1318                 matched |= (rc != 0);
1319         }
1320
1321         if (!matched)
1322                 return 0;
1323
1324         strcpy(net_entry, net);                 /* replace with matched net */
1325         return 1;
1326 }
1327
1328 static __u32
1329 lnet_netspec2net(char *netspec)
1330 {
1331         char   *bracket = strchr(netspec, '(');
1332         __u32   net;
1333
1334         if (bracket != NULL)
1335                 *bracket = 0;
1336
1337         net = libcfs_str2net(netspec);
1338
1339         if (bracket != NULL)
1340                 *bracket = '(';
1341
1342         return net;
1343 }
1344
1345 static int
1346 lnet_splitnets(char *source, struct list_head *nets)
1347 {
1348         int               offset = 0;
1349         int               offset2;
1350         int               len;
1351         struct lnet_text_buf  *tb;
1352         struct lnet_text_buf  *tb2;
1353         char             *sep;
1354         char             *bracket;
1355         __u32             net;
1356
1357         LASSERT(!list_empty(nets));
1358         LASSERT(nets->next == nets->prev);      /* single entry */
1359
1360         tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1361
1362         for (;;) {
1363                 sep = strchr(tb->ltb_text, ',');
1364                 bracket = strchr(tb->ltb_text, '(');
1365
1366                 if (sep != NULL &&
1367                     bracket != NULL &&
1368                     bracket < sep) {
1369                         /* netspec lists interfaces... */
1370
1371                         offset2 = offset + (int)(bracket - tb->ltb_text);
1372                         len = strlen(bracket);
1373
1374                         bracket = strchr(bracket + 1, ')');
1375
1376                         if (bracket == NULL ||
1377                             !(bracket[1] == ',' || bracket[1] == 0)) {
1378                                 lnet_syntax("ip2nets", source, offset2, len);
1379                                 return -EINVAL;
1380                         }
1381
1382                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
1383                 }
1384
1385                 if (sep != NULL)
1386                         *sep++ = 0;
1387
1388                 net = lnet_netspec2net(tb->ltb_text);
1389                 if (net == LNET_NET_ANY) {
1390                         lnet_syntax("ip2nets", source, offset,
1391                                     strlen(tb->ltb_text));
1392                         return -EINVAL;
1393                 }
1394
1395                 list_for_each_entry(tb2, nets, ltb_list) {
1396                         if (tb2 == tb)
1397                                 continue;
1398
1399                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1400                                 /* duplicate network */
1401                                 lnet_syntax("ip2nets", source, offset,
1402                                             strlen(tb->ltb_text));
1403                                 return -EINVAL;
1404                         }
1405                 }
1406
1407                 if (sep == NULL)
1408                         return 0;
1409
1410                 offset += (int)(sep - tb->ltb_text);
1411                 len = strlen(sep);
1412                 tb2 = lnet_new_text_buf(len);
1413                 if (tb2 == NULL)
1414                         return -ENOMEM;
1415
1416                 strncpy(tb2->ltb_text, sep, len);
1417                 tb2->ltb_text[len] = '\0';
1418                 list_add_tail(&tb2->ltb_list, nets);
1419
1420                 tb = tb2;
1421         }
1422 }
1423
1424 static int
1425 lnet_match_networks(const char **networksp, const char *ip2nets,
1426                     __u32 *ipaddrs, int nip)
1427 {
1428         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1429         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1430
1431         LIST_HEAD(raw_entries);
1432         LIST_HEAD(matched_nets);
1433         LIST_HEAD(current_nets);
1434         struct list_head *t;
1435         struct list_head *t2;
1436         struct lnet_text_buf  *tb;
1437         int               len;
1438         int               count;
1439         int               rc;
1440
1441         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1442                 CERROR("Error parsing ip2nets\n");
1443                 LASSERT(lnet_tbnob == 0);
1444                 return -EINVAL;
1445         }
1446
1447         networks[0] = 0;
1448         count = 0;
1449         len = 0;
1450         rc = 0;
1451
1452         while ((tb = list_first_entry_or_null(&raw_entries,
1453                                               struct lnet_text_buf,
1454                                               ltb_list)) != NULL) {
1455                 strncpy(source, tb->ltb_text, sizeof(source));
1456                 source[sizeof(source) - 1] = '\0';
1457
1458                 /* replace ltb_text with the network(s) add on match */
1459                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1460                 if (rc < 0)
1461                         break;
1462
1463                 list_del(&tb->ltb_list);
1464
1465                 if (rc == 0) {                  /* no match */
1466                         lnet_free_text_buf(tb);
1467                         continue;
1468                 }
1469
1470                 /* split into separate networks */
1471                 INIT_LIST_HEAD(&current_nets);
1472                 list_add(&tb->ltb_list, &current_nets);
1473                 rc = lnet_splitnets(source, &current_nets);
1474                 if (rc < 0)
1475                         break;
1476
1477                 list_for_each_safe(t, t2, &current_nets) {
1478                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1479
1480                         list_move_tail(&tb->ltb_list, &matched_nets);
1481
1482                         len += scnprintf(networks + len, sizeof(networks) - len,
1483                                          "%s%s", (len == 0) ? "" : ",",
1484                                          tb->ltb_text);
1485
1486                         if (len >= sizeof(networks)) {
1487                                 CERROR("Too many matched networks\n");
1488                                 rc = -E2BIG;
1489                                 goto out;
1490                         }
1491                 }
1492
1493                 count++;
1494         }
1495
1496  out:
1497         lnet_free_text_bufs(&raw_entries);
1498         lnet_free_text_bufs(&matched_nets);
1499         lnet_free_text_bufs(&current_nets);
1500         LASSERT(lnet_tbnob == 0);
1501
1502         if (rc < 0)
1503                 return rc;
1504
1505         *networksp = networks;
1506         return count;
1507 }
1508
1509 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns, bool v6)
1510 {
1511         struct lnet_inetdev *ifaces = NULL;
1512         struct net_device *dev;
1513         int nalloc = 0;
1514         int nip = 0;
1515         DECLARE_CONST_IN_IFADDR(ifa);
1516
1517         rtnl_lock();
1518         for_each_netdev(ns, dev) {
1519                 int flags = dev_get_flags(dev);
1520                 struct in_device *in_dev;
1521                 struct inet6_dev *in6_dev;
1522                 const struct inet6_ifaddr *ifa6;
1523                 int node_id;
1524                 int cpt;
1525
1526                 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1527                         continue;
1528
1529                 if (!(flags & IFF_UP)) {
1530                         CWARN("lnet: Ignoring interface %s: it's down\n",
1531                               dev->name);
1532                         continue;
1533                 }
1534
1535                 node_id = dev_to_node(&dev->dev);
1536                 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1537
1538                 in_dev = __in_dev_get_rtnl(dev);
1539                 if (!in_dev) {
1540                         if (!v6)
1541                                 CWARN("lnet: Interface %s has no IPv4 status.\n",
1542                                       dev->name);
1543                         goto try_v6;
1544                 }
1545
1546                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1547                         if (nip >= nalloc) {
1548                                 struct lnet_inetdev *tmp;
1549
1550                                 nalloc += LNET_INTERFACES_NUM;
1551                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1552                                                GFP_KERNEL);
1553                                 if (!tmp) {
1554                                         kfree(ifaces);
1555                                         ifaces = NULL;
1556                                         nip = -ENOMEM;
1557                                         goto unlock_rtnl;
1558                                 }
1559                                 ifaces = tmp;
1560                         }
1561
1562                         ifaces[nip].li_cpt = cpt;
1563                         ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1564                         ifaces[nip].li_ipv6 = false;
1565                         ifaces[nip].li_index = dev->ifindex;
1566                         ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1567                         ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1568                         strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1569                                 sizeof(ifaces[nip].li_name));
1570                         nip++;
1571                 }
1572                 endfor_ifa(in_dev);
1573
1574         try_v6:
1575                 if (!v6)
1576                         continue;
1577 #if IS_ENABLED(CONFIG_IPV6)
1578                 in6_dev = __in6_dev_get(dev);
1579                 if (!in6_dev) {
1580                         if (!in_dev)
1581                                 CWARN("lnet: Interface %s has no IP status.\n",
1582                                       dev->name);
1583                         continue;
1584                 }
1585
1586                 list_for_each_entry_rcu(ifa6, &in6_dev->addr_list, if_list) {
1587                         if (ifa6->flags & IFA_F_TEMPORARY)
1588                                 continue;
1589                         if (nip >= nalloc) {
1590                                 struct lnet_inetdev *tmp;
1591
1592                                 nalloc += LNET_INTERFACES_NUM;
1593                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1594                                                GFP_KERNEL);
1595                                 if (!tmp) {
1596                                         kfree(ifaces);
1597                                         ifaces = NULL;
1598                                         nip = -ENOMEM;
1599                                         goto unlock_rtnl;
1600                                 }
1601                                 ifaces = tmp;
1602                         }
1603
1604                         ifaces[nip].li_cpt = cpt;
1605                         ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1606                         ifaces[nip].li_ipv6 = true;
1607                         ifaces[nip].li_index = dev->ifindex;
1608                         memcpy(ifaces[nip].li_ipv6addr,
1609                                &ifa6->addr, sizeof(struct in6_addr));
1610                         strlcpy(ifaces[nip].li_name, dev->name,
1611                                 sizeof(ifaces[nip].li_name));
1612                         nip++;
1613                         /* As different IPv6 addresses don't have unique
1614                          * labels, it is safest just to use the first
1615                          * and ignore the rest.
1616                          */
1617                         break;
1618                 }
1619 #endif /* IS_ENABLED(CONFIG_IPV6) */
1620
1621         }
1622 unlock_rtnl:
1623         rtnl_unlock();
1624
1625         if (nip == 0) {
1626                 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1627                 nip = -ENOENT;
1628         }
1629
1630         *dev_list = ifaces;
1631         return nip;
1632 }
1633 EXPORT_SYMBOL(lnet_inet_enumerate);
1634
1635 int
1636 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1637 {
1638         struct lnet_inetdev *ifaces = NULL;
1639         __u32     *ipaddrs = NULL;
1640         int nip;
1641         int        rc;
1642         int i;
1643
1644         if (current->nsproxy && current->nsproxy->net_ns)
1645                 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns,
1646                                           false);
1647         else
1648                 nip = lnet_inet_enumerate(&ifaces, &init_net, false);
1649         if (nip < 0) {
1650                 if (nip != -ENOENT) {
1651                         LCONSOLE_ERROR_MSG(0x117,
1652                                            "Error %d enumerating local IP interfaces for ip2nets to match\n",
1653                                            nip);
1654                 } else {
1655                         LCONSOLE_ERROR_MSG(0x118,
1656                                            "No local IP interfaces for ip2nets to match\n");
1657                 }
1658                 return nip;
1659         }
1660
1661         CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1662         if (!ipaddrs) {
1663                 rc = -ENOMEM;
1664                 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1665                        nip, rc);
1666                 goto out_free_addrs;
1667         }
1668
1669         for (i = 0; i < nip; i++)
1670                 ipaddrs[i] = ifaces[i].li_ipaddr;
1671
1672         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1673         if (rc < 0) {
1674                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1675         } else if (rc == 0) {
1676                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1677                                    "any local IP interfaces\n");
1678                 rc = -ENOENT;
1679         }
1680         CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1681 out_free_addrs:
1682         kfree(ifaces);
1683         return rc > 0 ? 0 : rc;
1684 }