]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/libcfs/nidstrings.c
Merge branch 'timers/core' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[~andy/linux] / drivers / staging / lustre / lustre / 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 <linux/libcfs/libcfs.h>
44 #include <linux/lnet/lnet.h>
45
46 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
47  * consistent in all conversion functions.  Some code fragments are copied
48  * around for the sake of clarity...
49  */
50
51 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
52  * Choose the number of nidstrings to support the MAXIMUM expected number of
53  * concurrent users.  If there are more, the returned string will be volatile.
54  * NB this number must allow for a process to be descheduled for a timeslice
55  * between getting its string and using it.
56  */
57
58 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
59 static int       libcfs_nidstring_idx = 0;
60
61 static spinlock_t libcfs_nidstring_lock;
62
63 void libcfs_init_nidstrings (void)
64 {
65         spin_lock_init(&libcfs_nidstring_lock);
66 }
67
68 # define NIDSTR_LOCK(f)   spin_lock_irqsave(&libcfs_nidstring_lock, f)
69 # define NIDSTR_UNLOCK(f) spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
70
71 static char *
72 libcfs_next_nidstring (void)
73 {
74         char      *str;
75         unsigned long  flags;
76
77         NIDSTR_LOCK(flags);
78
79         str = libcfs_nidstrings[libcfs_nidstring_idx++];
80         if (libcfs_nidstring_idx ==
81             sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
82                 libcfs_nidstring_idx = 0;
83
84         NIDSTR_UNLOCK(flags);
85         return str;
86 }
87
88 static int  libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
89 static void libcfs_ip_addr2str(__u32 addr, char *str);
90 static int  libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
91 static void libcfs_decnum_addr2str(__u32 addr, char *str);
92 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
93 static int  libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
94 static int  libcfs_num_parse(char *str, int len, struct list_head *list);
95 static int  libcfs_num_match(__u32 addr, struct list_head *list);
96
97 struct netstrfns {
98         int       nf_type;
99         char    *nf_name;
100         char    *nf_modname;
101         void       (*nf_addr2str)(__u32 addr, char *str);
102         int     (*nf_str2addr)(const char *str, int nob, __u32 *addr);
103         int     (*nf_parse_addrlist)(char *str, int len,
104                                         struct list_head *list);
105         int     (*nf_match_addr)(__u32 addr, struct list_head *list);
106 };
107
108 static struct netstrfns  libcfs_netstrfns[] = {
109         {/* .nf_type      */  LOLND,
110          /* .nf_name      */  "lo",
111          /* .nf_modname   */  "klolnd",
112          /* .nf_addr2str  */  libcfs_decnum_addr2str,
113          /* .nf_str2addr  */  libcfs_lo_str2addr,
114          /* .nf_parse_addr*/  libcfs_num_parse,
115          /* .nf_match_addr*/  libcfs_num_match},
116         {/* .nf_type      */  SOCKLND,
117          /* .nf_name      */  "tcp",
118          /* .nf_modname   */  "ksocklnd",
119          /* .nf_addr2str  */  libcfs_ip_addr2str,
120          /* .nf_str2addr  */  libcfs_ip_str2addr,
121          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
122          /* .nf_match_addr*/  cfs_ip_addr_match},
123         {/* .nf_type      */  O2IBLND,
124          /* .nf_name      */  "o2ib",
125          /* .nf_modname   */  "ko2iblnd",
126          /* .nf_addr2str  */  libcfs_ip_addr2str,
127          /* .nf_str2addr  */  libcfs_ip_str2addr,
128          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
129          /* .nf_match_addr*/  cfs_ip_addr_match},
130         {/* .nf_type      */  CIBLND,
131          /* .nf_name      */  "cib",
132          /* .nf_modname   */  "kciblnd",
133          /* .nf_addr2str  */  libcfs_ip_addr2str,
134          /* .nf_str2addr  */  libcfs_ip_str2addr,
135          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
136          /* .nf_match_addr*/  cfs_ip_addr_match},
137         {/* .nf_type      */  OPENIBLND,
138          /* .nf_name      */  "openib",
139          /* .nf_modname   */  "kopeniblnd",
140          /* .nf_addr2str  */  libcfs_ip_addr2str,
141          /* .nf_str2addr  */  libcfs_ip_str2addr,
142          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
143          /* .nf_match_addr*/  cfs_ip_addr_match},
144         {/* .nf_type      */  IIBLND,
145          /* .nf_name      */  "iib",
146          /* .nf_modname   */  "kiiblnd",
147          /* .nf_addr2str  */  libcfs_ip_addr2str,
148          /* .nf_str2addr  */  libcfs_ip_str2addr,
149          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
150          /* .nf_match_addr*/  cfs_ip_addr_match},
151         {/* .nf_type      */  VIBLND,
152          /* .nf_name      */  "vib",
153          /* .nf_modname   */  "kviblnd",
154          /* .nf_addr2str  */  libcfs_ip_addr2str,
155          /* .nf_str2addr  */  libcfs_ip_str2addr,
156          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
157          /* .nf_match_addr*/  cfs_ip_addr_match},
158         {/* .nf_type      */  RALND,
159          /* .nf_name      */  "ra",
160          /* .nf_modname   */  "kralnd",
161          /* .nf_addr2str  */  libcfs_ip_addr2str,
162          /* .nf_str2addr  */  libcfs_ip_str2addr,
163          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
164          /* .nf_match_addr*/  cfs_ip_addr_match},
165         {/* .nf_type      */  QSWLND,
166          /* .nf_name      */  "elan",
167          /* .nf_modname   */  "kqswlnd",
168          /* .nf_addr2str  */  libcfs_decnum_addr2str,
169          /* .nf_str2addr  */  libcfs_num_str2addr,
170          /* .nf_parse_addrlist*/  libcfs_num_parse,
171          /* .nf_match_addr*/  libcfs_num_match},
172         {/* .nf_type      */  GMLND,
173          /* .nf_name      */  "gm",
174          /* .nf_modname   */  "kgmlnd",
175          /* .nf_addr2str  */  libcfs_hexnum_addr2str,
176          /* .nf_str2addr  */  libcfs_num_str2addr,
177          /* .nf_parse_addrlist*/  libcfs_num_parse,
178          /* .nf_match_addr*/  libcfs_num_match},
179         {/* .nf_type      */  MXLND,
180          /* .nf_name      */  "mx",
181          /* .nf_modname   */  "kmxlnd",
182          /* .nf_addr2str  */  libcfs_ip_addr2str,
183          /* .nf_str2addr  */  libcfs_ip_str2addr,
184          /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
185          /* .nf_match_addr*/  cfs_ip_addr_match},
186         {/* .nf_type      */  PTLLND,
187          /* .nf_name      */  "ptl",
188          /* .nf_modname   */  "kptllnd",
189          /* .nf_addr2str  */  libcfs_decnum_addr2str,
190          /* .nf_str2addr  */  libcfs_num_str2addr,
191          /* .nf_parse_addrlist*/  libcfs_num_parse,
192          /* .nf_match_addr*/  libcfs_num_match},
193         {/* .nf_type      */  GNILND,
194          /* .nf_name      */  "gni",
195          /* .nf_modname   */  "kgnilnd",
196          /* .nf_addr2str  */  libcfs_decnum_addr2str,
197          /* .nf_str2addr  */  libcfs_num_str2addr,
198          /* .nf_parse_addrlist*/  libcfs_num_parse,
199          /* .nf_match_addr*/  libcfs_num_match},
200         /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
201         {/* .nf_type      */  -1},
202 };
203
204 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
205
206 int
207 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
208 {
209         *addr = 0;
210         return 1;
211 }
212
213 void
214 libcfs_ip_addr2str(__u32 addr, char *str)
215 {
216 #if 0   /* never lookup */
217 #endif
218         snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
219                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
220                  (addr >> 8) & 0xff, addr & 0xff);
221 }
222
223 /* CAVEAT EMPTOR XscanfX
224  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
225  * sscanf may return immediately if it sees the terminating '0' in a string, so
226  * I initialise the %n variable to the expected length.  If sscanf sets it;
227  * fine, if it doesn't, then the scan ended at the end of the string, which is
228  * fine too :) */
229
230 int
231 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
232 {
233         int   a;
234         int   b;
235         int   c;
236         int   d;
237         int   n = nob;                    /* XscanfX */
238
239         /* numeric IP? */
240         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
241             n == nob &&
242             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
243             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
244                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
245                 return 1;
246         }
247
248         return 0;
249 }
250
251 void
252 libcfs_decnum_addr2str(__u32 addr, char *str)
253 {
254         snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
255 }
256
257 void
258 libcfs_hexnum_addr2str(__u32 addr, char *str)
259 {
260         snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
261 }
262
263 int
264 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
265 {
266         int     n;
267
268         n = nob;
269         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
270                 return 1;
271
272         n = nob;
273         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
274                 return 1;
275
276         n = nob;
277         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
278                 return 1;
279
280         return 0;
281 }
282
283 struct netstrfns *
284 libcfs_lnd2netstrfns(int lnd)
285 {
286         int    i;
287
288         if (lnd >= 0)
289                 for (i = 0; i < libcfs_nnetstrfns; i++)
290                         if (lnd == libcfs_netstrfns[i].nf_type)
291                                 return &libcfs_netstrfns[i];
292
293         return NULL;
294 }
295
296 struct netstrfns *
297 libcfs_namenum2netstrfns(const char *name)
298 {
299         struct netstrfns *nf;
300         int            i;
301
302         for (i = 0; i < libcfs_nnetstrfns; i++) {
303                 nf = &libcfs_netstrfns[i];
304                 if (nf->nf_type >= 0 &&
305                     !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
306                         return nf;
307         }
308         return NULL;
309 }
310
311 struct netstrfns *
312 libcfs_name2netstrfns(const char *name)
313 {
314         int    i;
315
316         for (i = 0; i < libcfs_nnetstrfns; i++)
317                 if (libcfs_netstrfns[i].nf_type >= 0 &&
318                     !strcmp(libcfs_netstrfns[i].nf_name, name))
319                         return &libcfs_netstrfns[i];
320
321         return NULL;
322 }
323
324 int
325 libcfs_isknown_lnd(int type)
326 {
327         return libcfs_lnd2netstrfns(type) != NULL;
328 }
329
330 char *
331 libcfs_lnd2modname(int lnd)
332 {
333         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
334
335         return (nf == NULL) ? NULL : nf->nf_modname;
336 }
337
338 char *
339 libcfs_lnd2str(int lnd)
340 {
341         char       *str;
342         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
343
344         if (nf != NULL)
345                 return nf->nf_name;
346
347         str = libcfs_next_nidstring();
348         snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
349         return str;
350 }
351
352 int
353 libcfs_str2lnd(const char *str)
354 {
355         struct netstrfns *nf = libcfs_name2netstrfns(str);
356
357         if (nf != NULL)
358                 return nf->nf_type;
359
360         return -1;
361 }
362
363 char *
364 libcfs_net2str(__u32 net)
365 {
366         int            lnd = LNET_NETTYP(net);
367         int            num = LNET_NETNUM(net);
368         struct netstrfns *nf  = libcfs_lnd2netstrfns(lnd);
369         char         *str = libcfs_next_nidstring();
370
371         if (nf == NULL)
372                 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
373         else if (num == 0)
374                 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
375         else
376                 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
377
378         return str;
379 }
380
381 char *
382 libcfs_nid2str(lnet_nid_t nid)
383 {
384         __u32        addr = LNET_NIDADDR(nid);
385         __u32        net = LNET_NIDNET(nid);
386         int            lnd = LNET_NETTYP(net);
387         int            nnum = LNET_NETNUM(net);
388         struct netstrfns *nf;
389         char         *str;
390         int            nob;
391
392         if (nid == LNET_NID_ANY)
393                 return "<?>";
394
395         nf = libcfs_lnd2netstrfns(lnd);
396         str = libcfs_next_nidstring();
397
398         if (nf == NULL)
399                 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
400         else {
401                 nf->nf_addr2str(addr, str);
402                 nob = strlen(str);
403                 if (nnum == 0)
404                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
405                                  nf->nf_name);
406                 else
407                         snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
408                                  nf->nf_name, nnum);
409         }
410
411         return str;
412 }
413
414 static struct netstrfns *
415 libcfs_str2net_internal(const char *str, __u32 *net)
416 {
417         struct netstrfns *uninitialized_var(nf);
418         int            nob;
419         int            netnum;
420         int            i;
421
422         for (i = 0; i < libcfs_nnetstrfns; i++) {
423                 nf = &libcfs_netstrfns[i];
424                 if (nf->nf_type >= 0 &&
425                     !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
426                         break;
427         }
428
429         if (i == libcfs_nnetstrfns)
430                 return NULL;
431
432         nob = strlen(nf->nf_name);
433
434         if (strlen(str) == (unsigned int)nob) {
435                 netnum = 0;
436         } else {
437                 if (nf->nf_type == LOLND) /* net number not allowed */
438                         return NULL;
439
440                 str += nob;
441                 i = strlen(str);
442                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
443                     i != (int)strlen(str))
444                         return NULL;
445         }
446
447         *net = LNET_MKNET(nf->nf_type, netnum);
448         return nf;
449 }
450
451 __u32
452 libcfs_str2net(const char *str)
453 {
454         __u32  net;
455
456         if (libcfs_str2net_internal(str, &net) != NULL)
457                 return net;
458
459         return LNET_NIDNET(LNET_NID_ANY);
460 }
461
462 lnet_nid_t
463 libcfs_str2nid(const char *str)
464 {
465         const char       *sep = strchr(str, '@');
466         struct netstrfns *nf;
467         __u32        net;
468         __u32        addr;
469
470         if (sep != NULL) {
471                 nf = libcfs_str2net_internal(sep + 1, &net);
472                 if (nf == NULL)
473                         return LNET_NID_ANY;
474         } else {
475                 sep = str + strlen(str);
476                 net = LNET_MKNET(SOCKLND, 0);
477                 nf = libcfs_lnd2netstrfns(SOCKLND);
478                 LASSERT (nf != NULL);
479         }
480
481         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
482                 return LNET_NID_ANY;
483
484         return LNET_MKNID(net, addr);
485 }
486
487 char *
488 libcfs_id2str(lnet_process_id_t id)
489 {
490         char *str = libcfs_next_nidstring();
491
492         if (id.pid == LNET_PID_ANY) {
493                 snprintf(str, LNET_NIDSTR_SIZE,
494                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
495                 return str;
496         }
497
498         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
499                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
500                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
501         return str;
502 }
503
504 int
505 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
506 {
507         if (!strcmp(str, "*")) {
508                 *nidp = LNET_NID_ANY;
509                 return 1;
510         }
511
512         *nidp = libcfs_str2nid(str);
513         return *nidp != LNET_NID_ANY;
514 }
515
516 /**
517  * Nid range list syntax.
518  * \verbatim
519  *
520  * <nidlist>     :== <nidrange> [ ' ' <nidrange> ]
521  * <nidrange>   :== <addrrange> '@' <net>
522  * <addrrange>       :== '*' |
523  *                     <ipaddr_range> |
524  *                       <cfs_expr_list>
525  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
526  *                       <cfs_expr_list>
527  * <cfs_expr_list>   :== <number> |
528  *                     <expr_list>
529  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
530  * <range_expr>      :== <number> |
531  *                     <number> '-' <number> |
532  *                     <number> '-' <number> '/' <number>
533  * <net>             :== <netname> | <netname><number>
534  * <netname>     :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
535  *                     "vib" | "ra" | "elan" | "mx" | "ptl"
536  * \endverbatim
537  */
538
539 /**
540  * Structure to represent \<nidrange\> token of the syntax.
541  *
542  * One of this is created for each \<net\> parsed.
543  */
544 struct nidrange {
545         /**
546          * Link to list of this structures which is built on nid range
547          * list parsing.
548          */
549         struct list_head nr_link;
550         /**
551          * List head for addrrange::ar_link.
552          */
553         struct list_head nr_addrranges;
554         /**
555          * Flag indicating that *@<net> is found.
556          */
557         int nr_all;
558         /**
559          * Pointer to corresponding element of libcfs_netstrfns.
560          */
561         struct netstrfns *nr_netstrfns;
562         /**
563          * Number of network. E.g. 5 if \<net\> is "elan5".
564          */
565         int nr_netnum;
566 };
567
568 /**
569  * Structure to represent \<addrrange\> token of the syntax.
570  */
571 struct addrrange {
572         /**
573          * Link to nidrange::nr_addrranges.
574          */
575         struct list_head ar_link;
576         /**
577          * List head for cfs_expr_list::el_list.
578          */
579         struct list_head ar_numaddr_ranges;
580 };
581
582 /**
583  * Nf_parse_addrlist method for networks using numeric addresses.
584  *
585  * Examples of such networks are gm and elan.
586  *
587  * \retval 0 if \a str parsed to numeric address
588  * \retval errno otherwise
589  */
590 static int
591 libcfs_num_parse(char *str, int len, struct list_head *list)
592 {
593         struct cfs_expr_list *el;
594         int     rc;
595
596         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
597         if (rc == 0)
598                 list_add_tail(&el->el_link, list);
599
600         return rc;
601 }
602
603 /**
604  * Parses \<addrrange\> token on the syntax.
605  *
606  * Allocates struct addrrange and links to \a nidrange via
607  * (nidrange::nr_addrranges)
608  *
609  * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
610  * \retval 0 otherwise
611  */
612 static int
613 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
614 {
615         struct addrrange *addrrange;
616
617         if (src->ls_len == 1 && src->ls_str[0] == '*') {
618                 nidrange->nr_all = 1;
619                 return 1;
620         }
621
622         LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
623         if (addrrange == NULL)
624                 return 0;
625         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
626         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
627
628         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
629                                                 src->ls_len,
630                                                 &addrrange->ar_numaddr_ranges);
631 }
632
633 /**
634  * Finds or creates struct nidrange.
635  *
636  * Checks if \a src is a valid network name, looks for corresponding
637  * nidrange on the ist of nidranges (\a nidlist), creates new struct
638  * nidrange if it is not found.
639  *
640  * \retval pointer to struct nidrange matching network specified via \a src
641  * \retval NULL if \a src does not match any network
642  */
643 static struct nidrange *
644 add_nidrange(const struct cfs_lstr *src,
645              struct list_head *nidlist)
646 {
647         struct netstrfns *nf;
648         struct nidrange *nr;
649         int endlen;
650         unsigned netnum;
651
652         if (src->ls_len >= LNET_NIDSTR_SIZE)
653                 return NULL;
654
655         nf = libcfs_namenum2netstrfns(src->ls_str);
656         if (nf == NULL)
657                 return NULL;
658         endlen = src->ls_len - strlen(nf->nf_name);
659         if (endlen == 0)
660                 /* network name only, e.g. "elan" or "tcp" */
661                 netnum = 0;
662         else {
663                 /* e.g. "elan25" or "tcp23", refuse to parse if
664                  * network name is not appended with decimal or
665                  * hexadecimal number */
666                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
667                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
668                         return NULL;
669         }
670
671         list_for_each_entry(nr, nidlist, nr_link) {
672                 if (nr->nr_netstrfns != nf)
673                         continue;
674                 if (nr->nr_netnum != netnum)
675                         continue;
676                 return nr;
677         }
678
679         LIBCFS_ALLOC(nr, sizeof(struct nidrange));
680         if (nr == NULL)
681                 return NULL;
682         list_add_tail(&nr->nr_link, nidlist);
683         INIT_LIST_HEAD(&nr->nr_addrranges);
684         nr->nr_netstrfns = nf;
685         nr->nr_all = 0;
686         nr->nr_netnum = netnum;
687
688         return nr;
689 }
690
691 /**
692  * Parses \<nidrange\> token of the syntax.
693  *
694  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
695  * \retval 0 otherwise
696  */
697 static int
698 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
699 {
700         struct cfs_lstr addrrange;
701         struct cfs_lstr net;
702         struct cfs_lstr tmp;
703         struct nidrange *nr;
704
705         tmp = *src;
706         if (cfs_gettok(src, '@', &addrrange) == 0)
707                 goto failed;
708
709         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
710                 goto failed;
711
712         nr = add_nidrange(&net, nidlist);
713         if (nr == NULL)
714                 goto failed;
715
716         if (parse_addrange(&addrrange, nr) != 0)
717                 goto failed;
718
719         return 1;
720  failed:
721         CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
722         return 0;
723 }
724
725 /**
726  * Frees addrrange structures of \a list.
727  *
728  * For each struct addrrange structure found on \a list it frees
729  * cfs_expr_list list attached to it and frees the addrrange itself.
730  *
731  * \retval none
732  */
733 static void
734 free_addrranges(struct list_head *list)
735 {
736         while (!list_empty(list)) {
737                 struct addrrange *ar;
738
739                 ar = list_entry(list->next, struct addrrange, ar_link);
740
741                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
742                 list_del(&ar->ar_link);
743                 LIBCFS_FREE(ar, sizeof(struct addrrange));
744         }
745 }
746
747 /**
748  * Frees nidrange strutures of \a list.
749  *
750  * For each struct nidrange structure found on \a list it frees
751  * addrrange list attached to it and frees the nidrange itself.
752  *
753  * \retval none
754  */
755 void
756 cfs_free_nidlist(struct list_head *list)
757 {
758         struct list_head *pos, *next;
759         struct nidrange *nr;
760
761         list_for_each_safe(pos, next, list) {
762                 nr = list_entry(pos, struct nidrange, nr_link);
763                 free_addrranges(&nr->nr_addrranges);
764                 list_del(pos);
765                 LIBCFS_FREE(nr, sizeof(struct nidrange));
766         }
767 }
768
769 /**
770  * Parses nid range list.
771  *
772  * Parses with rigorous syntax and overflow checking \a str into
773  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
774  * structures and links that structure to \a nidlist. The resulting
775  * list can be used to match a NID againts set of NIDS defined by \a
776  * str.
777  * \see cfs_match_nid
778  *
779  * \retval 1 on success
780  * \retval 0 otherwise
781  */
782 int
783 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
784 {
785         struct cfs_lstr src;
786         struct cfs_lstr res;
787         int rc;
788
789         src.ls_str = str;
790         src.ls_len = len;
791         INIT_LIST_HEAD(nidlist);
792         while (src.ls_str) {
793                 rc = cfs_gettok(&src, ' ', &res);
794                 if (rc == 0) {
795                         cfs_free_nidlist(nidlist);
796                         return 0;
797                 }
798                 rc = parse_nidrange(&res, nidlist);
799                 if (rc == 0) {
800                         cfs_free_nidlist(nidlist);
801                         return 0;
802                 }
803         }
804         return 1;
805 }
806
807 /*
808  * Nf_match_addr method for networks using numeric addresses
809  *
810  * \retval 1 on match
811  * \retval 0 otherwise
812  */
813 static int
814 libcfs_num_match(__u32 addr, struct list_head *numaddr)
815 {
816         struct cfs_expr_list *el;
817
818         LASSERT(!list_empty(numaddr));
819         el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
820
821         return cfs_expr_list_match(addr, el);
822 }
823
824 /**
825  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
826  *
827  * \see cfs_parse_nidlist()
828  *
829  * \retval 1 on match
830  * \retval 0  otherwises
831  */
832 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
833 {
834         struct nidrange *nr;
835         struct addrrange *ar;
836
837         list_for_each_entry(nr, nidlist, nr_link) {
838                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
839                         continue;
840                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
841                         continue;
842                 if (nr->nr_all)
843                         return 1;
844                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
845                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
846                                                        &ar->ar_numaddr_ranges))
847                                 return 1;
848         }
849         return 0;
850 }
851
852
853 EXPORT_SYMBOL(libcfs_isknown_lnd);
854 EXPORT_SYMBOL(libcfs_lnd2modname);
855 EXPORT_SYMBOL(libcfs_lnd2str);
856 EXPORT_SYMBOL(libcfs_str2lnd);
857 EXPORT_SYMBOL(libcfs_net2str);
858 EXPORT_SYMBOL(libcfs_nid2str);
859 EXPORT_SYMBOL(libcfs_str2net);
860 EXPORT_SYMBOL(libcfs_str2nid);
861 EXPORT_SYMBOL(libcfs_id2str);
862 EXPORT_SYMBOL(libcfs_str2anynid);
863 EXPORT_SYMBOL(cfs_free_nidlist);
864 EXPORT_SYMBOL(cfs_parse_nidlist);
865 EXPORT_SYMBOL(cfs_match_nid);