]> git.proxmox.com Git - qemu.git/blame - migration-tcp.c
migration: Make all posible migration functions static
[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
22f00a44 31static int socket_errno(MigrationState *s)
34c9dd8e 32{
8ad9fa5d 33 return socket_error();
34c9dd8e
AL
34}
35
22f00a44 36static int socket_write(MigrationState *s, const void * buf, size_t size)
34c9dd8e 37{
065e2813 38 return send(s->fd, buf, size, 0);
34c9dd8e
AL
39}
40
22f00a44 41static int tcp_close(MigrationState *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{
22f00a44 54 MigrationState *s = opaque;
34c9dd8e 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
22f00a44 78MigrationState *tcp_start_outgoing_migration(Monitor *mon,
f327aa0c 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;
22f00a44 86 MigrationState *s;
34c9dd8e
AL
87 int ret;
88
89 if (parse_host_port(&addr, host_port) < 0)
90 return NULL;
91
0edda1c4 92 s = migrate_new(mon, bandwidth_limit, detach, blk, inc);
34c9dd8e 93
065e2813
AL
94 s->get_error = socket_errno;
95 s->write = socket_write;
96 s->close = tcp_close;
34c9dd8e 97
40ff6d7e 98 s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8e 99 if (s->fd == -1) {
7267c094 100 g_free(s);
ff8d81d8 101 return NULL;
34c9dd8e
AL
102 }
103
17e90973 104 socket_set_nonblock(s->fd);
34c9dd8e 105
34c9dd8e
AL
106 do {
107 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
108 if (ret == -1)
065e2813 109 ret = -(s->get_error(s));
34c9dd8e 110
c1d36665 111 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
34c9dd8e
AL
112 qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
113 } while (ret == -EINTR);
114
c1d36665 115 if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
d0f2c4c6 116 DPRINTF("connect failed\n");
304e3a7c 117 migrate_fd_error(s);
34c9dd8e 118 } else if (ret >= 0)
065e2813 119 migrate_fd_connect(s);
34c9dd8e 120
7be4363a 121 return s;
34c9dd8e
AL
122}
123
124static void tcp_accept_incoming_migration(void *opaque)
125{
126 struct sockaddr_in addr;
127 socklen_t addrlen = sizeof(addr);
e0efb993 128 int s = (intptr_t)opaque;
34c9dd8e 129 QEMUFile *f;
511c0231 130 int c;
34c9dd8e
AL
131
132 do {
40ff6d7e 133 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
c1d36665 134 } while (c == -1 && socket_error() == EINTR);
34c9dd8e 135
d0f2c4c6 136 DPRINTF("accepted migration\n");
34c9dd8e
AL
137
138 if (c == -1) {
139 fprintf(stderr, "could not accept migration connection\n");
d092c108 140 goto out2;
34c9dd8e
AL
141 }
142
c1d36665 143 f = qemu_fopen_socket(c);
34c9dd8e
AL
144 if (f == NULL) {
145 fprintf(stderr, "could not qemu_fopen socket\n");
146 goto out;
147 }
148
511c0231 149 process_incoming_migration(f);
34c9dd8e
AL
150 qemu_fclose(f);
151out:
d092c108
SH
152 close(c);
153out2:
cfaf6d36
JQ
154 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
155 close(s);
34c9dd8e
AL
156}
157
158int tcp_start_incoming_migration(const char *host_port)
159{
160 struct sockaddr_in addr;
161 int val;
162 int s;
163
164 if (parse_host_port(&addr, host_port) < 0) {
165 fprintf(stderr, "invalid host/port combination: %s\n", host_port);
166 return -EINVAL;
167 }
168
40ff6d7e 169 s = qemu_socket(PF_INET, SOCK_STREAM, 0);
34c9dd8e 170 if (s == -1)
c1d36665 171 return -socket_error();
34c9dd8e
AL
172
173 val = 1;
174 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
175
176 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
177 goto err;
178
179 if (listen(s, 1) == -1)
180 goto err;
181
182 qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
e0efb993 183 (void *)(intptr_t)s);
34c9dd8e
AL
184
185 return 0;
186
187err:
188 close(s);
c1d36665 189 return -socket_error();
34c9dd8e 190}