]>
Commit | Line | Data |
---|---|---|
4951f65b CL |
1 | /* |
2 | * QEMU live migration via Unix Domain Sockets | |
3 | * | |
d984464e | 4 | * Copyright Red Hat, Inc. 2009-2016 |
4951f65b CL |
5 | * |
6 | * Authors: | |
7 | * Chris Lalancette <clalance@redhat.com> | |
d984464e | 8 | * Daniel P. Berrange <berrange@redhat.com> |
4951f65b CL |
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 | * | |
6b620ca3 PB |
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. | |
4951f65b CL |
15 | */ |
16 | ||
1393a485 | 17 | #include "qemu/osdep.h" |
d99598cc | 18 | |
4951f65b | 19 | #include "qemu-common.h" |
d99598cc | 20 | #include "qemu/error-report.h" |
d984464e | 21 | #include "qapi/error.h" |
caf71f86 | 22 | #include "migration/migration.h" |
557ec5a0 | 23 | #include "migration/qemu-file.h" |
d984464e DB |
24 | #include "io/channel-socket.h" |
25 | #include "trace.h" | |
4951f65b | 26 | |
4951f65b | 27 | |
e65c67e4 DB |
28 | static SocketAddress *tcp_build_address(const char *host_port, Error **errp) |
29 | { | |
30 | InetSocketAddress *iaddr = inet_parse(host_port, errp); | |
31 | SocketAddress *saddr; | |
32 | ||
33 | if (!iaddr) { | |
34 | return NULL; | |
35 | } | |
36 | ||
37 | saddr = g_new0(SocketAddress, 1); | |
38 | saddr->type = SOCKET_ADDRESS_KIND_INET; | |
39 | saddr->u.inet.data = iaddr; | |
40 | ||
41 | return saddr; | |
42 | } | |
43 | ||
44 | ||
d984464e DB |
45 | static SocketAddress *unix_build_address(const char *path) |
46 | { | |
47 | SocketAddress *saddr; | |
48 | ||
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); | |
53 | ||
54 | return saddr; | |
55 | } | |
4951f65b | 56 | |
d984464e | 57 | |
6f860ae7 DB |
58 | static void socket_outgoing_migration(Object *src, |
59 | Error *err, | |
60 | gpointer opaque) | |
4951f65b | 61 | { |
22f00a44 | 62 | MigrationState *s = opaque; |
d984464e | 63 | QIOChannel *sioc = QIO_CHANNEL(src); |
4951f65b | 64 | |
d984464e | 65 | if (err) { |
6f860ae7 | 66 | trace_migration_socket_outgoing_error(error_get_pretty(err)); |
89a02a9f | 67 | s->to_dst_file = NULL; |
d59ce6f3 | 68 | migrate_fd_error(s, err); |
e08c95ce | 69 | } else { |
6f860ae7 | 70 | trace_migration_socket_outgoing_connected(); |
d984464e | 71 | migration_set_outgoing_channel(s, sioc); |
4951f65b | 72 | } |
d984464e | 73 | object_unref(src); |
4951f65b CL |
74 | } |
75 | ||
6f860ae7 DB |
76 | static void socket_start_outgoing_migration(MigrationState *s, |
77 | SocketAddress *saddr, | |
78 | Error **errp) | |
4951f65b | 79 | { |
6f860ae7 | 80 | QIOChannelSocket *sioc = qio_channel_socket_new(); |
d984464e DB |
81 | qio_channel_socket_connect_async(sioc, |
82 | saddr, | |
6f860ae7 | 83 | socket_outgoing_migration, |
d984464e DB |
84 | s, |
85 | NULL); | |
86 | qapi_free_SocketAddress(saddr); | |
4951f65b CL |
87 | } |
88 | ||
e65c67e4 DB |
89 | void tcp_start_outgoing_migration(MigrationState *s, |
90 | const char *host_port, | |
91 | Error **errp) | |
92 | { | |
93 | SocketAddress *saddr = tcp_build_address(host_port, errp); | |
94 | socket_start_outgoing_migration(s, saddr, errp); | |
95 | } | |
96 | ||
6f860ae7 DB |
97 | void unix_start_outgoing_migration(MigrationState *s, |
98 | const char *path, | |
99 | Error **errp) | |
100 | { | |
101 | SocketAddress *saddr = unix_build_address(path); | |
102 | socket_start_outgoing_migration(s, saddr, errp); | |
103 | } | |
104 | ||
d984464e | 105 | |
6f860ae7 DB |
106 | static gboolean socket_accept_incoming_migration(QIOChannel *ioc, |
107 | GIOCondition condition, | |
108 | gpointer opaque) | |
4951f65b | 109 | { |
d984464e DB |
110 | QIOChannelSocket *sioc; |
111 | Error *err = NULL; | |
4951f65b | 112 | |
d984464e DB |
113 | sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), |
114 | &err); | |
115 | if (!sioc) { | |
116 | error_report("could not accept migration connection (%s)", | |
117 | error_get_pretty(err)); | |
4951f65b CL |
118 | goto out; |
119 | } | |
120 | ||
6f860ae7 | 121 | trace_migration_socket_incoming_accepted(); |
d984464e DB |
122 | |
123 | migration_set_incoming_channel(migrate_get_current(), | |
124 | QIO_CHANNEL(sioc)); | |
125 | object_unref(OBJECT(sioc)); | |
ab52a824 | 126 | |
4951f65b | 127 | out: |
d984464e DB |
128 | /* Close listening socket as its no longer needed */ |
129 | qio_channel_close(ioc, NULL); | |
130 | return FALSE; /* unregister */ | |
4951f65b CL |
131 | } |
132 | ||
d984464e | 133 | |
6f860ae7 DB |
134 | static void socket_start_incoming_migration(SocketAddress *saddr, |
135 | Error **errp) | |
4951f65b | 136 | { |
6f860ae7 | 137 | QIOChannelSocket *listen_ioc = qio_channel_socket_new(); |
4951f65b | 138 | |
d984464e DB |
139 | if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) { |
140 | object_unref(OBJECT(listen_ioc)); | |
141 | qapi_free_SocketAddress(saddr); | |
43eaae28 | 142 | return; |
4951f65b CL |
143 | } |
144 | ||
d984464e DB |
145 | qio_channel_add_watch(QIO_CHANNEL(listen_ioc), |
146 | G_IO_IN, | |
6f860ae7 | 147 | socket_accept_incoming_migration, |
d984464e DB |
148 | listen_ioc, |
149 | (GDestroyNotify)object_unref); | |
d984464e | 150 | qapi_free_SocketAddress(saddr); |
4951f65b | 151 | } |
6f860ae7 | 152 | |
e65c67e4 DB |
153 | void tcp_start_incoming_migration(const char *host_port, Error **errp) |
154 | { | |
155 | SocketAddress *saddr = tcp_build_address(host_port, errp); | |
156 | socket_start_incoming_migration(saddr, errp); | |
157 | } | |
158 | ||
6f860ae7 DB |
159 | void unix_start_incoming_migration(const char *path, Error **errp) |
160 | { | |
161 | SocketAddress *saddr = unix_build_address(path); | |
162 | socket_start_incoming_migration(saddr, errp); | |
163 | } |