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