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/nidstrings.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/libcfs.h>
44 #include <lnet/nidstr.h>
46 #ifdef HAVE_GETHOSTBYNAME
51 /* max value for numeric network address */
52 #define MAX_NUMERIC_VALUE 0xffffffff
54 #define IPSTRING_LENGTH 16
56 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
57 * consistent in all conversion functions. Some code fragments are copied
58 * around for the sake of clarity...
61 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
62 * Choose the number of nidstrings to support the MAXIMUM expected number of
63 * concurrent users. If there are more, the returned string will be volatile.
64 * NB this number must allow for a process to be descheduled for a timeslice
65 * between getting its string and using it.
68 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
69 static int libcfs_nidstring_idx = 0;
72 static spinlock_t libcfs_nidstring_lock;
74 void libcfs_init_nidstrings (void)
76 spin_lock_init(&libcfs_nidstring_lock);
79 # define NIDSTR_LOCK(f) spin_lock_irqsave(&libcfs_nidstring_lock, f)
80 # define NIDSTR_UNLOCK(f) spin_unlock_irqrestore(&libcfs_nidstring_lock, f)
82 # define NIDSTR_LOCK(f) (f=sizeof(f)) /* avoid set-but-unused warnings */
83 # define NIDSTR_UNLOCK(f) (f=sizeof(f))
87 libcfs_next_nidstring (void)
94 str = libcfs_nidstrings[libcfs_nidstring_idx++];
95 if (libcfs_nidstring_idx ==
96 sizeof(libcfs_nidstrings)/sizeof(libcfs_nidstrings[0]))
97 libcfs_nidstring_idx = 0;
103 static int libcfs_lo_str2addr(const char *str, int nob, __u32 *addr);
104 static void libcfs_ip_addr2str(__u32 addr, char *str);
105 static int libcfs_ip_str2addr(const char *str, int nob, __u32 *addr);
106 static bool cfs_ip_is_contiguous(struct list_head *nidlist);
107 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
108 static void libcfs_decnum_addr2str(__u32 addr, char *str);
109 static void libcfs_hexnum_addr2str(__u32 addr, char *str);
110 static int libcfs_num_str2addr(const char *str, int nob, __u32 *addr);
111 static int libcfs_num_parse(char *str, int len, struct list_head *list);
112 static int libcfs_num_match(__u32 addr, struct list_head *list);
113 static int libcfs_num_addr_range_print(char *buffer, int count,
114 struct list_head *list);
115 static int libcfs_ip_addr_range_print(char *buffer, int count,
116 struct list_head *list);
117 static bool cfs_num_is_contiguous(struct list_head *nidlist);
118 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
124 void (*nf_addr2str)(__u32 addr, char *str);
125 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
126 int (*nf_parse_addrlist)(char *str, int len,
127 struct list_head *list);
128 int (*nf_print_addrlist)(char *buffer, int count,
129 struct list_head *list);
130 int (*nf_match_addr)(__u32 addr, struct list_head *list);
131 bool (*nf_is_contiguous)(struct list_head *nidlist);
132 void (*nf_min_max)(struct list_head *nidlist, __u32 *min_nid,
136 static struct netstrfns libcfs_netstrfns[] = {
137 {/* .nf_type */ LOLND,
139 /* .nf_modname */ "klolnd",
140 /* .nf_addr2str */ libcfs_decnum_addr2str,
141 /* .nf_str2addr */ libcfs_lo_str2addr,
142 /* .nf_parse_addr*/ libcfs_num_parse,
143 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
144 /* .nf_match_addr*/ libcfs_num_match,
145 /* .nf_is_contiguous */ cfs_num_is_contiguous,
146 /* .nf_min_max */ cfs_num_min_max},
147 {/* .nf_type */ SOCKLND,
148 /* .nf_name */ "tcp",
149 /* .nf_modname */ "ksocklnd",
150 /* .nf_addr2str */ libcfs_ip_addr2str,
151 /* .nf_str2addr */ libcfs_ip_str2addr,
152 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
153 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
154 /* .nf_match_addr*/ cfs_ip_addr_match,
155 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
156 /* .nf_min_max */ cfs_ip_min_max},
157 {/* .nf_type */ O2IBLND,
158 /* .nf_name */ "o2ib",
159 /* .nf_modname */ "ko2iblnd",
160 /* .nf_addr2str */ libcfs_ip_addr2str,
161 /* .nf_str2addr */ libcfs_ip_str2addr,
162 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
163 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
164 /* .nf_match_addr*/ cfs_ip_addr_match,
165 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
166 /* .nf_min_max */ cfs_ip_min_max},
167 {/* .nf_type */ CIBLND,
168 /* .nf_name */ "cib",
169 /* .nf_modname */ "kciblnd",
170 /* .nf_addr2str */ libcfs_ip_addr2str,
171 /* .nf_str2addr */ libcfs_ip_str2addr,
172 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
173 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
174 /* .nf_match_addr*/ cfs_ip_addr_match,
175 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
176 /* .nf_min_max */ cfs_ip_min_max},
177 {/* .nf_type */ OPENIBLND,
178 /* .nf_name */ "openib",
179 /* .nf_modname */ "kopeniblnd",
180 /* .nf_addr2str */ libcfs_ip_addr2str,
181 /* .nf_str2addr */ libcfs_ip_str2addr,
182 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
183 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
184 /* .nf_match_addr*/ cfs_ip_addr_match,
185 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
186 /* .nf_min_max */ cfs_ip_min_max},
187 {/* .nf_type */ IIBLND,
188 /* .nf_name */ "iib",
189 /* .nf_modname */ "kiiblnd",
190 /* .nf_addr2str */ libcfs_ip_addr2str,
191 /* .nf_str2addr */ libcfs_ip_str2addr,
192 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
193 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
194 /* .nf_match_addr*/ cfs_ip_addr_match,
195 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
196 /* .nf_min_max */ cfs_ip_min_max},
197 {/* .nf_type */ VIBLND,
198 /* .nf_name */ "vib",
199 /* .nf_modname */ "kviblnd",
200 /* .nf_addr2str */ libcfs_ip_addr2str,
201 /* .nf_str2addr */ libcfs_ip_str2addr,
202 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
203 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
204 /* .nf_match_addr*/ cfs_ip_addr_match,
205 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
206 /* .nf_min_max */ cfs_ip_min_max},
207 {/* .nf_type */ RALND,
209 /* .nf_modname */ "kralnd",
210 /* .nf_addr2str */ libcfs_ip_addr2str,
211 /* .nf_str2addr */ libcfs_ip_str2addr,
212 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
213 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
214 /* .nf_match_addr*/ cfs_ip_addr_match,
215 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
216 /* .nf_min_max */ cfs_ip_min_max},
217 {/* .nf_type */ QSWLND,
218 /* .nf_name */ "elan",
219 /* .nf_modname */ "kqswlnd",
220 /* .nf_addr2str */ libcfs_decnum_addr2str,
221 /* .nf_str2addr */ libcfs_num_str2addr,
222 /* .nf_parse_addrlist*/ libcfs_num_parse,
223 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
224 /* .nf_match_addr*/ libcfs_num_match,
225 /* .nf_is_contiguous */ cfs_num_is_contiguous,
226 /* .nf_min_max */ cfs_num_min_max},
227 {/* .nf_type */ GMLND,
229 /* .nf_modname */ "kgmlnd",
230 /* .nf_addr2str */ libcfs_hexnum_addr2str,
231 /* .nf_str2addr */ libcfs_num_str2addr,
232 /* .nf_parse_addrlist*/ libcfs_num_parse,
233 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
234 /* .nf_match_addr*/ libcfs_num_match,
235 /* .nf_is_contiguous */ cfs_num_is_contiguous,
236 /* .nf_min_max */ cfs_num_min_max},
237 {/* .nf_type */ MXLND,
239 /* .nf_modname */ "kmxlnd",
240 /* .nf_addr2str */ libcfs_ip_addr2str,
241 /* .nf_str2addr */ libcfs_ip_str2addr,
242 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
243 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
244 /* .nf_match_addr*/ cfs_ip_addr_match,
245 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
246 /* .nf_min_max */ cfs_ip_min_max},
247 {/* .nf_type */ PTLLND,
248 /* .nf_name */ "ptl",
249 /* .nf_modname */ "kptllnd",
250 /* .nf_addr2str */ libcfs_decnum_addr2str,
251 /* .nf_str2addr */ libcfs_num_str2addr,
252 /* .nf_parse_addrlist*/ libcfs_num_parse,
253 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
254 /* .nf_match_addr*/ libcfs_num_match,
255 /* .nf_is_contiguous */ cfs_num_is_contiguous,
256 /* .nf_min_max */ cfs_num_min_max},
257 {/* .nf_type */ GNILND,
258 /* .nf_name */ "gni",
259 /* .nf_modname */ "kgnilnd",
260 /* .nf_addr2str */ libcfs_decnum_addr2str,
261 /* .nf_str2addr */ libcfs_num_str2addr,
262 /* .nf_parse_addrlist*/ libcfs_num_parse,
263 /* .nf_print_addrlist*/ libcfs_num_addr_range_print,
264 /* .nf_match_addr*/ libcfs_num_match,
265 /* .nf_is_contiguous */ cfs_num_is_contiguous,
266 /* .nf_min_max */ cfs_num_min_max},
267 {/* .nf_type */ GNIIPLND,
268 /* .nf_name */ "gip",
269 /* .nf_modname */ "kgnilnd",
270 /* .nf_addr2str */ libcfs_ip_addr2str,
271 /* .nf_str2addr */ libcfs_ip_str2addr,
272 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
273 /* .nf_print_addrlist*/ libcfs_ip_addr_range_print,
274 /* .nf_match_addr*/ cfs_ip_addr_match,
275 /* .nf_is_contiguous */ cfs_ip_is_contiguous,
276 /* .nf_min_max */ cfs_ip_min_max},
277 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
281 static const int libcfs_nnetstrfns =
282 sizeof(libcfs_netstrfns)/sizeof(libcfs_netstrfns[0]);
285 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
292 libcfs_ip_addr2str(__u32 addr, char *str)
294 #if 0 /* never lookup */
295 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
296 __u32 netip = htonl(addr);
297 struct hostent *he = gethostbyaddr(&netip, sizeof(netip), AF_INET);
300 snprintf(str, LNET_NIDSTR_SIZE, "%s", he->h_name);
305 snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
306 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
307 (addr >> 8) & 0xff, addr & 0xff);
310 /* CAVEAT EMPTOR XscanfX
311 * I use "%n" at the end of a sscanf format to detect trailing junk. However
312 * sscanf may return immediately if it sees the terminating '0' in a string, so
313 * I initialise the %n variable to the expected length. If sscanf sets it;
314 * fine, if it doesn't, then the scan ended at the end of the string, which is
318 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
324 int n = nob; /* XscanfX */
327 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
329 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
330 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
331 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
335 #if !defined(__KERNEL__) && defined HAVE_GETHOSTBYNAME
336 /* known hostname? */
337 if (('a' <= str[0] && str[0] <= 'z') ||
338 ('A' <= str[0] && str[0] <= 'Z')) {
341 LIBCFS_ALLOC(tmp, nob + 1);
345 memcpy(tmp, str, nob);
348 he = gethostbyname(tmp);
350 LIBCFS_FREE(tmp, nob);
353 __u32 ip = *(__u32 *)he->h_addr;
365 libcfs_decnum_addr2str(__u32 addr, char *str)
367 snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
371 libcfs_hexnum_addr2str(__u32 addr, char *str)
373 snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
377 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
382 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
386 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
390 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
396 static struct netstrfns *
397 libcfs_lnd2netstrfns(int lnd)
402 for (i = 0; i < libcfs_nnetstrfns; i++)
403 if (lnd == libcfs_netstrfns[i].nf_type)
404 return &libcfs_netstrfns[i];
409 static struct netstrfns *
410 libcfs_namenum2netstrfns(const char *name)
412 struct netstrfns *nf;
415 for (i = 0; i < libcfs_nnetstrfns; i++) {
416 nf = &libcfs_netstrfns[i];
417 if (nf->nf_type >= 0 &&
418 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
424 static struct netstrfns *
425 libcfs_name2netstrfns(const char *name)
429 for (i = 0; i < libcfs_nnetstrfns; i++)
430 if (libcfs_netstrfns[i].nf_type >= 0 &&
431 !strcmp(libcfs_netstrfns[i].nf_name, name))
432 return &libcfs_netstrfns[i];
438 libcfs_isknown_lnd(int type)
440 return libcfs_lnd2netstrfns(type) != NULL;
444 libcfs_lnd2modname(int lnd)
446 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
448 return (nf == NULL) ? NULL : nf->nf_modname;
452 libcfs_lnd2str(int lnd)
455 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
460 str = libcfs_next_nidstring();
461 snprintf(str, LNET_NIDSTR_SIZE, "?%u?", lnd);
466 libcfs_str2lnd(const char *str)
468 struct netstrfns *nf = libcfs_name2netstrfns(str);
477 libcfs_net2str(__u32 net)
479 int lnd = LNET_NETTYP(net);
480 int num = LNET_NETNUM(net);
481 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
482 char *str = libcfs_next_nidstring();
485 snprintf(str, LNET_NIDSTR_SIZE, "<%u:%u>", lnd, num);
487 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
489 snprintf(str, LNET_NIDSTR_SIZE, "%s%u", nf->nf_name, num);
495 libcfs_nid2str(lnet_nid_t nid)
497 __u32 addr = LNET_NIDADDR(nid);
498 __u32 net = LNET_NIDNET(nid);
499 int lnd = LNET_NETTYP(net);
500 int nnum = LNET_NETNUM(net);
501 struct netstrfns *nf;
505 if (nid == LNET_NID_ANY)
508 nf = libcfs_lnd2netstrfns(lnd);
509 str = libcfs_next_nidstring();
512 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%u:%u>", addr, lnd, nnum);
514 nf->nf_addr2str(addr, str);
517 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
520 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%u",
527 static struct netstrfns *
528 libcfs_str2net_internal(const char *str, __u32 *net)
530 struct netstrfns *nf = NULL;
535 for (i = 0; i < libcfs_nnetstrfns; i++) {
536 nf = &libcfs_netstrfns[i];
537 if (nf->nf_type >= 0 &&
538 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
542 if (i == libcfs_nnetstrfns)
545 nob = strlen(nf->nf_name);
547 if (strlen(str) == (unsigned int)nob) {
550 if (nf->nf_type == LOLND) /* net number not allowed */
555 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
556 i != (int)strlen(str))
560 *net = LNET_MKNET(nf->nf_type, netnum);
565 libcfs_str2net(const char *str)
569 if (libcfs_str2net_internal(str, &net) != NULL)
572 return LNET_NIDNET(LNET_NID_ANY);
576 libcfs_str2nid(const char *str)
578 const char *sep = strchr(str, '@');
579 struct netstrfns *nf;
584 nf = libcfs_str2net_internal(sep + 1, &net);
588 sep = str + strlen(str);
589 net = LNET_MKNET(SOCKLND, 0);
590 nf = libcfs_lnd2netstrfns(SOCKLND);
591 LASSERT (nf != NULL);
594 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
597 return LNET_MKNID(net, addr);
601 libcfs_id2str(lnet_process_id_t id)
603 char *str = libcfs_next_nidstring();
605 if (id.pid == LNET_PID_ANY) {
606 snprintf(str, LNET_NIDSTR_SIZE,
607 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
611 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
612 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
613 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
618 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
620 if (!strcmp(str, "*")) {
621 *nidp = LNET_NID_ANY;
625 *nidp = libcfs_str2nid(str);
626 return *nidp != LNET_NID_ANY;
630 * Nid range list syntax.
633 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
634 * <nidrange> :== <addrrange> '@' <net>
635 * <addrrange> :== '*' |
638 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
640 * <cfs_expr_list> :== <number> |
642 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
643 * <range_expr> :== <number> |
644 * <number> '-' <number> |
645 * <number> '-' <number> '/' <number>
646 * <net> :== <netname> | <netname><number>
647 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
648 * "vib" | "ra" | "elan" | "mx" | "ptl"
653 * Structure to represent \<nidrange\> token of the syntax.
655 * One of this is created for each \<net\> parsed.
659 * Link to list of this structures which is built on nid range
662 struct list_head nr_link;
664 * List head for addrrange::ar_link.
666 struct list_head nr_addrranges;
668 * Flag indicating that *@<net> is found.
672 * Pointer to corresponding element of libcfs_netstrfns.
674 struct netstrfns *nr_netstrfns;
676 * Number of network. E.g. 5 if \<net\> is "elan5".
682 * Structure to represent \<addrrange\> token of the syntax.
686 * Link to nidrange::nr_addrranges.
688 struct list_head ar_link;
690 * List head for cfs_expr_list::el_list.
692 struct list_head ar_numaddr_ranges;
696 * Nf_parse_addrlist method for networks using numeric addresses.
698 * Examples of such networks are gm and elan.
700 * \retval 0 if \a str parsed to numeric address
701 * \retval errno otherwise
704 libcfs_num_parse(char *str, int len, struct list_head *list)
706 struct cfs_expr_list *el;
709 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
711 list_add_tail(&el->el_link, list);
717 * Parses \<addrrange\> token on the syntax.
719 * Allocates struct addrrange and links to \a nidrange via
720 * (nidrange::nr_addrranges)
722 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
723 * \retval -errno otherwise
726 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
728 struct addrrange *addrrange;
730 if (src->ls_len == 1 && src->ls_str[0] == '*') {
731 nidrange->nr_all = 1;
735 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
736 if (addrrange == NULL)
738 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
739 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
741 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
743 &addrrange->ar_numaddr_ranges);
747 * Finds or creates struct nidrange.
749 * Checks if \a src is a valid network name, looks for corresponding
750 * nidrange on the ist of nidranges (\a nidlist), creates new struct
751 * nidrange if it is not found.
753 * \retval pointer to struct nidrange matching network specified via \a src
754 * \retval NULL if \a src does not match any network
756 static struct nidrange *
757 add_nidrange(const struct cfs_lstr *src,
758 struct list_head *nidlist)
760 struct netstrfns *nf;
765 if (src->ls_len >= LNET_NIDSTR_SIZE)
768 nf = libcfs_namenum2netstrfns(src->ls_str);
771 endlen = src->ls_len - strlen(nf->nf_name);
773 /* network name only, e.g. "elan" or "tcp" */
776 /* e.g. "elan25" or "tcp23", refuse to parse if
777 * network name is not appended with decimal or
778 * hexadecimal number */
779 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
780 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
784 list_for_each_entry(nr, nidlist, nr_link) {
785 if (nr->nr_netstrfns != nf)
787 if (nr->nr_netnum != netnum)
792 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
795 list_add_tail(&nr->nr_link, nidlist);
796 INIT_LIST_HEAD(&nr->nr_addrranges);
797 nr->nr_netstrfns = nf;
799 nr->nr_netnum = netnum;
805 * Parses \<nidrange\> token of the syntax.
807 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
808 * \retval 0 otherwise
811 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
813 struct cfs_lstr addrrange;
819 if (cfs_gettok(src, '@', &addrrange) == 0)
822 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
825 nr = add_nidrange(&net, nidlist);
829 if (parse_addrange(&addrrange, nr) != 0)
834 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
839 * Frees addrrange structures of \a list.
841 * For each struct addrrange structure found on \a list it frees
842 * cfs_expr_list list attached to it and frees the addrrange itself.
847 free_addrranges(struct list_head *list)
849 while (!list_empty(list)) {
850 struct addrrange *ar;
852 ar = list_entry(list->next, struct addrrange, ar_link);
854 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
855 list_del(&ar->ar_link);
856 LIBCFS_FREE(ar, sizeof(struct addrrange));
861 * Frees nidrange strutures of \a list.
863 * For each struct nidrange structure found on \a list it frees
864 * addrrange list attached to it and frees the nidrange itself.
869 cfs_free_nidlist(struct list_head *list)
871 struct list_head *pos, *next;
874 list_for_each_safe(pos, next, list) {
875 nr = list_entry(pos, struct nidrange, nr_link);
876 free_addrranges(&nr->nr_addrranges);
878 LIBCFS_FREE(nr, sizeof(struct nidrange));
883 * Parses nid range list.
885 * Parses with rigorous syntax and overflow checking \a str into
886 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
887 * structures and links that structure to \a nidlist. The resulting
888 * list can be used to match a NID againts set of NIDS defined by \a
892 * \retval 1 on success
893 * \retval 0 otherwise
896 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
904 INIT_LIST_HEAD(nidlist);
906 rc = cfs_gettok(&src, ' ', &res);
908 cfs_free_nidlist(nidlist);
911 rc = parse_nidrange(&res, nidlist);
913 cfs_free_nidlist(nidlist);
921 * Nf_match_addr method for networks using numeric addresses
924 * \retval 0 otherwise
927 libcfs_num_match(__u32 addr, struct list_head *numaddr)
929 struct cfs_expr_list *el;
931 LASSERT(!list_empty(numaddr));
932 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
934 return cfs_expr_list_match(addr, el);
938 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
940 * \see cfs_parse_nidlist()
943 * \retval 0 otherwises
945 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
948 struct addrrange *ar;
950 list_for_each_entry(nr, nidlist, nr_link) {
951 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
953 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
957 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
958 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
959 &ar->ar_numaddr_ranges))
966 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
969 struct cfs_expr_list *el;
971 list_for_each_entry(el, list, el_link) {
973 i += cfs_expr_list_print(buffer + i, count - i, el);
979 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
982 struct cfs_expr_list *el;
984 list_for_each_entry(el, list, el_link) {
987 i += cfs_snprintf(buffer + i, count - i, ".");
988 i += cfs_expr_list_print(buffer + i, count - i, el);
995 * Print the network part of the nidrange \a nr into the specified \a buffer.
997 * \retval number of characters written
1000 cfs_print_network(char *buffer, int count, struct nidrange *nr)
1002 struct netstrfns *nf = nr->nr_netstrfns;
1004 if (nr->nr_netnum == 0)
1005 return cfs_snprintf(buffer, count, "@%s", nf->nf_name);
1007 return cfs_snprintf(buffer, count, "@%s%u",
1008 nf->nf_name, nr->nr_netnum);
1013 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
1014 * At max \a count characters can be printed into \a buffer.
1016 * \retval number of characters written
1019 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
1020 struct nidrange *nr)
1023 struct addrrange *ar;
1024 struct netstrfns *nf = nr->nr_netstrfns;
1026 list_for_each_entry(ar, addrranges, ar_link) {
1028 i += cfs_snprintf(buffer + i, count - i, " ");
1029 i += nf->nf_print_addrlist(buffer + i, count - i,
1030 &ar->ar_numaddr_ranges);
1031 i += cfs_print_network(buffer + i, count - i, nr);
1038 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
1039 * At max \a count characters can be printed into \a buffer.
1040 * Nidranges are separated by a space character.
1042 * \retval number of characters written
1044 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
1047 struct nidrange *nr;
1052 list_for_each_entry(nr, nidlist, nr_link) {
1054 i += cfs_snprintf(buffer + i, count - i, " ");
1056 if (nr->nr_all != 0) {
1057 LASSERT(list_empty(&nr->nr_addrranges));
1058 i += cfs_snprintf(buffer + i, count - i, "*");
1059 i += cfs_print_network(buffer + i, count - i, nr);
1061 i += cfs_print_addrranges(buffer + i, count - i,
1062 &nr->nr_addrranges, nr);
1069 * Determines minimum and maximum addresses for a single
1070 * numeric address range
1076 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1079 struct cfs_expr_list *el;
1080 struct cfs_range_expr *re;
1081 __u32 tmp_ip_addr = 0;
1082 unsigned int min_ip[4] = {0};
1083 unsigned int max_ip[4] = {0};
1086 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1087 list_for_each_entry(re, &el->el_exprs, re_link) {
1088 min_ip[re_count] = re->re_lo;
1089 max_ip[re_count] = re->re_hi;
1094 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
1095 (min_ip[2] << 8) | min_ip[3]);
1097 if (min_nid != NULL)
1098 *min_nid = tmp_ip_addr;
1100 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
1101 (max_ip[2] << 8) | max_ip[3]);
1103 if (max_nid != NULL)
1104 *max_nid = tmp_ip_addr;
1108 * Determines minimum and maximum addresses for a single
1109 * numeric address range
1115 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
1118 struct cfs_expr_list *el;
1119 struct cfs_range_expr *re;
1120 unsigned int min_addr = 0;
1121 unsigned int max_addr = 0;
1123 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
1124 list_for_each_entry(re, &el->el_exprs, re_link) {
1125 if (re->re_lo < min_addr || min_addr == 0)
1126 min_addr = re->re_lo;
1127 if (re->re_hi > max_addr)
1128 max_addr = re->re_hi;
1132 if (min_nid != NULL)
1133 *min_nid = min_addr;
1134 if (max_nid != NULL)
1135 *max_nid = max_addr;
1139 * Determines whether an expression list in an nidrange contains exactly
1140 * one contiguous address range. Calls the correct netstrfns for the LND
1144 * \retval true if contiguous
1145 * \retval false if not contiguous
1147 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
1149 struct nidrange *nr;
1150 struct netstrfns *nf = NULL;
1151 char *lndname = NULL;
1154 list_for_each_entry(nr, nidlist, nr_link) {
1155 nf = nr->nr_netstrfns;
1156 if (lndname == NULL)
1157 lndname = nf->nf_name;
1159 netnum = nr->nr_netnum;
1161 if (strcmp(lndname, nf->nf_name) != 0 ||
1162 netnum != nr->nr_netnum)
1169 if (!nf->nf_is_contiguous(nidlist))
1174 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
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, int 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);
1305 nf->nf_addr2str(max_addr, 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,
1312 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
1315 * Determines the min and max NID values for num LNDs
1321 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
1324 struct nidrange *nr;
1325 struct addrrange *ar;
1326 unsigned int tmp_min_addr = 0;
1327 unsigned int tmp_max_addr = 0;
1328 unsigned int min_addr = 0;
1329 unsigned int max_addr = 0;
1331 list_for_each_entry(nr, nidlist, nr_link) {
1332 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1333 cfs_num_ar_min_max(ar, &tmp_min_addr,
1335 if (tmp_min_addr < min_addr || min_addr == 0)
1336 min_addr = tmp_min_addr;
1337 if (tmp_max_addr > max_addr)
1338 max_addr = tmp_min_addr;
1341 *max_nid = max_addr;
1342 *min_nid = min_addr;
1346 * Takes an nidlist and determines the minimum and maximum
1353 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
1356 struct nidrange *nr;
1357 struct addrrange *ar;
1358 __u32 tmp_min_ip_addr = 0;
1359 __u32 tmp_max_ip_addr = 0;
1360 __u32 min_ip_addr = 0;
1361 __u32 max_ip_addr = 0;
1363 list_for_each_entry(nr, nidlist, nr_link) {
1364 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
1365 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
1367 if (tmp_min_ip_addr < min_ip_addr || min_ip_addr == 0)
1368 min_ip_addr = tmp_min_ip_addr;
1369 if (tmp_max_ip_addr > max_ip_addr)
1370 max_ip_addr = tmp_max_ip_addr;
1374 if (min_nid != NULL)
1375 *min_nid = min_ip_addr;
1376 if (max_nid != NULL)
1377 *max_nid = max_ip_addr;
1382 EXPORT_SYMBOL(libcfs_isknown_lnd);
1383 EXPORT_SYMBOL(libcfs_lnd2modname);
1384 EXPORT_SYMBOL(libcfs_lnd2str);
1385 EXPORT_SYMBOL(libcfs_str2lnd);
1386 EXPORT_SYMBOL(libcfs_net2str);
1387 EXPORT_SYMBOL(libcfs_nid2str);
1388 EXPORT_SYMBOL(libcfs_str2net);
1389 EXPORT_SYMBOL(libcfs_str2nid);
1390 EXPORT_SYMBOL(libcfs_id2str);
1391 EXPORT_SYMBOL(libcfs_str2anynid);
1392 EXPORT_SYMBOL(cfs_free_nidlist);
1393 EXPORT_SYMBOL(cfs_parse_nidlist);
1394 EXPORT_SYMBOL(cfs_print_nidlist);
1395 EXPORT_SYMBOL(cfs_match_nid);