1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Use is subject to license terms.
6 * Copyright (c) 2011, 2015, Intel Corporation.
9 /* This file is part of Lustre, http://www.lustre.org/
11 * Author: Phil Schwan <phil@clusterfs.com>
14 #define DEBUG_SUBSYSTEM S_LNET
16 #include <linux/sunrpc/addr.h>
17 #include <libcfs/libcfs.h>
18 #include <uapi/linux/lnet/nidstr.h>
19 #include <lnet/lib-types.h>
21 /* max value for numeric network address */
22 #define MAX_NUMERIC_VALUE 0xffffffff
24 #define IPSTRING_LENGTH 16
26 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
27 * consistent in all conversion functions. Some code fragments are copied
28 * around for the sake of clarity...
31 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
32 * Choose the number of nidstrings to support the MAXIMUM expected number of
33 * concurrent users. If there are more, the returned string will be volatile.
34 * NB this number must allow for a process to be descheduled for a timeslice
35 * between getting its string and using it.
38 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
39 static int libcfs_nidstring_idx;
41 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
43 static struct netstrfns *libcfs_namenum2netstrfns(const char *name);
46 libcfs_next_nidstring(void)
51 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
53 str = libcfs_nidstrings[libcfs_nidstring_idx++];
54 if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
55 libcfs_nidstring_idx = 0;
57 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
60 EXPORT_SYMBOL(libcfs_next_nidstring);
63 * Nid range list syntax.
66 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
67 * <nidrange> :== <addrrange> '@' <net>
68 * <addrrange> :== '*' |
71 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
73 * <cfs_expr_list> :== <number> |
75 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
76 * <range_expr> :== <number> |
77 * <number> '-' <number> |
78 * <number> '-' <number> '/' <number>
79 * <net> :== <netname> | <netname><number>
80 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
81 * "vib" | "ra" | "elan" | "mx" | "ptl"
86 * Structure to represent \<nidrange\> token of the syntax.
88 * One of this is created for each \<net\> parsed.
92 * Link to list of this structures which is built on nid range
95 struct list_head nr_link;
97 * List head for addrrange::ar_link.
99 struct list_head nr_addrranges;
101 * Flag indicating that *@<net> is found.
105 * Pointer to corresponding element of libcfs_netstrfns.
107 struct netstrfns *nr_netstrfns;
109 * Number of network. E.g. 5 if \<net\> is "elan5".
115 * Structure to represent \<addrrange\> token of the syntax.
119 * Link to nidrange::nr_addrranges.
121 struct list_head ar_link;
123 * List head for cfs_expr_list::el_list.
125 struct list_head ar_numaddr_ranges;
129 * Parses \<addrrange\> token on the syntax.
131 * Allocates struct addrrange and links to \a nidrange via
132 * (nidrange::nr_addrranges)
134 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
135 * \retval -errno otherwise
138 parse_addrange(char *str, struct nidrange *nidrange)
140 struct addrrange *addrrange;
142 if (strcmp(str, "*") == 0) {
143 nidrange->nr_all = 1;
147 CFS_ALLOC_PTR(addrrange);
148 if (addrrange == NULL)
150 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
151 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
153 return nidrange->nr_netstrfns->nf_parse_addrlist(str, strlen(str),
154 &addrrange->ar_numaddr_ranges);
158 * Finds or creates struct nidrange.
160 * Checks if \a src is a valid network name, looks for corresponding
161 * nidrange on the ist of nidranges (\a nidlist), creates new struct
162 * nidrange if it is not found.
164 * \retval pointer to struct nidrange matching network specified via \a src
165 * \retval NULL if \a src does not match any network
167 static struct nidrange *
168 add_nidrange(char *str, struct list_head *nidlist)
170 struct netstrfns *nf;
175 nf = libcfs_namenum2netstrfns(str);
178 end = str + strlen(nf->nf_name);
180 /* network name only, e.g. "elan" or "tcp" */
183 /* e.g. "elan25" or "tcp23", refuse to parse if
184 * network name is not appended with decimal or
187 if (kstrtouint(end, 0, &netnum) != 0)
191 list_for_each_entry(nr, nidlist, nr_link) {
192 if (nr->nr_netstrfns != nf)
194 if (nr->nr_netnum != netnum)
202 list_add_tail(&nr->nr_link, nidlist);
203 INIT_LIST_HEAD(&nr->nr_addrranges);
204 nr->nr_netstrfns = nf;
206 nr->nr_netnum = netnum;
212 * Parses \<nidrange\> token of the syntax.
214 * \retval 0 if \a src parses to \<addrrange\> '@' \<net\>
215 * \retval -EINVAL otherwise
218 parse_nidrange(char *str, struct list_head *nidlist)
224 addrrange = strim(strsep(&str, "@"));
229 if (strchr(net, '@') != NULL || !*net)
232 nr = add_nidrange(net, nidlist);
236 if (parse_addrange(addrrange, nr) != 0)
245 * Frees addrrange structures of \a list.
247 * For each struct addrrange structure found on \a list it frees
248 * cfs_expr_list list attached to it and frees the addrrange itself.
253 free_addrranges(struct list_head *list)
255 struct addrrange *ar;
257 while ((ar = list_first_entry_or_null(list,
260 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
261 list_del(&ar->ar_link);
267 * Frees nidrange strutures of \a list.
269 * For each struct nidrange structure found on \a list it frees
270 * addrrange list attached to it and frees the nidrange itself.
275 cfs_free_nidlist(struct list_head *list)
277 struct list_head *pos, *next;
280 list_for_each_safe(pos, next, list) {
281 nr = list_entry(pos, struct nidrange, nr_link);
282 free_addrranges(&nr->nr_addrranges);
287 EXPORT_SYMBOL(cfs_free_nidlist);
290 * Parses nid range list.
292 * Parses with rigorous syntax and overflow checking \a str into
293 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
294 * structures and links that structure to \a nidlist. The resulting
295 * list can be used to match a NID againts set of NIDS defined by \a
299 * \retval 0 on success
300 * \retval -errno otherwise (-ENOMEM or -EINVAL)
303 cfs_parse_nidlist(char *orig, struct list_head *nidlist)
308 orig = kstrdup(orig, GFP_KERNEL);
312 INIT_LIST_HEAD(nidlist);
314 while (rc == 0 && str) {
315 char *tok = strsep(&str, " ");
318 rc = parse_nidrange(tok, nidlist);
322 cfs_free_nidlist(nidlist);
323 else if (list_empty(nidlist))
327 EXPORT_SYMBOL(cfs_parse_nidlist);
330 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
332 * \see cfs_parse_nidlist()
335 * \retval 0 otherwises
337 int cfs_match_nid(struct lnet_nid *nid, struct list_head *nidlist)
340 struct addrrange *ar;
342 if (!nid_is_nid4(nid))
344 list_for_each_entry(nr, nidlist, nr_link) {
345 if (nr->nr_netstrfns->nf_type != nid->nid_type)
347 if (nr->nr_netnum != be16_to_cpu(nid->nid_num))
351 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
352 if (nr->nr_netstrfns->nf_match_addr(
353 be32_to_cpu(nid->nid_addr[0]),
354 &ar->ar_numaddr_ranges))
359 EXPORT_SYMBOL(cfs_match_nid);
362 * Print the network part of the nidrange \a nr into the specified \a buffer.
364 * \retval number of characters written
367 cfs_print_network(char *buffer, int count, struct nidrange *nr)
369 struct netstrfns *nf = nr->nr_netstrfns;
371 if (nr->nr_netnum == 0)
372 return scnprintf(buffer, count, "@%s", nf->nf_name);
374 return scnprintf(buffer, count, "@%s%u",
375 nf->nf_name, nr->nr_netnum);
379 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
380 * At max \a count characters can be printed into \a buffer.
382 * \retval number of characters written
385 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
389 struct addrrange *ar;
390 struct netstrfns *nf = nr->nr_netstrfns;
392 list_for_each_entry(ar, addrranges, ar_link) {
394 i += scnprintf(buffer + i, count - i, " ");
395 i += nf->nf_print_addrlist(buffer + i, count - i,
396 &ar->ar_numaddr_ranges);
397 i += cfs_print_network(buffer + i, count - i, nr);
403 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
404 * At max \a count characters can be printed into \a buffer.
405 * Nidranges are separated by a space character.
407 * \retval number of characters written
409 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
417 list_for_each_entry(nr, nidlist, nr_link) {
419 i += scnprintf(buffer + i, count - i, " ");
421 if (nr->nr_all != 0) {
422 LASSERT(list_empty(&nr->nr_addrranges));
423 i += scnprintf(buffer + i, count - i, "*");
424 i += cfs_print_network(buffer + i, count - i, nr);
426 i += cfs_print_addrranges(buffer + i, count - i,
427 &nr->nr_addrranges, nr);
432 EXPORT_SYMBOL(cfs_print_nidlist);
435 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
442 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
444 snprintf(str, size, "%u.%u.%u.%u",
445 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
446 (addr >> 8) & 0xff, addr & 0xff);
450 libcfs_ip_addr2str_size(const __be32 *addr, size_t asize,
451 char *str, size_t size)
453 struct sockaddr_storage sa = {};
457 sa.ss_family = AF_INET;
458 memcpy(&((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
462 sa.ss_family = AF_INET6;
463 memcpy(&((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
470 rpc_ntop((struct sockaddr *)&sa, str, size);
473 /* CAVEAT EMPTOR XscanfX
474 * I use "%n" at the end of a sscanf format to detect trailing junk. However
475 * sscanf may return immediately if it sees the terminating '0' in a string, so
476 * I initialise the %n variable to the expected length. If sscanf sets it;
477 * fine, if it doesn't, then the scan ended at the end of the string, which is
480 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
486 int n = nob; /* XscanfX */
489 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
491 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
492 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
493 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
500 libcfs_ip_str2addr_size(const char *str, int nob,
501 __be32 *addr, size_t *alen)
503 struct sockaddr_storage sa;
505 /* Note: 'net' arg to rpc_pton is only needed for link-local
506 * addresses. Such addresses would not work with LNet routing,
507 * so we can assume they aren't used. So it doesn't matter
508 * which net namespace is passed.
510 if (rpc_pton(&init_net, str, nob,
511 (struct sockaddr *)&sa, sizeof(sa)) == 0)
513 if (sa.ss_family == AF_INET6) {
515 &((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
520 if (sa.ss_family == AF_INET) {
522 &((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
531 /* Used by lnet/config.c so it can't be static */
533 cfs_ip_addr_parse(char *str, int len_ignored, struct list_head *list)
535 struct cfs_expr_list *el;
540 while (rc == 0 && str) {
543 res = strsep(&str, ".");
547 } else if ((rc = cfs_expr_list_parse(res, strlen(res),
548 0, 255, &el)) == 0) {
549 list_add_tail(&el->el_link, list);
554 if (rc == 0 && i == 4)
556 cfs_expr_list_free_list(list);
558 return rc ?: -EINVAL;
562 * Print the range expression \a re into specified \a buffer.
563 * If \a bracketed is true, expression does not need additional
566 * \retval number of characters written
569 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
579 if (expr->re_lo == expr->re_hi)
580 i = scnprintf(buffer, count, "%u", expr->re_lo);
581 else if (expr->re_stride == 1)
582 i = scnprintf(buffer, count, "%s%u-%u%s",
583 s, expr->re_lo, expr->re_hi, e);
585 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
586 s, expr->re_lo, expr->re_hi,
592 * Print a list of range expressions (\a expr_list) into specified \a buffer.
593 * If the list contains several expressions, separate them with comma
594 * and surround the list with brackets.
596 * \retval number of characters written
599 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
601 struct cfs_range_expr *expr;
608 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
612 i += scnprintf(buffer + i, count - i, "[");
614 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
616 i += scnprintf(buffer + i, count - i, ",");
617 i += cfs_range_expr_print(buffer + i, count - i, expr,
622 i += scnprintf(buffer + i, count - i, "]");
628 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
631 struct cfs_expr_list *el;
633 list_for_each_entry(el, list, el_link) {
636 i += scnprintf(buffer + i, count - i, ".");
637 i += cfs_expr_list_print(buffer + i, count - i, el);
643 * Matches address (\a addr) against address set encoded in \a list.
645 * \retval 1 if \a addr matches
646 * \retval 0 otherwise
649 cfs_ip_addr_match(__u32 addr, struct list_head *list)
651 struct cfs_expr_list *el;
654 list_for_each_entry_reverse(el, list, el_link) {
655 if (!cfs_expr_list_match(addr & 0xff, el))
665 * Print the network part of the nidrange \a nr into the specified \a buffer.
667 * \retval number of characters written
670 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
672 snprintf(str, size, "%u", addr);
676 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
681 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
685 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
689 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
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 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
720 struct cfs_expr_list *el;
722 list_for_each_entry(el, list, el_link) {
724 i += cfs_expr_list_print(buffer + i, count - i, el);
730 * Nf_match_addr method for networks using numeric addresses
733 * \retval 0 otherwise
736 libcfs_num_match(__u32 addr, struct list_head *numaddr)
738 struct cfs_expr_list *el;
740 LASSERT(!list_empty(numaddr));
741 el = list_first_entry(numaddr, struct cfs_expr_list, el_link);
743 return cfs_expr_list_match(addr, el);
746 static struct netstrfns libcfs_netstrfns[] = {
749 .nf_modname = "klolnd",
750 .nf_addr2str = libcfs_decnum_addr2str,
751 .nf_str2addr = libcfs_lo_str2addr,
752 .nf_parse_addrlist = libcfs_num_parse,
753 .nf_print_addrlist = libcfs_num_addr_range_print,
754 .nf_match_addr = libcfs_num_match
756 { .nf_type = SOCKLND,
758 .nf_modname = "ksocklnd",
759 .nf_addr2str = libcfs_ip_addr2str,
760 .nf_addr2str_size = libcfs_ip_addr2str_size,
761 .nf_str2addr = libcfs_ip_str2addr,
762 .nf_str2addr_size = libcfs_ip_str2addr_size,
763 .nf_parse_addrlist = cfs_ip_addr_parse,
764 .nf_print_addrlist = libcfs_ip_addr_range_print,
765 .nf_match_addr = cfs_ip_addr_match
767 { .nf_type = O2IBLND,
769 .nf_modname = "ko2iblnd",
770 .nf_addr2str = libcfs_ip_addr2str,
771 .nf_str2addr = libcfs_ip_str2addr,
772 .nf_parse_addrlist = cfs_ip_addr_parse,
773 .nf_print_addrlist = libcfs_ip_addr_range_print,
774 .nf_match_addr = cfs_ip_addr_match
778 .nf_modname = "kgnilnd",
779 .nf_addr2str = libcfs_decnum_addr2str,
780 .nf_str2addr = libcfs_num_str2addr,
781 .nf_parse_addrlist = libcfs_num_parse,
782 .nf_print_addrlist = libcfs_num_addr_range_print,
783 .nf_match_addr = libcfs_num_match
785 { .nf_type = GNIIPLND,
787 .nf_modname = "kgnilnd",
788 .nf_addr2str = libcfs_ip_addr2str,
789 .nf_str2addr = libcfs_ip_str2addr,
790 .nf_parse_addrlist = cfs_ip_addr_parse,
791 .nf_print_addrlist = libcfs_ip_addr_range_print,
792 .nf_match_addr = cfs_ip_addr_match
794 { .nf_type = PTL4LND,
796 .nf_modname = "kptl4lnd",
797 .nf_addr2str = libcfs_decnum_addr2str,
798 .nf_str2addr = libcfs_num_str2addr,
799 .nf_parse_addrlist = libcfs_num_parse,
800 .nf_print_addrlist = libcfs_num_addr_range_print,
801 .nf_match_addr = libcfs_num_match
806 .nf_modname = "kkfilnd",
807 .nf_addr2str = libcfs_decnum_addr2str,
808 .nf_str2addr = libcfs_num_str2addr,
809 .nf_parse_addrlist = libcfs_num_parse,
810 .nf_print_addrlist = libcfs_num_addr_range_print,
811 .nf_match_addr = libcfs_num_match
815 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
817 static struct netstrfns *
818 type2net_info(__u32 net_type)
822 for (i = 0; i < libcfs_nnetstrfns; i++) {
823 if (libcfs_netstrfns[i].nf_type == net_type)
824 return &libcfs_netstrfns[i];
831 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
838 if (net_type != LNET_NETTYP(net_id))
841 net_num = LNET_NETNUM(net_id);
843 /* if there is a net number but the list passed in is empty, then
846 if (!net_num && list_empty(net_num_list))
848 else if (list_empty(net_num_list))
851 if (!libcfs_num_match(net_num, net_num_list))
858 cfs_match_nid_net(struct lnet_nid *nid, __u32 net_type,
859 struct list_head *net_num_list,
860 struct list_head *addr)
863 struct netstrfns *nf;
865 if (!addr || list_empty(addr) || !net_num_list)
868 nf = type2net_info(LNET_NETTYP(LNET_NID_NET(nid)));
872 /* FIXME handle long-addr nid */
873 address = LNET_NIDADDR(lnet_nid_to_nid4(nid));
875 /* if either the address or net number don't match then no match */
876 if (!nf->nf_match_addr(address, addr) ||
877 !cfs_match_net(LNET_NID_NET(nid), net_type, net_num_list))
882 EXPORT_SYMBOL(cfs_match_nid_net);
884 static struct netstrfns *
885 libcfs_lnd2netstrfns(__u32 lnd)
889 for (i = 0; i < libcfs_nnetstrfns; i++)
890 if (lnd == libcfs_netstrfns[i].nf_type)
891 return &libcfs_netstrfns[i];
896 static struct netstrfns *
897 libcfs_namenum2netstrfns(const char *name)
899 struct netstrfns *nf;
902 for (i = 0; i < libcfs_nnetstrfns; i++) {
903 nf = &libcfs_netstrfns[i];
904 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
910 static struct netstrfns *
911 libcfs_name2netstrfns(const char *name)
915 for (i = 0; i < libcfs_nnetstrfns; i++)
916 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
917 return &libcfs_netstrfns[i];
923 libcfs_isknown_lnd(__u32 lnd)
925 return libcfs_lnd2netstrfns(lnd) != NULL;
927 EXPORT_SYMBOL(libcfs_isknown_lnd);
930 libcfs_lnd2modname(__u32 lnd)
932 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
934 return (nf == NULL) ? NULL : nf->nf_modname;
936 EXPORT_SYMBOL(libcfs_lnd2modname);
939 libcfs_str2lnd(const char *str)
941 struct netstrfns *nf = libcfs_name2netstrfns(str);
948 EXPORT_SYMBOL(libcfs_str2lnd);
951 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
953 struct netstrfns *nf;
955 nf = libcfs_lnd2netstrfns(lnd);
957 snprintf(buf, buf_size, "?%u?", lnd);
959 snprintf(buf, buf_size, "%s", nf->nf_name);
963 EXPORT_SYMBOL(libcfs_lnd2str_r);
966 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
968 __u32 nnum = LNET_NETNUM(net);
969 __u32 lnd = LNET_NETTYP(net);
970 struct netstrfns *nf;
972 nf = libcfs_lnd2netstrfns(lnd);
974 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
976 snprintf(buf, buf_size, "%s", nf->nf_name);
978 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
982 EXPORT_SYMBOL(libcfs_net2str_r);
985 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
987 __u32 addr = LNET_NIDADDR(nid);
988 __u32 net = LNET_NIDNET(nid);
989 __u32 nnum = LNET_NETNUM(net);
990 __u32 lnd = LNET_NETTYP(net);
991 struct netstrfns *nf;
993 if (nid == LNET_NID_ANY) {
994 strncpy(buf, "<?>", buf_size);
995 buf[buf_size - 1] = '\0';
999 nf = libcfs_lnd2netstrfns(lnd);
1001 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1005 nf->nf_addr2str(addr, buf, buf_size);
1006 addr_len = strlen(buf);
1008 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1011 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1017 EXPORT_SYMBOL(libcfs_nid2str_r);
1020 libcfs_nidstr_r(const struct lnet_nid *nid, char *buf, size_t buf_size)
1024 struct netstrfns *nf;
1026 if (LNET_NID_IS_ANY(nid)) {
1027 strncpy(buf, "<?>", buf_size);
1028 buf[buf_size - 1] = '\0';
1032 nnum = be16_to_cpu(nid->nid_num);
1033 lnd = nid->nid_type;
1034 nf = libcfs_lnd2netstrfns(lnd);
1038 if (nf->nf_addr2str_size)
1039 nf->nf_addr2str_size(nid->nid_addr, NID_ADDR_BYTES(nid),
1042 nf->nf_addr2str(ntohl(nid->nid_addr[0]), buf, buf_size);
1043 addr_len = strlen(buf);
1045 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1048 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1052 int words = DIV_ROUND_UP(NID_ADDR_BYTES(nid), 4);
1055 for (i = 0; i < words && i < 4; i++)
1056 l = snprintf(buf+l, buf_size-l, "%s%x",
1057 i ? ":" : "", ntohl(nid->nid_addr[i]));
1058 snprintf(buf+l, buf_size-l, "@<%u:%u>", lnd, nnum);
1063 EXPORT_SYMBOL(libcfs_nidstr_r);
1065 static struct netstrfns *
1066 libcfs_str2net_internal(const char *str, __u32 *net)
1068 struct netstrfns *nf = NULL;
1070 unsigned int netnum;
1073 for (i = 0; i < libcfs_nnetstrfns; i++) {
1074 nf = &libcfs_netstrfns[i];
1075 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1079 if (i == libcfs_nnetstrfns)
1082 nob = strlen(nf->nf_name);
1084 if (strlen(str) == (unsigned int)nob) {
1087 if (nf->nf_type == LOLND) /* net number not allowed */
1092 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1093 i != (int)strlen(str))
1097 *net = LNET_MKNET(nf->nf_type, netnum);
1102 libcfs_str2net(const char *str)
1106 if (libcfs_str2net_internal(str, &net) != NULL)
1109 return LNET_NET_ANY;
1111 EXPORT_SYMBOL(libcfs_str2net);
1114 libcfs_str2nid(const char *str)
1116 const char *sep = strchr(str, '@');
1117 struct netstrfns *nf;
1122 nf = libcfs_str2net_internal(sep + 1, &net);
1124 return LNET_NID_ANY;
1126 sep = str + strlen(str);
1127 net = LNET_MKNET(SOCKLND, 0);
1128 nf = libcfs_lnd2netstrfns(SOCKLND);
1129 LASSERT(nf != NULL);
1132 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1133 return LNET_NID_ANY;
1135 return LNET_MKNID(net, addr);
1137 EXPORT_SYMBOL(libcfs_str2nid);
1140 libcfs_strnid(struct lnet_nid *nid, const char *str)
1142 const char *sep = strchr(str, '@');
1143 struct netstrfns *nf;
1147 nf = libcfs_str2net_internal(sep + 1, &net);
1151 if (strcmp(str, "<?>") == 0) {
1152 memcpy(nid, &LNET_ANY_NID, sizeof(*nid));
1155 sep = str + strlen(str);
1156 net = LNET_MKNET(SOCKLND, 0);
1157 nf = libcfs_lnd2netstrfns(SOCKLND);
1158 LASSERT(nf != NULL);
1161 memset(nid, 0, sizeof(*nid));
1162 nid->nid_type = LNET_NETTYP(net);
1163 nid->nid_num = htons(LNET_NETNUM(net));
1164 if (nf->nf_str2addr_size) {
1167 if (!nf->nf_str2addr_size(str, (int)(sep - str),
1168 nid->nid_addr, &asize))
1170 nid->nid_size = asize - 4;
1174 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1176 nid->nid_addr[0] = htonl(addr);
1181 EXPORT_SYMBOL(libcfs_strnid);
1184 libcfs_id2str(struct lnet_process_id id)
1186 char *str = libcfs_next_nidstring();
1188 if (id.pid == LNET_PID_ANY) {
1189 snprintf(str, LNET_NIDSTR_SIZE,
1190 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1194 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1195 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1196 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1199 EXPORT_SYMBOL(libcfs_id2str);
1202 libcfs_idstr(struct lnet_processid *id)
1204 char *str = libcfs_next_nidstring();
1206 if (id->pid == LNET_PID_ANY) {
1207 snprintf(str, LNET_NIDSTR_SIZE,
1208 "LNET_PID_ANY-%s", libcfs_nidstr(&id->nid));
1212 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1213 ((id->pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1214 (id->pid & ~LNET_PID_USERFLAG), libcfs_nidstr(&id->nid));
1217 EXPORT_SYMBOL(libcfs_idstr);
1220 libcfs_strid(struct lnet_processid *id, const char *str)
1222 char *tmp = strchr(str, '-');
1224 id->pid = LNET_PID_LUSTRE;
1226 strncmp("LNET_PID_ANY-", str, tmp - str) != 0) {
1227 char pid[LNET_NIDSTR_SIZE];
1230 strscpy(pid, str, tmp - str);
1231 rc = kstrtou32(pid, 10, &id->pid);
1239 return libcfs_strnid(&id->nid, tmp);
1241 EXPORT_SYMBOL(libcfs_strid);
1244 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1246 if (!strcmp(str, "*")) {
1247 *nidp = LNET_NID_ANY;
1251 *nidp = libcfs_str2nid(str);
1252 return *nidp != LNET_NID_ANY;
1254 EXPORT_SYMBOL(libcfs_str2anynid);