Whamcloud - gitweb
LU-8932 lnet: define new network driver ptl4lnd
[fs/lustre-release.git] / libcfs / libcfs / util / nidstrings.c
index e9149ec..8c4e495 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2014, Intel Corporation.
+ * Copyright (c) 2011, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -155,6 +151,44 @@ libcfs_ip_str2addr(const char *str, int nob, __u32 *addr)
        return 0;
 }
 
+int
+cfs_ip_addr_parse(char *str, int len, struct list_head *list)
+{
+       struct cfs_expr_list *el;
+       struct cfs_lstr src;
+       int rc;
+       int i;
+
+       src.ls_str = str;
+       src.ls_len = len;
+       i = 0;
+
+       while (src.ls_str != NULL) {
+               struct cfs_lstr res;
+
+               if (!cfs_gettok(&src, '.', &res)) {
+                       rc = -EINVAL;
+                       goto out;
+               }
+
+               rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
+               if (rc != 0)
+                       goto out;
+
+               list_add_tail(&el->el_link, list);
+               i++;
+       }
+
+       if (i == 4)
+               return 0;
+
+       rc = -EINVAL;
+out:
+       cfs_expr_list_free_list(list);
+
+       return rc;
+}
+
 static int
 libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
 {
@@ -170,6 +204,28 @@ libcfs_ip_addr_range_print(char *buffer, int count, struct list_head *list)
        return i;
 }
 
