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