blob: d6feb237ed1386b47493fb97b6693003f24eb291 [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"
aliguori34c9dd82008-10-13 03:14:31 +000018#include "buffered_file.h"
19#include "block.h"
20
21//#define DEBUG_MIGRATION_TCP
22
aliguori34c9dd82008-10-13 03:14:31 +000023#ifdef DEBUG_MIGRATION_TCP
malcd0f2c4c2010-02-07 02:03:50 +030024#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000025 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
26#else
malcd0f2c4c2010-02-07 02:03:50 +030027#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000028 do { } while (0)
29#endif
30
Juan Quintela22f00a42010-05-11 15:56:35 +020031static int socket_errno(MigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000032{
aliguori8ad9fa52008-11-12 22:29:11 +000033 return socket_error();
aliguori34c9dd82008-10-13 03:14:31 +000034}
35
Juan Quintela22f00a42010-05-11 15:56:35 +020036static int socket_write(MigrationState *s, const void * buf, size_t size)
aliguori34c9dd82008-10-13 03:14:31 +000037{
aliguori065e2812008-11-11 16:46:33 +000038 return send(s->fd, buf, size, 0);
aliguori34c9dd82008-10-13 03:14:31 +000039}
40
Juan Quintela22f00a42010-05-11 15:56:35 +020041static int tcp_close(MigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000042{
malcd0f2c4c2010-02-07 02:03:50 +030043 DPRINTF("tcp_close\n");
aliguori34c9dd82008-10-13 03:14:31 +000044 if (s->fd != -1) {
aliguoriff8d81d2008-10-24 22:10:31 +000045 close(s->fd);
46 s->fd = -1;
aliguori34c9dd82008-10-13 03:14:31 +000047 }
48 return 0;
49}
50
aliguori34c9dd82008-10-13 03:14:31 +000051
52static void tcp_wait_for_connect(void *opaque)
53{
Juan Quintela22f00a42010-05-11 15:56:35 +020054 MigrationState *s = opaque;
aliguori34c9dd82008-10-13 03:14:31 +000055 int val, ret;
blueswir14761a482008-10-25 11:25:48 +000056 socklen_t valsize = sizeof(val);
aliguori34c9dd82008-10-13 03:14:31 +000057
malcd0f2c4c2010-02-07 02:03:50 +030058 DPRINTF("connect completed\n");
aliguori34c9dd82008-10-13 03:14:31 +000059 do {
malc0a656f52009-05-21 05:26:23 +040060 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
aliguori065e2812008-11-11 16:46:33 +000061 } while (ret == -1 && (s->get_error(s)) == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +000062
63 if (ret < 0) {
aliguori065e2812008-11-11 16:46:33 +000064 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000065 return;
66 }
67
68 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
69
70 if (val == 0)
aliguori065e2812008-11-11 16:46:33 +000071 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +000072 else {
malcd0f2c4c2010-02-07 02:03:50 +030073 DPRINTF("error connecting %d\n", val);
aliguori065e2812008-11-11 16:46:33 +000074 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000075 }
76}
77
Juan Quintela22f00a42010-05-11 15:56:35 +020078MigrationState *tcp_start_outgoing_migration(Monitor *mon,
Jan Kiszkaf327aa02009-11-30 18:21:21 +010079 const char *host_port,
aliguoriff8d81d2008-10-24 22:10:31 +000080 int64_t bandwidth_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020081 int detach,
82 int blk,
83 int inc)
aliguori34c9dd82008-10-13 03:14:31 +000084{
85 struct sockaddr_in addr;
Juan Quintela22f00a42010-05-11 15:56:35 +020086 MigrationState *s;
aliguori34c9dd82008-10-13 03:14:31 +000087 int ret;
88
89 if (parse_host_port(&addr, host_port) < 0)
90 return NULL;
91
Anthony Liguori7267c092011-08-20 22:09:37 -050092 s = g_malloc0(sizeof(*s));
aliguori34c9dd82008-10-13 03:14:31 +000093
aliguori065e2812008-11-11 16:46:33 +000094 s->get_error = socket_errno;
95 s->write = socket_write;
96 s->close = tcp_close;
Juan Quintela3f77fc52010-05-11 15:51:36 +020097 s->cancel = migrate_fd_cancel;
98 s->get_status = migrate_fd_get_status;
99 s->release = migrate_fd_release;
aliguori34c9dd82008-10-13 03:14:31 +0000100
Juan Quintela3f77fc52010-05-11 15:51:36 +0200101 s->blk = blk;
102 s->shared = inc;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200103
aliguori34c9dd82008-10-13 03:14:31 +0000104 s->state = MIG_STATE_ACTIVE;
Jan Kiszkaf327aa02009-11-30 18:21:21 +0100105 s->mon = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000106 s->bandwidth_limit = bandwidth_limit;
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100107 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +0000108 if (s->fd == -1) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500109 g_free(s);
aliguoriff8d81d2008-10-24 22:10:31 +0000110 return NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000111 }
112
aliguori17e90972008-10-24 14:11:41 +0000113 socket_set_nonblock(s->fd);
aliguori34c9dd82008-10-13 03:14:31 +0000114
Jan Kiszkaf327aa02009-11-30 18:21:21 +0100115 if (!detach) {
116 migrate_fd_monitor_suspend(s, mon);
117 }
aliguori34c9dd82008-10-13 03:14:31 +0000118
119 do {
120 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
121 if (ret == -1)
aliguori065e2812008-11-11 16:46:33 +0000122 ret = -(s->get_error(s));
aliguori34c9dd82008-10-13 03:14:31 +0000123
aliguoric1d36662008-10-24 21:55:17 +0000124 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
aliguori34c9dd82008-10-13 03:14:31 +0000125 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
126 } while (ret == -EINTR);
127
aliguoric1d36662008-10-24 21:55:17 +0000128 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
malcd0f2c4c2010-02-07 02:03:50 +0300129 DPRINTF("connect failed\n");
Yoshiaki Tamura304e3a72010-06-10 06:50:10 +0900130 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +0000131 } else if (ret >= 0)
aliguori065e2812008-11-11 16:46:33 +0000132 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +0000133
Juan Quintela7be43632010-05-11 15:18:38 +0200134 return s;
aliguori34c9dd82008-10-13 03:14:31 +0000135}
136
137static void tcp_accept_incoming_migration(void *opaque)
138{
139 struct sockaddr_in addr;
140 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100141 int s = (intptr_t)opaque;
aliguori34c9dd82008-10-13 03:14:31 +0000142 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200143 int c;
aliguori34c9dd82008-10-13 03:14:31 +0000144
145 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100146 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000147 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000148
malcd0f2c4c2010-02-07 02:03:50 +0300149 DPRINTF("accepted migration\n");
aliguori34c9dd82008-10-13 03:14:31 +0000150
151 if (c == -1) {
152 fprintf(stderr, "could not accept migration connection\n");
Shahar Havivid092c102010-07-24 13:03:07 +0300153 goto out2;
aliguori34c9dd82008-10-13 03:14:31 +0000154 }
155
aliguoric1d36662008-10-24 21:55:17 +0000156 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000157 if (f == NULL) {
158 fprintf(stderr, "could not qemu_fopen socket\n");
159 goto out;
160 }
161
Juan Quintela511c0232010-06-09 14:10:55 +0200162 process_incoming_migration(f);
aliguori34c9dd82008-10-13 03:14:31 +0000163 qemu_fclose(f);
164out:
Shahar Havivid092c102010-07-24 13:03:07 +0300165 close(c);
166out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100167 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
168 close(s);
aliguori34c9dd82008-10-13 03:14:31 +0000169}
170
171int 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
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100182 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +0000183 if (s == -1)
aliguoric1d36662008-10-24 21:55:17 +0000184 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000185
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,
Stefan Weile0efb992011-02-23 19:09:16 +0100196 (void *)(intptr_t)s);
aliguori34c9dd82008-10-13 03:14:31 +0000197
198 return 0;
199
200err:
201 close(s);
aliguoric1d36662008-10-24 21:55:17 +0000202 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000203}