blob: b55f419b654f51de8309db59c43020194f632460 [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
malcd0f2c4c2010-02-07 02:03:50 +030025#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000026 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27#else
malcd0f2c4c2010-02-07 02:03:50 +030028#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000029 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{
malcd0f2c4c2010-02-07 02:03:50 +030044 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
malcd0f2c4c2010-02-07 02:03:50 +030059 DPRINTF("connect completed\n");
aliguori34c9dd82008-10-13 03:14:31 +000060 do {
malc0a656f52009-05-21 05:26:23 +040061 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &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 {
malcd0f2c4c2010-02-07 02:03:50 +030074 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
Jan Kiszkaf327aa02009-11-30 18:21:21 +010079MigrationState *tcp_start_outgoing_migration(Monitor *mon,
80 const char *host_port,
aliguoriff8d81d2008-10-24 22:10:31 +000081 int64_t bandwidth_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +020082 int detach,
83 int blk,
84 int inc)
aliguori34c9dd82008-10-13 03:14:31 +000085{
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));
aliguori34c9dd82008-10-13 03:14:31 +000094
aliguori065e2812008-11-11 16:46:33 +000095 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;
aliguori34c9dd82008-10-13 03:14:31 +0000101
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200102 s->mig_state.blk = blk;
103 s->mig_state.shared = inc;
104
aliguori34c9dd82008-10-13 03:14:31 +0000105 s->state = MIG_STATE_ACTIVE;
Jan Kiszkaf327aa02009-11-30 18:21:21 +0100106 s->mon = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000107 s->bandwidth_limit = bandwidth_limit;
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100108 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +0000109 if (s->fd == -1) {
110 qemu_free(s);
aliguoriff8d81d2008-10-24 22:10:31 +0000111 return NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000112 }
113
aliguori17e90972008-10-24 14:11:41 +0000114 socket_set_nonblock(s->fd);
aliguori34c9dd82008-10-13 03:14:31 +0000115
Jan Kiszkaf327aa02009-11-30 18:21:21 +0100116 if (!detach) {
117 migrate_fd_monitor_suspend(s, mon);
118 }
aliguori34c9dd82008-10-13 03:14:31 +0000119
120 do {
121 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
122 if (ret == -1)
aliguori065e2812008-11-11 16:46:33 +0000123 ret = -(s->get_error(s));
aliguori34c9dd82008-10-13 03:14:31 +0000124
aliguoric1d36662008-10-24 21:55:17 +0000125 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
aliguori34c9dd82008-10-13 03:14:31 +0000126 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
127 } while (ret == -EINTR);
128
aliguoric1d36662008-10-24 21:55:17 +0000129 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
malcd0f2c4c2010-02-07 02:03:50 +0300130 DPRINTF("connect failed\n");
Yoshiaki Tamura304e3a72010-06-10 06:50:10 +0900131 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +0000132 } else if (ret >= 0)
aliguori065e2812008-11-11 16:46:33 +0000133 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +0000134
135 return &s->mig_state;
136}
137
138static 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;
Juan Quintela511c0232010-06-09 14:10:55 +0200144 int c;
aliguori34c9dd82008-10-13 03:14:31 +0000145
146 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100147 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000148 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000149
malcd0f2c4c2010-02-07 02:03:50 +0300150 DPRINTF("accepted migration\n");
aliguori34c9dd82008-10-13 03:14:31 +0000151
152 if (c == -1) {
153 fprintf(stderr, "could not accept migration connection\n");
Shahar Havivid092c102010-07-24 13:03:07 +0300154 goto out2;
aliguori34c9dd82008-10-13 03:14:31 +0000155 }
156
aliguoric1d36662008-10-24 21:55:17 +0000157 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000158 if (f == NULL) {
159 fprintf(stderr, "could not qemu_fopen socket\n");
160 goto out;
161 }
162
Juan Quintela511c0232010-06-09 14:10:55 +0200163 process_incoming_migration(f);
aliguori34c9dd82008-10-13 03:14:31 +0000164 qemu_fclose(f);
165out:
Shahar Havivid092c102010-07-24 13:03:07 +0300166 close(c);
167out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100168 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
169 close(s);
aliguori34c9dd82008-10-13 03:14:31 +0000170}
171
172int tcp_start_incoming_migration(const char *host_port)
173{
174 struct sockaddr_in addr;
175 int val;
176 int s;
177
178 if (parse_host_port(&addr, host_port) < 0) {
179 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
180 return -EINVAL;
181 }
182
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100183 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +0000184 if (s == -1)
aliguoric1d36662008-10-24 21:55:17 +0000185 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000186
187 val = 1;
188 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
189
190 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
191 goto err;
192
193 if (listen(s, 1) == -1)
194 goto err;
195
196 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
197 (void *)(unsigned long)s);
198
199 return 0;
200
201err:
202 close(s);
aliguoric1d36662008-10-24 21:55:17 +0000203 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000204}