blob: 35a57818238b1ea5290bf04a8a494245722cface [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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori34c9dd82008-10-13 03:14:31 +000014 */
15
16#include "qemu-common.h"
17#include "qemu_socket.h"
18#include "migration.h"
19#include "qemu-char.h"
aliguori34c9dd82008-10-13 03:14:31 +000020#include "buffered_file.h"
21#include "block.h"
22
23//#define DEBUG_MIGRATION_TCP
24
aliguori34c9dd82008-10-13 03:14:31 +000025#ifdef DEBUG_MIGRATION_TCP
malcd0f2c4c2010-02-07 02:03:50 +030026#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000027 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
28#else
malcd0f2c4c2010-02-07 02:03:50 +030029#define DPRINTF(fmt, ...) \
aliguori34c9dd82008-10-13 03:14:31 +000030 do { } while (0)
31#endif
32
Juan Quintela22f00a42010-05-11 15:56:35 +020033static int socket_errno(MigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000034{
aliguori8ad9fa52008-11-12 22:29:11 +000035 return socket_error();
aliguori34c9dd82008-10-13 03:14:31 +000036}
37
Juan Quintela22f00a42010-05-11 15:56:35 +020038static int socket_write(MigrationState *s, const void * buf, size_t size)
aliguori34c9dd82008-10-13 03:14:31 +000039{
aliguori065e2812008-11-11 16:46:33 +000040 return send(s->fd, buf, size, 0);
aliguori34c9dd82008-10-13 03:14:31 +000041}
42
Juan Quintela22f00a42010-05-11 15:56:35 +020043static int tcp_close(MigrationState *s)
aliguori34c9dd82008-10-13 03:14:31 +000044{
Eduardo Habkost61a58722011-11-10 10:41:47 -020045 int r = 0;
malcd0f2c4c2010-02-07 02:03:50 +030046 DPRINTF("tcp_close\n");
aliguori34c9dd82008-10-13 03:14:31 +000047 if (s->fd != -1) {
Eduardo Habkost61a58722011-11-10 10:41:47 -020048 if (close(s->fd) < 0) {
49 r = -errno;
50 }
aliguoriff8d81d2008-10-24 22:10:31 +000051 s->fd = -1;
aliguori34c9dd82008-10-13 03:14:31 +000052 }
Eduardo Habkost61a58722011-11-10 10:41:47 -020053 return r;
aliguori34c9dd82008-10-13 03:14:31 +000054}
55
aliguori34c9dd82008-10-13 03:14:31 +000056static void tcp_wait_for_connect(void *opaque)
57{
Juan Quintela22f00a42010-05-11 15:56:35 +020058 MigrationState *s = opaque;
aliguori34c9dd82008-10-13 03:14:31 +000059 int val, ret;
blueswir14761a482008-10-25 11:25:48 +000060 socklen_t valsize = sizeof(val);
aliguori34c9dd82008-10-13 03:14:31 +000061
malcd0f2c4c2010-02-07 02:03:50 +030062 DPRINTF("connect completed\n");
aliguori34c9dd82008-10-13 03:14:31 +000063 do {
malc0a656f52009-05-21 05:26:23 +040064 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
Juan Quintelaefab4712011-02-23 11:52:12 +010065 } while (ret == -1 && (socket_error()) == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +000066
67 if (ret < 0) {
aliguori065e2812008-11-11 16:46:33 +000068 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000069 return;
70 }
71
72 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
73
74 if (val == 0)
aliguori065e2812008-11-11 16:46:33 +000075 migrate_fd_connect(s);
aliguori34c9dd82008-10-13 03:14:31 +000076 else {
malcd0f2c4c2010-02-07 02:03:50 +030077 DPRINTF("error connecting %d\n", val);
aliguori065e2812008-11-11 16:46:33 +000078 migrate_fd_error(s);
aliguori34c9dd82008-10-13 03:14:31 +000079 }
80}
81
Juan Quintela07af4452010-05-11 22:27:45 +020082int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
aliguori34c9dd82008-10-13 03:14:31 +000083{
84 struct sockaddr_in addr;
aliguori34c9dd82008-10-13 03:14:31 +000085 int ret;
86
Juan Quintela07af4452010-05-11 22:27:45 +020087 ret = parse_host_port(&addr, host_port);
88 if (ret < 0) {
89 return ret;
90 }
Juan Quintelaee86c612011-02-23 20:44:29 +010091
aliguori065e2812008-11-11 16:46:33 +000092 s->get_error = socket_errno;
93 s->write = socket_write;
94 s->close = tcp_close;
aliguori34c9dd82008-10-13 03:14:31 +000095
Kevin Wolf40ff6d72009-12-02 12:24:42 +010096 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
aliguori34c9dd82008-10-13 03:14:31 +000097 if (s->fd == -1) {
Juan Quintelaee86c612011-02-23 20:44:29 +010098 DPRINTF("Unable to open socket");
Juan Quintela8414ff32011-02-23 19:56:52 +010099 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000100 }
101
aliguori17e90972008-10-24 14:11:41 +0000102 socket_set_nonblock(s->fd);
aliguori34c9dd82008-10-13 03:14:31 +0000103
aliguori34c9dd82008-10-13 03:14:31 +0000104 do {
105 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
Juan Quintela8414ff32011-02-23 19:56:52 +0100106 if (ret == -1) {
107 ret = -socket_error();
108 }
109 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
aliguori34c9dd82008-10-13 03:14:31 +0000110 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
Juan Quintela8414ff32011-02-23 19:56:52 +0100111 return 0;
112 }
aliguori34c9dd82008-10-13 03:14:31 +0000113 } while (ret == -EINTR);
114
Juan Quintela8414ff32011-02-23 19:56:52 +0100115 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300116 DPRINTF("connect failed\n");
Yoshiaki Tamura304e3a72010-06-10 06:50:10 +0900117 migrate_fd_error(s);
Juan Quintela8414ff32011-02-23 19:56:52 +0100118 return ret;
119 }
120 migrate_fd_connect(s);
Juan Quintela07af4452010-05-11 22:27:45 +0200121 return 0;
aliguori34c9dd82008-10-13 03:14:31 +0000122}
123
124static void tcp_accept_incoming_migration(void *opaque)
125{
126 struct sockaddr_in addr;
127 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100128 int s = (intptr_t)opaque;
aliguori34c9dd82008-10-13 03:14:31 +0000129 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200130 int c;
aliguori34c9dd82008-10-13 03:14:31 +0000131
132 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100133 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000134 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000135
malcd0f2c4c2010-02-07 02:03:50 +0300136 DPRINTF("accepted migration\n");
aliguori34c9dd82008-10-13 03:14:31 +0000137
138 if (c == -1) {
139 fprintf(stderr, "could not accept migration connection\n");
Shahar Havivid092c102010-07-24 13:03:07 +0300140 goto out2;
aliguori34c9dd82008-10-13 03:14:31 +0000141 }
142
aliguoric1d36662008-10-24 21:55:17 +0000143 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000144 if (f == NULL) {
145 fprintf(stderr, "could not qemu_fopen socket\n");
146 goto out;
147 }
148
Juan Quintela511c0232010-06-09 14:10:55 +0200149 process_incoming_migration(f);
aliguori34c9dd82008-10-13 03:14:31 +0000150 qemu_fclose(f);
151out:
Shahar Havivid092c102010-07-24 13:03:07 +0300152 close(c);
153out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100154 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
155 close(s);
aliguori34c9dd82008-10-13 03:14:31 +0000156}
157
158int tcp_start_incoming_migration(const char *host_port)
159{
160 struct sockaddr_in addr;
161 int val;
162 int s;
163
Juan Quintelaee86c612011-02-23 20:44:29 +0100164 DPRINTF("Attempting to start an incoming migration\n");
165
aliguori34c9dd82008-10-13 03:14:31 +0000166 if (parse_host_port(&addr, host_port) < 0) {
167 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
168 return -EINVAL;
169 }
170
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100171 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
Juan Quintelaee86c612011-02-23 20:44:29 +0100172 if (s == -1) {
aliguoric1d36662008-10-24 21:55:17 +0000173 return -socket_error();
Juan Quintelaee86c612011-02-23 20:44:29 +0100174 }
aliguori34c9dd82008-10-13 03:14:31 +0000175
176 val = 1;
177 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
178
Juan Quintelaee86c612011-02-23 20:44:29 +0100179 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
aliguori34c9dd82008-10-13 03:14:31 +0000180 goto err;
Juan Quintelaee86c612011-02-23 20:44:29 +0100181 }
182 if (listen(s, 1) == -1) {
aliguori34c9dd82008-10-13 03:14:31 +0000183 goto err;
Juan Quintelaee86c612011-02-23 20:44:29 +0100184 }
aliguori34c9dd82008-10-13 03:14:31 +0000185
186 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100187 (void *)(intptr_t)s);
aliguori34c9dd82008-10-13 03:14:31 +0000188
189 return 0;
190
191err:
192 close(s);
aliguoric1d36662008-10-24 21:55:17 +0000193 return -socket_error();
aliguori34c9dd82008-10-13 03:14:31 +0000194}