Skip to content
Newer
Older
100644 204 lines (164 sloc) 4.58 KB
34c9dd8 Introduce TCP live migration protocol
aliguori authored
1 /*
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"
19 #include "buffered_file.h"
20 #include "block.h"
21
22 //#define DEBUG_MIGRATION_TCP
23
24 #ifdef DEBUG_MIGRATION_TCP
d0f2c4c @moosotc Do not use dprintf
moosotc authored
25 #define DPRINTF(fmt, ...) \
34c9dd8 Introduce TCP live migration protocol
aliguori authored
26 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27 #else
d0f2c4c @moosotc Do not use dprintf
moosotc authored
28 #define DPRINTF(fmt, ...) \
34c9dd8 Introduce TCP live migration protocol
aliguori authored
29 do { } while (0)
30 #endif
31
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
32 static int socket_errno(FdMigrationState *s)
34c9dd8 Introduce TCP live migration protocol
aliguori authored
33 {
8ad9fa5 Fix brown-paper-bag bugs from live-migration patch (Charles Duffy)
aliguori authored
34 return socket_error();
34c9dd8 Introduce TCP live migration protocol
aliguori authored
35 }
36
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
37 static int socket_write(FdMigrationState *s, const void * buf, size_t size)
34c9dd8 Introduce TCP live migration protocol
aliguori authored
38 {
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
39 return send(s->fd, buf, size, 0);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
40 }
41
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
42 static int tcp_close(FdMigrationState *s)
34c9dd8 Introduce TCP live migration protocol
aliguori authored
43 {
d0f2c4c @moosotc Do not use dprintf
moosotc authored
44 DPRINTF("tcp_close\n");
34c9dd8 Introduce TCP live migration protocol
aliguori authored
45 if (s->fd != -1) {
ff8d81d Fix whitespace in migration code.
aliguori authored
46 close(s->fd);
47 s->fd = -1;
34c9dd8 Introduce TCP live migration protocol
aliguori authored
48 }
49 return 0;
50 }
51
52
53 static void tcp_wait_for_connect(void *opaque)
54 {
55 FdMigrationState *s = opaque;
56 int val, ret;
4761a48 Fix GCC 4 signedness warning
blueswir1 authored
57 socklen_t valsize = sizeof(val);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
58
d0f2c4c @moosotc Do not use dprintf
moosotc authored
59 DPRINTF("connect completed\n");
34c9dd8 Introduce TCP live migration protocol
aliguori authored
60 do {
0a656f5 @moosotc Cast pointer arguments of get/setsockopt, send to void * to keep GCC
moosotc authored
61 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
62 } while (ret == -1 && (s->get_error(s)) == EINTR);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
63
64 if (ret < 0) {
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
65 migrate_fd_error(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
66 return;
67 }
68
69 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
70
71 if (val == 0)
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
72 migrate_fd_connect(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
73 else {
d0f2c4c @moosotc Do not use dprintf
moosotc authored
74 DPRINTF("error connecting %d\n", val);
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
75 migrate_fd_error(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
76 }
77 }
78
f327aa0 @jan-kiszka live migration: Propagate output monitor to callback handler
jan-kiszka authored
79 MigrationState *tcp_start_outgoing_migration(Monitor *mon,
80 const char *host_port,
ff8d81d Fix whitespace in migration code.
aliguori authored
81 int64_t bandwidth_limit,
c163b5c @liranschour Block live migration
liranschour authored
82 int detach,
83 int blk,
84 int inc)
34c9dd8 Introduce TCP live migration protocol
aliguori authored
85 {
86 struct sockaddr_in addr;
87 FdMigrationState *s;
88 int ret;
89
90 if (parse_host_port(&addr, host_port) < 0)
91 return NULL;
92
93 s = qemu_mallocz(sizeof(*s));
94
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
95 s->get_error = socket_errno;
96 s->write = socket_write;
97 s->close = tcp_close;
98 s->mig_state.cancel = migrate_fd_cancel;
99 s->mig_state.get_status = migrate_fd_get_status;
100 s->mig_state.release = migrate_fd_release;
34c9dd8 Introduce TCP live migration protocol
aliguori authored
101
c163b5c @liranschour Block live migration
liranschour authored
102 s->mig_state.blk = blk;
103 s->mig_state.shared = inc;
104
34c9dd8 Introduce TCP live migration protocol
aliguori authored
105 s->state = MIG_STATE_ACTIVE;
f327aa0 @jan-kiszka live migration: Propagate output monitor to callback handler
jan-kiszka authored
106 s->mon = NULL;
34c9dd8 Introduce TCP live migration protocol
aliguori authored
107 s->bandwidth_limit = bandwidth_limit;
40ff6d7 Don't leak file descriptors
Kevin Wolf authored
108 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
109 if (s->fd == -1) {
110 qemu_free(s);
ff8d81d Fix whitespace in migration code.
aliguori authored
111 return NULL;
34c9dd8 Introduce TCP live migration protocol
aliguori authored
112 }
113
17e9097 Fix windows build after migration changes
aliguori authored
114 socket_set_nonblock(s->fd);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
115
f327aa0 @jan-kiszka live migration: Propagate output monitor to callback handler
jan-kiszka authored
116 if (!detach) {
117 migrate_fd_monitor_suspend(s, mon);
118 }
34c9dd8 Introduce TCP live migration protocol
aliguori authored
119
120 do {
121 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
122 if (ret == -1)
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
123 ret = -(s->get_error(s));
34c9dd8 Introduce TCP live migration protocol
aliguori authored
124
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
125 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
34c9dd8 Introduce TCP live migration protocol
aliguori authored
126 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
127 } while (ret == -EINTR);
128
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
129 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
d0f2c4c @moosotc Do not use dprintf
moosotc authored
130 DPRINTF("connect failed\n");
304e3a7 migration-tcp: call migrate_fd_error() instead of close() and free().
Yoshiaki Tamura authored
131 migrate_fd_error(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
132 } else if (ret >= 0)
065e281 Reintroduce migrate-to-exec: support (Charles Duffy)
aliguori authored
133 migrate_fd_connect(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
134
135 return &s->mig_state;
136 }
137
138 static void tcp_accept_incoming_migration(void *opaque)
139 {
140 struct sockaddr_in addr;
141 socklen_t addrlen = sizeof(addr);
142 int s = (unsigned long)opaque;
143 QEMUFile *f;
511c023 @juanquintela Factorize common migration incoming code
juanquintela authored
144 int c;
34c9dd8 Introduce TCP live migration protocol
aliguori authored
145
146 do {
40ff6d7 Don't leak file descriptors
Kevin Wolf authored
147 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
148 } while (c == -1 && socket_error() == EINTR);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
149
d0f2c4c @moosotc Do not use dprintf
moosotc authored
150 DPRINTF("accepted migration\n");
34c9dd8 Introduce TCP live migration protocol
aliguori authored
151
152 if (c == -1) {
153 fprintf(stderr, "could not accept migration connection\n");
154 return;
155 }
156
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
157 f = qemu_fopen_socket(c);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
158 if (f == NULL) {
159 fprintf(stderr, "could not qemu_fopen socket\n");
160 goto out;
161 }
162
511c023 @juanquintela Factorize common migration incoming code
juanquintela authored
163 process_incoming_migration(f);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
164 qemu_fclose(f);
165 out:
cfaf6d3 @juanquintela migration: Clear fd also in error cases
juanquintela authored
166 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
167 close(s);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
168 close(c);
169 }
170
171 int tcp_start_incoming_migration(const char *host_port)
172 {
173 struct sockaddr_in addr;
174 int val;
175 int s;
176
177 if (parse_host_port(&addr, host_port) < 0) {
178 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
179 return -EINVAL;
180 }
181
40ff6d7 Don't leak file descriptors
Kevin Wolf authored
182 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8 Introduce TCP live migration protocol
aliguori authored
183 if (s == -1)
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
184 return -socket_error();
34c9dd8 Introduce TCP live migration protocol
aliguori authored
185
186 val = 1;
187 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
188
189 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
190 goto err;
191
192 if (listen(s, 1) == -1)
193 goto err;
194
195 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
196 (void *)(unsigned long)s);
197
198 return 0;
199
200 err:
201 close(s);
c1d3666 Live migration for Win32 (Hervé Poussineau)
aliguori authored
202 return -socket_error();
34c9dd8 Introduce TCP live migration protocol
aliguori authored
203 }
Something went wrong with that request. Please try again.