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