]>
Commit | Line | Data |
---|---|---|
34c9dd8e AL |
1 | /* |
2 | * QEMU live migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | * | |
6b620ca3 PB |
12 | * Contributions after 2012-01-13 are licensed under the terms of the |
13 | * GNU GPL, version 2 or (at your option) any later version. | |
34c9dd8e AL |
14 | */ |
15 | ||
d99598cc DDAG |
16 | #include <string.h> |
17 | ||
34c9dd8e | 18 | #include "qemu-common.h" |
d99598cc | 19 | #include "qemu/error-report.h" |
1de7afc9 | 20 | #include "qemu/sockets.h" |
caf71f86 | 21 | #include "migration/migration.h" |
557ec5a0 | 22 | #include "migration/qemu-file.h" |
737e150e | 23 | #include "block/block.h" |
6a1751b7 | 24 | #include "qemu/main-loop.h" |
34c9dd8e AL |
25 | |
26 | //#define DEBUG_MIGRATION_TCP | |
27 | ||
34c9dd8e | 28 | #ifdef DEBUG_MIGRATION_TCP |
d0f2c4c6 | 29 | #define DPRINTF(fmt, ...) \ |
34c9dd8e AL |
30 | do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0) |
31 | #else | |
d0f2c4c6 | 32 | #define DPRINTF(fmt, ...) \ |
34c9dd8e AL |
33 | do { } while (0) |
34 | #endif | |
35 | ||
233aa5c2 | 36 | static void tcp_wait_for_connect(int fd, void *opaque) |
34c9dd8e | 37 | { |
22f00a44 | 38 | MigrationState *s = opaque; |
34c9dd8e | 39 | |
233aa5c2 OW |
40 | if (fd < 0) { |
41 | DPRINTF("migrate connect error\n"); | |
b352365f | 42 | s->file = NULL; |
065e2813 | 43 | migrate_fd_error(s); |
233aa5c2 OW |
44 | } else { |
45 | DPRINTF("migrate connect success\n"); | |
b352365f | 46 | s->file = qemu_fopen_socket(fd, "wb"); |
065e2813 | 47 | migrate_fd_connect(s); |
34c9dd8e | 48 | } |
34c9dd8e AL |
49 | } |
50 | ||
f37afb5a | 51 | void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp) |
34c9dd8e | 52 | { |
f8bbc128 | 53 | inet_nonblocking_connect(host_port, tcp_wait_for_connect, s, errp); |
34c9dd8e AL |
54 | } |
55 | ||
56 | static void tcp_accept_incoming_migration(void *opaque) | |
57 | { | |
58 | struct sockaddr_in addr; | |
59 | socklen_t addrlen = sizeof(addr); | |
e0efb993 | 60 | int s = (intptr_t)opaque; |
34c9dd8e | 61 | QEMUFile *f; |
d99598cc | 62 | int c, err; |
34c9dd8e AL |
63 | |
64 | do { | |
40ff6d7e | 65 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
d99598cc DDAG |
66 | err = socket_error(); |
67 | } while (c < 0 && err == EINTR); | |
a6ef2909 | 68 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
09bac73c | 69 | closesocket(s); |
34c9dd8e | 70 | |
d0f2c4c6 | 71 | DPRINTF("accepted migration\n"); |
34c9dd8e | 72 | |
d99598cc DDAG |
73 | if (c < 0) { |
74 | error_report("could not accept migration connection (%s)", | |
75 | strerror(err)); | |
76 | return; | |
34c9dd8e AL |
77 | } |
78 | ||
0cc3f3cc | 79 | f = qemu_fopen_socket(c, "rb"); |
34c9dd8e | 80 | if (f == NULL) { |
d99598cc | 81 | error_report("could not qemu_fopen socket"); |
34c9dd8e AL |
82 | goto out; |
83 | } | |
84 | ||
511c0231 | 85 | process_incoming_migration(f); |
ab52a824 PB |
86 | return; |
87 | ||
34c9dd8e | 88 | out: |
09bac73c | 89 | closesocket(c); |
34c9dd8e AL |
90 | } |
91 | ||
43eaae28 | 92 | void tcp_start_incoming_migration(const char *host_port, Error **errp) |
34c9dd8e | 93 | { |
34c9dd8e AL |
94 | int s; |
95 | ||
d5c5dacc | 96 | s = inet_listen(host_port, NULL, 256, SOCK_STREAM, 0, errp); |
d5c5dacc | 97 | if (s < 0) { |
43eaae28 | 98 | return; |
ee86c61f | 99 | } |
34c9dd8e AL |
100 | |
101 | qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL, | |
e0efb993 | 102 | (void *)(intptr_t)s); |
34c9dd8e | 103 | } |