Whamcloud - gitweb
LU-13903 utils: move userland only nidstr.h handling
[fs/lustre-release.git] / lnet / lnet / nidstrings.c
index 824bdf4..821025b 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <libcfs/libcfs.h>
 #include <uapi/linux/lnet/nidstr.h>
+#include <lnet/lib-types.h>
 
 /* max value for numeric network address */
 #define MAX_NUMERIC_VALUE 0xffffffff
@@ -165,7 +166,7 @@ parse_addrange(const struct cfs_lstr *src, struct nidrange *nidrange)
                return 0;
        }
 
-       LIBCFS_ALLOC(addrrange, sizeof(struct addrrange));
+       CFS_ALLOC_PTR(addrrange);
        if (addrrange == NULL)
                return -ENOMEM;
        list_add_tail(&addrrange->ar_link, &nidrange->nr_addrranges);
@@ -222,7 +223,7 @@ add_nidrange(const struct cfs_lstr *src,
                return nr;
        }
 
-       LIBCFS_ALLOC(nr, sizeof(struct nidrange));
+       CFS_ALLOC_PTR(nr);
        if (nr == NULL)
                return NULL;
        list_add_tail(&nr->nr_link, nidlist);
@@ -283,7 +284,7 @@ free_addrranges(struct list_head *list)
 
                cfs_expr_list_free_list(&ar->ar_numaddr_ranges);
                list_del(&ar->ar_link);
-               LIBCFS_FREE(ar, sizeof(struct addrrange));
+               CFS_FREE_PTR(ar);
        }
 }
 
@@ -305,7 +306,7 @@ cfs_free_nidlist(struct list_head *list)
                nr = list_entry(pos, struct nidrange, nr_link);
                free_addrranges(&nr->nr_addrranges);
                list_del(pos);
-               LIBCFS_FREE(nr, sizeof(struct nidrange));
+               CFS_FREE_PTR(nr);
        }
 }
 EXPORT_SYMBOL(cfs_free_nidlist);
@@ -607,7 +608,7 @@ libcfs_num_str2addr(const char *str, int nob, __u32 *addr)
  * \retval 0 if \a str parsed to numeric address
  * \retval errno otherwise
  */
-static int
+int
 libcfs_num_parse(char *str, int len, struct list_head *list)
 {
        struct cfs_expr_list *el;
@@ -710,6 +711,72 @@ static struct netstrfns libcfs_netstrfns[] = {
 static const size_t libcfs_nnetstrfns = ARRAY_SIZE(libcfs_netstrfns);
 
 static struct netstrfns *
+type2net_info(__u32 net_type)
+{
+       int i;
+
+       for (i = 0; i < libcfs_nnetstrfns; i++) {
+               if (libcfs_netstrfns[i].nf_type == net_type)
+                       return &libcfs_netstrfns[i];
+       }
+
+       return NULL;
+}
+
+int
+cfs_match_net(__u32 net_id, __u32 net_type, struct list_head *net_num_list)
+{
+       __u32 net_num;
+
+       if (!net_num_list)
+               return 0;
+
+       if (net_type != LNET_NETTYP(net_id))
+               return 0;
+
+       net_num = LNET_NETNUM(net_id);
+
+       /* if there is a net number but the list passed in is empty, then
+        * there is no match.
+        */
+       if (!net_num && list_empty(net_num_list))
+               return 1;
+       else if (list_empty(net_num_list))
+               return 0;
+
+       if (!libcfs_num_match(net_num, net_num_list))
+               return 0;
+
+       return 1;
+}
+
+int
+cfs_match_nid_net(lnet_nid_t nid, __u32 net_type,
+                 struct list_head *net_num_list,
+                 struct list_head *addr)
+{
+       __u32 address;
+       struct netstrfns *nf;
+
+       if (!addr || !net_num_list)
+               return 0;
+
+       nf = type2net_info(LNET_NETTYP(LNET_NIDNET(nid)));
+       if (!nf || !net_num_list || !addr)
+               return 0;
+
+       address = LNET_NIDADDR(nid);
+
+       /* if either the address or net number don't match then no match */
+       if (!nf->nf_match_addr(address, addr) ||
+           !cfs_match_net(LNET_NIDNET(nid), net_type, net_num_list))
+               return 0;
+
+       return 1;
+}
+EXPORT_SYMBOL(cfs_match_nid_net);
+
+static struct netstrfns *
 libcfs_lnd2netstrfns(__u32 lnd)
 {
        int     i;
@@ -888,7 +955,7 @@ libcfs_str2net(const char *str)
        if (libcfs_str2net_internal(str, &net) != NULL)
                return net;
 
-       return LNET_NIDNET(LNET_NID_ANY);
+       return LNET_NET_ANY;
 }
 EXPORT_SYMBOL(libcfs_str2net);