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