From patchwork Mon Jan 23 14:21:43 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 137391 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DCA30B6FA1 for ; Tue, 24 Jan 2012 02:56:12 +1100 (EST) Received: from localhost ([::1]:43253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RpKoa-0004sG-JF for incoming@patchwork.ozlabs.org; Mon, 23 Jan 2012 09:24:08 -0500 Received: from eggs.gnu.org ([140.186.70.92]:54517) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RpKnn-00035f-84 for qemu-devel@nongnu.org; Mon, 23 Jan 2012 09:23:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RpKng-0007te-Fh for qemu-devel@nongnu.org; Mon, 23 Jan 2012 09:23:19 -0500 Received: from mout.perfora.net ([74.208.4.194]:58808) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RpKng-0007sD-1t for qemu-devel@nongnu.org; Mon, 23 Jan 2012 09:23:12 -0500 Received: from illuin.morrigu.org (adsl-99-62-40-129.dsl.aus2tx.sbcglobal.net [99.62.40.129]) by mrelay.perfora.net (node=mrus3) with ESMTP (Nemesis) id 0Mb8Z1-1S4vs43EwV-00KhD3; Mon, 23 Jan 2012 09:22:51 -0500 From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 23 Jan 2012 08:21:43 -0600 Message-Id: <1327328505-31106-6-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1327328505-31106-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1327328505-31106-1-git-send-email-mdroth@linux.vnet.ibm.com> X-Provags-ID: V02:K0:AoujPBC53whJIZ67V28Z617aWn7g6lhILpohytMs4b1 X5iWYr2fThH7Q3zZmbqhX0bhsoJSA76qk1Je/pkjmijprss/8m msO2AQtLgv3zBDE8GY4suFvtqb7QIai7HFxYASO9q6D9N4DPC1 uq1b7l0mhUeQfQBKSE9zr+FEF/WwKr05yVBXgNytfi/E7W3cDO NDvT7RFW9zOQNBhs/SXHZb9ivSc4IuhYbk5c7QJVDGGybnzhLA XqAOutloLGmIzmQE7BQbi1e6uwaI8mJJWQmLrosuhPZaSnBSxw +IByZJGv6ygbtNYZc+/Vf+IHjaZaPTSrBfnQ9f5hBI7TPFZjY4 j8/ggQZ1hALTuj8sku2BK2mYk0MxuEW/wPYn9Nw4E9yHK/dSDq mK2TSoxnnYH8RBZ7fSt4tCq6NnJ6NuxmJI= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.208.4.194 Cc: ghammer@redhat.com, aliguori@us.ibm.com Subject: [Qemu-devel] [PATCH 5/7] qemu-ga: add initial win32 support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This adds a win32 channel implementation that makes qemu-ga functional on Windows using virtio-serial (unix-listen/isa-serial not currently implemented). Unlike with the posix implementation, we do not use GIOChannel for the following reasons: - glib calls stat() on an fd to check whether S_IFCHR is set, which is the case for virtio-serial on win32. Because of that, a one-time check to determine whether the channel is readable is done by making a call to PeekConsoleInput(), which reports the underlying handle is not a valid console handle, and thus we can never read from the channel. - if one goes as far as to "trick" glib into thinking it is a normal file descripter, the buffering is done in such a way that data written to the output stream will subsequently result in that same data being read back as if it were input, causing an error loop. furthermore, a forced flush of the channel only moves the data into a secondary buffer managed by glib, so there's no way to prevent output from getting read back as input. The implementation here ties into the glib main loop by implementing a custom GSource that continually submits asynchronous/overlapped I/O to fill an GAChannel-managed read buffer, and tells glib to poll the corresponding event handle for a completion whenever there is no data/RPC in the read buffer to notify the main application about. Signed-off-by: Michael Roth --- Makefile.objs | 2 +- qemu-ga.c | 4 + qga/channel-win32.c | 337 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 342 insertions(+), 1 deletions(-) create mode 100644 qga/channel-win32.c diff --git a/Makefile.objs b/Makefile.objs index d14218b..93a4fb9 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -418,7 +418,7 @@ common-obj-y += qmp.o hmp.o qga-nested-y = commands.o guest-agent-command-state.o qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o -qga-nested-$(CONFIG_WIN32) += commands-win32.o +qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o qga-obj-y = $(addprefix qga/, $(qga-nested-y)) qga-obj-y += qemu-ga.o module.o qga-obj-$(CONFIG_WIN32) += oslib-win32.o diff --git a/qemu-ga.c b/qemu-ga.c index c6e9dd6..100a61c 100644 --- a/qemu-ga.c +++ b/qemu-ga.c @@ -31,7 +31,11 @@ #include "qga-qapi-types.h" #include "qga/channel.h" +#ifndef _WIN32 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" +#else +#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0" +#endif #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" struct GAState { diff --git a/qga/channel-win32.c b/qga/channel-win32.c new file mode 100644 index 0000000..0a16e02 --- /dev/null +++ b/qga/channel-win32.c @@ -0,0 +1,337 @@ +#include +#include +#include +#include +#include +#include +#include +#include "qga/guest-agent-core.h" +#include "qga/channel.h" + +typedef struct GAChannelReadState { + guint thread_id; + uint8_t *buf; + size_t buf_size; + size_t cur; /* current buffer start */ + size_t pending; /* pending buffered bytes to read */ + OVERLAPPED ov; + bool ov_pending; /* whether on async read is outstanding */ +} GAChannelReadState; + +struct GAChannel { + HANDLE handle; + GAChannelCallback cb; + gpointer user_data; + GAChannelReadState rstate; + GIOCondition pending_events; /* TODO: use GAWatch.pollfd.revents */ + GSource *source; +}; + +typedef struct GAWatch { + GSource source; + GPollFD pollfd; + GAChannel *channel; + GIOCondition events_mask; +} GAWatch; + +/* + * Called by glib prior to polling to set up poll events if polling is needed. + * + */ +static gboolean ga_channel_prepare(GSource *source, gint *timeout_ms) +{ + GAWatch *watch = (GAWatch *)source; + GAChannel *c = (GAChannel *)watch->channel; + GAChannelReadState *rs = &c->rstate; + DWORD count_read, count_to_read = 0; + bool success; + GIOCondition new_events = 0; + + g_debug("prepare"); + /* go ahead and submit another read if there's room in the buffer + * and no previous reads are outstanding + */ + if (!rs->ov_pending) { + if (rs->cur + rs->pending >= rs->buf_size) { + if (rs->cur) { + memmove(rs->buf, rs->buf + rs->cur, rs->pending); + rs->cur = 0; + } + } + count_to_read = rs->buf_size - rs->cur - rs->pending; + } + + if (rs->ov_pending || count_to_read <= 0) { + goto out; + } + + /* submit the read */ + success = ReadFile(c->handle, rs->buf + rs->cur + rs->pending, + count_to_read, &count_read, &rs->ov); + if (success) { + rs->pending += count_read; + rs->ov_pending = false; + } else { + if (GetLastError() == ERROR_IO_PENDING) { + rs->ov_pending = true; + } else { + new_events |= G_IO_ERR; + } + } + +out: + /* dont block forever, iterate the main loop every one and a while */ + *timeout_ms = 500; + /* if there's data in the read buffer, or another event is pending, + * skip polling and issue user cb. + */ + if (rs->pending) { + new_events |= G_IO_IN; + } + c->pending_events |= new_events; + return !!c->pending_events; +} + +/* + * Called by glib after an outstanding read request is completed. + */ +static gboolean ga_channel_check(GSource *source) +{ + GAWatch *watch = (GAWatch *)source; + GAChannel *c = (GAChannel *)watch->channel; + GAChannelReadState *rs = &c->rstate; + DWORD count_read, error; + BOOL success; + + GIOCondition new_events = 0; + + g_debug("check"); + + /* failing this implies we issued a read that completed immediately, + * yet no data was placed into the buffer (and thus we did not skip + * polling). but since EOF is not obtainable until we retrieve an + * overlapped result, it must be the case that there was data placed + * into the buffer, or an error was generated by Readfile(). in either + * case, we should've skipped the polling for this round. + */ + g_assert(rs->ov_pending); + + success = GetOverlappedResult(c->handle, &rs->ov, &count_read, FALSE); + if (success) { + g_debug("thread: overlapped result, count_read: %d", (int)count_read); + rs->pending += count_read; + new_events |= G_IO_IN; + } else { + error = GetLastError(); + if (error == 0 || error == ERROR_HANDLE_EOF || + error == ERROR_NO_SYSTEM_RESOURCES) { + /* note: On WinXP SP3 with virtio-win 0.1-15 vioser drivers, + * ENSR seems to be synonymous with when we'd normally expect + * ERROR_HANDLE_EOF. So treat it as such. Microsoft's + * recommendation for ERROR_NO_SYSTEM_RESOURCES is to + * retry the read, so this happens to work out anyway. + */ + new_events |= G_IO_HUP; + } else if (error != ERROR_IO_INCOMPLETE) { + g_critical("error retrieving overlapped result: %d", (int)error); + new_events |= G_IO_ERR; + } + } + + if (new_events) { + rs->ov_pending = 0; + } + c->pending_events |= new_events; + + return !!c->pending_events; +} + +/* + * Called by glib after either prepare or check routines signal readiness + */ +static gboolean ga_channel_dispatch(GSource *source, GSourceFunc unused, + gpointer user_data) +{ + GAWatch *watch = (GAWatch *)source; + GAChannel *c = (GAChannel *)watch->channel; + GAChannelReadState *rs = &c->rstate; + gboolean success; + + g_debug("dispatch"); + success = c->cb(watch->pollfd.revents, c->user_data); + + if (c->pending_events & G_IO_ERR) { + g_critical("channel error, removing source"); + return false; + } + + /* TODO: replace rs->pending with watch->revents */ + c->pending_events &= ~G_IO_HUP; + if (!rs->pending) { + c->pending_events &= ~G_IO_IN; + } else { + c->pending_events = 0; + } + return success; +} + +static void ga_channel_finalize(GSource *source) +{ + g_debug("finalize"); +} + +GSourceFuncs ga_channel_watch_funcs = { + ga_channel_prepare, + ga_channel_check, + ga_channel_dispatch, + ga_channel_finalize +}; + +static GSource *ga_channel_create_watch(GAChannel *c) +{ + GSource *source = g_source_new(&ga_channel_watch_funcs, sizeof(GAWatch)); + GAWatch *watch = (GAWatch *)source; + + watch->channel = c; + watch->pollfd.fd = (gintptr) c->rstate.ov.hEvent; + g_source_add_poll(source, &watch->pollfd); + + return source; +} + +GIOStatus ga_channel_read(GAChannel *c, char *buf, size_t size, gsize *count) +{ + GAChannelReadState *rs = &c->rstate; + GIOStatus status; + size_t to_read = 0; + + if (c->pending_events & G_IO_ERR) { + return G_IO_STATUS_ERROR; + } + + *count = to_read = MIN(size, rs->pending); + if (to_read) { + memcpy(buf, rs->buf + rs->cur, to_read); + rs->cur += to_read; + rs->pending -= to_read; + status = G_IO_STATUS_NORMAL; + } else { + status = G_IO_STATUS_AGAIN; + } + + return status; +} + +static GIOStatus ga_channel_write(GAChannel *c, const char *buf, size_t size, + size_t *count) +{ + GIOStatus status; + OVERLAPPED ov = {0}; + BOOL ret; + DWORD written; + + ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + ret = WriteFile(c->handle, buf, size, &written, &ov); + if (!ret) { + if (GetLastError() == ERROR_IO_PENDING) { + /* write is pending */ + ret = GetOverlappedResult(c->handle, &ov, &written, TRUE); + if (!ret) { + if (!GetLastError()) { + status = G_IO_STATUS_AGAIN; + } else { + status = G_IO_STATUS_ERROR; + } + } else { + /* write is complete */ + status = G_IO_STATUS_NORMAL; + *count = written; + } + } else { + status = G_IO_STATUS_ERROR; + } + } else { + /* write returned immediately */ + status = G_IO_STATUS_NORMAL; + *count = written; + } + + return status; +} + +GIOStatus ga_channel_write_all(GAChannel *c, const char *buf, size_t size) +{ + GIOStatus status = G_IO_STATUS_NORMAL;; + size_t count; + + while (size) { + status = ga_channel_write(c, buf, size, &count); + if (status == G_IO_STATUS_NORMAL) { + size -= count; + buf += count; + } else if (status != G_IO_STATUS_AGAIN) { + break; + } + } + + return status; +} + +static gboolean ga_channel_open(GAChannel *c, GAChannelMethod method, + const gchar *path) +{ + if (!method == GA_CHANNEL_VIRTIO_SERIAL) { + g_critical("unsupported communication method"); + return false; + } + + c->handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, + FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL); + if (c->handle == INVALID_HANDLE_VALUE) { + g_critical("error opening path"); + return false; + } + + return true; +} + +GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path, + GAChannelCallback cb, gpointer opaque) +{ + GAChannel *c = g_malloc0(sizeof(GAChannel)); + SECURITY_ATTRIBUTES sec_attrs; + + if (!ga_channel_open(c, method, path)) { + g_critical("error opening channel"); + g_free(c); + return NULL; + } + + c->cb = cb; + c->user_data = opaque; + + sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES); + sec_attrs.lpSecurityDescriptor = NULL; + sec_attrs.bInheritHandle = false; + + c->rstate.buf_size = QGA_READ_COUNT_DEFAULT; + c->rstate.buf = g_malloc(QGA_READ_COUNT_DEFAULT); + c->rstate.ov.hEvent = CreateEvent(&sec_attrs, FALSE, FALSE, NULL); + + c->source = ga_channel_create_watch(c); + g_source_attach(c->source, NULL); + return c; +} + +void ga_channel_free(GAChannel *c) +{ + if (c->source) { + g_source_destroy(c->source); + } + if (c->rstate.ov.hEvent) { + CloseHandle(c->rstate.ov.hEvent); + } + g_free(c->rstate.buf); + g_free(c); +}