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