Skip to content

Commit fa35591

Browse files
Vladimir Sementsov-Ogievskiyebblake
authored andcommitted
block/nbd: split nbd_establish_connection out of nbd_client_connect
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 <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> Signed-off-by: Eric Blake <[email protected]>
1 parent 03a970b commit fa35591

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

block/nbd.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ typedef struct BDRVNBDState {
9393
char *x_dirty_bitmap;
9494
} BDRVNBDState;
9595

96-
static int nbd_client_connect(BlockDriverState *bs, Error **errp);
96+
static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
97+
Error **errp);
98+
static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
99+
Error **errp);
97100

98101
static void nbd_clear_bdrvstate(BDRVNBDState *s)
99102
{
@@ -241,7 +244,9 @@ static bool nbd_client_connecting_wait(BDRVNBDState *s)
241244

242245
static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
243246
{
247+
int ret;
244248
Error *local_err = NULL;
249+
QIOChannelSocket *sioc;
245250

246251
if (!nbd_client_connecting(s)) {
247252
return;
@@ -280,19 +285,25 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
280285
s->ioc = NULL;
281286
}
282287

283-
s->connect_status = nbd_client_connect(s->bs, &local_err);
288+
sioc = nbd_establish_connection(s->saddr, &local_err);
289+
if (!sioc) {
290+
ret = -ECONNREFUSED;
291+
goto out;
292+
}
293+
294+
ret = nbd_client_handshake(s->bs, sioc, &local_err);
295+
296+
out:
297+
s->connect_status = ret;
284298
error_free(s->connect_err);
285299
s->connect_err = NULL;
286300
error_propagate(&s->connect_err, local_err);
287301

288-
if (s->connect_status < 0) {
289-
/* failed attempt */
290-
return;
302+
if (ret >= 0) {
303+
/* successfully connected */
304+
s->state = NBD_CLIENT_CONNECTED;
305+
qemu_co_queue_restart_all(&s->free_sema);
291306
}
292-
293-
/* successfully connected */
294-
s->state = NBD_CLIENT_CONNECTED;
295-
qemu_co_queue_restart_all(&s->free_sema);
296307
}
297308

298309
static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
@@ -1425,24 +1436,15 @@ static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
14251436
return sioc;
14261437
}
14271438

1428-
static int nbd_client_connect(BlockDriverState *bs, Error **errp)
1439+
/* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
1440+
static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
1441+
Error **errp)
14291442
{
14301443
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
14311444
AioContext *aio_context = bdrv_get_aio_context(bs);
14321445
int ret;
14331446

1434-
/*
1435-
* establish TCP connection, return error if it fails
1436-
* TODO: Configurable retry-until-timeout behaviour.
1437-
*/
1438-
QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp);
1439-
1440-
if (!sioc) {
1441-
return -ECONNREFUSED;
1442-
}
1443-
1444-
/* NBD handshake */
1445-
trace_nbd_client_connect(s->export);
1447+
trace_nbd_client_handshake(s->export);
14461448
qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
14471449
qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
14481450

@@ -1489,7 +1491,7 @@ static int nbd_client_connect(BlockDriverState *bs, Error **errp)
14891491
object_ref(OBJECT(s->ioc));
14901492
}
14911493

1492-
trace_nbd_client_connect_success(s->export);
1494+
trace_nbd_client_handshake_success(s->export);
14931495

14941496
return 0;
14951497

@@ -1894,6 +1896,7 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
18941896
{
18951897
int ret;
18961898
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1899+
QIOChannelSocket *sioc;
18971900

18981901
ret = nbd_process_options(bs, options, errp);
18991902
if (ret < 0) {
@@ -1904,7 +1907,16 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
19041907
qemu_co_mutex_init(&s->send_mutex);
19051908
qemu_co_queue_init(&s->free_sema);
19061909

1907-
ret = nbd_client_connect(bs, errp);
1910+
/*
1911+
* establish TCP connection, return error if it fails
1912+
* TODO: Configurable retry-until-timeout behaviour.
1913+
*/
1914+
sioc = nbd_establish_connection(s->saddr, errp);
1915+
if (!sioc) {
1916+
return -ECONNREFUSED;
1917+
}
1918+
1919+
ret = nbd_client_handshake(bs, sioc, errp);
19081920
if (ret < 0) {
19091921
nbd_clear_bdrvstate(s);
19101922
return ret;

block/trace-events

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ nbd_parse_blockstatus_compliance(const char *err) "ignoring extra data from non-
168168
nbd_structured_read_compliance(const char *type) "server sent non-compliant unaligned read %s chunk"
169169
nbd_read_reply_entry_fail(int ret, const char *err) "ret = %d, err: %s"
170170
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"
171-
nbd_client_connect(const char *export_name) "export '%s'"
172-
nbd_client_connect_success(const char *export_name) "export '%s'"
171+
nbd_client_handshake(const char *export_name) "export '%s'"
172+
nbd_client_handshake_success(const char *export_name) "export '%s'"
173173

174174
# ssh.c
175175
ssh_restart_coroutine(void *co) "co=%p"

0 commit comments

Comments
 (0)