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 | 22 +++++++++++
 qapi/block-core.json    | 87 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 175 insertions(+)

diff --git a/block/nbd.c b/block/nbd.c
index e5e16722ba..49f585eab5 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;
+        }
+
+        info->connector = g_new0(XDbgNbdClientConnector, 1);
+        switch (nbd_client_connection_get_thread_state(s->conn)) {
+        case NBD_CLIENT_CONNECTION_THREAD_IDLE:
+            info->connector->state = XDBG_NBD_CLIENT_CONNECTOR_STATE_IDLE;
+            break;
+        case NBD_CLIENT_CONNECTION_THREAD_RUNNING:
+            info->connector->state = XDBG_NBD_CLIENT_CONNECTOR_STATE_RUNNING;
+            break;
+        case NBD_CLIENT_CONNECTION_THREAD_PARKED:
+            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..78caea3c37 100644
--- a/nbd/client-connection.c
+++ b/nbd/client-connection.c
@@ -42,6 +42,10 @@ struct NBDClientConnection {
     bool do_negotiation;
     bool do_retry;
 
+    /*
+     * @mutex is taken under BDRVNBDState.requests_lock. Calling back into the
+     * block driver with it held is unsafe.
+     */
     QemuMutex mutex;
 
     NBDExportInfo updated_info;
@@ -78,6 +82,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..0aa3ced3fe 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -2217,6 +2217,93 @@
   'features': [ 'unstable' ],
   'allow-preconfig': true }
 
+##
+# @XDbgNbdClientState:
+#     An enumeration of NBD client states.
+#
+# @connecting-wait: No active connection.  Reached when the client
+#     hits an I/O error.  A request waits for the connection attempt
+#     to finish, while reconnect-delay has not expired.
+#
+# @connecting-nowait: No active connection.  Reached when
+#     reconnect-delay expires.  A request fails at once if no channel
+#     is ready.
+#
+# @connected: Client has a valid connection.  Reached when a request
+#     gets a channel from the connector.
+#
+# @quit: Client stopped and will never reconnect.  New requests
+#     fail.  A running attempt or a parked channel stays until the
+#     node is closed.  Reached on fatal error, teardown or yank.
+#
+# Since: 12.0
+##
+{ 'enum': 'XDbgNbdClientState',
+  'data': [ 'connecting-wait', 'connecting-nowait', 'connected', 'quit' ] }
+
+##
+# @XDbgNbdClientConnectorState:
+#     An enumeration of NBD client connector states.
+#
+# @idle: No connection attempt running and no parked channel.  In
+#     'connecting-wait' or 'connecting-nowait', the next request
+#     starts an attempt.
+#
+# @running: Connection attempt is in progress.  A request waits or
+#     fails, as described in 'connecting-wait' and
+#     'connecting-nowait'.
+#
+# @parked: Connection was successful and channel is kept.  The next
+#     request takes the channel and the client goes to 'connected'.
+#
+# Since: 12.0
+##
+{ 'enum': 'XDbgNbdClientConnectorState',
+  'data': [ 'idle', 'running', 'parked' ] }
+
+##
+# @XDbgNbdClientConnector:
+#     The NBD connector information.
+#
+# @state: State of the connector.
+#
+# Since: 12.0
+##
+{ 'struct': 'XDbgNbdClientConnector',
+  'data': { 'state': 'XDbgNbdClientConnectorState' } }
+
+##
+# @XDbgNbdClient:
+#     The NBD connection state of the client and its connector.
+#
+# @state: The client state.
+#
+# @connector: The connector details.
+#
+# Since: 12.0
+##
+{ 'struct': 'XDbgNbdClient',
+  'data': { 'state': 'XDbgNbdClientState',
+            'connector': 'XDbgNbdClientConnector' } }
+
+##
+# @x-debug-query-nbd-client:
+#     Get the NBD client state.
+#
+# @node-name: node name of a block node using the nbd driver.
+#     Use `query-named-block-nodes` to list block nodes.
+#
+# Features:
+#
+# @unstable: This command is meant for debugging and testing.
+#
+# Since: 12.0
+##
+{ '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


Reply via email to