Whamcloud - gitweb
LU-9859 lnet: simplify cfs_parse_nidlist()
[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  *
31  * lnet/lnet/nidstrings.c
32  *
33  * Author: Phil Schwan <phil@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_LNET
37
38 #include <linux/sunrpc/addr.h>
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(char *str, struct nidrange *nidrange)
161 {
162         struct addrrange *addrrange;
163
164         if (strcmp(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(str, strlen(str),
176                                                 &addrrange->ar_numaddr_ranges);
177 }
178
179 /**
180  * Finds or creates struct nidrange.
181  *
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.
185  *
186  * \retval pointer to struct nidrange matching network specified via \a src
187  * \retval NULL if \a src does not match any network
188  */
189 static struct nidrange *
190 add_nidrange(char *str, struct list_head *nidlist)
191 {
192         struct netstrfns *nf;
193         struct nidrange *nr;
194         char *end;
195         unsigned int netnum;
196
197         nf = libcfs_namenum2netstrfns(str);
198         if (nf == NULL)
199                 return NULL;
200         end = str + strlen(nf->nf_name);
201         if (!*end) {
202                 /* network name only, e.g. "elan" or "tcp" */
203                 netnum = 0;
204         } else {
205                 /* e.g. "elan25" or "tcp23", refuse to parse if
206                  * network name is not appended with decimal or
207                  * hexadecimal number
208                  */
209                 if (kstrtouint(end, 0, &netnum) != 0)
210                         return NULL;
211         }
212
213         list_for_each_entry(nr, nidlist, nr_link) {
214                 if (nr->nr_netstrfns != nf)
215                         continue;
216                 if (nr->nr_netnum != netnum)
217                         continue;
218                 return nr;
219         }
220
221         CFS_ALLOC_PTR(nr);
222         if (nr == NULL)
223                 return NULL;
224         list_add_tail(&nr->nr_link, nidlist);
225         INIT_LIST_HEAD(&nr->nr_addrranges);
226         nr->nr_netstrfns = nf;
227         nr->nr_all = 0;
228         nr->nr_netnum = netnum;
229
230         return nr;
231 }
232
233 /**
234  * Parses \<nidrange\> token of the syntax.
235  *
236  * \retval 0 if \a src parses to \<addrrange\> '@' \<net\>
237  * \retval -EINVAL otherwise
238  */
239 static int
240 parse_nidrange(char *str, struct list_head *nidlist)
241 {
242         char *addrrange;
243         char *net;
244         struct nidrange *nr;
245
246         addrrange = strim(strsep(&str, "@"));
247         if (!str)
248                 goto failed;
249
250         net = strim(str);
251         if (strchr(net, '@') != NULL || !*net)
252                 goto failed;
253
254         nr = add_nidrange(net, nidlist);
255         if (nr == NULL)
256                 goto failed;
257
258         if (parse_addrange(addrrange, nr) != 0)
259                 goto failed;
260
261         return 0;
262 failed:
263         return -EINVAL;
264 }
265
266 /**
267  * Frees addrrange structures of \a list.
268  *
269  * For each struct addrrange structure found on \a list it frees
270  * cfs_expr_list list attached to it and frees the addrrange itself.
271  *
272  * \retval none
273  */
274 static void
275 free_addrranges(struct list_head *list)
276 {
277         struct addrrange *ar;
278
279         while ((ar = list_first_entry_or_null(list,
280                                               struct addrrange,
281                                               ar_link)) != NULL) {
282                 cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
283                 list_del(&ar->ar_link);
284                 CFS_FREE_PTR(ar);
285         }
286 }
287
288 /**
289  * Frees nidrange strutures of \a list.
290  *
291  * For each struct nidrange structure found on \a list it frees
292  * addrrange list attached to it and frees the nidrange itself.
293  *
294  * \retval none
295  */
296 void
297 cfs_free_nidlist(struct list_head *list)
298 {
299         struct list_head *pos, *next;
300         struct nidrange *nr;
301
302         list_for_each_safe(pos, next, list) {
303                 nr = list_entry(pos, struct nidrange, nr_link);
304                 free_addrranges(&nr->nr_addrranges);
305                 list_del(pos);
306                 CFS_FREE_PTR(nr);
307         }
308 }
309 EXPORT_SYMBOL(cfs_free_nidlist);
310
311 /**
312  * Parses nid range list.
313  *
314  * Parses with rigorous syntax and overflow checking \a str into
315  * \<nidrange\> [ ' ' \<nidrange\> ], compiles \a str into set of
316  * structures and links that structure to \a nidlist. The resulting
317  * list can be used to match a NID againts set of NIDS defined by \a
318  * str.
319  * \see cfs_match_nid
320  *
321  * \retval 0 on success
322  * \retval -errno otherwise (-ENOMEM or -EINVAL)
323  */
324 int
325 cfs_parse_nidlist(char *orig, struct list_head *nidlist)
326 {
327         int rc = 0;
328         char *str;
329
330         orig = kstrdup(orig, GFP_KERNEL);
331         if (!orig)
332                 return -ENOMEM;
333
334         INIT_LIST_HEAD(nidlist);
335         str = orig;
336         while (rc == 0 && str) {
337                 char *tok = strsep(&str, " ");
338
339                 if (*tok)
340                         rc = parse_nidrange(tok, nidlist);
341         }
342         kfree(orig);
343         if (rc)
344                 cfs_free_nidlist(nidlist);
345         else if (list_empty(nidlist))
346                 rc = -EINVAL;
347         return rc;
348 }
349 EXPORT_SYMBOL(cfs_parse_nidlist);
350
351 /**
352  * Matches a nid (\a nid) against the compiled list of nidranges (\a nidlist).
353  *
354  * \see cfs_parse_nidlist()
355  *
356  * \retval 1 on match
357  * \retval 0  otherwises
358  */
359 int cfs_match_nid(struct lnet_nid *nid, struct list_head *nidlist)
360 {
361         struct nidrange *nr;
362         struct addrrange *ar;
363
364         if (!nid_is_nid4(nid))
365                 return 0;
366         list_for_each_entry(nr, nidlist, nr_link) {
367                 if (nr->nr_netstrfns->nf_type != nid->nid_type)
368                         continue;
369                 if (nr->nr_netnum != be16_to_cpu(nid->nid_num))
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(
375                                     be32_to_cpu(nid->nid_addr[0]),
376                                     &ar->ar_numaddr_ranges))
377                                 return 1;
378         }
379         return 0;
380 }
381 EXPORT_SYMBOL(cfs_match_nid);
382
383 /**
384  * Print the network part of the nidrange \a nr into the specified \a buffer.
385  *
386  * \retval number of characters written
387  */
388 static int
389 cfs_print_network(char *buffer, int count, struct nidrange *nr)
390 {
391         struct netstrfns *nf = nr->nr_netstrfns;
392
393         if (nr->nr_netnum == 0)
394                 return scnprintf(buffer, count, "@%s", nf->nf_name);
395         else
396                 return scnprintf(buffer, count, "@%s%u",
397                                     nf->nf_name, nr->nr_netnum);
398 }
399
400 /**
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.
403  *
404  * \retval number of characters written
405  */
406 static int
407 cfs_print_addrranges(char *buffer, int count, struct list_head *addrranges,
408                      struct nidrange *nr)
409 {
410         int i = 0;
411         struct addrrange *ar;
412         struct netstrfns *nf = nr->nr_netstrfns;
413
414         list_for_each_entry(ar, addrranges, ar_link) {
415                 if (i != 0)
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);
420         }
421         return i;
422 }
423
424 /**
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.
428  *
429  * \retval number of characters written
430  */
431 int cfs_print_nidlist(char *buffer, int count, struct list_head *nidlist)
432 {
433         int i = 0;
434         struct nidrange *nr;
435
436         if (count <= 0)
437                 return 0;
438
439         list_for_each_entry(nr, nidlist, nr_link) {
440                 if (i != 0)
441                         i += scnprintf(buffer + i, count - i, " ");
442
443                 if (nr->nr_all != 0) {
444                         LASSERT(list_empty(&nr->nr_addrranges));
445                         i += scnprintf(buffer + i, count - i, "*");
446                         i += cfs_print_network(buffer + i, count - i, nr);
447                 } else {
448                         i += cfs_print_addrranges(buffer + i, count - i,
449                                                   &nr->nr_addrranges, nr);
450                 }
451         }
452         return i;
453 }
454 EXPORT_SYMBOL(cfs_print_nidlist);
455
456 static int
457 libcfs_lo_str2addr(const char *str, int nob, __u32 *addr)
458 {
459         *addr = 0;
460         return 1;
461 }
462
463 static void
464 libcfs_ip_addr2str(__u32 addr, char *str, size_t size)
465 {
466         snprintf(str, size, "%u.%u.%u.%u",
467                  (addr >> 24) & 0xff, (addr >> 16) & 0xff,
468                  (addr >> 8) & 0xff, addr & 0xff);
469 }
470
471 static void
472 libcfs_ip_addr2str_size(const __be32 *addr, size_t asize,
473                         char *str, size_t size)
474 {
475         struct sockaddr_storage sa = {};
476
477         switch (asize) {
478         case 4:
479                 sa.ss_family = AF_INET;
480                 memcpy(&((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
481                        addr, asize);
482                 break;
483         case 16:
484                 sa.ss_family = AF_INET6;
485                 memcpy(&((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
486                        addr, asize);
487                 break;
488         default:
489                 return;
490         }
491
492         rpc_ntop((struct sockaddr *)&sa, str, size);
493 }
494
495 /* CAVEAT EMPTOR XscanfX
496  * I use "%n" at the end of a sscanf format to detect trailing junk.  However
497  * sscanf may return immediately if it sees the terminating '0' in a string, so
498  * I initialise the %n variable to the expected length.  If sscanf sets it;
499  * fine, if it doesn't, then the scan ended at the end of the string, which is
500  * fine too :) */
501 static int
502 libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
503 {
504         unsigned int    a;
505         unsigned int    b;
506         unsigned int    c;
507         unsigned int    d;
508         int             n = nob; /* XscanfX */
509
510         /* numeric IP? */
511         if (sscanf(str, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n) >= 4 &&
512             n == nob &&
513             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
514             (c & ~0xff) == 0 && (d & ~0xff) == 0) {
515                 *addr = ((a<<24)|(b<<16)|(c<<8)|d);
516                 return 1;
517         }
518         return 0;
519 }
520
521 static int
522 libcfs_ip_str2addr_size(const char *str, int nob,
523                         __be32 *addr, size_t *alen)
524 {
525         struct sockaddr_storage sa;
526
527         /* Note: 'net' arg to rpc_pton is only needed for link-local
528          * addresses.  Such addresses would not work with LNet routing,
529          * so we can assume they aren't used.  So it doesn't matter
530          * which net namespace is passed.
531          */
532         if (rpc_pton(&init_net, str, nob,
533                      (struct sockaddr *)&sa, sizeof(sa)) == 0)
534                 return 0;
535         if (sa.ss_family == AF_INET6) {
536                 memcpy(addr,
537                        &((struct sockaddr_in6 *)(&sa))->sin6_addr.s6_addr,
538                        16);
539                 *alen = 16;
540                 return 1;
541         }
542         if (sa.ss_family == AF_INET) {
543                 memcpy(addr,
544                        &((struct sockaddr_in *)(&sa))->sin_addr.s_addr,
545                        4);
546                 *alen = 4;
547                 return 1;
548         }
549         return 0;
550 }
551
552
553 /* Used by lnet/config.c so it can't be static */
554 int
555 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
556 {
557         struct cfs_expr_list *el;
558         struct cfs_lstr src;
559         int rc;
560         int i;
561
562         src.ls_str = str;
563         src.ls_len = len;
564         i = 0;
565
566         while (src.ls_str != NULL) {
567                 struct cfs_lstr res;
568
569                 if (!cfs_gettok(&src, '.', &res)) {
570                         rc = -EINVAL;
571                         goto out;
572                 }
573
574                 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
575                 if (rc != 0)
576                         goto out;
577
578                 list_add_tail(&el->el_link, list);
579                 i++;
580         }
581
582         if (i == 4)
583                 return 0;
584
585         rc = -EINVAL;
586 out:
587         cfs_expr_list_free_list(list);
588
589         return rc;
590 }
591
592 /**
593  * Print the range expression \a re into specified \a buffer.
594  * If \a bracketed is true, expression does not need additional
595  * brackets.
596  *
597  * \retval number of characters written
598  */
599 static int
600 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
601                      bool bracketed)
602 {
603         int i;
604         char s[] = "[";
605         char e[] = "]";
606
607         if (bracketed)
608                 s[0] = e[0] = '\0';
609
610         if (expr->re_lo == expr->re_hi)
611                 i = scnprintf(buffer, count, "%u", expr->re_lo);
612         else if (expr->re_stride == 1)
613                 i = scnprintf(buffer, count, "%s%u-%u%s",
614                               s, expr->re_lo, expr->re_hi, e);
615         else
616                 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
617                               s, expr->re_lo, expr->re_hi,
618                               expr->re_stride, e);
619         return i;
620 }
621
622 /**
623  * Print a list of range expressions (\a expr_list) into specified \a buffer.
624  * If the list contains several expressions, separate them with comma
625  * and surround the list with brackets.
626  *
627  * \retval number of characters written
628  */
629 static int
630 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
631 {
632         struct cfs_range_expr *expr;
633         int i = 0, j = 0;
634         int numexprs = 0;
635
636         if (count <= 0)
637                 return 0;
638
639         list_for_each_entry(expr, &expr_list->el_exprs, re_link)
640                 numexprs++;
641
642         if (numexprs > 1)
643                 i += scnprintf(buffer + i, count - i, "[");
644
645         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
646                 if (j++ != 0)
647                         i += scnprintf(buffer + i, count - i, ",");
648                 i += cfs_range_expr_print(buffer + i, count - i, expr,
649                                           numexprs > 1);
650         }
651
652         if (numexprs > 1)
653                 i += scnprintf(buffer + i, count - i, "]");
654
655         return i;
656 }
657
658 static int
659 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
660 {
661         int i = 0, j = 0;
662         struct cfs_expr_list *el;
663
664         list_for_each_entry(el, list, el_link) {
665                 LASSERT(j++ < 4);
666                 if (i != 0)
667                         i += scnprintf(buffer + i, count - i, ".");
668                 i += cfs_expr_list_print(buffer + i, count - i, el);
669         }
670         return i;
671 }
672
673 /**
674  * Matches address (\a addr) against address set encoded in \a list.
675  *
676  * \retval 1 if \a addr matches
677  * \retval 0 otherwise
678  */
679 int
680 cfs_ip_addr_match(__u32 addr, struct list_head *list)
681 {
682         struct cfs_expr_list *el;
683         int i = 0;
684
685         list_for_each_entry_reverse(el, list, el_link) {
686                 if (!cfs_expr_list_match(addr & 0xff, el))
687                         return 0;
688                 addr >>= 8;
689                 i++;
690         }
691
692         return i == 4;
693 }
694
695 /**
696  * Print the network part of the nidrange \a nr into the specified \a buffer.
697  *
698  * \retval number of characters written
699  */
700 static void
701 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
702 {
703         snprintf(str, size, "%u", addr);
704 }
705
706 static int
707 libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
708 {
709         int     n;
710
711         n = nob;
712         if (sscanf(str, "0x%x%n", addr, &n) >= 1 && n == nob)
713                 return 1;
714
715         n = nob;
716         if (sscanf(str, "0X%x%n", addr, &n) >= 1 && n == nob)
717                 return 1;
718
719         n = nob;
720         if (sscanf(str, "%u%n", addr, &n) >= 1 && n == nob)
721                 return 1;
722
723         return 0;
724 }
725
726 /**
727  * Nf_parse_addrlist method for networks using numeric addresses.
728  *
729  * Examples of such networks are gm and elan.
730  *
731  * \retval 0 if \a str parsed to numeric address
732  * \retval errno otherwise
733  */
734 int
735 libcfs_num_parse(char *str, int len, struct list_head *list)
736 {
737         struct cfs_expr_list *el;
738         int     rc;
739
740         rc = cfs_expr_list_parse(str, len, 0, MAX_NUMERIC_VALUE, &el);
741         if (rc == 0)
742                 list_add_tail(&el->el_link, list);
743
744         return rc;
745 }
746
747 static int
748 libcfs_num_addr_range_print(char *buffer, int count, struct list_head *list)
749 {
750         int i = 0, j = 0;
751         struct cfs_expr_list *el;
752
753         list_for_each_entry(el, list, el_link) {
754                 LASSERT(j++ < 1);
755                 i += cfs_expr_list_print(buffer + i, count - i, el);
756         }
757         return i;
758 }
759
760 /*
761  * Nf_match_addr method for networks using numeric addresses
762  *
763  * \retval 1 on match
764  * \retval 0 otherwise
765  */
766 static int
767 libcfs_num_match(__u32 addr, struct list_head *numaddr)
768 {
769         struct cfs_expr_list *el;
770
771         LASSERT(!list_empty(numaddr));
772         el = list_first_entry(numaddr, struct cfs_expr_list, el_link);
773
774         return cfs_expr_list_match(addr, el);
775 }
776
777 static struct netstrfns libcfs_netstrfns[] = {
778         { .nf_type              = LOLND,
779           .nf_name              = "lo",
780           .nf_modname           = "klolnd",
781           .nf_addr2str          = libcfs_decnum_addr2str,
782           .nf_str2addr          = libcfs_lo_str2addr,
783           .nf_parse_addrlist    = libcfs_num_parse,
784           .nf_print_addrlist    = libcfs_num_addr_range_print,
785           .nf_match_addr        = libcfs_num_match
786         },
787         { .nf_type              = SOCKLND,
788           .nf_name              = "tcp",
789           .nf_modname           = "ksocklnd",
790           .nf_addr2str          = libcfs_ip_addr2str,
791           .nf_addr2str_size     = libcfs_ip_addr2str_size,
792           .nf_str2addr          = libcfs_ip_str2addr,
793           .nf_str2addr_size     = libcfs_ip_str2addr_size,
794           .nf_parse_addrlist    = cfs_ip_addr_parse,
795           .nf_print_addrlist    = libcfs_ip_addr_range_print,
796           .nf_match_addr        = cfs_ip_addr_match
797         },
798         { .nf_type              = O2IBLND,
799           .nf_name              = "o2ib",
800           .nf_modname           = "ko2iblnd",
801           .nf_addr2str          = libcfs_ip_addr2str,
802           .nf_str2addr          = libcfs_ip_str2addr,
803           .nf_parse_addrlist    = cfs_ip_addr_parse,
804           .nf_print_addrlist    = libcfs_ip_addr_range_print,
805           .nf_match_addr        = cfs_ip_addr_match
806         },
807         { .nf_type              = GNILND,
808           .nf_name              = "gni",
809           .nf_modname           = "kgnilnd",
810           .nf_addr2str          = libcfs_decnum_addr2str,
811           .nf_str2addr          = libcfs_num_str2addr,
812           .nf_parse_addrlist    = libcfs_num_parse,
813           .nf_print_addrlist    = libcfs_num_addr_range_print,
814           .nf_match_addr        = libcfs_num_match
815         },
816         { .nf_type              = GNIIPLND,
817           .nf_name              = "gip",
818           .nf_modname           = "kgnilnd",
819           .nf_addr2str          = libcfs_ip_addr2str,
820           .nf_str2addr          = libcfs_ip_str2addr,
821           .nf_parse_addrlist    = cfs_ip_addr_parse,
822           .nf_print_addrlist    = libcfs_ip_addr_range_print,
823           .nf_match_addr        = cfs_ip_addr_match
824         },
825         { .nf_type              = PTL4LND,
826           .nf_name              = "ptlf",
827           .nf_modname           = "kptl4lnd",
828           .nf_addr2str          = libcfs_decnum_addr2str,
829           .nf_str2addr          = libcfs_num_str2addr,
830           .nf_parse_addrlist    = libcfs_num_parse,
831           .nf_print_addrlist    = libcfs_num_addr_range_print,
832           .nf_match_addr        = libcfs_num_match
833         },
834         {
835           .nf_type              = KFILND,
836           .nf_name              = "kfi",
837           .nf_modname           = "kkfilnd",
838           .nf_addr2str          = libcfs_decnum_addr2str,
839           .nf_str2addr          = libcfs_num_str2addr,
840           .nf_parse_addrlist    = libcfs_num_parse,
841           .nf_print_addrlist    = libcfs_num_addr_range_print,
842           .nf_match_addr        = libcfs_num_match
843         },
844 };
845
846 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
847
848 static struct netstrfns *
849 type2net_info(__u32 net_type)
850 {
851         int i;
852
853         for (i = 0; i < libcfs_nnetstrfns; i++) {
854                 if (libcfs_netstrfns[i].nf_type == net_type)
855                         return &libcfs_netstrfns[i];
856         }
857
858         return NULL;
859 }
860
861 int
862 cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
863 {
864         __u32 net_num;
865
866         if (!net_num_list)
867                 return 0;
868
869         if (net_type != LNET_NETTYP(net_id))
870                 return 0;
871
872         net_num = LNET_NETNUM(net_id);
873
874         /* if there is a net number but the list passed in is empty, then
875          * there is no match.
876          */
877         if (!net_num && list_empty(net_num_list))
878                 return 1;
879         else if (list_empty(net_num_list))
880                 return 0;
881
882         if (!libcfs_num_match(net_num, net_num_list))
883                 return 0;
884
885         return 1;
886 }
887
888 int
889 cfs_match_nid_net(struct lnet_nid *nid, __u32 net_type,
890                    struct list_head *net_num_list,
891                    struct list_head *addr)
892 {
893         __u32 address;
894         struct netstrfns *nf;
895
896         if (!addr || list_empty(addr) || !net_num_list)
897                 return 0;
898
899         nf = type2net_info(LNET_NETTYP(LNET_NID_NET(nid)));
900         if (!nf)
901                 return 0;
902
903         /* FIXME handle long-addr nid */
904         address = LNET_NIDADDR(lnet_nid_to_nid4(nid));
905
906         /* if either the address or net number don't match then no match */
907         if (!nf->nf_match_addr(address, addr) ||
908             !cfs_match_net(LNET_NID_NET(nid), net_type, net_num_list))
909                 return 0;
910
911         return 1;
912 }
913 EXPORT_SYMBOL(cfs_match_nid_net);
914
915 static struct netstrfns *
916 libcfs_lnd2netstrfns(__u32 lnd)
917 {
918         int     i;
919
920         for (i = 0; i < libcfs_nnetstrfns; i++)
921                 if (lnd == libcfs_netstrfns[i].nf_type)
922                         return &libcfs_netstrfns[i];
923
924         return NULL;
925 }
926
927 static struct netstrfns *
928 libcfs_namenum2netstrfns(const char *name)
929 {
930         struct netstrfns *nf;
931         int               i;
932
933         for (i = 0; i < libcfs_nnetstrfns; i++) {
934                 nf = &libcfs_netstrfns[i];
935                 if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
936                         return nf;
937         }
938         return NULL;
939 }
940
941 static struct netstrfns *
942 libcfs_name2netstrfns(const char *name)
943 {
944         int    i;
945
946         for (i = 0; i < libcfs_nnetstrfns; i++)
947                 if (!strcmp(libcfs_netstrfns[i].nf_name, name))
948                         return &libcfs_netstrfns[i];
949
950         return NULL;
951 }
952
953 int
954 libcfs_isknown_lnd(__u32 lnd)
955 {
956         return libcfs_lnd2netstrfns(lnd) != NULL;
957 }
958 EXPORT_SYMBOL(libcfs_isknown_lnd);
959
960 char *
961 libcfs_lnd2modname(__u32 lnd)
962 {
963         struct netstrfns *nf = libcfs_lnd2netstrfns(lnd);
964
965         return (nf == NULL) ? NULL : nf->nf_modname;
966 }
967 EXPORT_SYMBOL(libcfs_lnd2modname);
968
969 int
970 libcfs_str2lnd(const char *str)
971 {
972         struct netstrfns *nf = libcfs_name2netstrfns(str);
973
974         if (nf != NULL)
975                 return nf->nf_type;
976
977         return -ENXIO;
978 }
979 EXPORT_SYMBOL(libcfs_str2lnd);
980
981 char *
982 libcfs_lnd2str_r(__u32 lnd, char *buf, size_t buf_size)
983 {
984         struct netstrfns *nf;
985
986         nf = libcfs_lnd2netstrfns(lnd);
987         if (nf == NULL)
988                 snprintf(buf, buf_size, "?%u?", lnd);
989         else
990                 snprintf(buf, buf_size, "%s", nf->nf_name);
991
992         return buf;
993 }
994 EXPORT_SYMBOL(libcfs_lnd2str_r);
995
996 char *
997 libcfs_net2str_r(__u32 net, char *buf, size_t buf_size)
998 {
999         __u32             nnum = LNET_NETNUM(net);
1000         __u32             lnd  = LNET_NETTYP(net);
1001         struct netstrfns *nf;
1002
1003         nf = libcfs_lnd2netstrfns(lnd);
1004         if (nf == NULL)
1005                 snprintf(buf, buf_size, "<%u:%u>", lnd, nnum);
1006         else if (nnum == 0)
1007                 snprintf(buf, buf_size, "%s", nf->nf_name);
1008         else
1009                 snprintf(buf, buf_size, "%s%u", nf->nf_name, nnum);
1010
1011         return buf;
1012 }
1013 EXPORT_SYMBOL(libcfs_net2str_r);
1014
1015 char *
1016 libcfs_nid2str_r(lnet_nid_t nid, char *buf, size_t buf_size)
1017 {
1018         __u32             addr = LNET_NIDADDR(nid);
1019         __u32             net  = LNET_NIDNET(nid);
1020         __u32             nnum = LNET_NETNUM(net);
1021         __u32             lnd  = LNET_NETTYP(net);
1022         struct netstrfns *nf;
1023
1024         if (nid == LNET_NID_ANY) {
1025                 strncpy(buf, "<?>", buf_size);
1026                 buf[buf_size - 1] = '\0';
1027                 return buf;
1028         }
1029
1030         nf = libcfs_lnd2netstrfns(lnd);
1031         if (nf == NULL) {
1032                 snprintf(buf, buf_size, "%x@<%u:%u>", addr, lnd, nnum);
1033         } else {
1034                 size_t addr_len;
1035
1036                 nf->nf_addr2str(addr, buf, buf_size);
1037                 addr_len = strlen(buf);
1038                 if (nnum == 0)
1039                         snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1040                                  nf->nf_name);
1041                 else
1042                         snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1043                                  nf->nf_name, nnum);
1044         }
1045
1046         return buf;
1047 }
1048 EXPORT_SYMBOL(libcfs_nid2str_r);
1049
1050 char *
1051 libcfs_nidstr_r(const struct lnet_nid *nid, char *buf, size_t buf_size)
1052 {
1053         __u32 nnum;
1054         __u32 lnd;
1055         struct netstrfns *nf;
1056
1057         if (LNET_NID_IS_ANY(nid)) {
1058                 strncpy(buf, "<?>", buf_size);
1059                 buf[buf_size - 1] = '\0';
1060                 return buf;
1061         }
1062
1063         nnum = be16_to_cpu(nid->nid_num);
1064         lnd = nid->nid_type;
1065         nf = libcfs_lnd2netstrfns(lnd);
1066         if (nf) {
1067                 size_t addr_len;
1068
1069                 if (nf->nf_addr2str_size)
1070                         nf->nf_addr2str_size(nid->nid_addr, NID_ADDR_BYTES(nid),
1071                                              buf, buf_size);
1072                 else
1073                         nf->nf_addr2str(ntohl(nid->nid_addr[0]), buf, buf_size);
1074                 addr_len = strlen(buf);
1075                 if (nnum == 0)
1076                         snprintf(buf + addr_len, buf_size - addr_len, "@%s",
1077                                  nf->nf_name);
1078                 else
1079                         snprintf(buf + addr_len, buf_size - addr_len, "@%s%u",
1080                                  nf->nf_name, nnum);
1081         } else {
1082                 int l = 0;
1083                 int words = DIV_ROUND_UP(NID_ADDR_BYTES(nid), 4);
1084                 int i;
1085
1086                 for (i = 0; i < words && i < 4; i++)
1087                         l = snprintf(buf+l, buf_size-l, "%s%x",
1088                                      i ? ":" : "", ntohl(nid->nid_addr[i]));
1089                 snprintf(buf+l, buf_size-l, "@<%u:%u>", lnd, nnum);
1090         }
1091
1092         return buf;
1093 }
1094 EXPORT_SYMBOL(libcfs_nidstr_r);
1095
1096 static struct netstrfns *
1097 libcfs_str2net_internal(const char *str, __u32 *net)
1098 {
1099         struct netstrfns *nf = NULL;
1100         int               nob;
1101         unsigned int      netnum;
1102         int               i;
1103
1104         for (i = 0; i < libcfs_nnetstrfns; i++) {
1105                 nf = &libcfs_netstrfns[i];
1106                 if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
1107                         break;
1108         }
1109
1110         if (i == libcfs_nnetstrfns)
1111                 return NULL;
1112
1113         nob = strlen(nf->nf_name);
1114
1115         if (strlen(str) == (unsigned int)nob) {
1116                 netnum = 0;
1117         } else {
1118                 if (nf->nf_type == LOLND) /* net number not allowed */
1119                         return NULL;
1120
1121                 str += nob;
1122                 i = strlen(str);
1123                 if (sscanf(str, "%u%n", &netnum, &i) < 1 ||
1124                     i != (int)strlen(str))
1125                         return NULL;
1126         }
1127
1128         *net = LNET_MKNET(nf->nf_type, netnum);
1129         return nf;
1130 }
1131
1132 __u32
1133 libcfs_str2net(const char *str)
1134 {
1135         __u32  net;
1136
1137         if (libcfs_str2net_internal(str, &net) != NULL)
1138                 return net;
1139
1140         return LNET_NET_ANY;
1141 }
1142 EXPORT_SYMBOL(libcfs_str2net);
1143
1144 lnet_nid_t
1145 libcfs_str2nid(const char *str)
1146 {
1147         const char       *sep = strchr(str, '@');
1148         struct netstrfns *nf;
1149         __u32             net;
1150         __u32             addr;
1151
1152         if (sep != NULL) {
1153                 nf = libcfs_str2net_internal(sep + 1, &net);
1154                 if (nf == NULL)
1155                         return LNET_NID_ANY;
1156         } else {
1157                 sep = str + strlen(str);
1158                 net = LNET_MKNET(SOCKLND, 0);
1159                 nf = libcfs_lnd2netstrfns(SOCKLND);
1160                 LASSERT(nf != NULL);
1161         }
1162
1163         if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1164                 return LNET_NID_ANY;
1165
1166         return LNET_MKNID(net, addr);
1167 }
1168 EXPORT_SYMBOL(libcfs_str2nid);
1169
1170 int
1171 libcfs_strnid(struct lnet_nid *nid, const char *str)
1172 {
1173         const char       *sep = strchr(str, '@');
1174         struct netstrfns *nf;
1175         __u32             net;
1176
1177         if (sep != NULL) {
1178                 nf = libcfs_str2net_internal(sep + 1, &net);
1179                 if (nf == NULL)
1180                         return -EINVAL;
1181         } else {
1182                 sep = str + strlen(str);
1183                 net = LNET_MKNET(SOCKLND, 0);
1184                 nf = libcfs_lnd2netstrfns(SOCKLND);
1185                 LASSERT(nf != NULL);
1186         }
1187
1188         memset(nid, 0, sizeof(*nid));
1189         nid->nid_type = LNET_NETTYP(net);
1190         nid->nid_num = htons(LNET_NETNUM(net));
1191         if (nf->nf_str2addr_size) {
1192                 size_t asize = 0;
1193
1194                 if (!nf->nf_str2addr_size(str, (int)(sep - str),
1195                                           nid->nid_addr, &asize))
1196                         return -EINVAL;
1197                 nid->nid_size = asize - 4;
1198         } else {
1199                 __u32 addr;
1200
1201                 if (!nf->nf_str2addr(str, (int)(sep - str), &addr))
1202                         return -EINVAL;
1203                 nid->nid_addr[0] = htonl(addr);
1204                 nid->nid_size = 0;
1205         }
1206         return 0;
1207 }
1208 EXPORT_SYMBOL(libcfs_strnid);
1209
1210 char *
1211 libcfs_id2str(struct lnet_process_id id)
1212 {
1213         char *str = libcfs_next_nidstring();
1214
1215         if (id.pid == LNET_PID_ANY) {
1216                 snprintf(str, LNET_NIDSTR_SIZE,
1217                          "LNET_PID_ANY-%s", libcfs_nid2str(id.nid));
1218                 return str;
1219         }
1220
1221         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1222                  ((id.pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1223                  (id.pid & ~LNET_PID_USERFLAG), libcfs_nid2str(id.nid));
1224         return str;
1225 }
1226 EXPORT_SYMBOL(libcfs_id2str);
1227
1228 char *
1229 libcfs_idstr(struct lnet_processid *id)
1230 {
1231         char *str = libcfs_next_nidstring();
1232
1233         if (id->pid == LNET_PID_ANY) {
1234                 snprintf(str, LNET_NIDSTR_SIZE,
1235                          "LNET_PID_ANY-%s", libcfs_nidstr(&id->nid));
1236                 return str;
1237         }
1238
1239         snprintf(str, LNET_NIDSTR_SIZE, "%s%u-%s",
1240                  ((id->pid & LNET_PID_USERFLAG) != 0) ? "U" : "",
1241                  (id->pid & ~LNET_PID_USERFLAG), libcfs_nidstr(&id->nid));
1242         return str;
1243 }
1244 EXPORT_SYMBOL(libcfs_idstr);
1245
1246 int
1247 libcfs_strid(struct lnet_processid *id, const char *str)
1248 {
1249         char *tmp = strchr(str, '-');
1250
1251         id->pid = LNET_PID_LUSTRE;
1252         if (tmp &&
1253             strncmp("LNET_PID_ANY-", str, tmp - str) != 0) {
1254                 char pid[LNET_NIDSTR_SIZE];
1255                 int rc;
1256
1257                 strscpy(pid, str, tmp - str);
1258                 rc = kstrtou32(pid, 10, &id->pid);
1259                 if (rc < 0)
1260                         return rc;
1261                 tmp++;
1262         } else {
1263                 tmp = (char *)str;
1264         }
1265
1266         return libcfs_strnid(&id->nid, tmp);
1267 }
1268 EXPORT_SYMBOL(libcfs_strid);
1269
1270 int
1271 libcfs_str2anynid(lnet_nid_t *nidp, const char *str)
1272 {
1273         if (!strcmp(str, "*")) {
1274                 *nidp = LNET_NID_ANY;
1275                 return 1;
1276         }
1277
1278         *nidp = libcfs_str2nid(str);
1279         return *nidp != LNET_NID_ANY;
1280 }
1281 EXPORT_SYMBOL(libcfs_str2anynid);