Multiple fixes & improvements to QIOChannel & Win32
Subsystems (4)
v2 cover Multiple fixes & improvements to QIOChannel & Win32
This series started out as an attempt to fix the Win32 problems identified by Andrew Baumann
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg01343.html
It turned into a significantly larger cleanup of some chardev and osdep win32 portability code.
Patch 1 addresses Andrew's 2nd stated problem - handling of getpeername() failures, by fixing errno handling on Win32.
Patches 2-7 do some fixes in the test-io-channel-socket test case so that it is able to run on Win32.
Patches 8-12 are some fixes for the QIOChannel code
Patch 13 is the big one that changes QIOChannelSocket so that it uses a Win32 specific GSource implementation for creating watches. This is the key fix for Andrew's 1st stated problem.
At this point tests/test-io-channel-socket passes and
qemu-system-x86_64.exe -serial tcp:127.0.0.1:9000,server,nowait -device sga -display non
works on win32 once more.
Patches 14-16 are some cleanups to the chardev code to improve its clarity. They are not required for fixing any real problem
Patches 17-18 change the way we provide Win32 portability for sockets APIs inside QEMU. These do fix a number of bugs in the QEMU code related to mistaken use of errno instead of socket_error(). None of these bugs appear to be critical issues.
Based on this, I'm proposing 1-13 for QEMU 2.6 release as they fix critical win32 bugs.
Patches 14-18 can either be included in 2.6 or 2.7 - I'm ambivalent on which, since they're cleanups / minor fixes.
Changed in v2:
- Set event flags in qio_channel_socket_set_blocking instead of when creating GSource
- Drop now uneeded patches that set QIOChanel to blocking mode
- Change WSAEWOULDBLOCK into EAGAIN to avoid needing to check EWOULDBLOCK
- Drop patches that replaced qemu_accept/qemu_socket
- Drop wrapping of close/ioctl in favour of closesocket/ ioctlsocket.
Daniel P. Berrange (15):
osdep: fix socket_error() to work with Mingw64
io: use bind() to check for IPv4/6 availability
io: initialize sockets in test program
io: bind to socket before creating QIOChannelSocket
io: wait for incoming client in socket test
io: set correct error object in background reader test thread
io: assert errors before asserting content in I/O test
io: fix copy+paste mistake in socket error message
io: use qemu_accept to ensure SOCK_CLOEXEC is set
io: remove checking of EWOULDBLOCK
char: remove qemu_chr_finish_socket_connection method
char: remove socket_try_connect method
char: remove qemu_chr_open_socket_fd method
osdep: add wrappers for socket functions
osdep: remove use of socket_error() from all code
Paolo Bonzini (3):
io: pass HANDLE to g_source_add_poll on Win32
io: introduce qio_channel_create_socket_watch
io: implement socket watch for win32 using WSAEventSelect+select
Makefile | 4 +- block/sheepdog.c | 5 +- include/io/channel-watch.h | 20 ++- include/io/channel.h | 3 + include/qemu/sockets.h | 17 --- include/sysemu/os-posix.h | 9 ++ include/sysemu/os-win32.h | 106 ++++++++++++---- io/channel-command.c | 6 +- io/channel-file.c | 6 +- io/channel-socket.c | 84 ++++++++----- io/channel-watch.c | 152 +++++++++++++++++++++- io/channel.c | 14 +++ linux-user/flatload.c | 1 - migration/qemu-file-unix.c | 14 +-- migration/tcp.c | 7 +- net/socket.c | 19 ++- qemu-char.c | 96 ++++++-------- slirp/slirp.h | 2 - slirp/tcp_input.c | 4 - tests/io-channel-helpers.c | 6 +- tests/test-io-channel-socket.c | 92 +++++++------- util/oslib-win32.c | 280 ++++++++++++++++++++++++++++++++++++++++- util/qemu-coroutine-io.c | 6 +- util/qemu-sockets.c | 10 +- 24 files changed, 719 insertions(+), 244 deletions(-)
Signature · 1 line
2.5.0
PATCH v2 1/18 osdep: fix socket_error() to work with Mingw64
Historically QEMU has had a socket_error() macro that was defined to map to WSASocketError(). The os-win32.h header file would define errno constants that mapped to the WSA error constants. This worked fine with Mingw32 since its header files never defined any errno values, nor did it even provide an errno.h. So callers of socket_error() could match on traditional Exxxx constants and it would all "just work".
With Mingw64 though, things work rather differently. First there is an errno.h file which defines all the traditional errno constants you'd expect from a UNIX platform. There is then a winerror.h which defined the WSA error constants. Crucially the WSAExxxx errno values in winerror.h do not match the Exxxx errno values in error.h.
If QEMU had only imported winerror.h it would still work, but the qemu/osdep.h file unconditionally imports errno.h. So callers of socket_error() will get now WSAExxxx values back and compare them to the Exxx constants. This will always fail silently at runtime.
To solve this QEMU needs to stop assuming the WSAExxxx constant values match the Exxx constant values. Thus the socket_error() macro is turned into a small function that re-maps WSAExxxx values into Exxx.
--- include/qemu/sockets.h | 3 -- include/sysemu/os-posix.h | 2 ++ include/sysemu/os-win32.h | 27 +--------------- util/oslib-win32.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 30 deletions(-)
include/qemu/sockets.h +0 -3
@@ -7,8 +7,6 @@#include <winsock2.h> #include <ws2tcpip.h> -#define socket_error () WSAGetLastError() -int inet_aton (const char *cp,struct in_addr *ia);#else
@@ -20,7 +18,6 @@ int inet_aton(const char *cp, struct in_addr *ia);#include <netdb.h> #include <sys/un.h> -#define socket_error () errno#define closesocket (s) close(s)#endif /* !_WIN32 */
include/sysemu/os-posix.h +2 -0
@@ -34,6 +34,8 @@ void os_daemonize(void);void os_setup_post (void );int os_mlock (void ); +#define socket_error () errno +typedef struct timeval qemu_timeval ;#define qemu_gettimeofday (tp) gettimeofday(tp, NULL)
include/sysemu/os-win32.h +1 -26
@@ -29,32 +29,6 @@#include <winsock2.h> #include <windows.h> -/* Workaround for older versions of MinGW. */ -#ifndef ECONNREFUSED -# define ECONNREFUSED WSAECONNREFUSED -#endif -#ifndef EINPROGRESS -# define EINPROGRESS WSAEINPROGRESS -#endif -#ifndef EHOSTUNREACH -# define EHOSTUNREACH WSAEHOSTUNREACH -#endif -#ifndef EINTR -# define EINTR WSAEINTR -#endif -#ifndef EINPROGRESS -# define EINPROGRESS WSAEINPROGRESS -#endif -#ifndef ENETUNREACH -# define ENETUNREACH WSAENETUNREACH -#endif -#ifndef ENOTCONN -# define ENOTCONN WSAENOTCONN -#endif -#ifndef EWOULDBLOCK -# define EWOULDBLOCK WSAEWOULDBLOCK -#endif -#if defined(_WIN64)/* On w64, setjmp is implemented by _setjmp which needs a second parameter. * If this parameter is NULL, longjump does no stack unwinding.
@@ -80,6 +54,7 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);struct tm *localtime_r (const time_t *timep,struct tm *result);#endif /* CONFIG_LOCALTIME_R */ +int socket_error (void );static inline void os_setup_signal_handling (void ) {}static inline void os_daemonize (void ) {}
util/oslib-win32.c +78 -1
91 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v2 2/18 io: use bind() to check for IPv4/6 availability
In reply to Daniel P. BerrangeCurrently the test-io-channel-socket.c test uses getifaddrs to see if an IPv4/6 address is present on any host NIC, as a way to determine if IPv4/6 sockets can be used. This is problematic because getifaddrs is not available on Win32.
Rather than testing indirectly via getifaddrs, just create a socket and try to bind() to the loopback address instead.
--- tests/test-io-channel-socket.c | 79 +++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 48 deletions(-)
tests/test-io-channel-socket.c +31 -48
@@ -22,66 +22,49 @@#include "io/channel-socket.h" #include "io/channel-util.h" #include "io-channel-helpers.h" -#ifdef HAVE_IFADDRS_H -#include <ifaddrs.h> -#endif -static int check_protocol_support (bool *has_ipv4,bool *has_ipv6) +static int check_bind (struct sockaddr *sa,socklen_t salen,bool *has_proto) { -#ifdef HAVE_IFADDRS_H -struct ifaddrs *ifaddr =NULL , *ifa; -struct addrinfo hints = {0 }; -struct addrinfo *ai =NULL ; -int gaierr; - - *has_ipv4 = *has_ipv6 = false; +int fd; -if (getifaddrs (&ifaddr) <0 ) { -g_printerr ("Failed to lookup interface addresses: %s\n" , -strerror (errno)); + fd =socket (sa->sa_family,SOCK_STREAM ,0 ); +if (fd <0 ) {return -1 ; } -for (ifa = ifaddr; ifa !=NULL ; ifa = ifa->ifa_next) { -if (!ifa->ifa_addr) { -continue ; - } - -if (ifa->ifa_addr->sa_family ==AF_INET ) { - *has_ipv4 = true; - } -if (ifa->ifa_addr->sa_family ==AF_INET6 ) { - *has_ipv6 = true; +if (bind (fd, sa, salen) <0 ) { +close (fd); +if (errno ==EADDRNOTAVAIL ) { + *has_proto = false; +return 0 ; } +return -1 ; } -freeifaddrs (ifaddr); - - hints.ai_flags =AI_PASSIVE |AI_ADDRCONFIG ; - hints.ai_family =AF_INET6 ; - hints.ai_socktype =SOCK_STREAM ; - - gaierr =getaddrinfo ("::1" ,NULL , &hints, &ai); -if (gaierr !=0 ) { -if (gaierr ==EAI_ADDRFAMILY || - gaierr ==EAI_FAMILY || - gaierr ==EAI_NONAME ) { - *has_ipv6 = false; - }else { -g_printerr ("Failed to resolve ::1 address: %s\n" , -gai_strerror (gaierr)); -return -1 ; - } - } +close (fd); + *has_proto = true; +return 0 ; +} -freeaddrinfo (ai); +static int check_protocol_support (bool *has_ipv4,bool *has_ipv6) +{ +struct sockaddr_in sin = { + .sin_family =AF_INET , + .sin_addr = { .s_addr =htonl (INADDR_LOOPBACK ) }, + }; +struct sockaddr_in6 sin6 = { + .sin6_family =AF_INET6 , + .sin6_addr =IN6ADDR_LOOPBACK_INIT , + }; -return 0 ; -#else - *has_ipv4 = *has_ipv6 = false; +if (check_bind ((struct sockaddr *)&sin,sizeof (sin), has_ipv4) <0 ) { +return -1 ; + } +if (check_bind ((struct sockaddr *)&sin6,sizeof (sin6), has_ipv6) <0 ) { +return -1 ; + } -return -1 ; -#endif +return 0 ; }
Signature · 1 line
2.5.0
PATCH v2 3/18 io: initialize sockets in test program
In reply to Daniel P. BerrangeThe win32 sockets layer requires that socket_init() is called otherwise nothing will work.
--- tests/test-io-channel-socket.c | 1 + 1 file changed, 1 insertion(+)
tests/test-io-channel-socket.c +1 -0
@@ -489,6 +489,7 @@ int main(int argc, char **argv)bool has_ipv4, has_ipv6;module_call_init (MODULE_INIT_QOM ); +socket_init ();g_test_init (&argc, &argv,NULL );
Signature · 1 line
2.5.0
PATCH v2 4/18 io: bind to socket before creating QIOChannelSocket
In reply to Daniel P. BerrangeIn the QIOChannelSocket test we create a socket file descriptor and then try to create a QIOChannelSocket. This works on Linux, but fails on Win32 because it is not valid to call getsockname() on an unbound socket.
--- tests/test-io-channel-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tests/test-io-channel-socket.c +10 -0
@@ -470,10 +470,20 @@ static void test_io_channel_ipv4_fd(void) {QIOChannel *ioc;int fd =-1 ; +struct sockaddr_in sa = { + .sin_family =AF_INET , + .sin_addr = { + .s_addr =htonl (INADDR_LOOPBACK ), + } +/* Leave port unset for auto-assign */ + }; +socklen_t salen =sizeof (sa); fd =socket (AF_INET ,SOCK_STREAM ,0 ); g_assert_cmpint(fd, >, -1); + g_assert_cmpint(bind(fd, (struct sockaddr *)&sa, salen), ==, 0); + ioc =qio_channel_new_fd (fd, &error_abort); g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
Signature · 1 line
2.5.0
PATCH v2 5/18 io: wait for incoming client in socket test
In reply to Daniel P. BerrangeExercise the GSource code for server sockets by calling qio_channel_wait() prior to accepting the incoming client.
--- tests/test-io-channel-socket.c | 2 ++ 1 file changed, 2 insertions(+)
tests/test-io-channel-socket.c +2 -0
@@ -114,6 +114,7 @@ static void test_io_channel_setup_sync(SocketAddress *listen_addr, QIO_CHANNEL_SOCKET(*src), connect_addr, &error_abort);qio_channel_set_delay (*src, false); +qio_channel_wait (QIO_CHANNEL (lioc),G_IO_IN ); *dst =QIO_CHANNEL (qio_channel_socket_accept (lioc, &error_abort));g_assert (*dst);
@@ -181,6 +182,7 @@ static void test_io_channel_setup_async(SocketAddress *listen_addr,g_assert (!data.err); +qio_channel_wait (QIO_CHANNEL (lioc),G_IO_IN ); *dst =QIO_CHANNEL (qio_channel_socket_accept (lioc, &error_abort));g_assert (*dst);
Signature · 1 line
2.5.0
PATCH v2 6/18 io: set correct error object in background reader test thread
In reply to Daniel P. BerrangeThe reader thread was accidentally setting the error pointer intended for the writer thread. If both threads set errors this would result in QEMU abort'ing due to the error already being set.
--- tests/io-channel-helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/io-channel-helpers.c +1 -1
@@ -132,7 +132,7 @@ static gpointer test_io_thread_reader(gpointer opaque)if (ret ==QIO_CHANNEL_ERR_BLOCK ) {if (data->blocking) { -error_setg (&data->writeerr, +error_setg (&data->readerr,"Unexpected I/O blocking" );break ; }else {
Signature · 1 line
2.5.0
PATCH v2 7/18 io: assert errors before asserting content in I/O test
In reply to Daniel P. BerrangeWhen checking the results of an I/O operation test, assert that the error objects are NULL before asserting on the content. This is found to give more useful indication of the problem when diagnosing test failures.
--- tests/io-channel-helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/io-channel-helpers.c +2 -2
@@ -233,11 +233,11 @@ void qio_channel_test_run_reader(QIOChannelTest *test,void qio_channel_test_validate (QIOChannelTest *test) { +g_assert (test->readerr ==NULL ); +g_assert (test->writeerr ==NULL );g_assert_cmpint (memcmp (test->input, test->output, test->len), ==, 0); -g_assert (test->readerr ==NULL ); -g_assert (test->writeerr ==NULL );g_free (test->inputv);g_free (test->outputv);
Signature · 1 line
2.5.0
PATCH v2 8/18 io: fix copy+paste mistake in socket error message
In reply to Daniel P. Berranges/write/read/ in the error message reported afterreadmsg() fails
--- io/channel-socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io/channel-socket.c +1 -1
@@ -569,7 +569,7 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, goto retry; }else {error_setg_errno (errp,socket_error (), -"Unable to write to socket" ); +"Unable to read from socket" );return -1 ; } }
Signature · 1 line
2.5.0
PATCH v2 9/18 io: pass HANDLE to g_source_add_poll on Win32
In reply to Daniel P. BerrangeFrom: Paolo Bonzini <[email protected]>
--- io/channel-watch.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
io/channel-watch.c +11 -2
@@ -160,7 +160,11 @@ GSource *qio_channel_create_fd_watch(QIOChannel *ioc, ssource->condition = condition; +#ifdef CONFIG_WIN32 + ssource->fd.fd = (gint64 )_get_osfhandle (fd); +#else ssource->fd.fd = fd; +#endif ssource->fd.events = condition;g_source_add_poll (source, &ssource->fd);
@@ -186,10 +190,15 @@ GSource *qio_channel_create_fd_pair_watch(QIOChannel *ioc, ssource->condition = condition; +#ifdef CONFIG_WIN32 + ssource->fdread.fd = (gint64 )_get_osfhandle (fdread); + ssource->fdwrite.fd = (gint64 )_get_osfhandle (fdwrite); +#else ssource->fdread.fd = fdread; - ssource->fdread.events = condition &G_IO_IN ; - ssource->fdwrite.fd = fdwrite; +#endif + + ssource->fdread.events = condition &G_IO_IN ; ssource->fdwrite.events = condition &G_IO_OUT ;g_source_add_poll (source, &ssource->fdread);
Signature · 1 line
2.5.0
PATCH v2 10/18 io: introduce qio_channel_create_socket_watch
In reply to Daniel P. BerrangeFrom: Paolo Bonzini <[email protected]>
Sockets are not in the same namespace as file descriptors on Windows. As an initial step, introduce separate APIs for file descriptor and socket watches.
--- include/io/channel-watch.h | 20 +++++++++++++++++++- io/channel-socket.c | 6 +++--- io/channel-watch.c | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-)
include/io/channel-watch.h +19 -1
@@ -39,7 +39,7 @@* monitor the file descriptor @fd for the * I/O conditions in @condition. This is able * monitor block devices, character devices, -* sockets, pipes but not plain files. +* pipes but not plain files or, on Win32, sockets. * * Returns: the new main loop source */
@@ -48,6 +48,24 @@ GSource *qio_channel_create_fd_watch(QIOChannel *ioc, GIOCondition condition);/** +* qio_channel_create_socket_watch: +* @ioc: the channel object +* @fd: the file descriptor +* @condition: the I/O condition +* +* Create a new main loop source that is able to +* monitor the file descriptor @fd for the +* I/O conditions in @condition. This is equivalent +* to qio_channel_create_fd_watch on POSIX systems +* but not on Windows. +* +* Returns: the new main loop source +*/ +GSource *qio_channel_create_socket_watch (QIOChannel *ioc, +int fd, +GIOCondition condition); + +/** * qio_channel_create_fd_pair_watch: * @ioc: the channel object * @fdread: the file descriptor for reading
io/channel-socket.c +3 -3
@@ -714,9 +714,9 @@ static GSource *qio_channel_socket_create_watch(QIOChannel *ioc, GIOCondition condition) {QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); -return qio_channel_create_fd_watch (ioc, - sioc->fd, - condition); +return qio_channel_create_socket_watch (ioc, + sioc->fd, + condition); } static void qio_channel_socket_class_init(ObjectClass *klass,
io/channel-watch.c +15 -0
@@ -172,6 +172,21 @@ GSource *qio_channel_create_fd_watch(QIOChannel *ioc,return source; } +#ifdef CONFIG_WIN32 +GSource *qio_channel_create_socket_watch (QIOChannel *ioc, +int socket, +GIOCondition condition) +{ +abort (); +} +#else +GSource *qio_channel_create_socket_watch (QIOChannel *ioc, +int socket, +GIOCondition condition) +{ +return qio_channel_create_fd_watch (ioc, socket, condition); +} +#endif GSource *qio_channel_create_fd_pair_watch(QIOChannel *ioc, int fdread,
Signature · 1 line
2.5.0
PATCH v2 11/18 io: use qemu_accept to ensure SOCK_CLOEXEC is set
In reply to Daniel P. BerrangeThe QIOChannelSocket code mistakenly uses the bare accept() function which does not set SOCK_CLOEXEC.
--- io/channel-socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
io/channel-socket.c +2 -2
@@ -343,8 +343,8 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, retry:trace_qio_channel_socket_accept (ioc); - cioc->fd =accept (ioc->fd, (struct sockaddr *)&cioc->remoteAddr, - &cioc->remoteAddrLen); + cioc->fd =qemu_accept (ioc->fd, (struct sockaddr *)&cioc->remoteAddr, + &cioc->remoteAddrLen);if (cioc->fd <0 ) {trace_qio_channel_socket_accept_fail (ioc);if (socket_error () ==EINTR ) {
Signature · 1 line
2.5.0
PATCH v2 12/18 io: remove checking of EWOULDBLOCK
In reply to Daniel P. BerrangeSince we now canonicalize WSAEWOULDBLOCK into EAGAIN there is no longer any need to explicitly check EWOULDBLOCK for Win32.
--- io/channel-command.c | 6 ++---- io/channel-file.c | 6 ++---- io/channel-socket.c | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-)
io/channel-command.c +2 -4
@@ -236,8 +236,7 @@ static ssize_t qio_channel_command_readv(QIOChannel *ioc, retry: ret =readv (cioc->readfd, iov, niov);if (ret <0 ) { -if (errno ==EAGAIN || - errno ==EWOULDBLOCK ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (errno ==EINTR ) {
@@ -265,8 +264,7 @@ static ssize_t qio_channel_command_writev(QIOChannel *ioc, retry: ret =writev (cioc->writefd, iov, niov);if (ret <=0 ) { -if (errno ==EAGAIN || - errno ==EWOULDBLOCK ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (errno ==EINTR ) {
io/channel-file.c +2 -4
@@ -96,8 +96,7 @@ static ssize_t qio_channel_file_readv(QIOChannel *ioc, retry: ret =readv (fioc->fd, iov, niov);if (ret <0 ) { -if (errno ==EAGAIN || - errno ==EWOULDBLOCK ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (errno ==EINTR ) {
@@ -125,8 +124,7 @@ static ssize_t qio_channel_file_writev(QIOChannel *ioc, retry: ret =writev (fioc->fd, iov, niov);if (ret <=0 ) { -if (errno ==EAGAIN || - errno ==EWOULDBLOCK ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (errno ==EINTR ) {
io/channel-socket.c +2 -4
@@ -466,8 +466,7 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, retry: ret =recvmsg (sioc->fd, &msg, sflags);if (ret <0 ) { -if (socket_error () ==EAGAIN || -socket_error () ==EWOULDBLOCK ) { +if (socket_error () ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (socket_error () ==EINTR ) {
@@ -526,8 +525,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, retry: ret =sendmsg (sioc->fd, &msg,0 );if (ret <=0 ) { -if (socket_error () ==EAGAIN || -socket_error () ==EWOULDBLOCK ) { +if (socket_error () ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; }if (socket_error () ==EINTR ) {
Signature · 1 line
2.5.0
PATCH v2 13/18 io: implement socket watch for win32 using WSAEventSelect+select
In reply to Daniel P. BerrangeFrom: Paolo Bonzini <[email protected]>
On Win32 we cannot directly poll on socket handles. Instead we create a Win32 event object and associate the socket handle with the event. When the event signals readyness we then have to use select to determine which events are ready. Creating Win32 events is moderately heavyweight, so we don't want todo it every time we create a GSource, so this associates a single event with a QIOChannel.
--- include/io/channel.h | 3 ++ io/channel-socket.c | 34 +++++++++++--- io/channel-watch.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++- io/channel.c | 14 ++++++ 4 files changed, 170 insertions(+), 7 deletions(-)
include/io/channel.h +3 -0
@@ -78,6 +78,9 @@ typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,struct QIOChannel {Object parent;unsigned int features;/* bitmask of QIOChannelFeatures */ +#ifdef _WIN32 +HANDLE event;/* For use with GSource on Win32 */ +#endif };/**
io/channel-socket.c +28 -6
@@ -55,6 +55,10 @@ qio_channel_socket_new(void) ioc =QIO_CHANNEL (sioc); ioc->features |= (1 <<QIO_CHANNEL_FEATURE_SHUTDOWN ); +#ifdef WIN32 + ioc->event =CreateEvent (NULL , FALSE, FALSE,NULL ); +#endif +trace_qio_channel_socket_new (sioc);return sioc;
@@ -341,6 +345,11 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, cioc->remoteAddrLen =sizeof (ioc->remoteAddr); cioc->localAddrLen =sizeof (ioc->localAddr); +#ifdef WIN32 +QIO_CHANNEL (cioc)->event =CreateEvent (NULL , FALSE, FALSE,NULL ); +#endif + + retry:trace_qio_channel_socket_accept (ioc); cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
@@ -384,7 +393,10 @@ static void qio_channel_socket_finalize(Object *obj) {QIOChannelSocket *ioc =QIO_CHANNEL_SOCKET (obj);if (ioc->fd !=-1 ) { -close (ioc->fd); +#ifdef WIN32 +WSAEventSelect (ioc->fd,NULL ,0 ); +#endif +closesocket (ioc->fd); ioc->fd =-1 ; } }
@@ -634,6 +646,11 @@ qio_channel_socket_set_blocking(QIOChannel *ioc,qemu_set_block (sioc->fd); }else {qemu_set_nonblock (sioc->fd); +#ifdef WIN32 +WSAEventSelect (sioc->fd, ioc->event, +FD_READ |FD_ACCEPT |FD_CLOSE | +FD_CONNECT |FD_WRITE |FD_OOB ); +#endif }return 0 ; }
@@ -669,13 +686,18 @@ qio_channel_socket_close(QIOChannel *ioc, {QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); -if (closesocket (sioc->fd) <0 ) { +if (sioc->fd !=-1 ) { +#ifdef WIN32 +WSAEventSelect (sioc->fd,NULL ,0 ); +#endif +if (closesocket (sioc->fd) <0 ) { + sioc->fd =-1 ; +error_setg_errno (errp,socket_error (), +"Unable to close socket" ); +return -1 ; + } sioc->fd =-1 ; -error_setg_errno (errp,socket_error (), -"Unable to close socket" ); -return -1 ; } - sioc->fd =-1 ;return 0 ; }
io/channel-watch.c +125 -1
144 lines not on this page · raw message
io/channel.c +14 -0
@@ -274,10 +274,24 @@ void qio_channel_wait(QIOChannel *ioc, } +#ifdef _WIN32 +static void qio_channel_finalize (Object *obj) +{ +QIOChannel *ioc =QIO_CHANNEL (obj); + +if (ioc->event) { +CloseHandle (ioc->event); + } +} +#endif +static const TypeInfo qio_channel_info = { .parent =TYPE_OBJECT , .name =TYPE_QIO_CHANNEL , .instance_size =sizeof (QIOChannel), +#ifdef _WIN32 + .instance_finalize = qio_channel_finalize, +#endif .abstract = true, .class_size =sizeof (QIOChannelClass), };
Signature · 1 line
2.5.0
PATCH v2 14/18 char: remove qemu_chr_finish_socket_connection method
In reply to Daniel P. BerrangeThe qemu_chr_finish_socket_connection method is multiplexing two different actions into one method. Each caller of it though, only wants one specific action. The code is shorter & clearer if we thus remove the method and just inline the specific actions where needed.
--- qemu-char.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
qemu-char.c +7 -17
@@ -3091,20 +3091,6 @@ static void tcp_chr_close(CharDriverState *chr)qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); } -static void qemu_chr_finish_socket_connection (CharDriverState *chr, -QIOChannelSocket *sioc) -{ -TCPCharDriver *s = chr->opaque; - -if (s->is_listen) { - s->listen_ioc = sioc; - s->listen_tag =qio_channel_add_watch ( -QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); - }else { -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); - } -}static void qemu_chr_socket_connected (Object *src,Error *err,void *opaque) {
@@ -3119,7 +3105,8 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) } s->connect_err_reported = false; -qemu_chr_finish_socket_connection (chr, sioc); +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); }static bool qemu_chr_open_socket_fd (CharDriverState *chr,Error **errp)
@@ -3131,7 +3118,9 @@ static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)if (qio_channel_socket_listen_sync (sioc, s->addr, errp) <0 ) { goto fail; } -qemu_chr_finish_socket_connection (chr, sioc); + s->listen_ioc = sioc; + s->listen_tag =qio_channel_add_watch ( +QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); }else if (s->reconnect_time) { qio_channel_socket_connect_async(sioc, s->addr, qemu_chr_socket_connected,
@@ -3140,7 +3129,8 @@ static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { goto fail; } -qemu_chr_finish_socket_connection (chr, sioc); +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); }return true;
Signature · 1 line
2.5.0
PATCH v2 15/18 char: remove socket_try_connect method
In reply to Daniel P. BerrangeThe qemu_chr_open_socket_fd() method multiplexes three different actions into one method. The socket_try_connect() method is one of its callers, but it only ever want one specific action performed. By inlining that action into socket_try_connect() we see that there is not in fact any failure scenario, so there is not even any reason for socket_try_connect to exist. Just inline the asynchronous connection attempts directly at the places that need them. This shortens & clarifies the code.
--- qemu-char.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-)
qemu-char.c +10 -15
@@ -3121,10 +3121,6 @@ static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp) s->listen_ioc = sioc; s->listen_tag =qio_channel_add_watch (QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); - }else if (s->reconnect_time) { -qio_channel_socket_connect_async (sioc, s->addr, - qemu_chr_socket_connected, - chr,NULL ); }else {if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { goto fail;
@@ -4248,19 +4244,11 @@ static CharDriverState *qmp_chardev_open_parallel(const char *id,#endif /* WIN32 */ -static void socket_try_connect (CharDriverState *chr) -{ -Error *err =NULL ; - -if (!qemu_chr_open_socket_fd (chr, &err)) { -check_report_connect_error (chr, err); - } -} -static gboolean socket_reconnect_timeout (gpointer opaque) {CharDriverState *chr = opaque;TCPCharDriver *s = chr->opaque; +QIOChannelSocket *sioc; s->reconnect_timer =0 ;
@@ -4268,7 +4256,10 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } -socket_try_connect (chr); + sioc =qio_channel_socket_new (); +qio_channel_socket_connect_async (sioc, s->addr, + qemu_chr_socket_connected, + chr,NULL );return false; }
@@ -4288,6 +4279,7 @@ static CharDriverState *qmp_chardev_open_socket(const char *id,bool is_waitconnect = sock->has_wait ? sock->wait : false;int64_t reconnect = sock->has_reconnect ? sock->reconnect :0 ;ChardevCommon *common =qapi_ChardevSocket_base (sock); +QIOChannelSocket *sioc =NULL ; chr =qemu_chr_alloc (common, errp);if (!chr) {
@@ -4358,7 +4350,10 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, }if (s->reconnect_time) { -socket_try_connect (chr); + sioc =qio_channel_socket_new (); +qio_channel_socket_connect_async (sioc, s->addr, + qemu_chr_socket_connected, + chr,NULL ); }else if (!qemu_chr_open_socket_fd (chr, errp)) { goto error; }
Signature · 1 line
2.5.0
PATCH v2 16/18 char: remove qemu_chr_open_socket_fd method
In reply to Daniel P. BerrangeThe qemu_chr_open_socket_fd method takes care of either doing a synchronous socket connect, or creating a listener socket. Part of the work when creating the listener socket is to register a watch for incoming clients. The caller of qemu_chr_open_socket_fd may not want this watch created, as it might be doing a synchronous wait for the first client. Rather than passing yet more parameters into qemu_chr_open_socket_fd to let it handle this, just remove the qemu_chr_open_socket_fd method an inline its functionality into the caller. This allows for a clearer control flow and shorter code.
--- qemu-char.c | 59 ++++++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-)
qemu-char.c +24 -35
@@ -3109,32 +3109,6 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque)object_unref (OBJECT (sioc)); } -static bool qemu_chr_open_socket_fd (CharDriverState *chr,Error **errp) -{ -TCPCharDriver *s = chr->opaque; -QIOChannelSocket *sioc =qio_channel_socket_new (); - -if (s->is_listen) { -if (qio_channel_socket_listen_sync (sioc, s->addr, errp) <0 ) { - goto fail; - } - s->listen_ioc = sioc; - s->listen_tag =qio_channel_add_watch ( -QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); - }else { -if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { - goto fail; - } -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); - } - -return true; - - fail: -object_unref (OBJECT (sioc)); -return false; -}/*********************************************************/ /* Ring buffer chardev */
@@ -4349,25 +4323,40 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, s->reconnect_time = reconnect; } + sioc =qio_channel_socket_new ();if (s->reconnect_time) { - sioc =qio_channel_socket_new ();qio_channel_socket_connect_async (sioc, s->addr, qemu_chr_socket_connected, chr,NULL ); - }else if (!qemu_chr_open_socket_fd (chr, errp)) { - goto error; - } - -if (is_listen && is_waitconnect) { -fprintf (stderr,"QEMU waiting for connection on: %s\n" , - chr->filename); -tcp_chr_accept (QIO_CHANNEL (s->listen_ioc),G_IO_IN , chr); + }else if (s->is_listen) { +if (qio_channel_socket_listen_sync (sioc, s->addr, errp) <0 ) { + goto error; + } + s->listen_ioc = sioc; +if (is_waitconnect) { +fprintf (stderr,"QEMU waiting for connection on: %s\n" , + chr->filename); +tcp_chr_accept (QIO_CHANNEL (s->listen_ioc),G_IO_IN , chr); + }qio_channel_set_blocking (QIO_CHANNEL (s->listen_ioc), false,NULL ); +if (!s->ioc) { + s->listen_tag =qio_channel_add_watch ( +QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); + } + }else { +if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { + goto error; + } +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); }return chr; error: +if (sioc) { +object_unref (OBJECT (sioc)); + }if (s->tls_creds) {object_unref (OBJECT (s->tls_creds)); }
Signature · 1 line
2.5.0
PATCH v2 17/18 osdep: add wrappers for socket functions
In reply to Daniel P. BerrangeThe windows socket functions look identical to the normal POSIX sockets functions, but instead of setting errno, the caller needs to call WSAGetLastError(). QEMU has tried to deal with this incompatibility by defining a socket_error() method that callers must use that abstracts the difference between WSAGetLastError() and errno.
This approach is somewhat error prone though - many callers of the sockets functions are just using errno directly because it is easy to forget the need use a QEMU specific wrapper. It is not always immediately obvious that a particular function will in fact call into Windows sockets functions, so the dev may not even realize they need to use socket_error().
This introduces an alternative approach to portability inspired by the way GNULIB fixes portability problems. We use a macro to redefine the original socket function names to refer to a QEMU wrapper function. The wrapper function calls the original Win32 sockets method and then sets errno from the WSAGetLastError() value.
Thus all code can simply call the normal POSIX sockets APIs are have standard errno reporting on error, even on Windows. This makes the socket_error() method obsolete.
We also bring closesocket & ioctlsocket into this approach. Even though they are non-standard Win32 names, we can't wrap the normal close/ioctl methods since there's no reliable way to distinguish between a file descriptor and HANDLE in Win32.
--- Makefile | 4 +- include/qemu/sockets.h | 14 ---- include/sysemu/os-posix.h | 9 +++ include/sysemu/os-win32.h | 79 ++++++++++++++++++ linux-user/flatload.c | 1 - slirp/slirp.h | 2 - util/oslib-win32.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 291 insertions(+), 19 deletions(-)
Makefile +2 -2
@@ -238,7 +238,7 @@ qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-o qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a -qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o +qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o libqemuutil.a libqemustub.a fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-marshal.o fsdev/9p-iov-marshal.o libqemuutil.a libqemustub.a fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
@@ -329,7 +329,7 @@ ifneq ($(EXESUF),) qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI) endif -ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) +ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^) ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^)
include/qemu/sockets.h +0 -14
@@ -3,23 +3,9 @@#define QEMU_SOCKET_H #ifdef _WIN32 -#include <windows.h> -#include <winsock2.h> -#include <ws2tcpip.h> int inet_aton (const char *cp,struct in_addr *ia); -#else - -#include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <sys/un.h> - -#define closesocket (s) close(s) -#endif /* !_WIN32 */ #include "qapi-types.h"
include/sysemu/os-posix.h +9 -0
@@ -26,6 +26,12 @@#ifndef QEMU_OS_POSIX_H #define QEMU_OS_POSIX_H +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <sys/un.h> void os_set_line_buffering (void );void os_set_proc_name (const char *s);
@@ -36,6 +42,9 @@ int os_mlock(void);#define socket_error () errno +#define closesocket (s) close(s) +#define ioctlsocket (s, r, v) ioctl(s, r, v) +typedef struct timeval qemu_timeval ;#define qemu_gettimeofday (tp) gettimeofday(tp, NULL)
include/sysemu/os-win32.h +79 -0
89 lines not on this page · raw message
linux-user/flatload.c +0 -1
@@ -38,7 +38,6 @@#include "qemu.h" #include "flat.h" -#define ntohl (x) be32_to_cpu(x)#include <target_flat.h> //#define DEBUG
slirp/slirp.h +0 -2
@@ -14,8 +14,6 @@ typedef char *caddr_t;# include <iphlpapi.h> #else -# define ioctlsocket ioctl -# define closesocket (s) close(s)# if !defined(__HAIKU__)# define O_BINARY 0# endif
util/oslib-win32.c +201 -0
204 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v2 18/18 osdep: remove use of socket_error() from all code
In reply to Daniel P. BerrangeNow that QEMU wraps the Win32 sockets methods to automatically set errno upon failure, there is no reason for callers to use the socket_error() method. They can rely on accessing errno even on Win32. Remove all use of socket_error() from general code, leaving it as a static method in oslib-win32.c only.
--- block/sheepdog.c | 5 ++--- include/sysemu/os-posix.h | 2 -- include/sysemu/os-win32.h | 2 -- io/channel-socket.c | 38 +++++++++++++++++++------------------- migration/qemu-file-unix.c | 14 ++++++-------- migration/tcp.c | 7 +++---- net/socket.c | 19 ++++++++----------- slirp/tcp_input.c | 4 ---- util/oslib-win32.c | 2 +- util/qemu-coroutine-io.c | 6 ++---- util/qemu-sockets.c | 10 +++++----- 11 files changed, 46 insertions(+), 63 deletions(-)
block/sheepdog.c +2 -3
@@ -615,14 +615,13 @@ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, ret =qemu_co_send (sockfd, hdr,sizeof (*hdr));if (ret !=sizeof (*hdr)) {error_report ("failed to send a req, %s" ,strerror (errno)); - ret = -socket_error (); -return ret; +return -errno; } ret =qemu_co_send (sockfd, data, *wlen);if (ret != *wlen) { - ret = -socket_error ();error_report ("failed to send a req, %s" ,strerror (errno)); +return -errno; }return ret;
include/sysemu/os-posix.h +0 -2
@@ -40,8 +40,6 @@ void os_daemonize(void);void os_setup_post (void );int os_mlock (void ); -#define socket_error () errno -#define closesocket (s) close(s)#define ioctlsocket (s, r, v) ioctl(s, r, v)
include/sysemu/os-win32.h +0 -2
@@ -55,8 +55,6 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);struct tm *localtime_r (const time_t *timep,struct tm *result);#endif /* CONFIG_LOCALTIME_R */ -int socket_error (void ); -static inline void os_setup_signal_handling (void ) {}static inline void os_daemonize (void ) {}static inline void os_setup_post (void ) {}
io/channel-socket.c +19 -19
@@ -82,11 +82,11 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,if (getpeername (fd, (struct sockaddr *)&sioc->remoteAddr, &sioc->remoteAddrLen) <0 ) { -if (socket_error () ==ENOTCONN ) { +if (errno ==ENOTCONN ) {memset (&sioc->remoteAddr,0 ,sizeof (sioc->remoteAddr)); sioc->remoteAddrLen =sizeof (sioc->remoteAddr); }else { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to query remote socket address" ); goto error; }
@@ -94,7 +94,7 @@ qio_channel_socket_set_fd(QIOChannelSocket *sioc,if (getsockname (fd, (struct sockaddr *)&sioc->localAddr, &sioc->localAddrLen) <0 ) { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to query local socket address" ); goto error; }
@@ -356,7 +356,7 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, &cioc->remoteAddrLen);if (cioc->fd <0 ) {trace_qio_channel_socket_accept_fail (ioc); -if (socket_error () ==EINTR ) { +if (errno ==EINTR ) { goto retry; } goto error;
@@ -364,7 +364,7 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,if (getsockname (cioc->fd, (struct sockaddr *)&cioc->localAddr, &cioc->localAddrLen) <0 ) { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to query local socket address" ); goto error; }
@@ -478,14 +478,14 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, retry: ret =recvmsg (sioc->fd, &msg, sflags);if (ret <0 ) { -if (socket_error () ==EAGAIN ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; } -if (socket_error () ==EINTR ) { +if (errno ==EINTR ) { goto retry; } -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to read from socket" );return -1 ; }
@@ -537,13 +537,13 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, retry: ret =sendmsg (sioc->fd, &msg,0 );if (ret <=0 ) { -if (socket_error () ==EAGAIN ) { +if (errno ==EAGAIN ) {return QIO_CHANNEL_ERR_BLOCK ; } -if (socket_error () ==EINTR ) { +if (errno ==EINTR ) { goto retry; } -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to write to socket" );return -1 ; }
@@ -569,16 +569,16 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, iov[i].iov_len, 0);if (ret <0 ) { -if (socket_error () ==EAGAIN ) { +if (errno ==EAGAIN ) {if (done) {return done; }else {return QIO_CHANNEL_ERR_BLOCK ; } - }else if (socket_error () ==EINTR ) { + }else if (errno ==EINTR ) { goto retry; }else { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to read from socket" );return -1 ; }
@@ -611,16 +611,16 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, iov[i].iov_len, 0);if (ret <0 ) { -if (socket_error () ==EAGAIN ) { +if (errno ==EAGAIN ) {if (done) {return done; }else {return QIO_CHANNEL_ERR_BLOCK ; } - }else if (socket_error () ==EINTR ) { + }else if (errno ==EINTR ) { goto retry; }else { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to write to socket" );return -1 ; }
@@ -692,7 +692,7 @@ qio_channel_socket_close(QIOChannel *ioc,#endif if (closesocket (sioc->fd) <0 ) { sioc->fd =-1 ; -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to close socket" );return -1 ; }
@@ -723,7 +723,7 @@ qio_channel_socket_shutdown(QIOChannel *ioc, }if (shutdown (sioc->fd, sockhow) <0 ) { -error_setg_errno (errp,socket_error (), +error_setg_errno (errp, errno,"Unable to shutdown socket" );return -1 ; }
migration/qemu-file-unix.c +6 -8
@@ -53,18 +53,16 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, }if (size >0 ) { - err =socket_error (); - -if (err !=EAGAIN && err !=EWOULDBLOCK ) { +if (errno !=EAGAIN && errno !=EWOULDBLOCK ) {error_report ("socket_writev_buffer: Got err=%d for (%zu/%zu)" , - err, (size_t )size, (size_t )len); + errno, (size_t )size, (size_t )len);/* * If I've already sent some but only just got the error, I * could return the amount validly sent so far and wait for the * next call to report the error, but I'd rather flag the error * immediately. */ -return -err; +return -errno; }/* Emulate blocking */
@@ -99,15 +97,15 @@ static ssize_t socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos,if (len !=-1 ) {break ; } -if (socket_error () ==EAGAIN ) { +if (errno ==EAGAIN ) {yield_until_fd_readable (s->fd); - }else if (socket_error () !=EINTR ) { + }else if (errno !=EINTR ) {break ; } }if (len ==-1 ) { - len = -socket_error (); + len = -errno; }return len; }
migration/tcp.c +3 -4
@@ -59,12 +59,11 @@ static void tcp_accept_incoming_migration(void *opaque)socklen_t addrlen =sizeof (addr);int s = (intptr_t )opaque;QEMUFile *f; -int c, err; +int c;do { c =qemu_accept (s, (struct sockaddr *)&addr, &addrlen); - err =socket_error (); - }while (c <0 && err ==EINTR ); + }while (c <0 && errno ==EINTR );qemu_set_fd_handler (s,NULL ,NULL ,NULL );closesocket (s);
@@ -72,7 +71,7 @@ static void tcp_accept_incoming_migration(void *opaque)if (c <0 ) {error_report ("could not accept migration connection (%s)" , -strerror (err)); +strerror (errno));return ; }
net/socket.c +8 -11
@@ -145,15 +145,14 @@ static void net_socket_send_completed(NetClientState *nc, ssize_t len)static void net_socket_send (void *opaque) {NetSocketState *s = opaque; -int size, err; +int size;unsigned l;uint8_t buf1[NET_BUFSIZE ];const uint8_t *buf; size =qemu_recv (s->fd, buf1,sizeof (buf1),0 );if (size <0 ) { - err =socket_error (); -if (err !=EWOULDBLOCK ) +if (errno !=EWOULDBLOCK ) goto eoc; }else if (size ==0 ) {/* end of connection */
@@ -566,7 +565,7 @@ static int net_socket_connect_init(NetClientState *peer, const char *host_str) {NetSocketState *s; -int fd, connected, ret, err; +int fd, connected, ret;struct sockaddr_in saddr;if (parse_host_port (&saddr, host_str) <0 )
@@ -583,14 +582,12 @@ static int net_socket_connect_init(NetClientState *peer,for (;;) { ret =connect (fd, (struct sockaddr *)&saddr,sizeof (saddr));if (ret <0 ) { - err =socket_error (); -if (err ==EINTR || err ==EWOULDBLOCK ) { - }else if (err ==EINPROGRESS ) { -break ; -#ifdef _WIN32 - }else if (err ==WSAEALREADY || err ==WSAEINVAL ) { +if (errno ==EINTR || errno ==EWOULDBLOCK ) { +/* continue */ + }else if (errno ==EINPROGRESS || + errno ==EALREADY || + errno ==EINVAL ) {break ; -#endif }else {perror ("connect" );closesocket (fd);
slirp/tcp_input.c +0 -4
@@ -586,11 +586,7 @@ findso: }if ((tcp_fconnect (so, so->so_ffamily) ==-1 ) && -#if defined(_WIN32) - socket_error() != WSAEWOULDBLOCK -#else (errno !=EINPROGRESS ) && (errno !=EWOULDBLOCK ) -#endif ) {u_char code=ICMP_UNREACH_NET ; DEBUG_MISC((dfd, " tcp fconnect errno = %d-%s\n",
util/oslib-win32.c +1 -1
@@ -145,7 +145,7 @@ int socket_set_fast_reuse(int fd) } -int socket_error (void ) +static int socket_error (void ) {switch (WSAGetLastError ()) {case 0 :
util/qemu-coroutine-io.c +2 -4
@@ -35,18 +35,16 @@ qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt, {size_t done =0 ;ssize_t ret; -int err;while (done < bytes) { ret =iov_send_recv (sockfd, iov, iov_cnt, offset + done, bytes - done, do_send);if (ret >0 ) { done += ret; }else if (ret <0 ) { - err =socket_error (); -if (err ==EAGAIN || err ==EWOULDBLOCK ) { +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) {qemu_coroutine_yield (); }else if (done ==0 ) { -return -err; +return -errno; }else {break ; }
util/qemu-sockets.c +5 -5
@@ -268,7 +268,7 @@ static void wait_for_connect(void *opaque)do { rc =qemu_getsockopt (s->fd,SOL_SOCKET ,SO_ERROR , &val, &valsize); - }while (rc ==-1 &&socket_error () ==EINTR ); + }while (rc ==-1 && errno ==EINTR );/* update rc to contain error */ if (!rc && val) {
@@ -330,7 +330,7 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,do { rc =0 ;if (connect (sock, addr->ai_addr, addr->ai_addrlen) <0 ) { - rc = -socket_error (); + rc = -errno; } }while (rc == -EINTR );
@@ -787,7 +787,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp,do { rc =0 ;if (connect (sock, (struct sockaddr *) &un,sizeof (un)) <0 ) { - rc = -socket_error (); + rc = -errno; } }while (rc == -EINTR );
@@ -1082,7 +1082,7 @@ SocketAddress *socket_local_address(int fd, Error **errp)socklen_t sslen =sizeof (ss);if (getsockname (fd, (struct sockaddr *)&ss, &sslen) <0 ) { -error_setg_errno (errp,socket_error (),"%s" , +error_setg_errno (errp, errno,"%s" ,"Unable to query local socket address" );return NULL ; }
@@ -1097,7 +1097,7 @@ SocketAddress *socket_remote_address(int fd, Error **errp)socklen_t sslen =sizeof (ss);if (getpeername (fd, (struct sockaddr *)&ss, &sslen) <0 ) { -error_setg_errno (errp,socket_error (),"%s" , +error_setg_errno (errp, errno,"%s" ,"Unable to query remote socket address" );return NULL ; }
Signature · 1 line
2.5.0
Quoted 34 lines · Daniel P. Berrange
This series started out as an attempt to fix the Win32 problems identified by Andrew Baumann
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg01343.htmlIt turned into a significantly larger cleanup of some chardev and osdep win32 portability code.
Patch 1 addresses Andrew's 2nd stated problem - handling of
getpeername()failures, by fixing errno handling on Win32.Patches 2-7 do some fixes in the test-io-channel-socket test case so that it is able to run on Win32.
Patches 8-12 are some fixes for the QIOChannel code
Patch 13 is the big one that changes QIOChannelSocket so that it uses a Win32 specific GSource implementation for creating watches. This is the key fix for Andrew's 1st stated problem.
At this point tests/test-io-channel-socket passes and
qemu-system-x86_64.exe -serial tcp:127.0.0.1:9000,server,nowait -device sga -display non
works on win32 once more.
Patches 14-16 are some cleanups to the chardev code to improve its clarity. They are not required for fixing any real problem
Patches 17-18 change the way we provide Win32 portability for sockets APIs inside QEMU. These do fix a number of bugs in the QEMU code related to mistaken use of errno instead of
socket_error(). None of these bugs appear to be critical issues.
Quoted 5 lines
Based on this, I'm proposing 1-13 for QEMU 2.6 release as they fix critical win32 bugs.
Patches 14-18 can either be included in 2.6 or 2.7 - I'm ambivalent on which, since they're cleanups / minor fixes.
Thanks, please submit all of them in a pull request for 2.6.
We can then clean up EAGAIN vs. EWOULDBLOCK and add a checkpatch rule to prevent further introduction of EWOULDBLOCK.
Paolo
Hi folks,
Quoted 11 lines
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, 10 March 2016 9:37 AMQuoted 7 lines · Daniel P. Berrange
This series started out as an attempt to fix the Win32 problems identified by Andrew Baumann
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg01343.htmlIt turned into a significantly larger cleanup of some chardev and osdep win32 portability code.
[...]
Sorry for chiming in a bit late here. I've tested these patches (the complete set, not individually), and they do appear to fix my immediate problem: socket char devices now work again. So thank you!
However, I'm now seeing a problem I don't believe we had before: very slow responses to GDB commands. From looking at a packet capture (using a localhost tcp socket between qemu and my gdb client), it seems that a couple of operations will go through just fine, and then there is a 1 second delay between my client's request and qemu's response. After fiddling with poll timeouts, it became clear that we were noticing the socket events when waking up from the poll, but the events themselves were still not waking us. It turns out that we were not calling WSAEventSelect on the accept path. At least, the following patch fixed the problem for me:
qemu-char.c +1 -0
@@ -3047,6 +3047,7 @@ static gboolean tcp_chr_accept(QIOChannel *channel,return TRUE; } +qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL );tcp_chr_new_client (chr, sioc);object_unref (OBJECT (sioc));
However, I'd note that both callers of tcp_chr_new_client() make the same call to set blocking to false immediately before calling tcp_chr_new_client(). Furthermore, the doc comment for qio_channel_set_blocking() appears to suggest that non-blocking mode is the default. If that's true, maybe you don't even want to rely on the caller explicitly setting blocking to false?
Cheers,
Andrew
Quoted 11 lines · Andrew Baumann
Hi folks,
Quoted 9 lines
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, 10 March 2016 9:37 AMQuoted 5 lines · Daniel P. Berrange
This series started out as an attempt to fix the Win32 problems identified by Andrew Baumann
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg01343.html
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
It turned into a significantly larger cleanup of some chardev and osdep win32 portability code.
[...]
Sorry for chiming in a bit late here. I've tested these patches (the complete set, not individually), and they do appear to fix my immediate problem: socket char devices now work again. So thank you!
Thanks for confirming this, these patches have now merged into git msater.
Quoted 25 lines
However, I'm now seeing a problem I don't believe we had before:
very slow responses to GDB commands. From looking at a packet capture (using a localhost tcp socket between qemu and my gdb client), it seems that a couple of operations will go through just fine, and then there is a 1 second delay between my client's request and qemu's response. After fiddling with poll timeouts, it became clear that we were noticing the socket events when waking up from the poll, but the events themselves were still not waking us. It turns out that we were not calling WSAEventSelect on the accept path. At least, the following patch fixed the problem for me:qemu-char.c +1
-0@@ -3047,6 +3047,7 @@ static gboolean tcp_chr_accept(QIOChannel *channel,return TRUE; } +qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL );tcp_chr_new_client (chr, sioc);object_unref (OBJECT (sioc));
Quoted 6 lines
However, I'd note that both callers of
tcp_chr_new_client()make the same call to set blocking to false immediately before callingtcp_chr_new_client(). Furthermore, the doc comment forqio_channel_set_blocking()appears to suggest that non-blocking mode is the default. If that's true, maybe you don't even want to rely on the caller explicitly setting blocking to false?
No, the docs don't intend to suggest that - the default is in fact blocking mode, so its correct to place it into nonblocking mode explicitly.
I think I didn't notice the problem you describe because my original patch series had us call WSAEventSelect when creating the watch. This indirectly puts Win32 sockets into non-blocking mode. The patches which just merged however no longer call WSAEventSelect when creating the watch, instead requiring the caller to explicitly set the socket into non-blocking mode. So I think your suggested addition here is probably the right way to address this. I'll investigate and respond with a followup patch as needed.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|