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