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