qed: Consistency check support
[qemu.git] / migration-tcp.c
blobb55f419b654f51de8309db59c43020194f632460
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 "sysemu.h"
19 #include "buffered_file.h"
20 #include "block.h"
22 //#define DEBUG_MIGRATION_TCP
24 #ifdef DEBUG_MIGRATION_TCP
25 #define DPRINTF(fmt, ...) \
26 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27 #else
28 #define DPRINTF(fmt, ...) \
29 do { } while (0)
30 #endif
32 static int socket_errno(FdMigrationState *s)
34 return socket_error();
37 static int socket_write(FdMigrationState *s, const void * buf, size_t size)
39 return send(s->fd, buf, size, 0);
42 static int tcp_close(FdMigrationState *s)
44 DPRINTF("tcp_close\n");
45 if (s->fd != -1) {
46 close(s->fd);
47 s->fd = -1;
49 return 0;
53 static void tcp_wait_for_connect(void *opaque)
55 FdMigrationState *s = opaque;
56 int val, ret;
57 socklen_t valsize = sizeof(val);
59 DPRINTF("connect completed\n");
60 do {
61 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
62 } while (ret == -1 && (s->get_error(s)) == EINTR);
64 if (ret < 0) {
65 migrate_fd_error(s);
66 return;
69 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
71 if (val == 0)
72 migrate_fd_connect(s);
73 else {
74 DPRINTF("error connecting %d\n", val);
75 migrate_fd_error(s);
79 MigrationState *tcp_start_outgoing_migration(Monitor *mon,
80 const char *host_port,
81 int64_t bandwidth_limit,
82 int detach,
83 int blk,
84 int inc)
86 struct sockaddr_in addr;
87 FdMigrationState *s;
88 int ret;
90 if (parse_host_port(&addr, host_port) < 0)
91 return NULL;
93 s = qemu_mallocz(sizeof(*s));
95 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;
102 s->mig_state.blk = blk;
103 s->mig_state.shared = inc;
105 s->state = MIG_STATE_ACTIVE;
106 s->mon = NULL;
107 s->bandwidth_limit = bandwidth_limit;
108 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
109 if (s->fd == -1) {
110 qemu_free(s);
111 return NULL;
114 socket_set_nonblock(s->fd);
116 if (!detach) {
117 migrate_fd_monitor_suspend(s, mon);
120 do {
121 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
122 if (ret == -1)
123 ret = -(s->get_error(s));
125 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
126 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
127 } while (ret == -EINTR);
129 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
130 DPRINTF("connect failed\n");
131 migrate_fd_error(s);
132 } else if (ret >= 0)
133 migrate_fd_connect(s);
135 return &s->mig_state;
138 static void tcp_accept_incoming_migration(void *opaque)
140 struct sockaddr_in addr;
141 socklen_t addrlen = sizeof(addr);
142 int s = (unsigned long)opaque;
143 QEMUFile *f;
144 int c;
146 do {
147 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
148 } while (c == -1 && socket_error() == EINTR);
150 DPRINTF("accepted migration\n");
152 if (c == -1) {
153 fprintf(stderr, "could not accept migration connection\n");
154 goto out2;
157 f = qemu_fopen_socket(c);
158 if (f == NULL) {
159 fprintf(stderr, "could not qemu_fopen socket\n");
160 goto out;
163 process_incoming_migration(f);
164 qemu_fclose(f);
165 out:
166 close(c);
167 out2:
168 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
169 close(s);
172 int tcp_start_incoming_migration(const char *host_port)
174 struct sockaddr_in addr;
175 int val;
176 int s;
178 if (parse_host_port(&addr, host_port) < 0) {
179 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
180 return -EINVAL;
183 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
184 if (s == -1)
185 return -socket_error();
187 val = 1;
188 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
190 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
191 goto err;
193 if (listen(s, 1) == -1)
194 goto err;
196 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
197 (void *)(unsigned long)s);
199 return 0;
201 err:
202 close(s);
203 return -socket_error();