iotests: add test 260 to check bitmap life after snapshot + commit
[qemu/kevin.git] / migration / socket.c
blob97c9efde5969de73ca504a26f654707a587be0d7
1 /*
2 * QEMU live migration via socket
4 * Copyright Red Hat, Inc. 2009-2016
6 * Authors:
7 * Chris Lalancette <clalance@redhat.com>
8 * Daniel P. Berrange <berrange@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
13 * Contributions after 2012-01-13 are licensed under the terms of the
14 * GNU GPL, version 2 or (at your option) any later version.
17 #include "qemu/osdep.h"
18 #include "qemu/cutils.h"
20 #include "qemu/error-report.h"
21 #include "qapi/error.h"
22 #include "channel.h"
23 #include "socket.h"
24 #include "migration.h"
25 #include "qemu-file.h"
26 #include "io/channel-socket.h"
27 #include "io/net-listener.h"
28 #include "trace.h"
31 struct SocketOutgoingArgs {
32 SocketAddress *saddr;
33 } outgoing_args;
35 void socket_send_channel_create(QIOTaskFunc f, void *data)
37 QIOChannelSocket *sioc = qio_channel_socket_new();
38 qio_channel_socket_connect_async(sioc, outgoing_args.saddr,
39 f, data, NULL, NULL);
42 int socket_send_channel_destroy(QIOChannel *send)
44 /* Remove channel */
45 object_unref(OBJECT(send));
46 if (outgoing_args.saddr) {
47 qapi_free_SocketAddress(outgoing_args.saddr);
48 outgoing_args.saddr = NULL;
50 return 0;
53 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
55 SocketAddress *saddr;
57 saddr = g_new0(SocketAddress, 1);
58 saddr->type = SOCKET_ADDRESS_TYPE_INET;
60 if (inet_parse(&saddr->u.inet, host_port, errp)) {
61 qapi_free_SocketAddress(saddr);
62 return NULL;
65 return saddr;
69 static SocketAddress *unix_build_address(const char *path)
71 SocketAddress *saddr;
73 saddr = g_new0(SocketAddress, 1);
74 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
75 saddr->u.q_unix.path = g_strdup(path);
77 return saddr;
81 struct SocketConnectData {
82 MigrationState *s;
83 char *hostname;
86 static void socket_connect_data_free(void *opaque)
88 struct SocketConnectData *data = opaque;
89 if (!data) {
90 return;
92 g_free(data->hostname);
93 g_free(data);
96 static void socket_outgoing_migration(QIOTask *task,
97 gpointer opaque)
99 struct SocketConnectData *data = opaque;
100 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
101 Error *err = NULL;
103 if (qio_task_propagate_error(task, &err)) {
104 trace_migration_socket_outgoing_error(error_get_pretty(err));
105 } else {
106 trace_migration_socket_outgoing_connected(data->hostname);
108 migration_channel_connect(data->s, sioc, data->hostname, err);
109 object_unref(OBJECT(sioc));
112 static void socket_start_outgoing_migration(MigrationState *s,
113 SocketAddress *saddr,
114 Error **errp)
116 QIOChannelSocket *sioc = qio_channel_socket_new();
117 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
119 data->s = s;
121 /* in case previous migration leaked it */
122 qapi_free_SocketAddress(outgoing_args.saddr);
123 outgoing_args.saddr = saddr;
125 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
126 data->hostname = g_strdup(saddr->u.inet.host);
129 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
130 qio_channel_socket_connect_async(sioc,
131 saddr,
132 socket_outgoing_migration,
133 data,
134 socket_connect_data_free,
135 NULL);
138 void tcp_start_outgoing_migration(MigrationState *s,
139 const char *host_port,
140 Error **errp)
142 Error *err = NULL;
143 SocketAddress *saddr = tcp_build_address(host_port, &err);
144 if (!err) {
145 socket_start_outgoing_migration(s, saddr, &err);
147 error_propagate(errp, err);
150 void unix_start_outgoing_migration(MigrationState *s,
151 const char *path,
152 Error **errp)
154 SocketAddress *saddr = unix_build_address(path);
155 socket_start_outgoing_migration(s, saddr, errp);
159 static void socket_accept_incoming_migration(QIONetListener *listener,
160 QIOChannelSocket *cioc,
161 gpointer opaque)
163 trace_migration_socket_incoming_accepted();
165 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
166 migration_channel_process_incoming(QIO_CHANNEL(cioc));
168 if (migration_has_all_channels()) {
169 /* Close listening socket as its no longer needed */
170 qio_net_listener_disconnect(listener);
171 object_unref(OBJECT(listener));
176 static void socket_start_incoming_migration(SocketAddress *saddr,
177 Error **errp)
179 QIONetListener *listener = qio_net_listener_new();
180 size_t i;
181 int num = 1;
183 qio_net_listener_set_name(listener, "migration-socket-listener");
185 if (migrate_use_multifd()) {
186 num = migrate_multifd_channels();
189 if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
190 object_unref(OBJECT(listener));
191 return;
194 qio_net_listener_set_client_func_full(listener,
195 socket_accept_incoming_migration,
196 NULL, NULL,
197 g_main_context_get_thread_default());
199 for (i = 0; i < listener->nsioc; i++) {
200 SocketAddress *address =
201 qio_channel_socket_get_local_address(listener->sioc[i], errp);
202 if (!address) {
203 return;
205 migrate_add_address(address);
206 qapi_free_SocketAddress(address);
210 void tcp_start_incoming_migration(const char *host_port, Error **errp)
212 Error *err = NULL;
213 SocketAddress *saddr = tcp_build_address(host_port, &err);
214 if (!err) {
215 socket_start_incoming_migration(saddr, &err);
217 qapi_free_SocketAddress(saddr);
218 error_propagate(errp, err);
221 void unix_start_incoming_migration(const char *path, Error **errp)
223 SocketAddress *saddr = unix_build_address(path);
224 socket_start_incoming_migration(saddr, errp);
225 qapi_free_SocketAddress(saddr);