exec: check that alignment is a power of two
[qemu.git] / migration / socket.c
blob3456eb76e9309fca3c587ddf9190beb4efc4f395
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"
19 #include "qemu-common.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);
172 object_unref(OBJECT(listener));
174 if (!migrate_use_multifd()) {
175 migration_incoming_process();
181 static void socket_start_incoming_migration(SocketAddress *saddr,
182 Error **errp)
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));
190 return;
193 qio_net_listener_set_client_func_full(listener,
194 socket_accept_incoming_migration,
195 NULL, NULL,
196 g_main_context_get_thread_default());
199 void tcp_start_incoming_migration(const char *host_port, Error **errp)
201 Error *err = NULL;
202 SocketAddress *saddr = tcp_build_address(host_port, &err);
203 if (!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);