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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * libcfs/libcfs/util/nidstrings.c
33 * Author: Phil Schwan <phil@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_LNET
45 #include <libcfs/util/string.h>
46 #include <linux/lnet/lnet-types.h>
47 #include <linux/lnet/nidstr.h>
52 /* max value for numeric network address */
53 #define MAX_NUMERIC_VALUE 0xffffffff
55 #define IPSTRING_LENGTH 16
57 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
58 * consistent in all conversion functions. Some code fragments are copied
59 * around for the sake of clarity...
62 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
63 * Choose the number of nidstrings to support the MAXIMUM expected number of
64 * concurrent users. If there are more, the returned string will be volatile.
65 * NB this number must allow for a process to be descheduled for a timeslice
66 * between getting its string and using it.
69 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
70 static int libcfs_nidstring_idx;
73 libcfs_next_nidstring(void)
77 str = libcfs_nidstrings[libcfs_nidstring_idx++];
78 if (libcfs_nidstring_idx ==
79 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
80 libcfs_nidstring_idx = 0;
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);
124 #ifdef HAVE_GETHOSTBYNAME
125 /* known hostname? */
126 if (('a' <= str[0] && str[0] <= 'z') ||
127 ('A' <= str[0] && str[0] <= 'Z')) {
130 tmp = calloc(1, nob + 1);
134 memcpy(tmp, str, nob);
137 he = gethostbyname(tmp);
142 __u32 ip = *(__u32 *)he->h_addr;
154 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
156 struct cfs_expr_list *el;
165 while (src.ls_str != NULL) {
168 if (!cfs_gettok(&src, '.', &res)) {
173 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
177 list_add_tail(&el->el_link, list);
186 cfs_expr_list_free_list(list);
192 cfs_expr2str(struct list_head *list, char *str, size_t size)
194 struct cfs_expr_list *expr;
195 struct cfs_range_expr *range;
196 char tmp[LNET_NIDSTR_SIZE];
199 bool bracket = false;
203 list_for_each_entry(expr, list, el_link) {
205 list_for_each_entry(range, &expr->el_exprs, re_link) {
206 if (range->re_lo == range->re_hi) {
209 "%u.", range->re_lo);
210 } else if (range->re_lo < range->re_hi) {
211 if (range->re_stride > 1) {
213 format = "[%u-%u/%u,";
215 format = "%u-%u/%u,";
216 snprintf(tmp, LNET_NIDSTR_SIZE,
217 format, range->re_lo,
218 range->re_hi, range->re_stride);
225 snprintf(tmp, LNET_NIDSTR_SIZE,
226 format, range->re_lo,
238 strncat(str, tmp, size + len);
242 tmpc = str + (strlen(str) - 1);
253 * get rid of the trailing '.' at the end of the string
254 * only if we actually had something on the list passed in.
255 * otherwise we could write outside the array
257 if (!list_empty(list))
258 str[strlen(str)-1] = '\0';
263 libcfs_num_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
266 struct cfs_expr_list *expr_list;
267 struct cfs_range_expr *range;
269 int max_idx = max_addrs - 1;
270 int addrs_idx = max_idx;
272 list_for_each_entry(expr_list, addrranges, el_link) {
273 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
274 for (i = range->re_lo; i <= range->re_hi;
275 i += range->re_stride) {
279 addrs[addrs_idx] = i;
285 return max_idx - addrs_idx;
289 libcfs_ip_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
294 rc = cfs_ip_addr_range_gen(addrs, max_addrs, addrranges);
299 return max_addrs - rc - 1;
303 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
306 struct cfs_expr_list *el;
308 list_for_each_entry(el, list, el_link) {
311 i += scnprintf(buffer + i, count - i, ".");
312 i += cfs_expr_list_print(buffer + i, count - i, el);
318 cfs_ip_addr_range_gen_recurse(__u32 *ip_list, int *count, int shift,
319 __u32 result, struct list_head *head_el,
320 struct cfs_expr_list *octet_el)
324 struct cfs_expr_list *next_octet_el;
325 struct cfs_range_expr *octet_expr;
328 * each octet can have multiple expressions so we need to traverse
329 * all of the expressions
331 list_for_each_entry(octet_expr, &octet_el->el_exprs, re_link) {
332 for (i = octet_expr->re_lo; i <= octet_expr->re_hi; i++) {
333 if (((i - octet_expr->re_lo) % octet_expr->re_stride) == 0) {
335 * we have a hit calculate the result and
336 * pass it forward to the next iteration
340 list_entry(octet_el->el_link.next,
341 typeof(*next_octet_el),
343 value = result | (i << (shift * 8));
344 if (next_octet_el->el_link.next != head_el) {
346 * We still have more octets in
347 * the IP address so traverse
348 * that. We're doing a depth first
351 if (cfs_ip_addr_range_gen_recurse(ip_list, count,
354 next_octet_el) == -1)
358 * We have hit a leaf so store the
359 * calculated IP address in the
360 * list. If we have run out of
361 * space stop the recursion.
365 /* add ip to the list */
366 ip_list[*count] = value;
376 * only generate maximum of count ip addresses from the given expression
379 cfs_ip_addr_range_gen(__u32 *ip_list, int count, struct list_head *ip_addr_expr)
381 struct cfs_expr_list *octet_el;
384 octet_el = list_entry(ip_addr_expr->next, typeof(*octet_el), el_link);
386 (void) cfs_ip_addr_range_gen_recurse(ip_list, &idx, 3, 0, &octet_el->el_link, octet_el);
392 * Matches address (\a addr) against address set encoded in \a list.
394 * \retval 1 if \a addr matches
395 * \retval 0 otherwise
398 cfs_ip_addr_match(__u32 addr, struct list_head *list)
400 struct cfs_expr_list *el;
403 list_for_each_entry_reverse(el, list, el_link) {
404 if (!cfs_expr_list_match(addr & 0xff, el))
414 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
416 snprintf(str, size, "%u", addr);
420 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
425 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
429 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
433 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
440 * Nf_parse_addrlist method for networks using numeric addresses.
442 * Examples of such networks are gm and elan.
444 * \retval 0 if \a str parsed to numeric address
445 * \retval errno otherwise
448 libcfs_num_parse(char *str, int len, struct list_head *list)
450 struct cfs_expr_list *el;
453 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
455 list_add_tail(&el->el_link, list);
461 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
463 struct cfs_expr_list *el;
466 list_for_each_entry(el, list, el_link) {
468 i += cfs_expr_list_print(buffer + i, count - i, el);
474 * Nf_match_addr method for networks using numeric addresses
477 * \retval 0 otherwise
480 libcfs_num_match(__u32 addr, struct list_head *numaddr)
482 struct cfs_expr_list *el;
484 assert(!list_empty(numaddr));
485 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
487 return cfs_expr_list_match(addr, el);
490 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
491 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
493 static struct netstrfns libcfs_netstrfns[] = {
497 .nf_modname = "klolnd",
498 .nf_addr2str = libcfs_decnum_addr2str,
499 .nf_str2addr = libcfs_lo_str2addr,
500 .nf_parse_addrlist = libcfs_num_parse,
501 .nf_print_addrlist = libcfs_num_addr_range_print,
502 .nf_match_addr = libcfs_num_match,
503 .nf_min_max = cfs_num_min_max,
504 .nf_expand_addrrange = libcfs_num_addr_range_expand
509 .nf_modname = "ksocklnd",
510 .nf_addr2str = libcfs_ip_addr2str,
511 .nf_str2addr = libcfs_ip_str2addr,
512 .nf_parse_addrlist = cfs_ip_addr_parse,
513 .nf_print_addrlist = libcfs_ip_addr_range_print,
514 .nf_match_addr = cfs_ip_addr_match,
515 .nf_min_max = cfs_ip_min_max,
516 .nf_expand_addrrange = libcfs_ip_addr_range_expand
521 .nf_modname = "ko2iblnd",
522 .nf_addr2str = libcfs_ip_addr2str,
523 .nf_str2addr = libcfs_ip_str2addr,
524 .nf_parse_addrlist = cfs_ip_addr_parse,
525 .nf_print_addrlist = libcfs_ip_addr_range_print,
526 .nf_match_addr = cfs_ip_addr_match,
527 .nf_min_max = cfs_ip_min_max,
528 .nf_expand_addrrange = libcfs_ip_addr_range_expand
533 .nf_modname = "kgnilnd",
534 .nf_addr2str = libcfs_decnum_addr2str,
535 .nf_str2addr = libcfs_num_str2addr,
536 .nf_parse_addrlist = libcfs_num_parse,
537 .nf_print_addrlist = libcfs_num_addr_range_print,
538 .nf_match_addr = libcfs_num_match,
539 .nf_min_max = cfs_num_min_max,
540 .nf_expand_addrrange = libcfs_num_addr_range_expand
545 .nf_modname = "kgnilnd",
546 .nf_addr2str = libcfs_ip_addr2str,
547 .nf_str2addr = libcfs_ip_str2addr,
548 .nf_parse_addrlist = cfs_ip_addr_parse,
549 .nf_print_addrlist = libcfs_ip_addr_range_print,
550 .nf_match_addr = cfs_ip_addr_match,
551 .nf_min_max = cfs_ip_min_max,
552 .nf_expand_addrrange = libcfs_ip_addr_range_expand
557 .nf_modname = "kptl4lnd",
558 .nf_addr2str = libcfs_decnum_addr2str,
559 .nf_str2addr = libcfs_num_str2addr,
560 .nf_parse_addrlist = libcfs_num_parse,
561 .nf_print_addrlist = libcfs_num_addr_range_print,
562 .nf_match_addr = libcfs_num_match,
563 .nf_min_max = cfs_num_min_max,
564 .nf_expand_addrrange = libcfs_num_addr_range_expand
569 .nf_modname = "kkfilnd",
570 .nf_addr2str = libcfs_decnum_addr2str,
571 .nf_str2addr = libcfs_num_str2addr,
572 .nf_parse_addrlist = libcfs_num_parse,
573 .nf_print_addrlist = libcfs_num_addr_range_print,
574 .nf_match_addr = libcfs_num_match,
575 .nf_min_max = cfs_num_min_max,
576 .nf_expand_addrrange = libcfs_num_addr_range_expand
580 static const size_t libcfs_nnetstrfns =
581 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
583 static struct netstrfns *
584 libcfs_lnd2netstrfns(__u32 lnd)
588 for (i = 0; i < libcfs_nnetstrfns; i++)
589 if (lnd == libcfs_netstrfns[i].nf_type)
590 return &libcfs_netstrfns[i];
595 static struct netstrfns *
596 libcfs_namenum2netstrfns(const char *name)
598 struct netstrfns *nf;
601 for (i = 0; i < libcfs_nnetstrfns; i++) {
602 nf = &libcfs_netstrfns[i];
603 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
609 static struct netstrfns *
610 libcfs_name2netstrfns(const char *name)
614 for (i = 0; i < libcfs_nnetstrfns; i++)
615 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
616 return &libcfs_netstrfns[i];
622 libcfs_isknown_lnd(__u32 lnd)
624 return libcfs_lnd2netstrfns(lnd) != NULL;
628 libcfs_lnd2modname(__u32 lnd)
630 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
632 return (nf == NULL) ? NULL : nf->nf_modname;
636 libcfs_str2lnd(const char *str)
638 struct netstrfns *nf = libcfs_name2netstrfns(str);
647 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
649 struct netstrfns *nf;
651 nf = libcfs_lnd2netstrfns(lnd);
653 snprintf(buf, buf_size, "?%u?", lnd);
655 snprintf(buf, buf_size, "%s", nf->nf_name);
661 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
663 __u32 nnum = LNET_NETNUM(net);
664 __u32 lnd = LNET_NETTYP(net);
665 struct netstrfns *nf;
667 nf = libcfs_lnd2netstrfns(lnd);
669 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
671 snprintf(buf, buf_size, "%s", nf->nf_name);
673 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
679 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
681 __u32 addr = LNET_NIDADDR(nid);
682 __u32 net = LNET_NIDNET(nid);
683 __u32 nnum = LNET_NETNUM(net);
684 __u32 lnd = LNET_NETTYP(net);
685 struct netstrfns *nf;
687 if (nid == LNET_NID_ANY) {
688 strncpy(buf, "<?>", buf_size);
689 buf[buf_size - 1] = '\0';
693 nf = libcfs_lnd2netstrfns(lnd);
695 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
699 nf->nf_addr2str(addr, buf, buf_size);
700 addr_len = strlen(buf);
702 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
705 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
712 static struct netstrfns *
713 libcfs_str2net_internal(const char *str, __u32 *net)
715 struct netstrfns *nf = NULL;
720 for (i = 0; i < libcfs_nnetstrfns; i++) {
721 nf = &libcfs_netstrfns[i];
722 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
726 if (i == libcfs_nnetstrfns)
729 nob = strlen(nf->nf_name);
731 if (strlen(str) == (unsigned int)nob) {
734 if (nf->nf_type == LOLND) /* net number not allowed */
739 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
740 i != (int)strlen(str))
744 *net = LNET_MKNET(nf->nf_type, netnum);
749 libcfs_str2net(const char *str)
753 if (libcfs_str2net_internal(str, &net) != NULL)
760 libcfs_str2nid(const char *str)
762 const char *sep = strchr(str, '@');
763 struct netstrfns *nf;
768 nf = libcfs_str2net_internal(sep + 1, &net);
772 sep = str + strlen(str);
773 net = LNET_MKNET(SOCKLND, 0);
774 nf = libcfs_lnd2netstrfns(SOCKLND);
778 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
781 return LNET_MKNID(net, addr);
785 libcfs_id2str(struct lnet_process_id id)
787 char *str = libcfs_next_nidstring();
789 if (id.pid == LNET_PID_ANY) {
790 snprintf(str, LNET_NIDSTR_SIZE,
791 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
795 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
796 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
797 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
802 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
804 if (!strcmp(str, "*")) {
805 *nidp = LNET_NID_ANY;
809 *nidp = libcfs_str2nid(str);
810 return *nidp != LNET_NID_ANY;
814 * Nid range list syntax.
817 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
818 * <nidrange> :== <addrrange> '@' <net>
819 * <addrrange> :== '*' |
822 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
824 * <cfs_expr_list> :== <number> |
826 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
827 * <range_expr> :== <number> |
828 * <number> '-' <number> |
829 * <number> '-' <number> '/' <number>
830 * <net> :== <netname> | <netname><number>
831 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
832 * "vib" | "ra" | "elan" | "mx" | "ptl"
837 * Structure to represent \<nidrange\> token of the syntax.
839 * One of this is created for each \<net\> parsed.
843 * Link to list of this structures which is built on nid range
846 struct list_head nr_link;
848 * List head for addrrange::ar_link.
850 struct list_head nr_addrranges;
852 * Flag indicating that *@<net> is found.
856 * Pointer to corresponding element of libcfs_netstrfns.
858 struct netstrfns *nr_netstrfns;
860 * Number of network. E.g. 5 if \<net\> is "elan5".
866 * Structure to represent \<addrrange\> token of the syntax.
870 * Link to nidrange::nr_addrranges.
872 struct list_head ar_link;
874 * List head for cfs_expr_list::el_list.
876 struct list_head ar_numaddr_ranges;
880 * Parses \<addrrange\> token on the syntax.
882 * Allocates struct addrrange and links to \a nidrange via
883 * (nidrange::nr_addrranges)
885 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
886 * \retval -errno otherwise
889 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
891 struct addrrange *addrrange;
893 if (src->ls_len == 1 && src->ls_str[0] == '*') {
894 nidrange->nr_all = 1;
898 addrrange = calloc(1, sizeof(struct addrrange));
899 if (addrrange == NULL)
901 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
902 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
904 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
906 &addrrange->ar_numaddr_ranges);
910 * Finds or creates struct nidrange.
912 * Checks if \a src is a valid network name, looks for corresponding
913 * nidrange on the ist of nidranges (\a nidlist), creates new struct
914 * nidrange if it is not found.
916 * \retval pointer to struct nidrange matching network specified via \a src
917 * \retval NULL if \a src does not match any network
919 static struct nidrange *
920 add_nidrange(const struct cfs_lstr *src,
921 struct list_head *nidlist)
923 struct netstrfns *nf;
928 if (src->ls_len >= LNET_NIDSTR_SIZE)
931 nf = libcfs_namenum2netstrfns(src->ls_str);
934 endlen = src->ls_len - strlen(nf->nf_name);
936 /* network name only, e.g. "elan" or "tcp" */
939 /* e.g. "elan25" or "tcp23", refuse to parse if
940 * network name is not appended with decimal or
941 * hexadecimal number */
942 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
943 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
947 list_for_each_entry(nr, nidlist, nr_link) {
948 if (nr->nr_netstrfns != nf)
950 if (nr->nr_netnum != netnum)
955 nr = calloc(1, sizeof(struct nidrange));
958 list_add_tail(&nr->nr_link, nidlist);
959 INIT_LIST_HEAD(&nr->nr_addrranges);
960 nr->nr_netstrfns = nf;
962 nr->nr_netnum = netnum;
968 * Parses \<nidrange\> token of the syntax.
970 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
971 * \retval 0 otherwise
974 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
976 struct cfs_lstr addrrange;
982 if (cfs_gettok(src, '@', &addrrange) == 0)
985 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
988 nr = add_nidrange(&net, nidlist);
992 if (parse_addrange(&addrrange, nr) != 0)
997 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
998 tmp.ls_len, tmp.ls_str);
1003 libcfs_net_str_len(const char *str)
1006 struct netstrfns *nf = NULL;
1008 for (i = 0; i < libcfs_nnetstrfns; i++) {
1009 nf = &libcfs_netstrfns[i];
1010 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1011 return strlen(nf->nf_name);
1018 parse_net_range(char *str, __u32 len, struct list_head *net_num,
1021 struct cfs_lstr next;
1033 net_type_len = libcfs_net_str_len(str);
1035 if (net_type_len < len) {
1036 char c = str[net_type_len];
1038 str[net_type_len] = '\0';
1039 net = libcfs_str2net(str);
1040 str[net_type_len] = c;
1042 net = libcfs_str2net(str);
1045 if (net == LNET_NIDNET(LNET_NID_ANY))
1048 *net_type = LNET_NETTYP(net);
1051 * the net is either followed with an absolute number, *, or an
1052 * expression enclosed in []
1054 bracket = strchr(next.ls_str, '[');
1055 star = strchr(next.ls_str, '*');
1057 /* "*[" pattern not allowed */
1058 if (bracket && star && star < bracket)
1062 next.ls_str = str + net_type_len;
1063 next.ls_len = strlen(next.ls_str);
1065 next.ls_str = bracket;
1066 next.ls_len = strlen(bracket);
1069 /* if there is no net number just return */
1070 if (next.ls_len == 0)
1073 return libcfs_num_parse(next.ls_str, next.ls_len,
1078 parse_address(struct cfs_lstr *src, const __u32 net_type,
1079 struct list_head *addr)
1082 struct netstrfns *nf = NULL;
1084 for (i = 0; i < libcfs_nnetstrfns; i++) {
1085 nf = &libcfs_netstrfns[i];
1086 if (net_type == nf->nf_type)
1087 return nf->nf_parse_addrlist(src->ls_str, src->ls_len,
1095 cfs_parse_nid_parts(char *str, struct list_head *addr,
1096 struct list_head *net_num, __u32 *net_type)
1098 struct cfs_lstr next;
1099 struct cfs_lstr addrrange;
1107 next.ls_len = strlen(str);
1109 rc = cfs_gettok(&next, '@', &addrrange);
1114 /* only net is present */
1116 next.ls_len = strlen(str);
1121 /* assume only net is present */
1122 rc = parse_net_range(next.ls_str, next.ls_len, net_num, net_type);
1125 * if we successfully parsed the net range and there is no
1126 * address, or if we fail to parse the net range then return
1128 if ((!rc && !found) || rc)
1131 return parse_address(&addrrange, *net_type, addr);
1135 * Frees addrrange structures of \a list.
1137 * For each struct addrrange structure found on \a list it frees
1138 * cfs_expr_list list attached to it and frees the addrrange itself.
1143 free_addrranges(struct list_head *list)
1145 while (!list_empty(list)) {
1146 struct addrrange *ar;
1148 ar = list_entry(list->next, struct addrrange, ar_link);
1150 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
1151 list_del(&ar->ar_link);
1157 * Frees nidrange strutures of \a list.
1159 * For each struct nidrange structure found on \a list it frees
1160 * addrrange list attached to it and frees the nidrange itself.
1165 cfs_free_nidlist(struct list_head *list)
1167 struct list_head *pos, *next;
1168 struct nidrange *nr;
1170 list_for_each_safe(pos, next, list) {
1171 nr = list_entry(pos, struct nidrange, nr_link);
1172 free_addrranges(&nr->nr_addrranges);
1179 * Parses nid range list.
1181 * Parses with rigorous syntax and overflow checking \a str into
1182 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
1183 * structures and links that structure to \a nidlist. The resulting
1184 * list can be used to match a NID againts set of NIDS defined by \a
1186 * \see cfs_match_nid
1188 * \retval 1 on success
1189 * \retval 0 otherwise
1192 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
1194 struct cfs_lstr src;
1195 struct cfs_lstr res;
1200 INIT_LIST_HEAD(nidlist);
1201 while (src.ls_str) {
1202 rc = cfs_gettok(&src, ' ', &res);
1204 cfs_free_nidlist(nidlist);
1207 rc = parse_nidrange(&res, nidlist);
1209 cfs_free_nidlist(nidlist);
1217 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1219 * \see cfs_parse_nidlist()
1221 * \retval 1 on match
1222 * \retval 0 otherwises
1224 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
1226 struct nidrange *nr;
1227 struct addrrange *ar;
1229 list_for_each_entry(nr, nidlist, nr_link) {
1230 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1232 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1236 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1237 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1238 &ar->ar_numaddr_ranges))
1245 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
1252 if (net_type != LNET_NETTYP(net_id))
1255 net_num = LNET_NETNUM(net_id);
1258 * if there is a net number but the list passed in is empty, then
1259 * there is no match.
1261 if (!net_num && list_empty(net_num_list))
1263 else if (list_empty(net_num_list))
1266 if (!libcfs_num_match(net_num, net_num_list))
1273 * Print the network part of the nidrange \a nr into the specified \a buffer.
1275 * \retval number of characters written
1278 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1280 struct netstrfns *nf = nr->nr_netstrfns;
1282 if (nr->nr_netnum == 0)
1283 return scnprintf(buffer, count, "@%s", nf->nf_name);
1285 return scnprintf(buffer, count, "@%s%u",
1286 nf->nf_name, nr->nr_netnum);
1291 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1292 * At max \a count characters can be printed into \a buffer.
1294 * \retval number of characters written
1297 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1298 struct nidrange *nr)
1301 struct addrrange *ar;
1302 struct netstrfns *nf = nr->nr_netstrfns;
1304 list_for_each_entry(ar, addrranges, ar_link) {
1306 i += scnprintf(buffer + i, count - i, " ");
1307 i += nf->nf_print_addrlist(buffer + i, count - i,
1308 &ar->ar_numaddr_ranges);
1309 i += cfs_print_network(buffer + i, count - i, nr);
1315 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1316 * At max \a count characters can be printed into \a buffer.
1317 * Nidranges are separated by a space character.
1319 * \retval number of characters written
1321 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1324 struct nidrange *nr;
1329 list_for_each_entry(nr, nidlist, nr_link) {
1331 i += scnprintf(buffer + i, count - i, " ");
1333 if (nr->nr_all != 0) {
1334 assert(list_empty(&nr->nr_addrranges));
1335 i += scnprintf(buffer + i, count - i, "*");
1336 i += cfs_print_network(buffer + i, count - i, nr);
1338 i += cfs_print_addrranges(buffer + i, count - i,
1339 &nr->nr_addrranges, nr);
1346 * Determines minimum and maximum addresses for a single
1347 * numeric address range
1350 * \param[out] *min_nid __u32 representation of min NID
1351 * \param[out] *max_nid __u32 representation of max NID
1352 * \retval -EINVAL unsupported LNET range
1353 * \retval -ERANGE non-contiguous LNET range
1355 static int cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1358 struct cfs_expr_list *expr_list;
1359 struct cfs_range_expr *range;
1360 unsigned int min_ip[4] = {0};
1361 unsigned int max_ip[4] = {0};
1363 bool expect_full_octet = false;
1365 list_for_each_entry(expr_list, &ar->ar_numaddr_ranges, el_link) {
1368 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
1369 /* XXX: add support for multiple & non-contig. re's */
1373 /* if a previous octet was ranged, then all remaining
1374 * octets must be full for contiguous range */
1375 if (expect_full_octet && (range->re_lo != 0 ||
1376 range->re_hi != 255))
1379 if (range->re_stride != 1)
1382 if (range->re_lo > range->re_hi)
1385 if (range->re_lo != range->re_hi)
1386 expect_full_octet = true;
1388 min_ip[cur_octet] = range->re_lo;
1389 max_ip[cur_octet] = range->re_hi;
1397 if (min_nid != NULL)
1398 *min_nid = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1399 (min_ip[2] << 8) | min_ip[3]);
1401 if (max_nid != NULL)
1402 *max_nid = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1403 (max_ip[2] << 8) | max_ip[3]);
1409 * Determines minimum and maximum addresses for a single
1410 * numeric address range
1413 * \param[out] *min_nid __u32 representation of min NID
1414 * \param[out] *max_nid __u32 representation of max NID
1415 * \retval -EINVAL unsupported LNET range
1417 static int cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1420 struct cfs_expr_list *el;
1421 struct cfs_range_expr *re;
1422 unsigned int min_addr = 0;
1423 unsigned int max_addr = 0;
1425 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1428 list_for_each_entry(re, &el->el_exprs, re_link) {
1431 if (re->re_lo > re->re_hi)
1434 if (re->re_lo < min_addr || min_addr == 0)
1435 min_addr = re->re_lo;
1436 if (re->re_hi > max_addr)
1437 max_addr = re->re_hi;
1443 if (min_nid != NULL)
1444 *min_nid = min_addr;
1445 if (max_nid != NULL)
1446 *max_nid = max_addr;
1452 * Takes a linked list of nidrange expressions, determines the minimum
1453 * and maximum nid and creates appropriate nid structures
1456 * \param[out] *min_nid string representation of min NID
1457 * \param[out] *max_nid string representation of max NID
1458 * \retval -EINVAL unsupported LNET range
1459 * \retval -ERANGE non-contiguous LNET range
1461 int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1462 char *max_nid, size_t nidstr_length)
1464 struct nidrange *first_nidrange;
1466 struct netstrfns *nf;
1470 char min_addr_str[IPSTRING_LENGTH];
1471 char max_addr_str[IPSTRING_LENGTH];
1474 first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link);
1476 netnum = first_nidrange->nr_netnum;
1477 nf = first_nidrange->nr_netstrfns;
1478 lndname = nf->nf_name;
1480 rc = nf->nf_min_max(nidlist, &min_addr, &max_addr);
1484 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1485 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1487 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1489 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1496 * Determines the min and max NID values for num LNDs
1499 * \param[out] *min_nid if provided, returns string representation of min NID
1500 * \param[out] *max_nid if provided, returns string representation of max NID
1501 * \retval -EINVAL unsupported LNET range
1502 * \retval -ERANGE non-contiguous LNET range
1504 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1507 struct nidrange *nr;
1508 struct addrrange *ar;
1509 unsigned int tmp_min_addr = 0;
1510 unsigned int tmp_max_addr = 0;
1511 unsigned int min_addr = 0;
1512 unsigned int max_addr = 0;
1513 int nidlist_count = 0;
1516 list_for_each_entry(nr, nidlist, nr_link) {
1517 if (nidlist_count > 0)
1520 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1521 rc = cfs_num_ar_min_max(ar, &tmp_min_addr,
1526 if (tmp_min_addr < min_addr || min_addr == 0)
1527 min_addr = tmp_min_addr;
1528 if (tmp_max_addr > max_addr)
1529 max_addr = tmp_min_addr;
1532 if (max_nid != NULL)
1533 *max_nid = max_addr;
1534 if (min_nid != NULL)
1535 *min_nid = min_addr;
1541 * Takes an nidlist and determines the minimum and maximum
1545 * \param[out] *min_nid if provided, returns string representation of min NID
1546 * \param[out] *max_nid if provided, returns string representation of max NID
1547 * \retval -EINVAL unsupported LNET range
1548 * \retval -ERANGE non-contiguous LNET range
1550 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1553 struct nidrange *nr;
1554 struct addrrange *ar;
1555 __u32 tmp_min_ip_addr = 0;
1556 __u32 tmp_max_ip_addr = 0;
1557 __u32 min_ip_addr = 0;
1558 __u32 max_ip_addr = 0;
1559 int nidlist_count = 0;
1562 list_for_each_entry(nr, nidlist, nr_link) {
1563 if (nidlist_count > 0)
1568 max_ip_addr = 0xffffffff;
1572 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1573 rc = cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1578 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1579 min_ip_addr = tmp_min_ip_addr;
1580 if (tmp_max_ip_addr > max_ip_addr)
1581 max_ip_addr = tmp_max_ip_addr;
1587 if (max_nid != NULL)
1588 *max_nid = max_ip_addr;
1589 if (min_nid != NULL)
1590 *min_nid = min_ip_addr;
1596 libcfs_expand_nidrange(struct nidrange *nr, __u32 *addrs, int max_nids)
1598 struct addrrange *ar;
1599 int rc = 0, count = max_nids;
1600 struct netstrfns *nf = nr->nr_netstrfns;
1602 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1603 rc = nf->nf_expand_addrrange(&ar->ar_numaddr_ranges, addrs,
1611 return max_nids - count;
1614 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
1617 struct nidrange *nr;
1618 int rc = 0, count = max_nids;
1621 struct netstrfns *nf;
1624 addrs = calloc(max_nids, sizeof(__u32));
1628 list_for_each_entry(nr, nidlist, nr_link) {
1629 rc = libcfs_expand_nidrange(nr, addrs, count);
1636 nf = nr->nr_netstrfns;
1637 net = LNET_MKNET(nf->nf_type, nr->nr_netnum);
1639 for (i = count - 1; i >= count - rc; i--)
1640 lnet_nidlist[j++] = LNET_MKNID(net, addrs[i]);
1646 return max_nids - count;