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
568 static const size_t libcfs_nnetstrfns =
569 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
571 static struct netstrfns *
572 libcfs_lnd2netstrfns(__u32 lnd)
576 for (i = 0; i < libcfs_nnetstrfns; i++)
577 if (lnd == libcfs_netstrfns[i].nf_type)
578 return &libcfs_netstrfns[i];
583 static struct netstrfns *
584 libcfs_namenum2netstrfns(const char *name)
586 struct netstrfns *nf;
589 for (i = 0; i < libcfs_nnetstrfns; i++) {
590 nf = &libcfs_netstrfns[i];
591 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
597 static struct netstrfns *
598 libcfs_name2netstrfns(const char *name)
602 for (i = 0; i < libcfs_nnetstrfns; i++)
603 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
604 return &libcfs_netstrfns[i];
610 libcfs_isknown_lnd(__u32 lnd)
612 return libcfs_lnd2netstrfns(lnd) != NULL;
616 libcfs_lnd2modname(__u32 lnd)
618 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
620 return (nf == NULL) ? NULL : nf->nf_modname;
624 libcfs_str2lnd(const char *str)
626 struct netstrfns *nf = libcfs_name2netstrfns(str);
635 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
637 struct netstrfns *nf;
639 nf = libcfs_lnd2netstrfns(lnd);
641 snprintf(buf, buf_size, "?%u?", lnd);
643 snprintf(buf, buf_size, "%s", nf->nf_name);
649 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
651 __u32 nnum = LNET_NETNUM(net);
652 __u32 lnd = LNET_NETTYP(net);
653 struct netstrfns *nf;
655 nf = libcfs_lnd2netstrfns(lnd);
657 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
659 snprintf(buf, buf_size, "%s", nf->nf_name);
661 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
667 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
669 __u32 addr = LNET_NIDADDR(nid);
670 __u32 net = LNET_NIDNET(nid);
671 __u32 nnum = LNET_NETNUM(net);
672 __u32 lnd = LNET_NETTYP(net);
673 struct netstrfns *nf;
675 if (nid == LNET_NID_ANY) {
676 strncpy(buf, "<?>", buf_size);
677 buf[buf_size - 1] = '\0';
681 nf = libcfs_lnd2netstrfns(lnd);
683 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
687 nf->nf_addr2str(addr, buf, buf_size);
688 addr_len = strlen(buf);
690 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
693 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
700 static struct netstrfns *
701 libcfs_str2net_internal(const char *str, __u32 *net)
703 struct netstrfns *nf = NULL;
708 for (i = 0; i < libcfs_nnetstrfns; i++) {
709 nf = &libcfs_netstrfns[i];
710 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
714 if (i == libcfs_nnetstrfns)
717 nob = strlen(nf->nf_name);
719 if (strlen(str) == (unsigned int)nob) {
722 if (nf->nf_type == LOLND) /* net number not allowed */
727 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
728 i != (int)strlen(str))
732 *net = LNET_MKNET(nf->nf_type, netnum);
737 libcfs_str2net(const char *str)
741 if (libcfs_str2net_internal(str, &net) != NULL)
748 libcfs_str2nid(const char *str)
750 const char *sep = strchr(str, '@');
751 struct netstrfns *nf;
756 nf = libcfs_str2net_internal(sep + 1, &net);
760 sep = str + strlen(str);
761 net = LNET_MKNET(SOCKLND, 0);
762 nf = libcfs_lnd2netstrfns(SOCKLND);
766 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
769 return LNET_MKNID(net, addr);
773 libcfs_id2str(struct lnet_process_id id)
775 char *str = libcfs_next_nidstring();
777 if (id.pid == LNET_PID_ANY) {
778 snprintf(str, LNET_NIDSTR_SIZE,
779 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
783 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
784 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
785 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
790 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
792 if (!strcmp(str, "*")) {
793 *nidp = LNET_NID_ANY;
797 *nidp = libcfs_str2nid(str);
798 return *nidp != LNET_NID_ANY;
802 * Nid range list syntax.
805 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
806 * <nidrange> :== <addrrange> '@' <net>
807 * <addrrange> :== '*' |
810 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
812 * <cfs_expr_list> :== <number> |
814 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
815 * <range_expr> :== <number> |
816 * <number> '-' <number> |
817 * <number> '-' <number> '/' <number>
818 * <net> :== <netname> | <netname><number>
819 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
820 * "vib" | "ra" | "elan" | "mx" | "ptl"
825 * Structure to represent \<nidrange\> token of the syntax.
827 * One of this is created for each \<net\> parsed.
831 * Link to list of this structures which is built on nid range
834 struct list_head nr_link;
836 * List head for addrrange::ar_link.
838 struct list_head nr_addrranges;
840 * Flag indicating that *@<net> is found.
844 * Pointer to corresponding element of libcfs_netstrfns.
846 struct netstrfns *nr_netstrfns;
848 * Number of network. E.g. 5 if \<net\> is "elan5".
854 * Structure to represent \<addrrange\> token of the syntax.
858 * Link to nidrange::nr_addrranges.
860 struct list_head ar_link;
862 * List head for cfs_expr_list::el_list.
864 struct list_head ar_numaddr_ranges;
868 * Parses \<addrrange\> token on the syntax.
870 * Allocates struct addrrange and links to \a nidrange via
871 * (nidrange::nr_addrranges)
873 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
874 * \retval -errno otherwise
877 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
879 struct addrrange *addrrange;
881 if (src->ls_len == 1 && src->ls_str[0] == '*') {
882 nidrange->nr_all = 1;
886 addrrange = calloc(1, sizeof(struct addrrange));
887 if (addrrange == NULL)
889 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
890 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
892 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
894 &addrrange->ar_numaddr_ranges);
898 * Finds or creates struct nidrange.
900 * Checks if \a src is a valid network name, looks for corresponding
901 * nidrange on the ist of nidranges (\a nidlist), creates new struct
902 * nidrange if it is not found.
904 * \retval pointer to struct nidrange matching network specified via \a src
905 * \retval NULL if \a src does not match any network
907 static struct nidrange *
908 add_nidrange(const struct cfs_lstr *src,
909 struct list_head *nidlist)
911 struct netstrfns *nf;
916 if (src->ls_len >= LNET_NIDSTR_SIZE)
919 nf = libcfs_namenum2netstrfns(src->ls_str);
922 endlen = src->ls_len - strlen(nf->nf_name);
924 /* network name only, e.g. "elan" or "tcp" */
927 /* e.g. "elan25" or "tcp23", refuse to parse if
928 * network name is not appended with decimal or
929 * hexadecimal number */
930 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
931 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
935 list_for_each_entry(nr, nidlist, nr_link) {
936 if (nr->nr_netstrfns != nf)
938 if (nr->nr_netnum != netnum)
943 nr = calloc(1, sizeof(struct nidrange));
946 list_add_tail(&nr->nr_link, nidlist);
947 INIT_LIST_HEAD(&nr->nr_addrranges);
948 nr->nr_netstrfns = nf;
950 nr->nr_netnum = netnum;
956 * Parses \<nidrange\> token of the syntax.
958 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
959 * \retval 0 otherwise
962 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
964 struct cfs_lstr addrrange;
970 if (cfs_gettok(src, '@', &addrrange) == 0)
973 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
976 nr = add_nidrange(&net, nidlist);
980 if (parse_addrange(&addrrange, nr) != 0)
985 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
986 tmp.ls_len, tmp.ls_str);
991 libcfs_net_str_len(const char *str)
994 struct netstrfns *nf = NULL;
996 for (i = 0; i < libcfs_nnetstrfns; i++) {
997 nf = &libcfs_netstrfns[i];
998 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
999 return strlen(nf->nf_name);
1006 parse_net_range(char *str, __u32 len, struct list_head *net_num,
1009 struct cfs_lstr next;
1021 net_type_len = libcfs_net_str_len(str);
1023 if (net_type_len < len) {
1024 char c = str[net_type_len];
1026 str[net_type_len] = '\0';
1027 net = libcfs_str2net(str);
1028 str[net_type_len] = c;
1030 net = libcfs_str2net(str);
1033 if (net == LNET_NIDNET(LNET_NID_ANY))
1036 *net_type = LNET_NETTYP(net);
1039 * the net is either followed with an absolute number, *, or an
1040 * expression enclosed in []
1042 bracket = strchr(next.ls_str, '[');
1043 star = strchr(next.ls_str, '*');
1045 /* "*[" pattern not allowed */
1046 if (bracket && star && star < bracket)
1050 next.ls_str = str + net_type_len;
1051 next.ls_len = strlen(next.ls_str);
1053 next.ls_str = bracket;
1054 next.ls_len = strlen(bracket);
1057 /* if there is no net number just return */
1058 if (next.ls_len == 0)
1061 return libcfs_num_parse(next.ls_str, next.ls_len,
1066 parse_address(struct cfs_lstr *src, const __u32 net_type,
1067 struct list_head *addr)
1070 struct netstrfns *nf = NULL;
1072 for (i = 0; i < libcfs_nnetstrfns; i++) {
1073 nf = &libcfs_netstrfns[i];
1074 if (net_type == nf->nf_type)
1075 return nf->nf_parse_addrlist(src->ls_str, src->ls_len,
1083 cfs_parse_nid_parts(char *str, struct list_head *addr,
1084 struct list_head *net_num, __u32 *net_type)
1086 struct cfs_lstr next;
1087 struct cfs_lstr addrrange;
1095 next.ls_len = strlen(str);
1097 rc = cfs_gettok(&next, '@', &addrrange);
1102 /* only net is present */
1104 next.ls_len = strlen(str);
1109 /* assume only net is present */
1110 rc = parse_net_range(next.ls_str, next.ls_len, net_num, net_type);
1113 * if we successfully parsed the net range and there is no
1114 * address, or if we fail to parse the net range then return
1116 if ((!rc && !found) || rc)
1119 return parse_address(&addrrange, *net_type, addr);
1123 * Frees addrrange structures of \a list.
1125 * For each struct addrrange structure found on \a list it frees
1126 * cfs_expr_list list attached to it and frees the addrrange itself.
1131 free_addrranges(struct list_head *list)
1133 while (!list_empty(list)) {
1134 struct addrrange *ar;
1136 ar = list_entry(list->next, struct addrrange, ar_link);
1138 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
1139 list_del(&ar->ar_link);
1145 * Frees nidrange strutures of \a list.
1147 * For each struct nidrange structure found on \a list it frees
1148 * addrrange list attached to it and frees the nidrange itself.
1153 cfs_free_nidlist(struct list_head *list)
1155 struct list_head *pos, *next;
1156 struct nidrange *nr;
1158 list_for_each_safe(pos, next, list) {
1159 nr = list_entry(pos, struct nidrange, nr_link);
1160 free_addrranges(&nr->nr_addrranges);
1167 * Parses nid range list.
1169 * Parses with rigorous syntax and overflow checking \a str into
1170 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
1171 * structures and links that structure to \a nidlist. The resulting
1172 * list can be used to match a NID againts set of NIDS defined by \a
1174 * \see cfs_match_nid
1176 * \retval 1 on success
1177 * \retval 0 otherwise
1180 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
1182 struct cfs_lstr src;
1183 struct cfs_lstr res;
1188 INIT_LIST_HEAD(nidlist);
1189 while (src.ls_str) {
1190 rc = cfs_gettok(&src, ' ', &res);
1192 cfs_free_nidlist(nidlist);
1195 rc = parse_nidrange(&res, nidlist);
1197 cfs_free_nidlist(nidlist);
1205 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1207 * \see cfs_parse_nidlist()
1209 * \retval 1 on match
1210 * \retval 0 otherwises
1212 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
1214 struct nidrange *nr;
1215 struct addrrange *ar;
1217 list_for_each_entry(nr, nidlist, nr_link) {
1218 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1220 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1224 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1225 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1226 &ar->ar_numaddr_ranges))
1233 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
1240 if (net_type != LNET_NETTYP(net_id))
1243 net_num = LNET_NETNUM(net_id);
1246 * if there is a net number but the list passed in is empty, then
1247 * there is no match.
1249 if (!net_num && list_empty(net_num_list))
1251 else if (list_empty(net_num_list))
1254 if (!libcfs_num_match(net_num, net_num_list))
1261 * Print the network part of the nidrange \a nr into the specified \a buffer.
1263 * \retval number of characters written
1266 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1268 struct netstrfns *nf = nr->nr_netstrfns;
1270 if (nr->nr_netnum == 0)
1271 return scnprintf(buffer, count, "@%s", nf->nf_name);
1273 return scnprintf(buffer, count, "@%s%u",
1274 nf->nf_name, nr->nr_netnum);
1279 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1280 * At max \a count characters can be printed into \a buffer.
1282 * \retval number of characters written
1285 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1286 struct nidrange *nr)
1289 struct addrrange *ar;
1290 struct netstrfns *nf = nr->nr_netstrfns;
1292 list_for_each_entry(ar, addrranges, ar_link) {
1294 i += scnprintf(buffer + i, count - i, " ");
1295 i += nf->nf_print_addrlist(buffer + i, count - i,
1296 &ar->ar_numaddr_ranges);
1297 i += cfs_print_network(buffer + i, count - i, nr);
1303 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1304 * At max \a count characters can be printed into \a buffer.
1305 * Nidranges are separated by a space character.
1307 * \retval number of characters written
1309 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1312 struct nidrange *nr;
1317 list_for_each_entry(nr, nidlist, nr_link) {
1319 i += scnprintf(buffer + i, count - i, " ");
1321 if (nr->nr_all != 0) {
1322 assert(list_empty(&nr->nr_addrranges));
1323 i += scnprintf(buffer + i, count - i, "*");
1324 i += cfs_print_network(buffer + i, count - i, nr);
1326 i += cfs_print_addrranges(buffer + i, count - i,
1327 &nr->nr_addrranges, nr);
1334 * Determines minimum and maximum addresses for a single
1335 * numeric address range
1338 * \param[out] *min_nid __u32 representation of min NID
1339 * \param[out] *max_nid __u32 representation of max NID
1340 * \retval -EINVAL unsupported LNET range
1341 * \retval -ERANGE non-contiguous LNET range
1343 static int cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1346 struct cfs_expr_list *expr_list;
1347 struct cfs_range_expr *range;
1348 unsigned int min_ip[4] = {0};
1349 unsigned int max_ip[4] = {0};
1351 bool expect_full_octet = false;
1353 list_for_each_entry(expr_list, &ar->ar_numaddr_ranges, el_link) {
1356 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
1357 /* XXX: add support for multiple & non-contig. re's */
1361 /* if a previous octet was ranged, then all remaining
1362 * octets must be full for contiguous range */
1363 if (expect_full_octet && (range->re_lo != 0 ||
1364 range->re_hi != 255))
1367 if (range->re_stride != 1)
1370 if (range->re_lo > range->re_hi)
1373 if (range->re_lo != range->re_hi)
1374 expect_full_octet = true;
1376 min_ip[cur_octet] = range->re_lo;
1377 max_ip[cur_octet] = range->re_hi;
1385 if (min_nid != NULL)
1386 *min_nid = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1387 (min_ip[2] << 8) | min_ip[3]);
1389 if (max_nid != NULL)
1390 *max_nid = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1391 (max_ip[2] << 8) | max_ip[3]);
1397 * Determines minimum and maximum addresses for a single
1398 * numeric address range
1401 * \param[out] *min_nid __u32 representation of min NID
1402 * \param[out] *max_nid __u32 representation of max NID
1403 * \retval -EINVAL unsupported LNET range
1405 static int cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1408 struct cfs_expr_list *el;
1409 struct cfs_range_expr *re;
1410 unsigned int min_addr = 0;
1411 unsigned int max_addr = 0;
1413 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1416 list_for_each_entry(re, &el->el_exprs, re_link) {
1419 if (re->re_lo > re->re_hi)
1422 if (re->re_lo < min_addr || min_addr == 0)
1423 min_addr = re->re_lo;
1424 if (re->re_hi > max_addr)
1425 max_addr = re->re_hi;
1431 if (min_nid != NULL)
1432 *min_nid = min_addr;
1433 if (max_nid != NULL)
1434 *max_nid = max_addr;
1440 * Takes a linked list of nidrange expressions, determines the minimum
1441 * and maximum nid and creates appropriate nid structures
1444 * \param[out] *min_nid string representation of min NID
1445 * \param[out] *max_nid string representation of max NID
1446 * \retval -EINVAL unsupported LNET range
1447 * \retval -ERANGE non-contiguous LNET range
1449 int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1450 char *max_nid, size_t nidstr_length)
1452 struct nidrange *first_nidrange;
1454 struct netstrfns *nf;
1458 char min_addr_str[IPSTRING_LENGTH];
1459 char max_addr_str[IPSTRING_LENGTH];
1462 first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link);
1464 netnum = first_nidrange->nr_netnum;
1465 nf = first_nidrange->nr_netstrfns;
1466 lndname = nf->nf_name;
1468 rc = nf->nf_min_max(nidlist, &min_addr, &max_addr);
1472 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1473 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1475 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1477 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1484 * Determines the min and max NID values for num LNDs
1487 * \param[out] *min_nid if provided, returns string representation of min NID
1488 * \param[out] *max_nid if provided, returns string representation of max NID
1489 * \retval -EINVAL unsupported LNET range
1490 * \retval -ERANGE non-contiguous LNET range
1492 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1495 struct nidrange *nr;
1496 struct addrrange *ar;
1497 unsigned int tmp_min_addr = 0;
1498 unsigned int tmp_max_addr = 0;
1499 unsigned int min_addr = 0;
1500 unsigned int max_addr = 0;
1501 int nidlist_count = 0;
1504 list_for_each_entry(nr, nidlist, nr_link) {
1505 if (nidlist_count > 0)
1508 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1509 rc = cfs_num_ar_min_max(ar, &tmp_min_addr,
1514 if (tmp_min_addr < min_addr || min_addr == 0)
1515 min_addr = tmp_min_addr;
1516 if (tmp_max_addr > max_addr)
1517 max_addr = tmp_min_addr;
1520 if (max_nid != NULL)
1521 *max_nid = max_addr;
1522 if (min_nid != NULL)
1523 *min_nid = min_addr;
1529 * Takes an nidlist and determines the minimum and maximum
1533 * \param[out] *min_nid if provided, returns string representation of min NID
1534 * \param[out] *max_nid if provided, returns string representation of max NID
1535 * \retval -EINVAL unsupported LNET range
1536 * \retval -ERANGE non-contiguous LNET range
1538 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1541 struct nidrange *nr;
1542 struct addrrange *ar;
1543 __u32 tmp_min_ip_addr = 0;
1544 __u32 tmp_max_ip_addr = 0;
1545 __u32 min_ip_addr = 0;
1546 __u32 max_ip_addr = 0;
1547 int nidlist_count = 0;
1550 list_for_each_entry(nr, nidlist, nr_link) {
1551 if (nidlist_count > 0)
1556 max_ip_addr = 0xffffffff;
1560 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1561 rc = cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1566 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1567 min_ip_addr = tmp_min_ip_addr;
1568 if (tmp_max_ip_addr > max_ip_addr)
1569 max_ip_addr = tmp_max_ip_addr;
1575 if (max_nid != NULL)
1576 *max_nid = max_ip_addr;
1577 if (min_nid != NULL)
1578 *min_nid = min_ip_addr;
1584 libcfs_expand_nidrange(struct nidrange *nr, __u32 *addrs, int max_nids)
1586 struct addrrange *ar;
1587 int rc = 0, count = max_nids;
1588 struct netstrfns *nf = nr->nr_netstrfns;
1590 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1591 rc = nf->nf_expand_addrrange(&ar->ar_numaddr_ranges, addrs,
1599 return max_nids - count;
1602 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
1605 struct nidrange *nr;
1606 int rc = 0, count = max_nids;
1609 struct netstrfns *nf;
1612 addrs = calloc(max_nids, sizeof(__u32));
1616 list_for_each_entry(nr, nidlist, nr_link) {
1617 rc = libcfs_expand_nidrange(nr, addrs, count);
1624 nf = nr->nr_netstrfns;
1625 net = LNET_MKNET(nf->nf_type, nr->nr_netnum);
1627 for (i = count - 1; i >= count - rc; i--)
1628 lnet_nidlist[j++] = LNET_MKNID(net, addrs[i]);
1634 return max_nids - count;