1 /* 2 * QEMU live migration via socket 3 * 4 * Copyright Red Hat, Inc. 2009-2016 5 * 6 * Authors: 7 * Chris Lalancette <clalance@redhat.com> 8 * Daniel P. Berrange <berrange@redhat.com> 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 * 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. 15 */ 16 17 #include "qemu/osdep.h" 18 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" 29 30 31 struct SocketOutgoingArgs { 32 SocketAddress *saddr; 33 } outgoing_args; 34 35 void socket_send_channel_create(QIOTaskFunc f, void *data) 36 { 37 QIOChannelSocket *sioc = qio_channel_socket_new(); 38 qio_channel_socket_connect_async(sioc, outgoing_args.saddr, 39 f, data, NULL, NULL); 40 } 41 42 int socket_send_channel_destroy(QIOChannel *send) 43 { 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; 49 } 50 return 0; 51 } 52 53 static SocketAddress *tcp_build_address(const char *host_port, Error **errp) 54 { 55 SocketAddress *saddr; 56 57 saddr = g_new0(SocketAddress, 1); 58 saddr->type = SOCKET_ADDRESS_TYPE_INET; 59 60 if (inet_parse(&saddr->u.inet, host_port, errp)) { 61 qapi_free_SocketAddress(saddr); 62 return NULL; 63 } 64 65 return saddr; 66 } 67 68 69 static SocketAddress *unix_build_address(const char *path) 70 { 71 SocketAddress *saddr; 72 73 saddr = g_new0(SocketAddress, 1); 74 saddr->type = SOCKET_ADDRESS_TYPE_UNIX; 75 saddr->u.q_unix.path = g_strdup(path); 76 77 return saddr; 78 } 79 80 81 struct SocketConnectData { 82 MigrationState *s; 83 char *hostname; 84 }; 85 86 static void socket_connect_data_free(void *opaque) 87 { 88 struct SocketConnectData *data = opaque; 89 if (!data) { 90 return; 91 } 92 g_free(data->hostname); 93 g_free(data); 94 } 95 96 static void socket_outgoing_migration(QIOTask *task, 97 gpointer opaque) 98 { 99 struct SocketConnectData *data = opaque; 100 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task)); 101 Error *err = NULL; 102 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); 107 } 108 migration_channel_connect(data->s, sioc, data->hostname, err); 109 object_unref(OBJECT(sioc)); 110 } 111 112 static void socket_start_outgoing_migration(MigrationState *s, 113 SocketAddress *saddr, 114 Error **errp) 115 { 116 QIOChannelSocket *sioc = qio_channel_socket_new(); 117 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1); 118 119 data->s = s; 120 121 /* in case previous migration leaked it */ 122 qapi_free_SocketAddress(outgoing_args.saddr); 123 outgoing_args.saddr = saddr; 124 125 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) { 126 data->hostname = g_strdup(saddr->u.inet.host); 127 } 128 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); 136 } 137 138 void tcp_start_outgoing_migration(MigrationState *s, 139 const char *host_port, 140 Error **errp) 141 { 142 Error *err = NULL; 143 SocketAddress *saddr = tcp_build_address(host_port, &err); 144 if (!err) { 145 socket_start_outgoing_migration(s, saddr, &err); 146 } 147 error_propagate(errp, err); 148 } 149 150 void unix_start_outgoing_migration(MigrationState *s, 151 const char *path, 152 Error **errp) 153 { 154 SocketAddress *saddr = unix_build_address(path); 155 socket_start_outgoing_migration(s, saddr, errp); 156 } 157 158 159 static void socket_accept_incoming_migration(QIONetListener *listener, 160 QIOChannelSocket *cioc, 161 gpointer opaque) 162 { 163 trace_migration_socket_incoming_accepted(); 164 165 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming"); 166 migration_channel_process_incoming(QIO_CHANNEL(cioc)); 167 168 if (migration_has_all_channels()) { 169 /* Close listening socket as its no longer needed */ 170 qio_net_listener_disconnect(listener); 171 object_unref(OBJECT(listener)); 172 } 173 } 174 175 176 static void socket_start_incoming_migration(SocketAddress *saddr, 177 Error **errp) 178 { 179 QIONetListener *listener = qio_net_listener_new(); 180 181 qio_net_listener_set_name(listener, "migration-socket-listener"); 182 183 if (qio_net_listener_open_sync(listener, saddr, errp) < 0) { 184 object_unref(OBJECT(listener)); 185 return; 186 } 187 188 qio_net_listener_set_client_func_full(listener, 189 socket_accept_incoming_migration, 190 NULL, NULL, 191 g_main_context_get_thread_default()); 192 } 193 194 void tcp_start_incoming_migration(const char *host_port, Error **errp) 195 { 196 Error *err = NULL; 197 SocketAddress *saddr = tcp_build_address(host_port, &err); 198 if (!err) { 199 socket_start_incoming_migration(saddr, &err); 200 } 201 qapi_free_SocketAddress(saddr); 202 error_propagate(errp, err); 203 } 204 205 void unix_start_incoming_migration(const char *path, Error **errp) 206 { 207 SocketAddress *saddr = unix_build_address(path); 208 socket_start_incoming_migration(saddr, errp); 209 qapi_free_SocketAddress(saddr); 210 } 211