+/**
+ * Matches address (\a addr) against address set encoded in \a list.
+ *
+ * \retval 1 if \a addr matches
+ * \retval 0 otherwise
+ */
+int
+cfs_ip_addr_match(__u32 addr, struct list_head *list)
+{
+       struct cfs_expr_list *el;
+       int i = 0;
+
+       list_for_each_entry_reverse(el, list, el_link) {
+               if (!cfs_expr_list_match(addr & 0xff, el))
+                       return 0;
+               addr >>= 8;
+               i++;
+       }
+
+       return i == 4;
+}
+
 static void
 libcfs_decnum_addr2str(__u32 addr, char *str, size_t size)
 {
@@ -258,149 +314,187 @@ static void cfs_ip_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
 static bool cfs_num_is_contiguous(struct list_head *nidlist);
 static void cfs_num_min_max(struct list_head *nidlist, __u32 *min, __u32 *max);
 
-static struct netstrfns  libcfs_netstrfns[] = {
-       {/* .nf_type      */  LOLND,
-        /* .nf_name      */  "lo",
-        /* .nf_modname   */  "klolnd",
-        /* .nf_addr2str  */  libcfs_decnum_addr2str,
-        /* .nf_str2addr  */  libcfs_lo_str2addr,
-        /* .nf_parse_addr*/  libcfs_num_parse,
-        /* .nf_print_addrlist*/  libcfs_num_addr_range_print,
-        /* .nf_match_addr*/      libcfs_num_match,
-        /* .nf_is_contiguous */  cfs_num_is_contiguous,
-        /* .nf_min_max   */      cfs_num_min_max},
-       {/* .nf_type      */  SOCKLND,
-        /* .nf_name      */  "tcp",
-        /* .nf_modname   */  "ksocklnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  O2IBLND,
-        /* .nf_name      */  "o2ib",
-        /* .nf_modname   */  "ko2iblnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  CIBLND,
-        /* .nf_name      */  "cib",
-        /* .nf_modname   */  "kciblnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  OPENIBLND,
-        /* .nf_name      */  "openib",
-        /* .nf_modname   */  "kopeniblnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  IIBLND,
-        /* .nf_name      */  "iib",
-        /* .nf_modname   */  "kiiblnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  VIBLND,
-        /* .nf_name      */  "vib",
-        /* .nf_modname   */  "kviblnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */  RALND,
-        /* .nf_name      */  "ra",
-        /* .nf_modname   */  "kralnd",
-        /* .nf_addr2str  */  libcfs_ip_addr2str,
-        /* .nf_str2addr  */  libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */      QSWLND,
-        /* .nf_name      */      "elan",
-        /* .nf_modname   */      "kqswlnd",
-        /* .nf_addr2str  */      libcfs_decnum_addr2str,
-        /* .nf_str2addr  */      libcfs_num_str2addr,
-        /* .nf_parse_addrlist*/  libcfs_num_parse,
-        /* .nf_print_addrlist*/  libcfs_num_addr_range_print,
-        /* .nf_match_addr*/      libcfs_num_match,
-        /* .nf_is_contiguous */  cfs_num_is_contiguous,
-        /* .nf_min_max   */      cfs_num_min_max},
-       {/* .nf_type      */      GMLND,
-        /* .nf_name      */      "gm",
-        /* .nf_modname   */      "kgmlnd",
-        /* .nf_addr2str  */      libcfs_hexnum_addr2str,
-        /* .nf_str2addr  */      libcfs_num_str2addr,
-        /* .nf_parse_addrlist*/  libcfs_num_parse,
-        /* .nf_print_addrlist*/  libcfs_num_addr_range_print,
-        /* .nf_match_addr*/      libcfs_num_match,
-        /* .nf_is_contiguous */  cfs_num_is_contiguous,
-        /* .nf_min_max   */      cfs_num_min_max},
-       {/* .nf_type      */      MXLND,
-        /* .nf_name      */      "mx",
-        /* .nf_modname   */      "kmxlnd",
-        /* .nf_addr2str  */      libcfs_ip_addr2str,
-        /* .nf_str2addr  */      libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-       {/* .nf_type      */      PTLLND,
-        /* .nf_name      */      "ptl",
-        /* .nf_modname   */      "kptllnd",
-        /* .nf_addr2str  */      libcfs_decnum_addr2str,
-        /* .nf_str2addr  */      libcfs_num_str2addr,
-        /* .nf_parse_addrlist*/  libcfs_num_parse,
-        /* .nf_print_addrlist*/  libcfs_num_addr_range_print,
-        /* .nf_match_addr*/      libcfs_num_match,
-        /* .nf_is_contiguous */  cfs_num_is_contiguous,
-        /* .nf_min_max   */      cfs_num_min_max},
-       {/* .nf_type      */      GNILND,
-        /* .nf_name      */      "gni",
-        /* .nf_modname   */      "kgnilnd",
-        /* .nf_addr2str  */      libcfs_decnum_addr2str,
-        /* .nf_str2addr  */      libcfs_num_str2addr,
-        /* .nf_parse_addrlist*/  libcfs_num_parse,
-        /* .nf_print_addrlist*/  libcfs_num_addr_range_print,
-        /* .nf_match_addr*/      libcfs_num_match,
-        /* .nf_is_contiguous */  cfs_num_is_contiguous,
-        /* .nf_min_max   */      cfs_num_min_max},
-       {/* .nf_type      */      GNIIPLND,
-        /* .nf_name      */      "gip",
-        /* .nf_modname   */      "kgnilnd",
-        /* .nf_addr2str  */      libcfs_ip_addr2str,
-        /* .nf_str2addr  */      libcfs_ip_str2addr,
-        /* .nf_parse_addrlist*/  cfs_ip_addr_parse,
-        /* .nf_print_addrlist*/  libcfs_ip_addr_range_print,
-        /* .nf_match_addr*/      cfs_ip_addr_match,
-        /* .nf_is_contiguous */  cfs_ip_is_contiguous,
-        /* .nf_min_max   */      cfs_ip_min_max},
-        /* placeholder for net0 alias.  It MUST BE THE LAST ENTRY */
-       {/* .nf_type      */  -1},
+static struct netstrfns libcfs_netstrfns[] = {
+       {
+               .nf_type                = LOLND,
+               .nf_name                = "lo",
+               .nf_modname             = "klolnd",
+               .nf_addr2str            = libcfs_decnum_addr2str,
+               .nf_str2addr            = libcfs_lo_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       },
+       {
+               .nf_type                = SOCKLND,
+               .nf_name                = "tcp",
+               .nf_modname             = "ksocklnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = O2IBLND,
+               .nf_name                = "o2ib",
+               .nf_modname             = "ko2iblnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = CIBLND,
+               .nf_name                = "cib",
+               .nf_modname             = "kciblnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = OPENIBLND,
+               .nf_name                = "openib",
+               .nf_modname             = "kopeniblnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = IIBLND,
+               .nf_name                = "iib",
+               .nf_modname             = "kiiblnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = VIBLND,
+               .nf_name                = "vib",
+               .nf_modname             = "kviblnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = RALND,
+               .nf_name                = "ra",
+               .nf_modname             = "kralnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = QSWLND,
+               .nf_name                = "elan",
+               .nf_modname             = "kqswlnd",
+               .nf_addr2str            = libcfs_decnum_addr2str,
+               .nf_str2addr            = libcfs_num_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       },
+       {
+               .nf_type                = GMLND,
+               .nf_name                = "gm",
+               .nf_modname             = "kgmlnd",
+               .nf_addr2str            = libcfs_hexnum_addr2str,
+               .nf_str2addr            = libcfs_num_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       },
+       {
+               .nf_type                = MXLND,
+               .nf_name                = "mx",
+               .nf_modname             = "kmxlnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = PTLLND,
+               .nf_name                = "ptl",
+               .nf_modname             = "kptllnd",
+               .nf_addr2str            = libcfs_decnum_addr2str,
+               .nf_str2addr            = libcfs_num_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       },
+       {
+               .nf_type                = GNILND,
+               .nf_name                = "gni",
+               .nf_modname             = "kgnilnd",
+               .nf_addr2str            = libcfs_decnum_addr2str,
+               .nf_str2addr            = libcfs_num_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       },
+       {
+               .nf_type                = GNIIPLND,
+               .nf_name                = "gip",
+               .nf_modname             = "kgnilnd",
+               .nf_addr2str            = libcfs_ip_addr2str,
+               .nf_str2addr            = libcfs_ip_str2addr,
+               .nf_parse_addrlist      = cfs_ip_addr_parse,
+               .nf_print_addrlist      = libcfs_ip_addr_range_print,
+               .nf_match_addr          = cfs_ip_addr_match,
+               .nf_is_contiguous       = cfs_ip_is_contiguous,
+               .nf_min_max             = cfs_ip_min_max
+       },
+       {
+               .nf_type                = PTL4LND,
+               .nf_name                = "ptlf",
+               .nf_modname             = "kptl4lnd",
+               .nf_addr2str            = libcfs_decnum_addr2str,
+               .nf_str2addr            = libcfs_num_str2addr,
+               .nf_parse_addrlist      = libcfs_num_parse,
+               .nf_print_addrlist      = libcfs_num_addr_range_print,
+               .nf_match_addr          = libcfs_num_match,
+               .nf_is_contiguous       = cfs_num_is_contiguous,
+               .nf_min_max             = cfs_num_min_max
+       }
 };
 
 static const size_t libcfs_nnetstrfns =
@@ -426,8 +520,7 @@ libcfs_namenum2netstrfns(const char *name)
 
        for (i = 0; i < libcfs_nnetstrfns; i++) {
                nf = &libcfs_netstrfns[i];
-               if (nf->nf_type >= 0 &&
-                   !strncmp(name, nf->nf_name, strlen(nf->nf_name)))
+               if (!strncmp(name, nf->nf_name, strlen(nf->nf_name)))
                        return nf;
        }
        return NULL;
@@ -439,8 +532,7 @@ libcfs_name2netstrfns(const char *name)
        int    i;
 
        for (i = 0; i < libcfs_nnetstrfns; i++)
-               if (libcfs_netstrfns[i].nf_type >= 0 &&
-                   !strcmp(libcfs_netstrfns[i].nf_name, name))
+               if (!strcmp(libcfs_netstrfns[i].nf_name, name))
                        return &libcfs_netstrfns[i];
 
        return NULL;
@@ -547,8 +639,7 @@ libcfs_str2net_internal(const char *str, __u32 *net)
 
        for (i = 0; i < libcfs_nnetstrfns; i++) {
                nf = &libcfs_netstrfns[i];
-               if (nf->nf_type >= 0 &&
-                   !strncmp(str, nf->nf_name, strlen(nf->nf_name)))
+               if (!strncmp(str, nf->nf_name, strlen(nf->nf_name)))
                        break;
        }