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