blob: d9c4c986db1d5d154e3cc0fe59dc5448c6877e9f [file] [log] [blame]
aliguori34c9dd82008-10-13 03:14:31 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.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"
18#include "sysemu.h"
aliguori34c9dd82008-10-13 03:14:31 +000019#include "buffered_file.h"
20#include "block.h"
21
22//#define DEBUG_MIGRATION_TCP
23
aliguori34c9dd82008-10-13 03:14:31 +000024#ifdef DEBUG_MIGRATION_TCP
25#define dprintf(fmt, ...) \
26 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27#else
28#define dprintf(fmt, ...) \
29 do { } while (0)
30#endif
31
aliguori065e2812008-11-11 16:46:33 +000032static int socket_errno(FdMigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000033{
aliguori8ad9fa52008-11-12 22:29:11 +000034 return socket_error();
aliguori34c9dd82008-10-13 03:14:31 +000035}
36
aliguori065e2812008-11-11 16:46:33 +000037static int socket_write(FdMigrationState *s, const void * buf, size_t size)
aliguori34c9dd82008-10-13 03:14:31 +000038{
aliguori065e2812008-11-11 16:46:33 +000039 return send(s->fd, buf, size, 0);
aliguori34c9dd82008-10-13 03:14:31 +000040}
41
aliguori065e2812008-11-11 16:46:33 +000042static int tcp_close(FdMigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000043{
aliguori065e2812008-11-11 16:46:33 +000044 dprintf("tcp_close\n");
aliguori34c9dd82008-10-13 03:14:31 +000045 if (s->fd != -1) {
aliguoriff8d81d2008-10-24 22:10:31 +000046 close(s->fd);
47 s->fd = -1;
aliguori34c9dd82008-10-13 03:14:31 +000048 }
49 return 0;
50}
51
aliguori34c9dd82008-10-13 03:14:31 +000052
53static void tcp_wait_for_connect(void *opaque)
54{
55 FdMigrationState *s = opaque;
56 int val, ret;
blueswir14761a482008-10-25 11:25:48 +000057 socklen_t valsize = sizeof(val);
aliguori34c9dd82008-10-13 03:14:31 +000058
59 dprintf("connect completed\n");
60 do {
61 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
aliguori065e2812008-11-11 16:46:33 +000062 } while (ret == -1 && (s->get_error(s)) == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +000063
64 if (ret < 0) {
aliguori065e2812008-11-11 16:46:33 +000065 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000066 return;
67 }
68
69 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
70
71 if (val == 0)
aliguori065e2812008-11-11 16:46:33 +000072 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +000073 else {
74 dprintf("error connecting %d\n", val);
aliguori065e2812008-11-11 16:46:33 +000075 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000076 }
77}
78
aliguori34c9dd82008-10-13 03:14:31 +000079MigrationState *tcp_start_outgoing_migration(const char *host_port,
aliguoriff8d81d2008-10-24 22:10:31 +000080 int64_t bandwidth_limit,
aliguori731b0362009-03-05 23:01:42 +000081 int detach)
aliguori34c9dd82008-10-13 03:14:31 +000082{
83 struct sockaddr_in addr;
84 FdMigrationState *s;
85 int ret;
86
87 if (parse_host_port(&addr, host_port) < 0)
88 return NULL;
89
90 s = qemu_mallocz(sizeof(*s));
aliguori34c9dd82008-10-13 03:14:31 +000091
aliguori065e2812008-11-11 16:46:33 +000092 s->get_error = socket_errno;
93 s->write = socket_write;
94 s->close = tcp_close;
95 s->mig_state.cancel = migrate_fd_cancel;
96 s->mig_state.get_status = migrate_fd_get_status;
97 s->mig_state.release = migrate_fd_release;
aliguori34c9dd82008-10-13 03:14:31 +000098
99 s->state = MIG_STATE_ACTIVE;
aliguori731b0362009-03-05 23:01:42 +0000100 s->mon_resume = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000101 s->bandwidth_limit = bandwidth_limit;
102 s->fd = socket(PF_INET, SOCK_STREAM, 0);
103 if (s->fd == -1) {
104 qemu_free(s);
aliguoriff8d81d2008-10-24 22:10:31 +0000105 return NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000106 }
107
aliguori17e90972008-10-24 14:11:41 +0000108 socket_set_nonblock(s->fd);
aliguori34c9dd82008-10-13 03:14:31 +0000109
aliguori731b0362009-03-05 23:01:42 +0000110 if (!detach)
111 migrate_fd_monitor_suspend(s);
aliguori34c9dd82008-10-13 03:14:31 +0000112
113 do {
114 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
115 if (ret == -1)
aliguori065e2812008-11-11 16:46:33 +0000116 ret = -(s->get_error(s));
aliguori34c9dd82008-10-13 03:14:31 +0000117
aliguoric1d36662008-10-24 21:55:17 +0000118 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
aliguori34c9dd82008-10-13 03:14:31 +0000119 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
120 } while (ret == -EINTR);
121
aliguoric1d36662008-10-24 21:55:17 +0000122 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
aliguori34c9dd82008-10-13 03:14:31 +0000123 dprintf("connect failed\n");
124 close(s->fd);
125 qemu_free(s);
aliguori88d2d9e2008-10-24 22:08:22 +0000126 return NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000127 } else if (ret >= 0)
aliguori065e2812008-11-11 16:46:33 +0000128 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +0000129
130 return &s->mig_state;
131}
132
133static void tcp_accept_incoming_migration(void *opaque)
134{
135 struct sockaddr_in addr;
136 socklen_t addrlen = sizeof(addr);
137 int s = (unsigned long)opaque;
138 QEMUFile *f;
139 int c, ret;
140
141 do {
142 c = accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000143 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000144
145 dprintf("accepted migration\n");
146
147 if (c == -1) {
148 fprintf(stderr, "could not accept migration connection\n");
149 return;
150 }
151
aliguoric1d36662008-10-24 21:55:17 +0000152 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000153 if (f == NULL) {
154 fprintf(stderr, "could not qemu_fopen socket\n");
155 goto out;
156 }
157
158 vm_stop(0); /* just in case */
159 ret = qemu_loadvm_state(f);
160 if (ret < 0) {
161 fprintf(stderr, "load of migration failed\n");
162 goto out_fopen;
163 }
164 qemu_announce_self();
165 dprintf("successfully loaded vm state\n");
166
167 /* we've successfully migrated, close the server socket */
168 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
169 close(s);
170
171 vm_start();
172
173out_fopen:
174 qemu_fclose(f);
175out:
176 close(c);
177}
178
179int tcp_start_incoming_migration(const char *host_port)
180{
181 struct sockaddr_in addr;
182 int val;
183 int s;
184
185 if (parse_host_port(&addr, host_port) < 0) {
186 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
187 return -EINVAL;
188 }
189
190 s = socket(PF_INET, SOCK_STREAM, 0);
191 if (s == -1)
aliguoric1d36662008-10-24 21:55:17 +0000192 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000193
194 val = 1;
195 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
196
197 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
198 goto err;
199
200 if (listen(s, 1) == -1)
201 goto err;
202
203 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
204 (void *)(unsigned long)s);
205
206 return 0;
207
208err:
209 close(s);
aliguoric1d36662008-10-24 21:55:17 +0000210 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000211}