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 <linux/libcfs/libcfs.h>
40 #include <uapi/linux/lnet/nidstr.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;
252 if (!cfs_gettok(src, '@', &addrrange))
255 if (!cfs_gettok(src, '@', &net) || src->ls_str)
258 nr = add_nidrange(&net, nidlist);
262 if (parse_addrange(&addrrange, nr))
271 * Frees addrrange structures of \a list.
273 * For each struct addrrange structure found on \a list it frees
274 * cfs_expr_list list attached to it and frees the addrrange itself.
279 free_addrranges(struct list_head *list)
281 while (!list_empty(list)) {
282 struct addrrange *ar;
284 ar = list_entry(list->next, struct addrrange, ar_link);
286 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
287 list_del(&ar->ar_link);
288 LIBCFS_FREE(ar, sizeof(struct addrrange));
293 * Frees nidrange strutures of \a list.
295 * For each struct nidrange structure found on \a list it frees
296 * addrrange list attached to it and frees the nidrange itself.
301 cfs_free_nidlist(struct list_head *list)
303 struct list_head *pos, *next;
306 list_for_each_safe(pos, next, list) {
307 nr = list_entry(pos, struct nidrange, nr_link);
308 free_addrranges(&nr->nr_addrranges);
310 LIBCFS_FREE(nr, sizeof(struct nidrange));
313 EXPORT_SYMBOL(cfs_free_nidlist);
316 * Parses nid range list.
318 * Parses with rigorous syntax and overflow checking \a str into
319 * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
320 * structures and links that structure to \a nidlist. The resulting
321 * list can be used to match a NID againts set of NIDS defined by \a
325 * \retval 1 on success
326 * \retval 0 otherwise
329 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
337 INIT_LIST_HEAD(nidlist);
339 rc = cfs_gettok(&src, ' ', &res);
341 cfs_free_nidlist(nidlist);
344 rc = parse_nidrange(&res, nidlist);
346 cfs_free_nidlist(nidlist);
352 EXPORT_SYMBOL(cfs_parse_nidlist);
355 * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
357 * \see cfs_parse_nidlist()
360 * \retval 0 otherwises
362 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
365 struct addrrange *ar;
367 list_for_each_entry(nr, nidlist, nr_link) {
368 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
370 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
374 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
375 if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
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;
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, " ");
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 * Determines minimum and maximum addresses for a single
458 * numeric address range
464 static void cfs_ip_ar_min_max(struct addrrange *ar, __u32 *min_nid,
467 struct cfs_expr_list *el;
468 struct cfs_range_expr *re;
469 __u32 tmp_ip_addr = 0;
470 unsigned int min_ip[4] = {0};
471 unsigned int max_ip[4] = {0};
474 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
475 list_for_each_entry(re, &el->el_exprs, re_link) {
476 min_ip[re_count] = re->re_lo;
477 max_ip[re_count] = re->re_hi;
482 tmp_ip_addr = ((min_ip[0] << 24) | (min_ip[1] << 16) |
483 (min_ip[2] << 8) | min_ip[3]);
486 *min_nid = tmp_ip_addr;
488 tmp_ip_addr = ((max_ip[0] << 24) | (max_ip[1] << 16) |
489 (max_ip[2] << 8) | max_ip[3]);
492 *max_nid = tmp_ip_addr;
496 * Determines minimum and maximum addresses for a single
497 * numeric address range
503 static void cfs_num_ar_min_max(struct addrrange *ar, __u32 *min_nid,
506 struct cfs_expr_list *el;
507 struct cfs_range_expr *re;
508 unsigned int min_addr = 0;
509 unsigned int max_addr = 0;
511 list_for_each_entry(el, &ar->ar_numaddr_ranges, el_link) {
512 list_for_each_entry(re, &el->el_exprs, re_link) {
513 if (re->re_lo < min_addr || !min_addr)
514 min_addr = re->re_lo;
515 if (re->re_hi > max_addr)
516 max_addr = re->re_hi;
527 * Determines whether an expression list in an nidrange contains exactly
528 * one contiguous address range. Calls the correct netstrfns for the LND
532 * \retval true if contiguous
533 * \retval false if not contiguous
535 bool cfs_nidrange_is_contiguous(struct list_head *nidlist)
538 struct netstrfns *nf = NULL;
539 char *lndname = NULL;
542 list_for_each_entry(nr, nidlist, nr_link) {
543 nf = nr->nr_netstrfns;
545 lndname = nf->nf_name;
547 netnum = nr->nr_netnum;
549 if (strcmp(lndname, nf->nf_name) ||
550 netnum != nr->nr_netnum)
557 if (!nf->nf_is_contiguous(nidlist))
562 EXPORT_SYMBOL(cfs_nidrange_is_contiguous);
565 * Determines whether an expression list in an num nidrange contains exactly
566 * one contiguous address range.
570 * \retval true if contiguous
571 * \retval false if not contiguous
573 static bool cfs_num_is_contiguous(struct list_head *nidlist)
576 struct addrrange *ar;
577 struct cfs_expr_list *el;
578 struct cfs_range_expr *re;
580 __u32 last_end_nid = 0;
581 __u32 current_start_nid = 0;
582 __u32 current_end_nid = 0;
584 list_for_each_entry(nr, nidlist, nr_link) {
585 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
586 cfs_num_ar_min_max(ar, ¤t_start_nid,
589 (current_start_nid - last_end_nid != 1))
591 last_end_nid = current_end_nid;
592 list_for_each_entry(el, &ar->ar_numaddr_ranges,
594 list_for_each_entry(re, &el->el_exprs,
596 if (re->re_stride > 1)
599 re->re_hi - last_hi != 1)
611 * Determines whether an expression list in an ip nidrange contains exactly
612 * one contiguous address range.
616 * \retval true if contiguous
617 * \retval false if not contiguous
619 static bool cfs_ip_is_contiguous(struct list_head *nidlist)
622 struct addrrange *ar;
623 struct cfs_expr_list *el;
624 struct cfs_range_expr *re;
628 __u32 last_end_nid = 0;
629 __u32 current_start_nid = 0;
630 __u32 current_end_nid = 0;
632 list_for_each_entry(nr, nidlist, nr_link) {
633 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
636 cfs_ip_ar_min_max(ar, ¤t_start_nid,
639 (current_start_nid - last_end_nid != 1))
641 last_end_nid = current_end_nid;
642 list_for_each_entry(el, &ar->ar_numaddr_ranges,
645 list_for_each_entry(re, &el->el_exprs,
648 if (re->re_stride > 1 ||
649 (last_diff > 0 && last_hi != 255) ||
650 (last_diff > 0 && last_hi == 255 &&
654 last_diff = re->re_hi - re->re_lo;
664 * Takes a linked list of nidrange expressions, determines the minimum
665 * and maximum nid and creates appropriate nid structures
671 void cfs_nidrange_find_min_max(struct list_head *nidlist, char *min_nid,
672 char *max_nid, size_t nidstr_length)
675 struct netstrfns *nf = NULL;
679 char *lndname = NULL;
680 char min_addr_str[IPSTRING_LENGTH];
681 char max_addr_str[IPSTRING_LENGTH];
683 list_for_each_entry(nr, nidlist, nr_link) {
684 nf = nr->nr_netstrfns;
685 lndname = nf->nf_name;
687 netnum = nr->nr_netnum;
689 nf->nf_min_max(nidlist, &min_addr, &max_addr);
691 nf->nf_addr2str(min_addr, min_addr_str, sizeof(min_addr_str));
692 nf->nf_addr2str(max_addr, max_addr_str, sizeof(max_addr_str));
694 snprintf(min_nid, nidstr_length, "%s@%s%d", min_addr_str, lndname,
696 snprintf(max_nid, nidstr_length, "%s@%s%d", max_addr_str, lndname,
699 EXPORT_SYMBOL(cfs_nidrange_find_min_max);
702 * Determines the min and max NID values for num LNDs
708 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min_nid,
712 struct addrrange *ar;
713 unsigned int tmp_min_addr = 0;
714 unsigned int tmp_max_addr = 0;
715 unsigned int min_addr = 0;
716 unsigned int max_addr = 0;
718 list_for_each_entry(nr, nidlist, nr_link) {
719 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
720 cfs_num_ar_min_max(ar, &tmp_min_addr,
722 if (tmp_min_addr < min_addr || !min_addr)
723 min_addr = tmp_min_addr;
724 if (tmp_max_addr > max_addr)
725 max_addr = tmp_min_addr;
733 * Takes an nidlist and determines the minimum and maximum
740 static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min_nid,
744 struct addrrange *ar;
745 __u32 tmp_min_ip_addr = 0;
746 __u32 tmp_max_ip_addr = 0;
747 __u32 min_ip_addr = 0;
748 __u32 max_ip_addr = 0;
750 list_for_each_entry(nr, nidlist, nr_link) {
751 list_for_each_entry(ar, &nr->nr_addrranges, ar_link) {
752 cfs_ip_ar_min_max(ar, &tmp_min_ip_addr,
754 if (tmp_min_ip_addr < min_ip_addr || !min_ip_addr)
755 min_ip_addr = tmp_min_ip_addr;
756 if (tmp_max_ip_addr > max_ip_addr)
757 max_ip_addr = tmp_max_ip_addr;
762 *min_nid = min_ip_addr;
764 *max_nid = max_ip_addr;
768 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
775 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
777 snprintf(str, size, "%u.%u.%u.%u",
778 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
779 (addr >> 8) & 0xff, addr & 0xff);
783 * CAVEAT EMPTOR XscanfX
784 * I use "%n" at the end of a sscanf format to detect trailing junk. However
785 * sscanf may return immediately if it sees the terminating '0' in a string, so
786 * I initialise the %n variable to the expected length. If sscanf sets it;
787 * fine, if it doesn't, then the scan ended at the end of the string, which is
791 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
797 int n = nob; /* XscanfX */
800 if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
802 !(a & ~0xff) && !(b & ~0xff) &&
803 !(c & ~0xff) && !(d & ~0xff)) {
804 *addr = ((a << 24) | (b << 16) | (c << 8) | d);
811 /* Used by lnet/config.c so it can't be static */
813 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
815 struct cfs_expr_list *el;
827 if (!cfs_gettok(&src, '.', &res)) {
832 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
836 list_add_tail(&el->el_link, list);
845 cfs_expr_list_free_list(list);
851 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
854 struct cfs_expr_list *el;
856 list_for_each_entry(el, list, el_link) {
859 i += scnprintf(buffer + i, count - i, ".");
860 i += cfs_expr_list_print(buffer + i, count - i, el);
866 * Matches address (\a addr) against address set encoded in \a list.
868 * \retval 1 if \a addr matches
869 * \retval 0 otherwise
872 cfs_ip_addr_match(__u32 addr, struct list_head *list)
874 struct cfs_expr_list *el;
877 list_for_each_entry_reverse(el, list, el_link) {
878 if (!cfs_expr_list_match(addr & 0xff, el))
888 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
890 snprintf(str, size, "%u", addr);
894 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
899 if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
903 if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
907 if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
914 * Nf_parse_addrlist method for networks using numeric addresses.
916 * Examples of such networks are gm and elan.
918 * \retval 0 if \a str parsed to numeric address
919 * \retval errno otherwise
922 libcfs_num_parse(char *str, int len, struct list_head *list)
924 struct cfs_expr_list *el;
927 rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
929 list_add_tail(&el->el_link, list);
935 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
938 struct cfs_expr_list *el;
940 list_for_each_entry(el, list, el_link) {
942 i += cfs_expr_list_print(buffer + i, count - i, el);
948 * Nf_match_addr method for networks using numeric addresses
951 * \retval 0 otherwise
954 libcfs_num_match(__u32 addr, struct list_head *numaddr)
956 struct cfs_expr_list *el;
958 LASSERT(!list_empty(numaddr));
959 el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
961 return cfs_expr_list_match(addr, el);
964 static struct netstrfns libcfs_netstrfns[] = {
967 .nf_modname = "klolnd",
968 .nf_addr2str = libcfs_decnum_addr2str,
969 .nf_str2addr = libcfs_lo_str2addr,
970 .nf_parse_addrlist = libcfs_num_parse,
971 .nf_print_addrlist = libcfs_num_addr_range_print,
972 .nf_match_addr = libcfs_num_match,
973 .nf_is_contiguous = cfs_num_is_contiguous,
974 .nf_min_max = cfs_num_min_max },
975 { .nf_type = SOCKLND,
977 .nf_modname = "ksocklnd",
978 .nf_addr2str = libcfs_ip_addr2str,
979 .nf_str2addr = libcfs_ip_str2addr,
980 .nf_parse_addrlist = cfs_ip_addr_parse,
981 .nf_print_addrlist = libcfs_ip_addr_range_print,
982 .nf_match_addr = cfs_ip_addr_match,
983 .nf_is_contiguous = cfs_ip_is_contiguous,
984 .nf_min_max = cfs_ip_min_max },
985 { .nf_type = O2IBLND,
987 .nf_modname = "ko2iblnd",
988 .nf_addr2str = libcfs_ip_addr2str,
989 .nf_str2addr = libcfs_ip_str2addr,
990 .nf_parse_addrlist = cfs_ip_addr_parse,
991 .nf_print_addrlist = libcfs_ip_addr_range_print,
992 .nf_match_addr = cfs_ip_addr_match,
993 .nf_is_contiguous = cfs_ip_is_contiguous,
994 .nf_min_max = cfs_ip_min_max },
997 .nf_modname = "kgnilnd",
998 .nf_addr2str = libcfs_decnum_addr2str,
999 .nf_str2addr = libcfs_num_str2addr,
1000 .nf_parse_addrlist = libcfs_num_parse,
1001 .nf_print_addrlist = libcfs_num_addr_range_print,
1002 .nf_match_addr = libcfs_num_match,
1003 .nf_is_contiguous = cfs_num_is_contiguous,
1004 .nf_min_max = cfs_num_min_max },
1005 { .nf_type = GNIIPLND,
1007 .nf_modname = "kgnilnd",
1008 .nf_addr2str = libcfs_ip_addr2str,
1009 .nf_str2addr = libcfs_ip_str2addr,
1010 .nf_parse_addrlist = cfs_ip_addr_parse,
1011 .nf_print_addrlist = libcfs_ip_addr_range_print,
1012 .nf_match_addr = cfs_ip_addr_match,
1013 .nf_is_contiguous = cfs_ip_is_contiguous,
1014 .nf_min_max = cfs_ip_min_max },
1017 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
1019 static struct netstrfns *
1020 libcfs_lnd2netstrfns(__u32 lnd)
1024 for (i = 0; i < libcfs_nnetstrfns; i++)
1025 if (lnd == libcfs_netstrfns[i].nf_type)
1026 return &libcfs_netstrfns[i];
1031 static struct netstrfns *
1032 libcfs_namenum2netstrfns(const char *name)
1034 struct netstrfns *nf;
1037 for (i = 0; i < libcfs_nnetstrfns; i++) {
1038 nf = &libcfs_netstrfns[i];
1039 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
1045 static struct netstrfns *
1046 libcfs_name2netstrfns(const char *name)
1050 for (i = 0; i < libcfs_nnetstrfns; i++)
1051 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
1052 return &libcfs_netstrfns[i];
1058 libcfs_isknown_lnd(__u32 lnd)
1060 return !!libcfs_lnd2netstrfns(lnd);
1062 EXPORT_SYMBOL(libcfs_isknown_lnd);
1065 libcfs_lnd2modname(__u32 lnd)
1067 struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
1069 return nf ? nf->nf_modname : NULL;
1071 EXPORT_SYMBOL(libcfs_lnd2modname);
1074 libcfs_str2lnd(const char *str)
1076 struct netstrfns *nf = libcfs_name2netstrfns(str);
1083 EXPORT_SYMBOL(libcfs_str2lnd);
1086 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
1088 struct netstrfns *nf;
1090 nf = libcfs_lnd2netstrfns(lnd);
1092 snprintf(buf, buf_size, "?%u?", lnd);
1094 snprintf(buf, buf_size, "%s", nf->nf_name);
1098 EXPORT_SYMBOL(libcfs_lnd2str_r);
1101 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
1103 __u32 nnum = LNET_NETNUM(net);
1104 __u32 lnd = LNET_NETTYP(net);
1105 struct netstrfns *nf;
1107 nf = libcfs_lnd2netstrfns(lnd);
1109 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
1111 snprintf(buf, buf_size, "%s", nf->nf_name);
1113 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1117 EXPORT_SYMBOL(libcfs_net2str_r);
1120 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1122 __u32 addr = LNET_NIDADDR(nid);
1123 __u32 net = LNET_NIDNET(nid);
1124 __u32 nnum = LNET_NETNUM(net);
1125 __u32 lnd = LNET_NETTYP(net);
1126 struct netstrfns *nf;
1128 if (nid == LNET_NID_ANY) {
1129 strncpy(buf, "<?>", buf_size);
1130 buf[buf_size - 1] = '\0';
1134 nf = libcfs_lnd2netstrfns(lnd);
1136 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1140 nf->nf_addr2str(addr, buf, buf_size);
1141 addr_len = strlen(buf);
1143 snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1146 snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1152 EXPORT_SYMBOL(libcfs_nid2str_r);
1154 static struct netstrfns *
1155 libcfs_str2net_internal(const char *str, __u32 *net)
1157 struct netstrfns *nf = NULL;
1159 unsigned int netnum;
1162 for (i = 0; i < libcfs_nnetstrfns; i++) {
1163 nf = &libcfs_netstrfns[i];
1164 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1168 if (i == libcfs_nnetstrfns)
1171 nob = strlen(nf->nf_name);
1173 if (strlen(str) == (unsigned int)nob) {
1176 if (nf->nf_type == LOLND) /* net number not allowed */
1181 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1182 i != (int)strlen(str))
1186 *net = LNET_MKNET(nf->nf_type, netnum);
1191 libcfs_str2net(const char *str)
1195 if (libcfs_str2net_internal(str, &net))
1198 return LNET_NIDNET(LNET_NID_ANY);
1200 EXPORT_SYMBOL(libcfs_str2net);
1203 libcfs_str2nid(const char *str)
1205 const char *sep = strchr(str, '@');
1206 struct netstrfns *nf;
1211 nf = libcfs_str2net_internal(sep + 1, &net);
1213 return LNET_NID_ANY;
1215 sep = str + strlen(str);
1216 net = LNET_MKNET(SOCKLND, 0);
1217 nf = libcfs_lnd2netstrfns(SOCKLND);
1221 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1222 return LNET_NID_ANY;
1224 return LNET_MKNID(net, addr);
1226 EXPORT_SYMBOL(libcfs_str2nid);
1229 libcfs_id2str(struct lnet_process_id id)
1231 char *str = libcfs_next_nidstring();
1233 if (id.pid == LNET_PID_ANY) {
1234 snprintf(str, LNET_NIDSTR_SIZE,
1235 "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1239 snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1240 id.pid & LNET_PID_USERFLAG ? "U" : "",
1241 id.pid & ~LNET_PID_USERFLAG, libcfs_nid2str(id.nid));
1244 EXPORT_SYMBOL(libcfs_id2str);
1247 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1249 if (!strcmp(str, "*")) {
1250 *nidp = LNET_NID_ANY;
1254 *nidp = libcfs_str2nid(str);
1255 return *nidp != LNET_NID_ANY;
1257 EXPORT_SYMBOL(libcfs_str2anynid);