]>
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 <clalance@redhat.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 | * | |
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 | ||
16 | #include "qemu-common.h" | |
1de7afc9 | 17 | #include "qemu/sockets.h" |
caf71f86 | 18 | #include "migration/migration.h" |
557ec5a0 | 19 | #include "migration/qemu-file.h" |
737e150e | 20 | #include "block/block.h" |
4951f65b CL |
21 | |
22 | //#define DEBUG_MIGRATION_UNIX | |
23 | ||
24 | #ifdef DEBUG_MIGRATION_UNIX | |
d0f2c4c6 | 25 | #define DPRINTF(fmt, ...) \ |
4951f65b CL |
26 | do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0) |
27 | #else | |
d0f2c4c6 | 28 | #define DPRINTF(fmt, ...) \ |
4951f65b CL |
29 | do { } while (0) |
30 | #endif | |
31 | ||
22f00a44 | 32 | static int unix_errno(MigrationState *s) |
4951f65b CL |
33 | { |
34 | return errno; | |
35 | } | |
36 | ||
22f00a44 | 37 | static int unix_write(MigrationState *s, const void * buf, size_t size) |
4951f65b CL |
38 | { |
39 | return write(s->fd, buf, size); | |
40 | } | |
41 | ||
22f00a44 | 42 | static int unix_close(MigrationState *s) |
4951f65b | 43 | { |
8160bfbc | 44 | int r = 0; |
d0f2c4c6 | 45 | DPRINTF("unix_close\n"); |
6c360136 PB |
46 | if (close(s->fd) < 0) { |
47 | r = -errno; | |
4951f65b | 48 | } |
8160bfbc | 49 | return r; |
4951f65b CL |
50 | } |
51 | ||
e08c95ce | 52 | static void unix_wait_for_connect(int fd, void *opaque) |
4951f65b | 53 | { |
22f00a44 | 54 | MigrationState *s = opaque; |
4951f65b | 55 | |
e08c95ce PB |
56 | if (fd < 0) { |
57 | DPRINTF("migrate connect error\n"); | |
58 | s->fd = -1; | |
4951f65b | 59 | migrate_fd_error(s); |
e08c95ce PB |
60 | } else { |
61 | DPRINTF("migrate connect success\n"); | |
62 | s->fd = fd; | |
dd217b87 | 63 | socket_set_block(s->fd); |
4951f65b | 64 | migrate_fd_connect(s); |
4951f65b CL |
65 | } |
66 | } | |
67 | ||
f37afb5a | 68 | void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp) |
4951f65b | 69 | { |
4951f65b CL |
70 | s->get_error = unix_errno; |
71 | s->write = unix_write; | |
72 | s->close = unix_close; | |
4951f65b | 73 | |
f37afb5a | 74 | s->fd = unix_nonblocking_connect(path, unix_wait_for_connect, s, errp); |
4951f65b CL |
75 | } |
76 | ||
77 | static void unix_accept_incoming_migration(void *opaque) | |
78 | { | |
79 | struct sockaddr_un addr; | |
80 | socklen_t addrlen = sizeof(addr); | |
e0efb993 | 81 | int s = (intptr_t)opaque; |
4951f65b | 82 | QEMUFile *f; |
511c0231 | 83 | int c; |
4951f65b CL |
84 | |
85 | do { | |
40ff6d7e | 86 | c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen); |
efab4718 | 87 | } while (c == -1 && errno == EINTR); |
a6ef2909 PB |
88 | qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL); |
89 | close(s); | |
4951f65b | 90 | |
d0f2c4c6 | 91 | DPRINTF("accepted migration\n"); |
4951f65b CL |
92 | |
93 | if (c == -1) { | |
94 | fprintf(stderr, "could not accept migration connection\n"); | |
a6ef2909 | 95 | goto out; |
4951f65b CL |
96 | } |
97 | ||
98 | f = qemu_fopen_socket(c); | |
99 | if (f == NULL) { | |
100 | fprintf(stderr, "could not qemu_fopen socket\n"); | |
101 | goto out; | |
102 | } | |
103 | ||
511c0231 | 104 | process_incoming_migration(f); |
ab52a824 PB |
105 | return; |
106 | ||
4951f65b | 107 | out: |
ee86c61f | 108 | close(c); |
4951f65b CL |
109 | } |
110 | ||
43eaae28 | 111 | void unix_start_incoming_migration(const char *path, Error **errp) |
4951f65b | 112 | { |
ee86c61f | 113 | int s; |
4951f65b | 114 | |
e08c95ce PB |
115 | s = unix_listen(path, NULL, 0, errp); |
116 | if (s < 0) { | |
43eaae28 | 117 | return; |
4951f65b CL |
118 | } |
119 | ||
ee86c61f JQ |
120 | qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL, |
121 | (void *)(intptr_t)s); | |
4951f65b | 122 | } |