xref: /openbmc/qemu/migration/socket.c (revision 8f9abdf5)
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 #include "qemu/cutils.h"
19 
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 
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 QIOChannel *socket_send_channel_create_sync(Error **errp)
43 {
44     QIOChannelSocket *sioc = qio_channel_socket_new();
45 
46     if (!outgoing_args.saddr) {
47         object_unref(OBJECT(sioc));
48         error_setg(errp, "Initial sock address not set!");
49         return NULL;
50     }
51 
52     if (qio_channel_socket_connect_sync(sioc, outgoing_args.saddr, errp) < 0) {
53         object_unref(OBJECT(sioc));
54         return NULL;
55     }
56 
57     return QIO_CHANNEL(sioc);
58 }
59 
60 int socket_send_channel_destroy(QIOChannel *send)
61 {
62     /* Remove channel */
63     object_unref(OBJECT(send));
64     if (outgoing_args.saddr) {
65         qapi_free_SocketAddress(outgoing_args.saddr);
66         outgoing_args.saddr = NULL;
67     }
68     return 0;
69 }
70 
71 struct SocketConnectData {
72     MigrationState *s;
73     char *hostname;
74 };
75 
76 static void socket_connect_data_free(void *opaque)
77 {
78     struct SocketConnectData *data = opaque;
79     if (!data) {
80         return;
81     }
82     g_free(data->hostname);
83     g_free(data);
84 }
85 
86 static void socket_outgoing_migration(QIOTask *task,
87                                       gpointer opaque)
88 {
89     struct SocketConnectData *data = opaque;
90     QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
91     Error *err = NULL;
92 
93     if (qio_task_propagate_error(task, &err)) {
94         trace_migration_socket_outgoing_error(error_get_pretty(err));
95            goto out;
96     }
97 
98     trace_migration_socket_outgoing_connected(data->hostname);
99 
100     if (migrate_use_zero_copy_send() &&
101         !qio_channel_has_feature(sioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
102         error_setg(&err, "Zero copy send feature not detected in host kernel");
103     }
104 
105 out:
106     migration_channel_connect(data->s, sioc, data->hostname, err);
107     object_unref(OBJECT(sioc));
108 }
109 
110 static void
111 socket_start_outgoing_migration_internal(MigrationState *s,
112                                          SocketAddress *saddr,
113                                          Error **errp)
114 {
115     QIOChannelSocket *sioc = qio_channel_socket_new();
116     struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
117 
118     data->s = s;
119 
120     /* in case previous migration leaked it */
121     qapi_free_SocketAddress(outgoing_args.saddr);
122     outgoing_args.saddr = saddr;
123 
124     if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
125         data->hostname = g_strdup(saddr->u.inet.host);
126     }
127 
128     qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
129     qio_channel_socket_connect_async(sioc,
130                                      saddr,
131                                      socket_outgoing_migration,
132                                      data,
133                                      socket_connect_data_free,
134                                      NULL);
135 }
136 
137 void socket_start_outgoing_migration(MigrationState *s,
138                                      const char *str,
139                                      Error **errp)
140 {
141     Error *err = NULL;
142     SocketAddress *saddr = socket_parse(str, &err);
143     if (!err) {
144         socket_start_outgoing_migration_internal(s, saddr, &err);
145     }
146     error_propagate(errp, err);
147 }
148 
149 static void socket_accept_incoming_migration(QIONetListener *listener,
150                                              QIOChannelSocket *cioc,
151                                              gpointer opaque)
152 {
153     trace_migration_socket_incoming_accepted();
154 
155     if (migration_has_all_channels()) {
156         error_report("%s: Extra incoming migration connection; ignoring",
157                      __func__);
158         return;
159     }
160 
161     qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
162     migration_channel_process_incoming(QIO_CHANNEL(cioc));
163 }
164 
165 static void
166 socket_incoming_migration_end(void *opaque)
167 {
168     QIONetListener *listener = opaque;
169 
170     qio_net_listener_disconnect(listener);
171     object_unref(OBJECT(listener));
172 }
173 
174 static void
175 socket_start_incoming_migration_internal(SocketAddress *saddr,
176                                          Error **errp)
177 {
178     QIONetListener *listener = qio_net_listener_new();
179     MigrationIncomingState *mis = migration_incoming_get_current();
180     size_t i;
181     int num = 1;
182 
183     qio_net_listener_set_name(listener, "migration-socket-listener");
184 
185     if (migrate_use_multifd()) {
186         num = migrate_multifd_channels();
187     } else if (migrate_postcopy_preempt()) {
188         num = RAM_CHANNEL_MAX;
189     }
190 
191     if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
192         object_unref(OBJECT(listener));
193         return;
194     }
195 
196     mis->transport_data = listener;
197     mis->transport_cleanup = socket_incoming_migration_end;
198 
199     qio_net_listener_set_client_func_full(listener,
200                                           socket_accept_incoming_migration,
201                                           NULL, NULL,
202                                           g_main_context_get_thread_default());
203 
204     for (i = 0; i < listener->nsioc; i++)  {
205         SocketAddress *address =
206             qio_channel_socket_get_local_address(listener->sioc[i], errp);
207         if (!address) {
208             return;
209         }
210         migrate_add_address(address);
211         qapi_free_SocketAddress(address);
212     }
213 }
214 
215 void socket_start_incoming_migration(const char *str, Error **errp)
216 {
217     Error *err = NULL;
218     SocketAddress *saddr = socket_parse(str, &err);
219     if (!err) {
220         socket_start_incoming_migration_internal(saddr, &err);
221     }
222     qapi_free_SocketAddress(saddr);
223     error_propagate(errp, err);
224 }
225