Skip to contentSkip to conversation

Small fixes for qemu-sockets ipv4/ipv6 logic.

PATCH In review Paolo Bonzini 3 participants 7 messages Feb 28, 2014

lore
Share & tools
lore
#0 lore raw

cover Small fixes for qemu-sockets ipv4/ipv6 logic.

While reviewing the L2TPv3 submission, I wanted to tell Anton that IPv4/IPv6 is already done in qemu-sockets.c. Turns out it is not exactly the case; these patches fixes the two issues I spotted.

Paolo Bonzini (2):
socket: treat ipv4=on,ipv6=on uniformly
socket: handle ipv4/ipv6 in socket_dgram

 util/qemu-sockets.c | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH 1/2 socket: treat ipv4=on,ipv6=on uniformly

In some cases, "ipv4=on,ipv6=on" means "try both kinds of address"; in others, it means "try IPv6 only" just by virtue of how the code is structured.

Fix this to make things more consistent, and adjust coding style too.

---
 util/qemu-sockets.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
util/qemu-sockets.c +20 -13
index 8818d7c..7a9065b 100644
@@ -127,10 +127,13 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
     addr = qemu_opt_get(opts, "host");
 
     to = qemu_opt_get_number(opts, "to", 0);
-    if (qemu_opt_get_bool(opts, "ipv4", 0))
-        ai.ai_family = PF_INET;
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
+    }
 
     /* lookup */
     if (port_offset)
@@ -319,11 +322,12 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
         return NULL;
     }
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0)) {
-        ai.ai_family = PF_INET;
-    }
-    if (qemu_opt_get_bool(opts, "ipv6", 0)) {
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
     }
 
     /* lookup */
@@ -418,10 +422,13 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
         return -1;
     }
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0))
-        ai.ai_family = PF_INET;
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
+    }
 
     if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
         error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
Signature · 1 line
1.8.5.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] socket: treat ipv4=on, ipv6=on uniformly

Quoted 5 lines · Paolo Bonzini

In some cases, "ipv4=on,ipv6=on" means "try both kinds of address"; in others, it means "try IPv6 only" just by virtue of how the code is structured.

Fix this to make things more consistent, and adjust coding style too.

Begs the question which one you're picking as the consistent interpretation, "both" or "v6 only".

#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] socket: treat ipv4=on, ipv6=on uniformly

In reply to Paolo Bonzini
Quoted 7 lines · Paolo Bonzini

In some cases, "ipv4=on,ipv6=on" means "try both kinds of address"; in others, it means "try IPv6 only" just by virtue of how the code is structured.

util/qemu-sockets.c +0 -1
@@ -127,10 +127,13 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0))

util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) · line 134

Quoted 10 lines
util/qemu-sockets.c +7 -3
-        ai.ai_family = PF_INET;
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
+    }

This is wrong for the listening side.

ipv6 sockets can listen on both ipv4 and ipv6. qemu configures ipv6 sockets to do that, unconditionally.

So ipv4=yes,ipv6=no works correctly.
ipv4=yes,ipv6=yes works too.
ipv4=no,ipv6=yes doesn't work, but to fix that you have to set the IPV6_V6ONLY socket option according to the ipv4 option.

Canevat: Listening on both ipv4+ipv6 with a single file handle works for the wildcard address only. Specifying -- say -- host=localhost,ipv4=yes,ipv6=yes, then expect qemu to listen on both 127.0.0.1 and ::1 doesn't work.

This can only be fixed by allowing multiple listening filehandles. Which is a non-trivial change as this also affects the public api (which will have to report a list of listening addresses instead of a single address).

util/qemu-sockets.c static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp) · line 328

Quoted 13 lines
util/qemu-sockets.c +6 -5
@@ -319,11 +322,12 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0)) {
-        ai.ai_family = PF_INET;
-    }
-    if (qemu_opt_get_bool(opts, "ipv6", 0)) {
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }

For connect this change is fine I think.

util/qemu-sockets.c int inet_dgram_opts(QemuOpts *opts, Error **errp) · line 429

Quoted 13 lines
util/qemu-sockets.c +7 -4
@@ -418,10 +422,13 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0))
-        ai.ai_family = PF_INET;
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
+    }

Not fully sure, but I think this is fine too.

cheers,
Gerd

#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] socket: treat ipv4=on, ipv6=on uniformly

Il 28/02/2014 13:23, Gerd Hoffmann ha scritto:

Quoted 34 lines · Paolo Bonzini
Quoted 7 lines

