2 * QEMU live migration via socket
4 * Copyright Red Hat, Inc. 2009-2016
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"
24 #include "migration.h"
25 #include "qemu-file.h"
26 #include "io/channel-socket.h"
27 #include "io/net-listener.h"
29 #include "postcopy-ram.h"
32 struct SocketOutgoingArgs
{
36 void socket_send_channel_create(QIOTaskFunc f
, void *data
)
38 QIOChannelSocket
*sioc
= qio_channel_socket_new();
39 qio_channel_socket_connect_async(sioc
, outgoing_args
.saddr
,
43 QIOChannel
*socket_send_channel_create_sync(Error
**errp
)
45 QIOChannelSocket
*sioc
= qio_channel_socket_new();
47 if (!outgoing_args
.saddr
) {
48 object_unref(OBJECT(sioc
));
49 error_setg(errp
, "Initial sock address not set!");
53 if (qio_channel_socket_connect_sync(sioc
, outgoing_args
.saddr
, errp
) < 0) {
54 object_unref(OBJECT(sioc
));
58 return QIO_CHANNEL(sioc
);
61 int socket_send_channel_destroy(QIOChannel
*send
)
64 object_unref(OBJECT(send
));
65 if (outgoing_args
.saddr
) {
66 qapi_free_SocketAddress(outgoing_args
.saddr
);
67 outgoing_args
.saddr
= NULL
;
72 struct SocketConnectData
{
77 static void socket_connect_data_free(void *opaque
)
79 struct SocketConnectData
*data
= opaque
;
83 g_free(data
->hostname
);
87 static void socket_outgoing_migration(QIOTask
*task
,
90 struct SocketConnectData
*data
= opaque
;
91 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
94 if (qio_task_propagate_error(task
, &err
)) {
95 trace_migration_socket_outgoing_error(error_get_pretty(err
));
99 trace_migration_socket_outgoing_connected(data
->hostname
);
101 if (migrate_zero_copy_send() &&
102 !qio_channel_has_feature(sioc
, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY
)) {
103 error_setg(&err
, "Zero copy send feature not detected in host kernel");
107 migration_channel_connect(data
->s
, sioc
, data
->hostname
, err
);
108 object_unref(OBJECT(sioc
));
112 socket_start_outgoing_migration_internal(MigrationState
*s
,
113 SocketAddress
*saddr
,
116 QIOChannelSocket
*sioc
= qio_channel_socket_new();
117 struct SocketConnectData
*data
= g_new0(struct SocketConnectData
, 1);
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
,
132 socket_outgoing_migration
,
134 socket_connect_data_free
,
138 void socket_start_outgoing_migration(MigrationState
*s
,
143 SocketAddress
*saddr
= socket_parse(str
, &err
);
145 socket_start_outgoing_migration_internal(s
, saddr
, &err
);
147 error_propagate(errp
, err
);
150 static void socket_accept_incoming_migration(QIONetListener
*listener
,
151 QIOChannelSocket
*cioc
,
154 trace_migration_socket_incoming_accepted();
156 if (migration_has_all_channels()) {
157 error_report("%s: Extra incoming migration connection; ignoring",
162 qio_channel_set_name(QIO_CHANNEL(cioc
), "migration-socket-incoming");
163 migration_channel_process_incoming(QIO_CHANNEL(cioc
));
167 socket_incoming_migration_end(void *opaque
)
169 QIONetListener
*listener
= opaque
;
171 qio_net_listener_disconnect(listener
);
172 object_unref(OBJECT(listener
));
176 socket_start_incoming_migration_internal(SocketAddress
*saddr
,
179 QIONetListener
*listener
= qio_net_listener_new();
180 MigrationIncomingState
*mis
= migration_incoming_get_current();
184 qio_net_listener_set_name(listener
, "migration-socket-listener");
186 if (migrate_multifd()) {
187 num
= migrate_multifd_channels();
188 } else if (migrate_postcopy_preempt()) {
189 num
= RAM_CHANNEL_MAX
;
192 if (qio_net_listener_open_sync(listener
, saddr
, num
, errp
) < 0) {
193 object_unref(OBJECT(listener
));
197 mis
->transport_data
= listener
;
198 mis
->transport_cleanup
= socket_incoming_migration_end
;
200 qio_net_listener_set_client_func_full(listener
,
201 socket_accept_incoming_migration
,
203 g_main_context_get_thread_default());
205 for (i
= 0; i
< listener
->nsioc
; i
++) {
206 SocketAddress
*address
=
207 qio_channel_socket_get_local_address(listener
->sioc
[i
], errp
);
211 migrate_add_address(address
);
212 qapi_free_SocketAddress(address
);
216 void socket_start_incoming_migration(const char *str
, Error
**errp
)
219 SocketAddress
*saddr
= socket_parse(str
, &err
);
221 socket_start_incoming_migration_internal(saddr
, &err
);
223 qapi_free_SocketAddress(saddr
);
224 error_propagate(errp
, err
);