Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 1 | /* |
Cao jin | 474c624 | 2016-07-28 16:54:34 +0800 | [diff] [blame] | 2 | * QEMU live migration via socket |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 3 | * |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 4 | * Copyright Red Hat, Inc. 2009-2016 |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 5 | * |
| 6 | * Authors: |
| 7 | * Chris Lalancette <clalance@redhat.com> |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 8 | * Daniel P. Berrange <berrange@redhat.com> |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 9 | * |
| 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 Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 17 | #include "qemu/osdep.h" |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 18 | #include "qemu/cutils.h" |
Dr. David Alan Gilbert | d99598c | 2014-03-19 13:34:28 +0000 | [diff] [blame] | 19 | |
Dr. David Alan Gilbert | d99598c | 2014-03-19 13:34:28 +0000 | [diff] [blame] | 20 | #include "qemu/error-report.h" |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 21 | #include "qapi/error.h" |
Juan Quintela | dd4339c | 2017-04-17 17:07:04 +0200 | [diff] [blame] | 22 | #include "channel.h" |
Juan Quintela | 61e8b14 | 2017-04-05 17:40:11 +0200 | [diff] [blame] | 23 | #include "socket.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 24 | #include "migration.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 25 | #include "qemu-file.h" |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 26 | #include "io/channel-socket.h" |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 27 | #include "io/net-listener.h" |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 28 | #include "trace.h" |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 29 | #include "postcopy-ram.h" |
Juan Quintela | 1f0776f | 2023-03-01 21:18:45 +0100 | [diff] [blame] | 30 | #include "options.h" |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 31 | |
Juan Quintela | 3854956 | 2018-02-28 12:05:15 +0100 | [diff] [blame] | 32 | struct SocketOutgoingArgs { |
| 33 | SocketAddress *saddr; |
| 34 | } outgoing_args; |
| 35 | |
| 36 | void 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 Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 43 | QIOChannel *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 Quintela | 3854956 | 2018-02-28 12:05:15 +0100 | [diff] [blame] | 61 | int 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. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 72 | struct SocketConnectData { |
| 73 | MigrationState *s; |
| 74 | char *hostname; |
| 75 | }; |
| 76 | |
| 77 | static 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. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 87 | static void socket_outgoing_migration(QIOTask *task, |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 88 | gpointer opaque) |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 89 | { |
Daniel P. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 90 | struct SocketConnectData *data = opaque; |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 91 | QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task)); |
| 92 | Error *err = NULL; |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 93 | |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 94 | if (qio_task_propagate_error(task, &err)) { |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 95 | trace_migration_socket_outgoing_error(error_get_pretty(err)); |
Leonardo Bras | abb6295 | 2022-05-13 03:28:33 -0300 | [diff] [blame] | 96 | goto out; |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 97 | } |
Leonardo Bras | abb6295 | 2022-05-13 03:28:33 -0300 | [diff] [blame] | 98 | |
| 99 | trace_migration_socket_outgoing_connected(data->hostname); |
| 100 | |
Juan Quintela | b4bc342 | 2023-03-01 22:17:14 +0100 | [diff] [blame] | 101 | if (migrate_zero_copy_send() && |
Leonardo Bras | 5b1d9ba | 2022-05-13 03:28:37 -0300 | [diff] [blame] | 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"); |
Leonardo Bras | abb6295 | 2022-05-13 03:28:33 -0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | out: |
Dr. David Alan Gilbert | 688a3dc | 2017-12-15 17:16:55 +0000 | [diff] [blame] | 107 | migration_channel_connect(data->s, sioc, data->hostname, err); |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 108 | object_unref(OBJECT(sioc)); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 111 | static void |
| 112 | socket_start_outgoing_migration_internal(MigrationState *s, |
| 113 | SocketAddress *saddr, |
| 114 | Error **errp) |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 115 | { |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 116 | QIOChannelSocket *sioc = qio_channel_socket_new(); |
Daniel P. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 117 | struct SocketConnectData *data = g_new0(struct SocketConnectData, 1); |
Cao jin | 474c624 | 2016-07-28 16:54:34 +0800 | [diff] [blame] | 118 | |
Daniel P. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 119 | data->s = s; |
Juan Quintela | 3854956 | 2018-02-28 12:05:15 +0100 | [diff] [blame] | 120 | |
| 121 | /* in case previous migration leaked it */ |
| 122 | qapi_free_SocketAddress(outgoing_args.saddr); |
| 123 | outgoing_args.saddr = saddr; |
| 124 | |
Markus Armbruster | bd269eb | 2017-04-26 09:36:41 +0200 | [diff] [blame] | 125 | if (saddr->type == SOCKET_ADDRESS_TYPE_INET) { |
| 126 | data->hostname = g_strdup(saddr->u.inet.host); |
Daniel P. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 127 | } |
Cao jin | 474c624 | 2016-07-28 16:54:34 +0800 | [diff] [blame] | 128 | |
Daniel P. Berrange | 6f01f13 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 129 | qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing"); |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 130 | qio_channel_socket_connect_async(sioc, |
| 131 | saddr, |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 132 | socket_outgoing_migration, |
Daniel P. Berrange | e122636 | 2016-04-27 11:05:16 +0100 | [diff] [blame] | 133 | data, |
Peter Xu | 8005fdd | 2018-03-05 14:43:23 +0800 | [diff] [blame] | 134 | socket_connect_data_free, |
| 135 | NULL); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 138 | void socket_start_outgoing_migration(MigrationState *s, |
| 139 | const char *str, |
| 140 | Error **errp) |
Daniel P. Berrange | e65c67e | 2016-04-27 11:05:04 +0100 | [diff] [blame] | 141 | { |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 142 | Error *err = NULL; |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 143 | SocketAddress *saddr = socket_parse(str, &err); |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 144 | if (!err) { |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 145 | socket_start_outgoing_migration_internal(s, saddr, &err); |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 146 | } |
| 147 | error_propagate(errp, err); |
Daniel P. Berrange | e65c67e | 2016-04-27 11:05:04 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 150 | static void socket_accept_incoming_migration(QIONetListener *listener, |
| 151 | QIOChannelSocket *cioc, |
| 152 | gpointer opaque) |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 153 | { |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 154 | trace_migration_socket_incoming_accepted(); |
Daniel P. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 155 | |
Dr. David Alan Gilbert | a59136f | 2021-04-21 12:28:33 +0100 | [diff] [blame] | 156 | if (migration_has_all_channels()) { |
| 157 | error_report("%s: Extra incoming migration connection; ignoring", |
| 158 | __func__); |
| 159 | return; |
| 160 | } |
| 161 | |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 162 | qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming"); |
| 163 | migration_channel_process_incoming(QIO_CHANNEL(cioc)); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Dr. David Alan Gilbert | a59136f | 2021-04-21 12:28:33 +0100 | [diff] [blame] | 166 | static void |
| 167 | socket_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. Berrange | d984464 | 2016-04-27 11:05:02 +0100 | [diff] [blame] | 174 | |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 175 | static void |
| 176 | socket_start_incoming_migration_internal(SocketAddress *saddr, |
| 177 | Error **errp) |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 178 | { |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 179 | QIONetListener *listener = qio_net_listener_new(); |
Dr. David Alan Gilbert | a59136f | 2021-04-21 12:28:33 +0100 | [diff] [blame] | 180 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 181 | size_t i; |
Juan Quintela | 0705e56 | 2019-08-19 18:14:44 +0200 | [diff] [blame] | 182 | int num = 1; |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 183 | |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 184 | qio_net_listener_set_name(listener, "migration-socket-listener"); |
Daniel P. Berrange | 6f01f13 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 185 | |
Juan Quintela | 51b0754 | 2023-03-01 22:10:29 +0100 | [diff] [blame] | 186 | if (migrate_multifd()) { |
Juan Quintela | 0705e56 | 2019-08-19 18:14:44 +0200 | [diff] [blame] | 187 | num = migrate_multifd_channels(); |
Peter Xu | 36f62f1 | 2022-07-07 14:55:02 -0400 | [diff] [blame] | 188 | } else if (migrate_postcopy_preempt()) { |
| 189 | num = RAM_CHANNEL_MAX; |
Juan Quintela | 0705e56 | 2019-08-19 18:14:44 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) { |
Daniel P. Berrange | bdd847a | 2018-03-12 14:17:14 +0000 | [diff] [blame] | 193 | object_unref(OBJECT(listener)); |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 194 | return; |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Dr. David Alan Gilbert | a59136f | 2021-04-21 12:28:33 +0100 | [diff] [blame] | 197 | mis->transport_data = listener; |
| 198 | mis->transport_cleanup = socket_incoming_migration_end; |
| 199 | |
Peter Xu | e89f5ff | 2018-05-02 18:47:17 +0800 | [diff] [blame] | 200 | qio_net_listener_set_client_func_full(listener, |
| 201 | socket_accept_incoming_migration, |
| 202 | NULL, NULL, |
| 203 | g_main_context_get_thread_default()); |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 204 | |
| 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 Bonzini | 5e78bc6 | 2019-03-12 14:50:50 +0100 | [diff] [blame] | 212 | qapi_free_SocketAddress(address); |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 213 | } |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 214 | } |
Daniel P. Berrange | 6f860ae | 2016-04-27 11:05:03 +0100 | [diff] [blame] | 215 | |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 216 | void socket_start_incoming_migration(const char *str, Error **errp) |
Daniel P. Berrange | e65c67e | 2016-04-27 11:05:04 +0100 | [diff] [blame] | 217 | { |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 218 | Error *err = NULL; |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 219 | SocketAddress *saddr = socket_parse(str, &err); |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 220 | if (!err) { |
Longpeng(Mike) | d658f65 | 2020-08-06 15:40:29 +0800 | [diff] [blame] | 221 | socket_start_incoming_migration_internal(saddr, &err); |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 222 | } |
Juan Quintela | 9102d27 | 2017-10-27 12:23:29 +0200 | [diff] [blame] | 223 | qapi_free_SocketAddress(saddr); |
Dr. David Alan Gilbert | 9308ae5 | 2016-09-13 10:08:41 +0100 | [diff] [blame] | 224 | error_propagate(errp, err); |
Daniel P. Berrange | e65c67e | 2016-04-27 11:05:04 +0100 | [diff] [blame] | 225 | } |