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, 2015, 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 <lnet/types.h>
48 #include <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_ip_addr_range_print(char *buffer, int count, struct list_head *list)
196 struct cfs_expr_list *el;
198 list_for_each_entry(el, list, el_link) {
201 i += snprintf(buffer + i, count - i, ".");
202 i += cfs_expr_list_print(buffer + i, count - i, el);
208 * Matches address (\a addr) against address set encoded in \a list.
210 * \retval 1 if \a addr matches
211 * \retval 0 otherwise
214 cfs_ip_addr_match(__u32 addr, struct list_head *list)
216 struct cfs_expr_list *el;
219 list_for_each_entry_reverse(el, list, el_link) {
220 if (!cfs_expr_list_match(addr & 0xff, el))
230 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
232 snprintf(str, size, "%u", addr);
236 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
238 snprintf(str, size, "0x%x", addr);
242 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
247 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
251 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
255 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
262 * Nf_parse_addrlist method for networks using numeric addresses.
264 * Examples of such networks are gm and elan.
266 * \retval 0 if \a str parsed to numeric address
267 * \retval errno otherwise
270 libcfs_num_parse(char *str, int len, struct list_head *list)
272 struct cfs_expr_list *el;
275 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
277 list_add_tail(&el->el_link, list);
283 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
285 struct cfs_expr_list *el;
288 list_for_each_entry(el, list, el_link) {
290 i += cfs_expr_list_print(buffer + i, count - i, el);
296 * Nf_match_addr method for networks using numeric addresses
299 * \retval 0 otherwise
302 libcfs_num_match(__u32 addr, struct list_head *numaddr)
304 struct cfs_expr_list *el;
306 assert(!list_empty(numaddr));
307 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
309 return cfs_expr_list_match(addr, el);
312 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
313 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
314 static bool cfs_num_is_contiguous(struct list_head *nidlist);
315 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
317 static struct netstrfns libcfs_netstrfns[] = {
321 .nf_modname = "klolnd",
322 .nf_addr2str = libcfs_decnum_addr2str,
323 .nf_str2addr = libcfs_lo_str2addr,
324 .nf_parse_addrlist = libcfs_num_parse,
325 .nf_print_addrlist = libcfs_num_addr_range_print,
326 .nf_match_addr = libcfs_num_match,
327 .nf_is_contiguous = cfs_num_is_contiguous,
328 .nf_min_max = cfs_num_min_max
333 .nf_modname = "ksocklnd",
334 .nf_addr2str = libcfs_ip_addr2str,
335 .nf_str2addr = libcfs_ip_str2addr,
336 .nf_parse_addrlist = cfs_ip_addr_parse,
337 .nf_print_addrlist = libcfs_ip_addr_range_print,
338 .nf_match_addr = cfs_ip_addr_match,
339 .nf_is_contiguous = cfs_ip_is_contiguous,
340 .nf_min_max = cfs_ip_min_max
345 .nf_modname = "ko2iblnd",
346 .nf_addr2str = libcfs_ip_addr2str,
347 .nf_str2addr = libcfs_ip_str2addr,
348 .nf_parse_addrlist = cfs_ip_addr_parse,
349 .nf_print_addrlist = libcfs_ip_addr_range_print,
350 .nf_match_addr = cfs_ip_addr_match,
351 .nf_is_contiguous = cfs_ip_is_contiguous,
352 .nf_min_max = cfs_ip_min_max
357 .nf_modname = "kciblnd",
358 .nf_addr2str = libcfs_ip_addr2str,
359 .nf_str2addr = libcfs_ip_str2addr,
360 .nf_parse_addrlist = cfs_ip_addr_parse,
361 .nf_print_addrlist = libcfs_ip_addr_range_print,
362 .nf_match_addr = cfs_ip_addr_match,
363 .nf_is_contiguous = cfs_ip_is_contiguous,
364 .nf_min_max = cfs_ip_min_max
367 .nf_type = OPENIBLND,
369 .nf_modname = "kopeniblnd",
370 .nf_addr2str = libcfs_ip_addr2str,
371 .nf_str2addr = libcfs_ip_str2addr,
372 .nf_parse_addrlist = cfs_ip_addr_parse,
373 .nf_print_addrlist = libcfs_ip_addr_range_print,
374 .nf_match_addr = cfs_ip_addr_match,
375 .nf_is_contiguous = cfs_ip_is_contiguous,
376 .nf_min_max = cfs_ip_min_max
381 .nf_modname = "kiiblnd",
382 .nf_addr2str = libcfs_ip_addr2str,
383 .nf_str2addr = libcfs_ip_str2addr,
384 .nf_parse_addrlist = cfs_ip_addr_parse,
385 .nf_print_addrlist = libcfs_ip_addr_range_print,
386 .nf_match_addr = cfs_ip_addr_match,
387 .nf_is_contiguous = cfs_ip_is_contiguous,
388 .nf_min_max = cfs_ip_min_max
393 .nf_modname = "kviblnd",
394 .nf_addr2str = libcfs_ip_addr2str,
395 .nf_str2addr = libcfs_ip_str2addr,
396 .nf_parse_addrlist = cfs_ip_addr_parse,
397 .nf_print_addrlist = libcfs_ip_addr_range_print,
398 .nf_match_addr = cfs_ip_addr_match,
399 .nf_is_contiguous = cfs_ip_is_contiguous,
400 .nf_min_max = cfs_ip_min_max
405 .nf_modname = "kralnd",
406 .nf_addr2str = libcfs_ip_addr2str,
407 .nf_str2addr = libcfs_ip_str2addr,
408 .nf_parse_addrlist = cfs_ip_addr_parse,
409 .nf_print_addrlist = libcfs_ip_addr_range_print,
410 .nf_match_addr = cfs_ip_addr_match,
411 .nf_is_contiguous = cfs_ip_is_contiguous,
412 .nf_min_max = cfs_ip_min_max
417 .nf_modname = "kqswlnd",
418 .nf_addr2str = libcfs_decnum_addr2str,
419 .nf_str2addr = libcfs_num_str2addr,
420 .nf_parse_addrlist = libcfs_num_parse,
421 .nf_print_addrlist = libcfs_num_addr_range_print,
422 .nf_match_addr = libcfs_num_match,
423 .nf_is_contiguous = cfs_num_is_contiguous,
424 .nf_min_max = cfs_num_min_max
429 .nf_modname = "kgmlnd",
430 .nf_addr2str = libcfs_hexnum_addr2str,
431 .nf_str2addr = libcfs_num_str2addr,
432 .nf_parse_addrlist = libcfs_num_parse,
433 .nf_print_addrlist = libcfs_num_addr_range_print,
434 .nf_match_addr = libcfs_num_match,
435 .nf_is_contiguous = cfs_num_is_contiguous,
436 .nf_min_max = cfs_num_min_max
441 .nf_modname = "kmxlnd",
442 .nf_addr2str = libcfs_ip_addr2str,
443 .nf_str2addr = libcfs_ip_str2addr,
444 .nf_parse_addrlist = cfs_ip_addr_parse,
445 .nf_print_addrlist = libcfs_ip_addr_range_print,
446 .nf_match_addr = cfs_ip_addr_match,
447 .nf_is_contiguous = cfs_ip_is_contiguous,
448 .nf_min_max = cfs_ip_min_max
453 .nf_modname = "kptllnd",
454 .nf_addr2str = libcfs_decnum_addr2str,
455 .nf_str2addr = libcfs_num_str2addr,
456 .nf_parse_addrlist = libcfs_num_parse,
457 .nf_print_addrlist = libcfs_num_addr_range_print,
458 .nf_match_addr = libcfs_num_match,
459 .nf_is_contiguous = cfs_num_is_contiguous,
460 .nf_min_max = cfs_num_min_max
465 .nf_modname = "kgnilnd",
466 .nf_addr2str = libcfs_decnum_addr2str,
467 .nf_str2addr = libcfs_num_str2addr,
468 .nf_parse_addrlist = libcfs_num_parse,
469 .nf_print_addrlist = libcfs_num_addr_range_print,
470 .nf_match_addr = libcfs_num_match,
471 .nf_is_contiguous = cfs_num_is_contiguous,
472 .nf_min_max = cfs_num_min_max
477 .nf_modname = "kgnilnd",
478 .nf_addr2str = libcfs_ip_addr2str,
479 .nf_str2addr = libcfs_ip_str2addr,
480 .nf_parse_addrlist = cfs_ip_addr_parse,
481 .nf_print_addrlist = libcfs_ip_addr_range_print,
482 .nf_match_addr = cfs_ip_addr_match,
483 .nf_is_contiguous = cfs_ip_is_contiguous,
484 .nf_min_max = cfs_ip_min_max
488 static const size_t libcfs_nnetstrfns =
489 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
491 static struct netstrfns *
492 libcfs_lnd2netstrfns(__u32 lnd)
496 for (i = 0; i < libcfs_nnetstrfns; i++)
497 if (lnd == libcfs_netstrfns[i].nf_type)
498 return &libcfs_netstrfns[i];
503 static struct netstrfns *
504 libcfs_namenum2netstrfns(const char *name)
506 struct netstrfns *nf;
509 for (i = 0; i < libcfs_nnetstrfns; i++) {
510 nf = &libcfs_netstrfns[i];
511 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
517 static struct netstrfns *
518 libcfs_name2netstrfns(const char *name)
522 for (i = 0; i < libcfs_nnetstrfns; i++)
523 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
524 return &libcfs_netstrfns[i];
530 libcfs_isknown_lnd(__u32 lnd)
532 return libcfs_lnd2netstrfns(lnd) != NULL;
536 libcfs_lnd2modname(__u32 lnd)
538 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
540 return (nf == NULL) ? NULL : nf->nf_modname;
544 libcfs_str2lnd(const char *str)
546 struct netstrfns *nf = libcfs_name2netstrfns(str);
555 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
557 struct netstrfns *nf;
559 nf = libcfs_lnd2netstrfns(lnd);
561 snprintf(buf, buf_size, "?%u?", lnd);
563 snprintf(buf, buf_size, "%s", nf->nf_name);
569 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
571 __u32 nnum = LNET_NETNUM(net);
572 __u32 lnd = LNET_NETTYP(net);
573 struct netstrfns *nf;
575 nf = libcfs_lnd2netstrfns(lnd);
577 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
579 snprintf(buf, buf_size, "%s", nf->nf_name);
581 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
587 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
589 __u32 addr = LNET_NIDADDR(nid);
590 __u32 net = LNET_NIDNET(nid);
591 __u32 nnum = LNET_NETNUM(net);
592 __u32 lnd = LNET_NETTYP(net);
593 struct netstrfns *nf;
595 if (nid == LNET_NID_ANY) {
596 strncpy(buf, "<?>", buf_size);
597 buf[buf_size - 1] = '\0';
601 nf = libcfs_lnd2netstrfns(lnd);
603 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
607 nf->nf_addr2str(addr, buf, buf_size);
608 addr_len = strlen(buf);
610 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
613 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
620 static struct netstrfns *
621 libcfs_str2net_internal(const char *str, __u32 *net)
623 struct netstrfns *nf = NULL;
628 for (i = 0; i < libcfs_nnetstrfns; i++) {
629 nf = &libcfs_netstrfns[i];
630 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
634 if (i == libcfs_nnetstrfns)
637 nob = strlen(nf->nf_name);
639 if (strlen(str) == (unsigned int)nob) {
642 if (nf->nf_type == LOLND) /* net number not allowed */
647 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
648 i != (int)strlen(str))
652 *net = LNET_MKNET(nf->nf_type, netnum);
657 libcfs_str2net(const char *str)
661 if (libcfs_str2net_internal(str, &net) != NULL)
664 return LNET_NIDNET(LNET_NID_ANY);
668 libcfs_str2nid(const char *str)
670 const char *sep = strchr(str, '@');
671 struct netstrfns *nf;
676 nf = libcfs_str2net_internal(sep + 1, &net);
680 sep = str + strlen(str);
681 net = LNET_MKNET(SOCKLND, 0);
682 nf = libcfs_lnd2netstrfns(SOCKLND);
686 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
689 return LNET_MKNID(net, addr);
693 libcfs_id2str(lnet_process_id_t id)
695 char *str = libcfs_next_nidstring();
697 if (id.pid == LNET_PID_ANY) {
698 snprintf(str, LNET_NIDSTR_SIZE,
699 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
703 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
704 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
705 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
710 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
712 if (!strcmp(str, "*")) {
713 *nidp = LNET_NID_ANY;
717 *nidp = libcfs_str2nid(str);
718 return *nidp != LNET_NID_ANY;
722 * Nid range list syntax.
725 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
726 * <nidrange> :== <addrrange> '@' <net>
727 * <addrrange> :== '*' |
730 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
732 * <cfs_expr_list> :== <number> |
734 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
735 * <range_expr> :== <number> |
736 * <number> '-' <number> |
737 * <number> '-' <number> '/' <number>
738 * <net> :== <netname> | <netname><number>
739 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
740 * "vib" | "ra" | "elan" | "mx" | "ptl"
745 * Structure to represent \<nidrange\> token of the syntax.
747 * One of this is created for each \<net\> parsed.
751 * Link to list of this structures which is built on nid range
754 struct list_head nr_link;
756 * List head for addrrange::ar_link.
758 struct list_head nr_addrranges;
760 * Flag indicating that *@<net> is found.
764 * Pointer to corresponding element of libcfs_netstrfns.
766 struct netstrfns *nr_netstrfns;
768 * Number of network. E.g. 5 if \<net\> is "elan5".
774 * Structure to represent \<addrrange\> token of the syntax.
778 * Link to nidrange::nr_addrranges.
780 struct list_head ar_link;
782 * List head for cfs_expr_list::el_list.
784 struct list_head ar_numaddr_ranges;
788 * Parses \<addrrange\> token on the syntax.
790 * Allocates struct addrrange and links to \a nidrange via
791 * (nidrange::nr_addrranges)
793 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
794 * \retval -errno otherwise
797 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
799 struct addrrange *addrrange;
801 if (src->ls_len == 1 && src->ls_str[0] == '*') {
802 nidrange->nr_all = 1;
806 addrrange = calloc(1, sizeof(struct addrrange));
807 if (addrrange == NULL)
809 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
810 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
812 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
814 &addrrange->ar_numaddr_ranges);
818 * Finds or creates struct nidrange.
820 * Checks if \a src is a valid network name, looks for corresponding
821 * nidrange on the ist of nidranges (\a nidlist), creates new struct
822 * nidrange if it is not found.
824 * \retval pointer to struct nidrange matching network specified via \a src
825 * \retval NULL if \a src does not match any network
827 static struct nidrange *
828 add_nidrange(const struct cfs_lstr *src,
829 struct list_head *nidlist)
831 struct netstrfns *nf;
836 if (src->ls_len >= LNET_NIDSTR_SIZE)
839 nf = libcfs_namenum2netstrfns(src->ls_str);
842 endlen = src->ls_len - strlen(nf->nf_name);
844 /* network name only, e.g. "elan" or "tcp" */
847 /* e.g. "elan25" or "tcp23", refuse to parse if
848 * network name is not appended with decimal or
849 * hexadecimal number */
850 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
851 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
855 list_for_each_entry(nr, nidlist, nr_link) {
856 if (nr->nr_netstrfns != nf)
858 if (nr->nr_netnum != netnum)
863 nr = calloc(1, sizeof(struct nidrange));
866 list_add_tail(&nr->nr_link, nidlist);
867 INIT_LIST_HEAD(&nr->nr_addrranges);
868 nr->nr_netstrfns = nf;
870 nr->nr_netnum = netnum;
876 * Parses \<nidrange\> token of the syntax.
878 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
879 * \retval 0 otherwise
882 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
884 struct cfs_lstr addrrange;
890 if (cfs_gettok(src, '@', &addrrange) == 0)
893 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
896 nr = add_nidrange(&net, nidlist);
900 if (parse_addrange(&addrrange, nr) != 0)
905 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
906 tmp.ls_len, tmp.ls_str);
911 * Frees addrrange structures of \a list.
913 * For each struct addrrange structure found on \a list it frees
914 * cfs_expr_list list attached to it and frees the addrrange itself.
919 free_addrranges(struct list_head *list)
921 while (!list_empty(list)) {
922 struct addrrange *ar;
924 ar = list_entry(list->next, struct addrrange, ar_link);
926 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
927 list_del(&ar->ar_link);
933 * Frees nidrange strutures of \a list.
935 * For each struct nidrange structure found on \a list it frees
936 * addrrange list attached to it and frees the nidrange itself.
941 cfs_free_nidlist(struct list_head *list)
943 struct list_head *pos, *next;
946 list_for_each_safe(pos, next, list) {
947 nr = list_entry(pos, struct nidrange, nr_link);
948 free_addrranges(&nr->nr_addrranges);
955 * Parses nid range list.
957 * Parses with rigorous syntax and overflow checking \a str into
958 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
959 * structures and links that structure to \a nidlist. The resulting
960 * list can be used to match a NID againts set of NIDS defined by \a
964 * \retval 1 on success
965 * \retval 0 otherwise
968 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
976 INIT_LIST_HEAD(nidlist);
978 rc = cfs_gettok(&src, ' ', &res);
980 cfs_free_nidlist(nidlist);
983 rc = parse_nidrange(&res, nidlist);
985 cfs_free_nidlist(nidlist);
993 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
995 * \see cfs_parse_nidlist()
998 * \retval 0 otherwises
1000 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
1002 struct nidrange *nr;
1003 struct addrrange *ar;
1005 list_for_each_entry(nr, nidlist, nr_link) {
1006 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1008 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1012 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1013 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1014 &ar->ar_numaddr_ranges))
1021 * Print the network part of the nidrange \a nr into the specified \a buffer.
1023 * \retval number of characters written
1026 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1028 struct netstrfns *nf = nr->nr_netstrfns;
1030 if (nr->nr_netnum == 0)
1031 return snprintf(buffer, count, "@%s", nf->nf_name);
1033 return snprintf(buffer, count, "@%s%u",
1034 nf->nf_name, nr->nr_netnum);
1039 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1040 * At max \a count characters can be printed into \a buffer.
1042 * \retval number of characters written
1045 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1046 struct nidrange *nr)
1049 struct addrrange *ar;
1050 struct netstrfns *nf = nr->nr_netstrfns;
1052 list_for_each_entry(ar, addrranges, ar_link) {
1054 i += snprintf(buffer + i, count - i, " ");
1055 i += nf->nf_print_addrlist(buffer + i, count - i,
1056 &ar->ar_numaddr_ranges);
1057 i += cfs_print_network(buffer + i, count - i, nr);
1063 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1064 * At max \a count characters can be printed into \a buffer.
1065 * Nidranges are separated by a space character.
1067 * \retval number of characters written
1069 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1072 struct nidrange *nr;
1077 list_for_each_entry(nr, nidlist, nr_link) {
1079 i += snprintf(buffer + i, count - i, " ");
1081 if (nr->nr_all != 0) {
1082 assert(list_empty(&nr->nr_addrranges));
1083 i += snprintf(buffer + i, count - i, "*");
1084 i += cfs_print_network(buffer + i, count - i, nr);
1086 i += cfs_print_addrranges(buffer + i, count - i,
1087 &nr->nr_addrranges, nr);
1094 * Determines minimum and maximum addresses for a single
1095 * numeric address range
1101 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1104 struct cfs_expr_list *el;
1105 struct cfs_range_expr *re;
1106 __u32 tmp_ip_addr = 0;
1107 unsigned int min_ip[4] = {0};
1108 unsigned int max_ip[4] = {0};
1111 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1112 list_for_each_entry(re, &el->el_exprs, re_link) {
1113 min_ip[re_count] = re->re_lo;
1114 max_ip[re_count] = re->re_hi;
1119 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1120 (min_ip[2] << 8) | min_ip[3]);
1122 if (min_nid != NULL)
1123 *min_nid = tmp_ip_addr;
1125 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1126 (max_ip[2] << 8) | max_ip[3]);
1128 if (max_nid != NULL)
1129 *max_nid = tmp_ip_addr;
1133 * Determines minimum and maximum addresses for a single
1134 * numeric address range
1140 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1143 struct cfs_expr_list *el;
1144 struct cfs_range_expr *re;
1145 unsigned int min_addr = 0;
1146 unsigned int max_addr = 0;
1148 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1149 list_for_each_entry(re, &el->el_exprs, re_link) {
1150 if (re->re_lo < min_addr || min_addr == 0)
1151 min_addr = re->re_lo;
1152 if (re->re_hi > max_addr)
1153 max_addr = re->re_hi;
1157 if (min_nid != NULL)
1158 *min_nid = min_addr;
1159 if (max_nid != NULL)
1160 *max_nid = max_addr;
1164 * Determines whether an expression list in an nidrange contains exactly
1165 * one contiguous address range. Calls the correct netstrfns for the LND
1169 * \retval true if contiguous
1170 * \retval false if not contiguous
1172 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1174 struct nidrange *nr;
1175 struct netstrfns *nf = NULL;
1176 char *lndname = NULL;
1179 list_for_each_entry(nr, nidlist, nr_link) {
1180 nf = nr->nr_netstrfns;
1181 if (lndname == NULL)
1182 lndname = nf->nf_name;
1184 netnum = nr->nr_netnum;
1186 if (strcmp(lndname, nf->nf_name) != 0 ||
1187 netnum != nr->nr_netnum)
1194 if (!nf->nf_is_contiguous(nidlist))
1201 * Determines whether an expression list in an num nidrange contains exactly
1202 * one contiguous address range.
1206 * \retval true if contiguous
1207 * \retval false if not contiguous
1209 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1211 struct nidrange *nr;
1212 struct addrrange *ar;
1213 struct cfs_expr_list *el;
1214 struct cfs_range_expr *re;
1216 __u32 last_end_nid = 0;
1217 __u32 current_start_nid = 0;
1218 __u32 current_end_nid = 0;
1220 list_for_each_entry(nr, nidlist, nr_link) {
1221 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1222 cfs_num_ar_min_max(ar, ¤t_start_nid,
1224 if (last_end_nid != 0 &&
1225 (current_start_nid - last_end_nid != 1))
1227 last_end_nid = current_end_nid;
1228 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1230 list_for_each_entry(re, &el->el_exprs,
1232 if (re->re_stride > 1)
1234 else if (last_hi != 0 &&
1235 re->re_hi - last_hi != 1)
1237 last_hi = re->re_hi;
1247 * Determines whether an expression list in an ip nidrange contains exactly
1248 * one contiguous address range.
1252 * \retval true if contiguous
1253 * \retval false if not contiguous
1255 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1257 struct nidrange *nr;
1258 struct addrrange *ar;
1259 struct cfs_expr_list *el;
1260 struct cfs_range_expr *re;
1264 __u32 last_end_nid = 0;
1265 __u32 current_start_nid = 0;
1266 __u32 current_end_nid = 0;
1268 list_for_each_entry(nr, nidlist, nr_link) {
1269 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1272 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1274 if (last_end_nid != 0 &&
1275 (current_start_nid - last_end_nid != 1))
1277 last_end_nid = current_end_nid;
1278 list_for_each_entry(el,
1279 &ar->ar_numaddr_ranges,
1282 list_for_each_entry(re, &el->el_exprs,
1285 if (re->re_stride > 1 ||
1286 (last_diff > 0 && last_hi != 255) ||
1287 (last_diff > 0 && last_hi == 255 &&
1290 last_hi = re->re_hi;
1291 last_diff = re->re_hi - re->re_lo;
1301 * Takes a linked list of nidrange expressions, determines the minimum
1302 * and maximum nid and creates appropriate nid structures
1308 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1309 char *max_nid, size_t nidstr_length)
1311 struct nidrange *nr;
1312 struct netstrfns *nf = NULL;
1316 char *lndname = NULL;
1317 char min_addr_str[IPSTRING_LENGTH];
1318 char max_addr_str[IPSTRING_LENGTH];
1320 list_for_each_entry(nr, nidlist, nr_link) {
1321 nf = nr->nr_netstrfns;
1322 lndname = nf->nf_name;
1324 netnum = nr->nr_netnum;
1326 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1328 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1329 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1331 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1333 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1338 * Determines the min and max NID values for num LNDs
1344 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1347 struct nidrange *nr;
1348 struct addrrange *ar;
1349 unsigned int tmp_min_addr = 0;
1350 unsigned int tmp_max_addr = 0;
1351 unsigned int min_addr = 0;
1352 unsigned int max_addr = 0;
1354 list_for_each_entry(nr, nidlist, nr_link) {
1355 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1356 cfs_num_ar_min_max(ar, &tmp_min_addr,
1358 if (tmp_min_addr < min_addr || min_addr == 0)
1359 min_addr = tmp_min_addr;
1360 if (tmp_max_addr > max_addr)
1361 max_addr = tmp_min_addr;
1364 *max_nid = max_addr;
1365 *min_nid = min_addr;
1369 * Takes an nidlist and determines the minimum and maximum
1376 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1379 struct nidrange *nr;
1380 struct addrrange *ar;
1381 __u32 tmp_min_ip_addr = 0;
1382 __u32 tmp_max_ip_addr = 0;
1383 __u32 min_ip_addr = 0;
1384 __u32 max_ip_addr = 0;
1386 list_for_each_entry(nr, nidlist, nr_link) {
1387 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1388 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1390 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1391 min_ip_addr = tmp_min_ip_addr;
1392 if (tmp_max_ip_addr > max_ip_addr)
1393 max_ip_addr = tmp_max_ip_addr;
1397 if (min_nid != NULL)
1398 *min_nid = min_ip_addr;
1399 if (max_nid != NULL)
1400 *max_nid = max_ip_addr;