All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] nbd: Add an unstable query for the client state
@ 2026-11-13 14:08 Guillaume Vincke
  2026-11-13 14:08 ` [PATCH v2 1/2] " Guillaume Vincke
  2026-11-13 14:08 ` [PATCH v2 2/2] iotests: Make nbd-stale-connection test deterministic Guillaume Vincke
  0 siblings, 2 replies; 4+ messages in thread
From: Guillaume Vincke @ 2026-11-13 14:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, armbru,
	Guillaume Vincke

Based-on: <20261105164124.249395-1-guillaume.vincke@ovhcloud.com>

These changes follow the discussion on:
https://patchew.org/QEMU/20261105164124.249395-1-guillaume.vincke@ovhcloud.com/#au4tsMuhKjq62dVB@redhat.com
Even though Eric Blake suggested a query for NBD server state,
considering the nbd-stale-connection test we are trying to make
deterministic relies on qemu-nbd, which does not have QMP, this
patchset adds a command to query the NBD client state.

The created QMP command output lists the client internals. The
connector field is used to give the NBD client connection thread
details, i.e. when idle, running or when a channel is ready to be
picked up. Other fields such as in-flight or negotiated size can easily
be added at the top level while others like the connector's last error
would be added into the connector one.

Considering this command accesses the client's private state, it is
implemented in block/nbd.c where BDRVNBDState is defined.

v1: https://patchew.org/QEMU/20261109160609.1434289-1-guillaume.vincke@ovhcloud.com

v1 -> v2:
- patch 1: Connector is now always present, with an idle state. Read
client and connector states under the same lock. Add the NBD state
information to the QMP documentation. Fix documentation wording and
version number.
- patch 2: Simplify the connector lookup using the new QMP command
output.

Guillaume Vincke (2):
  nbd: Add an unstable query for the client state
  iotests: Make nbd-stale-connection test deterministic

 block/nbd.c                                   | 57 ++++++++++++
 include/block/nbd.h                           |  9 ++
 nbd/client-connection.c                       | 22 +++++
 qapi/block-core.json                          | 87 +++++++++++++++++++
 tests/qemu-iotests/tests/nbd-stale-connection | 17 ++--
 5 files changed, 187 insertions(+), 5 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] nbd: Add an unstable query for the client state
  2026-11-13 14:08 [PATCH v2 0/2] nbd: Add an unstable query for the client state Guillaume Vincke
@ 2026-11-13 14:08 ` Guillaume Vincke
  2026-11-16 10:39   ` Markus Armbruster
  2026-11-13 14:08 ` [PATCH v2 2/2] iotests: Make nbd-stale-connection test deterministic Guillaume Vincke
  1 sibling, 1 reply; 4+ messages in thread
From: Guillaume Vincke @ 2026-11-13 14:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, armbru,
	Guillaume Vincke

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 <guillaume.vincke@ovhcloud.com>
---
 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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] iotests: Make nbd-stale-connection test deterministic
  2026-11-13 14:08 [PATCH v2 0/2] nbd: Add an unstable query for the client state Guillaume Vincke
  2026-11-13 14:08 ` [PATCH v2 1/2] " Guillaume Vincke
@ 2026-11-13 14:08 ` Guillaume Vincke
  1 sibling, 0 replies; 4+ messages in thread
From: Guillaume Vincke @ 2026-11-13 14:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, armbru,
	Guillaume Vincke

At the moment, the test sleeps to wait for the connector to park a new
channel, which can fail on slow or loaded hosts.

Poll the x-debug-query-nbd-client command to verify the current state
instead of relying on timing.

Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
 tests/qemu-iotests/tests/nbd-stale-connection | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/tests/qemu-iotests/tests/nbd-stale-connection b/tests/qemu-iotests/tests/nbd-stale-connection
index e361bccfa9..3f7f68e979 100755
--- a/tests/qemu-iotests/tests/nbd-stale-connection
+++ b/tests/qemu-iotests/tests/nbd-stale-connection
@@ -12,14 +12,21 @@ import iotests
 from iotests import qemu_nbd_popen, qemu_img_create, file_path, log
 
 
-# estimated duration required by the connect thread to reach a running server
-MAX_NBD_CONNECT_DURATION = 1
-
 iotests.script_initialize(supported_fmts=['qcow2'])
 nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir)
 disk = file_path('disk')
 qemu_img_create('-f', iotests.imgfmt, disk, '1M')
 
+
+def wait_for_parked_connection(virtm, node_name):
+    with iotests.Timeout(30, "connect-thread did not reach 'parked' state"):
+        while True:
+            client = virtm.qmp('x-debug-query-nbd-client', node_name=node_name)
+            if client['return']['connector']['state'] == 'parked':
+                return
+            time.sleep(0.1)
+
+
 with iotests.VM() as vm:
 
     vm.launch()
@@ -44,7 +51,7 @@ with iotests.VM() as vm:
         # in CONNECTING, the operation starts the connect thread
         log(vm.qmp('x-qemu-io', device='nbd0', command='write -P 0x5a 0 64k'))
         # wait for the connect thread to get a new channel and stop
-        time.sleep(MAX_NBD_CONNECT_DURATION)
+        wait_for_parked_connection(vm, 'nbd0')
 
     iotests.log('=== NBD server down. The parked channel is closed ===')
 
@@ -56,5 +63,5 @@ with iotests.VM() as vm:
         # Wait for it to succeed
         iotests.log('=== Wait for the connect thread to park the new channel. '
                     'Subsequent operations are successful ===')
-        time.sleep(MAX_NBD_CONNECT_DURATION)
+        wait_for_parked_connection(vm, 'nbd0')
         log(vm.qmp('x-qemu-io', device='nbd0', command='write -P 0x5a 0 64k'))
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] nbd: Add an unstable query for the client state
  2026-11-13 14:08 ` [PATCH v2 1/2] " Guillaume Vincke
@ 2026-11-16 10:39   ` Markus Armbruster
  0 siblings, 0 replies; 4+ messages in thread
From: Markus Armbruster @ 2026-11-16 10:39 UTC (permalink / raw)
  To: Guillaume Vincke
  Cc: qemu-devel, qemu-block, eblake, vsementsov, kwolf, hreitz, armbru

Guillaume Vincke <guillaume.vincke@ovhcloud.com> writes:

> 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 <guillaume.vincke@ovhcloud.com>

[...]

> 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' } }

You got rid of "The two members are read independently and may not
reflect a single point in time."  Good!

You also made @connector mandatory.  I think that simplified things.

I could grill you some more on the state machine.  Bah, the doc comemnts
are good enough for a testing / debugging command.

> +
> +##
> +# @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.

QAPI schema
Acked-by: Markus Armbruster <armbru@redhat.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-16 10:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13 14:08 [PATCH v2 0/2] nbd: Add an unstable query for the client state Guillaume Vincke
2026-11-13 14:08 ` [PATCH v2 1/2] " Guillaume Vincke
2026-11-16 10:39   ` Markus Armbruster
2026-11-13 14:08 ` [PATCH v2 2/2] iotests: Make nbd-stale-connection test deterministic Guillaume Vincke

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.