Whamcloud - gitweb
LU-13785 lnet: Use lr_hops for avoid_asym_router_failure
[fs/lustre-release.git] / lnet / lnet / nidstrings.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/lnet/nidstrings.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40 #include <uapi/linux/lnet/nidstr.h>
41 #include <lnet/lib-types.h>
42
43 /* max value for numeric network address */
44 #define MAX_NUMERIC_VALUE 0xffffffff
45
46 #define IPSTRING_LENGTH 16
47
48 /* CAVEAT VENDITOR! Keep the canonical string representation of nets/nids
49  * consistent in all conversion functions.  Some code fragments are copied
50  * around for the sake of clarity...
51  */
52
53 /* CAVEAT EMPTOR! Racey temporary buffer allocation!
54  * Choose the number of nidstrings to support the MAXIMUM expected number of
55  * concurrent users.  If there are more, the returned string will be volatile.
56  * NB this number must allow for a process to be descheduled for a timeslice
57  * between getting its string and using it.
58  */
59
60 static char      libcfs_nidstrings[LNET_NIDSTR_COUNT][LNET_NIDSTR_SIZE];
61 static int       libcfs_nidstring_idx;
62
63 static DEFINE_SPINLOCK(libcfs_nidstring_lock);
64
65 static struct netstrfns *libcfs_namenum2netstrfns(const char *name);
66
67 char *
68 libcfs_next_nidstring(void)
69 {
70         char          *str;
71         unsigned long  flags;
72
73         spin_lock_irqsave(&libcfs_nidstring_lock, flags);
74
75         str = libcfs_nidstrings[libcfs_nidstring_idx++];
76         if (libcfs_nidstring_idx == ARRAY_SIZE(libcfs_nidstrings))
77                 libcfs_nidstring_idx = 0;
78
79         spin_unlock_irqrestore(&libcfs_nidstring_lock, flags);
80         return str;
81 }
82 EXPORT_SYMBOL(libcfs_next_nidstring);
83
84 /**
85  * Nid range list syntax.
86  * \verbatim
87  *
88  * <nidlist>         :== <nidrange> [ ' ' <nidrange> ]
89  * <nidrange>        :== <addrrange> '@' <net>
90  * <addrrange>       :== '*' |
91  *                       <ipaddr_range> |
92  *                       <cfs_expr_list>
93  * <ipaddr_range>    :== <cfs_expr_list>.<cfs_expr_list>.<cfs_expr_list>.
94  *                       <cfs_expr_list>
95  * <cfs_expr_list>   :== <number> |
96  *                       <expr_list>
97  * <expr_list>       :== '[' <range_expr> [ ',' <range_expr>] ']'
98  * <range_expr>      :== <number> |
99  *                       <number> '-' <number> |
100  *                       <number> '-' <number> '/' <number>
101  * <net>             :== <netname> | <netname><number>
102  * <netname>         :== "lo" | "tcp" | "o2ib" | "cib" | "openib" | "iib" |
103  *                       "vib" | "ra" | "elan" | "mx" | "ptl"
104  * \endverbatim
105  */
106
107 /**
108  * Structure to represent \<nidrange\> token of the syntax.
109  *
110  * One of this is created for each \<net\> parsed.
111  */
112 struct nidrange {
113         /**
114          * Link to list of this structures which is built on nid range
115          * list parsing.
116          */
117         struct list_head nr_link;
118         /**
119          * List head for addrrange::ar_link.
120          */
121         struct list_head nr_addrranges;
122         /**
123          * Flag indicating that *@<net> is found.
124          */
125         int nr_all;
126         /**
127          * Pointer to corresponding element of libcfs_netstrfns.
128          */
129         struct netstrfns *nr_netstrfns;
130         /**
131          * Number of network. E.g. 5 if \<net\> is "elan5".
132          */
133         int nr_netnum;
134 };
135
136 /**
137  * Structure to represent \<addrrange\> token of the syntax.
138  */
139 struct addrrange {
140         /**
141          * Link to nidrange::nr_addrranges.
142          */
143         struct list_head ar_link;
144         /**
145          * List head for cfs_expr_list::el_list.
146          */
147         struct list_head ar_numaddr_ranges;
148 };
149
150 /**
151  * Parses \<addrrange\> token on the syntax.
152  *
153  * Allocates struct addrrange and links to \a nidrange via
154  * (nidrange::nr_addrranges)
155  *
156  * \retval 0 if \a src parses to '*' | \<ipaddr_range\> | \<cfs_expr_list\>
157  * \retval -errno otherwise
158  */
159 static int
160 parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
161 {
162         struct addrrange *addrrange;
163
164         if (src->ls_len == 1 && src->ls_str[0] == '*') {
165                 nidrange->nr_all = 1;
166                 return 0;
167         }
168
169         CFS_ALLOC_PTR(addrrange);
170         if (addrrange == NULL)
171                 return -ENOMEM;
172         list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
173         INIT_LIST_HEAD(&addrrange->ar_numaddr_ranges);
174
175         return nidrange->nr_netstrfns->nf_parse_addrlist(src->ls_str,
176                                                 src->ls_len,
177                                                 &addrrange->ar_numaddr_ranges);
178 }
179
180 /**
181  * Finds or creates struct nidrange.
182  *
183  * Checks if \a src is a valid network name, looks for corresponding
184  * nidrange on the ist of nidranges (\a nidlist), creates new struct
185  * nidrange if it is not found.
186  *
187  * \retval pointer to struct nidrange matching network specified via \a src
188  * \retval NULL if \a src does not match any network
189  */
190 static struct nidrange *
191 add_nidrange(const struct cfs_lstr *src,
192              struct list_head *nidlist)
193 {
194         struct netstrfns *nf;
195         struct nidrange *nr;
196         int endlen;
197         unsigned netnum;
198
199         if (src->ls_len >= LNET_NIDSTR_SIZE)
200                 return NULL;
201
202         nf = libcfs_namenum2netstrfns(src->ls_str);
203         if (nf == NULL)
204                 return NULL;
205         endlen = src->ls_len - strlen(nf->nf_name);
206         if (endlen == 0)
207                 /* network name only, e.g. "elan" or "tcp" */
208                 netnum = 0;
209         else {
210                 /* e.g. "elan25" or "tcp23", refuse to parse if
211                  * network name is not appended with decimal or
212                  * hexadecimal number */
213                 if (!cfs_str2num_check(src->ls_str + strlen(nf->nf_name),
214                                        endlen, &netnum, 0, MAX_NUMERIC_VALUE))
215                         return NULL;
216         }
217
218         list_for_each_entry(nr, nidlist, nr_link) {
219                 if (nr->nr_netstrfns != nf)
220                         continue;
221                 if (nr->nr_netnum != netnum)
222                         continue;
223                 return nr;
224         }
225
226         CFS_ALLOC_PTR(nr);
227         if (nr == NULL)
228                 return NULL;
229         list_add_tail(&nr->nr_link, nidlist);
230         INIT_LIST_HEAD(&nr->nr_addrranges);
231         nr->nr_netstrfns = nf;
232         nr->nr_all = 0;
233         nr->nr_netnum = netnum;
234
235         return nr;
236 }
237
238 /**
239  * Parses \<nidrange\> token of the syntax.
240  *
241  * \retval 1 if \a src parses to \<addrrange\> '@' \<net\>
242  * \retval 0 otherwise
243  */
244 static int
245 parse_nidrange(struct cfs_lstr *src, struct list_head *nidlist)
246 {
247         struct cfs_lstr addrrange;
248         struct cfs_lstr net;
249         struct nidrange *nr;
250
251         if (cfs_gettok(src, '@', &addrrange) == 0)
252                 goto failed;
253
254         if (cfs_gettok(src, '@', &net) == 0 || src->ls_str != NULL)
255                 goto failed;
256
257         nr = add_nidrange(&net, nidlist);
258         if (nr == NULL)
259                 goto failed;
260
261         if (parse_addrange(&addrrange, nr) != 0)
262                 goto failed;
263
264         return 1;
265 failed:
266         return 0;
267 }
268
269 /**
270  * Frees addrrange structures of \a list.
271  *
272  * For each struct addrrange structure found on \a list it frees
273  * cfs_expr_list list attached to it and frees the addrrange itself.
274  *
275  * \retval none
276  */
277 static void
278 free_addrranges(struct list_head *list)
279 {
280         while (!list_empty(list)) {
281                 struct addrrange *ar;
282
283                 ar = list_entry(list->next, struct addrrange, ar_link);
284
285                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
286                 list_del(&ar->ar_link);
287                 CFS_FREE_PTR(ar);
288         }
289 }
290
291 /**
292  * Frees nidrange strutures of \a list.
293  *
294  * For each struct nidrange structure found on \a list it frees
295  * addrrange list attached to it and frees the nidrange itself.
296  *
297  * \retval none
298  */
299 void
300 cfs_free_nidlist(struct list_head *list)
301 {
302         struct list_head *pos, *next;
303         struct nidrange *nr;
304
305         list_for_each_safe(pos, next, list) {
306                 nr = list_entry(pos, struct nidrange, nr_link);
307                 free_addrranges(&nr->nr_addrranges);
308                 list_del(pos);
309                 CFS_FREE_PTR(nr);
310         }
311 }
312 EXPORT_SYMBOL(cfs_free_nidlist);
313
314 /**
315  * Parses nid range list.
316  *
317  * Parses with rigorous syntax and overflow checking \a str into
318  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
319  * structures and links that structure to \a nidlist. The resulting
320  * list can be used to match a NID againts set of NIDS defined by \a
321  * str.
322  * \see cfs_match_nid
323  *
324  * \retval 1 on success
325  * \retval 0 otherwise
326  */
327 int
328 cfs_parse_nidlist(char *str, int len, struct list_head *nidlist)
329 {
330         struct cfs_lstr src;
331         struct cfs_lstr res;
332         int rc;
333
334         src.ls_str = str;
335         src.ls_len = len;
336         INIT_LIST_HEAD(nidlist);
337         while (src.ls_str) {
338                 rc = cfs_gettok(&src, ' ', &res);
339                 if (rc == 0) {
340                         cfs_free_nidlist(nidlist);
341                         return 0;
342                 }
343                 rc = parse_nidrange(&res, nidlist);
344                 if (rc == 0) {
345                         cfs_free_nidlist(nidlist);
346                         return 0;
347                 }
348         }
349         return 1;
350 }
351 EXPORT_SYMBOL(cfs_parse_nidlist);
352
353 /**
354  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
355  *
356  * \see cfs_parse_nidlist()
357  *
358  * \retval 1 on match
359  * \retval 0  otherwises
360  */
361 int cfs_match_nid(lnet_nid_t nid, struct list_head *nidlist)
362 {
363         struct nidrange *nr;
364         struct addrrange *ar;
365
366         list_for_each_entry(nr, nidlist, nr_link) {
367                 if (nr->nr_netstrfns->nf_type != LNET_NETTYP(LNET_NIDNET(nid)))
368                         continue;
369                 if (nr->nr_netnum != LNET_NETNUM(LNET_NIDNET(nid)))
370                         continue;
371                 if (nr->nr_all)
372                         return 1;
373                 list_for_each_entry(ar, &nr->nr_addrranges, ar_link)
374                         if (nr->nr_netstrfns->nf_match_addr(LNET_NIDADDR(nid),
375                                                         &ar->ar_numaddr_ranges))
376                                 return 1;
377         }
378         return 0;
379 }
380 EXPORT_SYMBOL(cfs_match_nid);
381
382 /**
383  * Print the network part of the nidrange \a nr into the specified \a buffer.
384  *
385  * \retval number of characters written
386  */
387 static int
388 cfs_print_network(char *buffer, int count, struct nidrange *nr)
389 {
390         struct netstrfns *nf = nr->nr_netstrfns;
391
392         if (nr->nr_netnum == 0)
393                 return scnprintf(buffer, count, "@%s", nf->nf_name);
394         else
395                 return scnprintf(buffer, count, "@%s%u",
396                                     nf->nf_name, nr->nr_netnum);
397 }
398
399 /**
400  * Print a list of addrrange (\a addrranges) into the specified \a buffer.
401  * At max \a count characters can be printed into \a buffer.
402  *
403  * \retval number of characters written
404  */
405 static int
406 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
407                      struct nidrange *nr)
408 {
409         int i = 0;
410         struct addrrange *ar;
411         struct netstrfns *nf = nr->nr_netstrfns;
412
413         list_for_each_entry(ar, addrranges, ar_link) {
414                 if (i != 0)
415                         i += scnprintf(buffer + i, count - i, " ");
416                 i += nf->nf_print_addrlist(buffer + i, count - i,
417                                            &ar->ar_numaddr_ranges);
418                 i += cfs_print_network(buffer + i, count - i, nr);
419         }
420         return i;
421 }
422
423 /**
424  * Print a list of nidranges (\a nidlist) into the specified \a buffer.
425  * At max \a count characters can be printed into \a buffer.
426  * Nidranges are separated by a space character.
427  *
428  * \retval number of characters written
429  */
430 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
431 {
432         int i = 0;
433         struct nidrange *nr;
434
435         if (count <= 0)
436                 return 0;
437
438         list_for_each_entry(nr, nidlist, nr_link) {
439                 if (i != 0)
440                         i += scnprintf(buffer + i, count - i, " ");
441
442                 if (nr->nr_all != 0) {
443                         LASSERT(list_empty(&nr->nr_addrranges));
444                         i += scnprintf(buffer + i, count - i, "*");
445                         i += cfs_print_network(buffer + i, count - i, nr);
446                 } else {
447                         i += cfs_print_addrranges(buffer + i, count - i,
448                                                   &nr->nr_addrranges, nr);
449                 }
450         }
451         return i;
452 }
453 EXPORT_SYMBOL(cfs_print_nidlist);
454
455 static int
456 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
457 {
458         *addr = 0;
459         return 1;
460 }
461
462 static void
463 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
464 {
465         snprintf(str, size, "%u.%u.%u.%u",
466                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
467                  (addr >> 8) & 0xff, addr & 0xff);
468 }
469
470 /* CAVEAT EMPTOR XscanfX
471  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
472  * sscanf may return immediately if it sees the terminating '0' in a string, so
473  * I initialise the %n variable to the expected length.  If sscanf sets it;
474  * fine, if it doesn't, then the scan ended at the end of the string, which is
475  * fine too :) */
476 static int
477 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
478 {
479         unsigned int    a;
480         unsigned int    b;
481         unsigned int    c;
482         unsigned int    d;
483         int             n = nob; /* XscanfX */
484
485         /* numeric IP? */
486         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
487             n == nob &&
488             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
489             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
490                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
491                 return 1;
492         }
493         return 0;
494 }
495
496 /* Used by lnet/config.c so it can't be static */
497 int
498 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
499 {
500         struct cfs_expr_list *el;
501         struct cfs_lstr src;
502         int rc;
503         int i;
504
505         src.ls_str = str;
506         src.ls_len = len;
507         i = 0;
508
509         while (src.ls_str != NULL) {
510                 struct cfs_lstr res;
511
512                 if (!cfs_gettok(&src, '.', &res)) {
513                         rc = -EINVAL;
514                         goto out;
515                 }
516
517                 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
518                 if (rc != 0)
519                         goto out;
520
521                 list_add_tail(&el->el_link, list);
522                 i++;
523         }
524
525         if (i == 4)
526                 return 0;
527
528         rc = -EINVAL;
529 out:
530         cfs_expr_list_free_list(list);
531
532         return rc;
533 }
534
535 static int
536 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
537 {
538         int i = 0, j = 0;
539         struct cfs_expr_list *el;
540
541         list_for_each_entry(el, list, el_link) {
542                 LASSERT(j++ < 4);
543                 if (i != 0)
544                         i += scnprintf(buffer + i, count - i, ".");
545                 i += cfs_expr_list_print(buffer + i, count - i, el);
546         }
547         return i;
548 }
549
550 /**
551  * Matches address (\a addr) against address set encoded in \a list.
552  *
553  * \retval 1 if \a addr matches
554  * \retval 0 otherwise
555  */
556 int
557 cfs_ip_addr_match(__u32 addr, struct list_head *list)
558 {
559         struct cfs_expr_list *el;
560         int i = 0;
561
562         list_for_each_entry_reverse(el, list, el_link) {
563                 if (!cfs_expr_list_match(addr & 0xff, el))
564                         return 0;
565                 addr >>= 8;
566                 i++;
567         }
568
569         return i == 4;
570 }
571
572 /**
573  * Print the network part of the nidrange \a nr into the specified \a buffer.
574  *
575  * \retval number of characters written
576  */
577 static void
578 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
579 {
580         snprintf(str, size, "%u", addr);
581 }
582
583 static int
584 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
585 {
586         int     n;
587
588         n = nob;
589         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
590                 return 1;
591
592         n = nob;
593         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
594                 return 1;
595
596         n = nob;
597         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
598                 return 1;
599
600         return 0;
601 }
602
603 /**
604  * Nf_parse_addrlist method for networks using numeric addresses.
605  *
606  * Examples of such networks are gm and elan.
607  *
608  * \retval 0 if \a str parsed to numeric address
609  * \retval errno otherwise
610  */
611 int
612 libcfs_num_parse(char *str, int len, struct list_head *list)
613 {
614         struct cfs_expr_list *el;
615         int     rc;
616
617         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
618         if (rc == 0)
619                 list_add_tail(&el->el_link, list);
620
621         return rc;
622 }
623
624 static int
625 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
626 {
627         int i = 0, j = 0;
628         struct cfs_expr_list *el;
629
630         list_for_each_entry(el, list, el_link) {
631                 LASSERT(j++ < 1);
632                 i += cfs_expr_list_print(buffer + i, count - i, el);
633         }
634         return i;
635 }
636
637 /*
638  * Nf_match_addr method for networks using numeric addresses
639  *
640  * \retval 1 on match
641  * \retval 0 otherwise
642  */
643 static int
644 libcfs_num_match(__u32 addr, struct list_head *numaddr)
645 {
646         struct cfs_expr_list *el;
647
648         LASSERT(!list_empty(numaddr));
649         el = list_entry(numaddr->next, struct cfs_expr_list, el_link);
650
651         return cfs_expr_list_match(addr, el);
652 }
653
654 static struct netstrfns libcfs_netstrfns[] = {
655         { .nf_type              = LOLND,
656           .nf_name              = "lo",
657           .nf_modname           = "klolnd",
658           .nf_addr2str          = libcfs_decnum_addr2str,
659           .nf_str2addr          = libcfs_lo_str2addr,
660           .nf_parse_addrlist    = libcfs_num_parse,
661           .nf_print_addrlist    = libcfs_num_addr_range_print,
662           .nf_match_addr        = libcfs_num_match
663         },
664         { .nf_type              = SOCKLND,
665           .nf_name              = "tcp",
666           .nf_modname           = "ksocklnd",
667           .nf_addr2str          = libcfs_ip_addr2str,
668           .nf_str2addr          = libcfs_ip_str2addr,
669           .nf_parse_addrlist    = cfs_ip_addr_parse,
670           .nf_print_addrlist    = libcfs_ip_addr_range_print,
671           .nf_match_addr        = cfs_ip_addr_match
672         },
673         { .nf_type              = O2IBLND,
674           .nf_name              = "o2ib",
675           .nf_modname           = "ko2iblnd",
676           .nf_addr2str          = libcfs_ip_addr2str,
677           .nf_str2addr          = libcfs_ip_str2addr,
678           .nf_parse_addrlist    = cfs_ip_addr_parse,
679           .nf_print_addrlist    = libcfs_ip_addr_range_print,
680           .nf_match_addr        = cfs_ip_addr_match
681         },
682         { .nf_type              = GNILND,
683           .nf_name              = "gni",
684           .nf_modname           = "kgnilnd",
685           .nf_addr2str          = libcfs_decnum_addr2str,
686           .nf_str2addr          = libcfs_num_str2addr,
687           .nf_parse_addrlist    = libcfs_num_parse,
688           .nf_print_addrlist    = libcfs_num_addr_range_print,
689           .nf_match_addr        = libcfs_num_match
690         },
691         { .nf_type              = GNIIPLND,
692           .nf_name              = "gip",
693           .nf_modname           = "kgnilnd",
694           .nf_addr2str          = libcfs_ip_addr2str,
695           .nf_str2addr          = libcfs_ip_str2addr,
696           .nf_parse_addrlist    = cfs_ip_addr_parse,
697           .nf_print_addrlist    = libcfs_ip_addr_range_print,
698           .nf_match_addr        = cfs_ip_addr_match
699         },
700         { .nf_type              = PTL4LND,
701           .nf_name              = "ptlf",
702           .nf_modname           = "kptl4lnd",
703           .nf_addr2str          = libcfs_decnum_addr2str,
704           .nf_str2addr          = libcfs_num_str2addr,
705           .nf_parse_addrlist    = libcfs_num_parse,
706           .nf_print_addrlist    = libcfs_num_addr_range_print,
707           .nf_match_addr        = libcfs_num_match
708         },
709 };
710
711 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
712
713 static struct netstrfns *
714 type2net_info(__u32 net_type)
715 {
716         int i;
717
718         for (i = 0; i < libcfs_nnetstrfns; i++) {
719                 if (libcfs_netstrfns[i].nf_type == net_type)
720                         return &libcfs_netstrfns[i];
721         }
722
723         return NULL;
724 }
725
726 int
727 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
728 {
729         __u32 net_num;
730
731         if (!net_num_list)
732                 return 0;
733
734         if (net_type != LNET_NETTYP(net_id))
735                 return 0;
736
737         net_num = LNET_NETNUM(net_id);
738
739         /* if there is a net number but the list passed in is empty, then
740          * there is no match.
741          */
742         if (!net_num && list_empty(net_num_list))
743                 return 1;
744         else if (list_empty(net_num_list))
745                 return 0;
746
747         if (!libcfs_num_match(net_num, net_num_list))
748                 return 0;
749
750         return 1;
751 }
752
753 int
754 cfs_match_nid_net(lnet_nid_t nid, __u32 net_type,
755                   struct list_head *net_num_list,
756                   struct list_head *addr)
757 {
758         __u32 address;
759         struct netstrfns *nf;
760
761         if (!addr || !net_num_list)
762                 return 0;
763
764         nf = type2net_info(LNET_NETTYP(LNET_NIDNET(nid)));
765         if (!nf || !net_num_list || !addr)
766                 return 0;
767
768         address = LNET_NIDADDR(nid);
769
770         /* if either the address or net number don't match then no match */
771         if (!nf->nf_match_addr(address, addr) ||
772             !cfs_match_net(LNET_NIDNET(nid), net_type, net_num_list))
773                 return 0;
774
775         return 1;
776 }
777 EXPORT_SYMBOL(cfs_match_nid_net);
778
779 static struct netstrfns *
780 libcfs_lnd2netstrfns(__u32 lnd)
781 {
782         int     i;
783
784         for (i = 0; i < libcfs_nnetstrfns; i++)
785                 if (lnd == libcfs_netstrfns[i].nf_type)
786                         return &libcfs_netstrfns[i];
787
788         return NULL;
789 }
790
791 static struct netstrfns *
792 libcfs_namenum2netstrfns(const char *name)
793 {
794         struct netstrfns *nf;
795         int               i;
796
797         for (i = 0; i < libcfs_nnetstrfns; i++) {
798                 nf = &libcfs_netstrfns[i];
799                 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
800                         return nf;
801         }
802         return NULL;
803 }
804
805 static struct netstrfns *
806 libcfs_name2netstrfns(const char *name)
807 {
808         int    i;
809
810         for (i = 0; i < libcfs_nnetstrfns; i++)
811                 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
812                         return &libcfs_netstrfns[i];
813
814         return NULL;
815 }
816
817 int
818 libcfs_isknown_lnd(__u32 lnd)
819 {
820         return libcfs_lnd2netstrfns(lnd) != NULL;
821 }
822 EXPORT_SYMBOL(libcfs_isknown_lnd);
823
824 char *
825 libcfs_lnd2modname(__u32 lnd)
826 {
827         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
828
829         return (nf == NULL) ? NULL : nf->nf_modname;
830 }
831 EXPORT_SYMBOL(libcfs_lnd2modname);
832
833 int
834 libcfs_str2lnd(const char *str)
835 {
836         struct netstrfns *nf = libcfs_name2netstrfns(str);
837
838         if (nf != NULL)
839                 return nf->nf_type;
840
841         return -ENXIO;
842 }
843 EXPORT_SYMBOL(libcfs_str2lnd);
844
845 char *
846 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
847 {
848         struct netstrfns *nf;
849
850         nf = libcfs_lnd2netstrfns(lnd);
851         if (nf == NULL)
852                 snprintf(buf, buf_size, "?%u?", lnd);
853         else
854                 snprintf(buf, buf_size, "%s", nf->nf_name);
855
856         return buf;
857 }
858 EXPORT_SYMBOL(libcfs_lnd2str_r);
859
860 char *
861 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
862 {
863         __u32             nnum = LNET_NETNUM(net);
864         __u32             lnd  = LNET_NETTYP(net);
865         struct netstrfns *nf;
866
867         nf = libcfs_lnd2netstrfns(lnd);
868         if (nf == NULL)
869                 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
870         else if (nnum == 0)
871                 snprintf(buf, buf_size, "%s", nf->nf_name);
872         else
873                 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
874
875         return buf;
876 }
877 EXPORT_SYMBOL(libcfs_net2str_r);
878
879 char *
880 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
881 {
882         __u32             addr = LNET_NIDADDR(nid);
883         __u32             net  = LNET_NIDNET(nid);
884         __u32             nnum = LNET_NETNUM(net);
885         __u32             lnd  = LNET_NETTYP(net);
886         struct netstrfns *nf;
887
888         if (nid == LNET_NID_ANY) {
889                 strncpy(buf, "<?>", buf_size);
890                 buf[buf_size - 1] = '\0';
891                 return buf;
892         }
893
894         nf = libcfs_lnd2netstrfns(lnd);
895         if (nf == NULL) {
896                 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
897         } else {
898                 size_t addr_len;
899
900                 nf->nf_addr2str(addr, buf, buf_size);
901                 addr_len = strlen(buf);
902                 if (nnum == 0)
903                         snprintf(buf + addr_len, buf_size - addr_len, "@%s",
904                                  nf->nf_name);
905                 else
906                         snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
907                                  nf->nf_name, nnum);
908         }
909
910         return buf;
911 }
912 EXPORT_SYMBOL(libcfs_nid2str_r);
913
914 static struct netstrfns *
915 libcfs_str2net_internal(const char *str, __u32 *net)
916 {
917         struct netstrfns *nf = NULL;
918         int               nob;
919         unsigned int      netnum;
920         int               i;
921
922         for (i = 0; i < libcfs_nnetstrfns; i++) {
923                 nf = &libcfs_netstrfns[i];
924                 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
925                         break;
926         }
927
928         if (i == libcfs_nnetstrfns)
929                 return NULL;
930
931         nob = strlen(nf->nf_name);
932
933         if (strlen(str) == (unsigned int)nob) {
934                 netnum = 0;
935         } else {
936                 if (nf->nf_type == LOLND) /* net number not allowed */
937                         return NULL;
938
939                 str += nob;
940                 i = strlen(str);
941                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
942                     i != (int)strlen(str))
943                         return NULL;
944         }
945
946         *net = LNET_MKNET(nf->nf_type, netnum);
947         return nf;
948 }
949
950 __u32
951 libcfs_str2net(const char *str)
952 {
953         __u32  net;
954
955         if (libcfs_str2net_internal(str, &net) != NULL)
956                 return net;
957
958         return LNET_NET_ANY;
959 }
960 EXPORT_SYMBOL(libcfs_str2net);
961
962 lnet_nid_t
963 libcfs_str2nid(const char *str)
964 {
965         const char       *sep = strchr(str, '@');
966         struct netstrfns *nf;
967         __u32             net;
968         __u32             addr;
969
970         if (sep != NULL) {
971                 nf = libcfs_str2net_internal(sep + 1, &net);
972                 if (nf == NULL)
973                         return LNET_NID_ANY;
974         } else {
975                 sep = str + strlen(str);
976                 net = LNET_MKNET(SOCKLND, 0);
977                 nf = libcfs_lnd2netstrfns(SOCKLND);
978                 LASSERT(nf != NULL);
979         }
980
981         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
982                 return LNET_NID_ANY;
983
984         return LNET_MKNID(net, addr);
985 }
986 EXPORT_SYMBOL(libcfs_str2nid);
987
988 char *
989 libcfs_id2str(struct lnet_process_id id)
990 {
991         char *str = libcfs_next_nidstring();
992
993         if (id.pid == LNET_PID_ANY) {
994                 snprintf(str, LNET_NIDSTR_SIZE,
995                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
996                 return str;
997         }
998
999         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1000                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1001                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1002         return str;
1003 }
1004 EXPORT_SYMBOL(libcfs_id2str);
1005
1006 int
1007 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1008 {
1009         if (!strcmp(str, "*")) {
1010                 *nidp = LNET_NID_ANY;
1011                 return 1;
1012         }
1013
1014         *nidp = libcfs_str2nid(str);
1015         return *nidp != LNET_NID_ANY;
1016 }
1017 EXPORT_SYMBOL(libcfs_str2anynid);