From patchwork Mon May 11 11:14:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 11540469 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AE9CB159A for ; Mon, 11 May 2020 11:39:08 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E13C20708 for ; Mon, 11 May 2020 11:39:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=web.de header.i=@web.de header.b="stMMxOC5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E13C20708 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=web.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:37314 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jY6m3-0004di-EJ for patchwork-qemu-devel@patchwork.kernel.org; Mon, 11 May 2020 07:39:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55832) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jY6Oa-0000cO-2m; Mon, 11 May 2020 07:14:52 -0400 Received: from mout.web.de ([212.227.17.12]:57943) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jY6OY-0006tK-Mn; Mon, 11 May 2020 07:14:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1589195685; bh=9nGypIWIejpA7HfrS60ch3u2aL2qpDcRe5ObfV39NrQ=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=stMMxOC5X4CrTiHK3jDxNNEGFqU/lPuzj0ycUvh7PA662QigR4LTAwtsgwO0tIJ/S sBSiMk64+D1fnvhAR5Zo0WciHh41HZcF7OF4bMYXmoIp2RmTrbdlOSBItxzkKPQtbi XiPNsp3qImpGFg1+FUYnzKxX0FsSab7vh4Ve1etU= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from luklap ([89.247.255.192]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MNOV6-1jji4F25xC-00P0bh; Mon, 11 May 2020 13:14:45 +0200 Date: Mon, 11 May 2020 13:14:44 +0200 From: Lukas Straub To: qemu-devel Subject: [PATCH 3/5] block/nbd.c: Add yank feature Message-ID: <1e712fa7f08e4772c2a68197a851161bee51610f.1589193717.git.lukasstraub2@web.de> In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:CbWoCUPLZCMVgZAYdVQ9OIB5fjgRkbvXEKmDdD06kATq24YYNYn YEBaIRzw5Oxk9mEGbRwjS6as8ZZzf1qcwOdzAYxnY7nYiOd8sgXpeslloWCCEnoNzzegdIh l46wn8QduDAWAfnYyDladi2xx6nNpgM0zdLTrSNndS9WvW3Uj7Z6RTSIv2Z91302IJZ+PSg vNVrKjVwtunKVonOQ9xuA== X-UI-Out-Filterresults: notjunk:1;V03:K0:INt/hoZG3E8=:WDYkHSwaBul5QedSSoBVCn wLFGoPwPXzT/9CjK5+W5ibkZ7wF/gte5AwrgDwEqciklrzwIfYVkGSpx2XptX4NPxfdUnZqNC Lh26/WuXP6K8ZrxArRdAcjE1jZY211hK0B8IDgTYWAq6sNsxDjWrgIVF3C68YXNytvaXiwQmE sadW11eYrHpA5VpNB63NippXm29tZdQi/DmhldEFEUKr9nj0nav/FY0f8UQaXpGmyHw/Gjr9u nY+sAql88li+qt69T09HEO8GRo0ujIjsgDeQ618508EACbZ7bfbpghcUxa5pC3cIRt0sUMaRf n4Q7CftrStOIJUUlGpBkwYP1M+PjzYRa/6SyeU1kppxsO82iARHTc+QcKy1D04mpO0R5U6+S0 cunLJizIs9p2b0PPrxU9Wc5FSy9atwPYgJ3uDKxo9WoEmW432bJDM/sP27PjuMWco3EjzwwGN +tkKPJFT0TtLl6kVo9lSSqg62iXWvVa6GitAZ6r1ocY3gU25/ZyC5OP9QeuOuggpnObkdyM2e uiyvfFQsADojwt2l8EYcuSsHanj+Pt1BxUM4vrolpIod4V0DpAIlhZnI0ypcIthkWofOiILMC LNhhrzrX5vHHOqBLgEmpG1YQYxfDq7htuvCd7INwaR7clfWEVaklQ9OvIxPuDALYBfO0k8cZY H1Bb1rd86nb5lG17N6FbGxq6rJiiDyZSyI0aeaBsiUY6Hiib8es76ysHMb8SKVX8VP02MX0Kc FGkUpJyLbvDr7lGAo+oTEKixni4FamsyzWkhHD7SrMf1Dlw/2tIOmlzino+jDMkwmsTpl91iB soCrtUYXppTh7WaO69sY+y3pqgBlQ67jHWAytkSh36MHSJWccUsOnbsN0bbJMCiG9j519r8Bx oGdIvSnwMtTRLPVpyUz4KfNsddMh+kLbtQIRhYMyVXt1IHGnGVbQ5xebJK8x+SuXUV9kuCBrw Iaq35JalCiccmCiEHWJiDgrvPt35ZqeaqIaAvLK8dC6749DE+7ZLF03a3jgKrXMrbjryfUszk SUttqZBQVz9rEk+83r/HcY+xwK+RELXIyQF1U8/L0S4ACMzV96n/fYr5JxRH79D3lGMvCCbBa 2pbaBckwVvVlG3viLk4ZkZEpw9zL7Cviyn7ksh9vpoemxf8WXA/mvDO+Wt7rXTAlDlFcOLoJ7 MTXPwbJHdWC52o1oikECmDZUyy1sSqL7V18AVfVsW1woGejwgefUK1G7xPThzbGL6pQT3npI2 bjldkT8UxxHJj5DQq Received-SPF: pass client-ip=212.227.17.12; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/11 07:10:54 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action 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: Kevin Wolf , "Daniel P. =?utf-8?b?QmVycmFuZ8Op?= " , qemu-block , Juan Quintela , "Dr. David Alan Gilbert" , Max Reitz , Paolo Bonzini , =?utf-8?q?Marc-Andr=C3=A9?= Lureau Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Add yank option, pass it to the socket-channel and register a yank function which sets s->state = NBD_CLIENT_QUIT. This is the same behaviour as if an error occured. Signed-off-by: Lukas Straub --- Makefile.objs | 1 + block/nbd.c | 68 +++++++++++++++++++++++++++++++++----------- qapi/block-core.json | 5 +++- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/Makefile.objs b/Makefile.objs index 889115775c..4b213b3e78 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -18,6 +18,7 @@ block-obj-y += block.o blockjob.o job.o block-obj-y += block/ scsi/ block-obj-y += qemu-io-cmds.o block-obj-$(CONFIG_REPLICATION) += replication.o +block-obj-y += yank.o block-obj-m = block/ diff --git a/block/nbd.c b/block/nbd.c index 2160859f64..3c0fd3abb8 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -35,6 +35,7 @@ #include "qemu/option.h" #include "qemu/cutils.h" #include "qemu/main-loop.h" +#include "qemu/atomic.h" #include "qapi/qapi-visit-sockets.h" #include "qapi/qmp/qstring.h" @@ -43,6 +44,8 @@ #include "block/nbd.h" #include "block/block_int.h" +#include "yank.h" + #define EN_OPTSTR ":exportname=" #define MAX_NBD_REQUESTS 16 @@ -91,6 +94,7 @@ typedef struct BDRVNBDState { QCryptoTLSCreds *tlscreds; const char *hostname; char *x_dirty_bitmap; + bool yank; } BDRVNBDState; static int nbd_client_connect(BlockDriverState *bs, Error **errp); @@ -111,12 +115,12 @@ static void nbd_clear_bdrvstate(BDRVNBDState *s) static void nbd_channel_error(BDRVNBDState *s, int ret) { if (ret == -EIO) { - if (s->state == NBD_CLIENT_CONNECTED) { + if (atomic_read(&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) { + if (atomic_read(&s->state) == NBD_CLIENT_CONNECTED) { qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); } s->state = NBD_CLIENT_QUIT; @@ -167,7 +171,7 @@ static void nbd_client_attach_aio_context(BlockDriverState *bs, * s->connection_co is either yielded from nbd_receive_reply or from * nbd_co_reconnect_loop() */ - if (s->state == NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) == NBD_CLIENT_CONNECTED) { qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); } @@ -206,7 +210,7 @@ static void nbd_teardown_connection(BlockDriverState *bs) { BDRVNBDState *s = (BDRVNBDState *)bs->opaque; - if (s->state == NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) == NBD_CLIENT_CONNECTED) { /* finish any pending coroutines */ assert(s->ioc); qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); @@ -230,13 +234,14 @@ static void nbd_teardown_connection(BlockDriverState *bs) static bool nbd_client_connecting(BDRVNBDState *s) { - return s->state == NBD_CLIENT_CONNECTING_WAIT || - s->state == NBD_CLIENT_CONNECTING_NOWAIT; + NBDClientState state = atomic_read(&s->state); + return state == NBD_CLIENT_CONNECTING_WAIT || + state == NBD_CLIENT_CONNECTING_NOWAIT; } static bool nbd_client_connecting_wait(BDRVNBDState *s) { - return s->state == NBD_CLIENT_CONNECTING_WAIT; + return atomic_read(&s->state) == NBD_CLIENT_CONNECTING_WAIT; } static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) @@ -305,7 +310,7 @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s) nbd_reconnect_attempt(s); while (nbd_client_connecting(s)) { - if (s->state == NBD_CLIENT_CONNECTING_WAIT && + if (atomic_read(&s->state) == NBD_CLIENT_CONNECTING_WAIT && qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time_ns > delay_ns) { s->state = NBD_CLIENT_CONNECTING_NOWAIT; @@ -341,7 +346,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque) int ret = 0; Error *local_err = NULL; - while (s->state != NBD_CLIENT_QUIT) { + while (atomic_read(&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 @@ -356,7 +361,7 @@ static coroutine_fn void nbd_connection_entry(void *opaque) nbd_co_reconnect_loop(s); } - if (s->state != NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) != NBD_CLIENT_CONNECTED) { continue; } @@ -435,7 +440,7 @@ static int nbd_co_send_request(BlockDriverState *bs, qemu_co_queue_wait(&s->free_sema, &s->send_mutex); } - if (s->state != NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) != NBD_CLIENT_CONNECTED) { rc = -EIO; goto err; } @@ -462,7 +467,7 @@ static int nbd_co_send_request(BlockDriverState *bs, if (qiov) { qio_channel_set_cork(s->ioc, true); rc = nbd_send_request(s->ioc, request); - if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) { + if (rc >= 0 && atomic_read(&s->state) == NBD_CLIENT_CONNECTED) { if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, NULL) < 0) { rc = -EIO; @@ -777,7 +782,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->state != NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) != NBD_CLIENT_CONNECTED) { error_setg(errp, "Connection closed"); return -EIO; } @@ -936,7 +941,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, NBDReply local_reply; NBDStructuredReplyChunk *chunk; Error *local_err = NULL; - if (s->state != NBD_CLIENT_CONNECTED) { + if (atomic_read(&s->state) != NBD_CLIENT_CONNECTED) { error_setg(&local_err, "Connection closed"); nbd_iter_channel_error(iter, -EIO, &local_err); goto break_loop; @@ -961,7 +966,8 @@ 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->state != NBD_CLIENT_CONNECTED) { + if (nbd_reply_is_simple(reply) || + atomic_read(&s->state) != NBD_CLIENT_CONNECTED) { goto break_loop; } @@ -1395,6 +1401,14 @@ static int nbd_client_reopen_prepare(BDRVReopenState *state, return 0; } +static void nbd_yank(void *opaque) +{ + BlockDriverState *bs = opaque; + BDRVNBDState *s = (BDRVNBDState *)bs->opaque; + + atomic_set(&s->state, NBD_CLIENT_QUIT); +} + static void nbd_client_close(BlockDriverState *bs) { BDRVNBDState *s = (BDRVNBDState *)bs->opaque; @@ -1407,14 +1421,17 @@ static void nbd_client_close(BlockDriverState *bs) nbd_teardown_connection(bs); } -static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, +static QIOChannelSocket *nbd_establish_connection(BlockDriverState *bs, + SocketAddress *saddr, Error **errp) { + BDRVNBDState *s = (BDRVNBDState *)bs->opaque; QIOChannelSocket *sioc; Error *local_err = NULL; sioc = qio_channel_socket_new(); qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); + qio_channel_set_yank(QIO_CHANNEL(sioc), s->yank); qio_channel_socket_connect_sync(sioc, saddr, &local_err); if (local_err) { @@ -1438,7 +1455,7 @@ static int nbd_client_connect(BlockDriverState *bs, Error **errp) * establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. */ - QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp); + QIOChannelSocket *sioc = nbd_establish_connection(bs, s->saddr, errp); if (!sioc) { return -ECONNREFUSED; @@ -1829,6 +1846,12 @@ static QemuOptsList nbd_runtime_opts = { "future requests before a successful reconnect will " "immediately fail. Default 0", }, + { + .name = "yank", + .type = QEMU_OPT_BOOL, + .help = "Forcibly close the connection and don't attempt to " + "reconnect when the 'yank' qmp command is executed.", + }, { /* end of list */ } }, }; @@ -1888,6 +1911,8 @@ static int nbd_process_options(BlockDriverState *bs, QDict *options, s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0); + s->yank = qemu_opt_get_bool(opts, "yank", false); + ret = 0; error: @@ -1921,6 +1946,10 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags, /* successfully connected */ s->state = NBD_CLIENT_CONNECTED; + if (s->yank) { + yank_register_function(nbd_yank, bs); + } + 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); @@ -1972,6 +2001,11 @@ static void nbd_close(BlockDriverState *bs) BDRVNBDState *s = bs->opaque; nbd_client_close(bs); + + if (s->yank) { + yank_unregister_function(nbd_yank, bs); + } + nbd_clear_bdrvstate(s); } diff --git a/qapi/block-core.json b/qapi/block-core.json index 943df1926a..1c1578160e 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3862,6 +3862,8 @@ # reconnect. After that time, any delayed requests and all # future requests before a successful reconnect will # immediately fail. Default 0 (Since 4.2) +# @yank: Forcibly close the connection and don't attempt to reconnect when +# the 'yank' qmp command is executed. (Since: 5.1) # # Since: 2.9 ## @@ -3870,7 +3872,8 @@ '*export': 'str', '*tls-creds': 'str', '*x-dirty-bitmap': 'str', - '*reconnect-delay': 'uint32' } } + '*reconnect-delay': 'uint32', + 'yank': 'bool' } } ## # @BlockdevOptionsRaw: