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"
19 #include "qemu-common.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"
31 struct SocketOutgoingArgs
{
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
,
42 int socket_send_channel_destroy(QIOChannel
*send
)
45 object_unref(OBJECT(send
));
46 if (outgoing_args
.saddr
) {
47 qapi_free_SocketAddress(outgoing_args
.saddr
);
48 outgoing_args
.saddr
= NULL
;
53 static SocketAddress
*tcp_build_address(const char *host_port
, Error
**errp
)
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
);
69 static SocketAddress
*unix_build_address(const char *path
)
73 saddr
= g_new0(SocketAddress
, 1);
74 saddr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
75 saddr
->u
.q_unix
.path
= g_strdup(path
);
81 struct SocketConnectData
{
86 static void socket_connect_data_free(void *opaque
)
88 struct SocketConnectData
*data
= opaque
;
92 g_free(data
->hostname
);
96 static void socket_outgoing_migration(QIOTask
*task
,
99 struct SocketConnectData
*data
= opaque
;
100 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
103 if (qio_task_propagate_error(task
, &err
)) {
104 trace_migration_socket_outgoing_error(error_get_pretty(err
));
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
,
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 tcp_start_outgoing_migration(MigrationState
*s
,
139 const char *host_port
,
143 SocketAddress
*saddr
= tcp_build_address(host_port
, &err
);
145 socket_start_outgoing_migration(s
, saddr
, &err
);
147 error_propagate(errp
, err
);
150 void unix_start_outgoing_migration(MigrationState
*s
,
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
,
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
);
172 object_unref(OBJECT(listener
));
174 if (!migrate_use_multifd()) {
175 migration_incoming_process();
181 static void socket_start_incoming_migration(SocketAddress
*saddr
,
184 QIONetListener
*listener
= qio_net_listener_new();
186 qio_net_listener_set_name(listener
, "migration-socket-listener");
188 if (qio_net_listener_open_sync(listener
, saddr
, errp
) < 0) {
189 object_unref(OBJECT(listener
));
193 qio_net_listener_set_client_func_full(listener
,
194 socket_accept_incoming_migration
,
196 g_main_context_get_thread_default());
199 void tcp_start_incoming_migration(const char *host_port
, Error
**errp
)
202 SocketAddress
*saddr
= tcp_build_address(host_port
, &err
);
204 socket_start_incoming_migration(saddr
, &err
);
206 qapi_free_SocketAddress(saddr
);
207 error_propagate(errp
, err
);
210 void unix_start_incoming_migration(const char *path
, Error
**errp
)
212 SocketAddress
*saddr
= unix_build_address(path
);
213 socket_start_incoming_migration(saddr
, errp
);
214 qapi_free_SocketAddress(saddr
);