tcg: Optionally log FPU state in TCG -d cpu logging
[qemu.git] / migration / socket.c
blob122d8ccfbe44f9fe2a09eab40055c17d8a7fc603
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 "io/net-listener.h"
28 #include "trace.h"
31 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
33 SocketAddress *saddr;
35 saddr = g_new0(SocketAddress, 1);
36 saddr->type = SOCKET_ADDRESS_TYPE_INET;
38 if (inet_parse(&saddr->u.inet, host_port, errp)) {
39 qapi_free_SocketAddress(saddr);
40 return NULL;
43 return saddr;
47 static SocketAddress *unix_build_address(const char *path)
49 SocketAddress *saddr;
51 saddr = g_new0(SocketAddress, 1);
52 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
53 saddr->u.q_unix.path = g_strdup(path);
55 return saddr;
59 struct SocketConnectData {
60 MigrationState *s;
61 char *hostname;
64 static void socket_connect_data_free(void *opaque)
66 struct SocketConnectData *data = opaque;
67 if (!data) {
68 return;
70 g_free(data->hostname);
71 g_free(data);
74 static void socket_outgoing_migration(QIOTask *task,
75 gpointer opaque)
77 struct SocketConnectData *data = opaque;
78 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
79 Error *err = NULL;
81 if (qio_task_propagate_error(task, &err)) {
82 trace_migration_socket_outgoing_error(error_get_pretty(err));
83 } else {
84 trace_migration_socket_outgoing_connected(data->hostname);
86 migration_channel_connect(data->s, sioc, data->hostname, err);
87 object_unref(OBJECT(sioc));
90 static void socket_start_outgoing_migration(MigrationState *s,
91 SocketAddress *saddr,
92 Error **errp)
94 QIOChannelSocket *sioc = qio_channel_socket_new();
95 struct SocketConnectData *data = g_new0(struct SocketConnectData, 1);
97 data->s = s;
98 if (saddr->type == SOCKET_ADDRESS_TYPE_INET) {
99 data->hostname = g_strdup(saddr->u.inet.host);
102 qio_channel_set_name(QIO_CHANNEL(sioc), "migration-socket-outgoing");
103 qio_channel_socket_connect_async(sioc,
104 saddr,
105 socket_outgoing_migration,
106 data,
107 socket_connect_data_free,
108 NULL);
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 void socket_accept_incoming_migration(QIONetListener *listener,
134 QIOChannelSocket *cioc,
135 gpointer opaque)
137 trace_migration_socket_incoming_accepted();
139 qio_channel_set_name(QIO_CHANNEL(cioc), "migration-socket-incoming");
140 migration_channel_process_incoming(QIO_CHANNEL(cioc));
142 if (migration_has_all_channels()) {
143 /* Close listening socket as its no longer needed */
144 qio_net_listener_disconnect(listener);
146 object_unref(OBJECT(listener));
151 static void socket_start_incoming_migration(SocketAddress *saddr,
152 Error **errp)
154 QIONetListener *listener = qio_net_listener_new();
156 qio_net_listener_set_name(listener, "migration-socket-listener");
158 if (qio_net_listener_open_sync(listener, saddr, errp) < 0) {
159 object_unref(OBJECT(listener));
160 return;
163 qio_net_listener_set_client_func(listener,
164 socket_accept_incoming_migration,
165 NULL, NULL);
168 void tcp_start_incoming_migration(const char *host_port, Error **errp)
170 Error *err = NULL;
171 SocketAddress *saddr = tcp_build_address(host_port, &err);
172 if (!err) {
173 socket_start_incoming_migration(saddr, &err);
175 qapi_free_SocketAddress(saddr);
176 error_propagate(errp, err);
179 void unix_start_incoming_migration(const char *path, Error **errp)
181 SocketAddress *saddr = unix_build_address(path);
182 socket_start_incoming_migration(saddr, errp);
183 qapi_free_SocketAddress(saddr);