Patch to improve handling of server sockets
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
The existing code stopped walking the list of addresses returned by getaddrinfo() as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set the IPV6_V6ONLY socket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.
The attached patch against QEMU 0.11.0 extends inet_listen() to create sockets for as many addresses from the address list as possible and adapts its callers and their data structures to deal with a linked list of socket FDs rather than a single file descriptor.
So far I've only done some testing with the -vnc option. More testing is needed in the qemu-char area and for the parts of the code that get triggered from QEMU's Monitor.
Please review and comment.
cu
Reinhard
P.S. Please keep me in Cc when replying.
qemu-char.c renamed from qemu-char.c.orig +40 -18
@@ -1831,7 +1831,8 @@ return_err:/* TCP Net console */ typedef struct { - int fd, listen_fd; + int fd; + FdList *listen_fds; int connected; int max_size; int do_telnetopt;
@@ -1983,6 +1984,7 @@ static void tcp_chr_read(void *opaque)TCPCharDriver *s = chr->opaque;uint8_t buf[1024 ];int len, size; +FdList *fdl;if (!s->connected || s->max_size <=0 )return ;
@@ -1993,10 +1995,9 @@ static void tcp_chr_read(void *opaque)if (size ==0 ) {/* connection closed */ s->connected =0 ; -if (s->listen_fd >=0 ) { -qemu_set_fd_handler (s->listen_fd, tcp_chr_accept,NULL , chr); - } -qemu_set_fd_handler (s->fd,NULL ,NULL ,NULL ); +for (fdl = s->listen_fds; fdl !=NULL ; fdl = fdl->next) +qemu_set_fd_handler (fdl->fd, tcp_chr_accept,NULL , fdl); +qemu_set_fd_handler (s->fd,NULL ,NULL ,NULL );closesocket (s->fd); s->fd =-1 ; }else if (size >0 ) {
@@ -2045,7 +2046,8 @@ static void socket_set_nodelay(int fd)static void tcp_chr_accept (void *opaque) { -CharDriverState *chr = opaque; +FdList *fdl = opaque; +CharDriverState *chr = fdl->opaque;TCPCharDriver *s = chr->opaque;struct sockaddr_in saddr;#ifndef _WIN32
@@ -2066,7 +2068,7 @@ static void tcp_chr_accept(void *opaque) len =sizeof (saddr); addr = (struct sockaddr *)&saddr; } - fd =accept (s->listen_fd, addr, &len); + fd =accept (fdl->fd, addr, &len);if (fd <0 && errno !=EINTR ) {return ; }else if (fd >=0 ) {
@@ -2079,20 +2081,24 @@ static void tcp_chr_accept(void *opaque)if (s->do_nodelay)socket_set_nodelay (fd); s->fd = fd; -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +for (fdl = s->listen_fds; fdl !=NULL ; fdl = fdl->next) +qemu_set_fd_handler (fdl->fd,NULL ,NULL ,NULL );tcp_chr_connect (chr); }static void tcp_chr_close (CharDriverState *chr) {TCPCharDriver *s = chr->opaque; +FdList *fdl, *fdtmp;if (s->fd >=0 ) {qemu_set_fd_handler (s->fd,NULL ,NULL ,NULL );closesocket (s->fd); } -if (s->listen_fd >=0 ) { -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); -closesocket (s->listen_fd); +for (fdl = s->listen_fds; fdl !=NULL ; fdl = fdtmp) { + fdtmp = fdl->next; +qemu_set_fd_handler (fdl->fd,NULL ,NULL ,NULL ); +closesocket (fdl->fd); +free (fdl); }qemu_free (s); }
@@ -2108,6 +2114,7 @@ static CharDriverState *qemu_chr_open_tcint is_waitconnect =1 ;int do_nodelay =0 ;const char *ptr; +FdList *sockets =NULL , *fdl, *fdtmp; ptr = host_str;while ((ptr =strchr (ptr,',' ))) {
@@ -2155,11 +2162,18 @@ static CharDriverState *qemu_chr_open_tc }else {if (is_listen) { fd =inet_listen (host_str, chr->filename + offset,256 - offset, -SOCK_STREAM ,0 ); +SOCK_STREAM ,0 , &sockets); }else { fd =inet_connect (host_str,SOCK_STREAM ); } } + +if (sockets ==NULL ) { + sockets =malloc (sizeof (*sockets)); + sockets->next =NULL ; + sockets->fd = fd; + } +if (fd <0 ) goto fail;
@@ -2168,7 +2182,7 @@ static CharDriverState *qemu_chr_open_tc s->connected =0 ; s->fd =-1 ; - s->listen_fd =-1 ; + s->listen_fds =NULL ; s->msgfd =-1 ; s->is_unix = is_unix; s->do_nodelay = do_nodelay && !is_unix;
@@ -2179,8 +2193,11 @@ static CharDriverState *qemu_chr_open_tc chr->get_msgfd = tcp_get_msgfd;if (is_listen) { - s->listen_fd = fd; -qemu_set_fd_handler (s->listen_fd, tcp_chr_accept,NULL , chr); + s->listen_fds = sockets; +for (fdl = sockets; fdl !=NULL ; fdl = fdl->next) { + fdl->opaque = chr; +qemu_set_fd_handler (fdl->fd, tcp_chr_accept,NULL , fdl); + }if (is_telnet) s->do_telnetopt =1 ; }else {
@@ -2191,16 +2208,21 @@ static CharDriverState *qemu_chr_open_tc }if (is_listen && is_waitconnect) { +FdList *fdl;printf ("QEMU waiting for connection on: %s\n" , chr->filename ? chr->filename : host_str);tcp_chr_accept (chr); -socket_set_nonblock (s->listen_fd); +for (fdl = s->listen_fds; fdl !=NULL ; fdl = fdl->next) +socket_set_nonblock (fdl->fd); }return chr; fail: -if (fd >=0 ) -closesocket (fd); +for (fdl = sockets; fdl !=NULL ; fdl = fdtmp) { + fdtmp = fdl->next; +closesocket (fdl->fd); +free (fdl); + }qemu_free (s);qemu_free (chr);return NULL ;
qemu-sockets.c renamed from qemu-sockets.c.orig +31 -28
95 lines not on this page · raw message
qemu_socket.h renamed from qemu_socket.h.orig +8 -1
@@ -29,13 +29,20 @@ int inet_aton(const char *cp, struct in_#endif /* !_WIN32 */ +struct FdList ; +typedef struct FdList { +int fd; +void *opaque; +struct FdList *next; +}FdList ; +/* misc helpers */ void socket_set_nonblock (int fd);int send_all (int fd,const void *buf,int len1);/* New, ipv6-ready socket helper functions, see qemu-sockets.c */ int inet_listen (const char *str,char *ostr,int olen, -int socktype,int port_offset); +int socktype,int port_offset,FdList **sockets);int inet_connect (const char *str,int socktype);int unix_listen (const char *path,char *ostr,int olen);
vnc.c renamed from vnc.c.orig +49 -28
162 lines not on this page · raw message
vnc.h renamed from vnc.h.orig +2 -1
@@ -28,6 +28,7 @@#define __QEMU_VNC_H#include "qemu-common.h" +#include "qemu_socket.h" #include "console.h" #include "monitor.h" #include "audio/audio.h"
@@ -87,7 +88,7 @@ typedef struct VncDisplay VncDisplay;struct VncDisplay { -int lsock; +FdList *lsocks;DisplayState *ds;VncState *clients;kbd_layout_t *kbd_layout;
Quoted 11 lines · Reinhard Max
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
The existing code stopped walking the list of addresses returned by
getaddrinfo()as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set theIPV6_V6ONLYsocket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.The attached patch against QEMU 0.11.0 extends
inet_listen()to create sockets for as many addresses from the address list as possible and adapts its callers and their data structures to deal with a linked list of socket FDs rather than a single file descriptor.So far I've only done some testing with the -vnc option. More testing is needed in the qemu-char area and for the parts of the code that get triggered from QEMU's Monitor.
0.11.0 is pretty old. Please update your patch against the latest git.
But that said, I'm not sure we're doing the wrong thing right now. Gerd, what do you think about this behavior?
Regards,
Anthony Liguori
Quoted 8 lines
Please review and comment.
cu
Reinhard
P.S. Please keep me in Cc when replying.
Hi,
Quoted 1 line · Anthony Liguori
0.11.0 is pretty old. Please update your patch against the latest git.
Ok, will do.
Quoted 1 line
I'm not sure we're doing the wrong thing right now.
Well, I think it just can't be correct, that an IPv6-enabled process running on a dual-stack machine when it gets told to listen on "localhost", ends up only listening on ::1 and doesn't accept connections to 127.0.0.1.
cu
Reinhard
Quoted 11 lines · Reinhard Max
Hi,
Quoted 1 line · Anthony Liguori
0.11.0 is pretty old. Please update your patch against the latest git.
Ok, will do.
Quoted 1 line
I'm not sure we're doing the wrong thing right now.
Well, I think it just can't be correct, that an IPv6-enabled process running on a dual-stack machine when it gets told to listen on "localhost", ends up only listening on ::1
My understanding is that for the majority of systems, 127.0.0.1 should still connect to ::1. The reason we have the ipv4 and ipv6 options is because this doesn't work 100% of the time. Maybe your configuration is an example of this.
Honestly, I don't understand how ipv4 compat is supposed to work outside of qemu so I'm looking for some additional input.
Regards,
Anthony Liguori
Quoted 5 lines
and doesn't accept connections to 127.0.0.1.
cu
Reinhard
Quoted 11 lines · Anthony Liguori
Quoted 10 lines · Reinhard Max
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
My tigervnc (tightvnc successor) has IPv6 support and handles this just fine. There is also the option to force qemu to listen on ipv4 (or ipv6) only.
Quoted 7 lines
Quoted 7 lines
The existing code stopped walking the list of addresses returned by
getaddrinfo()as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set theIPV6_V6ONLYsocket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.
Indeed.
Quoted 2 lines
But that said, I'm not sure we're doing the wrong thing right now. Gerd, what do you think about this behavior?
Reinhard is correct. If a hostname resolves to multiple addresses like this ...
zweiblum kraxel ~# host zweiblum zweiblum.home.kraxel.org has address 192.168.2.101 zweiblum.home.kraxel.org has IPv6 address
2001:6f8:1cb3:2:216:41ff:fee1:3d40
... qemu should listen on all addresses returned. Which in turn requires multiple listening sockets.
Changing this is a big deal though, thats why I've taken the somewhat unclean shortcut to listen on the first match only when implementing this. Clients are supposed to try to connect to all addresses returned by the lookup (and they do if they got IPv6 support), thus this usually doesn't cause trouble in practice.
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more. Reporting the vnc server address in QMP is one. I'm sure there are more.
cheers,
Gerd
Quoted 35 lines · Gerd Hoffmann
Quoted 11 lines · Anthony Liguori
Quoted 10 lines · Reinhard Max
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
My tigervnc (tightvnc successor) has IPv6 support and handles this just fine. There is also the option to force qemu to listen on ipv4 (or ipv6) only.
Quoted 7 lines
Quoted 7 lines
The existing code stopped walking the list of addresses returned by
getaddrinfo()as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set theIPV6_V6ONLYsocket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.Indeed.
Quoted 2 lines
But that said, I'm not sure we're doing the wrong thing right now. Gerd, what do you think about this behavior?
Reinhard is correct. If a hostname resolves to multiple addresses like this ...
zweiblum kraxel ~# host zweiblum zweiblum.home.kraxel.org has address 192.168.2.101 zweiblum.home.kraxel.org has IPv6 address
2001:6f8:1cb3:2:216:41ff:fee1:3d40
Quoted 5 lines
... qemu should listen on all addresses returned. Which in turn requires multiple listening sockets.
Changing this is a big deal though, thats why I've taken the somewhat unclean shortcut to listen on the first match only when implementing this. Clients are supposed to try to connect to all addresses returned by the lookup (and they do if they got IPv6 support), thus this usually doesn't cause trouble in practice.
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more. Reporting the vnc server address in QMP is one. I'm sure there are more.
Okay, that makes sense. Personally, I'm inclined to agree that this is a client problem.
Regards,
Anthony Liguori
Quoted 3 lines
cheers,
Gerd
Quoted 1 line · Anthony Liguori
Personally, I'm inclined to agree that this is a client problem.
That would be a violation of the "be liberal in what you accept and conservative in what you produce" principle and there are plenty of scenarios where even a client that Does It Right[tm] would fail, one example being that both ends are in networks that use public IPv6 addresses, but have no IPv6 routing to each other (and yes, I've seen such networks).
cu
Reinhard
Quoted 48 lines · Anthony Liguori
Quoted 47 lines · Gerd Hoffmann
Quoted 11 lines · Anthony Liguori
Quoted 10 lines · Reinhard Max
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
My tigervnc (tightvnc successor) has IPv6 support and handles this just fine. There is also the option to force qemu to listen on ipv4 (or ipv6) only.
Quoted 7 lines
Quoted 7 lines
The existing code stopped walking the list of addresses returned by
getaddrinfo()as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set theIPV6_V6ONLYsocket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.Indeed.
Quoted 2 lines
But that said, I'm not sure we're doing the wrong thing right now. Gerd, what do you think about this behavior?
Reinhard is correct. If a hostname resolves to multiple addresses like this ...
zweiblum kraxel ~# host zweiblum zweiblum.home.kraxel.org has address 192.168.2.101 zweiblum.home.kraxel.org has IPv6 address
2001:6f8:1cb3:2:216:41ff:fee1:3d40... qemu should listen on all addresses returned. Which in turn requires multiple listening sockets.
Changing this is a big deal though, thats why I've taken the somewhat unclean shortcut to listen on the first match only when implementing this. Clients are supposed to try to connect to all addresses returned by the lookup (and they do if they got IPv6 support), thus this usually doesn't cause trouble in practice.
Quoted 7 lines
Quoted 4 lines
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more. Reporting the vnc server address in QMP is one. I'm sure there are more.
Okay, that makes sense. Personally, I'm inclined to agree that this is a client problem.
It isn't a client problem if QEMU is not listening on all the required addresses. In Gerd's case
Quoted 3 lines
Quoted 3 lines
zweiblum.home.kraxel.org has address 192.168.2.101 zweiblum.home.kraxel.org has IPv6 address
2001:6f8:1cb3:2:216:41ff:fee1:3d40
If QEMU only listens on 2001:6f8:1cb3:2:216:41ff:fee1:3d40, and a VNC client that only knows IPv4 tries to connect it will fail. There's nothing the client can do to solve this. QEMU has to be listening on all addresses associated with a name for the dual-stack setup to provide correct fallback for clients.
Daniel
Signature · 4 lines
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Quoted 1 line · Gerd Hoffmann
My tigervnc (tightvnc successor) has IPv6 support and handles this just fine.
Well, as I wrote, the code is not just used for vnc, but also for server sockets that (if I got that right) could be connected from telnet or netcat implementations that don't speak IPv6.
Quoted 1 line
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more.
Well, that's what my patch is about. Did you take a look at it?
Quoted 1 line
Reporting the vnc server address in QMP is one.
Not sure what QMP is (this was the first time I looked at QEMU's internals), but I think my patch only leaves one place TODO where I chose to report only the first address for now, but it shouldn't be too hard to fix that as well.
BTW, in some places I circumvented the need for reporting multiple addresses by simply reporting the name that was passed to QEMU instead.
cu
Reinhard
Hi,
Quoted 5 lines
Quoted 3 lines
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more.
Well, that's what my patch is about.
Sure.
Quoted 1 line
Did you take a look at it?
Briefly, yes. Overall it looks sensible to me. Devil is in the details though, see below.
Noticed that it probably should get a few helper functions to handle FdLists to avoid the quite simliar open-coded loop-over-all-fds loops all over the place.
Quoted 4 lines
Quoted 1 line
Reporting the vnc server address in QMP is one.
Not sure what QMP is (this was the first time I looked at QEMU's internals),
You'll run into qmp for sure when forward-porting the patches to the latest qemu bits. It is the machine-readable version of the monitor protocol (in qemu 0.12+).
Quoted 3 lines
but I think my patch only leaves one place TODO where I chose to report only the first address for now, but it shouldn't be too hard to fix that as well.
Yea. I've noticed that TODO ;)
Quoted 2 lines
BTW, in some places I circumvented the need for reporting multiple addresses by simply reporting the name that was passed to QEMU instead.
This is one of the issues which needs to be addressed somehow.
First I think qemu should be self-consistent here, i.e. either report the (single) name or the list of addressed everythere.
Second we have to care about the current users (especially libvirt). Today qemu usually reports the address I think. Thus I tend to stick to addresses to keep them happy.
We'll have a externally visible change in any case though. Either the switch from the address to the name or the switch from a single address to a list of addresses. Both changes might break existing users.
cheers,
Gerd
Hi,
Quoted 1 line · Gerd Hoffmann
Noticed that it probably should get a few helper functions to handle FdLists to avoid the quite simliar open-coded loop-over-all-fds loops all over the place.
indeed, thanks for the hint. I now have functions to create a new list element from an fd number and to destroy a list. Not sure if straight loops over existing lists can be further optimized, though.
Quoted 1 line
You'll run into qmp for sure when forward-porting the patches to the latest qemu bits. It is the machine-readable version of the monitor protocol (in qemu 0.12+).
I guess that's the qemu_opt_set() calls at the end of inet_listen_opts()?
Quoted 1 line
First I think qemu should be self-consistent here, i.e. either report the (single) name or the list of addressed everythere.
Yes, this mixture wasn't meant to be final, but it helped me getting the initial patch done with a minimal set of changes.
Quoted 1 line
Second we have to care about the current users (especially libvirt).
Wouldn't the users of that bit of information run it through getaddrinfo() anyways when trying to connect? So to them it shouldn't matter whether the name or an ASCII representation of the address is used.
Quoted 1 line
Today qemu usually reports the address I think. Thus I tend to stick to addresses to keep them happy.
But wouldn't going from single address to multiple addresses be a bigger change for the users (and likely break them all) while going from address to name would only break those that were not using getaddrinfo() to translate the address into its binary representation.
OTOH, going for multiple addresses would also allow starting qemu with more than a single -vnc option, which doesn't seem to be possible right now, and wich might come handy in situations when the set of addresses a qemu instance should be listening on is not available as a single DNS name.
cu
Reinhard
Hi,
Quoted 5 lines
Quoted 3 lines
You'll run into qmp for sure when forward-porting the patches to the latest qemu bits. It is the machine-readable version of the monitor protocol (in qemu 0.12+).
I guess that's the
qemu_opt_set()calls at the end ofinet_listen_opts()?
See docs in QMP/*, the changes in monitor.c and q${type}.[ch]
qemu_opt_set() in inet_listen_opts() is only slightly related. It is used to report back the address we've actually bound to. Used by 'info chardev' and I think vnc too. Yes, that has to be changed somehow ...
Quoted 5 lines
Quoted 1 line
Second we have to care about the current users (especially libvirt).
Wouldn't the users of that bit of information run it through
getaddrinfo()anyways when trying to connect? So to them it shouldn't matter whether the name or an ASCII representation of the address is used.
I don't know how it is used.
Quoted 7 lines
Quoted 2 lines
Today qemu usually reports the address I think. Thus I tend to stick to addresses to keep them happy.
But wouldn't going from single address to multiple addresses be a bigger change for the users (and likely break them all) while going from address to name would only break those that were not using
getaddrinfo()to translate the address into its binary representation.
It is probably best to bring this up on the libvirt list, this is the most important user of those interfaces and I think other virtual machine management folks are reading there too.
I personally don't care too much which way we pick.
Quoted 4 lines
OTOH, going for multiple addresses would also allow starting qemu with more than a single -vnc option, which doesn't seem to be possible right now, and wich might come handy in situations when the set of addresses a qemu instance should be listening on is not available as a single DNS name.
Good point.
cheers,
Gerd
Quoted 39 lines · Gerd Hoffmann
Hi,
Quoted 5 lines
Quoted 3 lines
When going for multiple listening sockets in qemu we have to figure how we'll handle this in a number of places as there is no single listening address any more.
Well, that's what my patch is about.
Sure.
Quoted 1 line
Did you take a look at it?
Briefly, yes. Overall it looks sensible to me. Devil is in the details though, see below.
Noticed that it probably should get a few helper functions to handle FdLists to avoid the quite simliar open-coded loop-over-all-fds loops all over the place.
Quoted 4 lines
Quoted 1 line
Reporting the vnc server address in QMP is one.
Not sure what QMP is (this was the first time I looked at QEMU's internals),
You'll run into qmp for sure when forward-porting the patches to the latest qemu bits. It is the machine-readable version of the monitor protocol (in qemu 0.12+).
Quoted 3 lines
but I think my patch only leaves one place TODO where I chose to report only the first address for now, but it shouldn't be too hard to fix that as well.
Yea. I've noticed that TODO ;)
Quoted 2 lines
BTW, in some places I circumvented the need for reporting multiple addresses by simply reporting the name that was passed to QEMU instead.
This is one of the issues which needs to be addressed somehow.
Quoted 8 lines
First I think qemu should be self-consistent here, i.e. either report the (single) name or the list of addressed everythere.
Second we have to care about the current users (especially libvirt). Today qemu usually reports the address I think. Thus I tend to stick to addresses to keep them happy.
From a libvirt POV the only place we use the address is in the graphics
notification event(s). For the event we know which specific address out of the list to report - the one we just did accept() on. libvirt does not use the 'info vnc' or 'query-vnc' output at all, so whether that reports IP address of hostname doesn't matter to us. It is probably easiest to just make it report the same info that was provided on the CLI to -vnc
Regards,
Daniel
Signature · 4 lines
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Quoted 11 lines · Reinhard Max
Hi,
I am maintaining the tightvnc package for openSUSE and was recently confronted with an alleged vnc problem with QWMU that turned out to be a shortcoming in QEMU's code for handling TCP server sockets, which is used by the vnc and char modules.
The problem occurs when the address to listen on is given as a name which resolves to multiple IP addresses the most prominent example being "localhost" resolving to 127.0.0.1 and ::1 .
Quoted 7 lines
The existing code stopped walking the list of addresses returned by
getaddrinfo()as soon as one socket was successfully opened and bound. The result was that a qemu instance started with "-vnc localhost:42" only listened on ::1, wasn't reachable through 127.0.0.1. The fact that the code set theIPV6_V6ONLYsocket option didn't help, because that option only works when the socket gets bound to the IPv6 wildcard address (::), but is useless for explicit address bindings.
This seems to be something we overlooked in the initial impl. I don't see any alternative but to make QEMU listen on multiple sockets in the scenario you describe. An even clear example is to consider binding QEMU to a machine with multiple non-localhost addresses, eg
myserver.com resolving to 192.168.122.41 + 2a00:123:456::1
because there's no way that the kernel can know/decide to automatically listen on 192.168.122.41, when given the address 2a00:123:456::1 and if the machine has many NICs, you can't assume the wildcard address is suitable either.
Quoted 4 lines
The attached patch against QEMU 0.11.0 extends
inet_listen()to create sockets for as many addresses from the address list as possible and adapts its callers and their data structures to deal with a linked list of socket FDs rather than a single file descriptor.
The approach looks reasonable, though the patch is somewhat mangled by the mix of tabs + spaces
Regards,
Daniel
Signature · 4 lines
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Hi,
I've attached a revised version of the patch against git as of this morning.
It has even more FIXMEs in it than the previous version, because of the changes that went into QEMU since 0.11.0 and I think I'll have to leave those to you guys who are more familiar with QEMUs internals than I am, but I hope my pactch can serve as a starting point getting the socket handling in QEMU more correct and flexible.
Quoted 1 line · Daniel P. Berrange
The approach looks reasonable, though the patch is somewhat mangled by the mix of tabs + spaces
Well, that's what XEmacs gives me by default. ;)
How about adding magic comments for vim and emacs to the source files that bring the editors in line with the project's coding style to make it easier for new contributors to follow the style?
BTW, while converting the tabs to spaces in my changes, I found that there are also quite a few tabs in the code already.
cu
Reinhard
qemu-char.c +31 -16
@@ -1915,7 +1915,8 @@ return_err:/* TCP Net console */ typedef struct { - int fd, listen_fd; + int fd; + FdList *listen_fds; int connected; int max_size; int do_telnetopt;
@@ -2068,6 +2069,7 @@ static void tcp_chr_read(void *opaque)TCPCharDriver *s = chr->opaque;uint8_t buf[READ_BUF_LEN ];int len, size; +FdList *fdl;if (!s->connected || s->max_size <=0 )return ;
@@ -2078,9 +2080,8 @@ static void tcp_chr_read(void *opaque)if (size ==0 ) {/* connection closed */ s->connected =0 ; -if (s->listen_fd >=0 ) { -qemu_set_fd_handler (s->listen_fd, tcp_chr_accept,NULL , chr); - } +for (fdl = s->listen_fds; fdl !=NULL ; fdl = fdl->next) +qemu_set_fd_handler (fdl->fd, tcp_chr_accept,NULL , fdl);qemu_set_fd_handler (s->fd,NULL ,NULL ,NULL );closesocket (s->fd); s->fd =-1 ;
@@ -2127,7 +2128,8 @@ static void socket_set_nodelay(int fd)static void tcp_chr_accept (void *opaque) { -CharDriverState *chr = opaque; +FdList *fds = opaque; +CharDriverState *chr = fds->opaque;TCPCharDriver *s = chr->opaque;struct sockaddr_in saddr;#ifndef _WIN32
@@ -2148,7 +2150,7 @@ static void tcp_chr_accept(void *opaque) len =sizeof (saddr); addr = (struct sockaddr *)&saddr; } - fd =qemu_accept (s->listen_fd, addr, &len); + fd =qemu_accept (fds->fd, addr, &len);if (fd <0 && errno !=EINTR ) {return ; }else if (fd >=0 ) {
@@ -2161,21 +2163,24 @@ static void tcp_chr_accept(void *opaque)if (s->do_nodelay)socket_set_nodelay (fd); s->fd = fd; -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +for (fds = s->listen_fds; fds !=NULL ; fds = fds->next) +qemu_set_fd_handler (fds->fd,NULL ,NULL ,NULL );tcp_chr_connect (chr); }static void tcp_chr_close (CharDriverState *chr) {TCPCharDriver *s = chr->opaque; +FdList *fds;if (s->fd >=0 ) {qemu_set_fd_handler (s->fd,NULL ,NULL ,NULL );closesocket (s->fd); } -if (s->listen_fd >=0 ) { -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); -closesocket (s->listen_fd); +for (fds = s->listen_fds; fds !=NULL ; fds = fds->next) { +qemu_set_fd_handler (fds->fd,NULL ,NULL ,NULL ); +closesocket (fds->fd); } +fdlist_free (fds);qemu_free (s);qemu_chr_event (chr,CHR_EVENT_CLOSED ); }
@@ -2190,6 +2195,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)int do_nodelay;int is_unix;int is_telnet; +FdList *socks =NULL ; is_listen =qemu_opt_get_bool (opts,"server" ,0 ); is_waitconnect =qemu_opt_get_bool (opts,"wait" ,1 );
@@ -2205,17 +2211,20 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)if (is_unix) {if (is_listen) { fd =unix_listen_opts (opts); +if (fd <=0 ) + socks =fdlist_new (fd); }else { fd =unix_connect_opts (opts); } }else {if (is_listen) { - fd =inet_listen_opts (opts,0 ); + socks =inet_listen_opts (opts,0 ); }else { fd =inet_connect_opts (opts); } } -if (fd <0 ) + +if (socks ==NULL && fd <0 ) goto fail;if (!is_waitconnect)
@@ -2223,7 +2232,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) s->connected =0 ; s->fd =-1 ; - s->listen_fd =-1 ; + s->listen_fds =NULL ; s->msgfd =-1 ; s->is_unix = is_unix; s->do_nodelay = do_nodelay && !is_unix;
@@ -2234,8 +2243,11 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) chr->get_msgfd = tcp_get_msgfd;if (is_listen) { - s->listen_fd = fd; -qemu_set_fd_handler (s->listen_fd, tcp_chr_accept,NULL , chr); + s->listen_fds = socks; +for (; socks !=NULL ; socks = socks->next) { + socks->opaque = chr; +qemu_set_fd_handler (socks->fd, tcp_chr_accept,NULL , socks); + }if (is_telnet) s->do_telnetopt =1 ;
@@ -2266,7 +2278,9 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)printf ("QEMU waiting for connection on: %s\n" , chr->filename);tcp_chr_accept (chr); -socket_set_nonblock (s->listen_fd); +for (socks = s->listen_fds; socks !=NULL ; socks = socks->next) { +socket_set_nonblock (socks->fd); + } }return chr;
@@ -2275,6 +2289,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)closesocket (fd);qemu_free (s);qemu_free (chr); +fdlist_free (socks);return NULL ; }
qemu-sockets.c +53 -28
157 lines not on this page · raw message
qemu_socket.h +11 -2
@@ -29,6 +29,13 @@ int inet_aton(const char *cp, struct in_addr *ia);#endif /* !_WIN32 */ +struct FdList ; +typedef struct FdList { +int fd; +void *opaque; +struct FdList *next; +}FdList ; +#include "qemu-option.h" /* misc helpers */
@@ -36,10 +43,12 @@ int qemu_socket(int domain, int type, int protocol);int qemu_accept (int s,struct sockaddr *addr,socklen_t *addrlen);void socket_set_nonblock (int fd);int send_all (int fd,const void *buf,int len1); +FdList *fdlist_new (int fd); +void fdlist_free (FdList *fds);/* New, ipv6-ready socket helper functions, see qemu-sockets.c */ -int inet_listen_opts (QemuOpts *opts,int port_offset); -int inet_listen (const char *str,char *ostr,int olen, +FdList *inet_listen_opts (QemuOpts *opts,int port_offset); +FdList *inet_listen (const char *str,char *ostr,int olen,int socktype,int port_offset);int inet_connect_opts (QemuOpts *opts);int inet_connect (const char *str,int socktype);
vnc.c +35 -20
133 lines not on this page · raw message
vnc.h +2 -1
@@ -28,6 +28,7 @@#define __QEMU_VNC_H#include "qemu-common.h" +#include "qemu_socket.h" #include "qemu-queue.h" #include "console.h" #include "monitor.h"
@@ -96,7 +97,7 @@ struct VncDisplay QTAILQ_HEAD(, VncState) clients;QEMUTimer *timer;int timer_interval; -int lsock; +FdList *lsocks;DisplayState *ds;kbd_layout_t *kbd_layout;int lock_key_sync;