From f018d8cd2123f495300935d5019931abbee4e5d9 Mon Sep 17 00:00:00 2001 From: Yang Hongyang Date: Fri, 19 Dec 2014 11:38:05 +0800 Subject: QEMUSizedBuffer: only free qsb that qemu_bufopen allocated Only free qsb that qemu_bufopen allocated, and also allow qemu_bufopen accept qsb as input for write operation. It will make the API more logical: 1.If you create the QEMUSizedBuffer yourself, you need to free it by using qsb_free() but not depends on other API like qemu_fclose. 2.allow qemu_bufopen() accept QEMUSizedBuffer as input for write operation, otherwise, it will be a little strange for this API won't accept the second parameter. This brings API change, since there are only 3 users of this API currently, this change only impact the first one which will be fixed in patch 2 of this patchset, so I think it is safe to do this change. 1 70 tests/test-vmstate.c <> return qemu_bufopen("r", qsb); 2 404 tests/test-vmstate.c <> QEMUFile *fsave = qemu_bufopen("w", NULL); 3 424 tests/test-vmstate.c <> QEMUFile *fsave = qemu_bufopen("w", NULL); Signed-off-by: Yang Hongyang Cc: Dr. David Alan Gilbert Cc: Juan Quintela Cc: Amit Shah Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Amit Shah --- migration/qemu-file-buf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'migration') diff --git a/migration/qemu-file-buf.c b/migration/qemu-file-buf.c index d33dd44..e97e0bd 100644 --- a/migration/qemu-file-buf.c +++ b/migration/qemu-file-buf.c @@ -395,6 +395,7 @@ QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *qsb) typedef struct QEMUBuffer { QEMUSizedBuffer *qsb; QEMUFile *file; + bool qsb_allocated; } QEMUBuffer; static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) @@ -424,7 +425,9 @@ static int buf_close(void *opaque) { QEMUBuffer *s = opaque; - qsb_free(s->qsb); + if (s->qsb_allocated) { + qsb_free(s->qsb); + } g_free(s); @@ -463,12 +466,11 @@ QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input) } s = g_malloc0(sizeof(QEMUBuffer)); - if (mode[0] == 'r') { - s->qsb = input; - } + s->qsb = input; if (s->qsb == NULL) { s->qsb = qsb_create(NULL, 0); + s->qsb_allocated = true; } if (!s->qsb) { g_free(s); -- cgit v1.1 From e1a8c9b67fc97d293211773edcae9e8e2f3367ab Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Thu, 8 Jan 2015 11:11:30 +0000 Subject: socket shutdown 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 Reviewed-by: Paolo Bonzini Reviewed-by: Amit Shah Signed-off-by: Amit Shah --- migration/qemu-file-unix.c | 23 +++++++++++++++++++---- migration/qemu-file.c | 12 ++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'migration') 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 a7f2a34..edc2830 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 || -- cgit v1.1 From 131fe9b843f9a1e55fcbf2457c9cb25c3711b9d8 Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Thu, 8 Jan 2015 11:11:31 +0000 Subject: Handle bi-directional communication for fd migration libvirt prefers opening the TCP connection itself, for two reasons. First, connection failed errors can be detected easier, without having to parse qemu's error output. Second, libvirt might be asked to secure the transfer by tunnelling the communication through an TLS layer. Therefore, libvirt opens the TCP connection itself and passes an FD to qemu using QMP and a POSIX-specific mechanism. Hence, in order to make the reverse-path work in such cases, qemu needs to distinguish if the transmitted FD is a socket (reverse-path available) or not (reverse-path might not be available) and use the corresponding abstraction. Signed-off-by: Cristian Klein Reviewed-by: Paolo Bonzini Reviewed-by: Amit Shah Signed-off-by: Amit Shah --- migration/fd.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'migration') diff --git a/migration/fd.c b/migration/fd.c index d2e523a..129da99 100644 --- a/migration/fd.c +++ b/migration/fd.c @@ -31,13 +31,29 @@ do { } while (0) #endif +static bool fd_is_socket(int fd) +{ + struct stat stat; + int ret = fstat(fd, &stat); + if (ret == -1) { + /* When in doubt say no */ + return false; + } + return S_ISSOCK(stat.st_mode); +} + void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp) { int fd = monitor_get_fd(cur_mon, fdname, errp); if (fd == -1) { return; } - s->file = qemu_fdopen(fd, "wb"); + + if (fd_is_socket(fd)) { + s->file = qemu_fopen_socket(fd, "wb"); + } else { + s->file = qemu_fdopen(fd, "wb"); + } migrate_fd_connect(s); } @@ -58,7 +74,11 @@ void fd_start_incoming_migration(const char *infd, Error **errp) DPRINTF("Attempting to start an incoming migration via fd\n"); fd = strtol(infd, NULL, 0); - f = qemu_fdopen(fd, "rb"); + if (fd_is_socket(fd)) { + f = qemu_fopen_socket(fd, "rb"); + } else { + f = qemu_fdopen(fd, "rb"); + } if(f == NULL) { error_setg_errno(errp, errno, "failed to open the source descriptor"); return; -- cgit v1.1 From a26ba26e214911dc879a23e797d2c269cdb38577 Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Thu, 8 Jan 2015 11:11:32 +0000 Subject: migration_cancel: shutdown migration socket Force shutdown on migration socket on cancel to cause the cancel to complete even if the socket is blocked on a dead network. Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Paolo Bonzini Reviewed-by: Amit Shah Signed-off-by: Amit Shah --- migration/migration.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'migration') diff --git a/migration/migration.c b/migration/migration.c index c49a05a..b3adbc6 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -330,6 +330,7 @@ void migrate_fd_error(MigrationState *s) static void migrate_fd_cancel(MigrationState *s) { int old_state ; + QEMUFile *f = migrate_get_current()->file; trace_migrate_fd_cancel(); do { @@ -339,6 +340,17 @@ static void migrate_fd_cancel(MigrationState *s) } migrate_set_state(s, old_state, MIG_STATE_CANCELLING); } while (s->state != MIG_STATE_CANCELLING); + + /* + * If we're unlucky the migration code might be stuck somewhere in a + * send/write while the network has failed and is waiting to timeout; + * if we've got shutdown(2) available then we can force it to quit. + * The outgoing qemu file gets closed in migrate_fd_cleanup that is + * called in a bh, so there is no race against this cancel. + */ + if (s->state == MIG_STATE_CANCELLING && f) { + qemu_file_shutdown(f); + } } void add_migration_state_change_notifier(Notifier *notify) -- cgit v1.1