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