From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
"open list:Network Block Dev..." <qemu-block@nongnu.org>,
Max Reitz <mreitz@redhat.com>
Subject: [PULL 6/9] block/nbd: split nbd_establish_connection out of nbd_client_connect
Date: Tue, 28 Jul 2020 10:04:04 -0500 [thread overview]
Message-ID: <20200728150408.291299-7-eblake@redhat.com> (raw)
In-Reply-To: <20200728150408.291299-1-eblake@redhat.com>
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
We are going to implement non-blocking version of
nbd_establish_connection, which for a while will be used only for
nbd_reconnect_attempt, not for nbd_open, so we need to call it
separately.
Refactor nbd_reconnect_attempt in a way which makes next commit
simpler.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200727184751.15704-2-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
---
block/nbd.c | 60 +++++++++++++++++++++++++++-------------------
block/trace-events | 4 ++--
2 files changed, 38 insertions(+), 26 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index 8934bcb47901..3558c173e34b 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -93,7 +93,10 @@ typedef struct BDRVNBDState {
char *x_dirty_bitmap;
} BDRVNBDState;
-static int nbd_client_connect(BlockDriverState *bs, Error **errp);
+static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
+ Error **errp);
+static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
+ Error **errp);
static void nbd_clear_bdrvstate(BDRVNBDState *s)
{
@@ -241,7 +244,9 @@ static bool nbd_client_connecting_wait(BDRVNBDState *s)
static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
{
+ int ret;
Error *local_err = NULL;
+ QIOChannelSocket *sioc;
if (!nbd_client_connecting(s)) {
return;
@@ -280,19 +285,25 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
s->ioc = NULL;
}
- s->connect_status = nbd_client_connect(s->bs, &local_err);
+ sioc = nbd_establish_connection(s->saddr, &local_err);
+ if (!sioc) {
+ ret = -ECONNREFUSED;
+ goto out;
+ }
+
+ ret = nbd_client_handshake(s->bs, sioc, &local_err);
+
+out:
+ s->connect_status = ret;
error_free(s->connect_err);
s->connect_err = NULL;
error_propagate(&s->connect_err, local_err);
- if (s->connect_status < 0) {
- /* failed attempt */
- return;
+ if (ret >= 0) {
+ /* successfully connected */
+ s->state = NBD_CLIENT_CONNECTED;
+ qemu_co_queue_restart_all(&s->free_sema);
}
-
- /* successfully connected */
- s->state = NBD_CLIENT_CONNECTED;
- qemu_co_queue_restart_all(&s->free_sema);
}
static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
@@ -1425,24 +1436,15 @@ static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
return sioc;
}
-static int nbd_client_connect(BlockDriverState *bs, Error **errp)
+/* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
+static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
+ Error **errp)
{
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
AioContext *aio_context = bdrv_get_aio_context(bs);
int ret;
- /*
- * establish TCP connection, return error if it fails
- * TODO: Configurable retry-until-timeout behaviour.
- */
- QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp);
-
- if (!sioc) {
- return -ECONNREFUSED;
- }
-
- /* NBD handshake */
- trace_nbd_client_connect(s->export);
+ trace_nbd_client_handshake(s->export);
qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
@@ -1489,7 +1491,7 @@ static int nbd_client_connect(BlockDriverState *bs, Error **errp)
object_ref(OBJECT(s->ioc));
}
- trace_nbd_client_connect_success(s->export);
+ trace_nbd_client_handshake_success(s->export);
return 0;
@@ -1894,6 +1896,7 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
{
int ret;
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
+ QIOChannelSocket *sioc;
ret = nbd_process_options(bs, options, errp);
if (ret < 0) {
@@ -1904,7 +1907,16 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
qemu_co_mutex_init(&s->send_mutex);
qemu_co_queue_init(&s->free_sema);
- ret = nbd_client_connect(bs, errp);
+ /*
+ * establish TCP connection, return error if it fails
+ * TODO: Configurable retry-until-timeout behaviour.
+ */
+ sioc = nbd_establish_connection(s->saddr, errp);
+ if (!sioc) {
+ return -ECONNREFUSED;
+ }
+
+ ret = nbd_client_handshake(bs, sioc, errp);
if (ret < 0) {
nbd_clear_bdrvstate(s);
return ret;
diff --git a/block/trace-events b/block/trace-events
index d3533ca8963c..915833506189 100644
--- a/block/trace-events
+++ b/block/trace-events
@@ -168,8 +168,8 @@ nbd_parse_blockstatus_compliance(const char *err) "ignoring extra data from non-
nbd_structured_read_compliance(const char *type) "server sent non-compliant unaligned read %s chunk"
nbd_read_reply_entry_fail(int ret, const char *err) "ret = %d, err: %s"
nbd_co_request_fail(uint64_t from, uint32_t len, uint64_t handle, uint16_t flags, uint16_t type, const char *name, int ret, const char *err) "Request failed { .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 ", .flags = 0x%" PRIx16 ", .type = %" PRIu16 " (%s) } ret = %d, err: %s"
-nbd_client_connect(const char *export_name) "export '%s'"
-nbd_client_connect_success(const char *export_name) "export '%s'"
+nbd_client_handshake(const char *export_name) "export '%s'"
+nbd_client_handshake_success(const char *export_name) "export '%s'"
# ssh.c
ssh_restart_coroutine(void *co) "co=%p"
--
2.27.0
next prev parent reply other threads:[~2020-07-28 15:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-28 15:03 [PULL 0/9] nbd patches for -rc2, 2020-07-28 Eric Blake
2020-07-28 15:03 ` [PULL 1/9] nbd: Fix large trim/zero requests Eric Blake
2020-07-28 15:04 ` [PULL 2/9] block: nbd: Fix convert qcow2 compressed to nbd Eric Blake
2020-07-28 15:04 ` [PULL 3/9] iotests: Make qemu_nbd_popen() a contextmanager Eric Blake
2020-07-28 15:04 ` [PULL 4/9] iotests: Add more qemu_img helpers Eric Blake
2020-07-28 15:04 ` [PULL 5/9] iotests: Test convert to qcow2 compressed to NBD Eric Blake
2020-07-28 15:04 ` Eric Blake [this message]
2020-07-28 15:04 ` [PULL 7/9] block/nbd: allow drain during reconnect attempt Eric Blake
2020-07-28 15:04 ` [PULL 8/9] block/nbd: on shutdown terminate connection attempt Eric Blake
2020-07-28 15:04 ` [PULL 9/9] block/nbd: nbd_co_reconnect_loop(): don't sleep if drained Eric Blake
2020-07-28 20:48 ` [PULL 0/9] nbd patches for -rc2, 2020-07-28 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200728150408.291299-7-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).