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);
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,
119 static struct netstrfns libcfs_netstrfns[] = {
120 {/* .nf_type */ LOLND,
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,
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,
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,
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 */
264 static const size_t libcfs_nnetstrfns =
265 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
268 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
275 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
277 snprintf(str, size, "%u.%u.%u.%u",
278 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
279 (addr >> 8) & 0xff, addr & 0xff);
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
289 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
295 int n = nob; /* XscanfX */
298 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
300 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
301 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
302 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
309 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
311 snprintf(str, size, "%u", addr);
315 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
317 snprintf(str, size, "0x%x", addr);
321 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
326 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
330 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
334 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
340 static struct netstrfns *
341 libcfs_lnd2netstrfns(__u32 lnd)
345 for (i = 0; i < libcfs_nnetstrfns; i++)
346 if (lnd == libcfs_netstrfns[i].nf_type)
347 return &libcfs_netstrfns[i];
352 static struct netstrfns *
353 libcfs_namenum2netstrfns(const char *name)
355 struct netstrfns *nf;
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)))
367 static struct netstrfns *
368 libcfs_name2netstrfns(const char *name)
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];
381 libcfs_isknown_lnd(__u32 lnd)
383 return libcfs_lnd2netstrfns(lnd) != NULL;
385 EXPORT_SYMBOL(libcfs_isknown_lnd);
388 libcfs_lnd2modname(__u32 lnd)
390 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
392 return (nf == NULL) ? NULL : nf->nf_modname;
394 EXPORT_SYMBOL(libcfs_lnd2modname);
397 libcfs_str2lnd(const char *str)
399 struct netstrfns *nf = libcfs_name2netstrfns(str);
406 EXPORT_SYMBOL(libcfs_str2lnd);
409 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
411 struct netstrfns *nf;
413 nf = libcfs_lnd2netstrfns(lnd);
415 snprintf(buf, buf_size, "?%u?", lnd);
417 snprintf(buf, buf_size, "%s", nf->nf_name);
421 EXPORT_SYMBOL(libcfs_lnd2str_r);
424 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
426 __u32 nnum = LNET_NETNUM(net);
427 __u32 lnd = LNET_NETTYP(net);
428 struct netstrfns *nf;
430 nf = libcfs_lnd2netstrfns(lnd);
432 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
434 snprintf(buf, buf_size, "%s", nf->nf_name);
436 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
440 EXPORT_SYMBOL(libcfs_net2str_r);
443 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
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;
451 if (nid == LNET_NID_ANY) {
452 strncpy(buf, "<?>", buf_size);
453 buf[buf_size - 1] = '\0';
457 nf = libcfs_lnd2netstrfns(lnd);
459 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
463 nf->nf_addr2str(addr, buf, buf_size);
464 addr_len = strlen(buf);
466 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
469 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
475 EXPORT_SYMBOL(libcfs_nid2str_r);
477 static struct netstrfns *
478 libcfs_str2net_internal(const char *str, __u32 *net)
480 struct netstrfns *nf = NULL;
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)))
492 if (i == libcfs_nnetstrfns)
495 nob = strlen(nf->nf_name);
497 if (strlen(str) == (unsigned int)nob) {
500 if (nf->nf_type == LOLND) /* net number not allowed */
505 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
506 i != (int)strlen(str))
510 *net = LNET_MKNET(nf->nf_type, netnum);
515 libcfs_str2net(const char *str)
519 if (libcfs_str2net_internal(str, &net) != NULL)
522 return LNET_NIDNET(LNET_NID_ANY);
524 EXPORT_SYMBOL(libcfs_str2net);
527 libcfs_str2nid(const char *str)
529 const char *sep = strchr(str, '@');
530 struct netstrfns *nf;
535 nf = libcfs_str2net_internal(sep + 1, &net);
539 sep = str + strlen(str);
540 net = LNET_MKNET(SOCKLND, 0);
541 nf = libcfs_lnd2netstrfns(SOCKLND);
545 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
548 return LNET_MKNID(net, addr);
550 EXPORT_SYMBOL(libcfs_str2nid);
553 libcfs_id2str(lnet_process_id_t id)
555 char *str = libcfs_next_nidstring();
557 if (id.pid == LNET_PID_ANY) {
558 snprintf(str, LNET_NIDSTR_SIZE,
559 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
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));
568 EXPORT_SYMBOL(libcfs_id2str);
571 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
573 if (!strcmp(str, "*")) {
574 *nidp = LNET_NID_ANY;
578 *nidp = libcfs_str2nid(str);
579 return *nidp != LNET_NID_ANY;
581 EXPORT_SYMBOL(libcfs_str2anynid);
584 * Nid range list syntax.
587 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
588 * <nidrange> :== <addrrange> '@' <net>
589 * <addrrange> :== '*' |
592 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
594 * <cfs_expr_list> :== <number> |
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"
607 * Structure to represent \<nidrange\> token of the syntax.
609 * One of this is created for each \<net\> parsed.
613 * Link to list of this structures which is built on nid range
616 struct list_head nr_link;
618 * List head for addrrange::ar_link.
620 struct list_head nr_addrranges;
622 * Flag indicating that *@<net> is found.
626 * Pointer to corresponding element of libcfs_netstrfns.
628 struct netstrfns *nr_netstrfns;
630 * Number of network. E.g. 5 if \<net\> is "elan5".
636 * Structure to represent \<addrrange\> token of the syntax.
640 * Link to nidrange::nr_addrranges.
642 struct list_head ar_link;
644 * List head for cfs_expr_list::el_list.
646 struct list_head ar_numaddr_ranges;
650 * Nf_parse_addrlist method for networks using numeric addresses.
652 * Examples of such networks are gm and elan.
654 * \retval 0 if \a str parsed to numeric address
655 * \retval errno otherwise
658 libcfs_num_parse(char *str, int len, struct list_head *list)
660 struct cfs_expr_list *el;
663 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
665 list_add_tail(&el->el_link, list);
671 * Parses \<addrrange\> token on the syntax.
673 * Allocates struct addrrange and links to \a nidrange via
674 * (nidrange::nr_addrranges)
676 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
677 * \retval -errno otherwise
680 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
682 struct addrrange *addrrange;
684 if (src->ls_len == 1 && src->ls_str[0] == '*') {
685 nidrange->nr_all = 1;
689 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
690 if (addrrange == NULL)
692 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
693 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
695 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
697 &addrrange->ar_numaddr_ranges);
701 * Finds or creates struct nidrange.
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.
707 * \retval pointer to struct nidrange matching network specified via \a src
708 * \retval NULL if \a src does not match any network
710 static struct nidrange *
711 add_nidrange(const struct cfs_lstr *src,
712 struct list_head *nidlist)
714 struct netstrfns *nf;
719 if (src->ls_len >= LNET_NIDSTR_SIZE)
722 nf = libcfs_namenum2netstrfns(src->ls_str);
725 endlen = src->ls_len - strlen(nf->nf_name);
727 /* network name only, e.g. "elan" or "tcp" */
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))
738 list_for_each_entry(nr, nidlist, nr_link) {
739 if (nr->nr_netstrfns != nf)
741 if (nr->nr_netnum != netnum)
746 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
749 list_add_tail(&nr->nr_link, nidlist);
750 INIT_LIST_HEAD(&nr->nr_addrranges);
751 nr->nr_netstrfns = nf;
753 nr->nr_netnum = netnum;
759 * Parses \<nidrange\> token of the syntax.
761 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
762 * \retval 0 otherwise
765 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
767 struct cfs_lstr addrrange;
773 if (cfs_gettok(src, '@', &addrrange) == 0)
776 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
779 nr = add_nidrange(&net, nidlist);
783 if (parse_addrange(&addrrange, nr) != 0)
788 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
793 * Frees addrrange structures of \a list.
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.
801 free_addrranges(struct list_head *list)
803 while (!list_empty(list)) {
804 struct addrrange *ar;
806 ar = list_entry(list->next, struct addrrange, ar_link);
808 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
809 list_del(&ar->ar_link);
810 LIBCFS_FREE(ar, sizeof(struct addrrange));
815 * Frees nidrange strutures of \a list.
817 * For each struct nidrange structure found on \a list it frees
818 * addrrange list attached to it and frees the nidrange itself.
823 cfs_free_nidlist(struct list_head *list)
825 struct list_head *pos, *next;
828 list_for_each_safe(pos, next, list) {
829 nr = list_entry(pos, struct nidrange, nr_link);
830 free_addrranges(&nr->nr_addrranges);
832 LIBCFS_FREE(nr, sizeof(struct nidrange));
835 EXPORT_SYMBOL(cfs_free_nidlist);
838 * Parses nid range list.
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
847 * \retval 1 on success
848 * \retval 0 otherwise
851 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
859 INIT_LIST_HEAD(nidlist);
861 rc = cfs_gettok(&src, ' ', &res);
863 cfs_free_nidlist(nidlist);
866 rc = parse_nidrange(&res, nidlist);
868 cfs_free_nidlist(nidlist);
874 EXPORT_SYMBOL(cfs_parse_nidlist);
877 * Nf_match_addr method for networks using numeric addresses
880 * \retval 0 otherwise
883 libcfs_num_match(__u32 addr, struct list_head *numaddr)
885 struct cfs_expr_list *el;
887 LASSERT(!list_empty(numaddr));
888 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
890 return cfs_expr_list_match(addr, el);
894 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
896 * \see cfs_parse_nidlist()
899 * \retval 0 otherwises
901 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
904 struct addrrange *ar;
906 list_for_each_entry(nr, nidlist, nr_link) {
907 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
909 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
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))
920 EXPORT_SYMBOL(cfs_match_nid);
923 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
926 struct cfs_expr_list *el;
928 list_for_each_entry(el, list, el_link) {
930 i += cfs_expr_list_print(buffer + i, count - i, el);
936 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
939 struct cfs_expr_list *el;
941 list_for_each_entry(el, list, el_link) {
944 i += scnprintf(buffer + i, count - i, ".");
945 i += cfs_expr_list_print(buffer + i, count - i, el);
952 * Print the network part of the nidrange \a nr into the specified \a buffer.
954 * \retval number of characters written
957 cfs_print_network(char *buffer, int count, struct nidrange *nr)
959 struct netstrfns *nf = nr->nr_netstrfns;
961 if (nr->nr_netnum == 0)
962 return scnprintf(buffer, count, "@%s", nf->nf_name);
964 return scnprintf(buffer, count, "@%s%u",
965 nf->nf_name, nr->nr_netnum);
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.
973 * \retval number of characters written
976 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
980 struct addrrange *ar;
981 struct netstrfns *nf = nr->nr_netstrfns;
983 list_for_each_entry(ar, addrranges, ar_link) {
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);
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.
998 * \retval number of characters written
1000 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1003 struct nidrange *nr;
1008 list_for_each_entry(nr, nidlist, nr_link) {
1010 i += scnprintf(buffer + i, count - i, " ");
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);
1017 i += cfs_print_addrranges(buffer + i, count - i,
1018 &nr->nr_addrranges, nr);
1023 EXPORT_SYMBOL(cfs_print_nidlist);
1026 * Determines minimum and maximum addresses for a single
1027 * numeric address range
1033 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
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};
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;
1051 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1052 (min_ip[2] << 8) | min_ip[3]);
1054 if (min_nid != NULL)
1055 *min_nid = tmp_ip_addr;
1057 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1058 (max_ip[2] << 8) | max_ip[3]);
1060 if (max_nid != NULL)
1061 *max_nid = tmp_ip_addr;
1065 * Determines minimum and maximum addresses for a single
1066 * numeric address range
1072 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1075 struct cfs_expr_list *el;
1076 struct cfs_range_expr *re;
1077 unsigned int min_addr = 0;
1078 unsigned int max_addr = 0;
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;
1089 if (min_nid != NULL)
1090 *min_nid = min_addr;
1091 if (max_nid != NULL)
1092 *max_nid = max_addr;
1096 * Determines whether an expression list in an nidrange contains exactly
1097 * one contiguous address range. Calls the correct netstrfns for the LND
1101 * \retval true if contiguous
1102 * \retval false if not contiguous
1104 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1106 struct nidrange *nr;
1107 struct netstrfns *nf = NULL;
1108 char *lndname = NULL;
1111 list_for_each_entry(nr, nidlist, nr_link) {
1112 nf = nr->nr_netstrfns;
1113 if (lndname == NULL)
1114 lndname = nf->nf_name;
1116 netnum = nr->nr_netnum;
1118 if (strcmp(lndname, nf->nf_name) != 0 ||
1119 netnum != nr->nr_netnum)
1126 if (!nf->nf_is_contiguous(nidlist))
1131 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
1134 * Determines whether an expression list in an num nidrange contains exactly
1135 * one contiguous address range.
1139 * \retval true if contiguous
1140 * \retval false if not contiguous
1142 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1144 struct nidrange *nr;
1145 struct addrrange *ar;
1146 struct cfs_expr_list *el;
1147 struct cfs_range_expr *re;
1149 __u32 last_end_nid = 0;
1150 __u32 current_start_nid = 0;
1151 __u32 current_end_nid = 0;
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, ¤t_start_nid,
1157 if (last_end_nid != 0 &&
1158 (current_start_nid - last_end_nid != 1))
1160 last_end_nid = current_end_nid;
1161 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1163 list_for_each_entry(re, &el->el_exprs,
1165 if (re->re_stride > 1)
1167 else if (last_hi != 0 &&
1168 re->re_hi - last_hi != 1)
1170 last_hi = re->re_hi;
1180 * Determines whether an expression list in an ip nidrange contains exactly
1181 * one contiguous address range.
1185 * \retval true if contiguous
1186 * \retval false if not contiguous
1188 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1190 struct nidrange *nr;
1191 struct addrrange *ar;
1192 struct cfs_expr_list *el;
1193 struct cfs_range_expr *re;
1197 __u32 last_end_nid = 0;
1198 __u32 current_start_nid = 0;
1199 __u32 current_end_nid = 0;
1201 list_for_each_entry(nr, nidlist, nr_link) {
1202 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1205 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1207 if (last_end_nid != 0 &&
1208 (current_start_nid - last_end_nid != 1))
1210 last_end_nid = current_end_nid;
1211 list_for_each_entry(el,
1212 &ar->ar_numaddr_ranges,
1215 list_for_each_entry(re, &el->el_exprs,
1218 if (re->re_stride > 1 ||
1219 (last_diff > 0 && last_hi != 255) ||
1220 (last_diff > 0 && last_hi == 255 &&
1223 last_hi = re->re_hi;
1224 last_diff = re->re_hi - re->re_lo;
1234 * Takes a linked list of nidrange expressions, determines the minimum
1235 * and maximum nid and creates appropriate nid structures
1241 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1242 char *max_nid, size_t nidstr_length)
1244 struct nidrange *nr;
1245 struct netstrfns *nf = NULL;
1249 char *lndname = NULL;
1250 char min_addr_str[IPSTRING_LENGTH];
1251 char max_addr_str[IPSTRING_LENGTH];
1253 list_for_each_entry(nr, nidlist, nr_link) {
1254 nf = nr->nr_netstrfns;
1255 lndname = nf->nf_name;
1257 netnum = nr->nr_netnum;
1259 nf->nf_min_max(nidlist, &min_addr, &max_addr);
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));
1264 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1266 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1269 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
1272 * Determines the min and max NID values for num LNDs
1278 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
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;
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,
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;
1298 *max_nid = max_addr;
1299 *min_nid = min_addr;
1303 * Takes an nidlist and determines the minimum and maximum
1310 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
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;
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,
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;
1331 if (min_nid != NULL)
1332 *min_nid = min_ip_addr;
1333 if (max_nid != NULL)
1334 *max_nid = max_ip_addr;