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 == ARRAY_SIZE(libcfs_nidstrings))
78 libcfs_nidstring_idx = 0;
80 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
83 EXPORT_SYMBOL(libcfs_next_nidstring);
86 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
93 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
95 snprintf(str, size, "%u.%u.%u.%u",
96 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
97 (addr >> 8) & 0xff, addr & 0xff);
100 /* CAVEAT EMPTOR XscanfX
101 * I use "%n" at the end of a sscanf format to detect trailing junk. However
102 * sscanf may return immediately if it sees the terminating '0' in a string, so
103 * I initialise the %n variable to the expected length. If sscanf sets it;
104 * fine, if it doesn't, then the scan ended at the end of the string, which is
107 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
113 int n = nob; /* XscanfX */
116 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
118 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
119 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
120 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
127 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
130 struct cfs_expr_list *el;
132 list_for_each_entry(el, list, el_link) {
135 i += scnprintf(buffer + i, count - i, ".");
136 i += cfs_expr_list_print(buffer + i, count - i, el);
142 * Print the network part of the nidrange \a nr into the specified \a buffer.
144 * \retval number of characters written
147 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
149 snprintf(str, size, "%u", addr);
153 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
155 snprintf(str, size, "0x%x", addr);
159 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
164 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
168 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
172 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
179 * Nf_parse_addrlist method for networks using numeric addresses.
181 * Examples of such networks are gm and elan.
183 * \retval 0 if \a str parsed to numeric address
184 * \retval errno otherwise
187 libcfs_num_parse(char *str, int len, struct list_head *list)
189 struct cfs_expr_list *el;
192 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
194 list_add_tail(&el->el_link, list);
200 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
203 struct cfs_expr_list *el;
205 list_for_each_entry(el, list, el_link) {
207 i += cfs_expr_list_print(buffer + i, count - i, el);
213 * Nf_match_addr method for networks using numeric addresses
216 * \retval 0 otherwise
219 libcfs_num_match(__u32 addr, struct list_head *numaddr)
221 struct cfs_expr_list *el;
223 LASSERT(!list_empty(numaddr));
224 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
226 return cfs_expr_list_match(addr, el);
229 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
230 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
231 static bool cfs_num_is_contiguous(struct list_head *nidlist);
232 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
234 static struct netstrfns libcfs_netstrfns[] = {
235 {/* .nf_type */ LOLND,
237 /* .nf_modname */ "klolnd",
238 /* .nf_addr2str */ libcfs_decnum_addr2str,
239 /* .nf_str2addr */ libcfs_lo_str2addr,
240 /* .nf_parse_addr*/ libcfs_num_parse,
241 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
242 /* .nf_match_addr*/ libcfs_num_match,
243 /* .nf_is_contiguous */ cfs_num_is_contiguous,
244 /* .nf_min_max */ cfs_num_min_max},
245 {/* .nf_type */ SOCKLND,
246 /* .nf_name */ "tcp",
247 /* .nf_modname */ "ksocklnd",
248 /* .nf_addr2str */ libcfs_ip_addr2str,
249 /* .nf_str2addr */ libcfs_ip_str2addr,
250 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
251 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
252 /* .nf_match_addr*/ cfs_ip_addr_match,
253 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
254 /* .nf_min_max */ cfs_ip_min_max},
255 {/* .nf_type */ O2IBLND,
256 /* .nf_name */ "o2ib",
257 /* .nf_modname */ "ko2iblnd",
258 /* .nf_addr2str */ libcfs_ip_addr2str,
259 /* .nf_str2addr */ libcfs_ip_str2addr,
260 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
261 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
262 /* .nf_match_addr*/ cfs_ip_addr_match,
263 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
264 /* .nf_min_max */ cfs_ip_min_max},
265 {/* .nf_type */ CIBLND,
266 /* .nf_name */ "cib",
267 /* .nf_modname */ "kciblnd",
268 /* .nf_addr2str */ libcfs_ip_addr2str,
269 /* .nf_str2addr */ libcfs_ip_str2addr,
270 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
271 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
272 /* .nf_match_addr*/ cfs_ip_addr_match,
273 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
274 /* .nf_min_max */ cfs_ip_min_max},
275 {/* .nf_type */ OPENIBLND,
276 /* .nf_name */ "openib",
277 /* .nf_modname */ "kopeniblnd",
278 /* .nf_addr2str */ libcfs_ip_addr2str,
279 /* .nf_str2addr */ libcfs_ip_str2addr,
280 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
281 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
282 /* .nf_match_addr*/ cfs_ip_addr_match,
283 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
284 /* .nf_min_max */ cfs_ip_min_max},
285 {/* .nf_type */ IIBLND,
286 /* .nf_name */ "iib",
287 /* .nf_modname */ "kiiblnd",
288 /* .nf_addr2str */ libcfs_ip_addr2str,
289 /* .nf_str2addr */ libcfs_ip_str2addr,
290 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
291 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
292 /* .nf_match_addr*/ cfs_ip_addr_match,
293 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
294 /* .nf_min_max */ cfs_ip_min_max},
295 {/* .nf_type */ VIBLND,
296 /* .nf_name */ "vib",
297 /* .nf_modname */ "kviblnd",
298 /* .nf_addr2str */ libcfs_ip_addr2str,
299 /* .nf_str2addr */ libcfs_ip_str2addr,
300 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
301 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
302 /* .nf_match_addr*/ cfs_ip_addr_match,
303 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
304 /* .nf_min_max */ cfs_ip_min_max},
305 {/* .nf_type */ RALND,
307 /* .nf_modname */ "kralnd",
308 /* .nf_addr2str */ libcfs_ip_addr2str,
309 /* .nf_str2addr */ libcfs_ip_str2addr,
310 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
311 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
312 /* .nf_match_addr*/ cfs_ip_addr_match,
313 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
314 /* .nf_min_max */ cfs_ip_min_max},
315 {/* .nf_type */ QSWLND,
316 /* .nf_name */ "elan",
317 /* .nf_modname */ "kqswlnd",
318 /* .nf_addr2str */ libcfs_decnum_addr2str,
319 /* .nf_str2addr */ libcfs_num_str2addr,
320 /* .nf_parse_addrlist*/ libcfs_num_parse,
321 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
322 /* .nf_match_addr*/ libcfs_num_match,
323 /* .nf_is_contiguous */ cfs_num_is_contiguous,
324 /* .nf_min_max */ cfs_num_min_max},
325 {/* .nf_type */ GMLND,
327 /* .nf_modname */ "kgmlnd",
328 /* .nf_addr2str */ libcfs_hexnum_addr2str,
329 /* .nf_str2addr */ libcfs_num_str2addr,
330 /* .nf_parse_addrlist*/ libcfs_num_parse,
331 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
332 /* .nf_match_addr*/ libcfs_num_match,
333 /* .nf_is_contiguous */ cfs_num_is_contiguous,
334 /* .nf_min_max */ cfs_num_min_max},
335 {/* .nf_type */ MXLND,
337 /* .nf_modname */ "kmxlnd",
338 /* .nf_addr2str */ libcfs_ip_addr2str,
339 /* .nf_str2addr */ libcfs_ip_str2addr,
340 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
341 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
342 /* .nf_match_addr*/ cfs_ip_addr_match,
343 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
344 /* .nf_min_max */ cfs_ip_min_max},
345 {/* .nf_type */ PTLLND,
346 /* .nf_name */ "ptl",
347 /* .nf_modname */ "kptllnd",
348 /* .nf_addr2str */ libcfs_decnum_addr2str,
349 /* .nf_str2addr */ libcfs_num_str2addr,
350 /* .nf_parse_addrlist*/ libcfs_num_parse,
351 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
352 /* .nf_match_addr*/ libcfs_num_match,
353 /* .nf_is_contiguous */ cfs_num_is_contiguous,
354 /* .nf_min_max */ cfs_num_min_max},
355 {/* .nf_type */ GNILND,
356 /* .nf_name */ "gni",
357 /* .nf_modname */ "kgnilnd",
358 /* .nf_addr2str */ libcfs_decnum_addr2str,
359 /* .nf_str2addr */ libcfs_num_str2addr,
360 /* .nf_parse_addrlist*/ libcfs_num_parse,
361 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
362 /* .nf_match_addr*/ libcfs_num_match,
363 /* .nf_is_contiguous */ cfs_num_is_contiguous,
364 /* .nf_min_max */ cfs_num_min_max},
365 {/* .nf_type */ GNIIPLND,
366 /* .nf_name */ "gip",
367 /* .nf_modname */ "kgnilnd",
368 /* .nf_addr2str */ libcfs_ip_addr2str,
369 /* .nf_str2addr */ libcfs_ip_str2addr,
370 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
371 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
372 /* .nf_match_addr*/ cfs_ip_addr_match,
373 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
374 /* .nf_min_max */ cfs_ip_min_max},
375 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
379 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
381 static struct netstrfns *
382 libcfs_lnd2netstrfns(__u32 lnd)
386 for (i = 0; i < libcfs_nnetstrfns; i++)
387 if (lnd == libcfs_netstrfns[i].nf_type)
388 return &libcfs_netstrfns[i];
393 static struct netstrfns *
394 libcfs_namenum2netstrfns(const char *name)
396 struct netstrfns *nf;
399 for (i = 0; i < libcfs_nnetstrfns; i++) {
400 nf = &libcfs_netstrfns[i];
401 if (nf->nf_type >= 0 &&
402 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
408 static struct netstrfns *
409 libcfs_name2netstrfns(const char *name)
413 for (i = 0; i < libcfs_nnetstrfns; i++)
414 if (libcfs_netstrfns[i].nf_type >= 0 &&
415 !strcmp(libcfs_netstrfns[i].nf_name, name))
416 return &libcfs_netstrfns[i];
422 libcfs_isknown_lnd(__u32 lnd)
424 return libcfs_lnd2netstrfns(lnd) != NULL;
426 EXPORT_SYMBOL(libcfs_isknown_lnd);
429 libcfs_lnd2modname(__u32 lnd)
431 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
433 return (nf == NULL) ? NULL : nf->nf_modname;
435 EXPORT_SYMBOL(libcfs_lnd2modname);
438 libcfs_str2lnd(const char *str)
440 struct netstrfns *nf = libcfs_name2netstrfns(str);
447 EXPORT_SYMBOL(libcfs_str2lnd);
450 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
452 struct netstrfns *nf;
454 nf = libcfs_lnd2netstrfns(lnd);
456 snprintf(buf, buf_size, "?%u?", lnd);
458 snprintf(buf, buf_size, "%s", nf->nf_name);
462 EXPORT_SYMBOL(libcfs_lnd2str_r);
465 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
467 __u32 nnum = LNET_NETNUM(net);
468 __u32 lnd = LNET_NETTYP(net);
469 struct netstrfns *nf;
471 nf = libcfs_lnd2netstrfns(lnd);
473 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
475 snprintf(buf, buf_size, "%s", nf->nf_name);
477 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
481 EXPORT_SYMBOL(libcfs_net2str_r);
484 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
486 __u32 addr = LNET_NIDADDR(nid);
487 __u32 net = LNET_NIDNET(nid);
488 __u32 nnum = LNET_NETNUM(net);
489 __u32 lnd = LNET_NETTYP(net);
490 struct netstrfns *nf;
492 if (nid == LNET_NID_ANY) {
493 strncpy(buf, "<?>", buf_size);
494 buf[buf_size - 1] = '\0';
498 nf = libcfs_lnd2netstrfns(lnd);
500 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
504 nf->nf_addr2str(addr, buf, buf_size);
505 addr_len = strlen(buf);
507 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
510 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
516 EXPORT_SYMBOL(libcfs_nid2str_r);
518 static struct netstrfns *
519 libcfs_str2net_internal(const char *str, __u32 *net)
521 struct netstrfns *nf = NULL;
526 for (i = 0; i < libcfs_nnetstrfns; i++) {
527 nf = &libcfs_netstrfns[i];
528 if (nf->nf_type >= 0 &&
529 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
533 if (i == libcfs_nnetstrfns)
536 nob = strlen(nf->nf_name);
538 if (strlen(str) == (unsigned int)nob) {
541 if (nf->nf_type == LOLND) /* net number not allowed */
546 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
547 i != (int)strlen(str))
551 *net = LNET_MKNET(nf->nf_type, netnum);
556 libcfs_str2net(const char *str)
560 if (libcfs_str2net_internal(str, &net) != NULL)
563 return LNET_NIDNET(LNET_NID_ANY);
565 EXPORT_SYMBOL(libcfs_str2net);
568 libcfs_str2nid(const char *str)
570 const char *sep = strchr(str, '@');
571 struct netstrfns *nf;
576 nf = libcfs_str2net_internal(sep + 1, &net);
580 sep = str + strlen(str);
581 net = LNET_MKNET(SOCKLND, 0);
582 nf = libcfs_lnd2netstrfns(SOCKLND);
586 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
589 return LNET_MKNID(net, addr);
591 EXPORT_SYMBOL(libcfs_str2nid);
594 libcfs_id2str(lnet_process_id_t id)
596 char *str = libcfs_next_nidstring();
598 if (id.pid == LNET_PID_ANY) {
599 snprintf(str, LNET_NIDSTR_SIZE,
600 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
604 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
605 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
606 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
609 EXPORT_SYMBOL(libcfs_id2str);
612 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
614 if (!strcmp(str, "*")) {
615 *nidp = LNET_NID_ANY;
619 *nidp = libcfs_str2nid(str);
620 return *nidp != LNET_NID_ANY;
622 EXPORT_SYMBOL(libcfs_str2anynid);
625 * Nid range list syntax.
628 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
629 * <nidrange> :== <addrrange> '@' <net>
630 * <addrrange> :== '*' |
633 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
635 * <cfs_expr_list> :== <number> |
637 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
638 * <range_expr> :== <number> |
639 * <number> '-' <number> |
640 * <number> '-' <number> '/' <number>
641 * <net> :== <netname> | <netname><number>
642 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
643 * "vib" | "ra" | "elan" | "mx" | "ptl"
648 * Structure to represent \<nidrange\> token of the syntax.
650 * One of this is created for each \<net\> parsed.
654 * Link to list of this structures which is built on nid range
657 struct list_head nr_link;
659 * List head for addrrange::ar_link.
661 struct list_head nr_addrranges;
663 * Flag indicating that *@<net> is found.
667 * Pointer to corresponding element of libcfs_netstrfns.
669 struct netstrfns *nr_netstrfns;
671 * Number of network. E.g. 5 if \<net\> is "elan5".
677 * Structure to represent \<addrrange\> token of the syntax.
681 * Link to nidrange::nr_addrranges.
683 struct list_head ar_link;
685 * List head for cfs_expr_list::el_list.
687 struct list_head ar_numaddr_ranges;
691 * Parses \<addrrange\> token on the syntax.
693 * Allocates struct addrrange and links to \a nidrange via
694 * (nidrange::nr_addrranges)
696 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
697 * \retval -errno otherwise
700 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
702 struct addrrange *addrrange;
704 if (src->ls_len == 1 && src->ls_str[0] == '*') {
705 nidrange->nr_all = 1;
709 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
710 if (addrrange == NULL)
712 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
713 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
715 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
717 &addrrange->ar_numaddr_ranges);
721 * Finds or creates struct nidrange.
723 * Checks if \a src is a valid network name, looks for corresponding
724 * nidrange on the ist of nidranges (\a nidlist), creates new struct
725 * nidrange if it is not found.
727 * \retval pointer to struct nidrange matching network specified via \a src
728 * \retval NULL if \a src does not match any network
730 static struct nidrange *
731 add_nidrange(const struct cfs_lstr *src,
732 struct list_head *nidlist)
734 struct netstrfns *nf;
739 if (src->ls_len >= LNET_NIDSTR_SIZE)
742 nf = libcfs_namenum2netstrfns(src->ls_str);
745 endlen = src->ls_len - strlen(nf->nf_name);
747 /* network name only, e.g. "elan" or "tcp" */
750 /* e.g. "elan25" or "tcp23", refuse to parse if
751 * network name is not appended with decimal or
752 * hexadecimal number */
753 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
754 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
758 list_for_each_entry(nr, nidlist, nr_link) {
759 if (nr->nr_netstrfns != nf)
761 if (nr->nr_netnum != netnum)
766 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
769 list_add_tail(&nr->nr_link, nidlist);
770 INIT_LIST_HEAD(&nr->nr_addrranges);
771 nr->nr_netstrfns = nf;
773 nr->nr_netnum = netnum;
779 * Parses \<nidrange\> token of the syntax.
781 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
782 * \retval 0 otherwise
785 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
787 struct cfs_lstr addrrange;
793 if (cfs_gettok(src, '@', &addrrange) == 0)
796 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
799 nr = add_nidrange(&net, nidlist);
803 if (parse_addrange(&addrrange, nr) != 0)
808 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
813 * Frees addrrange structures of \a list.
815 * For each struct addrrange structure found on \a list it frees
816 * cfs_expr_list list attached to it and frees the addrrange itself.
821 free_addrranges(struct list_head *list)
823 while (!list_empty(list)) {
824 struct addrrange *ar;
826 ar = list_entry(list->next, struct addrrange, ar_link);
828 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
829 list_del(&ar->ar_link);
830 LIBCFS_FREE(ar, sizeof(struct addrrange));
835 * Frees nidrange strutures of \a list.
837 * For each struct nidrange structure found on \a list it frees
838 * addrrange list attached to it and frees the nidrange itself.
843 cfs_free_nidlist(struct list_head *list)
845 struct list_head *pos, *next;
848 list_for_each_safe(pos, next, list) {
849 nr = list_entry(pos, struct nidrange, nr_link);
850 free_addrranges(&nr->nr_addrranges);
852 LIBCFS_FREE(nr, sizeof(struct nidrange));
855 EXPORT_SYMBOL(cfs_free_nidlist);
858 * Parses nid range list.
860 * Parses with rigorous syntax and overflow checking \a str into
861 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
862 * structures and links that structure to \a nidlist. The resulting
863 * list can be used to match a NID againts set of NIDS defined by \a
867 * \retval 1 on success
868 * \retval 0 otherwise
871 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
879 INIT_LIST_HEAD(nidlist);
881 rc = cfs_gettok(&src, ' ', &res);
883 cfs_free_nidlist(nidlist);
886 rc = parse_nidrange(&res, nidlist);
888 cfs_free_nidlist(nidlist);
894 EXPORT_SYMBOL(cfs_parse_nidlist);
897 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
899 * \see cfs_parse_nidlist()
902 * \retval 0 otherwises
904 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
907 struct addrrange *ar;
909 list_for_each_entry(nr, nidlist, nr_link) {
910 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
912 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
916 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
917 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
918 &ar->ar_numaddr_ranges))
923 EXPORT_SYMBOL(cfs_match_nid);
926 * Print the network part of the nidrange \a nr into the specified \a buffer.
928 * \retval number of characters written
931 cfs_print_network(char *buffer, int count, struct nidrange *nr)
933 struct netstrfns *nf = nr->nr_netstrfns;
935 if (nr->nr_netnum == 0)
936 return scnprintf(buffer, count, "@%s", nf->nf_name);
938 return scnprintf(buffer, count, "@%s%u",
939 nf->nf_name, nr->nr_netnum);
944 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
945 * At max \a count characters can be printed into \a buffer.
947 * \retval number of characters written
950 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
954 struct addrrange *ar;
955 struct netstrfns *nf = nr->nr_netstrfns;
957 list_for_each_entry(ar, addrranges, ar_link) {
959 i += scnprintf(buffer + i, count - i, " ");
960 i += nf->nf_print_addrlist(buffer + i, count - i,
961 &ar->ar_numaddr_ranges);
962 i += cfs_print_network(buffer + i, count - i, nr);
968 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
969 * At max \a count characters can be printed into \a buffer.
970 * Nidranges are separated by a space character.
972 * \retval number of characters written
974 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
982 list_for_each_entry(nr, nidlist, nr_link) {
984 i += scnprintf(buffer + i, count - i, " ");
986 if (nr->nr_all != 0) {
987 LASSERT(list_empty(&nr->nr_addrranges));
988 i += scnprintf(buffer + i, count - i, "*");
989 i += cfs_print_network(buffer + i, count - i, nr);
991 i += cfs_print_addrranges(buffer + i, count - i,
992 &nr->nr_addrranges, nr);
997 EXPORT_SYMBOL(cfs_print_nidlist);
1000 * Determines minimum and maximum addresses for a single
1001 * numeric address range
1007 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1010 struct cfs_expr_list *el;
1011 struct cfs_range_expr *re;
1012 __u32 tmp_ip_addr = 0;
1013 unsigned int min_ip[4] = {0};
1014 unsigned int max_ip[4] = {0};
1017 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1018 list_for_each_entry(re, &el->el_exprs, re_link) {
1019 min_ip[re_count] = re->re_lo;
1020 max_ip[re_count] = re->re_hi;
1025 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1026 (min_ip[2] << 8) | min_ip[3]);
1028 if (min_nid != NULL)
1029 *min_nid = tmp_ip_addr;
1031 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1032 (max_ip[2] << 8) | max_ip[3]);
1034 if (max_nid != NULL)
1035 *max_nid = tmp_ip_addr;
1039 * Determines minimum and maximum addresses for a single
1040 * numeric address range
1046 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1049 struct cfs_expr_list *el;
1050 struct cfs_range_expr *re;
1051 unsigned int min_addr = 0;
1052 unsigned int max_addr = 0;
1054 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1055 list_for_each_entry(re, &el->el_exprs, re_link) {
1056 if (re->re_lo < min_addr || min_addr == 0)
1057 min_addr = re->re_lo;
1058 if (re->re_hi > max_addr)
1059 max_addr = re->re_hi;
1063 if (min_nid != NULL)
1064 *min_nid = min_addr;
1065 if (max_nid != NULL)
1066 *max_nid = max_addr;
1070 * Determines whether an expression list in an nidrange contains exactly
1071 * one contiguous address range. Calls the correct netstrfns for the LND
1075 * \retval true if contiguous
1076 * \retval false if not contiguous
1078 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1080 struct nidrange *nr;
1081 struct netstrfns *nf = NULL;
1082 char *lndname = NULL;
1085 list_for_each_entry(nr, nidlist, nr_link) {
1086 nf = nr->nr_netstrfns;
1087 if (lndname == NULL)
1088 lndname = nf->nf_name;
1090 netnum = nr->nr_netnum;
1092 if (strcmp(lndname, nf->nf_name) != 0 ||
1093 netnum != nr->nr_netnum)
1100 if (!nf->nf_is_contiguous(nidlist))
1105 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
1108 * Determines whether an expression list in an num nidrange contains exactly
1109 * one contiguous address range.
1113 * \retval true if contiguous
1114 * \retval false if not contiguous
1116 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1118 struct nidrange *nr;
1119 struct addrrange *ar;
1120 struct cfs_expr_list *el;
1121 struct cfs_range_expr *re;
1123 __u32 last_end_nid = 0;
1124 __u32 current_start_nid = 0;
1125 __u32 current_end_nid = 0;
1127 list_for_each_entry(nr, nidlist, nr_link) {
1128 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1129 cfs_num_ar_min_max(ar, ¤t_start_nid,
1131 if (last_end_nid != 0 &&
1132 (current_start_nid - last_end_nid != 1))
1134 last_end_nid = current_end_nid;
1135 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1137 list_for_each_entry(re, &el->el_exprs,
1139 if (re->re_stride > 1)
1141 else if (last_hi != 0 &&
1142 re->re_hi - last_hi != 1)
1144 last_hi = re->re_hi;
1154 * Determines whether an expression list in an ip nidrange contains exactly
1155 * one contiguous address range.
1159 * \retval true if contiguous
1160 * \retval false if not contiguous
1162 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1164 struct nidrange *nr;
1165 struct addrrange *ar;
1166 struct cfs_expr_list *el;
1167 struct cfs_range_expr *re;
1171 __u32 last_end_nid = 0;
1172 __u32 current_start_nid = 0;
1173 __u32 current_end_nid = 0;
1175 list_for_each_entry(nr, nidlist, nr_link) {
1176 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1179 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1181 if (last_end_nid != 0 &&
1182 (current_start_nid - last_end_nid != 1))
1184 last_end_nid = current_end_nid;
1185 list_for_each_entry(el,
1186 &ar->ar_numaddr_ranges,
1189 list_for_each_entry(re, &el->el_exprs,
1192 if (re->re_stride > 1 ||
1193 (last_diff > 0 && last_hi != 255) ||
1194 (last_diff > 0 && last_hi == 255 &&
1197 last_hi = re->re_hi;
1198 last_diff = re->re_hi - re->re_lo;
1208 * Takes a linked list of nidrange expressions, determines the minimum
1209 * and maximum nid and creates appropriate nid structures
1215 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1216 char *max_nid, size_t nidstr_length)
1218 struct nidrange *nr;
1219 struct netstrfns *nf = NULL;
1223 char *lndname = NULL;
1224 char min_addr_str[IPSTRING_LENGTH];
1225 char max_addr_str[IPSTRING_LENGTH];
1227 list_for_each_entry(nr, nidlist, nr_link) {
1228 nf = nr->nr_netstrfns;
1229 lndname = nf->nf_name;
1231 netnum = nr->nr_netnum;
1233 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1235 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1236 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1238 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1240 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1243 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
1246 * Determines the min and max NID values for num LNDs
1252 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1255 struct nidrange *nr;
1256 struct addrrange *ar;
1257 unsigned int tmp_min_addr = 0;
1258 unsigned int tmp_max_addr = 0;
1259 unsigned int min_addr = 0;
1260 unsigned int max_addr = 0;
1262 list_for_each_entry(nr, nidlist, nr_link) {
1263 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1264 cfs_num_ar_min_max(ar, &tmp_min_addr,
1266 if (tmp_min_addr < min_addr || min_addr == 0)
1267 min_addr = tmp_min_addr;
1268 if (tmp_max_addr > max_addr)
1269 max_addr = tmp_min_addr;
1272 *max_nid = max_addr;
1273 *min_nid = min_addr;
1277 * Takes an nidlist and determines the minimum and maximum
1284 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1287 struct nidrange *nr;
1288 struct addrrange *ar;
1289 __u32 tmp_min_ip_addr = 0;
1290 __u32 tmp_max_ip_addr = 0;
1291 __u32 min_ip_addr = 0;
1292 __u32 max_ip_addr = 0;
1294 list_for_each_entry(nr, nidlist, nr_link) {
1295 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1296 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1298 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1299 min_ip_addr = tmp_min_ip_addr;
1300 if (tmp_max_ip_addr > max_ip_addr)
1301 max_ip_addr = tmp_max_ip_addr;
1305 if (min_nid != NULL)
1306 *min_nid = min_ip_addr;
1307 if (max_nid != NULL)
1308 *max_nid = max_ip_addr;