qemu-io: Use correct range limitations
[qemu/ar7.git] / migration / socket.c
blob5c0a38f7b9b6b1b52462bdd95336918a16c17668
1 /*
2 * QEMU live migration via Unix Domain Sockets
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 "migration/migration.h"
23 #include "migration/qemu-file.h"
24 #include "io/channel-socket.h"
25 #include "trace.h"
28 static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
30 InetSocketAddress *iaddr = inet_parse(host_port, errp);
31 SocketAddress *saddr;
33 if (!iaddr) {
34 return NULL;
37 saddr = g_new0(SocketAddress, 1);
38 saddr->type = SOCKET_ADDRESS_KIND_INET;
39 saddr->u.inet.data = iaddr;
41 return saddr;
45 static SocketAddress *unix_build_address(const char *path)
47 SocketAddress *saddr;
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);
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(Object *src,
74 Error *err,
75 gpointer opaque)
77 struct SocketConnectData *data = opaque;
78 QIOChannel *sioc = QIO_CHANNEL(src);
80 if (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 } else {
85 trace_migration_socket_outgoing_connected(data->hostname);
86 migration_channel_connect(data->s, sioc, data->hostname);
88 object_unref(src);
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);
97 data->s = s;
98 if (saddr->type == SOCKET_ADDRESS_KIND_INET) {
99 data->hostname = g_strdup(saddr->u.inet.data->host);
101 qio_channel_socket_connect_async(sioc,
102 saddr,
103 socket_outgoing_migration,
104 data,
105 socket_connect_data_free);
106 qapi_free_SocketAddress(saddr);
109 void tcp_start_outgoing_migration(MigrationState *s,
110 const char *host_port,
111 Error **errp)
113 SocketAddress *saddr = tcp_build_address(host_port, errp);
114 socket_start_outgoing_migration(s, saddr, errp);
117 void unix_start_outgoing_migration(MigrationState *s,
118 const char *path,
119 Error **errp)
121 SocketAddress *saddr = unix_build_address(path);
122 socket_start_outgoing_migration(s, saddr, errp);
126 static gboolean socket_accept_incoming_migration(QIOChannel *ioc,
127 GIOCondition condition,
128 gpointer opaque)
130 QIOChannelSocket *sioc;
131 Error *err = NULL;
133 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
134 &err);
135 if (!sioc) {
136 error_report("could not accept migration connection (%s)",
137 error_get_pretty(err));
138 goto out;
141 trace_migration_socket_incoming_accepted();
143 migration_channel_process_incoming(migrate_get_current(),
144 QIO_CHANNEL(sioc));
145 object_unref(OBJECT(sioc));
147 out:
148 /* Close listening socket as its no longer needed */
149 qio_channel_close(ioc, NULL);
150 return FALSE; /* unregister */
154 static void socket_start_incoming_migration(SocketAddress *saddr,
155 Error **errp)
157 QIOChannelSocket *listen_ioc = qio_channel_socket_new();
159 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
160 object_unref(OBJECT(listen_ioc));
161 qapi_free_SocketAddress(saddr);
162 return;
165 qio_channel_add_watch(QIO_CHANNEL(listen_ioc),
166 G_IO_IN,
167 socket_accept_incoming_migration,
168 listen_ioc,
169 (GDestroyNotify)object_unref);
170 qapi_free_SocketAddress(saddr);
173 void tcp_start_incoming_migration(const char *host_port, Error **errp)
175 SocketAddress *saddr = tcp_build_address(host_port, errp);
176 socket_start_incoming_migration(saddr, errp);
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);