From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org
Cc: amit.shah@redhat.com, cristian.klein@cs.umu.se, quintela@redhat.com
Subject: [Qemu-devel] [PATCH 1/3] socket shutdown
Date: Thu, 8 Jan 2015 11:11:30 +0000 [thread overview]
Message-ID: <1420715492-5109-2-git-send-email-dgilbert@redhat.com> (raw)
In-Reply-To: <1420715492-5109-1-git-send-email-dgilbert@redhat.com>
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Add QEMUFile interface to allow a socket to be 'shut down' - i.e. any
reads/writes will fail (and any blocking read/write will be woken).
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
---
include/migration/qemu-file.h | 10 ++++++++++
include/qemu/sockets.h | 7 +++++++
migration/qemu-file-unix.c | 23 +++++++++++++++++++----
migration/qemu-file.c | 12 ++++++++++++
4 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index 401676b..d843c00 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -84,6 +84,14 @@ typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque,
size_t size,
int *bytes_sent);
+/*
+ * Stop any read or write (depending on flags) on the underlying
+ * transport on the QEMUFile.
+ * Existing blocking reads/writes must be woken
+ * Returns 0 on success, -err on error
+ */
+typedef int (QEMUFileShutdownFunc)(void *opaque, bool rd, bool wr);
+
typedef struct QEMUFileOps {
QEMUFilePutBufferFunc *put_buffer;
QEMUFileGetBufferFunc *get_buffer;
@@ -94,6 +102,7 @@ typedef struct QEMUFileOps {
QEMURamHookFunc *after_ram_iterate;
QEMURamHookFunc *hook_ram_load;
QEMURamSaveFunc *save_page;
+ QEMUFileShutdownFunc *shut_down;
} QEMUFileOps;
struct QEMUSizedBuffer {
@@ -177,6 +186,7 @@ void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
int64_t qemu_file_get_rate_limit(QEMUFile *f);
int qemu_file_get_error(QEMUFile *f);
void qemu_file_set_error(QEMUFile *f, int ret);
+int qemu_file_shutdown(QEMUFile *f);
void qemu_fflush(QEMUFile *f);
static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index f47dae6..7992ece 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -44,6 +44,13 @@ int socket_set_fast_reuse(int fd);
int send_all(int fd, const void *buf, int len1);
int recv_all(int fd, void *buf, int len1, bool single_read);
+#ifdef WIN32
+/* Windows has different names for the same constants with the same values */
+#define SHUT_RD 0
+#define SHUT_WR 1
+#define SHUT_RDWR 2
+#endif
+
/* callback function for nonblocking connect
* valid fd on success, negative error code on failure
*/
diff --git a/migration/qemu-file-unix.c b/migration/qemu-file-unix.c
index 9682396..bfbc086 100644
--- a/migration/qemu-file-unix.c
+++ b/migration/qemu-file-unix.c
@@ -26,6 +26,7 @@
#include "qemu/sockets.h"
#include "block/coroutine.h"
#include "migration/qemu-file.h"
+#include "migration/qemu-file-internal.h"
typedef struct QEMUFileSocket {
int fd;
@@ -84,6 +85,17 @@ static int socket_close(void *opaque)
return 0;
}
+static int socket_shutdown(void *opaque, bool rd, bool wr)
+{
+ QEMUFileSocket *s = opaque;
+
+ if (shutdown(s->fd, rd ? (wr ? SHUT_RDWR : SHUT_RD) : SHUT_WR)) {
+ return -errno;
+ } else {
+ return 0;
+ }
+}
+
static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
int64_t pos)
{
@@ -192,15 +204,18 @@ QEMUFile *qemu_fdopen(int fd, const char *mode)
}
static const QEMUFileOps socket_read_ops = {
- .get_fd = socket_get_fd,
+ .get_fd = socket_get_fd,
.get_buffer = socket_get_buffer,
- .close = socket_close
+ .close = socket_close,
+ .shut_down = socket_shutdown
+
};
static const QEMUFileOps socket_write_ops = {
- .get_fd = socket_get_fd,
+ .get_fd = socket_get_fd,
.writev_buffer = socket_writev_buffer,
- .close = socket_close
+ .close = socket_close,
+ .shut_down = socket_shutdown
};
QEMUFile *qemu_fopen_socket(int fd, const char *mode)
diff --git a/migration/qemu-file.c b/migration/qemu-file.c
index d2d4007..edb0d10 100644
--- a/migration/qemu-file.c
+++ b/migration/qemu-file.c
@@ -30,6 +30,18 @@
#include "migration/qemu-file-internal.h"
#include "trace.h"
+/*
+ * Stop a file from being read/written - not all backing files can do this
+ * typically only sockets can.
+ */
+int qemu_file_shutdown(QEMUFile *f)
+{
+ if (!f->ops->shut_down) {
+ return -ENOSYS;
+ }
+ return f->ops->shut_down(f->opaque, true, true);
+}
+
bool qemu_file_mode_is_not_valid(const char *mode)
{
if (mode == NULL ||
--
2.1.0
next prev parent reply other threads:[~2015-01-08 11:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-08 11:11 [Qemu-devel] [PATCH 0/3] Migration cancel with dead network Dr. David Alan Gilbert (git)
2015-01-08 11:11 ` Dr. David Alan Gilbert (git) [this message]
2015-01-09 6:50 ` [Qemu-devel] [PATCH 1/3] socket shutdown Amit Shah
2015-01-08 11:11 ` [Qemu-devel] [PATCH 2/3] Handle bi-directional communication for fd migration Dr. David Alan Gilbert (git)
2015-01-08 11:11 ` [Qemu-devel] [PATCH 3/3] migration_cancel: shutdown migration socket Dr. David Alan Gilbert (git)
2015-01-08 11:22 ` [Qemu-devel] [PATCH 0/3] Migration cancel with dead network Daniel P. Berrange
2015-01-08 11:29 ` Dr. David Alan Gilbert
2015-01-08 11:33 ` Daniel P. Berrange
2015-01-08 12:10 ` Paolo Bonzini
2015-01-09 6:50 ` Amit Shah
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1420715492-5109-2-git-send-email-dgilbert@redhat.com \
--to=dgilbert@redhat.com \
--cc=amit.shah@redhat.com \
--cc=cristian.klein@cs.umu.se \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).