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