0a122b
From b60d6390ff9415bc5960ba20ff5cc56e50078f85 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <b60d6390ff9415bc5960ba20ff5cc56e50078f85.1387382496.git.minovotn@redhat.com>
0a122b
In-Reply-To: <c5386144fbf09f628148101bc674e2421cdd16e3.1387382496.git.minovotn@redhat.com>
0a122b
References: <c5386144fbf09f628148101bc674e2421cdd16e3.1387382496.git.minovotn@redhat.com>
0a122b
From: Nigel Croxon <ncroxon@redhat.com>
0a122b
Date: Thu, 14 Nov 2013 22:53:06 +0100
0a122b
Subject: [PATCH 30/46] rdma: proper getaddrinfo() handling
0a122b
0a122b
RH-Author: Nigel Croxon <ncroxon@redhat.com>
0a122b
Message-id: <1384469598-13137-31-git-send-email-ncroxon@redhat.com>
0a122b
Patchwork-id: 55714
0a122b
O-Subject: [RHEL7.0 PATCH 30/42] rdma: proper getaddrinfo() handling
0a122b
Bugzilla: 1011720
0a122b
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>
0a122b
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
0a122b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
0a122b
Bugzilla: 1011720
0a122b
https://bugzilla.redhat.com/show_bug.cgi?id=1011720
0a122b
0a122b
>From commit ID:
0a122b
commit 6470215b794d6d9f9ffbd82f669645715eb014f8
0a122b
Author: Michael R. Hines <mrhines@us.ibm.com>
0a122b
Date:   Fri Aug 9 16:05:43 2013 -0400
0a122b
0a122b
    rdma: proper getaddrinfo() handling
0a122b
0a122b
    getaddrinfo() already knows what it's doing,
0a122b
    but it can potentially return multiple addresses.
0a122b
    We need to handle that...
0a122b
0a122b
    Reviewed-by: Orit Wasserman <owasserm@redhat.com>
0a122b
    Signed-off-by: Michael R. Hines <mrhines@us.ibm.com>
0a122b
    Message-id: 1376078746-24948-5-git-send-email-mrhines@linux.vnet.ibm.com
0a122b
    Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
0a122b
---
0a122b
 migration-rdma.c |   56 +++++++++++++++++++++++++++--------------------------
0a122b
 1 files changed, 29 insertions(+), 27 deletions(-)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 migration-rdma.c | 56 +++++++++++++++++++++++++++++---------------------------
0a122b
 1 file changed, 29 insertions(+), 27 deletions(-)
0a122b
0a122b
diff --git a/migration-rdma.c b/migration-rdma.c
0a122b
index 9c02ad3..e6fd77a 100644
0a122b
--- a/migration-rdma.c
0a122b
+++ b/migration-rdma.c
0a122b
@@ -392,7 +392,6 @@ typedef struct RDMAContext {
0a122b
     uint64_t unregistrations[RDMA_SIGNALED_SEND_MAX];
0a122b
 
0a122b
     GHashTable *blockmap;
0a122b
-    bool ipv6;
0a122b
 } RDMAContext;
0a122b
 
0a122b
 /*
0a122b
@@ -745,7 +744,7 @@ static int qemu_rdma_resolve_host(RDMAContext *rdma, Error **errp)
0a122b
     char port_str[16];
0a122b
     struct rdma_cm_event *cm_event;
0a122b
     char ip[40] = "unknown";
0a122b
-    int af = rdma->ipv6 ? PF_INET6 : PF_INET;
0a122b
+    struct addrinfo *e;
0a122b
 
0a122b
     if (rdma->host == NULL || !strcmp(rdma->host, "")) {
0a122b
         ERROR(errp, "RDMA hostname has not been set");
0a122b
@@ -775,18 +774,23 @@ static int qemu_rdma_resolve_host(RDMAContext *rdma, Error **errp)
0a122b
         goto err_resolve_get_addr;
0a122b
     }
0a122b
 
0a122b
-    inet_ntop(af, &((struct sockaddr_in *) res->ai_addr)->sin_addr,
0a122b
-                                ip, sizeof ip);
0a122b
-    DPRINTF("%s => %s\n", rdma->host, ip);
0a122b
+    for (e = res; e != NULL; e = e->ai_next) {
0a122b
+        inet_ntop(e->ai_family,
0a122b
+            &((struct sockaddr_in *) e->ai_addr)->sin_addr, ip, sizeof ip);
0a122b
+        DPRINTF("Trying %s => %s\n", rdma->host, ip);
0a122b
 
0a122b
-    /* resolve the first address */
0a122b
-    ret = rdma_resolve_addr(rdma->cm_id, NULL, res->ai_addr,
0a122b
-            RDMA_RESOLVE_TIMEOUT_MS);
0a122b
-    if (ret) {
0a122b
-        ERROR(errp, "could not resolve address %s", rdma->host);
0a122b
-        goto err_resolve_get_addr;
0a122b
+        /* resolve the first address */
0a122b
+        ret = rdma_resolve_addr(rdma->cm_id, NULL, e->ai_addr,
0a122b
+                RDMA_RESOLVE_TIMEOUT_MS);
0a122b
+        if (!ret) {
0a122b
+            goto route;
0a122b
+        }
0a122b
     }
