]>
Commit | Line | Data |
---|---|---|
4951f65b CL |
1 | /* |
2 | * QEMU live migration via Unix Domain Sockets | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2009 | |
5 | * | |
6 | * Authors: | |
7 | * Chris Lalancette <[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. | |
4951f65b CL |
14 | */ |
15 | ||
d99598cc DDAG |
16 | #include <string.h> |
17 | ||
4951f65b | 18 | #include "qemu-common.h" |
d99598cc | 19 | #include "qemu/error-report.h" |
1de7afc9 | 20 | #include "qemu/sockets.h" |
6a1751b7 | 21 | #include "qemu/main-loop.h" |
caf71f86 | 22 | #include "migration/migration.h" |
557ec5a0 | 23 | #include "migration/qemu-file.h" |
737e150e | 24 | #include "block/block.h" |
4951f65b CL |
25 | |
26 | //#define DEBUG_MIGRATION_UNIX | |
27 | ||
28 | #ifdef DEBUG_MIGRATION_UNIX | |
d0f2c4c6 | 29 | #define DPRINTF(fmt, ...) \ |
4951f65b CL |
30 | do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0) |
31 | #else | |
d0f2c4c6 | 32 | #define DPRINTF(fmt, ...) \ |
4951f65b CL |
33 | do { } while (0) |
34 | #endif | |
35 | ||
e08c95ce | 36 | static void unix_wait_for_connect(int fd, void *opaque) |
4951f65b | 37 | { |
22f00a44 | 38 | MigrationState *s = opaque; |
4951f65b | 39 | |
e08c95ce PB |
40 | if (fd < 0) { |
41 | DPRINTF("migrate connect error\n"); | |
b352365f | 42 | s->file = NULL; |
4951f65b | 43 | migrate_fd_error(s); |
e08c95ce PB |
44 | } else { |
45 | DPRINTF("migrate connect success\n"); | |
b352365f | 46 | s->file = qemu_fopen_socket(fd, "wb"); |
4951f65b | 47 | migrate_fd_connect(s); |
4951f65b CL |
48 | } |
49 | } | |
50 | ||
f37afb5a | 51 | void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp) |
4951f65b | 52 | { |
f8bbc128 | 53 | unix_nonblocking_connect(path, unix_wait_for_connect, s, errp); |
4951f65b CL |
54 | } |
55 | ||
56 | static void unix_accept_incoming_migration(void *opaque) | |
57 | { | |
58 | struct sockaddr_un addr; | |
59 | socklen_t addrlen = sizeof(addr); | |
e0efb993 | 60 | int s = (intptr_t)opaque; |
4951f65b | 61 | QEMUFile *f; |
d99598cc | 62 | int c, err; |
4951f65b CL |
63 | |
64 | do { | |
40ff6d7e | 65 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
d99598cc DDAG |
66 | err = errno; |
67 | } while (c < 0 && err == EINTR); | |
a6ef2909 PB |
68 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
69 | close(s); | |
4951f65b | 70 | |
d0f2c4c6 | 71 | DPRINTF("accepted migration\n"); |
4951f65b | 72 | |
d99598cc DDAG |
73 | if (c < 0) { |
74 | error_report("could not accept migration connection (%s)", | |
75 | strerror(err)); | |
76 | return; | |
4951f65b CL |
77 | } |
78 | ||
0cc3f3cc | 79 | f = qemu_fopen_socket(c, "rb"); |
4951f65b | 80 | if (f == NULL) { |
d99598cc | 81 | error_report("could not qemu_fopen socket"); |
4951f65b CL |
82 | goto out; |
83 | } | |
84 | ||
511c0231 | 85 | process_incoming_migration(f); |
ab52a824 PB |
86 | return; |
87 | ||
4951f65b | 88 | out: |
ee86c61f | 89 | close(c); |
4951f65b CL |
90 | } |
91 | ||
43eaae28 | 92 | void unix_start_incoming_migration(const char *path, Error **errp) |
4951f65b | 93 | { |
ee86c61f | 94 | int s; |
4951f65b | 95 | |
e08c95ce PB |
96 | s = unix_listen(path, NULL, 0, errp); |
97 | if (s < 0) { | |
43eaae28 | 98 | return; |
4951f65b CL |
99 | } |
100 | ||
ee86c61f JQ |
101 | qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL, |
102 | (void *)(intptr_t)s); | |
4951f65b | 103 | } |