migration: convert unix socket protocol to use QIOChannel
[qemu.git] / migration / unix.c
1 /*
2 * QEMU live migration via Unix Domain Sockets
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 "migration/migration.h"
23 #include "migration/qemu-file.h"
24 #include "io/channel-socket.h"
25 #include "trace.h"
26
27
28 static SocketAddress *unix_build_address(const char *path)
29 {
30 SocketAddress *saddr;
31
32 saddr = g_new0(SocketAddress, 1);
33 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
34 saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
35 saddr->u.q_unix.data->path = g_strdup(path);
36
37 return saddr;
38 }
39
40
41 static void unix_outgoing_migration(Object *src,
42 Error *err,
43 gpointer opaque)
44 {
45 MigrationState *s = opaque;
46 QIOChannel *sioc = QIO_CHANNEL(src);
47
48 if (err) {
49 trace_migration_unix_outgoing_error(error_get_pretty(err));
50 s->to_dst_file = NULL;
51 migrate_fd_error(s, err);
52 } else {
53 trace_migration_unix_outgoing_connected();
54 migration_set_outgoing_channel(s, sioc);
55 }
56 object_unref(src);
57 }
58
59
60 void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp)
61 {
62 SocketAddress *saddr = unix_build_address(path);
63 QIOChannelSocket *sioc;
64 sioc = qio_channel_socket_new();
65 qio_channel_socket_connect_async(sioc,
66 saddr,
67 unix_outgoing_migration,
68 s,
69 NULL);
70 qapi_free_SocketAddress(saddr);
71 }
72
73
74 static gboolean unix_accept_incoming_migration(QIOChannel *ioc,
75 GIOCondition condition,
76 gpointer opaque)
77 {
78 QIOChannelSocket *sioc;
79 Error *err = NULL;
80
81 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
82 &err);
83 if (!sioc) {
84 error_report("could not accept migration connection (%s)",
85 error_get_pretty(err));
86 goto out;
87 }
88
89 trace_migration_unix_incoming_accepted();
90
91 migration_set_incoming_channel(migrate_get_current(),
92 QIO_CHANNEL(sioc));
93 object_unref(OBJECT(sioc));
94
95 out:
96 /* Close listening socket as its no longer needed */
97 qio_channel_close(ioc, NULL);
98 return FALSE; /* unregister */
99 }
100
101
102 void unix_start_incoming_migration(const char *path, Error **errp)
103 {
104 SocketAddress *saddr = unix_build_address(path);
105 QIOChannelSocket *listen_ioc;
106
107 listen_ioc = qio_channel_socket_new();
108 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
109 object_unref(OBJECT(listen_ioc));
110 qapi_free_SocketAddress(saddr);
111 return;
112 }
113
114 qio_channel_add_watch(QIO_CHANNEL(listen_ioc),
115 G_IO_IN,
116 unix_accept_incoming_migration,
117 listen_ioc,
118 (GDestroyNotify)object_unref);
119
120 qapi_free_SocketAddress(saddr);
121 }