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 "migration/migration.h" 23 #include "migration/qemu-file.h" 24 #include "io/channel-socket.h" 25 #include "trace.h" 26 27 28 static SocketAddress *tcp_build_address(const char *host_port, Error **errp) 29 { 30 SocketAddress *saddr; 31 32 saddr = g_new0(SocketAddress, 1); 33 saddr->type = SOCKET_ADDRESS_TYPE_INET; 34 35 if (inet_parse(&saddr->u.inet, host_port, errp)) { 36 qapi_free_SocketAddress(saddr); 37 return NULL; 38 } 39 40 return saddr; 41 } 42 43 44 static SocketAddress *unix_build_address(const char *path) 45 { 46 SocketAddress *saddr; 47 48 saddr = g_new0(SocketAddress, 1); 49 saddr->type = SOCKET_ADDRESS_TYPE_UNIX; 50 saddr->u.q_unix.path = g_strdup(path); 51 52 return saddr; 53 } 54 55 56 struct SocketConnectData { 57 MigrationState *s; 58 char *hostname; 59 }; 60 61 static void socket_connect_data_free(void *opaque) 62 { 63 struct SocketConnectData *data = opaque; 64 if (!data) { 65 return; 66 } 67 g_free(data->hostname); 68 g_free(data); 69 } 70 71 static void socket_outgoing_migration(QIOTask *task, 72 gpointer opaque) 73 { 74 struct SocketConnectData *data = opaque; 75 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task)); 76 Error *err = NULL; 77 78 if (qio_task_propagate_error(task, &err)) { 79 trace_migration_socket_outgoing_error(error_get_pretty(err)); 80 migrate_fd_error(data->s, err); 81 error_free(err); 82 } else { 83 trace_migration_socket_outgoing_connected(data->hostname); 84 migration_channel_connect(data->s, sioc, data->hostname); 85 } 86 object_unref(OBJECT(sioc)); 87 } 88 89 static void socket_start_outgoing_migration(MigrationState *s, 90 SocketAddress *saddr, 91 Error **errp) 92 { 93 QIOChannelSocket *sioc = qio_channel_socket_new(); 94 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1); 95 96 data->s = s; 97 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) { 98 data->hostname = g_strdup(saddr->u.inet.host); 99 } 100 101 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing"); 102 qio_channel_socket_connect_async(sioc, 103 saddr, 104 socket_outgoing_migration, 105 data, 106 socket_connect_data_free); 107 qapi_free_SocketAddress(saddr); 108 } 109 110 void tcp_start_outgoing_migration(MigrationState *s, 111 const char *host_port, 112 Error **errp) 113 { 114 Error *err = NULL; 115 SocketAddress *saddr = tcp_build_address(host_port, &err); 116 if (!err) { 117 socket_start_outgoing_migration(s, saddr, &err); 118 } 119 error_propagate(errp, err); 120 } 121 122 void unix_start_outgoing_migration(MigrationState *s, 123 const char *path, 124 Error **errp) 125 { 126 SocketAddress *saddr = unix_build_address(path); 127 socket_start_outgoing_migration(s, saddr, errp); 128 } 129 130 131 static gboolean socket_accept_incoming_migration(QIOChannel *ioc, 132 GIOCondition condition, 133 gpointer opaque) 134 { 135 QIOChannelSocket *sioc; 136 Error *err = NULL; 137 138 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), 139 &err); 140 if (!sioc) { 141 error_report("could not accept migration connection (%s)", 142 error_get_pretty(err)); 143 goto out; 144 } 145 146 trace_migration_socket_incoming_accepted(); 147 148 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-incoming"); 149 migration_channel_process_incoming(migrate_get_current(), 150 QIO_CHANNEL(sioc)); 151 object_unref(OBJECT(sioc)); 152 153 out: 154 /* Close listening socket as its no longer needed */ 155 qio_channel_close(ioc, NULL); 156 return FALSE; /* unregister */ 157 } 158 159 160 static void socket_start_incoming_migration(SocketAddress *saddr, 161 Error **errp) 162 { 163 QIOChannelSocket *listen_ioc = qio_channel_socket_new(); 164 165 qio_channel_set_name(QIO_CHANNEL(listen_ioc), 166 "migration-socket-listener"); 167 168 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) { 169 object_unref(OBJECT(listen_ioc)); 170 qapi_free_SocketAddress(saddr); 171 return; 172 } 173 174 qio_channel_add_watch(QIO_CHANNEL(listen_ioc), 175 G_IO_IN, 176 socket_accept_incoming_migration, 177 listen_ioc, 178 (GDestroyNotify)object_unref); 179 qapi_free_SocketAddress(saddr); 180 } 181 182 void tcp_start_incoming_migration(const char *host_port, Error **errp) 183 { 184 Error *err = NULL; 185 SocketAddress *saddr = tcp_build_address(host_port, &err); 186 if (!err) { 187 socket_start_incoming_migration(saddr, &err); 188 } 189 error_propagate(errp, err); 190 } 191 192 void unix_start_incoming_migration(const char *path, Error **errp) 193 { 194 SocketAddress *saddr = unix_build_address(path); 195 socket_start_incoming_migration(saddr, errp); 196 } 197