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