1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * libcfs/libcfs/nidstrings.c
40 * Author: Phil Schwan <phil@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_LNET
49 #include <libcfs/libcfs.h>
50 #include <lnet/lnet.h>
52 #ifdef HAVE_GETHOSTBYNAME
57 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
58 * consistent in all conversion functions. Some code fragments are copied
59 * around for the sake of clarity...
62 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
63 * Choose the number of nidstrings to support the MAXIMUM expected number of
64 * concurrent users. If there are more, the returned string will be volatile.
65 * NB this number must allow for a process to be descheduled for a timeslice
66 * between getting its string and using it.
69 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
70 static int libcfs_nidstring_idx = 0;
73 static cfs_spinlock_t libcfs_nidstring_lock;
75 void libcfs_init_nidstrings (void)
77 cfs_spin_lock_init(&libcfs_nidstring_lock);
80 # define NIDSTR_LOCK(f) cfs_spin_lock_irqsave(&libcfs_nidstring_lock, f)
81 # define NIDSTR_UNLOCK(f) cfs_spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
83 # define NIDSTR_LOCK(f) (f=sizeof(f)) /* avoid set-but-unused warnings */
84 # define NIDSTR_UNLOCK(f) (f=sizeof(f))
88 libcfs_next_nidstring (void)
95 str = libcfs_nidstrings[libcfs_nidstring_idx++];
96 if (libcfs_nidstring_idx ==
97 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
98 libcfs_nidstring_idx = 0;
100 NIDSTR_UNLOCK(flags);
104 static int libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
105 static void libcfs_ip_addr2str(__u32 addr, char *str);
106 static int libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
107 static void libcfs_decnum_addr2str(__u32 addr, char *str);
108 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
109 static int libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
110 static int libcfs_ip_parse(char *str, int len, cfs_list_t *list);
111 static int libcfs_num_parse(char *str, int len, cfs_list_t *list);
112 static int libcfs_ip_match(__u32 addr, cfs_list_t *list);
113 static int libcfs_num_match(__u32 addr, cfs_list_t *list);
119 void (*nf_addr2str)(__u32 addr, char *str);
120 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
121 int (*nf_parse_addrlist)(char *str, int len,
123 int (*nf_match_addr)(__u32 addr, cfs_list_t *list);
126 static struct netstrfns libcfs_netstrfns[] = {
127 {/* .nf_type */ LOLND,
129 /* .nf_modname */ "klolnd",
130 /* .nf_addr2str */ libcfs_decnum_addr2str,
131 /* .nf_str2addr */ libcfs_lo_str2addr,
132 /* .nf_parse_addr*/ libcfs_num_parse,
133 /* .nf_match_addr*/ libcfs_num_match},
134 {/* .nf_type */ SOCKLND,
135 /* .nf_name */ "tcp",
136 /* .nf_modname */ "ksocklnd",
137 /* .nf_addr2str */ libcfs_ip_addr2str,
138 /* .nf_str2addr */ libcfs_ip_str2addr,
139 /* .nf_parse_addrlist*/ libcfs_ip_parse,
140 /* .nf_match_addr*/ libcfs_ip_match},
141 {/* .nf_type */ O2IBLND,
142 /* .nf_name */ "o2ib",
143 /* .nf_modname */ "ko2iblnd",
144 /* .nf_addr2str */ libcfs_ip_addr2str,
145 /* .nf_str2addr */ libcfs_ip_str2addr,
146 /* .nf_parse_addrlist*/ libcfs_ip_parse,
147 /* .nf_match_addr*/ libcfs_ip_match},
148 {/* .nf_type */ CIBLND,
149 /* .nf_name */ "cib",
150 /* .nf_modname */ "kciblnd",
151 /* .nf_addr2str */ libcfs_ip_addr2str,
152 /* .nf_str2addr */ libcfs_ip_str2addr,
153 /* .nf_parse_addrlist*/ libcfs_ip_parse,
154 /* .nf_match_addr*/ libcfs_ip_match},
155 {/* .nf_type */ OPENIBLND,
156 /* .nf_name */ "openib",
157 /* .nf_modname */ "kopeniblnd",
158 /* .nf_addr2str */ libcfs_ip_addr2str,
159 /* .nf_str2addr */ libcfs_ip_str2addr,
160 /* .nf_parse_addrlist*/ libcfs_ip_parse,
161 /* .nf_match_addr*/ libcfs_ip_match},
162 {/* .nf_type */ IIBLND,
163 /* .nf_name */ "iib",
164 /* .nf_modname */ "kiiblnd",
165 /* .nf_addr2str */ libcfs_ip_addr2str,
166 /* .nf_str2addr */ libcfs_ip_str2addr,
167 /* .nf_parse_addrlist*/ libcfs_ip_parse,
168 /* .nf_match_addr*/ libcfs_ip_match},
169 {/* .nf_type */ VIBLND,
170 /* .nf_name */ "vib",
171 /* .nf_modname */ "kviblnd",
172 /* .nf_addr2str */ libcfs_ip_addr2str,
173 /* .nf_str2addr */ libcfs_ip_str2addr,
174 /* .nf_parse_addrlist*/ libcfs_ip_parse,
175 /* .nf_match_addr*/ libcfs_ip_match},
176 {/* .nf_type */ RALND,
178 /* .nf_modname */ "kralnd",
179 /* .nf_addr2str */ libcfs_ip_addr2str,
180 /* .nf_str2addr */ libcfs_ip_str2addr,
181 /* .nf_parse_addrlist*/ libcfs_ip_parse,
182 /* .nf_match_addr*/ libcfs_ip_match},
183 {/* .nf_type */ QSWLND,
184 /* .nf_name */ "elan",
185 /* .nf_modname */ "kqswlnd",
186 /* .nf_addr2str */ libcfs_decnum_addr2str,
187 /* .nf_str2addr */ libcfs_num_str2addr,
188 /* .nf_parse_addrlist*/ libcfs_num_parse,
189 /* .nf_match_addr*/ libcfs_num_match},
190 {/* .nf_type */ GMLND,
192 /* .nf_modname */ "kgmlnd",
193 /* .nf_addr2str */ libcfs_hexnum_addr2str,
194 /* .nf_str2addr */ libcfs_num_str2addr,
195 /* .nf_parse_addrlist*/ libcfs_num_parse,
196 /* .nf_match_addr*/ libcfs_num_match},
197 {/* .nf_type */ MXLND,
199 /* .nf_modname */ "kmxlnd",
200 /* .nf_addr2str */ libcfs_ip_addr2str,
201 /* .nf_str2addr */ libcfs_ip_str2addr,
202 /* .nf_parse_addrlist*/ libcfs_ip_parse,
203 /* .nf_match_addr*/ libcfs_ip_match},
204 {/* .nf_type */ PTLLND,
205 /* .nf_name */ "ptl",
206 /* .nf_modname */ "kptllnd",
207 /* .nf_addr2str */ libcfs_decnum_addr2str,
208 /* .nf_str2addr */ libcfs_num_str2addr,
209 /* .nf_parse_addrlist*/ libcfs_num_parse,
210 /* .nf_match_addr*/ libcfs_num_match},
211 {/* .nf_type */ GNILND,
212 /* .nf_name */ "gni",
213 /* .nf_modname */ "kgnilnd",
214 /* .nf_addr2str */ libcfs_decnum_addr2str,
215 /* .nf_str2addr */ libcfs_num_str2addr,
216 /* .nf_parse_addrlist*/ libcfs_num_parse,
217 /* .nf_match_addr*/ libcfs_num_match},
218 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
222 const int libcfs_nnetstrfns = sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
225 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
232 libcfs_ip_addr2str(__u32 addr, char *str)
234 #if 0 /* never lookup */
235 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
236 __u32 netip = htonl(addr);
237 struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
240 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
245 snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
246 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
247 (addr >> 8) & 0xff, addr & 0xff);
250 /* CAVEAT EMPTOR XscanfX
251 * I use "%n" at the end of a sscanf format to detect trailing junk. However
252 * sscanf may return immediately if it sees the terminating '0' in a string, so
253 * I initialise the %n variable to the expected length. If sscanf sets it;
254 * fine, if it doesn't, then the scan ended at the end of the string, which is
258 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
264 int n = nob; /* XscanfX */
267 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
269 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
270 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
271 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
275 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
276 /* known hostname? */
277 if (('a' <= str[0] && str[0] <= 'z') ||
278 ('A' <= str[0] && str[0] <= 'Z')) {
281 LIBCFS_ALLOC(tmp, nob + 1);
285 memcpy(tmp, str, nob);
288 he = gethostbyname(tmp);
290 LIBCFS_FREE(tmp, nob);
293 __u32 ip = *(__u32 *)he->h_addr;
305 libcfs_decnum_addr2str(__u32 addr, char *str)
307 snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
311 libcfs_hexnum_addr2str(__u32 addr, char *str)
313 snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
317 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
322 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
326 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
330 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
337 libcfs_lnd2netstrfns(int lnd)
342 for (i = 0; i < libcfs_nnetstrfns; i++)
343 if (lnd == libcfs_netstrfns[i].nf_type)
344 return &libcfs_netstrfns[i];
350 libcfs_namenum2netstrfns(const char *name)
352 struct netstrfns *nf;
355 for (i = 0; i < libcfs_nnetstrfns; i++) {
356 nf = &libcfs_netstrfns[i];
357 if (nf->nf_type >= 0 &&
358 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
365 libcfs_name2netstrfns(const char *name)
369 for (i = 0; i < libcfs_nnetstrfns; i++)
370 if (libcfs_netstrfns[i].nf_type >= 0 &&
371 !strcmp(libcfs_netstrfns[i].nf_name, name))
372 return &libcfs_netstrfns[i];
378 libcfs_isknown_lnd(int type)
380 return libcfs_lnd2netstrfns(type) != NULL;
384 libcfs_lnd2modname(int lnd)
386 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
388 return (nf == NULL) ? NULL : nf->nf_modname;
392 libcfs_lnd2str(int lnd)
395 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
400 str = libcfs_next_nidstring();
401 snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
406 libcfs_str2lnd(const char *str)
408 struct netstrfns *nf = libcfs_name2netstrfns(str);
417 libcfs_net2str(__u32 net)
419 int lnd = LNET_NETTYP(net);
420 int num = LNET_NETNUM(net);
421 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
422 char *str = libcfs_next_nidstring();
425 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
427 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
429 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
435 libcfs_nid2str(lnet_nid_t nid)
437 __u32 addr = LNET_NIDADDR(nid);
438 __u32 net = LNET_NIDNET(nid);
439 int lnd = LNET_NETTYP(net);
440 int nnum = LNET_NETNUM(net);
441 struct netstrfns *nf;
445 if (nid == LNET_NID_ANY)
448 nf = libcfs_lnd2netstrfns(lnd);
449 str = libcfs_next_nidstring();
452 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
454 nf->nf_addr2str(addr, str);
457 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
460 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
467 static struct netstrfns *
468 libcfs_str2net_internal(const char *str, __u32 *net)
470 struct netstrfns *nf;
475 for (i = 0; i < libcfs_nnetstrfns; i++) {
476 nf = &libcfs_netstrfns[i];
477 if (nf->nf_type >= 0 &&
478 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
482 if (i == libcfs_nnetstrfns)
485 nob = strlen(nf->nf_name);
487 if (strlen(str) == (unsigned int)nob) {
490 if (nf->nf_type == LOLND) /* net number not allowed */
495 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
496 i != (int)strlen(str))
500 *net = LNET_MKNET(nf->nf_type, netnum);
505 libcfs_str2net(const char *str)
509 if (libcfs_str2net_internal(str, &net) != NULL)
512 return LNET_NIDNET(LNET_NID_ANY);
516 libcfs_str2nid(const char *str)
518 const char *sep = strchr(str, '@');
519 struct netstrfns *nf;
524 nf = libcfs_str2net_internal(sep + 1, &net);
528 sep = str + strlen(str);
529 net = LNET_MKNET(SOCKLND, 0);
530 nf = libcfs_lnd2netstrfns(SOCKLND);
531 LASSERT (nf != NULL);
534 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
537 return LNET_MKNID(net, addr);
541 libcfs_id2str(lnet_process_id_t id)
543 char *str = libcfs_next_nidstring();
545 if (id.pid == LNET_PID_ANY) {
546 snprintf(str, LNET_NIDSTR_SIZE,
547 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
551 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
552 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
553 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
558 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
560 if (!strcmp(str, "*")) {
561 *nidp = LNET_NID_ANY;
565 *nidp = libcfs_str2nid(str);
566 return *nidp != LNET_NID_ANY;
570 * Nid range list syntax.
573 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
574 * <nidrange> :== <addrrange> '@' <net>
575 * <addrrange> :== '*' |
578 * <ipaddr_range> :== <numaddr_range>.<numaddr_range>.<numaddr_range>.
580 * <numaddr_range> :== <number> |
582 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
583 * <range_expr> :== <number> |
584 * <number> '-' <number> |
585 * <number> '-' <number> '/' <number>
586 * <net> :== <netname> | <netname><number>
587 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
588 * "vib" | "ra" | "elan" | "mx" | "ptl"
593 * Structure to represent NULL-less strings.
601 * Structure to represent \<nidrange\> token of the syntax.
603 * One of this is created for each \<net\> parsed.
607 * Link to list of this structures which is built on nid range
612 * List head for addrrange::ar_link.
614 cfs_list_t nr_addrranges;
616 * Flag indicating that *@<net> is found.
620 * Pointer to corresponding element of libcfs_netstrfns.
622 struct netstrfns *nr_netstrfns;
624 * Number of network. E.g. 5 if \<net\> is "elan5".
630 * Structure to represent \<addrrange\> token of the syntax.
634 * Link to nidrange::nr_addrranges.
638 * List head for numaddr_range::nar_link.
640 cfs_list_t ar_numaddr_ranges;
644 * Structure to represent \<numaddr_range\> token of the syntax.
646 struct numaddr_range {
648 * Link to addrrange::ar_numaddr_ranges.
652 * List head for range_expr::re_link.
654 cfs_list_t nar_range_exprs;
658 * Structure to represent \<range_expr\> token of the syntax.
662 * Link to numaddr_range::nar_range_exprs.
686 * Extracts tokens from strings.
688 * Looks for \a delim in string \a next, sets \a res to point to
689 * substring before the delimiter, sets \a next right after the found
692 * \retval 1 if \a res points to a string of non-whitespace characters
693 * \retval 0 otherwise
696 gettok(struct lstr *next, char delim, struct lstr *res)
700 if (next->ls_str == NULL)
703 /* skip leading white spaces */
704 while (next->ls_len) {
705 if (!cfs_iswhite(*next->ls_str))
710 if (next->ls_len == 0)
711 /* whitespaces only */
714 if (*next->ls_str == delim)
715 /* first non-writespace is the delimiter */
718 res->ls_str = next->ls_str;
719 end = memchr(next->ls_str, delim, next->ls_len);
721 /* there is no the delimeter in the string */
722 end = next->ls_str + next->ls_len;
725 next->ls_str = end + 1;
726 next->ls_len -= (end - res->ls_str + 1);
729 /* skip ending whitespaces */
730 while (--end != res->ls_str)
731 if (!cfs_iswhite(*end))
734 res->ls_len = end - res->ls_str + 1;
739 * Converts string to integer.
741 * Accepts decimal and hexadecimal number recordings.
743 * \retval 1 if first \a nob chars of \a str convert to decimal or
744 * hexadecimal integer in the range [\a min, \a max]
745 * \retval 0 otherwise
748 libcfs_str2num_check(const char *str, int nob, unsigned *num,
749 unsigned min, unsigned max)
755 if (sscanf(str, "%u%n", num, &n) != 1 || n != nob)
756 if (sscanf(str, "0x%x%n", num, &n) != 1 || n != nob)
757 if (sscanf(str, "0X%x%n", num, &n) != 1 || n != nob)
759 sprintf(nstr, "%u", *num);
760 if (n != strlen(nstr) || memcmp(nstr, str, n)) {
761 sprintf(nstr, "0x%x", *num);
762 if (n != strlen(nstr) || memcmp(nstr, str, n)) {
763 sprintf(nstr, "0X%x", *num);
764 if (n != strlen(nstr) || memcmp(nstr, str, n))
768 if (*num < min || *num > max)
774 * Parses \<range_expr\> token of the syntax.
776 * \retval pointer to allocated range_expr and initialized
777 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
780 * \<number\> '-' \<number\> |
781 * \<number\> '-' \<number\> '/' \<number\>
782 * \retval NULL othersize
784 static struct range_expr *
785 parse_range_expr(struct lstr *src, unsigned min, unsigned max)
788 struct range_expr *expr;
790 LIBCFS_ALLOC(expr, sizeof(struct range_expr));
794 if (libcfs_str2num_check(src->ls_str, src->ls_len, &expr->re_lo,
796 /* <number> is parsed */
797 expr->re_hi = expr->re_lo;
802 if (!gettok(src, '-', &tok))
804 if (!libcfs_str2num_check(tok.ls_str, tok.ls_len, &expr->re_lo,
808 if (libcfs_str2num_check(src->ls_str, src->ls_len, &expr->re_hi,
810 /* <number> - <number> is parsed */
815 /* go to check <number> '-' <number> '/' <number> */
816 if (gettok(src, '/', &tok)) {
817 if (!libcfs_str2num_check(tok.ls_str, tok.ls_len,
818 &expr->re_hi, min, max))
820 /* <number> - <number> / ... */
821 if (libcfs_str2num_check(src->ls_str, src->ls_len,
822 &expr->re_stride, min, max))
823 /* <number> - <number> / <number> is parsed */
828 LIBCFS_FREE(expr, sizeof(struct range_expr));
833 * Parses \<expr_list\> token of the syntax.
835 * \retval 1 if \a str parses to '[' \<range_expr\> [ ',' \<range_expr\>] ']'
836 * \retval 0 otherwise
839 parse_expr_list(struct lstr *str, cfs_list_t *list,
840 unsigned min, unsigned max)
843 struct range_expr *range;
845 if (str->ls_str[0] != '[' || str->ls_str[str->ls_len - 1] != ']')
850 while (str->ls_str) {
851 if (gettok(str, ',', &res) == 0)
853 range = parse_range_expr(&res, min, max);
856 cfs_list_add_tail(&range->re_link, list);
862 * Parses \<numaddr_range\> token of the syntax.
864 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
865 * \retval 0 otherwise
868 num_parse(char *str, int len,
869 cfs_list_t *list, unsigned min, unsigned max)
873 struct numaddr_range *numaddr;
878 LIBCFS_ALLOC(numaddr, sizeof(struct numaddr_range));
881 cfs_list_add_tail(&numaddr->nar_link, list);
882 CFS_INIT_LIST_HEAD(&numaddr->nar_range_exprs);
884 if (libcfs_str2num_check(src.ls_str, src.ls_len, &num, min, max)) {
886 struct range_expr *expr;
888 LIBCFS_ALLOC(expr, sizeof(struct range_expr));
892 expr->re_lo = expr->re_hi = num;
894 cfs_list_add_tail(&expr->re_link, &numaddr->nar_range_exprs);
898 return parse_expr_list(&src, &numaddr->nar_range_exprs, min, max);
902 * Nf_parse_addrlist method for networks using numeric addresses.
904 * Examples of such networks are gm and elan.
906 * \retval 1 if \a str parsed to numeric address
907 * \retval 0 otherwise
910 libcfs_num_parse(char *str, int len, cfs_list_t *list)
912 return num_parse(str, len, list, 0, MAX_NUMERIC_VALUE);
916 * Nf_parse_addrlist method for networks using ip addresses.
918 * Examples of such networks are tcp and o2ib.
920 * \retval 1 if \a str parsed to ip address
921 * \retval 0 otherwise
924 libcfs_ip_parse(char *str, int len,
927 struct lstr src, res;
934 if (gettok(&src, '.', &res) == 0)
936 if (!num_parse(res.ls_str, res.ls_len, list, 0, 255))
941 return (i == 4) ? 1 : 0;
945 * Parses \<addrrange\> token on the syntax.
947 * Allocates struct addrrange and links to \a nidrange via
948 * (nidrange::nr_addrranges)
950 * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<numaddr_range\>
951 * \retval 0 otherwise
954 parse_addrange(const struct lstr *src, struct nidrange *nidrange)
956 struct addrrange *addrrange;
958 if (src->ls_len == 1 && src->ls_str[0] == '*') {
959 nidrange->nr_all = 1;
963 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
964 if (addrrange == NULL)
966 cfs_list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
967 CFS_INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
969 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
971 &addrrange->ar_numaddr_ranges);
975 * Finds or creates struct nidrange.
977 * Checks if \a src is a valid network name, looks for corresponding
978 * nidrange on the ist of nidranges (\a nidlist), creates new struct
979 * nidrange if it is not found.
981 * \retval pointer to struct nidrange matching network specified via \a src
982 * \retval NULL if \a src does not match any network
984 static struct nidrange *
985 add_nidrange(const struct lstr *src,
988 struct netstrfns *nf;
993 if (src->ls_len >= LNET_NIDSTR_SIZE)
996 nf = libcfs_namenum2netstrfns(src->ls_str);
999 endlen = src->ls_len - strlen(nf->nf_name);
1001 /* network name only, e.g. "elan" or "tcp" */
1004 /* e.g. "elan25" or "tcp23", refuse to parse if
1005 * network name is not appended with decimal or
1006 * hexadecimal number */
1007 if (!libcfs_str2num_check(src->ls_str + strlen(nf->nf_name),
1009 0, MAX_NUMERIC_VALUE))
1013 cfs_list_for_each_entry(nr, nidlist, nr_link) {
1014 if (nr->nr_netstrfns != nf)
1016 if (nr->nr_netnum != netnum)
1021 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
1024 cfs_list_add_tail(&nr->nr_link, nidlist);
1025 CFS_INIT_LIST_HEAD(&nr->nr_addrranges);
1026 nr->nr_netstrfns = nf;
1028 nr->nr_netnum = netnum;
1034 * Parses \<nidrange\> token of the syntax.
1036 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
1037 * \retval 0 otherwise
1040 parse_nidrange(struct lstr *src, cfs_list_t *nidlist)
1042 struct lstr addrrange, net, tmp;
1043 struct nidrange *nr;
1046 if (gettok(src, '@', &addrrange) == 0)
1049 if (gettok(src, '@', &net) == 0 || src->ls_str != NULL)
1052 nr = add_nidrange(&net, nidlist);
1056 if (!parse_addrange(&addrrange, nr))
1061 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
1066 * Frees range_expr structures of \a list.
1071 free_range_exprs(cfs_list_t *list)
1073 cfs_list_t *pos, *next;
1075 cfs_list_for_each_safe(pos, next, list) {
1077 LIBCFS_FREE(cfs_list_entry(pos, struct range_expr, re_link),
1078 sizeof(struct range_expr));
1083 * Frees numaddr_range structures of \a list.
1085 * For each struct numaddr_range structure found on \a list it frees
1086 * range_expr list attached to it and frees the numddr_range itself.
1091 free_numaddr_ranges(cfs_list_t *list)
1093 cfs_list_t *pos, *next;
1094 struct numaddr_range *numaddr;
1096 cfs_list_for_each_safe(pos, next, list) {
1097 numaddr = cfs_list_entry(pos, struct numaddr_range, nar_link);
1098 free_range_exprs(&numaddr->nar_range_exprs);
1100 LIBCFS_FREE(numaddr, sizeof(struct numaddr_range));
1105 * Frees addrrange structures of \a list.
1107 * For each struct addrrange structure found on \a list it frees
1108 * numaddr_range list attached to it and frees the addrrange itself.
1113 free_addrranges(cfs_list_t *list)
1115 cfs_list_t *pos, *next;
1116 struct addrrange *ar;
1118 cfs_list_for_each_safe(pos, next, list) {
1119 ar = cfs_list_entry(pos, struct addrrange, ar_link);
1120 free_numaddr_ranges(&ar->ar_numaddr_ranges);
1122 LIBCFS_FREE(ar, sizeof(struct addrrange));
1127 * Frees nidrange strutures of \a list.
1129 * For each struct nidrange structure found on \a list it frees
1130 * addrrange list attached to it and frees the nidrange itself.
1135 cfs_free_nidlist(cfs_list_t *list)
1137 cfs_list_t *pos, *next;
1138 struct nidrange *nr;
1140 cfs_list_for_each_safe(pos, next, list) {
1141 nr = cfs_list_entry(pos, struct nidrange, nr_link);
1142 free_addrranges(&nr->nr_addrranges);
1144 LIBCFS_FREE(nr, sizeof(struct nidrange));
1149 * Parses nid range list.
1151 * Parses with rigorous syntax and overflow checking \a str into
1152 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
1153 * structures and links that structure to \a nidlist. The resulting
1154 * list can be used to match a NID againts set of NIDS defined by \a
1156 * \see cfs_match_nid
1158 * \retval 1 on success
1159 * \retval 0 otherwise
1162 cfs_parse_nidlist(char *str, int len, cfs_list_t *nidlist)
1164 struct lstr src, res;
1170 CFS_INIT_LIST_HEAD(nidlist);
1171 while (src.ls_str) {
1172 rc = gettok(&src, ' ', &res);
1174 cfs_free_nidlist(nidlist);
1177 rc = parse_nidrange(&res, nidlist);
1179 cfs_free_nidlist(nidlist);
1187 * Matches address (\a addr) against address set encoded in \a list.
1189 * \see libcfs_num_match(), libcfs_ip_match()
1191 * \retval 1 if \a addr matches
1192 * \retval 0 otherwise
1195 match_numaddr(__u32 addr, cfs_list_t *list, int shift, __u32 mask)
1197 struct numaddr_range *numaddr;
1198 struct range_expr *expr;
1202 cfs_list_for_each_entry(numaddr, list, nar_link) {
1203 ip = (addr >> shift) & mask;
1206 cfs_list_for_each_entry(expr, &numaddr->nar_range_exprs,
1208 if (ip >= expr->re_lo &&
1209 ip <= expr->re_hi &&
1210 ((ip - expr->re_lo) % expr->re_stride) == 0) {
1222 * Nf_match_addr method for networks using numeric addresses
1224 * \retval 1 on match
1225 * \retval 0 otherwise
1228 libcfs_num_match(__u32 addr, cfs_list_t *numaddr)
1230 return match_numaddr(addr, numaddr, 0, 0xffffffff);
1234 * Nf_match_addr method for networks using ip addresses
1236 * \retval 1 on match
1237 * \retval 0 otherwise
1240 libcfs_ip_match(__u32 addr, cfs_list_t *numaddr)
1242 return match_numaddr(addr, numaddr, 24, 0xff);
1246 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
1248 * \see cfs_parse_nidlist()
1250 * \retval 1 on match
1251 * \retval 0 otherwises
1253 int cfs_match_nid(lnet_nid_t nid, cfs_list_t *nidlist)
1255 struct nidrange *nr;
1256 struct addrrange *ar;
1259 cfs_list_for_each_entry(nr, nidlist, nr_link) {
1260 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
1262 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
1266 cfs_list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
1267 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
1268 &ar->ar_numaddr_ranges))
1276 EXPORT_SYMBOL(libcfs_isknown_lnd);
1277 EXPORT_SYMBOL(libcfs_lnd2modname);
1278 EXPORT_SYMBOL(libcfs_lnd2str);
1279 EXPORT_SYMBOL(libcfs_str2lnd);
1280 EXPORT_SYMBOL(libcfs_net2str);
1281 EXPORT_SYMBOL(libcfs_nid2str);
1282 EXPORT_SYMBOL(libcfs_str2net);
1283 EXPORT_SYMBOL(libcfs_str2nid);
1284 EXPORT_SYMBOL(libcfs_id2str);
1285 EXPORT_SYMBOL(libcfs_str2anynid);
1286 EXPORT_SYMBOL(cfs_iswhite);
1287 EXPORT_SYMBOL(cfs_free_nidlist);
1288 EXPORT_SYMBOL(cfs_parse_nidlist);
1289 EXPORT_SYMBOL(cfs_match_nid);