Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / nbd / client-connection.c
blob7123b1e18900b3a4533fc16a6dfdb9d99b95167d
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (c) 2021 Virtuozzo International GmbH.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include "block/nbd.h"
29 #include "qapi/qapi-visit-sockets.h"
30 #include "qapi/clone-visitor.h"
32 struct NBDClientConnection {
33 /* Initialization constants, never change */
34 SocketAddress *saddr; /* address to connect to */
35 QCryptoTLSCreds *tlscreds;
36 NBDExportInfo initial_info;
37 bool do_negotiation;
38 bool do_retry;
40 QemuMutex mutex;
43 * @sioc and @err represent a connection attempt. While running
44 * is true, they are only used by the connection thread, and mutex
45 * locking is not needed. Once the thread finishes,
46 * nbd_co_establish_connection then steals these pointers while
47 * under the mutex.
49 NBDExportInfo updated_info;
50 QIOChannelSocket *sioc;
51 QIOChannel *ioc;
52 Error *err;
54 /* All further fields are accessed only under mutex */
55 bool running; /* thread is running now */
56 bool detached; /* thread is detached and should cleanup the state */
59 * wait_co: if non-NULL, which coroutine to wake in
60 * nbd_co_establish_connection() after yield()
62 Coroutine *wait_co;
66 * The function isn't protected by any mutex, only call it when the client
67 * connection attempt has not yet started.
69 void nbd_client_connection_enable_retry(NBDClientConnection *conn)
71 conn->do_retry = true;
74 NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
75 bool do_negotiation,
76 const char *export_name,
77 const char *x_dirty_bitmap,
78 QCryptoTLSCreds *tlscreds)
80 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
82 object_ref(OBJECT(tlscreds));
83 *conn = (NBDClientConnection) {
84 .saddr = QAPI_CLONE(SocketAddress, saddr),
85 .tlscreds = tlscreds,
86 .do_negotiation = do_negotiation,
88 .initial_info.request_sizes = true,
89 .initial_info.structured_reply = true,
90 .initial_info.base_allocation = true,
91 .initial_info.x_dirty_bitmap = g_strdup(x_dirty_bitmap),
92 .initial_info.name = g_strdup(export_name ?: "")
95 qemu_mutex_init(&conn->mutex);
97 return conn;
100 static void nbd_client_connection_do_free(NBDClientConnection *conn)
102 if (conn->sioc) {
103 qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
104 object_unref(OBJECT(conn->sioc));
106 error_free(conn->err);
107 qapi_free_SocketAddress(conn->saddr);
108 object_unref(OBJECT(conn->tlscreds));
109 g_free(conn->initial_info.x_dirty_bitmap);
110 g_free(conn->initial_info.name);
111 g_free(conn);
115 * Connect to @addr and do NBD negotiation if @info is not null. If @tlscreds
116 * are given @outioc is returned. @outioc is provided only on success. The call
117 * may be cancelled from other thread by simply qio_channel_shutdown(sioc).
119 static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr,
120 NBDExportInfo *info, QCryptoTLSCreds *tlscreds,
121 QIOChannel **outioc, Error **errp)
123 int ret;
125 if (outioc) {
126 *outioc = NULL;
129 ret = qio_channel_socket_connect_sync(sioc, addr, errp);
130 if (ret < 0) {
131 return ret;
134 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
136 if (!info) {
137 return 0;
140 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), tlscreds,
141 tlscreds ? addr->u.inet.host : NULL,
142 outioc, info, errp);
143 if (ret < 0) {
145 * nbd_receive_negotiate() may setup tls ioc and return it even on
146 * failure path. In this case we should use it instead of original
147 * channel.
149 if (outioc && *outioc) {
150 qio_channel_close(QIO_CHANNEL(*outioc), NULL);
151 object_unref(OBJECT(*outioc));
152 *outioc = NULL;
153 } else {
154 qio_channel_close(QIO_CHANNEL(sioc), NULL);
157 return ret;
160 return 0;
163 static void *connect_thread_func(void *opaque)
165 NBDClientConnection *conn = opaque;
166 int ret;
167 bool do_free;
168 uint64_t timeout = 1;
169 uint64_t max_timeout = 16;
171 qemu_mutex_lock(&conn->mutex);
172 while (!conn->detached) {
173 assert(!conn->sioc);
174 conn->sioc = qio_channel_socket_new();
176 qemu_mutex_unlock(&conn->mutex);
178 error_free(conn->err);
179 conn->err = NULL;
180 conn->updated_info = conn->initial_info;
182 ret = nbd_connect(conn->sioc, conn->saddr,
183 conn->do_negotiation ? &conn->updated_info : NULL,
184 conn->tlscreds, &conn->ioc, &conn->err);
187 * conn->updated_info will finally be returned to the user. Clear the
188 * pointers to our internally allocated strings, which are IN parameters
189 * of nbd_receive_negotiate() and therefore nbd_connect(). Caller
190 * shoudn't be interested in these fields.
192 conn->updated_info.x_dirty_bitmap = NULL;
193 conn->updated_info.name = NULL;
195 qemu_mutex_lock(&conn->mutex);
197 if (ret < 0) {
198 object_unref(OBJECT(conn->sioc));
199 conn->sioc = NULL;
200 if (conn->do_retry && !conn->detached) {
201 qemu_mutex_unlock(&conn->mutex);
203 sleep(timeout);
204 if (timeout < max_timeout) {
205 timeout *= 2;
208 qemu_mutex_lock(&conn->mutex);
209 continue;
213 break;
216 /* mutex is locked */
218 assert(conn->running);
219 conn->running = false;
220 if (conn->wait_co) {
221 aio_co_wake(conn->wait_co);
222 conn->wait_co = NULL;
224 do_free = conn->detached;
226 qemu_mutex_unlock(&conn->mutex);
228 if (do_free) {
229 nbd_client_connection_do_free(conn);
232 return NULL;
235 void nbd_client_connection_release(NBDClientConnection *conn)
237 bool do_free = false;
239 if (!conn) {
240 return;
243 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
244 assert(!conn->detached);
245 if (conn->running) {
246 conn->detached = true;
247 } else {
248 do_free = true;
250 if (conn->sioc) {
251 qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
252 QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
256 if (do_free) {
257 nbd_client_connection_do_free(conn);
262 * Get a new connection in context of @conn:
263 * if the thread is running, wait for completion
264 * if the thread already succeeded in the background, and user didn't get the
265 * result, just return it now
266 * otherwise the thread is not running, so start a thread and wait for
267 * completion
269 * If @blocking is false, don't wait for the thread, return immediately.
271 * If @info is not NULL, also do nbd-negotiation after successful connection.
272 * In this case info is used only as out parameter, and is fully initialized by
273 * nbd_co_establish_connection(). "IN" fields of info as well as related only to
274 * nbd_receive_export_list() would be zero (see description of NBDExportInfo in
275 * include/block/nbd.h).
277 QIOChannel *coroutine_fn
278 nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
279 bool blocking, Error **errp)
281 QemuThread thread;
283 if (conn->do_negotiation) {
284 assert(info);
287 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
289 * Don't call nbd_co_establish_connection() in several coroutines in
290 * parallel. Only one call at once is supported.
292 assert(!conn->wait_co);
294 if (!conn->running) {
295 if (conn->sioc) {
296 /* Previous attempt finally succeeded in background */
297 if (conn->do_negotiation) {
298 memcpy(info, &conn->updated_info, sizeof(*info));
299 if (conn->ioc) {
300 /* TLS channel now has own reference to parent */
301 object_unref(OBJECT(conn->sioc));
302 conn->sioc = NULL;
304 return g_steal_pointer(&conn->ioc);
308 assert(!conn->ioc);
310 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
313 conn->running = true;
314 error_free(conn->err);
315 conn->err = NULL;
316 qemu_thread_create(&thread, "nbd-connect",
317 connect_thread_func, conn, QEMU_THREAD_DETACHED);
320 if (!blocking) {
321 return NULL;
324 conn->wait_co = qemu_coroutine_self();
328 * We are going to wait for connect-thread finish, but
329 * nbd_co_establish_connection_cancel() can interrupt.
331 qemu_coroutine_yield();
333 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
334 if (conn->running) {
336 * The connection attempt was canceled and the coroutine resumed
337 * before the connection thread finished its job. Report the
338 * attempt as failed, but leave the connection thread running,
339 * to reuse it for the next connection attempt.
341 error_setg(errp, "Connection attempt cancelled by other operation");
342 return NULL;
343 } else {
344 error_propagate(errp, conn->err);
345 conn->err = NULL;
346 if (!conn->sioc) {
347 return NULL;
349 if (conn->do_negotiation) {
350 memcpy(info, &conn->updated_info, sizeof(*info));
351 if (conn->ioc) {
352 /* TLS channel now has own reference to parent */
353 object_unref(OBJECT(conn->sioc));
354 conn->sioc = NULL;
356 return g_steal_pointer(&conn->ioc);
360 assert(!conn->ioc);
362 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
366 abort(); /* unreachable */
370 * nbd_co_establish_connection_cancel
371 * Cancel nbd_co_establish_connection() asynchronously.
373 * Note that this function neither directly stops the thread nor closes the
374 * socket, but rather safely wakes nbd_co_establish_connection() which is
375 * sleeping in yield()
377 void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
379 Coroutine *wait_co;
381 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
382 wait_co = g_steal_pointer(&conn->wait_co);
385 if (wait_co) {
386 aio_co_wake(wait_co);