]> git.proxmox.com Git - qemu.git/blame - migration-tcp.c
virtio-serial: Clean up virtser_bus_dev_print() output
[qemu.git] / migration-tcp.c
CommitLineData
34c9dd8e
AL
1/*
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"
34c9dd8e
AL
18#include "buffered_file.h"
19#include "block.h"
20
21//#define DEBUG_MIGRATION_TCP
22
34c9dd8e 23#ifdef DEBUG_MIGRATION_TCP
d0f2c4c6 24#define DPRINTF(fmt, ...) \
34c9dd8e
AL
25 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
26#else
d0f2c4c6 27#define DPRINTF(fmt, ...) \
34c9dd8e
AL
28 do { } while (0)
29#endif
30
065e2813 31static int socket_errno(FdMigrationState *s)
34c9dd8e 32{
8ad9fa5d 33 return socket_error();
34c9dd8e
AL
34}
35
065e2813 36static int socket_write(FdMigrationState *s, const void * buf, size_t size)
34c9dd8e 37{
065e2813 38 return send(s->fd, buf, size, 0);
34c9dd8e
AL
39}
40
065e2813 41static int tcp_close(FdMigrationState *s)
34c9dd8e 42{
d0f2c4c6 43 DPRINTF("tcp_close\n");
34c9dd8e 44 if (s->fd != -1) {
ff8d81d8
AL
45 close(s->fd);
46 s->fd = -1;
34c9dd8e
AL
47 }
48 return 0;
49}
50
34c9dd8e
AL
51
52static void tcp_wait_for_connect(void *opaque)
53{
54 FdMigrationState *s = opaque;
55 int val, ret;
4761a48b 56 socklen_t valsize = sizeof(val);
34c9dd8e 57
d0f2c4c6 58 DPRINTF("connect completed\n");
34c9dd8e 59 do {
0a656f5f 60 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
065e2813 61 } while (ret == -1 && (s->get_error(s)) == EINTR);
34c9dd8e
AL
62
63 if (ret < 0) {
065e2813 64 migrate_fd_error(s);
34c9dd8e
AL
65 return;
66 }
67
68 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
69
70 if (val == 0)
065e2813 71 migrate_fd_connect(s);
34c9dd8e 72 else {
d0f2c4c6 73 DPRINTF("error connecting %d\n", val);
065e2813 74 migrate_fd_error(s);
34c9dd8e 75 }
34c9dd8e
AL
76}
77
f327aa0c
JK
78MigrationState *tcp_start_outgoing_migration(Monitor *mon,
79 const char *host_port,
ff8d81d8 80 int64_t bandwidth_limit,
c163b5ca 81 int detach,
82 int blk,
83 int inc)
34c9dd8e
AL
84{
85 struct sockaddr_in addr;
86 FdMigrationState *s;
87 int ret;
88
89 if (parse_host_port(&addr, host_port) < 0)
90 return NULL;
91
92 s = qemu_mallocz(sizeof(*s));
34c9dd8e 93
065e2813
AL
94 s->get_error = socket_errno;
95 s->write = socket_write;
96 s->close = tcp_close;
97 s->mig_state.cancel = migrate_fd_cancel;
98 s->mig_state.get_status = migrate_fd_get_status;
99 s->mig_state.release = migrate_fd_release;
34c9dd8e 100
c163b5ca 101 s->mig_state.blk = blk;
102 s->mig_state.shared = inc;
103
34c9dd8e 104 s->state = MIG_STATE_ACTIVE;
f327aa0c 105 s->mon = NULL;
34c9dd8e 106 s->bandwidth_limit = bandwidth_limit;
40ff6d7e 107 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8e
AL
108 if (s->fd == -1) {
109 qemu_free(s);
ff8d81d8 110 return NULL;
34c9dd8e
AL
111 }
112
17e90973 113 socket_set_nonblock(s->fd);
34c9dd8e 114
f327aa0c
JK
115 if (!detach) {
116 migrate_fd_monitor_suspend(s, mon);
117 }
34c9dd8e
AL
118
119 do {
120 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
121 if (ret == -1)
065e2813 122 ret = -(s->get_error(s));
34c9dd8e 123
c1d36665 124 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
34c9dd8e
AL
125 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
126 } while (ret == -EINTR);
127
c1d36665 128 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
d0f2c4c6 129 DPRINTF("connect failed\n");
304e3a7c 130 migrate_fd_error(s);
34c9dd8e 131 } else if (ret >= 0)
065e2813 132 migrate_fd_connect(s);
34c9dd8e
AL
133
134 return &s->mig_state;
135}
136
137static void tcp_accept_incoming_migration(void *opaque)
138{
139 struct sockaddr_in addr;
140 socklen_t addrlen = sizeof(addr);
e0efb993 141 int s = (intptr_t)opaque;
34c9dd8e 142 QEMUFile *f;
511c0231 143 int c;
34c9dd8e
AL
144
145 do {
40ff6d7e 146 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
c1d36665 147 } while (c == -1 && socket_error() == EINTR);
34c9dd8e 148
d0f2c4c6 149 DPRINTF("accepted migration\n");
34c9dd8e
AL
150
151 if (c == -1) {
152 fprintf(stderr, "could not accept migration connection\n");
d092c108 153 goto out2;
34c9dd8e
AL
154 }
155
c1d36665 156 f = qemu_fopen_socket(c);
34c9dd8e
AL
157 if (f == NULL) {
158 fprintf(stderr, "could not qemu_fopen socket\n");
159 goto out;
160 }
161
511c0231 162 process_incoming_migration(f);
34c9dd8e
AL
163 qemu_fclose(f);
164out:
d092c108
SH
165 close(c);
166out2:
cfaf6d36
JQ
167 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
168 close(s);
34c9dd8e
AL
169}
170
171int tcp_start_incoming_migration(const char *host_port)
172{
173 struct sockaddr_in addr;
174 int val;
175 int s;
176
177 if (parse_host_port(&addr, host_port) < 0) {
178 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
179 return -EINVAL;
180 }
181
40ff6d7e 182 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8e 183 if (s == -1)
c1d36665 184 return -socket_error();
34c9dd8e
AL
185
186 val = 1;
187 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
188
189 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
190 goto err;
191
192 if (listen(s, 1) == -1)
193 goto err;
194
195 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
e0efb993 196 (void *)(intptr_t)s);
34c9dd8e
AL
197
198 return 0;
199
200err:
201 close(s);
c1d36665 202 return -socket_error();
34c9dd8e 203}