root / migration-tcp.c @ cc84de95
History | View | Annotate | Download (4.9 kB)
1 | 34c9dd8e | aliguori | /*
|
---|---|---|---|
2 | 34c9dd8e | aliguori | * QEMU live migration
|
3 | 34c9dd8e | aliguori | *
|
4 | 34c9dd8e | aliguori | * Copyright IBM, Corp. 2008
|
5 | 34c9dd8e | aliguori | *
|
6 | 34c9dd8e | aliguori | * Authors:
|
7 | 34c9dd8e | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | 34c9dd8e | aliguori | *
|
9 | 34c9dd8e | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | 34c9dd8e | aliguori | * the COPYING file in the top-level directory.
|
11 | 34c9dd8e | aliguori | *
|
12 | 34c9dd8e | aliguori | */
|
13 | 34c9dd8e | aliguori | |
14 | 34c9dd8e | aliguori | #include "qemu-common.h" |
15 | 34c9dd8e | aliguori | #include "qemu_socket.h" |
16 | 34c9dd8e | aliguori | #include "migration.h" |
17 | 34c9dd8e | aliguori | #include "qemu-char.h" |
18 | 34c9dd8e | aliguori | #include "sysemu.h" |
19 | 34c9dd8e | aliguori | #include "buffered_file.h" |
20 | 34c9dd8e | aliguori | #include "block.h" |
21 | 34c9dd8e | aliguori | |
22 | 34c9dd8e | aliguori | //#define DEBUG_MIGRATION_TCP
|
23 | 34c9dd8e | aliguori | |
24 | 34c9dd8e | aliguori | #ifdef DEBUG_MIGRATION_TCP
|
25 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) \
|
26 | 34c9dd8e | aliguori | do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0) |
27 | 34c9dd8e | aliguori | #else
|
28 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) \
|
29 | 34c9dd8e | aliguori | do { } while (0) |
30 | 34c9dd8e | aliguori | #endif
|
31 | 34c9dd8e | aliguori | |
32 | 065e2813 | aliguori | static int socket_errno(FdMigrationState *s) |
33 | 34c9dd8e | aliguori | { |
34 | 8ad9fa5d | aliguori | return socket_error();
|
35 | 34c9dd8e | aliguori | } |
36 | 34c9dd8e | aliguori | |
37 | 065e2813 | aliguori | static int socket_write(FdMigrationState *s, const void * buf, size_t size) |
38 | 34c9dd8e | aliguori | { |
39 | 065e2813 | aliguori | return send(s->fd, buf, size, 0); |
40 | 34c9dd8e | aliguori | } |
41 | 34c9dd8e | aliguori | |
42 | 065e2813 | aliguori | static int tcp_close(FdMigrationState *s) |
43 | 34c9dd8e | aliguori | { |
44 | d0f2c4c6 | malc | DPRINTF("tcp_close\n");
|
45 | 34c9dd8e | aliguori | if (s->fd != -1) { |
46 | ff8d81d8 | aliguori | close(s->fd); |
47 | ff8d81d8 | aliguori | s->fd = -1;
|
48 | 34c9dd8e | aliguori | } |
49 | 34c9dd8e | aliguori | return 0; |
50 | 34c9dd8e | aliguori | } |
51 | 34c9dd8e | aliguori | |
52 | 34c9dd8e | aliguori | |
53 | 34c9dd8e | aliguori | static void tcp_wait_for_connect(void *opaque) |
54 | 34c9dd8e | aliguori | { |
55 | 34c9dd8e | aliguori | FdMigrationState *s = opaque; |
56 | 34c9dd8e | aliguori | int val, ret;
|
57 | 4761a48b | blueswir1 | socklen_t valsize = sizeof(val);
|
58 | 34c9dd8e | aliguori | |
59 | d0f2c4c6 | malc | DPRINTF("connect completed\n");
|
60 | 34c9dd8e | aliguori | do {
|
61 | 0a656f5f | malc | ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
|
62 | 065e2813 | aliguori | } while (ret == -1 && (s->get_error(s)) == EINTR); |
63 | 34c9dd8e | aliguori | |
64 | 34c9dd8e | aliguori | if (ret < 0) { |
65 | 065e2813 | aliguori | migrate_fd_error(s); |
66 | 34c9dd8e | aliguori | return;
|
67 | 34c9dd8e | aliguori | } |
68 | 34c9dd8e | aliguori | |
69 | 34c9dd8e | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
70 | 34c9dd8e | aliguori | |
71 | 34c9dd8e | aliguori | if (val == 0) |
72 | 065e2813 | aliguori | migrate_fd_connect(s); |
73 | 34c9dd8e | aliguori | else {
|
74 | d0f2c4c6 | malc | DPRINTF("error connecting %d\n", val);
|
75 | 065e2813 | aliguori | migrate_fd_error(s); |
76 | 34c9dd8e | aliguori | } |
77 | 34c9dd8e | aliguori | } |
78 | 34c9dd8e | aliguori | |
79 | f327aa0c | Jan Kiszka | MigrationState *tcp_start_outgoing_migration(Monitor *mon, |
80 | f327aa0c | Jan Kiszka | const char *host_port, |
81 | ff8d81d8 | aliguori | int64_t bandwidth_limit, |
82 | c163b5ca | lirans@il.ibm.com | int detach,
|
83 | c163b5ca | lirans@il.ibm.com | int blk,
|
84 | c163b5ca | lirans@il.ibm.com | int inc)
|
85 | 34c9dd8e | aliguori | { |
86 | 34c9dd8e | aliguori | struct sockaddr_in addr;
|
87 | 34c9dd8e | aliguori | FdMigrationState *s; |
88 | 34c9dd8e | aliguori | int ret;
|
89 | 34c9dd8e | aliguori | |
90 | 34c9dd8e | aliguori | if (parse_host_port(&addr, host_port) < 0) |
91 | 34c9dd8e | aliguori | return NULL; |
92 | 34c9dd8e | aliguori | |
93 | 34c9dd8e | aliguori | s = qemu_mallocz(sizeof(*s));
|
94 | 34c9dd8e | aliguori | |
95 | 065e2813 | aliguori | s->get_error = socket_errno; |
96 | 065e2813 | aliguori | s->write = socket_write; |
97 | 065e2813 | aliguori | s->close = tcp_close; |
98 | 065e2813 | aliguori | s->mig_state.cancel = migrate_fd_cancel; |
99 | 065e2813 | aliguori | s->mig_state.get_status = migrate_fd_get_status; |
100 | 065e2813 | aliguori | s->mig_state.release = migrate_fd_release; |
101 | 34c9dd8e | aliguori | |
102 | c163b5ca | lirans@il.ibm.com | s->mig_state.blk = blk; |
103 | c163b5ca | lirans@il.ibm.com | s->mig_state.shared = inc; |
104 | c163b5ca | lirans@il.ibm.com | |
105 | 34c9dd8e | aliguori | s->state = MIG_STATE_ACTIVE; |
106 | f327aa0c | Jan Kiszka | s->mon = NULL;
|
107 | 34c9dd8e | aliguori | s->bandwidth_limit = bandwidth_limit; |
108 | 40ff6d7e | Kevin Wolf | s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
109 | 34c9dd8e | aliguori | if (s->fd == -1) { |
110 | 34c9dd8e | aliguori | qemu_free(s); |
111 | ff8d81d8 | aliguori | return NULL; |
112 | 34c9dd8e | aliguori | } |
113 | 34c9dd8e | aliguori | |
114 | 17e90973 | aliguori | socket_set_nonblock(s->fd); |
115 | 34c9dd8e | aliguori | |
116 | f327aa0c | Jan Kiszka | if (!detach) {
|
117 | f327aa0c | Jan Kiszka | migrate_fd_monitor_suspend(s, mon); |
118 | f327aa0c | Jan Kiszka | } |
119 | 34c9dd8e | aliguori | |
120 | 34c9dd8e | aliguori | do {
|
121 | 34c9dd8e | aliguori | ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)); |
122 | 34c9dd8e | aliguori | if (ret == -1) |
123 | 065e2813 | aliguori | ret = -(s->get_error(s)); |
124 | 34c9dd8e | aliguori | |
125 | c1d36665 | aliguori | if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
|
126 | 34c9dd8e | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s); |
127 | 34c9dd8e | aliguori | } while (ret == -EINTR);
|
128 | 34c9dd8e | aliguori | |
129 | c1d36665 | aliguori | if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) { |
130 | d0f2c4c6 | malc | DPRINTF("connect failed\n");
|
131 | 34c9dd8e | aliguori | close(s->fd); |
132 | 34c9dd8e | aliguori | qemu_free(s); |
133 | 88d2d9e0 | aliguori | return NULL; |
134 | 34c9dd8e | aliguori | } else if (ret >= 0) |
135 | 065e2813 | aliguori | migrate_fd_connect(s); |
136 | 34c9dd8e | aliguori | |
137 | 34c9dd8e | aliguori | return &s->mig_state;
|
138 | 34c9dd8e | aliguori | } |
139 | 34c9dd8e | aliguori | |
140 | 34c9dd8e | aliguori | static void tcp_accept_incoming_migration(void *opaque) |
141 | 34c9dd8e | aliguori | { |
142 | 34c9dd8e | aliguori | struct sockaddr_in addr;
|
143 | 34c9dd8e | aliguori | socklen_t addrlen = sizeof(addr);
|
144 | 34c9dd8e | aliguori | int s = (unsigned long)opaque; |
145 | 34c9dd8e | aliguori | QEMUFile *f; |
146 | 34c9dd8e | aliguori | int c, ret;
|
147 | 34c9dd8e | aliguori | |
148 | 34c9dd8e | aliguori | do {
|
149 | 40ff6d7e | Kevin Wolf | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
|
150 | c1d36665 | aliguori | } while (c == -1 && socket_error() == EINTR); |
151 | 34c9dd8e | aliguori | |
152 | d0f2c4c6 | malc | DPRINTF("accepted migration\n");
|
153 | 34c9dd8e | aliguori | |
154 | 34c9dd8e | aliguori | if (c == -1) { |
155 | 34c9dd8e | aliguori | fprintf(stderr, "could not accept migration connection\n");
|
156 | 34c9dd8e | aliguori | return;
|
157 | 34c9dd8e | aliguori | } |
158 | 34c9dd8e | aliguori | |
159 | c1d36665 | aliguori | f = qemu_fopen_socket(c); |
160 | 34c9dd8e | aliguori | if (f == NULL) { |
161 | 34c9dd8e | aliguori | fprintf(stderr, "could not qemu_fopen socket\n");
|
162 | 34c9dd8e | aliguori | goto out;
|
163 | 34c9dd8e | aliguori | } |
164 | 34c9dd8e | aliguori | |
165 | 34c9dd8e | aliguori | ret = qemu_loadvm_state(f); |
166 | 34c9dd8e | aliguori | if (ret < 0) { |
167 | 34c9dd8e | aliguori | fprintf(stderr, "load of migration failed\n");
|
168 | 34c9dd8e | aliguori | goto out_fopen;
|
169 | 34c9dd8e | aliguori | } |
170 | 34c9dd8e | aliguori | qemu_announce_self(); |
171 | d0f2c4c6 | malc | DPRINTF("successfully loaded vm state\n");
|
172 | 34c9dd8e | aliguori | |
173 | 34c9dd8e | aliguori | /* we've successfully migrated, close the server socket */
|
174 | 34c9dd8e | aliguori | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
175 | 34c9dd8e | aliguori | close(s); |
176 | d399f677 | Paolo Bonzini | if (autostart)
|
177 | d399f677 | Paolo Bonzini | vm_start(); |
178 | 34c9dd8e | aliguori | |
179 | 34c9dd8e | aliguori | out_fopen:
|
180 | 34c9dd8e | aliguori | qemu_fclose(f); |
181 | 34c9dd8e | aliguori | out:
|
182 | 34c9dd8e | aliguori | close(c); |
183 | 34c9dd8e | aliguori | } |
184 | 34c9dd8e | aliguori | |
185 | 34c9dd8e | aliguori | int tcp_start_incoming_migration(const char *host_port) |
186 | 34c9dd8e | aliguori | { |
187 | 34c9dd8e | aliguori | struct sockaddr_in addr;
|
188 | 34c9dd8e | aliguori | int val;
|
189 | 34c9dd8e | aliguori | int s;
|
190 | 34c9dd8e | aliguori | |
191 | 34c9dd8e | aliguori | if (parse_host_port(&addr, host_port) < 0) { |
192 | 34c9dd8e | aliguori | fprintf(stderr, "invalid host/port combination: %s\n", host_port);
|
193 | 34c9dd8e | aliguori | return -EINVAL;
|
194 | 34c9dd8e | aliguori | } |
195 | 34c9dd8e | aliguori | |
196 | 40ff6d7e | Kevin Wolf | s = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
197 | 34c9dd8e | aliguori | if (s == -1) |
198 | c1d36665 | aliguori | return -socket_error();
|
199 | 34c9dd8e | aliguori | |
200 | 34c9dd8e | aliguori | val = 1;
|
201 | 34c9dd8e | aliguori | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
202 | 34c9dd8e | aliguori | |
203 | 34c9dd8e | aliguori | if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
204 | 34c9dd8e | aliguori | goto err;
|
205 | 34c9dd8e | aliguori | |
206 | 34c9dd8e | aliguori | if (listen(s, 1) == -1) |
207 | 34c9dd8e | aliguori | goto err;
|
208 | 34c9dd8e | aliguori | |
209 | 34c9dd8e | aliguori | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, |
210 | 34c9dd8e | aliguori | (void *)(unsigned long)s); |
211 | 34c9dd8e | aliguori | |
212 | 34c9dd8e | aliguori | return 0; |
213 | 34c9dd8e | aliguori | |
214 | 34c9dd8e | aliguori | err:
|
215 | 34c9dd8e | aliguori | close(s); |
216 | c1d36665 | aliguori | return -socket_error();
|
217 | 34c9dd8e | aliguori | } |