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/
31 * lnet/lnet/nidstrings.c
33 * Author: Phil Schwan <phil@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/sunrpc/addr.h>
39 #include <libcfs/libcfs.h>
40 #include <uapi/linux/lnet/nidstr.h>
41 #include <lnet/lib-types.h>
43 /* max value for numeric network address */
44 #define MAX_NUMERIC_VALUE 0xffffffff
46 #define IPSTRING_LENGTH 16
48 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
49 * consistent in all conversion functions. Some code fragments are copied
50 * around for the sake of clarity...
53 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
54 * Choose the number of nidstrings to support the MAXIMUM expected number of
55 * concurrent users. If there are more, the returned string will be volatile.
56 * NB this number must allow for a process to be descheduled for a timeslice
57 * between getting its string and using it.
60 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
61 static int libcfs_nidstring_idx;
63 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
65 static struct netstrfns *libcfs_namenum2netstrfns(const char *name);
68 libcfs_next_nidstring(void)
73 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
75 str = libcfs_nidstrings[libcfs_nidstring_idx++];
76 if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
77 libcfs_nidstring_idx = 0;
79 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
82 EXPORT_SYMBOL(libcfs_next_nidstring);
85 * Nid range list syntax.
88 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
89 * <nidrange> :== <addrrange> '@' <net>
90 * <addrrange> :== '*' |
93 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
95 * <cfs_expr_list> :== <number> |
97 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
98 * <range_expr> :== <number> |
99 * <number> '-' <number> |
100 * <number> '-' <number> '/' <number>
101 * <net> :== <netname> | <netname><number>
102 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
103 * "vib" | "ra" | "elan" | "mx" | "ptl"
108 * Structure to represent \<nidrange\> token of the syntax.
110 * One of this is created for each \<net\> parsed.
114 * Link to list of this structures which is built on nid range
117 struct list_head nr_link;
119 * List head for addrrange::ar_link.
121 struct list_head nr_addrranges;
123 * Flag indicating that *@<net> is found.
127 * Pointer to corresponding element of libcfs_netstrfns.
129 struct netstrfns *nr_netstrfns;
131 * Number of network. E.g. 5 if \<net\> is "elan5".
137 * Structure to represent \<addrrange\> token of the syntax.
141 * Link to nidrange::nr_addrranges.
143 struct list_head ar_link;
145 * List head for cfs_expr_list::el_list.
147 struct list_head ar_numaddr_ranges;
151 * Parses \<addrrange\> token on the syntax.
153 * Allocates struct addrrange and links to \a nidrange via
154 * (nidrange::nr_addrranges)
156 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
157 * \retval -errno otherwise
160 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
162 struct addrrange *addrrange;
164 if (src->ls_len == 1 && src->ls_str[0] == '*') {
165 nidrange->nr_all = 1;
169 CFS_ALLOC_PTR(addrrange);
170 if (addrrange == NULL)
172 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
173 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
175 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
177 &addrrange->ar_numaddr_ranges);
181 * Finds or creates struct nidrange.
183 * Checks if \a src is a valid network name, looks for corresponding
184 * nidrange on the ist of nidranges (\a nidlist), creates new struct
185 * nidrange if it is not found.
187 * \retval pointer to struct nidrange matching network specified via \a src
188 * \retval NULL if \a src does not match any network
190 static struct nidrange *
191 add_nidrange(const struct cfs_lstr *src,
192 struct list_head *nidlist)
194 struct netstrfns *nf;
199 if (src->ls_len >= LNET_NIDSTR_SIZE)
202 nf = libcfs_namenum2netstrfns(src->ls_str);
205 endlen = src->ls_len - strlen(nf->nf_name);
207 /* network name only, e.g. "elan" or "tcp" */
210 /* e.g. "elan25" or "tcp23", refuse to parse if
211 * network name is not appended with decimal or
212 * hexadecimal number */
213 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
214 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
218 list_for_each_entry(nr, nidlist, nr_link) {
219 if (nr->nr_netstrfns != nf)
221 if (nr->nr_netnum != netnum)
229 list_add_tail(&nr->nr_link, nidlist);
230 INIT_LIST_HEAD(&nr->nr_addrranges);
231 nr->nr_netstrfns = nf;
233 nr->nr_netnum = netnum;
239 * Parses \<nidrange\> token of the syntax.
241 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
242 * \retval 0 otherwise
245 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
247 struct cfs_lstr addrrange;
251 if (cfs_gettok(src, '@', &addrrange) == 0)
254 if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
257 nr = add_nidrange(&net, nidlist);
261 if (parse_addrange(&addrrange, nr) != 0)
270 * Frees addrrange structures of \a list.
272 * For each struct addrrange structure found on \a list it frees
273 * cfs_expr_list list attached to it and frees the addrrange itself.
278 free_addrranges(struct list_head *list)
280 while (!list_empty(list)) {
281 struct addrrange *ar;
283 ar = list_entry(list->next, struct addrrange, ar_link);
285 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
286 list_del(&ar->ar_link);
292 * Frees nidrange strutures of \a list.
294 * For each struct nidrange structure found on \a list it frees
295 * addrrange list attached to it and frees the nidrange itself.
300 cfs_free_nidlist(struct list_head *list)
302 struct list_head *pos, *next;
305 list_for_each_safe(pos, next, list) {
306 nr = list_entry(pos, struct nidrange, nr_link);
307 free_addrranges(&nr->nr_addrranges);
312 EXPORT_SYMBOL(cfs_free_nidlist);
315 * Parses nid range list.
317 * Parses with rigorous syntax and overflow checking \a str into
318 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
319 * structures and links that structure to \a nidlist. The resulting
320 * list can be used to match a NID againts set of NIDS defined by \a
324 * \retval 1 on success
325 * \retval 0 otherwise
328 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
336 INIT_LIST_HEAD(nidlist);
338 rc = cfs_gettok(&src, ' ', &res);
340 cfs_free_nidlist(nidlist);
343 rc = parse_nidrange(&res, nidlist);
345 cfs_free_nidlist(nidlist);
351 EXPORT_SYMBOL(cfs_parse_nidlist);
354 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
356 * \see cfs_parse_nidlist()
359 * \retval 0 otherwises
361 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
364 struct addrrange *ar;
366 list_for_each_entry(nr, nidlist, nr_link) {
367 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
369 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
373 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
374 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
375 &ar->ar_numaddr_ranges))
380 EXPORT_SYMBOL(cfs_match_nid);
383 * Print the network part of the nidrange \a nr into the specified \a buffer.
385 * \retval number of characters written
388 cfs_print_network(char *buffer, int count, struct nidrange *nr)
390 struct netstrfns *nf = nr->nr_netstrfns;
392 if (nr->nr_netnum == 0)
393 return scnprintf(buffer, count, "@%s", nf->nf_name);
395 return scnprintf(buffer, count, "@%s%u",
396 nf->nf_name, nr->nr_netnum);
400 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
401 * At max \a count characters can be printed into \a buffer.
403 * \retval number of characters written
406 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
410 struct addrrange *ar;
411 struct netstrfns *nf = nr->nr_netstrfns;
413 list_for_each_entry(ar, addrranges, ar_link) {
415 i += scnprintf(buffer + i, count - i, " ");
416 i += nf->nf_print_addrlist(buffer + i, count - i,
417 &ar->ar_numaddr_ranges);
418 i += cfs_print_network(buffer + i, count - i, nr);
424 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
425 * At max \a count characters can be printed into \a buffer.
426 * Nidranges are separated by a space character.
428 * \retval number of characters written
430 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
438 list_for_each_entry(nr, nidlist, nr_link) {
440 i += scnprintf(buffer + i, count - i, " ");
442 if (nr->nr_all != 0) {
443 LASSERT(list_empty(&nr->nr_addrranges));
444 i += scnprintf(buffer + i, count - i, "*");
445 i += cfs_print_network(buffer + i, count - i, nr);
447 i += cfs_print_addrranges(buffer + i, count - i,
448 &nr->nr_addrranges, nr);
453 EXPORT_SYMBOL(cfs_print_nidlist);
456 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
463 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
465 snprintf(str, size, "%u.%u.%u.%u",
466 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
467 (addr >> 8) & 0xff, addr & 0xff);
471 libcfs_ip_addr2str_size(const __be32 *addr, size_t asize,
472 char *str, size_t size)
474 struct sockaddr_storage sa = {};
478 sa.ss_family = AF_INET;
479 memcpy(&((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
483 sa.ss_family = AF_INET6;
484 memcpy(&((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
491 rpc_ntop((struct sockaddr *)&sa, str, size);
494 /* CAVEAT EMPTOR XscanfX
495 * I use "%n" at the end of a sscanf format to detect trailing junk. However
496 * sscanf may return immediately if it sees the terminating '0' in a string, so
497 * I initialise the %n variable to the expected length. If sscanf sets it;
498 * fine, if it doesn't, then the scan ended at the end of the string, which is
501 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
507 int n = nob; /* XscanfX */
510 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
512 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
513 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
514 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
521 libcfs_ip_str2addr_size(const char *str, int nob,
522 __be32 *addr, size_t *alen)
524 struct sockaddr_storage sa;
526 /* Note: 'net' arg to rpc_pton is only needed for link-local
527 * addresses. Such addresses would not work with LNet routing,
528 * so we can assume they aren't used. So it doesn't matter
529 * which net namespace is passed.
531 if (rpc_pton(&init_net, str, nob,
532 (struct sockaddr *)&sa, sizeof(sa)) == 0)
534 if (sa.ss_family == AF_INET6) {
536 &((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
541 if (sa.ss_family == AF_INET) {
543 &((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
552 /* Used by lnet/config.c so it can't be static */
554 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
556 struct cfs_expr_list *el;
565 while (src.ls_str != NULL) {
568 if (!cfs_gettok(&src, '.', &res)) {
573 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
577 list_add_tail(&el->el_link, list);
586 cfs_expr_list_free_list(list);
592 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
595 struct cfs_expr_list *el;
597 list_for_each_entry(el, list, el_link) {
600 i += scnprintf(buffer + i, count - i, ".");
601 i += cfs_expr_list_print(buffer + i, count - i, el);
607 * Matches address (\a addr) against address set encoded in \a list.
609 * \retval 1 if \a addr matches
610 * \retval 0 otherwise
613 cfs_ip_addr_match(__u32 addr, struct list_head *list)
615 struct cfs_expr_list *el;
618 list_for_each_entry_reverse(el, list, el_link) {
619 if (!cfs_expr_list_match(addr & 0xff, el))
629 * Print the network part of the nidrange \a nr into the specified \a buffer.
631 * \retval number of characters written
634 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
636 snprintf(str, size, "%u", addr);
640 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
645 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
649 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
653 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
660 * Nf_parse_addrlist method for networks using numeric addresses.
662 * Examples of such networks are gm and elan.
664 * \retval 0 if \a str parsed to numeric address
665 * \retval errno otherwise
668 libcfs_num_parse(char *str, int len, struct list_head *list)
670 struct cfs_expr_list *el;
673 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
675 list_add_tail(&el->el_link, list);
681 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
684 struct cfs_expr_list *el;
686 list_for_each_entry(el, list, el_link) {
688 i += cfs_expr_list_print(buffer + i, count - i, el);
694 * Nf_match_addr method for networks using numeric addresses
697 * \retval 0 otherwise
700 libcfs_num_match(__u32 addr, struct list_head *numaddr)
702 struct cfs_expr_list *el;
704 LASSERT(!list_empty(numaddr));
705 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
707 return cfs_expr_list_match(addr, el);
710 static struct netstrfns libcfs_netstrfns[] = {
713 .nf_modname = "klolnd",
714 .nf_addr2str = libcfs_decnum_addr2str,
715 .nf_str2addr = libcfs_lo_str2addr,
716 .nf_parse_addrlist = libcfs_num_parse,
717 .nf_print_addrlist = libcfs_num_addr_range_print,
718 .nf_match_addr = libcfs_num_match
720 { .nf_type = SOCKLND,
722 .nf_modname = "ksocklnd",
723 .nf_addr2str = libcfs_ip_addr2str,
724 .nf_addr2str_size = libcfs_ip_addr2str_size,
725 .nf_str2addr = libcfs_ip_str2addr,
726 .nf_str2addr_size = libcfs_ip_str2addr_size,
727 .nf_parse_addrlist = cfs_ip_addr_parse,
728 .nf_print_addrlist = libcfs_ip_addr_range_print,
729 .nf_match_addr = cfs_ip_addr_match
731 { .nf_type = O2IBLND,
733 .nf_modname = "ko2iblnd",
734 .nf_addr2str = libcfs_ip_addr2str,
735 .nf_str2addr = libcfs_ip_str2addr,
736 .nf_parse_addrlist = cfs_ip_addr_parse,
737 .nf_print_addrlist = libcfs_ip_addr_range_print,
738 .nf_match_addr = cfs_ip_addr_match
742 .nf_modname = "kgnilnd",
743 .nf_addr2str = libcfs_decnum_addr2str,
744 .nf_str2addr = libcfs_num_str2addr,
745 .nf_parse_addrlist = libcfs_num_parse,
746 .nf_print_addrlist = libcfs_num_addr_range_print,
747 .nf_match_addr = libcfs_num_match
749 { .nf_type = GNIIPLND,
751 .nf_modname = "kgnilnd",
752 .nf_addr2str = libcfs_ip_addr2str,
753 .nf_str2addr = libcfs_ip_str2addr,
754 .nf_parse_addrlist = cfs_ip_addr_parse,
755 .nf_print_addrlist = libcfs_ip_addr_range_print,
756 .nf_match_addr = cfs_ip_addr_match
758 { .nf_type = PTL4LND,
760 .nf_modname = "kptl4lnd",
761 .nf_addr2str = libcfs_decnum_addr2str,
762 .nf_str2addr = libcfs_num_str2addr,
763 .nf_parse_addrlist = libcfs_num_parse,
764 .nf_print_addrlist = libcfs_num_addr_range_print,
765 .nf_match_addr = libcfs_num_match
769 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
771 static struct netstrfns *
772 type2net_info(__u32 net_type)
776 for (i = 0; i < libcfs_nnetstrfns; i++) {
777 if (libcfs_netstrfns[i].nf_type == net_type)
778 return &libcfs_netstrfns[i];
785 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
792 if (net_type != LNET_NETTYP(net_id))
795 net_num = LNET_NETNUM(net_id);
797 /* if there is a net number but the list passed in is empty, then
800 if (!net_num && list_empty(net_num_list))
802 else if (list_empty(net_num_list))
805 if (!libcfs_num_match(net_num, net_num_list))
812 cfs_match_nid_net(lnet_nid_t nid, __u32 net_type,
813 struct list_head *net_num_list,
814 struct list_head *addr)
817 struct netstrfns *nf;
819 if (!addr || !net_num_list)
822 nf = type2net_info(LNET_NETTYP(LNET_NIDNET(nid)));
823 if (!nf || !net_num_list || !addr)
826 address = LNET_NIDADDR(nid);
828 /* if either the address or net number don't match then no match */
829 if (!nf->nf_match_addr(address, addr) ||
830 !cfs_match_net(LNET_NIDNET(nid), net_type, net_num_list))
835 EXPORT_SYMBOL(cfs_match_nid_net);
837 static struct netstrfns *
838 libcfs_lnd2netstrfns(__u32 lnd)
842 for (i = 0; i < libcfs_nnetstrfns; i++)
843 if (lnd == libcfs_netstrfns[i].nf_type)
844 return &libcfs_netstrfns[i];
849 static struct netstrfns *
850 libcfs_namenum2netstrfns(const char *name)
852 struct netstrfns *nf;
855 for (i = 0; i < libcfs_nnetstrfns; i++) {
856 nf = &libcfs_netstrfns[i];
857 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
863 static struct netstrfns *
864 libcfs_name2netstrfns(const char *name)
868 for (i = 0; i < libcfs_nnetstrfns; i++)
869 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
870 return &libcfs_netstrfns[i];
876 libcfs_isknown_lnd(__u32 lnd)
878 return libcfs_lnd2netstrfns(lnd) != NULL;
880 EXPORT_SYMBOL(libcfs_isknown_lnd);
883 libcfs_lnd2modname(__u32 lnd)
885 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
887 return (nf == NULL) ? NULL : nf->nf_modname;
889 EXPORT_SYMBOL(libcfs_lnd2modname);
892 libcfs_str2lnd(const char *str)
894 struct netstrfns *nf = libcfs_name2netstrfns(str);
901 EXPORT_SYMBOL(libcfs_str2lnd);
904 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
906 struct netstrfns *nf;
908 nf = libcfs_lnd2netstrfns(lnd);
910 snprintf(buf, buf_size, "?%u?", lnd);
912 snprintf(buf, buf_size, "%s", nf->nf_name);
916 EXPORT_SYMBOL(libcfs_lnd2str_r);
919 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
921 __u32 nnum = LNET_NETNUM(net);
922 __u32 lnd = LNET_NETTYP(net);
923 struct netstrfns *nf;
925 nf = libcfs_lnd2netstrfns(lnd);
927 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
929 snprintf(buf, buf_size, "%s", nf->nf_name);
931 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
935 EXPORT_SYMBOL(libcfs_net2str_r);
938 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
940 __u32 addr = LNET_NIDADDR(nid);
941 __u32 net = LNET_NIDNET(nid);
942 __u32 nnum = LNET_NETNUM(net);
943 __u32 lnd = LNET_NETTYP(net);
944 struct netstrfns *nf;
946 if (nid == LNET_NID_ANY) {
947 strncpy(buf, "<?>", buf_size);
948 buf[buf_size - 1] = '\0';
952 nf = libcfs_lnd2netstrfns(lnd);
954 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
958 nf->nf_addr2str(addr, buf, buf_size);
959 addr_len = strlen(buf);
961 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
964 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
970 EXPORT_SYMBOL(libcfs_nid2str_r);
973 libcfs_nidstr_r(const struct lnet_nid *nid, char *buf, size_t buf_size)
975 __u32 nnum = be16_to_cpu(nid->nid_num);
976 __u32 lnd = nid->nid_type;
977 struct netstrfns *nf;
979 if (LNET_NID_IS_ANY(nid)) {
980 strncpy(buf, "<?>", buf_size);
981 buf[buf_size - 1] = '\0';
985 nf = libcfs_lnd2netstrfns(lnd);
989 if (nf->nf_addr2str_size)
990 nf->nf_addr2str_size(nid->nid_addr, NID_ADDR_BYTES(nid),
993 nf->nf_addr2str(ntohl(nid->nid_addr[0]), buf, buf_size);
994 addr_len = strlen(buf);
996 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
999 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1003 int words = DIV_ROUND_UP(NID_ADDR_BYTES(nid), 4);
1006 for (i = 0; i < words && i < 4; i++)
1007 l = snprintf(buf+l, buf_size-l, "%s%x",
1008 i ? ":" : "", ntohl(nid->nid_addr[i]));
1009 snprintf(buf+l, buf_size-l, "@<%u:%u>", lnd, nnum);
1014 EXPORT_SYMBOL(libcfs_nidstr_r);
1016 static struct netstrfns *
1017 libcfs_str2net_internal(const char *str, __u32 *net)
1019 struct netstrfns *nf = NULL;
1021 unsigned int netnum;
1024 for (i = 0; i < libcfs_nnetstrfns; i++) {
1025 nf = &libcfs_netstrfns[i];
1026 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1030 if (i == libcfs_nnetstrfns)
1033 nob = strlen(nf->nf_name);
1035 if (strlen(str) == (unsigned int)nob) {
1038 if (nf->nf_type == LOLND) /* net number not allowed */
1043 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1044 i != (int)strlen(str))
1048 *net = LNET_MKNET(nf->nf_type, netnum);
1053 libcfs_str2net(const char *str)
1057 if (libcfs_str2net_internal(str, &net) != NULL)
1060 return LNET_NET_ANY;
1062 EXPORT_SYMBOL(libcfs_str2net);
1065 libcfs_str2nid(const char *str)
1067 const char *sep = strchr(str, '@');
1068 struct netstrfns *nf;
1073 nf = libcfs_str2net_internal(sep + 1, &net);
1075 return LNET_NID_ANY;
1077 sep = str + strlen(str);
1078 net = LNET_MKNET(SOCKLND, 0);
1079 nf = libcfs_lnd2netstrfns(SOCKLND);
1080 LASSERT(nf != NULL);
1083 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1084 return LNET_NID_ANY;
1086 return LNET_MKNID(net, addr);
1088 EXPORT_SYMBOL(libcfs_str2nid);
1091 libcfs_strnid(struct lnet_nid *nid, const char *str)
1093 const char *sep = strchr(str, '@');
1094 struct netstrfns *nf;
1098 nf = libcfs_str2net_internal(sep + 1, &net);
1102 sep = str + strlen(str);
1103 net = LNET_MKNET(SOCKLND, 0);
1104 nf = libcfs_lnd2netstrfns(SOCKLND);
1105 LASSERT(nf != NULL);
1108 memset(nid, 0, sizeof(*nid));
1109 nid->nid_type = LNET_NETTYP(net);
1110 nid->nid_num = htons(LNET_NETNUM(net));
1111 if (nf->nf_str2addr_size) {
1114 if (!nf->nf_str2addr_size(str, (int)(sep - str),
1115 nid->nid_addr, &asize))
1117 nid->nid_size = asize - 4;
1121 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1123 nid->nid_addr[0] = htonl(addr);
1128 EXPORT_SYMBOL(libcfs_strnid);
1131 libcfs_id2str(struct lnet_process_id id)
1133 char *str = libcfs_next_nidstring();
1135 if (id.pid == LNET_PID_ANY) {
1136 snprintf(str, LNET_NIDSTR_SIZE,
1137 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1141 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1142 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1143 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1146 EXPORT_SYMBOL(libcfs_id2str);
1149 libcfs_idstr(struct lnet_processid *id)
1151 char *str = libcfs_next_nidstring();
1153 if (id->pid == LNET_PID_ANY) {
1154 snprintf(str, LNET_NIDSTR_SIZE,
1155 "LNET_PID_ANY-%s", libcfs_nidstr(&id->nid));
1159 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1160 ((id->pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1161 (id->pid & ~LNET_PID_USERFLAG), libcfs_nidstr(&id->nid));
1164 EXPORT_SYMBOL(libcfs_idstr);
1167 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1169 if (!strcmp(str, "*")) {
1170 *nidp = LNET_NID_ANY;
1174 *nidp = libcfs_str2nid(str);
1175 return *nidp != LNET_NID_ANY;
1177 EXPORT_SYMBOL(libcfs_str2anynid);