2021-06-15 22:07:05 +03:00
|
|
|
/*
|
|
|
|
* QEMU Block driver for NBD
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Virtuozzo International GmbH.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
|
|
|
|
#include "block/nbd.h"
|
|
|
|
|
|
|
|
#include "qapi/qapi-visit-sockets.h"
|
|
|
|
#include "qapi/clone-visitor.h"
|
|
|
|
|
|
|
|
struct NBDClientConnection {
|
2021-06-10 13:07:49 +03:00
|
|
|
/* Initialization constants, never change */
|
2021-06-15 22:07:05 +03:00
|
|
|
SocketAddress *saddr; /* address to connect to */
|
2021-06-10 13:07:49 +03:00
|
|
|
QCryptoTLSCreds *tlscreds;
|
|
|
|
NBDExportInfo initial_info;
|
|
|
|
bool do_negotiation;
|
2021-06-10 13:07:50 +03:00
|
|
|
bool do_retry;
|
2021-06-15 22:07:05 +03:00
|
|
|
|
|
|
|
QemuMutex mutex;
|
|
|
|
|
2021-09-06 22:06:49 +03:00
|
|
|
NBDExportInfo updated_info;
|
2021-06-15 22:07:05 +03:00
|
|
|
/*
|
2021-09-06 22:06:49 +03:00
|
|
|
* @sioc represents a successful result. While thread is running, @sioc is
|
|
|
|
* used only by thread and not protected by mutex. When thread is not
|
|
|
|
* running, @sioc is stolen by nbd_co_establish_connection() under mutex.
|
2021-06-15 22:07:05 +03:00
|
|
|
*/
|
|
|
|
QIOChannelSocket *sioc;
|
2021-06-10 13:07:49 +03:00
|
|
|
QIOChannel *ioc;
|
2021-09-06 22:06:49 +03:00
|
|
|
/*
|
|
|
|
* @err represents previous attempt. It may be copied by
|
|
|
|
* nbd_co_establish_connection() when it reports failure.
|
|
|
|
*/
|
2021-06-15 22:07:05 +03:00
|
|
|
Error *err;
|
|
|
|
|
|
|
|
/* All further fields are accessed only under mutex */
|
|
|
|
bool running; /* thread is running now */
|
|
|
|
bool detached; /* thread is detached and should cleanup the state */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait_co: if non-NULL, which coroutine to wake in
|
|
|
|
* nbd_co_establish_connection() after yield()
|
|
|
|
*/
|
|
|
|
Coroutine *wait_co;
|
|
|
|
};
|
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
/*
|
|
|
|
* The function isn't protected by any mutex, only call it when the client
|
|
|
|
* connection attempt has not yet started.
|
|
|
|
*/
|
|
|
|
void nbd_client_connection_enable_retry(NBDClientConnection *conn)
|
|
|
|
{
|
|
|
|
conn->do_retry = true;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:49 +03:00
|
|
|
NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
|
|
|
|
bool do_negotiation,
|
|
|
|
const char *export_name,
|
|
|
|
const char *x_dirty_bitmap,
|
|
|
|
QCryptoTLSCreds *tlscreds)
|
2021-06-15 22:07:05 +03:00
|
|
|
{
|
|
|
|
NBDClientConnection *conn = g_new(NBDClientConnection, 1);
|
|
|
|
|
2021-06-10 13:07:49 +03:00
|
|
|
object_ref(OBJECT(tlscreds));
|
2021-06-15 22:07:05 +03:00
|
|
|
*conn = (NBDClientConnection) {
|
|
|
|
.saddr = QAPI_CLONE(SocketAddress, saddr),
|
2021-06-10 13:07:49 +03:00
|
|
|
.tlscreds = tlscreds,
|
|
|
|
.do_negotiation = do_negotiation,
|
|
|
|
|
|
|
|
.initial_info.request_sizes = true,
|
|
|
|
.initial_info.structured_reply = true,
|
|
|
|
.initial_info.base_allocation = true,
|
|
|
|
.initial_info.x_dirty_bitmap = g_strdup(x_dirty_bitmap),
|
|
|
|
.initial_info.name = g_strdup(export_name ?: "")
|
2021-06-15 22:07:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
qemu_mutex_init(&conn->mutex);
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nbd_client_connection_do_free(NBDClientConnection *conn)
|
|
|
|
{
|
|
|
|
if (conn->sioc) {
|
|
|
|
qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
|
|
|
|
object_unref(OBJECT(conn->sioc));
|
|
|
|
}
|
|
|
|
error_free(conn->err);
|
|
|
|
qapi_free_SocketAddress(conn->saddr);
|
2021-06-10 13:07:49 +03:00
|
|
|
object_unref(OBJECT(conn->tlscreds));
|
|
|
|
g_free(conn->initial_info.x_dirty_bitmap);
|
|
|
|
g_free(conn->initial_info.name);
|
2021-06-15 22:07:05 +03:00
|
|
|
g_free(conn);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:49 +03:00
|
|
|
/*
|
|
|
|
* Connect to @addr and do NBD negotiation if @info is not null. If @tlscreds
|
|
|
|
* are given @outioc is returned. @outioc is provided only on success. The call
|
|
|
|
* may be cancelled from other thread by simply qio_channel_shutdown(sioc).
|
|
|
|
*/
|
|
|
|
static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr,
|
|
|
|
NBDExportInfo *info, QCryptoTLSCreds *tlscreds,
|
|
|
|
QIOChannel **outioc, Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (outioc) {
|
|
|
|
*outioc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = qio_channel_socket_connect_sync(sioc, addr, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
qio_channel_set_delay(QIO_CHANNEL(sioc), false);
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), tlscreds,
|
|
|
|
tlscreds ? addr->u.inet.host : NULL,
|
|
|
|
outioc, info, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
/*
|
|
|
|
* nbd_receive_negotiate() may setup tls ioc and return it even on
|
|
|
|
* failure path. In this case we should use it instead of original
|
|
|
|
* channel.
|
|
|
|
*/
|
|
|
|
if (outioc && *outioc) {
|
|
|
|
qio_channel_close(QIO_CHANNEL(*outioc), NULL);
|
|
|
|
object_unref(OBJECT(*outioc));
|
|
|
|
*outioc = NULL;
|
|
|
|
} else {
|
|
|
|
qio_channel_close(QIO_CHANNEL(sioc), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-15 22:07:05 +03:00
|
|
|
static void *connect_thread_func(void *opaque)
|
|
|
|
{
|
|
|
|
NBDClientConnection *conn = opaque;
|
|
|
|
int ret;
|
|
|
|
bool do_free;
|
2021-06-10 13:07:50 +03:00
|
|
|
uint64_t timeout = 1;
|
|
|
|
uint64_t max_timeout = 16;
|
2021-06-15 22:07:05 +03:00
|
|
|
|
2021-06-10 13:07:51 +03:00
|
|
|
qemu_mutex_lock(&conn->mutex);
|
|
|
|
while (!conn->detached) {
|
2021-09-06 22:06:49 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
2021-06-10 13:07:51 +03:00
|
|
|
assert(!conn->sioc);
|
2021-06-10 13:07:50 +03:00
|
|
|
conn->sioc = qio_channel_socket_new();
|
2021-06-15 22:07:05 +03:00
|
|
|
|
2021-06-10 13:07:51 +03:00
|
|
|
qemu_mutex_unlock(&conn->mutex);
|
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
conn->updated_info = conn->initial_info;
|
2021-06-10 13:07:49 +03:00
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
ret = nbd_connect(conn->sioc, conn->saddr,
|
|
|
|
conn->do_negotiation ? &conn->updated_info : NULL,
|
2021-09-06 22:06:49 +03:00
|
|
|
conn->tlscreds, &conn->ioc, &local_err);
|
2021-06-15 22:07:05 +03:00
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
/*
|
|
|
|
* conn->updated_info will finally be returned to the user. Clear the
|
|
|
|
* pointers to our internally allocated strings, which are IN parameters
|
|
|
|
* of nbd_receive_negotiate() and therefore nbd_connect(). Caller
|
|
|
|
* shoudn't be interested in these fields.
|
|
|
|
*/
|
|
|
|
conn->updated_info.x_dirty_bitmap = NULL;
|
|
|
|
conn->updated_info.name = NULL;
|
|
|
|
|
2021-06-10 13:07:51 +03:00
|
|
|
qemu_mutex_lock(&conn->mutex);
|
|
|
|
|
2021-09-06 22:06:49 +03:00
|
|
|
error_free(conn->err);
|
|
|
|
conn->err = NULL;
|
|
|
|
error_propagate(&conn->err, local_err);
|
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
object_unref(OBJECT(conn->sioc));
|
|
|
|
conn->sioc = NULL;
|
2021-06-10 13:07:51 +03:00
|
|
|
if (conn->do_retry && !conn->detached) {
|
|
|
|
qemu_mutex_unlock(&conn->mutex);
|
|
|
|
|
2021-06-10 13:07:50 +03:00
|
|
|
sleep(timeout);
|
|
|
|
if (timeout < max_timeout) {
|
|
|
|
timeout *= 2;
|
|
|
|
}
|
2021-06-10 13:07:51 +03:00
|
|
|
|
|
|
|
qemu_mutex_lock(&conn->mutex);
|
2021-06-10 13:07:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2021-06-15 22:07:05 +03:00
|
|
|
|
2021-06-10 13:07:51 +03:00
|
|
|
/* mutex is locked */
|
2021-06-15 22:07:05 +03:00
|
|
|
|
|
|
|
assert(conn->running);
|
|
|
|
conn->running = false;
|
|
|
|
if (conn->wait_co) {
|
|
|
|
aio_co_wake(conn->wait_co);
|
|
|
|
conn->wait_co = NULL;
|
|
|
|
}
|
|
|
|
do_free = conn->detached;
|
|
|
|
|
|
|
|
qemu_mutex_unlock(&conn->mutex);
|
|
|
|
|
|
|
|
if (do_free) {
|
|
|
|
nbd_client_connection_do_free(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nbd_client_connection_release(NBDClientConnection *conn)
|
|
|
|
{
|
|
|
|
bool do_free = false;
|
|
|
|
|
|
|
|
if (!conn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
WITH_QEMU_LOCK_GUARD(&conn->mutex) {
|
|
|
|
assert(!conn->detached);
|
|
|
|
if (conn->running) {
|
|
|
|
conn->detached = true;
|
|
|
|
} else {
|
|
|
|
do_free = true;
|
|
|
|
}
|
2021-06-10 13:07:51 +03:00
|
|
|
if (conn->sioc) {
|
|
|
|
qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
|
|
|
|
QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
|
|
|
|
}
|
2021-06-15 22:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_free) {
|
|
|
|
nbd_client_connection_do_free(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a new connection in context of @conn:
|
|
|
|
* if the thread is running, wait for completion
|
|
|
|
* if the thread already succeeded in the background, and user didn't get the
|
|
|
|
* result, just return it now
|
|
|
|
* otherwise the thread is not running, so start a thread and wait for
|
|
|
|
* completion
|
2021-06-10 13:07:49 +03:00
|
|
|
*
|
2021-06-10 13:07:59 +03:00
|
|
|
* If @blocking is false, don't wait for the thread, return immediately.
|
|
|
|
*
|
2021-06-10 13:07:49 +03:00
|
|
|
* If @info is not NULL, also do nbd-negotiation after successful connection.
|
|
|
|
* In this case info is used only as out parameter, and is fully initialized by
|
|
|
|
* nbd_co_establish_connection(). "IN" fields of info as well as related only to
|
|
|
|
* nbd_receive_export_list() would be zero (see description of NBDExportInfo in
|
|
|
|
* include/block/nbd.h).
|
2021-06-15 22:07:05 +03:00
|
|
|
*/
|
2021-06-10 13:07:56 +03:00
|
|
|
QIOChannel *coroutine_fn
|
2021-06-10 13:07:49 +03:00
|
|
|
nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
|
2021-06-10 13:07:59 +03:00
|
|
|
bool blocking, Error **errp)
|
2021-06-15 22:07:05 +03:00
|
|
|
{
|
|
|
|
QemuThread thread;
|
|
|
|
|
2021-06-10 13:07:49 +03:00
|
|
|
if (conn->do_negotiation) {
|
|
|
|
assert(info);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
WITH_QEMU_LOCK_GUARD(&conn->mutex) {
|
|
|
|
/*
|
|
|
|
* Don't call nbd_co_establish_connection() in several coroutines in
|
|
|
|
* parallel. Only one call at once is supported.
|
|
|
|
*/
|
|
|
|
assert(!conn->wait_co);
|
|
|
|
|
|
|
|
if (!conn->running) {
|
|
|
|
if (conn->sioc) {
|
|
|
|
/* Previous attempt finally succeeded in background */
|
2021-06-10 13:07:49 +03:00
|
|
|
if (conn->do_negotiation) {
|
|
|
|
memcpy(info, &conn->updated_info, sizeof(*info));
|
2021-06-10 13:07:56 +03:00
|
|
|
if (conn->ioc) {
|
|
|
|
/* TLS channel now has own reference to parent */
|
|
|
|
object_unref(OBJECT(conn->sioc));
|
|
|
|
conn->sioc = NULL;
|
|
|
|
|
|
|
|
return g_steal_pointer(&conn->ioc);
|
|
|
|
}
|
2021-06-10 13:07:49 +03:00
|
|
|
}
|
2021-06-10 13:07:56 +03:00
|
|
|
|
|
|
|
assert(!conn->ioc);
|
|
|
|
|
|
|
|
return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
|
2021-06-10 13:07:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
conn->running = true;
|
|
|
|
qemu_thread_create(&thread, "nbd-connect",
|
|
|
|
connect_thread_func, conn, QEMU_THREAD_DETACHED);
|
2021-06-15 22:07:05 +03:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:59 +03:00
|
|
|
if (!blocking) {
|
2021-09-06 22:06:49 +03:00
|
|
|
if (conn->err) {
|
|
|
|
error_propagate(errp, error_copy(conn->err));
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "No connection at the moment");
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:59 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
conn->wait_co = qemu_coroutine_self();
|
2021-06-15 22:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are going to wait for connect-thread finish, but
|
|
|
|
* nbd_co_establish_connection_cancel() can interrupt.
|
|
|
|
*/
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
WITH_QEMU_LOCK_GUARD(&conn->mutex) {
|
|
|
|
if (conn->running) {
|
|
|
|
/*
|
|
|
|
* The connection attempt was canceled and the coroutine resumed
|
|
|
|
* before the connection thread finished its job. Report the
|
|
|
|
* attempt as failed, but leave the connection thread running,
|
|
|
|
* to reuse it for the next connection attempt.
|
|
|
|
*/
|
2021-09-06 22:06:49 +03:00
|
|
|
if (conn->err) {
|
|
|
|
error_propagate(errp, error_copy(conn->err));
|
|
|
|
} else {
|
2021-09-06 22:06:50 +03:00
|
|
|
/*
|
|
|
|
* The only possible case here is cancelling by open_timer
|
|
|
|
* during nbd_open(). So, the error message is for that case.
|
|
|
|
* If we have more use cases, we can refactor
|
|
|
|
* nbd_co_establish_connection_cancel() to take an additional
|
|
|
|
* parameter cancel_reason, that would be passed than to the
|
|
|
|
* caller of cancelled nbd_co_establish_connection().
|
|
|
|
*/
|
|
|
|
error_setg(errp, "Connection attempt cancelled by timeout");
|
2021-09-06 22:06:49 +03:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2021-09-06 22:06:49 +03:00
|
|
|
/* Thread finished. There must be either error or sioc */
|
|
|
|
assert(!conn->err != !conn->sioc);
|
|
|
|
|
|
|
|
if (conn->err) {
|
|
|
|
error_propagate(errp, error_copy(conn->err));
|
2021-06-10 13:07:56 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-06 22:06:49 +03:00
|
|
|
|
2021-06-10 13:07:56 +03:00
|
|
|
if (conn->do_negotiation) {
|
2021-06-10 13:07:49 +03:00
|
|
|
memcpy(info, &conn->updated_info, sizeof(*info));
|
2021-06-10 13:07:56 +03:00
|
|
|
if (conn->ioc) {
|
|
|
|
/* TLS channel now has own reference to parent */
|
|
|
|
object_unref(OBJECT(conn->sioc));
|
|
|
|
conn->sioc = NULL;
|
|
|
|
|
|
|
|
return g_steal_pointer(&conn->ioc);
|
|
|
|
}
|
2021-06-10 13:07:49 +03:00
|
|
|
}
|
2021-06-10 13:07:56 +03:00
|
|
|
|
|
|
|
assert(!conn->ioc);
|
|
|
|
|
|
|
|
return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
|
2021-06-10 13:07:48 +03:00
|
|
|
}
|
2021-06-15 22:07:05 +03:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
abort(); /* unreachable */
|
2021-06-15 22:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nbd_co_establish_connection_cancel
|
|
|
|
* Cancel nbd_co_establish_connection() asynchronously.
|
|
|
|
*
|
|
|
|
* Note that this function neither directly stops the thread nor closes the
|
|
|
|
* socket, but rather safely wakes nbd_co_establish_connection() which is
|
|
|
|
* sleeping in yield()
|
|
|
|
*/
|
|
|
|
void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
|
|
|
|
{
|
|
|
|
Coroutine *wait_co;
|
|
|
|
|
2021-06-10 13:07:48 +03:00
|
|
|
WITH_QEMU_LOCK_GUARD(&conn->mutex) {
|
|
|
|
wait_co = g_steal_pointer(&conn->wait_co);
|
|
|
|
}
|
2021-06-15 22:07:05 +03:00
|
|
|
|
|
|
|
if (wait_co) {
|
|
|
|
aio_co_wake(wait_co);
|
|
|
|
}
|
|
|
|
}
|