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"
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
;
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
49 NBDExportInfo updated_info
;
50 QIOChannelSocket
*sioc
;
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()
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
,
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
),
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
);
100 static void nbd_client_connection_do_free(NBDClientConnection
*conn
)
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
);
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
)
129 ret
= qio_channel_socket_connect_sync(sioc
, addr
, errp
);
134 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
140 ret
= nbd_receive_negotiate(NULL
, QIO_CHANNEL(sioc
), tlscreds
,
141 tlscreds
? addr
->u
.inet
.host
: NULL
,
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
149 if (outioc
&& *outioc
) {
150 qio_channel_close(QIO_CHANNEL(*outioc
), NULL
);
151 object_unref(OBJECT(*outioc
));
154 qio_channel_close(QIO_CHANNEL(sioc
), NULL
);
163 static void *connect_thread_func(void *opaque
)
165 NBDClientConnection
*conn
= opaque
;
168 uint64_t timeout
= 1;
169 uint64_t max_timeout
= 16;
171 qemu_mutex_lock(&conn
->mutex
);
172 while (!conn
->detached
) {
174 conn
->sioc
= qio_channel_socket_new();
176 qemu_mutex_unlock(&conn
->mutex
);
178 error_free(conn
->err
);
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
);
198 object_unref(OBJECT(conn
->sioc
));
200 if (conn
->do_retry
&& !conn
->detached
) {
201 qemu_mutex_unlock(&conn
->mutex
);
204 if (timeout
< max_timeout
) {
208 qemu_mutex_lock(&conn
->mutex
);
216 /* mutex is locked */
218 assert(conn
->running
);
219 conn
->running
= false;
221 aio_co_wake(conn
->wait_co
);
222 conn
->wait_co
= NULL
;
224 do_free
= conn
->detached
;
226 qemu_mutex_unlock(&conn
->mutex
);
229 nbd_client_connection_do_free(conn
);
235 void nbd_client_connection_release(NBDClientConnection
*conn
)
237 bool do_free
= false;
243 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
244 assert(!conn
->detached
);
246 conn
->detached
= true;
251 qio_channel_shutdown(QIO_CHANNEL(conn
->sioc
),
252 QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
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
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
)
283 if (conn
->do_negotiation
) {
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
) {
296 /* Previous attempt finally succeeded in background */
297 if (conn
->do_negotiation
) {
298 memcpy(info
, &conn
->updated_info
, sizeof(*info
));
300 /* TLS channel now has own reference to parent */
301 object_unref(OBJECT(conn
->sioc
));
304 return g_steal_pointer(&conn
->ioc
);
310 return QIO_CHANNEL(g_steal_pointer(&conn
->sioc
));
313 conn
->running
= true;
314 error_free(conn
->err
);
316 qemu_thread_create(&thread
, "nbd-connect",
317 connect_thread_func
, conn
, QEMU_THREAD_DETACHED
);
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
) {
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");
344 error_propagate(errp
, conn
->err
);
349 if (conn
->do_negotiation
) {
350 memcpy(info
, &conn
->updated_info
, sizeof(*info
));
352 /* TLS channel now has own reference to parent */
353 object_unref(OBJECT(conn
->sioc
));
356 return g_steal_pointer(&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
)
381 WITH_QEMU_LOCK_GUARD(&conn
->mutex
) {
382 wait_co
= g_steal_pointer(&conn
->wait_co
);
386 aio_co_wake(wait_co
);