4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/lnet/nidstrings.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include "../../include/linux/libcfs/libcfs.h"
44 #include "../../include/linux/lnet/lnet.h"
46 /* max value for numeric network address */
47 #define MAX_NUMERIC_VALUE 0xffffffff
49 #define IPSTRING_LENGTH 16
51 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
52 * consistent in all conversion functions. Some code fragments are copied
53 * around for the sake of clarity...
56 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
57 * Choose the number of nidstrings to support the MAXIMUM expected number of
58 * concurrent users. If there are more, the returned string will be volatile.
59 * NB this number must allow for a process to be descheduled for a timeslice
60 * between getting its string and using it.
63 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
64 static int libcfs_nidstring_idx;
66 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
68 static struct netstrfns *libcfs_namenum2netstrfns(const char *name);
71 libcfs_next_nidstring(void)
76 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
78 str = libcfs_nidstrings[libcfs_nidstring_idx++];
79 if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
80 libcfs_nidstring_idx = 0;
82 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
85 EXPORT_SYMBOL(libcfs_next_nidstring);
88 * Nid range list syntax.
91 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
92 * <nidrange> :== <addrrange> '@' <net>
93 * <addrrange> :== '*' |
96 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
98 * <cfs_expr_list> :== <number> |
100 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
101 * <range_expr> :== <number> |
102 * <number> '-' <number> |
103 * <number> '-' <number> '/' <number>
104 * <net> :== <netname> | <netname><number>
105 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
106 * "vib" | "ra" | "elan" | "mx" | "ptl"
111 * Structure to represent \<nidrange\> token of the syntax.
113 * One of this is created for each \<net\> parsed.
117 * Link to list of this structures which is built on nid range
120 struct list_head nr_link;
122 * List head for addrrange::ar_link.
124 struct list_head nr_addrranges;
126 * Flag indicating that *@<net> is found.
130 * Pointer to corresponding element of libcfs_netstrfns.
132 struct netstrfns *nr_netstrfns;
134 * Number of network. E.g. 5 if \<net\> is "elan5".
140 * Structure to represent \<addrrange\> token of the syntax.
144 * Link to nidrange::nr_addrranges.
146 struct list_head ar_link;
148 * List head for cfs_expr_list::el_list.
150 struct list_head ar_numaddr_ranges;
154 * Parses \<addrrange\> token on the syntax.
156 * Allocates struct addrrange and links to \a nidrange via
157 * (nidrange::nr_addrranges)
159 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
160 * \retval -errno otherwise
163 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
165 struct addrrange *addrrange;
167 if (src->ls_len == 1 && src->ls_str[0] == '*') {
168 nidrange->nr_all = 1;
172 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
175 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
176 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
178 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
180 &addrrange->ar_numaddr_ranges);
184 * Finds or creates struct nidrange.
186 * Checks if \a src is a valid network name, looks for corresponding
187 * nidrange on the ist of nidranges (\a nidlist), creates new struct
188 * nidrange if it is not found.
190 * \retval pointer to struct nidrange matching network specified via \a src
191 * \retval NULL if \a src does not match any network
193 static struct nidrange *
194 add_nidrange(const struct cfs_lstr *src,
195 struct list_head *nidlist)
197 struct netstrfns *nf;
202 if (src->ls_len >= LNET_NIDSTR_SIZE)
205 nf = libcfs_namenum2netstrfns(src->ls_str);
208 endlen = src->ls_len - strlen(nf->nf_name);
210 /* network name only, e.g. "elan" or "tcp" */
214 * e.g. "elan25" or "tcp23", refuse to parse if
215 * network name is not appended with decimal or
218 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
219 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
223 list_for_each_entry(nr, nidlist, nr_link) {
224 if (nr->nr_netstrfns != nf)
226 if (nr->nr_netnum != netnum)
231 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
234 list_add_tail(&nr->nr_link, nidlist);
235 INIT_LIST_HEAD(&nr->nr_addrranges);
236 nr->nr_netstrfns = nf;
238 nr->nr_netnum = netnum;
244 * Parses \<nidrange\> token of the syntax.
246 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
247 * \retval 0 otherwise
250 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
252 struct cfs_lstr addrrange;
258 if (!cfs_gettok(src, '@', &addrrange))
261 if (!cfs_gettok(src, '@', &net) || src->ls_str)
264 nr = add_nidrange(&net, nidlist);
268 if (parse_addrange(&addrrange, nr))
277 * Frees addrrange structures of \a list.
279 * For each struct addrrange structure found on \a list it frees
280 * cfs_expr_list list attached to it and frees the addrrange itself.
285 free_addrranges(struct list_head *list)
287 while (!list_empty(list)) {
288 struct addrrange *ar;
290 ar = list_entry(list->next, struct addrrange, ar_link);
292 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
293 list_del(&ar->ar_link);
294 LIBCFS_FREE(ar, sizeof(struct addrrange));
299 * Frees nidrange strutures of \a list.
301 * For each struct nidrange structure found on \a list it frees
302 * addrrange list attached to it and frees the nidrange itself.
307 cfs_free_nidlist(struct list_head *list)
309 struct list_head *pos, *next;
312 list_for_each_safe(pos, next, list) {
313 nr = list_entry(pos, struct nidrange, nr_link);
314 free_addrranges(&nr->nr_addrranges);
316 LIBCFS_FREE(nr, sizeof(struct nidrange));
319 EXPORT_SYMBOL(cfs_free_nidlist);
322 * Parses nid range list.
324 * Parses with rigorous syntax and overflow checking \a str into
325 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
326 * structures and links that structure to \a nidlist. The resulting
327 * list can be used to match a NID againts set of NIDS defined by \a
331 * \retval 1 on success
332 * \retval 0 otherwise
335 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
343 INIT_LIST_HEAD(nidlist);
345 rc = cfs_gettok(&src, ' ', &res);
347 cfs_free_nidlist(nidlist);
350 rc = parse_nidrange(&res, nidlist);
352 cfs_free_nidlist(nidlist);
358 EXPORT_SYMBOL(cfs_parse_nidlist);
361 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
363 * \see cfs_parse_nidlist()
366 * \retval 0 otherwises
368 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
371 struct addrrange *ar;
373 list_for_each_entry(nr, nidlist, nr_link) {
374 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
376 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
380 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
381 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
382 &ar->ar_numaddr_ranges))
387 EXPORT_SYMBOL(cfs_match_nid);
390 * Print the network part of the nidrange \a nr into the specified \a buffer.
392 * \retval number of characters written
395 cfs_print_network(char *buffer, int count, struct nidrange *nr)
397 struct netstrfns *nf = nr->nr_netstrfns;
400 return scnprintf(buffer, count, "@%s", nf->nf_name);
402 return scnprintf(buffer, count, "@%s%u",
403 nf->nf_name, nr->nr_netnum);
407 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
408 * At max \a count characters can be printed into \a buffer.
410 * \retval number of characters written
413 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
417 struct addrrange *ar;
418 struct netstrfns *nf = nr->nr_netstrfns;
420 list_for_each_entry(ar, addrranges, ar_link) {
422 i += scnprintf(buffer + i, count - i, " ");
423 i += nf->nf_print_addrlist(buffer + i, count - i,
424 &ar->ar_numaddr_ranges);
425 i += cfs_print_network(buffer + i, count - i, nr);
431 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
432 * At max \a count characters can be printed into \a buffer.
433 * Nidranges are separated by a space character.
435 * \retval number of characters written
437 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
445 list_for_each_entry(nr, nidlist, nr_link) {
447 i += scnprintf(buffer + i, count - i, " ");
450 LASSERT(list_empty(&nr->nr_addrranges));
451 i += scnprintf(buffer + i, count - i, "*");
452 i += cfs_print_network(buffer + i, count - i, nr);
454 i += cfs_print_addrranges(buffer + i, count - i,
455 &nr->nr_addrranges, nr);
460 EXPORT_SYMBOL(cfs_print_nidlist);
463 * Determines minimum and maximum addresses for a single
464 * numeric address range
470 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
473 struct cfs_expr_list *el;
474 struct cfs_range_expr *re;
475 __u32 tmp_ip_addr = 0;
476 unsigned int min_ip[4] = {0};
477 unsigned int max_ip[4] = {0};
480 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
481 list_for_each_entry(re, &el->el_exprs, re_link) {
482 min_ip[re_count] = re->re_lo;
483 max_ip[re_count] = re->re_hi;
488 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
489 (min_ip[2] << 8) | min_ip[3]);
492 *min_nid = tmp_ip_addr;
494 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
495 (max_ip[2] << 8) | max_ip[3]);
498 *max_nid = tmp_ip_addr;
502 * Determines minimum and maximum addresses for a single
503 * numeric address range
509 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
512 struct cfs_expr_list *el;
513 struct cfs_range_expr *re;
514 unsigned int min_addr = 0;
515 unsigned int max_addr = 0;
517 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
518 list_for_each_entry(re, &el->el_exprs, re_link) {
519 if (re->re_lo < min_addr || !min_addr)
520 min_addr = re->re_lo;
521 if (re->re_hi > max_addr)
522 max_addr = re->re_hi;
533 * Determines whether an expression list in an nidrange contains exactly
534 * one contiguous address range. Calls the correct netstrfns for the LND
538 * \retval true if contiguous
539 * \retval false if not contiguous
541 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
544 struct netstrfns *nf = NULL;
545 char *lndname = NULL;
548 list_for_each_entry(nr, nidlist, nr_link) {
549 nf = nr->nr_netstrfns;
551 lndname = nf->nf_name;
553 netnum = nr->nr_netnum;
555 if (strcmp(lndname, nf->nf_name) ||
556 netnum != nr->nr_netnum)
563 if (!nf->nf_is_contiguous(nidlist))
568 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
571 * Determines whether an expression list in an num nidrange contains exactly
572 * one contiguous address range.
576 * \retval true if contiguous
577 * \retval false if not contiguous
579 static bool cfs_num_is_contiguous(struct list_head *nidlist)
582 struct addrrange *ar;
583 struct cfs_expr_list *el;
584 struct cfs_range_expr *re;
586 __u32 last_end_nid = 0;
587 __u32 current_start_nid = 0;
588 __u32 current_end_nid = 0;
590 list_for_each_entry(nr, nidlist, nr_link) {
591 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
592 cfs_num_ar_min_max(ar, ¤t_start_nid,
595 (current_start_nid - last_end_nid != 1))
597 last_end_nid = current_end_nid;
598 list_for_each_entry(el, &ar->ar_numaddr_ranges,
600 list_for_each_entry(re, &el->el_exprs,
602 if (re->re_stride > 1)
605 re->re_hi - last_hi != 1)
617 * Determines whether an expression list in an ip nidrange contains exactly
618 * one contiguous address range.
622 * \retval true if contiguous
623 * \retval false if not contiguous
625 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
628 struct addrrange *ar;
629 struct cfs_expr_list *el;
630 struct cfs_range_expr *re;
634 __u32 last_end_nid = 0;
635 __u32 current_start_nid = 0;
636 __u32 current_end_nid = 0;
638 list_for_each_entry(nr, nidlist, nr_link) {
639 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
642 cfs_ip_ar_min_max(ar, ¤t_start_nid,
645 (current_start_nid - last_end_nid != 1))
647 last_end_nid = current_end_nid;
648 list_for_each_entry(el, &ar->ar_numaddr_ranges,
651 list_for_each_entry(re, &el->el_exprs,
654 if (re->re_stride > 1 ||
655 (last_diff > 0 && last_hi != 255) ||
656 (last_diff > 0 && last_hi == 255 &&
660 last_diff = re->re_hi - re->re_lo;
670 * Takes a linked list of nidrange expressions, determines the minimum
671 * and maximum nid and creates appropriate nid structures
677 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
678 char *max_nid, size_t nidstr_length)
681 struct netstrfns *nf = NULL;
685 char *lndname = NULL;
686 char min_addr_str[IPSTRING_LENGTH];
687 char max_addr_str[IPSTRING_LENGTH];
689 list_for_each_entry(nr, nidlist, nr_link) {
690 nf = nr->nr_netstrfns;
691 lndname = nf->nf_name;
693 netnum = nr->nr_netnum;
695 nf->nf_min_max(nidlist, &min_addr, &max_addr);
697 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
698 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
700 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
702 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
705 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
708 * Determines the min and max NID values for num LNDs
714 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
718 struct addrrange *ar;
719 unsigned int tmp_min_addr = 0;
720 unsigned int tmp_max_addr = 0;
721 unsigned int min_addr = 0;
722 unsigned int max_addr = 0;
724 list_for_each_entry(nr, nidlist, nr_link) {
725 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
726 cfs_num_ar_min_max(ar, &tmp_min_addr,
728 if (tmp_min_addr < min_addr || !min_addr)
729 min_addr = tmp_min_addr;
730 if (tmp_max_addr > max_addr)
731 max_addr = tmp_min_addr;
739 * Takes an nidlist and determines the minimum and maximum
746 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
750 struct addrrange *ar;
751 __u32 tmp_min_ip_addr = 0;
752 __u32 tmp_max_ip_addr = 0;
753 __u32 min_ip_addr = 0;
754 __u32 max_ip_addr = 0;
756 list_for_each_entry(nr, nidlist, nr_link) {
757 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
758 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
760 if (tmp_min_ip_addr < min_ip_addr || !min_ip_addr)
761 min_ip_addr = tmp_min_ip_addr;
762 if (tmp_max_ip_addr > max_ip_addr)
763 max_ip_addr = tmp_max_ip_addr;
768 *min_nid = min_ip_addr;
770 *max_nid = max_ip_addr;
774 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
781 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
783 snprintf(str, size, "%u.%u.%u.%u",
784 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
785 (addr >> 8) & 0xff, addr & 0xff);
789 * CAVEAT EMPTOR XscanfX
790 * I use "%n" at the end of a sscanf format to detect trailing junk. However
791 * sscanf may return immediately if it sees the terminating '0' in a string, so
792 * I initialise the %n variable to the expected length. If sscanf sets it;
793 * fine, if it doesn't, then the scan ended at the end of the string, which is
797 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
803 int n = nob; /* XscanfX */
806 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
808 !(a & ~0xff) && !(b & ~0xff) &&
809 !(c & ~0xff) && !(d & ~0xff)) {
810 *addr = ((a << 24) | (b << 16) | (c << 8) | d);
817 /* Used by lnet/config.c so it can't be static */
819 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
821 struct cfs_expr_list *el;
833 if (!cfs_gettok(&src, '.', &res)) {
838 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
842 list_add_tail(&el->el_link, list);
851 cfs_expr_list_free_list(list);
857 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
860 struct cfs_expr_list *el;
862 list_for_each_entry(el, list, el_link) {
865 i += scnprintf(buffer + i, count - i, ".");
866 i += cfs_expr_list_print(buffer + i, count - i, el);
872 * Matches address (\a addr) against address set encoded in \a list.
874 * \retval 1 if \a addr matches
875 * \retval 0 otherwise
878 cfs_ip_addr_match(__u32 addr, struct list_head *list)
880 struct cfs_expr_list *el;
883 list_for_each_entry_reverse(el, list, el_link) {
884 if (!cfs_expr_list_match(addr & 0xff, el))
894 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
896 snprintf(str, size, "%u", addr);
900 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
905 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
909 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
913 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
920 * Nf_parse_addrlist method for networks using numeric addresses.
922 * Examples of such networks are gm and elan.
924 * \retval 0 if \a str parsed to numeric address
925 * \retval errno otherwise
928 libcfs_num_parse(char *str, int len, struct list_head *list)
930 struct cfs_expr_list *el;
933 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
935 list_add_tail(&el->el_link, list);
941 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
944 struct cfs_expr_list *el;
946 list_for_each_entry(el, list, el_link) {
948 i += cfs_expr_list_print(buffer + i, count - i, el);
954 * Nf_match_addr method for networks using numeric addresses
957 * \retval 0 otherwise
960 libcfs_num_match(__u32 addr, struct list_head *numaddr)
962 struct cfs_expr_list *el;
964 LASSERT(!list_empty(numaddr));
965 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
967 return cfs_expr_list_match(addr, el);
970 static struct netstrfns libcfs_netstrfns[] = {
973 .nf_modname = "klolnd",
974 .nf_addr2str = libcfs_decnum_addr2str,
975 .nf_str2addr = libcfs_lo_str2addr,
976 .nf_parse_addrlist = libcfs_num_parse,
977 .nf_print_addrlist = libcfs_num_addr_range_print,
978 .nf_match_addr = libcfs_num_match,
979 .nf_is_contiguous = cfs_num_is_contiguous,
980 .nf_min_max = cfs_num_min_max },
981 { .nf_type = SOCKLND,
983 .nf_modname = "ksocklnd",
984 .nf_addr2str = libcfs_ip_addr2str,
985 .nf_str2addr = libcfs_ip_str2addr,
986 .nf_parse_addrlist = cfs_ip_addr_parse,
987 .nf_print_addrlist = libcfs_ip_addr_range_print,
988 .nf_match_addr = cfs_ip_addr_match,
989 .nf_is_contiguous = cfs_ip_is_contiguous,
990 .nf_min_max = cfs_ip_min_max },
991 { .nf_type = O2IBLND,
993 .nf_modname = "ko2iblnd",
994 .nf_addr2str = libcfs_ip_addr2str,
995 .nf_str2addr = libcfs_ip_str2addr,
996 .nf_parse_addrlist = cfs_ip_addr_parse,
997 .nf_print_addrlist = libcfs_ip_addr_range_print,
998 .nf_match_addr = cfs_ip_addr_match,
999 .nf_is_contiguous = cfs_ip_is_contiguous,
1000 .nf_min_max = cfs_ip_min_max },
1001 { .nf_type = GNILND,
1003 .nf_modname = "kgnilnd",
1004 .nf_addr2str = libcfs_decnum_addr2str,
1005 .nf_str2addr = libcfs_num_str2addr,
1006 .nf_parse_addrlist = libcfs_num_parse,
1007 .nf_print_addrlist = libcfs_num_addr_range_print,
1008 .nf_match_addr = libcfs_num_match,
1009 .nf_is_contiguous = cfs_num_is_contiguous,
1010 .nf_min_max = cfs_num_min_max },
1011 { .nf_type = GNIIPLND,
1013 .nf_modname = "kgnilnd",
1014 .nf_addr2str = libcfs_ip_addr2str,
1015 .nf_str2addr = libcfs_ip_str2addr,
1016 .nf_parse_addrlist = cfs_ip_addr_parse,
1017 .nf_print_addrlist = libcfs_ip_addr_range_print,
1018 .nf_match_addr = cfs_ip_addr_match,
1019 .nf_is_contiguous = cfs_ip_is_contiguous,
1020 .nf_min_max = cfs_ip_min_max },
1023 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
1025 static struct netstrfns *
1026 libcfs_lnd2netstrfns(__u32 lnd)
1030 for (i = 0; i < libcfs_nnetstrfns; i++)
1031 if (lnd == libcfs_netstrfns[i].nf_type)
1032 return &libcfs_netstrfns[i];
1037 static struct netstrfns *
1038 libcfs_namenum2netstrfns(const char *name)
1040 struct netstrfns *nf;
1043 for (i = 0; i < libcfs_nnetstrfns; i++) {
1044 nf = &libcfs_netstrfns[i];
1045 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
1051 static struct netstrfns *
1052 libcfs_name2netstrfns(const char *name)
1056 for (i = 0; i < libcfs_nnetstrfns; i++)
1057 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
1058 return &libcfs_netstrfns[i];
1064 libcfs_isknown_lnd(__u32 lnd)
1066 return !!libcfs_lnd2netstrfns(lnd);
1068 EXPORT_SYMBOL(libcfs_isknown_lnd);
1071 libcfs_lnd2modname(__u32 lnd)
1073 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
1075 return nf ? nf->nf_modname : NULL;
1077 EXPORT_SYMBOL(libcfs_lnd2modname);
1080 libcfs_str2lnd(const char *str)
1082 struct netstrfns *nf = libcfs_name2netstrfns(str);
1089 EXPORT_SYMBOL(libcfs_str2lnd);
1092 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
1094 struct netstrfns *nf;
1096 nf = libcfs_lnd2netstrfns(lnd);
1098 snprintf(buf, buf_size, "?%u?", lnd);
1100 snprintf(buf, buf_size, "%s", nf->nf_name);
1104 EXPORT_SYMBOL(libcfs_lnd2str_r);
1107 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
1109 __u32 nnum = LNET_NETNUM(net);
1110 __u32 lnd = LNET_NETTYP(net);
1111 struct netstrfns *nf;
1113 nf = libcfs_lnd2netstrfns(lnd);
1115 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
1117 snprintf(buf, buf_size, "%s", nf->nf_name);
1119 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1123 EXPORT_SYMBOL(libcfs_net2str_r);
1126 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1128 __u32 addr = LNET_NIDADDR(nid);
1129 __u32 net = LNET_NIDNET(nid);
1130 __u32 nnum = LNET_NETNUM(net);
1131 __u32 lnd = LNET_NETTYP(net);
1132 struct netstrfns *nf;
1134 if (nid == LNET_NID_ANY) {
1135 strncpy(buf, "<?>", buf_size);
1136 buf[buf_size - 1] = '\0';
1140 nf = libcfs_lnd2netstrfns(lnd);
1142 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1146 nf->nf_addr2str(addr, buf, buf_size);
1147 addr_len = strlen(buf);
1149 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1152 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1158 EXPORT_SYMBOL(libcfs_nid2str_r);
1160 static struct netstrfns *
1161 libcfs_str2net_internal(const char *str, __u32 *net)
1163 struct netstrfns *uninitialized_var(nf);
1165 unsigned int netnum;
1168 for (i = 0; i < libcfs_nnetstrfns; i++) {
1169 nf = &libcfs_netstrfns[i];
1170 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1174 if (i == libcfs_nnetstrfns)
1177 nob = strlen(nf->nf_name);
1179 if (strlen(str) == (unsigned int)nob) {
1182 if (nf->nf_type == LOLND) /* net number not allowed */
1187 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1188 i != (int)strlen(str))
1192 *net = LNET_MKNET(nf->nf_type, netnum);
1197 libcfs_str2net(const char *str)
1201 if (libcfs_str2net_internal(str, &net))
1204 return LNET_NIDNET(LNET_NID_ANY);
1206 EXPORT_SYMBOL(libcfs_str2net);
1209 libcfs_str2nid(const char *str)
1211 const char *sep = strchr(str, '@');
1212 struct netstrfns *nf;
1217 nf = libcfs_str2net_internal(sep + 1, &net);
1219 return LNET_NID_ANY;
1221 sep = str + strlen(str);
1222 net = LNET_MKNET(SOCKLND, 0);
1223 nf = libcfs_lnd2netstrfns(SOCKLND);
1227 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1228 return LNET_NID_ANY;
1230 return LNET_MKNID(net, addr);
1232 EXPORT_SYMBOL(libcfs_str2nid);
1235 libcfs_id2str(lnet_process_id_t id)
1237 char *str = libcfs_next_nidstring();
1239 if (id.pid == LNET_PID_ANY) {
1240 snprintf(str, LNET_NIDSTR_SIZE,
1241 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1245 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1246 id.pid & LNET_PID_USERFLAG ? "U" : "",
1247 id.pid & ~LNET_PID_USERFLAG, libcfs_nid2str(id.nid));
1250 EXPORT_SYMBOL(libcfs_id2str);
1253 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1255 if (!strcmp(str, "*")) {
1256 *nidp = LNET_NID_ANY;
1260 *nidp = libcfs_str2nid(str);
1261 return *nidp != LNET_NID_ANY;
1263 EXPORT_SYMBOL(libcfs_str2anynid);