In some cases, "ipv4=on,ipv6=on" means "try both kinds of address"; in others, it means "try IPv6 only" just by virtue of how the code is structured.

util/qemu-sockets.c +0 -1
@@ -127,10 +127,13 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
 
-    if (qemu_opt_get_bool(opts, "ipv4", 0))

util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) · line 134

Quoted 10 lines
util/qemu-sockets.c +7 -3
-        ai.ai_family = PF_INET;
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
-        ai.ai_family = PF_INET6;
+    if (qemu_opt_get_bool(opts, "ipv4", 0) != qemu_opt_get_bool(opts, "ipv6", 0)) {
+        if (qemu_opt_get_bool(opts, "ipv4", 0)) {
+            ai.ai_family = PF_INET;
+        } else {
+            ai.ai_family = PF_INET6;
+        }
+    }

This is wrong for the listening side.

ipv6 sockets can listen on both ipv4 and ipv6. qemu configures ipv6 sockets to do that, unconditionally.

So ipv4=yes,ipv6=no works correctly.
ipv4=yes,ipv6=yes works too.
ipv4=no,ipv6=yes doesn't work, but to fix that you have to set the IPV6_V6ONLY socket option according to the ipv4 option.

Canevat: Listening on both ipv4+ipv6 with a single file handle works for the wildcard address only. Specifying -- say -- host=localhost,ipv4=yes,ipv6=yes, then expect qemu to listen on both 127.0.0.1 and ::1 doesn't work.

Quoted 4 lines

This can only be fixed by allowing multiple listening filehandles. Which is a non-trivial change as this also affects the public api (which will have to report a list of listening addresses instead of a single address).

Thanks for teaching me!

Do you think patch 2 is okay alone?

Paolo

#5 lore raw

PATCH 2/2 socket: handle ipv4/ipv6 in socket_dgram

In reply to Paolo Bonzini

We were forgetting to set the ipv4 and ipv6 QemuOpts properties, so socket_dgram was always trying _both_ ipv4 and ipv6. Forward the properties correctly.

---
 util/qemu-sockets.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
util/qemu-sockets.c +8 -1
index 7a9065b..7391aee 100644
@@ -958,15 +958,22 @@ int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
 
     opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
     switch (remote->kind) {
-    case SOCKET_ADDRESS_KIND_INET:
+    case SOCKET_ADDRESS_KIND_INET: {
+        bool ipv4 = remote->inet->ipv4 || !remote->inet->has_ipv4;
+        bool ipv6 = remote->inet->ipv6 || !remote->inet->has_ipv6;
         qemu_opt_set(opts, "host", remote->inet->host);
         qemu_opt_set(opts, "port", remote->inet->port);
         if (local) {
             qemu_opt_set(opts, "localaddr", local->inet->host);
             qemu_opt_set(opts, "localport", local->inet->port);
         }
+        if (!ipv4 || !ipv6) {
+            qemu_opt_set_bool(opts, "ipv4", ipv4);
+            qemu_opt_set_bool(opts, "ipv6", ipv6);
+        }
         fd = inet_dgram_opts(opts, errp);
         break;
+    }
 
     default:
         error_setg(errp, "socket type unsupported for datagram");
Signature · 1 line
1.8.5.3
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] socket: handle ipv4/ipv6 in socket_dgram

Quoted 4 lines · Paolo Bonzini
util/qemu-sockets.c +3 -1
-    case SOCKET_ADDRESS_KIND_INET:
+    case SOCKET_ADDRESS_KIND_INET: {
+        bool ipv4 = remote->inet->ipv4 || !remote->inet->has_ipv4;
+        bool ipv6 = remote->inet->ipv6 || !remote->inet->has_ipv6;

util/qemu-sockets.c int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) · line 973

Quoted 10 lines
util/qemu-sockets.c +4 -0
         qemu_opt_set(opts, "host", remote->inet->host);
         qemu_opt_set(opts, "port", remote->inet->port);
         if (local) {
             qemu_opt_set(opts, "localaddr", local->inet->host);
             qemu_opt_set(opts, "localport", local->inet->port);
         }
+        if (!ipv4 || !ipv6) {
+            qemu_opt_set_bool(opts, "ipv4", ipv4);
+            qemu_opt_set_bool(opts, "ipv6", ipv6);
+        }

I'd go for a simple

	if (remote->inet->has_ipv4) {
		qemu_opt_set_bool(opts, "ipv4", remote->inet->ipv4)
        }

cheers,
Gerd