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 * lnet/lnet/nidstrings.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/libcfs.h>
44 #include <lnet/nidstr.h>
46 /* max value for numeric network address */
47 #define MAX_NUMERIC_VALUE 0xffffffff
49 #define IPSTRING_LENGTH 16
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...
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.
63 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
64 static int libcfs_nidstring_idx;
66 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
69 libcfs_next_nidstring(void)
74 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
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;
81 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
84 EXPORT_SYMBOL(libcfs_next_nidstring);
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);
103 static struct netstrfns libcfs_netstrfns[] = {
104 {/* .nf_type */ LOLND,
106 /* .nf_modname */ "klolnd",
107 /* .nf_addr2str */ libcfs_decnum_addr2str,
108 /* .nf_str2addr */ libcfs_lo_str2addr,
109 /* .nf_parse_addr*/ libcfs_num_parse,
110 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
111 /* .nf_match_addr*/ libcfs_num_match,
112 /* .nf_is_contiguous */ cfs_num_is_contiguous,
113 /* .nf_min_max */ cfs_num_min_max},
114 {/* .nf_type */ SOCKLND,
115 /* .nf_name */ "tcp",
116 /* .nf_modname */ "ksocklnd",
117 /* .nf_addr2str */ libcfs_ip_addr2str,
118 /* .nf_str2addr */ libcfs_ip_str2addr,
119 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
120 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
121 /* .nf_match_addr*/ cfs_ip_addr_match,
122 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
123 /* .nf_min_max */ cfs_ip_min_max},
124 {/* .nf_type */ O2IBLND,
125 /* .nf_name */ "o2ib",
126 /* .nf_modname */ "ko2iblnd",
127 /* .nf_addr2str */ libcfs_ip_addr2str,
128 /* .nf_str2addr */ libcfs_ip_str2addr,
129 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
130 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
131 /* .nf_match_addr*/ cfs_ip_addr_match,
132 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
133 /* .nf_min_max */ cfs_ip_min_max},
134 {/* .nf_type */ CIBLND,
135 /* .nf_name */ "cib",
136 /* .nf_modname */ "kciblnd",
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 */ OPENIBLND,
145 /* .nf_name */ "openib",
146 /* .nf_modname */ "kopeniblnd",
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 */ IIBLND,
155 /* .nf_name */ "iib",
156 /* .nf_modname */ "kiiblnd",
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 */ VIBLND,
165 /* .nf_name */ "vib",
166 /* .nf_modname */ "kviblnd",
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 */ RALND,
176 /* .nf_modname */ "kralnd",
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 */ QSWLND,
185 /* .nf_name */ "elan",
186 /* .nf_modname */ "kqswlnd",
187 /* .nf_addr2str */ libcfs_decnum_addr2str,
188 /* .nf_str2addr */ libcfs_num_str2addr,
189 /* .nf_parse_addrlist*/ libcfs_num_parse,
190 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
191 /* .nf_match_addr*/ libcfs_num_match,
192 /* .nf_is_contiguous */ cfs_num_is_contiguous,
193 /* .nf_min_max */ cfs_num_min_max},
194 {/* .nf_type */ GMLND,
196 /* .nf_modname */ "kgmlnd",
197 /* .nf_addr2str */ libcfs_hexnum_addr2str,
198 /* .nf_str2addr */ libcfs_num_str2addr,
199 /* .nf_parse_addrlist*/ libcfs_num_parse,
200 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
201 /* .nf_match_addr*/ libcfs_num_match,
202 /* .nf_is_contiguous */ cfs_num_is_contiguous,
203 /* .nf_min_max */ cfs_num_min_max},
204 {/* .nf_type */ MXLND,
206 /* .nf_modname */ "kmxlnd",
207 /* .nf_addr2str */ libcfs_ip_addr2str,
208 /* .nf_str2addr */ libcfs_ip_str2addr,
209 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
210 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
211 /* .nf_match_addr*/ cfs_ip_addr_match,
212 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
213 /* .nf_min_max */ cfs_ip_min_max},
214 {/* .nf_type */ PTLLND,
215 /* .nf_name */ "ptl",
216 /* .nf_modname */ "kptllnd",
217 /* .nf_addr2str */ libcfs_decnum_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 */ GNILND,
225 /* .nf_name */ "gni",
226 /* .nf_modname */ "kgnilnd",
227 /* .nf_addr2str */ libcfs_decnum_addr2str,
228 /* .nf_str2addr */ libcfs_num_str2addr,
229 /* .nf_parse_addrlist*/ libcfs_num_parse,
230 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
231 /* .nf_match_addr*/ libcfs_num_match,
232 /* .nf_is_contiguous */ cfs_num_is_contiguous,
233 /* .nf_min_max */ cfs_num_min_max},
234 {/* .nf_type */ GNIIPLND,
235 /* .nf_name */ "gip",
236 /* .nf_modname */ "kgnilnd",
237 /* .nf_addr2str */ libcfs_ip_addr2str,
238 /* .nf_str2addr */ libcfs_ip_str2addr,
239 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
240 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
241 /* .nf_match_addr*/ cfs_ip_addr_match,
242 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
243 /* .nf_min_max */ cfs_ip_min_max},
244 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
248 static const size_t libcfs_nnetstrfns =
249 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
252 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
259 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
261 snprintf(str, size, "%u.%u.%u.%u",
262 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
263 (addr >> 8) & 0xff, addr & 0xff);
266 /* CAVEAT EMPTOR XscanfX
267 * I use "%n" at the end of a sscanf format to detect trailing junk. However
268 * sscanf may return immediately if it sees the terminating '0' in a string, so
269 * I initialise the %n variable to the expected length. If sscanf sets it;
270 * fine, if it doesn't, then the scan ended at the end of the string, which is
273 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
279 int n = nob; /* XscanfX */
282 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
284 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
285 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
286 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
293 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
295 snprintf(str, size, "%u", addr);
299 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
301 snprintf(str, size, "0x%x", addr);
305 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
310 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
314 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
318 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
324 static struct netstrfns *
325 libcfs_lnd2netstrfns(__u32 lnd)
329 for (i = 0; i < libcfs_nnetstrfns; i++)
330 if (lnd == libcfs_netstrfns[i].nf_type)
331 return &libcfs_netstrfns[i];
336 static struct netstrfns *
337 libcfs_namenum2netstrfns(const char *name)
339 struct netstrfns *nf;
342 for (i = 0; i < libcfs_nnetstrfns; i++) {
343 nf = &libcfs_netstrfns[i];
344 if (nf->nf_type >= 0 &&
345 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
351 static struct netstrfns *
352 libcfs_name2netstrfns(const char *name)
356 for (i = 0; i < libcfs_nnetstrfns; i++)
357 if (libcfs_netstrfns[i].nf_type >= 0 &&
358 !strcmp(libcfs_netstrfns[i].nf_name, name))
359 return &libcfs_netstrfns[i];
365 libcfs_isknown_lnd(__u32 lnd)
367 return libcfs_lnd2netstrfns(lnd) != NULL;
369 EXPORT_SYMBOL(libcfs_isknown_lnd);
372 libcfs_lnd2modname(__u32 lnd)
374 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
376 return (nf == NULL) ? NULL : nf->nf_modname;
378 EXPORT_SYMBOL(libcfs_lnd2modname);
381 libcfs_str2lnd(const char *str)
383 struct netstrfns *nf = libcfs_name2netstrfns(str);
390 EXPORT_SYMBOL(libcfs_str2lnd);
393 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
395 struct netstrfns *nf;
397 nf = libcfs_lnd2netstrfns(lnd);
399 snprintf(buf, buf_size, "?%u?", lnd);
401 snprintf(buf, buf_size, "%s", nf->nf_name);
405 EXPORT_SYMBOL(libcfs_lnd2str_r);
408 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
410 __u32 nnum = LNET_NETNUM(net);
411 __u32 lnd = LNET_NETTYP(net);
412 struct netstrfns *nf;
414 nf = libcfs_lnd2netstrfns(lnd);
416 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
418 snprintf(buf, buf_size, "%s", nf->nf_name);
420 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
424 EXPORT_SYMBOL(libcfs_net2str_r);
427 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
429 __u32 addr = LNET_NIDADDR(nid);
430 __u32 net = LNET_NIDNET(nid);
431 __u32 nnum = LNET_NETNUM(net);
432 __u32 lnd = LNET_NETTYP(net);
433 struct netstrfns *nf;
435 if (nid == LNET_NID_ANY) {
436 strncpy(buf, "<?>", buf_size);
437 buf[buf_size - 1] = '\0';
441 nf = libcfs_lnd2netstrfns(lnd);
443 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
447 nf->nf_addr2str(addr, buf, buf_size);
448 addr_len = strlen(buf);
450 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
453 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
459 EXPORT_SYMBOL(libcfs_nid2str_r);
461 static struct netstrfns *
462 libcfs_str2net_internal(const char *str, __u32 *net)
464 struct netstrfns *nf = NULL;
469 for (i = 0; i < libcfs_nnetstrfns; i++) {
470 nf = &libcfs_netstrfns[i];
471 if (nf->nf_type >= 0 &&
472 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
476 if (i == libcfs_nnetstrfns)
479 nob = strlen(nf->nf_name);
481 if (strlen(str) == (unsigned int)nob) {
484 if (nf->nf_type == LOLND) /* net number not allowed */
489 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
490 i != (int)strlen(str))
494 *net = LNET_MKNET(nf->nf_type, netnum);
499 libcfs_str2net(const char *str)
503 if (libcfs_str2net_internal(str, &net) != NULL)
506 return LNET_NIDNET(LNET_NID_ANY);
508 EXPORT_SYMBOL(libcfs_str2net);
511 libcfs_str2nid(const char *str)
513 const char *sep = strchr(str, '@');
514 struct netstrfns *nf;
519 nf = libcfs_str2net_internal(sep + 1, &net);
523 sep = str + strlen(str);
524 net = LNET_MKNET(SOCKLND, 0);
525 nf = libcfs_lnd2netstrfns(SOCKLND);
529 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
532 return LNET_MKNID(net, addr);
534 EXPORT_SYMBOL(libcfs_str2nid);
537 libcfs_id2str(lnet_process_id_t id)
539 char *str = libcfs_next_nidstring();
541 if (id.pid == LNET_PID_ANY) {
542 snprintf(str, LNET_NIDSTR_SIZE,
543 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
547 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
548 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
549 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
552 EXPORT_SYMBOL(libcfs_id2str);
555 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
557 if (!strcmp(str, "*")) {
558 *nidp = LNET_NID_ANY;
562 *nidp = libcfs_str2nid(str);
563 return *nidp != LNET_NID_ANY;
565 EXPORT_SYMBOL(libcfs_str2anynid);
568 * Nid range list syntax.
571 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
572 * <nidrange> :== <addrrange> '@' <net>
573 * <addrrange> :== '*' |
576 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
578 * <cfs_expr_list> :== <number> |
580 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
581 * <range_expr> :== <number> |
582 * <number> '-' <number> |
583 * <number> '-' <number> '/' <number>
584 * <net> :== <netname> | <netname><number>
585 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
586 * "vib" | "ra" | "elan" | "mx" | "ptl"
591 * Structure to represent \<nidrange\> token of the syntax.
593 * One of this is created for each \<net\> parsed.
597 * Link to list of this structures which is built on nid range
600 struct list_head nr_link;
602 * List head for addrrange::ar_link.
604 struct list_head nr_addrranges;
606 * Flag indicating that *@<net> is found.
610 * Pointer to corresponding element of libcfs_netstrfns.
612 struct netstrfns *nr_netstrfns;
614 * Number of network. E.g. 5 if \<net\> is "elan5".
620 * Structure to represent \<addrrange\> token of the syntax.
624 * Link to nidrange::nr_addrranges.
626 struct list_head ar_link;
628 * List head for cfs_expr_list::el_list.
630 struct list_head ar_numaddr_ranges;
634 * Nf_parse_addrlist method for networks using numeric addresses.
636 * Examples of such networks are gm and elan.
638 * \retval 0 if \a str parsed to numeric address
639 * \retval errno otherwise
642 libcfs_num_parse(char *str, int len, struct list_head *list)
644 struct cfs_expr_list *el;
647 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
649 list_add_tail(&el->el_link, list);
655 * Parses \<addrrange\> token on the syntax.
657 * Allocates struct addrrange and links to \a nidrange via
658 * (nidrange::nr_addrranges)
660 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
661 * \retval -errno otherwise
664 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
666 struct addrrange *addrrange;
668 if (src->ls_len == 1 && src->ls_str[0] == '*') {
669 nidrange->nr_all = 1;
673 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
674 if (addrrange == NULL)
676 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
677 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
679 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
681 &addrrange->ar_numaddr_ranges);
685 * Finds or creates struct nidrange.
687 * Checks if \a src is a valid network name, looks for corresponding
688 * nidrange on the ist of nidranges (\a nidlist), creates new struct
689 * nidrange if it is not found.
691 * \retval pointer to struct nidrange matching network specified via \a src
692 * \retval NULL if \a src does not match any network
694 static struct nidrange *
695 add_nidrange(const struct cfs_lstr *src,
696 struct list_head *nidlist)
698 struct netstrfns *nf;
703 if (src->ls_len >= LNET_NIDSTR_SIZE)
706 nf = libcfs_namenum2netstrfns(src->ls_str);
709 endlen = src->ls_len - strlen(nf->nf_name);
711 /* network name only, e.g. "elan" or "tcp" */
714 /* e.g. "elan25" or "tcp23", refuse to parse if
715 * network name is not appended with decimal or
716 * hexadecimal number */
717 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
718 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
722 list_for_each_entry(nr, nidlist, nr_link) {
723 if (nr->nr_netstrfns != nf)
725 if (nr->nr_netnum != netnum)
730 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
733 list_add_tail(&nr->nr_link, nidlist);
734 INIT_LIST_HEAD(&nr->nr_addrranges);
735 nr->nr_netstrfns = nf;
737 nr->nr_netnum = netnum;
743 * Parses \<nidrange\> token of the syntax.
745 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
746 * \retval 0 otherwise
749 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
751 struct cfs_lstr addrrange;
757 if (cfs_gettok(src, '@', &addrrange) == 0)
760 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
763 nr = add_nidrange(&net, nidlist);
767 if (parse_addrange(&addrrange, nr) != 0)
772 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
777 * Frees addrrange structures of \a list.
779 * For each struct addrrange structure found on \a list it frees
780 * cfs_expr_list list attached to it and frees the addrrange itself.
785 free_addrranges(struct list_head *list)
787 while (!list_empty(list)) {
788 struct addrrange *ar;
790 ar = list_entry(list->next, struct addrrange, ar_link);
792 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
793 list_del(&ar->ar_link);
794 LIBCFS_FREE(ar, sizeof(struct addrrange));
799 * Frees nidrange strutures of \a list.
801 * For each struct nidrange structure found on \a list it frees
802 * addrrange list attached to it and frees the nidrange itself.
807 cfs_free_nidlist(struct list_head *list)
809 struct list_head *pos, *next;
812 list_for_each_safe(pos, next, list) {
813 nr = list_entry(pos, struct nidrange, nr_link);
814 free_addrranges(&nr->nr_addrranges);
816 LIBCFS_FREE(nr, sizeof(struct nidrange));
819 EXPORT_SYMBOL(cfs_free_nidlist);
822 * Parses nid range list.
824 * Parses with rigorous syntax and overflow checking \a str into
825 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
826 * structures and links that structure to \a nidlist. The resulting
827 * list can be used to match a NID againts set of NIDS defined by \a
831 * \retval 1 on success
832 * \retval 0 otherwise
835 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
843 INIT_LIST_HEAD(nidlist);
845 rc = cfs_gettok(&src, ' ', &res);
847 cfs_free_nidlist(nidlist);
850 rc = parse_nidrange(&res, nidlist);
852 cfs_free_nidlist(nidlist);
858 EXPORT_SYMBOL(cfs_parse_nidlist);
861 * Nf_match_addr method for networks using numeric addresses
864 * \retval 0 otherwise
867 libcfs_num_match(__u32 addr, struct list_head *numaddr)
869 struct cfs_expr_list *el;
871 LASSERT(!list_empty(numaddr));
872 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
874 return cfs_expr_list_match(addr, el);
878 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
880 * \see cfs_parse_nidlist()
883 * \retval 0 otherwises
885 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
888 struct addrrange *ar;
890 list_for_each_entry(nr, nidlist, nr_link) {
891 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
893 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
897 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
898 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
899 &ar->ar_numaddr_ranges))
904 EXPORT_SYMBOL(cfs_match_nid);
907 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
910 struct cfs_expr_list *el;
912 list_for_each_entry(el, list, el_link) {
914 i += cfs_expr_list_print(buffer + i, count - i, el);
920 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
923 struct cfs_expr_list *el;
925 list_for_each_entry(el, list, el_link) {
928 i += scnprintf(buffer + i, count - i, ".");
929 i += cfs_expr_list_print(buffer + i, count - i, el);
936 * Print the network part of the nidrange \a nr into the specified \a buffer.
938 * \retval number of characters written
941 cfs_print_network(char *buffer, int count, struct nidrange *nr)
943 struct netstrfns *nf = nr->nr_netstrfns;
945 if (nr->nr_netnum == 0)
946 return scnprintf(buffer, count, "@%s", nf->nf_name);
948 return scnprintf(buffer, count, "@%s%u",
949 nf->nf_name, nr->nr_netnum);
954 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
955 * At max \a count characters can be printed into \a buffer.
957 * \retval number of characters written
960 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
964 struct addrrange *ar;
965 struct netstrfns *nf = nr->nr_netstrfns;
967 list_for_each_entry(ar, addrranges, ar_link) {
969 i += scnprintf(buffer + i, count - i, " ");
970 i += nf->nf_print_addrlist(buffer + i, count - i,
971 &ar->ar_numaddr_ranges);
972 i += cfs_print_network(buffer + i, count - i, nr);
978 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
979 * At max \a count characters can be printed into \a buffer.
980 * Nidranges are separated by a space character.
982 * \retval number of characters written
984 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
992 list_for_each_entry(nr, nidlist, nr_link) {
994 i += scnprintf(buffer + i, count - i, " ");
996 if (nr->nr_all != 0) {
997 LASSERT(list_empty(&nr->nr_addrranges));
998 i += scnprintf(buffer + i, count - i, "*");
999 i += cfs_print_network(buffer + i, count - i, nr);
1001 i += cfs_print_addrranges(buffer + i, count - i,
1002 &nr->nr_addrranges, nr);
1007 EXPORT_SYMBOL(cfs_print_nidlist);
1010 * Determines minimum and maximum addresses for a single
1011 * numeric address range
1017 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1020 struct cfs_expr_list *el;
1021 struct cfs_range_expr *re;
1022 __u32 tmp_ip_addr = 0;
1023 unsigned int min_ip[4] = {0};
1024 unsigned int max_ip[4] = {0};
1027 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1028 list_for_each_entry(re, &el->el_exprs, re_link) {
1029 min_ip[re_count] = re->re_lo;
1030 max_ip[re_count] = re->re_hi;
1035 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1036 (min_ip[2] << 8) | min_ip[3]);
1038 if (min_nid != NULL)
1039 *min_nid = tmp_ip_addr;
1041 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1042 (max_ip[2] << 8) | max_ip[3]);
1044 if (max_nid != NULL)
1045 *max_nid = tmp_ip_addr;
1049 * Determines minimum and maximum addresses for a single
1050 * numeric address range
1056 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1059 struct cfs_expr_list *el;
1060 struct cfs_range_expr *re;
1061 unsigned int min_addr = 0;
1062 unsigned int max_addr = 0;
1064 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1065 list_for_each_entry(re, &el->el_exprs, re_link) {
1066 if (re->re_lo < min_addr || min_addr == 0)
1067 min_addr = re->re_lo;
1068 if (re->re_hi > max_addr)
1069 max_addr = re->re_hi;
1073 if (min_nid != NULL)
1074 *min_nid = min_addr;
1075 if (max_nid != NULL)
1076 *max_nid = max_addr;
1080 * Determines whether an expression list in an nidrange contains exactly
1081 * one contiguous address range. Calls the correct netstrfns for the LND
1085 * \retval true if contiguous
1086 * \retval false if not contiguous
1088 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1090 struct nidrange *nr;
1091 struct netstrfns *nf = NULL;
1092 char *lndname = NULL;
1095 list_for_each_entry(nr, nidlist, nr_link) {
1096 nf = nr->nr_netstrfns;
1097 if (lndname == NULL)
1098 lndname = nf->nf_name;
1100 netnum = nr->nr_netnum;
1102 if (strcmp(lndname, nf->nf_name) != 0 ||
1103 netnum != nr->nr_netnum)
1110 if (!nf->nf_is_contiguous(nidlist))
1115 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
1118 * Determines whether an expression list in an num nidrange contains exactly
1119 * one contiguous address range.
1123 * \retval true if contiguous
1124 * \retval false if not contiguous
1126 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1128 struct nidrange *nr;
1129 struct addrrange *ar;
1130 struct cfs_expr_list *el;
1131 struct cfs_range_expr *re;
1133 __u32 last_end_nid = 0;
1134 __u32 current_start_nid = 0;
1135 __u32 current_end_nid = 0;
1137 list_for_each_entry(nr, nidlist, nr_link) {
1138 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1139 cfs_num_ar_min_max(ar, ¤t_start_nid,
1141 if (last_end_nid != 0 &&
1142 (current_start_nid - last_end_nid != 1))
1144 last_end_nid = current_end_nid;
1145 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1147 list_for_each_entry(re, &el->el_exprs,
1149 if (re->re_stride > 1)
1151 else if (last_hi != 0 &&
1152 re->re_hi - last_hi != 1)
1154 last_hi = re->re_hi;
1164 * Determines whether an expression list in an ip nidrange contains exactly
1165 * one contiguous address range.
1169 * \retval true if contiguous
1170 * \retval false if not contiguous
1172 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1174 struct nidrange *nr;
1175 struct addrrange *ar;
1176 struct cfs_expr_list *el;
1177 struct cfs_range_expr *re;
1181 __u32 last_end_nid = 0;
1182 __u32 current_start_nid = 0;
1183 __u32 current_end_nid = 0;
1185 list_for_each_entry(nr, nidlist, nr_link) {
1186 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1189 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1191 if (last_end_nid != 0 &&
1192 (current_start_nid - last_end_nid != 1))
1194 last_end_nid = current_end_nid;
1195 list_for_each_entry(el,
1196 &ar->ar_numaddr_ranges,
1199 list_for_each_entry(re, &el->el_exprs,
1202 if (re->re_stride > 1 ||
1203 (last_diff > 0 && last_hi != 255) ||
1204 (last_diff > 0 && last_hi == 255 &&
1207 last_hi = re->re_hi;
1208 last_diff = re->re_hi - re->re_lo;
1218 * Takes a linked list of nidrange expressions, determines the minimum
1219 * and maximum nid and creates appropriate nid structures
1225 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1226 char *max_nid, size_t nidstr_length)
1228 struct nidrange *nr;
1229 struct netstrfns *nf = NULL;
1233 char *lndname = NULL;
1234 char min_addr_str[IPSTRING_LENGTH];
1235 char max_addr_str[IPSTRING_LENGTH];
1237 list_for_each_entry(nr, nidlist, nr_link) {
1238 nf = nr->nr_netstrfns;
1239 lndname = nf->nf_name;
1241 netnum = nr->nr_netnum;
1243 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1245 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1246 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1248 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1250 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1253 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
1256 * Determines the min and max NID values for num LNDs
1262 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1265 struct nidrange *nr;
1266 struct addrrange *ar;
1267 unsigned int tmp_min_addr = 0;
1268 unsigned int tmp_max_addr = 0;
1269 unsigned int min_addr = 0;
1270 unsigned int max_addr = 0;
1272 list_for_each_entry(nr, nidlist, nr_link) {
1273 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1274 cfs_num_ar_min_max(ar, &tmp_min_addr,
1276 if (tmp_min_addr < min_addr || min_addr == 0)
1277 min_addr = tmp_min_addr;
1278 if (tmp_max_addr > max_addr)
1279 max_addr = tmp_min_addr;
1282 *max_nid = max_addr;
1283 *min_nid = min_addr;
1287 * Takes an nidlist and determines the minimum and maximum
1294 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1297 struct nidrange *nr;
1298 struct addrrange *ar;
1299 __u32 tmp_min_ip_addr = 0;
1300 __u32 tmp_max_ip_addr = 0;
1301 __u32 min_ip_addr = 0;
1302 __u32 max_ip_addr = 0;
1304 list_for_each_entry(nr, nidlist, nr_link) {
1305 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1306 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1308 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1309 min_ip_addr = tmp_min_ip_addr;
1310 if (tmp_max_ip_addr > max_ip_addr)
1311 max_ip_addr = tmp_max_ip_addr;
1315 if (min_nid != NULL)
1316 *min_nid = min_ip_addr;
1317 if (max_nid != NULL)
1318 *max_nid = max_ip_addr;