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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/util/nidstrings.c
34 * Author: Phil Schwan <phil@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_LNET
46 #include <libcfs/util/string.h>
47 #include <linux/lnet/lnet-types.h>
48 #include <linux/lnet/nidstr.h>
53 /* max value for numeric network address */
54 #define MAX_NUMERIC_VALUE 0xffffffff
56 #define IPSTRING_LENGTH 16
58 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
59 * consistent in all conversion functions. Some code fragments are copied
60 * around for the sake of clarity...
63 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
64 * Choose the number of nidstrings to support the MAXIMUM expected number of
65 * concurrent users. If there are more, the returned string will be volatile.
66 * NB this number must allow for a process to be descheduled for a timeslice
67 * between getting its string and using it.
70 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
71 static int libcfs_nidstring_idx;
74 libcfs_next_nidstring(void)
78 str = libcfs_nidstrings[libcfs_nidstring_idx++];
79 if (libcfs_nidstring_idx ==
80 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
81 libcfs_nidstring_idx = 0;
87 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
94 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
96 snprintf(str, size, "%u.%u.%u.%u",
97 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
98 (addr >> 8) & 0xff, addr & 0xff);
101 /* CAVEAT EMPTOR XscanfX
102 * I use "%n" at the end of a sscanf format to detect trailing junk. However
103 * sscanf may return immediately if it sees the terminating '0' in a string, so
104 * I initialise the %n variable to the expected length. If sscanf sets it;
105 * fine, if it doesn't, then the scan ended at the end of the string, which is
108 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
114 int n = nob; /* XscanfX */
117 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
119 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
120 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
121 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
125 #ifdef HAVE_GETHOSTBYNAME
126 /* known hostname? */
127 if (('a' <= str[0] && str[0] <= 'z') ||
128 ('A' <= str[0] && str[0] <= 'Z')) {
131 tmp = calloc(1, nob + 1);
135 memcpy(tmp, str, nob);
138 he = gethostbyname(tmp);
143 __u32 ip = *(__u32 *)he->h_addr;
155 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
157 struct cfs_expr_list *el;
166 while (src.ls_str != NULL) {
169 if (!cfs_gettok(&src, '.', &res)) {
174 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
178 list_add_tail(&el->el_link, list);
187 cfs_expr_list_free_list(list);
193 cfs_expr2str(struct list_head *list, char *str, size_t size)
195 struct cfs_expr_list *expr;
196 struct cfs_range_expr *range;
197 char tmp[LNET_NIDSTR_SIZE];
200 bool bracket = false;
204 list_for_each_entry(expr, list, el_link) {
206 list_for_each_entry(range, &expr->el_exprs, re_link) {
207 if (range->re_lo == range->re_hi) {
210 "%u.", range->re_lo);
211 } else if (range->re_lo < range->re_hi) {
212 if (range->re_stride > 1) {
214 format = "[%u-%u/%u,";
216 format = "%u-%u/%u,";
217 snprintf(tmp, LNET_NIDSTR_SIZE,
218 format, range->re_lo,
219 range->re_hi, range->re_stride);
226 snprintf(tmp, LNET_NIDSTR_SIZE,
227 format, range->re_lo,
239 strncat(str, tmp, size + len);
243 tmpc = str + (strlen(str) - 1);
254 * get rid of the trailing '.' at the end of the string
255 * only if we actually had something on the list passed in.
256 * otherwise we could write outside the array
258 if (!list_empty(list))
259 str[strlen(str)-1] = '\0';
264 libcfs_num_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
267 struct cfs_expr_list *expr_list;
268 struct cfs_range_expr *range;
270 int max_idx = max_addrs - 1;
271 int addrs_idx = max_idx;
273 list_for_each_entry(expr_list, addrranges, el_link) {
274 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
275 for (i = range->re_lo; i <= range->re_hi;
276 i += range->re_stride) {
280 addrs[addrs_idx] = i;
286 return max_idx - addrs_idx;
290 libcfs_ip_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
295 rc = cfs_ip_addr_range_gen(addrs, max_addrs, addrranges);
300 return max_addrs - rc - 1;
304 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
307 struct cfs_expr_list *el;
309 list_for_each_entry(el, list, el_link) {
312 i += scnprintf(buffer + i, count - i, ".");
313 i += cfs_expr_list_print(buffer + i, count - i, el);
319 cfs_ip_addr_range_gen_recurse(__u32 *ip_list, int *count, int shift,
320 __u32 result, struct list_head *head_el,
321 struct cfs_expr_list *octet_el)
325 struct cfs_expr_list *next_octet_el;
326 struct cfs_range_expr *octet_expr;
329 * each octet can have multiple expressions so we need to traverse
330 * all of the expressions
332 list_for_each_entry(octet_expr, &octet_el->el_exprs, re_link) {
333 for (i = octet_expr->re_lo; i <= octet_expr->re_hi; i++) {
334 if (((i - octet_expr->re_lo) % octet_expr->re_stride) == 0) {
336 * we have a hit calculate the result and
337 * pass it forward to the next iteration
341 list_entry(octet_el->el_link.next,
342 typeof(*next_octet_el),
344 value = result | (i << (shift * 8));
345 if (next_octet_el->el_link.next != head_el) {
347 * We still have more octets in
348 * the IP address so traverse
349 * that. We're doing a depth first
352 if (cfs_ip_addr_range_gen_recurse(ip_list, count,
355 next_octet_el) == -1)
359 * We have hit a leaf so store the
360 * calculated IP address in the
361 * list. If we have run out of
362 * space stop the recursion.
366 /* add ip to the list */
367 ip_list[*count] = value;
377 * only generate maximum of count ip addresses from the given expression
380 cfs_ip_addr_range_gen(__u32 *ip_list, int count, struct list_head *ip_addr_expr)
382 struct cfs_expr_list *octet_el;
385 octet_el = list_entry(ip_addr_expr->next, typeof(*octet_el), el_link);
387 (void) cfs_ip_addr_range_gen_recurse(ip_list, &idx, 3, 0, &octet_el->el_link, octet_el);
393 * Matches address (\a addr) against address set encoded in \a list.
395 * \retval 1 if \a addr matches
396 * \retval 0 otherwise
399 cfs_ip_addr_match(__u32 addr, struct list_head *list)
401 struct cfs_expr_list *el;
404 list_for_each_entry_reverse(el, list, el_link) {
405 if (!cfs_expr_list_match(addr & 0xff, el))
415 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
417 snprintf(str, size, "%u", addr);
421 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
426 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
430 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
434 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
441 * Nf_parse_addrlist method for networks using numeric addresses.
443 * Examples of such networks are gm and elan.
445 * \retval 0 if \a str parsed to numeric address
446 * \retval errno otherwise
449 libcfs_num_parse(char *str, int len, struct list_head *list)
451 struct cfs_expr_list *el;
454 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
456 list_add_tail(&el->el_link, list);
462 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
464 struct cfs_expr_list *el;
467 list_for_each_entry(el, list, el_link) {
469 i += cfs_expr_list_print(buffer + i, count - i, el);
475 * Nf_match_addr method for networks using numeric addresses
478 * \retval 0 otherwise
481 libcfs_num_match(__u32 addr, struct list_head *numaddr)
483 struct cfs_expr_list *el;
485 assert(!list_empty(numaddr));
486 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
488 return cfs_expr_list_match(addr, el);
491 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
492 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
494 static struct netstrfns libcfs_netstrfns[] = {
498 .nf_modname = "klolnd",
499 .nf_addr2str = libcfs_decnum_addr2str,
500 .nf_str2addr = libcfs_lo_str2addr,
501 .nf_parse_addrlist = libcfs_num_parse,
502 .nf_print_addrlist = libcfs_num_addr_range_print,
503 .nf_match_addr = libcfs_num_match,
504 .nf_min_max = cfs_num_min_max,
505 .nf_expand_addrrange = libcfs_num_addr_range_expand
510 .nf_modname = "ksocklnd",
511 .nf_addr2str = libcfs_ip_addr2str,
512 .nf_str2addr = libcfs_ip_str2addr,
513 .nf_parse_addrlist = cfs_ip_addr_parse,
514 .nf_print_addrlist = libcfs_ip_addr_range_print,
515 .nf_match_addr = cfs_ip_addr_match,
516 .nf_min_max = cfs_ip_min_max,
517 .nf_expand_addrrange = libcfs_ip_addr_range_expand
522 .nf_modname = "ko2iblnd",
523 .nf_addr2str = libcfs_ip_addr2str,
524 .nf_str2addr = libcfs_ip_str2addr,
525 .nf_parse_addrlist = cfs_ip_addr_parse,
526 .nf_print_addrlist = libcfs_ip_addr_range_print,
527 .nf_match_addr = cfs_ip_addr_match,
528 .nf_min_max = cfs_ip_min_max,
529 .nf_expand_addrrange = libcfs_ip_addr_range_expand
534 .nf_modname = "kgnilnd",
535 .nf_addr2str = libcfs_decnum_addr2str,
536 .nf_str2addr = libcfs_num_str2addr,
537 .nf_parse_addrlist = libcfs_num_parse,
538 .nf_print_addrlist = libcfs_num_addr_range_print,
539 .nf_match_addr = libcfs_num_match,
540 .nf_min_max = cfs_num_min_max,
541 .nf_expand_addrrange = libcfs_num_addr_range_expand
546 .nf_modname = "kgnilnd",
547 .nf_addr2str = libcfs_ip_addr2str,
548 .nf_str2addr = libcfs_ip_str2addr,
549 .nf_parse_addrlist = cfs_ip_addr_parse,
550 .nf_print_addrlist = libcfs_ip_addr_range_print,
551 .nf_match_addr = cfs_ip_addr_match,
552 .nf_min_max = cfs_ip_min_max,
553 .nf_expand_addrrange = libcfs_ip_addr_range_expand
558 .nf_modname = "kptl4lnd",
559 .nf_addr2str = libcfs_decnum_addr2str,
560 .nf_str2addr = libcfs_num_str2addr,
561 .nf_parse_addrlist = libcfs_num_parse,
562 .nf_print_addrlist = libcfs_num_addr_range_print,
563 .nf_match_addr = libcfs_num_match,
564 .nf_min_max = cfs_num_min_max,
565 .nf_expand_addrrange = libcfs_num_addr_range_expand
569 static const size_t libcfs_nnetstrfns =
570 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
572 static struct netstrfns *
573 libcfs_lnd2netstrfns(__u32 lnd)
577 for (i = 0; i < libcfs_nnetstrfns; i++)
578 if (lnd == libcfs_netstrfns[i].nf_type)
579 return &libcfs_netstrfns[i];
584 static struct netstrfns *
585 libcfs_namenum2netstrfns(const char *name)
587 struct netstrfns *nf;
590 for (i = 0; i < libcfs_nnetstrfns; i++) {
591 nf = &libcfs_netstrfns[i];
592 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
598 static struct netstrfns *
599 libcfs_name2netstrfns(const char *name)
603 for (i = 0; i < libcfs_nnetstrfns; i++)
604 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
605 return &libcfs_netstrfns[i];
611 libcfs_isknown_lnd(__u32 lnd)
613 return libcfs_lnd2netstrfns(lnd) != NULL;
617 libcfs_lnd2modname(__u32 lnd)
619 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
621 return (nf == NULL) ? NULL : nf->nf_modname;
625 libcfs_str2lnd(const char *str)
627 struct netstrfns *nf = libcfs_name2netstrfns(str);
636 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
638 struct netstrfns *nf;
640 nf = libcfs_lnd2netstrfns(lnd);
642 snprintf(buf, buf_size, "?%u?", lnd);
644 snprintf(buf, buf_size, "%s", nf->nf_name);
650 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
652 __u32 nnum = LNET_NETNUM(net);
653 __u32 lnd = LNET_NETTYP(net);
654 struct netstrfns *nf;
656 nf = libcfs_lnd2netstrfns(lnd);
658 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
660 snprintf(buf, buf_size, "%s", nf->nf_name);
662 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
668 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
670 __u32 addr = LNET_NIDADDR(nid);
671 __u32 net = LNET_NIDNET(nid);
672 __u32 nnum = LNET_NETNUM(net);
673 __u32 lnd = LNET_NETTYP(net);
674 struct netstrfns *nf;
676 if (nid == LNET_NID_ANY) {
677 strncpy(buf, "<?>", buf_size);
678 buf[buf_size - 1] = '\0';
682 nf = libcfs_lnd2netstrfns(lnd);
684 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
688 nf->nf_addr2str(addr, buf, buf_size);
689 addr_len = strlen(buf);
691 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
694 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
701 static struct netstrfns *
702 libcfs_str2net_internal(const char *str, __u32 *net)
704 struct netstrfns *nf = NULL;
709 for (i = 0; i < libcfs_nnetstrfns; i++) {
710 nf = &libcfs_netstrfns[i];
711 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
715 if (i == libcfs_nnetstrfns)
718 nob = strlen(nf->nf_name);
720 if (strlen(str) == (unsigned int)nob) {
723 if (nf->nf_type == LOLND) /* net number not allowed */
728 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
729 i != (int)strlen(str))
733 *net = LNET_MKNET(nf->nf_type, netnum);
738 libcfs_str2net(const char *str)
742 if (libcfs_str2net_internal(str, &net) != NULL)
749 libcfs_str2nid(const char *str)
751 const char *sep = strchr(str, '@');
752 struct netstrfns *nf;
757 nf = libcfs_str2net_internal(sep + 1, &net);
761 sep = str + strlen(str);
762 net = LNET_MKNET(SOCKLND, 0);
763 nf = libcfs_lnd2netstrfns(SOCKLND);
767 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
770 return LNET_MKNID(net, addr);
774 libcfs_id2str(struct lnet_process_id id)
776 char *str = libcfs_next_nidstring();
778 if (id.pid == LNET_PID_ANY) {
779 snprintf(str, LNET_NIDSTR_SIZE,
780 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
784 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
785 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
786 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
791 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
793 if (!strcmp(str, "*")) {
794 *nidp = LNET_NID_ANY;
798 *nidp = libcfs_str2nid(str);
799 return *nidp != LNET_NID_ANY;
803 * Nid range list syntax.
806 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
807 * <nidrange> :== <addrrange> '@' <net>
808 * <addrrange> :== '*' |
811 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
813 * <cfs_expr_list> :== <number> |
815 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
816 * <range_expr> :== <number> |
817 * <number> '-' <number> |
818 * <number> '-' <number> '/' <number>
819 * <net> :== <netname> | <netname><number>
820 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
821 * "vib" | "ra" | "elan" | "mx" | "ptl"
826 * Structure to represent \<nidrange\> token of the syntax.
828 * One of this is created for each \<net\> parsed.
832 * Link to list of this structures which is built on nid range
835 struct list_head nr_link;
837 * List head for addrrange::ar_link.
839 struct list_head nr_addrranges;
841 * Flag indicating that *@<net> is found.
845 * Pointer to corresponding element of libcfs_netstrfns.
847 struct netstrfns *nr_netstrfns;
849 * Number of network. E.g. 5 if \<net\> is "elan5".
855 * Structure to represent \<addrrange\> token of the syntax.
859 * Link to nidrange::nr_addrranges.
861 struct list_head ar_link;
863 * List head for cfs_expr_list::el_list.
865 struct list_head ar_numaddr_ranges;
869 * Parses \<addrrange\> token on the syntax.
871 * Allocates struct addrrange and links to \a nidrange via
872 * (nidrange::nr_addrranges)
874 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
875 * \retval -errno otherwise
878 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
880 struct addrrange *addrrange;
882 if (src->ls_len == 1 && src->ls_str[0] == '*') {
883 nidrange->nr_all = 1;
887 addrrange = calloc(1, sizeof(struct addrrange));
888 if (addrrange == NULL)
890 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
891 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
893 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
895 &addrrange->ar_numaddr_ranges);
899 * Finds or creates struct nidrange.
901 * Checks if \a src is a valid network name, looks for corresponding
902 * nidrange on the ist of nidranges (\a nidlist), creates new struct
903 * nidrange if it is not found.
905 * \retval pointer to struct nidrange matching network specified via \a src
906 * \retval NULL if \a src does not match any network
908 static struct nidrange *
909 add_nidrange(const struct cfs_lstr *src,
910 struct list_head *nidlist)
912 struct netstrfns *nf;
917 if (src->ls_len >= LNET_NIDSTR_SIZE)
920 nf = libcfs_namenum2netstrfns(src->ls_str);
923 endlen = src->ls_len - strlen(nf->nf_name);
925 /* network name only, e.g. "elan" or "tcp" */
928 /* e.g. "elan25" or "tcp23", refuse to parse if
929 * network name is not appended with decimal or
930 * hexadecimal number */
931 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
932 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
936 list_for_each_entry(nr, nidlist, nr_link) {
937 if (nr->nr_netstrfns != nf)
939 if (nr->nr_netnum != netnum)
944 nr = calloc(1, sizeof(struct nidrange));
947 list_add_tail(&nr->nr_link, nidlist);
948 INIT_LIST_HEAD(&nr->nr_addrranges);
949 nr->nr_netstrfns = nf;
951 nr->nr_netnum = netnum;
957 * Parses \<nidrange\> token of the syntax.
959 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
960 * \retval 0 otherwise
963 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
965 struct cfs_lstr addrrange;
971 if (cfs_gettok(src, '@', &addrrange) == 0)
974 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
977 nr = add_nidrange(&net, nidlist);
981 if (parse_addrange(&addrrange, nr) != 0)
986 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
987 tmp.ls_len, tmp.ls_str);
992 libcfs_net_str_len(const char *str)
995 struct netstrfns *nf = NULL;
997 for (i = 0; i < libcfs_nnetstrfns; i++) {
998 nf = &libcfs_netstrfns[i];
999 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1000 return strlen(nf->nf_name);
1007 parse_net_range(char *str, __u32 len, struct list_head *net_num,
1010 struct cfs_lstr next;
1022 net_type_len = libcfs_net_str_len(str);
1024 if (net_type_len < len) {
1025 char c = str[net_type_len];
1027 str[net_type_len] = '\0';
1028 net = libcfs_str2net(str);
1029 str[net_type_len] = c;
1031 net = libcfs_str2net(str);
1034 if (net == LNET_NIDNET(LNET_NID_ANY))
1037 *net_type = LNET_NETTYP(net);
1040 * the net is either followed with an absolute number, *, or an
1041 * expression enclosed in []
1043 bracket = strchr(next.ls_str, '[');
1044 star = strchr(next.ls_str, '*');
1046 /* "*[" pattern not allowed */
1047 if (bracket && star && star < bracket)
1051 next.ls_str = str + net_type_len;
1052 next.ls_len = strlen(next.ls_str);
1054 next.ls_str = bracket;
1055 next.ls_len = strlen(bracket);
1058 /* if there is no net number just return */
1059 if (next.ls_len == 0)
1062 return libcfs_num_parse(next.ls_str, next.ls_len,
1067 parse_address(struct cfs_lstr *src, const __u32 net_type,
1068 struct list_head *addr)
1071 struct netstrfns *nf = NULL;
1073 for (i = 0; i < libcfs_nnetstrfns; i++) {
1074 nf = &libcfs_netstrfns[i];
1075 if (net_type == nf->nf_type)
1076 return nf->nf_parse_addrlist(src->ls_str, src->ls_len,
1084 cfs_parse_nid_parts(char *str, struct list_head *addr,
1085 struct list_head *net_num, __u32 *net_type)
1087 struct cfs_lstr next;
1088 struct cfs_lstr addrrange;
1096 next.ls_len = strlen(str);
1098 rc = cfs_gettok(&next, '@', &addrrange);
1103 /* only net is present */
1105 next.ls_len = strlen(str);
1110 /* assume only net is present */
1111 rc = parse_net_range(next.ls_str, next.ls_len, net_num, net_type);
1114 * if we successfully parsed the net range and there is no
1115 * address, or if we fail to parse the net range then return
1117 if ((!rc && !found) || rc)
1120 return parse_address(&addrrange, *net_type, addr);
1124 * Frees addrrange structures of \a list.
1126 * For each struct addrrange structure found on \a list it frees
1127 * cfs_expr_list list attached to it and frees the addrrange itself.
1132 free_addrranges(struct list_head *list)
1134 while (!list_empty(list)) {
1135 struct addrrange *ar;
1137 ar = list_entry(list->next, struct addrrange, ar_link);
1139 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
1140 list_del(&ar->ar_link);
1146 * Frees nidrange strutures of \a list.
1148 * For each struct nidrange structure found on \a list it frees
1149 * addrrange list attached to it and frees the nidrange itself.
1154 cfs_free_nidlist(struct list_head *list)
1156 struct list_head *pos, *next;
1157 struct nidrange *nr;
1159 list_for_each_safe(pos, next, list) {
1160 nr = list_entry(pos, struct nidrange, nr_link);
1161 free_addrranges(&nr->nr_addrranges);
1168 * Parses nid range list.
1170 * Parses with rigorous syntax and overflow checking \a str into
1171 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
1172 * structures and links that structure to \a nidlist. The resulting
1173 * list can be used to match a NID againts set of NIDS defined by \a
1175 * \see cfs_match_nid
1177 * \retval 1 on success
1178 * \retval 0 otherwise
1181 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
1183 struct cfs_lstr src;
1184 struct cfs_lstr res;
1189 INIT_LIST_HEAD(nidlist);
1190 while (src.ls_str) {
1191 rc = cfs_gettok(&src, ' ', &res);
1193 cfs_free_nidlist(nidlist);
1196 rc = parse_nidrange(&res, nidlist);
1198 cfs_free_nidlist(nidlist);
1206 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1208 * \see cfs_parse_nidlist()
1210 * \retval 1 on match
1211 * \retval 0 otherwises
1213 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
1215 struct nidrange *nr;
1216 struct addrrange *ar;
1218 list_for_each_entry(nr, nidlist, nr_link) {
1219 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1221 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1225 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1226 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1227 &ar->ar_numaddr_ranges))
1233 static struct netstrfns *
1234 type2net_info(__u32 net_type)
1238 for (i = 0; i < libcfs_nnetstrfns; i++) {
1239 if (libcfs_netstrfns[i].nf_type == net_type)
1240 return &libcfs_netstrfns[i];
1247 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
1254 if (net_type != LNET_NETTYP(net_id))
1257 net_num = LNET_NETNUM(net_id);
1260 * if there is a net number but the list passed in is empty, then
1261 * there is no match.
1263 if (!net_num && list_empty(net_num_list))
1265 else if (list_empty(net_num_list))
1268 if (!libcfs_num_match(net_num, net_num_list))
1275 cfs_match_nid_net(lnet_nid_t nid, __u32 net_type,
1276 struct list_head *net_num_list,
1277 struct list_head *addr)
1280 struct netstrfns *fns;
1282 if (!addr || !net_num_list)
1285 fns = type2net_info(LNET_NETTYP(LNET_NIDNET(nid)));
1286 if (!fns || !net_num_list || !addr)
1289 address = LNET_NIDADDR(nid);
1291 /* if either the address or net number don't match then no match */
1292 if (!fns->nf_match_addr(address, addr) ||
1293 !cfs_match_net(LNET_NIDNET(nid), net_type, net_num_list))
1299 * Print the network part of the nidrange \a nr into the specified \a buffer.
1301 * \retval number of characters written
1304 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1306 struct netstrfns *nf = nr->nr_netstrfns;
1308 if (nr->nr_netnum == 0)
1309 return snprintf(buffer, count, "@%s", nf->nf_name);
1311 return snprintf(buffer, count, "@%s%u",
1312 nf->nf_name, nr->nr_netnum);
1317 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1318 * At max \a count characters can be printed into \a buffer.
1320 * \retval number of characters written
1323 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1324 struct nidrange *nr)
1327 struct addrrange *ar;
1328 struct netstrfns *nf = nr->nr_netstrfns;
1330 list_for_each_entry(ar, addrranges, ar_link) {
1332 i += scnprintf(buffer + i, count - i, " ");
1333 i += nf->nf_print_addrlist(buffer + i, count - i,
1334 &ar->ar_numaddr_ranges);
1335 i += cfs_print_network(buffer + i, count - i, nr);
1341 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1342 * At max \a count characters can be printed into \a buffer.
1343 * Nidranges are separated by a space character.
1345 * \retval number of characters written
1347 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1350 struct nidrange *nr;
1355 list_for_each_entry(nr, nidlist, nr_link) {
1357 i += scnprintf(buffer + i, count - i, " ");
1359 if (nr->nr_all != 0) {
1360 assert(list_empty(&nr->nr_addrranges));
1361 i += scnprintf(buffer + i, count - i, "*");
1362 i += cfs_print_network(buffer + i, count - i, nr);
1364 i += cfs_print_addrranges(buffer + i, count - i,
1365 &nr->nr_addrranges, nr);
1372 * Determines minimum and maximum addresses for a single
1373 * numeric address range
1376 * \param[out] *min_nid __u32 representation of min NID
1377 * \param[out] *max_nid __u32 representation of max NID
1378 * \retval -EINVAL unsupported LNET range
1379 * \retval -ERANGE non-contiguous LNET range
1381 static int cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1384 struct cfs_expr_list *expr_list;
1385 struct cfs_range_expr *range;
1386 unsigned int min_ip[4] = {0};
1387 unsigned int max_ip[4] = {0};
1389 bool expect_full_octet = false;
1391 list_for_each_entry(expr_list, &ar->ar_numaddr_ranges, el_link) {
1394 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
1395 /* XXX: add support for multiple & non-contig. re's */
1399 /* if a previous octet was ranged, then all remaining
1400 * octets must be full for contiguous range */
1401 if (expect_full_octet && (range->re_lo != 0 ||
1402 range->re_hi != 255))
1405 if (range->re_stride != 1)
1408 if (range->re_lo > range->re_hi)
1411 if (range->re_lo != range->re_hi)
1412 expect_full_octet = true;
1414 min_ip[cur_octet] = range->re_lo;
1415 max_ip[cur_octet] = range->re_hi;
1423 if (min_nid != NULL)
1424 *min_nid = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1425 (min_ip[2] << 8) | min_ip[3]);
1427 if (max_nid != NULL)
1428 *max_nid = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1429 (max_ip[2] << 8) | max_ip[3]);
1435 * Determines minimum and maximum addresses for a single
1436 * numeric address range
1439 * \param[out] *min_nid __u32 representation of min NID
1440 * \param[out] *max_nid __u32 representation of max NID
1441 * \retval -EINVAL unsupported LNET range
1443 static int cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1446 struct cfs_expr_list *el;
1447 struct cfs_range_expr *re;
1448 unsigned int min_addr = 0;
1449 unsigned int max_addr = 0;
1451 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1454 list_for_each_entry(re, &el->el_exprs, re_link) {
1457 if (re->re_lo > re->re_hi)
1460 if (re->re_lo < min_addr || min_addr == 0)
1461 min_addr = re->re_lo;
1462 if (re->re_hi > max_addr)
1463 max_addr = re->re_hi;
1469 if (min_nid != NULL)
1470 *min_nid = min_addr;
1471 if (max_nid != NULL)
1472 *max_nid = max_addr;
1478 * Takes a linked list of nidrange expressions, determines the minimum
1479 * and maximum nid and creates appropriate nid structures
1482 * \param[out] *min_nid string representation of min NID
1483 * \param[out] *max_nid string representation of max NID
1484 * \retval -EINVAL unsupported LNET range
1485 * \retval -ERANGE non-contiguous LNET range
1487 int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1488 char *max_nid, size_t nidstr_length)
1490 struct nidrange *first_nidrange;
1492 struct netstrfns *nf;
1496 char min_addr_str[IPSTRING_LENGTH];
1497 char max_addr_str[IPSTRING_LENGTH];
1500 first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link);
1502 netnum = first_nidrange->nr_netnum;
1503 nf = first_nidrange->nr_netstrfns;
1504 lndname = nf->nf_name;
1506 rc = nf->nf_min_max(nidlist, &min_addr, &max_addr);
1510 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1511 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1513 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1515 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1522 * Determines the min and max NID values for num LNDs
1525 * \param[out] *min_nid if provided, returns string representation of min NID
1526 * \param[out] *max_nid if provided, returns string representation of max NID
1527 * \retval -EINVAL unsupported LNET range
1528 * \retval -ERANGE non-contiguous LNET range
1530 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1533 struct nidrange *nr;
1534 struct addrrange *ar;
1535 unsigned int tmp_min_addr = 0;
1536 unsigned int tmp_max_addr = 0;
1537 unsigned int min_addr = 0;
1538 unsigned int max_addr = 0;
1539 int nidlist_count = 0;
1542 list_for_each_entry(nr, nidlist, nr_link) {
1543 if (nidlist_count > 0)
1546 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1547 rc = cfs_num_ar_min_max(ar, &tmp_min_addr,
1552 if (tmp_min_addr < min_addr || min_addr == 0)
1553 min_addr = tmp_min_addr;
1554 if (tmp_max_addr > max_addr)
1555 max_addr = tmp_min_addr;
1558 if (max_nid != NULL)
1559 *max_nid = max_addr;
1560 if (min_nid != NULL)
1561 *min_nid = min_addr;
1567 * Takes an nidlist and determines the minimum and maximum
1571 * \param[out] *min_nid if provided, returns string representation of min NID
1572 * \param[out] *max_nid if provided, returns string representation of max NID
1573 * \retval -EINVAL unsupported LNET range
1574 * \retval -ERANGE non-contiguous LNET range
1576 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1579 struct nidrange *nr;
1580 struct addrrange *ar;
1581 __u32 tmp_min_ip_addr = 0;
1582 __u32 tmp_max_ip_addr = 0;
1583 __u32 min_ip_addr = 0;
1584 __u32 max_ip_addr = 0;
1585 int nidlist_count = 0;
1588 list_for_each_entry(nr, nidlist, nr_link) {
1589 if (nidlist_count > 0)
1594 max_ip_addr = 0xffffffff;
1598 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1599 rc = cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1604 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1605 min_ip_addr = tmp_min_ip_addr;
1606 if (tmp_max_ip_addr > max_ip_addr)
1607 max_ip_addr = tmp_max_ip_addr;
1613 if (max_nid != NULL)
1614 *max_nid = max_ip_addr;
1615 if (min_nid != NULL)
1616 *min_nid = min_ip_addr;
1622 libcfs_expand_nidrange(struct nidrange *nr, __u32 *addrs, int max_nids)
1624 struct addrrange *ar;
1625 int rc = 0, count = max_nids;
1626 struct netstrfns *nf = nr->nr_netstrfns;
1628 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1629 rc = nf->nf_expand_addrrange(&ar->ar_numaddr_ranges, addrs,
1637 return max_nids - count;
1640 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
1643 struct nidrange *nr;
1644 int rc = 0, count = max_nids;
1647 struct netstrfns *nf;
1650 addrs = calloc(max_nids, sizeof(__u32));
1654 list_for_each_entry(nr, nidlist, nr_link) {
1655 rc = libcfs_expand_nidrange(nr, addrs, count);
1662 nf = nr->nr_netstrfns;
1663 net = LNET_MKNET(nf->nf_type, nr->nr_netnum);
1665 for (i = count - 1; i >= count - rc; i--)
1666 lnet_nidlist[j++] = LNET_MKNID(net, addrs[i]);
1672 return max_nids - count;