4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/util/nidstrings.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/util/string.h>
44 #include <libcfs/libcfs.h>
45 #include <lnet/nidstr.h>
46 #ifdef HAVE_GETHOSTBYNAME
50 /* max value for numeric network address */
51 #define MAX_NUMERIC_VALUE 0xffffffff
53 #define IPSTRING_LENGTH 16
55 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
56 * consistent in all conversion functions. Some code fragments are copied
57 * around for the sake of clarity...
60 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
61 * Choose the number of nidstrings to support the MAXIMUM expected number of
62 * concurrent users. If there are more, the returned string will be volatile.
63 * NB this number must allow for a process to be descheduled for a timeslice
64 * between getting its string and using it.
67 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
68 static int libcfs_nidstring_idx;
71 libcfs_next_nidstring(void)
75 str = libcfs_nidstrings[libcfs_nidstring_idx++];
76 if (libcfs_nidstring_idx ==
77 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
78 libcfs_nidstring_idx = 0;
83 static int libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
84 static void libcfs_ip_addr2str(__u32 addr, char *str, size_t size);
85 static int libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
86 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
87 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
88 static void libcfs_decnum_addr2str(__u32 addr, char *str, size_t size);
89 static void libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size);
90 static int libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
91 static int libcfs_num_parse(char *str, int len, struct list_head *list);
92 static int libcfs_num_match(__u32 addr, struct list_head *list);
93 static int libcfs_num_addr_range_print(char *buffer, int count,
94 struct list_head *list);
95 static int libcfs_ip_addr_range_print(char *buffer, int count,
96 struct list_head *list);
97 static bool cfs_num_is_contiguous(struct list_head *nidlist);
98 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
104 void (*nf_addr2str)(__u32 addr, char *str, size_t size);
105 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
106 int (*nf_parse_addrlist)(char *str, int len,
107 struct list_head *list);
108 int (*nf_print_addrlist)(char *buffer, int count,
109 struct list_head *list);
110 int (*nf_match_addr)(__u32 addr, struct list_head *list);
111 bool (*nf_is_contiguous)(struct list_head *nidlist);
112 void (*nf_min_max)(struct list_head *nidlist, __u32 *min_nid,
116 static struct netstrfns libcfs_netstrfns[] = {
117 {/* .nf_type */ LOLND,
119 /* .nf_modname */ "klolnd",
120 /* .nf_addr2str */ libcfs_decnum_addr2str,
121 /* .nf_str2addr */ libcfs_lo_str2addr,
122 /* .nf_parse_addr*/ libcfs_num_parse,
123 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
124 /* .nf_match_addr*/ libcfs_num_match,
125 /* .nf_is_contiguous */ cfs_num_is_contiguous,
126 /* .nf_min_max */ cfs_num_min_max},
127 {/* .nf_type */ SOCKLND,
128 /* .nf_name */ "tcp",
129 /* .nf_modname */ "ksocklnd",
130 /* .nf_addr2str */ libcfs_ip_addr2str,
131 /* .nf_str2addr */ libcfs_ip_str2addr,
132 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
133 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
134 /* .nf_match_addr*/ cfs_ip_addr_match,
135 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
136 /* .nf_min_max */ cfs_ip_min_max},
137 {/* .nf_type */ O2IBLND,
138 /* .nf_name */ "o2ib",
139 /* .nf_modname */ "ko2iblnd",
140 /* .nf_addr2str */ libcfs_ip_addr2str,
141 /* .nf_str2addr */ libcfs_ip_str2addr,
142 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
143 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
144 /* .nf_match_addr*/ cfs_ip_addr_match,
145 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
146 /* .nf_min_max */ cfs_ip_min_max},
147 {/* .nf_type */ CIBLND,
148 /* .nf_name */ "cib",
149 /* .nf_modname */ "kciblnd",
150 /* .nf_addr2str */ libcfs_ip_addr2str,
151 /* .nf_str2addr */ libcfs_ip_str2addr,
152 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
153 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
154 /* .nf_match_addr*/ cfs_ip_addr_match,
155 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
156 /* .nf_min_max */ cfs_ip_min_max},
157 {/* .nf_type */ OPENIBLND,
158 /* .nf_name */ "openib",
159 /* .nf_modname */ "kopeniblnd",
160 /* .nf_addr2str */ libcfs_ip_addr2str,
161 /* .nf_str2addr */ libcfs_ip_str2addr,
162 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
163 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
164 /* .nf_match_addr*/ cfs_ip_addr_match,
165 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
166 /* .nf_min_max */ cfs_ip_min_max},
167 {/* .nf_type */ IIBLND,
168 /* .nf_name */ "iib",
169 /* .nf_modname */ "kiiblnd",
170 /* .nf_addr2str */ libcfs_ip_addr2str,
171 /* .nf_str2addr */ libcfs_ip_str2addr,
172 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
173 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
174 /* .nf_match_addr*/ cfs_ip_addr_match,
175 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
176 /* .nf_min_max */ cfs_ip_min_max},
177 {/* .nf_type */ VIBLND,
178 /* .nf_name */ "vib",
179 /* .nf_modname */ "kviblnd",
180 /* .nf_addr2str */ libcfs_ip_addr2str,
181 /* .nf_str2addr */ libcfs_ip_str2addr,
182 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
183 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
184 /* .nf_match_addr*/ cfs_ip_addr_match,
185 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
186 /* .nf_min_max */ cfs_ip_min_max},
187 {/* .nf_type */ RALND,
189 /* .nf_modname */ "kralnd",
190 /* .nf_addr2str */ libcfs_ip_addr2str,
191 /* .nf_str2addr */ libcfs_ip_str2addr,
192 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
193 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
194 /* .nf_match_addr*/ cfs_ip_addr_match,
195 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
196 /* .nf_min_max */ cfs_ip_min_max},
197 {/* .nf_type */ QSWLND,
198 /* .nf_name */ "elan",
199 /* .nf_modname */ "kqswlnd",
200 /* .nf_addr2str */ libcfs_decnum_addr2str,
201 /* .nf_str2addr */ libcfs_num_str2addr,
202 /* .nf_parse_addrlist*/ libcfs_num_parse,
203 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
204 /* .nf_match_addr*/ libcfs_num_match,
205 /* .nf_is_contiguous */ cfs_num_is_contiguous,
206 /* .nf_min_max */ cfs_num_min_max},
207 {/* .nf_type */ GMLND,
209 /* .nf_modname */ "kgmlnd",
210 /* .nf_addr2str */ libcfs_hexnum_addr2str,
211 /* .nf_str2addr */ libcfs_num_str2addr,
212 /* .nf_parse_addrlist*/ libcfs_num_parse,
213 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
214 /* .nf_match_addr*/ libcfs_num_match,
215 /* .nf_is_contiguous */ cfs_num_is_contiguous,
216 /* .nf_min_max */ cfs_num_min_max},
217 {/* .nf_type */ MXLND,
219 /* .nf_modname */ "kmxlnd",
220 /* .nf_addr2str */ libcfs_ip_addr2str,
221 /* .nf_str2addr */ libcfs_ip_str2addr,
222 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
223 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
224 /* .nf_match_addr*/ cfs_ip_addr_match,
225 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
226 /* .nf_min_max */ cfs_ip_min_max},
227 {/* .nf_type */ PTLLND,
228 /* .nf_name */ "ptl",
229 /* .nf_modname */ "kptllnd",
230 /* .nf_addr2str */ libcfs_decnum_addr2str,
231 /* .nf_str2addr */ libcfs_num_str2addr,
232 /* .nf_parse_addrlist*/ libcfs_num_parse,
233 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
234 /* .nf_match_addr*/ libcfs_num_match,
235 /* .nf_is_contiguous */ cfs_num_is_contiguous,
236 /* .nf_min_max */ cfs_num_min_max},
237 {/* .nf_type */ GNILND,
238 /* .nf_name */ "gni",
239 /* .nf_modname */ "kgnilnd",
240 /* .nf_addr2str */ libcfs_decnum_addr2str,
241 /* .nf_str2addr */ libcfs_num_str2addr,
242 /* .nf_parse_addrlist*/ libcfs_num_parse,
243 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
244 /* .nf_match_addr*/ libcfs_num_match,
245 /* .nf_is_contiguous */ cfs_num_is_contiguous,
246 /* .nf_min_max */ cfs_num_min_max},
247 {/* .nf_type */ GNIIPLND,
248 /* .nf_name */ "gip",
249 /* .nf_modname */ "kgnilnd",
250 /* .nf_addr2str */ libcfs_ip_addr2str,
251 /* .nf_str2addr */ libcfs_ip_str2addr,
252 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
253 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
254 /* .nf_match_addr*/ cfs_ip_addr_match,
255 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
256 /* .nf_min_max */ cfs_ip_min_max},
257 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
261 static const size_t libcfs_nnetstrfns =
262 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
265 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
272 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
274 snprintf(str, size, "%u.%u.%u.%u",
275 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
276 (addr >> 8) & 0xff, addr & 0xff);
279 /* CAVEAT EMPTOR XscanfX
280 * I use "%n" at the end of a sscanf format to detect trailing junk. However
281 * sscanf may return immediately if it sees the terminating '0' in a string, so
282 * I initialise the %n variable to the expected length. If sscanf sets it;
283 * fine, if it doesn't, then the scan ended at the end of the string, which is
286 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
292 int n = nob; /* XscanfX */
295 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
297 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
298 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
299 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
303 #ifdef HAVE_GETHOSTBYNAME
304 /* known hostname? */
305 if (('a' <= str[0] && str[0] <= 'z') ||
306 ('A' <= str[0] && str[0] <= 'Z')) {
309 tmp = calloc(1, nob + 1);
313 memcpy(tmp, str, nob);
316 he = gethostbyname(tmp);
321 __u32 ip = *(__u32 *)he->h_addr;
333 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
335 snprintf(str, size, "%u", addr);
339 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
341 snprintf(str, size, "0x%x", addr);
345 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
350 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
354 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
358 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
364 static struct netstrfns *
365 libcfs_lnd2netstrfns(__u32 lnd)
369 for (i = 0; i < libcfs_nnetstrfns; i++)
370 if (lnd == libcfs_netstrfns[i].nf_type)
371 return &libcfs_netstrfns[i];
376 static struct netstrfns *
377 libcfs_namenum2netstrfns(const char *name)
379 struct netstrfns *nf;
382 for (i = 0; i < libcfs_nnetstrfns; i++) {
383 nf = &libcfs_netstrfns[i];
384 if (nf->nf_type >= 0 &&
385 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
391 static struct netstrfns *
392 libcfs_name2netstrfns(const char *name)
396 for (i = 0; i < libcfs_nnetstrfns; i++)
397 if (libcfs_netstrfns[i].nf_type >= 0 &&
398 !strcmp(libcfs_netstrfns[i].nf_name, name))
399 return &libcfs_netstrfns[i];
405 libcfs_isknown_lnd(__u32 lnd)
407 return libcfs_lnd2netstrfns(lnd) != NULL;
411 libcfs_lnd2modname(__u32 lnd)
413 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
415 return (nf == NULL) ? NULL : nf->nf_modname;
419 libcfs_str2lnd(const char *str)
421 struct netstrfns *nf = libcfs_name2netstrfns(str);
430 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
432 struct netstrfns *nf;
434 nf = libcfs_lnd2netstrfns(lnd);
436 snprintf(buf, buf_size, "?%u?", lnd);
438 snprintf(buf, buf_size, "%s", nf->nf_name);
444 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
446 __u32 nnum = LNET_NETNUM(net);
447 __u32 lnd = LNET_NETTYP(net);
448 struct netstrfns *nf;
450 nf = libcfs_lnd2netstrfns(lnd);
452 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
454 snprintf(buf, buf_size, "%s", nf->nf_name);
456 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
462 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
464 __u32 addr = LNET_NIDADDR(nid);
465 __u32 net = LNET_NIDNET(nid);
466 __u32 nnum = LNET_NETNUM(net);
467 __u32 lnd = LNET_NETTYP(net);
468 struct netstrfns *nf;
470 if (nid == LNET_NID_ANY) {
471 strncpy(buf, "<?>", buf_size);
472 buf[buf_size - 1] = '\0';
476 nf = libcfs_lnd2netstrfns(lnd);
478 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
482 nf->nf_addr2str(addr, buf, buf_size);
483 addr_len = strlen(buf);
485 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
488 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
495 static struct netstrfns *
496 libcfs_str2net_internal(const char *str, __u32 *net)
498 struct netstrfns *nf = NULL;
503 for (i = 0; i < libcfs_nnetstrfns; i++) {
504 nf = &libcfs_netstrfns[i];
505 if (nf->nf_type >= 0 &&
506 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
510 if (i == libcfs_nnetstrfns)
513 nob = strlen(nf->nf_name);
515 if (strlen(str) == (unsigned int)nob) {
518 if (nf->nf_type == LOLND) /* net number not allowed */
523 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
524 i != (int)strlen(str))
528 *net = LNET_MKNET(nf->nf_type, netnum);
533 libcfs_str2net(const char *str)
537 if (libcfs_str2net_internal(str, &net) != NULL)
540 return LNET_NIDNET(LNET_NID_ANY);
544 libcfs_str2nid(const char *str)
546 const char *sep = strchr(str, '@');
547 struct netstrfns *nf;
552 nf = libcfs_str2net_internal(sep + 1, &net);
556 sep = str + strlen(str);
557 net = LNET_MKNET(SOCKLND, 0);
558 nf = libcfs_lnd2netstrfns(SOCKLND);
562 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
565 return LNET_MKNID(net, addr);
569 libcfs_id2str(lnet_process_id_t id)
571 char *str = libcfs_next_nidstring();
573 if (id.pid == LNET_PID_ANY) {
574 snprintf(str, LNET_NIDSTR_SIZE,
575 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
579 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
580 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
581 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
586 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
588 if (!strcmp(str, "*")) {
589 *nidp = LNET_NID_ANY;
593 *nidp = libcfs_str2nid(str);
594 return *nidp != LNET_NID_ANY;
598 * Nid range list syntax.
601 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
602 * <nidrange> :== <addrrange> '@' <net>
603 * <addrrange> :== '*' |
606 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
608 * <cfs_expr_list> :== <number> |
610 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
611 * <range_expr> :== <number> |
612 * <number> '-' <number> |
613 * <number> '-' <number> '/' <number>
614 * <net> :== <netname> | <netname><number>
615 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
616 * "vib" | "ra" | "elan" | "mx" | "ptl"
621 * Structure to represent \<nidrange\> token of the syntax.
623 * One of this is created for each \<net\> parsed.
627 * Link to list of this structures which is built on nid range
630 struct list_head nr_link;
632 * List head for addrrange::ar_link.
634 struct list_head nr_addrranges;
636 * Flag indicating that *@<net> is found.
640 * Pointer to corresponding element of libcfs_netstrfns.
642 struct netstrfns *nr_netstrfns;
644 * Number of network. E.g. 5 if \<net\> is "elan5".
650 * Structure to represent \<addrrange\> token of the syntax.
654 * Link to nidrange::nr_addrranges.
656 struct list_head ar_link;
658 * List head for cfs_expr_list::el_list.
660 struct list_head ar_numaddr_ranges;
664 * Nf_parse_addrlist method for networks using numeric addresses.
666 * Examples of such networks are gm and elan.
668 * \retval 0 if \a str parsed to numeric address
669 * \retval errno otherwise
672 libcfs_num_parse(char *str, int len, struct list_head *list)
674 struct cfs_expr_list *el;
677 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
679 list_add_tail(&el->el_link, list);
685 * Parses \<addrrange\> token on the syntax.
687 * Allocates struct addrrange and links to \a nidrange via
688 * (nidrange::nr_addrranges)
690 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
691 * \retval -errno otherwise
694 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
696 struct addrrange *addrrange;
698 if (src->ls_len == 1 && src->ls_str[0] == '*') {
699 nidrange->nr_all = 1;
703 addrrange = calloc(1, sizeof(struct addrrange));
704 if (addrrange == NULL)
706 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
707 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
709 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
711 &addrrange->ar_numaddr_ranges);
715 * Finds or creates struct nidrange.
717 * Checks if \a src is a valid network name, looks for corresponding
718 * nidrange on the ist of nidranges (\a nidlist), creates new struct
719 * nidrange if it is not found.
721 * \retval pointer to struct nidrange matching network specified via \a src
722 * \retval NULL if \a src does not match any network
724 static struct nidrange *
725 add_nidrange(const struct cfs_lstr *src,
726 struct list_head *nidlist)
728 struct netstrfns *nf;
733 if (src->ls_len >= LNET_NIDSTR_SIZE)
736 nf = libcfs_namenum2netstrfns(src->ls_str);
739 endlen = src->ls_len - strlen(nf->nf_name);
741 /* network name only, e.g. "elan" or "tcp" */
744 /* e.g. "elan25" or "tcp23", refuse to parse if
745 * network name is not appended with decimal or
746 * hexadecimal number */
747 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
748 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
752 list_for_each_entry(nr, nidlist, nr_link) {
753 if (nr->nr_netstrfns != nf)
755 if (nr->nr_netnum != netnum)
760 nr = calloc(1, sizeof(struct nidrange));
763 list_add_tail(&nr->nr_link, nidlist);
764 INIT_LIST_HEAD(&nr->nr_addrranges);
765 nr->nr_netstrfns = nf;
767 nr->nr_netnum = netnum;
773 * Parses \<nidrange\> token of the syntax.
775 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
776 * \retval 0 otherwise
779 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
781 struct cfs_lstr addrrange;
787 if (cfs_gettok(src, '@', &addrrange) == 0)
790 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
793 nr = add_nidrange(&net, nidlist);
797 if (parse_addrange(&addrrange, nr) != 0)
802 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
807 * Frees addrrange structures of \a list.
809 * For each struct addrrange structure found on \a list it frees
810 * cfs_expr_list list attached to it and frees the addrrange itself.
815 free_addrranges(struct list_head *list)
817 while (!list_empty(list)) {
818 struct addrrange *ar;
820 ar = list_entry(list->next, struct addrrange, ar_link);
822 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
823 list_del(&ar->ar_link);
829 * Frees nidrange strutures of \a list.
831 * For each struct nidrange structure found on \a list it frees
832 * addrrange list attached to it and frees the nidrange itself.
837 cfs_free_nidlist(struct list_head *list)
839 struct list_head *pos, *next;
842 list_for_each_safe(pos, next, list) {
843 nr = list_entry(pos, struct nidrange, nr_link);
844 free_addrranges(&nr->nr_addrranges);
851 * Parses nid range list.
853 * Parses with rigorous syntax and overflow checking \a str into
854 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
855 * structures and links that structure to \a nidlist. The resulting
856 * list can be used to match a NID againts set of NIDS defined by \a
860 * \retval 1 on success
861 * \retval 0 otherwise
864 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
872 INIT_LIST_HEAD(nidlist);
874 rc = cfs_gettok(&src, ' ', &res);
876 cfs_free_nidlist(nidlist);
879 rc = parse_nidrange(&res, nidlist);
881 cfs_free_nidlist(nidlist);
889 * Nf_match_addr method for networks using numeric addresses
892 * \retval 0 otherwise
895 libcfs_num_match(__u32 addr, struct list_head *numaddr)
897 struct cfs_expr_list *el;
899 LASSERT(!list_empty(numaddr));
900 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
902 return cfs_expr_list_match(addr, el);
906 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
908 * \see cfs_parse_nidlist()
911 * \retval 0 otherwises
913 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
916 struct addrrange *ar;
918 list_for_each_entry(nr, nidlist, nr_link) {
919 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
921 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
925 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
926 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
927 &ar->ar_numaddr_ranges))
934 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
937 struct cfs_expr_list *el;
939 list_for_each_entry(el, list, el_link) {
941 i += cfs_expr_list_print(buffer + i, count - i, el);
947 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
950 struct cfs_expr_list *el;
952 list_for_each_entry(el, list, el_link) {
955 i += snprintf(buffer + i, count - i, ".");
956 i += cfs_expr_list_print(buffer + i, count - i, el);
963 * Print the network part of the nidrange \a nr into the specified \a buffer.
965 * \retval number of characters written
968 cfs_print_network(char *buffer, int count, struct nidrange *nr)
970 struct netstrfns *nf = nr->nr_netstrfns;
972 if (nr->nr_netnum == 0)
973 return snprintf(buffer, count, "@%s", nf->nf_name);
975 return snprintf(buffer, count, "@%s%u",
976 nf->nf_name, nr->nr_netnum);
981 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
982 * At max \a count characters can be printed into \a buffer.
984 * \retval number of characters written
987 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
991 struct addrrange *ar;
992 struct netstrfns *nf = nr->nr_netstrfns;
994 list_for_each_entry(ar, addrranges, ar_link) {
996 i += snprintf(buffer + i, count - i, " ");
997 i += nf->nf_print_addrlist(buffer + i, count - i,
998 &ar->ar_numaddr_ranges);
999 i += cfs_print_network(buffer + i, count - i, nr);
1005 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1006 * At max \a count characters can be printed into \a buffer.
1007 * Nidranges are separated by a space character.
1009 * \retval number of characters written
1011 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1014 struct nidrange *nr;
1019 list_for_each_entry(nr, nidlist, nr_link) {
1021 i += snprintf(buffer + i, count - i, " ");
1023 if (nr->nr_all != 0) {
1024 LASSERT(list_empty(&nr->nr_addrranges));
1025 i += snprintf(buffer + i, count - i, "*");
1026 i += cfs_print_network(buffer + i, count - i, nr);
1028 i += cfs_print_addrranges(buffer + i, count - i,
1029 &nr->nr_addrranges, nr);
1036 * Determines minimum and maximum addresses for a single
1037 * numeric address range
1043 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1046 struct cfs_expr_list *el;
1047 struct cfs_range_expr *re;
1048 __u32 tmp_ip_addr = 0;
1049 unsigned int min_ip[4] = {0};
1050 unsigned int max_ip[4] = {0};
1053 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1054 list_for_each_entry(re, &el->el_exprs, re_link) {
1055 min_ip[re_count] = re->re_lo;
1056 max_ip[re_count] = re->re_hi;
1061 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1062 (min_ip[2] << 8) | min_ip[3]);
1064 if (min_nid != NULL)
1065 *min_nid = tmp_ip_addr;
1067 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1068 (max_ip[2] << 8) | max_ip[3]);
1070 if (max_nid != NULL)
1071 *max_nid = tmp_ip_addr;
1075 * Determines minimum and maximum addresses for a single
1076 * numeric address range
1082 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1085 struct cfs_expr_list *el;
1086 struct cfs_range_expr *re;
1087 unsigned int min_addr = 0;
1088 unsigned int max_addr = 0;
1090 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1091 list_for_each_entry(re, &el->el_exprs, re_link) {
1092 if (re->re_lo < min_addr || min_addr == 0)
1093 min_addr = re->re_lo;
1094 if (re->re_hi > max_addr)
1095 max_addr = re->re_hi;
1099 if (min_nid != NULL)
1100 *min_nid = min_addr;
1101 if (max_nid != NULL)
1102 *max_nid = max_addr;
1106 * Determines whether an expression list in an nidrange contains exactly
1107 * one contiguous address range. Calls the correct netstrfns for the LND
1111 * \retval true if contiguous
1112 * \retval false if not contiguous
1114 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1116 struct nidrange *nr;
1117 struct netstrfns *nf = NULL;
1118 char *lndname = NULL;
1121 list_for_each_entry(nr, nidlist, nr_link) {
1122 nf = nr->nr_netstrfns;
1123 if (lndname == NULL)
1124 lndname = nf->nf_name;
1126 netnum = nr->nr_netnum;
1128 if (strcmp(lndname, nf->nf_name) != 0 ||
1129 netnum != nr->nr_netnum)
1136 if (!nf->nf_is_contiguous(nidlist))
1143 * Determines whether an expression list in an num nidrange contains exactly
1144 * one contiguous address range.
1148 * \retval true if contiguous
1149 * \retval false if not contiguous
1151 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1153 struct nidrange *nr;
1154 struct addrrange *ar;
1155 struct cfs_expr_list *el;
1156 struct cfs_range_expr *re;
1158 __u32 last_end_nid = 0;
1159 __u32 current_start_nid = 0;
1160 __u32 current_end_nid = 0;
1162 list_for_each_entry(nr, nidlist, nr_link) {
1163 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1164 cfs_num_ar_min_max(ar, ¤t_start_nid,
1166 if (last_end_nid != 0 &&
1167 (current_start_nid - last_end_nid != 1))
1169 last_end_nid = current_end_nid;
1170 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1172 list_for_each_entry(re, &el->el_exprs,
1174 if (re->re_stride > 1)
1176 else if (last_hi != 0 &&
1177 re->re_hi - last_hi != 1)
1179 last_hi = re->re_hi;
1189 * Determines whether an expression list in an ip nidrange contains exactly
1190 * one contiguous address range.
1194 * \retval true if contiguous
1195 * \retval false if not contiguous
1197 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1199 struct nidrange *nr;
1200 struct addrrange *ar;
1201 struct cfs_expr_list *el;
1202 struct cfs_range_expr *re;
1206 __u32 last_end_nid = 0;
1207 __u32 current_start_nid = 0;
1208 __u32 current_end_nid = 0;
1210 list_for_each_entry(nr, nidlist, nr_link) {
1211 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1214 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1216 if (last_end_nid != 0 &&
1217 (current_start_nid - last_end_nid != 1))
1219 last_end_nid = current_end_nid;
1220 list_for_each_entry(el,
1221 &ar->ar_numaddr_ranges,
1224 list_for_each_entry(re, &el->el_exprs,
1227 if (re->re_stride > 1 ||
1228 (last_diff > 0 && last_hi != 255) ||
1229 (last_diff > 0 && last_hi == 255 &&
1232 last_hi = re->re_hi;
1233 last_diff = re->re_hi - re->re_lo;
1243 * Takes a linked list of nidrange expressions, determines the minimum
1244 * and maximum nid and creates appropriate nid structures
1250 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1251 char *max_nid, size_t nidstr_length)
1253 struct nidrange *nr;
1254 struct netstrfns *nf = NULL;
1258 char *lndname = NULL;
1259 char min_addr_str[IPSTRING_LENGTH];
1260 char max_addr_str[IPSTRING_LENGTH];
1262 list_for_each_entry(nr, nidlist, nr_link) {
1263 nf = nr->nr_netstrfns;
1264 lndname = nf->nf_name;
1266 netnum = nr->nr_netnum;
1268 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1270 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1271 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1273 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1275 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1280 * Determines the min and max NID values for num LNDs
1286 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1289 struct nidrange *nr;
1290 struct addrrange *ar;
1291 unsigned int tmp_min_addr = 0;
1292 unsigned int tmp_max_addr = 0;
1293 unsigned int min_addr = 0;
1294 unsigned int max_addr = 0;
1296 list_for_each_entry(nr, nidlist, nr_link) {
1297 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1298 cfs_num_ar_min_max(ar, &tmp_min_addr,
1300 if (tmp_min_addr < min_addr || min_addr == 0)
1301 min_addr = tmp_min_addr;
1302 if (tmp_max_addr > max_addr)
1303 max_addr = tmp_min_addr;
1306 *max_nid = max_addr;
1307 *min_nid = min_addr;
1311 * Takes an nidlist and determines the minimum and maximum
1318 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1321 struct nidrange *nr;
1322 struct addrrange *ar;
1323 __u32 tmp_min_ip_addr = 0;
1324 __u32 tmp_max_ip_addr = 0;
1325 __u32 min_ip_addr = 0;
1326 __u32 max_ip_addr = 0;
1328 list_for_each_entry(nr, nidlist, nr_link) {
1329 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1330 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1332 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1333 min_ip_addr = tmp_min_ip_addr;
1334 if (tmp_max_ip_addr > max_ip_addr)
1335 max_ip_addr = tmp_max_ip_addr;
1339 if (min_nid != NULL)
1340 *min_nid = min_ip_addr;
1341 if (max_nid != NULL)
1342 *max_nid = max_ip_addr;