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