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 libcfs_num_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
196 struct cfs_expr_list *expr_list;
197 struct cfs_range_expr *range;
199 int max_idx = max_addrs - 1;
200 int addrs_idx = max_idx;
202 list_for_each_entry(expr_list, addrranges, el_link) {
203 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
204 for (i = range->re_lo; i <= range->re_hi;
205 i += range->re_stride) {
209 addrs[addrs_idx] = i;
215 return max_idx - addrs_idx;
219 libcfs_ip_addr_range_expand(struct list_head *addrranges, __u32 *addrs,
224 rc = cfs_ip_addr_range_gen(addrs, max_addrs, addrranges);
229 return max_addrs - rc - 1;
233 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
236 struct cfs_expr_list *el;
238 list_for_each_entry(el, list, el_link) {
241 i += snprintf(buffer + i, count - i, ".");
242 i += cfs_expr_list_print(buffer + i, count - i, el);
248 cfs_ip_addr_range_gen_recurse(__u32 *ip_list, int *count, int shift,
249 __u32 result, struct list_head *head_el,
250 struct cfs_expr_list *octet_el)
254 struct cfs_expr_list *next_octet_el;
255 struct cfs_range_expr *octet_expr;
258 * each octet can have multiple expressions so we need to traverse
259 * all of the expressions
261 list_for_each_entry(octet_expr, &octet_el->el_exprs, re_link) {
262 for (i = octet_expr->re_lo; i <= octet_expr->re_hi; i++) {
263 if (((i - octet_expr->re_lo) % octet_expr->re_stride) == 0) {
265 * we have a hit calculate the result and
266 * pass it forward to the next iteration
270 list_entry(octet_el->el_link.next,
271 typeof(*next_octet_el),
273 value = result | (i << (shift * 8));
274 if (next_octet_el->el_link.next != head_el) {
276 * We still have more octets in
277 * the IP address so traverse
278 * that. We're doing a depth first
281 if (cfs_ip_addr_range_gen_recurse(ip_list, count,
284 next_octet_el) == -1)
288 * We have hit a leaf so store the
289 * calculated IP address in the
290 * list. If we have run out of
291 * space stop the recursion.
295 /* add ip to the list */
296 ip_list[*count] = value;
306 * only generate maximum of count ip addresses from the given expression
309 cfs_ip_addr_range_gen(__u32 *ip_list, int count, struct list_head *ip_addr_expr)
311 struct cfs_expr_list *octet_el;
314 octet_el = list_entry(ip_addr_expr->next, typeof(*octet_el), el_link);
316 (void) cfs_ip_addr_range_gen_recurse(ip_list, &idx, 3, 0, &octet_el->el_link, octet_el);
322 * Matches address (\a addr) against address set encoded in \a list.
324 * \retval 1 if \a addr matches
325 * \retval 0 otherwise
328 cfs_ip_addr_match(__u32 addr, struct list_head *list)
330 struct cfs_expr_list *el;
333 list_for_each_entry_reverse(el, list, el_link) {
334 if (!cfs_expr_list_match(addr & 0xff, el))
344 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
346 snprintf(str, size, "%u", addr);
350 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
355 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
359 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
363 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
370 * Nf_parse_addrlist method for networks using numeric addresses.
372 * Examples of such networks are gm and elan.
374 * \retval 0 if \a str parsed to numeric address
375 * \retval errno otherwise
378 libcfs_num_parse(char *str, int len, struct list_head *list)
380 struct cfs_expr_list *el;
383 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
385 list_add_tail(&el->el_link, list);
391 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
393 struct cfs_expr_list *el;
396 list_for_each_entry(el, list, el_link) {
398 i += cfs_expr_list_print(buffer + i, count - i, el);
404 * Nf_match_addr method for networks using numeric addresses
407 * \retval 0 otherwise
410 libcfs_num_match(__u32 addr, struct list_head *numaddr)
412 struct cfs_expr_list *el;
414 assert(!list_empty(numaddr));
415 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
417 return cfs_expr_list_match(addr, el);
420 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
421 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
423 static struct netstrfns libcfs_netstrfns[] = {
427 .nf_modname = "klolnd",
428 .nf_addr2str = libcfs_decnum_addr2str,
429 .nf_str2addr = libcfs_lo_str2addr,
430 .nf_parse_addrlist = libcfs_num_parse,
431 .nf_print_addrlist = libcfs_num_addr_range_print,
432 .nf_match_addr = libcfs_num_match,
433 .nf_min_max = cfs_num_min_max,
434 .nf_expand_addrrange = libcfs_num_addr_range_expand
439 .nf_modname = "ksocklnd",
440 .nf_addr2str = libcfs_ip_addr2str,
441 .nf_str2addr = libcfs_ip_str2addr,
442 .nf_parse_addrlist = cfs_ip_addr_parse,
443 .nf_print_addrlist = libcfs_ip_addr_range_print,
444 .nf_match_addr = cfs_ip_addr_match,
445 .nf_min_max = cfs_ip_min_max,
446 .nf_expand_addrrange = libcfs_ip_addr_range_expand
451 .nf_modname = "ko2iblnd",
452 .nf_addr2str = libcfs_ip_addr2str,
453 .nf_str2addr = libcfs_ip_str2addr,
454 .nf_parse_addrlist = cfs_ip_addr_parse,
455 .nf_print_addrlist = libcfs_ip_addr_range_print,
456 .nf_match_addr = cfs_ip_addr_match,
457 .nf_min_max = cfs_ip_min_max,
458 .nf_expand_addrrange = libcfs_ip_addr_range_expand
463 .nf_modname = "kgnilnd",
464 .nf_addr2str = libcfs_decnum_addr2str,
465 .nf_str2addr = libcfs_num_str2addr,
466 .nf_parse_addrlist = libcfs_num_parse,
467 .nf_print_addrlist = libcfs_num_addr_range_print,
468 .nf_match_addr = libcfs_num_match,
469 .nf_min_max = cfs_num_min_max,
470 .nf_expand_addrrange = libcfs_num_addr_range_expand
475 .nf_modname = "kgnilnd",
476 .nf_addr2str = libcfs_ip_addr2str,
477 .nf_str2addr = libcfs_ip_str2addr,
478 .nf_parse_addrlist = cfs_ip_addr_parse,
479 .nf_print_addrlist = libcfs_ip_addr_range_print,
480 .nf_match_addr = cfs_ip_addr_match,
481 .nf_min_max = cfs_ip_min_max,
482 .nf_expand_addrrange = libcfs_ip_addr_range_expand
487 .nf_modname = "kptl4lnd",
488 .nf_addr2str = libcfs_decnum_addr2str,
489 .nf_str2addr = libcfs_num_str2addr,
490 .nf_parse_addrlist = libcfs_num_parse,
491 .nf_print_addrlist = libcfs_num_addr_range_print,
492 .nf_match_addr = libcfs_num_match,
493 .nf_min_max = cfs_num_min_max,
494 .nf_expand_addrrange = libcfs_num_addr_range_expand
498 static const size_t libcfs_nnetstrfns =
499 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
501 static struct netstrfns *
502 libcfs_lnd2netstrfns(__u32 lnd)
506 for (i = 0; i < libcfs_nnetstrfns; i++)
507 if (lnd == libcfs_netstrfns[i].nf_type)
508 return &libcfs_netstrfns[i];
513 static struct netstrfns *
514 libcfs_namenum2netstrfns(const char *name)
516 struct netstrfns *nf;
519 for (i = 0; i < libcfs_nnetstrfns; i++) {
520 nf = &libcfs_netstrfns[i];
521 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
527 static struct netstrfns *
528 libcfs_name2netstrfns(const char *name)
532 for (i = 0; i < libcfs_nnetstrfns; i++)
533 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
534 return &libcfs_netstrfns[i];
540 libcfs_isknown_lnd(__u32 lnd)
542 return libcfs_lnd2netstrfns(lnd) != NULL;
546 libcfs_lnd2modname(__u32 lnd)
548 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
550 return (nf == NULL) ? NULL : nf->nf_modname;
554 libcfs_str2lnd(const char *str)
556 struct netstrfns *nf = libcfs_name2netstrfns(str);
565 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
567 struct netstrfns *nf;
569 nf = libcfs_lnd2netstrfns(lnd);
571 snprintf(buf, buf_size, "?%u?", lnd);
573 snprintf(buf, buf_size, "%s", nf->nf_name);
579 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
581 __u32 nnum = LNET_NETNUM(net);
582 __u32 lnd = LNET_NETTYP(net);
583 struct netstrfns *nf;
585 nf = libcfs_lnd2netstrfns(lnd);
587 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
589 snprintf(buf, buf_size, "%s", nf->nf_name);
591 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
597 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
599 __u32 addr = LNET_NIDADDR(nid);
600 __u32 net = LNET_NIDNET(nid);
601 __u32 nnum = LNET_NETNUM(net);
602 __u32 lnd = LNET_NETTYP(net);
603 struct netstrfns *nf;
605 if (nid == LNET_NID_ANY) {
606 strncpy(buf, "<?>", buf_size);
607 buf[buf_size - 1] = '\0';
611 nf = libcfs_lnd2netstrfns(lnd);
613 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
617 nf->nf_addr2str(addr, buf, buf_size);
618 addr_len = strlen(buf);
620 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
623 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
630 static struct netstrfns *
631 libcfs_str2net_internal(const char *str, __u32 *net)
633 struct netstrfns *nf = NULL;
638 for (i = 0; i < libcfs_nnetstrfns; i++) {
639 nf = &libcfs_netstrfns[i];
640 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
644 if (i == libcfs_nnetstrfns)
647 nob = strlen(nf->nf_name);
649 if (strlen(str) == (unsigned int)nob) {
652 if (nf->nf_type == LOLND) /* net number not allowed */
657 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
658 i != (int)strlen(str))
662 *net = LNET_MKNET(nf->nf_type, netnum);
667 libcfs_str2net(const char *str)
671 if (libcfs_str2net_internal(str, &net) != NULL)
674 return LNET_NIDNET(LNET_NID_ANY);
678 libcfs_str2nid(const char *str)
680 const char *sep = strchr(str, '@');
681 struct netstrfns *nf;
686 nf = libcfs_str2net_internal(sep + 1, &net);
690 sep = str + strlen(str);
691 net = LNET_MKNET(SOCKLND, 0);
692 nf = libcfs_lnd2netstrfns(SOCKLND);
696 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
699 return LNET_MKNID(net, addr);
703 libcfs_id2str(struct lnet_process_id id)
705 char *str = libcfs_next_nidstring();
707 if (id.pid == LNET_PID_ANY) {
708 snprintf(str, LNET_NIDSTR_SIZE,
709 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
713 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
714 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
715 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
720 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
722 if (!strcmp(str, "*")) {
723 *nidp = LNET_NID_ANY;
727 *nidp = libcfs_str2nid(str);
728 return *nidp != LNET_NID_ANY;
732 * Nid range list syntax.
735 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
736 * <nidrange> :== <addrrange> '@' <net>
737 * <addrrange> :== '*' |
740 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
742 * <cfs_expr_list> :== <number> |
744 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
745 * <range_expr> :== <number> |
746 * <number> '-' <number> |
747 * <number> '-' <number> '/' <number>
748 * <net> :== <netname> | <netname><number>
749 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
750 * "vib" | "ra" | "elan" | "mx" | "ptl"
755 * Structure to represent \<nidrange\> token of the syntax.
757 * One of this is created for each \<net\> parsed.
761 * Link to list of this structures which is built on nid range
764 struct list_head nr_link;
766 * List head for addrrange::ar_link.
768 struct list_head nr_addrranges;
770 * Flag indicating that *@<net> is found.
774 * Pointer to corresponding element of libcfs_netstrfns.
776 struct netstrfns *nr_netstrfns;
778 * Number of network. E.g. 5 if \<net\> is "elan5".
784 * Structure to represent \<addrrange\> token of the syntax.
788 * Link to nidrange::nr_addrranges.
790 struct list_head ar_link;
792 * List head for cfs_expr_list::el_list.
794 struct list_head ar_numaddr_ranges;
798 * Parses \<addrrange\> token on the syntax.
800 * Allocates struct addrrange and links to \a nidrange via
801 * (nidrange::nr_addrranges)
803 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
804 * \retval -errno otherwise
807 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
809 struct addrrange *addrrange;
811 if (src->ls_len == 1 && src->ls_str[0] == '*') {
812 nidrange->nr_all = 1;
816 addrrange = calloc(1, sizeof(struct addrrange));
817 if (addrrange == NULL)
819 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
820 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
822 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
824 &addrrange->ar_numaddr_ranges);
828 * Finds or creates struct nidrange.
830 * Checks if \a src is a valid network name, looks for corresponding
831 * nidrange on the ist of nidranges (\a nidlist), creates new struct
832 * nidrange if it is not found.
834 * \retval pointer to struct nidrange matching network specified via \a src
835 * \retval NULL if \a src does not match any network
837 static struct nidrange *
838 add_nidrange(const struct cfs_lstr *src,
839 struct list_head *nidlist)
841 struct netstrfns *nf;
846 if (src->ls_len >= LNET_NIDSTR_SIZE)
849 nf = libcfs_namenum2netstrfns(src->ls_str);
852 endlen = src->ls_len - strlen(nf->nf_name);
854 /* network name only, e.g. "elan" or "tcp" */
857 /* e.g. "elan25" or "tcp23", refuse to parse if
858 * network name is not appended with decimal or
859 * hexadecimal number */
860 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
861 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
865 list_for_each_entry(nr, nidlist, nr_link) {
866 if (nr->nr_netstrfns != nf)
868 if (nr->nr_netnum != netnum)
873 nr = calloc(1, sizeof(struct nidrange));
876 list_add_tail(&nr->nr_link, nidlist);
877 INIT_LIST_HEAD(&nr->nr_addrranges);
878 nr->nr_netstrfns = nf;
880 nr->nr_netnum = netnum;
886 * Parses \<nidrange\> token of the syntax.
888 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
889 * \retval 0 otherwise
892 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
894 struct cfs_lstr addrrange;
900 if (cfs_gettok(src, '@', &addrrange) == 0)
903 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
906 nr = add_nidrange(&net, nidlist);
910 if (parse_addrange(&addrrange, nr) != 0)
915 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
916 tmp.ls_len, tmp.ls_str);
921 * Frees addrrange structures of \a list.
923 * For each struct addrrange structure found on \a list it frees
924 * cfs_expr_list list attached to it and frees the addrrange itself.
929 free_addrranges(struct list_head *list)
931 while (!list_empty(list)) {
932 struct addrrange *ar;
934 ar = list_entry(list->next, struct addrrange, ar_link);
936 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
937 list_del(&ar->ar_link);
943 * Frees nidrange strutures of \a list.
945 * For each struct nidrange structure found on \a list it frees
946 * addrrange list attached to it and frees the nidrange itself.
951 cfs_free_nidlist(struct list_head *list)
953 struct list_head *pos, *next;
956 list_for_each_safe(pos, next, list) {
957 nr = list_entry(pos, struct nidrange, nr_link);
958 free_addrranges(&nr->nr_addrranges);
965 * Parses nid range list.
967 * Parses with rigorous syntax and overflow checking \a str into
968 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
969 * structures and links that structure to \a nidlist. The resulting
970 * list can be used to match a NID againts set of NIDS defined by \a
974 * \retval 1 on success
975 * \retval 0 otherwise
978 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
986 INIT_LIST_HEAD(nidlist);
988 rc = cfs_gettok(&src, ' ', &res);
990 cfs_free_nidlist(nidlist);
993 rc = parse_nidrange(&res, nidlist);
995 cfs_free_nidlist(nidlist);
1003 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1005 * \see cfs_parse_nidlist()
1007 * \retval 1 on match
1008 * \retval 0 otherwises
1010 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
1012 struct nidrange *nr;
1013 struct addrrange *ar;
1015 list_for_each_entry(nr, nidlist, nr_link) {
1016 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1018 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1022 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1023 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1024 &ar->ar_numaddr_ranges))
1031 * Print the network part of the nidrange \a nr into the specified \a buffer.
1033 * \retval number of characters written
1036 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1038 struct netstrfns *nf = nr->nr_netstrfns;
1040 if (nr->nr_netnum == 0)
1041 return snprintf(buffer, count, "@%s", nf->nf_name);
1043 return snprintf(buffer, count, "@%s%u",
1044 nf->nf_name, nr->nr_netnum);
1049 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1050 * At max \a count characters can be printed into \a buffer.
1052 * \retval number of characters written
1055 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1056 struct nidrange *nr)
1059 struct addrrange *ar;
1060 struct netstrfns *nf = nr->nr_netstrfns;
1062 list_for_each_entry(ar, addrranges, ar_link) {
1064 i += snprintf(buffer + i, count - i, " ");
1065 i += nf->nf_print_addrlist(buffer + i, count - i,
1066 &ar->ar_numaddr_ranges);
1067 i += cfs_print_network(buffer + i, count - i, nr);
1073 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1074 * At max \a count characters can be printed into \a buffer.
1075 * Nidranges are separated by a space character.
1077 * \retval number of characters written
1079 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1082 struct nidrange *nr;
1087 list_for_each_entry(nr, nidlist, nr_link) {
1089 i += snprintf(buffer + i, count - i, " ");
1091 if (nr->nr_all != 0) {
1092 assert(list_empty(&nr->nr_addrranges));
1093 i += snprintf(buffer + i, count - i, "*");
1094 i += cfs_print_network(buffer + i, count - i, nr);
1096 i += cfs_print_addrranges(buffer + i, count - i,
1097 &nr->nr_addrranges, nr);
1104 * Determines minimum and maximum addresses for a single
1105 * numeric address range
1108 * \param[out] *min_nid __u32 representation of min NID
1109 * \param[out] *max_nid __u32 representation of max NID
1110 * \retval -EINVAL unsupported LNET range
1111 * \retval -ERANGE non-contiguous LNET range
1113 static int cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1116 struct cfs_expr_list *expr_list;
1117 struct cfs_range_expr *range;
1118 unsigned int min_ip[4] = {0};
1119 unsigned int max_ip[4] = {0};
1121 bool expect_full_octet = false;
1123 list_for_each_entry(expr_list, &ar->ar_numaddr_ranges, el_link) {
1126 list_for_each_entry(range, &expr_list->el_exprs, re_link) {
1127 /* XXX: add support for multiple & non-contig. re's */
1131 /* if a previous octet was ranged, then all remaining
1132 * octets must be full for contiguous range */
1133 if (expect_full_octet && (range->re_lo != 0 ||
1134 range->re_hi != 255))
1137 if (range->re_stride != 1)
1140 if (range->re_lo > range->re_hi)
1143 if (range->re_lo != range->re_hi)
1144 expect_full_octet = true;
1146 min_ip[cur_octet] = range->re_lo;
1147 max_ip[cur_octet] = range->re_hi;
1155 if (min_nid != NULL)
1156 *min_nid = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1157 (min_ip[2] << 8) | min_ip[3]);
1159 if (max_nid != NULL)
1160 *max_nid = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1161 (max_ip[2] << 8) | max_ip[3]);
1167 * Determines minimum and maximum addresses for a single
1168 * numeric address range
1171 * \param[out] *min_nid __u32 representation of min NID
1172 * \param[out] *max_nid __u32 representation of max NID
1173 * \retval -EINVAL unsupported LNET range
1175 static int cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1178 struct cfs_expr_list *el;
1179 struct cfs_range_expr *re;
1180 unsigned int min_addr = 0;
1181 unsigned int max_addr = 0;
1183 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1186 list_for_each_entry(re, &el->el_exprs, re_link) {
1189 if (re->re_lo > re->re_hi)
1192 if (re->re_lo < min_addr || min_addr == 0)
1193 min_addr = re->re_lo;
1194 if (re->re_hi > max_addr)
1195 max_addr = re->re_hi;
1201 if (min_nid != NULL)
1202 *min_nid = min_addr;
1203 if (max_nid != NULL)
1204 *max_nid = max_addr;
1210 * Takes a linked list of nidrange expressions, determines the minimum
1211 * and maximum nid and creates appropriate nid structures
1214 * \param[out] *min_nid string representation of min NID
1215 * \param[out] *max_nid string representation of max NID
1216 * \retval -EINVAL unsupported LNET range
1217 * \retval -ERANGE non-contiguous LNET range
1219 int cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1220 char *max_nid, size_t nidstr_length)
1222 struct nidrange *first_nidrange;
1224 struct netstrfns *nf;
1228 char min_addr_str[IPSTRING_LENGTH];
1229 char max_addr_str[IPSTRING_LENGTH];
1232 first_nidrange = list_entry(nidlist->next, struct nidrange, nr_link);
1234 netnum = first_nidrange->nr_netnum;
1235 nf = first_nidrange->nr_netstrfns;
1236 lndname = nf->nf_name;
1238 rc = nf->nf_min_max(nidlist, &min_addr, &max_addr);
1242 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1243 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1245 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1247 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1254 * Determines the min and max NID values for num LNDs
1257 * \param[out] *min_nid if provided, returns string representation of min NID
1258 * \param[out] *max_nid if provided, returns string representation of max NID
1259 * \retval -EINVAL unsupported LNET range
1260 * \retval -ERANGE non-contiguous LNET range
1262 static int cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1265 struct nidrange *nr;
1266 struct addrrange *ar;
1267 unsigned int tmp_min_addr = 0;
1268 unsigned int tmp_max_addr = 0;
1269 unsigned int min_addr = 0;
1270 unsigned int max_addr = 0;
1271 int nidlist_count = 0;
1274 list_for_each_entry(nr, nidlist, nr_link) {
1275 if (nidlist_count > 0)
1278 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1279 rc = cfs_num_ar_min_max(ar, &tmp_min_addr,
1284 if (tmp_min_addr < min_addr || min_addr == 0)
1285 min_addr = tmp_min_addr;
1286 if (tmp_max_addr > max_addr)
1287 max_addr = tmp_min_addr;
1290 if (max_nid != NULL)
1291 *max_nid = max_addr;
1292 if (min_nid != NULL)
1293 *min_nid = min_addr;
1299 * Takes an nidlist and determines the minimum and maximum
1303 * \param[out] *min_nid if provided, returns string representation of min NID
1304 * \param[out] *max_nid if provided, returns string representation of max NID
1305 * \retval -EINVAL unsupported LNET range
1306 * \retval -ERANGE non-contiguous LNET range
1308 static int cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1311 struct nidrange *nr;
1312 struct addrrange *ar;
1313 __u32 tmp_min_ip_addr = 0;
1314 __u32 tmp_max_ip_addr = 0;
1315 __u32 min_ip_addr = 0;
1316 __u32 max_ip_addr = 0;
1317 int nidlist_count = 0;
1320 list_for_each_entry(nr, nidlist, nr_link) {
1321 if (nidlist_count > 0)
1326 max_ip_addr = 0xffffffff;
1330 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1331 rc = cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1336 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1337 min_ip_addr = tmp_min_ip_addr;
1338 if (tmp_max_ip_addr > max_ip_addr)
1339 max_ip_addr = tmp_max_ip_addr;
1345 if (max_nid != NULL)
1346 *max_nid = max_ip_addr;
1347 if (min_nid != NULL)
1348 *min_nid = min_ip_addr;
1354 libcfs_expand_nidrange(struct nidrange *nr, __u32 *addrs, int max_nids)
1356 struct addrrange *ar;
1357 int rc = 0, count = max_nids;
1358 struct netstrfns *nf = nr->nr_netstrfns;
1360 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1361 rc = nf->nf_expand_addrrange(&ar->ar_numaddr_ranges, addrs,
1369 return max_nids - count;
1372 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
1375 struct nidrange *nr;
1376 int rc = 0, count = max_nids;
1379 struct netstrfns *nf;
1382 addrs = calloc(max_nids, sizeof(__u32));
1386 list_for_each_entry(nr, nidlist, nr_link) {
1387 rc = libcfs_expand_nidrange(nr, addrs, count);
1394 nf = nr->nr_netstrfns;
1395 net = LNET_MKNET(nf->nf_type, nr->nr_netnum);
1397 for (i = count - 1; i >= count - rc; i--)
1398 lnet_nidlist[j++] = LNET_MKNID(net, addrs[i]);
1404 return max_nids - count;