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