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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/util/nidstrings.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
50 #include <libcfs/util/string.h>
51 #include <lnet/types.h>
52 #include <lnet/nidstr.h>
57 /* max value for numeric network address */
58 #define MAX_NUMERIC_VALUE 0xffffffff
60 #define IPSTRING_LENGTH 16
62 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
63 * consistent in all conversion functions. Some code fragments are copied
64 * around for the sake of clarity...
67 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
68 * Choose the number of nidstrings to support the MAXIMUM expected number of
69 * concurrent users. If there are more, the returned string will be volatile.
70 * NB this number must allow for a process to be descheduled for a timeslice
71 * between getting its string and using it.
74 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
75 static int libcfs_nidstring_idx;
78 libcfs_next_nidstring(void)
82 str = libcfs_nidstrings[libcfs_nidstring_idx++];
83 if (libcfs_nidstring_idx ==
84 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
85 libcfs_nidstring_idx = 0;
91 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
98 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
100 snprintf(str, size, "%u.%u.%u.%u",
101 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
102 (addr >> 8) & 0xff, addr & 0xff);
105 /* CAVEAT EMPTOR XscanfX
106 * I use "%n" at the end of a sscanf format to detect trailing junk. However
107 * sscanf may return immediately if it sees the terminating '0' in a string, so
108 * I initialise the %n variable to the expected length. If sscanf sets it;
109 * fine, if it doesn't, then the scan ended at the end of the string, which is
112 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
118 int n = nob; /* XscanfX */
121 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
123 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
124 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
125 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
129 #ifdef HAVE_GETHOSTBYNAME
130 /* known hostname? */
131 if (('a' <= str[0] && str[0] <= 'z') ||
132 ('A' <= str[0] && str[0] <= 'Z')) {
135 tmp = calloc(1, nob + 1);
139 memcpy(tmp, str, nob);
142 he = gethostbyname(tmp);
147 __u32 ip = *(__u32 *)he->h_addr;
159 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
161 struct cfs_expr_list *el;
170 while (src.ls_str != NULL) {
173 if (!cfs_gettok(&src, '.', &res)) {
178 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
182 list_add_tail(&el->el_link, list);
191 cfs_expr_list_free_list(list);
197 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
200 struct cfs_expr_list *el;
202 list_for_each_entry(el, list, el_link) {
205 i += snprintf(buffer + i, count - i, ".");
206 i += cfs_expr_list_print(buffer + i, count - i, el);
212 * Matches address (\a addr) against address set encoded in \a list.
214 * \retval 1 if \a addr matches
215 * \retval 0 otherwise
218 cfs_ip_addr_match(__u32 addr, struct list_head *list)
220 struct cfs_expr_list *el;
223 list_for_each_entry_reverse(el, list, el_link) {
224 if (!cfs_expr_list_match(addr & 0xff, el))
234 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
236 snprintf(str, size, "%u", addr);
240 libcfs_hexnum_addr2str(__u32 addr, char *str, size_t size)
242 snprintf(str, size, "0x%x", addr);
246 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
251 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
255 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
259 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
266 * Nf_parse_addrlist method for networks using numeric addresses.
268 * Examples of such networks are gm and elan.
270 * \retval 0 if \a str parsed to numeric address
271 * \retval errno otherwise
274 libcfs_num_parse(char *str, int len, struct list_head *list)
276 struct cfs_expr_list *el;
279 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
281 list_add_tail(&el->el_link, list);
287 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
289 struct cfs_expr_list *el;
292 list_for_each_entry(el, list, el_link) {
294 i += cfs_expr_list_print(buffer + i, count - i, el);
300 * Nf_match_addr method for networks using numeric addresses
303 * \retval 0 otherwise
306 libcfs_num_match(__u32 addr, struct list_head *numaddr)
308 struct cfs_expr_list *el;
310 assert(!list_empty(numaddr));
311 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
313 return cfs_expr_list_match(addr, el);
316 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
317 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
318 static bool cfs_num_is_contiguous(struct list_head *nidlist);
319 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
321 static struct netstrfns libcfs_netstrfns[] = {
322 {/* .nf_type */ LOLND,
324 /* .nf_modname */ "klolnd",
325 /* .nf_addr2str */ libcfs_decnum_addr2str,
326 /* .nf_str2addr */ libcfs_lo_str2addr,
327 /* .nf_parse_addr*/ libcfs_num_parse,
328 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
329 /* .nf_match_addr*/ libcfs_num_match,
330 /* .nf_is_contiguous */ cfs_num_is_contiguous,
331 /* .nf_min_max */ cfs_num_min_max},
332 {/* .nf_type */ SOCKLND,
333 /* .nf_name */ "tcp",
334 /* .nf_modname */ "ksocklnd",
335 /* .nf_addr2str */ libcfs_ip_addr2str,
336 /* .nf_str2addr */ libcfs_ip_str2addr,
337 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
338 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
339 /* .nf_match_addr*/ cfs_ip_addr_match,
340 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
341 /* .nf_min_max */ cfs_ip_min_max},
342 {/* .nf_type */ O2IBLND,
343 /* .nf_name */ "o2ib",
344 /* .nf_modname */ "ko2iblnd",
345 /* .nf_addr2str */ libcfs_ip_addr2str,
346 /* .nf_str2addr */ libcfs_ip_str2addr,
347 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
348 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
349 /* .nf_match_addr*/ cfs_ip_addr_match,
350 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
351 /* .nf_min_max */ cfs_ip_min_max},
352 {/* .nf_type */ CIBLND,
353 /* .nf_name */ "cib",
354 /* .nf_modname */ "kciblnd",
355 /* .nf_addr2str */ libcfs_ip_addr2str,
356 /* .nf_str2addr */ libcfs_ip_str2addr,
357 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
358 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
359 /* .nf_match_addr*/ cfs_ip_addr_match,
360 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
361 /* .nf_min_max */ cfs_ip_min_max},
362 {/* .nf_type */ OPENIBLND,
363 /* .nf_name */ "openib",
364 /* .nf_modname */ "kopeniblnd",
365 /* .nf_addr2str */ libcfs_ip_addr2str,
366 /* .nf_str2addr */ libcfs_ip_str2addr,
367 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
368 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
369 /* .nf_match_addr*/ cfs_ip_addr_match,
370 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
371 /* .nf_min_max */ cfs_ip_min_max},
372 {/* .nf_type */ IIBLND,
373 /* .nf_name */ "iib",
374 /* .nf_modname */ "kiiblnd",
375 /* .nf_addr2str */ libcfs_ip_addr2str,
376 /* .nf_str2addr */ libcfs_ip_str2addr,
377 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
378 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
379 /* .nf_match_addr*/ cfs_ip_addr_match,
380 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
381 /* .nf_min_max */ cfs_ip_min_max},
382 {/* .nf_type */ VIBLND,
383 /* .nf_name */ "vib",
384 /* .nf_modname */ "kviblnd",
385 /* .nf_addr2str */ libcfs_ip_addr2str,
386 /* .nf_str2addr */ libcfs_ip_str2addr,
387 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
388 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
389 /* .nf_match_addr*/ cfs_ip_addr_match,
390 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
391 /* .nf_min_max */ cfs_ip_min_max},
392 {/* .nf_type */ RALND,
394 /* .nf_modname */ "kralnd",
395 /* .nf_addr2str */ libcfs_ip_addr2str,
396 /* .nf_str2addr */ libcfs_ip_str2addr,
397 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
398 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
399 /* .nf_match_addr*/ cfs_ip_addr_match,
400 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
401 /* .nf_min_max */ cfs_ip_min_max},
402 {/* .nf_type */ QSWLND,
403 /* .nf_name */ "elan",
404 /* .nf_modname */ "kqswlnd",
405 /* .nf_addr2str */ libcfs_decnum_addr2str,
406 /* .nf_str2addr */ libcfs_num_str2addr,
407 /* .nf_parse_addrlist*/ libcfs_num_parse,
408 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
409 /* .nf_match_addr*/ libcfs_num_match,
410 /* .nf_is_contiguous */ cfs_num_is_contiguous,
411 /* .nf_min_max */ cfs_num_min_max},
412 {/* .nf_type */ GMLND,
414 /* .nf_modname */ "kgmlnd",
415 /* .nf_addr2str */ libcfs_hexnum_addr2str,
416 /* .nf_str2addr */ libcfs_num_str2addr,
417 /* .nf_parse_addrlist*/ libcfs_num_parse,
418 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
419 /* .nf_match_addr*/ libcfs_num_match,
420 /* .nf_is_contiguous */ cfs_num_is_contiguous,
421 /* .nf_min_max */ cfs_num_min_max},
422 {/* .nf_type */ MXLND,
424 /* .nf_modname */ "kmxlnd",
425 /* .nf_addr2str */ libcfs_ip_addr2str,
426 /* .nf_str2addr */ libcfs_ip_str2addr,
427 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
428 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
429 /* .nf_match_addr*/ cfs_ip_addr_match,
430 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
431 /* .nf_min_max */ cfs_ip_min_max},
432 {/* .nf_type */ PTLLND,
433 /* .nf_name */ "ptl",
434 /* .nf_modname */ "kptllnd",
435 /* .nf_addr2str */ libcfs_decnum_addr2str,
436 /* .nf_str2addr */ libcfs_num_str2addr,
437 /* .nf_parse_addrlist*/ libcfs_num_parse,
438 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
439 /* .nf_match_addr*/ libcfs_num_match,
440 /* .nf_is_contiguous */ cfs_num_is_contiguous,
441 /* .nf_min_max */ cfs_num_min_max},
442 {/* .nf_type */ GNILND,
443 /* .nf_name */ "gni",
444 /* .nf_modname */ "kgnilnd",
445 /* .nf_addr2str */ libcfs_decnum_addr2str,
446 /* .nf_str2addr */ libcfs_num_str2addr,
447 /* .nf_parse_addrlist*/ libcfs_num_parse,
448 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
449 /* .nf_match_addr*/ libcfs_num_match,
450 /* .nf_is_contiguous */ cfs_num_is_contiguous,
451 /* .nf_min_max */ cfs_num_min_max},
452 {/* .nf_type */ GNIIPLND,
453 /* .nf_name */ "gip",
454 /* .nf_modname */ "kgnilnd",
455 /* .nf_addr2str */ libcfs_ip_addr2str,
456 /* .nf_str2addr */ libcfs_ip_str2addr,
457 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
458 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
459 /* .nf_match_addr*/ cfs_ip_addr_match,
460 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
461 /* .nf_min_max */ cfs_ip_min_max},
464 static const size_t libcfs_nnetstrfns =
465 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
467 static struct netstrfns *
468 libcfs_lnd2netstrfns(__u32 lnd)
472 for (i = 0; i < libcfs_nnetstrfns; i++)
473 if (lnd == libcfs_netstrfns[i].nf_type)
474 return &libcfs_netstrfns[i];
479 static struct netstrfns *
480 libcfs_namenum2netstrfns(const char *name)
482 struct netstrfns *nf;
485 for (i = 0; i < libcfs_nnetstrfns; i++) {
486 nf = &libcfs_netstrfns[i];
487 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
493 static struct netstrfns *
494 libcfs_name2netstrfns(const char *name)
498 for (i = 0; i < libcfs_nnetstrfns; i++)
499 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
500 return &libcfs_netstrfns[i];
506 libcfs_isknown_lnd(__u32 lnd)
508 return libcfs_lnd2netstrfns(lnd) != NULL;
512 libcfs_lnd2modname(__u32 lnd)
514 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
516 return (nf == NULL) ? NULL : nf->nf_modname;
520 libcfs_str2lnd(const char *str)
522 struct netstrfns *nf = libcfs_name2netstrfns(str);
531 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
533 struct netstrfns *nf;
535 nf = libcfs_lnd2netstrfns(lnd);
537 snprintf(buf, buf_size, "?%u?", lnd);
539 snprintf(buf, buf_size, "%s", nf->nf_name);
545 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
547 __u32 nnum = LNET_NETNUM(net);
548 __u32 lnd = LNET_NETTYP(net);
549 struct netstrfns *nf;
551 nf = libcfs_lnd2netstrfns(lnd);
553 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
555 snprintf(buf, buf_size, "%s", nf->nf_name);
557 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
563 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
565 __u32 addr = LNET_NIDADDR(nid);
566 __u32 net = LNET_NIDNET(nid);
567 __u32 nnum = LNET_NETNUM(net);
568 __u32 lnd = LNET_NETTYP(net);
569 struct netstrfns *nf;
571 if (nid == LNET_NID_ANY) {
572 strncpy(buf, "<?>", buf_size);
573 buf[buf_size - 1] = '\0';
577 nf = libcfs_lnd2netstrfns(lnd);
579 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
583 nf->nf_addr2str(addr, buf, buf_size);
584 addr_len = strlen(buf);
586 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
589 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
596 static struct netstrfns *
597 libcfs_str2net_internal(const char *str, __u32 *net)
599 struct netstrfns *nf = NULL;
604 for (i = 0; i < libcfs_nnetstrfns; i++) {
605 nf = &libcfs_netstrfns[i];
606 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
610 if (i == libcfs_nnetstrfns)
613 nob = strlen(nf->nf_name);
615 if (strlen(str) == (unsigned int)nob) {
618 if (nf->nf_type == LOLND) /* net number not allowed */
623 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
624 i != (int)strlen(str))
628 *net = LNET_MKNET(nf->nf_type, netnum);
633 libcfs_str2net(const char *str)
637 if (libcfs_str2net_internal(str, &net) != NULL)
640 return LNET_NIDNET(LNET_NID_ANY);
644 libcfs_str2nid(const char *str)
646 const char *sep = strchr(str, '@');
647 struct netstrfns *nf;
652 nf = libcfs_str2net_internal(sep + 1, &net);
656 sep = str + strlen(str);
657 net = LNET_MKNET(SOCKLND, 0);
658 nf = libcfs_lnd2netstrfns(SOCKLND);
662 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
665 return LNET_MKNID(net, addr);
669 libcfs_id2str(lnet_process_id_t id)
671 char *str = libcfs_next_nidstring();
673 if (id.pid == LNET_PID_ANY) {
674 snprintf(str, LNET_NIDSTR_SIZE,
675 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
679 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
680 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
681 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
686 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
688 if (!strcmp(str, "*")) {
689 *nidp = LNET_NID_ANY;
693 *nidp = libcfs_str2nid(str);
694 return *nidp != LNET_NID_ANY;
698 * Nid range list syntax.
701 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
702 * <nidrange> :== <addrrange> '@' <net>
703 * <addrrange> :== '*' |
706 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
708 * <cfs_expr_list> :== <number> |
710 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
711 * <range_expr> :== <number> |
712 * <number> '-' <number> |
713 * <number> '-' <number> '/' <number>
714 * <net> :== <netname> | <netname><number>
715 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
716 * "vib" | "ra" | "elan" | "mx" | "ptl"
721 * Structure to represent \<nidrange\> token of the syntax.
723 * One of this is created for each \<net\> parsed.
727 * Link to list of this structures which is built on nid range
730 struct list_head nr_link;
732 * List head for addrrange::ar_link.
734 struct list_head nr_addrranges;
736 * Flag indicating that *@<net> is found.
740 * Pointer to corresponding element of libcfs_netstrfns.
742 struct netstrfns *nr_netstrfns;
744 * Number of network. E.g. 5 if \<net\> is "elan5".
750 * Structure to represent \<addrrange\> token of the syntax.
754 * Link to nidrange::nr_addrranges.
756 struct list_head ar_link;
758 * List head for cfs_expr_list::el_list.
760 struct list_head ar_numaddr_ranges;
764 * Parses \<addrrange\> token on the syntax.
766 * Allocates struct addrrange and links to \a nidrange via
767 * (nidrange::nr_addrranges)
769 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
770 * \retval -errno otherwise
773 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
775 struct addrrange *addrrange;
777 if (src->ls_len == 1 && src->ls_str[0] == '*') {
778 nidrange->nr_all = 1;
782 addrrange = calloc(1, sizeof(struct addrrange));
783 if (addrrange == NULL)
785 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
786 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
788 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
790 &addrrange->ar_numaddr_ranges);
794 * Finds or creates struct nidrange.
796 * Checks if \a src is a valid network name, looks for corresponding
797 * nidrange on the ist of nidranges (\a nidlist), creates new struct
798 * nidrange if it is not found.
800 * \retval pointer to struct nidrange matching network specified via \a src
801 * \retval NULL if \a src does not match any network
803 static struct nidrange *
804 add_nidrange(const struct cfs_lstr *src,
805 struct list_head *nidlist)
807 struct netstrfns *nf;
812 if (src->ls_len >= LNET_NIDSTR_SIZE)
815 nf = libcfs_namenum2netstrfns(src->ls_str);
818 endlen = src->ls_len - strlen(nf->nf_name);
820 /* network name only, e.g. "elan" or "tcp" */
823 /* e.g. "elan25" or "tcp23", refuse to parse if
824 * network name is not appended with decimal or
825 * hexadecimal number */
826 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
827 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
831 list_for_each_entry(nr, nidlist, nr_link) {
832 if (nr->nr_netstrfns != nf)
834 if (nr->nr_netnum != netnum)
839 nr = calloc(1, sizeof(struct nidrange));
842 list_add_tail(&nr->nr_link, nidlist);
843 INIT_LIST_HEAD(&nr->nr_addrranges);
844 nr->nr_netstrfns = nf;
846 nr->nr_netnum = netnum;
852 * Parses \<nidrange\> token of the syntax.
854 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
855 * \retval 0 otherwise
858 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
860 struct cfs_lstr addrrange;
866 if (cfs_gettok(src, '@', &addrrange) == 0)
869 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
872 nr = add_nidrange(&net, nidlist);
876 if (parse_addrange(&addrrange, nr) != 0)
881 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
882 tmp.ls_len, tmp.ls_str);
887 * Frees addrrange structures of \a list.
889 * For each struct addrrange structure found on \a list it frees
890 * cfs_expr_list list attached to it and frees the addrrange itself.
895 free_addrranges(struct list_head *list)
897 while (!list_empty(list)) {
898 struct addrrange *ar;
900 ar = list_entry(list->next, struct addrrange, ar_link);
902 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
903 list_del(&ar->ar_link);
909 * Frees nidrange strutures of \a list.
911 * For each struct nidrange structure found on \a list it frees
912 * addrrange list attached to it and frees the nidrange itself.
917 cfs_free_nidlist(struct list_head *list)
919 struct list_head *pos, *next;
922 list_for_each_safe(pos, next, list) {
923 nr = list_entry(pos, struct nidrange, nr_link);
924 free_addrranges(&nr->nr_addrranges);
931 * Parses nid range list.
933 * Parses with rigorous syntax and overflow checking \a str into
934 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
935 * structures and links that structure to \a nidlist. The resulting
936 * list can be used to match a NID againts set of NIDS defined by \a
940 * \retval 1 on success
941 * \retval 0 otherwise
944 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
952 INIT_LIST_HEAD(nidlist);
954 rc = cfs_gettok(&src, ' ', &res);
956 cfs_free_nidlist(nidlist);
959 rc = parse_nidrange(&res, nidlist);
961 cfs_free_nidlist(nidlist);
969 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
971 * \see cfs_parse_nidlist()
974 * \retval 0 otherwises
976 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
979 struct addrrange *ar;
981 list_for_each_entry(nr, nidlist, nr_link) {
982 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
984 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
988 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
989 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
990 &ar->ar_numaddr_ranges))
997 * Print the network part of the nidrange \a nr into the specified \a buffer.
999 * \retval number of characters written
1002 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1004 struct netstrfns *nf = nr->nr_netstrfns;
1006 if (nr->nr_netnum == 0)
1007 return snprintf(buffer, count, "@%s", nf->nf_name);
1009 return snprintf(buffer, count, "@%s%u",
1010 nf->nf_name, nr->nr_netnum);
1015 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1016 * At max \a count characters can be printed into \a buffer.
1018 * \retval number of characters written
1021 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1022 struct nidrange *nr)
1025 struct addrrange *ar;
1026 struct netstrfns *nf = nr->nr_netstrfns;
1028 list_for_each_entry(ar, addrranges, ar_link) {
1030 i += snprintf(buffer + i, count - i, " ");
1031 i += nf->nf_print_addrlist(buffer + i, count - i,
1032 &ar->ar_numaddr_ranges);
1033 i += cfs_print_network(buffer + i, count - i, nr);
1039 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1040 * At max \a count characters can be printed into \a buffer.
1041 * Nidranges are separated by a space character.
1043 * \retval number of characters written
1045 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1048 struct nidrange *nr;
1053 list_for_each_entry(nr, nidlist, nr_link) {
1055 i += snprintf(buffer + i, count - i, " ");
1057 if (nr->nr_all != 0) {
1058 assert(list_empty(&nr->nr_addrranges));
1059 i += snprintf(buffer + i, count - i, "*");
1060 i += cfs_print_network(buffer + i, count - i, nr);
1062 i += cfs_print_addrranges(buffer + i, count - i,
1063 &nr->nr_addrranges, nr);
1070 * Determines minimum and maximum addresses for a single
1071 * numeric address range
1077 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1080 struct cfs_expr_list *el;
1081 struct cfs_range_expr *re;
1082 __u32 tmp_ip_addr = 0;
1083 unsigned int min_ip[4] = {0};
1084 unsigned int max_ip[4] = {0};
1087 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1088 list_for_each_entry(re, &el->el_exprs, re_link) {
1089 min_ip[re_count] = re->re_lo;
1090 max_ip[re_count] = re->re_hi;
1095 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1096 (min_ip[2] << 8) | min_ip[3]);
1098 if (min_nid != NULL)
1099 *min_nid = tmp_ip_addr;
1101 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1102 (max_ip[2] << 8) | max_ip[3]);
1104 if (max_nid != NULL)
1105 *max_nid = tmp_ip_addr;
1109 * Determines minimum and maximum addresses for a single
1110 * numeric address range
1116 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1119 struct cfs_expr_list *el;
1120 struct cfs_range_expr *re;
1121 unsigned int min_addr = 0;
1122 unsigned int max_addr = 0;
1124 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1125 list_for_each_entry(re, &el->el_exprs, re_link) {
1126 if (re->re_lo < min_addr || min_addr == 0)
1127 min_addr = re->re_lo;
1128 if (re->re_hi > max_addr)
1129 max_addr = re->re_hi;
1133 if (min_nid != NULL)
1134 *min_nid = min_addr;
1135 if (max_nid != NULL)
1136 *max_nid = max_addr;
1140 * Determines whether an expression list in an nidrange contains exactly
1141 * one contiguous address range. Calls the correct netstrfns for the LND
1145 * \retval true if contiguous
1146 * \retval false if not contiguous
1148 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1150 struct nidrange *nr;
1151 struct netstrfns *nf = NULL;
1152 char *lndname = NULL;
1155 list_for_each_entry(nr, nidlist, nr_link) {
1156 nf = nr->nr_netstrfns;
1157 if (lndname == NULL)
1158 lndname = nf->nf_name;
1160 netnum = nr->nr_netnum;
1162 if (strcmp(lndname, nf->nf_name) != 0 ||
1163 netnum != nr->nr_netnum)
1170 if (!nf->nf_is_contiguous(nidlist))
1177 * Determines whether an expression list in an num nidrange contains exactly
1178 * one contiguous address range.
1182 * \retval true if contiguous
1183 * \retval false if not contiguous
1185 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1187 struct nidrange *nr;
1188 struct addrrange *ar;
1189 struct cfs_expr_list *el;
1190 struct cfs_range_expr *re;
1192 __u32 last_end_nid = 0;
1193 __u32 current_start_nid = 0;
1194 __u32 current_end_nid = 0;
1196 list_for_each_entry(nr, nidlist, nr_link) {
1197 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1198 cfs_num_ar_min_max(ar, ¤t_start_nid,
1200 if (last_end_nid != 0 &&
1201 (current_start_nid - last_end_nid != 1))
1203 last_end_nid = current_end_nid;
1204 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1206 list_for_each_entry(re, &el->el_exprs,
1208 if (re->re_stride > 1)
1210 else if (last_hi != 0 &&
1211 re->re_hi - last_hi != 1)
1213 last_hi = re->re_hi;
1223 * Determines whether an expression list in an ip nidrange contains exactly
1224 * one contiguous address range.
1228 * \retval true if contiguous
1229 * \retval false if not contiguous
1231 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1233 struct nidrange *nr;
1234 struct addrrange *ar;
1235 struct cfs_expr_list *el;
1236 struct cfs_range_expr *re;
1240 __u32 last_end_nid = 0;
1241 __u32 current_start_nid = 0;
1242 __u32 current_end_nid = 0;
1244 list_for_each_entry(nr, nidlist, nr_link) {
1245 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1248 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1250 if (last_end_nid != 0 &&
1251 (current_start_nid - last_end_nid != 1))
1253 last_end_nid = current_end_nid;
1254 list_for_each_entry(el,
1255 &ar->ar_numaddr_ranges,
1258 list_for_each_entry(re, &el->el_exprs,
1261 if (re->re_stride > 1 ||
1262 (last_diff > 0 && last_hi != 255) ||
1263 (last_diff > 0 && last_hi == 255 &&
1266 last_hi = re->re_hi;
1267 last_diff = re->re_hi - re->re_lo;
1277 * Takes a linked list of nidrange expressions, determines the minimum
1278 * and maximum nid and creates appropriate nid structures
1284 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1285 char *max_nid, size_t nidstr_length)
1287 struct nidrange *nr;
1288 struct netstrfns *nf = NULL;
1292 char *lndname = NULL;
1293 char min_addr_str[IPSTRING_LENGTH];
1294 char max_addr_str[IPSTRING_LENGTH];
1296 list_for_each_entry(nr, nidlist, nr_link) {
1297 nf = nr->nr_netstrfns;
1298 lndname = nf->nf_name;
1300 netnum = nr->nr_netnum;
1302 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1304 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1305 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1307 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1309 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1314 * Determines the min and max NID values for num LNDs
1320 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1323 struct nidrange *nr;
1324 struct addrrange *ar;
1325 unsigned int tmp_min_addr = 0;
1326 unsigned int tmp_max_addr = 0;
1327 unsigned int min_addr = 0;
1328 unsigned int max_addr = 0;
1330 list_for_each_entry(nr, nidlist, nr_link) {
1331 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1332 cfs_num_ar_min_max(ar, &tmp_min_addr,
1334 if (tmp_min_addr < min_addr || min_addr == 0)
1335 min_addr = tmp_min_addr;
1336 if (tmp_max_addr > max_addr)
1337 max_addr = tmp_min_addr;
1340 *max_nid = max_addr;
1341 *min_nid = min_addr;
1345 * Takes an nidlist and determines the minimum and maximum
1352 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1355 struct nidrange *nr;
1356 struct addrrange *ar;
1357 __u32 tmp_min_ip_addr = 0;
1358 __u32 tmp_max_ip_addr = 0;
1359 __u32 min_ip_addr = 0;
1360 __u32 max_ip_addr = 0;
1362 list_for_each_entry(nr, nidlist, nr_link) {
1363 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1364 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1366 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1367 min_ip_addr = tmp_min_ip_addr;
1368 if (tmp_max_ip_addr > max_ip_addr)
1369 max_ip_addr = tmp_max_ip_addr;
1373 if (min_nid != NULL)
1374 *min_nid = min_ip_addr;
1375 if (max_nid != NULL)
1376 *max_nid = max_ip_addr;