]> git.proxmox.com Git - mirror_qemu.git/blob - migration/socket.c
Merge remote-tracking branch 'remotes/berrange/tags/qio-next-pull-request' into staging
[mirror_qemu.git] / migration / socket.c
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 "channel.h"
23 #include "socket.h"
24 #include "migration.h"
25 #include "qemu-file.h"
26 #include "io/channel-socket.h"
27 #include "trace.h"
28
29
30 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
31 {
32 SocketAddress *saddr;
33
34 saddr = g_new0(SocketAddress, 1);
35 saddr->type = SOCKET_ADDRESS_TYPE_INET;
36
37 if (inet_parse(&saddr->u.inet, host_port, errp)) {
38 qapi_free_SocketAddress(saddr);
39 return NULL;
40 }
41
42 return saddr;
43 }
44
45
46 static SocketAddress *unix_build_address(const char *path)
47 {
48 SocketAddress *saddr;
49
50 saddr = g_new0(SocketAddress, 1);
51 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
52 saddr->u.q_unix.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 } else {
83 trace_migration_socket_outgoing_connected(data->hostname);
84 }
85 migration_channel_connect(data->s, sioc, data->hostname, err);
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 NULL);
108 qapi_free_SocketAddress(saddr);
109 }
110
111 void tcp_start_outgoing_migration(MigrationState *s,
112 const char *host_port,
113 Error **errp)
114 {
115 Error *err = NULL;
116 SocketAddress *saddr = tcp_build_address(host_port, &err);
117 if (!err) {
118 socket_start_outgoing_migration(s, saddr, &err);
119 }
120 error_propagate(errp, err);
121 }
122
123 void unix_start_outgoing_migration(MigrationState *s,
124 const char *path,
125 Error **errp)
126 {
127 SocketAddress *saddr = unix_build_address(path);
128 socket_start_outgoing_migration(s, saddr, errp);
129 }
130
131
132 static gboolean socket_accept_incoming_migration(QIOChannel *ioc,
133 GIOCondition condition,
134 gpointer opaque)
135 {
136 QIOChannelSocket *sioc;
137 Error *err = NULL;
138
139 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
140 &err);
141 if (!sioc) {
142 error_report("could not accept migration connection (%s)",
143 error_get_pretty(err));
144 goto out;
145 }
146
147 trace_migration_socket_incoming_accepted();
148
149 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-incoming");
150 migration_channel_process_incoming(QIO_CHANNEL(sioc));
151 object_unref(OBJECT(sioc));
152
153 out:
154 if (migration_has_all_channels()) {
155 /* Close listening socket as its no longer needed */
156 qio_channel_close(ioc, NULL);
157 return G_SOURCE_REMOVE;
158 } else {
159 return G_SOURCE_CONTINUE;
160 }
161 }
162
163
164 static void socket_start_incoming_migration(SocketAddress *saddr,
165 Error **errp)
166 {
167 QIOChannelSocket *listen_ioc = qio_channel_socket_new();
168
169 qio_channel_set_name(QIO_CHANNEL(listen_ioc),
170 "migration-socket-listener");
171
172 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
173 object_unref(OBJECT(listen_ioc));
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 }
183
184 void tcp_start_incoming_migration(const char *host_port, Error **errp)
185 {
186 Error *err = NULL;
187 SocketAddress *saddr = tcp_build_address(host_port, &err);
188 if (!err) {
189 socket_start_incoming_migration(saddr, &err);
190 }
191 qapi_free_SocketAddress(saddr);
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 qapi_free_SocketAddress(saddr);
200 }