Whamcloud - gitweb
b=17471 change conf_param syntax to match set_param
[fs/lustre-release.git] / libcfs / libcfs / nidstrings.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/nidstrings.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 #define DEBUG_SUBSYSTEM S_LNET
46
47 #include <libcfs/libcfs.h>
48 #include <lnet/lnet.h>
49 #ifndef __KERNEL__
50 #ifdef HAVE_GETHOSTBYNAME
51 # include <netdb.h>
52 #endif
53 #endif
54
55 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
56  * consistent in all conversion functions.  Some code fragments are copied
57  * around for the sake of clarity...
58  */
59
60 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
61  * Choose the number of nidstrings to support the MAXIMUM expected number of
62  * concurrent users.  If there are more, the returned string will be volatile.
63  * NB this number must allow for a process to be descheduled for a timeslice
64  * between getting its string and using it.
65  */
66
67 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
68 static int       libcfs_nidstring_idx = 0;
69
70 #ifdef __KERNEL__
71 static cfs_spinlock_t libcfs_nidstring_lock;
72
73 void libcfs_init_nidstrings (void)
74 {
75         cfs_spin_lock_init(&libcfs_nidstring_lock);
76 }
77
78 # define NIDSTR_LOCK(f)   cfs_spin_lock_irqsave(&libcfs_nidstring_lock, f)
79 # define NIDSTR_UNLOCK(f) cfs_spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
80 #else
81 # define NIDSTR_LOCK(f)   (f=0)                 /* avoid unused var warnings */
82 # define NIDSTR_UNLOCK(f) (f=0)
83 #endif
84
85 static char *
86 libcfs_next_nidstring (void)
87 {
88         char          *str;
89         unsigned long  flags;
90
91         NIDSTR_LOCK(flags);
92
93         str = libcfs_nidstrings[libcfs_nidstring_idx++];
94         if (libcfs_nidstring_idx ==
95             sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
96                 libcfs_nidstring_idx = 0;
97
98         NIDSTR_UNLOCK(flags);
99         return str;
100 }
101
102 static int  libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
103 static void libcfs_ip_addr2str(__u32 addr, char *str);
104 static int  libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
105 static void libcfs_decnum_addr2str(__u32 addr, char *str);
106 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
107 static int  libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
108 static int  libcfs_ip_parse(char *str, int len, cfs_list_t *list);
109 static int  libcfs_num_parse(char *str, int len, cfs_list_t *list);
110 static int  libcfs_ip_match(__u32 addr, cfs_list_t *list);
111 static int  libcfs_num_match(__u32 addr, cfs_list_t *list);
112
113 struct netstrfns {
114         int          nf_type;
115         char        *nf_name;
116         char        *nf_modname;
117         void       (*nf_addr2str)(__u32 addr, char *str);
118         int        (*nf_str2addr)(const char *str, int nob, __u32 *addr);
119         int        (*nf_parse_addrlist)(char *str, int len,
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_match_addr*/  libcfs_num_match},
132         {/* .nf_type      */  SOCKLND,
133          /* .nf_name      */  "tcp",
134          /* .nf_modname   */  "ksocklnd",
135          /* .nf_addr2str  */  libcfs_ip_addr2str,
136          /* .nf_str2addr  */  libcfs_ip_str2addr,
137          /* .nf_parse_addrlist*/  libcfs_ip_parse,
138          /* .nf_match_addr*/  libcfs_ip_match},
139         {/* .nf_type      */  O2IBLND,
140          /* .nf_name      */  "o2ib",
141          /* .nf_modname   */  "ko2iblnd",
142          /* .nf_addr2str  */  libcfs_ip_addr2str,
143          /* .nf_str2addr  */  libcfs_ip_str2addr,
144          /* .nf_parse_addrlist*/  libcfs_ip_parse,
145          /* .nf_match_addr*/  libcfs_ip_match},
146         {/* .nf_type      */  CIBLND,
147          /* .nf_name      */  "cib",
148          /* .nf_modname   */  "kciblnd",
149          /* .nf_addr2str  */  libcfs_ip_addr2str,
150          /* .nf_str2addr  */  libcfs_ip_str2addr,
151          /* .nf_parse_addrlist*/  libcfs_ip_parse,
152          /* .nf_match_addr*/  libcfs_ip_match},
153         {/* .nf_type      */  OPENIBLND,
154          /* .nf_name      */  "openib",
155          /* .nf_modname   */  "kopeniblnd",
156          /* .nf_addr2str  */  libcfs_ip_addr2str,
157          /* .nf_str2addr  */  libcfs_ip_str2addr,
158          /* .nf_parse_addrlist*/  libcfs_ip_parse,
159          /* .nf_match_addr*/  libcfs_ip_match},
160         {/* .nf_type      */  IIBLND,
161          /* .nf_name      */  "iib",
162          /* .nf_modname   */  "kiiblnd",
163          /* .nf_addr2str  */  libcfs_ip_addr2str,
164          /* .nf_str2addr  */  libcfs_ip_str2addr,
165          /* .nf_parse_addrlist*/  libcfs_ip_parse,
166          /* .nf_match_addr*/  libcfs_ip_match},
167         {/* .nf_type      */  VIBLND,
168          /* .nf_name      */  "vib",
169          /* .nf_modname   */  "kviblnd",
170          /* .nf_addr2str  */  libcfs_ip_addr2str,
171          /* .nf_str2addr  */  libcfs_ip_str2addr,
172          /* .nf_parse_addrlist*/  libcfs_ip_parse,
173          /* .nf_match_addr*/  libcfs_ip_match},
174         {/* .nf_type      */  RALND,
175          /* .nf_name      */  "ra",
176          /* .nf_modname   */  "kralnd",
177          /* .nf_addr2str  */  libcfs_ip_addr2str,
178          /* .nf_str2addr  */  libcfs_ip_str2addr,
179          /* .nf_parse_addrlist*/  libcfs_ip_parse,
180          /* .nf_match_addr*/  libcfs_ip_match},
181         {/* .nf_type      */  QSWLND,
182          /* .nf_name      */  "elan",
183          /* .nf_modname   */  "kqswlnd",
184          /* .nf_addr2str  */  libcfs_decnum_addr2str,
185          /* .nf_str2addr  */  libcfs_num_str2addr,
186          /* .nf_parse_addrlist*/  libcfs_num_parse,
187          /* .nf_match_addr*/  libcfs_num_match},
188         {/* .nf_type      */  GMLND,
189          /* .nf_name      */  "gm",
190          /* .nf_modname   */  "kgmlnd",
191          /* .nf_addr2str  */  libcfs_hexnum_addr2str,
192          /* .nf_str2addr  */  libcfs_num_str2addr,
193          /* .nf_parse_addrlist*/  libcfs_num_parse,
194          /* .nf_match_addr*/  libcfs_num_match},
195         {/* .nf_type      */  MXLND,
196          /* .nf_name      */  "mx",
197          /* .nf_modname   */  "kmxlnd",
198          /* .nf_addr2str  */  libcfs_ip_addr2str,
199          /* .nf_str2addr  */  libcfs_ip_str2addr,
200          /* .nf_parse_addrlist*/  libcfs_ip_parse,
201          /* .nf_match_addr*/  libcfs_ip_match},
202         {/* .nf_type      */  PTLLND,
203          /* .nf_name      */  "ptl",
204          /* .nf_modname   */  "kptllnd",
205          /* .nf_addr2str  */  libcfs_decnum_addr2str,
206          /* .nf_str2addr  */  libcfs_num_str2addr,
207          /* .nf_parse_addrlist*/  libcfs_num_parse,
208          /* .nf_match_addr*/  libcfs_num_match},
209         /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
210         {/* .nf_type      */  -1},
211 };
212
213 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
214
215 int
216 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
217 {
218         *addr = 0;
219         return 1;
220 }
221
222 void
223 libcfs_ip_addr2str(__u32 addr, char *str)
224 {
225 #if 0   /* never lookup */
226 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
227         __u32           netip = htonl(addr);
228         struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
229
230         if (he != NULL) {
231                 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
232                 return;
233         }
234 #endif
235 #endif
236         snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
237                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
238                  (addr >> 8) & 0xff, addr & 0xff);
239 }
240
241 /* CAVEAT EMPTOR XscanfX
242  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
243  * sscanf may return immediately if it sees the terminating '0' in a string, so
244  * I initialise the %n variable to the expected length.  If sscanf sets it;
245  * fine, if it doesn't, then the scan ended at the end of the string, which is
246  * fine too :) */
247
248 int
249 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
250 {
251         int   a;
252         int   b;
253         int   c;
254         int   d;
255         int   n = nob;                          /* XscanfX */
256
257         /* numeric IP? */
258         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
259             n == nob &&
260             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
261             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
262                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
263                 return 1;
264         }
265
266 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
267         /* known hostname? */
268         if (('a' <= str[0] && str[0] <= 'z') ||
269             ('A' <= str[0] && str[0] <= 'Z')) {
270                 char *tmp;
271
272                 LIBCFS_ALLOC(tmp, nob + 1);
273                 if (tmp != NULL) {
274                         struct hostent *he;
275
276                         memcpy(tmp, str, nob);
277                         tmp[nob] = 0;
278
279                         he = gethostbyname(tmp);
280
281                         LIBCFS_FREE(tmp, nob);
282
283                         if (he != NULL) {
284                                 __u32 ip = *(__u32 *)he->h_addr;
285
286                                 *addr = ntohl(ip);
287                                 return 1;
288                         }
289                 }
290         }
291 #endif
292         return 0;
293 }
294
295 void
296 libcfs_decnum_addr2str(__u32 addr, char *str)
297 {
298         snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
299 }
300
301 void
302 libcfs_hexnum_addr2str(__u32 addr, char *str)
303 {
304         snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
305 }
306
307 int
308 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
309 {
310         int     n;
311
312         n = nob;
313         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
314                 return 1;
315
316         n = nob;
317         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
318                 return 1;
319
320         n = nob;
321         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
322                 return 1;
323
324         return 0;
325 }
326
327 struct netstrfns *
328 libcfs_lnd2netstrfns(int lnd)
329 {
330         int    i;
331
332         if (lnd >= 0)
333                 for (i = 0; i < libcfs_nnetstrfns; i++)
334                         if (lnd == libcfs_netstrfns[i].nf_type)
335                                 return &libcfs_netstrfns[i];
336
337         return NULL;
338 }
339
340 struct netstrfns *
341 libcfs_namenum2netstrfns(const char *name)
342 {
343         struct netstrfns *nf;
344         int               i;
345
346         for (i = 0; i < libcfs_nnetstrfns; i++) {
347                 nf = &libcfs_netstrfns[i];
348                 if (nf->nf_type >= 0 &&
349                     !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
350                         return nf;
351         }
352         return NULL;
353 }
354
355 struct netstrfns *
356 libcfs_name2netstrfns(const char *name)
357 {
358         int    i;
359
360         for (i = 0; i < libcfs_nnetstrfns; i++)
361                 if (libcfs_netstrfns[i].nf_type >= 0 &&
362                     !strcmp(libcfs_netstrfns[i].nf_name, name))
363                         return &libcfs_netstrfns[i];
364
365         return NULL;
366 }
367
368 int
369 libcfs_isknown_lnd(int type)
370 {
371         return libcfs_lnd2netstrfns(type) != NULL;
372 }
373
374 char *
375 libcfs_lnd2modname(int lnd)
376 {
377         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
378
379         return (nf == NULL) ? NULL : nf->nf_modname;
380 }
381
382 char *
383 libcfs_lnd2str(int lnd)
384 {
385         char           *str;
386         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
387
388         if (nf != NULL)
389                 return nf->nf_name;
390
391         str = libcfs_next_nidstring();
392         snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
393         return str;
394 }
395
396 int
397 libcfs_str2lnd(const char *str)
398 {
399         struct netstrfns *nf = libcfs_name2netstrfns(str);
400
401         if (nf != NULL)
402                 return nf->nf_type;
403
404         return -1;
405 }
406
407 char *
408 libcfs_net2str(__u32 net)
409 {
410         int               lnd = LNET_NETTYP(net);
411         int               num = LNET_NETNUM(net);
412         struct netstrfns *nf  = libcfs_lnd2netstrfns(lnd);
413         char             *str = libcfs_next_nidstring();
414
415         if (nf == NULL)
416                 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
417         else if (num == 0)
418                 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
419         else
420                 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
421
422         return str;
423 }
424
425 char *
426 libcfs_nid2str(lnet_nid_t nid)
427 {
428         __u32             addr = LNET_NIDADDR(nid);
429         __u32             net = LNET_NIDNET(nid);
430         int               lnd = LNET_NETTYP(net);
431         int               nnum = LNET_NETNUM(net);
432         struct netstrfns *nf;
433         char             *str;
434         int               nob;
435
436         if (nid == LNET_NID_ANY)
437                 return "LNET_NID_ANY";
438
439         nf = libcfs_lnd2netstrfns(lnd);
440         str = libcfs_next_nidstring();
441
442         if (nf == NULL)
443                 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
444         else {
445                 nf->nf_addr2str(addr, str);
446                 nob = strlen(str);
447                 if (nnum == 0)
448                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
449                                  nf->nf_name);
450                 else
451                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
452                                  nf->nf_name, nnum);
453         }
454
455         return str;
456 }
457
458 static struct netstrfns *
459 libcfs_str2net_internal(const char *str, __u32 *net)
460 {
461         struct netstrfns *nf;
462         int               nob;
463         int               netnum;
464         int               i;
465
466         for (i = 0; i < libcfs_nnetstrfns; i++) {
467                 nf = &libcfs_netstrfns[i];
468                 if (nf->nf_type >= 0 &&
469                     !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
470                         break;
471         }
472
473         if (i == libcfs_nnetstrfns)
474                 return NULL;
475
476         nob = strlen(nf->nf_name);
477
478         if (strlen(str) == (unsigned int)nob) {
479                 netnum = 0;
480         } else {
481                 if (nf->nf_type == LOLND) /* net number not allowed */
482                         return NULL;
483
484                 str += nob;
485                 i = strlen(str);
486                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
487                     i != (int)strlen(str))
488                         return NULL;
489         }
490
491         *net = LNET_MKNET(nf->nf_type, netnum);
492         return nf;
493 }
494
495 __u32
496 libcfs_str2net(const char *str)
497 {
498         __u32  net;
499
500         if (libcfs_str2net_internal(str, &net) != NULL)
501                 return net;
502
503         return LNET_NIDNET(LNET_NID_ANY);
504 }
505
506 lnet_nid_t
507 libcfs_str2nid(const char *str)
508 {
509         const char       *sep = strchr(str, '@');
510         struct netstrfns *nf;
511         __u32             net;
512         __u32             addr;
513
514         if (sep != NULL) {
515                 nf = libcfs_str2net_internal(sep + 1, &net);
516                 if (nf == NULL)
517                         return LNET_NID_ANY;
518         } else {
519                 sep = str + strlen(str);
520                 net = LNET_MKNET(SOCKLND, 0);
521                 nf = libcfs_lnd2netstrfns(SOCKLND);
522                 LASSERT (nf != NULL);
523         }
524
525         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
526                 return LNET_NID_ANY;
527
528         return LNET_MKNID(net, addr);
529 }
530
531 char *
532 libcfs_id2str(lnet_process_id_t id)
533 {
534         char *str = libcfs_next_nidstring();
535
536         if (id.pid == LNET_PID_ANY) {
537                 snprintf(str, LNET_NIDSTR_SIZE,
538                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
539                 return str;
540         }
541
542         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
543                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
544                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
545         return str;
546 }
547
548 int
549 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
550 {
551         if (!strcmp(str, "*")) {
552                 *nidp = LNET_NID_ANY;
553                 return 1;
554         }
555
556         *nidp = libcfs_str2nid(str);
557         return *nidp != LNET_NID_ANY;
558 }
559
560 /* parse server details from name */
561 int libcfs_str2server(char *name, int *type, __u32 *idx, char **endptr)
562 {
563         char *ptr;
564         int i;
565
566         ptr = strstr(name, "-MDT");
567         if (ptr) {
568                 *type = SVTYPE_MDT;
569         } else {
570                 ptr = strstr(name, "-OST");
571                 if (ptr)
572                         *type = SVTYPE_OST;
573                 else
574                         return -EINVAL;
575         }
576         ptr += 4;
577
578         if (strncmp(ptr, "all", 3) == 0) {
579                 ptr += 3;
580                 *type |= SVTYPE_ALL;
581                 goto out;
582         }
583         if (*ptr == '*') {
584                 ptr++;
585                 *type |= SVTYPE_ALL;
586                 goto out;
587         }
588
589 #if __KERNEL__
590         *idx = simple_strtoul(ptr, NULL, 16);
591 #else
592         *idx = strtoul(ptr, NULL, 16);
593 #endif
594         /* Require 4 hex digits */
595         for (i = 0; i < 4; i++)
596                 if (!isxdigit(*ptr++))
597                         return -EINVAL;
598 out:
599         /* Only acceptable garbage at the end of name is [-.:,] etc. */
600         if (isalnum(*ptr))
601                 return -EINVAL;
602
603         if (endptr)
604                 *endptr = ptr;
605         return 0;
606 }
607
608 /**
609  * Nid range list syntax.
610  * \verbatim
611  *
612  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
613  * <nidrange>        :== <addrrange> '@' <net>
614  * <addrrange>       :== '*' |
615  *                       <ipaddr_range> |
616  *                       <numaddr_range>
617  * <ipaddr_range>    :== <numaddr_range>.<numaddr_range>.<numaddr_range>.
618  *                       <numaddr_range>
619  * <numaddr_range>   :== <number> |
620  *                       <expr_list>
621  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
622  * <range_expr>      :== <number> |
623  *                       <number> '-' <number> |
624  *                       <number> '-' <number> '/' <number>
625  * <net>             :== <netname> | <netname><number>
626  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
627  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
628  * \endverbatim
629  */
630
631 /**
632  * Structure to represent NULL-less strings.
633  */
634 struct lstr {
635         char *ls_str;
636         int ls_len;
637 };
638
639 /**
640  * Structure to represent \<nidrange\> token of the syntax.
641  *
642  * One of this is created for each \<net\> parsed.
643  */
644 struct nidrange {
645         /**
646          * Link to list of this structures which is built on nid range
647          * list parsing.
648          */
649         cfs_list_t nr_link;
650         /**
651          * List head for addrrange::ar_link.
652          */
653         cfs_list_t nr_addrranges;
654         /**
655          * Flag indicating that *@<net> is found.
656          */
657         int nr_all;
658         /**
659          * Pointer to corresponding element of libcfs_netstrfns.
660          */
661         struct netstrfns *nr_netstrfns;
662         /**
663          * Number of network. E.g. 5 if \<net\> is "elan5".
664          */
665         int nr_netnum;
666 };
667
668 /**
669  * Structure to represent \<addrrange\> token of the syntax.
670  */
671 struct addrrange {
672         /**
673          * Link to nidrange::nr_addrranges.
674          */
675         cfs_list_t ar_link;
676         /**
677          * List head for numaddr_range::nar_link.
678          */
679         cfs_list_t ar_numaddr_ranges;
680 };
681
682 /**
683  * Structure to represent \<numaddr_range\> token of the syntax.
684  */
685 struct numaddr_range {
686         /**
687          * Link to addrrange::ar_numaddr_ranges.
688          */
689         cfs_list_t nar_link;
690         /**
691          * List head for range_expr::re_link.
692          */
693         cfs_list_t nar_range_exprs;
694 };
695
696 /**
697  * Structure to represent \<range_expr\> token of the syntax.
698  */
699 struct range_expr {
700         /**
701          * Link to numaddr_range::nar_range_exprs.
702          */
703         cfs_list_t re_link;
704         __u32 re_lo;
705         __u32 re_hi;
706         __u32 re_stride;
707 };
708
709 int
710 cfs_iswhite(char c)
711 {
712         switch (c) {
713         case ' ':
714         case '\t':
715         case '\n':
716         case '\r':
717                 return 1;
718         default:
719                 break;
720         }
721         return 0;
722 }
723
724 /*
725  * Extracts tokens from strings.
726  *
727  * Looks for \a delim in string \a next, sets \a res to point to
728  * substring before the delimiter, sets \a next right after the found
729  * delimiter.
730  *
731  * \retval 1 if \a res points to a string of non-whitespace characters
732  * \retval 0 otherwise
733  */
734 static int
735 gettok(struct lstr *next, char delim, struct lstr *res)
736 {
737         char *end;
738
739         if (next->ls_str == NULL)
740                 return 0;
741
742         /* skip leading white spaces */
743         while (next->ls_len) {
744                 if (!cfs_iswhite(*next->ls_str))
745                         break;
746                 next->ls_str ++;
747                 next->ls_len --;
748         }
749         if (next->ls_len == 0)
750                 /* whitespaces only */
751                 return 0;
752
753         if (*next->ls_str == delim)
754                 /* first non-writespace is the delimiter */
755                 return 0;
756
757         res->ls_str = next->ls_str;
758         end = memchr(next->ls_str, delim, next->ls_len);
759         if (end == NULL) {
760                 /* there is no the delimeter in the string */
761                 end = next->ls_str + next->ls_len;
762                 next->ls_str = NULL;
763         } else {
764                 next->ls_str = end + 1;
765                 next->ls_len -= (end - res->ls_str + 1);
766         }
767
768         /* skip ending whitespaces */
769         while (--end != res->ls_str)
770                 if (!cfs_iswhite(*end))
771                         break;
772
773         res->ls_len = end - res->ls_str + 1;
774         return 1;
775 }
776
777 /**
778  * Converts string to integer.
779  *
780  * Accepts decimal and hexadecimal number recordings.
781  *
782  * \retval 1 if first \a nob chars of \a str convert to decimal or
783  * hexadecimal integer in the range [\a min, \a max]
784  * \retval 0 otherwise
785  */
786 static int
787 libcfs_str2num_check(const char *str, int nob, unsigned *num,
788                      unsigned min, unsigned max)
789 {
790         int n;
791         char nstr[12];
792
793         n = nob;
794         if (sscanf(str, "%u%n", num, &n) != 1 || n != nob)
795                 if (sscanf(str, "0x%x%n", num, &n) != 1 || n != nob)
796                         if (sscanf(str, "0X%x%n", num, &n) != 1 || n != nob)
797                                 return 0;
798         sprintf(nstr, "%u", *num);
799         if (n != strlen(nstr) || memcmp(nstr, str, n)) {
800                 sprintf(nstr, "0x%x", *num);
801                 if (n != strlen(nstr) || memcmp(nstr, str, n)) {
802                         sprintf(nstr, "0X%x", *num);
803                         if (n != strlen(nstr) || memcmp(nstr, str, n))
804                                 return 0;
805                 }
806         }
807         if (*num < min || *num > max)
808                 return 0;
809         return 1;
810 }
811
812 /**
813  * Parses \<range_expr\> token of the syntax.
814  *
815  * \retval pointer to allocated range_expr and initialized
816  * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
817  `* src parses to
818  * \<number\> |
819  * \<number\> '-' \<number\> |
820  * \<number\> '-' \<number\> '/' \<number\>
821  * \retval NULL othersize
822  */
823 static struct range_expr *
824 parse_range_expr(struct lstr *src, unsigned min, unsigned max)
825 {
826         struct lstr tok;
827         struct range_expr *expr;
828
829         LIBCFS_ALLOC(expr, sizeof(struct range_expr));
830         if (expr == NULL)
831                 return NULL;
832
833         if (libcfs_str2num_check(src->ls_str, src->ls_len, &expr->re_lo,
834                                  min, max)) {
835                 /* <number> is parsed */
836                 expr->re_hi = expr->re_lo;
837                 expr->re_stride = 1;
838                 return expr;
839         }
840
841         if (!gettok(src, '-', &tok))
842                 goto failed;
843         if (!libcfs_str2num_check(tok.ls_str, tok.ls_len, &expr->re_lo,
844                                   min, max))
845                 goto failed;
846         /* <number> - */
847         if (libcfs_str2num_check(src->ls_str, src->ls_len, &expr->re_hi,
848                                  min, max)) {
849                 /* <number> - <number> is parsed */
850                 expr->re_stride = 1;
851                 return expr;
852         }
853
854         /* go to check <number> '-' <number> '/' <number> */
855         if (gettok(src, '/', &tok)) {
856                 if (!libcfs_str2num_check(tok.ls_str, tok.ls_len,
857                                           &expr->re_hi, min, max))
858                         goto failed;
859                 /* <number> - <number> / ... */
860                 if (libcfs_str2num_check(src->ls_str, src->ls_len,
861                                          &expr->re_stride, min, max))
862                         /* <number> - <number> / <number> is parsed */
863                         return expr;
864         }
865
866 failed:
867         LIBCFS_FREE(expr, sizeof(struct range_expr));
868         return NULL;
869 }
870
871 /**
872  * Parses \<expr_list\> token of the syntax.
873  *
874  * \retval 1 if \a str parses to '[' \<range_expr\> [ ',' \<range_expr\>] ']'
875  * \retval 0 otherwise
876  */
877 static int
878 parse_expr_list(struct lstr *str, cfs_list_t *list,
879                 unsigned min, unsigned max)
880 {
881         struct lstr res;
882         struct range_expr *range;
883
884         if (str->ls_str[0] != '[' || str->ls_str[str->ls_len - 1] != ']')
885                 return 0;
886         str->ls_str ++;
887         str->ls_len -= 2;
888
889         while (str->ls_str) {
890                 if (gettok(str, ',', &res) == 0)
891                         return 0;
892                 range = parse_range_expr(&res, min, max);
893                 if (range == NULL)
894                         return 0;
895                 cfs_list_add_tail(&range->re_link, list);
896         }
897         return 1;
898 }
899
900 /**
901  * Parses \<numaddr_range\> token of the syntax.
902  *
903  * \retval 1 if \a str parses to \<number\> | \<expr_list\>
904  * \retval 0 otherwise
905  */
906 static int
907 num_parse(char *str, int len,
908           cfs_list_t *list, unsigned min, unsigned max)
909 {
910         __u32 num;
911         struct lstr src;
912         struct numaddr_range *numaddr;
913
914         src.ls_str = str;
915         src.ls_len = len;
916
917         LIBCFS_ALLOC(numaddr, sizeof(struct numaddr_range));
918         if (numaddr == NULL)
919                 return 0;
920         cfs_list_add_tail(&numaddr->nar_link, list);
921         CFS_INIT_LIST_HEAD(&numaddr->nar_range_exprs);
922
923         if (libcfs_str2num_check(src.ls_str, src.ls_len, &num, min, max)) {
924                 /* <number> */
925                 struct range_expr *expr;
926
927                 LIBCFS_ALLOC(expr, sizeof(struct range_expr));
928                 if (expr == NULL)
929                         return 0;
930
931                 expr->re_lo = expr->re_hi = num;
932                 expr->re_stride = 1;
933                 cfs_list_add_tail(&expr->re_link, &numaddr->nar_range_exprs);
934                 return 1;
935         }
936
937         return parse_expr_list(&src, &numaddr->nar_range_exprs, min, max);
938 }
939
940 /**
941  * Nf_parse_addrlist method for networks using numeric addresses.
942  *
943  * Examples of such networks are gm and elan.
944  *
945  * \retval 1 if \a str parsed to numeric address
946  * \retval 0 otherwise
947  */
948 static int
949 libcfs_num_parse(char *str, int len, cfs_list_t *list)
950 {
951         return num_parse(str, len, list, 0, MAX_NUMERIC_VALUE);
952 }
953
954 /**
955  * Nf_parse_addrlist method for networks using ip addresses.
956  *
957  * Examples of such networks are tcp and o2ib.
958  *
959  * \retval 1 if \a str parsed to ip address
960  * \retval 0 otherwise
961  */
962 static int
963 libcfs_ip_parse(char *str, int len,
964                 cfs_list_t *list)
965 {
966         struct lstr src, res;
967         int i;
968
969         src.ls_str = str;
970         src.ls_len = len;
971         i = 0;
972         while (src.ls_str) {
973                 if (gettok(&src, '.', &res) == 0)
974                         return 0;
975                 if (!num_parse(res.ls_str, res.ls_len, list, 0, 255))
976                         return 0;
977                 i ++;
978         }
979
980         return (i == 4) ? 1 : 0;
981 }
982
983 /**
984  * Parses \<addrrange\> token on the syntax.
985  *
986  * Allocates struct addrrange and links to \a nidrange via
987  * (nidrange::nr_addrranges)
988  *
989  * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<numaddr_range\>
990  * \retval 0 otherwise
991  */
992 static int
993 parse_addrange(const struct lstr *src, struct nidrange *nidrange)
994 {
995         struct addrrange *addrrange;
996
997         if (src->ls_len == 1 && src->ls_str[0] == '*') {
998                 nidrange->nr_all = 1;
999                 return 1;
1000         }
1001
1002         LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
1003         if (addrrange == NULL)
1004                 return 0;
1005         cfs_list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
1006         CFS_INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
1007
1008         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
1009                                                 src->ls_len,
1010                                                 &addrrange->ar_numaddr_ranges);
1011 }
1012
1013 /**
1014  * Finds or creates struct nidrange.
1015  *
1016  * Checks if \a src is a valid network name, looks for corresponding
1017  * nidrange on the ist of nidranges (\a nidlist), creates new struct
1018  * nidrange if it is not found.
1019  *
1020  * \retval pointer to struct nidrange matching network specified via \a src
1021  * \retval NULL if \a src does not match any network
1022  */
1023 static struct nidrange *
1024 add_nidrange(const struct lstr *src,
1025              cfs_list_t *nidlist)
1026 {
1027         struct netstrfns *nf;
1028         struct nidrange *nr;
1029         int endlen;
1030         unsigned netnum;
1031
1032         if (src->ls_len >= LNET_NIDSTR_SIZE)
1033                 return NULL;
1034
1035         nf = libcfs_namenum2netstrfns(src->ls_str);
1036         if (nf == NULL)
1037                 return NULL;
1038         endlen = src->ls_len - strlen(nf->nf_name);
1039         if (endlen == 0)
1040                 /* network name only, e.g. "elan" or "tcp" */
1041                 netnum = 0;
1042         else {
1043                 /* e.g. "elan25" or "tcp23", refuse to parse if
1044                  * network name is not appended with decimal or
1045                  * hexadecimal number */
1046                 if (!libcfs_str2num_check(src->ls_str + strlen(nf->nf_name),
1047                                           endlen, &netnum,
1048                                           0, MAX_NUMERIC_VALUE))
1049                         return NULL;
1050         }
1051
1052         cfs_list_for_each_entry(nr, nidlist, nr_link) {
1053                 if (nr->nr_netstrfns != nf)
1054                         continue;
1055                 if (nr->nr_netnum != netnum)
1056                         continue;
1057                 return nr;
1058         }
1059
1060         LIBCFS_ALLOC(nr, sizeof(struct nidrange));
1061         if (nr == NULL)
1062                 return NULL;
1063         cfs_list_add_tail(&nr->nr_link, nidlist);
1064         CFS_INIT_LIST_HEAD(&nr->nr_addrranges);
1065         nr->nr_netstrfns = nf;
1066         nr->nr_all = 0;
1067         nr->nr_netnum = netnum;
1068
1069         return nr;
1070 }
1071
1072 /**
1073  * Parses \<nidrange\> token of the syntax.
1074  *
1075  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
1076  * \retval 0 otherwise
1077  */
1078 static int
1079 parse_nidrange(struct lstr *src, cfs_list_t *nidlist)
1080 {
1081         struct lstr addrrange, net, tmp;
1082         struct nidrange *nr;
1083
1084         tmp = *src;
1085         if (gettok(src, '@', &addrrange) == 0)
1086                 goto failed;
1087
1088         if (gettok(src, '@', &net) == 0 || src->ls_str != NULL)
1089                 goto failed;
1090
1091         nr = add_nidrange(&net, nidlist);
1092         if (nr == NULL)
1093                 goto failed;
1094
1095         if (!parse_addrange(&addrrange, nr))
1096                 goto failed;
1097
1098         return 1;
1099  failed:
1100         CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
1101         return 0;
1102 }
1103
1104 /**
1105  * Frees range_expr structures of \a list.
1106  *
1107  * \retval none
1108  */
1109 static void
1110 free_range_exprs(cfs_list_t *list)
1111 {
1112         cfs_list_t *pos, *next;
1113
1114         cfs_list_for_each_safe(pos, next, list) {
1115                 cfs_list_del(pos);
1116                 LIBCFS_FREE(cfs_list_entry(pos, struct range_expr, re_link),
1117                             sizeof(struct range_expr));
1118         }
1119 }
1120
1121 /**
1122  * Frees numaddr_range structures of \a list.
1123  *
1124  * For each struct numaddr_range structure found on \a list it frees
1125  * range_expr list attached to it and frees the numddr_range itself.
1126  *
1127  * \retval none
1128  */
1129 static void
1130 free_numaddr_ranges(cfs_list_t *list)
1131 {
1132         cfs_list_t *pos, *next;
1133         struct numaddr_range *numaddr;
1134
1135         cfs_list_for_each_safe(pos, next, list) {
1136                 numaddr = cfs_list_entry(pos, struct numaddr_range, nar_link);
1137                 free_range_exprs(&numaddr->nar_range_exprs);
1138                 cfs_list_del(pos);
1139                 LIBCFS_FREE(numaddr, sizeof(struct numaddr_range));
1140         }
1141 }
1142
1143 /**
1144  * Frees addrrange structures of \a list.
1145  *
1146  * For each struct addrrange structure found on \a list it frees
1147  * numaddr_range list attached to it and frees the addrrange itself.
1148  *
1149  * \retval none
1150  */
1151 static void
1152 free_addrranges(cfs_list_t *list)
1153 {
1154         cfs_list_t *pos, *next;
1155         struct addrrange *ar;
1156
1157         cfs_list_for_each_safe(pos, next, list) {
1158                 ar = cfs_list_entry(pos, struct addrrange, ar_link);
1159                 free_numaddr_ranges(&ar->ar_numaddr_ranges);
1160                 cfs_list_del(pos);
1161                 LIBCFS_FREE(ar, sizeof(struct addrrange));
1162         }
1163 }
1164
1165 /**
1166  * Frees nidrange strutures of \a list.
1167  *
1168  * For each struct nidrange structure found on \a list it frees
1169  * addrrange list attached to it and frees the nidrange itself.
1170  *
1171  * \retval none
1172  */
1173 void
1174 cfs_free_nidlist(cfs_list_t *list)
1175 {
1176         cfs_list_t *pos, *next;
1177         struct nidrange *nr;
1178
1179         cfs_list_for_each_safe(pos, next, list) {
1180                 nr = cfs_list_entry(pos, struct nidrange, nr_link);
1181                 free_addrranges(&nr->nr_addrranges);
1182                 cfs_list_del(pos);
1183                 LIBCFS_FREE(nr, sizeof(struct nidrange));
1184         }
1185 }
1186
1187 /**
1188  * Parses nid range list.
1189  *
1190  * Parses with rigorous syntax and overflow checking \a str into
1191  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
1192  * structures and links that structure to \a nidlist. The resulting
1193  * list can be used to match a NID againts set of NIDS defined by \a
1194  * str.
1195  * \see cfs_match_nid
1196  *
1197  * \retval 1 on success
1198  * \retval 0 otherwise
1199  */
1200 int
1201 cfs_parse_nidlist(char *str, int len, cfs_list_t *nidlist)
1202 {
1203         struct lstr src, res;
1204         int rc;
1205         ENTRY;
1206
1207         src.ls_str = str;
1208         src.ls_len = len;
1209         CFS_INIT_LIST_HEAD(nidlist);
1210         while (src.ls_str) {
1211                 rc = gettok(&src, ' ', &res);
1212                 if (rc == 0) {
1213                         cfs_free_nidlist(nidlist);
1214                         RETURN(0);
1215                 }
1216                 rc = parse_nidrange(&res, nidlist);
1217                 if (rc == 0) {
1218                         cfs_free_nidlist(nidlist);
1219                         RETURN(0);
1220                 }
1221         }
1222         RETURN(1);
1223 }
1224
1225 /**
1226  * Matches address (\a addr) against address set encoded in \a list.
1227  *
1228  * \see libcfs_num_match(), libcfs_ip_match()
1229  *
1230  * \retval 1 if \a addr matches
1231  * \retval 0 otherwise
1232  */
1233 static int
1234 match_numaddr(__u32 addr, cfs_list_t *list, int shift, __u32 mask)
1235 {
1236         struct numaddr_range *numaddr;
1237         struct range_expr *expr;
1238         int ip, ok;
1239         ENTRY;
1240
1241         cfs_list_for_each_entry(numaddr, list, nar_link) {
1242                 ip = (addr >> shift) & mask;
1243                 shift -= 8;
1244                 ok = 0;
1245                 cfs_list_for_each_entry(expr, &numaddr->nar_range_exprs,
1246                                         re_link) {
1247                         if (ip >= expr->re_lo &&
1248                             ip <= expr->re_hi &&
1249                             ((ip - expr->re_lo) % expr->re_stride) == 0) {
1250                                 ok = 1;
1251                                 break;
1252                         }
1253                 }
1254                 if (!ok)
1255                         RETURN(0);
1256         }
1257         RETURN(1);
1258 }
1259
1260 /*
1261  * Nf_match_addr method for networks using numeric addresses
1262  *
1263  * \retval 1 on match
1264  * \retval 0 otherwise
1265  */
1266 static int
1267 libcfs_num_match(__u32 addr, cfs_list_t *numaddr)
1268 {
1269         return match_numaddr(addr, numaddr, 0, 0xffffffff);
1270 }
1271
1272 /*
1273  * Nf_match_addr method for networks using ip addresses
1274  *
1275  * \retval 1 on match
1276  * \retval 0 otherwise
1277  */
1278 static int
1279 libcfs_ip_match(__u32 addr, cfs_list_t *numaddr)
1280 {
1281         return match_numaddr(addr, numaddr, 24, 0xff);
1282 }
1283
1284 /**
1285  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1286  *
1287  * \see cfs_parse_nidlist()
1288  *
1289  * \retval 1 on match
1290  * \retval 0  otherwises
1291  */
1292 int cfs_match_nid(lnet_nid_t nid, cfs_list_t *nidlist)
1293 {
1294         struct nidrange *nr;
1295         struct addrrange *ar;
1296         ENTRY;
1297
1298         cfs_list_for_each_entry(nr, nidlist, nr_link) {
1299                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1300                         continue;
1301                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1302                         continue;
1303                 if (nr->nr_all)
1304                         RETURN(1);
1305                 cfs_list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1306                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1307                                                        &ar->ar_numaddr_ranges))
1308                                 RETURN(1);
1309         }
1310         RETURN(0);
1311 }
1312
1313 #ifdef __KERNEL__
1314
1315 EXPORT_SYMBOL(libcfs_isknown_lnd);
1316 EXPORT_SYMBOL(libcfs_lnd2modname);
1317 EXPORT_SYMBOL(libcfs_lnd2str);
1318 EXPORT_SYMBOL(libcfs_str2lnd);
1319 EXPORT_SYMBOL(libcfs_net2str);
1320 EXPORT_SYMBOL(libcfs_nid2str);
1321 EXPORT_SYMBOL(libcfs_str2net);
1322 EXPORT_SYMBOL(libcfs_str2nid);
1323 EXPORT_SYMBOL(libcfs_id2str);
1324 EXPORT_SYMBOL(libcfs_str2anynid);
1325 EXPORT_SYMBOL(libcfs_str2server);
1326 EXPORT_SYMBOL(cfs_iswhite);
1327 EXPORT_SYMBOL(cfs_free_nidlist);
1328 EXPORT_SYMBOL(cfs_parse_nidlist);
1329 EXPORT_SYMBOL(cfs_match_nid);
1330
1331 #endif