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
50 #include <libcfs/util/string.h>
51 #include <lnet/types.h>
52 #include <lnet/nidstr.h>
53 #ifdef HAVE_GETHOSTBYNAME
57 /* max value for numeric network address */
58 #define MAX_NUMERIC_VALUE 0xffffffff
60 #define IPSTRING_LENGTH 16
62 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
63 * consistent in all conversion functions. Some code fragments are copied
64 * around for the sake of clarity...
67 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
68 * Choose the number of nidstrings to support the MAXIMUM expected number of
69 * concurrent users. If there are more, the returned string will be volatile.
70 * NB this number must allow for a process to be descheduled for a timeslice
71 * between getting its string and using it.
74 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
75 static int libcfs_nidstring_idx;
78 libcfs_next_nidstring(void)
82 str = libcfs_nidstrings[libcfs_nidstring_idx++];
83 if (libcfs_nidstring_idx ==
84 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
85 libcfs_nidstring_idx = 0;
90 static int libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
91 static void libcfs_ip_addr2str(__u32 addr, char *str, size_t size);
92 static int libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
93 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
94 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
95 static void libcfs_decnum_addr2str(__u32 addr, char *str, size_t size);
96 static void libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size);
97 static int libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
98 static int libcfs_num_parse(char *str, int len, struct list_head *list);
99 static int libcfs_num_match(__u32 addr, struct list_head *list);
100 static int libcfs_num_addr_range_print(char *buffer, int count,
101 struct list_head *list);
102 static int libcfs_ip_addr_range_print(char *buffer, int count,
103 struct list_head *list);
104 static bool cfs_num_is_contiguous(struct list_head *nidlist);
105 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
111 void (*nf_addr2str)(__u32 addr, char *str, size_t size);
112 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
113 int (*nf_parse_addrlist)(char *str, int len,
114 struct list_head *list);
115 int (*nf_print_addrlist)(char *buffer, int count,
116 struct list_head *list);
117 int (*nf_match_addr)(__u32 addr, struct list_head *list);
118 bool (*nf_is_contiguous)(struct list_head *nidlist);
119 void (*nf_min_max)(struct list_head *nidlist, __u32 *min_nid,
123 static struct netstrfns libcfs_netstrfns[] = {
124 {/* .nf_type */ LOLND,
126 /* .nf_modname */ "klolnd",
127 /* .nf_addr2str */ libcfs_decnum_addr2str,
128 /* .nf_str2addr */ libcfs_lo_str2addr,
129 /* .nf_parse_addr*/ libcfs_num_parse,
130 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
131 /* .nf_match_addr*/ libcfs_num_match,
132 /* .nf_is_contiguous */ cfs_num_is_contiguous,
133 /* .nf_min_max */ cfs_num_min_max},
134 {/* .nf_type */ SOCKLND,
135 /* .nf_name */ "tcp",
136 /* .nf_modname */ "ksocklnd",
137 /* .nf_addr2str */ libcfs_ip_addr2str,
138 /* .nf_str2addr */ libcfs_ip_str2addr,
139 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
140 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
141 /* .nf_match_addr*/ cfs_ip_addr_match,
142 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
143 /* .nf_min_max */ cfs_ip_min_max},
144 {/* .nf_type */ O2IBLND,
145 /* .nf_name */ "o2ib",
146 /* .nf_modname */ "ko2iblnd",
147 /* .nf_addr2str */ libcfs_ip_addr2str,
148 /* .nf_str2addr */ libcfs_ip_str2addr,
149 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
150 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
151 /* .nf_match_addr*/ cfs_ip_addr_match,
152 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
153 /* .nf_min_max */ cfs_ip_min_max},
154 {/* .nf_type */ CIBLND,
155 /* .nf_name */ "cib",
156 /* .nf_modname */ "kciblnd",
157 /* .nf_addr2str */ libcfs_ip_addr2str,
158 /* .nf_str2addr */ libcfs_ip_str2addr,
159 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
160 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
161 /* .nf_match_addr*/ cfs_ip_addr_match,
162 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
163 /* .nf_min_max */ cfs_ip_min_max},
164 {/* .nf_type */ OPENIBLND,
165 /* .nf_name */ "openib",
166 /* .nf_modname */ "kopeniblnd",
167 /* .nf_addr2str */ libcfs_ip_addr2str,
168 /* .nf_str2addr */ libcfs_ip_str2addr,
169 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
170 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
171 /* .nf_match_addr*/ cfs_ip_addr_match,
172 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
173 /* .nf_min_max */ cfs_ip_min_max},
174 {/* .nf_type */ IIBLND,
175 /* .nf_name */ "iib",
176 /* .nf_modname */ "kiiblnd",
177 /* .nf_addr2str */ libcfs_ip_addr2str,
178 /* .nf_str2addr */ libcfs_ip_str2addr,
179 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
180 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
181 /* .nf_match_addr*/ cfs_ip_addr_match,
182 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
183 /* .nf_min_max */ cfs_ip_min_max},
184 {/* .nf_type */ VIBLND,
185 /* .nf_name */ "vib",
186 /* .nf_modname */ "kviblnd",
187 /* .nf_addr2str */ libcfs_ip_addr2str,
188 /* .nf_str2addr */ libcfs_ip_str2addr,
189 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
190 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
191 /* .nf_match_addr*/ cfs_ip_addr_match,
192 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
193 /* .nf_min_max */ cfs_ip_min_max},
194 {/* .nf_type */ RALND,
196 /* .nf_modname */ "kralnd",
197 /* .nf_addr2str */ libcfs_ip_addr2str,
198 /* .nf_str2addr */ libcfs_ip_str2addr,
199 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
200 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
201 /* .nf_match_addr*/ cfs_ip_addr_match,
202 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
203 /* .nf_min_max */ cfs_ip_min_max},
204 {/* .nf_type */ QSWLND,
205 /* .nf_name */ "elan",
206 /* .nf_modname */ "kqswlnd",
207 /* .nf_addr2str */ libcfs_decnum_addr2str,
208 /* .nf_str2addr */ libcfs_num_str2addr,
209 /* .nf_parse_addrlist*/ libcfs_num_parse,
210 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
211 /* .nf_match_addr*/ libcfs_num_match,
212 /* .nf_is_contiguous */ cfs_num_is_contiguous,
213 /* .nf_min_max */ cfs_num_min_max},
214 {/* .nf_type */ GMLND,
216 /* .nf_modname */ "kgmlnd",
217 /* .nf_addr2str */ libcfs_hexnum_addr2str,
218 /* .nf_str2addr */ libcfs_num_str2addr,
219 /* .nf_parse_addrlist*/ libcfs_num_parse,
220 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
221 /* .nf_match_addr*/ libcfs_num_match,
222 /* .nf_is_contiguous */ cfs_num_is_contiguous,
223 /* .nf_min_max */ cfs_num_min_max},
224 {/* .nf_type */ MXLND,
226 /* .nf_modname */ "kmxlnd",
227 /* .nf_addr2str */ libcfs_ip_addr2str,
228 /* .nf_str2addr */ libcfs_ip_str2addr,
229 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
230 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
231 /* .nf_match_addr*/ cfs_ip_addr_match,
232 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
233 /* .nf_min_max */ cfs_ip_min_max},
234 {/* .nf_type */ PTLLND,
235 /* .nf_name */ "ptl",
236 /* .nf_modname */ "kptllnd",
237 /* .nf_addr2str */ libcfs_decnum_addr2str,
238 /* .nf_str2addr */ libcfs_num_str2addr,
239 /* .nf_parse_addrlist*/ libcfs_num_parse,
240 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
241 /* .nf_match_addr*/ libcfs_num_match,
242 /* .nf_is_contiguous */ cfs_num_is_contiguous,
243 /* .nf_min_max */ cfs_num_min_max},
244 {/* .nf_type */ GNILND,
245 /* .nf_name */ "gni",
246 /* .nf_modname */ "kgnilnd",
247 /* .nf_addr2str */ libcfs_decnum_addr2str,
248 /* .nf_str2addr */ libcfs_num_str2addr,
249 /* .nf_parse_addrlist*/ libcfs_num_parse,
250 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
251 /* .nf_match_addr*/ libcfs_num_match,
252 /* .nf_is_contiguous */ cfs_num_is_contiguous,
253 /* .nf_min_max */ cfs_num_min_max},
254 {/* .nf_type */ GNIIPLND,
255 /* .nf_name */ "gip",
256 /* .nf_modname */ "kgnilnd",
257 /* .nf_addr2str */ libcfs_ip_addr2str,
258 /* .nf_str2addr */ libcfs_ip_str2addr,
259 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
260 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
261 /* .nf_match_addr*/ cfs_ip_addr_match,
262 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
263 /* .nf_min_max */ cfs_ip_min_max},
264 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
268 static const size_t libcfs_nnetstrfns =
269 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
272 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
279 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
281 snprintf(str, size, "%u.%u.%u.%u",
282 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
283 (addr >> 8) & 0xff, addr & 0xff);
286 /* CAVEAT EMPTOR XscanfX
287 * I use "%n" at the end of a sscanf format to detect trailing junk. However
288 * sscanf may return immediately if it sees the terminating '0' in a string, so
289 * I initialise the %n variable to the expected length. If sscanf sets it;
290 * fine, if it doesn't, then the scan ended at the end of the string, which is
293 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
299 int n = nob; /* XscanfX */
302 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
304 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
305 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
306 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
310 #ifdef HAVE_GETHOSTBYNAME
311 /* known hostname? */
312 if (('a' <= str[0] && str[0] <= 'z') ||
313 ('A' <= str[0] && str[0] <= 'Z')) {
316 tmp = calloc(1, nob + 1);
320 memcpy(tmp, str, nob);
323 he = gethostbyname(tmp);
328 __u32 ip = *(__u32 *)he->h_addr;
340 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
342 snprintf(str, size, "%u", addr);
346 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
348 snprintf(str, size, "0x%x", addr);
352 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
357 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
361 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
365 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
371 static struct netstrfns *
372 libcfs_lnd2netstrfns(__u32 lnd)
376 for (i = 0; i < libcfs_nnetstrfns; i++)
377 if (lnd == libcfs_netstrfns[i].nf_type)
378 return &libcfs_netstrfns[i];
383 static struct netstrfns *
384 libcfs_namenum2netstrfns(const char *name)
386 struct netstrfns *nf;
389 for (i = 0; i < libcfs_nnetstrfns; i++) {
390 nf = &libcfs_netstrfns[i];
391 if (nf->nf_type >= 0 &&
392 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
398 static struct netstrfns *
399 libcfs_name2netstrfns(const char *name)
403 for (i = 0; i < libcfs_nnetstrfns; i++)
404 if (libcfs_netstrfns[i].nf_type >= 0 &&
405 !strcmp(libcfs_netstrfns[i].nf_name, name))
406 return &libcfs_netstrfns[i];
412 libcfs_isknown_lnd(__u32 lnd)
414 return libcfs_lnd2netstrfns(lnd) != NULL;
418 libcfs_lnd2modname(__u32 lnd)
420 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
422 return (nf == NULL) ? NULL : nf->nf_modname;
426 libcfs_str2lnd(const char *str)
428 struct netstrfns *nf = libcfs_name2netstrfns(str);
437 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
439 struct netstrfns *nf;
441 nf = libcfs_lnd2netstrfns(lnd);
443 snprintf(buf, buf_size, "?%u?", lnd);
445 snprintf(buf, buf_size, "%s", nf->nf_name);
451 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
453 __u32 nnum = LNET_NETNUM(net);
454 __u32 lnd = LNET_NETTYP(net);
455 struct netstrfns *nf;
457 nf = libcfs_lnd2netstrfns(lnd);
459 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
461 snprintf(buf, buf_size, "%s", nf->nf_name);
463 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
469 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
471 __u32 addr = LNET_NIDADDR(nid);
472 __u32 net = LNET_NIDNET(nid);
473 __u32 nnum = LNET_NETNUM(net);
474 __u32 lnd = LNET_NETTYP(net);
475 struct netstrfns *nf;
477 if (nid == LNET_NID_ANY) {
478 strncpy(buf, "<?>", buf_size);
479 buf[buf_size - 1] = '\0';
483 nf = libcfs_lnd2netstrfns(lnd);
485 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
489 nf->nf_addr2str(addr, buf, buf_size);
490 addr_len = strlen(buf);
492 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
495 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
502 static struct netstrfns *
503 libcfs_str2net_internal(const char *str, __u32 *net)
505 struct netstrfns *nf = NULL;
510 for (i = 0; i < libcfs_nnetstrfns; i++) {
511 nf = &libcfs_netstrfns[i];
512 if (nf->nf_type >= 0 &&
513 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
517 if (i == libcfs_nnetstrfns)
520 nob = strlen(nf->nf_name);
522 if (strlen(str) == (unsigned int)nob) {
525 if (nf->nf_type == LOLND) /* net number not allowed */
530 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
531 i != (int)strlen(str))
535 *net = LNET_MKNET(nf->nf_type, netnum);
540 libcfs_str2net(const char *str)
544 if (libcfs_str2net_internal(str, &net) != NULL)
547 return LNET_NIDNET(LNET_NID_ANY);
551 libcfs_str2nid(const char *str)
553 const char *sep = strchr(str, '@');
554 struct netstrfns *nf;
559 nf = libcfs_str2net_internal(sep + 1, &net);
563 sep = str + strlen(str);
564 net = LNET_MKNET(SOCKLND, 0);
565 nf = libcfs_lnd2netstrfns(SOCKLND);
569 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
572 return LNET_MKNID(net, addr);
576 libcfs_id2str(lnet_process_id_t id)
578 char *str = libcfs_next_nidstring();
580 if (id.pid == LNET_PID_ANY) {
581 snprintf(str, LNET_NIDSTR_SIZE,
582 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
586 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
587 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
588 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
593 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
595 if (!strcmp(str, "*")) {
596 *nidp = LNET_NID_ANY;
600 *nidp = libcfs_str2nid(str);
601 return *nidp != LNET_NID_ANY;
605 * Nid range list syntax.
608 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
609 * <nidrange> :== <addrrange> '@' <net>
610 * <addrrange> :== '*' |
613 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
615 * <cfs_expr_list> :== <number> |
617 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
618 * <range_expr> :== <number> |
619 * <number> '-' <number> |
620 * <number> '-' <number> '/' <number>
621 * <net> :== <netname> | <netname><number>
622 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
623 * "vib" | "ra" | "elan" | "mx" | "ptl"
628 * Structure to represent \<nidrange\> token of the syntax.
630 * One of this is created for each \<net\> parsed.
634 * Link to list of this structures which is built on nid range
637 struct list_head nr_link;
639 * List head for addrrange::ar_link.
641 struct list_head nr_addrranges;
643 * Flag indicating that *@<net> is found.
647 * Pointer to corresponding element of libcfs_netstrfns.
649 struct netstrfns *nr_netstrfns;
651 * Number of network. E.g. 5 if \<net\> is "elan5".
657 * Structure to represent \<addrrange\> token of the syntax.
661 * Link to nidrange::nr_addrranges.
663 struct list_head ar_link;
665 * List head for cfs_expr_list::el_list.
667 struct list_head ar_numaddr_ranges;
671 * Nf_parse_addrlist method for networks using numeric addresses.
673 * Examples of such networks are gm and elan.
675 * \retval 0 if \a str parsed to numeric address
676 * \retval errno otherwise
679 libcfs_num_parse(char *str, int len, struct list_head *list)
681 struct cfs_expr_list *el;
684 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
686 list_add_tail(&el->el_link, list);
692 * Parses \<addrrange\> token on the syntax.
694 * Allocates struct addrrange and links to \a nidrange via
695 * (nidrange::nr_addrranges)
697 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
698 * \retval -errno otherwise
701 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
703 struct addrrange *addrrange;
705 if (src->ls_len == 1 && src->ls_str[0] == '*') {
706 nidrange->nr_all = 1;
710 addrrange = calloc(1, sizeof(struct addrrange));
711 if (addrrange == NULL)
713 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
714 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
716 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
718 &addrrange->ar_numaddr_ranges);
722 * Finds or creates struct nidrange.
724 * Checks if \a src is a valid network name, looks for corresponding
725 * nidrange on the ist of nidranges (\a nidlist), creates new struct
726 * nidrange if it is not found.
728 * \retval pointer to struct nidrange matching network specified via \a src
729 * \retval NULL if \a src does not match any network
731 static struct nidrange *
732 add_nidrange(const struct cfs_lstr *src,
733 struct list_head *nidlist)
735 struct netstrfns *nf;
740 if (src->ls_len >= LNET_NIDSTR_SIZE)
743 nf = libcfs_namenum2netstrfns(src->ls_str);
746 endlen = src->ls_len - strlen(nf->nf_name);
748 /* network name only, e.g. "elan" or "tcp" */
751 /* e.g. "elan25" or "tcp23", refuse to parse if
752 * network name is not appended with decimal or
753 * hexadecimal number */
754 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
755 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
759 list_for_each_entry(nr, nidlist, nr_link) {
760 if (nr->nr_netstrfns != nf)
762 if (nr->nr_netnum != netnum)
767 nr = calloc(1, sizeof(struct nidrange));
770 list_add_tail(&nr->nr_link, nidlist);
771 INIT_LIST_HEAD(&nr->nr_addrranges);
772 nr->nr_netstrfns = nf;
774 nr->nr_netnum = netnum;
780 * Parses \<nidrange\> token of the syntax.
782 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
783 * \retval 0 otherwise
786 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
788 struct cfs_lstr addrrange;
794 if (cfs_gettok(src, '@', &addrrange) == 0)
797 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
800 nr = add_nidrange(&net, nidlist);
804 if (parse_addrange(&addrrange, nr) != 0)
809 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
810 tmp.ls_len, tmp.ls_str);
815 * Frees addrrange structures of \a list.
817 * For each struct addrrange structure found on \a list it frees
818 * cfs_expr_list list attached to it and frees the addrrange itself.
823 free_addrranges(struct list_head *list)
825 while (!list_empty(list)) {
826 struct addrrange *ar;
828 ar = list_entry(list->next, struct addrrange, ar_link);
830 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
831 list_del(&ar->ar_link);
837 * Frees nidrange strutures of \a list.
839 * For each struct nidrange structure found on \a list it frees
840 * addrrange list attached to it and frees the nidrange itself.
845 cfs_free_nidlist(struct list_head *list)
847 struct list_head *pos, *next;
850 list_for_each_safe(pos, next, list) {
851 nr = list_entry(pos, struct nidrange, nr_link);
852 free_addrranges(&nr->nr_addrranges);
859 * Parses nid range list.
861 * Parses with rigorous syntax and overflow checking \a str into
862 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
863 * structures and links that structure to \a nidlist. The resulting
864 * list can be used to match a NID againts set of NIDS defined by \a
868 * \retval 1 on success
869 * \retval 0 otherwise
872 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
880 INIT_LIST_HEAD(nidlist);
882 rc = cfs_gettok(&src, ' ', &res);
884 cfs_free_nidlist(nidlist);
887 rc = parse_nidrange(&res, nidlist);
889 cfs_free_nidlist(nidlist);
897 * Nf_match_addr method for networks using numeric addresses
900 * \retval 0 otherwise
903 libcfs_num_match(__u32 addr, struct list_head *numaddr)
905 struct cfs_expr_list *el;
907 assert(!list_empty(numaddr));
908 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
910 return cfs_expr_list_match(addr, el);
914 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
916 * \see cfs_parse_nidlist()
919 * \retval 0 otherwises
921 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
924 struct addrrange *ar;
926 list_for_each_entry(nr, nidlist, nr_link) {
927 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
929 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
933 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
934 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
935 &ar->ar_numaddr_ranges))
942 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
944 struct cfs_expr_list *el;
947 list_for_each_entry(el, list, el_link) {
949 i += cfs_expr_list_print(buffer + i, count - i, el);
955 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
958 struct cfs_expr_list *el;
960 list_for_each_entry(el, list, el_link) {
963 i += snprintf(buffer + i, count - i, ".");
964 i += cfs_expr_list_print(buffer + i, count - i, el);
971 * Print the network part of the nidrange \a nr into the specified \a buffer.
973 * \retval number of characters written
976 cfs_print_network(char *buffer, int count, struct nidrange *nr)
978 struct netstrfns *nf = nr->nr_netstrfns;
980 if (nr->nr_netnum == 0)
981 return snprintf(buffer, count, "@%s", nf->nf_name);
983 return snprintf(buffer, count, "@%s%u",
984 nf->nf_name, nr->nr_netnum);
989 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
990 * At max \a count characters can be printed into \a buffer.
992 * \retval number of characters written
995 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
999 struct addrrange *ar;
1000 struct netstrfns *nf = nr->nr_netstrfns;
1002 list_for_each_entry(ar, addrranges, ar_link) {
1004 i += snprintf(buffer + i, count - i, " ");
1005 i += nf->nf_print_addrlist(buffer + i, count - i,
1006 &ar->ar_numaddr_ranges);
1007 i += cfs_print_network(buffer + i, count - i, nr);
1013 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1014 * At max \a count characters can be printed into \a buffer.
1015 * Nidranges are separated by a space character.
1017 * \retval number of characters written
1019 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1022 struct nidrange *nr;
1027 list_for_each_entry(nr, nidlist, nr_link) {
1029 i += snprintf(buffer + i, count - i, " ");
1031 if (nr->nr_all != 0) {
1032 assert(list_empty(&nr->nr_addrranges));
1033 i += snprintf(buffer + i, count - i, "*");
1034 i += cfs_print_network(buffer + i, count - i, nr);
1036 i += cfs_print_addrranges(buffer + i, count - i,
1037 &nr->nr_addrranges, nr);
1044 * Determines minimum and maximum addresses for a single
1045 * numeric address range
1051 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1054 struct cfs_expr_list *el;
1055 struct cfs_range_expr *re;
1056 __u32 tmp_ip_addr = 0;
1057 unsigned int min_ip[4] = {0};
1058 unsigned int max_ip[4] = {0};
1061 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1062 list_for_each_entry(re, &el->el_exprs, re_link) {
1063 min_ip[re_count] = re->re_lo;
1064 max_ip[re_count] = re->re_hi;
1069 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1070 (min_ip[2] << 8) | min_ip[3]);
1072 if (min_nid != NULL)
1073 *min_nid = tmp_ip_addr;
1075 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1076 (max_ip[2] << 8) | max_ip[3]);
1078 if (max_nid != NULL)
1079 *max_nid = tmp_ip_addr;
1083 * Determines minimum and maximum addresses for a single
1084 * numeric address range
1090 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1093 struct cfs_expr_list *el;
1094 struct cfs_range_expr *re;
1095 unsigned int min_addr = 0;
1096 unsigned int max_addr = 0;
1098 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1099 list_for_each_entry(re, &el->el_exprs, re_link) {
1100 if (re->re_lo < min_addr || min_addr == 0)
1101 min_addr = re->re_lo;
1102 if (re->re_hi > max_addr)
1103 max_addr = re->re_hi;
1107 if (min_nid != NULL)
1108 *min_nid = min_addr;
1109 if (max_nid != NULL)
1110 *max_nid = max_addr;
1114 * Determines whether an expression list in an nidrange contains exactly
1115 * one contiguous address range. Calls the correct netstrfns for the LND
1119 * \retval true if contiguous
1120 * \retval false if not contiguous
1122 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1124 struct nidrange *nr;
1125 struct netstrfns *nf = NULL;
1126 char *lndname = NULL;
1129 list_for_each_entry(nr, nidlist, nr_link) {
1130 nf = nr->nr_netstrfns;
1131 if (lndname == NULL)
1132 lndname = nf->nf_name;
1134 netnum = nr->nr_netnum;
1136 if (strcmp(lndname, nf->nf_name) != 0 ||
1137 netnum != nr->nr_netnum)
1144 if (!nf->nf_is_contiguous(nidlist))
1151 * Determines whether an expression list in an num nidrange contains exactly
1152 * one contiguous address range.
1156 * \retval true if contiguous
1157 * \retval false if not contiguous
1159 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1161 struct nidrange *nr;
1162 struct addrrange *ar;
1163 struct cfs_expr_list *el;
1164 struct cfs_range_expr *re;
1166 __u32 last_end_nid = 0;
1167 __u32 current_start_nid = 0;
1168 __u32 current_end_nid = 0;
1170 list_for_each_entry(nr, nidlist, nr_link) {
1171 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1172 cfs_num_ar_min_max(ar, ¤t_start_nid,
1174 if (last_end_nid != 0 &&
1175 (current_start_nid - last_end_nid != 1))
1177 last_end_nid = current_end_nid;
1178 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1180 list_for_each_entry(re, &el->el_exprs,
1182 if (re->re_stride > 1)
1184 else if (last_hi != 0 &&
1185 re->re_hi - last_hi != 1)
1187 last_hi = re->re_hi;
1197 * Determines whether an expression list in an ip nidrange contains exactly
1198 * one contiguous address range.
1202 * \retval true if contiguous
1203 * \retval false if not contiguous
1205 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1207 struct nidrange *nr;
1208 struct addrrange *ar;
1209 struct cfs_expr_list *el;
1210 struct cfs_range_expr *re;
1214 __u32 last_end_nid = 0;
1215 __u32 current_start_nid = 0;
1216 __u32 current_end_nid = 0;
1218 list_for_each_entry(nr, nidlist, nr_link) {
1219 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1222 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1224 if (last_end_nid != 0 &&
1225 (current_start_nid - last_end_nid != 1))
1227 last_end_nid = current_end_nid;
1228 list_for_each_entry(el,
1229 &ar->ar_numaddr_ranges,
1232 list_for_each_entry(re, &el->el_exprs,
1235 if (re->re_stride > 1 ||
1236 (last_diff > 0 && last_hi != 255) ||
1237 (last_diff > 0 && last_hi == 255 &&
1240 last_hi = re->re_hi;
1241 last_diff = re->re_hi - re->re_lo;
1251 * Takes a linked list of nidrange expressions, determines the minimum
1252 * and maximum nid and creates appropriate nid structures
1258 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1259 char *max_nid, size_t nidstr_length)
1261 struct nidrange *nr;
1262 struct netstrfns *nf = NULL;
1266 char *lndname = NULL;
1267 char min_addr_str[IPSTRING_LENGTH];
1268 char max_addr_str[IPSTRING_LENGTH];
1270 list_for_each_entry(nr, nidlist, nr_link) {
1271 nf = nr->nr_netstrfns;
1272 lndname = nf->nf_name;
1274 netnum = nr->nr_netnum;
1276 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1278 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1279 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1281 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1283 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1288 * Determines the min and max NID values for num LNDs
1294 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1297 struct nidrange *nr;
1298 struct addrrange *ar;
1299 unsigned int tmp_min_addr = 0;
1300 unsigned int tmp_max_addr = 0;
1301 unsigned int min_addr = 0;
1302 unsigned int max_addr = 0;
1304 list_for_each_entry(nr, nidlist, nr_link) {
1305 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1306 cfs_num_ar_min_max(ar, &tmp_min_addr,
1308 if (tmp_min_addr < min_addr || min_addr == 0)
1309 min_addr = tmp_min_addr;
1310 if (tmp_max_addr > max_addr)
1311 max_addr = tmp_min_addr;
1314 *max_nid = max_addr;
1315 *min_nid = min_addr;
1319 * Takes an nidlist and determines the minimum and maximum
1326 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1329 struct nidrange *nr;
1330 struct addrrange *ar;
1331 __u32 tmp_min_ip_addr = 0;
1332 __u32 tmp_max_ip_addr = 0;
1333 __u32 min_ip_addr = 0;
1334 __u32 max_ip_addr = 0;
1336 list_for_each_entry(nr, nidlist, nr_link) {
1337 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1338 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1340 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1341 min_ip_addr = tmp_min_ip_addr;
1342 if (tmp_max_ip_addr > max_ip_addr)
1343 max_ip_addr = tmp_max_ip_addr;
1347 if (min_nid != NULL)
1348 *min_nid = min_ip_addr;
1349 if (max_nid != NULL)
1350 *max_nid = max_ip_addr;