Whamcloud - gitweb
b00267d422284e66d31db0ab46696e7a8b4d15db
[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, 2015, 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 -ENOMEM;
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 -EINVAL;
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 -EINVAL;
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         __u32             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 there are no hops set then we want to flag this value as
763          * unset since hops is an optional parameter */
764         if (!got_hops)
765                 hops = LNET_UNDEFINED_HOPS;
766
767         LASSERT(!list_empty(&nets));
768         LASSERT(!list_empty(&gateways));
769
770         list_for_each(tmp1, &nets) {
771                 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
772                 net = libcfs_str2net(ltb->ltb_text);
773                 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
774
775                 list_for_each(tmp2, &gateways) {
776                         ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
777                         nid = libcfs_str2nid(ltb->ltb_text);
778                         LASSERT(nid != LNET_NID_ANY);
779
780                         if (lnet_islocalnid(nid)) {
781                                 *im_a_router = 1;
782                                 continue;
783                         }
784
785                         rc = lnet_add_route(net, hops, nid, priority);
786                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
787                                 CERROR("Can't create route "
788                                        "to %s via %s\n",
789                                        libcfs_net2str(net),
790                                        libcfs_nid2str(nid));
791                                 goto out;
792                         }
793                 }
794         }
795
796         myrc = 0;
797         goto out;
798
799 token_error:
800         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
801 out:
802         lnet_free_text_bufs(&nets);
803         lnet_free_text_bufs(&gateways);
804         return myrc;
805 }
806
807 static int
808 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
809 {
810         struct lnet_text_buf   *ltb;
811
812         while (!list_empty(tbs)) {
813                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
814
815                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
816                         lnet_free_text_bufs(tbs);
817                         return -EINVAL;
818                 }
819
820                 list_del(&ltb->ltb_list);
821                 lnet_free_text_buf(ltb);
822         }
823
824         return 0;
825 }
826
827 int
828 lnet_parse_routes (char *routes, int *im_a_router)
829 {
830         struct list_head tbs;
831         int              rc = 0;
832
833         *im_a_router = 0;
834
835         INIT_LIST_HEAD(&tbs);
836
837         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
838                 CERROR("Error parsing routes\n");
839                 rc = -EINVAL;
840         } else {
841                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
842         }
843
844         LASSERT (lnet_tbnob == 0);
845         return rc;
846 }
847
848 static int
849 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
850 {
851         struct list_head list = LIST_HEAD_INIT(list);
852         int             rc;
853         int             i;
854
855         rc = cfs_ip_addr_parse(token, len, &list);
856         if (rc != 0)
857                 return rc;
858
859         for (rc = i = 0; !rc && i < nip; i++)
860                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
861
862         cfs_expr_list_free_list(&list);
863
864         return rc;
865 }
866
867 static int
868 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
869 {
870         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
871
872         int   matched = 0;
873         int   ntokens = 0;
874         int   len;
875         char *net = NULL;
876         char *sep;
877         char *token;
878         int   rc;
879
880         LASSERT (strlen(net_entry) < sizeof(tokens));
881
882         /* work on a copy of the string */
883         strcpy(tokens, net_entry);
884         sep = tokens;
885         for (;;) {
886                 /* scan for token start */
887                 while (isspace(*sep))
888                         sep++;
889                 if (*sep == 0)
890                         break;
891
892                 token = sep++;
893
894                 /* scan for token end */
895                 while (*sep != 0 && !isspace(*sep))
896                         sep++;
897                 if (*sep != 0)
898                         *sep++ = 0;
899
900                 if (ntokens++ == 0) {
901                         net = token;
902                         continue;
903                 }
904
905                 len = strlen(token);
906
907                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
908                 if (rc < 0) {
909                         lnet_syntax("ip2nets", net_entry,
910                                     (int)(token - tokens), len);
911                         return rc;
912                 }
913
914                 matched |= (rc != 0);
915         }
916
917         if (!matched)
918                 return 0;
919
920         strcpy(net_entry, net);                 /* replace with matched net */
921         return 1;
922 }
923
924 static __u32
925 lnet_netspec2net(char *netspec)
926 {
927         char   *bracket = strchr(netspec, '(');
928         __u32   net;
929
930         if (bracket != NULL)
931                 *bracket = 0;
932
933         net = libcfs_str2net(netspec);
934
935         if (bracket != NULL)
936                 *bracket = '(';
937
938         return net;
939 }
940
941 static int
942 lnet_splitnets(char *source, struct list_head *nets)
943 {
944         int               offset = 0;
945         int               offset2;
946         int               len;
947         struct lnet_text_buf  *tb;
948         struct lnet_text_buf  *tb2;
949         struct list_head *t;
950         char             *sep;
951         char             *bracket;
952         __u32             net;
953
954         LASSERT(!list_empty(nets));
955         LASSERT(nets->next == nets->prev);      /* single entry */
956
957         tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
958
959         for (;;) {
960                 sep = strchr(tb->ltb_text, ',');
961                 bracket = strchr(tb->ltb_text, '(');
962
963                 if (sep != NULL &&
964                     bracket != NULL &&
965                     bracket < sep) {
966                         /* netspec lists interfaces... */
967
968                         offset2 = offset + (int)(bracket - tb->ltb_text);
969                         len = strlen(bracket);
970
971                         bracket = strchr(bracket + 1, ')');
972
973                         if (bracket == NULL ||
974                             !(bracket[1] == ',' || bracket[1] == 0)) {
975                                 lnet_syntax("ip2nets", source, offset2, len);
976                                 return -EINVAL;
977                         }
978
979                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
980                 }
981
982                 if (sep != NULL)
983                         *sep++ = 0;
984
985                 net = lnet_netspec2net(tb->ltb_text);
986                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
987                         lnet_syntax("ip2nets", source, offset,
988                                     strlen(tb->ltb_text));
989                         return -EINVAL;
990                 }
991
992                 list_for_each(t, nets) {
993                         tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
994
995                         if (tb2 == tb)
996                                 continue;
997
998                         if (net == lnet_netspec2net(tb2->ltb_text)) {
999                                 /* duplicate network */
1000                                 lnet_syntax("ip2nets", source, offset,
1001                                             strlen(tb->ltb_text));
1002                                 return -EINVAL;
1003                         }
1004                 }
1005
1006                 if (sep == NULL)
1007                         return 0;
1008
1009                 offset += (int)(sep - tb->ltb_text);
1010                 len = strlen(sep);
1011                 tb2 = lnet_new_text_buf(len);
1012                 if (tb2 == NULL)
1013                         return -ENOMEM;
1014
1015                 strncpy(tb2->ltb_text, sep, len);
1016                 tb2->ltb_text[len] = '\0';
1017                 list_add_tail(&tb2->ltb_list, nets);
1018
1019                 tb = tb2;
1020         }
1021 }
1022
1023 static int
1024 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1025 {
1026         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1027         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1028
1029         struct list_head  raw_entries;
1030         struct list_head  matched_nets;
1031         struct list_head  current_nets;
1032         struct list_head *t;
1033         struct list_head *t2;
1034         struct lnet_text_buf  *tb;
1035         struct lnet_text_buf  *tb2;
1036         __u32             net1;
1037         __u32             net2;
1038         int               len;
1039         int               count;
1040         int               dup;
1041         int               rc;
1042
1043         INIT_LIST_HEAD(&raw_entries);
1044         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1045                 CERROR("Error parsing ip2nets\n");
1046                 LASSERT (lnet_tbnob == 0);
1047                 return -EINVAL;
1048         }
1049
1050         INIT_LIST_HEAD(&matched_nets);
1051         INIT_LIST_HEAD(&current_nets);
1052         networks[0] = 0;
1053         count = 0;
1054         len = 0;
1055         rc = 0;
1056
1057         while (!list_empty(&raw_entries)) {
1058                 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1059                                 ltb_list);
1060
1061                 strncpy(source, tb->ltb_text, sizeof(source));
1062                 source[sizeof(source) - 1] = '\0';
1063
1064                 /* replace ltb_text with the network(s) add on match */
1065                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1066                 if (rc < 0)
1067                         break;
1068
1069                 list_del(&tb->ltb_list);
1070
1071                 if (rc == 0) {                  /* no match */
1072                         lnet_free_text_buf(tb);
1073                         continue;
1074                 }
1075
1076                 /* split into separate networks */
1077                 INIT_LIST_HEAD(&current_nets);
1078                 list_add(&tb->ltb_list, &current_nets);
1079                 rc = lnet_splitnets(source, &current_nets);
1080                 if (rc < 0)
1081                         break;
1082
1083                 dup = 0;
1084                 list_for_each(t, &current_nets) {
1085                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1086                         net1 = lnet_netspec2net(tb->ltb_text);
1087                         LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1088
1089                         list_for_each(t2, &matched_nets) {
1090                                 tb2 = list_entry(t2, struct lnet_text_buf,
1091                                                  ltb_list);
1092                                 net2 = lnet_netspec2net(tb2->ltb_text);
1093                                 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1094
1095                                 if (net1 == net2) {
1096                                         dup = 1;
1097                                         break;
1098                                 }
1099                         }
1100
1101                         if (dup)
1102                                 break;
1103                 }
1104
1105                 if (dup) {
1106                         lnet_free_text_bufs(&current_nets);
1107                         continue;
1108                 }
1109
1110                 list_for_each_safe(t, t2, &current_nets) {
1111                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1112
1113                         list_del(&tb->ltb_list);
1114                         list_add_tail(&tb->ltb_list, &matched_nets);
1115
1116                         len += snprintf(networks + len, sizeof(networks) - len,
1117                                         "%s%s", (len == 0) ? "" : ",",
1118                                         tb->ltb_text);
1119
1120                         if (len >= sizeof(networks)) {
1121                                 CERROR("Too many matched networks\n");
1122                                 rc = -E2BIG;
1123                                 goto out;
1124                         }
1125                 }
1126
1127                 count++;
1128         }
1129
1130  out:
1131         lnet_free_text_bufs(&raw_entries);
1132         lnet_free_text_bufs(&matched_nets);
1133         lnet_free_text_bufs(&current_nets);
1134         LASSERT (lnet_tbnob == 0);
1135
1136         if (rc < 0)
1137                 return rc;
1138
1139         *networksp = networks;
1140         return count;
1141 }
1142
1143 static void
1144 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1145 {
1146         LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1147 }
1148
1149 static int
1150 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1151 {
1152         int        up;
1153         __u32      netmask;
1154         __u32     *ipaddrs;
1155         __u32     *ipaddrs2;
1156         int        nip;
1157         char     **ifnames;
1158         int        nif = lnet_ipif_enumerate(&ifnames);
1159         int        i;
1160         int        rc;
1161
1162         if (nif <= 0)
1163                 return nif;
1164
1165         LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1166         if (ipaddrs == NULL) {
1167                 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1168                 lnet_ipif_free_enumeration(ifnames, nif);
1169                 return -ENOMEM;
1170         }
1171
1172         for (i = nip = 0; i < nif; i++) {
1173                 if (!strcmp(ifnames[i], "lo"))
1174                         continue;
1175
1176                 rc = lnet_ipif_query(ifnames[i], &up,
1177                                        &ipaddrs[nip], &netmask);
1178                 if (rc != 0) {
1179                         CWARN("Can't query interface %s: %d\n",
1180                               ifnames[i], rc);
1181                         continue;
1182                 }
1183
1184                 if (!up) {
1185                         CWARN("Ignoring interface %s: it's down\n",
1186                               ifnames[i]);
1187                         continue;
1188                 }
1189
1190                 nip++;
1191         }
1192
1193         lnet_ipif_free_enumeration(ifnames, nif);
1194
1195         if (nip == nif) {
1196                 *ipaddrsp = ipaddrs;
1197         } else {
1198                 if (nip > 0) {
1199                         LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1200                         if (ipaddrs2 == NULL) {
1201                                 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1202                                 nip = -ENOMEM;
1203                         } else {
1204                                 memcpy(ipaddrs2, ipaddrs,
1205                                         nip * sizeof(*ipaddrs));
1206                                 *ipaddrsp = ipaddrs2;
1207                                 rc = nip;
1208                         }
1209                 }
1210                 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1211         }
1212         return nip;
1213 }
1214
1215 int
1216 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1217 {
1218         __u32     *ipaddrs = NULL;
1219         int        nip = lnet_ipaddr_enumerate(&ipaddrs);
1220         int        rc;
1221
1222         if (nip < 0) {
1223                 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1224                                    "interfaces for ip2nets to match\n", nip);
1225                 return nip;
1226         }
1227
1228         if (nip == 0) {
1229                 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1230                                    "for ip2nets to match\n");
1231                 return -ENOENT;
1232         }
1233
1234         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1235         lnet_ipaddr_free_enumeration(ipaddrs, nip);
1236
1237         if (rc < 0) {
1238                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1239                 return rc;
1240         }
1241
1242         if (rc == 0) {
1243                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1244                                    "any local IP interfaces\n");
1245                 return -ENOENT;
1246         }
1247
1248         return 0;
1249 }