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