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(char *str, struct nidrange *nidrange)
162 struct addrrange *addrrange;
164 if (strcmp(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(str, strlen(str),
176 &addrrange->ar_numaddr_ranges);
180 * Finds or creates struct nidrange.
182 * Checks if \a src is a valid network name, looks for corresponding
183 * nidrange on the ist of nidranges (\a nidlist), creates new struct
184 * nidrange if it is not found.
186 * \retval pointer to struct nidrange matching network specified via \a src
187 * \retval NULL if \a src does not match any network
189 static struct nidrange *
190 add_nidrange(char *str, struct list_head *nidlist)
192 struct netstrfns *nf;
197 nf = libcfs_namenum2netstrfns(str);
200 end = str + strlen(nf->nf_name);
202 /* network name only, e.g. "elan" or "tcp" */
205 /* e.g. "elan25" or "tcp23", refuse to parse if
206 * network name is not appended with decimal or
209 if (kstrtouint(end, 0, &netnum) != 0)
213 list_for_each_entry(nr, nidlist, nr_link) {
214 if (nr->nr_netstrfns != nf)
216 if (nr->nr_netnum != netnum)
224 list_add_tail(&nr->nr_link, nidlist);
225 INIT_LIST_HEAD(&nr->nr_addrranges);
226 nr->nr_netstrfns = nf;
228 nr->nr_netnum = netnum;
234 * Parses \<nidrange\> token of the syntax.
236 * \retval 0 if \a src parses to \<addrrange\> '@' \<net\>
237 * \retval -EINVAL otherwise
240 parse_nidrange(char *str, struct list_head *nidlist)
246 addrrange = strim(strsep(&str, "@"));
251 if (strchr(net, '@') != NULL || !*net)
254 nr = add_nidrange(net, nidlist);
258 if (parse_addrange(addrrange, nr) != 0)
267 * Frees addrrange structures of \a list.
269 * For each struct addrrange structure found on \a list it frees
270 * cfs_expr_list list attached to it and frees the addrrange itself.
275 free_addrranges(struct list_head *list)
277 struct addrrange *ar;
279 while ((ar = list_first_entry_or_null(list,
282 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
283 list_del(&ar->ar_link);
289 * Frees nidrange strutures of \a list.
291 * For each struct nidrange structure found on \a list it frees
292 * addrrange list attached to it and frees the nidrange itself.
297 cfs_free_nidlist(struct list_head *list)
299 struct list_head *pos, *next;
302 list_for_each_safe(pos, next, list) {
303 nr = list_entry(pos, struct nidrange, nr_link);
304 free_addrranges(&nr->nr_addrranges);
309 EXPORT_SYMBOL(cfs_free_nidlist);
312 * Parses nid range list.
314 * Parses with rigorous syntax and overflow checking \a str into
315 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
316 * structures and links that structure to \a nidlist. The resulting
317 * list can be used to match a NID againts set of NIDS defined by \a
321 * \retval 0 on success
322 * \retval -errno otherwise (-ENOMEM or -EINVAL)
325 cfs_parse_nidlist(char *orig, struct list_head *nidlist)
330 orig = kstrdup(orig, GFP_KERNEL);
334 INIT_LIST_HEAD(nidlist);
336 while (rc == 0 && str) {
337 char *tok = strsep(&str, " ");
340 rc = parse_nidrange(tok, nidlist);
344 cfs_free_nidlist(nidlist);
345 else if (list_empty(nidlist))
349 EXPORT_SYMBOL(cfs_parse_nidlist);
352 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
354 * \see cfs_parse_nidlist()
357 * \retval 0 otherwises
359 int cfs_match_nid(struct lnet_nid *nid, struct list_head *nidlist)
362 struct addrrange *ar;
364 if (!nid_is_nid4(nid))
366 list_for_each_entry(nr, nidlist, nr_link) {
367 if (nr->nr_netstrfns->nf_type != nid->nid_type)
369 if (nr->nr_netnum != be16_to_cpu(nid->nid_num))
373 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
374 if (nr->nr_netstrfns->nf_match_addr(
375 be32_to_cpu(nid->nid_addr[0]),
376 &ar->ar_numaddr_ranges))
381 EXPORT_SYMBOL(cfs_match_nid);
384 * Print the network part of the nidrange \a nr into the specified \a buffer.
386 * \retval number of characters written
389 cfs_print_network(char *buffer, int count, struct nidrange *nr)
391 struct netstrfns *nf = nr->nr_netstrfns;
393 if (nr->nr_netnum == 0)
394 return scnprintf(buffer, count, "@%s", nf->nf_name);
396 return scnprintf(buffer, count, "@%s%u",
397 nf->nf_name, nr->nr_netnum);
401 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
402 * At max \a count characters can be printed into \a buffer.
404 * \retval number of characters written
407 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
411 struct addrrange *ar;
412 struct netstrfns *nf = nr->nr_netstrfns;
414 list_for_each_entry(ar, addrranges, ar_link) {
416 i += scnprintf(buffer + i, count - i, " ");
417 i += nf->nf_print_addrlist(buffer + i, count - i,
418 &ar->ar_numaddr_ranges);
419 i += cfs_print_network(buffer + i, count - i, nr);
425 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
426 * At max \a count characters can be printed into \a buffer.
427 * Nidranges are separated by a space character.
429 * \retval number of characters written
431 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
439 list_for_each_entry(nr, nidlist, nr_link) {
441 i += scnprintf(buffer + i, count - i, " ");
443 if (nr->nr_all != 0) {
444 LASSERT(list_empty(&nr->nr_addrranges));
445 i += scnprintf(buffer + i, count - i, "*");
446 i += cfs_print_network(buffer + i, count - i, nr);
448 i += cfs_print_addrranges(buffer + i, count - i,
449 &nr->nr_addrranges, nr);
454 EXPORT_SYMBOL(cfs_print_nidlist);
457 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
464 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
466 snprintf(str, size, "%u.%u.%u.%u",
467 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
468 (addr >> 8) & 0xff, addr & 0xff);
472 libcfs_ip_addr2str_size(const __be32 *addr, size_t asize,
473 char *str, size_t size)
475 struct sockaddr_storage sa = {};
479 sa.ss_family = AF_INET;
480 memcpy(&((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
484 sa.ss_family = AF_INET6;
485 memcpy(&((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
492 rpc_ntop((struct sockaddr *)&sa, str, size);
495 /* CAVEAT EMPTOR XscanfX
496 * I use "%n" at the end of a sscanf format to detect trailing junk. However
497 * sscanf may return immediately if it sees the terminating '0' in a string, so
498 * I initialise the %n variable to the expected length. If sscanf sets it;
499 * fine, if it doesn't, then the scan ended at the end of the string, which is
502 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
508 int n = nob; /* XscanfX */
511 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
513 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
514 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
515 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
522 libcfs_ip_str2addr_size(const char *str, int nob,
523 __be32 *addr, size_t *alen)
525 struct sockaddr_storage sa;
527 /* Note: 'net' arg to rpc_pton is only needed for link-local
528 * addresses. Such addresses would not work with LNet routing,
529 * so we can assume they aren't used. So it doesn't matter
530 * which net namespace is passed.
532 if (rpc_pton(&init_net, str, nob,
533 (struct sockaddr *)&sa, sizeof(sa)) == 0)
535 if (sa.ss_family == AF_INET6) {
537 &((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
542 if (sa.ss_family == AF_INET) {
544 &((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
553 /* Used by lnet/config.c so it can't be static */
555 cfs_ip_addr_parse(char *str, int len_ignored, struct list_head *list)
557 struct cfs_expr_list *el;
562 while (rc == 0 && str) {
565 res = strsep(&str, ".");
569 } else if ((rc = cfs_expr_list_parse(res, strlen(res),
570 0, 255, &el)) == 0) {
571 list_add_tail(&el->el_link, list);
576 if (rc == 0 && i == 4)
578 cfs_expr_list_free_list(list);
580 return rc ?: -EINVAL;
584 * Print the range expression \a re into specified \a buffer.
585 * If \a bracketed is true, expression does not need additional
588 * \retval number of characters written
591 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
601 if (expr->re_lo == expr->re_hi)
602 i = scnprintf(buffer, count, "%u", expr->re_lo);
603 else if (expr->re_stride == 1)
604 i = scnprintf(buffer, count, "%s%u-%u%s",
605 s, expr->re_lo, expr->re_hi, e);
607 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
608 s, expr->re_lo, expr->re_hi,
614 * Print a list of range expressions (\a expr_list) into specified \a buffer.
615 * If the list contains several expressions, separate them with comma
616 * and surround the list with brackets.
618 * \retval number of characters written
621 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
623 struct cfs_range_expr *expr;
630 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
634 i += scnprintf(buffer + i, count - i, "[");
636 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
638 i += scnprintf(buffer + i, count - i, ",");
639 i += cfs_range_expr_print(buffer + i, count - i, expr,
644 i += scnprintf(buffer + i, count - i, "]");
650 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
653 struct cfs_expr_list *el;
655 list_for_each_entry(el, list, el_link) {
658 i += scnprintf(buffer + i, count - i, ".");
659 i += cfs_expr_list_print(buffer + i, count - i, el);
665 * Matches address (\a addr) against address set encoded in \a list.
667 * \retval 1 if \a addr matches
668 * \retval 0 otherwise
671 cfs_ip_addr_match(__u32 addr, struct list_head *list)
673 struct cfs_expr_list *el;
676 list_for_each_entry_reverse(el, list, el_link) {
677 if (!cfs_expr_list_match(addr & 0xff, el))
687 * Print the network part of the nidrange \a nr into the specified \a buffer.
689 * \retval number of characters written
692 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
694 snprintf(str, size, "%u", addr);
698 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
703 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
707 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
711 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
718 * Nf_parse_addrlist method for networks using numeric addresses.
720 * Examples of such networks are gm and elan.
722 * \retval 0 if \a str parsed to numeric address
723 * \retval errno otherwise
726 libcfs_num_parse(char *str, int len, struct list_head *list)
728 struct cfs_expr_list *el;
731 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
733 list_add_tail(&el->el_link, list);
739 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
742 struct cfs_expr_list *el;
744 list_for_each_entry(el, list, el_link) {
746 i += cfs_expr_list_print(buffer + i, count - i, el);
752 * Nf_match_addr method for networks using numeric addresses
755 * \retval 0 otherwise
758 libcfs_num_match(__u32 addr, struct list_head *numaddr)
760 struct cfs_expr_list *el;
762 LASSERT(!list_empty(numaddr));
763 el = list_first_entry(numaddr, struct cfs_expr_list, el_link);
765 return cfs_expr_list_match(addr, el);
768 static struct netstrfns libcfs_netstrfns[] = {
771 .nf_modname = "klolnd",
772 .nf_addr2str = libcfs_decnum_addr2str,
773 .nf_str2addr = libcfs_lo_str2addr,
774 .nf_parse_addrlist = libcfs_num_parse,
775 .nf_print_addrlist = libcfs_num_addr_range_print,
776 .nf_match_addr = libcfs_num_match
778 { .nf_type = SOCKLND,
780 .nf_modname = "ksocklnd",
781 .nf_addr2str = libcfs_ip_addr2str,
782 .nf_addr2str_size = libcfs_ip_addr2str_size,
783 .nf_str2addr = libcfs_ip_str2addr,
784 .nf_str2addr_size = libcfs_ip_str2addr_size,
785 .nf_parse_addrlist = cfs_ip_addr_parse,
786 .nf_print_addrlist = libcfs_ip_addr_range_print,
787 .nf_match_addr = cfs_ip_addr_match
789 { .nf_type = O2IBLND,
791 .nf_modname = "ko2iblnd",
792 .nf_addr2str = libcfs_ip_addr2str,
793 .nf_str2addr = libcfs_ip_str2addr,
794 .nf_parse_addrlist = cfs_ip_addr_parse,
795 .nf_print_addrlist = libcfs_ip_addr_range_print,
796 .nf_match_addr = cfs_ip_addr_match
800 .nf_modname = "kgnilnd",
801 .nf_addr2str = libcfs_decnum_addr2str,
802 .nf_str2addr = libcfs_num_str2addr,
803 .nf_parse_addrlist = libcfs_num_parse,
804 .nf_print_addrlist = libcfs_num_addr_range_print,
805 .nf_match_addr = libcfs_num_match
807 { .nf_type = GNIIPLND,
809 .nf_modname = "kgnilnd",
810 .nf_addr2str = libcfs_ip_addr2str,
811 .nf_str2addr = libcfs_ip_str2addr,
812 .nf_parse_addrlist = cfs_ip_addr_parse,
813 .nf_print_addrlist = libcfs_ip_addr_range_print,
814 .nf_match_addr = cfs_ip_addr_match
816 { .nf_type = PTL4LND,
818 .nf_modname = "kptl4lnd",
819 .nf_addr2str = libcfs_decnum_addr2str,
820 .nf_str2addr = libcfs_num_str2addr,
821 .nf_parse_addrlist = libcfs_num_parse,
822 .nf_print_addrlist = libcfs_num_addr_range_print,
823 .nf_match_addr = libcfs_num_match
828 .nf_modname = "kkfilnd",
829 .nf_addr2str = libcfs_decnum_addr2str,
830 .nf_str2addr = libcfs_num_str2addr,
831 .nf_parse_addrlist = libcfs_num_parse,
832 .nf_print_addrlist = libcfs_num_addr_range_print,
833 .nf_match_addr = libcfs_num_match
837 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
839 static struct netstrfns *
840 type2net_info(__u32 net_type)
844 for (i = 0; i < libcfs_nnetstrfns; i++) {
845 if (libcfs_netstrfns[i].nf_type == net_type)
846 return &libcfs_netstrfns[i];
853 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
860 if (net_type != LNET_NETTYP(net_id))
863 net_num = LNET_NETNUM(net_id);
865 /* if there is a net number but the list passed in is empty, then
868 if (!net_num && list_empty(net_num_list))
870 else if (list_empty(net_num_list))
873 if (!libcfs_num_match(net_num, net_num_list))
880 cfs_match_nid_net(struct lnet_nid *nid, __u32 net_type,
881 struct list_head *net_num_list,
882 struct list_head *addr)
885 struct netstrfns *nf;
887 if (!addr || list_empty(addr) || !net_num_list)
890 nf = type2net_info(LNET_NETTYP(LNET_NID_NET(nid)));
894 /* FIXME handle long-addr nid */
895 address = LNET_NIDADDR(lnet_nid_to_nid4(nid));
897 /* if either the address or net number don't match then no match */
898 if (!nf->nf_match_addr(address, addr) ||
899 !cfs_match_net(LNET_NID_NET(nid), net_type, net_num_list))
904 EXPORT_SYMBOL(cfs_match_nid_net);
906 static struct netstrfns *
907 libcfs_lnd2netstrfns(__u32 lnd)
911 for (i = 0; i < libcfs_nnetstrfns; i++)
912 if (lnd == libcfs_netstrfns[i].nf_type)
913 return &libcfs_netstrfns[i];
918 static struct netstrfns *
919 libcfs_namenum2netstrfns(const char *name)
921 struct netstrfns *nf;
924 for (i = 0; i < libcfs_nnetstrfns; i++) {
925 nf = &libcfs_netstrfns[i];
926 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
932 static struct netstrfns *
933 libcfs_name2netstrfns(const char *name)
937 for (i = 0; i < libcfs_nnetstrfns; i++)
938 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
939 return &libcfs_netstrfns[i];
945 libcfs_isknown_lnd(__u32 lnd)
947 return libcfs_lnd2netstrfns(lnd) != NULL;
949 EXPORT_SYMBOL(libcfs_isknown_lnd);
952 libcfs_lnd2modname(__u32 lnd)
954 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
956 return (nf == NULL) ? NULL : nf->nf_modname;
958 EXPORT_SYMBOL(libcfs_lnd2modname);
961 libcfs_str2lnd(const char *str)
963 struct netstrfns *nf = libcfs_name2netstrfns(str);
970 EXPORT_SYMBOL(libcfs_str2lnd);
973 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
975 struct netstrfns *nf;
977 nf = libcfs_lnd2netstrfns(lnd);
979 snprintf(buf, buf_size, "?%u?", lnd);
981 snprintf(buf, buf_size, "%s", nf->nf_name);
985 EXPORT_SYMBOL(libcfs_lnd2str_r);
988 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
990 __u32 nnum = LNET_NETNUM(net);
991 __u32 lnd = LNET_NETTYP(net);
992 struct netstrfns *nf;
994 nf = libcfs_lnd2netstrfns(lnd);
996 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
998 snprintf(buf, buf_size, "%s", nf->nf_name);
1000 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1004 EXPORT_SYMBOL(libcfs_net2str_r);
1007 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1009 __u32 addr = LNET_NIDADDR(nid);
1010 __u32 net = LNET_NIDNET(nid);
1011 __u32 nnum = LNET_NETNUM(net);
1012 __u32 lnd = LNET_NETTYP(net);
1013 struct netstrfns *nf;
1015 if (nid == LNET_NID_ANY) {
1016 strncpy(buf, "<?>", buf_size);
1017 buf[buf_size - 1] = '\0';
1021 nf = libcfs_lnd2netstrfns(lnd);
1023 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1027 nf->nf_addr2str(addr, buf, buf_size);
1028 addr_len = strlen(buf);
1030 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1033 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1039 EXPORT_SYMBOL(libcfs_nid2str_r);
1042 libcfs_nidstr_r(const struct lnet_nid *nid, char *buf, size_t buf_size)
1046 struct netstrfns *nf;
1048 if (LNET_NID_IS_ANY(nid)) {
1049 strncpy(buf, "<?>", buf_size);
1050 buf[buf_size - 1] = '\0';
1054 nnum = be16_to_cpu(nid->nid_num);
1055 lnd = nid->nid_type;
1056 nf = libcfs_lnd2netstrfns(lnd);
1060 if (nf->nf_addr2str_size)
1061 nf->nf_addr2str_size(nid->nid_addr, NID_ADDR_BYTES(nid),
1064 nf->nf_addr2str(ntohl(nid->nid_addr[0]), buf, buf_size);
1065 addr_len = strlen(buf);
1067 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1070 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1074 int words = DIV_ROUND_UP(NID_ADDR_BYTES(nid), 4);
1077 for (i = 0; i < words && i < 4; i++)
1078 l = snprintf(buf+l, buf_size-l, "%s%x",
1079 i ? ":" : "", ntohl(nid->nid_addr[i]));
1080 snprintf(buf+l, buf_size-l, "@<%u:%u>", lnd, nnum);
1085 EXPORT_SYMBOL(libcfs_nidstr_r);
1087 static struct netstrfns *
1088 libcfs_str2net_internal(const char *str, __u32 *net)
1090 struct netstrfns *nf = NULL;
1092 unsigned int netnum;
1095 for (i = 0; i < libcfs_nnetstrfns; i++) {
1096 nf = &libcfs_netstrfns[i];
1097 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1101 if (i == libcfs_nnetstrfns)
1104 nob = strlen(nf->nf_name);
1106 if (strlen(str) == (unsigned int)nob) {
1109 if (nf->nf_type == LOLND) /* net number not allowed */
1114 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1115 i != (int)strlen(str))
1119 *net = LNET_MKNET(nf->nf_type, netnum);
1124 libcfs_str2net(const char *str)
1128 if (libcfs_str2net_internal(str, &net) != NULL)
1131 return LNET_NET_ANY;
1133 EXPORT_SYMBOL(libcfs_str2net);
1136 libcfs_str2nid(const char *str)
1138 const char *sep = strchr(str, '@');
1139 struct netstrfns *nf;
1144 nf = libcfs_str2net_internal(sep + 1, &net);
1146 return LNET_NID_ANY;
1148 sep = str + strlen(str);
1149 net = LNET_MKNET(SOCKLND, 0);
1150 nf = libcfs_lnd2netstrfns(SOCKLND);
1151 LASSERT(nf != NULL);
1154 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1155 return LNET_NID_ANY;
1157 return LNET_MKNID(net, addr);
1159 EXPORT_SYMBOL(libcfs_str2nid);
1162 libcfs_strnid(struct lnet_nid *nid, const char *str)
1164 const char *sep = strchr(str, '@');
1165 struct netstrfns *nf;
1169 nf = libcfs_str2net_internal(sep + 1, &net);
1173 if (strcmp(str, "<?>") == 0) {
1174 memcpy(nid, &LNET_ANY_NID, sizeof(*nid));
1177 sep = str + strlen(str);
1178 net = LNET_MKNET(SOCKLND, 0);
1179 nf = libcfs_lnd2netstrfns(SOCKLND);
1180 LASSERT(nf != NULL);
1183 memset(nid, 0, sizeof(*nid));
1184 nid->nid_type = LNET_NETTYP(net);
1185 nid->nid_num = htons(LNET_NETNUM(net));
1186 if (nf->nf_str2addr_size) {
1189 if (!nf->nf_str2addr_size(str, (int)(sep - str),
1190 nid->nid_addr, &asize))
1192 nid->nid_size = asize - 4;
1196 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1198 nid->nid_addr[0] = htonl(addr);
1203 EXPORT_SYMBOL(libcfs_strnid);
1206 libcfs_id2str(struct lnet_process_id id)
1208 char *str = libcfs_next_nidstring();
1210 if (id.pid == LNET_PID_ANY) {
1211 snprintf(str, LNET_NIDSTR_SIZE,
1212 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1216 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1217 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1218 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1221 EXPORT_SYMBOL(libcfs_id2str);
1224 libcfs_idstr(struct lnet_processid *id)
1226 char *str = libcfs_next_nidstring();
1228 if (id->pid == LNET_PID_ANY) {
1229 snprintf(str, LNET_NIDSTR_SIZE,
1230 "LNET_PID_ANY-%s", libcfs_nidstr(&id->nid));
1234 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1235 ((id->pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1236 (id->pid & ~LNET_PID_USERFLAG), libcfs_nidstr(&id->nid));
1239 EXPORT_SYMBOL(libcfs_idstr);
1242 libcfs_strid(struct lnet_processid *id, const char *str)
1244 char *tmp = strchr(str, '-');
1246 id->pid = LNET_PID_LUSTRE;
1248 strncmp("LNET_PID_ANY-", str, tmp - str) != 0) {
1249 char pid[LNET_NIDSTR_SIZE];
1252 strscpy(pid, str, tmp - str);
1253 rc = kstrtou32(pid, 10, &id->pid);
1261 return libcfs_strnid(&id->nid, tmp);
1263 EXPORT_SYMBOL(libcfs_strid);
1266 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1268 if (!strcmp(str, "*")) {
1269 *nidp = LNET_NID_ANY;
1273 *nidp = libcfs_str2nid(str);
1274 return *nidp != LNET_NID_ANY;
1276 EXPORT_SYMBOL(libcfs_str2anynid);