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