ppc/xive: Add support for the PC MMIOs
[qemu/armbru.git] / migration / socket.c
blob1b6f5baefbc36281c7f75d2ba2ecf7727743206d
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"
29 #include "postcopy-ram.h"
30 #include "options.h"
32 struct SocketOutgoingArgs {
33 SocketAddress *saddr;
34 } outgoing_args;
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,
40 f, data, NULL, NULL);
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!");
50 return NULL;
53 if (qio_channel_socket_connect_sync(sioc, outgoing_args.saddr, errp) < 0) {
54 object_unref(OBJECT(sioc));
55 return NULL;
58 return QIO_CHANNEL(sioc);
61 int socket_send_channel_destroy(QIOChannel *send)
63 /* Remove channel */
64 object_unref(OBJECT(send));
65 if (outgoing_args.saddr) {
66 qapi_free_SocketAddress(outgoing_args.saddr);
67 outgoing_args.saddr = NULL;
69 return 0;
72 struct SocketConnectData {
73 MigrationState *s;
74 char *hostname;
77 static void socket_connect_data_free(void *opaque)
79 struct SocketConnectData *data = opaque;
80 if (!data) {
81 return;
83 g_free(data->hostname);
84 g_free(data);
87 static void socket_outgoing_migration(QIOTask *task,
88 gpointer opaque)
90 struct SocketConnectData *data = opaque;
91 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
92 Error *err = NULL;
94 if (qio_task_propagate_error(task, &err)) {
95 trace_migration_socket_outgoing_error(error_get_pretty(err));
96 goto out;
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");
106 out:
107 migration_channel_connect(data->s, sioc, data->hostname, err);
108 object_unref(OBJECT(sioc));
111 static void
112 socket_start_outgoing_migration_internal(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 socket_start_outgoing_migration(MigrationState *s,
139 const char *str,
140 Error **errp)
142 Error *err = NULL;
143 SocketAddress *saddr = socket_parse(str, &err);
144 if (!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,
152 gpointer opaque)
154 trace_migration_socket_incoming_accepted();
156 if (migration_has_all_channels()) {
157 error_report("%s: Extra incoming migration connection; ignoring",
158 __func__);
159 return;
162 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
163 migration_channel_process_incoming(QIO_CHANNEL(cioc));
166 static void
167 socket_incoming_migration_end(void *opaque)
169 QIONetListener *listener = opaque;
171 qio_net_listener_disconnect(listener);
172 object_unref(OBJECT(listener));
175 static void
176 socket_start_incoming_migration_internal(SocketAddress *saddr,
177 Error **errp)
179 QIONetListener *listener = qio_net_listener_new();
180 MigrationIncomingState *mis = migration_incoming_get_current();
181 size_t i;
182 int num = 1;
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));
194 return;
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,
202 NULL, NULL,
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);
208 if (!address) {
209 return;
211 migrate_add_address(address);
212 qapi_free_SocketAddress(address);
216 void socket_start_incoming_migration(const char *str, Error **errp)
218 Error *err = NULL;
219 SocketAddress *saddr = socket_parse(str, &err);
220 if (!err) {
221 socket_start_incoming_migration_internal(saddr, &err);
223 qapi_free_SocketAddress(saddr);
224 error_propagate(errp, err);