Whamcloud - gitweb
LU-6245 uapi: move libcfs/lnet UAPI header into own uapi directory
[fs/lustre-release.git] / libcfs / libcfs / util / nidstrings.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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 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  * libcfs/libcfs/util/nidstrings.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdbool.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include <libcfs/util/string.h>
47 #include <linux/lnet/lnet-types.h>
48 #include <linux/lnet/nidstr.h>
49 #ifdef HAVE_NETDB_H
50 # include <netdb.h>
51 #endif
52
53 /* max value for numeric network address */
54 #define MAX_NUMERIC_VALUE 0xffffffff
55
56 #define IPSTRING_LENGTH 16
57
58 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
59  * consistent in all conversion functions.  Some code fragments are copied
60  * around for the sake of clarity...
61  */
62
63 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
64  * Choose the number of nidstrings to support the MAXIMUM expected number of
65  * concurrent users.  If there are more, the returned string will be volatile.
66  * NB this number must allow for a process to be descheduled for a timeslice
67  * between getting its string and using it.
68  */
69
70 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
71 static int       libcfs_nidstring_idx;
72
73 char *
74 libcfs_next_nidstring(void)
75 {
76         char          *str;
77
78         str = libcfs_nidstrings[libcfs_nidstring_idx++];
79         if (libcfs_nidstring_idx ==
80             sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
81                 libcfs_nidstring_idx = 0;
82
83         return str;
84 }
85
86 static int
87 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
88 {
89         *addr = 0;
90         return 1;
91 }
92
93 static void
94 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
95 {
96         snprintf(str, size, "%u.%u.%u.%u",
97                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
98                  (addr >> 8) & 0xff, addr & 0xff);
99 }
100
101 /* CAVEAT EMPTOR XscanfX
102  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
103  * sscanf may return immediately if it sees the terminating '0' in a string, so
104  * I initialise the %n variable to the expected length.  If sscanf sets it;
105  * fine, if it doesn't, then the scan ended at the end of the string, which is
106  * fine too :) */
107 static int
108 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
109 {
110         unsigned int    a;
111         unsigned int    b;
112         unsigned int    c;
113         unsigned int    d;
114         int             n = nob; /* XscanfX */
115
116         /* numeric IP? */
117         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
118             n == nob &&
119             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
120             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
121                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
122                 return 1;
123         }
124
125 #ifdef HAVE_GETHOSTBYNAME
126         /* known hostname? */
127         if (('a' <= str[0] && str[0] <= 'z') ||
128             ('A' <= str[0] && str[0] <= 'Z')) {
129                 char *tmp;
130
131                 tmp = calloc(1, nob + 1);
132                 if (tmp != NULL) {
133                         struct hostent *he;
134
135                         memcpy(tmp, str, nob);
136                         tmp[nob] = 0;
137
138                         he = gethostbyname(tmp);
139
140                         free(tmp);
141
142                         if (he != NULL) {
143                                 __u32 ip = *(__u32 *)he->h_addr;
144
145                                 *addr = ntohl(ip);
146                                 return 1;
147                         }
148                 }
149         }
150 #endif
151         return 0;
152 }
153
154 int
155 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
156 {
157         struct cfs_expr_list *el;
158         struct cfs_lstr src;
159         int rc;
160         int i;
161
162         src.ls_str = str;
163         src.ls_len = len;
164         i = 0;
165
166         while (src.ls_str != NULL) {
167                 struct cfs_lstr res;
168
169                 if (!cfs_gettok(&src, '.', &res)) {
170                         rc = -EINVAL;
171                         goto out;
172                 }
173
174                 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
175                 if (rc != 0)
176                         goto out;
177
178                 list_add_tail(&el->el_link, list);
179                 i++;
180         }
181
182         if (i == 4)
183                 return 0;
184
185         rc = -EINVAL;
186 out:
187         cfs_expr_list_free_list(list);
188
189         return rc;
190 }
191
192 static int
193 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
194 {
195         int i = 0, j = 0;
196         struct cfs_expr_list *el;
197
198         list_for_each_entry(el, list, el_link) {
199                 assert(j++ < 4);
200                 if (i != 0)
201                         i += snprintf(buffer + i, count - i, ".");
202                 i += cfs_expr_list_print(buffer + i, count - i, el);
203         }
204         return i;
205 }
206
207 /**
208  * Matches address (\a addr) against address set encoded in \a list.
209  *
210  * \retval 1 if \a addr matches
211  * \retval 0 otherwise
212  */
213 int
214 cfs_ip_addr_match(__u32 addr, struct list_head *list)
215 {
216         struct cfs_expr_list *el;
217         int i = 0;
218
219         list_for_each_entry_reverse(el, list, el_link) {
220                 if (!cfs_expr_list_match(addr & 0xff, el))
221                         return 0;
222                 addr >>= 8;
223                 i++;
224         }
225
226         return i == 4;
227 }
228
229 static void
230 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
231 {
232         snprintf(str, size, "%u", addr);
233 }
234
235 static int
236 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
237 {
238         int     n;
239
240         n = nob;
241         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
242                 return 1;
243
244         n = nob;
245         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
246                 return 1;
247
248         n = nob;
249         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
250                 return 1;
251
252         return 0;
253 }
254
255 /**
256  * Nf_parse_addrlist method for networks using numeric addresses.
257  *
258  * Examples of such networks are gm and elan.
259  *
260  * \retval 0 if \a str parsed to numeric address
261  * \retval errno otherwise
262  */
263 static int
264 libcfs_num_parse(char *str, int len, struct list_head *list)
265 {
266         struct cfs_expr_list *el;
267         int     rc;
268
269         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
270         if (rc == 0)
271                 list_add_tail(&el->el_link, list);
272
273         return rc;
274 }
275
276 static int
277 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
278 {
279         struct cfs_expr_list *el;
280         int i = 0, j = 0;
281
282         list_for_each_entry(el, list, el_link) {
283                 assert(j++ < 1);
284                 i += cfs_expr_list_print(buffer + i, count - i, el);
285         }
286         return i;
287 }
288
289 /*
290  * Nf_match_addr method for networks using numeric addresses
291  *
292  * \retval 1 on match
293  * \retval 0 otherwise
294  */
295 static int
296 libcfs_num_match(__u32 addr, struct list_head *numaddr)
297 {
298         struct cfs_expr_list *el;
299
300         assert(!list_empty(numaddr));
301         el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
302
303         return cfs_expr_list_match(addr, el);
304 }
305
306 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
307 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
308 static bool cfs_num_is_contiguous(struct list_head *nidlist);
309 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
310
311 static struct netstrfns libcfs_netstrfns[] = {
312         {
313                 .nf_type                = LOLND,
314                 .nf_name                = "lo",
315                 .nf_modname             = "klolnd",
316                 .nf_addr2str            = libcfs_decnum_addr2str,
317                 .nf_str2addr            = libcfs_lo_str2addr,
318                 .nf_parse_addrlist      = libcfs_num_parse,
319                 .nf_print_addrlist      = libcfs_num_addr_range_print,
320                 .nf_match_addr          = libcfs_num_match,
321                 .nf_is_contiguous       = cfs_num_is_contiguous,
322                 .nf_min_max             = cfs_num_min_max
323         },
324         {
325                 .nf_type                = SOCKLND,
326                 .nf_name                = "tcp",
327                 .nf_modname             = "ksocklnd",
328                 .nf_addr2str            = libcfs_ip_addr2str,
329                 .nf_str2addr            = libcfs_ip_str2addr,
330                 .nf_parse_addrlist      = cfs_ip_addr_parse,
331                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
332                 .nf_match_addr          = cfs_ip_addr_match,
333                 .nf_is_contiguous       = cfs_ip_is_contiguous,
334                 .nf_min_max             = cfs_ip_min_max
335         },
336         {
337                 .nf_type                = O2IBLND,
338                 .nf_name                = "o2ib",
339                 .nf_modname             = "ko2iblnd",
340                 .nf_addr2str            = libcfs_ip_addr2str,
341                 .nf_str2addr            = libcfs_ip_str2addr,
342                 .nf_parse_addrlist      = cfs_ip_addr_parse,
343                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
344                 .nf_match_addr          = cfs_ip_addr_match,
345                 .nf_is_contiguous       = cfs_ip_is_contiguous,
346                 .nf_min_max             = cfs_ip_min_max
347         },
348         {
349                 .nf_type                = GNILND,
350                 .nf_name                = "gni",
351                 .nf_modname             = "kgnilnd",
352                 .nf_addr2str            = libcfs_decnum_addr2str,
353                 .nf_str2addr            = libcfs_num_str2addr,
354                 .nf_parse_addrlist      = libcfs_num_parse,
355                 .nf_print_addrlist      = libcfs_num_addr_range_print,
356                 .nf_match_addr          = libcfs_num_match,
357                 .nf_is_contiguous       = cfs_num_is_contiguous,
358                 .nf_min_max             = cfs_num_min_max
359         },
360         {
361                 .nf_type                = GNIIPLND,
362                 .nf_name                = "gip",
363                 .nf_modname             = "kgnilnd",
364                 .nf_addr2str            = libcfs_ip_addr2str,
365                 .nf_str2addr            = libcfs_ip_str2addr,
366                 .nf_parse_addrlist      = cfs_ip_addr_parse,
367                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
368                 .nf_match_addr          = cfs_ip_addr_match,
369                 .nf_is_contiguous       = cfs_ip_is_contiguous,
370                 .nf_min_max             = cfs_ip_min_max
371         },
372         {
373                 .nf_type                = PTL4LND,
374                 .nf_name                = "ptlf",
375                 .nf_modname             = "kptl4lnd",
376                 .nf_addr2str            = libcfs_decnum_addr2str,
377                 .nf_str2addr            = libcfs_num_str2addr,
378                 .nf_parse_addrlist      = libcfs_num_parse,
379                 .nf_print_addrlist      = libcfs_num_addr_range_print,
380                 .nf_match_addr          = libcfs_num_match,
381                 .nf_is_contiguous       = cfs_num_is_contiguous,
382                 .nf_min_max             = cfs_num_min_max
383         }
384 };
385
386 static const size_t libcfs_nnetstrfns =
387         sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
388
389 static struct netstrfns *
390 libcfs_lnd2netstrfns(__u32 lnd)
391 {
392         int     i;
393
394         for (i = 0; i < libcfs_nnetstrfns; i++)
395                 if (lnd == libcfs_netstrfns[i].nf_type)
396                         return &libcfs_netstrfns[i];
397
398         return NULL;
399 }
400
401 static struct netstrfns *
402 libcfs_namenum2netstrfns(const char *name)
403 {
404         struct netstrfns *nf;
405         int               i;
406
407         for (i = 0; i < libcfs_nnetstrfns; i++) {
408                 nf = &libcfs_netstrfns[i];
409                 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
410                         return nf;
411         }
412         return NULL;
413 }
414
415 static struct netstrfns *
416 libcfs_name2netstrfns(const char *name)
417 {
418         int    i;
419
420         for (i = 0; i < libcfs_nnetstrfns; i++)
421                 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
422                         return &libcfs_netstrfns[i];
423
424         return NULL;
425 }
426
427 int
428 libcfs_isknown_lnd(__u32 lnd)
429 {
430         return libcfs_lnd2netstrfns(lnd) != NULL;
431 }
432
433 char *
434 libcfs_lnd2modname(__u32 lnd)
435 {
436         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
437
438         return (nf == NULL) ? NULL : nf->nf_modname;
439 }
440
441 int
442 libcfs_str2lnd(const char *str)
443 {
444         struct netstrfns *nf = libcfs_name2netstrfns(str);
445
446         if (nf != NULL)
447                 return nf->nf_type;
448
449         return -1;
450 }
451
452 char *
453 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
454 {
455         struct netstrfns *nf;
456
457         nf = libcfs_lnd2netstrfns(lnd);
458         if (nf == NULL)
459                 snprintf(buf, buf_size, "?%u?", lnd);
460         else
461                 snprintf(buf, buf_size, "%s", nf->nf_name);
462
463         return buf;
464 }
465
466 char *
467 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
468 {
469         __u32             nnum = LNET_NETNUM(net);
470         __u32             lnd  = LNET_NETTYP(net);
471         struct netstrfns *nf;
472
473         nf = libcfs_lnd2netstrfns(lnd);
474         if (nf == NULL)
475                 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
476         else if (nnum == 0)
477                 snprintf(buf, buf_size, "%s", nf->nf_name);
478         else
479                 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
480
481         return buf;
482 }
483
484 char *
485 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
486 {
487         __u32             addr = LNET_NIDADDR(nid);
488         __u32             net  = LNET_NIDNET(nid);
489         __u32             nnum = LNET_NETNUM(net);
490         __u32             lnd  = LNET_NETTYP(net);
491         struct netstrfns *nf;
492
493         if (nid == LNET_NID_ANY) {
494                 strncpy(buf, "<?>", buf_size);
495                 buf[buf_size - 1] = '\0';
496                 return buf;
497         }
498
499         nf = libcfs_lnd2netstrfns(lnd);
500         if (nf == NULL) {
501                 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
502         } else {
503                 size_t addr_len;
504
505                 nf->nf_addr2str(addr, buf, buf_size);
506                 addr_len = strlen(buf);
507                 if (nnum == 0)
508                         snprintf(buf + addr_len, buf_size - addr_len, "@%s",
509                                  nf->nf_name);
510                 else
511                         snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
512                                  nf->nf_name, nnum);
513         }
514
515         return buf;
516 }
517
518 static struct netstrfns *
519 libcfs_str2net_internal(const char *str, __u32 *net)
520 {
521         struct netstrfns *nf = NULL;
522         int               nob;
523         unsigned int      netnum;
524         int               i;
525
526         for (i = 0; i < libcfs_nnetstrfns; i++) {
527                 nf = &libcfs_netstrfns[i];
528                 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
529                         break;
530         }
531
532         if (i == libcfs_nnetstrfns)
533                 return NULL;
534
535         nob = strlen(nf->nf_name);
536
537         if (strlen(str) == (unsigned int)nob) {
538                 netnum = 0;
539         } else {
540                 if (nf->nf_type == LOLND) /* net number not allowed */
541                         return NULL;
542
543                 str += nob;
544                 i = strlen(str);
545                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
546                     i != (int)strlen(str))
547                         return NULL;
548         }
549
550         *net = LNET_MKNET(nf->nf_type, netnum);
551         return nf;
552 }
553
554 __u32
555 libcfs_str2net(const char *str)
556 {
557         __u32  net;
558
559         if (libcfs_str2net_internal(str, &net) != NULL)
560                 return net;
561
562         return LNET_NIDNET(LNET_NID_ANY);
563 }
564
565 lnet_nid_t
566 libcfs_str2nid(const char *str)
567 {
568         const char       *sep = strchr(str, '@');
569         struct netstrfns *nf;
570         __u32             net;
571         __u32             addr;
572
573         if (sep != NULL) {
574                 nf = libcfs_str2net_internal(sep + 1, &net);
575                 if (nf == NULL)
576                         return LNET_NID_ANY;
577         } else {
578                 sep = str + strlen(str);
579                 net = LNET_MKNET(SOCKLND, 0);
580                 nf = libcfs_lnd2netstrfns(SOCKLND);
581                 assert(nf != NULL);
582         }
583
584         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
585                 return LNET_NID_ANY;
586
587         return LNET_MKNID(net, addr);
588 }
589
590 char *
591 libcfs_id2str(struct lnet_process_id id)
592 {
593         char *str = libcfs_next_nidstring();
594
595         if (id.pid == LNET_PID_ANY) {
596                 snprintf(str, LNET_NIDSTR_SIZE,
597                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
598                 return str;
599         }
600
601         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
602                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
603                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
604         return str;
605 }
606
607 int
608 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
609 {
610         if (!strcmp(str, "*")) {
611                 *nidp = LNET_NID_ANY;
612                 return 1;
613         }
614
615         *nidp = libcfs_str2nid(str);
616         return *nidp != LNET_NID_ANY;
617 }
618
619 /**
620  * Nid range list syntax.
621  * \verbatim
622  *
623  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
624  * <nidrange>        :== <addrrange> '@' <net>
625  * <addrrange>       :== '*' |
626  *                       <ipaddr_range> |
627  *                       <cfs_expr_list>
628  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
629  *                       <cfs_expr_list>
630  * <cfs_expr_list>   :== <number> |
631  *                       <expr_list>
632  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
633  * <range_expr>      :== <number> |
634  *                       <number> '-' <number> |
635  *                       <number> '-' <number> '/' <number>
636  * <net>             :== <netname> | <netname><number>
637  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
638  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
639  * \endverbatim
640  */
641
642 /**
643  * Structure to represent \<nidrange\> token of the syntax.
644  *
645  * One of this is created for each \<net\> parsed.
646  */
647 struct nidrange {
648         /**
649          * Link to list of this structures which is built on nid range
650          * list parsing.
651          */
652         struct list_head nr_link;
653         /**
654          * List head for addrrange::ar_link.
655          */
656         struct list_head nr_addrranges;
657         /**
658          * Flag indicating that *@<net> is found.
659          */
660         int nr_all;
661         /**
662          * Pointer to corresponding element of libcfs_netstrfns.
663          */
664         struct netstrfns *nr_netstrfns;
665         /**
666          * Number of network. E.g. 5 if \<net\> is "elan5".
667          */
668         int nr_netnum;
669 };
670
671 /**
672  * Structure to represent \<addrrange\> token of the syntax.
673  */
674 struct addrrange {
675         /**
676          * Link to nidrange::nr_addrranges.
677          */
678         struct list_head ar_link;
679         /**
680          * List head for cfs_expr_list::el_list.
681          */
682         struct list_head ar_numaddr_ranges;
683 };
684
685 /**
686  * Parses \<addrrange\> token on the syntax.
687  *
688  * Allocates struct addrrange and links to \a nidrange via
689  * (nidrange::nr_addrranges)
690  *
691  * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
692  * \retval -errno otherwise
693  */
694 static int
695 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
696 {
697         struct addrrange *addrrange;
698
699         if (src->ls_len == 1 && src->ls_str[0] == '*') {
700                 nidrange->nr_all = 1;
701                 return 0;
702         }
703
704         addrrange = calloc(1, sizeof(struct addrrange));
705         if (addrrange == NULL)
706                 return -ENOMEM;
707         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
708         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
709
710         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
711                                                 src->ls_len,
712                                                 &addrrange->ar_numaddr_ranges);
713 }
714
715 /**
716  * Finds or creates struct nidrange.
717  *
718  * Checks if \a src is a valid network name, looks for corresponding
719  * nidrange on the ist of nidranges (\a nidlist), creates new struct
720  * nidrange if it is not found.
721  *
722  * \retval pointer to struct nidrange matching network specified via \a src
723  * \retval NULL if \a src does not match any network
724  */
725 static struct nidrange *
726 add_nidrange(const struct cfs_lstr *src,
727              struct list_head *nidlist)
728 {
729         struct netstrfns *nf;
730         struct nidrange *nr;
731         int endlen;
732         unsigned netnum;
733
734         if (src->ls_len >= LNET_NIDSTR_SIZE)
735                 return NULL;
736
737         nf = libcfs_namenum2netstrfns(src->ls_str);
738         if (nf == NULL)
739                 return NULL;
740         endlen = src->ls_len - strlen(nf->nf_name);
741         if (endlen == 0)
742                 /* network name only, e.g. "elan" or "tcp" */
743                 netnum = 0;
744         else {
745                 /* e.g. "elan25" or "tcp23", refuse to parse if
746                  * network name is not appended with decimal or
747                  * hexadecimal number */
748                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
749                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
750                         return NULL;
751         }
752
753         list_for_each_entry(nr, nidlist, nr_link) {
754                 if (nr->nr_netstrfns != nf)
755                         continue;
756                 if (nr->nr_netnum != netnum)
757                         continue;
758                 return nr;
759         }
760
761         nr = calloc(1, sizeof(struct nidrange));
762         if (nr == NULL)
763                 return NULL;
764         list_add_tail(&nr->nr_link, nidlist);
765         INIT_LIST_HEAD(&nr->nr_addrranges);
766         nr->nr_netstrfns = nf;
767         nr->nr_all = 0;
768         nr->nr_netnum = netnum;
769
770         return nr;
771 }
772
773 /**
774  * Parses \<nidrange\> token of the syntax.
775  *
776  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
777  * \retval 0 otherwise
778  */
779 static int
780 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
781 {
782         struct cfs_lstr addrrange;
783         struct cfs_lstr net;
784         struct cfs_lstr tmp;
785         struct nidrange *nr;
786
787         tmp = *src;
788         if (cfs_gettok(src, '@', &addrrange) == 0)
789                 goto failed;
790
791         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
792                 goto failed;
793
794         nr = add_nidrange(&net, nidlist);
795         if (nr == NULL)
796                 goto failed;
797
798         if (parse_addrange(&addrrange, nr) != 0)
799                 goto failed;
800
801         return 1;
802  failed:
803         fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
804                 tmp.ls_len, tmp.ls_str);
805         return 0;
806 }
807
808 /**
809  * Frees addrrange structures of \a list.
810  *
811  * For each struct addrrange structure found on \a list it frees
812  * cfs_expr_list list attached to it and frees the addrrange itself.
813  *
814  * \retval none
815  */
816 static void
817 free_addrranges(struct list_head *list)
818 {
819         while (!list_empty(list)) {
820                 struct addrrange *ar;
821
822                 ar = list_entry(list->next, struct addrrange, ar_link);
823
824                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
825                 list_del(&ar->ar_link);
826                 free(ar);
827         }
828 }
829
830 /**
831  * Frees nidrange strutures of \a list.
832  *
833  * For each struct nidrange structure found on \a list it frees
834  * addrrange list attached to it and frees the nidrange itself.
835  *
836  * \retval none
837  */
838 void
839 cfs_free_nidlist(struct list_head *list)
840 {
841         struct list_head *pos, *next;
842         struct nidrange *nr;
843
844         list_for_each_safe(pos, next, list) {
845                 nr = list_entry(pos, struct nidrange, nr_link);
846                 free_addrranges(&nr->nr_addrranges);
847                 list_del(pos);
848                 free(nr);
849         }
850 }
851
852 /**
853  * Parses nid range list.
854  *
855  * Parses with rigorous syntax and overflow checking \a str into
856  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
857  * structures and links that structure to \a nidlist. The resulting
858  * list can be used to match a NID againts set of NIDS defined by \a
859  * str.
860  * \see cfs_match_nid
861  *
862  * \retval 1 on success
863  * \retval 0 otherwise
864  */
865 int
866 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
867 {
868         struct cfs_lstr src;
869         struct cfs_lstr res;
870         int rc;
871
872         src.ls_str = str;
873         src.ls_len = len;
874         INIT_LIST_HEAD(nidlist);
875         while (src.ls_str) {
876                 rc = cfs_gettok(&src, ' ', &res);
877                 if (rc == 0) {
878                         cfs_free_nidlist(nidlist);
879                         return 0;
880                 }
881                 rc = parse_nidrange(&res, nidlist);
882                 if (rc == 0) {
883                         cfs_free_nidlist(nidlist);
884                         return 0;
885                 }
886         }
887         return 1;
888 }
889
890 /**
891  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
892  *
893  * \see cfs_parse_nidlist()
894  *
895  * \retval 1 on match
896  * \retval 0  otherwises
897  */
898 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
899 {
900         struct nidrange *nr;
901         struct addrrange *ar;
902
903         list_for_each_entry(nr, nidlist, nr_link) {
904                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
905                         continue;
906                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
907                         continue;
908                 if (nr->nr_all)
909                         return 1;
910                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
911                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
912                                                         &ar->ar_numaddr_ranges))
913                                 return 1;
914         }
915         return 0;
916 }
917
918 /**
919  * Print the network part of the nidrange \a nr into the specified \a buffer.
920  *
921  * \retval number of characters written
922  */
923 static int
924 cfs_print_network(char *buffer, int count, struct nidrange *nr)
925 {
926         struct netstrfns *nf = nr->nr_netstrfns;
927
928         if (nr->nr_netnum == 0)
929                 return snprintf(buffer, count, "@%s", nf->nf_name);
930         else
931                 return snprintf(buffer, count, "@%s%u",
932                                     nf->nf_name, nr->nr_netnum);
933 }
934
935
936 /**
937  * Print a list of addrrange (\a addrranges) into the specified \a buffer.
938  * At max \a count characters can be printed into \a buffer.
939  *
940  * \retval number of characters written
941  */
942 static int
943 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
944                      struct nidrange *nr)
945 {
946         int i = 0;
947         struct addrrange *ar;
948         struct netstrfns *nf = nr->nr_netstrfns;
949
950         list_for_each_entry(ar, addrranges, ar_link) {
951                 if (i != 0)
952                         i += snprintf(buffer + i, count - i, " ");
953                 i += nf->nf_print_addrlist(buffer + i, count - i,
954                                            &ar->ar_numaddr_ranges);
955                 i += cfs_print_network(buffer + i, count - i, nr);
956         }
957         return i;
958 }
959
960 /**
961  * Print a list of nidranges (\a nidlist) into the specified \a buffer.
962  * At max \a count characters can be printed into \a buffer.
963  * Nidranges are separated by a space character.
964  *
965  * \retval number of characters written
966  */
967 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
968 {
969         int i = 0;
970         struct nidrange *nr;
971
972         if (count <= 0)
973                 return 0;
974
975         list_for_each_entry(nr, nidlist, nr_link) {
976                 if (i != 0)
977                         i += snprintf(buffer + i, count - i, " ");
978
979                 if (nr->nr_all != 0) {
980                         assert(list_empty(&nr->nr_addrranges));
981                         i += snprintf(buffer + i, count - i, "*");
982                         i += cfs_print_network(buffer + i, count - i, nr);
983                 } else {
984                         i += cfs_print_addrranges(buffer + i, count - i,
985                                                   &nr->nr_addrranges, nr);
986                 }
987         }
988         return i;
989 }
990
991 /**
992  * Determines minimum and maximum addresses for a single
993  * numeric address range
994  *
995  * \param       ar
996  * \param       min_nid
997  * \param       max_nid
998  */
999 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1000                               __u32 *max_nid)
1001 {
1002         struct cfs_expr_list    *el;
1003         struct cfs_range_expr   *re;
1004         __u32                   tmp_ip_addr = 0;
1005         unsigned int            min_ip[4] = {0};
1006         unsigned int            max_ip[4] = {0};
1007         int                     re_count = 0;
1008
1009         list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1010                 list_for_each_entry(re, &el->el_exprs, re_link) {
1011                         min_ip[re_count] = re->re_lo;
1012                         max_ip[re_count] = re->re_hi;
1013                         re_count++;
1014                 }
1015         }
1016
1017         tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1018                        (min_ip[2] << 8) | min_ip[3]);
1019
1020         if (min_nid != NULL)
1021                 *min_nid = tmp_ip_addr;
1022
1023         tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1024                        (max_ip[2] << 8) | max_ip[3]);
1025
1026         if (max_nid != NULL)
1027                 *max_nid = tmp_ip_addr;
1028 }
1029
1030 /**
1031  * Determines minimum and maximum addresses for a single
1032  * numeric address range
1033  *
1034  * \param       ar
1035  * \param       min_nid
1036  * \param       max_nid
1037  */
1038 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1039                                __u32 *max_nid)
1040 {
1041         struct cfs_expr_list    *el;
1042         struct cfs_range_expr   *re;
1043         unsigned int            min_addr = 0;
1044         unsigned int            max_addr = 0;
1045
1046         list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1047                 list_for_each_entry(re, &el->el_exprs, re_link) {
1048                         if (re->re_lo < min_addr || min_addr == 0)
1049                                 min_addr = re->re_lo;
1050                         if (re->re_hi > max_addr)
1051                                 max_addr = re->re_hi;
1052                 }
1053         }
1054
1055         if (min_nid != NULL)
1056                 *min_nid = min_addr;
1057         if (max_nid != NULL)
1058                 *max_nid = max_addr;
1059 }
1060
1061 /**
1062  * Determines whether an expression list in an nidrange contains exactly
1063  * one contiguous address range. Calls the correct netstrfns for the LND
1064  *
1065  * \param       *nidlist
1066  *
1067  * \retval      true if contiguous
1068  * \retval      false if not contiguous
1069  */
1070 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1071 {
1072         struct nidrange         *nr;
1073         struct netstrfns        *nf = NULL;
1074         char                    *lndname = NULL;
1075         int                     netnum = -1;
1076
1077         list_for_each_entry(nr, nidlist, nr_link) {
1078                 nf = nr->nr_netstrfns;
1079                 if (lndname == NULL)
1080                         lndname = nf->nf_name;
1081                 if (netnum == -1)
1082                         netnum = nr->nr_netnum;
1083
1084                 if (strcmp(lndname, nf->nf_name) != 0 ||
1085                     netnum != nr->nr_netnum)
1086                         return false;
1087         }
1088
1089         if (nf == NULL)
1090                 return false;
1091
1092         if (!nf->nf_is_contiguous(nidlist))
1093                 return false;
1094
1095         return true;
1096 }
1097
1098 /**
1099  * Determines whether an expression list in an num nidrange contains exactly
1100  * one contiguous address range.
1101  *
1102  * \param       *nidlist
1103  *
1104  * \retval      true if contiguous
1105  * \retval      false if not contiguous
1106  */
1107 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1108 {
1109         struct nidrange         *nr;
1110         struct addrrange        *ar;
1111         struct cfs_expr_list    *el;
1112         struct cfs_range_expr   *re;
1113         int                     last_hi = 0;
1114         __u32                   last_end_nid = 0;
1115         __u32                   current_start_nid = 0;
1116         __u32                   current_end_nid = 0;
1117
1118         list_for_each_entry(nr, nidlist, nr_link) {
1119                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1120                         cfs_num_ar_min_max(ar, &current_start_nid,
1121                                            &current_end_nid);
1122                         if (last_end_nid != 0 &&
1123                             (current_start_nid - last_end_nid != 1))
1124                                         return false;
1125                         last_end_nid = current_end_nid;
1126                         list_for_each_entry(el, &ar->ar_numaddr_ranges,
1127                                             el_link) {
1128                                 list_for_each_entry(re, &el->el_exprs,
1129                                                     re_link) {
1130                                         if (re->re_stride > 1)
1131                                                 return false;
1132                                         else if (last_hi != 0 &&
1133                                                  re->re_hi - last_hi != 1)
1134                                                 return false;
1135                                         last_hi = re->re_hi;
1136                                 }
1137                         }
1138                 }
1139         }
1140
1141         return true;
1142 }
1143
1144 /**
1145  * Determines whether an expression list in an ip nidrange contains exactly
1146  * one contiguous address range.
1147  *
1148  * \param       *nidlist
1149  *
1150  * \retval      true if contiguous
1151  * \retval      false if not contiguous
1152  */
1153 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1154 {
1155         struct nidrange         *nr;
1156         struct addrrange        *ar;
1157         struct cfs_expr_list    *el;
1158         struct cfs_range_expr   *re;
1159         int                     expr_count;
1160         int                     last_hi = 255;
1161         int                     last_diff = 0;
1162         __u32                   last_end_nid = 0;
1163         __u32                   current_start_nid = 0;
1164         __u32                   current_end_nid = 0;
1165
1166         list_for_each_entry(nr, nidlist, nr_link) {
1167                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1168                         last_hi = 255;
1169                         last_diff = 0;
1170                         cfs_ip_ar_min_max(ar, &current_start_nid,
1171                                           &current_end_nid);
1172                         if (last_end_nid != 0 &&
1173                             (current_start_nid - last_end_nid != 1))
1174                                         return false;
1175                         last_end_nid = current_end_nid;
1176                         list_for_each_entry(el,
1177                                             &ar->ar_numaddr_ranges,
1178                                             el_link) {
1179                                 expr_count = 0;
1180                                 list_for_each_entry(re, &el->el_exprs,
1181                                                     re_link) {
1182                                         expr_count++;
1183                                         if (re->re_stride > 1 ||
1184                                             (last_diff > 0 && last_hi != 255) ||
1185                                             (last_diff > 0 && last_hi == 255 &&
1186                                              re->re_lo > 0))
1187                                                 return false;
1188                                         last_hi = re->re_hi;
1189                                         last_diff = re->re_hi - re->re_lo;
1190                                 }
1191                         }
1192                 }
1193         }
1194
1195         return true;
1196 }
1197
1198 /**
1199  * Takes a linked list of nidrange expressions, determines the minimum
1200  * and maximum nid and creates appropriate nid structures
1201  *
1202  * \param       *nidlist
1203  * \param       *min_nid
1204  * \param       *max_nid
1205  */
1206 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1207                                char *max_nid, size_t nidstr_length)
1208 {
1209         struct nidrange         *nr;
1210         struct netstrfns        *nf = NULL;
1211         int                     netnum = -1;
1212         __u32                   min_addr;
1213         __u32                   max_addr;
1214         char                    *lndname = NULL;
1215         char                    min_addr_str[IPSTRING_LENGTH];
1216         char                    max_addr_str[IPSTRING_LENGTH];
1217
1218         list_for_each_entry(nr, nidlist, nr_link) {
1219                 nf = nr->nr_netstrfns;
1220                 lndname = nf->nf_name;
1221                 if (netnum == -1)
1222                         netnum = nr->nr_netnum;
1223
1224                 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1225         }
1226         nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1227         nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1228
1229         snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1230                  netnum);
1231         snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1232                  netnum);
1233 }
1234
1235 /**
1236  * Determines the min and max NID values for num LNDs
1237  *
1238  * \param       *nidlist
1239  * \param       *min_nid
1240  * \param       *max_nid
1241  */
1242 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1243                             __u32 *max_nid)
1244 {
1245         struct nidrange         *nr;
1246         struct addrrange        *ar;
1247         unsigned int            tmp_min_addr = 0;
1248         unsigned int            tmp_max_addr = 0;
1249         unsigned int            min_addr = 0;
1250         unsigned int            max_addr = 0;
1251
1252         list_for_each_entry(nr, nidlist, nr_link) {
1253                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1254                         cfs_num_ar_min_max(ar, &tmp_min_addr,
1255                                            &tmp_max_addr);
1256                         if (tmp_min_addr < min_addr || min_addr == 0)
1257                                 min_addr = tmp_min_addr;
1258                         if (tmp_max_addr > max_addr)
1259                                 max_addr = tmp_min_addr;
1260                 }
1261         }
1262         *max_nid = max_addr;
1263         *min_nid = min_addr;
1264 }
1265
1266 /**
1267  * Takes an nidlist and determines the minimum and maximum
1268  * ip addresses.
1269  *
1270  * \param       *nidlist
1271  * \param       *min_nid
1272  * \param       *max_nid
1273  */
1274 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1275                            __u32 *max_nid)
1276 {
1277         struct nidrange         *nr;
1278         struct addrrange        *ar;
1279         __u32                   tmp_min_ip_addr = 0;
1280         __u32                   tmp_max_ip_addr = 0;
1281         __u32                   min_ip_addr = 0;
1282         __u32                   max_ip_addr = 0;
1283
1284         list_for_each_entry(nr, nidlist, nr_link) {
1285                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1286                         cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1287                                           &tmp_max_ip_addr);
1288                         if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1289                                 min_ip_addr = tmp_min_ip_addr;
1290                         if (tmp_max_ip_addr > max_ip_addr)
1291                                 max_ip_addr = tmp_max_ip_addr;
1292                 }
1293         }
1294
1295         if (min_nid != NULL)
1296                 *min_nid = min_ip_addr;
1297         if (max_nid != NULL)
1298                 *max_nid = max_ip_addr;
1299 }