Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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_NIDNET(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 = LNET_MKNID(net->net_id, 0);
450
451         /* Store net namespace in which current ni is being created */
452         if (current->nsproxy && current->nsproxy->net_ns)
453                 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
454         else
455                 ni->ni_net_ns = get_net(&init_net);
456
457         ni->ni_state = LNET_NI_STATE_INIT;
458         ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
459         list_add_tail(&ni->ni_netlist, &net->net_ni_added);
460
461         /*
462          * if an interface name is provided then make sure to add in that
463          * interface name in NI
464          */
465         if (iface)
466                 if (lnet_ni_add_interface(ni, iface) != 0)
467                         goto failed;
468
469         return ni;
470 failed:
471         lnet_ni_free(ni);
472         return NULL;
473 }
474
475 /* allocate and add to the provided network */
476 struct lnet_ni *
477 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
478 {
479         struct lnet_ni          *ni;
480         int                     rc;
481
482         ni = lnet_ni_alloc_common(net, iface);
483         if (!ni)
484                 return NULL;
485
486         if (!el) {
487                 ni->ni_cpts  = NULL;
488                 ni->ni_ncpts = LNET_CPT_NUMBER;
489         } else {
490                 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
491                 if (rc <= 0) {
492                         CERROR("Failed to set CPTs for NI %s(%s): %d\n",
493                                libcfs_net2str(net->net_id),
494                                (iface != NULL) ? iface : "", rc);
495                         goto failed;
496                 }
497
498                 LASSERT(rc <= LNET_CPT_NUMBER);
499                 if (rc == LNET_CPT_NUMBER) {
500                         CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
501                         ni->ni_cpts = NULL;
502                 }
503
504                 ni->ni_ncpts = rc;
505         }
506
507         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
508         if (rc != 0)
509                 goto failed;
510
511         return ni;
512 failed:
513         lnet_ni_free(ni);
514         return NULL;
515 }
516
517 struct lnet_ni *
518 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
519                           char *iface)
520 {
521         struct lnet_ni          *ni;
522         int                     rc;
523
524         ni = lnet_ni_alloc_common(net, iface);
525         if (!ni)
526                 return NULL;
527
528         if (ncpts == 0) {
529                 ni->ni_cpts  = NULL;
530                 ni->ni_ncpts = LNET_CPT_NUMBER;
531         } else {
532                 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
533
534                 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
535                 if (ni->ni_cpts == NULL)
536                         goto failed;
537                 memcpy(ni->ni_cpts, cpts, array_size);
538                 ni->ni_ncpts = ncpts;
539         }
540
541         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
542         if (rc != 0)
543                 goto failed;
544
545         return ni;
546 failed:
547         lnet_ni_free(ni);
548         return NULL;
549 }
550
551 /*
552  * Parse the networks string and create the matching set of NIs on the
553  * nilist.
554  */
555 int
556 lnet_parse_networks(struct list_head *netlist, const char *networks)
557 {
558         struct cfs_expr_list *net_el = NULL;
559         struct cfs_expr_list *ni_el = NULL;
560         int             tokensize;
561         char            *tokens;
562         char            *str;
563         struct lnet_net *net;
564         struct lnet_ni  *ni = NULL;
565         __u32           net_id;
566         int             nnets = 0;
567
568         if (networks == NULL) {
569                 CERROR("networks string is undefined\n");
570                 return -EINVAL;
571         }
572
573         if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
574                 /* _WAY_ conservative */
575                 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
576                                    "long\n");
577                 return -EINVAL;
578         }
579
580         tokensize = strlen(networks) + 1;
581
582         LIBCFS_ALLOC(tokens, tokensize);
583         if (tokens == NULL) {
584                 CERROR("Can't allocate net tokens\n");
585                 return -ENOMEM;
586         }
587
588         memcpy(tokens, networks, tokensize);
589         str = tokens;
590
591         /*
592          * Main parser loop.
593          *
594          * NB we don't check interface conflicts here; it's the LNDs
595          * responsibility (if it cares at all)
596          */
597         do {
598                 char *nistr;
599                 char *elstr;
600                 char *name;
601                 int rc;
602
603                 /*
604                  * Parse a network string into its components.
605                  *
606                  * <name>{"("...")"}{"["<el>"]"}
607                  */
608
609                 /* Network name (mandatory) */
610                 while (isspace(*str))
611                         *str++ = '\0';
612                 if (!*str)
613                         break;
614                 name = str;
615                 str += strcspn(str, SPACESTR ":()[],");
616                 while (isspace(*str))
617                         *str++ = '\0';
618
619                 /* Interface list (optional) */
620                 if (*str == '(') {
621                         *str++ = '\0';
622                         nistr = str;
623                         str += strcspn(str, ")");
624                         if (*str != ')') {
625                                 str = nistr;
626                                 goto failed_syntax;
627                         }
628                         do {
629                                 *str++ = '\0';
630                         } while (isspace(*str));
631                 } else {
632                         nistr = NULL;
633                 }
634
635                 /* CPT expression (optional) */
636                 if (*str == '[') {
637                         elstr = str;
638                         str += strcspn(str, "]");
639                         if (*str != ']') {
640                                 str = elstr;
641                                 goto failed_syntax;
642                         }
643                         rc = cfs_expr_list_parse(elstr, str - elstr + 1,
644                                                 0, LNET_CPT_NUMBER - 1,
645                                                 &net_el);
646                         if (rc != 0) {
647                                 str = elstr;
648                                 goto failed_syntax;
649                         }
650                         *elstr = '\0';
651                         do {
652                                 *str++ = '\0';
653                         } while (isspace(*str));
654                 }
655
656                 /* Bad delimiters */
657                 if (*str && (strchr(DELIMITERS, *str) != NULL))
658                         goto failed_syntax;
659
660                 /* go to the next net if it exits */
661                 str += strcspn(str, ",");
662                 if (*str == ',')
663                         *str++ = '\0';
664
665                 /*
666                  * At this point the name is properly terminated.
667                  */
668                 net_id = libcfs_str2net(name);
669                 if (net_id == LNET_NET_ANY) {
670                         LCONSOLE_ERROR_MSG(0x113,
671                                         "Unrecognised network type\n");
672                         str = name;
673                         goto failed_syntax;
674                 }
675
676                 if (LNET_NETTYP(net_id) == LOLND) {
677                         /* Loopback is implicit, and there can be only one. */
678                         if (net_el) {
679                                 cfs_expr_list_free(net_el);
680                                 net_el = NULL;
681                         }
682                         /* Should we error out instead? */
683                         continue;
684                 }
685
686                 /*
687                  * All network paramaters are now known.
688                  */
689                 nnets++;
690
691                 /* always allocate a net, since we will eventually add an
692                  * interface to it, or we will fail, in which case we'll
693                  * just delete it */
694                 net = lnet_net_alloc(net_id, netlist);
695                 if (IS_ERR_OR_NULL(net))
696                         goto failed;
697
698                 if (!nistr) {
699                         /*
700                          * No interface list was specified, allocate a
701                          * ni using the defaults.
702                          */
703                         ni = lnet_ni_alloc(net, net_el, NULL);
704                         if (IS_ERR_OR_NULL(ni))
705                                 goto failed;
706
707                         if (!nistr) {
708                                 if (net_el) {
709                                         cfs_expr_list_free(net_el);
710                                         net_el = NULL;
711                                 }
712                                 continue;
713                         }
714                 }
715
716                 do {
717                         elstr = NULL;
718
719                         /* Interface name (mandatory) */
720                         while (isspace(*nistr))
721                                 *nistr++ = '\0';
722                         name = nistr;
723                         nistr += strcspn(nistr, SPACESTR "[],");
724                         while (isspace(*nistr))
725                                 *nistr++ = '\0';
726
727                         /* CPT expression (optional) */
728                         if (*nistr == '[') {
729                                 elstr = nistr;
730                                 nistr += strcspn(nistr, "]");
731                                 if (*nistr != ']') {
732                                         str = elstr;
733                                         goto failed_syntax;
734                                 }
735                                 rc = cfs_expr_list_parse(elstr,
736                                                         nistr - elstr + 1,
737                                                         0, LNET_CPT_NUMBER - 1,
738                                                         &ni_el);
739                                 if (rc != 0) {
740                                         str = elstr;
741                                         goto failed_syntax;
742                                 }
743                                 *elstr = '\0';
744                                 do {
745                                         *nistr++ = '\0';
746                                 } while (isspace(*nistr));
747                         } else {
748                                 ni_el = net_el;
749                         }
750
751                         /*
752                          * End of single interface specificaton,
753                          * advance to the start of the next one, if
754                          * any.
755                          */
756                         if (*nistr == ',') {
757                                 do {
758                                         *nistr++ = '\0';
759                                 } while (isspace(*nistr));
760                                 if (!*nistr) {
761                                         str = nistr;
762                                         goto failed_syntax;
763                                 }
764                         } else if (*nistr) {
765                                 str = nistr;
766                                 goto failed_syntax;
767                         }
768
769                         /*
770                          * At this point the name is properly terminated.
771                          */
772                         if (!*name) {
773                                 str = name;
774                                 goto failed_syntax;
775                         }
776
777                         ni = lnet_ni_alloc(net, ni_el, name);
778                         if (IS_ERR_OR_NULL(ni))
779                                 goto failed;
780
781                         if (ni_el) {
782                                 if (ni_el != net_el) {
783                                         cfs_expr_list_free(ni_el);
784                                         ni_el = NULL;
785                                 }
786                         }
787                 } while (*nistr);
788
789                 if (net_el) {
790                         cfs_expr_list_free(net_el);
791                         net_el = NULL;
792                 }
793         } while (*str);
794
795         LIBCFS_FREE(tokens, tokensize);
796         return nnets;
797
798  failed_syntax:
799         lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
800  failed:
801         /* free the net list and all the nis on each net */
802         while (!list_empty(netlist)) {
803                 net = list_entry(netlist->next, struct lnet_net, net_list);
804
805                 list_del_init(&net->net_list);
806                 lnet_net_free(net);
807         }
808
809         if (ni_el && ni_el != net_el)
810                 cfs_expr_list_free(ni_el);
811         if (net_el)
812                 cfs_expr_list_free(net_el);
813
814         LIBCFS_FREE(tokens, tokensize);
815
816         return -EINVAL;
817 }
818
819 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
820 {
821         struct lnet_text_buf *ltb;
822         int nob;
823
824         /* NB allocate space for the terminating 0 */
825         nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
826         if (nob > LNET_SINGLE_TEXTBUF_NOB) {
827                 /* _way_ conservative for "route net gateway..." */
828                 CERROR("text buffer too big\n");
829                 return NULL;
830         }
831
832         if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
833                 CERROR("Too many text buffers\n");
834                 return NULL;
835         }
836
837         LIBCFS_ALLOC(ltb, nob);
838         if (ltb == NULL)
839                 return NULL;
840
841         ltb->ltb_size = nob;
842         ltb->ltb_text[0] = 0;
843         lnet_tbnob += nob;
844         return ltb;
845 }
846
847 static void
848 lnet_free_text_buf(struct lnet_text_buf *ltb)
849 {
850         lnet_tbnob -= ltb->ltb_size;
851         LIBCFS_FREE(ltb, ltb->ltb_size);
852 }
853
854 static void
855 lnet_free_text_bufs(struct list_head *tbs)
856 {
857         struct lnet_text_buf  *ltb;
858
859         while (!list_empty(tbs)) {
860                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
861
862                 list_del(&ltb->ltb_list);
863                 lnet_free_text_buf(ltb);
864         }
865 }
866
867 static int
868 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
869 {
870         LIST_HEAD(pending);
871         const char *sep;
872         int nob;
873         int i;
874         struct lnet_text_buf *ltb;
875
876         /* Split 'str' into separate commands */
877         for (;;) {
878                 /* skip leading whitespace */
879                 while (isspace(*str))
880                         str++;
881
882                 /* scan for separator or comment */
883                 for (sep = str; *sep != 0; sep++)
884                         if (lnet_issep(*sep) || *sep == '#')
885                                 break;
886
887                 nob = (int)(sep - str);
888                 if (nob > 0) {
889                         ltb = lnet_new_text_buf(nob);
890                         if (ltb == NULL) {
891                                 lnet_free_text_bufs(&pending);
892                                 return -ENOMEM;
893                         }
894
895                         for (i = 0; i < nob; i++)
896                                 if (isspace(str[i]))
897                                         ltb->ltb_text[i] = ' ';
898                                 else
899                                         ltb->ltb_text[i] = str[i];
900
901                         ltb->ltb_text[nob] = 0;
902
903                         list_add_tail(&ltb->ltb_list, &pending);
904                 }
905
906                 if (*sep == '#') {
907                         /* scan for separator */
908                         do {
909                                 sep++;
910                         } while (*sep != 0 && !lnet_issep(*sep));
911                 }
912
913                 if (*sep == 0)
914                         break;
915
916                 str = sep + 1;
917         }
918
919         list_splice(&pending, tbs->prev);
920         return 0;
921 }
922
923 static int
924 lnet_expand1tb(struct list_head *list,
925                char *str, char *sep1, char *sep2,
926                char *item, int itemlen)
927 {
928         int              len1 = (int)(sep1 - str);
929         int              len2 = strlen(sep2 + 1);
930         struct lnet_text_buf *ltb;
931
932         LASSERT (*sep1 == '[');
933         LASSERT (*sep2 == ']');
934
935         ltb = lnet_new_text_buf(len1 + itemlen + len2);
936         if (ltb == NULL)
937                 return -ENOMEM;
938
939         memcpy(ltb->ltb_text, str, len1);
940         memcpy(&ltb->ltb_text[len1], item, itemlen);
941         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
942         ltb->ltb_text[len1 + itemlen + len2] = 0;
943
944         list_add_tail(&ltb->ltb_list, list);
945         return 0;
946 }
947
948 static int
949 lnet_str2tbs_expand(struct list_head *tbs, char *str)
950 {
951         char              num[16];
952         LIST_HEAD(pending);
953         char             *sep;
954         char             *sep2;
955         char             *parsed;
956         char             *enditem;
957         int               lo;
958         int               hi;
959         int               stride;
960         int               i;
961         int               nob;
962         int               scanned;
963
964         sep = strchr(str, '[');
965         if (sep == NULL)                        /* nothing to expand */
966                 return 0;
967
968         sep2 = strchr(sep, ']');
969         if (sep2 == NULL)
970                 goto failed;
971
972         for (parsed = sep; parsed < sep2; parsed = enditem) {
973
974                 enditem = ++parsed;
975                 while (enditem < sep2 && *enditem != ',')
976                         enditem++;
977
978                 if (enditem == parsed)          /* no empty items */
979                         goto failed;
980
981                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
982
983                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
984
985                                 /* simple string enumeration */
986                                 if (lnet_expand1tb(&pending, str, sep, sep2,
987                                                    parsed, (int)(enditem - parsed)) != 0)
988                                         goto failed;
989
990                                 continue;
991                         }
992
993                         stride = 1;
994                 }
995
996                 /* range expansion */
997
998                 if (enditem != parsed + scanned) /* no trailing junk */
999                         goto failed;
1000
1001                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1002                     (hi - lo) % stride != 0)
1003                         goto failed;
1004
1005                 for (i = lo; i <= hi; i += stride) {
1006
1007                         snprintf(num, sizeof(num), "%d", i);
1008                         nob = strlen(num);
1009                         if (nob + 1 == sizeof(num))
1010                                 goto failed;
1011
1012                         if (lnet_expand1tb(&pending, str, sep, sep2,
1013                                            num, nob) != 0)
1014                                 goto failed;
1015                 }
1016         }
1017
1018         list_splice(&pending, tbs->prev);
1019         return 1;
1020
1021  failed:
1022         lnet_free_text_bufs(&pending);
1023         return -EINVAL;
1024 }
1025
1026 static int
1027 lnet_parse_hops (char *str, unsigned int *hops)
1028 {
1029         int     len = strlen(str);
1030         int     nob = len;
1031
1032         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1033                 nob == len &&
1034                 *hops > 0 && *hops < 256);
1035 }
1036
1037 #define LNET_PRIORITY_SEPARATOR (':')
1038
1039 static int
1040 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1041 {
1042         int   nob;
1043         char *sep;
1044         int   len;
1045
1046         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1047         if (sep == NULL) {
1048                 *priority = 0;
1049                 return 0;
1050         }
1051         len = strlen(sep + 1);
1052
1053         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1054                 /* Update the caller's token pointer so it treats the found
1055                    priority as the token to report in the error message. */
1056                 *token += sep - str + 1;
1057                 return -EINVAL;
1058         }
1059
1060         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1061
1062         /*
1063          * Change priority separator to \0 to be able to parse NID
1064          */
1065         *sep = '\0';
1066         return 0;
1067 }
1068
1069 static int
1070 lnet_parse_route(char *str, int *im_a_router)
1071 {
1072         /* static scratch buffer OK (single threaded) */
1073         static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1074
1075         LIST_HEAD(nets);
1076         LIST_HEAD(gateways);
1077         struct list_head *tmp1;
1078         struct list_head *tmp2;
1079         __u32 net;
1080         lnet_nid_t nid;
1081         struct lnet_text_buf *ltb;
1082         int rc;
1083         char *sep;
1084         char *token = str;
1085         int ntokens = 0;
1086         int myrc = -1;
1087         __u32 hops;
1088         int got_hops = 0;
1089         unsigned int priority = 0;
1090
1091         /* save a copy of the string for error messages */
1092         strncpy(cmd, str, sizeof(cmd));
1093         cmd[sizeof(cmd) - 1] = '\0';
1094
1095         sep = str;
1096         for (;;) {
1097                 /* scan for token start */
1098                 while (isspace(*sep))
1099                         sep++;
1100                 if (*sep == 0) {
1101                         if (ntokens < (got_hops ? 3 : 2))
1102                                 goto token_error;
1103                         break;
1104                 }
1105
1106                 ntokens++;
1107                 token = sep++;
1108
1109                 /* scan for token end */
1110                 while (*sep != 0 && !isspace(*sep))
1111                         sep++;
1112                 if (*sep != 0)
1113                         *sep++ = 0;
1114
1115                 if (ntokens == 1) {
1116                         tmp2 = &nets;           /* expanding nets */
1117                 } else if (ntokens == 2 &&
1118                            lnet_parse_hops(token, &hops)) {
1119                         got_hops = 1;           /* got a hop count */
1120                         continue;
1121                 } else {
1122                         tmp2 = &gateways;       /* expanding gateways */
1123                 }
1124
1125                 ltb = lnet_new_text_buf(strlen(token));
1126                 if (ltb == NULL)
1127                         goto out;
1128
1129                 strcpy(ltb->ltb_text, token);
1130                 tmp1 = &ltb->ltb_list;
1131                 list_add_tail(tmp1, tmp2);
1132
1133                 while (tmp1 != tmp2) {
1134                         ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1135
1136                         rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1137                         if (rc < 0)
1138                                 goto token_error;
1139
1140                         tmp1 = tmp1->next;
1141
1142                         if (rc > 0) {           /* expanded! */
1143                                 list_del(&ltb->ltb_list);
1144                                 lnet_free_text_buf(ltb);
1145                                 continue;
1146                         }
1147
1148                         if (ntokens == 1) {
1149                                 net = libcfs_str2net(ltb->ltb_text);
1150                                 if (net == LNET_NET_ANY ||
1151                                     LNET_NETTYP(net) == LOLND)
1152                                         goto token_error;
1153                         } else {
1154                                 rc = lnet_parse_priority(ltb->ltb_text,
1155                                                          &priority, &token);
1156                                 if (rc < 0)
1157                                         goto token_error;
1158
1159                                 nid = libcfs_str2nid(ltb->ltb_text);
1160                                 if (nid == LNET_NID_ANY || nid == LNET_NID_LO_0)
1161                                         goto token_error;
1162                         }
1163                 }
1164         }
1165
1166         /* if there are no hops set then we want to flag this value as
1167          * unset since hops is an optional parameter */
1168         if (!got_hops)
1169                 hops = LNET_UNDEFINED_HOPS;
1170
1171         LASSERT(!list_empty(&nets));
1172         LASSERT(!list_empty(&gateways));
1173
1174         list_for_each(tmp1, &nets) {
1175                 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1176                 net = libcfs_str2net(ltb->ltb_text);
1177                 LASSERT(net != LNET_NET_ANY);
1178
1179                 list_for_each(tmp2, &gateways) {
1180                         ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1181                         nid = libcfs_str2nid(ltb->ltb_text);
1182                         LASSERT(nid != LNET_NID_ANY);
1183
1184                         if (lnet_islocalnid(nid)) {
1185                                 *im_a_router = 1;
1186                                 continue;
1187                         }
1188
1189                         rc = lnet_add_route(net, hops, nid, priority, 1);
1190                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1191                                 CERROR("Can't create route "
1192                                        "to %s via %s\n",
1193                                        libcfs_net2str(net),
1194                                        libcfs_nid2str(nid));
1195                                 goto out;
1196                         }
1197                 }
1198         }
1199
1200         myrc = 0;
1201         goto out;
1202
1203 token_error:
1204         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1205 out:
1206         lnet_free_text_bufs(&nets);
1207         lnet_free_text_bufs(&gateways);
1208         return myrc;
1209 }
1210
1211 static int
1212 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1213 {
1214         struct lnet_text_buf   *ltb;
1215
1216         while (!list_empty(tbs)) {
1217                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1218
1219                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1220                         lnet_free_text_bufs(tbs);
1221                         return -EINVAL;
1222                 }
1223
1224                 list_del(&ltb->ltb_list);
1225                 lnet_free_text_buf(ltb);
1226         }
1227
1228         return 0;
1229 }
1230
1231 int
1232 lnet_parse_routes(const char *routes, int *im_a_router)
1233 {
1234         LIST_HEAD(tbs);
1235         int rc = 0;
1236
1237         *im_a_router = 0;
1238
1239         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1240                 CERROR("Error parsing routes\n");
1241                 rc = -EINVAL;
1242         } else {
1243                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1244         }
1245
1246         LASSERT (lnet_tbnob == 0);
1247         return rc;
1248 }
1249
1250 static int
1251 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1252 {
1253         LIST_HEAD(list);
1254         int             rc;
1255         int             i;
1256
1257         rc = cfs_ip_addr_parse(token, len, &list);
1258         if (rc != 0)
1259                 return rc;
1260
1261         for (rc = i = 0; !rc && i < nip; i++)
1262                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1263
1264         cfs_expr_list_free_list(&list);
1265
1266         return rc;
1267 }
1268
1269 static int
1270 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1271 {
1272         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1273
1274         int   matched = 0;
1275         int   ntokens = 0;
1276         int   len;
1277         char *net = NULL;
1278         char *sep;
1279         char *token;
1280         int   rc;
1281
1282         LASSERT(strlen(net_entry) < sizeof(tokens));
1283
1284         /* work on a copy of the string */
1285         strcpy(tokens, net_entry);
1286         sep = tokens;
1287         for (;;) {
1288                 /* scan for token start */
1289                 while (isspace(*sep))
1290                         sep++;
1291                 if (*sep == 0)
1292                         break;
1293
1294                 token = sep++;
1295
1296                 /* scan for token end */
1297                 while (*sep != 0 && !isspace(*sep))
1298                         sep++;
1299                 if (*sep != 0)
1300                         *sep++ = 0;
1301
1302                 if (ntokens++ == 0) {
1303                         net = token;
1304                         continue;
1305                 }
1306
1307                 len = strlen(token);
1308
1309                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1310                 if (rc < 0) {
1311                         lnet_syntax("ip2nets", net_entry,
1312                                     (int)(token - tokens), len);
1313                         return rc;
1314                 }
1315
1316                 matched |= (rc != 0);
1317         }
1318
1319         if (!matched)
1320                 return 0;
1321
1322         strcpy(net_entry, net);                 /* replace with matched net */
1323         return 1;
1324 }
1325
1326 static __u32
1327 lnet_netspec2net(char *netspec)
1328 {
1329         char   *bracket = strchr(netspec, '(');
1330         __u32   net;
1331
1332         if (bracket != NULL)
1333                 *bracket = 0;
1334
1335         net = libcfs_str2net(netspec);
1336
1337         if (bracket != NULL)
1338                 *bracket = '(';
1339
1340         return net;
1341 }
1342
1343 static int
1344 lnet_splitnets(char *source, struct list_head *nets)
1345 {
1346         int               offset = 0;
1347         int               offset2;
1348         int               len;
1349         struct lnet_text_buf  *tb;
1350         struct lnet_text_buf  *tb2;
1351         struct list_head *t;
1352         char             *sep;
1353         char             *bracket;
1354         __u32             net;
1355
1356         LASSERT(!list_empty(nets));
1357         LASSERT(nets->next == nets->prev);      /* single entry */
1358
1359         tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1360
1361         for (;;) {
1362                 sep = strchr(tb->ltb_text, ',');
1363                 bracket = strchr(tb->ltb_text, '(');
1364
1365                 if (sep != NULL &&
1366                     bracket != NULL &&
1367                     bracket < sep) {
1368                         /* netspec lists interfaces... */
1369
1370                         offset2 = offset + (int)(bracket - tb->ltb_text);
1371                         len = strlen(bracket);
1372
1373                         bracket = strchr(bracket + 1, ')');
1374
1375                         if (bracket == NULL ||
1376                             !(bracket[1] == ',' || bracket[1] == 0)) {
1377                                 lnet_syntax("ip2nets", source, offset2, len);
1378                                 return -EINVAL;
1379                         }
1380
1381                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
1382                 }
1383
1384                 if (sep != NULL)
1385                         *sep++ = 0;
1386
1387                 net = lnet_netspec2net(tb->ltb_text);
1388                 if (net == LNET_NET_ANY) {
1389                         lnet_syntax("ip2nets", source, offset,
1390                                     strlen(tb->ltb_text));
1391                         return -EINVAL;
1392                 }
1393
1394                 list_for_each(t, nets) {
1395                         tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1396
1397                         if (tb2 == tb)
1398                                 continue;
1399
1400                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1401                                 /* duplicate network */
1402                                 lnet_syntax("ip2nets", source, offset,
1403                                             strlen(tb->ltb_text));
1404                                 return -EINVAL;
1405                         }
1406                 }
1407
1408                 if (sep == NULL)
1409                         return 0;
1410
1411                 offset += (int)(sep - tb->ltb_text);
1412                 len = strlen(sep);
1413                 tb2 = lnet_new_text_buf(len);
1414                 if (tb2 == NULL)
1415                         return -ENOMEM;
1416
1417                 strncpy(tb2->ltb_text, sep, len);
1418                 tb2->ltb_text[len] = '\0';
1419                 list_add_tail(&tb2->ltb_list, nets);
1420
1421                 tb = tb2;
1422         }
1423 }
1424
1425 static int
1426 lnet_match_networks(const char **networksp, const char *ip2nets,
1427                     __u32 *ipaddrs, int nip)
1428 {
1429         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1430         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1431
1432         LIST_HEAD(raw_entries);
1433         LIST_HEAD(matched_nets);
1434         LIST_HEAD(current_nets);
1435         struct list_head *t;
1436         struct list_head *t2;
1437         struct lnet_text_buf  *tb;
1438         int               len;
1439         int               count;
1440         int               rc;
1441
1442         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1443                 CERROR("Error parsing ip2nets\n");
1444                 LASSERT(lnet_tbnob == 0);
1445                 return -EINVAL;
1446         }
1447
1448         networks[0] = 0;
1449         count = 0;
1450         len = 0;
1451         rc = 0;
1452
1453         while (!list_empty(&raw_entries)) {
1454                 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1455                                 ltb_list);
1456
1457                 strncpy(source, tb->ltb_text, sizeof(source));
1458                 source[sizeof(source) - 1] = '\0';
1459
1460                 /* replace ltb_text with the network(s) add on match */
1461                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1462                 if (rc < 0)
1463                         break;
1464
1465                 list_del(&tb->ltb_list);
1466
1467                 if (rc == 0) {                  /* no match */
1468                         lnet_free_text_buf(tb);
1469                         continue;
1470                 }
1471
1472                 /* split into separate networks */
1473                 INIT_LIST_HEAD(&current_nets);
1474                 list_add(&tb->ltb_list, &current_nets);
1475                 rc = lnet_splitnets(source, &current_nets);
1476                 if (rc < 0)
1477                         break;
1478
1479                 list_for_each_safe(t, t2, &current_nets) {
1480                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1481
1482                         list_move_tail(&tb->ltb_list, &matched_nets);
1483
1484                         len += scnprintf(networks + len, sizeof(networks) - len,
1485                                          "%s%s", (len == 0) ? "" : ",",
1486                                          tb->ltb_text);
1487
1488                         if (len >= sizeof(networks)) {
1489                                 CERROR("Too many matched networks\n");
1490                                 rc = -E2BIG;
1491                                 goto out;
1492                         }
1493                 }
1494
1495                 count++;
1496         }
1497
1498  out:
1499         lnet_free_text_bufs(&raw_entries);
1500         lnet_free_text_bufs(&matched_nets);
1501         lnet_free_text_bufs(&current_nets);
1502         LASSERT(lnet_tbnob == 0);
1503
1504         if (rc < 0)
1505                 return rc;
1506
1507         *networksp = networks;
1508         return count;
1509 }
1510
1511 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns)
1512 {
1513         struct lnet_inetdev *ifaces = NULL;
1514         struct net_device *dev;
1515         int nalloc = 0;
1516         int nip = 0;
1517         DECLARE_CONST_IN_IFADDR(ifa);
1518
1519         rtnl_lock();
1520         for_each_netdev(ns, dev) {
1521                 int flags = dev_get_flags(dev);
1522                 struct in_device *in_dev;
1523                 int node_id;
1524                 int cpt;
1525
1526                 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1527                         continue;
1528
1529                 if (!(flags & IFF_UP)) {
1530                         CWARN("lnet: Ignoring interface %s: it's down\n",
1531                               dev->name);
1532                         continue;
1533                 }
1534
1535                 in_dev = __in_dev_get_rtnl(dev);
1536                 if (!in_dev) {
1537                         CWARN("lnet: Interface %s has no IPv4 status.\n",
1538                               dev->name);
1539                         continue;
1540                 }
1541
1542                 node_id = dev_to_node(&dev->dev);
1543                 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1544
1545                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1546                         if (nip >= nalloc) {
1547                                 struct lnet_inetdev *tmp;
1548
1549                                 nalloc += LNET_INTERFACES_NUM;
1550                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1551                                                GFP_KERNEL);
1552                                 if (!tmp) {
1553                                         kfree(ifaces);
1554                                         ifaces = NULL;
1555                                         nip = -ENOMEM;
1556                                         goto unlock_rtnl;
1557                                 }
1558                                 ifaces = tmp;
1559                         }
1560
1561                         ifaces[nip].li_cpt = cpt;
1562                         ifaces[nip].li_flags = flags;
1563                         ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1564                         ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1565                         strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1566                                 sizeof(ifaces[nip].li_name));
1567                         nip++;
1568                 }
1569                 endfor_ifa(in_dev);
1570         }
1571 unlock_rtnl:
1572         rtnl_unlock();
1573
1574         if (nip == 0) {
1575                 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1576                 nip = -ENOENT;
1577         }
1578
1579         *dev_list = ifaces;
1580         return nip;
1581 }
1582 EXPORT_SYMBOL(lnet_inet_enumerate);
1583
1584 int
1585 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1586 {
1587         struct lnet_inetdev *ifaces = NULL;
1588         __u32     *ipaddrs = NULL;
1589         int nip;
1590         int        rc;
1591         int i;
1592
1593         if (current->nsproxy && current->nsproxy->net_ns)
1594                 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns);
1595         else
1596                 nip = lnet_inet_enumerate(&ifaces, &init_net);
1597         if (nip < 0) {
1598                 if (nip != -ENOENT) {
1599                         LCONSOLE_ERROR_MSG(0x117,
1600                                            "Error %d enumerating local IP interfaces for ip2nets to match\n",
1601                                            nip);
1602                 } else {
1603                         LCONSOLE_ERROR_MSG(0x118,
1604                                            "No local IP interfaces for ip2nets to match\n");
1605                 }
1606                 return nip;
1607         }
1608
1609         CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1610         if (!ipaddrs) {
1611                 rc = -ENOMEM;
1612                 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1613                        nip, rc);
1614                 goto out_free_addrs;
1615         }
1616
1617         for (i = 0; i < nip; i++)
1618                 ipaddrs[i] = ifaces[i].li_ipaddr;
1619
1620         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1621         if (rc < 0) {
1622                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1623         } else if (rc == 0) {
1624                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1625                                    "any local IP interfaces\n");
1626                 rc = -ENOENT;
1627         }
1628         CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1629 out_free_addrs:
1630         kfree(ifaces);
1631         return rc > 0 ? 0 : rc;
1632 }