blob: 1b6f5baefbc36281c7f75d2ba2ecf7727743206d [file] [log] [blame]
Chris Lalancette4951f652009-08-05 17:24:29 +02001/*
Cao jin474c6242016-07-28 16:54:34 +08002 * QEMU live migration via socket
Chris Lalancette4951f652009-08-05 17:24:29 +02003 *
Daniel P. Berranged9844642016-04-27 11:05:02 +01004 * Copyright Red Hat, Inc. 2009-2016
Chris Lalancette4951f652009-08-05 17:24:29 +02005 *
6 * Authors:
7 * Chris Lalancette <clalance@redhat.com>
Daniel P. Berranged9844642016-04-27 11:05:02 +01008 * Daniel P. Berrange <berrange@redhat.com>
Chris Lalancette4951f652009-08-05 17:24:29 +02009 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010013 * Contributions after 2012-01-13 are licensed under the terms of the
14 * GNU GPL, version 2 or (at your option) any later version.
Chris Lalancette4951f652009-08-05 17:24:29 +020015 */
16
Peter Maydell1393a482016-01-26 18:16:54 +000017#include "qemu/osdep.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010018#include "qemu/cutils.h"
Dr. David Alan Gilbertd99598c2014-03-19 13:34:28 +000019
Dr. David Alan Gilbertd99598c2014-03-19 13:34:28 +000020#include "qemu/error-report.h"
Daniel P. Berranged9844642016-04-27 11:05:02 +010021#include "qapi/error.h"
Juan Quinteladd4339c2017-04-17 17:07:04 +020022#include "channel.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020023#include "socket.h"
Juan Quintela6666c962017-04-24 20:07:27 +020024#include "migration.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020025#include "qemu-file.h"
Daniel P. Berranged9844642016-04-27 11:05:02 +010026#include "io/channel-socket.h"
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +000027#include "io/net-listener.h"
Daniel P. Berranged9844642016-04-27 11:05:02 +010028#include "trace.h"
Peter Xu36f62f12022-07-07 14:55:02 -040029#include "postcopy-ram.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010030#include "options.h"
Chris Lalancette4951f652009-08-05 17:24:29 +020031
Juan Quintela38549562018-02-28 12:05:15 +010032struct SocketOutgoingArgs {
33 SocketAddress *saddr;
34} outgoing_args;
35
36void socket_send_channel_create(QIOTaskFunc f, void *data)
37{
38 QIOChannelSocket *sioc = qio_channel_socket_new();
39 qio_channel_socket_connect_async(sioc, outgoing_args.saddr,
40 f, data, NULL, NULL);
41}
42
Peter Xu36f62f12022-07-07 14:55:02 -040043QIOChannel *socket_send_channel_create_sync(Error **errp)
44{
45 QIOChannelSocket *sioc = qio_channel_socket_new();
46
47 if (!outgoing_args.saddr) {
48 object_unref(OBJECT(sioc));
49 error_setg(errp, "Initial sock address not set!");
50 return NULL;
51 }
52
53 if (qio_channel_socket_connect_sync(sioc, outgoing_args.saddr, errp) < 0) {
54 object_unref(OBJECT(sioc));
55 return NULL;
56 }
57
58 return QIO_CHANNEL(sioc);
59}
60
Juan Quintela38549562018-02-28 12:05:15 +010061int socket_send_channel_destroy(QIOChannel *send)
62{
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;
68 }
69 return 0;
70}
71
Daniel P. Berrangee1226362016-04-27 11:05:16 +010072struct SocketConnectData {
73 MigrationState *s;
74 char *hostname;
75};
76
77static void socket_connect_data_free(void *opaque)
78{
79 struct SocketConnectData *data = opaque;
80 if (!data) {
81 return;
82 }
83 g_free(data->hostname);
84 g_free(data);
85}
86
Daniel P. Berrange60e705c2016-08-11 15:20:58 +010087static void socket_outgoing_migration(QIOTask *task,
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +010088 gpointer opaque)
Chris Lalancette4951f652009-08-05 17:24:29 +020089{
Daniel P. Berrangee1226362016-04-27 11:05:16 +010090 struct SocketConnectData *data = opaque;
Daniel P. Berrange60e705c2016-08-11 15:20:58 +010091 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
92 Error *err = NULL;
Chris Lalancette4951f652009-08-05 17:24:29 +020093
Daniel P. Berrange60e705c2016-08-11 15:20:58 +010094 if (qio_task_propagate_error(task, &err)) {
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +010095 trace_migration_socket_outgoing_error(error_get_pretty(err));
Leonardo Brasabb62952022-05-13 03:28:33 -030096 goto out;
Chris Lalancette4951f652009-08-05 17:24:29 +020097 }
Leonardo Brasabb62952022-05-13 03:28:33 -030098
99 trace_migration_socket_outgoing_connected(data->hostname);
100
Juan Quintelab4bc3422023-03-01 22:17:14 +0100101 if (migrate_zero_copy_send() &&
Leonardo Bras5b1d9ba2022-05-13 03:28:37 -0300102 !qio_channel_has_feature(sioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
103 error_setg(&err, "Zero copy send feature not detected in host kernel");
Leonardo Brasabb62952022-05-13 03:28:33 -0300104 }
105
106out:
Dr. David Alan Gilbert688a3dc2017-12-15 17:16:55 +0000107 migration_channel_connect(data->s, sioc, data->hostname, err);
Daniel P. Berrange60e705c2016-08-11 15:20:58 +0100108 object_unref(OBJECT(sioc));
Chris Lalancette4951f652009-08-05 17:24:29 +0200109}
110
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800111static void
112socket_start_outgoing_migration_internal(MigrationState *s,
113 SocketAddress *saddr,
114 Error **errp)
Chris Lalancette4951f652009-08-05 17:24:29 +0200115{
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +0100116 QIOChannelSocket *sioc = qio_channel_socket_new();
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100117 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
Cao jin474c6242016-07-28 16:54:34 +0800118
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100119 data->s = s;
Juan Quintela38549562018-02-28 12:05:15 +0100120
121 /* in case previous migration leaked it */
122 qapi_free_SocketAddress(outgoing_args.saddr);
123 outgoing_args.saddr = saddr;
124
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200125 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
126 data->hostname = g_strdup(saddr->u.inet.host);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100127 }
Cao jin474c6242016-07-28 16:54:34 +0800128
Daniel P. Berrange6f01f132016-09-30 11:57:14 +0100129 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
Daniel P. Berranged9844642016-04-27 11:05:02 +0100130 qio_channel_socket_connect_async(sioc,
131 saddr,
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +0100132 socket_outgoing_migration,
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100133 data,
Peter Xu8005fdd2018-03-05 14:43:23 +0800134 socket_connect_data_free,
135 NULL);
Chris Lalancette4951f652009-08-05 17:24:29 +0200136}
137
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800138void socket_start_outgoing_migration(MigrationState *s,
139 const char *str,
140 Error **errp)
Daniel P. Berrangee65c67e2016-04-27 11:05:04 +0100141{
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100142 Error *err = NULL;
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800143 SocketAddress *saddr = socket_parse(str, &err);
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100144 if (!err) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800145 socket_start_outgoing_migration_internal(s, saddr, &err);
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100146 }
147 error_propagate(errp, err);
Daniel P. Berrangee65c67e2016-04-27 11:05:04 +0100148}
149
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +0000150static void socket_accept_incoming_migration(QIONetListener *listener,
151 QIOChannelSocket *cioc,
152 gpointer opaque)
Chris Lalancette4951f652009-08-05 17:24:29 +0200153{
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +0100154 trace_migration_socket_incoming_accepted();
Daniel P. Berranged9844642016-04-27 11:05:02 +0100155
Dr. David Alan Gilberta59136f2021-04-21 12:28:33 +0100156 if (migration_has_all_channels()) {
157 error_report("%s: Extra incoming migration connection; ignoring",
158 __func__);
159 return;
160 }
161
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +0000162 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
163 migration_channel_process_incoming(QIO_CHANNEL(cioc));
Chris Lalancette4951f652009-08-05 17:24:29 +0200164}
165
Dr. David Alan Gilberta59136f2021-04-21 12:28:33 +0100166static void
167socket_incoming_migration_end(void *opaque)
168{
169 QIONetListener *listener = opaque;
170
171 qio_net_listener_disconnect(listener);
172 object_unref(OBJECT(listener));
173}
Daniel P. Berranged9844642016-04-27 11:05:02 +0100174
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800175static void
176socket_start_incoming_migration_internal(SocketAddress *saddr,
177 Error **errp)
Chris Lalancette4951f652009-08-05 17:24:29 +0200178{
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +0000179 QIONetListener *listener = qio_net_listener_new();
Dr. David Alan Gilberta59136f2021-04-21 12:28:33 +0100180 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela9aca82b2019-02-27 11:51:27 +0100181 size_t i;
Juan Quintela0705e562019-08-19 18:14:44 +0200182 int num = 1;
Chris Lalancette4951f652009-08-05 17:24:29 +0200183
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +0000184 qio_net_listener_set_name(listener, "migration-socket-listener");
Daniel P. Berrange6f01f132016-09-30 11:57:14 +0100185
Juan Quintela51b07542023-03-01 22:10:29 +0100186 if (migrate_multifd()) {
Juan Quintela0705e562019-08-19 18:14:44 +0200187 num = migrate_multifd_channels();
Peter Xu36f62f12022-07-07 14:55:02 -0400188 } else if (migrate_postcopy_preempt()) {
189 num = RAM_CHANNEL_MAX;
Juan Quintela0705e562019-08-19 18:14:44 +0200190 }
191
192 if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
Daniel P. Berrangebdd847a2018-03-12 14:17:14 +0000193 object_unref(OBJECT(listener));
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200194 return;
Chris Lalancette4951f652009-08-05 17:24:29 +0200195 }
196
Dr. David Alan Gilberta59136f2021-04-21 12:28:33 +0100197 mis->transport_data = listener;
198 mis->transport_cleanup = socket_incoming_migration_end;
199
Peter Xue89f5ff2018-05-02 18:47:17 +0800200 qio_net_listener_set_client_func_full(listener,
201 socket_accept_incoming_migration,
202 NULL, NULL,
203 g_main_context_get_thread_default());
Juan Quintela9aca82b2019-02-27 11:51:27 +0100204
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;
210 }
211 migrate_add_address(address);
Paolo Bonzini5e78bc62019-03-12 14:50:50 +0100212 qapi_free_SocketAddress(address);
Juan Quintela9aca82b2019-02-27 11:51:27 +0100213 }
Chris Lalancette4951f652009-08-05 17:24:29 +0200214}
Daniel P. Berrange6f860ae2016-04-27 11:05:03 +0100215
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800216void socket_start_incoming_migration(const char *str, Error **errp)
Daniel P. Berrangee65c67e2016-04-27 11:05:04 +0100217{
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100218 Error *err = NULL;
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800219 SocketAddress *saddr = socket_parse(str, &err);
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100220 if (!err) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800221 socket_start_incoming_migration_internal(saddr, &err);
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100222 }
Juan Quintela9102d272017-10-27 12:23:29 +0200223 qapi_free_SocketAddress(saddr);
Dr. David Alan Gilbert9308ae52016-09-13 10:08:41 +0100224 error_propagate(errp, err);
Daniel P. Berrangee65c67e2016-04-27 11:05:04 +0100225}