qapi: add ordereddict.py helper library
[qemu/mdroth.git] / migration-tcp.c
blobd3d80c97022e2b23484987050afdbebde7a3e2af
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(FdMigrationState *s)
33 return socket_error();
36 static int socket_write(FdMigrationState *s, const void * buf, size_t size)
38 return send(s->fd, buf, size, 0);
41 static int tcp_close(FdMigrationState *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 FdMigrationState *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 MigrationState *tcp_start_outgoing_migration(Monitor *mon,
79 const char *host_port,
80 int64_t bandwidth_limit,
81 int detach,
82 int blk,
83 int inc)
85 struct sockaddr_in addr;
86 FdMigrationState *s;
87 int ret;
89 if (parse_host_port(&addr, host_port) < 0)
90 return NULL;
92 s = qemu_mallocz(sizeof(*s));
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;
101 s->mig_state.blk = blk;
102 s->mig_state.shared = inc;
104 s->state = MIG_STATE_ACTIVE;
105 s->mon = NULL;
106 s->bandwidth_limit = bandwidth_limit;
107 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
108 if (s->fd == -1) {
109 qemu_free(s);
110 return NULL;
113 socket_set_nonblock(s->fd);
115 if (!detach) {
116 migrate_fd_monitor_suspend(s, mon);
119 do {
120 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
121 if (ret == -1)
122 ret = -(s->get_error(s));
124 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
125 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
126 } while (ret == -EINTR);
128 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
129 DPRINTF("connect failed\n");
130 migrate_fd_error(s);
131 } else if (ret >= 0)
132 migrate_fd_connect(s);
134 return &s->mig_state;
137 static void tcp_accept_incoming_migration(void *opaque)
139 struct sockaddr_in addr;
140 socklen_t addrlen = sizeof(addr);
141 int s = (intptr_t)opaque;
142 QEMUFile *f;
143 int c;
145 do {
146 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
147 } while (c == -1 && socket_error() == EINTR);
149 DPRINTF("accepted migration\n");
151 if (c == -1) {
152 fprintf(stderr, "could not accept migration connection\n");
153 goto out2;
156 f = qemu_fopen_socket(c);
157 if (f == NULL) {
158 fprintf(stderr, "could not qemu_fopen socket\n");
159 goto out;
162 process_incoming_migration(f);
163 qemu_fclose(f);
164 out:
165 close(c);
166 out2:
167 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
168 close(s);
171 int tcp_start_incoming_migration(const char *host_port)
173 struct sockaddr_in addr;
174 int val;
175 int s;
177 if (parse_host_port(&addr, host_port) < 0) {
178 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
179 return -EINVAL;
182 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
183 if (s == -1)
184 return -socket_error();
186 val = 1;
187 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
189 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
190 goto err;
192 if (listen(s, 1) == -1)
193 goto err;
195 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
196 (void *)(intptr_t)s);
198 return 0;
200 err:
201 close(s);
202 return -socket_error();