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