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