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