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