]>
Commit | Line | Data |
---|---|---|
4951f65b | 1 | /* |
474c624d | 2 | * QEMU live migration via socket |
4951f65b | 3 | * |
d984464e | 4 | * Copyright Red Hat, Inc. 2009-2016 |
4951f65b CL |
5 | * |
6 | * Authors: | |
7 | * Chris Lalancette <[email protected]> | |
d984464e | 8 | * Daniel P. Berrange <[email protected]> |
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 | |
e1226365 DB |
58 | struct SocketConnectData { |
59 | MigrationState *s; | |
60 | char *hostname; | |
61 | }; | |
62 | ||
63 | static void socket_connect_data_free(void *opaque) | |
64 | { | |
65 | struct SocketConnectData *data = opaque; | |
66 | if (!data) { | |
67 | return; | |
68 | } | |
69 | g_free(data->hostname); | |
70 | g_free(data); | |
71 | } | |
72 | ||
6f860ae7 DB |
73 | static void socket_outgoing_migration(Object *src, |
74 | Error *err, | |
75 | gpointer opaque) | |
4951f65b | 76 | { |
e1226365 | 77 | struct SocketConnectData *data = opaque; |
d984464e | 78 | QIOChannel *sioc = QIO_CHANNEL(src); |
4951f65b | 79 | |
d984464e | 80 | if (err) { |
6f860ae7 | 81 | trace_migration_socket_outgoing_error(error_get_pretty(err)); |
e1226365 DB |
82 | data->s->to_dst_file = NULL; |
83 | migrate_fd_error(data->s, err); | |
e08c95ce | 84 | } else { |
e1226365 | 85 | trace_migration_socket_outgoing_connected(data->hostname); |
22724f49 | 86 | migration_channel_connect(data->s, sioc, data->hostname); |
4951f65b | 87 | } |
d984464e | 88 | object_unref(src); |
4951f65b CL |
89 | } |
90 | ||
6f860ae7 DB |
91 | static void socket_start_outgoing_migration(MigrationState *s, |
92 | SocketAddress *saddr, | |
93 | Error **errp) | |
4951f65b | 94 | { |
6f860ae7 | 95 | QIOChannelSocket *sioc = qio_channel_socket_new(); |
e1226365 | 96 | struct SocketConnectData *data = g_new0(struct SocketConnectData, 1); |
474c624d | 97 | |
e1226365 DB |
98 | data->s = s; |
99 | if (saddr->type == SOCKET_ADDRESS_KIND_INET) { | |
100 | data->hostname = g_strdup(saddr->u.inet.data->host); | |
101 | } | |
474c624d | 102 | |
d984464e DB |
103 | qio_channel_socket_connect_async(sioc, |
104 | saddr, | |
6f860ae7 | 105 | socket_outgoing_migration, |
e1226365 DB |
106 | data, |
107 | socket_connect_data_free); | |
d984464e | 108 | qapi_free_SocketAddress(saddr); |
4951f65b CL |
109 | } |
110 | ||
e65c67e4 DB |
111 | void tcp_start_outgoing_migration(MigrationState *s, |
112 | const char *host_port, | |
113 | Error **errp) | |
114 | { | |
9308ae54 DDAG |
115 | Error *err = NULL; |
116 | SocketAddress *saddr = tcp_build_address(host_port, &err); | |
117 | if (!err) { | |
118 | socket_start_outgoing_migration(s, saddr, &err); | |
119 | } | |
120 | error_propagate(errp, err); | |
e65c67e4 DB |
121 | } |
122 | ||
6f860ae7 DB |
123 | void unix_start_outgoing_migration(MigrationState *s, |
124 | const char *path, | |
125 | Error **errp) | |
126 | { | |
127 | SocketAddress *saddr = unix_build_address(path); | |
128 | socket_start_outgoing_migration(s, saddr, errp); | |
129 | } | |
130 | ||
d984464e | 131 | |
6f860ae7 DB |
132 | static gboolean socket_accept_incoming_migration(QIOChannel *ioc, |
133 | GIOCondition condition, | |
134 | gpointer opaque) | |
4951f65b | 135 | { |
d984464e DB |
136 | QIOChannelSocket *sioc; |
137 | Error *err = NULL; | |
4951f65b | 138 | |
d984464e DB |
139 | sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), |
140 | &err); | |
141 | if (!sioc) { | |
142 | error_report("could not accept migration connection (%s)", | |
143 | error_get_pretty(err)); | |
4951f65b CL |
144 | goto out; |
145 | } | |
146 | ||
6f860ae7 | 147 | trace_migration_socket_incoming_accepted(); |
d984464e | 148 | |
22724f49 DB |
149 | migration_channel_process_incoming(migrate_get_current(), |
150 | QIO_CHANNEL(sioc)); | |
d984464e | 151 | object_unref(OBJECT(sioc)); |
ab52a824 | 152 | |
4951f65b | 153 | out: |
d984464e DB |
154 | /* Close listening socket as its no longer needed */ |
155 | qio_channel_close(ioc, NULL); | |
156 | return FALSE; /* unregister */ | |
4951f65b CL |
157 | } |
158 | ||
d984464e | 159 | |
6f860ae7 DB |
160 | static void socket_start_incoming_migration(SocketAddress *saddr, |
161 | Error **errp) | |
4951f65b | 162 | { |
6f860ae7 | 163 | QIOChannelSocket *listen_ioc = qio_channel_socket_new(); |
4951f65b | 164 | |
d984464e DB |
165 | if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) { |
166 | object_unref(OBJECT(listen_ioc)); | |
167 | qapi_free_SocketAddress(saddr); | |
43eaae28 | 168 | return; |
4951f65b CL |
169 | } |
170 | ||
d984464e DB |
171 | qio_channel_add_watch(QIO_CHANNEL(listen_ioc), |
172 | G_IO_IN, | |
6f860ae7 | 173 | socket_accept_incoming_migration, |
d984464e DB |
174 | listen_ioc, |
175 | (GDestroyNotify)object_unref); | |
d984464e | 176 | qapi_free_SocketAddress(saddr); |
4951f65b | 177 | } |
6f860ae7 | 178 | |
e65c67e4 DB |
179 | void tcp_start_incoming_migration(const char *host_port, Error **errp) |
180 | { | |
9308ae54 DDAG |
181 | Error *err = NULL; |
182 | SocketAddress *saddr = tcp_build_address(host_port, &err); | |
183 | if (!err) { | |
184 | socket_start_incoming_migration(saddr, &err); | |
185 | } | |
186 | error_propagate(errp, err); | |
e65c67e4 DB |
187 | } |
188 | ||
6f860ae7 DB |
189 | void unix_start_incoming_migration(const char *path, Error **errp) |
190 | { | |
191 | SocketAddress *saddr = unix_build_address(path); | |
192 | socket_start_incoming_migration(saddr, errp); | |
193 | } |