blob: 169de886775a1d3112678ae22607d6ef1b3ec0f0 [file] [log] [blame]
Chris Lalancette4951f652009-08-05 17:24:29 +02001/*
2 * QEMU live migration via Unix Domain Sockets
3 *
4 * Copyright Red Hat, Inc. 2009
5 *
6 * Authors:
7 * Chris Lalancette <clalance@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Chris Lalancette4951f652009-08-05 17:24:29 +020014 */
15
16#include "qemu-common.h"
17#include "qemu_socket.h"
18#include "migration.h"
19#include "qemu-char.h"
Chris Lalancette4951f652009-08-05 17:24:29 +020020#include "buffered_file.h"
21#include "block.h"
22
23//#define DEBUG_MIGRATION_UNIX
24
25#ifdef DEBUG_MIGRATION_UNIX
malcd0f2c4c2010-02-07 02:03:50 +030026#define DPRINTF(fmt, ...) \
Chris Lalancette4951f652009-08-05 17:24:29 +020027 do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
28#else
malcd0f2c4c2010-02-07 02:03:50 +030029#define DPRINTF(fmt, ...) \
Chris Lalancette4951f652009-08-05 17:24:29 +020030 do { } while (0)
31#endif
32
Juan Quintela22f00a42010-05-11 15:56:35 +020033static int unix_errno(MigrationState *s)
Chris Lalancette4951f652009-08-05 17:24:29 +020034{
35 return errno;
36}
37
Juan Quintela22f00a42010-05-11 15:56:35 +020038static int unix_write(MigrationState *s, const void * buf, size_t size)
Chris Lalancette4951f652009-08-05 17:24:29 +020039{
40 return write(s->fd, buf, size);
41}
42
Juan Quintela22f00a42010-05-11 15:56:35 +020043static int unix_close(MigrationState *s)
Chris Lalancette4951f652009-08-05 17:24:29 +020044{
Eduardo Habkost8160bfb2011-11-10 10:41:48 -020045 int r = 0;
malcd0f2c4c2010-02-07 02:03:50 +030046 DPRINTF("unix_close\n");
Chris Lalancette4951f652009-08-05 17:24:29 +020047 if (s->fd != -1) {
Eduardo Habkost8160bfb2011-11-10 10:41:48 -020048 if (close(s->fd) < 0) {
49 r = -errno;
50 }
Chris Lalancette4951f652009-08-05 17:24:29 +020051 s->fd = -1;
52 }
Eduardo Habkost8160bfb2011-11-10 10:41:48 -020053 return r;
Chris Lalancette4951f652009-08-05 17:24:29 +020054}
55
56static void unix_wait_for_connect(void *opaque)
57{
Juan Quintela22f00a42010-05-11 15:56:35 +020058 MigrationState *s = opaque;
Chris Lalancette4951f652009-08-05 17:24:29 +020059 int val, ret;
60 socklen_t valsize = sizeof(val);
61
malcd0f2c4c2010-02-07 02:03:50 +030062 DPRINTF("connect completed\n");
Chris Lalancette4951f652009-08-05 17:24:29 +020063 do {
64 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
Juan Quintelaefab4712011-02-23 11:52:12 +010065 } while (ret == -1 && errno == EINTR);
Chris Lalancette4951f652009-08-05 17:24:29 +020066
67 if (ret < 0) {
68 migrate_fd_error(s);
69 return;
70 }
71
72 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
73
74 if (val == 0)
75 migrate_fd_connect(s);
76 else {
malcd0f2c4c2010-02-07 02:03:50 +030077 DPRINTF("error connecting %d\n", val);
Chris Lalancette4951f652009-08-05 17:24:29 +020078 migrate_fd_error(s);
79 }
80}
81
Juan Quintela07af4452010-05-11 22:27:45 +020082int unix_start_outgoing_migration(MigrationState *s, const char *path)
Chris Lalancette4951f652009-08-05 17:24:29 +020083{
Chris Lalancette4951f652009-08-05 17:24:29 +020084 struct sockaddr_un addr;
85 int ret;
86
87 addr.sun_family = AF_UNIX;
88 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
Chris Lalancette4951f652009-08-05 17:24:29 +020089 s->get_error = unix_errno;
90 s->write = unix_write;
91 s->close = unix_close;
Chris Lalancette4951f652009-08-05 17:24:29 +020092
Kevin Wolf40ff6d72009-12-02 12:24:42 +010093 s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
Juan Quintelaee86c612011-02-23 20:44:29 +010094 if (s->fd == -1) {
malcd0f2c4c2010-02-07 02:03:50 +030095 DPRINTF("Unable to open socket");
Juan Quintela8414ff32011-02-23 19:56:52 +010096 return -errno;
Chris Lalancette4951f652009-08-05 17:24:29 +020097 }
98
99 socket_set_nonblock(s->fd);
100
Chris Lalancette4951f652009-08-05 17:24:29 +0200101 do {
102 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
Juan Quintela8414ff32011-02-23 19:56:52 +0100103 if (ret == -1) {
Juan Quintelaefab4712011-02-23 11:52:12 +0100104 ret = -errno;
Juan Quintela8414ff32011-02-23 19:56:52 +0100105 }
106 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
Chris Lalancette4951f652009-08-05 17:24:29 +0200107 qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
Juan Quintela8414ff32011-02-23 19:56:52 +0100108 return 0;
109 }
Chris Lalancette4951f652009-08-05 17:24:29 +0200110 } while (ret == -EINTR);
111
Juan Quintela8414ff32011-02-23 19:56:52 +0100112 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300113 DPRINTF("connect failed\n");
Juan Quintela8414ff32011-02-23 19:56:52 +0100114 migrate_fd_error(s);
115 return ret;
Daniel P. Berrange2dd650e2009-12-11 21:01:14 +0000116 }
Juan Quintela8414ff32011-02-23 19:56:52 +0100117 migrate_fd_connect(s);
Juan Quintela07af4452010-05-11 22:27:45 +0200118 return 0;
Chris Lalancette4951f652009-08-05 17:24:29 +0200119}
120
121static void unix_accept_incoming_migration(void *opaque)
122{
123 struct sockaddr_un addr;
124 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100125 int s = (intptr_t)opaque;
Chris Lalancette4951f652009-08-05 17:24:29 +0200126 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200127 int c;
Chris Lalancette4951f652009-08-05 17:24:29 +0200128
129 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100130 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
Juan Quintelaefab4712011-02-23 11:52:12 +0100131 } while (c == -1 && errno == EINTR);
Chris Lalancette4951f652009-08-05 17:24:29 +0200132
malcd0f2c4c2010-02-07 02:03:50 +0300133 DPRINTF("accepted migration\n");
Chris Lalancette4951f652009-08-05 17:24:29 +0200134
135 if (c == -1) {
136 fprintf(stderr, "could not accept migration connection\n");
Juan Quintelaee86c612011-02-23 20:44:29 +0100137 goto out2;
Chris Lalancette4951f652009-08-05 17:24:29 +0200138 }
139
140 f = qemu_fopen_socket(c);
141 if (f == NULL) {
142 fprintf(stderr, "could not qemu_fopen socket\n");
143 goto out;
144 }
145
Juan Quintela511c0232010-06-09 14:10:55 +0200146 process_incoming_migration(f);
Chris Lalancette4951f652009-08-05 17:24:29 +0200147 qemu_fclose(f);
148out:
Juan Quintelaee86c612011-02-23 20:44:29 +0100149 close(c);
150out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100151 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
152 close(s);
Chris Lalancette4951f652009-08-05 17:24:29 +0200153}
154
155int unix_start_incoming_migration(const char *path)
156{
Juan Quintelaee86c612011-02-23 20:44:29 +0100157 struct sockaddr_un addr;
158 int s;
159 int ret;
Chris Lalancette4951f652009-08-05 17:24:29 +0200160
malcd0f2c4c2010-02-07 02:03:50 +0300161 DPRINTF("Attempting to start an incoming migration\n");
Chris Lalancette4951f652009-08-05 17:24:29 +0200162
Juan Quintelaee86c612011-02-23 20:44:29 +0100163 s = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
164 if (s == -1) {
Chris Lalancette4951f652009-08-05 17:24:29 +0200165 fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
Juan Quintelaee86c612011-02-23 20:44:29 +0100166 return -errno;
Chris Lalancette4951f652009-08-05 17:24:29 +0200167 }
168
Juan Quintelaee86c612011-02-23 20:44:29 +0100169 memset(&addr, 0, sizeof(addr));
170 addr.sun_family = AF_UNIX;
171 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
Chris Lalancette4951f652009-08-05 17:24:29 +0200172
Juan Quintelaee86c612011-02-23 20:44:29 +0100173 unlink(addr.sun_path);
174 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
175 ret = -errno;
176 fprintf(stderr, "bind(unix:%s): %s\n", addr.sun_path, strerror(errno));
Chris Lalancette4951f652009-08-05 17:24:29 +0200177 goto err;
178 }
Juan Quintelaee86c612011-02-23 20:44:29 +0100179 if (listen(s, 1) == -1) {
180 fprintf(stderr, "listen(unix:%s): %s\n", addr.sun_path,
181 strerror(errno));
182 ret = -errno;
Chris Lalancette4951f652009-08-05 17:24:29 +0200183 goto err;
184 }
185
Juan Quintelaee86c612011-02-23 20:44:29 +0100186 qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
187 (void *)(intptr_t)s);
Chris Lalancette4951f652009-08-05 17:24:29 +0200188
189 return 0;
190
191err:
Juan Quintelaee86c612011-02-23 20:44:29 +0100192 close(s);
193 return ret;
Chris Lalancette4951f652009-08-05 17:24:29 +0200194}