blob: f979b5fb9d025ec9c4e923f1ad5fd854f8ff30c7 [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 *
12 */
13
14#include "qemu-common.h"
15#include "qemu_socket.h"
16#include "migration.h"
17#include "qemu-char.h"
Chris Lalancette4951f652009-08-05 17:24:29 +020018#include "buffered_file.h"
19#include "block.h"
20
21//#define DEBUG_MIGRATION_UNIX
22
23#ifdef DEBUG_MIGRATION_UNIX
malcd0f2c4c2010-02-07 02:03:50 +030024#define DPRINTF(fmt, ...) \
Chris Lalancette4951f652009-08-05 17:24:29 +020025 do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
26#else
malcd0f2c4c2010-02-07 02:03:50 +030027#define DPRINTF(fmt, ...) \
Chris Lalancette4951f652009-08-05 17:24:29 +020028 do { } while (0)
29#endif
30
Juan Quintela22f00a42010-05-11 15:56:35 +020031static int unix_errno(MigrationState *s)
Chris Lalancette4951f652009-08-05 17:24:29 +020032{
33 return errno;
34}
35
Juan Quintela22f00a42010-05-11 15:56:35 +020036static int unix_write(MigrationState *s, const void * buf, size_t size)
Chris Lalancette4951f652009-08-05 17:24:29 +020037{
38 return write(s->fd, buf, size);
39}
40
Juan Quintela22f00a42010-05-11 15:56:35 +020041static int unix_close(MigrationState *s)
Chris Lalancette4951f652009-08-05 17:24:29 +020042{
malcd0f2c4c2010-02-07 02:03:50 +030043 DPRINTF("unix_close\n");
Chris Lalancette4951f652009-08-05 17:24:29 +020044 if (s->fd != -1) {
45 close(s->fd);
46 s->fd = -1;
47 }
48 return 0;
49}
50
51static void unix_wait_for_connect(void *opaque)
52{
Juan Quintela22f00a42010-05-11 15:56:35 +020053 MigrationState *s = opaque;
Chris Lalancette4951f652009-08-05 17:24:29 +020054 int val, ret;
55 socklen_t valsize = sizeof(val);
56
malcd0f2c4c2010-02-07 02:03:50 +030057 DPRINTF("connect completed\n");
Chris Lalancette4951f652009-08-05 17:24:29 +020058 do {
59 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
Juan Quintelaefab4712011-02-23 11:52:12 +010060 } while (ret == -1 && errno == EINTR);
Chris Lalancette4951f652009-08-05 17:24:29 +020061
62 if (ret < 0) {
63 migrate_fd_error(s);
64 return;
65 }
66
67 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
68
69 if (val == 0)
70 migrate_fd_connect(s);
71 else {
malcd0f2c4c2010-02-07 02:03:50 +030072 DPRINTF("error connecting %d\n", val);
Chris Lalancette4951f652009-08-05 17:24:29 +020073 migrate_fd_error(s);
74 }
75}
76
Juan Quintela07af4452010-05-11 22:27:45 +020077int unix_start_outgoing_migration(MigrationState *s, const char *path)
Chris Lalancette4951f652009-08-05 17:24:29 +020078{
Chris Lalancette4951f652009-08-05 17:24:29 +020079 struct sockaddr_un addr;
80 int ret;
81
82 addr.sun_family = AF_UNIX;
83 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
Chris Lalancette4951f652009-08-05 17:24:29 +020084 s->get_error = unix_errno;
85 s->write = unix_write;
86 s->close = unix_close;
Chris Lalancette4951f652009-08-05 17:24:29 +020087
Kevin Wolf40ff6d72009-12-02 12:24:42 +010088 s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
Chris Lalancette4951f652009-08-05 17:24:29 +020089 if (s->fd < 0) {
malcd0f2c4c2010-02-07 02:03:50 +030090 DPRINTF("Unable to open socket");
Juan Quintela8414ff32011-02-23 19:56:52 +010091 return -errno;
Chris Lalancette4951f652009-08-05 17:24:29 +020092 }
93
94 socket_set_nonblock(s->fd);
95
Chris Lalancette4951f652009-08-05 17:24:29 +020096 do {
97 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
Juan Quintela8414ff32011-02-23 19:56:52 +010098 if (ret == -1) {
Juan Quintelaefab4712011-02-23 11:52:12 +010099 ret = -errno;
Juan Quintela8414ff32011-02-23 19:56:52 +0100100 }
101 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
Chris Lalancette4951f652009-08-05 17:24:29 +0200102 qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
Juan Quintela8414ff32011-02-23 19:56:52 +0100103 return 0;
104 }
Chris Lalancette4951f652009-08-05 17:24:29 +0200105 } while (ret == -EINTR);
106
Juan Quintela8414ff32011-02-23 19:56:52 +0100107 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300108 DPRINTF("connect failed\n");
Juan Quintela8414ff32011-02-23 19:56:52 +0100109 migrate_fd_error(s);
110 return ret;
Daniel P. Berrange2dd650e2009-12-11 21:01:14 +0000111 }
Juan Quintela8414ff32011-02-23 19:56:52 +0100112 migrate_fd_connect(s);
Juan Quintela07af4452010-05-11 22:27:45 +0200113 return 0;
Chris Lalancette4951f652009-08-05 17:24:29 +0200114}
115
116static void unix_accept_incoming_migration(void *opaque)
117{
118 struct sockaddr_un addr;
119 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100120 int s = (intptr_t)opaque;
Chris Lalancette4951f652009-08-05 17:24:29 +0200121 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200122 int c;
Chris Lalancette4951f652009-08-05 17:24:29 +0200123
124 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100125 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
Juan Quintelaefab4712011-02-23 11:52:12 +0100126 } while (c == -1 && errno == EINTR);
Chris Lalancette4951f652009-08-05 17:24:29 +0200127
malcd0f2c4c2010-02-07 02:03:50 +0300128 DPRINTF("accepted migration\n");
Chris Lalancette4951f652009-08-05 17:24:29 +0200129
130 if (c == -1) {
131 fprintf(stderr, "could not accept migration connection\n");
132 return;
133 }
134
135 f = qemu_fopen_socket(c);
136 if (f == NULL) {
137 fprintf(stderr, "could not qemu_fopen socket\n");
138 goto out;
139 }
140
Juan Quintela511c0232010-06-09 14:10:55 +0200141 process_incoming_migration(f);
Chris Lalancette4951f652009-08-05 17:24:29 +0200142 qemu_fclose(f);
143out:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100144 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
145 close(s);
Chris Lalancette4951f652009-08-05 17:24:29 +0200146 close(c);
147}
148
149int unix_start_incoming_migration(const char *path)
150{
151 struct sockaddr_un un;
152 int sock;
153
malcd0f2c4c2010-02-07 02:03:50 +0300154 DPRINTF("Attempting to start an incoming migration\n");
Chris Lalancette4951f652009-08-05 17:24:29 +0200155
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100156 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
Chris Lalancette4951f652009-08-05 17:24:29 +0200157 if (sock < 0) {
158 fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
159 return -EINVAL;
160 }
161
162 memset(&un, 0, sizeof(un));
163 un.sun_family = AF_UNIX;
164 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
165
166 unlink(un.sun_path);
167 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
168 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
169 goto err;
170 }
171 if (listen(sock, 1) < 0) {
172 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
173 goto err;
174 }
175
176 qemu_set_fd_handler2(sock, NULL, unix_accept_incoming_migration, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100177 (void *)(intptr_t)sock);
Chris Lalancette4951f652009-08-05 17:24:29 +0200178
179 return 0;
180
181err:
182 close(sock);
183
184 return -EINVAL;
185}