2009-08-05 19:24:29 +04:00
|
|
|
/*
|
|
|
|
* QEMU live migration via Unix Domain Sockets
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Chris Lalancette <clalance@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-13 20:44:23 +04:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-08-05 19:24:29 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu_socket.h"
|
|
|
|
#include "migration.h"
|
|
|
|
#include "qemu-char.h"
|
|
|
|
#include "buffered_file.h"
|
|
|
|
#include "block.h"
|
|
|
|
|
|
|
|
//#define DEBUG_MIGRATION_UNIX
|
|
|
|
|
|
|
|
#ifdef DEBUG_MIGRATION_UNIX
|
2010-02-07 02:03:50 +03:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-08-05 19:24:29 +04:00
|
|
|
do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
2010-02-07 02:03:50 +03:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-08-05 19:24:29 +04:00
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
2010-05-11 17:56:35 +04:00
|
|
|
static int unix_errno(MigrationState *s)
|
2009-08-05 19:24:29 +04:00
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2010-05-11 17:56:35 +04:00
|
|
|
static int unix_write(MigrationState *s, const void * buf, size_t size)
|
2009-08-05 19:24:29 +04:00
|
|
|
{
|
|
|
|
return write(s->fd, buf, size);
|
|
|
|
}
|
|
|
|
|
2010-05-11 17:56:35 +04:00
|
|
|
static int unix_close(MigrationState *s)
|
2009-08-05 19:24:29 +04:00
|
|
|
{
|
2011-11-10 16:41:48 +04:00
|
|
|
int r = 0;
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("unix_close\n");
|
2009-08-05 19:24:29 +04:00
|
|
|
if (s->fd != -1) {
|
2011-11-10 16:41:48 +04:00
|
|
|
if (close(s->fd) < 0) {
|
|
|
|
r = -errno;
|
|
|
|
}
|
2009-08-05 19:24:29 +04:00
|
|
|
s->fd = -1;
|
|
|
|
}
|
2011-11-10 16:41:48 +04:00
|
|
|
return r;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unix_wait_for_connect(void *opaque)
|
|
|
|
{
|
2010-05-11 17:56:35 +04:00
|
|
|
MigrationState *s = opaque;
|
2009-08-05 19:24:29 +04:00
|
|
|
int val, ret;
|
|
|
|
socklen_t valsize = sizeof(val);
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("connect completed\n");
|
2009-08-05 19:24:29 +04:00
|
|
|
do {
|
|
|
|
ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
|
2011-02-23 13:52:12 +03:00
|
|
|
} while (ret == -1 && errno == EINTR);
|
2009-08-05 19:24:29 +04:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
migrate_fd_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
migrate_fd_connect(s);
|
|
|
|
else {
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("error connecting %d\n", val);
|
2009-08-05 19:24:29 +04:00
|
|
|
migrate_fd_error(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 00:27:45 +04:00
|
|
|
int unix_start_outgoing_migration(MigrationState *s, const char *path)
|
2009-08-05 19:24:29 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
|
|
|
|
s->get_error = unix_errno;
|
|
|
|
s->write = unix_write;
|
|
|
|
s->close = unix_close;
|
|
|
|
|
2009-12-02 14:24:42 +03:00
|
|
|
s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
2011-02-23 22:44:29 +03:00
|
|
|
if (s->fd == -1) {
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("Unable to open socket");
|
2011-02-23 21:56:52 +03:00
|
|
|
return -errno;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
socket_set_nonblock(s->fd);
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
|
2011-02-23 21:56:52 +03:00
|
|
|
if (ret == -1) {
|
2011-02-23 13:52:12 +03:00
|
|
|
ret = -errno;
|
2011-02-23 21:56:52 +03:00
|
|
|
}
|
|
|
|
if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
|
2009-08-05 19:24:29 +04:00
|
|
|
qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
|
2011-02-23 21:56:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2009-08-05 19:24:29 +04:00
|
|
|
} while (ret == -EINTR);
|
|
|
|
|
2011-02-23 21:56:52 +03:00
|
|
|
if (ret < 0) {
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("connect failed\n");
|
2011-02-23 21:56:52 +03:00
|
|
|
migrate_fd_error(s);
|
|
|
|
return ret;
|
2009-12-12 00:01:14 +03:00
|
|
|
}
|
2011-02-23 21:56:52 +03:00
|
|
|
migrate_fd_connect(s);
|
2010-05-12 00:27:45 +04:00
|
|
|
return 0;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unix_accept_incoming_migration(void *opaque)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
2011-02-23 21:09:16 +03:00
|
|
|
int s = (intptr_t)opaque;
|
2009-08-05 19:24:29 +04:00
|
|
|
QEMUFile *f;
|
2010-06-09 16:10:55 +04:00
|
|
|
int c;
|
2009-08-05 19:24:29 +04:00
|
|
|
|
|
|
|
do {
|
2009-12-02 14:24:42 +03:00
|
|
|
c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
|
2011-02-23 13:52:12 +03:00
|
|
|
} while (c == -1 && errno == EINTR);
|
2009-08-05 19:24:29 +04:00
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("accepted migration\n");
|
2009-08-05 19:24:29 +04:00
|
|
|
|
|
|
|
if (c == -1) {
|
|
|
|
fprintf(stderr, "could not accept migration connection\n");
|
2011-02-23 22:44:29 +03:00
|
|
|
goto out2;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
f = qemu_fopen_socket(c);
|
|
|
|
if (f == NULL) {
|
|
|
|
fprintf(stderr, "could not qemu_fopen socket\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-06-09 16:10:55 +04:00
|
|
|
process_incoming_migration(f);
|
2009-08-05 19:24:29 +04:00
|
|
|
qemu_fclose(f);
|
|
|
|
out:
|
2011-02-23 22:44:29 +03:00
|
|
|
close(c);
|
|
|
|
out2:
|
2010-03-10 02:10:35 +03:00
|
|
|
qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
|
|
|
|
close(s);
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int unix_start_incoming_migration(const char *path)
|
|
|
|
{
|
2011-02-23 22:44:29 +03:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
int s;
|
|
|
|
int ret;
|
2009-08-05 19:24:29 +04:00
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("Attempting to start an incoming migration\n");
|
2009-08-05 19:24:29 +04:00
|
|
|
|
2011-02-23 22:44:29 +03:00
|
|
|
s = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s == -1) {
|
2009-08-05 19:24:29 +04:00
|
|
|
fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
|
2011-02-23 22:44:29 +03:00
|
|
|
return -errno;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|
|
|
|
|
2011-02-23 22:44:29 +03:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
|
2009-08-05 19:24:29 +04:00
|
|
|
|
2011-02-23 22:44:29 +03:00
|
|
|
unlink(addr.sun_path);
|
|
|
|
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
fprintf(stderr, "bind(unix:%s): %s\n", addr.sun_path, strerror(errno));
|
2009-08-05 19:24:29 +04:00
|
|
|
goto err;
|
|
|
|
}
|
2011-02-23 22:44:29 +03:00
|
|
|
if (listen(s, 1) == -1) {
|
|
|
|
fprintf(stderr, "listen(unix:%s): %s\n", addr.sun_path,
|
|
|
|
strerror(errno));
|
|
|
|
ret = -errno;
|
2009-08-05 19:24:29 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-02-23 22:44:29 +03:00
|
|
|
qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
|
|
|
|
(void *)(intptr_t)s);
|
2009-08-05 19:24:29 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2011-02-23 22:44:29 +03:00
|
|
|
close(s);
|
|
|
|
return ret;
|
2009-08-05 19:24:29 +04:00
|
|
|
}
|