blob: f6b22882013d27370c8427fc7ecb4ab1a979558e [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 Quintela07af4452010-05-11 22:27:45 +020078int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
aliguori34c9dd82008-10-13 03:14:31 +000079{
80 struct sockaddr_in addr;
aliguori34c9dd82008-10-13 03:14:31 +000081 int ret;
82
Juan Quintela07af4452010-05-11 22:27:45 +020083 ret = parse_host_port(&addr, host_port);
84 if (ret < 0) {
85 return ret;
86 }
aliguori065e2812008-11-11 16:46:33 +000087 s->get_error = socket_errno;
88 s->write = socket_write;
89 s->close = tcp_close;
aliguori34c9dd82008-10-13 03:14:31 +000090
Kevin Wolf40ff6d72009-12-02 12:24:42 +010091 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +000092 if (s->fd == -1) {
Juan Quintela07af4452010-05-11 22:27:45 +020093 return -1;
aliguori34c9dd82008-10-13 03:14:31 +000094 }
95
aliguori17e90972008-10-24 14:11:41 +000096 socket_set_nonblock(s->fd);
aliguori34c9dd82008-10-13 03:14:31 +000097
aliguori34c9dd82008-10-13 03:14:31 +000098 do {
99 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
100 if (ret == -1)
aliguori065e2812008-11-11 16:46:33 +0000101 ret = -(s->get_error(s));
aliguori34c9dd82008-10-13 03:14:31 +0000102
aliguoric1d36662008-10-24 21:55:17 +0000103 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
aliguori34c9dd82008-10-13 03:14:31 +0000104 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
105 } while (ret == -EINTR);
106
aliguoric1d36662008-10-24 21:55:17 +0000107 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
malcd0f2c4c2010-02-07 02:03:50 +0300108 DPRINTF("connect failed\n");
Yoshiaki Tamura304e3a72010-06-10 06:50:10 +0900109 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +0000110 } else if (ret >= 0)
aliguori065e2812008-11-11 16:46:33 +0000111 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +0000112
Juan Quintela07af4452010-05-11 22:27:45 +0200113 return 0;
aliguori34c9dd82008-10-13 03:14:31 +0000114}
115
116static void tcp_accept_incoming_migration(void *opaque)
117{
118 struct sockaddr_in addr;
119 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100120 int s = (intptr_t)opaque;
aliguori34c9dd82008-10-13 03:14:31 +0000121 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200122 int c;
aliguori34c9dd82008-10-13 03:14:31 +0000123
124 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100125 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000126 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000127
malcd0f2c4c2010-02-07 02:03:50 +0300128 DPRINTF("accepted migration\n");
aliguori34c9dd82008-10-13 03:14:31 +0000129
130 if (c == -1) {
131 fprintf(stderr, "could not accept migration connection\n");
Shahar Havivid092c102010-07-24 13:03:07 +0300132 goto out2;
aliguori34c9dd82008-10-13 03:14:31 +0000133 }
134
aliguoric1d36662008-10-24 21:55:17 +0000135 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000136 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);
aliguori34c9dd82008-10-13 03:14:31 +0000142 qemu_fclose(f);
143out:
Shahar Havivid092c102010-07-24 13:03:07 +0300144 close(c);
145out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100146 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
147 close(s);
aliguori34c9dd82008-10-13 03:14:31 +0000148}
149
150int tcp_start_incoming_migration(const char *host_port)
151{
152 struct sockaddr_in addr;
153 int val;
154 int s;
155
156 if (parse_host_port(&addr, host_port) < 0) {
157 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
158 return -EINVAL;
159 }
160
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100161 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +0000162 if (s == -1)
aliguoric1d36662008-10-24 21:55:17 +0000163 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000164
165 val = 1;
166 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
167
168 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
169 goto err;
170
171 if (listen(s, 1) == -1)
172 goto err;
173
174 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100175 (void *)(intptr_t)s);
aliguori34c9dd82008-10-13 03:14:31 +0000176
177 return 0;
178
179err:
180 close(s);
aliguoric1d36662008-10-24 21:55:17 +0000181 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000182}