target/i386: move cpu_sync_bndcs_hflags() function
[qemu/ar7.git] / migration / socket.c
blob757d3821a1f9ec6a57c61269c6f2042e62441812
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"
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"
30 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
32 SocketAddress *saddr;
34 saddr = g_new0(SocketAddress, 1);
35 saddr->type = SOCKET_ADDRESS_TYPE_INET;
37 if (inet_parse(&saddr->u.inet, host_port, errp)) {
38 qapi_free_SocketAddress(saddr);
39 return NULL;
42 return saddr;
46 static SocketAddress *unix_build_address(const char *path)
48 SocketAddress *saddr;
50 saddr = g_new0(SocketAddress, 1);
51 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
52 saddr->u.q_unix.path = g_strdup(path);
54 return saddr;
58 struct SocketConnectData {
59 MigrationState *s;
60 char *hostname;
63 static void socket_connect_data_free(void *opaque)
65 struct SocketConnectData *data = opaque;
66 if (!data) {
67 return;
69 g_free(data->hostname);
70 g_free(data);
73 static void socket_outgoing_migration(QIOTask *task,
74 gpointer opaque)
76 struct SocketConnectData *data = opaque;
77 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
78 Error *err = NULL;
80 if (qio_task_propagate_error(task, &err)) {
81 trace_migration_socket_outgoing_error(error_get_pretty(err));
82 migrate_fd_error(data->s, err);
83 error_free(err);
84 } else {
85 trace_migration_socket_outgoing_connected(data->hostname);
86 migration_channel_connect(data->s, sioc, data->hostname);
88 object_unref(OBJECT(sioc));
91 static void socket_start_outgoing_migration(MigrationState *s,
92 SocketAddress *saddr,
93 Error **errp)
95 QIOChannelSocket *sioc = qio_channel_socket_new();
96 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
98 data->s = s;
99 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
100 data->hostname = g_strdup(saddr->u.inet.host);
103 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
104 qio_channel_socket_connect_async(sioc,
105 saddr,
106 socket_outgoing_migration,
107 data,
108 socket_connect_data_free);
109 qapi_free_SocketAddress(saddr);
112 void tcp_start_outgoing_migration(MigrationState *s,
113 const char *host_port,
114 Error **errp)
116 Error *err = NULL;
117 SocketAddress *saddr = tcp_build_address(host_port, &err);
118 if (!err) {
119 socket_start_outgoing_migration(s, saddr, &err);
121 error_propagate(errp, err);
124 void unix_start_outgoing_migration(MigrationState *s,
125 const char *path,
126 Error **errp)
128 SocketAddress *saddr = unix_build_address(path);
129 socket_start_outgoing_migration(s, saddr, errp);
133 static gboolean socket_accept_incoming_migration(QIOChannel *ioc,
134 GIOCondition condition,
135 gpointer opaque)
137 QIOChannelSocket *sioc;
138 Error *err = NULL;
140 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
141 &err);
142 if (!sioc) {
143 error_report("could not accept migration connection (%s)",
144 error_get_pretty(err));
145 goto out;
148 trace_migration_socket_incoming_accepted();
150 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-incoming");
151 migration_channel_process_incoming(QIO_CHANNEL(sioc));
152 object_unref(OBJECT(sioc));
154 out:
155 /* Close listening socket as its no longer needed */
156 qio_channel_close(ioc, NULL);
157 return FALSE; /* unregister */
161 static void socket_start_incoming_migration(SocketAddress *saddr,
162 Error **errp)
164 QIOChannelSocket *listen_ioc = qio_channel_socket_new();
166 qio_channel_set_name(QIO_CHANNEL(listen_ioc),
167 "migration-socket-listener");
169 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
170 object_unref(OBJECT(listen_ioc));
171 qapi_free_SocketAddress(saddr);
172 return;
175 qio_channel_add_watch(QIO_CHANNEL(listen_ioc),
176 G_IO_IN,
177 socket_accept_incoming_migration,
178 listen_ioc,
179 (GDestroyNotify)object_unref);
180 qapi_free_SocketAddress(saddr);
183 void tcp_start_incoming_migration(const char *host_port, Error **errp)
185 Error *err = NULL;
186 SocketAddress *saddr = tcp_build_address(host_port, &err);
187 if (!err) {
188 socket_start_incoming_migration(saddr, &err);
190 error_propagate(errp, err);
193 void unix_start_incoming_migration(const char *path, Error **errp)
195 SocketAddress *saddr = unix_build_address(path);
196 socket_start_incoming_migration(saddr, errp);