From patchwork Tue Feb 5 03:57:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 1036422 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43trVN0YNQz9sMl for ; Tue, 5 Feb 2019 15:01:32 +1100 (AEDT) Received: from localhost ([127.0.0.1]:53673 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqrvO-0003Ry-24 for incoming@patchwork.ozlabs.org; Mon, 04 Feb 2019 23:01:30 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51097) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqrrL-0000gQ-7Q for qemu-devel@nongnu.org; Mon, 04 Feb 2019 22:57:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gqrrJ-0002on-Jr for qemu-devel@nongnu.org; Mon, 04 Feb 2019 22:57:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51914) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gqrrG-0002my-Vn; Mon, 04 Feb 2019 22:57:15 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 45FC98762D; Tue, 5 Feb 2019 03:57:14 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-162.phx2.redhat.com [10.3.116.162]) by smtp.corp.redhat.com (Postfix) with ESMTP id 791105C57C; Tue, 5 Feb 2019 03:57:13 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 4 Feb 2019 21:57:00 -0600 Message-Id: <20190205035704.26014-5-eblake@redhat.com> In-Reply-To: <20190205035704.26014-1-eblake@redhat.com> References: <20190205035704.26014-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 05 Feb 2019 03:57:14 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 4/8] block/nbd: move connection code from block/nbd to block/nbd-client X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Vladimir Sementsov-Ogievskiy Keep all connection code in one file, to be able to implement reconnect in further patches. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20190201130138.94525-3-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake [eblake: format tweak] Signed-off-by: Eric Blake --- block/nbd-client.h | 2 +- block/nbd-client.c | 38 ++++++++++++++++++++++++++++++++++++-- block/nbd.c | 40 ++-------------------------------------- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/block/nbd-client.h b/block/nbd-client.h index cfc90550b99..2f047ba6142 100644 --- a/block/nbd-client.h +++ b/block/nbd-client.h @@ -41,7 +41,7 @@ typedef struct NBDClientSession { NBDClientSession *nbd_get_client_session(BlockDriverState *bs); int nbd_client_init(BlockDriverState *bs, - QIOChannelSocket *sock, + SocketAddress *saddr, const char *export_name, QCryptoTLSCreds *tlscreds, const char *hostname, diff --git a/block/nbd-client.c b/block/nbd-client.c index fd58cda2f17..80dc8cff473 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -991,8 +991,29 @@ void nbd_client_close(BlockDriverState *bs) nbd_teardown_connection(bs); } +static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, + Error **errp) +{ + QIOChannelSocket *sioc; + Error *local_err = NULL; + + sioc = qio_channel_socket_new(); + qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); + + qio_channel_socket_connect_sync(sioc, saddr, &local_err); + if (local_err) { + object_unref(OBJECT(sioc)); + error_propagate(errp, local_err); + return NULL; + } + + qio_channel_set_delay(QIO_CHANNEL(sioc), false); + + return sioc; +} + int nbd_client_init(BlockDriverState *bs, - QIOChannelSocket *sioc, + SocketAddress *saddr, const char *export, QCryptoTLSCreds *tlscreds, const char *hostname, @@ -1002,6 +1023,16 @@ int nbd_client_init(BlockDriverState *bs, NBDClientSession *client = nbd_get_client_session(bs); int ret; + /* + * establish TCP connection, return error if it fails + * TODO: Configurable retry-until-timeout behaviour. + */ + QIOChannelSocket *sioc = nbd_establish_connection(saddr, errp); + + if (!sioc) { + return -ECONNREFUSED; + } + /* NBD handshake */ logout("session init %s\n", export); qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL); @@ -1017,6 +1048,7 @@ int nbd_client_init(BlockDriverState *bs, g_free(client->info.name); if (ret < 0) { logout("Failed to negotiate with the NBD server\n"); + object_unref(OBJECT(sioc)); return ret; } if (x_dirty_bitmap && !client->info.base_allocation) { @@ -1042,7 +1074,6 @@ int nbd_client_init(BlockDriverState *bs, qemu_co_mutex_init(&client->send_mutex); qemu_co_queue_init(&client->free_sema); client->sioc = sioc; - object_ref(OBJECT(client->sioc)); if (!client->ioc) { client->ioc = QIO_CHANNEL(sioc); @@ -1068,6 +1099,9 @@ int nbd_client_init(BlockDriverState *bs, NBDRequest request = { .type = NBD_CMD_DISC }; nbd_send_request(client->ioc ?: QIO_CHANNEL(sioc), &request); + + object_unref(OBJECT(sioc)); + return ret; } } diff --git a/block/nbd.c b/block/nbd.c index e87699fb73b..9db5eded892 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -295,30 +295,6 @@ NBDClientSession *nbd_get_client_session(BlockDriverState *bs) return &s->client; } -static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, - Error **errp) -{ - QIOChannelSocket *sioc; - Error *local_err = NULL; - - sioc = qio_channel_socket_new(); - qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); - - qio_channel_socket_connect_sync(sioc, - saddr, - &local_err); - if (local_err) { - object_unref(OBJECT(sioc)); - error_propagate(errp, local_err); - return NULL; - } - - qio_channel_set_delay(QIO_CHANNEL(sioc), false); - - return sioc; -} - - static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) { Object *obj; @@ -394,7 +370,6 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags, BDRVNBDState *s = bs->opaque; QemuOpts *opts = NULL; Error *local_err = NULL; - QIOChannelSocket *sioc = NULL; QCryptoTLSCreds *tlscreds = NULL; const char *hostname = NULL; int ret = -EINVAL; @@ -434,22 +409,11 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags, hostname = s->saddr->u.inet.host; } - /* establish TCP connection, return error if it fails - * TODO: Configurable retry-until-timeout behaviour. - */ - sioc = nbd_establish_connection(s->saddr, errp); - if (!sioc) { - ret = -ECONNREFUSED; - goto error; - } - /* NBD handshake */ - ret = nbd_client_init(bs, sioc, s->export, tlscreds, hostname, + ret = nbd_client_init(bs, s->saddr, s->export, tlscreds, hostname, qemu_opt_get(opts, "x-dirty-bitmap"), errp); + error: - if (sioc) { - object_unref(OBJECT(sioc)); - } if (tlscreds) { object_unref(OBJECT(tlscreds)); }