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