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