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, 2012, 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 "../../include/linux/libcfs/libcfs.h"
44 #include "../../include/linux/lnet/lnet.h"
46 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
47 * consistent in all conversion functions. Some code fragments are copied
48 * around for the sake of clarity...
51 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
52 * Choose the number of nidstrings to support the MAXIMUM expected number of
53 * concurrent users. If there are more, the returned string will be volatile.
54 * NB this number must allow for a process to be descheduled for a timeslice
55 * between getting its string and using it.
58 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
59 static int libcfs_nidstring_idx;
61 static spinlock_t libcfs_nidstring_lock;
63 void libcfs_init_nidstrings(void)
65 spin_lock_init(&libcfs_nidstring_lock);
69 libcfs_next_nidstring(void)
74 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
76 str = libcfs_nidstrings[libcfs_nidstring_idx++];
77 if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
78 libcfs_nidstring_idx = 0;
80 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
84 static int libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
90 static void libcfs_ip_addr2str(__u32 addr, char *str)
92 snprintf(str, LNET_NIDSTR_SIZE, "%u.%u.%u.%u",
93 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
94 (addr >> 8) & 0xff, addr & 0xff);
97 static int libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
103 int n = nob; /* XscanfX */
106 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
108 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
109 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
110 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
117 static void libcfs_decnum_addr2str(__u32 addr, char *str)
119 snprintf(str, LNET_NIDSTR_SIZE, "%u", addr);
122 static void libcfs_hexnum_addr2str(__u32 addr, char *str)
124 snprintf(str, LNET_NIDSTR_SIZE, "0x%x", addr);
127 static int libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
132 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
136 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
140 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
147 * Nf_parse_addrlist method for networks using numeric addresses.
149 * Examples of such networks are gm and elan.
151 * \retval 0 if \a str parsed to numeric address
152 * \retval errno otherwise
155 libcfs_num_parse(char *str, int len, struct list_head *list)
157 struct cfs_expr_list *el;
160 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
162 list_add_tail(&el->el_link, list);
168 * Nf_match_addr method for networks using numeric addresses
171 * \retval 0 otherwise
174 libcfs_num_match(__u32 addr, struct list_head *numaddr)
176 struct cfs_expr_list *el;
178 LASSERT(!list_empty(numaddr));
179 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
181 return cfs_expr_list_match(addr, el);
188 void (*nf_addr2str)(__u32 addr, char *str);
189 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
190 int (*nf_parse_addrlist)(char *str, int len,
191 struct list_head *list);
192 int (*nf_match_addr)(__u32 addr, struct list_head *list);
195 static struct netstrfns libcfs_netstrfns[] = {
196 {/* .nf_type */ LOLND,
198 /* .nf_modname */ "klolnd",
199 /* .nf_addr2str */ libcfs_decnum_addr2str,
200 /* .nf_str2addr */ libcfs_lo_str2addr,
201 /* .nf_parse_addr*/ libcfs_num_parse,
202 /* .nf_match_addr*/ libcfs_num_match},
203 {/* .nf_type */ SOCKLND,
204 /* .nf_name */ "tcp",
205 /* .nf_modname */ "ksocklnd",
206 /* .nf_addr2str */ libcfs_ip_addr2str,
207 /* .nf_str2addr */ libcfs_ip_str2addr,
208 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
209 /* .nf_match_addr*/ cfs_ip_addr_match},
210 {/* .nf_type */ O2IBLND,
211 /* .nf_name */ "o2ib",
212 /* .nf_modname */ "ko2iblnd",
213 /* .nf_addr2str */ libcfs_ip_addr2str,
214 /* .nf_str2addr */ libcfs_ip_str2addr,
215 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
216 /* .nf_match_addr*/ cfs_ip_addr_match},
217 {/* .nf_type */ CIBLND,
218 /* .nf_name */ "cib",
219 /* .nf_modname */ "kciblnd",
220 /* .nf_addr2str */ libcfs_ip_addr2str,
221 /* .nf_str2addr */ libcfs_ip_str2addr,
222 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
223 /* .nf_match_addr*/ cfs_ip_addr_match},
224 {/* .nf_type */ OPENIBLND,
225 /* .nf_name */ "openib",
226 /* .nf_modname */ "kopeniblnd",
227 /* .nf_addr2str */ libcfs_ip_addr2str,
228 /* .nf_str2addr */ libcfs_ip_str2addr,
229 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
230 /* .nf_match_addr*/ cfs_ip_addr_match},
231 {/* .nf_type */ IIBLND,
232 /* .nf_name */ "iib",
233 /* .nf_modname */ "kiiblnd",
234 /* .nf_addr2str */ libcfs_ip_addr2str,
235 /* .nf_str2addr */ libcfs_ip_str2addr,
236 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
237 /* .nf_match_addr*/ cfs_ip_addr_match},
238 {/* .nf_type */ VIBLND,
239 /* .nf_name */ "vib",
240 /* .nf_modname */ "kviblnd",
241 /* .nf_addr2str */ libcfs_ip_addr2str,
242 /* .nf_str2addr */ libcfs_ip_str2addr,
243 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
244 /* .nf_match_addr*/ cfs_ip_addr_match},
245 {/* .nf_type */ RALND,
247 /* .nf_modname */ "kralnd",
248 /* .nf_addr2str */ libcfs_ip_addr2str,
249 /* .nf_str2addr */ libcfs_ip_str2addr,
250 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
251 /* .nf_match_addr*/ cfs_ip_addr_match},
252 {/* .nf_type */ QSWLND,
253 /* .nf_name */ "elan",
254 /* .nf_modname */ "kqswlnd",
255 /* .nf_addr2str */ libcfs_decnum_addr2str,
256 /* .nf_str2addr */ libcfs_num_str2addr,
257 /* .nf_parse_addrlist*/ libcfs_num_parse,
258 /* .nf_match_addr*/ libcfs_num_match},
259 {/* .nf_type */ GMLND,
261 /* .nf_modname */ "kgmlnd",
262 /* .nf_addr2str */ libcfs_hexnum_addr2str,
263 /* .nf_str2addr */ libcfs_num_str2addr,
264 /* .nf_parse_addrlist*/ libcfs_num_parse,
265 /* .nf_match_addr*/ libcfs_num_match},
266 {/* .nf_type */ MXLND,
268 /* .nf_modname */ "kmxlnd",
269 /* .nf_addr2str */ libcfs_ip_addr2str,
270 /* .nf_str2addr */ libcfs_ip_str2addr,
271 /* .nf_parse_addrlist*/ cfs_ip_addr_parse,
272 /* .nf_match_addr*/ cfs_ip_addr_match},
273 {/* .nf_type */ PTLLND,
274 /* .nf_name */ "ptl",
275 /* .nf_modname */ "kptllnd",
276 /* .nf_addr2str */ libcfs_decnum_addr2str,
277 /* .nf_str2addr */ libcfs_num_str2addr,
278 /* .nf_parse_addrlist*/ libcfs_num_parse,
279 /* .nf_match_addr*/ libcfs_num_match},
280 {/* .nf_type */ GNILND,
281 /* .nf_name */ "gni",
282 /* .nf_modname */ "kgnilnd",
283 /* .nf_addr2str */ libcfs_decnum_addr2str,
284 /* .nf_str2addr */ libcfs_num_str2addr,
285 /* .nf_parse_addrlist*/ libcfs_num_parse,
286 /* .nf_match_addr*/ libcfs_num_match},
287 /* placeholder for net0 alias. It MUST BE THE LAST ENTRY */
291 static const int libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
293 /* CAVEAT EMPTOR XscanfX
294 * I use "%n" at the end of a sscanf format to detect trailing junk. However
295 * sscanf may return immediately if it sees the terminating '0' in a string, so
296 * I initialise the %n variable to the expected length. If sscanf sets it;
297 * fine, if it doesn't, then the scan ended at the end of the string, which is
300 static struct netstrfns *
301 libcfs_lnd2netstrfns(int lnd)
306 for (i = 0; i < libcfs_nnetstrfns; i++)
307 if (lnd == libcfs_netstrfns[i].nf_type)
308 return &libcfs_netstrfns[i];
313 static struct netstrfns *
314 libcfs_namenum2netstrfns(const char *name)
316 struct netstrfns *nf;
319 for (i = 0; i < libcfs_nnetstrfns; i++) {
320 nf = &libcfs_netstrfns[i];
321 if (nf->nf_type >= 0 &&
322 !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
328 static struct netstrfns *
329 libcfs_name2netstrfns(const char *name)
333 for (i = 0; i < libcfs_nnetstrfns; i++)
334 if (libcfs_netstrfns[i].nf_type >= 0 &&
335 !strcmp(libcfs_netstrfns[i].nf_name, name))
336 return &libcfs_netstrfns[i];
342 libcfs_isknown_lnd(int type)
344 return libcfs_lnd2netstrfns(type) != NULL;
346 EXPORT_SYMBOL(libcfs_isknown_lnd);
349 libcfs_lnd2modname(int lnd)
351 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
353 return (nf == NULL) ? NULL : nf->nf_modname;
355 EXPORT_SYMBOL(libcfs_lnd2modname);
358 libcfs_lnd2str(int lnd)
361 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
366 str = libcfs_next_nidstring();
367 snprintf(str, LNET_NIDSTR_SIZE, "?%d?", lnd);
370 EXPORT_SYMBOL(libcfs_lnd2str);
373 libcfs_str2lnd(const char *str)
375 struct netstrfns *nf = libcfs_name2netstrfns(str);
382 EXPORT_SYMBOL(libcfs_str2lnd);
385 libcfs_net2str(__u32 net)
387 int lnd = LNET_NETTYP(net);
388 int num = LNET_NETNUM(net);
389 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
390 char *str = libcfs_next_nidstring();
393 snprintf(str, LNET_NIDSTR_SIZE, "<%d:%d>", lnd, num);
395 snprintf(str, LNET_NIDSTR_SIZE, "%s", nf->nf_name);
397 snprintf(str, LNET_NIDSTR_SIZE, "%s%d", nf->nf_name, num);
401 EXPORT_SYMBOL(libcfs_net2str);
404 libcfs_nid2str(lnet_nid_t nid)
406 __u32 addr = LNET_NIDADDR(nid);
407 __u32 net = LNET_NIDNET(nid);
408 int lnd = LNET_NETTYP(net);
409 int nnum = LNET_NETNUM(net);
410 struct netstrfns *nf;
414 if (nid == LNET_NID_ANY)
417 nf = libcfs_lnd2netstrfns(lnd);
418 str = libcfs_next_nidstring();
421 snprintf(str, LNET_NIDSTR_SIZE, "%x@<%d:%d>", addr, lnd, nnum);
423 nf->nf_addr2str(addr, str);
426 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s",
429 snprintf(str + nob, LNET_NIDSTR_SIZE - nob, "@%s%d",
435 EXPORT_SYMBOL(libcfs_nid2str);
437 static struct netstrfns *
438 libcfs_str2net_internal(const char *str, __u32 *net)
440 struct netstrfns *uninitialized_var(nf);
445 for (i = 0; i < libcfs_nnetstrfns; i++) {
446 nf = &libcfs_netstrfns[i];
447 if (nf->nf_type >= 0 &&
448 !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
452 if (i == libcfs_nnetstrfns)
455 nob = strlen(nf->nf_name);
457 if (strlen(str) == (unsigned int)nob) {
460 if (nf->nf_type == LOLND) /* net number not allowed */
465 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
466 i != (int)strlen(str))
470 *net = LNET_MKNET(nf->nf_type, netnum);
475 libcfs_str2net(const char *str)
479 if (libcfs_str2net_internal(str, &net) != NULL)
482 return LNET_NIDNET(LNET_NID_ANY);
484 EXPORT_SYMBOL(libcfs_str2net);
487 libcfs_str2nid(const char *str)
489 const char *sep = strchr(str, '@');
490 struct netstrfns *nf;
495 nf = libcfs_str2net_internal(sep + 1, &net);
499 sep = str + strlen(str);
500 net = LNET_MKNET(SOCKLND, 0);
501 nf = libcfs_lnd2netstrfns(SOCKLND);
505 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
508 return LNET_MKNID(net, addr);
510 EXPORT_SYMBOL(libcfs_str2nid);
513 libcfs_id2str(lnet_process_id_t id)
515 char *str = libcfs_next_nidstring();
517 if (id.pid == LNET_PID_ANY) {
518 snprintf(str, LNET_NIDSTR_SIZE,
519 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
523 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
524 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
525 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
528 EXPORT_SYMBOL(libcfs_id2str);
531 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
533 if (!strcmp(str, "*")) {
534 *nidp = LNET_NID_ANY;
538 *nidp = libcfs_str2nid(str);
539 return *nidp != LNET_NID_ANY;
541 EXPORT_SYMBOL(libcfs_str2anynid);
544 * Nid range list syntax.
547 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
548 * <nidrange> :== <addrrange> '@' <net>
549 * <addrrange> :== '*' |
552 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
554 * <cfs_expr_list> :== <number> |
556 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
557 * <range_expr> :== <number> |
558 * <number> '-' <number> |
559 * <number> '-' <number> '/' <number>
560 * <net> :== <netname> | <netname><number>
561 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
562 * "vib" | "ra" | "elan" | "mx" | "ptl"
567 * Structure to represent \<nidrange\> token of the syntax.
569 * One of this is created for each \<net\> parsed.
573 * Link to list of this structures which is built on nid range
576 struct list_head nr_link;
578 * List head for addrrange::ar_link.
580 struct list_head nr_addrranges;
582 * Flag indicating that *@<net> is found.
586 * Pointer to corresponding element of libcfs_netstrfns.
588 struct netstrfns *nr_netstrfns;
590 * Number of network. E.g. 5 if \<net\> is "elan5".
596 * Structure to represent \<addrrange\> token of the syntax.
600 * Link to nidrange::nr_addrranges.
602 struct list_head ar_link;
604 * List head for cfs_expr_list::el_list.
606 struct list_head ar_numaddr_ranges;
610 * Parses \<addrrange\> token on the syntax.
612 * Allocates struct addrrange and links to \a nidrange via
613 * (nidrange::nr_addrranges)
615 * \retval 1 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
616 * \retval 0 otherwise
619 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
621 struct addrrange *addrrange;
623 if (src->ls_len == 1 && src->ls_str[0] == '*') {
624 nidrange->nr_all = 1;
628 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
629 if (addrrange == NULL)
631 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
632 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
634 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
636 &addrrange->ar_numaddr_ranges);
640 * Finds or creates struct nidrange.
642 * Checks if \a src is a valid network name, looks for corresponding
643 * nidrange on the ist of nidranges (\a nidlist), creates new struct
644 * nidrange if it is not found.
646 * \retval pointer to struct nidrange matching network specified via \a src
647 * \retval NULL if \a src does not match any network
649 static struct nidrange *
650 add_nidrange(const struct cfs_lstr *src,
651 struct list_head *nidlist)
653 struct netstrfns *nf;
658 if (src->ls_len >= LNET_NIDSTR_SIZE)
661 nf = libcfs_namenum2netstrfns(src->ls_str);
664 endlen = src->ls_len - strlen(nf->nf_name);
666 /* network name only, e.g. "elan" or "tcp" */
669 /* e.g. "elan25" or "tcp23", refuse to parse if
670 * network name is not appended with decimal or
671 * hexadecimal number */
672 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
673 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
677 list_for_each_entry(nr, nidlist, nr_link) {
678 if (nr->nr_netstrfns != nf)
680 if (nr->nr_netnum != netnum)
685 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
688 list_add_tail(&nr->nr_link, nidlist);
689 INIT_LIST_HEAD(&nr->nr_addrranges);
690 nr->nr_netstrfns = nf;
692 nr->nr_netnum = netnum;
698 * Parses \<nidrange\> token of the syntax.
700 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
701 * \retval 0 otherwise
704 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
706 struct cfs_lstr addrrange;
712 if (cfs_gettok(src, '@', &addrrange) == 0)
715 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
718 nr = add_nidrange(&net, nidlist);
722 if (parse_addrange(&addrrange, nr) != 0)
727 CWARN("can't parse nidrange: \"%.*s\"\n", tmp.ls_len, tmp.ls_str);
732 * Frees addrrange structures of \a list.
734 * For each struct addrrange structure found on \a list it frees
735 * cfs_expr_list list attached to it and frees the addrrange itself.
740 free_addrranges(struct list_head *list)
742 while (!list_empty(list)) {
743 struct addrrange *ar;
745 ar = list_entry(list->next, struct addrrange, ar_link);
747 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
748 list_del(&ar->ar_link);
749 LIBCFS_FREE(ar, sizeof(struct addrrange));
754 * Frees nidrange strutures of \a list.
756 * For each struct nidrange structure found on \a list it frees
757 * addrrange list attached to it and frees the nidrange itself.
762 cfs_free_nidlist(struct list_head *list)
764 struct list_head *pos, *next;
767 list_for_each_safe(pos, next, list) {
768 nr = list_entry(pos, struct nidrange, nr_link);
769 free_addrranges(&nr->nr_addrranges);
771 LIBCFS_FREE(nr, sizeof(struct nidrange));
774 EXPORT_SYMBOL(cfs_free_nidlist);
777 * Parses nid range list.
779 * Parses with rigorous syntax and overflow checking \a str into
780 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
781 * structures and links that structure to \a nidlist. The resulting
782 * list can be used to match a NID againts set of NIDS defined by \a
786 * \retval 1 on success
787 * \retval 0 otherwise
790 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
798 INIT_LIST_HEAD(nidlist);
800 rc = cfs_gettok(&src, ' ', &res);
802 cfs_free_nidlist(nidlist);
805 rc = parse_nidrange(&res, nidlist);
807 cfs_free_nidlist(nidlist);
813 EXPORT_SYMBOL(cfs_parse_nidlist);
816 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
818 * \see cfs_parse_nidlist()
821 * \retval 0 otherwises
823 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
826 struct addrrange *ar;
828 list_for_each_entry(nr, nidlist, nr_link) {
829 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
831 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
835 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
836 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
837 &ar->ar_numaddr_ranges))
842 EXPORT_SYMBOL(cfs_match_nid);