From nobody Wed Nov 12 05:46:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1568740534; cv=none; d=zoho.com; s=zohoarc; b=NRD3mrsNZ/fgQE+gBuzfRf69Y7BmYLrTLiFs+ov3Q6t1xrU4gcZlfTKfFQc+pzyckhc363tIdWa/knvK5MfJxgSFFpoBLG1jRPyeWrHARpRKko3HQy+kKsT241jPKGb7jwmu+N+igexwG6z4U2z3AcCdEm9agCEaWSKK4qYlnWc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568740534; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ada2eqKtQFZSHG8cX2Jc2wDMCFa0HX3V+y9csGH0fXE=; b=nPl0g0X7itdggqBpwGNd0qfJHBfnxO+uuQlUbDC0bL7hNjJDILKaVNt0fqRi/M1AumXc1TPAwQfxasipUmsIB8/z8JIpWy6wAScMAhwJtZ2PzfOZ4iyoVj8qYWLZCjDrCBF1tTDS2yNxrXThagMvUAhbGRGoo9R96R6iRFzfv+Y= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568740534614534.6721729655435; Tue, 17 Sep 2019 10:15:34 -0700 (PDT) Received: from localhost ([::1]:48776 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH4f-0008Hx-6x for importer@patchew.org; Tue, 17 Sep 2019 13:15:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50647) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH2g-0006lt-Tq for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iAH2f-00033L-Ir for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:30 -0400 Received: from relay.sw.ru ([185.231.240.75]:55314) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iAH2c-000303-N7; Tue, 17 Sep 2019 13:13:26 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iAH2Z-0003rq-NJ; Tue, 17 Sep 2019 20:13:23 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Tue, 17 Sep 2019 20:13:20 +0300 Message-Id: <20190917171322.4127-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190917171322.4127-1-vsementsov@virtuozzo.com> References: <20190917171322.4127-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v9 1/3] qemu-coroutine-sleep: introduce qemu_co_sleep_wake X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Introduce a function to gracefully wake a coroutine sleeping in qemu_co_sleep_ns(). Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Kevin Wolf Reviewed-by: Eric Blake --- include/qemu/coroutine.h | 23 +++++++++++++++-- util/qemu-coroutine-sleep.c | 51 +++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 9801e7f5a4..8d55663062 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -273,10 +273,29 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock); */ void qemu_co_rwlock_unlock(CoRwlock *lock); =20 +typedef struct QemuCoSleepState QemuCoSleepState; + +/** + * Yield the coroutine for a given duration. During this yield, @sleep_sta= te + * (if not NULL) is set to an opaque pointer, which may be used for + * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when = the + * timer fires. Don't save the obtained value to other variables and don't= call + * qemu_co_sleep_wake from another aio context. + */ +void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, + QemuCoSleepState **sleep_state= ); +static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64= _t ns) +{ + qemu_co_sleep_ns_wakeable(type, ns, NULL); +} + /** - * Yield the coroutine for a given duration + * Wake a coroutine if it is sleeping in qemu_co_sleep_ns. The timer will = be + * deleted. @sleep_state must be the variable whose address was given to + * qemu_co_sleep_ns() and should be checked to be non-NULL before calling + * qemu_co_sleep_wake(). */ -void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns); +void qemu_co_sleep_wake(QemuCoSleepState *sleep_state); =20 /** * Yield until a file descriptor becomes readable diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 4bfdd30cbf..ae91b92b6e 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -17,31 +17,56 @@ #include "qemu/timer.h" #include "block/aio.h" =20 -static void co_sleep_cb(void *opaque) -{ - Coroutine *co =3D opaque; +static const char *qemu_co_sleep_ns__scheduled =3D "qemu_co_sleep_ns"; + +struct QemuCoSleepState { + Coroutine *co; + QEMUTimer *ts; + QemuCoSleepState **user_state_pointer; +}; =20 +void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) +{ /* Write of schedule protected by barrier write in aio_co_schedule */ - atomic_set(&co->scheduled, NULL); - aio_co_wake(co); + const char *scheduled =3D atomic_cmpxchg(&sleep_state->co->scheduled, + qemu_co_sleep_ns__scheduled, NU= LL); + + assert(scheduled =3D=3D qemu_co_sleep_ns__scheduled); + if (sleep_state->user_state_pointer) { + *sleep_state->user_state_pointer =3D NULL; + } + timer_del(sleep_state->ts); + aio_co_wake(sleep_state->co); } =20 -void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) +static void co_sleep_cb(void *opaque) +{ + qemu_co_sleep_wake(opaque); +} + +void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, + QemuCoSleepState **sleep_state) { AioContext *ctx =3D qemu_get_current_aio_context(); - QEMUTimer *ts; - Coroutine *co =3D qemu_coroutine_self(); + QemuCoSleepState state =3D { + .co =3D qemu_coroutine_self(), + .ts =3D aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state), + .user_state_pointer =3D sleep_state, + }; =20 - const char *scheduled =3D atomic_cmpxchg(&co->scheduled, NULL, __func_= _); + const char *scheduled =3D atomic_cmpxchg(&state.co->scheduled, NULL, + qemu_co_sleep_ns__scheduled); if (scheduled) { fprintf(stderr, "%s: Co-routine was already scheduled in '%s'\n", __func__, scheduled); abort(); } - ts =3D aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, co); - timer_mod(ts, qemu_clock_get_ns(type) + ns); + + if (sleep_state) { + *sleep_state =3D &state; + } + timer_mod(state.ts, qemu_clock_get_ns(type) + ns); qemu_coroutine_yield(); - timer_del(ts); - timer_free(ts); + timer_free(state.ts); } --=20 2.21.0 From nobody Wed Nov 12 05:46:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1568740834; cv=none; d=zoho.com; s=zohoarc; b=Zj40UdnMJjasa4okQ1szlA1vmnqNSTqqgKFmzE7j57OgdsUCErdGth1yI2FgRRMbH8mDQ0SbJU0oHUYmrzXa0PPuZNDaQHuYqFN8Rbjlnaqd70Eh2faOPap2zH+/K4FGPLTd7nb8oBFE+BUheQDIXn4pnXJwifbN40o4jlhK5qs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568740834; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=DaZCIEqPqvvkquCNLiiWVrfv9y0ML/R43aQrzZqTVtk=; b=jA99DuLTaiEU2omHDAYOn9309wQbuokd4IWjqqTHbFZqRBfw90wzgEJqJmaYLe+fdvF8lZ8C8GhvlhC7Iq2rsiSSaiavzI7RN/ybI0zRNsSl1dq9+h+qi+stp788UGLj0Mwh0PWOGKSpC6kiOFNsBZ7vnP3uHqrzRIrquUSwWGM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568740834193727.9433423189362; Tue, 17 Sep 2019 10:20:34 -0700 (PDT) Received: from localhost ([::1]:48822 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH9V-00049e-0M for importer@patchew.org; Tue, 17 Sep 2019 13:20:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50685) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH2k-0006sr-KF for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iAH2i-00036O-4m for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:34 -0400 Received: from relay.sw.ru ([185.231.240.75]:55312) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iAH2c-000302-ND; Tue, 17 Sep 2019 13:13:27 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iAH2Z-0003rq-RC; Tue, 17 Sep 2019 20:13:23 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Tue, 17 Sep 2019 20:13:21 +0300 Message-Id: <20190917171322.4127-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190917171322.4127-1-vsementsov@virtuozzo.com> References: <20190917171322.4127-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v9 2/3] block/nbd: nbd reconnect X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Implement 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. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/nbd.c | 332 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 269 insertions(+), 63 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 813c40d8f0..bec01d85c7 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -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 @@ -55,6 +56,8 @@ typedef struct { } NBDClientRequest; =20 typedef enum NBDClientState { + NBD_CLIENT_CONNECTING_WAIT, + NBD_CLIENT_CONNECTING_NOWAIT, NBD_CLIENT_CONNECTED, NBD_CLIENT_QUIT } NBDClientState; @@ -67,8 +70,14 @@ typedef struct BDRVNBDState { CoMutex send_mutex; CoQueue free_sema; Coroutine *connection_co; + QemuCoSleepState *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; =20 NBDClientRequest requests[MAX_NBD_REQUESTS]; NBDReply reply; @@ -83,10 +92,21 @@ typedef struct BDRVNBDState { char *x_dirty_bitmap; } BDRVNBDState; =20 -/* @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 =3D NBD_CLIENT_QUIT; + if (ret =3D=3D -EIO) { + if (s->state =3D=3D NBD_CLIENT_CONNECTED) { + s->state =3D s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : + NBD_CLIENT_CONNECTING_NOWAIT; + } + } else { + if (s->state =3D=3D NBD_CLIENT_CONNECTED) { + qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); + } + s->state =3D NBD_CLIENT_QUIT; + } } =20 static void nbd_recv_coroutines_wake_all(BDRVNBDState *s) @@ -129,7 +149,13 @@ static void nbd_client_attach_aio_context(BlockDriverS= tate *bs, { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; =20 - 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 =3D=3D NBD_CLIENT_CONNECTED) { + qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); + } =20 bdrv_inc_in_flight(bs); =20 @@ -140,24 +166,151 @@ static void nbd_client_attach_aio_context(BlockDrive= rState *bs, aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs); } =20 +static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) +{ + BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; =20 -static void nbd_teardown_connection(BlockDriverState *bs) + s->drained =3D 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 =3D (BDRVNBDState *)bs->opaque; =20 - assert(s->ioc); + s->drained =3D false; + if (s->wait_drained_end) { + s->wait_drained_end =3D false; + aio_co_wake(s->connection_co); + } +} + =20 - /* finish any pending coroutines */ - qio_channel_shutdown(s->ioc, - QIO_CHANNEL_SHUTDOWN_BOTH, - NULL); +static void nbd_teardown_connection(BlockDriverState *bs) +{ + BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + + if (s->state =3D=3D NBD_CLIENT_CONNECTED) { + /* finish any pending coroutines */ + assert(s->ioc); + qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); + } + s->state =3D 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); +} =20 - nbd_client_detach_aio_context(bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; - object_unref(OBJECT(s->ioc)); - s->ioc =3D NULL; +static bool nbd_client_connecting(BDRVNBDState *s) +{ + return s->state =3D=3D NBD_CLIENT_CONNECTING_WAIT || + s->state =3D=3D NBD_CLIENT_CONNECTING_NOWAIT; +} + +static bool nbd_client_connecting_wait(BDRVNBDState *s) +{ + return s->state =3D=3D NBD_CLIENT_CONNECTING_WAIT; +} + +static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) +{ + Error *local_err =3D NULL; + + if (!nbd_client_connecting(s)) { + return; + } + + /* 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 =3D true; + qemu_coroutine_yield(); + s->wait_in_flight =3D 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 wi= ll + * 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 =3D NULL; + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + } + + s->connect_status =3D nbd_client_connect(s->bs, &local_err); + error_free(s->connect_err); + s->connect_err =3D NULL; + error_propagate(&s->connect_err, local_err); + local_err =3D NULL; + + if (s->connect_status < 0) { + /* failed attempt */ + return; + } + + /* successfully connected */ + s->state =3D 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 =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + uint64_t delay_ns =3D s->reconnect_delay * NANOSECONDS_PER_SECOND; + uint64_t timeout =3D 1 * NANOSECONDS_PER_SECOND; + uint64_t max_timeout =3D 16 * NANOSECONDS_PER_SECOND; + + nbd_reconnect_attempt(s); + + while (nbd_client_connecting(s)) { + if (s->state =3D=3D NBD_CLIENT_CONNECTING_WAIT && + qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time_ns > delay= _ns) + { + s->state =3D NBD_CLIENT_CONNECTING_NOWAIT; + qemu_co_queue_restart_all(&s->free_sema); + } + + qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout, + &s->connection_co_sleep_ns_state); + if (s->drained) { + bdrv_dec_in_flight(s->bs); + s->wait_drained_end =3D true; + while (s->drained) { + /* + * We may be entered once from nbd_client_attach_aio_conte= xt_bh + * and then from nbd_client_co_drain_end. So here is a loo= p. + */ + qemu_coroutine_yield(); + } + bdrv_inc_in_flight(s->bs); + } + if (timeout < max_timeout) { + timeout *=3D 2; + } + + nbd_reconnect_attempt(s); + } } =20 static coroutine_fn void nbd_connection_entry(void *opaque) @@ -177,16 +330,26 @@ static coroutine_fn void nbd_connection_entry(void *o= paque) * Therefore we keep an additional in_flight reference all the tim= e and * only drop it temporarily here. */ + + if (nbd_client_connecting(s)) { + nbd_reconnect_loop(s); + } + + if (s->state !=3D NBD_CLIENT_CONNECTED) { + continue; + } + assert(s->reply.handle =3D=3D 0); ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err); =20 if (local_err) { trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_er= r)); error_free(local_err); + local_err =3D NULL; } if (ret <=3D 0) { nbd_channel_error(s, ret ? ret : -EIO); - break; + continue; } =20 /* @@ -201,7 +364,7 @@ static coroutine_fn void nbd_connection_entry(void *opa= que) (nbd_reply_is_structured(&s->reply) && !s->info.structured_rep= ly)) { nbd_channel_error(s, -EINVAL); - break; + continue; } =20 /* @@ -220,10 +383,19 @@ static coroutine_fn void nbd_connection_entry(void *o= paque) qemu_coroutine_yield(); } =20 + qemu_co_queue_restart_all(&s->free_sema); nbd_recv_coroutines_wake_all(s); bdrv_dec_in_flight(s->bs); =20 s->connection_co =3D NULL; + if (s->ioc) { + nbd_client_detach_aio_context(s->bs); + object_unref(OBJECT(s->sioc)); + s->sioc =3D NULL; + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + } + aio_wait_kick(); } =20 @@ -235,7 +407,7 @@ static int nbd_co_send_request(BlockDriverState *bs, int rc, i =3D -1; =20 qemu_co_mutex_lock(&s->send_mutex); - while (s->in_flight =3D=3D MAX_NBD_REQUESTS) { + while (s->in_flight =3D=3D MAX_NBD_REQUESTS || nbd_client_connecting_w= ait(s)) { qemu_co_queue_wait(&s->free_sema, &s->send_mutex); } =20 @@ -286,7 +458,11 @@ err: s->requests[i].coroutine =3D NULL; s->in_flight--; } - qemu_co_queue_next(&s->free_sema); + if (s->in_flight =3D=3D 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 +842,15 @@ static coroutine_fn int nbd_co_receive_one_chunk( } else { /* For assert at loop start in nbd_connection_entry */ *reply =3D s->reply; - s->reply.handle =3D 0; } + s->reply.handle =3D 0; =20 - 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); } =20 @@ -781,7 +962,11 @@ break_loop: =20 qemu_co_mutex_lock(&s->send_mutex); s->in_flight--; - qemu_co_queue_next(&s->free_sema); + if (s->in_flight =3D=3D 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); =20 return false; @@ -927,20 +1112,26 @@ static int nbd_co_request(BlockDriverState *bs, NBDR= equest *request, } else { assert(request->type !=3D NBD_CMD_WRITE); } - ret =3D nbd_co_send_request(bs, request, write_qiov); - if (ret < 0) { - return ret; - } =20 - ret =3D 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->ha= ndle, - request->flags, request->type, - nbd_cmd_lookup(request->type), - ret, error_get_pretty(local_err)); - error_free(local_err); - } + do { + ret =3D nbd_co_send_request(bs, request, write_qiov); + if (ret < 0) { + continue; + } + + ret =3D 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 =3D NULL; + } + } while (ret < 0 && nbd_client_connecting_wait(s)); + return ret ? ret : request_ret; } =20 @@ -981,20 +1172,24 @@ static int nbd_client_co_preadv(BlockDriverState *bs= , uint64_t offset, request.len -=3D slop; } =20 - ret =3D nbd_co_send_request(bs, &request, NULL); - if (ret < 0) { - return ret; - } + do { + ret =3D nbd_co_send_request(bs, &request, NULL); + if (ret < 0) { + continue; + } + + ret =3D nbd_co_receive_cmdread_reply(s, request.handle, offset, qi= ov, + &request_ret, &local_err); + if (local_err) { + trace_nbd_co_request_fail(request.from, request.len, request.h= andle, + request.flags, request.type, + nbd_cmd_lookup(request.type), + ret, error_get_pretty(local_err)); + error_free(local_err); + local_err =3D NULL; + } + } while (ret < 0 && nbd_client_connecting_wait(s)); =20 - ret =3D 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.handl= e, - request.flags, request.type, - nbd_cmd_lookup(request.type), - ret, error_get_pretty(local_err)); - error_free(local_err); - } return ret ? ret : request_ret; } =20 @@ -1131,20 +1326,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 =3D nbd_co_send_request(bs, &request, NULL); - if (ret < 0) { - return ret; - } + do { + ret =3D nbd_co_send_request(bs, &request, NULL); + if (ret < 0) { + continue; + } + + ret =3D 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.h= andle, + request.flags, request.type, + nbd_cmd_lookup(request.type), + ret, error_get_pretty(local_err)); + error_free(local_err); + local_err =3D NULL; + } + } while (ret < 0 && nbd_client_connecting_wait(s)); =20 - ret =3D nbd_co_receive_blockstatus_reply(s, request.handle, bytes, - &extent, &request_ret, &local_e= rr); - if (local_err) { - trace_nbd_co_request_fail(request.from, request.len, request.handl= e, - 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; } @@ -1163,9 +1363,9 @@ static void nbd_client_close(BlockDriverState *bs) BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; NBDRequest request =3D { .type =3D NBD_CMD_DISC }; =20 - assert(s->ioc); - - nbd_send_request(s->ioc, &request); + if (s->ioc) { + nbd_send_request(s->ioc, &request); + } =20 nbd_teardown_connection(bs); } @@ -1808,6 +2008,8 @@ static BlockDriver bdrv_nbd =3D { .bdrv_getlength =3D nbd_getlength, .bdrv_detach_aio_context =3D nbd_client_detach_aio_context, .bdrv_attach_aio_context =3D nbd_client_attach_aio_context, + .bdrv_co_drain_begin =3D nbd_client_co_drain_begin, + .bdrv_co_drain_end =3D nbd_client_co_drain_end, .bdrv_refresh_filename =3D nbd_refresh_filename, .bdrv_co_block_status =3D nbd_client_co_block_status, .bdrv_dirname =3D nbd_dirname, @@ -1830,6 +2032,8 @@ static BlockDriver bdrv_nbd_tcp =3D { .bdrv_getlength =3D nbd_getlength, .bdrv_detach_aio_context =3D nbd_client_detach_aio_context, .bdrv_attach_aio_context =3D nbd_client_attach_aio_context, + .bdrv_co_drain_begin =3D nbd_client_co_drain_begin, + .bdrv_co_drain_end =3D nbd_client_co_drain_end, .bdrv_refresh_filename =3D nbd_refresh_filename, .bdrv_co_block_status =3D nbd_client_co_block_status, .bdrv_dirname =3D nbd_dirname, @@ -1852,6 +2056,8 @@ static BlockDriver bdrv_nbd_unix =3D { .bdrv_getlength =3D nbd_getlength, .bdrv_detach_aio_context =3D nbd_client_detach_aio_context, .bdrv_attach_aio_context =3D nbd_client_attach_aio_context, + .bdrv_co_drain_begin =3D nbd_client_co_drain_begin, + .bdrv_co_drain_end =3D nbd_client_co_drain_end, .bdrv_refresh_filename =3D nbd_refresh_filename, .bdrv_co_block_status =3D nbd_client_co_block_status, .bdrv_dirname =3D nbd_dirname, --=20 2.21.0 From nobody Wed Nov 12 05:46:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1568740736; cv=none; d=zoho.com; s=zohoarc; b=TDV6SYIms2HeIgDhV5eH4ilHrwe3vSQ7ufHhjGoguZIGIig/kS2GdbjROa82TIbJHZagroyBYFg9BqOIYmMoSu5EK3IsD1XimaXZbj8+R+5QiOA7tNH/clwk3fB1TGVxohI05p9nx9zrAigFKQ3Z2vmKlID/QUyvy3EBvXpQ1Mw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568740736; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=UKLnQn5AtV5HcGRFeFR8B6lAFNBYxGBGbvVkt0+yrpI=; b=YCBznmufTZSgzuXaCQHlBjmpq06kP++I2sVI+wbnNKXk45WA/tRju/Y6vMZLI/AH0xTEy3OZ56TR3cnIU5DObhozSgoh+gu0B9TbcVC+nCE08UZPC0lUxIVgc8FqEW0Ghbf7q1P0lGBiSyMJCSmx3LXFaEcB6Ob7rpZtBiGD0CU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156874073640944.18458832135855; Tue, 17 Sep 2019 10:18:56 -0700 (PDT) Received: from localhost ([::1]:48800 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH7p-0002Fx-5O for importer@patchew.org; Tue, 17 Sep 2019 13:18:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50654) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAH2h-0006ma-Fw for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iAH2f-00033j-VT for qemu-devel@nongnu.org; Tue, 17 Sep 2019 13:13:31 -0400 Received: from relay.sw.ru ([185.231.240.75]:55316) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iAH2c-000306-Mz; Tue, 17 Sep 2019 13:13:26 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iAH2Z-0003rq-UA; Tue, 17 Sep 2019 20:13:24 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Tue, 17 Sep 2019 20:13:22 +0300 Message-Id: <20190917171322.4127-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190917171322.4127-1-vsementsov@virtuozzo.com> References: <20190917171322.4127-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v9 3/3] iotests: test nbd reconnect X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Add test, which starts backup to nbd target and restarts nbd server during backup. Signed-off-by: Vladimir Sementsov-Ogievskiy Tested-by: Eric Blake --- tests/qemu-iotests/264 | 65 +++++++++++++++++++++++++++++++++++ tests/qemu-iotests/264.out | 12 +++++++ tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 4 +++ 4 files changed, 82 insertions(+) create mode 100755 tests/qemu-iotests/264 create mode 100644 tests/qemu-iotests/264.out diff --git a/tests/qemu-iotests/264 b/tests/qemu-iotests/264 new file mode 100755 index 0000000000..e70f91c5ca --- /dev/null +++ b/tests/qemu-iotests/264 @@ -0,0 +1,65 @@ +#!/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 . +# + +import time + +import iotests +from iotests import qemu_img_create, file_path, qemu_nbd_popen, log + +disk_a, disk_b, nbd_sock =3D 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 =3D qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) +time.sleep(1) + +vm =3D iotests.VM().add_drive(disk_a) +vm.launch() +vm.hmp_qemu_io('drive0', 'write 0 5M') + +vm.qmp_log('blockdev-add', filters=3D[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=3D'drive0', sync=3D'full', target=3D'= backup0', + speed=3D(1 * 1024 * 1024)) + +time.sleep(1) +log('Kill NBD server') +srv.kill() + +jobs =3D 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 =3D qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) + +vm.qmp_log('block-job-set-speed', device=3D'drive0', speed=3D0) +e =3D vm.event_wait('BLOCK_JOB_COMPLETED') +log('Backup completed: {}'.format(e['data']['offset'])) + +vm.qmp_log('blockdev-del', node_name=3D'backup0') +srv.kill() +vm.shutdown() diff --git a/tests/qemu-iotests/264.out b/tests/qemu-iotests/264.out new file mode 100644 index 0000000000..4a2f4aa509 --- /dev/null +++ b/tests/qemu-iotests/264.out @@ -0,0 +1,12 @@ +{"execute": "blockdev-add", "arguments": {"driver": "raw", "file": {"drive= r": "nbd", "reconnect-delay": 10, "server": {"path": "TEST_DIR/PID-nbd-sock= ", "type": "unix"}}, "node-name": "backup0"}} +{"return": {}} +{"execute": "blockdev-backup", "arguments": {"device": "drive0", "speed": = 1048576, "sync": "full", "target": "backup0"}} +{"return": {}} +Kill NBD server +Backup job is still in progress +Start NBD server +{"execute": "block-job-set-speed", "arguments": {"device": "drive0", "spee= d": 0}} +{"return": {}} +Backup completed: 5242880 +{"execute": "blockdev-del", "arguments": {"node-name": "backup0"}} +{"return": {}} diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 5d3da937e4..4f6dd6f153 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -275,5 +275,6 @@ 258 rw quick 262 rw quick migration 263 rw quick +264 rw quick 265 rw auto quick 266 rw quick diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index b26271187c..a9c496dd7e 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -229,6 +229,10 @@ def qemu_nbd_early_pipe(*args): else: return exitcode, subp.communicate()[0] =20 +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=3Dimgfmt, fmt2=3Dimgfmt): '''Return True if two image files are identical''' return qemu_img('compare', '-f', fmt1, --=20 2.21.0 From nobody Wed Nov 12 05:46:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1569317356; cv=none; d=zoho.com; s=zohoarc; b=WaOx8zegO9HBDebDe0xaNh/Kk6sfaX0IqIhs3p77oFMcltfbkVPg03qVYAlG7BIV65SbAU+5CyGNFgJH3gALWF0HcBXkcEbpAZdZRMcU1shxSTx/BJh3hs+Cgs1166Y9BzzdeDsIZoGwFcLkqDSYYMs6YTeWDNioyVVw7AkGKy8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569317356; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=Gdwqx75nSZIpJi6F9aZ3ooethmntRAXbj1rfqqUEGkI=; b=Q445W6EFbFlKtO7OWDY28mUYhHr572wpHeJ/9E5KX2n9aL9WsE8STACuKrp2a/zxxMu66TK6zBA+5qzM/3rm1FmH2pbMjlYPfM/ZBsKMz9tkYs4sgI0gvw/+oltzpXzlFtiKH90EgVbajkYIYM7ngTG9j6aJoT9N1AkId2lpT4k= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1569317356608655.9938129224032; Tue, 24 Sep 2019 02:29:16 -0700 (PDT) Received: from localhost ([::1]:43104 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iCh85-00083f-Po for importer@patchew.org; Tue, 24 Sep 2019 05:29:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54639) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iCh79-0007YF-LD for qemu-devel@nongnu.org; Tue, 24 Sep 2019 05:28:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iCh77-0005pL-Uv for qemu-devel@nongnu.org; Tue, 24 Sep 2019 05:28:07 -0400 Received: from relay.sw.ru ([185.231.240.75]:43772) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iCh73-0005mv-VR; Tue, 24 Sep 2019 05:28:02 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iCh70-0005dC-JI; Tue, 24 Sep 2019 12:27:58 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v9 4/3] iotests: test nbd reconnect Date: Tue, 24 Sep 2019 12:27:58 +0300 Message-Id: <20190924092758.7937-1-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190917171322.4127-1-vsementsov@virtuozzo.com> References: <20190917171322.4127-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Add test, which starts backup to nbd target and restarts nbd server during backup. Signed-off-by: Vladimir Sementsov-Ogievskiy --- Updated test: avoid race conditions + some improvements. - introduce qemu_io_silent_check and use it to wait for NBD server start - use size variable instead of '5M' in all places - use smart waiting loop instead of sleep, to wait for job progress - do srv.wait() after srv.kill(), to be sure that server is stopped - drop throttling earlier Now, test lasts 3 seconds for me. If it is wrong for you, be free to drop 'quick' tag. tests/qemu-iotests/264 | 95 +++++++++++++++++++++++++++++++++++ tests/qemu-iotests/264.out | 13 +++++ tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 11 ++++ 4 files changed, 120 insertions(+) create mode 100755 tests/qemu-iotests/264 create mode 100644 tests/qemu-iotests/264.out diff --git a/tests/qemu-iotests/264 b/tests/qemu-iotests/264 new file mode 100755 index 0000000000..c8cd97ae2b --- /dev/null +++ b/tests/qemu-iotests/264 @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# +# Test nbd reconnect +# +# Copyright (c) 2019 Virtuozzo International GmbH. +# +# 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 . +# + +import time + +import iotests +from iotests import qemu_img_create, qemu_io_silent_check, file_path, \ + qemu_nbd_popen, log + +disk_a, disk_b, nbd_sock =3D file_path('disk_a', 'disk_b', 'nbd-sock') +nbd_uri =3D 'nbd+unix:///?socket=3D' + nbd_sock +size =3D 5 * 1024 * 1024 +wait_limit =3D 3 +wait_step =3D 0.2 + +qemu_img_create('-f', iotests.imgfmt, disk_a, str(size)) +qemu_img_create('-f', iotests.imgfmt, disk_b, str(size)) +srv =3D qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) + +# Wait for NBD server availability +t =3D 0 +ok =3D False +while t < wait_limit: + ok =3D qemu_io_silent_check('-f', 'raw', '-c', 'read 0 512', nbd_uri) + if ok: + break + time.sleep(wait_step) + t +=3D wait_step + +assert ok + +vm =3D iotests.VM().add_drive(disk_a) +vm.launch() +vm.hmp_qemu_io('drive0', 'write 0 {}'.format(size)) + +vm.qmp_log('blockdev-add', filters=3D[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=3D'drive0', sync=3D'full', target=3D'= backup0', + speed=3D(1 * 1024 * 1024)) + +# Wait for some progress +t =3D 0 +while t < wait_limit: + jobs =3D vm.qmp('query-block-jobs')['return'] + if jobs and jobs[0]['offset'] > 0: + break + time.sleep(wait_step) + t +=3D wait_step + +if jobs and jobs[0]['offset'] > 0: + log('Backup job is started') + +log('Kill NBD server') +srv.kill() +srv.wait() + +jobs =3D vm.qmp('query-block-jobs')['return'] +if jobs and jobs[0]['offset'] < jobs[0]['len']: + log('Backup job is still in progress') + +vm.qmp_log('block-job-set-speed', device=3D'drive0', speed=3D0) + +# Emulate server down time for 1 second +time.sleep(1) + +log('Start NBD server') +srv =3D qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b) + +e =3D vm.event_wait('BLOCK_JOB_COMPLETED') +log('Backup completed: {}'.format(e['data']['offset'])) + +vm.qmp_log('blockdev-del', node_name=3D'backup0') +srv.kill() +vm.shutdown() diff --git a/tests/qemu-iotests/264.out b/tests/qemu-iotests/264.out new file mode 100644 index 0000000000..3000944b09 --- /dev/null +++ b/tests/qemu-iotests/264.out @@ -0,0 +1,13 @@ +{"execute": "blockdev-add", "arguments": {"driver": "raw", "file": {"drive= r": "nbd", "reconnect-delay": 10, "server": {"path": "TEST_DIR/PID-nbd-sock= ", "type": "unix"}}, "node-name": "backup0"}} +{"return": {}} +{"execute": "blockdev-backup", "arguments": {"device": "drive0", "speed": = 1048576, "sync": "full", "target": "backup0"}} +{"return": {}} +Backup job is started +Kill NBD server +Backup job is still in progress +{"execute": "block-job-set-speed", "arguments": {"device": "drive0", "spee= d": 0}} +{"return": {}} +Start NBD server +Backup completed: 5242880 +{"execute": "blockdev-del", "arguments": {"node-name": "backup0"}} +{"return": {}} diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 5d3da937e4..4f6dd6f153 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -275,5 +275,6 @@ 258 rw quick 262 rw quick migration 263 rw quick +264 rw quick 265 rw auto quick 266 rw quick diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index b26271187c..8912bd4661 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -164,6 +164,13 @@ def qemu_io_silent(*args): (-exitcode, ' '.join(args))) return exitcode =20 +def qemu_io_silent_check(*args): + '''Run qemu-io and return the true if subprocess returned 0''' + args =3D qemu_io_args + list(args) + exitcode =3D subprocess.call(args, stdout=3Dopen('/dev/null', 'w'), + stderr=3Dsubprocess.STDOUT) + return exitcode =3D=3D 0 + def get_virtio_scsi_device(): if qemu_default_machine =3D=3D 's390-ccw-virtio': return 'virtio-scsi-ccw' @@ -229,6 +236,10 @@ def qemu_nbd_early_pipe(*args): else: return exitcode, subp.communicate()[0] =20 +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=3Dimgfmt, fmt2=3Dimgfmt): '''Return True if two image files are identical''' return qemu_img('compare', '-f', fmt1, --=20 2.21.0