Whamcloud - gitweb
LU-5568 lnet: fix kernel crash when network failed to start
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
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 void
52 lnet_syntax(char *name, char *str, int offset, int width)
53 {
54         static char dots[LNET_SINGLE_TEXTBUF_NOB];
55         static char dashes[LNET_SINGLE_TEXTBUF_NOB];
56
57         memset(dots, '.', sizeof(dots));
58         dots[sizeof(dots)-1] = 0;
59         memset(dashes, '-', sizeof(dashes));
60         dashes[sizeof(dashes)-1] = 0;
61
62         LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
63         LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
64                            (int)strlen(name), dots, offset, dots,
65                             (width < 1) ? 0 : width - 1, dashes);
66 }
67
68 int
69 lnet_issep (char c)
70 {
71         switch (c) {
72         case '\n':
73         case '\r':
74         case ';':
75                 return 1;
76         default:
77                 return 0;
78         }
79 }
80
81 int
82 lnet_net_unique(__u32 net, struct list_head *nilist)
83 {
84         struct list_head *tmp;
85         lnet_ni_t        *ni;
86
87         list_for_each(tmp, nilist) {
88                 ni = list_entry(tmp, lnet_ni_t, ni_list);
89
90                 if (LNET_NIDNET(ni->ni_nid) == net)
91                         return 0;
92         }
93
94         return 1;
95 }
96
97 void
98 lnet_ni_free(struct lnet_ni *ni)
99 {
100         int i;
101
102         if (ni->ni_refs != NULL)
103                 cfs_percpt_free(ni->ni_refs);
104
105         if (ni->ni_tx_queues != NULL)
106                 cfs_percpt_free(ni->ni_tx_queues);
107
108         if (ni->ni_cpts != NULL)
109                 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
110
111 #ifndef __KERNEL__
112 # ifdef HAVE_LIBPTHREAD
113         pthread_mutex_destroy(&ni->ni_lock);
114 # endif
115 #endif
116         for (i = 0; i < LNET_MAX_INTERFACES &&
117                     ni->ni_interfaces[i] != NULL; i++) {
118                 LIBCFS_FREE(ni->ni_interfaces[i],
119                             strlen(ni->ni_interfaces[i]) + 1);
120         }
121         LIBCFS_FREE(ni, sizeof(*ni));
122 }
123
124 lnet_ni_t *
125 lnet_ni_alloc(__u32 net, struct cfs_expr_list *el, struct list_head *nilist)
126 {
127         struct lnet_tx_queue    *tq;
128         struct lnet_ni          *ni;
129         int                     rc;
130         int                     i;
131
132         if (!lnet_net_unique(net, nilist)) {
133                 LCONSOLE_ERROR_MSG(0x111, "Duplicate network specified: %s\n",
134                                    libcfs_net2str(net));
135                 return NULL;
136         }
137
138         LIBCFS_ALLOC(ni, sizeof(*ni));
139         if (ni == NULL) {
140                 CERROR("Out of memory creating network %s\n",
141                        libcfs_net2str(net));
142                 return NULL;
143         }
144
145 #ifdef __KERNEL__
146         spin_lock_init(&ni->ni_lock);
147 #else
148 # ifdef HAVE_LIBPTHREAD
149         pthread_mutex_init(&ni->ni_lock, NULL);
150 # endif
151 #endif
152         INIT_LIST_HEAD(&ni->ni_cptlist);
153         ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
154                                        sizeof(*ni->ni_refs[0]));
155         if (ni->ni_refs == NULL)
156                 goto failed;
157
158         ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
159                                             sizeof(*ni->ni_tx_queues[0]));
160         if (ni->ni_tx_queues == NULL)
161                 goto failed;
162
163         cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
164                 INIT_LIST_HEAD(&tq->tq_delayed);
165
166         if (el == NULL) {
167                 ni->ni_cpts  = NULL;
168                 ni->ni_ncpts = LNET_CPT_NUMBER;
169         } else {
170                 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
171                 if (rc <= 0) {
172                         CERROR("Failed to set CPTs for NI %s: %d\n",
173                                libcfs_net2str(net), rc);
174                         goto failed;
175                 }
176
177                 LASSERT(rc <= LNET_CPT_NUMBER);
178                 if (rc == LNET_CPT_NUMBER) {
179                         LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
180                         ni->ni_cpts = NULL;
181                 }
182
183                 ni->ni_ncpts = rc;
184         }
185
186         /* LND will fill in the address part of the NID */
187         ni->ni_nid = LNET_MKNID(net, 0);
188         ni->ni_last_alive = cfs_time_current_sec();
189         list_add_tail(&ni->ni_list, nilist);
190         return ni;
191  failed:
192         lnet_ni_free(ni);
193         return NULL;
194 }
195
196 int
197 lnet_parse_networks(struct list_head *nilist, char *networks)
198 {
199         struct cfs_expr_list *el = NULL;
200         int             tokensize;
201         char            *tokens;
202         char            *str;
203         char            *tmp;
204         struct lnet_ni  *ni;
205         __u32           net;
206         int             nnets = 0;
207
208         if (networks == NULL) {
209                 CERROR("networks string is undefined\n");
210                 return -EINVAL;
211         }
212
213         if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
214                 /* _WAY_ conservative */
215                 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
216                                    "long\n");
217                 return -EINVAL;
218         }
219
220         tokensize = strlen(networks) + 1;
221
222         LIBCFS_ALLOC(tokens, tokensize);
223         if (tokens == NULL) {
224                 CERROR("Can't allocate net tokens\n");
225                 return -ENOMEM;
226         }
227
228         memcpy(tokens, networks, tokensize);
229         str = tmp = tokens;
230
231         /* Add in the loopback network */
232         ni = lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, nilist);
233         if (ni == NULL)
234                 goto failed;
235
236         while (str != NULL && *str != 0) {
237                 char    *comma = strchr(str, ',');
238                 char    *bracket = strchr(str, '(');
239                 char    *square = strchr(str, '[');
240                 char    *iface;
241                 int     niface;
242                 int     rc;
243
244                 /* NB we don't check interface conflicts here; it's the LNDs
245                  * responsibility (if it cares at all) */
246
247                 if (square != NULL && (comma == NULL || square < comma)) {
248                         /* i.e: o2ib0(ib0)[1,2], number between square
249                          * brackets are CPTs this NI needs to be bond */
250                         if (bracket != NULL && bracket > square) {
251                                 tmp = square;
252                                 goto failed_syntax;
253                         }
254
255                         tmp = strchr(square, ']');
256                         if (tmp == NULL) {
257                                 tmp = square;
258                                 goto failed_syntax;
259                         }
260
261                         rc = cfs_expr_list_parse(square, tmp - square + 1,
262                                                  0, LNET_CPT_NUMBER - 1, &el);
263                         if (rc != 0) {
264                                 tmp = square;
265                                 goto failed_syntax;
266                         }
267
268                         while (square <= tmp)
269                                 *square++ = ' ';
270                 }
271
272                 if (bracket == NULL ||
273                     (comma != NULL && comma < bracket)) {
274
275                         /* no interface list specified */
276
277                         if (comma != NULL)
278                                 *comma++ = 0;
279                         net = libcfs_str2net(cfs_trimwhite(str));
280
281                         if (net == LNET_NIDNET(LNET_NID_ANY)) {
282                                 LCONSOLE_ERROR_MSG(0x113, "Unrecognised network"
283                                                    " type\n");
284                                 tmp = str;
285                                 goto failed_syntax;
286                         }
287
288                         if (LNET_NETTYP(net) != LOLND && /* LO is implicit */
289                             lnet_ni_alloc(net, el, nilist) == NULL)
290                                 goto failed;
291
292                         if (el != NULL) {
293                                 cfs_expr_list_free(el);
294                                 el = NULL;
295                         }
296
297                         str = comma;
298                         continue;
299                 }
300
301                 *bracket = 0;
302                 net = libcfs_str2net(cfs_trimwhite(str));
303                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
304                         tmp = str;
305                         goto failed_syntax;
306                 }
307
308                 nnets++;
309                 ni = lnet_ni_alloc(net, el, nilist);
310                 if (ni == NULL)
311                         goto failed;
312
313                 if (el != NULL) {
314                         cfs_expr_list_free(el);
315                         el = NULL;
316                 }
317
318                 niface = 0;
319                 iface = bracket + 1;
320
321                 bracket = strchr(iface, ')');
322                 if (bracket == NULL) {
323                         tmp = iface;
324                         goto failed_syntax;
325                 }
326
327                 *bracket = 0;
328                 do {
329                         comma = strchr(iface, ',');
330                         if (comma != NULL)
331                                 *comma++ = 0;
332
333                         iface = cfs_trimwhite(iface);
334                         if (*iface == 0) {
335                                 tmp = iface;
336                                 goto failed_syntax;
337                         }
338
339                         if (niface == LNET_MAX_INTERFACES) {
340                                 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
341                                                    "for net %s\n",
342                                                    libcfs_net2str(net));
343                                 goto failed;
344                         }
345
346                         /* Allocate a seperate piece of memory and copy
347                          * into it the string, so we don't have
348                          * a depencency on the tokens string.  This way we
349                          * can free the tokens at the end of the function.
350                          * The newly allocated ni_interfaces[] can be
351                          * freed when freeing the NI */
352                         LIBCFS_ALLOC(ni->ni_interfaces[niface],
353                                      strlen(iface) + 1);
354                         if (ni->ni_interfaces[niface] == NULL) {
355                                 CERROR("Can't allocate net interface name\n");
356                                 goto failed;
357                         }
358                         strncpy(ni->ni_interfaces[niface], iface,
359                                 strlen(iface));
360                         niface++;
361                         iface = comma;
362                 } while (iface != NULL);
363
364                 str = bracket + 1;
365                 comma = strchr(bracket + 1, ',');
366                 if (comma != NULL) {
367                         *comma = 0;
368                         str = cfs_trimwhite(str);
369                         if (*str != 0) {
370                                 tmp = str;
371                                 goto failed_syntax;
372                         }
373                         str = comma + 1;
374                         continue;
375                 }
376
377                 str = cfs_trimwhite(str);
378                 if (*str != 0) {
379                         tmp = str;
380                         goto failed_syntax;
381                 }
382         }
383
384         LASSERT(!list_empty(nilist));
385
386         LIBCFS_FREE(tokens, tokensize);
387         return nnets;
388
389  failed_syntax:
390         lnet_syntax("networks", networks, (int)(tmp - tokens), strlen(tmp));
391  failed:
392         while (!list_empty(nilist)) {
393                 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
394
395                 list_del(&ni->ni_list);
396                 lnet_ni_free(ni);
397         }
398
399         if (el != NULL)
400                 cfs_expr_list_free(el);
401
402         LIBCFS_FREE(tokens, tokensize);
403
404         return -EINVAL;
405 }
406
407 struct lnet_text_buf *lnet_new_text_buf(int str_len)
408 {
409         struct lnet_text_buf *ltb;
410         int nob;
411
412         /* NB allocate space for the terminating 0 */
413         nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
414         if (nob > LNET_SINGLE_TEXTBUF_NOB) {
415                 /* _way_ conservative for "route net gateway..." */
416                 CERROR("text buffer too big\n");
417                 return NULL;
418         }
419
420         if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
421                 CERROR("Too many text buffers\n");
422                 return NULL;
423         }
424
425         LIBCFS_ALLOC(ltb, nob);
426         if (ltb == NULL)
427                 return NULL;
428
429         ltb->ltb_size = nob;
430         ltb->ltb_text[0] = 0;
431         lnet_tbnob += nob;
432         return ltb;
433 }
434
435 void
436 lnet_free_text_buf(struct lnet_text_buf *ltb)
437 {
438         lnet_tbnob -= ltb->ltb_size;
439         LIBCFS_FREE(ltb, ltb->ltb_size);
440 }
441
442 void
443 lnet_free_text_bufs(struct list_head *tbs)
444 {
445         struct lnet_text_buf  *ltb;
446
447         while (!list_empty(tbs)) {
448                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
449
450                 list_del(&ltb->ltb_list);
451                 lnet_free_text_buf(ltb);
452         }
453 }
454
455 void
456 lnet_print_text_bufs(struct list_head *tbs)
457 {
458         struct list_head *tmp;
459         struct lnet_text_buf  *ltb;
460
461         list_for_each(tmp, tbs) {
462                 ltb = list_entry(tmp, struct lnet_text_buf, ltb_list);
463
464                 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
465         }
466
467         CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
468 }
469
470 int
471 lnet_str2tbs_sep(struct list_head *tbs, char *str)
472 {
473         struct list_head  pending;
474         char             *sep;
475         int               nob;
476         int               i;
477         struct lnet_text_buf  *ltb;
478
479         INIT_LIST_HEAD(&pending);
480
481         /* Split 'str' into separate commands */
482         for (;;) {
483                 /* skip leading whitespace */
484                 while (cfs_iswhite(*str))
485                         str++;
486
487                 /* scan for separator or comment */
488                 for (sep = str; *sep != 0; sep++)
489                         if (lnet_issep(*sep) || *sep == '#')
490                                 break;
491
492                 nob = (int)(sep - str);
493                 if (nob > 0) {
494                         ltb = lnet_new_text_buf(nob);
495                         if (ltb == NULL) {
496                                 lnet_free_text_bufs(&pending);
497                                 return -1;
498                         }
499
500                         for (i = 0; i < nob; i++)
501                                 if (cfs_iswhite(str[i]))
502                                         ltb->ltb_text[i] = ' ';
503                                 else
504                                         ltb->ltb_text[i] = str[i];
505
506                         ltb->ltb_text[nob] = 0;
507
508                         list_add_tail(&ltb->ltb_list, &pending);
509                 }
510
511                 if (*sep == '#') {
512                         /* scan for separator */
513                         do {
514                                 sep++;
515                         } while (*sep != 0 && !lnet_issep(*sep));
516                 }
517
518                 if (*sep == 0)
519                         break;
520
521                 str = sep + 1;
522         }
523
524         list_splice(&pending, tbs->prev);
525         return 0;
526 }
527
528 int
529 lnet_expand1tb(struct list_head *list,
530                char *str, char *sep1, char *sep2,
531                char *item, int itemlen)
532 {
533         int              len1 = (int)(sep1 - str);
534         int              len2 = strlen(sep2 + 1);
535         struct lnet_text_buf *ltb;
536
537         LASSERT (*sep1 == '[');
538         LASSERT (*sep2 == ']');
539
540         ltb = lnet_new_text_buf(len1 + itemlen + len2);
541         if (ltb == NULL)
542                 return -ENOMEM;
543
544         memcpy(ltb->ltb_text, str, len1);
545         memcpy(&ltb->ltb_text[len1], item, itemlen);
546         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
547         ltb->ltb_text[len1 + itemlen + len2] = 0;
548
549         list_add_tail(&ltb->ltb_list, list);
550         return 0;
551 }
552
553 int
554 lnet_str2tbs_expand(struct list_head *tbs, char *str)
555 {
556         char              num[16];
557         struct list_head  pending;
558         char             *sep;
559         char             *sep2;
560         char             *parsed;
561         char             *enditem;
562         int               lo;
563         int               hi;
564         int               stride;
565         int               i;
566         int               nob;
567         int               scanned;
568
569         INIT_LIST_HEAD(&pending);
570
571         sep = strchr(str, '[');
572         if (sep == NULL)                        /* nothing to expand */
573                 return 0;
574
575         sep2 = strchr(sep, ']');
576         if (sep2 == NULL)
577                 goto failed;
578
579         for (parsed = sep; parsed < sep2; parsed = enditem) {
580
581                 enditem = ++parsed;
582                 while (enditem < sep2 && *enditem != ',')
583                         enditem++;
584
585                 if (enditem == parsed)          /* no empty items */
586                         goto failed;
587
588                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
589
590                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
591
592                                 /* simple string enumeration */
593                                 if (lnet_expand1tb(&pending, str, sep, sep2,
594                                                    parsed, (int)(enditem - parsed)) != 0)
595                                         goto failed;
596
597                                 continue;
598                         }
599
600                         stride = 1;
601                 }
602
603                 /* range expansion */
604
605                 if (enditem != parsed + scanned) /* no trailing junk */
606                         goto failed;
607
608                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
609                     (hi - lo) % stride != 0)
610                         goto failed;
611
612                 for (i = lo; i <= hi; i += stride) {
613
614                         snprintf(num, sizeof(num), "%d", i);
615                         nob = strlen(num);
616                         if (nob + 1 == sizeof(num))
617                                 goto failed;
618
619                         if (lnet_expand1tb(&pending, str, sep, sep2,
620                                            num, nob) != 0)
621                                 goto failed;
622                 }
623         }
624
625         list_splice(&pending, tbs->prev);
626         return 1;
627
628  failed:
629         lnet_free_text_bufs(&pending);
630         return -1;
631 }
632
633 int
634 lnet_parse_hops (char *str, unsigned int *hops)
635 {
636         int     len = strlen(str);
637         int     nob = len;
638
639         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
640                 nob == len &&
641                 *hops > 0 && *hops < 256);
642 }
643
644 #define LNET_PRIORITY_SEPARATOR (':')
645
646 int
647 lnet_parse_priority(char *str, unsigned int *priority, char **token)
648 {
649         int   nob;
650         char *sep;
651         int   len;
652
653         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
654         if (sep == NULL) {
655                 *priority = 0;
656                 return 0;
657         }
658         len = strlen(sep + 1);
659
660         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
661                 /* Update the caller's token pointer so it treats the found
662                    priority as the token to report in the error message. */
663                 *token += sep - str + 1;
664                 return -1;
665         }
666
667         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
668
669         /*
670          * Change priority separator to \0 to be able to parse NID
671          */
672         *sep = '\0';
673         return 0;
674 }
675
676 int
677 lnet_parse_route (char *str, int *im_a_router)
678 {
679         /* static scratch buffer OK (single threaded) */
680         static char       cmd[LNET_SINGLE_TEXTBUF_NOB];
681
682         struct list_head  nets;
683         struct list_head  gateways;
684         struct list_head *tmp1;
685         struct list_head *tmp2;
686         __u32             net;
687         lnet_nid_t        nid;
688         struct lnet_text_buf  *ltb;
689         int               rc;
690         char             *sep;
691         char             *token = str;
692         int               ntokens = 0;
693         int               myrc = -1;
694         unsigned int      hops;
695         int               got_hops = 0;
696         unsigned int      priority = 0;
697
698         INIT_LIST_HEAD(&gateways);
699         INIT_LIST_HEAD(&nets);
700
701         /* save a copy of the string for error messages */
702         strncpy(cmd, str, sizeof(cmd));
703         cmd[sizeof(cmd) - 1] = '\0';
704
705         sep = str;
706         for (;;) {
707                 /* scan for token start */
708                 while (cfs_iswhite(*sep))
709                         sep++;
710                 if (*sep == 0) {
711                         if (ntokens < (got_hops ? 3 : 2))
712                                 goto token_error;
713                         break;
714                 }
715
716                 ntokens++;
717                 token = sep++;
718
719                 /* scan for token end */
720                 while (*sep != 0 && !cfs_iswhite(*sep))
721                         sep++;
722                 if (*sep != 0)
723                         *sep++ = 0;
724
725                 if (ntokens == 1) {
726                         tmp2 = &nets;           /* expanding nets */
727                 } else if (ntokens == 2 &&
728                            lnet_parse_hops(token, &hops)) {
729                         got_hops = 1;           /* got a hop count */
730                         continue;
731                 } else {
732                         tmp2 = &gateways;       /* expanding gateways */
733                 }
734
735                 ltb = lnet_new_text_buf(strlen(token));
736                 if (ltb == NULL)
737                         goto out;
738
739                 strcpy(ltb->ltb_text, token);
740                 tmp1 = &ltb->ltb_list;
741                 list_add_tail(tmp1, tmp2);
742
743                 while (tmp1 != tmp2) {
744                         ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
745
746                         rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
747                         if (rc < 0)
748                                 goto token_error;
749
750                         tmp1 = tmp1->next;
751
752                         if (rc > 0) {           /* expanded! */
753                                 list_del(&ltb->ltb_list);
754                                 lnet_free_text_buf(ltb);
755                                 continue;
756                         }
757
758                         if (ntokens == 1) {
759                                 net = libcfs_str2net(ltb->ltb_text);
760                                 if (net == LNET_NIDNET(LNET_NID_ANY) ||
761                                     LNET_NETTYP(net) == LOLND)
762                                         goto token_error;
763                         } else {
764                                 rc = lnet_parse_priority(ltb->ltb_text,
765                                                          &priority, &token);
766                                 if (rc < 0)
767                                         goto token_error;
768
769                                 nid = libcfs_str2nid(ltb->ltb_text);
770                                 if (nid == LNET_NID_ANY ||
771                                     LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
772                                         goto token_error;
773                         }
774                 }
775         }
776
777         if (!got_hops)
778                 hops = 1;
779
780         LASSERT(!list_empty(&nets));
781         LASSERT(!list_empty(&gateways));
782
783         list_for_each(tmp1, &nets) {
784                 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
785                 net = libcfs_str2net(ltb->ltb_text);
786                 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
787
788                 list_for_each(tmp2, &gateways) {
789                         ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
790                         nid = libcfs_str2nid(ltb->ltb_text);
791                         LASSERT(nid != LNET_NID_ANY);
792
793                         if (lnet_islocalnid(nid)) {
794                                 *im_a_router = 1;
795                                 continue;
796                         }
797
798                         rc = lnet_add_route(net, hops, nid, priority);
799                         if (rc != 0) {
800                                 CERROR("Can't create route "
801                                        "to %s via %s\n",
802                                        libcfs_net2str(net),
803                                        libcfs_nid2str(nid));
804                                 goto out;
805                         }
806                 }
807         }
808
809         myrc = 0;
810         goto out;
811
812 token_error:
813         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
814 out:
815         lnet_free_text_bufs(&nets);
816         lnet_free_text_bufs(&gateways);
817         return myrc;
818 }
819
820 int
821 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
822 {
823         struct lnet_text_buf   *ltb;
824
825         while (!list_empty(tbs)) {
826                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
827
828                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
829                         lnet_free_text_bufs(tbs);
830                         return -EINVAL;
831                 }
832
833                 list_del(&ltb->ltb_list);
834                 lnet_free_text_buf(ltb);
835         }
836
837         return 0;
838 }
839
840 int
841 lnet_parse_routes (char *routes, int *im_a_router)
842 {
843         struct list_head tbs;
844         int              rc = 0;
845
846         *im_a_router = 0;
847
848         INIT_LIST_HEAD(&tbs);
849
850         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
851                 CERROR("Error parsing routes\n");
852                 rc = -EINVAL;
853         } else {
854                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
855         }
856
857         LASSERT (lnet_tbnob == 0);
858         return rc;
859 }
860
861 int
862 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
863 {
864         struct list_head list = LIST_HEAD_INIT(list);
865         int             rc;
866         int             i;
867
868         rc = cfs_ip_addr_parse(token, len, &list);
869         if (rc != 0)
870                 return rc;
871
872         for (rc = i = 0; !rc && i < nip; i++)
873                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
874
875         cfs_ip_addr_free(&list);
876
877         return rc;
878 }
879
880 int
881 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
882 {
883         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
884
885         int   matched = 0;
886         int   ntokens = 0;
887         int   len;
888         char *net = NULL;
889         char *sep;
890         char *token;
891         int   rc;
892
893         LASSERT (strlen(net_entry) < sizeof(tokens));
894
895         /* work on a copy of the string */
896         strcpy(tokens, net_entry);
897         sep = tokens;
898         for (;;) {
899                 /* scan for token start */
900                 while (cfs_iswhite(*sep))
901                         sep++;
902                 if (*sep == 0)
903                         break;
904
905                 token = sep++;
906
907                 /* scan for token end */
908                 while (*sep != 0 && !cfs_iswhite(*sep))
909                         sep++;
910                 if (*sep != 0)
911                         *sep++ = 0;
912
913                 if (ntokens++ == 0) {
914                         net = token;
915                         continue;
916                 }
917
918                 len = strlen(token);
919
920                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
921                 if (rc < 0) {
922                         lnet_syntax("ip2nets", net_entry,
923                                     (int)(token - tokens), len);
924                         return rc;
925                 }
926
927                 matched |= (rc != 0);
928         }
929
930         if (!matched)
931                 return 0;
932
933         strcpy(net_entry, net);                 /* replace with matched net */
934         return 1;
935 }
936
937 __u32
938 lnet_netspec2net(char *netspec)
939 {
940         char   *bracket = strchr(netspec, '(');
941         __u32   net;
942
943         if (bracket != NULL)
944                 *bracket = 0;
945
946         net = libcfs_str2net(netspec);
947
948         if (bracket != NULL)
949                 *bracket = '(';
950
951         return net;
952 }
953
954 int
955 lnet_splitnets(char *source, struct list_head *nets)
956 {
957         int               offset = 0;
958         int               offset2;
959         int               len;
960         struct lnet_text_buf  *tb;
961         struct lnet_text_buf  *tb2;
962         struct list_head *t;
963         char             *sep;
964         char             *bracket;
965         __u32             net;
966
967         LASSERT(!list_empty(nets));
968         LASSERT(nets->next == nets->prev);      /* single entry */
969
970         tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
971
972         for (;;) {
973                 sep = strchr(tb->ltb_text, ',');
974                 bracket = strchr(tb->ltb_text, '(');
975
976                 if (sep != NULL &&
977                     bracket != NULL &&
978                     bracket < sep) {
979                         /* netspec lists interfaces... */
980
981                         offset2 = offset + (int)(bracket - tb->ltb_text);
982                         len = strlen(bracket);
983
984                         bracket = strchr(bracket + 1, ')');
985
986                         if (bracket == NULL ||
987                             !(bracket[1] == ',' || bracket[1] == 0)) {
988                                 lnet_syntax("ip2nets", source, offset2, len);
989                                 return -EINVAL;
990                         }
991
992                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
993                 }
994
995                 if (sep != NULL)
996                         *sep++ = 0;
997
998                 net = lnet_netspec2net(tb->ltb_text);
999                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1000                         lnet_syntax("ip2nets", source, offset,
1001                                     strlen(tb->ltb_text));
1002                         return -EINVAL;
1003                 }
1004
1005                 list_for_each(t, nets) {
1006                         tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1007
1008                         if (tb2 == tb)
1009                                 continue;
1010
1011                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1012                                 /* duplicate network */
1013                                 lnet_syntax("ip2nets", source, offset,
1014                                             strlen(tb->ltb_text));
1015                                 return -EINVAL;
1016                         }
1017                 }
1018
1019                 if (sep == NULL)
1020                         return 0;
1021
1022                 offset += (int)(sep - tb->ltb_text);
1023                 len = strlen(sep);
1024                 tb2 = lnet_new_text_buf(len);
1025                 if (tb2 == NULL)
1026                         return -ENOMEM;
1027
1028                 strncpy(tb2->ltb_text, sep, len);
1029                 tb2->ltb_text[len] = '\0';
1030                 list_add_tail(&tb2->ltb_list, nets);
1031
1032                 tb = tb2;
1033         }
1034 }
1035
1036 int
1037 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1038 {
1039         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1040         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1041
1042         struct list_head  raw_entries;
1043         struct list_head  matched_nets;
1044         struct list_head  current_nets;
1045         struct list_head *t;
1046         struct list_head *t2;
1047         struct lnet_text_buf  *tb;
1048         struct lnet_text_buf  *tb2;
1049         __u32             net1;
1050         __u32             net2;
1051         int               len;
1052         int               count;
1053         int               dup;
1054         int               rc;
1055
1056         INIT_LIST_HEAD(&raw_entries);
1057         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1058                 CERROR("Error parsing ip2nets\n");
1059                 LASSERT (lnet_tbnob == 0);
1060                 return -EINVAL;
1061         }
1062
1063         INIT_LIST_HEAD(&matched_nets);
1064         INIT_LIST_HEAD(&current_nets);
1065         networks[0] = 0;
1066         count = 0;
1067         len = 0;
1068         rc = 0;
1069
1070         while (!list_empty(&raw_entries)) {
1071                 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1072                                 ltb_list);
1073
1074                 strncpy(source, tb->ltb_text, sizeof(source));
1075                 source[sizeof(source) - 1] = '\0';
1076
1077                 /* replace ltb_text with the network(s) add on match */
1078                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1079                 if (rc < 0)
1080                         break;
1081
1082                 list_del(&tb->ltb_list);
1083
1084                 if (rc == 0) {                  /* no match */
1085                         lnet_free_text_buf(tb);
1086                         continue;
1087                 }
1088
1089                 /* split into separate networks */
1090                 INIT_LIST_HEAD(&current_nets);
1091                 list_add(&tb->ltb_list, &current_nets);
1092                 rc = lnet_splitnets(source, &current_nets);
1093                 if (rc < 0)
1094                         break;
1095
1096                 dup = 0;
1097                 list_for_each(t, &current_nets) {
1098                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1099                         net1 = lnet_netspec2net(tb->ltb_text);
1100                         LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1101
1102                         list_for_each(t2, &matched_nets) {
1103                                 tb2 = list_entry(t2, struct lnet_text_buf,
1104                                                  ltb_list);
1105                                 net2 = lnet_netspec2net(tb2->ltb_text);
1106                                 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1107
1108                                 if (net1 == net2) {
1109                                         dup = 1;
1110                                         break;
1111                                 }
1112                         }
1113
1114                         if (dup)
1115                                 break;
1116                 }
1117
1118                 if (dup) {
1119                         lnet_free_text_bufs(&current_nets);
1120                         continue;
1121                 }
1122
1123                 list_for_each_safe(t, t2, &current_nets) {
1124                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1125
1126                         list_del(&tb->ltb_list);
1127                         list_add_tail(&tb->ltb_list, &matched_nets);
1128
1129                         len += snprintf(networks + len, sizeof(networks) - len,
1130                                         "%s%s", (len == 0) ? "" : ",",
1131                                         tb->ltb_text);
1132
1133                         if (len >= sizeof(networks)) {
1134                                 CERROR("Too many matched networks\n");
1135                                 rc = -E2BIG;
1136                                 goto out;
1137                         }
1138                 }
1139
1140                 count++;
1141         }
1142
1143  out:
1144         lnet_free_text_bufs(&raw_entries);
1145         lnet_free_text_bufs(&matched_nets);
1146         lnet_free_text_bufs(&current_nets);
1147         LASSERT (lnet_tbnob == 0);
1148
1149         if (rc < 0)
1150                 return rc;
1151
1152         *networksp = networks;
1153         return count;
1154 }
1155
1156 #ifdef __KERNEL__
1157 void
1158 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1159 {
1160         LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1161 }
1162
1163 int
1164 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1165 {
1166         int        up;
1167         __u32      netmask;
1168         __u32     *ipaddrs;
1169         __u32     *ipaddrs2;
1170         int        nip;
1171         char     **ifnames;
1172         int        nif = libcfs_ipif_enumerate(&ifnames);
1173         int        i;
1174         int        rc;
1175
1176         if (nif <= 0)
1177                 return nif;
1178
1179         LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1180         if (ipaddrs == NULL) {
1181                 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1182                 libcfs_ipif_free_enumeration(ifnames, nif);
1183                 return -ENOMEM;
1184         }
1185
1186         for (i = nip = 0; i < nif; i++) {
1187                 if (!strcmp(ifnames[i], "lo"))
1188                         continue;
1189
1190                 rc = libcfs_ipif_query(ifnames[i], &up,
1191                                        &ipaddrs[nip], &netmask);
1192                 if (rc != 0) {
1193                         CWARN("Can't query interface %s: %d\n",
1194                               ifnames[i], rc);
1195                         continue;
1196                 }
1197
1198                 if (!up) {
1199                         CWARN("Ignoring interface %s: it's down\n",
1200                               ifnames[i]);
1201                         continue;
1202                 }
1203
1204                 nip++;
1205         }
1206
1207         libcfs_ipif_free_enumeration(ifnames, nif);
1208
1209         if (nip == nif) {
1210                 *ipaddrsp = ipaddrs;
1211         } else {
1212                 if (nip > 0) {
1213                         LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1214                         if (ipaddrs2 == NULL) {
1215                                 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1216                                 nip = -ENOMEM;
1217                         } else {
1218                                 memcpy(ipaddrs2, ipaddrs,
1219                                         nip * sizeof(*ipaddrs));
1220                                 *ipaddrsp = ipaddrs2;
1221                                 rc = nip;
1222                         }
1223                 }
1224                 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1225         }
1226         return nip;
1227 }
1228
1229 int
1230 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1231 {
1232         __u32     *ipaddrs;
1233         int        nip = lnet_ipaddr_enumerate(&ipaddrs);
1234         int        rc;
1235
1236         if (nip < 0) {
1237                 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1238                                    "interfaces for ip2nets to match\n", nip);
1239                 return nip;
1240         }
1241
1242         if (nip == 0) {
1243                 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1244                                    "for ip2nets to match\n");
1245                 return -ENOENT;
1246         }
1247
1248         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1249         lnet_ipaddr_free_enumeration(ipaddrs, nip);
1250
1251         if (rc < 0) {
1252                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1253                 return rc;
1254         }
1255
1256         if (rc == 0) {
1257                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1258                                    "any local IP interfaces\n");
1259                 return -ENOENT;
1260         }
1261
1262         return 0;
1263 }
1264
1265 int
1266 lnet_set_ip_niaddr (lnet_ni_t *ni)
1267 {
1268         __u32  net = LNET_NIDNET(ni->ni_nid);
1269         char **names;
1270         int    n;
1271         __u32  ip;
1272         __u32  netmask;
1273         int    up;
1274         int    i;
1275         int    rc;
1276
1277         /* Convenience for LNDs that use the IP address of a local interface as
1278          * the local address part of their NID */
1279
1280         if (ni->ni_interfaces[0] != NULL) {
1281
1282                 CLASSERT (LNET_MAX_INTERFACES > 1);
1283
1284                 if (ni->ni_interfaces[1] != NULL) {
1285                         CERROR("Net %s doesn't support multiple interfaces\n",
1286                                libcfs_net2str(net));
1287                         return -EPERM;
1288                 }
1289
1290                 rc = libcfs_ipif_query(ni->ni_interfaces[0],
1291                                        &up, &ip, &netmask);
1292                 if (rc != 0) {
1293                         CERROR("Net %s can't query interface %s: %d\n",
1294                                libcfs_net2str(net), ni->ni_interfaces[0], rc);
1295                         return -EPERM;
1296                 }
1297
1298                 if (!up) {
1299                         CERROR("Net %s can't use interface %s: it's down\n",
1300                                libcfs_net2str(net), ni->ni_interfaces[0]);
1301                         return -ENETDOWN;
1302                 }
1303
1304                 ni->ni_nid = LNET_MKNID(net, ip);
1305                 return 0;
1306         }
1307
1308         n = libcfs_ipif_enumerate(&names);
1309         if (n <= 0) {
1310                 CERROR("Net %s can't enumerate interfaces: %d\n",
1311                        libcfs_net2str(net), n);
1312                 return 0;
1313         }
1314
1315         for (i = 0; i < n; i++) {
1316                 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
1317                         continue;
1318
1319                 rc = libcfs_ipif_query(names[i], &up, &ip, &netmask);
1320
1321                 if (rc != 0) {
1322                         CWARN("Net %s can't query interface %s: %d\n",
1323                               libcfs_net2str(net), names[i], rc);
1324                         continue;
1325                 }
1326
1327                 if (!up) {
1328                         CWARN("Net %s ignoring interface %s (down)\n",
1329                               libcfs_net2str(net), names[i]);
1330                         continue;
1331                 }
1332
1333                 libcfs_ipif_free_enumeration(names, n);
1334                 ni->ni_nid = LNET_MKNID(net, ip);
1335                 return 0;
1336         }
1337
1338         CERROR("Net %s can't find any interfaces\n", libcfs_net2str(net));
1339         libcfs_ipif_free_enumeration(names, n);
1340         return -ENOENT;
1341 }
1342 EXPORT_SYMBOL(lnet_set_ip_niaddr);
1343
1344 #endif