Merge tag 'pull-target-arm-20231219' of https://git.linaro.org/people/pmaydell/qemu...
[qemu/ar7.git] / migration / socket.c
blob98e3ea1514719d7059c30a192ff265e1dc1ba28c
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"
31 #include "qapi/clone-visitor.h"
32 #include "qapi/qapi-visit-sockets.h"
34 struct SocketOutgoingArgs {
35 SocketAddress *saddr;
36 } outgoing_args;
38 void socket_send_channel_create(QIOTaskFunc f, void *data)
40 QIOChannelSocket *sioc = qio_channel_socket_new();
41 qio_channel_socket_connect_async(sioc, outgoing_args.saddr,
42 f, data, NULL, NULL);
45 QIOChannel *socket_send_channel_create_sync(Error **errp)
47 QIOChannelSocket *sioc = qio_channel_socket_new();
49 if (!outgoing_args.saddr) {
50 object_unref(OBJECT(sioc));
51 error_setg(errp, "Initial sock address not set!");
52 return NULL;
55 if (qio_channel_socket_connect_sync(sioc, outgoing_args.saddr, errp) < 0) {
56 object_unref(OBJECT(sioc));
57 return NULL;
60 return QIO_CHANNEL(sioc);
63 int socket_send_channel_destroy(QIOChannel *send)
65 /* Remove channel */
66 object_unref(OBJECT(send));
67 if (outgoing_args.saddr) {
68 qapi_free_SocketAddress(outgoing_args.saddr);
69 outgoing_args.saddr = NULL;
71 return 0;
74 struct SocketConnectData {
75 MigrationState *s;
76 char *hostname;
79 static void socket_connect_data_free(void *opaque)
81 struct SocketConnectData *data = opaque;
82 if (!data) {
83 return;
85 g_free(data->hostname);
86 g_free(data);
89 static void socket_outgoing_migration(QIOTask *task,
90 gpointer opaque)
92 struct SocketConnectData *data = opaque;
93 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
94 Error *err = NULL;
96 if (qio_task_propagate_error(task, &err)) {
97 trace_migration_socket_outgoing_error(error_get_pretty(err));
98 goto out;
101 trace_migration_socket_outgoing_connected(data->hostname);
103 if (migrate_zero_copy_send() &&
104 !qio_channel_has_feature(sioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
105 error_setg(&err, "Zero copy send feature not detected in host kernel");
108 out:
109 migration_channel_connect(data->s, sioc, data->hostname, err);
110 object_unref(OBJECT(sioc));
113 void socket_start_outgoing_migration(MigrationState *s,
114 SocketAddress *saddr,
115 Error **errp)
117 QIOChannelSocket *sioc = qio_channel_socket_new();
118 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
119 SocketAddress *addr = QAPI_CLONE(SocketAddress, saddr);
121 data->s = s;
123 /* in case previous migration leaked it */
124 qapi_free_SocketAddress(outgoing_args.saddr);
125 outgoing_args.saddr = addr;
127 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
128 data->hostname = g_strdup(saddr->u.inet.host);
131 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
132 qio_channel_socket_connect_async(sioc,
133 saddr,
134 socket_outgoing_migration,
135 data,
136 socket_connect_data_free,
137 NULL);
140 static void socket_accept_incoming_migration(QIONetListener *listener,
141 QIOChannelSocket *cioc,
142 gpointer opaque)
144 trace_migration_socket_incoming_accepted();
146 if (migration_has_all_channels()) {
147 error_report("%s: Extra incoming migration connection; ignoring",
148 __func__);
149 return;
152 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
153 migration_channel_process_incoming(QIO_CHANNEL(cioc));
156 static void
157 socket_incoming_migration_end(void *opaque)
159 QIONetListener *listener = opaque;
161 qio_net_listener_disconnect(listener);
162 object_unref(OBJECT(listener));
165 void socket_start_incoming_migration(SocketAddress *saddr,
166 Error **errp)
168 QIONetListener *listener = qio_net_listener_new();
169 MigrationIncomingState *mis = migration_incoming_get_current();
170 size_t i;
171 int num = 1;
173 qio_net_listener_set_name(listener, "migration-socket-listener");
175 if (migrate_multifd()) {
176 num = migrate_multifd_channels();
177 } else if (migrate_postcopy_preempt()) {
178 num = RAM_CHANNEL_MAX;
181 if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
182 object_unref(OBJECT(listener));
183 return;
186 mis->transport_data = listener;
187 mis->transport_cleanup = socket_incoming_migration_end;
189 qio_net_listener_set_client_func_full(listener,
190 socket_accept_incoming_migration,
191 NULL, NULL,
192 g_main_context_get_thread_default());
194 for (i = 0; i < listener->nsioc; i++) {
195 SocketAddress *address =
196 qio_channel_socket_get_local_address(listener->sioc[i], errp);
197 if (!address) {
198 return;
200 migrate_add_address(address);
201 qapi_free_SocketAddress(address);