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-common.h"
21 #include "qemu/error-report.h"
22 #include "qapi/error.h"
25 #include "migration.h"
26 #include "qemu-file.h"
27 #include "io/channel-socket.h"
28 #include "io/net-listener.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 int socket_send_channel_destroy(QIOChannel
*send
)
46 object_unref(OBJECT(send
));
47 if (outgoing_args
.saddr
) {
48 qapi_free_SocketAddress(outgoing_args
.saddr
);
49 outgoing_args
.saddr
= NULL
;
54 static SocketAddress
*tcp_build_address(const char *host_port
, Error
**errp
)
58 saddr
= g_new0(SocketAddress
, 1);
59 saddr
->type
= SOCKET_ADDRESS_TYPE_INET
;
61 if (inet_parse(&saddr
->u
.inet
, host_port
, errp
)) {
62 qapi_free_SocketAddress(saddr
);
70 static SocketAddress
*unix_build_address(const char *path
)
74 saddr
= g_new0(SocketAddress
, 1);
75 saddr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
76 saddr
->u
.q_unix
.path
= g_strdup(path
);
82 struct SocketConnectData
{
87 static void socket_connect_data_free(void *opaque
)
89 struct SocketConnectData
*data
= opaque
;
93 g_free(data
->hostname
);
97 static void socket_outgoing_migration(QIOTask
*task
,
100 struct SocketConnectData
*data
= opaque
;
101 QIOChannel
*sioc
= QIO_CHANNEL(qio_task_get_source(task
));
104 if (qio_task_propagate_error(task
, &err
)) {
105 trace_migration_socket_outgoing_error(error_get_pretty(err
));
107 trace_migration_socket_outgoing_connected(data
->hostname
);
109 migration_channel_connect(data
->s
, sioc
, data
->hostname
, err
);
110 object_unref(OBJECT(sioc
));
113 static void socket_start_outgoing_migration(MigrationState
*s
,
114 SocketAddress
*saddr
,
117 QIOChannelSocket
*sioc
= qio_channel_socket_new();
118 struct SocketConnectData
*data
= g_new0(struct SocketConnectData
, 1);
122 /* in case previous migration leaked it */
123 qapi_free_SocketAddress(outgoing_args
.saddr
);
124 outgoing_args
.saddr
= saddr
;
126 if (saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
127 data
->hostname
= g_strdup(saddr
->u
.inet
.host
);
130 qio_channel_set_name(QIO_CHANNEL(sioc
), "migration-socket-outgoing");
131 qio_channel_socket_connect_async(sioc
,
133 socket_outgoing_migration
,
135 socket_connect_data_free
,
139 void tcp_start_outgoing_migration(MigrationState
*s
,
140 const char *host_port
,
144 SocketAddress
*saddr
= tcp_build_address(host_port
, &err
);
146 socket_start_outgoing_migration(s
, saddr
, &err
);
148 error_propagate(errp
, err
);
151 void unix_start_outgoing_migration(MigrationState
*s
,
155 SocketAddress
*saddr
= unix_build_address(path
);
156 socket_start_outgoing_migration(s
, saddr
, errp
);
160 static void socket_accept_incoming_migration(QIONetListener
*listener
,
161 QIOChannelSocket
*cioc
,
164 trace_migration_socket_incoming_accepted();
166 qio_channel_set_name(QIO_CHANNEL(cioc
), "migration-socket-incoming");
167 migration_channel_process_incoming(QIO_CHANNEL(cioc
));
169 if (migration_has_all_channels()) {
170 /* Close listening socket as its no longer needed */
171 qio_net_listener_disconnect(listener
);
172 object_unref(OBJECT(listener
));
177 static void socket_start_incoming_migration(SocketAddress
*saddr
,
180 QIONetListener
*listener
= qio_net_listener_new();
183 qio_net_listener_set_name(listener
, "migration-socket-listener");
185 if (qio_net_listener_open_sync(listener
, saddr
, errp
) < 0) {
186 object_unref(OBJECT(listener
));
190 qio_net_listener_set_client_func_full(listener
,
191 socket_accept_incoming_migration
,
193 g_main_context_get_thread_default());
195 for (i
= 0; i
< listener
->nsioc
; i
++) {
196 SocketAddress
*address
=
197 qio_channel_socket_get_local_address(listener
->sioc
[i
], errp
);
201 migrate_add_address(address
);
202 qapi_free_SocketAddress(address
);
206 void tcp_start_incoming_migration(const char *host_port
, Error
**errp
)
209 SocketAddress
*saddr
= tcp_build_address(host_port
, &err
);
211 socket_start_incoming_migration(saddr
, &err
);
213 qapi_free_SocketAddress(saddr
);
214 error_propagate(errp
, err
);
217 void unix_start_incoming_migration(const char *path
, Error
**errp
)
219 SocketAddress
*saddr
= unix_build_address(path
);
220 socket_start_incoming_migration(saddr
, errp
);
221 qapi_free_SocketAddress(saddr
);