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