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