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