* [PATCH v4 1/2] nbd: Add an unstable query for the client state
2026-12-03 12:20 [PATCH v4 0/2] nbd: Add an unstable query for the client state Guillaume Vincke
@ 2026-12-03 12:20 ` Guillaume Vincke
2026-12-03 18:41 ` Eric Blake
2026-12-03 12:20 ` [PATCH v4 2/2] iotests: Add coverage for x-debug-query-nbd-client QMP command Guillaume Vincke
1 sibling, 1 reply; 6+ messages in thread
From: Guillaume Vincke @ 2026-12-03 12:20 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, Eric Blake, Vladimir Sementsov-Ogievskiy, Kevin Wolf,
Hanna Reitz, Markus Armbruster, 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.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
Acked-by: Markus Armbruster <armbru@redhat.com>
---
qapi/block-core.json | 87 +++++++++++++++++++++++++++++++++++++++++
include/block/nbd.h | 4 ++
block/nbd.c | 84 +++++++++++++++++++++++++--------------
nbd/client-connection.c | 22 +++++++++++
4 files changed, 168 insertions(+), 29 deletions(-)
diff --git a/qapi/block-core.json b/qapi/block-core.json
index 7aa4fa2df9..be5d120071 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -2217,6 +2217,93 @@
'features': [ 'unstable' ],
'allow-preconfig': true }
+##
+# @NbdClientState:
+# 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': 'NbdClientState',
+ 'data': [ 'connecting-wait', 'connecting-nowait', 'connected', 'quit' ] }
+
+##
+# @NbdClientConnectorState:
+# 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': 'NbdClientConnectorState',
+ 'data': [ 'idle', 'running', 'parked' ] }
+
+##
+# @XDbgNbdClientConnector:
+# The NBD connector information.
+#
+# @state: State of the connector.
+#
+# Since: 12.0
+##
+{ 'struct': 'XDbgNbdClientConnector',
+ 'data': { 'state': 'NbdClientConnectorState' } }
+
+##
+# @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': 'NbdClientState',
+ '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.
diff --git a/include/block/nbd.h b/include/block/nbd.h
index ab40842da9..b7a1a8ebbe 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -23,6 +23,7 @@
#include "block/export.h"
#include "io/channel-socket.h"
#include "crypto/tlscreds.h"
+#include "qapi/qapi-types-block-core.h"
#include "qapi/error.h"
#include "qemu/bswap.h"
@@ -492,6 +493,9 @@ const char *nbd_mode_lookup(NBDMode mode);
/* nbd/client-connection.c */
void nbd_client_connection_enable_retry(NBDClientConnection *conn);
+NbdClientConnectorState
+nbd_client_get_connector_state(NBDClientConnection *conn);
+
NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
bool do_negotiation,
const char *export_name,
diff --git a/block/nbd.c b/block/nbd.c
index fb4295bb23..01f27db726 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"
@@ -58,13 +60,6 @@ typedef struct {
bool receiving; /* sleeping in the yield in nbd_receive_replies */
} NBDClientRequest;
-typedef enum NBDClientState {
- NBD_CLIENT_CONNECTING_WAIT,
- NBD_CLIENT_CONNECTING_NOWAIT,
- NBD_CLIENT_CONNECTED,
- NBD_CLIENT_QUIT
-} NBDClientState;
-
typedef struct BDRVNBDState {
QIOChannel *ioc; /* The current I/O channel */
NBDExportInfo info;
@@ -74,7 +69,7 @@ typedef struct BDRVNBDState {
* reconnect_delay_timer.
*/
QemuMutex requests_lock;
- NBDClientState state;
+ NbdClientState state;
CoQueue free_sema;
unsigned in_flight;
NBDClientRequest requests[MAX_NBD_REQUESTS];
@@ -176,17 +171,17 @@ static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s)
/* Called with s->requests_lock held. */
static void coroutine_fn nbd_channel_error_locked(BDRVNBDState *s, int ret)
{
- if (s->state == NBD_CLIENT_CONNECTED) {
+ if (s->state == NBD_CLIENT_STATE_CONNECTED) {
qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
}
if (ret == -EIO) {
- if (s->state == NBD_CLIENT_CONNECTED) {
- s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
- NBD_CLIENT_CONNECTING_NOWAIT;
+ if (s->state == NBD_CLIENT_STATE_CONNECTED) {
+ s->state = s->reconnect_delay ? NBD_CLIENT_STATE_CONNECTING_WAIT :
+ NBD_CLIENT_STATE_CONNECTING_NOWAIT;
}
} else {
- s->state = NBD_CLIENT_QUIT;
+ s->state = NBD_CLIENT_STATE_QUIT;
}
}
@@ -210,10 +205,10 @@ static void reconnect_delay_timer_cb(void *opaque)
reconnect_delay_timer_del(s);
WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
- if (s->state != NBD_CLIENT_CONNECTING_WAIT) {
+ if (s->state != NBD_CLIENT_STATE_CONNECTING_WAIT) {
return;
}
- s->state = NBD_CLIENT_CONNECTING_NOWAIT;
+ s->state = NBD_CLIENT_STATE_CONNECTING_NOWAIT;
}
nbd_co_establish_connection_cancel(s->conn);
}
@@ -243,7 +238,7 @@ static void nbd_teardown_connection(BlockDriverState *bs)
}
WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
- s->state = NBD_CLIENT_QUIT;
+ s->state = NBD_CLIENT_STATE_QUIT;
}
}
@@ -279,7 +274,7 @@ static bool nbd_client_will_reconnect(BDRVNBDState *s)
* Called only after a socket error, so this is not performance sensitive.
*/
QEMU_LOCK_GUARD(&s->requests_lock);
- return s->state == NBD_CLIENT_CONNECTING_WAIT;
+ return s->state == NBD_CLIENT_STATE_CONNECTING_WAIT;
}
/*
@@ -371,7 +366,7 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,
/* successfully connected */
WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
- s->state = NBD_CLIENT_CONNECTED;
+ s->state = NBD_CLIENT_STATE_CONNECTED;
}
return 0;
@@ -380,15 +375,15 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,
/* Called with s->requests_lock held. */
static bool nbd_client_connecting(BDRVNBDState *s)
{
- return s->state == NBD_CLIENT_CONNECTING_WAIT ||
- s->state == NBD_CLIENT_CONNECTING_NOWAIT;
+ return s->state == NBD_CLIENT_STATE_CONNECTING_WAIT ||
+ s->state == NBD_CLIENT_STATE_CONNECTING_NOWAIT;
}
/* Called with s->requests_lock taken. */
static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_attempt(BDRVNBDState *s)
{
int ret;
- bool blocking = s->state == NBD_CLIENT_CONNECTING_WAIT;
+ bool blocking = s->state == NBD_CLIENT_STATE_CONNECTING_WAIT;
/*
* Now we are sure that nobody is accessing the channel, and no one will
@@ -402,7 +397,7 @@ static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_attempt(BDRVNBDState *s)
if (blocking && !s->reconnect_delay_timer) {
/*
* It's the first reconnect attempt after switching to
- * NBD_CLIENT_CONNECTING_WAIT
+ * NBD_CLIENT_STATE_CONNECTING_WAIT
*/
g_assert(s->reconnect_delay);
reconnect_delay_timer_init(s,
@@ -517,17 +512,17 @@ nbd_co_send_request(BlockDriverState *bs, NBDRequest *request,
qemu_mutex_lock(&s->requests_lock);
while (s->in_flight == MAX_NBD_REQUESTS ||
- (s->state != NBD_CLIENT_CONNECTED && s->in_flight > 0)) {
+ (s->state != NBD_CLIENT_STATE_CONNECTED && s->in_flight > 0)) {
qemu_co_queue_wait(&s->free_sema, &s->requests_lock);
}
s->in_flight++;
- if (s->state != NBD_CLIENT_CONNECTED) {
+ if (s->state != NBD_CLIENT_STATE_CONNECTED) {
if (nbd_client_connecting(s)) {
nbd_reconnect_attempt(s);
qemu_co_queue_restart_all(&s->free_sema);
}
- if (s->state != NBD_CLIENT_CONNECTED) {
+ if (s->state != NBD_CLIENT_STATE_CONNECTED) {
rc = -EIO;
goto err;
}
@@ -1573,7 +1568,7 @@ static void nbd_yank(void *opaque)
QEMU_LOCK_GUARD(&s->requests_lock);
qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
- s->state = NBD_CLIENT_QUIT;
+ s->state = NBD_CLIENT_STATE_QUIT;
}
static void nbd_client_close(BlockDriverState *bs)
@@ -2016,7 +2011,7 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
s->open_timeout * NANOSECONDS_PER_SECOND);
}
- s->state = NBD_CLIENT_CONNECTING_WAIT;
+ s->state = NBD_CLIENT_STATE_CONNECTING_WAIT;
ret = nbd_do_establish_connection(bs, true, errp);
if (ret < 0) {
goto fail;
@@ -2185,8 +2180,8 @@ static void nbd_cancel_in_flight(BlockDriverState *bs)
reconnect_delay_timer_del(s);
qemu_mutex_lock(&s->requests_lock);
- if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
- s->state = NBD_CLIENT_CONNECTING_NOWAIT;
+ if (s->state == NBD_CLIENT_STATE_CONNECTING_WAIT) {
+ s->state = NBD_CLIENT_STATE_CONNECTING_NOWAIT;
}
qemu_mutex_unlock(&s->requests_lock);
@@ -2305,6 +2300,37 @@ 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) {
+ info->state = s->state;
+ info->connector = g_new0(XDbgNbdClientConnector, 1);
+ info->connector->state = nbd_client_get_connector_state(s->conn);
+ }
+
+ return info;
+}
+
static void bdrv_nbd_init(void)
{
bdrv_register(&bdrv_nbd);
diff --git a/nbd/client-connection.c b/nbd/client-connection.c
index 6a4f080717..231164766e 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;
}
+NbdClientConnectorState
+nbd_client_get_connector_state(NBDClientConnection *conn)
+{
+ NbdClientConnectorState ret;
+
+ WITH_QEMU_LOCK_GUARD(&conn->mutex) {
+ if (conn->running) {
+ ret = NBD_CLIENT_CONNECTOR_STATE_RUNNING;
+ } else if (conn->sioc) {
+ ret = NBD_CLIENT_CONNECTOR_STATE_PARKED;
+ } else {
+ ret = NBD_CLIENT_CONNECTOR_STATE_IDLE;
+ }
+ }
+
+ return ret;
+}
+
NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
bool do_negotiation,
const char *export_name,
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v4 2/2] iotests: Add coverage for x-debug-query-nbd-client QMP command
2026-12-03 12:20 [PATCH v4 0/2] nbd: Add an unstable query for the client state Guillaume Vincke
2026-12-03 12:20 ` [PATCH v4 1/2] " Guillaume Vincke
@ 2026-12-03 12:20 ` Guillaume Vincke
2026-12-03 18:42 ` Eric Blake
1 sibling, 1 reply; 6+ messages in thread
From: Guillaume Vincke @ 2026-12-03 12:20 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, Eric Blake, Vladimir Sementsov-Ogievskiy, Kevin Wolf,
Hanna Reitz, Markus Armbruster, Guillaume Vincke
Check that running it on a nonexistent or non-nbd node fails. Also
check each state reached when the server restarts.
Running x-qemu-io on a bare node would drain its temporary
BlockBackend, which blocks until the request completes and hides
connecting-wait. We force the I/O to go through a device to avoid this
issue.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Tested-by: Eric Blake <eblake@redhat.com>
---
tests/qemu-iotests/tests/nbd-client-query | 78 +++++++++++++++++++
tests/qemu-iotests/tests/nbd-client-query.out | 15 ++++
2 files changed, 93 insertions(+)
create mode 100755 tests/qemu-iotests/tests/nbd-client-query
create mode 100644 tests/qemu-iotests/tests/nbd-client-query.out
diff --git a/tests/qemu-iotests/tests/nbd-client-query b/tests/qemu-iotests/tests/nbd-client-query
new file mode 100755
index 0000000000..1cc2be7404
--- /dev/null
+++ b/tests/qemu-iotests/tests/nbd-client-query
@@ -0,0 +1,78 @@
+#!/usr/bin/env python3
+# group: rw auto quick
+#
+# Test the x-debug-query-nbd-client QMP command
+#
+# Copyright (C) 2026 OVHcloud
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import time
+import iotests
+
+
+iotests.script_initialize(supported_fmts=['qcow2'])
+nbd_sock = iotests.file_path('nbd-sock', base_dir=iotests.sock_dir)
+disk = iotests.file_path('disk')
+iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
+node_name = 'nbd0'
+
+
+def wait_for_state(virtm, state, connector_state):
+ with iotests.Timeout(30, f"did not reach {state}/{connector_state}"):
+ while True:
+ resp = virtm.qmp('x-debug-query-nbd-client', node_name=node_name)
+ resp_st = resp['return']['state']
+ resp_conn_st = resp['return']['connector']['state']
+ if resp_st == state and resp_conn_st == connector_state:
+ break
+ time.sleep(0.1)
+ iotests.log(resp)
+
+
+with iotests.VM() as vm:
+ vm.add_device('virtio-scsi,id=scsi0')
+ vm.launch()
+
+ # error when the node does not exist
+ iotests.log(vm.qmp('x-debug-query-nbd-client', node_name=node_name))
+
+ # error when the node is not an nbd client
+ vm.cmd('blockdev-add', driver='null-co', node_name='null0')
+ iotests.log(vm.qmp('x-debug-query-nbd-client', node_name='null0'))
+
+ # start the server
+ with iotests.qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk):
+ # add a nbd node
+ vm.cmd('blockdev-add', {
+ 'driver': 'nbd',
+ 'node-name': 'nbd0',
+ 'server': {'type': 'unix', 'path': nbd_sock},
+ 'reconnect-delay': 2,
+ })
+ vm.cmd('device_add', driver='scsi-hd', drive='nbd0', id='dev0')
+ # the client is connected
+ iotests.log(vm.qmp('x-debug-query-nbd-client', node_name=node_name))
+
+ # server is down. An IO moves it to the connecting-wait state
+ iotests.log(
+ vm.qmp('x-qemu-io', qdev='dev0', command='aio_write -P 0x5a 0 64k'))
+ wait_for_state(vm, 'connecting-wait', 'running')
+
+ # when reconnect delay is reached, we get to connecting-nowait
+ wait_for_state(vm, 'connecting-nowait', 'running')
+
+ # restart the server
+ with iotests.qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk):
+ # the connector gets to the parked state
+ wait_for_state(vm, 'connecting-nowait', 'parked')
+
+ # new request is successful and the client is back in connected
+ iotests.log(
+ vm.qmp('x-qemu-io', qdev='dev0', command='write -P 0x5a 0 64k'))
+ iotests.log(vm.qmp('x-debug-query-nbd-client', node_name=node_name))
+
+ # yanking the NBD node moves the client to the quit state
+ nbd_block_node = {'type': 'block-node', 'node-name': node_name}
+ iotests.log(vm.qmp('yank', instances=[nbd_block_node]))
+ iotests.log(vm.qmp('x-debug-query-nbd-client', node_name=node_name))
diff --git a/tests/qemu-iotests/tests/nbd-client-query.out b/tests/qemu-iotests/tests/nbd-client-query.out
new file mode 100644
index 0000000000..100b66df64
--- /dev/null
+++ b/tests/qemu-iotests/tests/nbd-client-query.out
@@ -0,0 +1,15 @@
+{"error": {"class": "GenericError", "desc": "Node 'nbd0' not found"}}
+{"error": {"class": "GenericError", "desc": "Node 'null0' is not an NBD client"}}
+Start NBD server
+{"return": {"connector": {"state": "idle"}, "state": "connected"}}
+Kill NBD server
+{"return": {}}
+{"return": {"connector": {"state": "running"}, "state": "connecting-wait"}}
+{"return": {"connector": {"state": "running"}, "state": "connecting-nowait"}}
+Start NBD server
+{"return": {"connector": {"state": "parked"}, "state": "connecting-nowait"}}
+{"return": {}}
+{"return": {"connector": {"state": "idle"}, "state": "connected"}}
+{"return": {}}
+{"return": {"connector": {"state": "idle"}, "state": "quit"}}
+Kill NBD server
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread