999d4c9aacf842d2dd811bbb1b169e1c3db0e2d1
[sdk/emulator/qemu.git] / migration-tcp.c
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"
18 #include "buffered_file.h"
19 #include "block.h"
20
21 //#define DEBUG_MIGRATION_TCP
22
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
30
31 static int socket_errno(MigrationState *s)
32 {
33     return socket_error();
34 }
35
36 static int socket_write(MigrationState *s, const void * buf, size_t size)
37 {
38     return send(s->fd, buf, size, 0);
39 }
40
41 static int tcp_close(MigrationState *s)
42 {
43     DPRINTF("tcp_close\n");
44     if (s->fd != -1) {
45         close(s->fd);
46         s->fd = -1;
47     }
48     return 0;
49 }
50
51
52 static void tcp_wait_for_connect(void *opaque)
53 {
54     MigrationState *s = opaque;
55     int val, ret;
56     socklen_t valsize = sizeof(val);
57
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);
62
63     if (ret < 0) {
64         migrate_fd_error(s);
65         return;
66     }
67
68     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
69
70     if (val == 0)
71         migrate_fd_connect(s);
72     else {
73         DPRINTF("error connecting %d\n", val);
74         migrate_fd_error(s);
75     }
76 }
77
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)
84 {
85     struct sockaddr_in addr;
86     MigrationState *s;
87     int ret;
88
89     if (parse_host_port(&addr, host_port) < 0)
90         return NULL;
91
92     s = migrate_new(mon, bandwidth_limit, detach, blk, inc);
93
94     s->get_error = socket_errno;
95     s->write = socket_write;
96     s->close = tcp_close;
97
98     s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
99     if (s->fd == -1) {
100         g_free(s);
101         return NULL;
102     }
103
104     socket_set_nonblock(s->fd);
105
106     do {
107         ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
108         if (ret == -1)
109             ret = -(s->get_error(s));
110
111         if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
112             qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
113     } while (ret == -EINTR);
114
115     if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
116         DPRINTF("connect failed\n");
117         migrate_fd_error(s);
118     } else if (ret >= 0)
119         migrate_fd_connect(s);
120
121     return s;
122 }
123
124 static void tcp_accept_incoming_migration(void *opaque)
125 {
126     struct sockaddr_in addr;
127     socklen_t addrlen = sizeof(addr);
128     int s = (intptr_t)opaque;
129     QEMUFile *f;
130     int c;
131
132     do {
133         c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
134     } while (c == -1 && socket_error() == EINTR);
135
136     DPRINTF("accepted migration\n");
137
138     if (c == -1) {
139         fprintf(stderr, "could not accept migration connection\n");
140         goto out2;
141     }
142
143     f = qemu_fopen_socket(c);
144     if (f == NULL) {
145         fprintf(stderr, "could not qemu_fopen socket\n");
146         goto out;
147     }
148
149     process_incoming_migration(f);
150     qemu_fclose(f);
151 out:
152     close(c);
153 out2:
154     qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
155     close(s);
156 }
157
158 int 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
169     s = qemu_socket(PF_INET, SOCK_STREAM, 0);
170     if (s == -1)
171         return -socket_error();
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,
183                          (void *)(intptr_t)s);
184
185     return 0;
186
187 err:
188     close(s);
189     return -socket_error();
190 }