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);
126 /* Used by lnet/config.c so it can't be static */
128 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
130 struct cfs_expr_list *el;
139 while (src.ls_str != NULL) {
142 if (!cfs_gettok(&src, '.', &res)) {
147 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
151 list_add_tail(&el->el_link, list);
160 cfs_expr_list_free_list(list);
166 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
169 struct cfs_expr_list *el;
171 list_for_each_entry(el, list, el_link) {
174 i += scnprintf(buffer + i, count - i, ".");
175 i += cfs_expr_list_print(buffer + i, count - i, el);
181 * Matches address (\a addr) against address set encoded in \a list.
183 * \retval 1 if \a addr matches
184 * \retval 0 otherwise
187 cfs_ip_addr_match(__u32 addr, struct list_head *list)
189 struct cfs_expr_list *el;
192 list_for_each_entry_reverse(el, list, el_link) {
193 if (!cfs_expr_list_match(addr & 0xff, el))
203 * Print the network part of the nidrange \a nr into the specified \a buffer.
205 * \retval number of characters written
208 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
210 snprintf(str, size, "%u", addr);
214 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
216 snprintf(str, size, "0x%x", addr);
220 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
225 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
229 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
233 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
240 * Nf_parse_addrlist method for networks using numeric addresses.
242 * Examples of such networks are gm and elan.
244 * \retval 0 if \a str parsed to numeric address
245 * \retval errno otherwise
248 libcfs_num_parse(char *str, int len, struct list_head *list)
250 struct cfs_expr_list *el;
253 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
255 list_add_tail(&el->el_link, list);
261 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
264 struct cfs_expr_list *el;
266 list_for_each_entry(el, list, el_link) {
268 i += cfs_expr_list_print(buffer + i, count - i, el);
274 * Nf_match_addr method for networks using numeric addresses
277 * \retval 0 otherwise
280 libcfs_num_match(__u32 addr, struct list_head *numaddr)
282 struct cfs_expr_list *el;
284 LASSERT(!list_empty(numaddr));
285 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
287 return cfs_expr_list_match(addr, el);
290 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
291 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
292 static bool cfs_num_is_contiguous(struct list_head *nidlist);
293 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
295 static struct netstrfns libcfs_netstrfns[] = {
296 {/* .nf_type */ LOLND,
298 /* .nf_modname */ "klolnd",
299 /* .nf_addr2str */ libcfs_decnum_addr2str,
300 /* .nf_str2addr */ libcfs_lo_str2addr,
301 /* .nf_parse_addr*/ libcfs_num_parse,
302 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
303 /* .nf_match_addr*/ libcfs_num_match,
304 /* .nf_is_contiguous */ cfs_num_is_contiguous,
305 /* .nf_min_max */ cfs_num_min_max},
306 {/* .nf_type */ SOCKLND,
307 /* .nf_name */ "tcp",
308 /* .nf_modname */ "ksocklnd",
309 /* .nf_addr2str */ libcfs_ip_addr2str,
310 /* .nf_str2addr */ libcfs_ip_str2addr,
311 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
312 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
313 /* .nf_match_addr*/ cfs_ip_addr_match,
314 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
315 /* .nf_min_max */ cfs_ip_min_max},
316 {/* .nf_type */ O2IBLND,
317 /* .nf_name */ "o2ib",
318 /* .nf_modname */ "ko2iblnd",
319 /* .nf_addr2str */ libcfs_ip_addr2str,
320 /* .nf_str2addr */ libcfs_ip_str2addr,
321 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
322 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
323 /* .nf_match_addr*/ cfs_ip_addr_match,
324 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
325 /* .nf_min_max */ cfs_ip_min_max},
326 {/* .nf_type */ CIBLND,
327 /* .nf_name */ "cib",
328 /* .nf_modname */ "kciblnd",
329 /* .nf_addr2str */ libcfs_ip_addr2str,
330 /* .nf_str2addr */ libcfs_ip_str2addr,
331 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
332 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
333 /* .nf_match_addr*/ cfs_ip_addr_match,
334 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
335 /* .nf_min_max */ cfs_ip_min_max},
336 {/* .nf_type */ OPENIBLND,
337 /* .nf_name */ "openib",
338 /* .nf_modname */ "kopeniblnd",
339 /* .nf_addr2str */ libcfs_ip_addr2str,
340 /* .nf_str2addr */ libcfs_ip_str2addr,
341 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
342 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
343 /* .nf_match_addr*/ cfs_ip_addr_match,
344 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
345 /* .nf_min_max */ cfs_ip_min_max},
346 {/* .nf_type */ IIBLND,
347 /* .nf_name */ "iib",
348 /* .nf_modname */ "kiiblnd",
349 /* .nf_addr2str */ libcfs_ip_addr2str,
350 /* .nf_str2addr */ libcfs_ip_str2addr,
351 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
352 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
353 /* .nf_match_addr*/ cfs_ip_addr_match,
354 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
355 /* .nf_min_max */ cfs_ip_min_max},
356 {/* .nf_type */ VIBLND,
357 /* .nf_name */ "vib",
358 /* .nf_modname */ "kviblnd",
359 /* .nf_addr2str */ libcfs_ip_addr2str,
360 /* .nf_str2addr */ libcfs_ip_str2addr,
361 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
362 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
363 /* .nf_match_addr*/ cfs_ip_addr_match,
364 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
365 /* .nf_min_max */ cfs_ip_min_max},
366 {/* .nf_type */ RALND,
368 /* .nf_modname */ "kralnd",
369 /* .nf_addr2str */ libcfs_ip_addr2str,
370 /* .nf_str2addr */ libcfs_ip_str2addr,
371 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
372 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
373 /* .nf_match_addr*/ cfs_ip_addr_match,
374 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
375 /* .nf_min_max */ cfs_ip_min_max},
376 {/* .nf_type */ QSWLND,
377 /* .nf_name */ "elan",
378 /* .nf_modname */ "kqswlnd",
379 /* .nf_addr2str */ libcfs_decnum_addr2str,
380 /* .nf_str2addr */ libcfs_num_str2addr,
381 /* .nf_parse_addrlist*/ libcfs_num_parse,
382 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
383 /* .nf_match_addr*/ libcfs_num_match,
384 /* .nf_is_contiguous */ cfs_num_is_contiguous,
385 /* .nf_min_max */ cfs_num_min_max},
386 {/* .nf_type */ GMLND,
388 /* .nf_modname */ "kgmlnd",
389 /* .nf_addr2str */ libcfs_hexnum_addr2str,
390 /* .nf_str2addr */ libcfs_num_str2addr,
391 /* .nf_parse_addrlist*/ libcfs_num_parse,
392 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
393 /* .nf_match_addr*/ libcfs_num_match,
394 /* .nf_is_contiguous */ cfs_num_is_contiguous,
395 /* .nf_min_max */ cfs_num_min_max},
396 {/* .nf_type */ MXLND,
398 /* .nf_modname */ "kmxlnd",
399 /* .nf_addr2str */ libcfs_ip_addr2str,
400 /* .nf_str2addr */ libcfs_ip_str2addr,
401 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
402 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
403 /* .nf_match_addr*/ cfs_ip_addr_match,
404 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
405 /* .nf_min_max */ cfs_ip_min_max},
406 {/* .nf_type */ PTLLND,
407 /* .nf_name */ "ptl",
408 /* .nf_modname */ "kptllnd",
409 /* .nf_addr2str */ libcfs_decnum_addr2str,
410 /* .nf_str2addr */ libcfs_num_str2addr,
411 /* .nf_parse_addrlist*/ libcfs_num_parse,
412 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
413 /* .nf_match_addr*/ libcfs_num_match,
414 /* .nf_is_contiguous */ cfs_num_is_contiguous,
415 /* .nf_min_max */ cfs_num_min_max},
416 {/* .nf_type */ GNILND,
417 /* .nf_name */ "gni",
418 /* .nf_modname */ "kgnilnd",
419 /* .nf_addr2str */ libcfs_decnum_addr2str,
420 /* .nf_str2addr */ libcfs_num_str2addr,
421 /* .nf_parse_addrlist*/ libcfs_num_parse,
422 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
423 /* .nf_match_addr*/ libcfs_num_match,
424 /* .nf_is_contiguous */ cfs_num_is_contiguous,
425 /* .nf_min_max */ cfs_num_min_max},
426 {/* .nf_type */ GNIIPLND,
427 /* .nf_name */ "gip",
428 /* .nf_modname */ "kgnilnd",
429 /* .nf_addr2str */ libcfs_ip_addr2str,
430 /* .nf_str2addr */ libcfs_ip_str2addr,
431 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
432 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
433 /* .nf_match_addr*/ cfs_ip_addr_match,
434 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
435 /* .nf_min_max */ cfs_ip_min_max},
436 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
440 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
442 static struct netstrfns *
443 libcfs_lnd2netstrfns(__u32 lnd)
447 for (i = 0; i < libcfs_nnetstrfns; i++)
448 if (lnd == libcfs_netstrfns[i].nf_type)
449 return &libcfs_netstrfns[i];
454 static struct netstrfns *
455 libcfs_namenum2netstrfns(const char *name)
457 struct netstrfns *nf;
460 for (i = 0; i < libcfs_nnetstrfns; i++) {
461 nf = &libcfs_netstrfns[i];
462 if (nf->nf_type >= 0 &&
463 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
469 static struct netstrfns *
470 libcfs_name2netstrfns(const char *name)
474 for (i = 0; i < libcfs_nnetstrfns; i++)
475 if (libcfs_netstrfns[i].nf_type >= 0 &&
476 !strcmp(libcfs_netstrfns[i].nf_name, name))
477 return &libcfs_netstrfns[i];
483 libcfs_isknown_lnd(__u32 lnd)
485 return libcfs_lnd2netstrfns(lnd) != NULL;
487 EXPORT_SYMBOL(libcfs_isknown_lnd);
490 libcfs_lnd2modname(__u32 lnd)
492 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
494 return (nf == NULL) ? NULL : nf->nf_modname;
496 EXPORT_SYMBOL(libcfs_lnd2modname);
499 libcfs_str2lnd(const char *str)
501 struct netstrfns *nf = libcfs_name2netstrfns(str);
508 EXPORT_SYMBOL(libcfs_str2lnd);
511 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
513 struct netstrfns *nf;
515 nf = libcfs_lnd2netstrfns(lnd);
517 snprintf(buf, buf_size, "?%u?", lnd);
519 snprintf(buf, buf_size, "%s", nf->nf_name);
523 EXPORT_SYMBOL(libcfs_lnd2str_r);
526 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
528 __u32 nnum = LNET_NETNUM(net);
529 __u32 lnd = LNET_NETTYP(net);
530 struct netstrfns *nf;
532 nf = libcfs_lnd2netstrfns(lnd);
534 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
536 snprintf(buf, buf_size, "%s", nf->nf_name);
538 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
542 EXPORT_SYMBOL(libcfs_net2str_r);
545 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
547 __u32 addr = LNET_NIDADDR(nid);
548 __u32 net = LNET_NIDNET(nid);
549 __u32 nnum = LNET_NETNUM(net);
550 __u32 lnd = LNET_NETTYP(net);
551 struct netstrfns *nf;
553 if (nid == LNET_NID_ANY) {
554 strncpy(buf, "<?>", buf_size);
555 buf[buf_size - 1] = '\0';
559 nf = libcfs_lnd2netstrfns(lnd);
561 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
565 nf->nf_addr2str(addr, buf, buf_size);
566 addr_len = strlen(buf);
568 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
571 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
577 EXPORT_SYMBOL(libcfs_nid2str_r);
579 static struct netstrfns *
580 libcfs_str2net_internal(const char *str, __u32 *net)
582 struct netstrfns *nf = NULL;
587 for (i = 0; i < libcfs_nnetstrfns; i++) {
588 nf = &libcfs_netstrfns[i];
589 if (nf->nf_type >= 0 &&
590 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
594 if (i == libcfs_nnetstrfns)
597 nob = strlen(nf->nf_name);
599 if (strlen(str) == (unsigned int)nob) {
602 if (nf->nf_type == LOLND) /* net number not allowed */
607 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
608 i != (int)strlen(str))
612 *net = LNET_MKNET(nf->nf_type, netnum);
617 libcfs_str2net(const char *str)
621 if (libcfs_str2net_internal(str, &net) != NULL)
624 return LNET_NIDNET(LNET_NID_ANY);
626 EXPORT_SYMBOL(libcfs_str2net);
629 libcfs_str2nid(const char *str)
631 const char *sep = strchr(str, '@');
632 struct netstrfns *nf;
637 nf = libcfs_str2net_internal(sep + 1, &net);
641 sep = str + strlen(str);
642 net = LNET_MKNET(SOCKLND, 0);
643 nf = libcfs_lnd2netstrfns(SOCKLND);
647 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
650 return LNET_MKNID(net, addr);
652 EXPORT_SYMBOL(libcfs_str2nid);
655 libcfs_id2str(lnet_process_id_t id)
657 char *str = libcfs_next_nidstring();
659 if (id.pid == LNET_PID_ANY) {
660 snprintf(str, LNET_NIDSTR_SIZE,
661 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
665 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
666 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
667 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
670 EXPORT_SYMBOL(libcfs_id2str);
673 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
675 if (!strcmp(str, "*")) {
676 *nidp = LNET_NID_ANY;
680 *nidp = libcfs_str2nid(str);
681 return *nidp != LNET_NID_ANY;
683 EXPORT_SYMBOL(libcfs_str2anynid);
686 * Nid range list syntax.
689 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
690 * <nidrange> :== <addrrange> '@' <net>
691 * <addrrange> :== '*' |
694 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
696 * <cfs_expr_list> :== <number> |
698 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
699 * <range_expr> :== <number> |
700 * <number> '-' <number> |
701 * <number> '-' <number> '/' <number>
702 * <net> :== <netname> | <netname><number>
703 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
704 * "vib" | "ra" | "elan" | "mx" | "ptl"
709 * Structure to represent \<nidrange\> token of the syntax.
711 * One of this is created for each \<net\> parsed.
715 * Link to list of this structures which is built on nid range
718 struct list_head nr_link;
720 * List head for addrrange::ar_link.
722 struct list_head nr_addrranges;
724 * Flag indicating that *@<net> is found.
728 * Pointer to corresponding element of libcfs_netstrfns.
730 struct netstrfns *nr_netstrfns;
732 * Number of network. E.g. 5 if \<net\> is "elan5".
738 * Structure to represent \<addrrange\> token of the syntax.
742 * Link to nidrange::nr_addrranges.
744 struct list_head ar_link;
746 * List head for cfs_expr_list::el_list.
748 struct list_head ar_numaddr_ranges;
752 * Parses \<addrrange\> token on the syntax.
754 * Allocates struct addrrange and links to \a nidrange via
755 * (nidrange::nr_addrranges)
757 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
758 * \retval -errno otherwise
761 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
763 struct addrrange *addrrange;
765 if (src->ls_len == 1 && src->ls_str[0] == '*') {
766 nidrange->nr_all = 1;
770 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
771 if (addrrange == NULL)
773 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
774 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
776 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
778 &addrrange->ar_numaddr_ranges);
782 * Finds or creates struct nidrange.
784 * Checks if \a src is a valid network name, looks for corresponding
785 * nidrange on the ist of nidranges (\a nidlist), creates new struct
786 * nidrange if it is not found.
788 * \retval pointer to struct nidrange matching network specified via \a src
789 * \retval NULL if \a src does not match any network
791 static struct nidrange *
792 add_nidrange(const struct cfs_lstr *src,
793 struct list_head *nidlist)
795 struct netstrfns *nf;
800 if (src->ls_len >= LNET_NIDSTR_SIZE)
803 nf = libcfs_namenum2netstrfns(src->ls_str);
806 endlen = src->ls_len - strlen(nf->nf_name);
808 /* network name only, e.g. "elan" or "tcp" */
811 /* e.g. "elan25" or "tcp23", refuse to parse if
812 * network name is not appended with decimal or
813 * hexadecimal number */
814 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
815 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
819 list_for_each_entry(nr, nidlist, nr_link) {
820 if (nr->nr_netstrfns != nf)
822 if (nr->nr_netnum != netnum)
827 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
830 list_add_tail(&nr->nr_link, nidlist);
831 INIT_LIST_HEAD(&nr->nr_addrranges);
832 nr->nr_netstrfns = nf;
834 nr->nr_netnum = netnum;
840 * Parses \<nidrange\> token of the syntax.
842 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
843 * \retval 0 otherwise
846 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
848 struct cfs_lstr addrrange;
854 if (cfs_gettok(src, '@', &addrrange) == 0)
857 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
860 nr = add_nidrange(&net, nidlist);
864 if (parse_addrange(&addrrange, nr) != 0)
869 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
874 * Frees addrrange structures of \a list.
876 * For each struct addrrange structure found on \a list it frees
877 * cfs_expr_list list attached to it and frees the addrrange itself.
882 free_addrranges(struct list_head *list)
884 while (!list_empty(list)) {
885 struct addrrange *ar;
887 ar = list_entry(list->next, struct addrrange, ar_link);
889 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
890 list_del(&ar->ar_link);
891 LIBCFS_FREE(ar, sizeof(struct addrrange));
896 * Frees nidrange strutures of \a list.
898 * For each struct nidrange structure found on \a list it frees
899 * addrrange list attached to it and frees the nidrange itself.
904 cfs_free_nidlist(struct list_head *list)
906 struct list_head *pos, *next;
909 list_for_each_safe(pos, next, list) {
910 nr = list_entry(pos, struct nidrange, nr_link);
911 free_addrranges(&nr->nr_addrranges);
913 LIBCFS_FREE(nr, sizeof(struct nidrange));
916 EXPORT_SYMBOL(cfs_free_nidlist);
919 * Parses nid range list.
921 * Parses with rigorous syntax and overflow checking \a str into
922 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
923 * structures and links that structure to \a nidlist. The resulting
924 * list can be used to match a NID againts set of NIDS defined by \a
928 * \retval 1 on success
929 * \retval 0 otherwise
932 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
940 INIT_LIST_HEAD(nidlist);
942 rc = cfs_gettok(&src, ' ', &res);
944 cfs_free_nidlist(nidlist);
947 rc = parse_nidrange(&res, nidlist);
949 cfs_free_nidlist(nidlist);
955 EXPORT_SYMBOL(cfs_parse_nidlist);
958 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
960 * \see cfs_parse_nidlist()
963 * \retval 0 otherwises
965 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
968 struct addrrange *ar;
970 list_for_each_entry(nr, nidlist, nr_link) {
971 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
973 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
977 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
978 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
979 &ar->ar_numaddr_ranges))
984 EXPORT_SYMBOL(cfs_match_nid);
987 * Print the network part of the nidrange \a nr into the specified \a buffer.
989 * \retval number of characters written
992 cfs_print_network(char *buffer, int count, struct nidrange *nr)
994 struct netstrfns *nf = nr->nr_netstrfns;
996 if (nr->nr_netnum == 0)
997 return scnprintf(buffer, count, "@%s", nf->nf_name);
999 return scnprintf(buffer, count, "@%s%u",
1000 nf->nf_name, nr->nr_netnum);
1005 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1006 * At max \a count characters can be printed into \a buffer.
1008 * \retval number of characters written
1011 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1012 struct nidrange *nr)
1015 struct addrrange *ar;
1016 struct netstrfns *nf = nr->nr_netstrfns;
1018 list_for_each_entry(ar, addrranges, ar_link) {
1020 i += scnprintf(buffer + i, count - i, " ");
1021 i += nf->nf_print_addrlist(buffer + i, count - i,
1022 &ar->ar_numaddr_ranges);
1023 i += cfs_print_network(buffer + i, count - i, nr);
1029 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1030 * At max \a count characters can be printed into \a buffer.
1031 * Nidranges are separated by a space character.
1033 * \retval number of characters written
1035 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1038 struct nidrange *nr;
1043 list_for_each_entry(nr, nidlist, nr_link) {
1045 i += scnprintf(buffer + i, count - i, " ");
1047 if (nr->nr_all != 0) {
1048 LASSERT(list_empty(&nr->nr_addrranges));
1049 i += scnprintf(buffer + i, count - i, "*");
1050 i += cfs_print_network(buffer + i, count - i, nr);
1052 i += cfs_print_addrranges(buffer + i, count - i,
1053 &nr->nr_addrranges, nr);
1058 EXPORT_SYMBOL(cfs_print_nidlist);
1061 * Determines minimum and maximum addresses for a single
1062 * numeric address range
1068 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1071 struct cfs_expr_list *el;
1072 struct cfs_range_expr *re;
1073 __u32 tmp_ip_addr = 0;
1074 unsigned int min_ip[4] = {0};
1075 unsigned int max_ip[4] = {0};
1078 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1079 list_for_each_entry(re, &el->el_exprs, re_link) {
1080 min_ip[re_count] = re->re_lo;
1081 max_ip[re_count] = re->re_hi;
1086 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1087 (min_ip[2] << 8) | min_ip[3]);
1089 if (min_nid != NULL)
1090 *min_nid = tmp_ip_addr;
1092 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1093 (max_ip[2] << 8) | max_ip[3]);
1095 if (max_nid != NULL)
1096 *max_nid = tmp_ip_addr;
1100 * Determines minimum and maximum addresses for a single
1101 * numeric address range
1107 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1110 struct cfs_expr_list *el;
1111 struct cfs_range_expr *re;
1112 unsigned int min_addr = 0;
1113 unsigned int max_addr = 0;
1115 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1116 list_for_each_entry(re, &el->el_exprs, re_link) {
1117 if (re->re_lo < min_addr || min_addr == 0)
1118 min_addr = re->re_lo;
1119 if (re->re_hi > max_addr)
1120 max_addr = re->re_hi;
1124 if (min_nid != NULL)
1125 *min_nid = min_addr;
1126 if (max_nid != NULL)
1127 *max_nid = max_addr;
1131 * Determines whether an expression list in an nidrange contains exactly
1132 * one contiguous address range. Calls the correct netstrfns for the LND
1136 * \retval true if contiguous
1137 * \retval false if not contiguous
1139 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1141 struct nidrange *nr;
1142 struct netstrfns *nf = NULL;
1143 char *lndname = NULL;
1146 list_for_each_entry(nr, nidlist, nr_link) {
1147 nf = nr->nr_netstrfns;
1148 if (lndname == NULL)
1149 lndname = nf->nf_name;
1151 netnum = nr->nr_netnum;
1153 if (strcmp(lndname, nf->nf_name) != 0 ||
1154 netnum != nr->nr_netnum)
1161 if (!nf->nf_is_contiguous(nidlist))
1166 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
1169 * Determines whether an expression list in an num nidrange contains exactly
1170 * one contiguous address range.
1174 * \retval true if contiguous
1175 * \retval false if not contiguous
1177 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1179 struct nidrange *nr;
1180 struct addrrange *ar;
1181 struct cfs_expr_list *el;
1182 struct cfs_range_expr *re;
1184 __u32 last_end_nid = 0;
1185 __u32 current_start_nid = 0;
1186 __u32 current_end_nid = 0;
1188 list_for_each_entry(nr, nidlist, nr_link) {
1189 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1190 cfs_num_ar_min_max(ar, ¤t_start_nid,
1192 if (last_end_nid != 0 &&
1193 (current_start_nid - last_end_nid != 1))
1195 last_end_nid = current_end_nid;
1196 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1198 list_for_each_entry(re, &el->el_exprs,
1200 if (re->re_stride > 1)
1202 else if (last_hi != 0 &&
1203 re->re_hi - last_hi != 1)
1205 last_hi = re->re_hi;
1215 * Determines whether an expression list in an ip nidrange contains exactly
1216 * one contiguous address range.
1220 * \retval true if contiguous
1221 * \retval false if not contiguous
1223 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1225 struct nidrange *nr;
1226 struct addrrange *ar;
1227 struct cfs_expr_list *el;
1228 struct cfs_range_expr *re;
1232 __u32 last_end_nid = 0;
1233 __u32 current_start_nid = 0;
1234 __u32 current_end_nid = 0;
1236 list_for_each_entry(nr, nidlist, nr_link) {
1237 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1240 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1242 if (last_end_nid != 0 &&
1243 (current_start_nid - last_end_nid != 1))
1245 last_end_nid = current_end_nid;
1246 list_for_each_entry(el,
1247 &ar->ar_numaddr_ranges,
1250 list_for_each_entry(re, &el->el_exprs,
1253 if (re->re_stride > 1 ||
1254 (last_diff > 0 && last_hi != 255) ||
1255 (last_diff > 0 && last_hi == 255 &&
1258 last_hi = re->re_hi;
1259 last_diff = re->re_hi - re->re_lo;
1269 * Takes a linked list of nidrange expressions, determines the minimum
1270 * and maximum nid and creates appropriate nid structures
1276 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1277 char *max_nid, size_t nidstr_length)
1279 struct nidrange *nr;
1280 struct netstrfns *nf = NULL;
1284 char *lndname = NULL;
1285 char min_addr_str[IPSTRING_LENGTH];
1286 char max_addr_str[IPSTRING_LENGTH];
1288 list_for_each_entry(nr, nidlist, nr_link) {
1289 nf = nr->nr_netstrfns;
1290 lndname = nf->nf_name;
1292 netnum = nr->nr_netnum;
1294 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1296 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1297 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1299 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1301 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1304 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
1307 * Determines the min and max NID values for num LNDs
1313 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1316 struct nidrange *nr;
1317 struct addrrange *ar;
1318 unsigned int tmp_min_addr = 0;
1319 unsigned int tmp_max_addr = 0;
1320 unsigned int min_addr = 0;
1321 unsigned int max_addr = 0;
1323 list_for_each_entry(nr, nidlist, nr_link) {
1324 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1325 cfs_num_ar_min_max(ar, &tmp_min_addr,
1327 if (tmp_min_addr < min_addr || min_addr == 0)
1328 min_addr = tmp_min_addr;
1329 if (tmp_max_addr > max_addr)
1330 max_addr = tmp_min_addr;
1333 *max_nid = max_addr;
1334 *min_nid = min_addr;
1338 * Takes an nidlist and determines the minimum and maximum
1345 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1348 struct nidrange *nr;
1349 struct addrrange *ar;
1350 __u32 tmp_min_ip_addr = 0;
1351 __u32 tmp_max_ip_addr = 0;
1352 __u32 min_ip_addr = 0;
1353 __u32 max_ip_addr = 0;
1355 list_for_each_entry(nr, nidlist, nr_link) {
1356 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1357 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1359 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1360 min_ip_addr = tmp_min_ip_addr;
1361 if (tmp_max_ip_addr > max_ip_addr)
1362 max_ip_addr = tmp_max_ip_addr;
1366 if (min_nid != NULL)
1367 *min_nid = min_ip_addr;
1368 if (max_nid != NULL)
1369 *max_nid = max_ip_addr;