*/
#include "qemu/osdep.h"
+#include "trace.h"
#include "block/nbd.h"
#include "qapi/qapi-visit-sockets.h"
#include "qapi/clone-visitor.h"
+#include "qemu/coroutine.h"
struct NBDClientConnection {
- /* Initialization constants */
+ /* Initialization constants, never change */
SocketAddress *saddr; /* address to connect to */
+ QCryptoTLSCreds *tlscreds;
+ char *tlshostname;
+ NBDExportInfo initial_info;
+ bool do_negotiation;
+ bool do_retry;
QemuMutex mutex;
+ NBDExportInfo updated_info;
/*
- * @sioc and @err represent a connection attempt. While running
- * is true, they are only used by the connection thread, and mutex
- * locking is not needed. Once the thread finishes,
- * nbd_co_establish_connection then steals these pointers while
- * under the mutex.
+ * @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.
*/
QIOChannelSocket *sioc;
+ QIOChannel *ioc;
+ /*
+ * @err represents previous attempt. It may be copied by
+ * nbd_co_establish_connection() when it reports failure.
+ */
Error *err;
/* All further fields are accessed only under mutex */
Coroutine *wait_co;
};
-NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr)
+/*
+ * 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;
+}
+
+NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
+ bool do_negotiation,
+ const char *export_name,
+ const char *x_dirty_bitmap,
+ QCryptoTLSCreds *tlscreds,
+ const char *tlshostname)
{
NBDClientConnection *conn = g_new(NBDClientConnection, 1);
+ object_ref(OBJECT(tlscreds));
*conn = (NBDClientConnection) {
.saddr = QAPI_CLONE(SocketAddress, saddr),
+ .tlscreds = tlscreds,
+ .tlshostname = g_strdup(tlshostname),
+ .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 ?: "")
};
qemu_mutex_init(&conn->mutex);
}
error_free(conn->err);
qapi_free_SocketAddress(conn->saddr);
+ g_free(conn->tlshostname);
+ object_unref(OBJECT(conn->tlscreds));
+ g_free(conn->initial_info.x_dirty_bitmap);
+ g_free(conn->initial_info.name);
g_free(conn);
}
-static void *connect_thread_func(void *opaque)
+/*
+ * 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,
+ const char *tlshostname,
+ QIOChannel **outioc, Error **errp)
{
- NBDClientConnection *conn = opaque;
int ret;
- bool do_free;
- conn->sioc = qio_channel_socket_new();
+ if (outioc) {
+ *outioc = NULL;
+ }
- error_free(conn->err);
- conn->err = NULL;
- ret = qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn->err);
+ ret = qio_channel_socket_connect_sync(sioc, addr, errp);
if (ret < 0) {
- object_unref(OBJECT(conn->sioc));
- conn->sioc = NULL;
+ return ret;
+ }
+
+ qio_channel_set_delay(QIO_CHANNEL(sioc), false);
+
+ if (!info) {
+ return 0;
}
- qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false);
+ ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), tlscreds, tlshostname,
+ 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(*outioc, NULL);
+ object_unref(OBJECT(*outioc));
+ *outioc = NULL;
+ } else {
+ qio_channel_close(QIO_CHANNEL(sioc), NULL);
+ }
+
+ return ret;
+ }
+
+ return 0;
+}
+
+static void *connect_thread_func(void *opaque)
+{
+ NBDClientConnection *conn = opaque;
+ int ret;
+ bool do_free;
+ uint64_t timeout = 1;
+ uint64_t max_timeout = 16;
qemu_mutex_lock(&conn->mutex);
+ while (!conn->detached) {
+ Error *local_err = NULL;
+
+ assert(!conn->sioc);
+ conn->sioc = qio_channel_socket_new();
+
+ qemu_mutex_unlock(&conn->mutex);
+
+ conn->updated_info = conn->initial_info;
+
+ ret = nbd_connect(conn->sioc, conn->saddr,
+ conn->do_negotiation ? &conn->updated_info : NULL,
+ conn->tlscreds, conn->tlshostname,
+ &conn->ioc, &local_err);
+
+ /*
+ * 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
+ * shouldn't be interested in these fields.
+ */
+ conn->updated_info.x_dirty_bitmap = NULL;
+ conn->updated_info.name = NULL;
+
+ qemu_mutex_lock(&conn->mutex);
+
+ error_free(conn->err);
+ conn->err = NULL;
+ error_propagate(&conn->err, local_err);
+
+ if (ret < 0) {
+ object_unref(OBJECT(conn->sioc));
+ conn->sioc = NULL;
+ if (conn->do_retry && !conn->detached) {
+ trace_nbd_connect_thread_sleep(timeout);
+ qemu_mutex_unlock(&conn->mutex);
+
+ sleep(timeout);
+ if (timeout < max_timeout) {
+ timeout *= 2;
+ }
+
+ qemu_mutex_lock(&conn->mutex);
+ continue;
+ }
+ }
+
+ break;
+ }
+
+ /* mutex is locked */
assert(conn->running);
conn->running = false;
} else {
do_free = true;
}
+ if (conn->sioc) {
+ qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
+ QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
+ }
}
if (do_free) {
* result, just return it now
* otherwise the thread is not running, so start a thread and wait for
* completion
+ *
+ * If @blocking is false, don't wait for the thread, return immediately.
+ *
+ * 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).
*/
-QIOChannelSocket *coroutine_fn
-nbd_co_establish_connection(NBDClientConnection *conn, Error **errp)
+QIOChannel *coroutine_fn
+nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
+ bool blocking, Error **errp)
{
QemuThread thread;
+ if (conn->do_negotiation) {
+ assert(info);
+ }
+
WITH_QEMU_LOCK_GUARD(&conn->mutex) {
/*
* Don't call nbd_co_establish_connection() in several coroutines in
if (!conn->running) {
if (conn->sioc) {
/* Previous attempt finally succeeded in background */
- return g_steal_pointer(&conn->sioc);
+ if (conn->do_negotiation) {
+ memcpy(info, &conn->updated_info, sizeof(*info));
+ 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);
+ }
+ }
+
+ assert(!conn->ioc);
+
+ return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
}
conn->running = true;
- error_free(conn->err);
- conn->err = NULL;
qemu_thread_create(&thread, "nbd-connect",
connect_thread_func, conn, QEMU_THREAD_DETACHED);
}
+ if (!blocking) {
+ if (conn->err) {
+ error_propagate(errp, error_copy(conn->err));
+ } else {
+ error_setg(errp, "No connection at the moment");
+ }
+
+ return NULL;
+ }
+
conn->wait_co = qemu_coroutine_self();
}
* attempt as failed, but leave the connection thread running,
* to reuse it for the next connection attempt.
*/
- error_setg(errp, "Connection attempt cancelled by other operation");
+ if (conn->err) {
+ error_propagate(errp, error_copy(conn->err));
+ } else {
+ /*
+ * 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");
+ }
+
return NULL;
} else {
- error_propagate(errp, conn->err);
- conn->err = NULL;
- return g_steal_pointer(&conn->sioc);
+ /* Thread finished. There must be either error or sioc */
+ assert(!conn->err != !conn->sioc);
+
+ if (conn->err) {
+ error_propagate(errp, error_copy(conn->err));
+ return NULL;
+ }
+
+ if (conn->do_negotiation) {
+ memcpy(info, &conn->updated_info, sizeof(*info));
+ 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);
+ }
+ }
+
+ assert(!conn->ioc);
+
+ return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
}
}