migration: move migrate_new to do_migrate
[qemu.git] / migration-tcp.c
blobf6b22882013d27370c8427fc7ecb4ab1a979558e
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "qemu_socket.h"
16 #include "migration.h"
17 #include "qemu-char.h"
18 #include "buffered_file.h"
19 #include "block.h"
21 //#define DEBUG_MIGRATION_TCP
23 #ifdef DEBUG_MIGRATION_TCP
24 #define DPRINTF(fmt, ...) \
25 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) \
28 do { } while (0)
29 #endif
31 static int socket_errno(MigrationState *s)
33 return socket_error();
36 static int socket_write(MigrationState *s, const void * buf, size_t size)
38 return send(s->fd, buf, size, 0);
41 static int tcp_close(MigrationState *s)
43 DPRINTF("tcp_close\n");
44 if (s->fd != -1) {
45 close(s->fd);
46 s->fd = -1;
48 return 0;
52 static void tcp_wait_for_connect(void *opaque)
54 MigrationState *s = opaque;
55 int val, ret;
56 socklen_t valsize = sizeof(val);
58 DPRINTF("connect completed\n");
59 do {
60 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
61 } while (ret == -1 && (s->get_error(s)) == EINTR);
63 if (ret < 0) {
64 migrate_fd_error(s);
65 return;
68 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
70 if (val == 0)
71 migrate_fd_connect(s);
72 else {
73 DPRINTF("error connecting %d\n", val);
74 migrate_fd_error(s);
78 int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
80 struct sockaddr_in addr;
81 int ret;
83 ret = parse_host_port(&addr, host_port);
84 if (ret < 0) {
85 return ret;
87 s->get_error = socket_errno;
88 s->write = socket_write;
89 s->close = tcp_close;
91 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
92 if (s->fd == -1) {
93 return -1;
96 socket_set_nonblock(s->fd);
98 do {
99 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
100 if (ret == -1)
101 ret = -(s->get_error(s));
103 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
104 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
105 } while (ret == -EINTR);
107 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
108 DPRINTF("connect failed\n");
109 migrate_fd_error(s);
110 } else if (ret >= 0)
111 migrate_fd_connect(s);
113 return 0;
116 static void tcp_accept_incoming_migration(void *opaque)
118 struct sockaddr_in addr;
119 socklen_t addrlen = sizeof(addr);
120 int s = (intptr_t)opaque;
121 QEMUFile *f;
122 int c;
124 do {
125 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
126 } while (c == -1 && socket_error() == EINTR);
128 DPRINTF("accepted migration\n");
130 if (c == -1) {
131 fprintf(stderr, "could not accept migration connection\n");
132 goto out2;
135 f = qemu_fopen_socket(c);
136 if (f == NULL) {
137 fprintf(stderr, "could not qemu_fopen socket\n");
138 goto out;
141 process_incoming_migration(f);
142 qemu_fclose(f);
143 out:
144 close(c);
145 out2:
146 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
147 close(s);
150 int tcp_start_incoming_migration(const char *host_port)
152 struct sockaddr_in addr;
153 int val;
154 int s;
156 if (parse_host_port(&addr, host_port) < 0) {
157 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
158 return -EINVAL;
161 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
162 if (s == -1)
163 return -socket_error();
165 val = 1;
166 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
168 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
169 goto err;
171 if (listen(s, 1) == -1)
172 goto err;
174 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
175 (void *)(intptr_t)s);
177 return 0;
179 err:
180 close(s);
181 return -socket_error();