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[] = {
318 {/* .nf_type */ LOLND,
320 /* .nf_modname */ "klolnd",
321 /* .nf_addr2str */ libcfs_decnum_addr2str,
322 /* .nf_str2addr */ libcfs_lo_str2addr,
323 /* .nf_parse_addr*/ libcfs_num_parse,
324 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
325 /* .nf_match_addr*/ libcfs_num_match,
326 /* .nf_is_contiguous */ cfs_num_is_contiguous,
327 /* .nf_min_max */ cfs_num_min_max},
328 {/* .nf_type */ SOCKLND,
329 /* .nf_name */ "tcp",
330 /* .nf_modname */ "ksocklnd",
331 /* .nf_addr2str */ libcfs_ip_addr2str,
332 /* .nf_str2addr */ libcfs_ip_str2addr,
333 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
334 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
335 /* .nf_match_addr*/ cfs_ip_addr_match,
336 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
337 /* .nf_min_max */ cfs_ip_min_max},
338 {/* .nf_type */ O2IBLND,
339 /* .nf_name */ "o2ib",
340 /* .nf_modname */ "ko2iblnd",
341 /* .nf_addr2str */ libcfs_ip_addr2str,
342 /* .nf_str2addr */ libcfs_ip_str2addr,
343 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
344 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
345 /* .nf_match_addr*/ cfs_ip_addr_match,
346 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
347 /* .nf_min_max */ cfs_ip_min_max},
348 {/* .nf_type */ CIBLND,
349 /* .nf_name */ "cib",
350 /* .nf_modname */ "kciblnd",
351 /* .nf_addr2str */ libcfs_ip_addr2str,
352 /* .nf_str2addr */ libcfs_ip_str2addr,
353 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
354 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
355 /* .nf_match_addr*/ cfs_ip_addr_match,
356 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
357 /* .nf_min_max */ cfs_ip_min_max},
358 {/* .nf_type */ OPENIBLND,
359 /* .nf_name */ "openib",
360 /* .nf_modname */ "kopeniblnd",
361 /* .nf_addr2str */ libcfs_ip_addr2str,
362 /* .nf_str2addr */ libcfs_ip_str2addr,
363 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
364 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
365 /* .nf_match_addr*/ cfs_ip_addr_match,
366 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
367 /* .nf_min_max */ cfs_ip_min_max},
368 {/* .nf_type */ IIBLND,
369 /* .nf_name */ "iib",
370 /* .nf_modname */ "kiiblnd",
371 /* .nf_addr2str */ libcfs_ip_addr2str,
372 /* .nf_str2addr */ libcfs_ip_str2addr,
373 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
374 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
375 /* .nf_match_addr*/ cfs_ip_addr_match,
376 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
377 /* .nf_min_max */ cfs_ip_min_max},
378 {/* .nf_type */ VIBLND,
379 /* .nf_name */ "vib",
380 /* .nf_modname */ "kviblnd",
381 /* .nf_addr2str */ libcfs_ip_addr2str,
382 /* .nf_str2addr */ libcfs_ip_str2addr,
383 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
384 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
385 /* .nf_match_addr*/ cfs_ip_addr_match,
386 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
387 /* .nf_min_max */ cfs_ip_min_max},
388 {/* .nf_type */ RALND,
390 /* .nf_modname */ "kralnd",
391 /* .nf_addr2str */ libcfs_ip_addr2str,
392 /* .nf_str2addr */ libcfs_ip_str2addr,
393 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
394 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
395 /* .nf_match_addr*/ cfs_ip_addr_match,
396 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
397 /* .nf_min_max */ cfs_ip_min_max},
398 {/* .nf_type */ QSWLND,
399 /* .nf_name */ "elan",
400 /* .nf_modname */ "kqswlnd",
401 /* .nf_addr2str */ libcfs_decnum_addr2str,
402 /* .nf_str2addr */ libcfs_num_str2addr,
403 /* .nf_parse_addrlist*/ libcfs_num_parse,
404 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
405 /* .nf_match_addr*/ libcfs_num_match,
406 /* .nf_is_contiguous */ cfs_num_is_contiguous,
407 /* .nf_min_max */ cfs_num_min_max},
408 {/* .nf_type */ GMLND,
410 /* .nf_modname */ "kgmlnd",
411 /* .nf_addr2str */ libcfs_hexnum_addr2str,
412 /* .nf_str2addr */ libcfs_num_str2addr,
413 /* .nf_parse_addrlist*/ libcfs_num_parse,
414 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
415 /* .nf_match_addr*/ libcfs_num_match,
416 /* .nf_is_contiguous */ cfs_num_is_contiguous,
417 /* .nf_min_max */ cfs_num_min_max},
418 {/* .nf_type */ MXLND,
420 /* .nf_modname */ "kmxlnd",
421 /* .nf_addr2str */ libcfs_ip_addr2str,
422 /* .nf_str2addr */ libcfs_ip_str2addr,
423 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
424 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
425 /* .nf_match_addr*/ cfs_ip_addr_match,
426 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
427 /* .nf_min_max */ cfs_ip_min_max},
428 {/* .nf_type */ PTLLND,
429 /* .nf_name */ "ptl",
430 /* .nf_modname */ "kptllnd",
431 /* .nf_addr2str */ libcfs_decnum_addr2str,
432 /* .nf_str2addr */ libcfs_num_str2addr,
433 /* .nf_parse_addrlist*/ libcfs_num_parse,
434 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
435 /* .nf_match_addr*/ libcfs_num_match,
436 /* .nf_is_contiguous */ cfs_num_is_contiguous,
437 /* .nf_min_max */ cfs_num_min_max},
438 {/* .nf_type */ GNILND,
439 /* .nf_name */ "gni",
440 /* .nf_modname */ "kgnilnd",
441 /* .nf_addr2str */ libcfs_decnum_addr2str,
442 /* .nf_str2addr */ libcfs_num_str2addr,
443 /* .nf_parse_addrlist*/ libcfs_num_parse,
444 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
445 /* .nf_match_addr*/ libcfs_num_match,
446 /* .nf_is_contiguous */ cfs_num_is_contiguous,
447 /* .nf_min_max */ cfs_num_min_max},
448 {/* .nf_type */ GNIIPLND,
449 /* .nf_name */ "gip",
450 /* .nf_modname */ "kgnilnd",
451 /* .nf_addr2str */ libcfs_ip_addr2str,
452 /* .nf_str2addr */ libcfs_ip_str2addr,
453 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
454 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
455 /* .nf_match_addr*/ cfs_ip_addr_match,
456 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
457 /* .nf_min_max */ cfs_ip_min_max},
460 static const size_t libcfs_nnetstrfns =
461 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
463 static struct netstrfns *
464 libcfs_lnd2netstrfns(__u32 lnd)
468 for (i = 0; i < libcfs_nnetstrfns; i++)
469 if (lnd == libcfs_netstrfns[i].nf_type)
470 return &libcfs_netstrfns[i];
475 static struct netstrfns *
476 libcfs_namenum2netstrfns(const char *name)
478 struct netstrfns *nf;
481 for (i = 0; i < libcfs_nnetstrfns; i++) {
482 nf = &libcfs_netstrfns[i];
483 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
489 static struct netstrfns *
490 libcfs_name2netstrfns(const char *name)
494 for (i = 0; i < libcfs_nnetstrfns; i++)
495 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
496 return &libcfs_netstrfns[i];
502 libcfs_isknown_lnd(__u32 lnd)
504 return libcfs_lnd2netstrfns(lnd) != NULL;
508 libcfs_lnd2modname(__u32 lnd)
510 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
512 return (nf == NULL) ? NULL : nf->nf_modname;
516 libcfs_str2lnd(const char *str)
518 struct netstrfns *nf = libcfs_name2netstrfns(str);
527 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
529 struct netstrfns *nf;
531 nf = libcfs_lnd2netstrfns(lnd);
533 snprintf(buf, buf_size, "?%u?", lnd);
535 snprintf(buf, buf_size, "%s", nf->nf_name);
541 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
543 __u32 nnum = LNET_NETNUM(net);
544 __u32 lnd = LNET_NETTYP(net);
545 struct netstrfns *nf;
547 nf = libcfs_lnd2netstrfns(lnd);
549 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
551 snprintf(buf, buf_size, "%s", nf->nf_name);
553 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
559 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
561 __u32 addr = LNET_NIDADDR(nid);
562 __u32 net = LNET_NIDNET(nid);
563 __u32 nnum = LNET_NETNUM(net);
564 __u32 lnd = LNET_NETTYP(net);
565 struct netstrfns *nf;
567 if (nid == LNET_NID_ANY) {
568 strncpy(buf, "<?>", buf_size);
569 buf[buf_size - 1] = '\0';
573 nf = libcfs_lnd2netstrfns(lnd);
575 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
579 nf->nf_addr2str(addr, buf, buf_size);
580 addr_len = strlen(buf);
582 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
585 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
592 static struct netstrfns *
593 libcfs_str2net_internal(const char *str, __u32 *net)
595 struct netstrfns *nf = NULL;
600 for (i = 0; i < libcfs_nnetstrfns; i++) {
601 nf = &libcfs_netstrfns[i];
602 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
606 if (i == libcfs_nnetstrfns)
609 nob = strlen(nf->nf_name);
611 if (strlen(str) == (unsigned int)nob) {
614 if (nf->nf_type == LOLND) /* net number not allowed */
619 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
620 i != (int)strlen(str))
624 *net = LNET_MKNET(nf->nf_type, netnum);
629 libcfs_str2net(const char *str)
633 if (libcfs_str2net_internal(str, &net) != NULL)
636 return LNET_NIDNET(LNET_NID_ANY);
640 libcfs_str2nid(const char *str)
642 const char *sep = strchr(str, '@');
643 struct netstrfns *nf;
648 nf = libcfs_str2net_internal(sep + 1, &net);
652 sep = str + strlen(str);
653 net = LNET_MKNET(SOCKLND, 0);
654 nf = libcfs_lnd2netstrfns(SOCKLND);
658 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
661 return LNET_MKNID(net, addr);
665 libcfs_id2str(lnet_process_id_t id)
667 char *str = libcfs_next_nidstring();
669 if (id.pid == LNET_PID_ANY) {
670 snprintf(str, LNET_NIDSTR_SIZE,
671 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
675 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
676 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
677 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
682 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
684 if (!strcmp(str, "*")) {
685 *nidp = LNET_NID_ANY;
689 *nidp = libcfs_str2nid(str);
690 return *nidp != LNET_NID_ANY;
694 * Nid range list syntax.
697 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
698 * <nidrange> :== <addrrange> '@' <net>
699 * <addrrange> :== '*' |
702 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
704 * <cfs_expr_list> :== <number> |
706 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
707 * <range_expr> :== <number> |
708 * <number> '-' <number> |
709 * <number> '-' <number> '/' <number>
710 * <net> :== <netname> | <netname><number>
711 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
712 * "vib" | "ra" | "elan" | "mx" | "ptl"
717 * Structure to represent \<nidrange\> token of the syntax.
719 * One of this is created for each \<net\> parsed.
723 * Link to list of this structures which is built on nid range
726 struct list_head nr_link;
728 * List head for addrrange::ar_link.
730 struct list_head nr_addrranges;
732 * Flag indicating that *@<net> is found.
736 * Pointer to corresponding element of libcfs_netstrfns.
738 struct netstrfns *nr_netstrfns;
740 * Number of network. E.g. 5 if \<net\> is "elan5".
746 * Structure to represent \<addrrange\> token of the syntax.
750 * Link to nidrange::nr_addrranges.
752 struct list_head ar_link;
754 * List head for cfs_expr_list::el_list.
756 struct list_head ar_numaddr_ranges;
760 * Parses \<addrrange\> token on the syntax.
762 * Allocates struct addrrange and links to \a nidrange via
763 * (nidrange::nr_addrranges)
765 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
766 * \retval -errno otherwise
769 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
771 struct addrrange *addrrange;
773 if (src->ls_len == 1 && src->ls_str[0] == '*') {
774 nidrange->nr_all = 1;
778 addrrange = calloc(1, sizeof(struct addrrange));
779 if (addrrange == NULL)
781 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
782 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
784 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
786 &addrrange->ar_numaddr_ranges);
790 * Finds or creates struct nidrange.
792 * Checks if \a src is a valid network name, looks for corresponding
793 * nidrange on the ist of nidranges (\a nidlist), creates new struct
794 * nidrange if it is not found.
796 * \retval pointer to struct nidrange matching network specified via \a src
797 * \retval NULL if \a src does not match any network
799 static struct nidrange *
800 add_nidrange(const struct cfs_lstr *src,
801 struct list_head *nidlist)
803 struct netstrfns *nf;
808 if (src->ls_len >= LNET_NIDSTR_SIZE)
811 nf = libcfs_namenum2netstrfns(src->ls_str);
814 endlen = src->ls_len - strlen(nf->nf_name);
816 /* network name only, e.g. "elan" or "tcp" */
819 /* e.g. "elan25" or "tcp23", refuse to parse if
820 * network name is not appended with decimal or
821 * hexadecimal number */
822 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
823 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
827 list_for_each_entry(nr, nidlist, nr_link) {
828 if (nr->nr_netstrfns != nf)
830 if (nr->nr_netnum != netnum)
835 nr = calloc(1, sizeof(struct nidrange));
838 list_add_tail(&nr->nr_link, nidlist);
839 INIT_LIST_HEAD(&nr->nr_addrranges);
840 nr->nr_netstrfns = nf;
842 nr->nr_netnum = netnum;
848 * Parses \<nidrange\> token of the syntax.
850 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
851 * \retval 0 otherwise
854 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
856 struct cfs_lstr addrrange;
862 if (cfs_gettok(src, '@', &addrrange) == 0)
865 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
868 nr = add_nidrange(&net, nidlist);
872 if (parse_addrange(&addrrange, nr) != 0)
877 fprintf(stderr, "can't parse nidrange: \"%.*s\"\n",
878 tmp.ls_len, tmp.ls_str);
883 * Frees addrrange structures of \a list.
885 * For each struct addrrange structure found on \a list it frees
886 * cfs_expr_list list attached to it and frees the addrrange itself.
891 free_addrranges(struct list_head *list)
893 while (!list_empty(list)) {
894 struct addrrange *ar;
896 ar = list_entry(list->next, struct addrrange, ar_link);
898 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
899 list_del(&ar->ar_link);
905 * Frees nidrange strutures of \a list.
907 * For each struct nidrange structure found on \a list it frees
908 * addrrange list attached to it and frees the nidrange itself.
913 cfs_free_nidlist(struct list_head *list)
915 struct list_head *pos, *next;
918 list_for_each_safe(pos, next, list) {
919 nr = list_entry(pos, struct nidrange, nr_link);
920 free_addrranges(&nr->nr_addrranges);
927 * Parses nid range list.
929 * Parses with rigorous syntax and overflow checking \a str into
930 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
931 * structures and links that structure to \a nidlist. The resulting
932 * list can be used to match a NID againts set of NIDS defined by \a
936 * \retval 1 on success
937 * \retval 0 otherwise
940 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
948 INIT_LIST_HEAD(nidlist);
950 rc = cfs_gettok(&src, ' ', &res);
952 cfs_free_nidlist(nidlist);
955 rc = parse_nidrange(&res, nidlist);
957 cfs_free_nidlist(nidlist);
965 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
967 * \see cfs_parse_nidlist()
970 * \retval 0 otherwises
972 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
975 struct addrrange *ar;
977 list_for_each_entry(nr, nidlist, nr_link) {
978 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
980 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
984 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
985 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
986 &ar->ar_numaddr_ranges))
993 * Print the network part of the nidrange \a nr into the specified \a buffer.
995 * \retval number of characters written
998 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1000 struct netstrfns *nf = nr->nr_netstrfns;
1002 if (nr->nr_netnum == 0)
1003 return snprintf(buffer, count, "@%s", nf->nf_name);
1005 return snprintf(buffer, count, "@%s%u",
1006 nf->nf_name, nr->nr_netnum);
1011 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1012 * At max \a count characters can be printed into \a buffer.
1014 * \retval number of characters written
1017 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1018 struct nidrange *nr)
1021 struct addrrange *ar;
1022 struct netstrfns *nf = nr->nr_netstrfns;
1024 list_for_each_entry(ar, addrranges, ar_link) {
1026 i += snprintf(buffer + i, count - i, " ");
1027 i += nf->nf_print_addrlist(buffer + i, count - i,
1028 &ar->ar_numaddr_ranges);
1029 i += cfs_print_network(buffer + i, count - i, nr);
1035 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1036 * At max \a count characters can be printed into \a buffer.
1037 * Nidranges are separated by a space character.
1039 * \retval number of characters written
1041 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1044 struct nidrange *nr;
1049 list_for_each_entry(nr, nidlist, nr_link) {
1051 i += snprintf(buffer + i, count - i, " ");
1053 if (nr->nr_all != 0) {
1054 assert(list_empty(&nr->nr_addrranges));
1055 i += snprintf(buffer + i, count - i, "*");
1056 i += cfs_print_network(buffer + i, count - i, nr);
1058 i += cfs_print_addrranges(buffer + i, count - i,
1059 &nr->nr_addrranges, nr);
1066 * Determines minimum and maximum addresses for a single
1067 * numeric address range
1073 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1076 struct cfs_expr_list *el;
1077 struct cfs_range_expr *re;
1078 __u32 tmp_ip_addr = 0;
1079 unsigned int min_ip[4] = {0};
1080 unsigned int max_ip[4] = {0};
1083 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1084 list_for_each_entry(re, &el->el_exprs, re_link) {
1085 min_ip[re_count] = re->re_lo;
1086 max_ip[re_count] = re->re_hi;
1091 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1092 (min_ip[2] << 8) | min_ip[3]);
1094 if (min_nid != NULL)
1095 *min_nid = tmp_ip_addr;
1097 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1098 (max_ip[2] << 8) | max_ip[3]);
1100 if (max_nid != NULL)
1101 *max_nid = tmp_ip_addr;
1105 * Determines minimum and maximum addresses for a single
1106 * numeric address range
1112 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1115 struct cfs_expr_list *el;
1116 struct cfs_range_expr *re;
1117 unsigned int min_addr = 0;
1118 unsigned int max_addr = 0;
1120 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1121 list_for_each_entry(re, &el->el_exprs, re_link) {
1122 if (re->re_lo < min_addr || min_addr == 0)
1123 min_addr = re->re_lo;
1124 if (re->re_hi > max_addr)
1125 max_addr = re->re_hi;
1129 if (min_nid != NULL)
1130 *min_nid = min_addr;
1131 if (max_nid != NULL)
1132 *max_nid = max_addr;
1136 * Determines whether an expression list in an nidrange contains exactly
1137 * one contiguous address range. Calls the correct netstrfns for the LND
1141 * \retval true if contiguous
1142 * \retval false if not contiguous
1144 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1146 struct nidrange *nr;
1147 struct netstrfns *nf = NULL;
1148 char *lndname = NULL;
1151 list_for_each_entry(nr, nidlist, nr_link) {
1152 nf = nr->nr_netstrfns;
1153 if (lndname == NULL)
1154 lndname = nf->nf_name;
1156 netnum = nr->nr_netnum;
1158 if (strcmp(lndname, nf->nf_name) != 0 ||
1159 netnum != nr->nr_netnum)
1166 if (!nf->nf_is_contiguous(nidlist))
1173 * Determines whether an expression list in an num nidrange contains exactly
1174 * one contiguous address range.
1178 * \retval true if contiguous
1179 * \retval false if not contiguous
1181 static bool cfs_num_is_contiguous(struct list_head *nidlist)
1183 struct nidrange *nr;
1184 struct addrrange *ar;
1185 struct cfs_expr_list *el;
1186 struct cfs_range_expr *re;
1188 __u32 last_end_nid = 0;
1189 __u32 current_start_nid = 0;
1190 __u32 current_end_nid = 0;
1192 list_for_each_entry(nr, nidlist, nr_link) {
1193 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1194 cfs_num_ar_min_max(ar, ¤t_start_nid,
1196 if (last_end_nid != 0 &&
1197 (current_start_nid - last_end_nid != 1))
1199 last_end_nid = current_end_nid;
1200 list_for_each_entry(el, &ar->ar_numaddr_ranges,
1202 list_for_each_entry(re, &el->el_exprs,
1204 if (re->re_stride > 1)
1206 else if (last_hi != 0 &&
1207 re->re_hi - last_hi != 1)
1209 last_hi = re->re_hi;
1219 * Determines whether an expression list in an ip nidrange contains exactly
1220 * one contiguous address range.
1224 * \retval true if contiguous
1225 * \retval false if not contiguous
1227 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
1229 struct nidrange *nr;
1230 struct addrrange *ar;
1231 struct cfs_expr_list *el;
1232 struct cfs_range_expr *re;
1236 __u32 last_end_nid = 0;
1237 __u32 current_start_nid = 0;
1238 __u32 current_end_nid = 0;
1240 list_for_each_entry(nr, nidlist, nr_link) {
1241 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1244 cfs_ip_ar_min_max(ar, ¤t_start_nid,
1246 if (last_end_nid != 0 &&
1247 (current_start_nid - last_end_nid != 1))
1249 last_end_nid = current_end_nid;
1250 list_for_each_entry(el,
1251 &ar->ar_numaddr_ranges,
1254 list_for_each_entry(re, &el->el_exprs,
1257 if (re->re_stride > 1 ||
1258 (last_diff > 0 && last_hi != 255) ||
1259 (last_diff > 0 && last_hi == 255 &&
1262 last_hi = re->re_hi;
1263 last_diff = re->re_hi - re->re_lo;
1273 * Takes a linked list of nidrange expressions, determines the minimum
1274 * and maximum nid and creates appropriate nid structures
1280 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
1281 char *max_nid, size_t nidstr_length)
1283 struct nidrange *nr;
1284 struct netstrfns *nf = NULL;
1288 char *lndname = NULL;
1289 char min_addr_str[IPSTRING_LENGTH];
1290 char max_addr_str[IPSTRING_LENGTH];
1292 list_for_each_entry(nr, nidlist, nr_link) {
1293 nf = nr->nr_netstrfns;
1294 lndname = nf->nf_name;
1296 netnum = nr->nr_netnum;
1298 nf->nf_min_max(nidlist, &min_addr, &max_addr);
1300 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
1301 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
1303 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
1305 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
1310 * Determines the min and max NID values for num LNDs
1316 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1319 struct nidrange *nr;
1320 struct addrrange *ar;
1321 unsigned int tmp_min_addr = 0;
1322 unsigned int tmp_max_addr = 0;
1323 unsigned int min_addr = 0;
1324 unsigned int max_addr = 0;
1326 list_for_each_entry(nr, nidlist, nr_link) {
1327 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1328 cfs_num_ar_min_max(ar, &tmp_min_addr,
1330 if (tmp_min_addr < min_addr || min_addr == 0)
1331 min_addr = tmp_min_addr;
1332 if (tmp_max_addr > max_addr)
1333 max_addr = tmp_min_addr;
1336 *max_nid = max_addr;
1337 *min_nid = min_addr;
1341 * Takes an nidlist and determines the minimum and maximum
1348 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1351 struct nidrange *nr;
1352 struct addrrange *ar;
1353 __u32 tmp_min_ip_addr = 0;
1354 __u32 tmp_max_ip_addr = 0;
1355 __u32 min_ip_addr = 0;
1356 __u32 max_ip_addr = 0;
1358 list_for_each_entry(nr, nidlist, nr_link) {
1359 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1360 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1362 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1363 min_ip_addr = tmp_min_ip_addr;
1364 if (tmp_max_ip_addr > max_ip_addr)
1365 max_ip_addr = tmp_max_ip_addr;
1369 if (min_nid != NULL)
1370 *min_nid = min_ip_addr;
1371 if (max_nid != NULL)
1372 *max_nid = max_ip_addr;