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, 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/nidstrings.c
34 * Author: Phil Schwan <phil@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include "../../include/linux/libcfs/libcfs.h"
40 #include "../../include/linux/lnet/lnet.h"
42 /* max value for numeric network address */
43 #define MAX_NUMERIC_VALUE 0xffffffff
45 #define IPSTRING_LENGTH 16
47 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
48 * consistent in all conversion functions. Some code fragments are copied
49 * around for the sake of clarity...
52 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
53 * Choose the number of nidstrings to support the MAXIMUM expected number of
54 * concurrent users. If there are more, the returned string will be volatile.
55 * NB this number must allow for a process to be descheduled for a timeslice
56 * between getting its string and using it.
59 static char libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
60 static int libcfs_nidstring_idx;
62 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
64 static struct netstrfns *libcfs_namenum2netstrfns(const char *name);
67 libcfs_next_nidstring(void)
72 spin_lock_irqsave(&libcfs_nidstring_lock, flags);
74 str = libcfs_nidstrings[libcfs_nidstring_idx++];
75 if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
76 libcfs_nidstring_idx = 0;
78 spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
81 EXPORT_SYMBOL(libcfs_next_nidstring);
84 * Nid range list syntax.
87 * <nidlist> :== <nidrange> [ ' ' <nidrange> ]
88 * <nidrange> :== <addrrange> '@' <net>
89 * <addrrange> :== '*' |
92 * <ipaddr_range> :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
94 * <cfs_expr_list> :== <number> |
96 * <expr_list> :== '[' <range_expr> [ ',' <range_expr>] ']'
97 * <range_expr> :== <number> |
98 * <number> '-' <number> |
99 * <number> '-' <number> '/' <number>
100 * <net> :== <netname> | <netname><number>
101 * <netname> :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
102 * "vib" | "ra" | "elan" | "mx" | "ptl"
107 * Structure to represent \<nidrange\> token of the syntax.
109 * One of this is created for each \<net\> parsed.
113 * Link to list of this structures which is built on nid range
116 struct list_head nr_link;
118 * List head for addrrange::ar_link.
120 struct list_head nr_addrranges;
122 * Flag indicating that *@<net> is found.
126 * Pointer to corresponding element of libcfs_netstrfns.
128 struct netstrfns *nr_netstrfns;
130 * Number of network. E.g. 5 if \<net\> is "elan5".
136 * Structure to represent \<addrrange\> token of the syntax.
140 * Link to nidrange::nr_addrranges.
142 struct list_head ar_link;
144 * List head for cfs_expr_list::el_list.
146 struct list_head ar_numaddr_ranges;
150 * Parses \<addrrange\> token on the syntax.
152 * Allocates struct addrrange and links to \a nidrange via
153 * (nidrange::nr_addrranges)
155 * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
156 * \retval -errno otherwise
159 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
161 struct addrrange *addrrange;
163 if (src->ls_len == 1 && src->ls_str[0] == '*') {
164 nidrange->nr_all = 1;
168 LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
171 list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
172 INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
174 return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_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(const struct cfs_lstr *src,
191 struct list_head *nidlist)
193 struct netstrfns *nf;
198 if (src->ls_len >= LNET_NIDSTR_SIZE)
201 nf = libcfs_namenum2netstrfns(src->ls_str);
204 endlen = src->ls_len - strlen(nf->nf_name);
206 /* network name only, e.g. "elan" or "tcp" */
210 * e.g. "elan25" or "tcp23", refuse to parse if
211 * network name is not appended with decimal or
214 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
215 endlen, &netnum, 0, MAX_NUMERIC_VALUE))
219 list_for_each_entry(nr, nidlist, nr_link) {
220 if (nr->nr_netstrfns != nf)
222 if (nr->nr_netnum != netnum)
227 LIBCFS_ALLOC(nr, sizeof(struct nidrange));
230 list_add_tail(&nr->nr_link, nidlist);
231 INIT_LIST_HEAD(&nr->nr_addrranges);
232 nr->nr_netstrfns = nf;
234 nr->nr_netnum = netnum;
240 * Parses \<nidrange\> token of the syntax.
242 * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
243 * \retval 0 otherwise
246 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
248 struct cfs_lstr addrrange;
254 if (!cfs_gettok(src, '@', &addrrange))
257 if (!cfs_gettok(src, '@', &net) || src->ls_str)
260 nr = add_nidrange(&net, nidlist);
264 if (parse_addrange(&addrrange, nr))
273 * Frees addrrange structures of \a list.
275 * For each struct addrrange structure found on \a list it frees
276 * cfs_expr_list list attached to it and frees the addrrange itself.
281 free_addrranges(struct list_head *list)
283 while (!list_empty(list)) {
284 struct addrrange *ar;
286 ar = list_entry(list->next, struct addrrange, ar_link);
288 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
289 list_del(&ar->ar_link);
290 LIBCFS_FREE(ar, sizeof(struct addrrange));
295 * Frees nidrange strutures of \a list.
297 * For each struct nidrange structure found on \a list it frees
298 * addrrange list attached to it and frees the nidrange itself.
303 cfs_free_nidlist(struct list_head *list)
305 struct list_head *pos, *next;
308 list_for_each_safe(pos, next, list) {
309 nr = list_entry(pos, struct nidrange, nr_link);
310 free_addrranges(&nr->nr_addrranges);
312 LIBCFS_FREE(nr, sizeof(struct nidrange));
315 EXPORT_SYMBOL(cfs_free_nidlist);
318 * Parses nid range list.
320 * Parses with rigorous syntax and overflow checking \a str into
321 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
322 * structures and links that structure to \a nidlist. The resulting
323 * list can be used to match a NID againts set of NIDS defined by \a
327 * \retval 1 on success
328 * \retval 0 otherwise
331 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
339 INIT_LIST_HEAD(nidlist);
341 rc = cfs_gettok(&src, ' ', &res);
343 cfs_free_nidlist(nidlist);
346 rc = parse_nidrange(&res, nidlist);
348 cfs_free_nidlist(nidlist);
354 EXPORT_SYMBOL(cfs_parse_nidlist);
357 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
359 * \see cfs_parse_nidlist()
362 * \retval 0 otherwises
364 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
367 struct addrrange *ar;
369 list_for_each_entry(nr, nidlist, nr_link) {
370 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
372 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
376 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
377 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
378 &ar->ar_numaddr_ranges))
383 EXPORT_SYMBOL(cfs_match_nid);
386 * Print the network part of the nidrange \a nr into the specified \a buffer.
388 * \retval number of characters written
391 cfs_print_network(char *buffer, int count, struct nidrange *nr)
393 struct netstrfns *nf = nr->nr_netstrfns;
396 return scnprintf(buffer, count, "@%s", nf->nf_name);
398 return scnprintf(buffer, count, "@%s%u",
399 nf->nf_name, nr->nr_netnum);
403 * Print a list of addrrange (\a addrranges) into the specified \a buffer.
404 * At max \a count characters can be printed into \a buffer.
406 * \retval number of characters written
409 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
413 struct addrrange *ar;
414 struct netstrfns *nf = nr->nr_netstrfns;
416 list_for_each_entry(ar, addrranges, ar_link) {
418 i += scnprintf(buffer + i, count - i, " ");
419 i += nf->nf_print_addrlist(buffer + i, count - i,
420 &ar->ar_numaddr_ranges);
421 i += cfs_print_network(buffer + i, count - i, nr);
427 * Print a list of nidranges (\a nidlist) into the specified \a buffer.
428 * At max \a count characters can be printed into \a buffer.
429 * Nidranges are separated by a space character.
431 * \retval number of characters written
433 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
441 list_for_each_entry(nr, nidlist, nr_link) {
443 i += scnprintf(buffer + i, count - i, " ");
446 LASSERT(list_empty(&nr->nr_addrranges));
447 i += scnprintf(buffer + i, count - i, "*");
448 i += cfs_print_network(buffer + i, count - i, nr);
450 i += cfs_print_addrranges(buffer + i, count - i,
451 &nr->nr_addrranges, nr);
456 EXPORT_SYMBOL(cfs_print_nidlist);
459 * Determines minimum and maximum addresses for a single
460 * numeric address range
466 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
469 struct cfs_expr_list *el;
470 struct cfs_range_expr *re;
471 __u32 tmp_ip_addr = 0;
472 unsigned int min_ip[4] = {0};
473 unsigned int max_ip[4] = {0};
476 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
477 list_for_each_entry(re, &el->el_exprs, re_link) {
478 min_ip[re_count] = re->re_lo;
479 max_ip[re_count] = re->re_hi;
484 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
485 (min_ip[2] << 8) | min_ip[3]);
488 *min_nid = tmp_ip_addr;
490 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
491 (max_ip[2] << 8) | max_ip[3]);
494 *max_nid = tmp_ip_addr;
498 * Determines minimum and maximum addresses for a single
499 * numeric address range
505 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
508 struct cfs_expr_list *el;
509 struct cfs_range_expr *re;
510 unsigned int min_addr = 0;
511 unsigned int max_addr = 0;
513 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
514 list_for_each_entry(re, &el->el_exprs, re_link) {
515 if (re->re_lo < min_addr || !min_addr)
516 min_addr = re->re_lo;
517 if (re->re_hi > max_addr)
518 max_addr = re->re_hi;
529 * Determines whether an expression list in an nidrange contains exactly
530 * one contiguous address range. Calls the correct netstrfns for the LND
534 * \retval true if contiguous
535 * \retval false if not contiguous
537 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
540 struct netstrfns *nf = NULL;
541 char *lndname = NULL;
544 list_for_each_entry(nr, nidlist, nr_link) {
545 nf = nr->nr_netstrfns;
547 lndname = nf->nf_name;
549 netnum = nr->nr_netnum;
551 if (strcmp(lndname, nf->nf_name) ||
552 netnum != nr->nr_netnum)
559 if (!nf->nf_is_contiguous(nidlist))
564 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
567 * Determines whether an expression list in an num nidrange contains exactly
568 * one contiguous address range.
572 * \retval true if contiguous
573 * \retval false if not contiguous
575 static bool cfs_num_is_contiguous(struct list_head *nidlist)
578 struct addrrange *ar;
579 struct cfs_expr_list *el;
580 struct cfs_range_expr *re;
582 __u32 last_end_nid = 0;
583 __u32 current_start_nid = 0;
584 __u32 current_end_nid = 0;
586 list_for_each_entry(nr, nidlist, nr_link) {
587 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
588 cfs_num_ar_min_max(ar, ¤t_start_nid,
591 (current_start_nid - last_end_nid != 1))
593 last_end_nid = current_end_nid;
594 list_for_each_entry(el, &ar->ar_numaddr_ranges,
596 list_for_each_entry(re, &el->el_exprs,
598 if (re->re_stride > 1)
601 re->re_hi - last_hi != 1)
613 * Determines whether an expression list in an ip nidrange contains exactly
614 * one contiguous address range.
618 * \retval true if contiguous
619 * \retval false if not contiguous
621 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
624 struct addrrange *ar;
625 struct cfs_expr_list *el;
626 struct cfs_range_expr *re;
630 __u32 last_end_nid = 0;
631 __u32 current_start_nid = 0;
632 __u32 current_end_nid = 0;
634 list_for_each_entry(nr, nidlist, nr_link) {
635 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
638 cfs_ip_ar_min_max(ar, ¤t_start_nid,
641 (current_start_nid - last_end_nid != 1))
643 last_end_nid = current_end_nid;
644 list_for_each_entry(el, &ar->ar_numaddr_ranges,
647 list_for_each_entry(re, &el->el_exprs,
650 if (re->re_stride > 1 ||
651 (last_diff > 0 && last_hi != 255) ||
652 (last_diff > 0 && last_hi == 255 &&
656 last_diff = re->re_hi - re->re_lo;
666 * Takes a linked list of nidrange expressions, determines the minimum
667 * and maximum nid and creates appropriate nid structures
673 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
674 char *max_nid, size_t nidstr_length)
677 struct netstrfns *nf = NULL;
681 char *lndname = NULL;
682 char min_addr_str[IPSTRING_LENGTH];
683 char max_addr_str[IPSTRING_LENGTH];
685 list_for_each_entry(nr, nidlist, nr_link) {
686 nf = nr->nr_netstrfns;
687 lndname = nf->nf_name;
689 netnum = nr->nr_netnum;
691 nf->nf_min_max(nidlist, &min_addr, &max_addr);
693 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
694 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
696 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
698 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
701 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
704 * Determines the min and max NID values for num LNDs
710 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
714 struct addrrange *ar;
715 unsigned int tmp_min_addr = 0;
716 unsigned int tmp_max_addr = 0;
717 unsigned int min_addr = 0;
718 unsigned int max_addr = 0;
720 list_for_each_entry(nr, nidlist, nr_link) {
721 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
722 cfs_num_ar_min_max(ar, &tmp_min_addr,
724 if (tmp_min_addr < min_addr || !min_addr)
725 min_addr = tmp_min_addr;
726 if (tmp_max_addr > max_addr)
727 max_addr = tmp_min_addr;
735 * Takes an nidlist and determines the minimum and maximum
742 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
746 struct addrrange *ar;
747 __u32 tmp_min_ip_addr = 0;
748 __u32 tmp_max_ip_addr = 0;
749 __u32 min_ip_addr = 0;
750 __u32 max_ip_addr = 0;
752 list_for_each_entry(nr, nidlist, nr_link) {
753 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
754 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
756 if (tmp_min_ip_addr < min_ip_addr || !min_ip_addr)
757 min_ip_addr = tmp_min_ip_addr;
758 if (tmp_max_ip_addr > max_ip_addr)
759 max_ip_addr = tmp_max_ip_addr;
764 *min_nid = min_ip_addr;
766 *max_nid = max_ip_addr;
770 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
777 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
779 snprintf(str, size, "%u.%u.%u.%u",
780 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
781 (addr >> 8) & 0xff, addr & 0xff);
785 * CAVEAT EMPTOR XscanfX
786 * I use "%n" at the end of a sscanf format to detect trailing junk. However
787 * sscanf may return immediately if it sees the terminating '0' in a string, so
788 * I initialise the %n variable to the expected length. If sscanf sets it;
789 * fine, if it doesn't, then the scan ended at the end of the string, which is
793 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
799 int n = nob; /* XscanfX */
802 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
804 !(a & ~0xff) && !(b & ~0xff) &&
805 !(c & ~0xff) && !(d & ~0xff)) {
806 *addr = ((a << 24) | (b << 16) | (c << 8) | d);
813 /* Used by lnet/config.c so it can't be static */
815 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
817 struct cfs_expr_list *el;
829 if (!cfs_gettok(&src, '.', &res)) {
834 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
838 list_add_tail(&el->el_link, list);
847 cfs_expr_list_free_list(list);
853 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
856 struct cfs_expr_list *el;
858 list_for_each_entry(el, list, el_link) {
861 i += scnprintf(buffer + i, count - i, ".");
862 i += cfs_expr_list_print(buffer + i, count - i, el);
868 * Matches address (\a addr) against address set encoded in \a list.
870 * \retval 1 if \a addr matches
871 * \retval 0 otherwise
874 cfs_ip_addr_match(__u32 addr, struct list_head *list)
876 struct cfs_expr_list *el;
879 list_for_each_entry_reverse(el, list, el_link) {
880 if (!cfs_expr_list_match(addr & 0xff, el))
890 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
892 snprintf(str, size, "%u", addr);
896 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
901 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
905 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
909 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
916 * Nf_parse_addrlist method for networks using numeric addresses.
918 * Examples of such networks are gm and elan.
920 * \retval 0 if \a str parsed to numeric address
921 * \retval errno otherwise
924 libcfs_num_parse(char *str, int len, struct list_head *list)
926 struct cfs_expr_list *el;
929 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
931 list_add_tail(&el->el_link, list);
937 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
940 struct cfs_expr_list *el;
942 list_for_each_entry(el, list, el_link) {
944 i += cfs_expr_list_print(buffer + i, count - i, el);
950 * Nf_match_addr method for networks using numeric addresses
953 * \retval 0 otherwise
956 libcfs_num_match(__u32 addr, struct list_head *numaddr)
958 struct cfs_expr_list *el;
960 LASSERT(!list_empty(numaddr));
961 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
963 return cfs_expr_list_match(addr, el);
966 static struct netstrfns libcfs_netstrfns[] = {
969 .nf_modname = "klolnd",
970 .nf_addr2str = libcfs_decnum_addr2str,
971 .nf_str2addr = libcfs_lo_str2addr,
972 .nf_parse_addrlist = libcfs_num_parse,
973 .nf_print_addrlist = libcfs_num_addr_range_print,
974 .nf_match_addr = libcfs_num_match,
975 .nf_is_contiguous = cfs_num_is_contiguous,
976 .nf_min_max = cfs_num_min_max },
977 { .nf_type = SOCKLND,
979 .nf_modname = "ksocklnd",
980 .nf_addr2str = libcfs_ip_addr2str,
981 .nf_str2addr = libcfs_ip_str2addr,
982 .nf_parse_addrlist = cfs_ip_addr_parse,
983 .nf_print_addrlist = libcfs_ip_addr_range_print,
984 .nf_match_addr = cfs_ip_addr_match,
985 .nf_is_contiguous = cfs_ip_is_contiguous,
986 .nf_min_max = cfs_ip_min_max },
987 { .nf_type = O2IBLND,
989 .nf_modname = "ko2iblnd",
990 .nf_addr2str = libcfs_ip_addr2str,
991 .nf_str2addr = libcfs_ip_str2addr,
992 .nf_parse_addrlist = cfs_ip_addr_parse,
993 .nf_print_addrlist = libcfs_ip_addr_range_print,
994 .nf_match_addr = cfs_ip_addr_match,
995 .nf_is_contiguous = cfs_ip_is_contiguous,
996 .nf_min_max = cfs_ip_min_max },
999 .nf_modname = "kgnilnd",
1000 .nf_addr2str = libcfs_decnum_addr2str,
1001 .nf_str2addr = libcfs_num_str2addr,
1002 .nf_parse_addrlist = libcfs_num_parse,
1003 .nf_print_addrlist = libcfs_num_addr_range_print,
1004 .nf_match_addr = libcfs_num_match,
1005 .nf_is_contiguous = cfs_num_is_contiguous,
1006 .nf_min_max = cfs_num_min_max },
1007 { .nf_type = GNIIPLND,
1009 .nf_modname = "kgnilnd",
1010 .nf_addr2str = libcfs_ip_addr2str,
1011 .nf_str2addr = libcfs_ip_str2addr,
1012 .nf_parse_addrlist = cfs_ip_addr_parse,
1013 .nf_print_addrlist = libcfs_ip_addr_range_print,
1014 .nf_match_addr = cfs_ip_addr_match,
1015 .nf_is_contiguous = cfs_ip_is_contiguous,
1016 .nf_min_max = cfs_ip_min_max },
1019 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
1021 static struct netstrfns *
1022 libcfs_lnd2netstrfns(__u32 lnd)
1026 for (i = 0; i < libcfs_nnetstrfns; i++)
1027 if (lnd == libcfs_netstrfns[i].nf_type)
1028 return &libcfs_netstrfns[i];
1033 static struct netstrfns *
1034 libcfs_namenum2netstrfns(const char *name)
1036 struct netstrfns *nf;
1039 for (i = 0; i < libcfs_nnetstrfns; i++) {
1040 nf = &libcfs_netstrfns[i];
1041 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
1047 static struct netstrfns *
1048 libcfs_name2netstrfns(const char *name)
1052 for (i = 0; i < libcfs_nnetstrfns; i++)
1053 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
1054 return &libcfs_netstrfns[i];
1060 libcfs_isknown_lnd(__u32 lnd)
1062 return !!libcfs_lnd2netstrfns(lnd);
1064 EXPORT_SYMBOL(libcfs_isknown_lnd);
1067 libcfs_lnd2modname(__u32 lnd)
1069 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
1071 return nf ? nf->nf_modname : NULL;
1073 EXPORT_SYMBOL(libcfs_lnd2modname);
1076 libcfs_str2lnd(const char *str)
1078 struct netstrfns *nf = libcfs_name2netstrfns(str);
1085 EXPORT_SYMBOL(libcfs_str2lnd);
1088 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
1090 struct netstrfns *nf;
1092 nf = libcfs_lnd2netstrfns(lnd);
1094 snprintf(buf, buf_size, "?%u?", lnd);
1096 snprintf(buf, buf_size, "%s", nf->nf_name);
1100 EXPORT_SYMBOL(libcfs_lnd2str_r);
1103 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
1105 __u32 nnum = LNET_NETNUM(net);
1106 __u32 lnd = LNET_NETTYP(net);
1107 struct netstrfns *nf;
1109 nf = libcfs_lnd2netstrfns(lnd);
1111 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
1113 snprintf(buf, buf_size, "%s", nf->nf_name);
1115 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1119 EXPORT_SYMBOL(libcfs_net2str_r);
1122 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1124 __u32 addr = LNET_NIDADDR(nid);
1125 __u32 net = LNET_NIDNET(nid);
1126 __u32 nnum = LNET_NETNUM(net);
1127 __u32 lnd = LNET_NETTYP(net);
1128 struct netstrfns *nf;
1130 if (nid == LNET_NID_ANY) {
1131 strncpy(buf, "<?>", buf_size);
1132 buf[buf_size - 1] = '\0';
1136 nf = libcfs_lnd2netstrfns(lnd);
1138 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1142 nf->nf_addr2str(addr, buf, buf_size);
1143 addr_len = strlen(buf);
1145 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1148 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1154 EXPORT_SYMBOL(libcfs_nid2str_r);
1156 static struct netstrfns *
1157 libcfs_str2net_internal(const char *str, __u32 *net)
1159 struct netstrfns *uninitialized_var(nf);
1161 unsigned int netnum;
1164 for (i = 0; i < libcfs_nnetstrfns; i++) {
1165 nf = &libcfs_netstrfns[i];
1166 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1170 if (i == libcfs_nnetstrfns)
1173 nob = strlen(nf->nf_name);
1175 if (strlen(str) == (unsigned int)nob) {
1178 if (nf->nf_type == LOLND) /* net number not allowed */
1183 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1184 i != (int)strlen(str))
1188 *net = LNET_MKNET(nf->nf_type, netnum);
1193 libcfs_str2net(const char *str)
1197 if (libcfs_str2net_internal(str, &net))
1200 return LNET_NIDNET(LNET_NID_ANY);
1202 EXPORT_SYMBOL(libcfs_str2net);
1205 libcfs_str2nid(const char *str)
1207 const char *sep = strchr(str, '@');
1208 struct netstrfns *nf;
1213 nf = libcfs_str2net_internal(sep + 1, &net);
1215 return LNET_NID_ANY;
1217 sep = str + strlen(str);
1218 net = LNET_MKNET(SOCKLND, 0);
1219 nf = libcfs_lnd2netstrfns(SOCKLND);
1223 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1224 return LNET_NID_ANY;
1226 return LNET_MKNID(net, addr);
1228 EXPORT_SYMBOL(libcfs_str2nid);
1231 libcfs_id2str(lnet_process_id_t id)
1233 char *str = libcfs_next_nidstring();
1235 if (id.pid == LNET_PID_ANY) {
1236 snprintf(str, LNET_NIDSTR_SIZE,
1237 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1241 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1242 id.pid & LNET_PID_USERFLAG ? "U" : "",
1243 id.pid & ~LNET_PID_USERFLAG, libcfs_nid2str(id.nid));
1246 EXPORT_SYMBOL(libcfs_id2str);
1249 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1251 if (!strcmp(str, "*")) {
1252 *nidp = LNET_NID_ANY;
1256 *nidp = libcfs_str2nid(str);
1257 return *nidp != LNET_NID_ANY;
1259 EXPORT_SYMBOL(libcfs_str2anynid);