]>
Commit | Line | Data |
---|---|---|
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" | |
18 | #include "sysemu.h" | |
34c9dd8e AL |
19 | #include "buffered_file.h" |
20 | #include "block.h" | |
21 | ||
22 | //#define DEBUG_MIGRATION_TCP | |
23 | ||
34c9dd8e AL |
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 | |
31 | ||
065e2813 | 32 | static int socket_errno(FdMigrationState *s) |
34c9dd8e | 33 | { |
8ad9fa5d | 34 | return socket_error(); |
34c9dd8e AL |
35 | } |
36 | ||
065e2813 | 37 | static int socket_write(FdMigrationState *s, const void * buf, size_t size) |
34c9dd8e | 38 | { |
065e2813 | 39 | return send(s->fd, buf, size, 0); |
34c9dd8e AL |
40 | } |
41 | ||
065e2813 | 42 | static int tcp_close(FdMigrationState *s) |
34c9dd8e | 43 | { |
065e2813 | 44 | dprintf("tcp_close\n"); |
34c9dd8e | 45 | if (s->fd != -1) { |
ff8d81d8 AL |
46 | close(s->fd); |
47 | s->fd = -1; | |
34c9dd8e AL |
48 | } |
49 | return 0; | |
50 | } | |
51 | ||
34c9dd8e AL |
52 | |
53 | static void tcp_wait_for_connect(void *opaque) | |
54 | { | |
55 | FdMigrationState *s = opaque; | |
56 | int val, ret; | |
4761a48b | 57 | socklen_t valsize = sizeof(val); |
34c9dd8e AL |
58 | |
59 | dprintf("connect completed\n"); | |
60 | do { | |
0a656f5f | 61 | ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize); |
065e2813 | 62 | } while (ret == -1 && (s->get_error(s)) == EINTR); |
34c9dd8e AL |
63 | |
64 | if (ret < 0) { | |
065e2813 | 65 | migrate_fd_error(s); |
34c9dd8e AL |
66 | return; |
67 | } | |
68 | ||
69 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
70 | ||
71 | if (val == 0) | |
065e2813 | 72 | migrate_fd_connect(s); |
34c9dd8e AL |
73 | else { |
74 | dprintf("error connecting %d\n", val); | |
065e2813 | 75 | migrate_fd_error(s); |
34c9dd8e | 76 | } |
34c9dd8e AL |
77 | } |
78 | ||
79 | MigrationState *tcp_start_outgoing_migration(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; |
731b0364 | 105 | s->mon_resume = NULL; |
34c9dd8e AL |
106 | s->bandwidth_limit = bandwidth_limit; |
107 | s->fd = socket(PF_INET, SOCK_STREAM, 0); | |
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 | |
731b0364 AL |
115 | if (!detach) |
116 | migrate_fd_monitor_suspend(s); | |
34c9dd8e AL |
117 | |
118 | do { | |
119 | ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)); | |
120 | if (ret == -1) | |
065e2813 | 121 | ret = -(s->get_error(s)); |
34c9dd8e | 122 | |
c1d36665 | 123 | if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) |
34c9dd8e AL |
124 | qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s); |
125 | } while (ret == -EINTR); | |
126 | ||
c1d36665 | 127 | if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) { |
34c9dd8e AL |
128 | dprintf("connect failed\n"); |
129 | close(s->fd); | |
130 | qemu_free(s); | |
88d2d9e0 | 131 | return NULL; |
34c9dd8e | 132 | } else if (ret >= 0) |
065e2813 | 133 | migrate_fd_connect(s); |
34c9dd8e AL |
134 | |
135 | return &s->mig_state; | |
136 | } | |
137 | ||
138 | static void tcp_accept_incoming_migration(void *opaque) | |
139 | { | |
140 | struct sockaddr_in addr; | |
141 | socklen_t addrlen = sizeof(addr); | |
142 | int s = (unsigned long)opaque; | |
143 | QEMUFile *f; | |
144 | int c, ret; | |
145 | ||
146 | do { | |
147 | c = accept(s, (struct sockaddr *)&addr, &addrlen); | |
c1d36665 | 148 | } while (c == -1 && socket_error() == EINTR); |
34c9dd8e AL |
149 | |
150 | dprintf("accepted migration\n"); | |
151 | ||
152 | if (c == -1) { | |
153 | fprintf(stderr, "could not accept migration connection\n"); | |
154 | return; | |
155 | } | |
156 | ||
c1d36665 | 157 | f = qemu_fopen_socket(c); |
34c9dd8e AL |
158 | if (f == NULL) { |
159 | fprintf(stderr, "could not qemu_fopen socket\n"); | |
160 | goto out; | |
161 | } | |
162 | ||
34c9dd8e AL |
163 | ret = qemu_loadvm_state(f); |
164 | if (ret < 0) { | |
165 | fprintf(stderr, "load of migration failed\n"); | |
166 | goto out_fopen; | |
167 | } | |
168 | qemu_announce_self(); | |
169 | dprintf("successfully loaded vm state\n"); | |
170 | ||
171 | /* we've successfully migrated, close the server socket */ | |
172 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); | |
173 | close(s); | |
d399f677 PB |
174 | if (autostart) |
175 | vm_start(); | |
34c9dd8e | 176 | |
34c9dd8e AL |
177 | out_fopen: |
178 | qemu_fclose(f); | |
179 | out: | |
180 | close(c); | |
181 | } | |
182 | ||
183 | int tcp_start_incoming_migration(const char *host_port) | |
184 | { | |
185 | struct sockaddr_in addr; | |
186 | int val; | |
187 | int s; | |
188 | ||
189 | if (parse_host_port(&addr, host_port) < 0) { | |
190 | fprintf(stderr, "invalid host/port combination: %s\n", host_port); | |
191 | return -EINVAL; | |
192 | } | |
193 | ||
194 | s = socket(PF_INET, SOCK_STREAM, 0); | |
195 | if (s == -1) | |
c1d36665 | 196 | return -socket_error(); |
34c9dd8e AL |
197 | |
198 | val = 1; | |
199 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
200 | ||
201 | if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
202 | goto err; | |
203 | ||
204 | if (listen(s, 1) == -1) | |
205 | goto err; | |
206 | ||
207 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, | |
208 | (void *)(unsigned long)s); | |
209 | ||
210 | return 0; | |
211 | ||
212 | err: | |
213 | close(s); | |
c1d36665 | 214 | return -socket_error(); |
34c9dd8e | 215 | } |