Whamcloud - gitweb
a78dd85b2597dd41306ecbfa8cf92b6b87dba06a
[fs/lustre-release.git] / libcfs / libcfs / nidstrings.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/nidstrings.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44 #include <lnet/lnet.h>
45 #ifndef __KERNEL__
46 #ifdef HAVE_GETHOSTBYNAME
47 # include <netdb.h>
48 #endif
49 #endif
50
51 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
52  * consistent in all conversion functions.  Some code fragments are copied
53  * around for the sake of clarity...
54  */
55
56 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
57  * Choose the number of nidstrings to support the MAXIMUM expected number of
58  * concurrent users.  If there are more, the returned string will be volatile.
59  * NB this number must allow for a process to be descheduled for a timeslice
60  * between getting its string and using it.
61  */
62
63 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
64 static int       libcfs_nidstring_idx = 0;
65
66 #ifdef __KERNEL__
67 static spinlock_t libcfs_nidstring_lock;
68
69 void libcfs_init_nidstrings (void)
70 {
71         spin_lock_init(&libcfs_nidstring_lock);
72 }
73
74 # define NIDSTR_LOCK(f)   spin_lock_irqsave(&libcfs_nidstring_lock, f)
75 # define NIDSTR_UNLOCK(f) spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
76 #else
77 # define NIDSTR_LOCK(f)   (f=sizeof(f))  /* avoid set-but-unused warnings */
78 # define NIDSTR_UNLOCK(f) (f=sizeof(f))
79 #endif
80
81 static char *
82 libcfs_next_nidstring (void)
83 {
84         char          *str;
85         unsigned long  flags;
86
87         NIDSTR_LOCK(flags);
88
89         str = libcfs_nidstrings[libcfs_nidstring_idx++];
90         if (libcfs_nidstring_idx ==
91             sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
92                 libcfs_nidstring_idx = 0;
93
94         NIDSTR_UNLOCK(flags);
95         return str;
96 }
97
98 static int  libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
99 static void libcfs_ip_addr2str(__u32 addr, char *str);
100 static int  libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
101 static void libcfs_decnum_addr2str(__u32 addr, char *str);
102 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
103 static int  libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
104 static int  libcfs_num_parse(char *str, int len, struct list_head *list);
105 static int  libcfs_num_match(__u32 addr, struct list_head *list);
106 static int  libcfs_num_addr_range_print(char *buffer, int count,
107                                         struct list_head *list);
108 static int  libcfs_ip_addr_range_print(char *buffer, int count,
109                                        struct list_head *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                                         struct list_head *list);
119         int     (*nf_print_addrlist)(char *buffer, int count,
120                                      struct list_head *list);
121         int     (*nf_match_addr)(__u32 addr, struct list_head *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         {/* .nf_type      */  GNIIPLND,
230          /* .nf_name      */  "gip",
231          /* .nf_modname   */  "kgnilnd",
232          /* .nf_addr2str  */  libcfs_ip_addr2str,
233          /* .nf_str2addr  */  libcfs_ip_str2addr,
234          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
235          /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
236          /* .nf_match_addr*/  cfs_ip_addr_match},
237         /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
238         {/* .nf_type      */  -1},
239 };
240
241 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
242
243 int
244 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
245 {
246         *addr = 0;
247         return 1;
248 }
249
250 void
251 libcfs_ip_addr2str(__u32 addr, char *str)
252 {
253 #if 0   /* never lookup */
254 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
255         __u32           netip = htonl(addr);
256         struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
257
258         if (he != NULL) {
259                 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
260                 return;
261         }
262 #endif
263 #endif
264         snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
265                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
266                  (addr >> 8) & 0xff, addr & 0xff);
267 }
268
269 /* CAVEAT EMPTOR XscanfX
270  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
271  * sscanf may return immediately if it sees the terminating '0' in a string, so
272  * I initialise the %n variable to the expected length.  If sscanf sets it;
273  * fine, if it doesn't, then the scan ended at the end of the string, which is
274  * fine too :) */
275
276 int
277 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
278 {
279         int   a;
280         int   b;
281         int   c;
282         int   d;
283         int   n = nob;                          /* XscanfX */
284
285         /* numeric IP? */
286         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
287             n == nob &&
288             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
289             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
290                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
291                 return 1;
292         }
293
294 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
295         /* known hostname? */
296         if (('a' <= str[0] && str[0] <= 'z') ||
297             ('A' <= str[0] && str[0] <= 'Z')) {
298                 char *tmp;
299
300                 LIBCFS_ALLOC(tmp, nob + 1);
301                 if (tmp != NULL) {
302                         struct hostent *he;
303
304                         memcpy(tmp, str, nob);
305                         tmp[nob] = 0;
306
307                         he = gethostbyname(tmp);
308
309                         LIBCFS_FREE(tmp, nob);
310
311                         if (he != NULL) {
312                                 __u32 ip = *(__u32 *)he->h_addr;
313
314                                 *addr = ntohl(ip);
315                                 return 1;
316                         }
317                 }
318         }
319 #endif
320         return 0;
321 }
322
323 void
324 libcfs_decnum_addr2str(__u32 addr, char *str)
325 {
326         snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
327 }
328
329 void
330 libcfs_hexnum_addr2str(__u32 addr, char *str)
331 {
332         snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
333 }
334
335 int
336 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
337 {
338         int     n;
339
340         n = nob;
341         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
342                 return 1;
343
344         n = nob;
345         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
346                 return 1;
347
348         n = nob;
349         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
350                 return 1;
351
352         return 0;
353 }
354
355 struct netstrfns *
356 libcfs_lnd2netstrfns(int lnd)
357 {
358         int    i;
359
360         if (lnd >= 0)
361                 for (i = 0; i < libcfs_nnetstrfns; i++)
362                         if (lnd == libcfs_netstrfns[i].nf_type)
363                                 return &libcfs_netstrfns[i];
364
365         return NULL;
366 }
367
368 struct netstrfns *
369 libcfs_namenum2netstrfns(const char *name)
370 {
371         struct netstrfns *nf;
372         int               i;
373
374         for (i = 0; i < libcfs_nnetstrfns; i++) {
375                 nf = &libcfs_netstrfns[i];
376                 if (nf->nf_type >= 0 &&
377                     !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
378                         return nf;
379         }
380         return NULL;
381 }
382
383 struct netstrfns *
384 libcfs_name2netstrfns(const char *name)
385 {
386         int    i;
387
388         for (i = 0; i < libcfs_nnetstrfns; i++)
389                 if (libcfs_netstrfns[i].nf_type >= 0 &&
390                     !strcmp(libcfs_netstrfns[i].nf_name, name))
391                         return &libcfs_netstrfns[i];
392
393         return NULL;
394 }
395
396 int
397 libcfs_isknown_lnd(int type)
398 {
399         return libcfs_lnd2netstrfns(type) != NULL;
400 }
401
402 char *
403 libcfs_lnd2modname(int lnd)
404 {
405         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
406
407         return (nf == NULL) ? NULL : nf->nf_modname;
408 }
409
410 char *
411 libcfs_lnd2str(int lnd)
412 {
413         char           *str;
414         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
415
416         if (nf != NULL)
417                 return nf->nf_name;
418
419         str = libcfs_next_nidstring();
420         snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
421         return str;
422 }
423
424 int
425 libcfs_str2lnd(const char *str)
426 {
427         struct netstrfns *nf = libcfs_name2netstrfns(str);
428
429         if (nf != NULL)
430                 return nf->nf_type;
431
432         return -1;
433 }
434
435 char *
436 libcfs_net2str(__u32 net)
437 {
438         int               lnd = LNET_NETTYP(net);
439         int               num = LNET_NETNUM(net);
440         struct netstrfns *nf  = libcfs_lnd2netstrfns(lnd);
441         char             *str = libcfs_next_nidstring();
442
443         if (nf == NULL)
444                 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
445         else if (num == 0)
446                 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
447         else
448                 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
449
450         return str;
451 }
452
453 char *
454 libcfs_nid2str(lnet_nid_t nid)
455 {
456         __u32             addr = LNET_NIDADDR(nid);
457         __u32             net = LNET_NIDNET(nid);
458         int               lnd = LNET_NETTYP(net);
459         int               nnum = LNET_NETNUM(net);
460         struct netstrfns *nf;
461         char             *str;
462         int               nob;
463
464         if (nid == LNET_NID_ANY)
465                 return "<?>";
466
467         nf = libcfs_lnd2netstrfns(lnd);
468         str = libcfs_next_nidstring();
469
470         if (nf == NULL)
471                 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
472         else {
473                 nf->nf_addr2str(addr, str);
474                 nob = strlen(str);
475                 if (nnum == 0)
476                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
477                                  nf->nf_name);
478                 else
479                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
480                                  nf->nf_name, nnum);
481         }
482
483         return str;
484 }
485
486 static struct netstrfns *
487 libcfs_str2net_internal(const char *str, __u32 *net)
488 {
489         struct netstrfns *nf = NULL;
490         int               nob;
491         int               netnum;
492         int               i;
493
494         for (i = 0; i < libcfs_nnetstrfns; i++) {
495                 nf = &libcfs_netstrfns[i];
496                 if (nf->nf_type >= 0 &&
497                     !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
498                         break;
499         }
500
501         if (i == libcfs_nnetstrfns)
502                 return NULL;
503
504         nob = strlen(nf->nf_name);
505
506         if (strlen(str) == (unsigned int)nob) {
507                 netnum = 0;
508         } else {
509                 if (nf->nf_type == LOLND) /* net number not allowed */
510                         return NULL;
511
512                 str += nob;
513                 i = strlen(str);
514                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
515                     i != (int)strlen(str))
516                         return NULL;
517         }
518
519         *net = LNET_MKNET(nf->nf_type, netnum);
520         return nf;
521 }
522
523 __u32
524 libcfs_str2net(const char *str)
525 {
526         __u32  net;
527
528         if (libcfs_str2net_internal(str, &net) != NULL)
529                 return net;
530
531         return LNET_NIDNET(LNET_NID_ANY);
532 }
533
534 lnet_nid_t
535 libcfs_str2nid(const char *str)
536 {
537         const char       *sep = strchr(str, '@');
538         struct netstrfns *nf;
539         __u32             net;
540         __u32             addr;
541
542         if (sep != NULL) {
543                 nf = libcfs_str2net_internal(sep + 1, &net);
544                 if (nf == NULL)
545                         return LNET_NID_ANY;
546         } else {
547                 sep = str + strlen(str);
548                 net = LNET_MKNET(SOCKLND, 0);
549                 nf = libcfs_lnd2netstrfns(SOCKLND);
550                 LASSERT (nf != NULL);
551         }
552
553         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
554                 return LNET_NID_ANY;
555
556         return LNET_MKNID(net, addr);
557 }
558
559 char *
560 libcfs_id2str(lnet_process_id_t id)
561 {
562         char *str = libcfs_next_nidstring();
563
564         if (id.pid == LNET_PID_ANY) {
565                 snprintf(str, LNET_NIDSTR_SIZE,
566                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
567                 return str;
568         }
569
570         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
571                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
572                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
573         return str;
574 }
575
576 int
577 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
578 {
579         if (!strcmp(str, "*")) {
580                 *nidp = LNET_NID_ANY;
581                 return 1;
582         }
583
584         *nidp = libcfs_str2nid(str);
585         return *nidp != LNET_NID_ANY;
586 }
587
588 /**
589  * Nid range list syntax.
590  * \verbatim
591  *
592  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
593  * <nidrange>        :== <addrrange> '@' <net>
594  * <addrrange>       :== '*' |
595  *                       <ipaddr_range> |
596  *                       <cfs_expr_list>
597  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
598  *                       <cfs_expr_list>
599  * <cfs_expr_list>   :== <number> |
600  *                       <expr_list>
601  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
602  * <range_expr>      :== <number> |
603  *                       <number> '-' <number> |
604  *                       <number> '-' <number> '/' <number>
605  * <net>             :== <netname> | <netname><number>
606  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
607  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
608  * \endverbatim
609  */
610
611 /**
612  * Structure to represent \<nidrange\> token of the syntax.
613  *
614  * One of this is created for each \<net\> parsed.
615  */
616 struct nidrange {
617         /**
618          * Link to list of this structures which is built on nid range
619          * list parsing.
620          */
621         struct list_head nr_link;
622         /**
623          * List head for addrrange::ar_link.
624          */
625         struct list_head nr_addrranges;
626         /**
627          * Flag indicating that *@<net> is found.
628          */
629         int nr_all;
630         /**
631          * Pointer to corresponding element of libcfs_netstrfns.
632          */
633         struct netstrfns *nr_netstrfns;
634         /**
635          * Number of network. E.g. 5 if \<net\> is "elan5".
636          */
637         int nr_netnum;
638 };
639
640 /**
641  * Structure to represent \<addrrange\> token of the syntax.
642  */
643 struct addrrange {
644         /**
645          * Link to nidrange::nr_addrranges.
646          */
647         struct list_head ar_link;
648         /**
649          * List head for cfs_expr_list::el_list.
650          */
651         struct list_head ar_numaddr_ranges;
652 };
653
654 /**
655  * Nf_parse_addrlist method for networks using numeric addresses.
656  *
657  * Examples of such networks are gm and elan.
658  *
659  * \retval 0 if \a str parsed to numeric address
660  * \retval errno otherwise
661  */
662 static int
663 libcfs_num_parse(char *str, int len, struct list_head *list)
664 {
665         struct cfs_expr_list *el;
666         int     rc;
667
668         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
669         if (rc == 0)
670                 list_add_tail(&el->el_link, list);
671
672         return rc;
673 }
674
675 /**
676  * Parses \<addrrange\> token on the syntax.
677  *
678  * Allocates struct addrrange and links to \a nidrange via
679  * (nidrange::nr_addrranges)
680  *
681  * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
682  * \retval -errno otherwise
683  */
684 static int
685 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
686 {
687         struct addrrange *addrrange;
688
689         if (src->ls_len == 1 && src->ls_str[0] == '*') {
690                 nidrange->nr_all = 1;
691                 return 0;
692         }
693
694         LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
695         if (addrrange == NULL)
696                 return -ENOMEM;
697         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
698         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
699
700         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
701                                                 src->ls_len,
702                                                 &addrrange->ar_numaddr_ranges);
703 }
704
705 /**
706  * Finds or creates struct nidrange.
707  *
708  * Checks if \a src is a valid network name, looks for corresponding
709  * nidrange on the ist of nidranges (\a nidlist), creates new struct
710  * nidrange if it is not found.
711  *
712  * \retval pointer to struct nidrange matching network specified via \a src
713  * \retval NULL if \a src does not match any network
714  */
715 static struct nidrange *
716 add_nidrange(const struct cfs_lstr *src,
717              struct list_head *nidlist)
718 {
719         struct netstrfns *nf;
720         struct nidrange *nr;
721         int endlen;
722         unsigned netnum;
723
724         if (src->ls_len >= LNET_NIDSTR_SIZE)
725                 return NULL;
726
727         nf = libcfs_namenum2netstrfns(src->ls_str);
728         if (nf == NULL)
729                 return NULL;
730         endlen = src->ls_len - strlen(nf->nf_name);
731         if (endlen == 0)
732                 /* network name only, e.g. "elan" or "tcp" */
733                 netnum = 0;
734         else {
735                 /* e.g. "elan25" or "tcp23", refuse to parse if
736                  * network name is not appended with decimal or
737                  * hexadecimal number */
738                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
739                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
740                         return NULL;
741         }
742
743         list_for_each_entry(nr, nidlist, nr_link) {
744                 if (nr->nr_netstrfns != nf)
745                         continue;
746                 if (nr->nr_netnum != netnum)
747                         continue;
748                 return nr;
749         }
750
751         LIBCFS_ALLOC(nr, sizeof(struct nidrange));
752         if (nr == NULL)
753                 return NULL;
754         list_add_tail(&nr->nr_link, nidlist);
755         INIT_LIST_HEAD(&nr->nr_addrranges);
756         nr->nr_netstrfns = nf;
757         nr->nr_all = 0;
758         nr->nr_netnum = netnum;
759
760         return nr;
761 }
762
763 /**
764  * Parses \<nidrange\> token of the syntax.
765  *
766  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
767  * \retval 0 otherwise
768  */
769 static int
770 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
771 {
772         struct cfs_lstr addrrange;
773         struct cfs_lstr net;
774         struct cfs_lstr tmp;
775         struct nidrange *nr;
776
777         tmp = *src;
778         if (cfs_gettok(src, '@', &addrrange) == 0)
779                 goto failed;
780
781         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
782                 goto failed;
783
784         nr = add_nidrange(&net, nidlist);
785         if (nr == NULL)
786                 goto failed;
787
788         if (parse_addrange(&addrrange, nr) != 0)
789                 goto failed;
790
791         return 1;
792  failed:
793         CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
794         return 0;
795 }
796
797 /**
798  * Frees addrrange structures of \a list.
799  *
800  * For each struct addrrange structure found on \a list it frees
801  * cfs_expr_list list attached to it and frees the addrrange itself.
802  *
803  * \retval none
804  */
805 static void
806 free_addrranges(struct list_head *list)
807 {
808         while (!list_empty(list)) {
809                 struct addrrange *ar;
810
811                 ar = list_entry(list->next, struct addrrange, ar_link);
812
813                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
814                 list_del(&ar->ar_link);
815                 LIBCFS_FREE(ar, sizeof(struct addrrange));
816         }
817 }
818
819 /**
820  * Frees nidrange strutures of \a list.
821  *
822  * For each struct nidrange structure found on \a list it frees
823  * addrrange list attached to it and frees the nidrange itself.
824  *
825  * \retval none
826  */
827 void
828 cfs_free_nidlist(struct list_head *list)
829 {
830         struct list_head *pos, *next;
831         struct nidrange *nr;
832
833         list_for_each_safe(pos, next, list) {
834                 nr = list_entry(pos, struct nidrange, nr_link);
835                 free_addrranges(&nr->nr_addrranges);
836                 list_del(pos);
837                 LIBCFS_FREE(nr, sizeof(struct nidrange));
838         }
839 }
840
841 /**
842  * Parses nid range list.
843  *
844  * Parses with rigorous syntax and overflow checking \a str into
845  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
846  * structures and links that structure to \a nidlist. The resulting
847  * list can be used to match a NID againts set of NIDS defined by \a
848  * str.
849  * \see cfs_match_nid
850  *
851  * \retval 1 on success
852  * \retval 0 otherwise
853  */
854 int
855 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
856 {
857         struct cfs_lstr src;
858         struct cfs_lstr res;
859         int rc;
860         ENTRY;
861
862         src.ls_str = str;
863         src.ls_len = len;
864         INIT_LIST_HEAD(nidlist);
865         while (src.ls_str) {
866                 rc = cfs_gettok(&src, ' ', &res);
867                 if (rc == 0) {
868                         cfs_free_nidlist(nidlist);
869                         RETURN(0);
870                 }
871                 rc = parse_nidrange(&res, nidlist);
872                 if (rc == 0) {
873                         cfs_free_nidlist(nidlist);
874                         RETURN(0);
875                 }
876         }
877         RETURN(1);
878 }
879
880 /*
881  * Nf_match_addr method for networks using numeric addresses
882  *
883  * \retval 1 on match
884  * \retval 0 otherwise
885  */
886 static int
887 libcfs_num_match(__u32 addr, struct list_head *numaddr)
888 {
889         struct cfs_expr_list *el;
890
891         LASSERT(!list_empty(numaddr));
892         el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
893
894         return cfs_expr_list_match(addr, el);
895 }
896
897 /**
898  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
899  *
900  * \see cfs_parse_nidlist()
901  *
902  * \retval 1 on match
903  * \retval 0  otherwises
904  */
905 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
906 {
907         struct nidrange *nr;
908         struct addrrange *ar;
909         ENTRY;
910
911         list_for_each_entry(nr, nidlist, nr_link) {
912                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
913                         continue;
914                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
915                         continue;
916                 if (nr->nr_all)
917                         RETURN(1);
918                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
919                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
920                                                         &ar->ar_numaddr_ranges))
921                                 RETURN(1);
922         }
923         RETURN(0);
924 }
925
926 static int
927 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
928 {
929         int i = 0, j = 0;
930         struct cfs_expr_list *el;
931
932         list_for_each_entry(el, list, el_link) {
933                 LASSERT(j++ < 1);
934                 i += cfs_expr_list_print(buffer + i, count - i, el);
935         }
936         return i;
937 }
938
939 static int
940 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
941 {
942         int i = 0, j = 0;
943         struct cfs_expr_list *el;
944
945         list_for_each_entry(el, list, el_link) {
946                 LASSERT(j++ < 4);
947                 if (i != 0)
948                         i += cfs_snprintf(buffer + i, count - i, ".");
949                 i += cfs_expr_list_print(buffer + i, count - i, el);
950         }
951         return i;
952 }
953
954
955 /**
956  * Print the network part of the nidrange \a nr into the specified \a buffer.
957  *
958  * \retval number of characters written
959  */
960 static int
961 cfs_print_network(char *buffer, int count, struct nidrange *nr)
962 {
963         struct netstrfns *nf = nr->nr_netstrfns;
964
965         if (nr->nr_netnum == 0)
966                 return cfs_snprintf(buffer, count, "@%s", nf->nf_name);
967         else
968                 return cfs_snprintf(buffer, count, "@%s%u",
969                                     nf->nf_name, nr->nr_netnum);
970 }
971
972
973 /**
974  * Print a list of addrrange (\a addrranges) into the specified \a buffer.
975  * At max \a count characters can be printed into \a buffer.
976  *
977  * \retval number of characters written
978  */
979 static int
980 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
981                      struct nidrange *nr)
982 {
983         int i = 0;
984         struct addrrange *ar;
985         struct netstrfns *nf = nr->nr_netstrfns;
986
987         list_for_each_entry(ar, addrranges, ar_link) {
988                 if (i != 0)
989                         i += cfs_snprintf(buffer + i, count - i, " ");
990                 i += nf->nf_print_addrlist(buffer + i, count - i,
991                                            &ar->ar_numaddr_ranges);
992                 i += cfs_print_network(buffer + i, count - i, nr);
993         }
994         return i;
995 }
996
997
998 /**
999  * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1000  * At max \a count characters can be printed into \a buffer.
1001  * Nidranges are separated by a space character.
1002  *
1003  * \retval number of characters written
1004  */
1005 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1006 {
1007         int i = 0;
1008         struct nidrange *nr;
1009         ENTRY;
1010
1011         if (count <= 0)
1012                 RETURN(0);
1013
1014         list_for_each_entry(nr, nidlist, nr_link) {
1015                 if (i != 0)
1016                         i += cfs_snprintf(buffer + i, count - i, " ");
1017
1018                 if (nr->nr_all != 0) {
1019                         LASSERT(list_empty(&nr->nr_addrranges));
1020                         i += cfs_snprintf(buffer + i, count - i, "*");
1021                         i += cfs_print_network(buffer + i, count - i, nr);
1022                 } else {
1023                         i += cfs_print_addrranges(buffer + i, count - i,
1024                                                   &nr->nr_addrranges, nr);
1025                 }
1026         }
1027         RETURN(i);
1028 }
1029
1030 #ifdef __KERNEL__
1031
1032 EXPORT_SYMBOL(libcfs_isknown_lnd);
1033 EXPORT_SYMBOL(libcfs_lnd2modname);
1034 EXPORT_SYMBOL(libcfs_lnd2str);
1035 EXPORT_SYMBOL(libcfs_str2lnd);
1036 EXPORT_SYMBOL(libcfs_net2str);
1037 EXPORT_SYMBOL(libcfs_nid2str);
1038 EXPORT_SYMBOL(libcfs_str2net);
1039 EXPORT_SYMBOL(libcfs_str2nid);
1040 EXPORT_SYMBOL(libcfs_id2str);
1041 EXPORT_SYMBOL(libcfs_str2anynid);
1042 EXPORT_SYMBOL(cfs_free_nidlist);
1043 EXPORT_SYMBOL(cfs_parse_nidlist);
1044 EXPORT_SYMBOL(cfs_print_nidlist);
1045 EXPORT_SYMBOL(cfs_match_nid);
1046
1047 #endif