xref: /openbmc/qemu/migration/socket.c (revision ce799a04)
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 #include "options.h"
31 
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 
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 
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 
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 
87 static void socket_outgoing_migration(QIOTask *task,
88                                       gpointer opaque)
89 {
90     struct SocketConnectData *data = opaque;
91     QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
92     Error *err = NULL;
93 
94     if (qio_task_propagate_error(task, &err)) {
95         trace_migration_socket_outgoing_error(error_get_pretty(err));
96            goto out;
97     }
98 
99     trace_migration_socket_outgoing_connected(data->hostname);
100 
101     if (migrate_zero_copy_send() &&
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");
104     }
105 
106 out:
107     migration_channel_connect(data->s, sioc, data->hostname, err);
108     object_unref(OBJECT(sioc));
109 }
110 
111 static void
112 socket_start_outgoing_migration_internal(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 socket_start_outgoing_migration(MigrationState *s,
139                                      const char *str,
140                                      Error **errp)
141 {
142     Error *err = NULL;
143     SocketAddress *saddr = socket_parse(str, &err);
144     if (!err) {
145         socket_start_outgoing_migration_internal(s, saddr, &err);
146     }
147     error_propagate(errp, err);
148 }
149 
150 static void socket_accept_incoming_migration(QIONetListener *listener,
151                                              QIOChannelSocket *cioc,
152                                              gpointer opaque)
153 {
154     trace_migration_socket_incoming_accepted();
155 
156     if (migration_has_all_channels()) {
157         error_report("%s: Extra incoming migration connection; ignoring",
158                      __func__);
159         return;
160     }
161 
162     qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
163     migration_channel_process_incoming(QIO_CHANNEL(cioc));
164 }
165 
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 }
174 
175 static void
176 socket_start_incoming_migration_internal(SocketAddress *saddr,
177                                          Error **errp)
178 {
179     QIONetListener *listener = qio_net_listener_new();
180     MigrationIncomingState *mis = migration_incoming_get_current();
181     size_t i;
182     int num = 1;
183 
184     qio_net_listener_set_name(listener, "migration-socket-listener");
185 
186     if (migrate_multifd()) {
187         num = migrate_multifd_channels();
188     } else if (migrate_postcopy_preempt()) {
189         num = RAM_CHANNEL_MAX;
190     }
191 
192     if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
193         object_unref(OBJECT(listener));
194         return;
195     }
196 
197     mis->transport_data = listener;
198     mis->transport_cleanup = socket_incoming_migration_end;
199 
200     qio_net_listener_set_client_func_full(listener,
201                                           socket_accept_incoming_migration,
202                                           NULL, NULL,
203                                           g_main_context_get_thread_default());
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);
212         qapi_free_SocketAddress(address);
213     }
214 }
215 
216 void socket_start_incoming_migration(const char *str, Error **errp)
217 {
218     Error *err = NULL;
219     SocketAddress *saddr = socket_parse(str, &err);
220     if (!err) {
221         socket_start_incoming_migration_internal(saddr, &err);
222     }
223     qapi_free_SocketAddress(saddr);
224     error_propagate(errp, err);
225 }
226