]> git.openfabrics.org - ~shefty/librdmacm.git/commitdiff
cmatose: Replace use of getaddrinfo with rdma_getaddrinfo
authorSean Hefty <sean.hefty@intel.com>
Thu, 29 Sep 2011 21:23:47 +0000 (14:23 -0700)
committerSean Hefty <sean.hefty@intel.com>
Fri, 2 Mar 2012 01:06:48 +0000 (17:06 -0800)
Now that rdma_getaddrinfo exists, use it rather than getaddrinfo.
This will eventually allow us to specify native IB addresses into
cmatose once AF_IB support is there.

Signed-off-by: Sean Hefty <sean.hefty@intel.com>
Makefile.am
examples/cmatose.c
examples/common.c [new file with mode: 0644]
examples/common.h [new file with mode: 0644]

index 1e3e582e9c7c531ed34e7ebb9f29ef12ff0023cf..c9265fcf67955e760aa292b0f8ddc8be7affc4ca 100644 (file)
@@ -21,7 +21,7 @@ src_librdmacm_la_DEPENDENCIES =  $(srcdir)/src/librdmacm.map
 bin_PROGRAMS = examples/ucmatose examples/rping examples/udaddy examples/mckey \
               examples/rdma_client examples/rdma_server examples/rdma_xclient \
               examples/rdma_xserver
-examples_ucmatose_SOURCES = examples/cmatose.c
+examples_ucmatose_SOURCES = examples/cmatose.c examples/common.c
 examples_ucmatose_LDADD = $(top_builddir)/src/librdmacm.la
 examples_rping_SOURCES = examples/rping.c
 examples_rping_LDADD = $(top_builddir)/src/librdmacm.la
index 3f2ff03e25ad5ae789c4d4c3e67a0cea7aaa79ac..9c3a75e8585d26fb0273e16c8d6c4a96bf552ec1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
+ * Copyright (c) 2005-2006,2011 Intel Corporation.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
 #include <getopt.h>
 
 #include <rdma/rdma_cma.h>
