Whamcloud - gitweb
7606d73b32e011dcfe8a65fa60d67e4198f18cc8
[fs/lustre-release.git] / libcfs / libcfs / 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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/nidstrings.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44 #include <lnet/lnet.h>
45 #ifndef __KERNEL__
46 #ifdef HAVE_GETHOSTBYNAME
47 # include <netdb.h>
48 #endif
49 #endif
50
51 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
52  * consistent in all conversion functions.  Some code fragments are copied
53  * around for the sake of clarity...
54  */
55
56 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
57  * Choose the number of nidstrings to support the MAXIMUM expected number of
58  * concurrent users.  If there are more, the returned string will be volatile.
59  * NB this number must allow for a process to be descheduled for a timeslice
60  * between getting its string and using it.
61  */
62
63 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
64 static int       libcfs_nidstring_idx = 0;
65
66 #ifdef __KERNEL__
67 static spinlock_t libcfs_nidstring_lock;
68
69 void libcfs_init_nidstrings (void)
70 {
71         spin_lock_init(&libcfs_nidstring_lock);
72 }
73
74 # define NIDSTR_LOCK(f)   spin_lock_irqsave(&libcfs_nidstring_lock, f)
75 # define NIDSTR_UNLOCK(f) spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
76 #else
77 # define NIDSTR_LOCK(f)   (f=sizeof(f))  /* avoid set-but-unused warnings */
78 # define NIDSTR_UNLOCK(f) (f=sizeof(f))
79 #endif
80
81 static char *
82 libcfs_next_nidstring (void)
83 {
84         char          *str;
85         unsigned long  flags;
86
87         NIDSTR_LOCK(flags);
88
89         str = libcfs_nidstrings[libcfs_nidstring_idx++];
90         if (libcfs_nidstring_idx ==
91             sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
92                 libcfs_nidstring_idx = 0;
93
94         NIDSTR_UNLOCK(flags);
95         return str;
96 }
97
98 static int  libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
99 static void libcfs_ip_addr2str(__u32 addr, char *str);
100 static int  libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
101 static void libcfs_decnum_addr2str(__u32 addr, char *str);
102 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
103 static int  libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
104 static int  libcfs_num_parse(char *str, int len, struct list_head *list);
105 static int  libcfs_num_match(__u32 addr, struct list_head *list);
106
107 struct netstrfns {
108         int      nf_type;
109         char    *nf_name;
110         char    *nf_modname;
111         void    (*nf_addr2str)(__u32 addr, char *str);
112         int     (*nf_str2addr)(const char *str, int nob, __u32 *addr);
113         int     (*nf_parse_addrlist)(char *str, int len,
114                                         struct list_head *list);
115         int     (*nf_match_addr)(__u32 addr, struct list_head *list);
116 };
117
118 static struct netstrfns  libcfs_netstrfns[] = {
119         {/* .nf_type      */  LOLND,
120          /* .nf_name      */  "lo",
121          /* .nf_modname   */  "klolnd",
122          /* .nf_addr2str  */  libcfs_decnum_addr2str,
123          /* .nf_str2addr  */  libcfs_lo_str2addr,
124          /* .nf_parse_addr*/  libcfs_num_parse,
125          /* .nf_match_addr*/  libcfs_num_match},
126         {/* .nf_type      */  SOCKLND,
127          /* .nf_name      */  "tcp",
128          /* .nf_modname   */  "ksocklnd",
129          /* .nf_addr2str  */  libcfs_ip_addr2str,
130          /* .nf_str2addr  */  libcfs_ip_str2addr,
131          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
132          /* .nf_match_addr*/  cfs_ip_addr_match},
133         {/* .nf_type      */  O2IBLND,
134          /* .nf_name      */  "o2ib",
135          /* .nf_modname   */  "ko2iblnd",
136          /* .nf_addr2str  */  libcfs_ip_addr2str,
137          /* .nf_str2addr  */  libcfs_ip_str2addr,
138          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
139          /* .nf_match_addr*/  cfs_ip_addr_match},
140         {/* .nf_type      */  CIBLND,
141          /* .nf_name      */  "cib",
142          /* .nf_modname   */  "kciblnd",
143          /* .nf_addr2str  */  libcfs_ip_addr2str,
144          /* .nf_str2addr  */  libcfs_ip_str2addr,
145          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
146          /* .nf_match_addr*/  cfs_ip_addr_match},
147         {/* .nf_type      */  OPENIBLND,
148          /* .nf_name      */  "openib",
149          /* .nf_modname   */  "kopeniblnd",
150          /* .nf_addr2str  */  libcfs_ip_addr2str,
151          /* .nf_str2addr  */  libcfs_ip_str2addr,
152          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
153          /* .nf_match_addr*/  cfs_ip_addr_match},
154         {/* .nf_type      */  IIBLND,
155          /* .nf_name      */  "iib",
156          /* .nf_modname   */  "kiiblnd",
157          /* .nf_addr2str  */  libcfs_ip_addr2str,
158          /* .nf_str2addr  */  libcfs_ip_str2addr,
159          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
160          /* .nf_match_addr*/  cfs_ip_addr_match},
161         {/* .nf_type      */  VIBLND,
162          /* .nf_name      */  "vib",
163          /* .nf_modname   */  "kviblnd",
164          /* .nf_addr2str  */  libcfs_ip_addr2str,
165          /* .nf_str2addr  */  libcfs_ip_str2addr,
166          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
167          /* .nf_match_addr*/  cfs_ip_addr_match},
168         {/* .nf_type      */  RALND,
169          /* .nf_name      */  "ra",
170          /* .nf_modname   */  "kralnd",
171          /* .nf_addr2str  */  libcfs_ip_addr2str,
172          /* .nf_str2addr  */  libcfs_ip_str2addr,
173          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
174          /* .nf_match_addr*/  cfs_ip_addr_match},
175         {/* .nf_type      */  QSWLND,
176          /* .nf_name      */  "elan",
177          /* .nf_modname   */  "kqswlnd",
178          /* .nf_addr2str  */  libcfs_decnum_addr2str,
179          /* .nf_str2addr  */  libcfs_num_str2addr,
180          /* .nf_parse_addrlist*/  libcfs_num_parse,
181          /* .nf_match_addr*/  libcfs_num_match},
182         {/* .nf_type      */  GMLND,
183          /* .nf_name      */  "gm",
184          /* .nf_modname   */  "kgmlnd",
185          /* .nf_addr2str  */  libcfs_hexnum_addr2str,
186          /* .nf_str2addr  */  libcfs_num_str2addr,
187          /* .nf_parse_addrlist*/  libcfs_num_parse,
188          /* .nf_match_addr*/  libcfs_num_match},
189         {/* .nf_type      */  MXLND,
190          /* .nf_name      */  "mx",
191          /* .nf_modname   */  "kmxlnd",
192          /* .nf_addr2str  */  libcfs_ip_addr2str,
193          /* .nf_str2addr  */  libcfs_ip_str2addr,
194          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
195          /* .nf_match_addr*/  cfs_ip_addr_match},
196         {/* .nf_type      */  PTLLND,
197          /* .nf_name      */  "ptl",
198          /* .nf_modname   */  "kptllnd",
199          /* .nf_addr2str  */  libcfs_decnum_addr2str,
200          /* .nf_str2addr  */  libcfs_num_str2addr,
201          /* .nf_parse_addrlist*/  libcfs_num_parse,
202          /* .nf_match_addr*/  libcfs_num_match},
203         {/* .nf_type      */  GNILND,
204          /* .nf_name      */  "gni",
205          /* .nf_modname   */  "kgnilnd",
206          /* .nf_addr2str  */  libcfs_decnum_addr2str,
207          /* .nf_str2addr  */  libcfs_num_str2addr,
208          /* .nf_parse_addrlist*/  libcfs_num_parse,
209          /* .nf_match_addr*/  libcfs_num_match},
210         {/* .nf_type      */  GNIIPLND,
211          /* .nf_name      */  "gip",
212          /* .nf_modname   */  "kgnilnd",
213          /* .nf_addr2str  */  libcfs_ip_addr2str,
214          /* .nf_str2addr  */  libcfs_ip_str2addr,
215          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
216          /* .nf_match_addr*/  cfs_ip_addr_match},
217         /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
218         {/* .nf_type      */  -1},
219 };
220
221 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
222
223 int
224 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
225 {
226         *addr = 0;
227         return 1;
228 }
229
230 void
231 libcfs_ip_addr2str(__u32 addr, char *str)
232 {
233 #if 0   /* never lookup */
234 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
235         __u32           netip = htonl(addr);
236         struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
237
238         if (he != NULL) {
239                 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
240                 return;
241         }
242 #endif
243 #endif
244         snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
245                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
246                  (addr >> 8) & 0xff, addr & 0xff);
247 }
248
249 /* CAVEAT EMPTOR XscanfX
250  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
251  * sscanf may return immediately if it sees the terminating '0' in a string, so
252  * I initialise the %n variable to the expected length.  If sscanf sets it;
253  * fine, if it doesn't, then the scan ended at the end of the string, which is
254  * fine too :) */
255
256 int
257 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
258 {
259         int   a;
260         int   b;
261         int   c;
262         int   d;
263         int   n = nob;                          /* XscanfX */
264
265         /* numeric IP? */
266         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
267             n == nob &&
268             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
269             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
270                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
271                 return 1;
272         }
273
274 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
275         /* known hostname? */
276         if (('a' <= str[0] && str[0] <= 'z') ||
277             ('A' <= str[0] && str[0] <= 'Z')) {
278                 char *tmp;
279
280                 LIBCFS_ALLOC(tmp, nob + 1);
281                 if (tmp != NULL) {
282                         struct hostent *he;
283
284                         memcpy(tmp, str, nob);
285                         tmp[nob] = 0;
286
287                         he = gethostbyname(tmp);
288
289                         LIBCFS_FREE(tmp, nob);
290
291                         if (he != NULL) {
292                                 __u32 ip = *(__u32 *)he->h_addr;
293
294                                 *addr = ntohl(ip);
295                                 return 1;
296                         }
297                 }
298         }
299 #endif
300         return 0;
301 }
302
303 void
304 libcfs_decnum_addr2str(__u32 addr, char *str)
305 {
306         snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
307 }
308
309 void
310 libcfs_hexnum_addr2str(__u32 addr, char *str)
311 {
312         snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
313 }
314
315 int
316 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
317 {
318         int     n;
319
320         n = nob;
321         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
322                 return 1;
323
324         n = nob;
325         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
326                 return 1;
327
328         n = nob;
329         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
330                 return 1;
331
332         return 0;
333 }
334
335 struct netstrfns *
336 libcfs_lnd2netstrfns(int lnd)
337 {
338         int    i;
339
340         if (lnd >= 0)
341                 for (i = 0; i < libcfs_nnetstrfns; i++)
342                         if (lnd == libcfs_netstrfns[i].nf_type)
343                                 return &libcfs_netstrfns[i];
344
345         return NULL;
346 }
347
348 struct netstrfns *
349 libcfs_namenum2netstrfns(const char *name)
350 {
351         struct netstrfns *nf;
352         int               i;
353
354         for (i = 0; i < libcfs_nnetstrfns; i++) {
355                 nf = &libcfs_netstrfns[i];
356                 if (nf->nf_type >= 0 &&
357                     !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
358                         return nf;
359         }
360         return NULL;
361 }
362
363 struct netstrfns *
364 libcfs_name2netstrfns(const char *name)
365 {
366         int    i;
367
368         for (i = 0; i < libcfs_nnetstrfns; i++)
369                 if (libcfs_netstrfns[i].nf_type >= 0 &&
370                     !strcmp(libcfs_netstrfns[i].nf_name, name))
371                         return &libcfs_netstrfns[i];
372
373         return NULL;
374 }
375
376 int
377 libcfs_isknown_lnd(int type)
378 {
379         return libcfs_lnd2netstrfns(type) != NULL;
380 }
381
382 char *
383 libcfs_lnd2modname(int lnd)
384 {
385         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
386
387         return (nf == NULL) ? NULL : nf->nf_modname;
388 }
389
390 char *
391 libcfs_lnd2str(int lnd)
392 {
393         char           *str;
394         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
395
396         if (nf != NULL)
397                 return nf->nf_name;
398
399         str = libcfs_next_nidstring();
400         snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
401         return str;
402 }
403
404 int
405 libcfs_str2lnd(const char *str)
406 {
407         struct netstrfns *nf = libcfs_name2netstrfns(str);
408
409         if (nf != NULL)
410                 return nf->nf_type;
411
412         return -1;
413 }
414
415 char *
416 libcfs_net2str(__u32 net)
417 {
418         int               lnd = LNET_NETTYP(net);
419         int               num = LNET_NETNUM(net);
420         struct netstrfns *nf  = libcfs_lnd2netstrfns(lnd);
421         char             *str = libcfs_next_nidstring();
422
423         if (nf == NULL)
424                 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
425         else if (num == 0)
426                 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
427         else
428                 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
429
430         return str;
431 }
432
433 char *
434 libcfs_nid2str(lnet_nid_t nid)
435 {
436         __u32             addr = LNET_NIDADDR(nid);
437         __u32             net = LNET_NIDNET(nid);
438         int               lnd = LNET_NETTYP(net);
439         int               nnum = LNET_NETNUM(net);
440         struct netstrfns *nf;
441         char             *str;
442         int               nob;
443
444         if (nid == LNET_NID_ANY)
445                 return "<?>";
446
447         nf = libcfs_lnd2netstrfns(lnd);
448         str = libcfs_next_nidstring();
449
450         if (nf == NULL)
451                 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
452         else {
453                 nf->nf_addr2str(addr, str);
454                 nob = strlen(str);
455                 if (nnum == 0)
456                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
457                                  nf->nf_name);
458                 else
459                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
460                                  nf->nf_name, nnum);
461         }
462
463         return str;
464 }
465
466 static struct netstrfns *
467 libcfs_str2net_internal(const char *str, __u32 *net)
468 {
469         struct netstrfns *nf = NULL;
470         int               nob;
471         int               netnum;
472         int               i;
473
474         for (i = 0; i < libcfs_nnetstrfns; i++) {
475                 nf = &libcfs_netstrfns[i];
476                 if (nf->nf_type >= 0 &&
477                     !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
478                         break;
479         }
480
481         if (i == libcfs_nnetstrfns)
482                 return NULL;
483
484         nob = strlen(nf->nf_name);
485
486         if (strlen(str) == (unsigned int)nob) {
487                 netnum = 0;
488         } else {
489                 if (nf->nf_type == LOLND) /* net number not allowed */
490                         return NULL;
491
492                 str += nob;
493                 i = strlen(str);
494                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
495                     i != (int)strlen(str))
496                         return NULL;
497         }
498
499         *net = LNET_MKNET(nf->nf_type, netnum);
500         return nf;
501 }
502
503 __u32
504 libcfs_str2net(const char *str)
505 {
506         __u32  net;
507
508         if (libcfs_str2net_internal(str, &net) != NULL)
509                 return net;
510
511         return LNET_NIDNET(LNET_NID_ANY);
512 }
513
514 lnet_nid_t
515 libcfs_str2nid(const char *str)
516 {
517         const char       *sep = strchr(str, '@');
518         struct netstrfns *nf;
519         __u32             net;
520         __u32             addr;
521
522         if (sep != NULL) {
523                 nf = libcfs_str2net_internal(sep + 1, &net);
524                 if (nf == NULL)
525                         return LNET_NID_ANY;
526         } else {
527                 sep = str + strlen(str);
528                 net = LNET_MKNET(SOCKLND, 0);
529                 nf = libcfs_lnd2netstrfns(SOCKLND);
530                 LASSERT (nf != NULL);
531         }
532
533         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
534                 return LNET_NID_ANY;
535
536         return LNET_MKNID(net, addr);
537 }
538
539 char *
540 libcfs_id2str(lnet_process_id_t id)
541 {
542         char *str = libcfs_next_nidstring();
543
544         if (id.pid == LNET_PID_ANY) {
545                 snprintf(str, LNET_NIDSTR_SIZE,
546                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
547                 return str;
548         }
549
550         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
551                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
552                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
553         return str;
554 }
555
556 int
557 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
558 {
559         if (!strcmp(str, "*")) {
560                 *nidp = LNET_NID_ANY;
561                 return 1;
562         }
563
564         *nidp = libcfs_str2nid(str);
565         return *nidp != LNET_NID_ANY;
566 }
567
568 /**
569  * Nid range list syntax.
570  * \verbatim
571  *
572  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
573  * <nidrange>        :== <addrrange> '@' <net>
574  * <addrrange>       :== '*' |
575  *                       <ipaddr_range> |
576  *                       <cfs_expr_list>
577  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
578  *                       <cfs_expr_list>
579  * <cfs_expr_list>   :== <number> |
580  *                       <expr_list>
581  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
582  * <range_expr>      :== <number> |
583  *                       <number> '-' <number> |
584  *                       <number> '-' <number> '/' <number>
585  * <net>             :== <netname> | <netname><number>
586  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
587  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
588  * \endverbatim
589  */
590
591 /**
592  * Structure to represent \<nidrange\> token of the syntax.
593  *
594  * One of this is created for each \<net\> parsed.
595  */
596 struct nidrange {
597         /**
598          * Link to list of this structures which is built on nid range
599          * list parsing.
600          */
601         struct list_head nr_link;
602         /**
603          * List head for addrrange::ar_link.
604          */
605         struct list_head nr_addrranges;
606         /**
607          * Flag indicating that *@<net> is found.
608          */
609         int nr_all;
610         /**
611          * Pointer to corresponding element of libcfs_netstrfns.
612          */
613         struct netstrfns *nr_netstrfns;
614         /**
615          * Number of network. E.g. 5 if \<net\> is "elan5".
616          */
617         int nr_netnum;
618 };
619
620 /**
621  * Structure to represent \<addrrange\> token of the syntax.
622  */
623 struct addrrange {
624         /**
625          * Link to nidrange::nr_addrranges.
626          */
627         struct list_head ar_link;
628         /**
629          * List head for cfs_expr_list::el_list.
630          */
631         struct list_head ar_numaddr_ranges;
632 };
633
634 /**
635  * Nf_parse_addrlist method for networks using numeric addresses.
636  *
637  * Examples of such networks are gm and elan.
638  *
639  * \retval 0 if \a str parsed to numeric address
640  * \retval errno otherwise
641  */
642 static int
643 libcfs_num_parse(char *str, int len, struct list_head *list)
644 {
645         struct cfs_expr_list *el;
646         int     rc;
647
648         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
649         if (rc == 0)
650                 list_add_tail(&el->el_link, list);
651
652         return rc;
653 }
654
655 /**
656  * Parses \<addrrange\> token on the syntax.
657  *
658  * Allocates struct addrrange and links to \a nidrange via
659  * (nidrange::nr_addrranges)
660  *
661  * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
662  * \retval 0 otherwise
663  */
664 static int
665 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
666 {
667         struct addrrange *addrrange;
668
669         if (src->ls_len == 1 && src->ls_str[0] == '*') {
670                 nidrange->nr_all = 1;
671                 return 1;
672         }
673
674         LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
675         if (addrrange == NULL)
676                 return 0;
677         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
678         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
679
680         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
681                                                 src->ls_len,
682                                                 &addrrange->ar_numaddr_ranges);
683 }
684
685 /**
686  * Finds or creates struct nidrange.
687  *
688  * Checks if \a src is a valid network name, looks for corresponding
689  * nidrange on the ist of nidranges (\a nidlist), creates new struct
690  * nidrange if it is not found.
691  *
692  * \retval pointer to struct nidrange matching network specified via \a src
693  * \retval NULL if \a src does not match any network
694  */
695 static struct nidrange *
696 add_nidrange(const struct cfs_lstr *src,
697              struct list_head *nidlist)
698 {
699         struct netstrfns *nf;
700         struct nidrange *nr;
701         int endlen;
702         unsigned netnum;
703
704         if (src->ls_len >= LNET_NIDSTR_SIZE)
705                 return NULL;
706
707         nf = libcfs_namenum2netstrfns(src->ls_str);
708         if (nf == NULL)
709                 return NULL;
710         endlen = src->ls_len - strlen(nf->nf_name);
711         if (endlen == 0)
712                 /* network name only, e.g. "elan" or "tcp" */
713                 netnum = 0;
714         else {
715                 /* e.g. "elan25" or "tcp23", refuse to parse if
716                  * network name is not appended with decimal or
717                  * hexadecimal number */
718                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
719                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
720                         return NULL;
721         }
722
723         list_for_each_entry(nr, nidlist, nr_link) {
724                 if (nr->nr_netstrfns != nf)
725                         continue;
726                 if (nr->nr_netnum != netnum)
727                         continue;
728                 return nr;
729         }
730
731         LIBCFS_ALLOC(nr, sizeof(struct nidrange));
732         if (nr == NULL)
733                 return NULL;
734         list_add_tail(&nr->nr_link, nidlist);
735         INIT_LIST_HEAD(&nr->nr_addrranges);
736         nr->nr_netstrfns = nf;
737         nr->nr_all = 0;
738         nr->nr_netnum = netnum;
739
740         return nr;
741 }
742
743 /**
744  * Parses \<nidrange\> token of the syntax.
745  *
746  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
747  * \retval 0 otherwise
748  */
749 static int
750 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
751 {
752         struct cfs_lstr addrrange;
753         struct cfs_lstr net;
754         struct cfs_lstr tmp;
755         struct nidrange *nr;
756
757         tmp = *src;
758         if (cfs_gettok(src, '@', &addrrange) == 0)
759                 goto failed;
760
761         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
762                 goto failed;
763
764         nr = add_nidrange(&net, nidlist);
765         if (nr == NULL)
766                 goto failed;
767
768         if (parse_addrange(&addrrange, nr) != 0)
769                 goto failed;
770
771         return 1;
772  failed:
773         CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
774         return 0;
775 }
776
777 /**
778  * Frees addrrange structures of \a list.
779  *
780  * For each struct addrrange structure found on \a list it frees
781  * cfs_expr_list list attached to it and frees the addrrange itself.
782  *
783  * \retval none
784  */
785 static void
786 free_addrranges(struct list_head *list)
787 {
788         while (!list_empty(list)) {
789                 struct addrrange *ar;
790
791                 ar = list_entry(list->next, struct addrrange, ar_link);
792
793                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
794                 list_del(&ar->ar_link);
795                 LIBCFS_FREE(ar, sizeof(struct addrrange));
796         }
797 }
798
799 /**
800  * Frees nidrange strutures of \a list.
801  *
802  * For each struct nidrange structure found on \a list it frees
803  * addrrange list attached to it and frees the nidrange itself.
804  *
805  * \retval none
806  */
807 void
808 cfs_free_nidlist(struct list_head *list)
809 {
810         struct list_head *pos, *next;
811         struct nidrange *nr;
812
813         list_for_each_safe(pos, next, list) {
814                 nr = list_entry(pos, struct nidrange, nr_link);
815                 free_addrranges(&nr->nr_addrranges);
816                 list_del(pos);
817                 LIBCFS_FREE(nr, sizeof(struct nidrange));
818         }
819 }
820
821 /**
822  * Parses nid range list.
823  *
824  * Parses with rigorous syntax and overflow checking \a str into
825  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
826  * structures and links that structure to \a nidlist. The resulting
827  * list can be used to match a NID againts set of NIDS defined by \a
828  * str.
829  * \see cfs_match_nid
830  *
831  * \retval 1 on success
832  * \retval 0 otherwise
833  */
834 int
835 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
836 {
837         struct cfs_lstr src;
838         struct cfs_lstr res;
839         int rc;
840         ENTRY;
841
842         src.ls_str = str;
843         src.ls_len = len;
844         INIT_LIST_HEAD(nidlist);
845         while (src.ls_str) {
846                 rc = cfs_gettok(&src, ' ', &res);
847                 if (rc == 0) {
848                         cfs_free_nidlist(nidlist);
849                         RETURN(0);
850                 }
851                 rc = parse_nidrange(&res, nidlist);
852                 if (rc == 0) {
853                         cfs_free_nidlist(nidlist);
854                         RETURN(0);
855                 }
856         }
857         RETURN(1);
858 }
859
860 /*
861  * Nf_match_addr method for networks using numeric addresses
862  *
863  * \retval 1 on match
864  * \retval 0 otherwise
865  */
866 static int
867 libcfs_num_match(__u32 addr, struct list_head *numaddr)
868 {
869         struct cfs_expr_list *el;
870
871         LASSERT(!list_empty(numaddr));
872         el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
873
874         return cfs_expr_list_match(addr, el);
875 }
876
877 /**
878  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
879  *
880  * \see cfs_parse_nidlist()
881  *
882  * \retval 1 on match
883  * \retval 0  otherwises
884  */
885 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
886 {
887         struct nidrange *nr;
888         struct addrrange *ar;
889         ENTRY;
890
891         list_for_each_entry(nr, nidlist, nr_link) {
892                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
893                         continue;
894                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
895                         continue;
896                 if (nr->nr_all)
897                         RETURN(1);
898                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
899                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
900                                                         &ar->ar_numaddr_ranges))
901                                 RETURN(1);
902         }
903         RETURN(0);
904 }
905
906 #ifdef __KERNEL__
907
908 EXPORT_SYMBOL(libcfs_isknown_lnd);
909 EXPORT_SYMBOL(libcfs_lnd2modname);
910 EXPORT_SYMBOL(libcfs_lnd2str);
911 EXPORT_SYMBOL(libcfs_str2lnd);
912 EXPORT_SYMBOL(libcfs_net2str);
913 EXPORT_SYMBOL(libcfs_nid2str);
914 EXPORT_SYMBOL(libcfs_str2net);
915 EXPORT_SYMBOL(libcfs_str2nid);
916 EXPORT_SYMBOL(libcfs_id2str);
917 EXPORT_SYMBOL(libcfs_str2anynid);
918 EXPORT_SYMBOL(cfs_free_nidlist);
919 EXPORT_SYMBOL(cfs_parse_nidlist);
920 EXPORT_SYMBOL(cfs_match_nid);
921
922 #endif