]> git.proxmox.com Git - qemu.git/blame - migration-unix.c
sun4u: give ISA bus to ISA methods
[qemu.git] / migration-unix.c
CommitLineData
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 *
12 */
13
14#include "qemu-common.h"
15#include "qemu_socket.h"
16#include "migration.h"
17#include "qemu-char.h"
4951f65b
CL
18#include "buffered_file.h"
19#include "block.h"
20
21//#define DEBUG_MIGRATION_UNIX
22
23#ifdef DEBUG_MIGRATION_UNIX
d0f2c4c6 24#define DPRINTF(fmt, ...) \
4951f65b
CL
25 do { printf("migration-unix: " fmt, ## __VA_ARGS__); } while (0)
26#else
d0f2c4c6 27#define DPRINTF(fmt, ...) \
4951f65b
CL
28 do { } while (0)
29#endif
30
22f00a44 31static int unix_errno(MigrationState *s)
4951f65b
CL
32{
33 return errno;
34}
35
22f00a44 36static int unix_write(MigrationState *s, const void * buf, size_t size)
4951f65b
CL
37{
38 return write(s->fd, buf, size);
39}
40
22f00a44 41static int unix_close(MigrationState *s)
4951f65b 42{
8160bfbc 43 int r = 0;
d0f2c4c6 44 DPRINTF("unix_close\n");
4951f65b 45 if (s->fd != -1) {
8160bfbc
EH
46 if (close(s->fd) < 0) {
47 r = -errno;
48 }
4951f65b
CL
49 s->fd = -1;
50 }
8160bfbc 51 return r;
4951f65b
CL
52}
53
54static void unix_wait_for_connect(void *opaque)
55{
22f00a44 56 MigrationState *s = opaque;
4951f65b
CL
57 int val, ret;
58 socklen_t valsize = sizeof(val);
59
d0f2c4c6 60 DPRINTF("connect completed\n");
4951f65b
CL
61 do {
62 ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
efab4718 63 } while (ret == -1 && errno == EINTR);
4951f65b
CL
64
65 if (ret < 0) {
66 migrate_fd_error(s);
67 return;
68 }
69
70 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
71
72 if (val == 0)
73 migrate_fd_connect(s);
74 else {
d0f2c4c6 75 DPRINTF("error connecting %d\n", val);
4951f65b
CL
76 migrate_fd_error(s);
77 }
78}
79
07af4452 80int unix_start_outgoing_migration(MigrationState *s, const char *path)
4951f65b 81{
4951f65b
CL
82 struct sockaddr_un addr;
83 int ret;
84
85 addr.sun_family = AF_UNIX;
86 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
4951f65b
CL
87 s->get_error = unix_errno;
88 s->write = unix_write;
89 s->close = unix_close;
4951f65b 90
40ff6d7e 91 s->fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
ee86c61f 92 if (s->fd == -1) {
d0f2c4c6 93 DPRINTF("Unable to open socket");
8414ff3b 94 return -errno;
4951f65b
CL
95 }
96
97 socket_set_nonblock(s->fd);
98
4951f65b
CL
99 do {
100 ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
8414ff3b 101 if (ret == -1) {
efab4718 102 ret = -errno;
8414ff3b
JQ
103 }
104 if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
4951f65b 105 qemu_set_fd_handler2(s->fd, NULL, NULL, unix_wait_for_connect, s);
8414ff3b
JQ
106 return 0;
107 }
4951f65b
CL
108 } while (ret == -EINTR);
109
8414ff3b 110 if (ret < 0) {
d0f2c4c6 111 DPRINTF("connect failed\n");
8414ff3b
JQ
112 migrate_fd_error(s);
113 return ret;
2dd650e5 114 }
8414ff3b 115 migrate_fd_connect(s);
07af4452 116 return 0;
4951f65b
CL
117}
118
119static void unix_accept_incoming_migration(void *opaque)
120{
121 struct sockaddr_un addr;
122 socklen_t addrlen = sizeof(addr);
e0efb993 123 int s = (intptr_t)opaque;
4951f65b 124 QEMUFile *f;
511c0231 125 int c;
4951f65b
CL
126
127 do {
40ff6d7e 128 c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
efab4718 129 } while (c == -1 && errno == EINTR);
4951f65b 130
d0f2c4c6 131 DPRINTF("accepted migration\n");
4951f65b
CL
132
133 if (c == -1) {
134 fprintf(stderr, "could not accept migration connection\n");
ee86c61f 135 goto out2;
4951f65b
CL
136 }
137
138 f = qemu_fopen_socket(c);
139 if (f == NULL) {
140 fprintf(stderr, "could not qemu_fopen socket\n");
141 goto out;
142 }
143
511c0231 144 process_incoming_migration(f);
4951f65b
CL
145 qemu_fclose(f);
146out:
ee86c61f
JQ
147 close(c);
148out2:
cfaf6d36
JQ
149 qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
150 close(s);
4951f65b
CL
151}
152
153int unix_start_incoming_migration(const char *path)
154{
ee86c61f
JQ
155 struct sockaddr_un addr;
156 int s;
157 int ret;
4951f65b 158
d0f2c4c6 159 DPRINTF("Attempting to start an incoming migration\n");
4951f65b 160
ee86c61f
JQ
161 s = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
162 if (s == -1) {
4951f65b 163 fprintf(stderr, "Could not open unix socket: %s\n", strerror(errno));
ee86c61f 164 return -errno;
4951f65b
CL
165 }
166
ee86c61f
JQ
167 memset(&addr, 0, sizeof(addr));
168 addr.sun_family = AF_UNIX;
169 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
4951f65b 170
ee86c61f
JQ
171 unlink(addr.sun_path);
172 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
173 ret = -errno;
174 fprintf(stderr, "bind(unix:%s): %s\n", addr.sun_path, strerror(errno));
4951f65b
CL
175 goto err;
176 }
ee86c61f
JQ
177 if (listen(s, 1) == -1) {
178 fprintf(stderr, "listen(unix:%s): %s\n", addr.sun_path,
179 strerror(errno));
180 ret = -errno;
4951f65b
CL
181 goto err;
182 }
183
ee86c61f
JQ
184 qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
185 (void *)(intptr_t)s);
4951f65b
CL
186
187 return 0;
188
189err:
ee86c61f
JQ
190 close(s);
191 return ret;
4951f65b 192}