0a122b
 
0a122b
+    ERROR(errp, "could not resolve address %s", rdma->host);
0a122b
+    goto err_resolve_get_addr;
0a122b
+
0a122b
+route:
0a122b
     qemu_rdma_dump_gid("source_resolve_addr", rdma->cm_id);
0a122b
 
0a122b
     ret = rdma_get_cm_event(rdma->channel, &cm_event);
0a122b
@@ -2260,8 +2264,6 @@ err_rdma_source_connect:
0a122b
 static int qemu_rdma_dest_init(RDMAContext *rdma, Error **errp)
0a122b
 {
0a122b
     int ret = -EINVAL, idx;
0a122b
-    int af = rdma->ipv6 ? PF_INET6 : PF_INET;
0a122b
-    struct sockaddr_in sin;
0a122b
     struct rdma_cm_id *listen_id;
0a122b
     char ip[40] = "unknown";
0a122b
     struct addrinfo *res;
0a122b
@@ -2292,35 +2294,36 @@ static int qemu_rdma_dest_init(RDMAContext *rdma, Error **errp)
0a122b
         goto err_dest_init_create_listen_id;
0a122b
     }
0a122b
 
0a122b
-    memset(&sin, 0, sizeof(sin));
0a122b
-    sin.sin_family = af;
0a122b
-    sin.sin_port = htons(rdma->port);
0a122b
     snprintf(port_str, 16, "%d", rdma->port);
0a122b
     port_str[15] = '\0';
0a122b
 
0a122b
     if (rdma->host && strcmp("", rdma->host)) {
0a122b
+        struct addrinfo *e;
0a122b
+
0a122b
         ret = getaddrinfo(rdma->host, port_str, NULL, &res;;
0a122b
         if (ret < 0) {
0a122b
             ERROR(errp, "could not getaddrinfo address %s", rdma->host);
0a122b
             goto err_dest_init_bind_addr;
0a122b
         }
0a122b
 
0a122b
+        for (e = res; e != NULL; e = e->ai_next) {
0a122b
+            inet_ntop(e->ai_family,
0a122b
+                &((struct sockaddr_in *) e->ai_addr)->sin_addr, ip, sizeof ip);
0a122b
+            DPRINTF("Trying %s => %s\n", rdma->host, ip);
0a122b
+            ret = rdma_bind_addr(listen_id, e->ai_addr);
0a122b
+            if (!ret) {
0a122b
+                goto listen;
0a122b
+            }
0a122b
+        }
0a122b
 
0a122b
-        inet_ntop(af, &((struct sockaddr_in *) res->ai_addr)->sin_addr,
0a122b
-                                    ip, sizeof ip);
0a122b
+        ERROR(errp, "Error: could not rdma_bind_addr!");
0a122b
+        goto err_dest_init_bind_addr;
0a122b
     } else {
0a122b
         ERROR(errp, "migration host and port not specified!");
0a122b
         ret = -EINVAL;
0a122b
         goto err_dest_init_bind_addr;
0a122b
     }
0a122b
-
0a122b
-    DPRINTF("%s => %s\n", rdma->host, ip);
0a122b
-
0a122b
-    ret = rdma_bind_addr(listen_id, res->ai_addr);
0a122b
-    if (ret) {
0a122b
-        ERROR(errp, "Error: could not rdma_bind_addr!");
0a122b
-        goto err_dest_init_bind_addr;
0a122b
-    }
0a122b
+listen:
0a122b
 
0a122b
     rdma->listen_id = listen_id;
0a122b
     qemu_rdma_dump_gid("dest_init", listen_id);
0a122b
@@ -2351,7 +2354,6 @@ static void *qemu_rdma_data_init(const char *host_port, Error **errp)
0a122b
         if (addr != NULL) {
0a122b
             rdma->port = atoi(addr->port);
0a122b
             rdma->host = g_strdup(addr->host);
0a122b
-            rdma->ipv6 = addr->ipv6;
0a122b
         } else {
0a122b
             ERROR(errp, "bad RDMA migration address '%s'", host_port);
0a122b
             g_free(rdma);
0a122b
-- 
0a122b
1.7.11.7
0a122b