-
-#if __BYTE_ORDER == __BIG_ENDIAN
-static inline uint64_t cpu_to_be64(uint64_t x) { return x; }
-static inline uint32_t cpu_to_be32(uint32_t x) { return x; }
-#else
-static inline uint64_t cpu_to_be64(uint64_t x) { return bswap_64(x); }
-static inline uint32_t cpu_to_be32(uint32_t x) { return bswap_32(x); }
-#endif
+#include "common.h"
 
 struct cmatest_node {
        int                     id;
@@ -75,14 +68,14 @@ struct cmatest {
        int                     connects_left;
        int                     disconnects_left;
 
-       struct rdma_addr        addr;
+       struct rdma_addrinfo    *rai;
 };
 
 static struct cmatest test;
 static int connections = 1;
 static int message_size = 100;
 static int message_count = 10;
-static uint16_t port = 7471;
+static char *port = "7471";
 static uint8_t set_tos = 0;
 static uint8_t tos;
 static uint8_t migrate = 0;
@@ -258,6 +251,8 @@ static int route_handler(struct cmatest_node *node)
        conn_param.responder_resources = 1;
        conn_param.initiator_depth = 1;
        conn_param.retry_count = 5;
+       conn_param.private_data = test.rai->ai_connect;
+       conn_param.private_data_len = test.rai->ai_connect_len;
        ret = rdma_connect(node->cma_id, &conn_param);
        if (ret) {
                perror("cmatose: failure connecting");
@@ -502,31 +497,10 @@ static int migrate_channel(struct rdma_cm_id *listen_id)
        return ret;
 }
 
-static int get_addr(char *dst, struct sockaddr *addr)
-{
-       struct addrinfo *res;
-       int ret;
-
-       ret = getaddrinfo(dst, NULL, NULL, &res);
-       if (ret) {
-               printf("getaddrinfo failed - invalid hostname or IP address\n");
-               return ret;
-       }
-
-       if (res->ai_family == PF_INET)
-               memcpy(addr, res->ai_addr, sizeof(struct sockaddr_in));
-       else if (res->ai_family == PF_INET6)
-               memcpy(addr, res->ai_addr, sizeof(struct sockaddr_in6));
-       else
-               ret = -1;
-
-        freeaddrinfo(res);
-        return ret;
-}
-
 static int run_server(void)
 {
        struct rdma_cm_id *listen_id;
+       struct rdma_addrinfo hints;
        int i, ret;
 
        printf("cmatose: starting server\n");
@@ -536,22 +510,16 @@ static int run_server(void)
                return ret;
        }
 
-       if (src_addr) {
-               ret = get_addr(src_addr, &test.addr.src_addr);
-               if (ret)
-                       goto out;
-               if (test.addr.src_addr.sa_family == AF_INET)
-                       test.addr.src_sin.sin_port = port;
-               else
-                       test.addr.src_sin6.sin6_port = port;
-               
-       } else {
-               test.addr.src_addr.sa_family = AF_INET;
-               test.addr.src_sin.sin_port = port;
+       memset(&hints, 0, sizeof hints);
+       hints.ai_flags = RAI_PASSIVE;
+       hints.ai_port_space = RDMA_PS_TCP;
+       ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &test.rai);
+       if (ret) {
+               perror("cmatose: getrdmaaddr error");
+               goto out;
        }
 
-       ret = rdma_bind_addr(listen_id, &test.addr.src_addr);
-
+       ret = rdma_bind_addr(listen_id, test.rai->ai_src_addr);
        if (ret) {
                perror("cmatose: bind address failed");
                goto out;
@@ -614,29 +582,23 @@ out:
 
 static int run_client(void)
 {
+       struct rdma_addrinfo hints;
        int i, ret, ret2;
 
        printf("cmatose: starting client\n");
-       if (src_addr) {
-               ret = get_addr(src_addr, &test.addr.src_addr);
-               if (ret)
-                       return ret;
-       }
 
-       ret = get_addr(dst_addr, &test.addr.dst_addr);
-       if (ret)
+       memset(&hints, 0, sizeof hints);
+       hints.ai_port_space = RDMA_PS_TCP;
+       ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &test.rai);
+       if (ret) {
+               perror("cmatose: getaddrinfo error");
                return ret;
-
-       if (test.addr.dst_addr.sa_family == AF_INET)
-               test.addr.dst_sin.sin_port = port;
-       else
-               test.addr.dst_sin6.sin6_port = port;
+       }
 
        printf("cmatose: connecting\n");
        for (i = 0; i < connections; i++) {
-               ret = rdma_resolve_addr(test.nodes[i].cma_id,
-                                       src_addr ? &test.addr.src_addr : NULL,
-                                       &test.addr.dst_addr, 2000);
+               ret = rdma_resolve_addr(test.nodes[i].cma_id, test.rai->ai_src_addr,
+                                       test.rai->ai_dst_addr, 2000);
                if (ret) {
                        perror("cmatose: failure getting addr");
                        connect_error();
@@ -705,7 +667,7 @@ int main(int argc, char **argv)
                        tos = (uint8_t) strtoul(optarg, NULL, 0);
                        break;
                case 'p':
-                       port = atoi(optarg);
+                       port = optarg;
                        break;
                case 'm':
                        migrate = 1;
@@ -744,6 +706,8 @@ int main(int argc, char **argv)
        printf("test complete\n");
        destroy_nodes();
        rdma_destroy_event_channel(test.channel);
+       if (test.rai)
+               rdma_freeaddrinfo(test.rai);
 
        printf("return status %d\n", ret);
        return ret;
diff --git a/examples/common.c b/examples/common.c
new file mode 100644 (file)
index 0000000..3eeb1e9
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * $Id$
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <byteswap.h>
+
+#include <rdma/rdma_cma.h>
+#include "common.h"
+
+int get_rdma_addr(char *src, char *dst, char *port,
+                 struct rdma_addrinfo *hints, struct rdma_addrinfo **rai)
+{
+       struct rdma_addrinfo rai_hints, *res;
+       int ret;
+
+       if (hints->ai_flags & RAI_PASSIVE)
+               return rdma_getaddrinfo(src, port, hints, rai);
+
+       rai_hints = *hints;
+       if (src) {
+               rai_hints.ai_flags = RAI_PASSIVE;
+               ret = rdma_getaddrinfo(src, NULL, &rai_hints, &res);
+               if (ret)
+                       return ret;
+
+               rai_hints.ai_src_addr = res->ai_src_addr;
+               rai_hints.ai_src_len = res->ai_src_len;
+               rai_hints.ai_flags &= ~RAI_PASSIVE;
+       }
+
+       ret = rdma_getaddrinfo(dst, port, &rai_hints, rai);
+       if (src)
+               rdma_freeaddrinfo(res);
+
+       return ret;
+}
diff --git a/examples/common.h b/examples/common.h
new file mode 100644 (file)
index 0000000..8d9fea0
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2005-2011 Intel Corporation.  All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * $Id$
+ */
+
+#include <stdlib.h>
+#include <sys/types.h>
+#include <byteswap.h>
+
+#include <rdma/rdma_cma.h>
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+static inline uint64_t cpu_to_be64(uint64_t x) { return x; }
+static inline uint32_t cpu_to_be32(uint32_t x) { return x; }
+#else
+static inline uint64_t cpu_to_be64(uint64_t x) { return bswap_64(x); }
+static inline uint32_t cpu_to_be32(uint32_t x) { return bswap_32(x); }
+#endif
+
+int get_rdma_addr(char *src, char *dst, char *port,
+                 struct rdma_addrinfo *hints, struct rdma_addrinfo **rai);