hw/arm/nseries: Use TYPE_TMP105 instead of hardcoded string
[qemu/ar7.git] / migration / socket.c
blob49bacf78fd68aa7e8ccb3ace97ace09c84f5d4f6
1 /*
2 * QEMU live migration via socket
4 * Copyright Red Hat, Inc. 2009-2016
6 * Authors:
7 * Chris Lalancette <clalance@redhat.com>
8 * Daniel P. Berrange <berrange@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
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.
17 #include "qemu/osdep.h"
18 #include "qemu/cutils.h"
20 #include "qemu-common.h"
21 #include "qemu/error-report.h"
22 #include "qapi/error.h"
23 #include "channel.h"
24 #include "socket.h"
25 #include "migration.h"
26 #include "qemu-file.h"
27 #include "io/channel-socket.h"
28 #include "io/net-listener.h"
29 #include "trace.h"
32 struct SocketOutgoingArgs {
33 SocketAddress *saddr;
34 } outgoing_args;
36 void socket_send_channel_create(QIOTaskFunc f, void *data)
38 QIOChannelSocket *sioc = qio_channel_socket_new();
39 qio_channel_socket_connect_async(sioc, outgoing_args.saddr,
40 f, data, NULL, NULL);
43 int socket_send_channel_destroy(QIOChannel *send)
45 /* Remove channel */
46 object_unref(OBJECT(send));
47 if (outgoing_args.saddr) {
48 qapi_free_SocketAddress(outgoing_args.saddr);
49 outgoing_args.saddr = NULL;
51 return 0;
54 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
56 SocketAddress *saddr;
58 saddr = g_new0(SocketAddress, 1);
59 saddr->type = SOCKET_ADDRESS_TYPE_INET;
61 if (inet_parse(&saddr->u.inet, host_port, errp)) {
62 qapi_free_SocketAddress(saddr);
63 return NULL;
66 return saddr;
70 static SocketAddress *unix_build_address(const char *path)
72 SocketAddress *saddr;
74 saddr = g_new0(SocketAddress, 1);
75 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
76 saddr->u.q_unix.path = g_strdup(path);
78 return saddr;
82 struct SocketConnectData {
83 MigrationState *s;
84 char *hostname;
87 static void socket_connect_data_free(void *opaque)
89 struct SocketConnectData *data = opaque;
90 if (!data) {
91 return;
93 g_free(data->hostname);
94 g_free(data);
97 static void socket_outgoing_migration(QIOTask *task,
98 gpointer opaque)
100 struct SocketConnectData *data = opaque;
101 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
102 Error *err = NULL;
104 if (qio_task_propagate_error(task, &err)) {
105 trace_migration_socket_outgoing_error(error_get_pretty(err));
106 } else {
107 trace_migration_socket_outgoing_connected(data->hostname);
109 migration_channel_connect(data->s, sioc, data->hostname, err);
110 object_unref(OBJECT(sioc));
113 static void socket_start_outgoing_migration(MigrationState *s,
114 SocketAddress *saddr,
115 Error **errp)
117 QIOChannelSocket *sioc = qio_channel_socket_new();
118 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
120 data->s = s;
122 /* in case previous migration leaked it */
123 qapi_free_SocketAddress(outgoing_args.saddr);
124 outgoing_args.saddr = saddr;
126 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
127 data->hostname = g_strdup(saddr->u.inet.host);
130 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
131 qio_channel_socket_connect_async(sioc,
132 saddr,
133 socket_outgoing_migration,
134 data,
135 socket_connect_data_free,
136 NULL);
139 void tcp_start_outgoing_migration(MigrationState *s,
140 const char *host_port,
141 Error **errp)
143 Error *err = NULL;
144 SocketAddress *saddr = tcp_build_address(host_port, &err);
145 if (!err) {
146 socket_start_outgoing_migration(s, saddr, &err);
148 error_propagate(errp, err);
151 void unix_start_outgoing_migration(MigrationState *s,
152 const char *path,
153 Error **errp)
155 SocketAddress *saddr = unix_build_address(path);
156 socket_start_outgoing_migration(s, saddr, errp);
160 static void socket_accept_incoming_migration(QIONetListener *listener,
161 QIOChannelSocket *cioc,
162 gpointer opaque)
164 trace_migration_socket_incoming_accepted();
166 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
167 migration_channel_process_incoming(QIO_CHANNEL(cioc));
169 if (migration_has_all_channels()) {
170 /* Close listening socket as its no longer needed */
171 qio_net_listener_disconnect(listener);
172 object_unref(OBJECT(listener));
177 static void socket_start_incoming_migration(SocketAddress *saddr,
178 Error **errp)
180 QIONetListener *listener = qio_net_listener_new();
181 size_t i;
183 qio_net_listener_set_name(listener, "migration-socket-listener");
185 if (qio_net_listener_open_sync(listener, saddr, errp) < 0) {
186 object_unref(OBJECT(listener));
187 return;
190 qio_net_listener_set_client_func_full(listener,
191 socket_accept_incoming_migration,
192 NULL, NULL,
193 g_main_context_get_thread_default());
195 for (i = 0; i < listener->nsioc; i++) {
196 SocketAddress *address =
197 qio_channel_socket_get_local_address(listener->sioc[i], errp);
198 if (!address) {
199 return;
201 migrate_add_address(address);
202 qapi_free_SocketAddress(address);
206 void tcp_start_incoming_migration(const char *host_port, Error **errp)
208 Error *err = NULL;
209 SocketAddress *saddr = tcp_build_address(host_port, &err);
210 if (!err) {
211 socket_start_incoming_migration(saddr, &err);
213 qapi_free_SocketAddress(saddr);
214 error_propagate(errp, err);
217 void unix_start_incoming_migration(const char *path, Error **errp)
219 SocketAddress *saddr = unix_build_address(path);
220 socket_start_incoming_migration(saddr, errp);
221 qapi_free_SocketAddress(saddr);