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