Whamcloud - gitweb
LU-13641 socklnd: remove tcp bonding
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/ctype.h>
36 #include <linux/inetdevice.h>
37 #include <linux/nsproxy.h>
38 #include <net/net_namespace.h>
39 #include <lnet/lib-lnet.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 static void
56 lnet_syntax(const char *name, const char *str, int offset, int width)
57 {
58         static char dots[LNET_SINGLE_TEXTBUF_NOB];
59         static char dashes[LNET_SINGLE_TEXTBUF_NOB];
60
61         memset(dots, '.', sizeof(dots));
62         dots[sizeof(dots)-1] = 0;
63         memset(dashes, '-', sizeof(dashes));
64         dashes[sizeof(dashes)-1] = 0;
65
66         LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
67         LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
68                            (int)strlen(name), dots, offset, dots,
69                             (width < 1) ? 0 : width - 1, dashes);
70 }
71
72 static int
73 lnet_issep (char c)
74 {
75         switch (c) {
76         case '\n':
77         case '\r':
78         case ';':
79                 return 1;
80         default:
81                 return 0;
82         }
83 }
84
85 bool
86 lnet_net_unique(__u32 net_id, struct list_head *netlist,
87                 struct lnet_net **net)
88 {
89         struct lnet_net  *net_l;
90
91         if (!netlist)
92                 return true;
93
94         list_for_each_entry(net_l, netlist, net_list) {
95                 if (net_l->net_id == net_id) {
96                         if (net != NULL)
97                                 *net = net_l;
98                         return false;
99                 }
100         }
101
102         return true;
103 }
104
105 /* check that the NI is unique within the list of NIs already added to
106  * a network */
107 bool
108 lnet_ni_unique_net(struct list_head *nilist, char *iface)
109 {
110         struct list_head *tmp;
111         struct lnet_ni *ni;
112
113         list_for_each(tmp, nilist) {
114                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
115
116                 if (ni->ni_interface != NULL &&
117                     strncmp(ni->ni_interface, iface, strlen(iface)) == 0)
118                         return false;
119         }
120
121         return true;
122 }
123 static bool
124 in_array(__u32 *array, __u32 size, __u32 value)
125 {
126         int i;
127
128         for (i = 0; i < size; i++) {
129                 if (array[i] == value)
130                         return false;
131         }
132
133         return true;
134 }
135
136 static int
137 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
138 {
139         __u32 *added_cpts = NULL;
140         int i, j = 0, rc = 0;
141
142         /*
143          * no need to go futher since a subset of the NIs already exist on
144          * all CPTs
145          */
146         if (net->net_ncpts == LNET_CPT_NUMBER)
147                 return 0;
148
149         if (cpts == NULL) {
150                 /* there is an NI which will exist on all CPTs */
151                 if (net->net_cpts != NULL)
152                         CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
153                 net->net_cpts = NULL;
154                 net->net_ncpts = LNET_CPT_NUMBER;
155                 return 0;
156         }
157
158         if (net->net_cpts == NULL) {
159                 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
160                 if (net->net_cpts == NULL)
161                         return -ENOMEM;
162                 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
163                 net->net_ncpts = ncpts;
164                 return 0;
165         }
166
167         CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
168         if (added_cpts == NULL)
169                 return -ENOMEM;
170
171         for (i = 0; i < ncpts; i++) {
172                 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
173                         added_cpts[j] = cpts[i];
174                         j++;
175                 }
176         }
177
178         /* append the new cpts if any to the list of cpts in the net */
179         if (j > 0) {
180                 __u32 *array = NULL, *loc;
181                 __u32 total_entries = j + net->net_ncpts;
182
183                 CFS_ALLOC_PTR_ARRAY(array, total_entries);
184                 if (array == NULL) {
185                         rc = -ENOMEM;
186                         goto failed;
187                 }
188
189                 memcpy(array, net->net_cpts,
190                        net->net_ncpts * sizeof(*net->net_cpts));
191                 loc = array + net->net_ncpts;
192                 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
193
194                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
195                 net->net_ncpts = total_entries;
196                 net->net_cpts = array;
197         }
198
199 failed:
200         CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
201
202         return rc;
203 }
204
205 static void
206 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
207 {
208         struct lnet_ni *ni;
209         int rc;
210
211         /*
212          * Operation Assumption:
213          *      This function is called after an NI has been removed from
214          *      its parent net.
215          *
216          * if we're removing an NI which exists on all CPTs then
217          * we have to check if any of the other NIs on this net also
218          * exists on all CPTs. If none, then we need to build our Net CPT
219          * list based on the remaining NIs.
220          *
221          * If the NI being removed exist on a subset of the CPTs then we
222          * alo rebuild the Net CPT list based on the remaining NIs, which
223          * should resutl in the expected Net CPT list.
224          */
225
226         /*
227          * sometimes this function can be called due to some failure
228          * creating an NI, before any of the cpts are allocated, so check
229          * for that case and don't do anything
230          */
231         if (ncpts == 0)
232                 return;
233
234         if (ncpts == LNET_CPT_NUMBER) {
235                 /*
236                  * first iteration through the NI list in the net to see
237                  * if any of the NIs exist on all the CPTs. If one is
238                  * found then our job is done.
239                  */
240                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
241                         if (ni->ni_ncpts == LNET_CPT_NUMBER)
242                                 return;
243                 }
244         }
245
246         /*
247          * Rebuild the Net CPT list again, thereby only including only the
248          * CPTs which the remaining NIs are associated with.
249          */
250         if (net->net_cpts != NULL) {
251                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
252                 net->net_cpts = NULL;
253         }
254
255         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
256                 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
257                                           net);
258                 if (rc != 0) {
259                         CERROR("Out of Memory\n");
260                         /*
261                          * do our best to keep on going. Delete
262                          * the net cpts and set it to NULL. This
263                          * way we can keep on going but less
264                          * efficiently, since memory accesses might be
265                          * accross CPT lines.
266                          */
267                         if (net->net_cpts != NULL) {
268                                 CFS_FREE_PTR_ARRAY(net->net_cpts,
269                                                    net->net_ncpts);
270                                 net->net_cpts = NULL;
271                                 net->net_ncpts = LNET_CPT_NUMBER;
272                         }
273                         return;
274                 }
275         }
276 }
277
278 void
279 lnet_ni_free(struct lnet_ni *ni)
280 {
281         lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
282
283         if (ni->ni_refs != NULL)
284                 cfs_percpt_free(ni->ni_refs);
285
286         if (ni->ni_tx_queues != NULL)
287                 cfs_percpt_free(ni->ni_tx_queues);
288
289         if (ni->ni_cpts != NULL)
290                 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
291
292         if (ni->ni_interface != NULL) {
293                 LIBCFS_FREE(ni->ni_interface,
294                             strlen(ni->ni_interface) + 1);
295         }
296
297         /* release reference to net namespace */
298         if (ni->ni_net_ns != NULL)
299                 put_net(ni->ni_net_ns);
300
301         LIBCFS_FREE(ni, sizeof(*ni));
302 }
303
304 void
305 lnet_net_free(struct lnet_net *net)
306 {
307         struct list_head *tmp, *tmp2;
308         struct lnet_ni *ni;
309
310         LASSERT(list_empty(&net->net_ni_zombie));
311
312         /*
313          * delete any nis that haven't been added yet. This could happen
314          * if there is a failure on net startup
315          */
316         list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
317                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
318                 list_del_init(&ni->ni_netlist);
319                 lnet_ni_free(ni);
320         }
321
322         /* delete any nis which have been started. */
323         list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
324                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
325                 list_del_init(&ni->ni_netlist);
326                 lnet_ni_free(ni);
327         }
328
329         if (net->net_cpts != NULL)
330                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
331
332         LIBCFS_FREE(net, sizeof(*net));
333 }
334
335 struct lnet_net *
336 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
337 {
338         struct lnet_net         *net;
339
340         if (!lnet_net_unique(net_id, net_list, &net)) {
341                 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
342                        libcfs_net2str(net->net_id));
343                 return net;
344         }
345
346         LIBCFS_ALLOC(net, sizeof(*net));
347         if (net == NULL) {
348                 CERROR("Out of memory creating network %s\n",
349                        libcfs_net2str(net_id));
350                 return NULL;
351         }
352
353         INIT_LIST_HEAD(&net->net_list);
354         INIT_LIST_HEAD(&net->net_ni_list);
355         INIT_LIST_HEAD(&net->net_ni_added);
356         INIT_LIST_HEAD(&net->net_ni_zombie);
357         INIT_LIST_HEAD(&net->net_rtr_pref_nids);
358         spin_lock_init(&net->net_lock);
359
360         net->net_id = net_id;
361         net->net_last_alive = ktime_get_real_seconds();
362
363         net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
364
365         /* initialize global paramters to undefiend */
366         net->net_tunables.lct_peer_timeout = -1;
367         net->net_tunables.lct_max_tx_credits = -1;
368         net->net_tunables.lct_peer_tx_credits = -1;
369         net->net_tunables.lct_peer_rtr_credits = -1;
370
371         if (net_list)
372                 list_add_tail(&net->net_list, net_list);
373
374         return net;
375 }
376
377 static int
378 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
379 {
380         int niface = 0;
381
382         if (ni == NULL)
383                 return -ENOMEM;
384
385         /* Allocate a separate piece of memory and copy
386          * into it the string, so we don't have
387          * a depencency on the tokens string.  This way we
388          * can free the tokens at the end of the function.
389          * The newly allocated ni_interface can be
390          * freed when freeing the NI */
391         if (ni->ni_interface != NULL)
392                 niface++;
393
394         if (niface >= 1) {
395                 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
396                                    "for net %s\n",
397                                    libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
398                 return -EINVAL;
399         }
400
401         LIBCFS_ALLOC(ni->ni_interface,
402                      strlen(iface) + 1);
403
404         if (ni->ni_interface == NULL) {
405                 CERROR("Can't allocate net interface name\n");
406                 return -ENOMEM;
407         }
408
409         strncpy(ni->ni_interface, iface,
410                 strlen(iface) + 1);
411
412         return 0;
413 }
414
415 static struct lnet_ni *
416 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
417 {
418         struct lnet_tx_queue    *tq;
419         struct lnet_ni          *ni;
420         int                     i;
421
422         if (iface != NULL)
423                 /* make sure that this NI is unique in the net it's
424                  * being added to */
425                 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
426                         return NULL;
427
428         LIBCFS_ALLOC(ni, sizeof(*ni));
429         if (ni == NULL) {
430                 CERROR("Out of memory creating network interface %s%s\n",
431                        libcfs_net2str(net->net_id),
432                        (iface != NULL) ? iface : "");
433                 return NULL;
434         }
435
436         spin_lock_init(&ni->ni_lock);
437         INIT_LIST_HEAD(&ni->ni_netlist);
438         INIT_LIST_HEAD(&ni->ni_recovery);
439         LNetInvalidateMDHandle(&ni->ni_ping_mdh);
440         ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
441                                        sizeof(*ni->ni_refs[0]));
442         if (ni->ni_refs == NULL)
443                 goto failed;
444
445         ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
446                                             sizeof(*ni->ni_tx_queues[0]));
447         if (ni->ni_tx_queues == NULL)
448                 goto failed;
449
450         cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
451                 INIT_LIST_HEAD(&tq->tq_delayed);
452
453         ni->ni_net = net;
454         /* LND will fill in the address part of the NID */
455         ni->ni_nid = LNET_MKNID(net->net_id, 0);
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 (!list_empty(netlist)) {
809                 net = list_entry(netlist->next, struct lnet_net, net_list);
810
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 (!list_empty(tbs)) {
866                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
867
868                 list_del(&ltb->ltb_list);
869                 lnet_free_text_buf(ltb);
870         }
871 }
872
873 static int
874 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
875 {
876         LIST_HEAD(pending);
877         const char *sep;
878         int nob;
879         int i;
880         struct lnet_text_buf *ltb;
881
882         /* Split 'str' into separate commands */
883         for (;;) {
884                 /* skip leading whitespace */
885                 while (isspace(*str))
886                         str++;
887
888                 /* scan for separator or comment */
889                 for (sep = str; *sep != 0; sep++)
890                         if (lnet_issep(*sep) || *sep == '#')
891                                 break;
892
893                 nob = (int)(sep - str);
894                 if (nob > 0) {
895                         ltb = lnet_new_text_buf(nob);
896                         if (ltb == NULL) {
897                                 lnet_free_text_bufs(&pending);
898                                 return -ENOMEM;
899                         }
900
901                         for (i = 0; i < nob; i++)
902                                 if (isspace(str[i]))
903                                         ltb->ltb_text[i] = ' ';
904                                 else
905                                         ltb->ltb_text[i] = str[i];
906
907                         ltb->ltb_text[nob] = 0;
908
909                         list_add_tail(&ltb->ltb_list, &pending);
910                 }
911
912                 if (*sep == '#') {
913                         /* scan for separator */
914                         do {
915                                 sep++;
916                         } while (*sep != 0 && !lnet_issep(*sep));
917                 }
918
919                 if (*sep == 0)
920                         break;
921
922                 str = sep + 1;
923         }
924
925         list_splice(&pending, tbs->prev);
926         return 0;
927 }
928
929 static int
930 lnet_expand1tb(struct list_head *list,
931                char *str, char *sep1, char *sep2,
932                char *item, int itemlen)
933 {
934         int              len1 = (int)(sep1 - str);
935         int              len2 = strlen(sep2 + 1);
936         struct lnet_text_buf *ltb;
937
938         LASSERT (*sep1 == '[');
939         LASSERT (*sep2 == ']');
940
941         ltb = lnet_new_text_buf(len1 + itemlen + len2);
942         if (ltb == NULL)
943                 return -ENOMEM;
944
945         memcpy(ltb->ltb_text, str, len1);
946         memcpy(&ltb->ltb_text[len1], item, itemlen);
947         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
948         ltb->ltb_text[len1 + itemlen + len2] = 0;
949
950         list_add_tail(&ltb->ltb_list, list);
951         return 0;
952 }
953
954 static int
955 lnet_str2tbs_expand(struct list_head *tbs, char *str)
956 {
957         char              num[16];
958         LIST_HEAD(pending);
959         char             *sep;
960         char             *sep2;
961         char             *parsed;
962         char             *enditem;
963         int               lo;
964         int               hi;
965         int               stride;
966         int               i;
967         int               nob;
968         int               scanned;
969
970         sep = strchr(str, '[');
971         if (sep == NULL)                        /* nothing to expand */
972                 return 0;
973
974         sep2 = strchr(sep, ']');
975         if (sep2 == NULL)
976                 goto failed;
977
978         for (parsed = sep; parsed < sep2; parsed = enditem) {
979
980                 enditem = ++parsed;
981                 while (enditem < sep2 && *enditem != ',')
982                         enditem++;
983
984                 if (enditem == parsed)          /* no empty items */
985                         goto failed;
986
987                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
988
989                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
990
991                                 /* simple string enumeration */
992                                 if (lnet_expand1tb(&pending, str, sep, sep2,
993                                                    parsed, (int)(enditem - parsed)) != 0)
994                                         goto failed;
995
996                                 continue;
997                         }
998
999                         stride = 1;
1000                 }
1001
1002                 /* range expansion */
1003
1004                 if (enditem != parsed + scanned) /* no trailing junk */
1005                         goto failed;
1006
1007                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1008                     (hi - lo) % stride != 0)
1009                         goto failed;
1010
1011                 for (i = lo; i <= hi; i += stride) {
1012
1013                         snprintf(num, sizeof(num), "%d", i);
1014                         nob = strlen(num);
1015                         if (nob + 1 == sizeof(num))
1016                                 goto failed;
1017
1018                         if (lnet_expand1tb(&pending, str, sep, sep2,
1019                                            num, nob) != 0)
1020                                 goto failed;
1021                 }
1022         }
1023
1024         list_splice(&pending, tbs->prev);
1025         return 1;
1026
1027  failed:
1028         lnet_free_text_bufs(&pending);
1029         return -EINVAL;
1030 }
1031
1032 static int
1033 lnet_parse_hops (char *str, unsigned int *hops)
1034 {
1035         int     len = strlen(str);
1036         int     nob = len;
1037
1038         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1039                 nob == len &&
1040                 *hops > 0 && *hops < 256);
1041 }
1042
1043 #define LNET_PRIORITY_SEPARATOR (':')
1044
1045 static int
1046 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1047 {
1048         int   nob;
1049         char *sep;
1050         int   len;
1051
1052         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1053         if (sep == NULL) {
1054                 *priority = 0;
1055                 return 0;
1056         }
1057         len = strlen(sep + 1);
1058
1059         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1060                 /* Update the caller's token pointer so it treats the found
1061                    priority as the token to report in the error message. */
1062                 *token += sep - str + 1;
1063                 return -EINVAL;
1064         }
1065
1066         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1067
1068         /*
1069          * Change priority separator to \0 to be able to parse NID
1070          */
1071         *sep = '\0';
1072         return 0;
1073 }
1074
1075 static int
1076 lnet_parse_route(char *str, int *im_a_router)
1077 {
1078         /* static scratch buffer OK (single threaded) */
1079         static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1080
1081         LIST_HEAD(nets);
1082         LIST_HEAD(gateways);
1083         struct list_head *tmp1;
1084         struct list_head *tmp2;
1085         __u32 net;
1086         lnet_nid_t nid;
1087         struct lnet_text_buf *ltb;
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                                 nid = libcfs_str2nid(ltb->ltb_text);
1166                                 if (nid == LNET_NID_ANY || nid == LNET_NID_LO_0)
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(tmp1, &nets) {
1181                 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1182                 net = libcfs_str2net(ltb->ltb_text);
1183                 LASSERT(net != LNET_NET_ANY);
1184
1185                 list_for_each(tmp2, &gateways) {
1186                         ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1187                         nid = libcfs_str2nid(ltb->ltb_text);
1188                         LASSERT(nid != LNET_NID_ANY);
1189
1190                         if (lnet_islocalnid(nid)) {
1191                                 *im_a_router = 1;
1192                                 continue;
1193                         }
1194
1195                         rc = lnet_add_route(net, hops, nid, priority, 1);
1196                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1197                                 CERROR("Can't create route "
1198                                        "to %s via %s\n",
1199                                        libcfs_net2str(net),
1200                                        libcfs_nid2str(nid));
1201                                 goto out;
1202                         }
1203                 }
1204         }
1205
1206         myrc = 0;
1207         goto out;
1208
1209 token_error:
1210         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1211 out:
1212         lnet_free_text_bufs(&nets);
1213         lnet_free_text_bufs(&gateways);
1214         return myrc;
1215 }
1216
1217 static int
1218 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1219 {
1220         struct lnet_text_buf   *ltb;
1221
1222         while (!list_empty(tbs)) {
1223                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1224
1225                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1226                         lnet_free_text_bufs(tbs);
1227                         return -EINVAL;
1228                 }
1229
1230                 list_del(&ltb->ltb_list);
1231                 lnet_free_text_buf(ltb);
1232         }
1233
1234         return 0;
1235 }
1236
1237 int
1238 lnet_parse_routes(const char *routes, int *im_a_router)
1239 {
1240         LIST_HEAD(tbs);
1241         int rc = 0;
1242
1243         *im_a_router = 0;
1244
1245         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1246                 CERROR("Error parsing routes\n");
1247                 rc = -EINVAL;
1248         } else {
1249                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1250         }
1251
1252         LASSERT (lnet_tbnob == 0);
1253         return rc;
1254 }
1255
1256 static int
1257 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1258 {
1259         LIST_HEAD(list);
1260         int             rc;
1261         int             i;
1262
1263         rc = cfs_ip_addr_parse(token, len, &list);
1264         if (rc != 0)
1265                 return rc;
1266
1267         for (rc = i = 0; !rc && i < nip; i++)
1268                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1269
1270         cfs_expr_list_free_list(&list);
1271
1272         return rc;
1273 }
1274
1275 static int
1276 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1277 {
1278         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1279
1280         int   matched = 0;
1281         int   ntokens = 0;
1282         int   len;
1283         char *net = NULL;
1284         char *sep;
1285         char *token;
1286         int   rc;
1287
1288         LASSERT(strlen(net_entry) < sizeof(tokens));
1289
1290         /* work on a copy of the string */
1291         strcpy(tokens, net_entry);
1292         sep = tokens;
1293         for (;;) {
1294                 /* scan for token start */
1295                 while (isspace(*sep))
1296                         sep++;
1297                 if (*sep == 0)
1298                         break;
1299
1300                 token = sep++;
1301
1302                 /* scan for token end */
1303                 while (*sep != 0 && !isspace(*sep))
1304                         sep++;
1305                 if (*sep != 0)
1306                         *sep++ = 0;
1307
1308                 if (ntokens++ == 0) {
1309                         net = token;
1310                         continue;
1311                 }
1312
1313                 len = strlen(token);
1314
1315                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1316                 if (rc < 0) {
1317                         lnet_syntax("ip2nets", net_entry,
1318                                     (int)(token - tokens), len);
1319                         return rc;
1320                 }
1321
1322                 matched |= (rc != 0);
1323         }
1324
1325         if (!matched)
1326                 return 0;
1327
1328         strcpy(net_entry, net);                 /* replace with matched net */
1329         return 1;
1330 }
1331
1332 static __u32
1333 lnet_netspec2net(char *netspec)
1334 {
1335         char   *bracket = strchr(netspec, '(');
1336         __u32   net;
1337
1338         if (bracket != NULL)
1339                 *bracket = 0;
1340
1341         net = libcfs_str2net(netspec);
1342
1343         if (bracket != NULL)
1344                 *bracket = '(';
1345
1346         return net;
1347 }
1348
1349 static int
1350 lnet_splitnets(char *source, struct list_head *nets)
1351 {
1352         int               offset = 0;
1353         int               offset2;
1354         int               len;
1355         struct lnet_text_buf  *tb;
1356         struct lnet_text_buf  *tb2;
1357         struct list_head *t;
1358         char             *sep;
1359         char             *bracket;
1360         __u32             net;
1361
1362         LASSERT(!list_empty(nets));
1363         LASSERT(nets->next == nets->prev);      /* single entry */
1364
1365         tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1366
1367         for (;;) {
1368                 sep = strchr(tb->ltb_text, ',');
1369                 bracket = strchr(tb->ltb_text, '(');
1370
1371                 if (sep != NULL &&
1372                     bracket != NULL &&
1373                     bracket < sep) {
1374                         /* netspec lists interfaces... */
1375
1376                         offset2 = offset + (int)(bracket - tb->ltb_text);
1377                         len = strlen(bracket);
1378
1379                         bracket = strchr(bracket + 1, ')');
1380
1381                         if (bracket == NULL ||
1382                             !(bracket[1] == ',' || bracket[1] == 0)) {
1383                                 lnet_syntax("ip2nets", source, offset2, len);
1384                                 return -EINVAL;
1385                         }
1386
1387                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
1388                 }
1389
1390                 if (sep != NULL)
1391                         *sep++ = 0;
1392
1393                 net = lnet_netspec2net(tb->ltb_text);
1394                 if (net == LNET_NET_ANY) {
1395                         lnet_syntax("ip2nets", source, offset,
1396                                     strlen(tb->ltb_text));
1397                         return -EINVAL;
1398                 }
1399
1400                 list_for_each(t, nets) {
1401                         tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1402
1403                         if (tb2 == tb)
1404                                 continue;
1405
1406                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1407                                 /* duplicate network */
1408                                 lnet_syntax("ip2nets", source, offset,
1409                                             strlen(tb->ltb_text));
1410                                 return -EINVAL;
1411                         }
1412                 }
1413
1414                 if (sep == NULL)
1415                         return 0;
1416
1417                 offset += (int)(sep - tb->ltb_text);
1418                 len = strlen(sep);
1419                 tb2 = lnet_new_text_buf(len);
1420                 if (tb2 == NULL)
1421                         return -ENOMEM;
1422
1423                 strncpy(tb2->ltb_text, sep, len);
1424                 tb2->ltb_text[len] = '\0';
1425                 list_add_tail(&tb2->ltb_list, nets);
1426
1427                 tb = tb2;
1428         }
1429 }
1430
1431 static int
1432 lnet_match_networks(const char **networksp, const char *ip2nets,
1433                     __u32 *ipaddrs, int nip)
1434 {
1435         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1436         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1437
1438         LIST_HEAD(raw_entries);
1439         LIST_HEAD(matched_nets);
1440         LIST_HEAD(current_nets);
1441         struct list_head *t;
1442         struct list_head *t2;
1443         struct lnet_text_buf  *tb;
1444         int               len;
1445         int               count;
1446         int               rc;
1447
1448         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1449                 CERROR("Error parsing ip2nets\n");
1450                 LASSERT(lnet_tbnob == 0);
1451                 return -EINVAL;
1452         }
1453
1454         networks[0] = 0;
1455         count = 0;
1456         len = 0;
1457         rc = 0;
1458
1459         while (!list_empty(&raw_entries)) {
1460                 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1461                                 ltb_list);
1462
1463                 strncpy(source, tb->ltb_text, sizeof(source));
1464                 source[sizeof(source) - 1] = '\0';
1465
1466                 /* replace ltb_text with the network(s) add on match */
1467                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1468                 if (rc < 0)
1469                         break;
1470
1471                 list_del(&tb->ltb_list);
1472
1473                 if (rc == 0) {                  /* no match */
1474                         lnet_free_text_buf(tb);
1475                         continue;
1476                 }
1477
1478                 /* split into separate networks */
1479                 INIT_LIST_HEAD(&current_nets);
1480                 list_add(&tb->ltb_list, &current_nets);
1481                 rc = lnet_splitnets(source, &current_nets);
1482                 if (rc < 0)
1483                         break;
1484
1485                 list_for_each_safe(t, t2, &current_nets) {
1486                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1487
1488                         list_move_tail(&tb->ltb_list, &matched_nets);
1489
1490                         len += scnprintf(networks + len, sizeof(networks) - len,
1491                                          "%s%s", (len == 0) ? "" : ",",
1492                                          tb->ltb_text);
1493
1494                         if (len >= sizeof(networks)) {
1495                                 CERROR("Too many matched networks\n");
1496                                 rc = -E2BIG;
1497                                 goto out;
1498                         }
1499                 }
1500
1501                 count++;
1502         }
1503
1504  out:
1505         lnet_free_text_bufs(&raw_entries);
1506         lnet_free_text_bufs(&matched_nets);
1507         lnet_free_text_bufs(&current_nets);
1508         LASSERT(lnet_tbnob == 0);
1509
1510         if (rc < 0)
1511                 return rc;
1512
1513         *networksp = networks;
1514         return count;
1515 }
1516
1517 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns)
1518 {
1519         struct lnet_inetdev *ifaces = NULL;
1520         struct net_device *dev;
1521         int nalloc = 0;
1522         int nip = 0;
1523         DECLARE_CONST_IN_IFADDR(ifa);
1524
1525         rtnl_lock();
1526         for_each_netdev(ns, dev) {
1527                 int flags = dev_get_flags(dev);
1528                 struct in_device *in_dev;
1529                 int node_id;
1530                 int cpt;
1531
1532                 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1533                         continue;
1534
1535                 if (!(flags & IFF_UP)) {
1536                         CWARN("lnet: Ignoring interface %s: it's down\n",
1537                               dev->name);
1538                         continue;
1539                 }
1540
1541                 in_dev = __in_dev_get_rtnl(dev);
1542                 if (!in_dev) {
1543                         CWARN("lnet: Interface %s has no IPv4 status.\n",
1544                               dev->name);
1545                         continue;
1546                 }
1547
1548                 node_id = dev_to_node(&dev->dev);
1549                 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1550
1551                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1552                         if (nip >= nalloc) {
1553                                 struct lnet_inetdev *tmp;
1554
1555                                 nalloc += LNET_INTERFACES_NUM;
1556                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1557                                                GFP_KERNEL);
1558                                 if (!tmp) {
1559                                         kfree(ifaces);
1560                                         ifaces = NULL;
1561                                         nip = -ENOMEM;
1562                                         goto unlock_rtnl;
1563                                 }
1564                                 ifaces = tmp;
1565                         }
1566
1567                         ifaces[nip].li_cpt = cpt;
1568                         ifaces[nip].li_flags = flags;
1569                         ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1570                         ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1571                         strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1572                                 sizeof(ifaces[nip].li_name));
1573                         nip++;
1574                 }
1575                 endfor_ifa(in_dev);
1576         }
1577 unlock_rtnl:
1578         rtnl_unlock();
1579
1580         if (nip == 0) {
1581                 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1582                 nip = -ENOENT;
1583         }
1584
1585         *dev_list = ifaces;
1586         return nip;
1587 }
1588 EXPORT_SYMBOL(lnet_inet_enumerate);
1589
1590 int
1591 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1592 {
1593         struct lnet_inetdev *ifaces = NULL;
1594         __u32     *ipaddrs = NULL;
1595         int nip;
1596         int        rc;
1597         int i;
1598
1599         if (current->nsproxy && current->nsproxy->net_ns)
1600                 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns);
1601         else
1602                 nip = lnet_inet_enumerate(&ifaces, &init_net);
1603         if (nip < 0) {
1604                 if (nip != -ENOENT) {
1605                         LCONSOLE_ERROR_MSG(0x117,
1606                                            "Error %d enumerating local IP interfaces for ip2nets to match\n",
1607                                            nip);
1608                 } else {
1609                         LCONSOLE_ERROR_MSG(0x118,
1610                                            "No local IP interfaces for ip2nets to match\n");
1611                 }
1612                 return nip;
1613         }
1614
1615         CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1616         if (!ipaddrs) {
1617                 rc = -ENOMEM;
1618                 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1619                        nip, rc);
1620                 goto out_free_addrs;
1621         }
1622
1623         for (i = 0; i < nip; i++)
1624                 ipaddrs[i] = ifaces[i].li_ipaddr;
1625
1626         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1627         if (rc < 0) {
1628                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1629         } else if (rc == 0) {
1630                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1631                                    "any local IP interfaces\n");
1632                 rc = -ENOENT;
1633         }
1634         CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1635 out_free_addrs:
1636         kfree(ifaces);
1637         return rc > 0 ? 0 : rc;
1638 }