At the moment, NBD client internals cannot be accessed. Therefore, tests have to infer the client state from I/O results and wait with sleeps, which is racy.
Add the x-debug-query-nbd-client QMP command that reports the state of the client's connection and of its connector. Other fields will be added when needed. The next patch uses this command to make the nbd-stale-connection test deterministic. Signed-off-by: Guillaume Vincke <[email protected]> --- block/nbd.c | 57 +++++++++++++++++++++++++++++ include/block/nbd.h | 9 +++++ nbd/client-connection.c | 18 ++++++++++ qapi/block-core.json | 79 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+) diff --git a/block/nbd.c b/block/nbd.c index e5e16722ba..b9f8d92e43 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -35,9 +35,11 @@ #include "qemu/cutils.h" #include "qemu/main-loop.h" +#include "qapi/qapi-commands-block-core.h" #include "qapi/qapi-visit-sockets.h" #include "qobject/qstring.h" #include "qapi/clone-visitor.h" +#include "qapi/error.h" #include "block/qdict.h" #include "block/nbd.h" @@ -2246,6 +2248,61 @@ static BlockDriver bdrv_nbd_unix = { .bdrv_detach_aio_context = nbd_detach_aio_context, }; +XDbgNbdClient *qmp_x_debug_query_nbd_client(const char *node_name, + Error **errp) +{ + BlockDriverState *bs; + BDRVNBDState *s; + XDbgNbdClient *info; + + bs = bdrv_find_node(node_name); + if (!bs) { + error_setg(errp, "Node '%s' not found", node_name); + return NULL; + } + + if (bs->drv != &bdrv_nbd && + bs->drv != &bdrv_nbd_tcp && + bs->drv != &bdrv_nbd_unix) { + error_setg(errp, "Node '%s' is not an NBD client", node_name); + return NULL; + } + + s = bs->opaque; + info = g_new0(XDbgNbdClient, 1); + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + switch (s->state) { + case NBD_CLIENT_CONNECTING_WAIT: + info->state = XDBG_NBD_CLIENT_STATE_CONNECTING_WAIT; + break; + case NBD_CLIENT_CONNECTING_NOWAIT: + info->state = XDBG_NBD_CLIENT_STATE_CONNECTING_NOWAIT; + break; + case NBD_CLIENT_CONNECTED: + info->state = XDBG_NBD_CLIENT_STATE_CONNECTED; + break; + case NBD_CLIENT_QUIT: + info->state = XDBG_NBD_CLIENT_STATE_QUIT; + break; + } + } + + switch (nbd_client_connection_get_thread_state(s->conn)) { + case NBD_CLIENT_CONNECTION_THREAD_IDLE: + break; + case NBD_CLIENT_CONNECTION_THREAD_RUNNING: + info->connector = g_new0(XDbgNbdClientConnector, 1); + info->connector->state = XDBG_NBD_CLIENT_CONNECTOR_STATE_RUNNING; + break; + case NBD_CLIENT_CONNECTION_THREAD_PARKED: + info->connector = g_new0(XDbgNbdClientConnector, 1); + info->connector->state = XDBG_NBD_CLIENT_CONNECTOR_STATE_PARKED; + break; + } + + return info; +} + static void bdrv_nbd_init(void) { bdrv_register(&bdrv_nbd); diff --git a/include/block/nbd.h b/include/block/nbd.h index ab40842da9..15ddec91ac 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -492,6 +492,15 @@ const char *nbd_mode_lookup(NBDMode mode); /* nbd/client-connection.c */ void nbd_client_connection_enable_retry(NBDClientConnection *conn); +typedef enum NBDClientConnectionThreadState { + NBD_CLIENT_CONNECTION_THREAD_IDLE, + NBD_CLIENT_CONNECTION_THREAD_RUNNING, + NBD_CLIENT_CONNECTION_THREAD_PARKED, +} NBDClientConnectionThreadState; + +NBDClientConnectionThreadState +nbd_client_connection_get_thread_state(NBDClientConnection *conn); + NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, bool do_negotiation, const char *export_name, diff --git a/nbd/client-connection.c b/nbd/client-connection.c index aec62934d1..7172ba49b3 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -78,6 +78,24 @@ void nbd_client_connection_enable_retry(NBDClientConnection *conn) conn->do_retry = true; } +NBDClientConnectionThreadState +nbd_client_connection_get_thread_state(NBDClientConnection *conn) +{ + NBDClientConnectionThreadState ret; + + WITH_QEMU_LOCK_GUARD(&conn->mutex) { + if (conn->running) { + ret = NBD_CLIENT_CONNECTION_THREAD_RUNNING; + } else if (conn->sioc) { + ret = NBD_CLIENT_CONNECTION_THREAD_PARKED; + } else { + ret = NBD_CLIENT_CONNECTION_THREAD_IDLE; + } + } + + return ret; +} + NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, bool do_negotiation, const char *export_name, diff --git a/qapi/block-core.json b/qapi/block-core.json index 23c5787d99..973be06dfd 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2217,6 +2217,85 @@ 'features': [ 'unstable' ], 'allow-preconfig': true } +## +# @XDbgNbdClientState: +# Enum of NBD client state. +# +# @connecting-wait: No active connection. A request waits for the +# connection attempt to finish, while reconnect-delay has not +# expired. +# +# @connecting-nowait: No active connection. A request fails at once +# if no channel is ready. +# +# @connected: Client has a valid connection. +# +# @quit: Client will not reconnect. +# +# Since: 11.2 +## +{ 'enum': 'XDbgNbdClientState', + 'data': [ 'connecting-wait', 'connecting-nowait', 'connected', 'quit' ] } + +## +# @XDbgNbdClientConnectorState: +# Enum of NBD client connector state. +# +# @running: Connection attempt is in progress. +# +# @parked: Connection was successful and channel is kept for the next +# request. +# +# Since: 11.2 +## +{ 'enum': 'XDbgNbdClientConnectorState', + 'data': [ 'running', 'parked' ] } + +## +# @XDbgNbdClientConnector: +# The NBD connector information. +# +# @state: State of the connector. +# +# Since: 11.2 +## +{ 'struct': 'XDbgNbdClientConnector', + 'data': { 'state': 'XDbgNbdClientConnectorState' } } + +## +# @XDbgNbdClient: +# The NBD connection state of the client and its connector. +# The two members are read independently and may not reflect a +# single point in time. +# +# @state: The client state. +# +# @connector: The connector details. Only present while a connection +# attempt is running or its channel is parked. +# +# Since: 11.2 +## +{ 'struct': 'XDbgNbdClient', + 'data': { 'state': 'XDbgNbdClientState', + '*connector': 'XDbgNbdClientConnector' } } + +## +# @x-debug-query-nbd-client: +# Get the NBD client state. +# +# @node-name: node name of the NBD client to query. +# +# Features: +# +# @unstable: This command is meant for debugging and testing. +# +# Since: 11.2 +## +{ 'command': 'x-debug-query-nbd-client', + 'data': { 'node-name': 'str' }, + 'returns': 'XDbgNbdClient', + 'features': [ 'unstable' ] } + ## # @drive-mirror: # Start mirroring a block device's writes to a new destination. -- 2.43.0
