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