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 \<range_expr\> token of the syntax. If \a bracketed is false,
130 * \a src should only have a single token which can be \<number\> or \*
132 * \retval pointer to allocated range_expr and initialized
133 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
136 * \<number\> '-' \<number\> |
137 * \<number\> '-' \<number\> '/' \<number\>
138 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
139 * -ENOMEM will be returned.
142 cfs_range_expr_parse(char *src, unsigned int min, unsigned int max,
143 int bracketed, struct cfs_range_expr **expr)
145 struct cfs_range_expr *re;
149 LIBCFS_ALLOC(re, sizeof(*re));
154 if (strcmp(src, "*") == 0) {
161 if (kstrtouint(src, 0, &num) == 0) {
162 if (num < min || num > max)
164 /* <number> is parsed */
166 re->re_hi = re->re_lo;
173 tok = strim(strsep(&src, "-"));
176 if (kstrtouint(tok, 0, &num) != 0 ||
177 num < min || num > max)
182 if (kstrtouint(strim(src), 0, &num) == 0) {
183 if (num < min || num > max)
186 /* <number> - <number> is parsed */
191 /* go to check <number> '-' <number> '/' <number> */
192 tok = strim(strsep(&src, "/"));
195 if (kstrtouint(tok, 0, &num) != 0 ||
196 num < min || num > max)
199 if (kstrtouint(strim(src), 0, &num) != 0 ||
200 num < min || num > max)
209 LIBCFS_FREE(re, sizeof(*re));
214 * Matches value (\a value) against ranges expression list \a expr_list.
216 * \retval 1 if \a value matches
217 * \retval 0 otherwise
220 cfs_expr_list_match(u32 value, struct cfs_expr_list *expr_list)
222 struct cfs_range_expr *expr;
224 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
225 if (value >= expr->re_lo && value <= expr->re_hi &&
226 ((value - expr->re_lo) % expr->re_stride) == 0)
234 * Convert express list (\a expr_list) to an array of all matched values
236 * \retval N N is total number of all matched values
237 * \retval 0 if expression list is empty
238 * \retval < 0 for failure
241 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, u32 **valpp)
243 struct cfs_range_expr *expr;
248 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
249 for (i = expr->re_lo; i <= expr->re_hi; i++) {
250 if (((i - expr->re_lo) % expr->re_stride) == 0)
255 if (count == 0) /* empty expression list */
259 CERROR("Number of values %d exceeds max allowed %d\n",
264 CFS_ALLOC_PTR_ARRAY(val, count);
269 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
270 for (i = expr->re_lo; i <= expr->re_hi; i++) {
271 if (((i - expr->re_lo) % expr->re_stride) == 0)
279 EXPORT_SYMBOL(cfs_expr_list_values);
282 * Frees cfs_range_expr structures of \a expr_list.
287 cfs_expr_list_free(struct cfs_expr_list *expr_list)
289 while (!list_empty(&expr_list->el_exprs)) {
290 struct cfs_range_expr *expr;
292 expr = list_entry(expr_list->el_exprs.next,
293 struct cfs_range_expr, re_link);
294 list_del(&expr->re_link);
295 LIBCFS_FREE(expr, sizeof(*expr));
298 LIBCFS_FREE(expr_list, sizeof(*expr_list));
300 EXPORT_SYMBOL(cfs_expr_list_free);
303 * Parses \<cfs_expr_list\> token of the syntax.
305 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
306 * \retval -errno otherwise
309 cfs_expr_list_parse(char *str, int len, unsigned int min, unsigned int max,
310 struct cfs_expr_list **elpp)
312 struct cfs_expr_list *expr_list;
313 struct cfs_range_expr *expr;
317 CFS_ALLOC_PTR(expr_list);
321 str = kstrndup(str, len, GFP_KERNEL);
323 CFS_FREE_PTR(expr_list);
329 INIT_LIST_HEAD(&expr_list->el_exprs);
332 src[strlen(src) - 1] == ']') {
334 src[strlen(src)-1] = '\0';
338 char *tok = strsep(&src, ",");
346 rc = cfs_range_expr_parse(tok, min, max, 1, &expr);
350 list_add_tail(&expr->re_link, &expr_list->el_exprs);
353 rc = cfs_range_expr_parse(src, min, max, 0, &expr);
355 list_add_tail(&expr->re_link, &expr_list->el_exprs);
360 cfs_expr_list_free(expr_list);
366 EXPORT_SYMBOL(cfs_expr_list_parse);
369 * Frees cfs_expr_list structures of \a list.
371 * For each struct cfs_expr_list structure found on \a list it frees
372 * range_expr list attached to it and frees the cfs_expr_list itself.
377 cfs_expr_list_free_list(struct list_head *list)
379 struct cfs_expr_list *el;
381 while (!list_empty(list)) {
382 el = list_first_entry(list,
383 struct cfs_expr_list, el_link);
384 list_del(&el->el_link);
385 cfs_expr_list_free(el);
390 * Parses \<addrrange\> token on the syntax.
392 * Allocates struct addrrange and links to \a nidrange via
393 * (nidrange::nr_addrranges)
395 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
396 * \retval -errno otherwise
399 parse_addrange(char *str, struct nidrange *nidrange)
401 struct addrrange *addrrange;
403 if (strcmp(str, "*") == 0) {
404 nidrange->nr_all = 1;
408 CFS_ALLOC_PTR(addrrange);
409 if (addrrange == NULL)
411 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
412 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
414 return nidrange->nr_netstrfns->nf_parse_addrlist(str, strlen(str),
415 &addrrange->ar_numaddr_ranges);
419 * Finds or creates struct nidrange.
421 * Checks if \a src is a valid network name, looks for corresponding
422 * nidrange on the ist of nidranges (\a nidlist), creates new struct
423 * nidrange if it is not found.
425 * \retval pointer to struct nidrange matching network specified via \a src
426 * \retval NULL if \a src does not match any network
428 static struct nidrange *
429 add_nidrange(char *str, struct list_head *nidlist)
431 struct netstrfns *nf;
436 nf = libcfs_namenum2netstrfns(str);
439 end = str + strlen(nf->nf_name);
441 /* network name only, e.g. "elan" or "tcp" */
444 /* e.g. "elan25" or "tcp23", refuse to parse if
445 * network name is not appended with decimal or
448 if (kstrtouint(end, 0, &netnum) != 0)
452 list_for_each_entry(nr, nidlist, nr_link) {
453 if (nr->nr_netstrfns != nf)
455 if (nr->nr_netnum != netnum)
463 list_add_tail(&nr->nr_link, nidlist);
464 INIT_LIST_HEAD(&nr->nr_addrranges);
465 nr->nr_netstrfns = nf;
467 nr->nr_netnum = netnum;
473 * Parses \<nidrange\> token of the syntax.
475 * \retval 0 if \a src parses to \<addrrange\> '@' \<net\>
476 * \retval -EINVAL otherwise
479 parse_nidrange(char *str, struct list_head *nidlist)
485 addrrange = strim(strsep(&str, "@"));
490 if (strchr(net, '@') != NULL || !*net)
493 nr = add_nidrange(net, nidlist);
497 if (parse_addrange(addrrange, nr) != 0)
506 * Frees addrrange structures of \a list.
508 * For each struct addrrange structure found on \a list it frees
509 * cfs_expr_list list attached to it and frees the addrrange itself.
514 free_addrranges(struct list_head *list)
516 struct addrrange *ar;
518 while ((ar = list_first_entry_or_null(list,
521 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
522 list_del(&ar->ar_link);
528 * Frees nidrange strutures of \a list.
530 * For each struct nidrange structure found on \a list it frees
531 * addrrange list attached to it and frees the nidrange itself.
536 cfs_free_nidlist(struct list_head *list)
538 struct list_head *pos, *next;
541 list_for_each_safe(pos, next, list) {
542 nr = list_entry(pos, struct nidrange, nr_link);
543 free_addrranges(&nr->nr_addrranges);
548 EXPORT_SYMBOL(cfs_free_nidlist);
551 * Parses nid range list.
553 * Parses with rigorous syntax and overflow checking \a str into
554 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
555 * structures and links that structure to \a nidlist. The resulting
556 * list can be used to match a NID againts set of NIDS defined by \a
560 * \retval 0 on success
561 * \retval -errno otherwise (-ENOMEM or -EINVAL)
564 cfs_parse_nidlist(char *orig, struct list_head *nidlist)
569 orig = kstrdup(orig, GFP_KERNEL);
573 INIT_LIST_HEAD(nidlist);
575 while (rc == 0 && str) {
576 char *tok = strsep(&str, " ");
579 rc = parse_nidrange(tok, nidlist);
583 cfs_free_nidlist(nidlist);
584 else if (list_empty(nidlist))
588 EXPORT_SYMBOL(cfs_parse_nidlist);
591 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
593 * \see cfs_parse_nidlist()
596 * \retval 0 otherwises
598 int cfs_match_nid(struct lnet_nid *nid, struct list_head *nidlist)
601 struct addrrange *ar;
603 if (!nid_is_nid4(nid))
605 list_for_each_entry(nr, nidlist, nr_link) {
606 if (nr->nr_netstrfns->nf_type != nid->nid_type)
608 if (nr->nr_netnum != be16_to_cpu(nid->nid_num))
612 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
613 if (nr->nr_netstrfns->nf_match_addr(
614 be32_to_cpu(nid->nid_addr[0]),
615 &ar->ar_numaddr_ranges))
620 EXPORT_SYMBOL(cfs_match_nid);
623 * Print the network part of the nidrange \a nr into the specified \a buffer.
625 * \retval number of characters written
628 cfs_print_network(char *buffer, int count, struct nidrange *nr)
630 struct netstrfns *nf = nr->nr_netstrfns;
632 if (nr->nr_netnum == 0)
633 return scnprintf(buffer, count, "@%s", nf->nf_name);
635 return scnprintf(buffer, count, "@%s%u",
636 nf->nf_name, nr->nr_netnum);
640 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
641 * At max \a count characters can be printed into \a buffer.
643 * \retval number of characters written
646 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
650 struct addrrange *ar;
651 struct netstrfns *nf = nr->nr_netstrfns;
653 list_for_each_entry(ar, addrranges, ar_link) {
655 i += scnprintf(buffer + i, count - i, " ");
656 i += nf->nf_print_addrlist(buffer + i, count - i,
657 &ar->ar_numaddr_ranges);
658 i += cfs_print_network(buffer + i, count - i, nr);
664 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
665 * At max \a count characters can be printed into \a buffer.
666 * Nidranges are separated by a space character.
668 * \retval number of characters written
670 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
678 list_for_each_entry(nr, nidlist, nr_link) {
680 i += scnprintf(buffer + i, count - i, " ");
682 if (nr->nr_all != 0) {
683 LASSERT(list_empty(&nr->nr_addrranges));
684 i += scnprintf(buffer + i, count - i, "*");
685 i += cfs_print_network(buffer + i, count - i, nr);
687 i += cfs_print_addrranges(buffer + i, count - i,
688 &nr->nr_addrranges, nr);
693 EXPORT_SYMBOL(cfs_print_nidlist);
696 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
703 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
705 snprintf(str, size, "%u.%u.%u.%u",
706 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
707 (addr >> 8) & 0xff, addr & 0xff);
711 libcfs_ip_addr2str_size(const __be32 *addr, size_t asize,
712 char *str, size_t size)
714 struct sockaddr_storage sa = {};
718 sa.ss_family = AF_INET;
719 memcpy(&((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
723 sa.ss_family = AF_INET6;
724 memcpy(&((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
731 rpc_ntop((struct sockaddr *)&sa, str, size);
734 /* CAVEAT EMPTOR XscanfX
735 * I use "%n" at the end of a sscanf format to detect trailing junk. However
736 * sscanf may return immediately if it sees the terminating '0' in a string, so
737 * I initialise the %n variable to the expected length. If sscanf sets it;
738 * fine, if it doesn't, then the scan ended at the end of the string, which is
741 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
747 int n = nob; /* XscanfX */
750 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
752 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
753 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
754 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
761 libcfs_ip_str2addr_size(const char *str, int nob,
762 __be32 *addr, size_t *alen)
764 struct sockaddr_storage sa;
766 /* Note: 'net' arg to rpc_pton is only needed for link-local
767 * addresses. Such addresses would not work with LNet routing,
768 * so we can assume they aren't used. So it doesn't matter
769 * which net namespace is passed.
771 if (rpc_pton(&init_net, str, nob,
772 (struct sockaddr *)&sa, sizeof(sa)) == 0)
774 if (sa.ss_family == AF_INET6) {
776 &((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
781 if (sa.ss_family == AF_INET) {
783 &((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
792 /* Used by lnet/config.c so it can't be static */
794 cfs_ip_addr_parse(char *str, int len_ignored, struct list_head *list)
796 struct cfs_expr_list *el;
801 while (rc == 0 && str) {
804 res = strsep(&str, ".");
808 } else if ((rc = cfs_expr_list_parse(res, strlen(res),
809 0, 255, &el)) == 0) {
810 list_add_tail(&el->el_link, list);
815 if (rc == 0 && i == 4)
817 cfs_expr_list_free_list(list);
819 return rc ?: -EINVAL;
823 * Print the range expression \a re into specified \a buffer.
824 * If \a bracketed is true, expression does not need additional
827 * \retval number of characters written
830 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
840 if (expr->re_lo == expr->re_hi)
841 i = scnprintf(buffer, count, "%u", expr->re_lo);
842 else if (expr->re_stride == 1)
843 i = scnprintf(buffer, count, "%s%u-%u%s",
844 s, expr->re_lo, expr->re_hi, e);
846 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
847 s, expr->re_lo, expr->re_hi,
853 * Print a list of range expressions (\a expr_list) into specified \a buffer.
854 * If the list contains several expressions, separate them with comma
855 * and surround the list with brackets.
857 * \retval number of characters written
860 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
862 struct cfs_range_expr *expr;
869 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
873 i += scnprintf(buffer + i, count - i, "[");
875 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
877 i += scnprintf(buffer + i, count - i, ",");
878 i += cfs_range_expr_print(buffer + i, count - i, expr,
883 i += scnprintf(buffer + i, count - i, "]");
889 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
892 struct cfs_expr_list *el;
894 list_for_each_entry(el, list, el_link) {
897 i += scnprintf(buffer + i, count - i, ".");
898 i += cfs_expr_list_print(buffer + i, count - i, el);
904 * Matches address (\a addr) against address set encoded in \a list.
906 * \retval 1 if \a addr matches
907 * \retval 0 otherwise
910 cfs_ip_addr_match(__u32 addr, struct list_head *list)
912 struct cfs_expr_list *el;
915 list_for_each_entry_reverse(el, list, el_link) {
916 if (!cfs_expr_list_match(addr & 0xff, el))
926 * Print the network part of the nidrange \a nr into the specified \a buffer.
928 * \retval number of characters written
931 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
933 snprintf(str, size, "%u", addr);
937 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
942 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
946 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
950 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
957 * Nf_parse_addrlist method for networks using numeric addresses.
959 * Examples of such networks are gm and elan.
961 * \retval 0 if \a str parsed to numeric address
962 * \retval errno otherwise
965 libcfs_num_parse(char *str, int len, struct list_head *list)
967 struct cfs_expr_list *el;
970 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
972 list_add_tail(&el->el_link, list);
978 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
981 struct cfs_expr_list *el;
983 list_for_each_entry(el, list, el_link) {
985 i += cfs_expr_list_print(buffer + i, count - i, el);
991 * Nf_match_addr method for networks using numeric addresses
994 * \retval 0 otherwise
997 libcfs_num_match(__u32 addr, struct list_head *numaddr)
999 struct cfs_expr_list *el;
1001 LASSERT(!list_empty(numaddr));
1002 el = list_first_entry(numaddr, struct cfs_expr_list, el_link);
1004 return cfs_expr_list_match(addr, el);
1007 static struct netstrfns libcfs_netstrfns[] = {
1010 .nf_modname = "klolnd",
1011 .nf_addr2str = libcfs_decnum_addr2str,
1012 .nf_str2addr = libcfs_lo_str2addr,
1013 .nf_parse_addrlist = libcfs_num_parse,
1014 .nf_print_addrlist = libcfs_num_addr_range_print,
1015 .nf_match_addr = libcfs_num_match
1017 { .nf_type = SOCKLND,
1019 .nf_modname = "ksocklnd",
1020 .nf_addr2str = libcfs_ip_addr2str,
1021 .nf_addr2str_size = libcfs_ip_addr2str_size,
1022 .nf_str2addr = libcfs_ip_str2addr,
1023 .nf_str2addr_size = libcfs_ip_str2addr_size,
1024 .nf_parse_addrlist = cfs_ip_addr_parse,
1025 .nf_print_addrlist = libcfs_ip_addr_range_print,
1026 .nf_match_addr = cfs_ip_addr_match
1028 { .nf_type = O2IBLND,
1030 .nf_modname = "ko2iblnd",
1031 .nf_addr2str = libcfs_ip_addr2str,
1032 .nf_str2addr = libcfs_ip_str2addr,
1033 .nf_parse_addrlist = cfs_ip_addr_parse,
1034 .nf_print_addrlist = libcfs_ip_addr_range_print,
1035 .nf_match_addr = cfs_ip_addr_match
1037 { .nf_type = GNILND,
1039 .nf_modname = "kgnilnd",
1040 .nf_addr2str = libcfs_decnum_addr2str,
1041 .nf_str2addr = libcfs_num_str2addr,
1042 .nf_parse_addrlist = libcfs_num_parse,
1043 .nf_print_addrlist = libcfs_num_addr_range_print,
1044 .nf_match_addr = libcfs_num_match
1046 { .nf_type = GNIIPLND,
1048 .nf_modname = "kgnilnd",
1049 .nf_addr2str = libcfs_ip_addr2str,
1050 .nf_str2addr = libcfs_ip_str2addr,
1051 .nf_parse_addrlist = cfs_ip_addr_parse,
1052 .nf_print_addrlist = libcfs_ip_addr_range_print,
1053 .nf_match_addr = cfs_ip_addr_match
1055 { .nf_type = PTL4LND,
1057 .nf_modname = "kptl4lnd",
1058 .nf_addr2str = libcfs_decnum_addr2str,
1059 .nf_str2addr = libcfs_num_str2addr,
1060 .nf_parse_addrlist = libcfs_num_parse,
1061 .nf_print_addrlist = libcfs_num_addr_range_print,
1062 .nf_match_addr = libcfs_num_match
1067 .nf_modname = "kkfilnd",
1068 .nf_addr2str = libcfs_decnum_addr2str,
1069 .nf_str2addr = libcfs_num_str2addr,
1070 .nf_parse_addrlist = libcfs_num_parse,
1071 .nf_print_addrlist = libcfs_num_addr_range_print,
1072 .nf_match_addr = libcfs_num_match
1076 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
1078 static struct netstrfns *
1079 type2net_info(__u32 net_type)
1083 for (i = 0; i < libcfs_nnetstrfns; i++) {
1084 if (libcfs_netstrfns[i].nf_type == net_type)
1085 return &libcfs_netstrfns[i];
1092 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
1099 if (net_type != LNET_NETTYP(net_id))
1102 net_num = LNET_NETNUM(net_id);
1104 /* if there is a net number but the list passed in is empty, then
1105 * there is no match.
1107 if (!net_num && list_empty(net_num_list))
1109 else if (list_empty(net_num_list))
1112 if (!libcfs_num_match(net_num, net_num_list))
1119 cfs_match_nid_net(struct lnet_nid *nid, __u32 net_type,
1120 struct list_head *net_num_list,
1121 struct list_head *addr)
1124 struct netstrfns *nf;
1126 if (!addr || list_empty(addr) || !net_num_list)
1129 nf = type2net_info(LNET_NETTYP(LNET_NID_NET(nid)));
1133 /* FIXME handle long-addr nid */
1134 address = LNET_NIDADDR(lnet_nid_to_nid4(nid));
1136 /* if either the address or net number don't match then no match */
1137 if (!nf->nf_match_addr(address, addr) ||
1138 !cfs_match_net(LNET_NID_NET(nid), net_type, net_num_list))
1143 EXPORT_SYMBOL(cfs_match_nid_net);
1145 static struct netstrfns *
1146 libcfs_lnd2netstrfns(__u32 lnd)
1150 for (i = 0; i < libcfs_nnetstrfns; i++)
1151 if (lnd == libcfs_netstrfns[i].nf_type)
1152 return &libcfs_netstrfns[i];
1157 static struct netstrfns *
1158 libcfs_namenum2netstrfns(const char *name)
1160 struct netstrfns *nf;
1163 for (i = 0; i < libcfs_nnetstrfns; i++) {
1164 nf = &libcfs_netstrfns[i];
1165 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
1171 static struct netstrfns *
1172 libcfs_name2netstrfns(const char *name)
1176 for (i = 0; i < libcfs_nnetstrfns; i++)
1177 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
1178 return &libcfs_netstrfns[i];
1184 libcfs_isknown_lnd(__u32 lnd)
1186 return libcfs_lnd2netstrfns(lnd) != NULL;
1188 EXPORT_SYMBOL(libcfs_isknown_lnd);
1191 libcfs_lnd2modname(__u32 lnd)
1193 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
1195 return (nf == NULL) ? NULL : nf->nf_modname;
1197 EXPORT_SYMBOL(libcfs_lnd2modname);
1200 libcfs_str2lnd(const char *str)
1202 struct netstrfns *nf = libcfs_name2netstrfns(str);
1209 EXPORT_SYMBOL(libcfs_str2lnd);
1212 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
1214 struct netstrfns *nf;
1216 nf = libcfs_lnd2netstrfns(lnd);
1218 snprintf(buf, buf_size, "?%u?", lnd);
1220 snprintf(buf, buf_size, "%s", nf->nf_name);
1224 EXPORT_SYMBOL(libcfs_lnd2str_r);
1227 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
1229 __u32 nnum = LNET_NETNUM(net);
1230 __u32 lnd = LNET_NETTYP(net);
1231 struct netstrfns *nf;
1233 nf = libcfs_lnd2netstrfns(lnd);
1235 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
1237 snprintf(buf, buf_size, "%s", nf->nf_name);
1239 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1243 EXPORT_SYMBOL(libcfs_net2str_r);
1246 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1248 __u32 addr = LNET_NIDADDR(nid);
1249 __u32 net = LNET_NIDNET(nid);
1250 __u32 nnum = LNET_NETNUM(net);
1251 __u32 lnd = LNET_NETTYP(net);
1252 struct netstrfns *nf;
1254 if (nid == LNET_NID_ANY) {
1255 strncpy(buf, "<?>", buf_size);
1256 buf[buf_size - 1] = '\0';
1260 nf = libcfs_lnd2netstrfns(lnd);
1262 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1266 nf->nf_addr2str(addr, buf, buf_size);
1267 addr_len = strlen(buf);
1269 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1272 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1278 EXPORT_SYMBOL(libcfs_nid2str_r);
1281 libcfs_nidstr_r(const struct lnet_nid *nid, char *buf, size_t buf_size)
1285 struct netstrfns *nf;
1287 if (LNET_NID_IS_ANY(nid)) {
1288 strncpy(buf, "<?>", buf_size);
1289 buf[buf_size - 1] = '\0';
1293 nnum = be16_to_cpu(nid->nid_num);
1294 lnd = nid->nid_type;
1295 nf = libcfs_lnd2netstrfns(lnd);
1299 if (nf->nf_addr2str_size)
1300 nf->nf_addr2str_size(nid->nid_addr, NID_ADDR_BYTES(nid),
1303 nf->nf_addr2str(ntohl(nid->nid_addr[0]), buf, buf_size);
1304 addr_len = strlen(buf);
1306 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1309 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1313 int words = DIV_ROUND_UP(NID_ADDR_BYTES(nid), 4);
1316 for (i = 0; i < words && i < 4; i++)
1317 l = snprintf(buf+l, buf_size-l, "%s%x",
1318 i ? ":" : "", ntohl(nid->nid_addr[i]));
1319 snprintf(buf+l, buf_size-l, "@<%u:%u>", lnd, nnum);
1324 EXPORT_SYMBOL(libcfs_nidstr_r);
1326 static struct netstrfns *
1327 libcfs_str2net_internal(const char *str, __u32 *net)
1329 struct netstrfns *nf = NULL;
1331 unsigned int netnum;
1334 for (i = 0; i < libcfs_nnetstrfns; i++) {
1335 nf = &libcfs_netstrfns[i];
1336 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1340 if (i == libcfs_nnetstrfns)
1343 nob = strlen(nf->nf_name);
1345 if (strlen(str) == (unsigned int)nob) {
1348 if (nf->nf_type == LOLND) /* net number not allowed */
1353 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1354 i != (int)strlen(str))
1358 *net = LNET_MKNET(nf->nf_type, netnum);
1363 libcfs_str2net(const char *str)
1367 if (libcfs_str2net_internal(str, &net) != NULL)
1370 return LNET_NET_ANY;
1372 EXPORT_SYMBOL(libcfs_str2net);
1375 libcfs_str2nid(const char *str)
1377 const char *sep = strchr(str, '@');
1378 struct netstrfns *nf;
1383 nf = libcfs_str2net_internal(sep + 1, &net);
1385 return LNET_NID_ANY;
1387 sep = str + strlen(str);
1388 net = LNET_MKNET(SOCKLND, 0);
1389 nf = libcfs_lnd2netstrfns(SOCKLND);
1390 LASSERT(nf != NULL);
1393 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1394 return LNET_NID_ANY;
1396 return LNET_MKNID(net, addr);
1398 EXPORT_SYMBOL(libcfs_str2nid);
1401 libcfs_strnid(struct lnet_nid *nid, const char *str)
1403 const char *sep = strchr(str, '@');
1404 struct netstrfns *nf;
1408 nf = libcfs_str2net_internal(sep + 1, &net);
1412 if (strcmp(str, "<?>") == 0) {
1413 memcpy(nid, &LNET_ANY_NID, sizeof(*nid));
1416 sep = str + strlen(str);
1417 net = LNET_MKNET(SOCKLND, 0);
1418 nf = libcfs_lnd2netstrfns(SOCKLND);
1419 LASSERT(nf != NULL);
1422 memset(nid, 0, sizeof(*nid));
1423 nid->nid_type = LNET_NETTYP(net);
1424 nid->nid_num = htons(LNET_NETNUM(net));
1425 if (nf->nf_str2addr_size) {
1428 if (!nf->nf_str2addr_size(str, (int)(sep - str),
1429 nid->nid_addr, &asize))
1431 nid->nid_size = asize - 4;
1435 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1437 nid->nid_addr[0] = htonl(addr);
1442 EXPORT_SYMBOL(libcfs_strnid);
1445 libcfs_id2str(struct lnet_process_id id)
1447 char *str = libcfs_next_nidstring();
1449 if (id.pid == LNET_PID_ANY) {
1450 snprintf(str, LNET_NIDSTR_SIZE,
1451 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1455 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1456 ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1457 (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1460 EXPORT_SYMBOL(libcfs_id2str);
1463 libcfs_idstr(struct lnet_processid *id)
1465 char *str = libcfs_next_nidstring();
1467 if (id->pid == LNET_PID_ANY) {
1468 snprintf(str, LNET_NIDSTR_SIZE,
1469 "LNET_PID_ANY-%s", libcfs_nidstr(&id->nid));
1473 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1474 ((id->pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1475 (id->pid & ~LNET_PID_USERFLAG), libcfs_nidstr(&id->nid));
1478 EXPORT_SYMBOL(libcfs_idstr);
1481 libcfs_strid(struct lnet_processid *id, const char *str)
1483 char *tmp = strchr(str, '-');
1485 id->pid = LNET_PID_LUSTRE;
1487 strncmp("LNET_PID_ANY-", str, tmp - str) != 0) {
1488 char pid[LNET_NIDSTR_SIZE];
1491 strscpy(pid, str, tmp - str);
1492 rc = kstrtou32(pid, 10, &id->pid);
1500 return libcfs_strnid(&id->nid, tmp);
1502 EXPORT_SYMBOL(libcfs_strid);
1505 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1507 if (!strcmp(str, "*")) {
1508 *nidp = LNET_NID_ANY;
1512 *nidp = libcfs_str2nid(str);
1513 return *nidp != LNET_NID_ANY;
1515 EXPORT_SYMBOL(libcfs_str2anynid);