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