Whamcloud - gitweb
b49376b9cf29256854c9ecbdb92d155ba1ff1c7f
[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, cfs_list_t *list);
105 static int  libcfs_num_match(__u32 addr, cfs_list_t *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                                         cfs_list_t *list);
115         int        (*nf_match_addr)(__u32 addr, cfs_list_t *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         /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
211         {/* .nf_type      */  -1},
212 };
213
214 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
215
216 int
217 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
218 {
219         *addr = 0;
220         return 1;
221 }
222
223 void
224 libcfs_ip_addr2str(__u32 addr, char *str)
225 {
226 #if 0   /* never lookup */
227 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
228         __u32           netip = htonl(addr);
229         struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
230
231         if (he != NULL) {
232                 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
233                 return;
234         }
235 #endif
236 #endif
237         snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
238                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
239                  (addr >> 8) & 0xff, addr & 0xff);
240 }
241
242 /* CAVEAT EMPTOR XscanfX
243  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
244  * sscanf may return immediately if it sees the terminating '0' in a string, so
245  * I initialise the %n variable to the expected length.  If sscanf sets it;
246  * fine, if it doesn't, then the scan ended at the end of the string, which is
247  * fine too :) */
248
249 int
250 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
251 {
252         int   a;
253         int   b;
254         int   c;
255         int   d;
256         int   n = nob;                          /* XscanfX */
257
258         /* numeric IP? */
259         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
260             n == nob &&
261             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
262             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
263                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
264                 return 1;
265         }
266
267 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
268         /* known hostname? */
269         if (('a' <= str[0] && str[0] <= 'z') ||
270             ('A' <= str[0] && str[0] <= 'Z')) {
271                 char *tmp;
272
273                 LIBCFS_ALLOC(tmp, nob + 1);
274                 if (tmp != NULL) {
275                         struct hostent *he;
276
277                         memcpy(tmp, str, nob);
278                         tmp[nob] = 0;
279
280                         he = gethostbyname(tmp);
281
282                         LIBCFS_FREE(tmp, nob);
283
284                         if (he != NULL) {
285                                 __u32 ip = *(__u32 *)he->h_addr;
286
287                                 *addr = ntohl(ip);
288                                 return 1;
289                         }
290                 }
291         }
292 #endif
293         return 0;
294 }
295
296 void
297 libcfs_decnum_addr2str(__u32 addr, char *str)
298 {
299         snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
300 }
301
302 void
303 libcfs_hexnum_addr2str(__u32 addr, char *str)
304 {
305         snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
306 }
307
308 int
309 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
310 {
311         int     n;
312
313         n = nob;
314         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
315                 return 1;
316
317         n = nob;
318         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
319                 return 1;
320
321         n = nob;
322         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
323                 return 1;
324
325         return 0;
326 }
327
328 struct netstrfns *
329 libcfs_lnd2netstrfns(int lnd)
330 {
331         int    i;
332
333         if (lnd >= 0)
334                 for (i = 0; i < libcfs_nnetstrfns; i++)
335                         if (lnd == libcfs_netstrfns[i].nf_type)
336                                 return &libcfs_netstrfns[i];
337
338         return NULL;
339 }
340
341 struct netstrfns *
342 libcfs_namenum2netstrfns(const char *name)
343 {
344         struct netstrfns *nf;
345         int               i;
346
347         for (i = 0; i < libcfs_nnetstrfns; i++) {
348                 nf = &libcfs_netstrfns[i];
349                 if (nf->nf_type >= 0 &&
350                     !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
351                         return nf;
352         }
353         return NULL;
354 }
355
356 struct netstrfns *
357 libcfs_name2netstrfns(const char *name)
358 {
359         int    i;
360
361         for (i = 0; i < libcfs_nnetstrfns; i++)
362                 if (libcfs_netstrfns[i].nf_type >= 0 &&
363                     !strcmp(libcfs_netstrfns[i].nf_name, name))
364                         return &libcfs_netstrfns[i];
365
366         return NULL;
367 }
368
369 int
370 libcfs_isknown_lnd(int type)
371 {
372         return libcfs_lnd2netstrfns(type) != NULL;
373 }
374
375 char *
376 libcfs_lnd2modname(int lnd)
377 {
378         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
379
380         return (nf == NULL) ? NULL : nf->nf_modname;
381 }
382
383 char *
384 libcfs_lnd2str(int lnd)
385 {
386         char           *str;
387         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
388
389         if (nf != NULL)
390                 return nf->nf_name;
391
392         str = libcfs_next_nidstring();
393         snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
394         return str;
395 }
396
397 int
398 libcfs_str2lnd(const char *str)
399 {
400         struct netstrfns *nf = libcfs_name2netstrfns(str);
401
402         if (nf != NULL)
403                 return nf->nf_type;
404
405         return -1;
406 }
407
408 char *
409 libcfs_net2str(__u32 net)
410 {
411         int               lnd = LNET_NETTYP(net);
412         int               num = LNET_NETNUM(net);
413         struct netstrfns *nf  = libcfs_lnd2netstrfns(lnd);
414         char             *str = libcfs_next_nidstring();
415
416         if (nf == NULL)
417                 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
418         else if (num == 0)
419                 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
420         else
421                 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
422
423         return str;
424 }
425
426 char *
427 libcfs_nid2str(lnet_nid_t nid)
428 {
429         __u32             addr = LNET_NIDADDR(nid);
430         __u32             net = LNET_NIDNET(nid);
431         int               lnd = LNET_NETTYP(net);
432         int               nnum = LNET_NETNUM(net);
433         struct netstrfns *nf;
434         char             *str;
435         int               nob;
436
437         if (nid == LNET_NID_ANY)
438                 return "<?>";
439
440         nf = libcfs_lnd2netstrfns(lnd);
441         str = libcfs_next_nidstring();
442
443         if (nf == NULL)
444                 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
445         else {
446                 nf->nf_addr2str(addr, str);
447                 nob = strlen(str);
448                 if (nnum == 0)
449                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
450                                  nf->nf_name);
451                 else
452                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
453                                  nf->nf_name, nnum);
454         }
455
456         return str;
457 }
458
459 static struct netstrfns *
460 libcfs_str2net_internal(const char *str, __u32 *net)
461 {
462         struct netstrfns *nf = NULL;
463         int               nob;
464         int               netnum;
465         int               i;
466
467         for (i = 0; i < libcfs_nnetstrfns; i++) {
468                 nf = &libcfs_netstrfns[i];
469                 if (nf->nf_type >= 0 &&
470                     !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
471                         break;
472         }
473
474         if (i == libcfs_nnetstrfns)
475                 return NULL;
476
477         nob = strlen(nf->nf_name);
478
479         if (strlen(str) == (unsigned int)nob) {
480                 netnum = 0;
481         } else {
482                 if (nf->nf_type == LOLND) /* net number not allowed */
483                         return NULL;
484
485                 str += nob;
486                 i = strlen(str);
487                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
488                     i != (int)strlen(str))
489                         return NULL;
490         }
491
492         *net = LNET_MKNET(nf->nf_type, netnum);
493         return nf;
494 }
495
496 __u32
497 libcfs_str2net(const char *str)
498 {
499         __u32  net;
500
501         if (libcfs_str2net_internal(str, &net) != NULL)
502                 return net;
503
504         return LNET_NIDNET(LNET_NID_ANY);
505 }
506
507 lnet_nid_t
508 libcfs_str2nid(const char *str)
509 {
510         const char       *sep = strchr(str, '@');
511         struct netstrfns *nf;
512         __u32             net;
513         __u32             addr;
514
515         if (sep != NULL) {
516                 nf = libcfs_str2net_internal(sep + 1, &net);
517                 if (nf == NULL)
518                         return LNET_NID_ANY;
519         } else {
520                 sep = str + strlen(str);
521                 net = LNET_MKNET(SOCKLND, 0);
522                 nf = libcfs_lnd2netstrfns(SOCKLND);
523                 LASSERT (nf != NULL);
524         }
525
526         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
527                 return LNET_NID_ANY;
528
529         return LNET_MKNID(net, addr);
530 }
531
532 char *
533 libcfs_id2str(lnet_process_id_t id)
534 {
535         char *str = libcfs_next_nidstring();
536
537         if (id.pid == LNET_PID_ANY) {
538                 snprintf(str, LNET_NIDSTR_SIZE,
539                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
540                 return str;
541         }
542
543         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
544                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
545                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
546         return str;
547 }
548
549 int
550 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
551 {
552         if (!strcmp(str, "*")) {
553                 *nidp = LNET_NID_ANY;
554                 return 1;
555         }
556
557         *nidp = libcfs_str2nid(str);
558         return *nidp != LNET_NID_ANY;
559 }
560
561 /**
562  * Nid range list syntax.
563  * \verbatim
564  *
565  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
566  * <nidrange>        :== <addrrange> '@' <net>
567  * <addrrange>       :== '*' |
568  *                       <ipaddr_range> |
569  *                       <cfs_expr_list>
570  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
571  *                       <cfs_expr_list>
572  * <cfs_expr_list>   :== <number> |
573  *                       <expr_list>
574  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
575  * <range_expr>      :== <number> |
576  *                       <number> '-' <number> |
577  *                       <number> '-' <number> '/' <number>
578  * <net>             :== <netname> | <netname><number>
579  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
580  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
581  * \endverbatim
582  */
583
584 /**
585  * Structure to represent \<nidrange\> token of the syntax.
586  *
587  * One of this is created for each \<net\> parsed.
588  */
589 struct nidrange {
590         /**
591          * Link to list of this structures which is built on nid range
592          * list parsing.
593          */
594         cfs_list_t nr_link;
595         /**
596          * List head for addrrange::ar_link.
597          */
598         cfs_list_t nr_addrranges;
599         /**
600          * Flag indicating that *@<net> is found.
601          */
602         int nr_all;
603         /**
604          * Pointer to corresponding element of libcfs_netstrfns.
605          */
606         struct netstrfns *nr_netstrfns;
607         /**
608          * Number of network. E.g. 5 if \<net\> is "elan5".
609          */
610         int nr_netnum;
611 };
612
613 /**
614  * Structure to represent \<addrrange\> token of the syntax.
615  */
616 struct addrrange {
617         /**
618          * Link to nidrange::nr_addrranges.
619          */
620         cfs_list_t ar_link;
621         /**
622          * List head for cfs_expr_list::el_list.
623          */
624         cfs_list_t ar_numaddr_ranges;
625 };
626
627 /**
628  * Nf_parse_addrlist method for networks using numeric addresses.
629  *
630  * Examples of such networks are gm and elan.
631  *
632  * \retval 0 if \a str parsed to numeric address
633  * \retval errno otherwise
634  */
635 static int
636 libcfs_num_parse(char *str, int len, cfs_list_t *list)
637 {
638         struct cfs_expr_list *el;
639         int     rc;
640
641         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
642         if (rc == 0)
643                 cfs_list_add_tail(&el->el_link, list);
644
645         return rc;
646 }
647
648 /**
649  * Parses \<addrrange\> token on the syntax.
650  *
651  * Allocates struct addrrange and links to \a nidrange via
652  * (nidrange::nr_addrranges)
653  *
654  * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
655  * \retval 0 otherwise
656  */
657 static int
658 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
659 {
660         struct addrrange *addrrange;
661
662         if (src->ls_len == 1 && src->ls_str[0] == '*') {
663                 nidrange->nr_all = 1;
664                 return 1;
665         }
666
667         LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
668         if (addrrange == NULL)
669                 return 0;
670         cfs_list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
671         CFS_INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
672
673         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
674                                                 src->ls_len,
675                                                 &addrrange->ar_numaddr_ranges);
676 }
677
678 /**
679  * Finds or creates struct nidrange.
680  *
681  * Checks if \a src is a valid network name, looks for corresponding
682  * nidrange on the ist of nidranges (\a nidlist), creates new struct
683  * nidrange if it is not found.
684  *
685  * \retval pointer to struct nidrange matching network specified via \a src
686  * \retval NULL if \a src does not match any network
687  */
688 static struct nidrange *
689 add_nidrange(const struct cfs_lstr *src,
690              cfs_list_t *nidlist)
691 {
692         struct netstrfns *nf;
693         struct nidrange *nr;
694         int endlen;
695         unsigned netnum;
696
697         if (src->ls_len >= LNET_NIDSTR_SIZE)
698                 return NULL;
699
700         nf = libcfs_namenum2netstrfns(src->ls_str);
701         if (nf == NULL)
702                 return NULL;
703         endlen = src->ls_len - strlen(nf->nf_name);
704         if (endlen == 0)
705                 /* network name only, e.g. "elan" or "tcp" */
706                 netnum = 0;
707         else {
708                 /* e.g. "elan25" or "tcp23", refuse to parse if
709                  * network name is not appended with decimal or
710                  * hexadecimal number */
711                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
712                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
713                         return NULL;
714         }
715
716         cfs_list_for_each_entry(nr, nidlist, nr_link) {
717                 if (nr->nr_netstrfns != nf)
718                         continue;
719                 if (nr->nr_netnum != netnum)
720                         continue;
721                 return nr;
722         }
723
724         LIBCFS_ALLOC(nr, sizeof(struct nidrange));
725         if (nr == NULL)
726                 return NULL;
727         cfs_list_add_tail(&nr->nr_link, nidlist);
728         CFS_INIT_LIST_HEAD(&nr->nr_addrranges);
729         nr->nr_netstrfns = nf;
730         nr->nr_all = 0;
731         nr->nr_netnum = netnum;
732
733         return nr;
734 }
735
736 /**
737  * Parses \<nidrange\> token of the syntax.
738  *
739  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
740  * \retval 0 otherwise
741  */
742 static int
743 parse_nidrange(struct cfs_lstr *src, cfs_list_t *nidlist)
744 {
745         struct cfs_lstr addrrange;
746         struct cfs_lstr net;
747         struct cfs_lstr tmp;
748         struct nidrange *nr;
749
750         tmp = *src;
751         if (cfs_gettok(src, '@', &addrrange) == 0)
752                 goto failed;
753
754         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
755                 goto failed;
756
757         nr = add_nidrange(&net, nidlist);
758         if (nr == NULL)
759                 goto failed;
760
761         if (parse_addrange(&addrrange, nr) != 0)
762                 goto failed;
763
764         return 1;
765  failed:
766         CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
767         return 0;
768 }
769
770 /**
771  * Frees addrrange structures of \a list.
772  *
773  * For each struct addrrange structure found on \a list it frees
774  * cfs_expr_list list attached to it and frees the addrrange itself.
775  *
776  * \retval none
777  */
778 static void
779 free_addrranges(cfs_list_t *list)
780 {
781         while (!cfs_list_empty(list)) {
782                 struct addrrange *ar;
783
784                 ar = cfs_list_entry(list->next, struct addrrange, ar_link);
785
786                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
787                 cfs_list_del(&ar->ar_link);
788                 LIBCFS_FREE(ar, sizeof(struct addrrange));
789         }
790 }
791
792 /**
793  * Frees nidrange strutures of \a list.
794  *
795  * For each struct nidrange structure found on \a list it frees
796  * addrrange list attached to it and frees the nidrange itself.
797  *
798  * \retval none
799  */
800 void
801 cfs_free_nidlist(cfs_list_t *list)
802 {
803         cfs_list_t *pos, *next;
804         struct nidrange *nr;
805
806         cfs_list_for_each_safe(pos, next, list) {
807                 nr = cfs_list_entry(pos, struct nidrange, nr_link);
808                 free_addrranges(&nr->nr_addrranges);
809                 cfs_list_del(pos);
810                 LIBCFS_FREE(nr, sizeof(struct nidrange));
811         }
812 }
813
814 /**
815  * Parses nid range list.
816  *
817  * Parses with rigorous syntax and overflow checking \a str into
818  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
819  * structures and links that structure to \a nidlist. The resulting
820  * list can be used to match a NID againts set of NIDS defined by \a
821  * str.
822  * \see cfs_match_nid
823  *
824  * \retval 1 on success
825  * \retval 0 otherwise
826  */
827 int
828 cfs_parse_nidlist(char *str, int len, cfs_list_t *nidlist)
829 {
830         struct cfs_lstr src;
831         struct cfs_lstr res;
832         int rc;
833         ENTRY;
834
835         src.ls_str = str;
836         src.ls_len = len;
837         CFS_INIT_LIST_HEAD(nidlist);
838         while (src.ls_str) {
839                 rc = cfs_gettok(&src, ' ', &res);
840                 if (rc == 0) {
841                         cfs_free_nidlist(nidlist);
842                         RETURN(0);
843                 }
844                 rc = parse_nidrange(&res, nidlist);
845                 if (rc == 0) {
846                         cfs_free_nidlist(nidlist);
847                         RETURN(0);
848                 }
849         }
850         RETURN(1);
851 }
852
853 /*
854  * Nf_match_addr method for networks using numeric addresses
855  *
856  * \retval 1 on match
857  * \retval 0 otherwise
858  */
859 static int
860 libcfs_num_match(__u32 addr, cfs_list_t *numaddr)
861 {
862         struct cfs_expr_list *el;
863
864         LASSERT(!cfs_list_empty(numaddr));
865         el = cfs_list_entry(numaddr->next, struct cfs_expr_list, el_link);
866
867         return cfs_expr_list_match(addr, el);
868 }
869
870 /**
871  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
872  *
873  * \see cfs_parse_nidlist()
874  *
875  * \retval 1 on match
876  * \retval 0  otherwises
877  */
878 int cfs_match_nid(lnet_nid_t nid, cfs_list_t *nidlist)
879 {
880         struct nidrange *nr;
881         struct addrrange *ar;
882         ENTRY;
883
884         cfs_list_for_each_entry(nr, nidlist, nr_link) {
885                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
886                         continue;
887                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
888                         continue;
889                 if (nr->nr_all)
890                         RETURN(1);
891                 cfs_list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
892                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
893                                                        &ar->ar_numaddr_ranges))
894                                 RETURN(1);
895         }
896         RETURN(0);
897 }
898
899 #ifdef __KERNEL__
900
901 EXPORT_SYMBOL(libcfs_isknown_lnd);
902 EXPORT_SYMBOL(libcfs_lnd2modname);
903 EXPORT_SYMBOL(libcfs_lnd2str);
904 EXPORT_SYMBOL(libcfs_str2lnd);
905 EXPORT_SYMBOL(libcfs_net2str);
906 EXPORT_SYMBOL(libcfs_nid2str);
907 EXPORT_SYMBOL(libcfs_str2net);
908 EXPORT_SYMBOL(libcfs_str2nid);
909 EXPORT_SYMBOL(libcfs_id2str);
910 EXPORT_SYMBOL(libcfs_str2anynid);
911 EXPORT_SYMBOL(cfs_free_nidlist);
912 EXPORT_SYMBOL(cfs_parse_nidlist);
913 EXPORT_SYMBOL(cfs_match_nid);
914
915 #endif