NBD reconnect
v7 cover NBD reconnect
Hi all!
Here is NBD reconnect. Previously, if connection failed all current and future requests will fail. After the series, nbd-client driver will try to reconnect unlimited times. During first @reconnect-delay seconds of reconnecting all requests will wait for the connection, and if it is established requests will be resent. After @reconnect-delay period all requests will be failed (until successful reconnect).
v7:
almost all: rebased on merged nbd.c and nbd-client.c (including patch subject) 01-04: add Eric's r-b
04: wording
05: new
06: rewrite to remove timer earlier
07: new
08:
- rebase on 05 and 07
- drop "All rights reserved"
- handle drain
- improve handling aio context attach
09: move 249 -> 257
Vladimir Sementsov-Ogievskiy (9):
block/nbd: split connection_co start out of nbd_client_connect block/nbd: use non-blocking io channel for nbd negotiation block/nbd: move from quit to state
block/nbd: add cmdline and qapi parameter reconnect-delay block/nbd: refactor nbd connection parameters
qemu-coroutine-sleep: introduce qemu_co_sleep_wake qemu/units: add SI decimal units
block/nbd: nbd reconnect
iotests: test nbd reconnect
qapi/block-core.json | 11 +- include/block/nbd.h | 3 +- include/qemu/coroutine.h | 17 +- include/qemu/units.h | 7 + block/nbd.c | 531 +++++++++++++++++++++++++--------- block/null.c | 2 +- block/sheepdog.c | 2 +- nbd/client.c | 16 +- qemu-nbd.c | 2 +- tests/test-bdrv-drain.c | 6 +- tests/test-block-iothread.c | 2 +- util/qemu-coroutine-sleep.c | 47 ++- tests/qemu-iotests/257 | 63 ++++ tests/qemu-iotests/257.out | 10 + tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 4 + 16 files changed, 551 insertions(+), 173 deletions(-) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
Signature · 1 line
2.18.0
PATCH v7 1/9 block/nbd: split connection_co start out of nbd_client_connect
nbd_client_connect is going to be used from connection_co, so, let's refactor nbd_client_connect in advance, leaving io channel configuration all in nbd_client_connect.
--- block/nbd.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
block/nbd.c +13 -9
@@ -1232,14 +1232,8 @@ static int nbd_client_connect(BlockDriverState *bs,object_ref (OBJECT (s->ioc)); } -/* -* Now that we're connected, set the socket to be non-blocking and -* kick the reply mechanism. -*/ qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL ); - s->connection_co =qemu_coroutine_create (nbd_connection_entry, s); -bdrv_inc_in_flight (bs); -nbd_client_attach_aio_context (bs,bdrv_get_aio_context (bs)); +qio_channel_attach_aio_context (s->ioc,bdrv_get_aio_context (bs));trace_nbd_client_connect_success (export);
@@ -1270,14 +1264,24 @@ static int nbd_client_init(BlockDriverState *bs,const char *x_dirty_bitmap, Error **errp) { + int ret;BDRVNBDState *s = (BDRVNBDState *)bs->opaque; s->bs = bs;qemu_co_mutex_init (&s->send_mutex);qemu_co_queue_init (&s->free_sema); -return nbd_client_connect (bs, saddr, export, tlscreds, hostname, - x_dirty_bitmap, errp); + ret =nbd_client_connect (bs, saddr, export, tlscreds, hostname, + x_dirty_bitmap, errp); +if (ret <0 ) { +return ret; + } + + s->connection_co =qemu_coroutine_create (nbd_connection_entry, s); +bdrv_inc_in_flight (bs); +aio_co_schedule (bdrv_get_aio_context (bs), s->connection_co); + +return 0 ; }static int nbd_parse_uri (const char *filename,QDict *options)
Signature · 1 line
2.18.0
PATCH v7 2/9 block/nbd: use non-blocking io channel for nbd negotiation
In reply to Vladimir Sementsov-OgievskiyNo reason to use blocking channel for negotiation and we'll benefit in further reconnect feature, as qio_channel reads and writes will do qemu_coroutine_yield while waiting for io completion.
--- include/block/nbd.h | 3 ++- block/nbd.c | 16 +++++++--------- nbd/client.c | 16 +++++++++++----- qemu-nbd.c | 2 +- 4 files changed, 21 insertions(+), 16 deletions(-)
include/block/nbd.h +2 -1
@@ -304,7 +304,8 @@ struct NBDExportInfo { };typedef struct NBDExportInfo NBDExportInfo ; -int nbd_receive_negotiate (QIOChannel *ioc,QCryptoTLSCreds *tlscreds, +int nbd_receive_negotiate (AioContext *aio_context,QIOChannel *ioc, +QCryptoTLSCreds *tlscreds,const char *hostname,QIOChannel **outioc,NBDExportInfo *info,Error **errp);void nbd_free_export_list (NBDExportInfo *info,int count);
block/nbd.c +7 -9
@@ -1176,6 +1176,7 @@ static int nbd_client_connect(BlockDriverState *bs, Error **errp) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque; +AioContext *aio_context =bdrv_get_aio_context (bs);int ret;/*
@@ -1190,15 +1191,16 @@ static int nbd_client_connect(BlockDriverState *bs,/* NBD handshake */ trace_nbd_client_connect (export); -qio_channel_set_blocking (QIO_CHANNEL (sioc), true,NULL ); +qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL ); +qio_channel_attach_aio_context (QIO_CHANNEL (sioc), aio_context); s->info.request_sizes = true; s->info.structured_reply = true; s->info.base_allocation = true; s->info.x_dirty_bitmap =g_strdup (x_dirty_bitmap); s->info.name =g_strdup (export ?:"" ); - ret =nbd_receive_negotiate (QIO_CHANNEL (sioc), tlscreds, hostname, - &s->ioc, &s->info, errp); + ret =nbd_receive_negotiate (aio_context,QIO_CHANNEL (sioc), tlscreds, + hostname, &s->ioc, &s->info, errp);g_free (s->info.x_dirty_bitmap);g_free (s->info.name);if (ret <0 ) {
@@ -1232,18 +1234,14 @@ static int nbd_client_connect(BlockDriverState *bs,object_ref (OBJECT (s->ioc)); } -qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL ); -qio_channel_attach_aio_context (s->ioc,bdrv_get_aio_context (bs)); -trace_nbd_client_connect_success (export);return 0 ; fail:/* -* We have connected, but must fail for other reasons. The -* connection is still blocking; send NBD_CMD_DISC as a courtesy -* to the server. +* We have connected, but must fail for other reasons. +* Send NBD_CMD_DISC as a courtesy to the server. */ {NBDRequest request = { .type =NBD_CMD_DISC };
nbd/client.c +11 -5
@@ -867,7 +867,8 @@ static int nbd_list_meta_contexts(QIOChannel *ioc,* 2: server is newstyle, but lacks structured replies * 3: server is newstyle and set up for structured replies */ -static int nbd_start_negotiate (QIOChannel *ioc,QCryptoTLSCreds *tlscreds, +static int nbd_start_negotiate (AioContext *aio_context,QIOChannel *ioc, +QCryptoTLSCreds *tlscreds,const char *hostname,QIOChannel **outioc,bool structured_reply,bool *zeroes,Error **errp)
@@ -934,6 +935,10 @@ static int nbd_start_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,return -EINVAL ; } ioc = *outioc; +if (aio_context) { +qio_channel_set_blocking (ioc, false,NULL ); +qio_channel_attach_aio_context (ioc, aio_context); + } }else {error_setg (errp,"Server does not support STARTTLS" );return -EINVAL ;
@@ -998,7 +1003,8 @@ static int nbd_negotiate_finish_oldstyle(QIOChannel *ioc, NBDExportInfo *info,* Returns: negative errno: failure talking to server * 0: server is connected */ -int nbd_receive_negotiate (QIOChannel *ioc,QCryptoTLSCreds *tlscreds, +int nbd_receive_negotiate (AioContext *aio_context,QIOChannel *ioc, +QCryptoTLSCreds *tlscreds,const char *hostname,QIOChannel **outioc,NBDExportInfo *info,Error **errp) {
@@ -1009,7 +1015,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,assert (info->name);trace_nbd_receive_negotiate_name (info->name); - result =nbd_start_negotiate (ioc, tlscreds, hostname, outioc, + result =nbd_start_negotiate (aio_context, ioc, tlscreds, hostname, outioc, info->structured_reply, &zeroes, errp); info->structured_reply = false;
@@ -1129,8 +1135,8 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,QIOChannel *sioc =NULL ; *info =NULL ; - result =nbd_start_negotiate (ioc, tlscreds, hostname, &sioc, true,NULL , - errp); + result =nbd_start_negotiate (NULL , ioc, tlscreds, hostname, &sioc, true, +NULL , errp);if (tlscreds && sioc) { ioc = sioc; }
qemu-nbd.c +1 -1
@@ -362,7 +362,7 @@ static void *nbd_client_thread(void *arg) goto out; } - ret =nbd_receive_negotiate (QIO_CHANNEL (sioc), + ret =nbd_receive_negotiate (NULL ,QIO_CHANNEL (sioc),NULL ,NULL ,NULL , &info, &local_error);if (ret <0 ) {if (local_error) {
Signature · 1 line
2.18.0
PATCH v7 3/9 block/nbd: move from quit to state
In reply to Vladimir Sementsov-OgievskiyTo implement reconnect we need several states for the client:
CONNECTED, QUIT and two different CONNECTING states. CONNECTING states will be added in the following patches. This patch implements CONNECTED and QUIT.
QUIT means, that we should close the connection and fail all current and further requests (like old quit = true).
CONNECTED means that connection is ok, we can send requests (like old quit = false).
For receiving loop we use a comparison of the current state with QUIT, because reconnect will be in the same loop, so it should be looping until the end.
Opposite, for requests we use a comparison of the current state with CONNECTED, as we don't want to send requests in future CONNECTING states.
--- block/nbd.c | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-)
block/nbd.c +37 -21
@@ -53,6 +53,11 @@ typedef struct {bool receiving;/* waiting for connection_co? */ } NBDClientRequest; +typedef enum NBDClientState { +NBD_CLIENT_CONNECTED , +NBD_CLIENT_QUIT +}NBDClientState ; + typedef struct BDRVNBDState { QIOChannelSocket *sioc; /* The master data channel */ QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
@@ -62,17 +67,23 @@ typedef struct BDRVNBDState {CoQueue free_sema;Coroutine *connection_co;int in_flight; +NBDClientState state;NBDClientRequest requests[MAX_NBD_REQUESTS ];NBDReply reply;BlockDriverState *bs; -bool quit;/* For nbd_refresh_filename() */ SocketAddress *saddr;char *export, *tlscredsid; } BDRVNBDState; +/* @ret will be used for reconnect in future */ +static void nbd_channel_error (BDRVNBDState *s,int ret) +{ + s->state =NBD_CLIENT_QUIT ; +} +static void nbd_recv_coroutines_wake_all (BDRVNBDState *s) {int i;
@@ -151,7 +162,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque)int ret =0 ;Error *local_err =NULL ; -while (!s->quit) { +while (s->state !=NBD_CLIENT_QUIT ) {/* * The NBD client can only really be considered idle when it has * yielded from qio_channel_readv_all_eof(), waiting for data. This is
@@ -169,6 +180,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque)error_free (local_err); }if (ret <=0 ) { +nbd_channel_error (s, ret ? ret : -EIO );break ; }
@@ -183,6 +195,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque) !s->requests[i].receiving || (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) { +nbd_channel_error (s, -EINVAL );break ; }
@@ -202,7 +215,6 @@ static coroutine_fn void nbd_connection_entry(void *opaque)qemu_coroutine_yield (); } - s->quit = true;nbd_recv_coroutines_wake_all (s);bdrv_dec_in_flight (s->bs);
@@ -215,12 +227,18 @@ static int nbd_co_send_request(BlockDriverState *bs, QEMUIOVector *qiov) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque; -int rc, i; +int rc, i =-1 ;qemu_co_mutex_lock (&s->send_mutex);while (s->in_flight ==MAX_NBD_REQUESTS ) {qemu_co_queue_wait (&s->free_sema, &s->send_mutex); } + +if (s->state !=NBD_CLIENT_CONNECTED ) { + rc = -EIO ; + goto err; + } + s->in_flight++;for (i =0 ; i <MAX_NBD_REQUESTS ; i++) {
@@ -238,16 +256,12 @@ static int nbd_co_send_request(BlockDriverState *bs, request->handle =INDEX_TO_HANDLE (s, i); -if (s->quit) { - rc = -EIO ; - goto err; - }assert (s->ioc);if (qiov) {qio_channel_set_cork (s->ioc, true); rc =nbd_send_request (s->ioc, request); -if (rc >=0 && !s->quit) { +if (rc >=0 && s->state ==NBD_CLIENT_CONNECTED ) {if (qio_channel_writev_all (s->ioc, qiov->iov, qiov->niov,NULL ) <0 ) { rc = -EIO ;
@@ -262,9 +276,11 @@ static int nbd_co_send_request(BlockDriverState *bs, err:if (rc <0 ) { - s->quit = true; - s->requests[i].coroutine =NULL ; - s->in_flight--; +nbd_channel_error (s, rc); +if (i !=-1 ) { + s->requests[i].coroutine =NULL ; + s->in_flight--; + }qemu_co_queue_next (&s->free_sema); }qemu_co_mutex_unlock (&s->send_mutex);
@@ -556,7 +572,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( s->requests[i].receiving = true;qemu_coroutine_yield (); s->requests[i].receiving = false; -if (s->quit) { +if (s->state !=NBD_CLIENT_CONNECTED ) {error_setg (errp,"Connection closed" );return -EIO ; }
@@ -640,7 +656,7 @@ static coroutine_fn int nbd_co_receive_one_chunk( request_ret, qiov, payload, errp);if (ret <0 ) { - s->quit = true; +nbd_channel_error (s, ret); }else {/* For assert at loop start in nbd_connection_entry */ if (reply) {
@@ -710,7 +726,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,NBDReply local_reply;NBDStructuredReplyChunk *chunk;Error *local_err =NULL ; -if (s->quit) { +if (s->state !=NBD_CLIENT_CONNECTED ) {error_setg (&local_err,"Connection closed" );nbd_iter_channel_error (iter, -EIO , &local_err); goto break_loop;
@@ -735,7 +751,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, }/* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ -if (nbd_reply_is_simple (reply) || s->quit) { +if (nbd_reply_is_simple (reply) || s->state !=NBD_CLIENT_CONNECTED ) { goto break_loop; }
@@ -809,14 +825,14 @@ static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, ret =nbd_parse_offset_hole_payload (s, &reply.structured, payload, offset, qiov, &local_err);if (ret <0 ) { - s->quit = true; +nbd_channel_error (s, ret);nbd_iter_channel_error (&iter, ret, &local_err); }break ;default :if (!nbd_reply_type_is_error (chunk->type)) {/* not allowed reply type */ - s->quit = true; +nbd_channel_error (s, -EINVAL );error_setg (&local_err,"Unexpected reply type: %d (%s) for CMD_READ" , chunk->type,nbd_reply_type_lookup (chunk->type));
@@ -854,7 +870,7 @@ static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,switch (chunk->type) {case NBD_REPLY_TYPE_BLOCK_STATUS :if (received) { - s->quit = true; +nbd_channel_error (s, -EINVAL );error_setg (&local_err,"Several BLOCK_STATUS chunks in reply" );nbd_iter_channel_error (&iter, -EINVAL , &local_err); }
@@ -864,13 +880,13 @@ static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s, payload, length, extent, &local_err);if (ret <0 ) { - s->quit = true; +nbd_channel_error (s, ret);nbd_iter_channel_error (&iter, ret, &local_err); }break ;default :if (!nbd_reply_type_is_error (chunk->type)) { - s->quit = true; +nbd_channel_error (s, -EINVAL ); error_setg(&local_err, "Unexpected reply type: %d (%s) " "for CMD_BLOCK_STATUS",
Signature · 1 line
2.18.0
Re: [Qemu-devel] [PATCH v7 3/9] block/nbd: move from quit to state
Quoted 35 lines · Vladimir Sementsov-Ogievskiy
To implement reconnect we need several states for the client:
CONNECTED, QUIT and two different CONNECTING states. CONNECTING states will be added in the following patches. This patch implements CONNECTED and QUIT.QUIT means, that we should close the connection and fail all current and further requests (like old quit = true).
CONNECTED means that connection is ok, we can send requests (like old quit = false).
For receiving loop we use a comparison of the current state with QUIT, because reconnect will be in the same loop, so it should be looping until the end.
Opposite, for requests we use a comparison of the current state with CONNECTED, as we don't want to send requests in future CONNECTING states.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Reviewed-by Eric Blake <[email protected]>--- block/nbd.c | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-)block/nbd.c +1
-1@@ -556,7 +572,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( s->requests[i].receiving = true;qemu_coroutine_yield (); s->requests[i].receiving = false; -if (s->quit) { +if (s->state !=NBD_CLIENT_CONNECTED ) {error_setg (errp,"Connection closed" );return -EIO ; }
block/nbd.c static coroutine_fn int nbd_co_receive_one_chunk( · line 659
Quoted 5 lines
block/nbd.c +1
-1@@ -640,7 +656,7 @@ static coroutine_fn int nbd_co_receive_one_chunk( request_ret, qiov, payload, errp);if (ret <0 ) { - s->quit = true; +nbd_channel_error (s, ret);
Minor merge conflict with changes in the meantime; easy enough to sort out.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH v7 4/9 block/nbd: add cmdline and qapi parameter reconnect-delay
In reply to Vladimir Sementsov-OgievskiyReconnect will be implemented in the following commit, so for now, in semantics below, disconnect itself is a "serious error".
--- qapi/block-core.json | 11 ++++++++++- block/nbd.c | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-)
qapi/block-core.json +10 -1
@@ -3858,13 +3858,22 @@ # traditional "base:allocation" block status (see # NBD_OPT_LIST_META_CONTEXT in the NBD protocol) (since 3.0) # +# @reconnect-delay: On an unexpected disconnect, the nbd client tries to +# connect again until succeeding or encountering a serious +# error. During the first @reconnect-delay seconds, all +# requests are paused and will be rerun on a successful +# reconnect. After that time, any delayed requests and all +# future requests before a successful reconnect will +# immediately fail. Default 0 (Since 4.1) +# # Since: 2.9 ## { 'struct': 'BlockdevOptionsNbd', 'data': { 'server': 'SocketAddress', '*export': 'str', '*tls-creds': 'str', - '*x-dirty-bitmap': 'str' } } + '*x-dirty-bitmap': 'str', + '*reconnect-delay': 'uint32' } } ## # @BlockdevOptionsRaw:
block/nbd.c +15 -1
@@ -1276,6 +1276,7 @@ static int nbd_client_init(BlockDriverState *bs,QCryptoTLSCreds *tlscreds, const char *hostname, const char *x_dirty_bitmap, + uint32_t reconnect_delay, Error **errp) { int ret;
@@ -1601,6 +1602,17 @@ static QemuOptsList nbd_runtime_opts = { .help = "experimental: expose named dirty bitmap in place of " "block status", }, + { + .name = "reconnect-delay", + .type = QEMU_OPT_NUMBER, + .help ="On an unexpected disconnect, the nbd client tries to " +"connect again until succeeding or encountering a serious " +"error. During the first @reconnect-delay seconds, all " +"requests are paused and will be rerun on a successful " +"reconnect. After that time, any delayed requests and all " +"future requests before a successful reconnect will " + "immediately fail. Default 0", + }, { /* end of list */ } }, };
@@ -1652,7 +1664,9 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,/* NBD handshake */ ret =nbd_client_init (bs, s->saddr, s->export, tlscreds, hostname, -qemu_opt_get (opts,"x-dirty-bitmap" ), errp); +qemu_opt_get (opts,"x-dirty-bitmap" ), +qemu_opt_get_number (opts,"reconnect-delay" ,0 ), + errp); error:if (tlscreds) {
Signature · 1 line
2.18.0
Re: [Qemu-devel] [PATCH v7 4/9] block/nbd: add cmdline and qapi parameter reconnect-delay
Quoted 15 lines · Vladimir Sementsov-Ogievskiy
Reconnect will be implemented in the following commit, so for now, in semantics below, disconnect itself is a "serious error".
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Reviewed-by Eric Blake <[email protected]>--- qapi/block-core.json | 11 ++++++++++- block/nbd.c | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-)qapi/block-core.json +0
-0
qapi/block-core.json line 3867
Quoted 10 lines
qapi/block-core.json +7
-0@@ -3858,13 +3858,22 @@ # traditional "base:allocation" block status (see # NBD_OPT_LIST_META_CONTEXT in the NBD protocol) (since 3.0) # +# @reconnect-delay: On an unexpected disconnect, the nbd client tries to +# connect again until succeeding or encountering a serious +# error. During the first @reconnect-delay seconds, all +# requests are paused and will be rerun on a successful +# reconnect. After that time, any delayed requests and all +# future requests before a successful reconnect will +# immediately fail. Default 0 (Since 4.1)
4.2 now; sorry for the delays. I can make that change; I'll definitely stage at least through this patch for my first pull request as soon as 4.2 opens next week; while still looking at the rest of the series to see if it is ready to go as well.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH v7 5/9 block/nbd: refactor nbd connection parameters
In reply to Vladimir Sementsov-OgievskiyWe'll need some connection parameters to be available all the time to implement nbd reconnect. So, let's refactor them: define additional parameters in BDRVNBDState, drop them from function parameters, drop nbd_client_init and separate options parsing instead from nbd_open.
--- block/nbd.c | 125 +++++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 61 deletions(-)
block/nbd.c +64 -61
@@ -73,9 +73,13 @@ typedef struct BDRVNBDState {NBDReply reply;BlockDriverState *bs; -/* For nbd_refresh_filename() */ +/* Connection parameters */ +uint32_t reconnect_delay;SocketAddress *saddr;char *export, *tlscredsid; +QCryptoTLSCreds *tlscreds; +const char *hostname; +char *x_dirty_bitmap; } BDRVNBDState;/* @ret will be used for reconnect in future */
@@ -1183,13 +1187,7 @@ static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,return sioc; } -static int nbd_client_connect (BlockDriverState *bs, -SocketAddress *saddr, -const char *export, -QCryptoTLSCreds *tlscreds, -const char *hostname, -const char *x_dirty_bitmap, -Error **errp) +static int nbd_client_connect (BlockDriverState *bs,Error **errp) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque;AioContext *aio_context =bdrv_get_aio_context (bs);
@@ -1199,33 +1197,33 @@ static int nbd_client_connect(BlockDriverState *bs,* establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. */ -QIOChannelSocket *sioc =nbd_establish_connection (saddr, errp); +QIOChannelSocket *sioc =nbd_establish_connection (s->saddr, errp);if (!sioc) {return -ECONNREFUSED ; }/* NBD handshake */ -trace_nbd_client_connect (export); +trace_nbd_client_connect (s->export);qio_channel_set_blocking (QIO_CHANNEL (sioc), false,NULL );qio_channel_attach_aio_context (QIO_CHANNEL (sioc), aio_context); s->info.request_sizes = true; s->info.structured_reply = true; s->info.base_allocation = true; - s->info.x_dirty_bitmap =g_strdup (x_dirty_bitmap); - s->info.name =g_strdup (export ?:"" ); - ret =nbd_receive_negotiate (aio_context,QIO_CHANNEL (sioc), tlscreds, - hostname, &s->ioc, &s->info, errp); + s->info.x_dirty_bitmap =g_strdup (s->x_dirty_bitmap); + s->info.name =g_strdup (s->export ?:"" ); + ret =nbd_receive_negotiate (aio_context,QIO_CHANNEL (sioc), s->tlscreds, + s->hostname, &s->ioc, &s->info, errp);g_free (s->info.x_dirty_bitmap);g_free (s->info.name);if (ret <0 ) {object_unref (OBJECT (sioc));return ret; } -if (x_dirty_bitmap && !s->info.base_allocation) { +if (s->x_dirty_bitmap && !s->info.base_allocation) {error_setg (errp,"requested x-dirty-bitmap %s not found" , - x_dirty_bitmap); + s->x_dirty_bitmap); ret = -EINVAL ; goto fail; }
@@ -1250,7 +1248,7 @@ static int nbd_client_connect(BlockDriverState *bs,object_ref (OBJECT (s->ioc)); } -trace_nbd_client_connect_success (export); +trace_nbd_client_connect_success (s->export);return 0 ;
@@ -1270,34 +1268,9 @@ static int nbd_client_connect(BlockDriverState *bs, } } -static int nbd_client_init (BlockDriverState *bs, -SocketAddress *saddr, -const char *export, -QCryptoTLSCreds *tlscreds, -const char *hostname, -const char *x_dirty_bitmap, -uint32_t reconnect_delay, -Error **errp) -{ -int ret; -BDRVNBDState *s = (BDRVNBDState *)bs->opaque; - - s->bs = bs; -qemu_co_mutex_init (&s->send_mutex); -qemu_co_queue_init (&s->free_sema); - - ret =nbd_client_connect (bs, saddr, export, tlscreds, hostname, - x_dirty_bitmap, errp); -if (ret <0 ) { -return ret; - } - - s->connection_co =qemu_coroutine_create (nbd_connection_entry, s); -bdrv_inc_in_flight (bs); -aio_co_schedule (bdrv_get_aio_context (bs), s->connection_co); - -return 0 ; -} +/* +* Parse nbd_open options +*/ static int nbd_parse_uri (const char *filename,QDict *options) {
@@ -1617,14 +1590,12 @@ static QemuOptsList nbd_runtime_opts = { }, }; -static int nbd_open (BlockDriverState *bs,QDict *options,int flags, -Error **errp) +static int nbd_process_options (BlockDriverState *bs,QDict *options, +Error **errp) {BDRVNBDState *s = bs->opaque; -QemuOpts *opts =NULL ; +QemuOpts *opts;Error *local_err =NULL ; -QCryptoTLSCreds *tlscreds =NULL ; -const char *hostname =NULL ;int ret = -EINVAL ; opts =qemu_opts_create (&nbd_runtime_opts,NULL ,0 , &error_abort);
@@ -1649,8 +1620,8 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags, s->tlscredsid =g_strdup (qemu_opt_get (opts,"tls-creds" ));if (s->tlscredsid) { - tlscreds =nbd_get_tls_creds (s->tlscredsid, errp); -if (!tlscreds) { + s->tlscreds =nbd_get_tls_creds (s->tlscredsid, errp); +if (!s->tlscreds) { goto error; }
@@ -1659,20 +1630,19 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,error_setg (errp,"TLS only supported over IP sockets" ); goto error; } - hostname = s->saddr->u.inet.host; + s->hostname = s->saddr->u.inet.host; } -/* NBD handshake */ - ret =nbd_client_init (bs, s->saddr, s->export, tlscreds, hostname, -qemu_opt_get (opts,"x-dirty-bitmap" ), -qemu_opt_get_number (opts,"reconnect-delay" ,0 ), - errp); + s->x_dirty_bitmap =g_strdup (qemu_opt_get (opts,"x-dirty-bitmap" )); + s->reconnect_delay =qemu_opt_get_number (opts,"reconnect-delay" ,0 ); + + ret =0 ; error: -if (tlscreds) { -object_unref (OBJECT (tlscreds)); - }if (ret <0 ) { +if (s->tlscreds) { +object_unref (OBJECT (s->tlscreds)); + }qapi_free_SocketAddress (s->saddr);g_free (s->export);g_free (s->tlscredsid);
@@ -1681,6 +1651,35 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,return ret; } +static int nbd_open (BlockDriverState *bs,QDict *options,int flags, +Error **errp) +{ +int ret; +BDRVNBDState *s = (BDRVNBDState *)bs->opaque; + + ret =nbd_process_options (bs, options, errp); +if (ret <0 ) { +return ret; + } + + s->bs = bs; +qemu_co_mutex_init (&s->send_mutex); +qemu_co_queue_init (&s->free_sema); + + ret =nbd_client_connect (bs, errp); +if (ret <0 ) { +return ret; + } +/* successfully connected */ + s->state =NBD_CLIENT_CONNECTED ; + + s->connection_co =qemu_coroutine_create (nbd_connection_entry, s); +bdrv_inc_in_flight (bs); +aio_co_schedule (bdrv_get_aio_context (bs), s->connection_co); + +return 0 ; +} +static int nbd_co_flush (BlockDriverState *bs) {return nbd_client_co_flush (bs);
@@ -1726,9 +1725,13 @@ static void nbd_close(BlockDriverState *bs)nbd_client_close (bs); +if (s->tlscreds) { +object_unref (OBJECT (s->tlscreds)); + }qapi_free_SocketAddress (s->saddr);g_free (s->export);g_free (s->tlscredsid); +g_free (s->x_dirty_bitmap); }static int64_t nbd_getlength (BlockDriverState *bs)
Signature · 1 line
2.18.0
Re: [Qemu-devel] [PATCH v7 5/9] block/nbd: refactor nbd connection parameters
Quoted 27 lines · Vladimir Sementsov-Ogievskiy
We'll need some connection parameters to be available all the time to implement nbd reconnect. So, let's refactor them: define additional parameters in BDRVNBDState, drop them from function parameters, drop
nbd_client_initand separate options parsing instead fromnbd_open.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/nbd.c | 125 +++++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 61 deletions(-)block/nbd.c +4
-6@@ -1659,20 +1630,19 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,error_setg (errp,"TLS only supported over IP sockets" ); goto error; } - hostname = s->saddr->u.inet.host; + s->hostname = s->saddr->u.inet.host; } -/* NBD handshake */ - ret =nbd_client_init (bs, s->saddr, s->export, tlscreds, hostname, -qemu_opt_get (opts,"x-dirty-bitmap" ), -qemu_opt_get_number (opts,"reconnect-delay" ,0 ), - errp); + s->x_dirty_bitmap =g_strdup (qemu_opt_get (opts,"x-dirty-bitmap" )); + s->reconnect_delay =qemu_opt_get_number (opts,"reconnect-delay" ,0 ); +
block/nbd.c static int nbd_open(BlockDriverState *bs, QDict *options, int flags, · line 1645
Quoted 10 lines
block/nbd.c +4
-3+ ret =0 ; error: -if (tlscreds) { -object_unref (OBJECT (tlscreds)); - }if (ret <0 ) { +if (s->tlscreds) { +object_unref (OBJECT (s->tlscreds)); + }
Pre-existing, but object_unref(NULL) is safe, making this a useless 'if'.
block/nbd.c static void nbd_close(BlockDriverState *bs) · line 1730
Quoted 7 lines
block/nbd.c +3
-0@@ -1726,9 +1725,13 @@ static void nbd_close(BlockDriverState *bs)nbd_client_close (bs); +if (s->tlscreds) { +object_unref (OBJECT (s->tlscreds)); + }
and copied here.
block/nbd.c static void nbd_close(BlockDriverState *bs) · line 1734
Quoted 4 lines
block/nbd.c +1
-0qapi_free_SocketAddress (s->saddr);g_free (s->export);g_free (s->tlscredsid); +g_free (s->x_dirty_bitmap);
Do we need to duplicate s->x_dirty_bitmap with s->info.x_dirty_bitmap, or make the latter be const char * and reuse the pointer from the former rather than strdup'ing it? (I don't think it affects the refactoring done in this patch, but is possibly worth a separate cleanup patch).
I can fix up the two useless 'if's.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH v7 6/9 qemu-coroutine-sleep: introduce qemu_co_sleep_wake
In reply to Vladimir Sementsov-OgievskiyIntroduce a function to gracefully wake a coroutine sleeping in qemu_co_sleep_ns().
--- include/qemu/coroutine.h | 17 ++++++++++++-- block/null.c | 2 +- block/sheepdog.c | 2 +- tests/test-bdrv-drain.c | 6 ++--- tests/test-block-iothread.c | 2 +- util/qemu-coroutine-sleep.c | 47 +++++++++++++++++++++++++++---------- 6 files changed, 55 insertions(+), 21 deletions(-)
include/qemu/coroutine.h +15 -2
@@ -274,9 +274,22 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock);void qemu_co_rwlock_unlock (CoRwlock *lock);/** -* Yield the coroutine for a given duration +* Yield the coroutine for a given duration. During this yield @sleep_state (if +* not NULL) is set to opaque pointer, which may be used for +* qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when timer +* shoots. Don't save obtained value to other variables and don't call +* qemu_co_sleep_wake from another aio context. */ -void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns); +void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns, +void **sleep_state); + +/** +* Wake a coroutine if it is sleeping by qemu_co_sleep_ns. Timer will be +* deleted. @sleep_state must be the variable which address was given to +* qemu_co_sleep_ns() and should be checked to be non-NULL before calling +* qemu_co_sleep_wake(). +*/ +void qemu_co_sleep_wake (void *sleep_state);/** * Yield until a file descriptor becomes readable
block/null.c +1 -1
@@ -109,7 +109,7 @@ static coroutine_fn int null_co_common(BlockDriverState *bs)BDRVNullState *s = bs->opaque;if (s->latency_ns) { -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME , s->latency_ns); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME , s->latency_ns,NULL ); }return 0 ; }
block/sheepdog.c +1 -1
@@ -742,7 +742,7 @@ static coroutine_fn void reconnect_to_sdog(void *opaque)if (s->fd <0 ) {trace_sheepdog_reconnect_to_sdog ();error_report_err (local_err); -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000000ULL ); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000000ULL ,NULL ); } };
tests/test-bdrv-drain.c +3 -3
@@ -42,7 +42,7 @@ static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)BDRVTestState *s = bs->opaque; s->drain_count++;if (s->sleep_in_drain_begin) { -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ,NULL ); } }
@@ -73,7 +73,7 @@ static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,* it to complete. We need to sleep a while as bdrv_drain_invoke() comes * first and polls its result, too, but it shouldn't accidentally complete * this request yet. */ -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ,NULL );if (s->bh_indirection_ctx) { aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
@@ -818,7 +818,7 @@ static int coroutine_fn test_job_run(Job *job, Error **errp)/* Avoid job_sleep_ns() because it marks the job as !busy. We want to * emulate some actual activity (probably some I/O) here so that drain * has to wait for this activity to stop. */ -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000 ); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000 ,NULL );job_pause_point (&s->common.job); }
tests/test-block-iothread.c +1 -1
@@ -380,7 +380,7 @@ static int coroutine_fn test_job_run(Job *job, Error **errp)* emulate some actual activity (probably some I/O) here so that the * drain involved in AioContext switches has to wait for this activity * to stop. */ -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000 ); +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000 ,NULL );job_pause_point (&s->common.job); }
util/qemu-coroutine-sleep.c +34 -13
65 lines not on this page · raw message
Signature · 1 line
2.18.0
PATCH v7 7/9 qemu/units: add SI decimal units
In reply to Vladimir Sementsov-Ogievskiy--- include/qemu/units.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/qemu/units.h +7 -0
@@ -17,4 +17,11 @@#define PiB (INT64_C(1) << 50)#define EiB (INT64_C(1) << 60) +#define SI_k 1000LL +#define SI_M 1000000LL +#define SI_G 1000000000LL +#define SI_T 1000000000000LL +#define SI_P 1000000000000000LL +#define SI_E 1000000000000000000LL +#endif
Signature · 1 line
2.18.0
Re: [Qemu-devel] [PATCH v7 7/9] qemu/units: add SI decimal units
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/qemu/units.h | 7 +++++++ 1 file changed, 7 insertions(+)include/qemu/units.h +0
-0
include/qemu/units.h line 25
Quoted 9 lines
include/qemu/units.h +6
-0@@ -17,4 +17,11 @@#define PiB (INT64_C(1) << 50)#define EiB (INT64_C(1) << 60) +#define SI_k 1000LL +#define SI_M 1000000LL +#define SI_G 1000000000LL +#define SI_T 1000000000000LL +#define SI_P 1000000000000000LL +#define SI_E 1000000000000000000LL
Looks correct; and it's the sort of thing that if we do once here, we don't have to repeat elsewhere. But bike-shedding a bit, would it be any easier to read as:
#define SI_k 1000LL
#define SI_M (1000LL * 1000)
#define SI_G (1000LL * 1000 * 1000)
...
or even:
#define SI_k 1000LL
#define SI_M (SI_k * 1000)
#define SI_G (SI_M * 1000)
...
Also, would it be worth swapping out existing constants in the code base that should instead be using these macros, so that they actually have a use and so that we can see whether using them adds legibility?
For example, block/nvme.c, block/qapi.c, block/sheepdog.c, blockdev.c, util/async.c, util/oslib-win32.c, util/qemu-thread-posix.c, util/qemu-timer.c all seem to be dealing with conversions between seconds and subdivisions thereof, where constants 1000000 or larger are in use and could be rewritten with these.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v7 7/9] qemu/units: add SI decimal units
Quoted 9 lines · Eric Blake
Also, would it be worth swapping out existing constants in the code base that should instead be using these macros, so that they actually have a use and so that we can see whether using them adds legibility?
For example,
block/nvme.c,block/qapi.c,block/sheepdog.c,blockdev.c,util/async.c,util/oslib-win32.c,util/qemu-thread-posix.c,util/qemu-timer.call seem to be dealing with conversions between seconds and subdivisions thereof, where constants 1000000 or larger are in use and could be rewritten with these.
I'm not sure that it would be more readable to replace 1000000000LL with SI_G -- I would tend to assume the latter would be 2^30. Using "1000LL * 1000 * 1000" inline at the point of use, or better still abstracting any particular use into something more semantically meaningful as we already do with NANOSECONDS_PER_SECOND, would be my personal preference.
thanks
-- PMM
PATCH v7 8/9 block/nbd: nbd reconnect
In reply to Vladimir Sementsov-OgievskiyImplement reconnect. To achieve this:
1. add new modes:
connecting-wait: means, that reconnecting is in progress, and there
were small number of reconnect attempts, so all requests are waiting for the connection.
connecting-nowait: reconnecting is in progress, there were a lot of
attempts of reconnect, all requests will return errors.
two old modes are used too:
connected: normal state
quit: exiting after fatal error or on close
Possible transitions are:
* -> quit
connecting-* -> connected
connecting-wait -> connecting-nowait (transition is done after
reconnect-delay seconds in connecting-wait mode)
connected -> connecting-wait
2. Implement reconnect in connection_co. So, in connecting-* mode, connection_co, tries to reconnect unlimited times.
3. Retry nbd queries on channel error, if we are in connecting-wait state.
--- block/nbd.c | 336 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 272 insertions(+), 64 deletions(-)
block/nbd.c +272 -64
@@ -1,6 +1,7 @@/* * QEMU Block driver for NBD * +* Copyright (c) 2019 Virtuozzo International GmbH. * Copyright (C) 2016 Red Hat, Inc. * Copyright (C) 2008 Bull S.A.S. * Author: Laurent Vivier <[email protected]>
@@ -33,6 +34,7 @@#include "qemu/uri.h" #include "qemu/option.h" #include "qemu/cutils.h" +#include "qemu/units.h" #include "qapi/qapi-visit-sockets.h" #include "qapi/qmp/qstring.h"
@@ -54,6 +56,8 @@ typedef struct { } NBDClientRequest;typedef enum NBDClientState { +NBD_CLIENT_CONNECTING_WAIT , +NBD_CLIENT_CONNECTING_NOWAIT ,NBD_CLIENT_CONNECTED ,NBD_CLIENT_QUIT }NBDClientState ;
@@ -66,8 +70,14 @@ typedef struct BDRVNBDState {CoMutex send_mutex;CoQueue free_sema;Coroutine *connection_co; +void *connection_co_sleep_ns_state; +bool drained; +bool wait_drained_end;int in_flight;NBDClientState state; +int connect_status; +Error *connect_err; +bool wait_in_flight;NBDClientRequest requests[MAX_NBD_REQUESTS ];NBDReply reply;
@@ -82,10 +92,21 @@ typedef struct BDRVNBDState {char *x_dirty_bitmap; } BDRVNBDState; -/* @ret will be used for reconnect in future */ +static int nbd_client_connect (BlockDriverState *bs,Error **errp); +static void nbd_channel_error (BDRVNBDState *s,int ret) { - s->state =NBD_CLIENT_QUIT ; +if (ret == -EIO ) { +if (s->state ==NBD_CLIENT_CONNECTED ) { + s->state = s->reconnect_delay ?NBD_CLIENT_CONNECTING_WAIT : +NBD_CLIENT_CONNECTING_NOWAIT ; + } + }else { +if (s->state ==NBD_CLIENT_CONNECTED ) { +qio_channel_shutdown (s->ioc,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); + } + s->state =NBD_CLIENT_QUIT ; + } }static void nbd_recv_coroutines_wake_all (BDRVNBDState *s)
@@ -128,7 +149,13 @@ static void nbd_client_attach_aio_context(BlockDriverState *bs, {BDRVNBDState *s = (BDRVNBDState *)bs->opaque; -qio_channel_attach_aio_context (QIO_CHANNEL (s->ioc), new_context); +/* +* s->connection_co is either yielded from nbd_receive_reply or from +* nbd_reconnect_loop() +*/ +if (s->state ==NBD_CLIENT_CONNECTED ) { +qio_channel_attach_aio_context (QIO_CHANNEL (s->ioc), new_context); + }bdrv_inc_in_flight (bs);
@@ -139,29 +166,157 @@ static void nbd_client_attach_aio_context(BlockDriverState *bs,aio_wait_bh_oneshot (new_context, nbd_client_attach_aio_context_bh, bs); } +static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) +{ +BDRVNBDState *s = (BDRVNBDState *)bs->opaque; -static void nbd_teardown_connection (BlockDriverState *bs) + s->drained = true; +if (s->connection_co_sleep_ns_state) { +qemu_co_sleep_wake (s->connection_co_sleep_ns_state); + } +} + +static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs) {BDRVNBDState *s = (BDRVNBDState *)bs->opaque; -assert (s->ioc); + s->drained = false; +if (s->wait_drained_end) { + s->wait_drained_end = false; +aio_co_wake (s->connection_co); + } +} + -/* finish any pending coroutines */ -qio_channel_shutdown (s->ioc, -QIO_CHANNEL_SHUTDOWN_BOTH , -NULL ); +static void nbd_teardown_connection (BlockDriverState *bs) +{ +BDRVNBDState *s = (BDRVNBDState *)bs->opaque; + +if (s->state ==NBD_CLIENT_CONNECTED ) { +/* finish any pending coroutines */ +assert (s->ioc); +qio_channel_shutdown (s->ioc,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); + } + s->state =NBD_CLIENT_QUIT ; +if (s->connection_co) { +if (s->connection_co_sleep_ns_state) { +qemu_co_sleep_wake (s->connection_co_sleep_ns_state); + } + }BDRV_POLL_WHILE (bs, s->connection_co); +} -nbd_client_detach_aio_context (bs); -object_unref (OBJECT (s->sioc)); - s->sioc =NULL ; -object_unref (OBJECT (s->ioc)); - s->ioc =NULL ; +static bool nbd_client_connecting (BDRVNBDState *s) +{ +return s->state ==NBD_CLIENT_CONNECTING_WAIT || + s->state ==NBD_CLIENT_CONNECTING_NOWAIT ; +} + +static bool nbd_client_connecting_wait (BDRVNBDState *s) +{ +return s->state ==NBD_CLIENT_CONNECTING_WAIT ; +} + +static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) +{ +Error *local_err =NULL ; + +if (!nbd_client_connecting (s)) { +return ; + } +assert (nbd_client_connecting (s)); + +/* Wait for completion of all in-flight requests */ + +qemu_co_mutex_lock (&s->send_mutex); + +while (s->in_flight >0 ) { +qemu_co_mutex_unlock (&s->send_mutex); +nbd_recv_coroutines_wake_all (s); + s->wait_in_flight = true; +qemu_coroutine_yield (); + s->wait_in_flight = false; +qemu_co_mutex_lock (&s->send_mutex); + } + +qemu_co_mutex_unlock (&s->send_mutex); + +if (!nbd_client_connecting (s)) { +return ; + } + +/* +* Now we are sure that nobody is accessing the channel, and no one will +* try until we set the state to CONNECTED. +*/ + +/* Finalize previous connection if any */ +if (s->ioc) { +nbd_client_detach_aio_context (s->bs); +object_unref (OBJECT (s->sioc)); + s->sioc =NULL ; +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; + } + + s->connect_status =nbd_client_connect (s->bs, &local_err); +error_free (s->connect_err); + s->connect_err =NULL ; +error_propagate (&s->connect_err, local_err); + local_err =NULL ; + +if (s->connect_status <0 ) { +/* failed attempt */ +return ; + } + +/* successfully connected */ + s->state =NBD_CLIENT_CONNECTED ; +qemu_co_queue_restart_all (&s->free_sema); +} + +static coroutine_fn void nbd_reconnect_loop(BDRVNBDState *s) +{ +uint64_t start_time_ns =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ); +uint64_t delay_ns = s->reconnect_delay *SI_G ; +uint64_t timeout =SI_G ;/* 1 sec */ +uint64_t max_timeout =16 *SI_G ; + +nbd_reconnect_attempt (s); + +while (nbd_client_connecting (s)) { +if (s->state ==NBD_CLIENT_CONNECTING_WAIT && +qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) - start_time_ns > delay_ns) + { + s->state =NBD_CLIENT_CONNECTING_NOWAIT ; +qemu_co_queue_restart_all (&s->free_sema); + } + +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME , timeout, + &s->connection_co_sleep_ns_state); +if (s->drained) { +bdrv_dec_in_flight (s->bs); + s->wait_drained_end = true; +while (s->drained) { +/* +* We may be entered once from nbd_client_attach_aio_context_bh +* and then from nbd_client_co_drain_end. So here is a loop. +*/ +qemu_coroutine_yield (); + } +bdrv_inc_in_flight (s->bs); + } +if (timeout < max_timeout) { + timeout *=2 ; + } + +nbd_reconnect_attempt (s); + } } static coroutine_fn void nbd_connection_entry(void *opaque) { -BDRVNBDState *s = opaque; +BDRVNBDState *s = (BDRVNBDState *)opaque;uint64_t i;int ret =0 ;Error *local_err =NULL ;
@@ -176,16 +331,26 @@ static coroutine_fn void nbd_connection_entry(void *opaque)* Therefore we keep an additional in_flight reference all the time and * only drop it temporarily here. */ + +if (nbd_client_connecting (s)) { +nbd_reconnect_loop (s); + } + +if (s->state !=NBD_CLIENT_CONNECTED ) { +continue ; + } +assert (s->reply.handle ==0 ); ret =nbd_receive_reply (s->bs, s->ioc, &s->reply, &local_err);if (local_err) {trace_nbd_read_reply_entry_fail (ret,error_get_pretty (local_err));error_free (local_err); + local_err =NULL ; }if (ret <=0 ) {nbd_channel_error (s, ret ? ret : -EIO ); -break ; +continue ; }/*
@@ -200,7 +365,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque) (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) {nbd_channel_error (s, -EINVAL ); -break ; +continue ; }/*
@@ -219,10 +384,19 @@ static coroutine_fn void nbd_connection_entry(void *opaque)qemu_coroutine_yield (); } +qemu_co_queue_restart_all (&s->free_sema);nbd_recv_coroutines_wake_all (s);bdrv_dec_in_flight (s->bs); s->connection_co =NULL ; +if (s->ioc) { +nbd_client_detach_aio_context (s->bs); +object_unref (OBJECT (s->sioc)); + s->sioc =NULL ; +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; + } +aio_wait_kick (); }
@@ -234,7 +408,7 @@ static int nbd_co_send_request(BlockDriverState *bs,int rc, i =-1 ;qemu_co_mutex_lock (&s->send_mutex); -while (s->in_flight ==MAX_NBD_REQUESTS ) { +while (s->in_flight ==MAX_NBD_REQUESTS ||nbd_client_connecting_wait (s)) {qemu_co_queue_wait (&s->free_sema, &s->send_mutex); }
@@ -285,7 +459,11 @@ err: s->requests[i].coroutine =NULL ; s->in_flight--; } -qemu_co_queue_next (&s->free_sema); +if (s->in_flight ==0 && s->wait_in_flight) { +aio_co_wake (s->connection_co); + }else { +qemu_co_queue_next (&s->free_sema); + } }qemu_co_mutex_unlock (&s->send_mutex);return rc;
@@ -666,10 +844,15 @@ static coroutine_fn int nbd_co_receive_one_chunk(if (reply) { *reply = s->reply; } - s->reply.handle =0 ; } + s->reply.handle =0 ; -if (s->connection_co) { +if (s->connection_co && !s->wait_in_flight) { +/* +* We must check s->wait_in_flight, because we may entered by +* nbd_recv_coroutines_wake_all(), in this case we should not +* wake connection_co here, it will woken by last request. +*/ aio_co_wake (s->connection_co); }
@@ -781,7 +964,11 @@ break_loop:qemu_co_mutex_lock (&s->send_mutex); s->in_flight--; -qemu_co_queue_next (&s->free_sema); +if (s->in_flight ==0 && s->wait_in_flight) { +aio_co_wake (s->connection_co); + }else { +qemu_co_queue_next (&s->free_sema); + }qemu_co_mutex_unlock (&s->send_mutex);return false;
@@ -927,20 +1114,26 @@ static int nbd_co_request(BlockDriverState *bs, NBDRequest *request, }else {assert (request->type !=NBD_CMD_WRITE ); } - ret =nbd_co_send_request (bs, request, write_qiov); -if (ret <0 ) { -return ret; - } - ret =nbd_co_receive_return_code (s, request->handle, - &request_ret, &local_err); -if (local_err) { -trace_nbd_co_request_fail (request->from, request->len, request->handle, - request->flags, request->type, -nbd_cmd_lookup (request->type), - ret,error_get_pretty (local_err)); -error_free (local_err); - } +do { + ret =nbd_co_send_request (bs, request, write_qiov); +if (ret <0 ) { +continue ; + } + + ret =nbd_co_receive_return_code (s, request->handle, + &request_ret, &local_err); +if (local_err) { +trace_nbd_co_request_fail (request->from, request->len, + request->handle, request->flags, + request->type, +nbd_cmd_lookup (request->type), + ret,error_get_pretty (local_err)); +error_free (local_err); + local_err =NULL ; + } + }while (ret <0 &&nbd_client_connecting_wait (s)); +return ret ? ret : request_ret; }
@@ -981,20 +1174,24 @@ static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, request.len -= slop; } - ret =nbd_co_send_request (bs, &request,NULL ); -if (ret <0 ) { -return ret; - } +do { + ret =nbd_co_send_request (bs, &request,NULL ); +if (ret <0 ) { +continue ; + } + + ret =nbd_co_receive_cmdread_reply (s, request.handle, offset, qiov, + &request_ret, &local_err); +if (local_err) { +trace_nbd_co_request_fail (request.from, request.len, request.handle, + request.flags, request.type, +nbd_cmd_lookup (request.type), + ret,error_get_pretty (local_err)); +error_free (local_err); + local_err =NULL ; + } + }while (ret <0 &&nbd_client_connecting_wait (s)); - ret =nbd_co_receive_cmdread_reply (s, request.handle, offset, qiov, - &request_ret, &local_err); -if (local_err) { -trace_nbd_co_request_fail (request.from, request.len, request.handle, - request.flags, request.type, -nbd_cmd_lookup (request.type), - ret,error_get_pretty (local_err)); -error_free (local_err); - }return ret ? ret : request_ret; }
@@ -1127,20 +1324,25 @@ static int coroutine_fn nbd_client_co_block_status(if (s->info.min_block) {assert (QEMU_IS_ALIGNED (request.len, s->info.min_block)); } - ret =nbd_co_send_request (bs, &request,NULL ); -if (ret <0 ) { -return ret; - } +do { + ret =nbd_co_send_request (bs, &request,NULL ); +if (ret <0 ) { +continue ; + } + + ret =nbd_co_receive_blockstatus_reply (s, request.handle, bytes, + &extent, &request_ret, + &local_err); +if (local_err) { +trace_nbd_co_request_fail (request.from, request.len, request.handle, + request.flags, request.type, +nbd_cmd_lookup (request.type), + ret,error_get_pretty (local_err)); +error_free (local_err); + local_err =NULL ; + } + }while (ret <0 &&nbd_client_connecting_wait (s)); - ret =nbd_co_receive_blockstatus_reply (s, request.handle, bytes, - &extent, &request_ret, &local_err); -if (local_err) { -trace_nbd_co_request_fail (request.from, request.len, request.handle, - request.flags, request.type, -nbd_cmd_lookup (request.type), - ret,error_get_pretty (local_err)); -error_free (local_err); - }if (ret <0 || request_ret <0 ) {return ret ? ret : request_ret; }
@@ -1159,9 +1361,9 @@ static void nbd_client_close(BlockDriverState *bs)BDRVNBDState *s = (BDRVNBDState *)bs->opaque;NBDRequest request = { .type =NBD_CMD_DISC }; -assert (s->ioc); - -nbd_send_request (s->ioc, &request); +if (s->ioc) { +nbd_send_request (s->ioc, &request); + }nbd_teardown_connection (bs); }
@@ -1808,6 +2010,8 @@ static BlockDriver bdrv_nbd = { .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_client_detach_aio_context, .bdrv_attach_aio_context = nbd_client_attach_aio_context, + .bdrv_co_drain_begin = nbd_client_co_drain_begin, + .bdrv_co_drain_end = nbd_client_co_drain_end, .bdrv_refresh_filename = nbd_refresh_filename, .bdrv_co_block_status = nbd_client_co_block_status, .bdrv_dirname = nbd_dirname,
@@ -1830,6 +2034,8 @@ static BlockDriver bdrv_nbd_tcp = { .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_client_detach_aio_context, .bdrv_attach_aio_context = nbd_client_attach_aio_context, + .bdrv_co_drain_begin = nbd_client_co_drain_begin, + .bdrv_co_drain_end = nbd_client_co_drain_end, .bdrv_refresh_filename = nbd_refresh_filename, .bdrv_co_block_status = nbd_client_co_block_status, .bdrv_dirname = nbd_dirname,
@@ -1852,6 +2058,8 @@ static BlockDriver bdrv_nbd_unix = { .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_client_detach_aio_context, .bdrv_attach_aio_context = nbd_client_attach_aio_context, + .bdrv_co_drain_begin = nbd_client_co_drain_begin, + .bdrv_co_drain_end = nbd_client_co_drain_end, .bdrv_refresh_filename = nbd_refresh_filename, .bdrv_co_block_status = nbd_client_co_block_status, .bdrv_dirname = nbd_dirname,
Signature · 1 line
2.18.0
PATCH v7 9/9 iotests: test nbd reconnect
In reply to Vladimir Sementsov-OgievskiyAdd test, which starts backup to nbd target and restarts nbd server during backup.
--- tests/qemu-iotests/257 | 63 +++++++++++++++++++++++++++++++++++ tests/qemu-iotests/257.out | 10 ++++++ tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 4 +++ 4 files changed, 78 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out
tests/qemu-iotests/257 new file +63 -0
@@ -0,0 +1,63 @@ +#!/usr/bin/env python +# +# Test nbd reconnect +# +# Copyright (c) 2019 Virtuozzo International GmbH. All rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import time + +import iotests +from iotests import qemu_img_create, file_path, qemu_nbd_popen, log + +disk_a, disk_b, nbd_sock = file_path('disk_a', 'disk_b', 'nbd-sock') + +qemu_img_create('-f', iotests.imgfmt, disk_a, '5M') +qemu_img_create('-f', iotests.imgfmt, disk_b, '5M') +srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) +time.sleep(1) + +vm = iotests.VM().add_drive(disk_a) +vm.launch() +vm.hmp_qemu_io('drive0', 'write 0 5M') + +vm.qmp_log('blockdev-add', filters=[iotests.filter_qmp_testfiles], + **{'node_name': 'backup0', + 'driver': 'raw', + 'file': {'driver': 'nbd', + 'server': {'type': 'unix', 'path': nbd_sock}, + 'reconnect-delay': 10}}) +vm.qmp_log('blockdev-backup', device='drive0', sync='full', target='backup0') + +time.sleep(1) +log('Kill NBD server') +srv.kill() + +jobs = vm.qmp('query-block-jobs')['return'] +if jobs and jobs[0]['offset'] < jobs[0]['len']: + log('Backup job is still in progress') + +time.sleep(1) + +log('Start NBD server') +srv = qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) + +e = vm.event_wait('BLOCK_JOB_COMPLETED') +log('Backup completed: {}'.format(e['data']['offset'])) + +vm.qmp_log('blockdev-del', node_name='backup0') +srv.kill() +vm.shutdown()
tests/qemu-iotests/257.out new file +10 -0
@@ -0,0 +1,10 @@ +{"execute": "blockdev-add", "arguments": {"driver": "raw", "file": {"driver": "nbd", "reconnect-delay": 10, "server": {"path": "TEST_DIR/PID-nbd-sock", "type": "unix"}}, "node-name": "backup0"}} +{"return": {}} +{"execute": "blockdev-backup", "arguments": {"device": "drive0", "sync": "full", "target": "backup0"}} +{"return": {}} +Kill NBD server +Backup job is still in progress +Start NBD server +Backup completed: 5242880 +{"execute": "blockdev-del", "arguments": {"node-name": "backup0"}} +{"return": {}}
tests/qemu-iotests/group +1 -0
@@ -269,3 +269,4 @@ 254 rw auto backing quick 255 rw auto quick 256 rw auto quick +257 rw auto quick
tests/qemu-iotests/iotests.py +4 -0
@@ -226,6 +226,10 @@ def qemu_nbd_early_pipe(*args): else: return exitcode, subp.communicate()[0] +def qemu_nbd_popen(*args): + '''Run qemu-nbd in daemon mode and return the parent's exit code''' + return subprocess.Popen(qemu_nbd_args + ['--persistent'] + list(args)) + def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt): '''Return True if two image files are identical''' return qemu_img('compare', '-f', fmt1,
Signature · 1 line
2.18.0
ping
Quoted 54 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here is NBD reconnect. Previously, if connection failed all current and future requests will fail. After the series, nbd-client driver will try to reconnect unlimited times. During first @reconnect-delay seconds of reconnecting all requests will wait for the connection, and if it is established requests will be resent. After @reconnect-delay period all requests will be failed (until successful reconnect).v7:
almost all: rebased on mergednbd.candnbd-client.c(including patch subject) 01-04: add Eric's r-b
04: wording
05: new
06: rewrite to remove timer earlier
07: new
08:
- rebase on 05 and 07
- drop "All rights reserved"
- handle drain
- improve handling aio context attach
09: move 249 -> 257Vladimir Sementsov-Ogievskiy (9):
block/nbd: splitconnection_costart out ofnbd_client_connectblock/nbd: use non-blocking io channel for nbd negotiation block/nbd: move from quit to state
block/nbd: add cmdline and qapi parameter reconnect-delay block/nbd: refactor nbd connection parameters
qemu-coroutine-sleep: introduceqemu_co_sleep_wakeqemu/units: add SI decimal units
block/nbd: nbd reconnect
iotests: test nbd reconnectqapi/block-core.json | 11 +- include/block/nbd.h | 3 +- include/qemu/coroutine.h | 17 +- include/qemu/units.h | 7 + block/nbd.c | 531 +++++++++++++++++++++++++--------- block/null.c | 2 +- block/sheepdog.c | 2 +- nbd/client.c | 16 +- qemu-nbd.c | 2 +- tests/test-bdrv-drain.c | 6 +- tests/test-block-iothread.c | 2 +- util/qemu-coroutine-sleep.c | 47 ++- tests/qemu-iotests/257 | 63 ++++ tests/qemu-iotests/257.out | 10 + tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 4 + 16 files changed, 551 insertions(+), 173 deletions(-)create mode 100755 tests/qemu-iotests/257
create mode 100644 tests/qemu-iotests/257.out
Signature · 2 lines
Best regards, Vladimir
Should I resend with 07 dropped?
Quoted 59 lines · Vladimir Sementsov-Ogievskiy
ping
Quoted 54 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here is NBD reconnect. Previously, if connection failed all current and future requests will fail. After the series, nbd-client driver will try to reconnect unlimited times. During first @reconnect-delay seconds of reconnecting all requests will wait for the connection, and if it is established requests will be resent. After @reconnect-delay period all requests will be failed (until successful reconnect).v7:
almost all: rebased on mergednbd.candnbd-client.c(including patch subject) 01-04: add Eric's r-b
04: wording
05: new
06: rewrite to remove timer earlier
07: new
08:
- rebase on 05 and 07
- drop "All rights reserved"
- handle drain
- improve handling aio context attach
09: move 249 -> 257Vladimir Sementsov-Ogievskiy (9):
block/nbd: splitconnection_costart out ofnbd_client_connectblock/nbd: use non-blocking io channel for nbd negotiation block/nbd: move from quit to state
block/nbd: add cmdline and qapi parameter reconnect-delay block/nbd: refactor nbd connection parameters
qemu-coroutine-sleep: introduceqemu_co_sleep_wakeqemu/units: add SI decimal units
block/nbd: nbd reconnect
iotests: test nbd reconnectqapi/block-core.json | 11 +-
include/block/nbd.h | 3 +- include/qemu/coroutine.h | 17 +- include/qemu/units.h | 7 +
block/nbd.c | 531 +++++++++++++++++++++++++--------- block/null.c | 2 +-
block/sheepdog.c | 2 +-
nbd/client.c | 16 +-
qemu-nbd.c | 2 +- tests/test-bdrv-drain.c | 6 +- tests/test-block-iothread.c | 2 +- util/qemu-coroutine-sleep.c | 47 ++- tests/qemu-iotests/257 | 63 ++++ tests/qemu-iotests/257.out | 10 + tests/qemu-iotests/group | 1 +tests/qemu-iotests/iotests.py| 4 +16 files changed, 551 insertions(+), 173 deletions(-)create mode 100755 tests/qemu-iotests/257
create mode 100644 tests/qemu-iotests/257.out
Signature · 2 lines
Best regards, Vladimir
Quoted 2 lines · Vladimir Sementsov-Ogievskiy
Should I resend with 07 dropped?
At this point, the earlier patches in the series are now in-tree, and the later patches need rebasing again...
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
v7:
almost all: rebased on mergednbd.candnbd-client.c(including patch subject) 01-04: add Eric's r-b
04: wording
05: new
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
06: rewrite to remove timer earlier
07: new
08:
- rebase on 05 and 07
- drop "All rights reserved"
- handle drain
- improve handling aio context attach
09: move 249 -> 257
257 snuck into the tree for a different test, so you'll get to move it again.
But yes, dropping patch 7 (with controversial SI unit addition) in favor of more discernable constants locally (such as NANOSECONDS_PER_SECOND) as part of the rebase is a good idea.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org