Whamcloud - gitweb
LU-8913 nodemap: fix nodemap range format '*@<net>' support
[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, 2017, 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 int cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
307 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
308
309 static struct netstrfns libcfs_netstrfns[] = {
310         {
311                 .nf_type                = LOLND,
312                 .nf_name                = "lo",
313                 .nf_modname             = "klolnd",
314                 .nf_addr2str            = libcfs_decnum_addr2str,
315                 .nf_str2addr            = libcfs_lo_str2addr,
316                 .nf_parse_addrlist      = libcfs_num_parse,
317                 .nf_print_addrlist      = libcfs_num_addr_range_print,
318                 .nf_match_addr          = libcfs_num_match,
319                 .nf_min_max             = cfs_num_min_max
320         },
321         {
322                 .nf_type                = SOCKLND,
323                 .nf_name                = "tcp",
324                 .nf_modname             = "ksocklnd",
325                 .nf_addr2str            = libcfs_ip_addr2str,
326                 .nf_str2addr            = libcfs_ip_str2addr,
327                 .nf_parse_addrlist      = cfs_ip_addr_parse,
328                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
329                 .nf_match_addr          = cfs_ip_addr_match,
330                 .nf_min_max             = cfs_ip_min_max
331         },
332         {
333                 .nf_type                = O2IBLND,
334                 .nf_name                = "o2ib",
335                 .nf_modname             = "ko2iblnd",
336                 .nf_addr2str            = libcfs_ip_addr2str,
337                 .nf_str2addr            = libcfs_ip_str2addr,
338                 .nf_parse_addrlist      = cfs_ip_addr_parse,
339                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
340                 .nf_match_addr          = cfs_ip_addr_match,
341                 .nf_min_max             = cfs_ip_min_max
342         },
343         {
344                 .nf_type                = GNILND,
345                 .nf_name                = "gni",
346                 .nf_modname             = "kgnilnd",
347                 .nf_addr2str            = libcfs_decnum_addr2str,
348                 .nf_str2addr            = libcfs_num_str2addr,
349                 .nf_parse_addrlist      = libcfs_num_parse,
350                 .nf_print_addrlist      = libcfs_num_addr_range_print,
351                 .nf_match_addr          = libcfs_num_match,
352                 .nf_min_max             = cfs_num_min_max
353         },
354         {
355                 .nf_type                = GNIIPLND,
356                 .nf_name                = "gip",
357                 .nf_modname             = "kgnilnd",
358                 .nf_addr2str            = libcfs_ip_addr2str,
359                 .nf_str2addr            = libcfs_ip_str2addr,
360                 .nf_parse_addrlist      = cfs_ip_addr_parse,
361                 .nf_print_addrlist      = libcfs_ip_addr_range_print,
362                 .nf_match_addr          = cfs_ip_addr_match,
363                 .nf_min_max             = cfs_ip_min_max
364         },
365         {
366                 .nf_type                = PTL4LND,
367                 .nf_name                = "ptlf",
368                 .nf_modname             = "kptl4lnd",
369                 .nf_addr2str            = libcfs_decnum_addr2str,
370                 .nf_str2addr            = libcfs_num_str2addr,
371                 .nf_parse_addrlist      = libcfs_num_parse,
372                 .nf_print_addrlist      = libcfs_num_addr_range_print,
373                 .nf_match_addr          = libcfs_num_match,
374                 .nf_min_max             = cfs_num_min_max
375         }
376 };
377
378 static const size_t libcfs_nnetstrfns =
379         sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
380
381 static struct netstrfns *
382 libcfs_lnd2netstrfns(__u32 lnd)
383 {
384         int     i;
385
386         for (i = 0; i < libcfs_nnetstrfns; i++)
387                 if (lnd == libcfs_netstrfns[i].nf_type)
388                         return &libcfs_netstrfns[i];
389
390         return NULL;
391 }
392
393 static struct netstrfns *
394 libcfs_namenum2netstrfns(const char *name)
395 {
396         struct netstrfns *nf;
397         int               i;
398
399         for (i = 0; i < libcfs_nnetstrfns; i++) {
400                 nf = &libcfs_netstrfns[i];
401                 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
402                         return nf;
403         }
404         return NULL;
405 }
406
407 static struct netstrfns *
408 libcfs_name2netstrfns(const char *name)
409 {
410         int    i;
411
412         for (i = 0; i < libcfs_nnetstrfns; i++)
413                 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
414                         return &libcfs_netstrfns[i];
415
416         return NULL;
417 }
418
419 int
420 libcfs_isknown_lnd(__u32 lnd)
421 {
422         return libcfs_lnd2netstrfns(lnd) != NULL;
423 }
424
425 char *
426 libcfs_lnd2modname(__u32 lnd)
427 {
428         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
429
430         return (nf == NULL) ? NULL : nf->nf_modname;
431 }
432
433 int
434 libcfs_str2lnd(const char *str)
435 {
436         struct netstrfns *nf = libcfs_name2netstrfns(str);
437
438         if (nf != NULL)
439                 return nf->nf_type;
440
441         return -1;
442 }
443
444 char *
445 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
446 {
447         struct netstrfns *nf;
448
449         nf = libcfs_lnd2netstrfns(lnd);
450         if (nf == NULL)
451                 snprintf(buf, buf_size, "?%u?", lnd);
452         else
453                 snprintf(buf, buf_size, "%s", nf->nf_name);
454
455         return buf;
456 }
457
458 char *
459 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
460 {
461         __u32             nnum = LNET_NETNUM(net);
462         __u32             lnd  = LNET_NETTYP(net);
463         struct netstrfns *nf;
464
465         nf = libcfs_lnd2netstrfns(lnd);
466         if (nf == NULL)
467                 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
468         else if (nnum == 0)
469                 snprintf(buf, buf_size, "%s", nf->nf_name);
470         else
471                 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
472
473         return buf;
474 }
475
476 char *
477 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
478 {
479         __u32             addr = LNET_NIDADDR(nid);
480         __u32             net  = LNET_NIDNET(nid);
481         __u32             nnum = LNET_NETNUM(net);
482         __u32             lnd  = LNET_NETTYP(net);
483         struct netstrfns *nf;
484
485         if (nid == LNET_NID_ANY) {
486                 strncpy(buf, "<?>", buf_size);
487                 buf[buf_size - 1] = '\0';
488                 return buf;
489         }
490
491         nf = libcfs_lnd2netstrfns(lnd);
492         if (nf == NULL) {
493                 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
494         } else {
495                 size_t addr_len;
496
497                 nf->nf_addr2str(addr, buf, buf_size);
498                 addr_len = strlen(buf);
499                 if (nnum == 0)
500                         snprintf(buf + addr_len, buf_size - addr_len, "@%s",
501                                  nf->nf_name);
502                 else
503                         snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
504                                  nf->nf_name, nnum);
505         }
506
507         return buf;
508 }
509
510 static struct netstrfns *
511 libcfs_str2net_internal(const char *str, __u32 *net)
512 {
513         struct netstrfns *nf = NULL;
514         int               nob;
515         unsigned int      netnum;
516         int               i;
517
518         for (i = 0; i < libcfs_nnetstrfns; i++) {
519                 nf = &libcfs_netstrfns[i];
520                 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
521                         break;
522         }
523
524         if (i == libcfs_nnetstrfns)
525                 return NULL;
526
527         nob = strlen(nf->nf_name);
528
529         if (strlen(str) == (unsigned int)nob) {
530                 netnum = 0;
531         } else {
532                 if (nf->nf_type == LOLND) /* net number not allowed */
533                         return NULL;
534
535                 str += nob;
536                 i = strlen(str);
537                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
538                     i != (int)strlen(str))
539                         return NULL;
540         }
541
542         *net = LNET_MKNET(nf->nf_type, netnum);
543         return nf;
544 }
545
546 __u32
547 libcfs_str2net(const char *str)
548 {
549         __u32  net;
550
551         if (libcfs_str2net_internal(str, &net) != NULL)
552                 return net;
553
554         return LNET_NIDNET(LNET_NID_ANY);
555 }
556
557 lnet_nid_t
558 libcfs_str2nid(const char *str)
559 {
560         const char       *sep = strchr(str, '@');
561         struct netstrfns *nf;
562         __u32             net;
563         __u32             addr;
564
565         if (sep != NULL) {
566                 nf = libcfs_str2net_internal(sep + 1, &net);
567                 if (nf == NULL)
568                         return LNET_NID_ANY;
569         } else {
570                 sep = str + strlen(str);
571                 net = LNET_MKNET(SOCKLND, 0);
572                 nf = libcfs_lnd2netstrfns(SOCKLND);
573                 assert(nf != NULL);
574         }
575
576         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
577                 return LNET_NID_ANY;
578
579         return LNET_MKNID(net, addr);
580 }
581
582 char *
583 libcfs_id2str(struct lnet_process_id id)
584 {
585         char *str = libcfs_next_nidstring();
586
587         if (id.pid == LNET_PID_ANY) {
588                 snprintf(str, LNET_NIDSTR_SIZE,
589                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
590                 return str;
591         }
592
593         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
594                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
595                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
596         return str;
597 }
598
599 int
600 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
601 {
602         if (!strcmp(str, "*")) {
603                 *nidp = LNET_NID_ANY;
604                 return 1;
605         }
606
607         *nidp = libcfs_str2nid(str);
608         return *nidp != LNET_NID_ANY;
609 }
610
611 /**
612  * Nid range list syntax.
613  * \verbatim
614  *
615  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
616  * <nidrange>        :== <addrrange> '@' <net>
617  * <addrrange>       :== '*' |
618  *                       <ipaddr_range> |
619  *                       <cfs_expr_list>
620  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
621  *                       <cfs_expr_list>
622  * <cfs_expr_list>   :== <number> |
623  *                       <expr_list>
624  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
625  * <range_expr>      :== <number> |
626  *                       <number> '-' <number> |
627  *                       <number> '-' <number> '/' <number>
628  * <net>             :== <netname> | <netname><number>
629  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
630  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
631  * \endverbatim
632  */
633
634 /**
635  * Structure to represent \<nidrange\> token of the syntax.
636  *
637  * One of this is created for each \<net\> parsed.
638  */
639 struct nidrange {
640         /**
641          * Link to list of this structures which is built on nid range
642          * list parsing.
643          */
644         struct list_head nr_link;
645         /**
646          * List head for addrrange::ar_link.
647          */
648         struct list_head nr_addrranges;
649         /**
650          * Flag indicating that *@<net> is found.
651          */
652         int nr_all;
653         /**
654          * Pointer to corresponding element of libcfs_netstrfns.
655          */
656         struct netstrfns *nr_netstrfns;
657         /**
658          * Number of network. E.g. 5 if \<net\> is "elan5".
659          */
660         int nr_netnum;
661 };
662
663 /**
664  * Structure to represent \<addrrange\> token of the syntax.
665  */
666 struct addrrange {
667         /**
668          * Link to nidrange::nr_addrranges.
669          */
670         struct list_head ar_link;
671         /**
672          * List head for cfs_expr_list::el_list.
673          */
674         struct list_head ar_numaddr_ranges;
675 };
676
677 /**
678  * Parses \<addrrange\> token on the syntax.
679  *
680  * Allocates struct addrrange and links to \a nidrange via
681  * (nidrange::nr_addrranges)
682  *
683  * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
684  * \retval -errno otherwise
685  */
686 static int
687 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
688 {
689         struct addrrange *addrrange;
690
691         if (src->ls_len == 1 && src->ls_str[0] == '*') {
692                 nidrange->nr_all = 1;
693                 return 0;
694         }
695
696         addrrange = calloc(1, sizeof(struct addrrange));
697         if (addrrange == NULL)
698                 return -ENOMEM;
699         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
700         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
701
702         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
703                                                 src->ls_len,
704                                                 &addrrange->ar_numaddr_ranges);
705 }
706
707 /**
708  * Finds or creates struct nidrange.
709  *
710  * Checks if \a src is a valid network name, looks for corresponding
711  * nidrange on the ist of nidranges (\a nidlist), creates new struct
712  * nidrange if it is not found.
713  *
714  * \retval pointer to struct nidrange matching network specified via \a src
715  * \retval NULL if \a src does not match any network
716  */
717 static struct nidrange *
718 add_nidrange(const struct cfs_lstr *src,
719              struct list_head *nidlist)
720 {
721         struct netstrfns *nf;
722         struct nidrange *nr;
723         int endlen;
724         unsigned netnum;
725
726         if (src->ls_len >= LNET_NIDSTR_SIZE)
727                 return NULL;
728
729         nf = libcfs_namenum2netstrfns(src->ls_str);
730         if (nf == NULL)
731                 return NULL;
732         endlen = src->ls_len - strlen(nf->nf_name);
733         if (endlen == 0)
734                 /* network name only, e.g. "elan" or "tcp" */
735                 netnum = 0;
736         else {
737                 /* e.g. "elan25" or "tcp23", refuse to parse if
738                  * network name is not appended with decimal or
739                  * hexadecimal number */
740                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
741                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
742                         return NULL;
743         }
744
745         list_for_each_entry(nr, nidlist, nr_link) {
746                 if (nr->nr_netstrfns != nf)
747                         continue;
748                 if (nr->nr_netnum != netnum)
749                         continue;
750                 return nr;
751         }
752
753         nr = calloc(1, sizeof(struct nidrange));
754         if (nr == NULL)
755                 return NULL;
756         list_add_tail(&nr->nr_link, nidlist);
757         INIT_LIST_HEAD(&nr->nr_addrranges);
758         nr->nr_netstrfns = nf;
759         nr->nr_all = 0;
760         nr->nr_netnum = netnum;
761
762         return nr;
763 }
764
765 /**
766  * Parses \<nidrange\> token of the syntax.
767  *
768  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
769  * \retval 0 otherwise
770  */
771 static int
772 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
773 {
774         struct cfs_lstr addrrange;
775         struct cfs_lstr net;
776         struct cfs_lstr tmp;
777         struct nidrange *nr;
778
779         tmp = *src;
780         if (cfs_gettok(src, '@', &addrrange) == 0)
781                 goto failed;
782
783         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
784                 goto failed;
785
786         nr = add_nidrange(&net, nidlist);
787         if (nr == NULL)
788                 goto failed;
789
790         if (parse_addrange(&addrrange, nr) != 0)
791                 goto failed;
792
793         return 1;
794  failed:
795         fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
796                 tmp.ls_len, tmp.ls_str);
797         return 0;
798 }
799
800 /**
801  * Frees addrrange structures of \a list.
802  *
803  * For each struct addrrange structure found on \a list it frees
804  * cfs_expr_list list attached to it and frees the addrrange itself.
805  *
806  * \retval none
807  */
808 static void
809 free_addrranges(struct list_head *list)
810 {
811         while (!list_empty(list)) {
812                 struct addrrange *ar;
813
814                 ar = list_entry(list->next, struct addrrange, ar_link);
815
816                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
817                 list_del(&ar->ar_link);
818                 free(ar);
819         }
820 }
821
822 /**
823  * Frees nidrange strutures of \a list.
824  *
825  * For each struct nidrange structure found on \a list it frees
826  * addrrange list attached to it and frees the nidrange itself.
827  *
828  * \retval none
829  */
830 void
831 cfs_free_nidlist(struct list_head *list)
832 {
833         struct list_head *pos, *next;
834         struct nidrange *nr;
835
836         list_for_each_safe(pos, next, list) {
837                 nr = list_entry(pos, struct nidrange, nr_link);
838                 free_addrranges(&nr->nr_addrranges);
839                 list_del(pos);
840                 free(nr);
841         }
842 }
843
844 /**
845  * Parses nid range list.
846  *
847  * Parses with rigorous syntax and overflow checking \a str into
848  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
849  * structures and links that structure to \a nidlist. The resulting
850  * list can be used to match a NID againts set of NIDS defined by \a
851  * str.
852  * \see cfs_match_nid
853  *
854  * \retval 1 on success
855  * \retval 0 otherwise
856  */
857 int
858 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
859 {
860         struct cfs_lstr src;
861         struct cfs_lstr res;
862         int rc;
863
864         src.ls_str = str;
865         src.ls_len = len;
866         INIT_LIST_HEAD(nidlist);
867         while (src.ls_str) {
868                 rc = cfs_gettok(&src, ' ', &res);
869                 if (rc == 0) {
870                         cfs_free_nidlist(nidlist);
871                         return 0;
872                 }
873                 rc = parse_nidrange(&res, nidlist);
874                 if (rc == 0) {
875                         cfs_free_nidlist(nidlist);
876                         return 0;
877                 }
878         }
879         return 1;
880 }
881
882 /**
883  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
884  *
885  * \see cfs_parse_nidlist()
886  *
887  * \retval 1 on match
888  * \retval 0  otherwises
889  */
890 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
891 {
892         struct nidrange *nr;
893         struct addrrange *ar;
894
895         list_for_each_entry(nr, nidlist, nr_link) {
896                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
897                         continue;
898                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
899                         continue;
900                 if (nr->nr_all)
901                         return 1;
902                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
903                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
904                                                         &ar->ar_numaddr_ranges))
905                                 return 1;
906         }
907         return 0;
908 }
909
910 /**
911  * Print the network part of the nidrange \a nr into the specified \a buffer.
912  *
913  * \retval number of characters written
914  */
915 static int
916 cfs_print_network(char *buffer, int count, struct nidrange *nr)
917 {
918         struct netstrfns *nf = nr->nr_netstrfns;
919
920         if (nr->nr_netnum == 0)
921                 return snprintf(buffer, count, "@%s", nf->nf_name);
922         else
923                 return snprintf(buffer, count, "@%s%u",
924                                     nf->nf_name, nr->nr_netnum);
925 }
926
927
928 /**
929  * Print a list of addrrange (\a addrranges) into the specified \a buffer.
930  * At max \a count characters can be printed into \a buffer.
931  *
932  * \retval number of characters written
933  */
934 static int
935 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
936                      struct nidrange *nr)
937 {
938         int i = 0;
939         struct addrrange *ar;
940         struct netstrfns *nf = nr->nr_netstrfns;
941
942         list_for_each_entry(ar, addrranges, ar_link) {
943                 if (i != 0)
944                         i += snprintf(buffer + i, count - i, " ");
945                 i += nf->nf_print_addrlist(buffer + i, count - i,
946                                            &ar->ar_numaddr_ranges);
947                 i += cfs_print_network(buffer + i, count - i, nr);
948         }
949         return i;
950 }
951
952 /**
953  * Print a list of nidranges (\a nidlist) into the specified \a buffer.
954  * At max \a count characters can be printed into \a buffer.
955  * Nidranges are separated by a space character.
956  *
957  * \retval number of characters written
958  */
959 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
960 {
961         int i = 0;
962         struct nidrange *nr;
963
964         if (count <= 0)
965                 return 0;
966
967         list_for_each_entry(nr, nidlist, nr_link) {
968                 if (i != 0)
969                         i += snprintf(buffer + i, count - i, " ");
970
971                 if (nr->nr_all != 0) {
972                         assert(list_empty(&nr->nr_addrranges));
973                         i += snprintf(buffer + i, count - i, "*");
974                         i += cfs_print_network(buffer + i, count - i, nr);
975                 } else {
976                         i += cfs_print_addrranges(buffer + i, count - i,
977                                                   &nr->nr_addrranges, nr);
978                 }
979         }
980         return i;
981 }
982
983 /**
984  * Determines minimum and maximum addresses for a single
985  * numeric address range
986  *
987  * \param       ar
988  * \param[out]  *min_nid __u32 representation of min NID
989  * \param[out]  *max_nid __u32 representation of max NID
990  * \retval      -EINVAL unsupported LNET range
991  * \retval      -ERANGE non-contiguous LNET range
992  */
993 static int cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
994                               __u32 *max_nid)
995 {
996         struct cfs_expr_list *expr_list;
997         struct cfs_range_expr *range;
998         unsigned int min_ip[4] = {0};
999         unsigned int max_ip[4] = {0};
1000         int cur_octet = 0;
1001         bool expect_full_octet = false;
1002
1003         list_for_each_entry(expr_list, &ar->ar_numaddr_ranges, el_link) {
1004                 int re_count = 0;
1005
1006                 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
1007                         /* XXX: add support for multiple & non-contig. re's */
1008                         if (re_count > 0)
1009                                 return -EINVAL;
1010
1011                         /* if a previous octet was ranged, then all remaining
1012                          * octets must be full for contiguous range */
1013                         if (expect_full_octet && (range->re_lo != 0 ||
1014                                                   range->re_hi != 255))
1015                                 return -ERANGE;
1016
1017                         if (range->re_stride != 1)
1018                                 return -ERANGE;
1019
1020                         if (range->re_lo > range->re_hi)
1021                                 return -EINVAL;
1022
1023                         if (range->re_lo != range->re_hi)
1024                                 expect_full_octet = true;
1025
1026                         min_ip[cur_octet] = range->re_lo;
1027                         max_ip[cur_octet] = range->re_hi;
1028
1029                         re_count++;
1030                 }
1031
1032                 cur_octet++;
1033         }
1034
1035         if (min_nid != NULL)
1036                 *min_nid = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1037                             (min_ip[2] << 8) | min_ip[3]);
1038
1039         if (max_nid != NULL)
1040                 *max_nid = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1041                             (max_ip[2] << 8) | max_ip[3]);
1042
1043         return 0;
1044 }
1045
1046 /**
1047  * Determines minimum and maximum addresses for a single
1048  * numeric address range
1049  *
1050  * \param       ar
1051  * \param[out]  *min_nid __u32 representation of min NID
1052  * \param[out]  *max_nid __u32 representation of max NID
1053  * \retval      -EINVAL unsupported LNET range
1054  */
1055 static int cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1056                                __u32 *max_nid)
1057 {
1058         struct cfs_expr_list *el;
1059         struct cfs_range_expr *re;
1060         unsigned int min_addr = 0;
1061         unsigned int max_addr = 0;
1062
1063         list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1064                 int re_count = 0;
1065
1066                 list_for_each_entry(re, &el->el_exprs, re_link) {
1067                         if (re_count > 0)
1068                                 return -EINVAL;
1069                         if (re->re_lo > re->re_hi)
1070                                 return -EINVAL;
1071
1072                         if (re->re_lo < min_addr || min_addr == 0)
1073                                 min_addr = re->re_lo;
1074                         if (re->re_hi > max_addr)
1075                                 max_addr = re->re_hi;
1076
1077                         re_count++;
1078                 }
1079         }
1080
1081         if (min_nid != NULL)
1082                 *min_nid = min_addr;
1083         if (max_nid != NULL)
1084                 *max_nid = max_addr;
1085
1086         return 0;
1087 }
1088
1089 /**
1090  * Takes a linked list of nidrange expressions, determines the minimum
1091  * and maximum nid and creates appropriate nid structures
1092  *
1093  * \param       *nidlist
1094  * \param[out]  *min_nid string representation of min NID
1095  * \param[out]  *max_nid string representation of max NID
1096  * \retval      -EINVAL unsupported LNET range
1097  * \retval      -ERANGE non-contiguous LNET range
1098  */
1099 int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1100                               char *max_nid, size_t nidstr_length)
1101 {
1102         struct nidrange *first_nidrange;
1103         int netnum;
1104         struct netstrfns *nf;
1105         char *lndname;
1106         __u32 min_addr;
1107         __u32 max_addr;
1108         char min_addr_str[IPSTRING_LENGTH];
1109         char max_addr_str[IPSTRING_LENGTH];
1110         int rc;
1111
1112         first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link);
1113
1114         netnum = first_nidrange->nr_netnum;
1115         nf = first_nidrange->nr_netstrfns;
1116         lndname = nf->nf_name;
1117
1118         rc = nf->nf_min_max(nidlist, &min_addr, &max_addr);
1119         if (rc < 0)
1120                 return rc;
1121
1122         nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1123         nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1124
1125         snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1126                  netnum);
1127         snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1128                  netnum);
1129
1130         return 0;
1131 }
1132
1133 /**
1134  * Determines the min and max NID values for num LNDs
1135  *
1136  * \param       *nidlist
1137  * \param[out]  *min_nid if provided, returns string representation of min NID
1138  * \param[out]  *max_nid if provided, returns string representation of max NID
1139  * \retval      -EINVAL unsupported LNET range
1140  * \retval      -ERANGE non-contiguous LNET range
1141  */
1142 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1143                             __u32 *max_nid)
1144 {
1145         struct nidrange *nr;
1146         struct addrrange *ar;
1147         unsigned int tmp_min_addr = 0;
1148         unsigned int tmp_max_addr = 0;
1149         unsigned int min_addr = 0;
1150         unsigned int max_addr = 0;
1151         int nidlist_count = 0;
1152         int rc;
1153
1154         list_for_each_entry(nr, nidlist, nr_link) {
1155                 if (nidlist_count > 0)
1156                         return -EINVAL;
1157
1158                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1159                         rc = cfs_num_ar_min_max(ar, &tmp_min_addr,
1160                                                 &tmp_max_addr);
1161                         if (rc < 0)
1162                                 return rc;
1163
1164                         if (tmp_min_addr < min_addr || min_addr == 0)
1165                                 min_addr = tmp_min_addr;
1166                         if (tmp_max_addr > max_addr)
1167                                 max_addr = tmp_min_addr;
1168                 }
1169         }
1170         if (max_nid != NULL)
1171                 *max_nid = max_addr;
1172         if (min_nid != NULL)
1173                 *min_nid = min_addr;
1174
1175         return 0;
1176 }
1177
1178 /**
1179  * Takes an nidlist and determines the minimum and maximum
1180  * ip addresses.
1181  *
1182  * \param       *nidlist
1183  * \param[out]  *min_nid if provided, returns string representation of min NID
1184  * \param[out]  *max_nid if provided, returns string representation of max NID
1185  * \retval      -EINVAL unsupported LNET range
1186  * \retval      -ERANGE non-contiguous LNET range
1187  */
1188 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1189                            __u32 *max_nid)
1190 {
1191         struct nidrange *nr;
1192         struct addrrange *ar;
1193         __u32 tmp_min_ip_addr = 0;
1194         __u32 tmp_max_ip_addr = 0;
1195         __u32 min_ip_addr = 0;
1196         __u32 max_ip_addr = 0;
1197         int nidlist_count = 0;
1198         int rc;
1199
1200         list_for_each_entry(nr, nidlist, nr_link) {
1201                 if (nidlist_count > 0)
1202                         return -EINVAL;
1203
1204                 if (nr->nr_all) {
1205                         min_ip_addr = 0;
1206                         max_ip_addr = 0xffffffff;
1207                         break;
1208                 }
1209
1210                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1211                         rc = cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1212                                                &tmp_max_ip_addr);
1213                         if (rc < 0)
1214                                 return rc;
1215
1216                         if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1217                                 min_ip_addr = tmp_min_ip_addr;
1218                         if (tmp_max_ip_addr > max_ip_addr)
1219                                 max_ip_addr = tmp_max_ip_addr;
1220                 }
1221
1222                 nidlist_count++;
1223         }
1224
1225         if (max_nid != NULL)
1226                 *max_nid = max_ip_addr;
1227         if (min_nid != NULL)
1228                 *min_nid = min_ip_addr;
1229
1230         return 0;
1231 }