blob: 440804db755136b22e0eaabec99541d9298e1ce7 [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
Amos Kongd5c5dac2012-05-11 00:28:35 +080082int tcp_start_outgoing_migration(MigrationState *s, const char *host_port,
83 Error **errp)
aliguori34c9dd82008-10-13 03:14:31 +000084{
aliguori065e2812008-11-11 16:46:33 +000085 s->get_error = socket_errno;
86 s->write = socket_write;
87 s->close = tcp_close;
aliguori34c9dd82008-10-13 03:14:31 +000088
Amos Kongd5c5dac2012-05-11 00:28:35 +080089 s->fd = inet_connect(host_port, false, errp);
aliguori34c9dd82008-10-13 03:14:31 +000090
Amos Kongd5c5dac2012-05-11 00:28:35 +080091 if (!error_is_set(errp)) {
92 migrate_fd_connect(s);
93 } else if (error_is_type(*errp, QERR_SOCKET_CONNECT_IN_PROGRESS)) {
94 DPRINTF("connect in progress\n");
95 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
96 } else if (error_is_type(*errp, QERR_SOCKET_CREATE_FAILED)) {
97 DPRINTF("connect failed\n");
98 return -1;
99 } else if (error_is_type(*errp, QERR_SOCKET_CONNECT_FAILED)) {
malcd0f2c4c2010-02-07 02:03:50 +0300100 DPRINTF("connect failed\n");
Yoshiaki Tamura304e3a72010-06-10 06:50:10 +0900101 migrate_fd_error(s);
Amos Kongd5c5dac2012-05-11 00:28:35 +0800102 return -1;
103 } else {
104 DPRINTF("unknown error\n");
105 return -1;
Juan Quintela8414ff32011-02-23 19:56:52 +0100106 }
Amos Kongd5c5dac2012-05-11 00:28:35 +0800107
Juan Quintela07af4452010-05-11 22:27:45 +0200108 return 0;
aliguori34c9dd82008-10-13 03:14:31 +0000109}
110
111static void tcp_accept_incoming_migration(void *opaque)
112{
113 struct sockaddr_in addr;
114 socklen_t addrlen = sizeof(addr);
Stefan Weile0efb992011-02-23 19:09:16 +0100115 int s = (intptr_t)opaque;
aliguori34c9dd82008-10-13 03:14:31 +0000116 QEMUFile *f;
Juan Quintela511c0232010-06-09 14:10:55 +0200117 int c;
aliguori34c9dd82008-10-13 03:14:31 +0000118
119 do {
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100120 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
aliguoric1d36662008-10-24 21:55:17 +0000121 } while (c == -1 && socket_error() == EINTR);
aliguori34c9dd82008-10-13 03:14:31 +0000122
malcd0f2c4c2010-02-07 02:03:50 +0300123 DPRINTF("accepted migration\n");
aliguori34c9dd82008-10-13 03:14:31 +0000124
125 if (c == -1) {
126 fprintf(stderr, "could not accept migration connection\n");
Shahar Havivid092c102010-07-24 13:03:07 +0300127 goto out2;
aliguori34c9dd82008-10-13 03:14:31 +0000128 }
129
aliguoric1d36662008-10-24 21:55:17 +0000130 f = qemu_fopen_socket(c);
aliguori34c9dd82008-10-13 03:14:31 +0000131 if (f == NULL) {
132 fprintf(stderr, "could not qemu_fopen socket\n");
133 goto out;
134 }
135
Juan Quintela511c0232010-06-09 14:10:55 +0200136 process_incoming_migration(f);
aliguori34c9dd82008-10-13 03:14:31 +0000137 qemu_fclose(f);
138out:
Shahar Havivid092c102010-07-24 13:03:07 +0300139 close(c);
140out2:
Juan Quintelacfaf6d32010-03-10 00:10:35 +0100141 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
142 close(s);
aliguori34c9dd82008-10-13 03:14:31 +0000143}
144
Amos Kongd5c5dac2012-05-11 00:28:35 +0800145int tcp_start_incoming_migration(const char *host_port, Error **errp)
aliguori34c9dd82008-10-13 03:14:31 +0000146{
aliguori34c9dd82008-10-13 03:14:31 +0000147 int s;
148
Amos Kongd5c5dac2012-05-11 00:28:35 +0800149 s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp);
Juan Quintelaee86c612011-02-23 20:44:29 +0100150
Amos Kongd5c5dac2012-05-11 00:28:35 +0800151 if (s < 0) {
152 return -1;
Juan Quintelaee86c612011-02-23 20:44:29 +0100153 }
aliguori34c9dd82008-10-13 03:14:31 +0000154
155 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100156 (void *)(intptr_t)s);
aliguori34c9dd82008-10-13 03:14:31 +0000157
158 return 0;
aliguori34c9dd82008-10-13 03:14:31 +0000159}