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
25 #include "qemu/osdep.h"
28 #include "block/nbd.h"
30 #include "qapi/qapi-visit-sockets.h"
31 #include "qapi/clone-visitor.h"
33 struct NBDClientConnection
{
34 /* Initialization constants, never change */
35 SocketAddress
*saddr
; /* address to connect to */
36 QCryptoTLSCreds
*tlscreds
;
38 NBDExportInfo initial_info
;
44 NBDExportInfo updated_info
;
46 * @sioc represents a successful result. While thread is running, @sioc is
47 * used only by thread and not protected by mutex. When thread is not
48 * running, @sioc is stolen by nbd_co_establish_connection() under mutex.
50 QIOChannelSocket
*sioc
;
53 * @err represents previous attempt. It may be copied by
54 * nbd_co_establish_connection() when it reports failure.
58 /* All further fields are accessed only under mutex */
59 bool running
; /* thread is running now */
60 bool detached
; /* thread is detached and should cleanup the state */
63 * wait_co: if non-NULL, which coroutine to wake in
64 * nbd_co_establish_connection() after yield()
70 * The function isn't protected by any mutex, only call it when the client
71 * connection attempt has not yet started.
73 void nbd_client_connection_enable_retry(NBDClientConnection
*conn
)
75 conn
->do_retry
= true;
78 NBDClientConnection
*nbd_client_connection_new(const SocketAddress
*saddr
,
80 const char *export_name
,
81 const char *x_dirty_bitmap
,
82 QCryptoTLSCreds
*tlscreds
,
83 const char *tlshostname
)
85 NBDClientConnection
*conn
= g_new(NBDClientConnection
, 1);
87 object_ref(OBJECT(tlscreds
));
88 *conn
= (NBDClientConnection
) {
89 .saddr
= QAPI_CLONE(SocketAddress
, saddr
),
91 .tlshostname
= g_strdup(tlshostname
),
92 .do_negotiation
= do_negotiation
,
94 .initial_info
.request_sizes
= true,
95 .initial_info
.structured_reply
= true,
96 .initial_info
.base_allocation
= true,
97 .initial_info
.x_dirty_bitmap
= g_strdup(x_dirty_bitmap
),
98 .initial_info
.name
= g_strdup(export_name
?: "")
101 qemu_mutex_init(&conn
->mutex
);
106 static void nbd_client_connection_do_free(NBDClientConnection
*conn
)
109 qio_channel_close(QIO_CHANNEL(conn
->sioc
), NULL
);
110 object_unref(OBJECT(conn
->sioc
));
112 error_free(conn
->err
);
113 qapi_free_SocketAddress(conn
->saddr
);
114 g_free(conn
->tlshostname
);
115 object_unref(OBJECT(conn
->tlscreds
));
116 g_free(conn
->initial_info
.x_dirty_bitmap
);
117 g_free(conn
->initial_info
.name
);
122 * Connect to @addr and do NBD negotiation if @info is not null. If @tlscreds
123 * are given @outioc is returned. @outioc is provided only on success. The call
124 * may be cancelled from other thread by simply qio_channel_shutdown(sioc).
126 static int nbd_connect(QIOChannelSocket
*sioc
, SocketAddress
*addr
,
127 NBDExportInfo
*info
, QCryptoTLSCreds
*tlscreds
,
128 const char *tlshostname
,
129 QIOChannel
**outioc
, Error
**errp
)
137 ret
= qio_channel_socket_connect_sync(sioc
, addr
, errp
);
142 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
148 ret
= nbd_receive_negotiate(NULL
, QIO_CHANNEL(sioc
), tlscreds
,
153 * nbd_receive_negotiate() may setup tls ioc and return it even on
154 * failure path. In this case we should use it instead of original
157 if (outioc
&& *outioc
) {
158 qio_channel_close(QIO_CHANNEL(*outioc
), NULL
);
159 object_unref(OBJECT(*outioc
));
162 qio_channel_close(QIO_CHANNEL(sioc
), NULL
);
171 static void *connect_thread_func(void *opaque
)
173 NBDClientConnection
*conn
= opaque
;
176 uint64_t timeout
= 1;
177 uint64_t max_timeout
= 16;
179 qemu_mutex_lock(&conn
->mutex
);
180 while (!conn
->detached
) {
181 Error
*local_err
= NULL
;
184 conn
->sioc
= qio_channel_socket_new();
186 qemu_mutex_unlock(&conn
->mutex
);
188 conn
->updated_info
= conn
->initial_info
;
190 ret
= nbd_connect(conn
->sioc
, conn
->saddr
,
191 conn
->do_negotiation
? &conn
->updated_info
: NULL
,
192 conn
->tlscreds
, conn
->tlshostname
,
193 &conn
->ioc
, &local_err
);
196 * conn->updated_info will finally be returned to the user. Clear the
197 * pointers to our internally allocated strings, which are IN parameters
198 * of nbd_receive_negotiate() and therefore nbd_connect(). Caller
199 * shoudn't be interested in these fields.
201 conn
->updated_info
.x_dirty_bitmap
= NULL
;
202 conn
->updated_info
.name
= NULL
;
204 qemu_mutex_lock(&conn
->mutex
);
206 error_free(conn
->err
);
208 error_propagate(&conn
->err
, local_err
);
211 object_unref(OBJECT(conn
->sioc
));
213 if (conn
->do_retry
&& !conn
->detached
) {
214 trace_nbd_connect_thread_sleep(timeout
);
215 qemu_mutex_unlock(&conn
->mutex
);
218 if (timeout
< max_timeout
) {
222 qemu_mutex_lock(&conn
->mutex
);
230 /* mutex is locked */
232 assert(conn
->running
);
233 conn
->running
= false;
235 aio_co_wake(conn
->wait_co
);
236 conn
->wait_co
= NULL
;
238 do_free
= conn
->detached
;
240 qemu_mutex_unlock(&conn
->mutex
);
243 nbd_client_connection_do_free(conn
);
249 void nbd_client_connection_release(NBDClientConnection
*conn
)
251 bool do_free
= false;
257 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
258 assert(!conn
->detached
);
260 conn
->detached
= true;
265 qio_channel_shutdown(QIO_CHANNEL(conn
->sioc
),
266 QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
271 nbd_client_connection_do_free(conn
);
276 * Get a new connection in context of @conn:
277 * if the thread is running, wait for completion
278 * if the thread already succeeded in the background, and user didn't get the
279 * result, just return it now
280 * otherwise the thread is not running, so start a thread and wait for
283 * If @blocking is false, don't wait for the thread, return immediately.
285 * If @info is not NULL, also do nbd-negotiation after successful connection.
286 * In this case info is used only as out parameter, and is fully initialized by
287 * nbd_co_establish_connection(). "IN" fields of info as well as related only to
288 * nbd_receive_export_list() would be zero (see description of NBDExportInfo in
289 * include/block/nbd.h).
291 QIOChannel
*coroutine_fn
292 nbd_co_establish_connection(NBDClientConnection
*conn
, NBDExportInfo
*info
,
293 bool blocking
, Error
**errp
)
297 if (conn
->do_negotiation
) {
301 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
303 * Don't call nbd_co_establish_connection() in several coroutines in
304 * parallel. Only one call at once is supported.
306 assert(!conn
->wait_co
);
308 if (!conn
->running
) {
310 /* Previous attempt finally succeeded in background */
311 if (conn
->do_negotiation
) {
312 memcpy(info
, &conn
->updated_info
, sizeof(*info
));
314 /* TLS channel now has own reference to parent */
315 object_unref(OBJECT(conn
->sioc
));
318 return g_steal_pointer(&conn
->ioc
);
324 return QIO_CHANNEL(g_steal_pointer(&conn
->sioc
));
327 conn
->running
= true;
328 qemu_thread_create(&thread
, "nbd-connect",
329 connect_thread_func
, conn
, QEMU_THREAD_DETACHED
);
334 error_propagate(errp
, error_copy(conn
->err
));
336 error_setg(errp
, "No connection at the moment");
342 conn
->wait_co
= qemu_coroutine_self();
346 * We are going to wait for connect-thread finish, but
347 * nbd_co_establish_connection_cancel() can interrupt.
349 qemu_coroutine_yield();
351 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
354 * The connection attempt was canceled and the coroutine resumed
355 * before the connection thread finished its job. Report the
356 * attempt as failed, but leave the connection thread running,
357 * to reuse it for the next connection attempt.
360 error_propagate(errp
, error_copy(conn
->err
));
363 * The only possible case here is cancelling by open_timer
364 * during nbd_open(). So, the error message is for that case.
365 * If we have more use cases, we can refactor
366 * nbd_co_establish_connection_cancel() to take an additional
367 * parameter cancel_reason, that would be passed than to the
368 * caller of cancelled nbd_co_establish_connection().
370 error_setg(errp
, "Connection attempt cancelled by timeout");
375 /* Thread finished. There must be either error or sioc */
376 assert(!conn
->err
!= !conn
->sioc
);
379 error_propagate(errp
, error_copy(conn
->err
));
383 if (conn
->do_negotiation
) {
384 memcpy(info
, &conn
->updated_info
, sizeof(*info
));
386 /* TLS channel now has own reference to parent */
387 object_unref(OBJECT(conn
->sioc
));
390 return g_steal_pointer(&conn
->ioc
);
396 return QIO_CHANNEL(g_steal_pointer(&conn
->sioc
));
400 abort(); /* unreachable */
404 * nbd_co_establish_connection_cancel
405 * Cancel nbd_co_establish_connection() asynchronously.
407 * Note that this function neither directly stops the thread nor closes the
408 * socket, but rather safely wakes nbd_co_establish_connection() which is
409 * sleeping in yield()
411 void nbd_co_establish_connection_cancel(NBDClientConnection
*conn
)
415 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
416 wait_co
= g_steal_pointer(&conn
->wait_co
);
420 aio_co_wake(wait_co
);