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