From nobody Sat May 4 12:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511346405406921.1916670848291; Wed, 22 Nov 2017 02:26:45 -0800 (PST) Received: from localhost ([::1]:38715 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSEa-0005nM-Jb for importer@patchew.org; Wed, 22 Nov 2017 05:26:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33965) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8c-0001Nx-3k for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004mN-99 for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:13 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:13991 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHS8T-0004k2-Ee for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:06 -0500 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id vAMAJwhG016254; Wed, 22 Nov 2017 13:19:59 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:54 +0300 Message-Id: <20171122101958.17065-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 1/5] nbd/server: refactor negotiation functions parameters 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: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead of passing currently negotiating option and its length to many of negotiation functions let's just store them on NBDClient struct to be state-variables of negotiation phase. This unifies semantics of negotiation functions and this allows to track changes of remaining option length in future patches. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 164 +++++++++++++++++++++++++++++--------------------------= ---- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 7d6801b427..bccc0274e7 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -102,6 +102,10 @@ struct NBDClient { bool closing; =20 bool structured_reply; + + uint32_t opt; /* Current option being negotiated */ + uint32_t optlen; /* remaining length of data in ioc for the option bei= ng + negotiated now */ }; =20 /* That's all folks */ @@ -137,10 +141,12 @@ static void nbd_client_receive_next_request(NBDClient= *client); =20 /* Send a reply header, including length, but no payload. * Return -errno on error, 0 on success. */ -static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type, - uint32_t opt, uint32_t len, Error **= errp) +static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type, + uint32_t len, Error **errp) { uint64_t magic; + QIOChannel *ioc =3D client->ioc; + uint32_t opt =3D client->opt; =20 trace_nbd_negotiate_send_rep_len(opt, nbd_opt_lookup(opt), type, nbd_rep_lookup(type), len); @@ -174,17 +180,17 @@ static int nbd_negotiate_send_rep_len(QIOChannel *ioc= , uint32_t type, =20 /* Send a reply header with default 0 length. * Return -errno on error, 0 on success. */ -static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t= opt, +static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type, Error **errp) { - return nbd_negotiate_send_rep_len(ioc, type, opt, 0, errp); + return nbd_negotiate_send_rep_len(client, type, 0, errp); } =20 /* Send an error reply. * Return -errno on error, 0 on success. */ -static int GCC_FMT_ATTR(5, 6) -nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type, - uint32_t opt, Error **errp, const char *fmt, ..= .) +static int GCC_FMT_ATTR(4, 5) +nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, + Error **errp, const char *fmt, ...) { va_list va; char *msg; @@ -197,11 +203,11 @@ nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t = type, len =3D strlen(msg); assert(len < 4096); trace_nbd_negotiate_send_rep_err(msg); - ret =3D nbd_negotiate_send_rep_len(ioc, type, opt, len, errp); + ret =3D nbd_negotiate_send_rep_len(client, type, len, errp); if (ret < 0) { goto out; } - if (nbd_write(ioc, msg, len, errp) < 0) { + if (nbd_write(client->ioc, msg, len, errp) < 0) { error_prepend(errp, "write failed (error message): "); ret =3D -EIO; } else { @@ -215,21 +221,21 @@ out: =20 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. * Return -errno on error, 0 on success. */ -static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp, +static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, Error **errp) { size_t name_len, desc_len; uint32_t len; const char *name =3D exp->name ? exp->name : ""; const char *desc =3D exp->description ? exp->description : ""; + QIOChannel *ioc =3D client->ioc; int ret; =20 trace_nbd_negotiate_send_rep_list(name, desc); name_len =3D strlen(name); desc_len =3D strlen(desc); len =3D name_len + desc_len + sizeof(len); - ret =3D nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, = len, - errp); + ret =3D nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp); if (ret < 0) { return ret; } @@ -258,20 +264,21 @@ static int nbd_negotiate_send_rep_list(QIOChannel *io= c, NBDExport *exp, static int nbd_negotiate_handle_list(NBDClient *client, Error **errp) { NBDExport *exp; + assert(client->opt =3D=3D NBD_OPT_LIST); =20 /* For each export, send a NBD_REP_SERVER reply. */ QTAILQ_FOREACH(exp, &exports, next) { - if (nbd_negotiate_send_rep_list(client->ioc, exp, errp)) { + if (nbd_negotiate_send_rep_list(client, exp, errp)) { return -EINVAL; } } /* Finish with a NBD_REP_ACK. */ - return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST, = errp); + return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); } =20 /* Send a reply to NBD_OPT_EXPORT_NAME. * Return -errno on error, 0 on success. */ -static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t le= ngth, +static int nbd_negotiate_handle_export_name(NBDClient *client, uint16_t myflags, bool no_zero= es, Error **errp) { @@ -288,15 +295,15 @@ static int nbd_negotiate_handle_export_name(NBDClient= *client, uint32_t length, [10 .. 133] reserved (0) [unless no_zeroes] */ trace_nbd_negotiate_handle_export_name(); - if (length >=3D sizeof(name)) { + if (client->optlen >=3D sizeof(name)) { error_setg(errp, "Bad length received"); return -EINVAL; } - if (nbd_read(client->ioc, name, length, errp) < 0) { + if (nbd_read(client->ioc, name, client->optlen, errp) < 0) { error_prepend(errp, "read failed: "); return -EINVAL; } - name[length] =3D '\0'; + name[client->optlen] =3D '\0'; =20 trace_nbd_negotiate_handle_export_name_request(name); =20 @@ -326,14 +333,14 @@ static int nbd_negotiate_handle_export_name(NBDClient= *client, uint32_t length, /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes. * The buffer does NOT include the info type prefix. * Return -errno on error, 0 if ready to send more. */ -static int nbd_negotiate_send_info(NBDClient *client, uint32_t opt, +static int nbd_negotiate_send_info(NBDClient *client, uint16_t info, uint32_t length, void *b= uf, Error **errp) { int rc; =20 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length); - rc =3D nbd_negotiate_send_rep_len(client->ioc, NBD_REP_INFO, opt, + rc =3D nbd_negotiate_send_rep_len(client, NBD_REP_INFO, sizeof(info) + length, errp); if (rc < 0) { return rc; @@ -351,8 +358,7 @@ static int nbd_negotiate_send_info(NBDClient *client, u= int32_t opt, /* Handle NBD_OPT_INFO and NBD_OPT_GO. * Return -errno on error, 0 if ready for next option, and 1 to move * into transmission phase. */ -static int nbd_negotiate_handle_info(NBDClient *client, uint32_t length, - uint32_t opt, uint16_t myflags, +static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags, Error **errp) { int rc; @@ -373,7 +379,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, 2 bytes: N, number of requests (can be 0) N * 2 bytes: N requests */ - if (length < sizeof(namelen) + sizeof(requests)) { + if (client->optlen < sizeof(namelen) + sizeof(requests)) { msg =3D "overall request too short"; goto invalid; } @@ -381,8 +387,10 @@ static int nbd_negotiate_handle_info(NBDClient *client= , uint32_t length, return -EIO; } be32_to_cpus(&namelen); - length -=3D sizeof(namelen); - if (namelen > length - sizeof(requests) || (length - namelen) % 2) { + client->optlen -=3D sizeof(namelen); + if (namelen > client->optlen - sizeof(requests) || + (client->optlen - namelen) % 2) + { msg =3D "name length is incorrect"; goto invalid; } @@ -390,16 +398,16 @@ static int nbd_negotiate_handle_info(NBDClient *clien= t, uint32_t length, return -EIO; } name[namelen] =3D '\0'; - length -=3D namelen; + client->optlen -=3D namelen; trace_nbd_negotiate_handle_export_name_request(name); =20 if (nbd_read(client->ioc, &requests, sizeof(requests), errp) < 0) { return -EIO; } be16_to_cpus(&requests); - length -=3D sizeof(requests); + client->optlen -=3D sizeof(requests); trace_nbd_negotiate_handle_info_requests(requests); - if (requests !=3D length / sizeof(request)) { + if (requests !=3D client->optlen / sizeof(request)) { msg =3D "incorrect number of requests for overall length"; goto invalid; } @@ -408,7 +416,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, return -EIO; } be16_to_cpus(&request); - length -=3D sizeof(request); + client->optlen -=3D sizeof(request); trace_nbd_negotiate_handle_info_request(request, nbd_info_lookup(request)); /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE; @@ -423,18 +431,18 @@ static int nbd_negotiate_handle_info(NBDClient *clien= t, uint32_t length, break; } } - assert(length =3D=3D 0); + assert(client->optlen =3D=3D 0); =20 exp =3D nbd_export_find(name); if (!exp) { - return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_UNKNOWN, - opt, errp, "export '%s' not pres= ent", + return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN, + errp, "export '%s' not present", name); } =20 /* Don't bother sending NBD_INFO_NAME unless client requested it */ if (sendname) { - rc =3D nbd_negotiate_send_info(client, opt, NBD_INFO_NAME, namelen= , name, + rc =3D nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, nam= e, errp); if (rc < 0) { return rc; @@ -446,7 +454,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, if (exp->description) { size_t len =3D strlen(exp->description); =20 - rc =3D nbd_negotiate_send_info(client, opt, NBD_INFO_DESCRIPTION, + rc =3D nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION, len, exp->description, errp); if (rc < 0) { return rc; @@ -458,7 +466,8 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, * whether this is OPT_INFO or OPT_GO. */ /* minimum - 1 for back-compat, or 512 if client is new enough. * TODO: consult blk_bs(blk)->bl.request_alignment? */ - sizes[0] =3D (opt =3D=3D NBD_OPT_INFO || blocksize) ? BDRV_SECTOR_SIZE= : 1; + sizes[0] =3D + (client->opt =3D=3D NBD_OPT_INFO || blocksize) ? BDRV_SECTOR_S= IZE : 1; /* preferred - Hard-code to 4096 for now. * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */ sizes[1] =3D 4096; @@ -468,7 +477,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, cpu_to_be32s(&sizes[0]); cpu_to_be32s(&sizes[1]); cpu_to_be32s(&sizes[2]); - rc =3D nbd_negotiate_send_info(client, opt, NBD_INFO_BLOCK_SIZE, + rc =3D nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE, sizeof(sizes), sizes, errp); if (rc < 0) { return rc; @@ -479,7 +488,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint32_t length, exp->nbdflags | myflags); stq_be_p(buf, exp->size); stw_be_p(buf + 8, exp->nbdflags | myflags); - rc =3D nbd_negotiate_send_info(client, opt, NBD_INFO_EXPORT, + rc =3D nbd_negotiate_send_info(client, NBD_INFO_EXPORT, sizeof(buf), buf, errp); if (rc < 0) { return rc; @@ -489,21 +498,21 @@ static int nbd_negotiate_handle_info(NBDClient *clien= t, uint32_t length, * request block sizes, return an error. * TODO: consult blk_bs(blk)->request_align, and only error if it * is not 1? */ - if (opt =3D=3D NBD_OPT_INFO && !blocksize) { - return nbd_negotiate_send_rep_err(client->ioc, - NBD_REP_ERR_BLOCK_SIZE_REQD, opt, + if (client->opt =3D=3D NBD_OPT_INFO && !blocksize) { + return nbd_negotiate_send_rep_err(client, + NBD_REP_ERR_BLOCK_SIZE_REQD, errp, "request NBD_INFO_BLOCK_SIZE to " "use this export"); } =20 /* Final reply */ - rc =3D nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, opt, errp); + rc =3D nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); if (rc < 0) { return rc; } =20 - if (opt =3D=3D NBD_OPT_GO) { + if (client->opt =3D=3D NBD_OPT_GO) { client->exp =3D exp; QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); nbd_export_get(client->exp); @@ -512,10 +521,10 @@ static int nbd_negotiate_handle_info(NBDClient *clien= t, uint32_t length, return rc; =20 invalid: - if (nbd_drop(client->ioc, length, errp) < 0) { + if (nbd_drop(client->ioc, client->optlen, errp) < 0) { return -EIO; } - return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_INVALID, op= t, + return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, errp, "%s", msg); } =20 @@ -529,11 +538,12 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDC= lient *client, QIOChannelTLS *tioc; struct NBDTLSHandshakeData data =3D { 0 }; =20 + assert(client->opt =3D=3D NBD_OPT_STARTTLS); + trace_nbd_negotiate_handle_starttls(); ioc =3D client->ioc; =20 - if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, - NBD_OPT_STARTTLS, errp) < 0) { + if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) { return NULL; } =20 @@ -573,22 +583,20 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDC= lient *client, * -errno transmission error occurred or @fatal was requested, errp is set * 0 error message successfully sent to client, errp is not set */ -static int nbd_reject_length(NBDClient *client, uint32_t length, - uint32_t option, bool fatal, Error **errp) +static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp) { int ret; =20 - assert(length); - if (nbd_drop(client->ioc, length, errp) < 0) { + assert(client->optlen); + if (nbd_drop(client->ioc, client->optlen, errp) < 0) { return -EIO; } - ret =3D nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_INVALID, - option, errp, + ret =3D nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, errp, "option '%s' should have zero length", - nbd_opt_lookup(option)); + nbd_opt_lookup(client->opt)); if (fatal && !ret) { error_setg(errp, "option '%s' should have zero length", - nbd_opt_lookup(option)); + nbd_opt_lookup(client->opt)); return -EINVAL; } return ret; @@ -666,12 +674,14 @@ static int nbd_negotiate_options(NBDClient *client, u= int16_t myflags, return -EINVAL; } option =3D be32_to_cpu(option); + client->opt =3D option; =20 if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) { error_prepend(errp, "read failed: "); return -EINVAL; } length =3D be32_to_cpu(length); + client->optlen =3D length; =20 trace_nbd_negotiate_options_check_option(option, nbd_opt_lookup(option)); @@ -687,8 +697,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, if (length) { /* Unconditionally drop the connection if the client * can't start a TLS negotiation correctly */ - return nbd_reject_length(client, length, option, true, - errp); + return nbd_reject_length(client, true, errp); } tioc =3D nbd_negotiate_handle_starttls(client, errp); if (!tioc) { @@ -709,9 +718,8 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, if (nbd_drop(client->ioc, length, errp) < 0) { return -EIO; } - ret =3D nbd_negotiate_send_rep_err(client->ioc, - NBD_REP_ERR_TLS_REQD, - option, errp, + ret =3D nbd_negotiate_send_rep_err(client, + NBD_REP_ERR_TLS_REQD, err= p, "Option 0x%" PRIx32 "not permitted before TLS= ", option); @@ -727,8 +735,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, switch (option) { case NBD_OPT_LIST: if (length) { - ret =3D nbd_reject_length(client, length, option, fals= e, - errp); + ret =3D nbd_reject_length(client, false, errp); } else { ret =3D nbd_negotiate_handle_list(client, errp); } @@ -738,18 +745,17 @@ static int nbd_negotiate_options(NBDClient *client, u= int16_t myflags, /* NBD spec says we must try to reply before * disconnecting, but that we must also tolerate * guests that don't wait for our reply. */ - nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, option, N= ULL); + nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL); return 1; =20 case NBD_OPT_EXPORT_NAME: - return nbd_negotiate_handle_export_name(client, length, + return nbd_negotiate_handle_export_name(client, myflags, no_zeroes, errp); =20 case NBD_OPT_INFO: case NBD_OPT_GO: - ret =3D nbd_negotiate_handle_info(client, length, option, - myflags, errp); + ret =3D nbd_negotiate_handle_info(client, myflags, errp); if (ret =3D=3D 1) { assert(option =3D=3D NBD_OPT_GO); return 0; @@ -758,32 +764,27 @@ static int nbd_negotiate_options(NBDClient *client, u= int16_t myflags, =20 case NBD_OPT_STARTTLS: if (length) { - ret =3D nbd_reject_length(client, length, option, fals= e, - errp); + ret =3D nbd_reject_length(client, false, errp); } else if (client->tlscreds) { - ret =3D nbd_negotiate_send_rep_err(client->ioc, - NBD_REP_ERR_INVALID, - option, errp, + ret =3D nbd_negotiate_send_rep_err(client, + NBD_REP_ERR_INVALID, = errp, "TLS already enabled"= ); } else { - ret =3D nbd_negotiate_send_rep_err(client->ioc, - NBD_REP_ERR_POLICY, - option, errp, + ret =3D nbd_negotiate_send_rep_err(client, + NBD_REP_ERR_POLICY, e= rrp, "TLS not configured"); } break; =20 case NBD_OPT_STRUCTURED_REPLY: if (length) { - ret =3D nbd_reject_length(client, length, option, fals= e, - errp); + ret =3D nbd_reject_length(client, false, errp); } else if (client->structured_reply) { ret =3D nbd_negotiate_send_rep_err( - client->ioc, NBD_REP_ERR_INVALID, option, errp, + client, NBD_REP_ERR_INVALID, errp, "structured reply already negotiated"); } else { - ret =3D nbd_negotiate_send_rep(client->ioc, NBD_REP_AC= K, - option, errp); + ret =3D nbd_negotiate_send_rep(client, NBD_REP_ACK, er= rp); client->structured_reply =3D true; myflags |=3D NBD_FLAG_SEND_DF; } @@ -793,9 +794,8 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, if (nbd_drop(client->ioc, length, errp) < 0) { return -EIO; } - ret =3D nbd_negotiate_send_rep_err(client->ioc, - NBD_REP_ERR_UNSUP, - option, errp, + ret =3D nbd_negotiate_send_rep_err(client, + NBD_REP_ERR_UNSUP, errp, "Unsupported option 0x%" PRIx32 " (%s)", option, nbd_opt_lookup(option)); @@ -808,7 +808,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, */ switch (option) { case NBD_OPT_EXPORT_NAME: - return nbd_negotiate_handle_export_name(client, length, + return nbd_negotiate_handle_export_name(client, myflags, no_zeroes, errp); =20 --=20 2.11.1 From nobody Sat May 4 12:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511346147325583.1875968015785; Wed, 22 Nov 2017 02:22:27 -0800 (PST) Received: from localhost ([::1]:38693 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSAX-0002VH-7p for importer@patchew.org; Wed, 22 Nov 2017 05:22:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33927) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8Z-0001N2-4j for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004lw-3y for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:11 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:28748 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHS8T-0004jy-HI for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:05 -0500 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id vAMAJwhH016254; Wed, 22 Nov 2017 13:19:59 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:55 +0300 Message-Id: <20171122101958.17065-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 2/5] nbd/server: add nbd_opt_{read, drop} to track client->optlen 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: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/server.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index bccc0274e7..c9445a89e9 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -139,6 +139,19 @@ static void nbd_client_receive_next_request(NBDClient = *client); =20 */ =20 +static inline int nbd_opt_read(NBDClient *client, void *buffer, size_t siz= e, + Error **errp) +{ + client->optlen -=3D size; + return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EI= O : 0; +} + +static inline int nbd_opt_drop(NBDClient *client, size_t size, Error **err= p) +{ + client->optlen -=3D size; + return nbd_drop(client->ioc, size, errp); +} + /* Send a reply header, including length, but no payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type, @@ -299,7 +312,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, error_setg(errp, "Bad length received"); return -EINVAL; } - if (nbd_read(client->ioc, name, client->optlen, errp) < 0) { + if (nbd_opt_read(client, name, client->optlen, errp) < 0) { error_prepend(errp, "read failed: "); return -EINVAL; } @@ -383,40 +396,36 @@ static int nbd_negotiate_handle_info(NBDClient *clien= t, uint16_t myflags, msg =3D "overall request too short"; goto invalid; } - if (nbd_read(client->ioc, &namelen, sizeof(namelen), errp) < 0) { + if (nbd_opt_read(client, &namelen, sizeof(namelen), errp) < 0) { return -EIO; } be32_to_cpus(&namelen); - client->optlen -=3D sizeof(namelen); if (namelen > client->optlen - sizeof(requests) || (client->optlen - namelen) % 2) { msg =3D "name length is incorrect"; goto invalid; } - if (nbd_read(client->ioc, name, namelen, errp) < 0) { + if (nbd_opt_read(client, name, namelen, errp) < 0) { return -EIO; } name[namelen] =3D '\0'; - client->optlen -=3D namelen; trace_nbd_negotiate_handle_export_name_request(name); =20 - if (nbd_read(client->ioc, &requests, sizeof(requests), errp) < 0) { + if (nbd_opt_read(client, &requests, sizeof(requests), errp) < 0) { return -EIO; } be16_to_cpus(&requests); - client->optlen -=3D sizeof(requests); trace_nbd_negotiate_handle_info_requests(requests); if (requests !=3D client->optlen / sizeof(request)) { msg =3D "incorrect number of requests for overall length"; goto invalid; } while (requests--) { - if (nbd_read(client->ioc, &request, sizeof(request), errp) < 0) { + if (nbd_opt_read(client, &request, sizeof(request), errp) < 0) { return -EIO; } be16_to_cpus(&request); - client->optlen -=3D sizeof(request); trace_nbd_negotiate_handle_info_request(request, nbd_info_lookup(request)); /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE; @@ -521,7 +530,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, return rc; =20 invalid: - if (nbd_drop(client->ioc, client->optlen, errp) < 0) { + if (nbd_opt_drop(client, client->optlen, errp) < 0) { return -EIO; } return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, @@ -715,7 +724,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, return -EINVAL; =20 default: - if (nbd_drop(client->ioc, length, errp) < 0) { + if (nbd_opt_drop(client, length, errp) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client, @@ -791,7 +800,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, break; =20 default: - if (nbd_drop(client->ioc, length, errp) < 0) { + if (nbd_opt_drop(client, length, errp) < 0) { return -EIO; } ret =3D nbd_negotiate_send_rep_err(client, @@ -821,6 +830,7 @@ static int nbd_negotiate_options(NBDClient *client, uin= t16_t myflags, if (ret < 0) { return ret; } + assert(client->optlen =3D=3D 0); } } =20 --=20 2.11.1 From nobody Sat May 4 12:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511346303524576.8075080811625; Wed, 22 Nov 2017 02:25:03 -0800 (PST) Received: from localhost ([::1]:38706 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSDC-0004vb-Cy for importer@patchew.org; Wed, 22 Nov 2017 05:24:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8Z-0001N6-6w for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004mI-7q for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:11 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:36975 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHS8T-0004k1-MR for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:06 -0500 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id vAMAJwhI016254; Wed, 22 Nov 2017 13:19:59 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:56 +0300 Message-Id: <20171122101958.17065-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 3/5] nbd/server: add helper nbd_opt_invalid 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: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/server.c | 74 +++++++++++++++++++++++++++++++++++++-------------------= ---- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index c9445a89e9..79b937d88f 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -199,20 +199,15 @@ static int nbd_negotiate_send_rep(NBDClient *client, = uint32_t type, return nbd_negotiate_send_rep_len(client, type, 0, errp); } =20 -/* Send an error reply. - * Return -errno on error, 0 on success. */ -static int GCC_FMT_ATTR(4, 5) -nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, - Error **errp, const char *fmt, ...) +static int GCC_FMT_ATTR(3, 0) +nbd_negotiate_send_rep_err_v(NBDClient *client, uint32_t type, + const char *fmt, va_list va, Error **errp) { - va_list va; char *msg; int ret; size_t len; =20 - va_start(va, fmt); msg =3D g_strdup_vprintf(fmt, va); - va_end(va); len =3D strlen(msg); assert(len < 4096); trace_nbd_negotiate_send_rep_err(msg); @@ -232,6 +227,43 @@ out: return ret; } =20 +static int GCC_FMT_ATTR(4, 5) +nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type, + Error **errp, const char *fmt, ...) +{ + va_list va; + int ret; + + va_start(va, fmt); + ret =3D nbd_negotiate_send_rep_err_v(client, type, fmt, va, errp); + va_end(va); + + return ret; +} + +/* nbd_opt_invalid + * Drop reminded option data and reply with NBD_REP_ERR_INVALID + */ +static int nbd_opt_invalid(NBDClient *client, + Error **errp, const char *fmt, ...) +{ + int ret; + va_list va; + + if (client->optlen > 0) { + if (nbd_opt_drop(client, client->optlen, errp) < 0) { + return -EIO; + } + } + + va_start(va, fmt); + ret =3D nbd_negotiate_send_rep_err_v(client, NBD_REP_ERR_INVALID, + fmt, va, errp); + va_end(va); + + return ret; +} + /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, @@ -384,7 +416,6 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, bool blocksize =3D false; uint32_t sizes[3]; char buf[sizeof(uint64_t) + sizeof(uint16_t)]; - const char *msg; =20 /* Client sends: 4 bytes: L, name length (can be 0) @@ -393,8 +424,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, N * 2 bytes: N requests */ if (client->optlen < sizeof(namelen) + sizeof(requests)) { - msg =3D "overall request too short"; - goto invalid; + return nbd_opt_invalid(client, errp, "overall request too short"); } if (nbd_opt_read(client, &namelen, sizeof(namelen), errp) < 0) { return -EIO; @@ -403,8 +433,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, if (namelen > client->optlen - sizeof(requests) || (client->optlen - namelen) % 2) { - msg =3D "name length is incorrect"; - goto invalid; + return nbd_opt_invalid(client, errp, "name length is incorrect"); } if (nbd_opt_read(client, name, namelen, errp) < 0) { return -EIO; @@ -418,8 +447,8 @@ static int nbd_negotiate_handle_info(NBDClient *client,= uint16_t myflags, be16_to_cpus(&requests); trace_nbd_negotiate_handle_info_requests(requests); if (requests !=3D client->optlen / sizeof(request)) { - msg =3D "incorrect number of requests for overall length"; - goto invalid; + return nbd_opt_invalid( + client, errp, "incorrect number of requests for overall length= "); } while (requests--) { if (nbd_opt_read(client, &request, sizeof(request), errp) < 0) { @@ -528,13 +557,6 @@ static int nbd_negotiate_handle_info(NBDClient *client= , uint16_t myflags, rc =3D 1; } return rc; - - invalid: - if (nbd_opt_drop(client, client->optlen, errp) < 0) { - return -EIO; - } - return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, - errp, "%s", msg); } =20 =20 @@ -597,12 +619,8 @@ static int nbd_reject_length(NBDClient *client, bool f= atal, Error **errp) int ret; =20 assert(client->optlen); - if (nbd_drop(client->ioc, client->optlen, errp) < 0) { - return -EIO; - } - ret =3D nbd_negotiate_send_rep_err(client, NBD_REP_ERR_INVALID, errp, - "option '%s' should have zero length", - nbd_opt_lookup(client->opt)); + ret =3D nbd_opt_invalid(client, errp, "option '%s' should have zero le= ngth", + nbd_opt_lookup(client->opt)); if (fatal && !ret) { error_setg(errp, "option '%s' should have zero length", nbd_opt_lookup(client->opt)); --=20 2.11.1 From nobody Sat May 4 12:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151134614449138.8516302313036; Wed, 22 Nov 2017 02:22:24 -0800 (PST) Received: from localhost ([::1]:38692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSAQ-0002RB-4L for importer@patchew.org; Wed, 22 Nov 2017 05:22:06 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33932) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8Z-0001N8-8f for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004lq-31 for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:11 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:2285 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHS8T-0004k3-HE for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:05 -0500 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id vAMAJwhJ016254; Wed, 22 Nov 2017 13:20:00 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:57 +0300 Message-Id: <20171122101958.17065-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 4/5] nbd: rename nbd_option and nbd_opt_reply 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: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rename nbd_optino and nbd_opt_reply to NBDOption and NBDOptionReply to correspond to Qemu coding style and other structures here. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- include/block/nbd.h | 8 ++++---- nbd/client.c | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 113c707a5e..978e443366 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -28,20 +28,20 @@ =20 /* Handshake phase structs - this struct is passed on the wire */ =20 -struct nbd_option { +struct NBDOption { uint64_t magic; /* NBD_OPTS_MAGIC */ uint32_t option; /* NBD_OPT_* */ uint32_t length; } QEMU_PACKED; -typedef struct nbd_option nbd_option; +typedef struct NBDOption NBDOption; =20 -struct nbd_opt_reply { +struct NBDOptionReply { uint64_t magic; /* NBD_REP_MAGIC */ uint32_t option; /* NBD_OPT_* */ uint32_t type; /* NBD_REP_* */ uint32_t length; } QEMU_PACKED; -typedef struct nbd_opt_reply nbd_opt_reply; +typedef struct NBDOptionReply NBDOptionReply; =20 /* Transmission phase structs * diff --git a/nbd/client.c b/nbd/client.c index eea236ca06..89f80f9590 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -66,7 +66,7 @@ static int nbd_send_option_request(QIOChannel *ioc, uint3= 2_t opt, uint32_t len, const char *data, Error **errp) { - nbd_option req; + NBDOption req; QEMU_BUILD_BUG_ON(sizeof(req) !=3D 16); =20 if (len =3D=3D -1) { @@ -109,7 +109,7 @@ static void nbd_send_opt_abort(QIOChannel *ioc) * payload. Return 0 if successful, -1 with errp set if it is * impossible to continue. */ static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt, - nbd_opt_reply *reply, Error **errp) + NBDOptionReply *reply, Error **errp) { QEMU_BUILD_BUG_ON(sizeof(*reply) !=3D 20); if (nbd_read(ioc, reply, sizeof(*reply), errp) < 0) { @@ -146,7 +146,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc, ui= nt32_t opt, * can fall back to other approaches), or -1 with errp set for other * errors. */ -static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply, +static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply, Error **errp) { char *msg =3D NULL; @@ -239,7 +239,7 @@ static int nbd_handle_reply_err(QIOChannel *ioc, nbd_op= t_reply *reply, static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match, Error **errp) { - nbd_opt_reply reply; + NBDOptionReply reply; uint32_t len; uint32_t namelen; char name[NBD_MAX_NAME_SIZE + 1]; @@ -325,7 +325,7 @@ static int nbd_receive_list(QIOChannel *ioc, const char= *want, bool *match, static int nbd_opt_go(QIOChannel *ioc, const char *wantname, NBDExportInfo *info, Error **errp) { - nbd_opt_reply reply; + NBDOptionReply reply; uint32_t len =3D strlen(wantname); uint16_t type; int error; @@ -517,7 +517,7 @@ static int nbd_receive_query_exports(QIOChannel *ioc, */ static int nbd_request_simple_option(QIOChannel *ioc, int opt, Error **err= p) { - nbd_opt_reply reply; + NBDOptionReply reply; int error; =20 if (nbd_send_option_request(ioc, opt, 0, NULL, errp) < 0) { --=20 2.11.1 From nobody Sat May 4 12:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151134623832393.13806773197189; Wed, 22 Nov 2017 02:23:58 -0800 (PST) Received: from localhost ([::1]:38703 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHSC5-0004JK-K8 for importer@patchew.org; Wed, 22 Nov 2017 05:23:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33967) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHS8c-0001Ny-57 for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHS8U-0004mD-7p for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:14 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:19090 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHS8T-0004k0-MW for qemu-devel@nongnu.org; Wed, 22 Nov 2017 05:20:06 -0500 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id vAMAJwhK016254; Wed, 22 Nov 2017 13:20:00 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 22 Nov 2017 13:19:58 +0300 Message-Id: <20171122101958.17065-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20171122101958.17065-1-vsementsov@virtuozzo.com> References: <20171122101958.17065-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 5/5] nbd/server: structurize option reply sending 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: kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/server.c | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 79b937d88f..975fe8efe9 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -152,43 +152,29 @@ static inline int nbd_opt_drop(NBDClient *client, siz= e_t size, Error **errp) return nbd_drop(client->ioc, size, errp); } =20 +static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option, + uint32_t type, uint32_t length) +{ + stq_be_p(&rep->magic, NBD_REP_MAGIC); + stl_be_p(&rep->option, option); + stl_be_p(&rep->type, type); + stl_be_p(&rep->length, length); +} + /* Send a reply header, including length, but no payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type, uint32_t len, Error **errp) { - uint64_t magic; - QIOChannel *ioc =3D client->ioc; - uint32_t opt =3D client->opt; + NBDOptionReply rep; =20 - trace_nbd_negotiate_send_rep_len(opt, nbd_opt_lookup(opt), + trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->o= pt), type, nbd_rep_lookup(type), len); =20 assert(len < NBD_MAX_BUFFER_SIZE); - magic =3D cpu_to_be64(NBD_REP_MAGIC); - if (nbd_write(ioc, &magic, sizeof(magic), errp) < 0) { - error_prepend(errp, "write failed (rep magic): "); - return -EINVAL; - } - - opt =3D cpu_to_be32(opt); - if (nbd_write(ioc, &opt, sizeof(opt), errp) < 0) { - error_prepend(errp, "write failed (rep opt): "); - return -EINVAL; - } - - type =3D cpu_to_be32(type); - if (nbd_write(ioc, &type, sizeof(type), errp) < 0) { - error_prepend(errp, "write failed (rep type): "); - return -EINVAL; - } =20 - len =3D cpu_to_be32(len); - if (nbd_write(ioc, &len, sizeof(len), errp) < 0) { - error_prepend(errp, "write failed (rep data length): "); - return -EINVAL; - } - return 0; + set_be_option_rep(&rep, client->opt, type, len); + return nbd_write(client->ioc, &rep, sizeof(rep), errp); } =20 /* Send a reply header with default 0 length. --=20 2.11.1